dmesg.c revision 1.20
1272343Sngie/*	$OpenBSD: dmesg.c,v 1.20 2006/12/24 00:47:27 djm 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. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/msgbuf.h>
35#include <sys/sysctl.h>
36
37#include <err.h>
38#include <fcntl.h>
39#include <kvm.h>
40#include <limits.h>
41#include <nlist.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46#include <unistd.h>
47#include <vis.h>
48
49struct nlist nl[] = {
50#define	X_MSGBUF	0
51	{ "_msgbufp" },
52	{ NULL },
53};
54
55void usage(void);
56
57#define	KREAD(addr, var) \
58	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
59
60int
61main(int argc, char *argv[])
62{
63	int ch, newl, skip, i;
64	char *p;
65	struct msgbuf cur;
66	char *memf, *nlistf, *bufdata;
67	char buf[5];
68
69	memf = nlistf = NULL;
70	while ((ch = getopt(argc, argv, "M:N:")) != -1)
71		switch(ch) {
72		case 'M':
73			memf = optarg;
74			break;
75		case 'N':
76			nlistf = optarg;
77			break;
78		case '?':
79		default:
80			usage();
81		}
82	argc -= optind;
83	argv += optind;
84
85	if (memf == NULL && nlistf == NULL) {
86		int mib[2], msgbufsize;
87		size_t len;
88
89		mib[0] = CTL_KERN;
90		mib[1] = KERN_MSGBUFSIZE;
91		len = sizeof(msgbufsize);
92		if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0))
93			err(1, "sysctl: KERN_MSGBUFSIZE");
94
95		msgbufsize += sizeof(struct msgbuf) - 1;
96		bufdata = malloc(msgbufsize);
97		if (bufdata == NULL)
98			errx(1, "couldn't allocate space for buffer data");
99
100		memset(bufdata, 0, msgbufsize);
101		mib[1] = KERN_MSGBUF;
102		len = msgbufsize;
103		if (sysctl(mib, 2, bufdata, &len, NULL, 0))
104			err(1, "sysctl: KERN_MSGBUF");
105
106		memcpy(&cur, bufdata, sizeof(cur));
107		bufdata = ((struct msgbuf *)bufdata)->msg_bufc;
108	} else {
109#ifndef NOKVM
110		struct msgbuf *bufp;
111		kvm_t *kd;
112
113		/* Read in kernel message buffer, do sanity checks. */
114		if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY,
115		    "dmesg")) == NULL)
116			return (1);
117
118		if (kvm_nlist(kd, nl) == -1)
119			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
120		if (nl[X_MSGBUF].n_type == 0)
121			errx(1, "%s: msgbufp not found",
122			    nlistf ? nlistf : "namelist");
123		if (KREAD(nl[X_MSGBUF].n_value, bufp))
124			errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd),
125			    nl[X_MSGBUF].n_value);
126		if (KREAD((long)bufp, cur))
127			errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd),
128			    (unsigned long)bufp);
129		if (cur.msg_magic != MSG_MAGIC)
130			errx(1, "magic number incorrect");
131		bufdata = malloc(cur.msg_bufs);
132		if (bufdata == NULL)
133			errx(1, "couldn't allocate space for buffer data");
134		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
135		    cur.msg_bufs) != cur.msg_bufs)
136			errx(1, "kvm_read: %s", kvm_geterr(kd));
137		kvm_close(kd);
138#endif
139	}
140
141	if (cur.msg_bufx >= cur.msg_bufs)
142		cur.msg_bufx = 0;
143	/*
144	 * The message buffer is circular; start at the read pointer, and
145	 * go to the write pointer - 1.
146	 */
147	for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
148	    i < cur.msg_bufs; i++, p++) {
149		if (p == bufdata + cur.msg_bufs)
150			p = bufdata;
151		ch = *p;
152		/* Skip "\n<.*>" syslog sequences. */
153		if (skip) {
154			if (ch == '>')
155				newl = skip = 0;
156			continue;
157		}
158		if (newl && ch == '<') {
159			skip = 1;
160			continue;
161		}
162		if (ch == '\0')
163			continue;
164		newl = ch == '\n';
165		vis(buf, ch, 0, 0);
166		if (buf[1] == 0)
167			putchar(buf[0]);
168		else
169			printf("%s", buf);
170	}
171	if (!newl)
172		putchar('\n');
173	return (0);
174}
175
176void
177usage(void)
178{
179	extern char *__progname;
180
181	fprintf(stderr, "usage: %s [-M core] [-N system]\n", __progname);
182	exit(1);
183}
184