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