dmesg.c revision 1.11 1 /* $NetBSD: dmesg.c,v 1.11 1997/09/14 08:53:46 lukem 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 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: dmesg.c,v 1.11 1997/09/14 08:53:46 lukem 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 int main __P((int, char *[]));
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 int ch, newl, skip;
81 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:")) != -1)
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))
118 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
119 nl[X_MSGBUF].n_value);
120 if (KREAD((long)bufp, cur))
121 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
122 (unsigned long)bufp);
123 kvm_close(kd);
124 if (cur.msg_magic != MSG_MAGIC)
125 errx(1, "magic number incorrect");
126 if (cur.msg_bufx >= MSG_BSIZE)
127 cur.msg_bufx = 0;
128
129 /*
130 * The message buffer is circular; start at the read pointer, and
131 * go to the write pointer - 1.
132 */
133 p = ep = cur.msg_bufc + (cur.msg_bufx - 1 + MSG_BSIZE) % MSG_BSIZE;
134 newl = skip = 0;
135 do {
136 if (++p == cur.msg_bufc + MSG_BSIZE)
137 p = cur.msg_bufc;
138 ch = *p;
139 /* Skip "\n<.*>" syslog sequences. */
140 if (skip) {
141 if (ch == '>')
142 newl = skip = 0;
143 continue;
144 }
145 if (newl && ch == '<') {
146 skip = 1;
147 continue;
148 }
149 if (ch == '\0')
150 continue;
151 newl = ch == '\n';
152 (void)vis(buf, ch, 0, 0);
153 if (buf[1] == 0)
154 (void)putchar(buf[0]);
155 else
156 (void)printf("%s", buf);
157 } while (p != ep);
158 if (!newl)
159 (void)putchar('\n');
160 exit(0);
161 }
162
163 void
164 usage()
165 {
166 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
167 exit(1);
168 }
169