Home | History | Annotate | Line # | Download | only in dmesg
dmesg.c revision 1.27.40.1
      1  1.27.40.1  pgoyette /*	$NetBSD: dmesg.c,v 1.27.40.1 2018/04/07 04:12:09 pgoyette 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.26     lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     34       1.26     lukem  The Regents of the University of California.  All rights reserved.");
     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.27.40.1  pgoyette __RCSID("$NetBSD: dmesg.c,v 1.27.40.1 2018/04/07 04:12:09 pgoyette 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.27.40.1  pgoyette #include <time.h>
     52        1.6       cgd #include <kvm.h>
     53        1.1       cgd #include <nlist.h>
     54        1.6       cgd #include <stdio.h>
     55       1.17    simonb #include <stddef.h>
     56        1.1       cgd #include <stdlib.h>
     57        1.5       jtc #include <unistd.h>
     58        1.6       cgd #include <vis.h>
     59        1.1       cgd 
     60       1.20    simonb #ifndef SMALL
     61       1.27     joerg static struct nlist nl[] = {
     62        1.6       cgd #define	X_MSGBUF	0
     63       1.25  christos 	{ .n_name = "_msgbufp" },
     64       1.25  christos 	{ .n_name = NULL },
     65        1.1       cgd };
     66        1.1       cgd 
     67       1.27     joerg __dead static void	usage(void);
     68        1.1       cgd 
     69        1.6       cgd #define	KREAD(addr, var) \
     70        1.6       cgd 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
     71       1.24       dsl #endif
     72        1.6       cgd 
     73        1.6       cgd int
     74       1.18    simonb main(int argc, char *argv[])
     75        1.1       cgd {
     76       1.17    simonb 	struct kern_msgbuf cur;
     77  1.27.40.1  pgoyette 	int ch, newl, log, i;
     78  1.27.40.1  pgoyette 	size_t tstamp, size;
     79       1.20    simonb 	char *p, *bufdata;
     80  1.27.40.1  pgoyette 	char buf[5];
     81       1.20    simonb #ifndef SMALL
     82  1.27.40.1  pgoyette 	char tbuf[64];
     83       1.20    simonb 	char *memf, *nlistf;
     84  1.27.40.1  pgoyette 	struct timeval boottime;
     85  1.27.40.1  pgoyette 	int ptime = 0;
     86  1.27.40.1  pgoyette 
     87  1.27.40.1  pgoyette 	static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
     88  1.27.40.1  pgoyette 	size = sizeof(boottime);
     89  1.27.40.1  pgoyette 
     90  1.27.40.1  pgoyette 	boottime.tv_sec = 0;
     91  1.27.40.1  pgoyette 	boottime.tv_usec = 0;
     92  1.27.40.1  pgoyette 	ptime = 0;
     93  1.27.40.1  pgoyette 
     94  1.27.40.1  pgoyette         (void)sysctl(bmib, 2, &boottime, &size, NULL, 0);
     95        1.1       cgd 
     96        1.6       cgd 	memf = nlistf = NULL;
     97  1.27.40.1  pgoyette 	while ((ch = getopt(argc, argv, "M:N:qt")) != -1)
     98        1.1       cgd 		switch(ch) {
     99        1.1       cgd 		case 'M':
    100        1.6       cgd 			memf = optarg;
    101        1.1       cgd 			break;
    102        1.1       cgd 		case 'N':
    103        1.6       cgd 			nlistf = optarg;
    104        1.1       cgd 			break;
    105  1.27.40.1  pgoyette 		case 'q':
    106  1.27.40.1  pgoyette 			ptime = -1;
    107  1.27.40.1  pgoyette 			break;
    108  1.27.40.1  pgoyette 		case 't':
    109  1.27.40.1  pgoyette 			ptime = 1;
    110  1.27.40.1  pgoyette 			break;
    111        1.1       cgd 		case '?':
    112        1.1       cgd 		default:
    113        1.1       cgd 			usage();
    114        1.1       cgd 		}
    115        1.1       cgd 	argc -= optind;
    116        1.1       cgd 	argv += optind;
    117        1.1       cgd 
    118       1.19    simonb 	if (memf == NULL) {
    119       1.20    simonb #endif
    120  1.27.40.1  pgoyette 		static const int mmib[2] = { CTL_KERN, KERN_MSGBUF };
    121       1.17    simonb 
    122  1.27.40.1  pgoyette 		if (sysctl(mmib, 2, NULL, &size, NULL, 0) == -1 ||
    123       1.24       dsl 		    (bufdata = malloc(size)) == NULL ||
    124  1.27.40.1  pgoyette 		    sysctl(mmib, 2, bufdata, &size, NULL, 0) == -1)
    125       1.17    simonb 			err(1, "can't get msgbuf");
    126       1.17    simonb 
    127       1.17    simonb 		/* make a dummy struct msgbuf for the display logic */
    128       1.24       dsl 		cur.msg_bufx = 0;
    129       1.24       dsl 		cur.msg_bufs = size;
    130       1.20    simonb #ifndef SMALL
    131       1.17    simonb 	} else {
    132       1.17    simonb 		kvm_t *kd;
    133       1.17    simonb 		struct kern_msgbuf *bufp;
    134       1.17    simonb 
    135       1.17    simonb 		/*
    136       1.17    simonb 		 * Read in message buffer header and data, and do sanity
    137       1.17    simonb 		 * checks.
    138       1.17    simonb 		 */
    139       1.17    simonb 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
    140       1.17    simonb 		if (kd == NULL)
    141       1.17    simonb 			exit (1);
    142       1.17    simonb 		if (kvm_nlist(kd, nl) == -1)
    143       1.17    simonb 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    144       1.17    simonb 		if (nl[X_MSGBUF].n_type == 0)
    145       1.17    simonb 			errx(1, "%s: msgbufp not found", nlistf ? nlistf :
    146       1.17    simonb 			    "namelist");
    147       1.17    simonb 		if (KREAD(nl[X_MSGBUF].n_value, bufp))
    148       1.17    simonb 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    149       1.17    simonb 			    nl[X_MSGBUF].n_value);
    150       1.17    simonb 		if (kvm_read(kd, (long)bufp, &cur,
    151       1.17    simonb 		    offsetof(struct kern_msgbuf, msg_bufc)) !=
    152       1.17    simonb 		    offsetof(struct kern_msgbuf, msg_bufc))
    153       1.17    simonb 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    154       1.17    simonb 			    (unsigned long)bufp);
    155       1.17    simonb 		if (cur.msg_magic != MSG_MAGIC)
    156       1.17    simonb 			errx(1, "magic number incorrect");
    157       1.17    simonb 		bufdata = malloc(cur.msg_bufs);
    158       1.17    simonb 		if (bufdata == NULL)
    159       1.17    simonb 			errx(1, "couldn't allocate space for buffer data");
    160       1.17    simonb 		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
    161       1.17    simonb 		    cur.msg_bufs) != cur.msg_bufs)
    162       1.17    simonb 			errx(1, "kvm_read: %s", kvm_geterr(kd));
    163       1.17    simonb 		kvm_close(kd);
    164       1.17    simonb 		if (cur.msg_bufx >= cur.msg_bufs)
    165       1.17    simonb 			cur.msg_bufx = 0;
    166       1.17    simonb 	}
    167       1.20    simonb #endif
    168        1.1       cgd 
    169        1.1       cgd 	/*
    170       1.14     enami 	 * The message buffer is circular; start at the write pointer
    171       1.14     enami 	 * (which points the oldest character), and go to the write
    172       1.14     enami 	 * pointer - 1 (which points the newest character).  I.e, loop
    173       1.14     enami 	 * over cur.msg_bufs times.  Unused area is skipped since it
    174       1.14     enami 	 * contains nul.
    175        1.1       cgd 	 */
    176  1.27.40.1  pgoyette 	for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
    177       1.14     enami 	    i < cur.msg_bufs; i++, p++) {
    178       1.24       dsl #ifndef SMALL
    179       1.12       leo 		if (p == bufdata + cur.msg_bufs)
    180       1.12       leo 			p = bufdata;
    181  1.27.40.1  pgoyette #define ADDC(c)				\
    182  1.27.40.1  pgoyette     do 					\
    183  1.27.40.1  pgoyette 	if (tstamp < sizeof(tbuf) - 1)	\
    184  1.27.40.1  pgoyette 		tbuf[tstamp++] = (c);	\
    185  1.27.40.1  pgoyette     while (/*CONSTCOND*/0)
    186  1.27.40.1  pgoyette #else
    187  1.27.40.1  pgoyette #define ADDC(c)
    188       1.24       dsl #endif
    189        1.1       cgd 		ch = *p;
    190        1.1       cgd 		if (ch == '\0')
    191        1.1       cgd 			continue;
    192  1.27.40.1  pgoyette 		/* Skip "\n<.*>" syslog sequences. */
    193  1.27.40.1  pgoyette 		/* Gather timestamp sequences */
    194  1.27.40.1  pgoyette 		if (newl) {
    195  1.27.40.1  pgoyette 			switch (ch) {
    196  1.27.40.1  pgoyette 			case '[':
    197  1.27.40.1  pgoyette 				ADDC(ch);
    198  1.27.40.1  pgoyette 				continue;
    199  1.27.40.1  pgoyette 			case '<':
    200  1.27.40.1  pgoyette 				log = 1;
    201  1.27.40.1  pgoyette 				continue;
    202  1.27.40.1  pgoyette 			case '>':
    203  1.27.40.1  pgoyette 				log = 0;
    204  1.27.40.1  pgoyette 				continue;
    205  1.27.40.1  pgoyette 			case ']':
    206  1.27.40.1  pgoyette 				ADDC(ch);
    207  1.27.40.1  pgoyette 				ADDC('\0');
    208  1.27.40.1  pgoyette 				tstamp = 0;
    209  1.27.40.1  pgoyette #ifndef SMALL
    210  1.27.40.1  pgoyette 				if (ptime == 1) {
    211  1.27.40.1  pgoyette 					intmax_t sec;
    212  1.27.40.1  pgoyette 					time_t t;
    213  1.27.40.1  pgoyette 					long nsec;
    214  1.27.40.1  pgoyette 					struct tm tm;
    215  1.27.40.1  pgoyette 
    216  1.27.40.1  pgoyette 					sscanf(tbuf, "[%jd.%ld]", &sec, &nsec);
    217  1.27.40.1  pgoyette 					t = boottime.tv_sec + sec;
    218  1.27.40.1  pgoyette 					if (localtime_r(&t, &tm) != NULL) {
    219  1.27.40.1  pgoyette 						strftime(tbuf, sizeof(tbuf),
    220  1.27.40.1  pgoyette 						    "[%a %b %e %H:%M:%S %Z %Y]",
    221  1.27.40.1  pgoyette 						     &tm);
    222  1.27.40.1  pgoyette 						printf("%s ", tbuf);
    223  1.27.40.1  pgoyette 					}
    224  1.27.40.1  pgoyette 					continue;
    225  1.27.40.1  pgoyette 				} else if (ptime != -1)
    226  1.27.40.1  pgoyette 					printf("%s ", tbuf);
    227  1.27.40.1  pgoyette #endif
    228  1.27.40.1  pgoyette 				continue;
    229  1.27.40.1  pgoyette 			case ' ':
    230  1.27.40.1  pgoyette 				if (!tstamp)
    231  1.27.40.1  pgoyette 					continue;
    232  1.27.40.1  pgoyette 				/*FALLTHROUGH*/
    233  1.27.40.1  pgoyette 			default:
    234  1.27.40.1  pgoyette 				if (tstamp) {
    235  1.27.40.1  pgoyette 				    ADDC(ch);
    236  1.27.40.1  pgoyette 				    continue;
    237  1.27.40.1  pgoyette 				}
    238  1.27.40.1  pgoyette 				if (log)
    239  1.27.40.1  pgoyette 					continue;
    240  1.27.40.1  pgoyette 				break;
    241  1.27.40.1  pgoyette 			}
    242  1.27.40.1  pgoyette 			newl = 0;
    243  1.27.40.1  pgoyette 		}
    244        1.6       cgd 		newl = ch == '\n';
    245       1.21  augustss 		(void)vis(buf, ch, VIS_NOSLASH, 0);
    246       1.24       dsl #ifndef SMALL
    247        1.6       cgd 		if (buf[1] == 0)
    248        1.6       cgd 			(void)putchar(buf[0]);
    249        1.6       cgd 		else
    250       1.24       dsl #endif
    251        1.6       cgd 			(void)printf("%s", buf);
    252       1.12       leo 	}
    253        1.1       cgd 	if (!newl)
    254        1.1       cgd 		(void)putchar('\n');
    255  1.27.40.1  pgoyette 	return EXIT_SUCCESS;
    256        1.1       cgd }
    257        1.1       cgd 
    258       1.24       dsl #ifndef SMALL
    259       1.27     joerg static void
    260       1.18    simonb usage(void)
    261        1.1       cgd {
    262       1.15     enami 
    263  1.27.40.1  pgoyette 	(void)fprintf(stderr, "Usage: %s [-qt] [-M core] [-N system]\n",
    264  1.27.40.1  pgoyette 		getprogname());
    265  1.27.40.1  pgoyette 	exit(EXIT_FAILURE);
    266        1.1       cgd }
    267       1.24       dsl #endif
    268