xref: /netbsd-src/sbin/dmesg/dmesg.c (revision ce63d6c20fc4ec8ddc95c84bb229e3c4ecf82b69)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * 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 char copyright[] =
36 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)dmesg.c	5.9 (Berkeley) 5/2/91";
42 static char rcsid[] = "$Header: /cvsroot/src/sbin/dmesg/dmesg.c,v 1.3 1993/03/23 00:26:54 cgd Exp $";
43 #endif /* not lint */
44 
45 #include <sys/cdefs.h>
46 #include <sys/msgbuf.h>
47 #include <time.h>
48 #include <nlist.h>
49 #include <kvm.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <ctype.h>
53 
54 struct nlist nl[] = {
55 #define	X_MSGBUFP	0
56 	{ "_msgbufp" },
57 	{ NULL },
58 };
59 
60 void usage(), vputc();
61 void err __P((const char *, ...));
62 
63 main(argc, argv)
64 	int argc;
65 	char **argv;
66 {
67 	register int ch, newl, skip;
68 	register char *p, *ep;
69 	struct msgbuf cur;
70 	int msgbufat;
71 	char *core, *namelist;
72 
73 	core = namelist = NULL;
74 	while ((ch = getopt(argc, argv, "M:N:")) != EOF)
75 		switch(ch) {
76 		case 'M':
77 			core = optarg;
78 			break;
79 		case 'N':
80 			namelist = optarg;
81 			break;
82 		case '?':
83 		default:
84 			usage();
85 		}
86 	argc -= optind;
87 	argv += optind;
88 
89 	/* Read in kernel message buffer, do sanity checks. */
90 	if (kvm_openfiles(namelist, core, NULL) == -1)
91 		err("kvm_openfiles: %s", kvm_geterr());
92 	if (kvm_nlist(nl) == -1)
93 		err("kvm_nlist: %s", kvm_geterr());
94 	if (nl[X_MSGBUFP].n_type == 0)
95 		err("msgbufp not found namelist");
96 
97         kvm_read((void *)nl[X_MSGBUFP].n_value, (void *)&msgbufat, sizeof(msgbufat));
98         kvm_read((void *)msgbufat, (void *)&cur, sizeof(cur));
99 	if (cur.msg_magic != MSG_MAGIC)
100 		err("magic number incorrect");
101 	if (cur.msg_bufx >= MSG_BSIZE)
102 		cur.msg_bufx = 0;
103 
104 	/*
105 	 * The message buffer is circular; start at the read pointer, and
106 	 * go to the write pointer - 1.
107 	 */
108 	p = cur.msg_bufc + cur.msg_bufx;
109 	ep = cur.msg_bufc + cur.msg_bufx - 1;
110 	for (newl = skip = 0; p != ep; ++p) {
111 		if (p == cur.msg_bufc + MSG_BSIZE)
112 			p = cur.msg_bufc;
113 		ch = *p;
114 		/* Skip "\n<.*>" syslog sequences. */
115 		if (skip) {
116 			if (ch == '>')
117 				newl = skip = 0;
118 			continue;
119 		}
120 		if (newl && ch == '<') {
121 			skip = 1;
122 			continue;
123 		}
124 		if (ch == '\0')
125 			continue;
126 		newl = (ch = *p) == '\n';
127 		vputc(ch);
128 	}
129 	if (!newl)
130 		(void)putchar('\n');
131 	exit(0);
132 }
133 
134 void
135 vputc(ch)
136 	register int ch;
137 {
138 	int meta;
139 
140 	if (!isascii(ch)) {
141 		(void)putchar('M');
142 		(void)putchar('-');
143 		ch = toascii(ch);
144 		meta = 1;
145 	} else
146 		meta = 0;
147 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
148 		(void)putchar(ch);
149 	else {
150 		(void)putchar('^');
151 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
152 	}
153 }
154 
155 #if __STDC__
156 #include <stdarg.h>
157 #else
158 #include <varargs.h>
159 #endif
160 
161 void
162 #if __STDC__
163 err(const char *fmt, ...)
164 #else
165 err(fmt, va_alist)
166 	char *fmt;
167         va_dcl
168 #endif
169 {
170 	va_list ap;
171 #if __STDC__
172 	va_start(ap, fmt);
173 #else
174 	va_start(ap);
175 #endif
176 	(void)fprintf(stderr, "dmesg: ");
177 	(void)vfprintf(stderr, fmt, ap);
178 	va_end(ap);
179 	(void)fprintf(stderr, "\n");
180 	exit(1);
181 	/* NOTREACHED */
182 }
183 
184 void
185 usage()
186 {
187 	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
188 	exit(1);
189 }
190