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