Home | History | Annotate | Line # | Download | only in dmesg
dmesg.c revision 1.8
      1  1.8  cgd /*	$NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $	*/
      2  1.8  cgd 
      3  1.1  cgd /*-
      4  1.6  cgd  * Copyright (c) 1991, 1993
      5  1.6  cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1  cgd  * modification, are permitted provided that the following conditions
      9  1.1  cgd  * are met:
     10  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1  cgd  *    must display the following acknowledgement:
     17  1.1  cgd  *	This product includes software developed by the University of
     18  1.1  cgd  *	California, Berkeley and its contributors.
     19  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  cgd  *    may be used to endorse or promote products derived from this software
     21  1.1  cgd  *    without specific prior written permission.
     22  1.1  cgd  *
     23  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  cgd  * SUCH DAMAGE.
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.1  cgd #ifndef lint
     37  1.6  cgd static char copyright[] =
     38  1.6  cgd "@(#) Copyright (c) 1991, 1993\n\
     39  1.6  cgd 	The Regents of the University of California.  All rights reserved.\n";
     40  1.1  cgd #endif /* not lint */
     41  1.1  cgd 
     42  1.1  cgd #ifndef lint
     43  1.8  cgd #if 0
     44  1.6  cgd static char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
     45  1.8  cgd #else
     46  1.8  cgd static char rcsid[] = "$NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $";
     47  1.8  cgd #endif
     48  1.1  cgd #endif /* not lint */
     49  1.1  cgd 
     50  1.1  cgd #include <sys/cdefs.h>
     51  1.1  cgd #include <sys/msgbuf.h>
     52  1.6  cgd 
     53  1.7  cgd #include <err.h>
     54  1.6  cgd #include <fcntl.h>
     55  1.6  cgd #include <kvm.h>
     56  1.6  cgd #include <limits.h>
     57  1.1  cgd #include <nlist.h>
     58  1.6  cgd #include <stdio.h>
     59  1.1  cgd #include <stdlib.h>
     60  1.6  cgd #include <time.h>
     61  1.5  jtc #include <unistd.h>
     62  1.6  cgd #include <vis.h>
     63  1.1  cgd 
     64  1.1  cgd struct nlist nl[] = {
     65  1.6  cgd #define	X_MSGBUF	0
     66  1.1  cgd 	{ "_msgbufp" },
     67  1.1  cgd 	{ NULL },
     68  1.1  cgd };
     69  1.1  cgd 
     70  1.6  cgd void usage __P((void));
     71  1.1  cgd 
     72  1.6  cgd #define	KREAD(addr, var) \
     73  1.6  cgd 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
     74  1.6  cgd 
     75  1.6  cgd int
     76  1.1  cgd main(argc, argv)
     77  1.1  cgd 	int argc;
     78  1.6  cgd 	char *argv[];
     79  1.1  cgd {
     80  1.1  cgd 	register int ch, newl, skip;
     81  1.1  cgd 	register char *p, *ep;
     82  1.6  cgd 	struct msgbuf *bufp, cur;
     83  1.6  cgd 	char *memf, *nlistf;
     84  1.6  cgd 	kvm_t *kd;
     85  1.6  cgd 	char buf[5];
     86  1.1  cgd 
     87  1.6  cgd 	memf = nlistf = NULL;
     88  1.1  cgd 	while ((ch = getopt(argc, argv, "M:N:")) != EOF)
     89  1.1  cgd 		switch(ch) {
     90  1.1  cgd 		case 'M':
     91  1.6  cgd 			memf = optarg;
     92  1.1  cgd 			break;
     93  1.1  cgd 		case 'N':
     94  1.6  cgd 			nlistf = optarg;
     95  1.1  cgd 			break;
     96  1.1  cgd 		case '?':
     97  1.1  cgd 		default:
     98  1.1  cgd 			usage();
     99  1.1  cgd 		}
    100  1.1  cgd 	argc -= optind;
    101  1.1  cgd 	argv += optind;
    102  1.1  cgd 
    103  1.6  cgd 	/*
    104  1.6  cgd 	 * Discard setgid privileges if not the running kernel so that bad
    105  1.6  cgd 	 * guys can't print interesting stuff from kernel memory.
    106  1.6  cgd 	 */
    107  1.6  cgd 	if (memf != NULL || nlistf != NULL)
    108  1.6  cgd 		setgid(getgid());
    109  1.6  cgd 
    110  1.1  cgd 	/* Read in kernel message buffer, do sanity checks. */
    111  1.6  cgd 	if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL)
    112  1.6  cgd 		exit (1);
    113  1.6  cgd 	if (kvm_nlist(kd, nl) == -1)
    114  1.6  cgd 		errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    115  1.6  cgd 	if (nl[X_MSGBUF].n_type == 0)
    116  1.6  cgd 		errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist");
    117  1.6  cgd 	if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
    118  1.6  cgd 		errx(1, "kvm_read: %s", kvm_geterr(kd));
    119  1.6  cgd 	kvm_close(kd);
    120  1.1  cgd 	if (cur.msg_magic != MSG_MAGIC)
    121  1.6  cgd 		errx(1, "magic number incorrect");
    122  1.1  cgd 	if (cur.msg_bufx >= MSG_BSIZE)
    123  1.1  cgd 		cur.msg_bufx = 0;
    124  1.1  cgd 
    125  1.1  cgd 	/*
    126  1.1  cgd 	 * The message buffer is circular; start at the read pointer, and
    127  1.1  cgd 	 * go to the write pointer - 1.
    128  1.1  cgd 	 */
    129  1.1  cgd 	p = cur.msg_bufc + cur.msg_bufx;
    130  1.1  cgd 	ep = cur.msg_bufc + cur.msg_bufx - 1;
    131  1.1  cgd 	for (newl = skip = 0; p != ep; ++p) {
    132  1.1  cgd 		if (p == cur.msg_bufc + MSG_BSIZE)
    133  1.1  cgd 			p = cur.msg_bufc;
    134  1.1  cgd 		ch = *p;
    135  1.1  cgd 		/* Skip "\n<.*>" syslog sequences. */
    136  1.1  cgd 		if (skip) {
    137  1.1  cgd 			if (ch == '>')
    138  1.1  cgd 				newl = skip = 0;
    139  1.1  cgd 			continue;
    140  1.1  cgd 		}
    141  1.1  cgd 		if (newl && ch == '<') {
    142  1.1  cgd 			skip = 1;
    143  1.1  cgd 			continue;
    144  1.1  cgd 		}
    145  1.1  cgd 		if (ch == '\0')
    146  1.1  cgd 			continue;
    147  1.6  cgd 		newl = ch == '\n';
    148  1.6  cgd 		(void)vis(buf, ch, 0, 0);
    149  1.6  cgd 		if (buf[1] == 0)
    150  1.6  cgd 			(void)putchar(buf[0]);
    151  1.6  cgd 		else
    152  1.6  cgd 			(void)printf("%s", buf);
    153  1.1  cgd 	}
    154  1.1  cgd 	if (!newl)
    155  1.1  cgd 		(void)putchar('\n');
    156  1.1  cgd 	exit(0);
    157  1.1  cgd }
    158  1.1  cgd 
    159  1.1  cgd void
    160  1.1  cgd usage()
    161  1.1  cgd {
    162  1.1  cgd 	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
    163  1.1  cgd 	exit(1);
    164  1.1  cgd }
    165