xref: /freebsd/sbin/dmesg/dmesg.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static const char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/msgbuf.h>
49 
50 #include <err.h>
51 #include <fcntl.h>
52 #include <kvm.h>
53 #include <locale.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <vis.h>
58 
59 struct nlist nl[] = {
60 #define	X_MSGBUF	0
61 	{ "_msgbufp" },
62 	{ NULL },
63 };
64 
65 void usage __P((void));
66 
67 #define	KREAD(addr, var) \
68 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char *argv[];
74 {
75 	register int ch, newl, skip;
76 	register char *p, *ep;
77 	struct msgbuf *bufp, cur;
78 	char *bp, *memf, *nlistf;
79 	kvm_t *kd;
80 	char buf[5];
81 
82 	(void) setlocale(LC_CTYPE, "");
83 	memf = nlistf = NULL;
84 	while ((ch = getopt(argc, argv, "M:N:")) != -1)
85 		switch(ch) {
86 		case 'M':
87 			memf = optarg;
88 			break;
89 		case 'N':
90 			nlistf = optarg;
91 			break;
92 		case '?':
93 		default:
94 			usage();
95 		}
96 	argc -= optind;
97 	argv += optind;
98 
99 	/*
100 	 * Discard setgid privileges if not the running kernel so that bad
101 	 * guys can't print interesting stuff from kernel memory.
102 	 */
103 	if (memf != NULL || nlistf != NULL)
104 		setgid(getgid());
105 
106 	/* Read in kernel message buffer, do sanity checks. */
107 	if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL)
108 		exit (1);
109 	if (kvm_nlist(kd, nl) == -1)
110 		errx(1, "kvm_nlist: %s", kvm_geterr(kd));
111 	if (nl[X_MSGBUF].n_type == 0)
112 		errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist");
113 	if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
114 		errx(1, "kvm_read: %s", kvm_geterr(kd));
115 	if (cur.msg_magic != MSG_MAGIC)
116 		errx(1, "kernel message buffer has different magic number");
117 	bp = malloc(cur.msg_size);
118 	if (!bp)
119 		errx(1, "malloc failed");
120 	if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
121 	    cur.msg_size)
122 		errx(1, "kvm_read: %s", kvm_geterr(kd));
123 	if (cur.msg_bufx >= cur.msg_size)
124 		cur.msg_bufx = 0;
125 	kvm_close(kd);
126 
127 	/*
128 	 * The message buffer is circular.  If the buffer has wrapped, the
129 	 * write pointer points to the oldest data.  Otherwise, the write
130 	 * pointer points to \0's following the data.  Read the entire
131 	 * buffer starting at the write pointer and ignore nulls so that
132 	 * we effectively start at the oldest data.
133 	 */
134 	p = bp + cur.msg_bufx;
135 	ep = (cur.msg_bufx == 0 ? bp + cur.msg_size : p);
136 	newl = skip = 0;
137 	do {
138 		if (p == bp + cur.msg_size)
139 			p = bp;
140 		ch = *p;
141 		/* Skip "\n<.*>" syslog sequences. */
142 		if (skip) {
143 			if (ch == '>')
144 				newl = skip = 0;
145 			continue;
146 		}
147 		if (newl && ch == '<') {
148 			skip = 1;
149 			continue;
150 		}
151 		if (ch == '\0')
152 			continue;
153 		newl = ch == '\n';
154 		(void)vis(buf, ch, 0, 0);
155 		if (buf[1] == 0)
156 			(void)putchar(buf[0]);
157 		else
158 			(void)printf("%s", buf);
159 	} while (++p != ep);
160 	if (!newl)
161 		(void)putchar('\n');
162 	exit(0);
163 }
164 
165 void
166 usage()
167 {
168 	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
169 	exit(1);
170 }
171