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