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