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