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