dmesg.c revision 1.23 1 /* $NetBSD: dmesg.c,v 1.23 2005/01/20 15:55:01 xtraeme 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
34 The Regents of the University of California. All rights reserved.\n");
35 #endif /* not lint */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93";
40 #else
41 __RCSID("$NetBSD: dmesg.c,v 1.23 2005/01/20 15:55:01 xtraeme Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/msgbuf.h>
47 #include <sys/sysctl.h>
48
49 #include <err.h>
50 #include <fcntl.h>
51 #include <kvm.h>
52 #include <nlist.h>
53 #include <stdio.h>
54 #include <stddef.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <vis.h>
58
59 #ifndef SMALL
60 struct nlist nl[] = {
61 #define X_MSGBUF 0
62 { "_msgbufp" },
63 { NULL },
64 };
65 #endif
66
67 void usage(void);
68
69 #define KREAD(addr, var) \
70 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
71
72 int
73 main(int argc, char *argv[])
74 {
75 struct kern_msgbuf cur;
76 int ch, newl, skip, i;
77 char *p, *bufdata;
78 #ifndef SMALL
79 char *memf, *nlistf;
80 #endif
81 char buf[5];
82
83 #ifndef SMALL
84 memf = nlistf = NULL;
85 while ((ch = getopt(argc, argv, "M:N:")) != -1)
86 switch(ch) {
87 case 'M':
88 memf = optarg;
89 break;
90 case 'N':
91 nlistf = optarg;
92 break;
93 case '?':
94 default:
95 usage();
96 }
97 argc -= optind;
98 argv += optind;
99
100 if (memf == NULL) {
101 #endif
102 size_t size;
103 int mib[2];
104
105 mib[0] = CTL_KERN;
106 mib[1] = KERN_MSGBUF;
107
108 if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1)
109 errx(1, "can't get size of msgbuf");
110
111 if ((bufdata = malloc(size)) == NULL)
112 err(1, "couldn't allocate space for buffer data");
113
114 if (sysctl(mib, 2, bufdata, &size, NULL, 0) == -1)
115 err(1, "can't get msgbuf");
116
117 /* make a dummy struct msgbuf for the display logic */
118 cur.msg_bufx = cur.msg_bufs = size;
119 #ifndef SMALL
120 } else {
121 kvm_t *kd;
122 struct kern_msgbuf *bufp;
123
124 /*
125 * Read in message buffer header and data, and do sanity
126 * checks.
127 */
128 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
129 if (kd == NULL)
130 exit (1);
131 if (kvm_nlist(kd, nl) == -1)
132 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
133 if (nl[X_MSGBUF].n_type == 0)
134 errx(1, "%s: msgbufp not found", nlistf ? nlistf :
135 "namelist");
136 if (KREAD(nl[X_MSGBUF].n_value, bufp))
137 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
138 nl[X_MSGBUF].n_value);
139 if (kvm_read(kd, (long)bufp, &cur,
140 offsetof(struct kern_msgbuf, msg_bufc)) !=
141 offsetof(struct kern_msgbuf, msg_bufc))
142 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
143 (unsigned long)bufp);
144 if (cur.msg_magic != MSG_MAGIC)
145 errx(1, "magic number incorrect");
146 bufdata = malloc(cur.msg_bufs);
147 if (bufdata == NULL)
148 errx(1, "couldn't allocate space for buffer data");
149 if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
150 cur.msg_bufs) != cur.msg_bufs)
151 errx(1, "kvm_read: %s", kvm_geterr(kd));
152 kvm_close(kd);
153 if (cur.msg_bufx >= cur.msg_bufs)
154 cur.msg_bufx = 0;
155 }
156 #endif
157
158 /*
159 * The message buffer is circular; start at the write pointer
160 * (which points the oldest character), and go to the write
161 * pointer - 1 (which points the newest character). I.e, loop
162 * over cur.msg_bufs times. Unused area is skipped since it
163 * contains nul.
164 */
165 for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
166 i < cur.msg_bufs; i++, p++) {
167 if (p == bufdata + cur.msg_bufs)
168 p = bufdata;
169 ch = *p;
170 /* Skip "\n<.*>" syslog sequences. */
171 if (skip) {
172 if (ch == '>')
173 newl = skip = 0;
174 continue;
175 }
176 if (newl && ch == '<') {
177 skip = 1;
178 continue;
179 }
180 if (ch == '\0')
181 continue;
182 newl = ch == '\n';
183 (void)vis(buf, ch, VIS_NOSLASH, 0);
184 if (buf[1] == 0)
185 (void)putchar(buf[0]);
186 else
187 (void)printf("%s", buf);
188 }
189 if (!newl)
190 (void)putchar('\n');
191 exit(0);
192 }
193
194 void
195 usage(void)
196 {
197
198 #ifdef SMALL
199 (void)fprintf(stderr, "usage: dmesg\n");
200 #else
201 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
202 #endif
203 exit(1);
204 }
205