dmesg.c revision 1.17 1 /* $NetBSD: dmesg.c,v 1.17 2000/06/16 00:20:23 simonb 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.17 2000/06/16 00:20:23 simonb Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/msgbuf.h>
51 #include <sys/sysctl.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 <stddef.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63 #include <unistd.h>
64 #include <vis.h>
65
66 struct nlist nl[] = {
67 #define X_MSGBUF 0
68 { "_msgbufp" },
69 { NULL },
70 };
71
72 int main __P((int, char *[]));
73 void usage __P((void));
74
75 #define KREAD(addr, var) \
76 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
77
78 int
79 main(argc, argv)
80 int argc;
81 char *argv[];
82 {
83 struct kern_msgbuf cur;
84 int ch, newl, skip, i;
85 char *p, *memf, *nlistf, *bufdata;
86 char buf[5];
87
88 memf = nlistf = NULL;
89 while ((ch = getopt(argc, argv, "M:N:")) != -1)
90 switch(ch) {
91 case 'M':
92 memf = optarg;
93 break;
94 case 'N':
95 nlistf = optarg;
96 break;
97 case '?':
98 default:
99 usage();
100 }
101 argc -= optind;
102 argv += optind;
103
104 if (memf == NULL && nlistf == NULL) {
105 size_t size;
106 int mib[2];
107
108 mib[0] = CTL_KERN;
109 mib[1] = KERN_MSGBUF;
110
111 if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1)
112 errx(1, "can't get size of msgbuf");
113
114 if ((bufdata = malloc(size)) == NULL)
115 err(1, "couldn't allocate space for buffer data");
116
117 if (sysctl(mib, 2, bufdata, &size, NULL, 0) == -1)
118 err(1, "can't get msgbuf");
119
120 /* make a dummy struct msgbuf for the display logic */
121 cur.msg_bufx = cur.msg_bufs = size;
122 } else {
123 kvm_t *kd;
124 struct kern_msgbuf *bufp;
125
126 /*
127 * Read in message buffer header and data, and do sanity
128 * checks.
129 */
130 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
131 if (kd == NULL)
132 exit (1);
133 if (kvm_nlist(kd, nl) == -1)
134 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
135 if (nl[X_MSGBUF].n_type == 0)
136 errx(1, "%s: msgbufp not found", nlistf ? nlistf :
137 "namelist");
138 if (KREAD(nl[X_MSGBUF].n_value, bufp))
139 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
140 nl[X_MSGBUF].n_value);
141 if (kvm_read(kd, (long)bufp, &cur,
142 offsetof(struct kern_msgbuf, msg_bufc)) !=
143 offsetof(struct kern_msgbuf, msg_bufc))
144 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
145 (unsigned long)bufp);
146 if (cur.msg_magic != MSG_MAGIC)
147 errx(1, "magic number incorrect");
148 bufdata = malloc(cur.msg_bufs);
149 if (bufdata == NULL)
150 errx(1, "couldn't allocate space for buffer data");
151 if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
152 cur.msg_bufs) != cur.msg_bufs)
153 errx(1, "kvm_read: %s", kvm_geterr(kd));
154 kvm_close(kd);
155 if (cur.msg_bufx >= cur.msg_bufs)
156 cur.msg_bufx = 0;
157 }
158
159 /*
160 * The message buffer is circular; start at the write pointer
161 * (which points the oldest character), and go to the write
162 * pointer - 1 (which points the newest character). I.e, loop
163 * over cur.msg_bufs times. Unused area is skipped since it
164 * contains nul.
165 */
166 for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
167 i < cur.msg_bufs; i++, p++) {
168 if (p == bufdata + cur.msg_bufs)
169 p = bufdata;
170 ch = *p;
171 /* Skip "\n<.*>" syslog sequences. */
172 if (skip) {
173 if (ch == '>')
174 newl = skip = 0;
175 continue;
176 }
177 if (newl && ch == '<') {
178 skip = 1;
179 continue;
180 }
181 if (ch == '\0')
182 continue;
183 newl = ch == '\n';
184 (void)vis(buf, ch, 0, 0);
185 if (buf[1] == 0)
186 (void)putchar(buf[0]);
187 else
188 (void)printf("%s", buf);
189 }
190 if (!newl)
191 (void)putchar('\n');
192 exit(0);
193 }
194
195 void
196 usage()
197 {
198
199 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
200 exit(1);
201 }
202