1b12d9cbf0b6cfce815624160c7c5011bb0d5d14
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)dmesg.c  8.1 (Berkeley) 6/5/93
31  * $FreeBSD: src/sbin/dmesg/dmesg.c,v 1.11.2.3 2001/08/08 22:32:15 obrien Exp $
32  */
33
34 #include <sys/types.h>
35 #include <sys/msgbuf.h>
36 #include <sys/sysctl.h>
37
38 #include <err.h>
39 #include <fcntl.h>
40 #include <kvm.h>
41 #include <locale.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <vis.h>
46 #include <sys/syslog.h>
47
48 struct nlist nl[] = {
49 #define X_MSGBUF        0
50         { "_msgbufp",   0, 0, 0, 0 },
51         { NULL,         0, 0, 0, 0 },
52 };
53
54 static void dumpbuf(char *bp, size_t bufpos, size_t buflen,
55                     int *newl, int *skip, int *pri);
56 void usage(void);
57
58 #define KREAD(addr, var) \
59         kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
60
61 #define INCRBUFSIZE     65536
62
63 int all_opt;
64
65 int
66 main(int argc, char **argv)
67 {
68         int newl, skip;
69         struct msgbuf *bufp, cur;
70         char *bp, *memf, *nlistf;
71         kvm_t *kd;
72         int ch;
73         int clear = 0;
74         int pri = 0;
75         int tailmode = 0;
76         int kno;
77         unsigned int rindex;
78         size_t buflen, bufpos;
79
80         setlocale(LC_CTYPE, "");
81         memf = nlistf = NULL;
82         while ((ch = getopt(argc, argv, "acfM:N:n:")) != -1) {
83                 switch(ch) {
84                 case 'a':
85                         all_opt++;
86                         break;
87                 case 'c':
88                         clear = 1;
89                         break;
90                 case 'f':
91                         ++tailmode;
92                         break;
93                 case 'M':
94                         memf = optarg;
95                         break;
96                 case 'N':
97                         nlistf = optarg;
98                         break;
99                 case 'n':
100                         kno = strtol(optarg, NULL, 0);
101                         asprintf(&memf, "/var/crash/vmcore.%d", kno);
102                         asprintf(&nlistf, "/var/crash/kern.%d", kno);
103                         break;
104                 case '?':
105                 default:
106                         usage();
107                 }
108         }
109         argc -= optind;
110         argv += optind;
111
112         newl = 0;
113         skip = 0;
114         pri = 0;
115
116         if (memf == NULL && nlistf == NULL && tailmode == 0) {
117                 /* Running kernel. Use sysctl. */
118                 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
119                         err(1, "sysctl kern.msgbuf");
120                 buflen += 4096;         /* add some slop */
121                 if ((bp = malloc(buflen)) == NULL)
122                         errx(1, "malloc failed");
123                 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
124                         err(1, "sysctl kern.msgbuf");
125                 /* We get a dewrapped buffer using sysctl. */
126                 bufpos = 0;
127                 dumpbuf(bp, bufpos, buflen, &newl, &skip, &pri);
128         } else {
129                 /* Read in kernel message buffer, do sanity checks. */
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",
137                             nlistf ? nlistf : "namelist");
138                 bp = malloc(INCRBUFSIZE);
139                 if (!bp)
140                         errx(1, "malloc failed");
141                 if (KREAD(nl[X_MSGBUF].n_value, bufp))
142                         errx(1, "kvm_read: %s", kvm_geterr(kd));
143                 if (KREAD((long)bufp, cur))
144                         errx(1, "kvm_read: %s", kvm_geterr(kd));
145                 if (cur.msg_magic != MSG_MAGIC)
146                         errx(1, "kernel message buffer has different magic "
147                             "number");
148
149                 /*
150                  * Start point.  Use rindex == bufx as our end-of-buffer
151                  * indication but for the initial rindex from the kernel
152                  * this means the buffer has cycled and is 100% full.
153                  */
154                 rindex = cur.msg_bufr;
155                 if (rindex == cur.msg_bufx) {
156                         if (++rindex >= cur.msg_size)
157                                 rindex = 0;
158                 }
159
160                 for (;;) {
161                         if (cur.msg_bufx >= rindex)
162                                 buflen = cur.msg_bufx - rindex;
163                         else
164                                 buflen = cur.msg_size - rindex;
165                         if (buflen > INCRBUFSIZE)
166                                 buflen = INCRBUFSIZE;
167                         if (kvm_read(kd, (long)cur.msg_ptr + rindex,
168                                      bp, buflen) != (ssize_t)buflen) {
169                                 errx(1, "kvm_read: %s", kvm_geterr(kd));
170                         }
171                         if (buflen)
172                                 dumpbuf(bp, 0, buflen, &newl, &skip, &pri);
173                         rindex += buflen;
174                         if (rindex >= cur.msg_size)
175                                 rindex = 0;
176                         if (rindex == cur.msg_bufx) {
177                                 if (tailmode == 0)
178                                         break;
179                                 fflush(stdout);
180                                 if (tailmode == 1)
181                                         sleep(1);
182                         }
183                         if (KREAD((long)bufp, cur))
184                                 errx(1, "kvm_read: %s", kvm_geterr(kd));
185                 }
186                 kvm_close(kd);
187         }
188         if (!newl)
189                 putchar('\n');
190         if (clear) {
191                 if (sysctlbyname("kern.msgbuf_clear", NULL, NULL,
192                                  &clear, sizeof(int)) != 0) {
193                         err(1, "sysctl kern.msgbuf_clear");
194                 }
195         }
196         return(0);
197 }
198
199 static
200 void
201 dumpbuf(char *bp, size_t bufpos, size_t buflen,
202         int *newl, int *skip, int *pri)
203 {
204         int ch;
205         char *p, *ep;
206         char buf[5];
207
208         /*
209          * The message buffer is circular.  If the buffer has wrapped, the
210          * write pointer points to the oldest data.  Otherwise, the write
211          * pointer points to \0's following the data.  Read the entire
212          * buffer starting at the write pointer and ignore nulls so that
213          * we effectively start at the oldest data.
214          */
215         p = bp + bufpos;
216         ep = (bufpos == 0 ? bp + buflen : p);
217         do {
218                 if (p == bp + buflen)
219                         p = bp;
220                 ch = *p;
221                 /* Skip "\n<.*>" syslog sequences. */
222                 if (*skip) {
223                         if (ch == '\n') {
224                                 *skip = 0;
225                                 *newl = 1;
226                         } if (ch == '>') {
227                                 if (LOG_FAC(*pri) == LOG_KERN || all_opt)
228                                         *newl = *skip = 0;
229                         } else if (ch >= '0' && ch <= '9') {
230                                 *pri *= 10;
231                                 *pri += ch - '0';
232                         }
233                         continue;
234                 }
235                 if (*newl && ch == '<') {
236                         *pri = 0;
237                         *skip = 1;
238                         continue;
239                 }
240                 if (ch == '\0')
241                         continue;
242                 *newl = ch == '\n';
243                 vis(buf, ch, 0, 0);
244                 if (buf[1] == 0)
245                         putchar(buf[0]);
246                 else
247                         printf("%s", buf);
248         } while (++p != ep);
249 }
250
251 void
252 usage(void)
253 {
254         fprintf(stderr, "usage: dmesg [-ac] [-M core] [-N system]\n");
255         exit(1);
256 }