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