dmesg.c revision 1.9
1/*	$OpenBSD: dmesg.c,v 1.9 2001/06/04 14:59:47 mickey Exp $	*/
2/*	$NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $	*/
3
4/*-
5 * Copyright (c) 1991, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char copyright[] =
39"@(#) Copyright (c) 1991, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45static char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
46#else
47static char rcsid[] = "$OpenBSD: dmesg.c,v 1.9 2001/06/04 14:59:47 mickey Exp $";
48#endif
49#endif /* not lint */
50
51#include <sys/cdefs.h>
52#include <sys/msgbuf.h>
53
54#include <err.h>
55#include <fcntl.h>
56#include <kvm.h>
57#include <limits.h>
58#include <nlist.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <time.h>
62#include <unistd.h>
63#include <vis.h>
64
65struct nlist nl[] = {
66#define	X_MSGBUF	0
67	{ "_msgbufp" },
68	{ NULL },
69};
70
71void usage __P((void));
72
73#define	KREAD(addr, var) \
74	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
75
76int
77main(argc, argv)
78	int argc;
79	char *argv[];
80{
81	register int ch, newl, skip, i;
82	register char *p, *ep;
83	struct msgbuf *bufp, cur;
84	char *memf, *nlistf, *bufdata;
85	kvm_t *kd;
86	char buf[5];
87
88	memf = nlistf = NULL;
89	while ((ch = getopt(argc, argv, "M:N:")) != -1)
90		switch(ch) {
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	/*
105	 * Discard setgid privileges if not the running kernel so that bad
106	 * guys can't print interesting stuff from kernel memory.
107	 */
108	if (memf != NULL || nlistf != NULL) {
109		setegid(getgid());
110		setgid(getgid());
111	}
112
113	/* Read in kernel message buffer, do sanity checks. */
114	if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL)
115		return (1);
116
117	setegid(getgid());
118	setgid(getgid());
119
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", nlistf ? nlistf : "namelist");
124	if (KREAD(nl[X_MSGBUF].n_value, bufp))
125		errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd),
126		    nl[X_MSGBUF].n_value);
127	if (KREAD((long)bufp, cur))
128		errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd),
129		    (unsigned long)bufp);
130	if (cur.msg_magic != MSG_MAGIC)
131		errx(1, "magic number incorrect");
132	bufdata = malloc(cur.msg_bufs);
133	if (bufdata == NULL)
134		errx(1, "couldn't allocate space for buffer data");
135	if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
136	    cur.msg_bufs) != cur.msg_bufs)
137		errx(1, "kvm_read: %s", kvm_geterr(kd));
138	if (cur.msg_bufx >= cur.msg_bufs)
139		cur.msg_bufx = 0;
140	kvm_close(kd);
141
142	/*
143	 * The message buffer is circular; start at the read pointer, and
144	 * go to the write pointer - 1.
145	 */
146	for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
147	    i < cur.msg_bufs; i++, p++) {
148		if (p == bufdata + cur.msg_bufs)
149			p = bufdata;
150		ch = *p;
151		/* Skip "\n<.*>" syslog sequences. */
152		if (skip) {
153			if (ch == '>')
154				newl = skip = 0;
155			continue;
156		}
157		if (newl && ch == '<') {
158			skip = 1;
159			continue;
160		}
161		if (ch == '\0')
162			continue;
163		newl = ch == '\n';
164		(void)vis(buf, ch, 0, 0);
165		if (buf[1] == 0)
166			(void)putchar(buf[0]);
167		else
168			(void)printf("%s", buf);
169	}
170	if (!newl)
171		(void)putchar('\n');
172	return (0);
173}
174
175void
176usage()
177{
178	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
179	exit(1);
180}
181