Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sbin / dmesg / dmesg.c
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 const 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 #if 0
42 static const char sccsid[] = "@(#)dmesg.c       8.1 (Berkeley) 6/5/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/sbin/dmesg/dmesg.c,v 1.11.2.3 2001/08/08 22:32:15 obrien Exp $";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49 #include <sys/msgbuf.h>
50 #include <sys/sysctl.h>
51
52 #include <err.h>
53 #include <fcntl.h>
54 #include <kvm.h>
55 #include <locale.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <vis.h>
60 #include <sys/syslog.h>
61
62 struct nlist nl[] = {
63 #define X_MSGBUF        0
64         { "_msgbufp" },
65         { NULL },
66 };
67
68 void usage __P((void));
69
70 #define KREAD(addr, var) \
71         kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
72
73 int
74 main(argc, argv)
75         int argc;
76         char *argv[];
77 {
78         int ch, newl, skip;
79         char *p, *ep;
80         struct msgbuf *bufp, cur;
81         char *bp, *memf, *nlistf;
82         kvm_t *kd;
83         char buf[5];
84         int all = 0;
85         int pri;
86         size_t buflen;
87         int bufpos;
88
89         (void) setlocale(LC_CTYPE, "");
90         memf = nlistf = NULL;
91         while ((ch = getopt(argc, argv, "aM:N:")) != -1)
92                 switch(ch) {
93                 case 'a':
94                         all++;
95                         break;
96                 case 'M':
97                         memf = optarg;
98                         break;
99                 case 'N':
100                         nlistf = optarg;
101                         break;
102                 case '?':
103                 default:
104                         usage();
105                 }
106         argc -= optind;
107         argv += optind;
108
109         if (memf == NULL && nlistf == NULL) {
110                 /* Running kernel. Use sysctl. */
111                 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
112                         err(1, "sysctl kern.msgbuf");
113                 if ((bp = malloc(buflen)) == NULL)
114                         errx(1, "malloc failed");
115                 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
116                         err(1, "sysctl kern.msgbuf");
117                 /* We get a dewrapped buffer using sysctl. */
118                 bufpos = 0;
119         } else {
120                 /* Read in kernel message buffer, do sanity checks. */
121                 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
122                 if (kd == NULL)
123                         exit (1);
124                 if (kvm_nlist(kd, nl) == -1)
125                         errx(1, "kvm_nlist: %s", kvm_geterr(kd));
126                 if (nl[X_MSGBUF].n_type == 0)
127                         errx(1, "%s: msgbufp not found",
128                             nlistf ? nlistf : "namelist");
129                 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
130                         errx(1, "kvm_read: %s", kvm_geterr(kd));
131                 if (cur.msg_magic != MSG_MAGIC)
132                         errx(1, "kernel message buffer has different magic "
133                             "number");
134                 bp = malloc(cur.msg_size);
135                 if (!bp)
136                         errx(1, "malloc failed");
137                 if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
138                     cur.msg_size)
139                         errx(1, "kvm_read: %s", kvm_geterr(kd));
140                 kvm_close(kd);
141                 buflen = cur.msg_size;
142                 bufpos = cur.msg_bufx;
143                 if (bufpos >= buflen)
144                         bufpos = 0;
145         }
146
147         /*
148          * The message buffer is circular.  If the buffer has wrapped, the
149          * write pointer points to the oldest data.  Otherwise, the write
150          * pointer points to \0's following the data.  Read the entire
151          * buffer starting at the write pointer and ignore nulls so that
152          * we effectively start at the oldest data.
153          */
154         p = bp + bufpos;
155         ep = (bufpos == 0 ? bp + buflen : p);
156         newl = skip = 0;
157         do {
158                 if (p == bp + buflen)
159                         p = bp;
160                 ch = *p;
161                 /* Skip "\n<.*>" syslog sequences. */
162                 if (skip) {
163                         if (ch == '\n') {
164                                 skip = 0;
165                                 newl = 1;
166                         } if (ch == '>') {
167                                 if (LOG_FAC(pri) == LOG_KERN || all)
168                                         newl = skip = 0;
169                         } else if (ch >= '0' && ch <= '9') {
170                                 pri *= 10;
171                                 pri += ch - '0';
172                         }
173                         continue;
174                 }
175                 if (newl && ch == '<') {
176                         pri = 0;
177                         skip = 1;
178                         continue;
179                 }
180                 if (ch == '\0')
181                         continue;
182                 newl = ch == '\n';
183                 (void)vis(buf, ch, 0, 0);
184                 if (buf[1] == 0)
185                         (void)putchar(buf[0]);
186                 else
187                         (void)printf("%s", buf);
188         } while (++p != ep);
189         if (!newl)
190                 (void)putchar('\n');
191         exit(0);
192 }
193
194 void
195 usage()
196 {
197         (void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n");
198         exit(1);
199 }