xref: /freebsd/sbin/dmesg/dmesg.c (revision 4784a469)
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 #if 0
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1991, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 static const char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
43 #endif /* not lint */
44 #endif
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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, bufpos;
86 
87 	(void) setlocale(LC_CTYPE, "");
88 	memf = nlistf = NULL;
89 	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
90 		switch(ch) {
91 		case 'a':
92 			all++;
93 			break;
94 		case 'M':
95 			memf = optarg;
96 			break;
97 		case 'N':
98 			nlistf = optarg;
99 			break;
100 		case '?':
101 		default:
102 			usage();
103 		}
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (memf == NULL && nlistf == NULL) {
108 		/* Running kernel. Use sysctl. */
109 		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
110 			err(1, "sysctl kern.msgbuf");
111 		if ((bp = malloc(buflen)) == NULL)
112 			errx(1, "malloc failed");
113 		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
114 			err(1, "sysctl kern.msgbuf");
115 		/* We get a dewrapped buffer using sysctl. */
116 		bufpos = 0;
117 	} else {
118 		/* Read in kernel message buffer, do sanity checks. */
119 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
120 		if (kd == NULL)
121 			exit (1);
122 		if (kvm_nlist(kd, nl) == -1)
123 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
124 		if (nl[X_MSGBUF].n_type == 0)
125 			errx(1, "%s: msgbufp not found",
126 			    nlistf ? nlistf : "namelist");
127 		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
128 			errx(1, "kvm_read: %s", kvm_geterr(kd));
129 		if (cur.msg_magic != MSG_MAGIC)
130 			errx(1, "kernel message buffer has different magic "
131 			    "number");
132 		bp = malloc(cur.msg_size);
133 		if (!bp)
134 			errx(1, "malloc failed");
135 		if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
136 		    cur.msg_size)
137 			errx(1, "kvm_read: %s", kvm_geterr(kd));
138 		kvm_close(kd);
139 		buflen = cur.msg_size;
140 		bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
141 	}
142 
143 	/*
144 	 * The message buffer is circular.  If the buffer has wrapped, the
145 	 * write pointer points to the oldest data.  Otherwise, the write
146 	 * pointer points to \0's following the data.  Read the entire
147 	 * buffer starting at the write pointer and ignore nulls so that
148 	 * we effectively start at the oldest data.
149 	 */
150 	p = bp + bufpos;
151 	ep = (bufpos == 0 ? bp + buflen : p);
152 	newl = skip = 0;
153 	do {
154 		if (p == bp + buflen)
155 			p = bp;
156 		ch = *p;
157 		/* Skip "\n<.*>" syslog sequences. */
158 		if (skip) {
159 			if (ch == '\n') {
160 				skip = 0;
161 				newl = 1;
162 			} if (ch == '>') {
163 				if (LOG_FAC(pri) == LOG_KERN || all)
164 					newl = skip = 0;
165 			} else if (ch >= '0' && ch <= '9') {
166 				pri *= 10;
167 				pri += ch - '0';
168 			}
169 			continue;
170 		}
171 		if (newl && ch == '<') {
172 			pri = 0;
173 			skip = 1;
174 			continue;
175 		}
176 		if (ch == '\0')
177 			continue;
178 		newl = ch == '\n';
179 		(void)vis(buf, ch, 0, 0);
180 		if (buf[1] == 0)
181 			(void)putchar(buf[0]);
182 		else
183 			(void)printf("%s", buf);
184 	} while (++p != ep);
185 	if (!newl)
186 		(void)putchar('\n');
187 	exit(0);
188 }
189 
190 void
191 usage(void)
192 {
193 	(void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n");
194 	exit(1);
195 }
196