xref: /dragonfly/sbin/dmesg/dmesg.c (revision 3170ffd7)
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  * @(#) 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  * $DragonFly: src/sbin/dmesg/dmesg.c,v 1.7 2005/01/14 06:38:41 cpressey Exp $
33  */
34 
35 #include <sys/types.h>
36 #include <sys/msgbuf.h>
37 #include <sys/sysctl.h>
38 
39 #include <err.h>
40 #include <fcntl.h>
41 #include <kvm.h>
42 #include <locale.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <vis.h>
47 #include <sys/syslog.h>
48 
49 struct nlist nl[] = {
50 #define	X_MSGBUF	0
51 	{ "_msgbufp",	0, 0, 0, 0 },
52 	{ NULL,		0, 0, 0, 0 },
53 };
54 
55 void usage(void);
56 
57 #define	KREAD(addr, var) \
58 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
59 
60 int
61 main(int argc, char **argv)
62 {
63 	int ch, newl, skip;
64 	char *p, *ep;
65 	struct msgbuf *bufp, cur;
66 	char *bp, *memf, *nlistf;
67 	kvm_t *kd;
68 	char buf[5];
69 	int all = 0;
70 	int pri = 0;
71 	size_t buflen, bufpos;
72 
73 	setlocale(LC_CTYPE, "");
74 	memf = nlistf = NULL;
75 	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
76 		switch(ch) {
77 		case 'a':
78 			all++;
79 			break;
80 		case 'M':
81 			memf = optarg;
82 			break;
83 		case 'N':
84 			nlistf = optarg;
85 			break;
86 		case '?':
87 		default:
88 			usage();
89 		}
90 	argc -= optind;
91 	argv += optind;
92 
93 	if (memf == NULL && nlistf == NULL) {
94 		/* Running kernel. Use sysctl. */
95 		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
96 			err(1, "sysctl kern.msgbuf");
97 		buflen += 4096;		/* add some slop */
98 		if ((bp = malloc(buflen)) == NULL)
99 			errx(1, "malloc failed");
100 		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
101 			err(1, "sysctl kern.msgbuf");
102 		/* We get a dewrapped buffer using sysctl. */
103 		bufpos = 0;
104 	} else {
105 		/* Read in kernel message buffer, do sanity checks. */
106 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
107 		if (kd == NULL)
108 			exit (1);
109 		if (kvm_nlist(kd, nl) == -1)
110 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
111 		if (nl[X_MSGBUF].n_type == 0)
112 			errx(1, "%s: msgbufp not found",
113 			    nlistf ? nlistf : "namelist");
114 		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
115 			errx(1, "kvm_read: %s", kvm_geterr(kd));
116 		if (cur.msg_magic != MSG_MAGIC)
117 			errx(1, "kernel message buffer has different magic "
118 			    "number");
119 		bp = malloc(cur.msg_size);
120 		if (!bp)
121 			errx(1, "malloc failed");
122 		if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
123 		    (ssize_t)cur.msg_size)
124 			errx(1, "kvm_read: %s", kvm_geterr(kd));
125 		kvm_close(kd);
126 		buflen = cur.msg_size;
127 		bufpos = cur.msg_bufx;
128 		if (bufpos >= buflen)
129 			bufpos = 0;
130 	}
131 
132 	/*
133 	 * The message buffer is circular.  If the buffer has wrapped, the
134 	 * write pointer points to the oldest data.  Otherwise, the write
135 	 * pointer points to \0's following the data.  Read the entire
136 	 * buffer starting at the write pointer and ignore nulls so that
137 	 * we effectively start at the oldest data.
138 	 */
139 	p = bp + bufpos;
140 	ep = (bufpos == 0 ? bp + buflen : p);
141 	newl = skip = 0;
142 	do {
143 		if (p == bp + buflen)
144 			p = bp;
145 		ch = *p;
146 		/* Skip "\n<.*>" syslog sequences. */
147 		if (skip) {
148 			if (ch == '\n') {
149 				skip = 0;
150 				newl = 1;
151 			} if (ch == '>') {
152 				if (LOG_FAC(pri) == LOG_KERN || all)
153 					newl = skip = 0;
154 			} else if (ch >= '0' && ch <= '9') {
155 				pri *= 10;
156 				pri += ch - '0';
157 			}
158 			continue;
159 		}
160 		if (newl && ch == '<') {
161 			pri = 0;
162 			skip = 1;
163 			continue;
164 		}
165 		if (ch == '\0')
166 			continue;
167 		newl = ch == '\n';
168 		vis(buf, ch, 0, 0);
169 		if (buf[1] == 0)
170 			putchar(buf[0]);
171 		else
172 			printf("%s", buf);
173 	} while (++p != ep);
174 	if (!newl)
175 		putchar('\n');
176 	exit(0);
177 }
178 
179 void
180 usage(void)
181 {
182 	fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n");
183 	exit(1);
184 }
185