xref: /freebsd/sbin/dmesg/dmesg.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static const char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/msgbuf.h>
50 #include <sys/sysctl.h>
51 
52 #include <err.h>
53 #include <fcntl.h>
54 #include <kvm.h>
55 #include <locale.h>
56 #include <nlist.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 #include <vis.h>
61 #include <sys/syslog.h>
62 
63 struct nlist nl[] = {
64 #define	X_MSGBUF	0
65 	{ "_msgbufp" },
66 	{ NULL },
67 };
68 
69 void usage(void) __dead2;
70 
71 #define	KREAD(addr, var) \
72 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	int ch, newl, skip;
78 	char *p, *ep;
79 	struct msgbuf *bufp, cur;
80 	char *bp, *memf, *nlistf;
81 	kvm_t *kd;
82 	char buf[5];
83 	int all = 0;
84 	int pri;
85 	size_t buflen;
86 	int bufpos;
87 
88 	(void) setlocale(LC_CTYPE, "");
89 	memf = nlistf = NULL;
90 	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
91 		switch(ch) {
92 		case 'a':
93 			all++;
94 			break;
95 		case 'M':
96 			memf = optarg;
97 			break;
98 		case 'N':
99 			nlistf = optarg;
100 			break;
101 		case '?':
102 		default:
103 			usage();
104 		}
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (memf == NULL && nlistf == NULL) {
109 		/* Running kernel. Use sysctl. */
110 		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
111 			err(1, "sysctl kern.msgbuf");
112 		if ((bp = malloc(buflen)) == NULL)
113 			errx(1, "malloc failed");
114 		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
115 			err(1, "sysctl kern.msgbuf");
116 		/* We get a dewrapped buffer using sysctl. */
117 		bufpos = 0;
118 	} else {
119 		/* Read in kernel message buffer, do sanity checks. */
120 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
121 		if (kd == NULL)
122 			exit (1);
123 		if (kvm_nlist(kd, nl) == -1)
124 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
125 		if (nl[X_MSGBUF].n_type == 0)
126 			errx(1, "%s: msgbufp not found",
127 			    nlistf ? nlistf : "namelist");
128 		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
129 			errx(1, "kvm_read: %s", kvm_geterr(kd));
130 		if (cur.msg_magic != MSG_MAGIC)
131 			errx(1, "kernel message buffer has different magic "
132 			    "number");
133 		bp = malloc(cur.msg_size);
134 		if (!bp)
135 			errx(1, "malloc failed");
136 		if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
137 		    cur.msg_size)
138 			errx(1, "kvm_read: %s", kvm_geterr(kd));
139 		kvm_close(kd);
140 		buflen = cur.msg_size;
141 		bufpos = cur.msg_bufx;
142 		if (bufpos >= buflen)
143 			bufpos = 0;
144 	}
145 
146 	/*
147 	 * The message buffer is circular.  If the buffer has wrapped, the
148 	 * write pointer points to the oldest data.  Otherwise, the write
149 	 * pointer points to \0's following the data.  Read the entire
150 	 * buffer starting at the write pointer and ignore nulls so that
151 	 * we effectively start at the oldest data.
152 	 */
153 	p = bp + bufpos;
154 	ep = (bufpos == 0 ? bp + buflen : p);
155 	newl = skip = 0;
156 	do {
157 		if (p == bp + buflen)
158 			p = bp;
159 		ch = *p;
160 		/* Skip "\n<.*>" syslog sequences. */
161 		if (skip) {
162 			if (ch == '\n') {
163 				skip = 0;
164 				newl = 1;
165 			} if (ch == '>') {
166 				if (LOG_FAC(pri) == LOG_KERN || all)
167 					newl = skip = 0;
168 			} else if (ch >= '0' && ch <= '9') {
169 				pri *= 10;
170 				pri += ch - '0';
171 			}
172 			continue;
173 		}
174 		if (newl && ch == '<') {
175 			pri = 0;
176 			skip = 1;
177 			continue;
178 		}
179 		if (ch == '\0')
180 			continue;
181 		newl = ch == '\n';
182 		(void)vis(buf, ch, 0, 0);
183 		if (buf[1] == 0)
184 			(void)putchar(buf[0]);
185 		else
186 			(void)printf("%s", buf);
187 	} while (++p != ep);
188 	if (!newl)
189 		(void)putchar('\n');
190 	exit(0);
191 }
192 
193 void
194 usage(void)
195 {
196 	(void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n");
197 	exit(1);
198 }
199