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