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