dmesg.c revision 136092
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 * 4. 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
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) 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
38static const char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sbin/dmesg/dmesg.c 136092 2004-10-03 15:48:32Z stefanf $");
43
44#include <sys/types.h>
45#include <sys/msgbuf.h>
46#include <sys/sysctl.h>
47
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <kvm.h>
52#include <limits.h>
53#include <locale.h>
54#include <nlist.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59#include <vis.h>
60#include <sys/syslog.h>
61
62struct nlist nl[] = {
63#define	X_MSGBUF	0
64	{ "_msgbufp" },
65	{ NULL },
66};
67
68void usage(void) __dead2;
69
70#define	KREAD(addr, var) \
71	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
72
73int
74main(int argc, char *argv[])
75{
76	struct msgbuf *bufp, cur;
77	char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
78	kvm_t *kd;
79	size_t buflen, bufpos;
80	long pri;
81	int all, ch;
82
83	all = 0;
84	(void) setlocale(LC_CTYPE, "");
85	memf = nlistf = NULL;
86	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
87		switch(ch) {
88		case 'a':
89			all++;
90			break;
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) {
105		/*
106		 * Running kernel.  Use sysctl.  This gives an unwrapped
107		 * buffer as a side effect.
108		 */
109		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
110			err(1, "sysctl kern.msgbuf");
111		if ((bp = malloc(buflen + 2)) == NULL)
112			errx(1, "malloc failed");
113		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
114			err(1, "sysctl kern.msgbuf");
115	} else {
116		/* Read in kernel message buffer and do sanity checks. */
117		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
118		if (kd == NULL)
119			exit (1);
120		if (kvm_nlist(kd, nl) == -1)
121			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
122		if (nl[X_MSGBUF].n_type == 0)
123			errx(1, "%s: msgbufp not found",
124			    nlistf ? nlistf : "namelist");
125		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
126			errx(1, "kvm_read: %s", kvm_geterr(kd));
127		if (cur.msg_magic != MSG_MAGIC)
128			errx(1, "kernel message buffer has different magic "
129			    "number");
130		if ((bp = malloc(cur.msg_size + 2)) == NULL)
131			errx(1, "malloc failed");
132
133		/* Unwrap the circular buffer to start from the oldest data. */
134		bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
135		if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
136		    cur.msg_size - bufpos) != cur.msg_size - bufpos)
137			errx(1, "kvm_read: %s", kvm_geterr(kd));
138		if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
139		    &bp[cur.msg_size - bufpos], bufpos) != bufpos)
140			errx(1, "kvm_read: %s", kvm_geterr(kd));
141		kvm_close(kd);
142		buflen = cur.msg_size;
143	}
144
145	/*
146	 * Ensure that the buffer ends with a newline and a \0 to avoid
147	 * complications below.  We left space above.
148	 */
149	if (buflen == 0 || bp[buflen - 1] != '\n')
150		bp[buflen++] = '\n';
151	bp[buflen] = '\0';
152
153	if ((visbp = malloc(4 * buflen + 1)) == NULL)
154		errx(1, "malloc failed");
155
156	/*
157	 * The message buffer is circular, but has been unwrapped so that
158	 * the oldest data comes first.  The data will be preceded by \0's
159	 * if the message buffer was not full.
160	 */
161	p = bp;
162	ep = &bp[buflen];
163	if (*p == '\0') {
164		/* Strip leading \0's */
165		while (*p == '\0')
166			p++;
167	} else if (!all) {
168		/* Skip the first line, since it is probably incomplete. */
169		p = memchr(p, '\n', ep - p);
170		p++;
171	}
172	for (; p < ep; p = nextp) {
173		nextp = memchr(p, '\n', ep - p);
174		nextp++;
175
176		/* Skip ^<[0-9]+> syslog sequences. */
177		if (*p == '<') {
178			errno = 0;
179			pri = strtol(p + 1, &q, 10);
180			if (*q == '>' && pri >= 0 && pri < INT_MAX &&
181			    errno == 0) {
182				if (LOG_FAC(pri) != LOG_KERN && !all)
183					continue;
184				p = q + 1;
185			}
186		}
187
188		(void)strvisx(visbp, p, nextp - p, 0);
189		(void)printf("%s", visbp);
190	}
191	exit(0);
192}
193
194void
195usage(void)
196{
197	(void)fprintf(stderr, "usage: dmesg [-a] [-M core [-N system]]\n");
198	exit(1);
199}
200