xref: /openbsd-src/sbin/dmesg/dmesg.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: dmesg.c,v 1.22 2010/07/02 22:02:06 deraadt Exp $	*/
2 /*	$NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/msgbuf.h>
35 #include <sys/sysctl.h>
36 
37 #include <err.h>
38 #include <fcntl.h>
39 #include <kvm.h>
40 #include <limits.h>
41 #include <nlist.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <vis.h>
48 
49 #ifndef NOKVM
50 struct nlist nl[] = {
51 #define	X_MSGBUF	0
52 	{ "_msgbufp" },
53 	{ NULL },
54 };
55 #endif
56 
57 void usage(void);
58 
59 #define	KREAD(addr, var) \
60 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	int ch, newl, skip, i;
66 	char *p;
67 	struct msgbuf cur;
68 	char *memf, *nlistf, *bufdata = NULL;
69 	char buf[5];
70 
71 	memf = nlistf = NULL;
72 	while ((ch = getopt(argc, argv, "M:N:")) != -1)
73 		switch(ch) {
74 		case 'M':
75 			memf = optarg;
76 			break;
77 		case 'N':
78 			nlistf = optarg;
79 			break;
80 		case '?':
81 		default:
82 			usage();
83 		}
84 	argc -= optind;
85 	argv += optind;
86 
87 	if (memf == NULL && nlistf == NULL) {
88 		int mib[2], msgbufsize;
89 		size_t len;
90 
91 		mib[0] = CTL_KERN;
92 		mib[1] = KERN_MSGBUFSIZE;
93 		len = sizeof(msgbufsize);
94 		if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0))
95 			err(1, "sysctl: KERN_MSGBUFSIZE");
96 
97 		msgbufsize += sizeof(struct msgbuf) - 1;
98 		bufdata = malloc(msgbufsize);
99 		if (bufdata == NULL)
100 			errx(1, "couldn't allocate space for buffer data");
101 
102 		memset(bufdata, 0, msgbufsize);
103 		mib[1] = KERN_MSGBUF;
104 		len = msgbufsize;
105 		if (sysctl(mib, 2, bufdata, &len, NULL, 0))
106 			err(1, "sysctl: KERN_MSGBUF");
107 
108 		memcpy(&cur, bufdata, sizeof(cur));
109 		bufdata = ((struct msgbuf *)bufdata)->msg_bufc;
110 	} else {
111 #ifndef NOKVM
112 		struct msgbuf *bufp;
113 		kvm_t *kd;
114 
115 		/* Read in kernel message buffer, do sanity checks. */
116 		if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY,
117 		    "dmesg")) == NULL)
118 			return (1);
119 
120 		if (kvm_nlist(kd, nl) == -1)
121 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
122 		if (nl[X_MSGBUF].n_type == 0)
123 			errx(1, "%s: msgbufp not found",
124 			    nlistf ? nlistf : "namelist");
125 		if (KREAD(nl[X_MSGBUF].n_value, bufp))
126 			errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd),
127 			    nl[X_MSGBUF].n_value);
128 		if (KREAD((long)bufp, cur))
129 			errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd),
130 			    (unsigned long)bufp);
131 		if (cur.msg_magic != MSG_MAGIC)
132 			errx(1, "magic number incorrect");
133 		bufdata = malloc(cur.msg_bufs);
134 		if (bufdata == NULL)
135 			errx(1, "couldn't allocate space for buffer data");
136 		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
137 		    cur.msg_bufs) != cur.msg_bufs)
138 			errx(1, "kvm_read: %s", kvm_geterr(kd));
139 		kvm_close(kd);
140 #endif
141 	}
142 
143 	if (cur.msg_bufx >= cur.msg_bufs)
144 		cur.msg_bufx = 0;
145 	/*
146 	 * The message buffer is circular; start at the read pointer, and
147 	 * go to the write pointer - 1.
148 	 */
149 	for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
150 	    i < cur.msg_bufs; i++, p++) {
151 		if (p == bufdata + cur.msg_bufs)
152 			p = bufdata;
153 		ch = *p;
154 		/* Skip "\n<.*>" syslog sequences. */
155 		if (skip) {
156 			if (ch == '>')
157 				newl = skip = 0;
158 			continue;
159 		}
160 		if (newl && ch == '<') {
161 			skip = 1;
162 			continue;
163 		}
164 		if (ch == '\0')
165 			continue;
166 		newl = ch == '\n';
167 		vis(buf, ch, 0, 0);
168 		if (buf[1] == 0)
169 			putchar(buf[0]);
170 		else
171 			printf("%s", buf);
172 	}
173 	if (!newl)
174 		putchar('\n');
175 	return (0);
176 }
177 
178 void
179 usage(void)
180 {
181 	extern char *__progname;
182 
183 	fprintf(stderr, "usage: %s [-M core] [-N system]\n", __progname);
184 	exit(1);
185 }
186