Home | History | Annotate | Line # | Download | only in dmesg
dmesg.c revision 1.16.8.1
      1  1.16.8.1  minoura /*	$NetBSD: dmesg.c,v 1.16.8.1 2000/06/22 16:05:22 minoura Exp $	*/
      2       1.1      cgd /*-
      3       1.6      cgd  * Copyright (c) 1991, 1993
      4       1.6      cgd  *	The Regents of the University of California.  All rights reserved.
      5       1.1      cgd  *
      6       1.1      cgd  * Redistribution and use in source and binary forms, with or without
      7       1.1      cgd  * modification, are permitted provided that the following conditions
      8       1.1      cgd  * are met:
      9       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     10       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     11       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     13       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     14       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     15       1.1      cgd  *    must display the following acknowledgement:
     16       1.1      cgd  *	This product includes software developed by the University of
     17       1.1      cgd  *	California, Berkeley and its contributors.
     18       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     19       1.1      cgd  *    may be used to endorse or promote products derived from this software
     20       1.1      cgd  *    without specific prior written permission.
     21       1.1      cgd  *
     22       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1      cgd  * SUCH DAMAGE.
     33       1.1      cgd  */
     34       1.1      cgd 
     35      1.11    lukem #include <sys/cdefs.h>
     36       1.1      cgd #ifndef lint
     37      1.11    lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     38      1.11    lukem 	The Regents of the University of California.  All rights reserved.\n");
     39       1.1      cgd #endif /* not lint */
     40       1.1      cgd 
     41       1.1      cgd #ifndef lint
     42       1.8      cgd #if 0
     43       1.6      cgd static char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
     44       1.8      cgd #else
     45  1.16.8.1  minoura __RCSID("$NetBSD: dmesg.c,v 1.16.8.1 2000/06/22 16:05:22 minoura Exp $");
     46       1.8      cgd #endif
     47       1.1      cgd #endif /* not lint */
     48       1.1      cgd 
     49  1.16.8.1  minoura #include <sys/param.h>
     50       1.1      cgd #include <sys/msgbuf.h>
     51  1.16.8.1  minoura #include <sys/sysctl.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.1      cgd #include <nlist.h>
     57       1.6      cgd #include <stdio.h>
     58  1.16.8.1  minoura #include <stddef.h>
     59       1.1      cgd #include <stdlib.h>
     60       1.5      jtc #include <unistd.h>
     61       1.6      cgd #include <vis.h>
     62       1.1      cgd 
     63  1.16.8.1  minoura #ifndef SMALL
     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.16.8.1  minoura #endif
     70       1.1      cgd 
     71  1.16.8.1  minoura int	main(int, char *[]);
     72  1.16.8.1  minoura void	usage(void);
     73       1.1      cgd 
     74       1.6      cgd #define	KREAD(addr, var) \
     75       1.6      cgd 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
     76       1.6      cgd 
     77       1.6      cgd int
     78  1.16.8.1  minoura main(int argc, char *argv[])
     79       1.1      cgd {
     80  1.16.8.1  minoura 	struct kern_msgbuf cur;
     81      1.14    enami 	int ch, newl, skip, i;
     82  1.16.8.1  minoura 	char *p, *bufdata;
     83  1.16.8.1  minoura #ifndef SMALL
     84  1.16.8.1  minoura 	char *memf, *nlistf;
     85  1.16.8.1  minoura #endif
     86       1.6      cgd 	char buf[5];
     87       1.1      cgd 
     88  1.16.8.1  minoura #ifndef SMALL
     89       1.6      cgd 	memf = nlistf = NULL;
     90      1.11    lukem 	while ((ch = getopt(argc, argv, "M:N:")) != -1)
     91       1.1      cgd 		switch(ch) {
     92       1.1      cgd 		case 'M':
     93       1.6      cgd 			memf = optarg;
     94       1.1      cgd 			break;
     95       1.1      cgd 		case 'N':
     96       1.6      cgd 			nlistf = optarg;
     97       1.1      cgd 			break;
     98       1.1      cgd 		case '?':
     99       1.1      cgd 		default:
    100       1.1      cgd 			usage();
    101       1.1      cgd 		}
    102       1.1      cgd 	argc -= optind;
    103       1.1      cgd 	argv += optind;
    104       1.1      cgd 
    105  1.16.8.1  minoura 	if (memf == NULL) {
    106  1.16.8.1  minoura #endif
    107  1.16.8.1  minoura 		size_t size;
    108  1.16.8.1  minoura 		int mib[2];
    109  1.16.8.1  minoura 
    110  1.16.8.1  minoura 		mib[0] = CTL_KERN;
    111  1.16.8.1  minoura 		mib[1] = KERN_MSGBUF;
    112       1.6      cgd 
    113  1.16.8.1  minoura 		if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1)
    114  1.16.8.1  minoura 			errx(1, "can't get size of msgbuf");
    115  1.16.8.1  minoura 
    116  1.16.8.1  minoura 		if ((bufdata = malloc(size)) == NULL)
    117  1.16.8.1  minoura 			err(1, "couldn't allocate space for buffer data");
    118  1.16.8.1  minoura 
    119  1.16.8.1  minoura 		if (sysctl(mib, 2, bufdata, &size, NULL, 0) == -1)
    120  1.16.8.1  minoura 			err(1, "can't get msgbuf");
    121  1.16.8.1  minoura 
    122  1.16.8.1  minoura 		/* make a dummy struct msgbuf for the display logic */
    123  1.16.8.1  minoura 		cur.msg_bufx =  cur.msg_bufs = size;
    124  1.16.8.1  minoura #ifndef SMALL
    125  1.16.8.1  minoura 	} else {
    126  1.16.8.1  minoura 		kvm_t *kd;
    127  1.16.8.1  minoura 		struct kern_msgbuf *bufp;
    128  1.16.8.1  minoura 
    129  1.16.8.1  minoura 		/*
    130  1.16.8.1  minoura 		 * Read in message buffer header and data, and do sanity
    131  1.16.8.1  minoura 		 * checks.
    132  1.16.8.1  minoura 		 */
    133  1.16.8.1  minoura 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
    134  1.16.8.1  minoura 		if (kd == NULL)
    135  1.16.8.1  minoura 			exit (1);
    136  1.16.8.1  minoura 		if (kvm_nlist(kd, nl) == -1)
    137  1.16.8.1  minoura 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    138  1.16.8.1  minoura 		if (nl[X_MSGBUF].n_type == 0)
    139  1.16.8.1  minoura 			errx(1, "%s: msgbufp not found", nlistf ? nlistf :
    140  1.16.8.1  minoura 			    "namelist");
    141  1.16.8.1  minoura 		if (KREAD(nl[X_MSGBUF].n_value, bufp))
    142  1.16.8.1  minoura 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    143  1.16.8.1  minoura 			    nl[X_MSGBUF].n_value);
    144  1.16.8.1  minoura 		if (kvm_read(kd, (long)bufp, &cur,
    145  1.16.8.1  minoura 		    offsetof(struct kern_msgbuf, msg_bufc)) !=
    146  1.16.8.1  minoura 		    offsetof(struct kern_msgbuf, msg_bufc))
    147  1.16.8.1  minoura 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    148  1.16.8.1  minoura 			    (unsigned long)bufp);
    149  1.16.8.1  minoura 		if (cur.msg_magic != MSG_MAGIC)
    150  1.16.8.1  minoura 			errx(1, "magic number incorrect");
    151  1.16.8.1  minoura 		bufdata = malloc(cur.msg_bufs);
    152  1.16.8.1  minoura 		if (bufdata == NULL)
    153  1.16.8.1  minoura 			errx(1, "couldn't allocate space for buffer data");
    154  1.16.8.1  minoura 		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
    155  1.16.8.1  minoura 		    cur.msg_bufs) != cur.msg_bufs)
    156  1.16.8.1  minoura 			errx(1, "kvm_read: %s", kvm_geterr(kd));
    157  1.16.8.1  minoura 		kvm_close(kd);
    158  1.16.8.1  minoura 		if (cur.msg_bufx >= cur.msg_bufs)
    159  1.16.8.1  minoura 			cur.msg_bufx = 0;
    160  1.16.8.1  minoura 	}
    161  1.16.8.1  minoura #endif
    162       1.1      cgd 
    163       1.1      cgd 	/*
    164      1.14    enami 	 * The message buffer is circular; start at the write pointer
    165      1.14    enami 	 * (which points the oldest character), and go to the write
    166      1.14    enami 	 * pointer - 1 (which points the newest character).  I.e, loop
    167      1.14    enami 	 * over cur.msg_bufs times.  Unused area is skipped since it
    168      1.14    enami 	 * contains nul.
    169       1.1      cgd 	 */
    170      1.14    enami 	for (newl = skip = i = 0, p = bufdata + cur.msg_bufx;
    171      1.14    enami 	    i < cur.msg_bufs; i++, p++) {
    172      1.12      leo 		if (p == bufdata + cur.msg_bufs)
    173      1.12      leo 			p = bufdata;
    174       1.1      cgd 		ch = *p;
    175       1.1      cgd 		/* Skip "\n<.*>" syslog sequences. */
    176       1.1      cgd 		if (skip) {
    177       1.1      cgd 			if (ch == '>')
    178       1.1      cgd 				newl = skip = 0;
    179       1.1      cgd 			continue;
    180       1.1      cgd 		}
    181       1.1      cgd 		if (newl && ch == '<') {
    182       1.1      cgd 			skip = 1;
    183       1.1      cgd 			continue;
    184       1.1      cgd 		}
    185       1.1      cgd 		if (ch == '\0')
    186       1.1      cgd 			continue;
    187       1.6      cgd 		newl = ch == '\n';
    188       1.6      cgd 		(void)vis(buf, ch, 0, 0);
    189       1.6      cgd 		if (buf[1] == 0)
    190       1.6      cgd 			(void)putchar(buf[0]);
    191       1.6      cgd 		else
    192       1.6      cgd 			(void)printf("%s", buf);
    193      1.12      leo 	}
    194       1.1      cgd 	if (!newl)
    195       1.1      cgd 		(void)putchar('\n');
    196       1.1      cgd 	exit(0);
    197       1.1      cgd }
    198       1.1      cgd 
    199       1.1      cgd void
    200  1.16.8.1  minoura usage(void)
    201       1.1      cgd {
    202      1.15    enami 
    203  1.16.8.1  minoura #ifdef SMALL
    204  1.16.8.1  minoura 	(void)fprintf(stderr, "usage: dmesg\n");
    205  1.16.8.1  minoura #else
    206       1.1      cgd 	(void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n");
    207  1.16.8.1  minoura #endif
    208       1.1      cgd 	exit(1);
    209       1.1      cgd }
    210