1169695Skan/*-
2169695Skan * SPDX-License-Identifier: BSD-3-Clause
3169695Skan *
4169695Skan * Copyright (c) 1991, 1993
5169695Skan *	The Regents of the University of California.  All rights reserved.
6169695Skan *
7169695Skan * Redistribution and use in source and binary forms, with or without
8169695Skan * modification, are permitted provided that the following conditions
9169695Skan * are met:
10169695Skan * 1. Redistributions of source code must retain the above copyright
11169695Skan *    notice, this list of conditions and the following disclaimer.
12169695Skan * 2. Redistributions in binary form must reproduce the above copyright
13169695Skan *    notice, this list of conditions and the following disclaimer in the
14169695Skan *    documentation and/or other materials provided with the distribution.
15169695Skan * 3. Neither the name of the University nor the names of its contributors
16169695Skan *    may be used to endorse or promote products derived from this software
17169695Skan *    without specific prior written permission.
18169695Skan *
19169695Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22169695Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29169695Skan * SUCH DAMAGE.
30169695Skan */
31169695Skan
32169695Skan#include <sys/types.h>
33169695Skan#include <sys/msgbuf.h>
34169695Skan#include <sys/sysctl.h>
35169695Skan
36169695Skan#include <ctype.h>
37169695Skan#include <err.h>
38169695Skan#include <errno.h>
39169695Skan#include <fcntl.h>
40169695Skan#include <kvm.h>
41169695Skan#include <limits.h>
42169695Skan#include <locale.h>
43169695Skan#include <nlist.h>
44169695Skan#include <stdio.h>
45169695Skan#include <stdbool.h>
46169695Skan#include <stdlib.h>
47169695Skan#include <string.h>
48169695Skan#include <unistd.h>
49169695Skan#include <vis.h>
50169695Skan#include <sys/syslog.h>
51169695Skan
52169695Skanstatic struct nlist nl[] = {
53169695Skan#define	X_MSGBUF	0
54169695Skan	{ "_msgbufp", 0, 0, 0, 0 },
55169695Skan	{ NULL, 0, 0, 0, 0 },
56169695Skan};
57169695Skan
58169695Skanvoid usage(void) __dead2;
59169695Skan
60169695Skan#define	KREAD(addr, var) \
61169695Skan	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
62169695Skan
63169695Skanint
64169695Skanmain(int argc, char *argv[])
65169695Skan{
66169695Skan	struct msgbuf *bufp, cur;
67169695Skan	char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
68169695Skan	kvm_t *kd;
69169695Skan	size_t buflen, bufpos;
70169695Skan	long pri;
71169695Skan	int ch, clear;
72169695Skan	bool all;
73169695Skan
74169695Skan	all = false;
75169695Skan	clear = false;
76169695Skan	(void) setlocale(LC_CTYPE, "");
77169695Skan	memf = nlistf = NULL;
78169695Skan	while ((ch = getopt(argc, argv, "acM:N:")) != -1)
79169695Skan		switch(ch) {
80169695Skan		case 'a':
81169695Skan			all = true;
82169695Skan			break;
83169695Skan		case 'c':
84169695Skan			clear = true;
85169695Skan			break;
86169695Skan		case 'M':
87169695Skan			memf = optarg;
88169695Skan			break;
89169695Skan		case 'N':
90169695Skan			nlistf = optarg;
91169695Skan			break;
92169695Skan		case '?':
93169695Skan		default:
94169695Skan			usage();
95169695Skan		}
96169695Skan	argc -= optind;
97169695Skan	if (argc != 0)
98169695Skan		usage();
99169695Skan
100169695Skan	if (memf == NULL) {
101169695Skan		/*
102169695Skan		 * Running kernel.  Use sysctl.  This gives an unwrapped buffer
103169695Skan		 * as a side effect.  Remove nulterm (if present) so the value
104169695Skan		 * returned by sysctl is formatted as the rest of the code
105169695Skan		 * expects (the same as the value read from a core file below).
106169695Skan		 */
107169695Skan		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
108169695Skan			err(1, "sysctl kern.msgbuf");
109169695Skan		/* Allocate extra room for growth between the sysctl calls. */
110169695Skan		buflen += buflen/8;
111169695Skan		/* Allocate more than sysctl sees, for room to append \n\0. */
112169695Skan		if ((bp = malloc(buflen + 2)) == NULL)
113			errx(1, "malloc failed");
114		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
115			err(1, "sysctl kern.msgbuf");
116		if (buflen > 0 && bp[buflen - 1] == '\0')
117			buflen--;
118		if (clear)
119			if (sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof(int)))
120				err(1, "sysctl kern.msgbuf_clear");
121	} else {
122		/* Read in kernel message buffer and do sanity checks. */
123		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
124		if (kd == NULL)
125			exit (1);
126		if (kvm_nlist(kd, nl) == -1)
127			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
128		if (nl[X_MSGBUF].n_type == 0)
129			errx(1, "%s: msgbufp not found",
130			    nlistf ? nlistf : "namelist");
131		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
132			errx(1, "kvm_read: %s", kvm_geterr(kd));
133		if (cur.msg_magic != MSG_MAGIC)
134			errx(1, "kernel message buffer has different magic "
135			    "number");
136		if ((bp = malloc(cur.msg_size + 2)) == NULL)
137			errx(1, "malloc failed");
138
139		/* Unwrap the circular buffer to start from the oldest data. */
140		bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
141		if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
142		    cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos))
143			errx(1, "kvm_read: %s", kvm_geterr(kd));
144		if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
145		    &bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos)
146			errx(1, "kvm_read: %s", kvm_geterr(kd));
147		kvm_close(kd);
148		buflen = cur.msg_size;
149	}
150
151	/*
152	 * Ensure that the buffer ends with a newline and a \0 to avoid
153	 * complications below.  We left space above.
154	 */
155	if (buflen == 0 || bp[buflen - 1] != '\n')
156		bp[buflen++] = '\n';
157	bp[buflen] = '\0';
158
159	if ((visbp = malloc(4 * buflen + 1)) == NULL)
160		errx(1, "malloc failed");
161
162	/*
163	 * The message buffer is circular, but has been unwrapped so that
164	 * the oldest data comes first.  The data will be preceded by \0's
165	 * if the message buffer was not full.
166	 */
167	p = bp;
168	ep = &bp[buflen];
169	if (*p == '\0') {
170		/* Strip leading \0's */
171		while (*p == '\0')
172			p++;
173	}
174	for (; p < ep; p = nextp) {
175		nextp = memchr(p, '\n', ep - p);
176		nextp++;
177
178		/* Skip ^<[0-9]+> syslog sequences. */
179		if (*p == '<' && isdigit(*(p+1))) {
180			errno = 0;
181			pri = strtol(p + 1, &q, 10);
182			if (*q == '>' && pri >= 0 && pri < INT_MAX &&
183			    errno == 0) {
184				if (LOG_FAC(pri) != LOG_KERN && !all)
185					continue;
186				p = q + 1;
187			}
188		}
189
190		(void)strvisx(visbp, p, nextp - p, 0);
191		(void)printf("%s", visbp);
192	}
193	exit(0);
194}
195
196void
197usage(void)
198{
199	fprintf(stderr, "usage: dmesg [-ac] [-M core [-N system]]\n");
200	exit(1);
201}
202