xref: /freebsd/sbin/dmesg/dmesg.c (revision 8dcf55a40e13e7288fb3484416592d9cebc4a35f)
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 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 static char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
42 #endif /* not lint */
43 
44 #include <sys/cdefs.h>
45 #include <sys/msgbuf.h>
46 
47 #include <fcntl.h>
48 #include <kvm.h>
49 #include <limits.h>
50 #include <nlist.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <time.h>
54 #include <unistd.h>
55 #include <vis.h>
56 #include <locale.h>
57 
58 struct nlist nl[] = {
59 #define	X_MSGBUF	0
60 	{ "_msgbufp" },
61 	{ NULL },
62 };
63 
64 void usage __P((void));
65 
66 #define	KREAD(addr, var) \
67 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char *argv[];
73 {
74 	register int ch, newl, skip;
75 	register char *p, *ep;
76 	struct msgbuf *bufp, cur;
77 	char *memf, *nlistf;
78 	kvm_t *kd;
79 	char buf[5];
80 
81 	(void) setlocale(LC_CTYPE, "");
82 	memf = nlistf = NULL;
83 	while ((ch = getopt(argc, argv, "M:N:")) != EOF)
84 		switch(ch) {
85 		case 'M':
86 			memf = optarg;
87 			break;
88 		case 'N':
89 			nlistf = optarg;
90 			break;
91 		case '?':
92 		default:
93 			usage();
94 		}
95 	argc -= optind;
96 	argv += optind;
97 
98 	/*
99 	 * Discard setgid privileges if not the running kernel so that bad
100 	 * guys can't print interesting stuff from kernel memory.
101 	 */
102 	if (memf != NULL || nlistf != NULL)
103 		setgid(getgid());
104 
105 	/* Read in kernel message buffer, do sanity checks. */
106 	if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL)
107 		exit (1);
108 	if (kvm_nlist(kd, nl) == -1)
109 		errx(1, "kvm_nlist: %s", kvm_geterr(kd));
110 	if (nl[X_MSGBUF].n_type == 0)
111 		errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist");
112 	if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
113 		errx(1, "kvm_read: %s", kvm_geterr(kd));
114 	kvm_close(kd);
115 	if (cur.msg_magic != MSG_MAGIC)
116 		errx(1, "magic number incorrect");
117 	if (cur.msg_bufx >= MSG_BSIZE)
118 		cur.msg_bufx = 0;
119 
120 	/*
121 	 * The message buffer is circular; start at the read pointer, and
122 	 * go to the write pointer - 1.
123 	 */
124 	p = cur.msg_bufc + cur.msg_bufx;
125 	ep = cur.msg_bufc + cur.msg_bufx - 1;
126 	for (newl = skip = 0; p != ep; ++p) {
127 		if (p == cur.msg_bufc + MSG_BSIZE)
128 			p = cur.msg_bufc;
129 		ch = *p;
130 		/* Skip "\n<.*>" syslog sequences. */
131 		if (skip) {
132 			if (ch == '>')
133 				newl = skip = 0;
134 			continue;
135 		}
136 		if (newl && ch == '<') {
137 			skip = 1;
138 			continue;
139 		}
140 		if (ch == '\0')
141 			continue;
142 		newl = ch == '\n';
143 		(void)vis(buf, ch, 0, 0);
144 		if (buf[1] == 0)
145 			(void)putchar(buf[0]);
146 		else
147 			(void)printf("%s", buf);
148 	}
149 	if (!newl)
150 		(void)putchar('\n');
151 	exit(0);
152 }
153 
154 void
155 usage()
156 {
157 	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
158 	exit(1);
159 }
160