xref: /freebsd/sbin/dmesg/dmesg.c (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1991, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #endif
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/msgbuf.h>
43 #include <sys/sysctl.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <kvm.h>
50 #include <limits.h>
51 #include <locale.h>
52 #include <nlist.h>
53 #include <stdio.h>
54 #include <stdbool.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <vis.h>
59 #include <sys/syslog.h>
60 
61 static struct nlist nl[] = {
62 #define	X_MSGBUF	0
63 	{ "_msgbufp", 0, 0, 0, 0 },
64 	{ NULL, 0, 0, 0, 0 },
65 };
66 
67 void usage(void) __dead2;
68 
69 #define	KREAD(addr, var) \
70 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
71 
72 int
73 main(int argc, char *argv[])
74 {
75 	struct msgbuf *bufp, cur;
76 	char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
77 	kvm_t *kd;
78 	size_t buflen, bufpos;
79 	long pri;
80 	int ch, clear;
81 	bool all;
82 
83 	all = false;
84 	clear = false;
85 	(void) setlocale(LC_CTYPE, "");
86 	memf = nlistf = NULL;
87 	while ((ch = getopt(argc, argv, "acM:N:")) != -1)
88 		switch(ch) {
89 		case 'a':
90 			all = true;
91 			break;
92 		case 'c':
93 			clear = true;
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 	if (argc != 0)
107 		usage();
108 
109 	if (memf == NULL) {
110 		/*
111 		 * Running kernel.  Use sysctl.  This gives an unwrapped buffer
112 		 * as a side effect.  Remove nulterm (if present) so the value
113 		 * returned by sysctl is formatted as the rest of the code
114 		 * expects (the same as the value read from a core file below).
115 		 */
116 		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
117 			err(1, "sysctl kern.msgbuf");
118 		/* Allocate extra room for growth between the sysctl calls. */
119 		buflen += buflen/8;
120 		/* Allocate more than sysctl sees, for room to append \n\0. */
121 		if ((bp = malloc(buflen + 2)) == NULL)
122 			errx(1, "malloc failed");
123 		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
124 			err(1, "sysctl kern.msgbuf");
125 		if (buflen > 0 && bp[buflen - 1] == '\0')
126 			buflen--;
127 		if (clear)
128 			if (sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof(int)))
129 				err(1, "sysctl kern.msgbuf_clear");
130 	} else {
131 		/* Read in kernel message buffer and do sanity checks. */
132 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
133 		if (kd == NULL)
134 			exit (1);
135 		if (kvm_nlist(kd, nl) == -1)
136 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
137 		if (nl[X_MSGBUF].n_type == 0)
138 			errx(1, "%s: msgbufp not found",
139 			    nlistf ? nlistf : "namelist");
140 		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
141 			errx(1, "kvm_read: %s", kvm_geterr(kd));
142 		if (cur.msg_magic != MSG_MAGIC)
143 			errx(1, "kernel message buffer has different magic "
144 			    "number");
145 		if ((bp = malloc(cur.msg_size + 2)) == NULL)
146 			errx(1, "malloc failed");
147 
148 		/* Unwrap the circular buffer to start from the oldest data. */
149 		bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
150 		if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
151 		    cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos))
152 			errx(1, "kvm_read: %s", kvm_geterr(kd));
153 		if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
154 		    &bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos)
155 			errx(1, "kvm_read: %s", kvm_geterr(kd));
156 		kvm_close(kd);
157 		buflen = cur.msg_size;
158 	}
159 
160 	/*
161 	 * Ensure that the buffer ends with a newline and a \0 to avoid
162 	 * complications below.  We left space above.
163 	 */
164 	if (buflen == 0 || bp[buflen - 1] != '\n')
165 		bp[buflen++] = '\n';
166 	bp[buflen] = '\0';
167 
168 	if ((visbp = malloc(4 * buflen + 1)) == NULL)
169 		errx(1, "malloc failed");
170 
171 	/*
172 	 * The message buffer is circular, but has been unwrapped so that
173 	 * the oldest data comes first.  The data will be preceded by \0's
174 	 * if the message buffer was not full.
175 	 */
176 	p = bp;
177 	ep = &bp[buflen];
178 	if (*p == '\0') {
179 		/* Strip leading \0's */
180 		while (*p == '\0')
181 			p++;
182 	}
183 	for (; p < ep; p = nextp) {
184 		nextp = memchr(p, '\n', ep - p);
185 		nextp++;
186 
187 		/* Skip ^<[0-9]+> syslog sequences. */
188 		if (*p == '<' && isdigit(*(p+1))) {
189 			errno = 0;
190 			pri = strtol(p + 1, &q, 10);
191 			if (*q == '>' && pri >= 0 && pri < INT_MAX &&
192 			    errno == 0) {
193 				if (LOG_FAC(pri) != LOG_KERN && !all)
194 					continue;
195 				p = q + 1;
196 			}
197 		}
198 
199 		(void)strvisx(visbp, p, nextp - p, 0);
200 		(void)printf("%s", visbp);
201 	}
202 	exit(0);
203 }
204 
205 void
206 usage(void)
207 {
208 	fprintf(stderr, "usage: dmesg [-ac] [-M core [-N system]]\n");
209 	exit(1);
210 }
211