Home | History | Annotate | Line # | Download | only in dmesg
dmesg.c revision 1.34
      1 /*	$NetBSD: dmesg.c,v 1.34 2018/09/19 00:15:05 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.34 2018/09/19 00:15:05 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 <ctype.h>
     51 #include <fcntl.h>
     52 #include <time.h>
     53 #include <kvm.h>
     54 #include <nlist.h>
     55 #include <stdio.h>
     56 #include <stddef.h>
     57 #include <stdlib.h>
     58 #include <unistd.h>
     59 #include <vis.h>
     60 
     61 #ifndef SMALL
     62 static struct nlist nl[] = {
     63 #define	X_MSGBUF	0
     64 	{ .n_name = "_msgbufp" },
     65 	{ .n_name = NULL },
     66 };
     67 
     68 __dead static void	usage(void);
     69 
     70 #define	KREAD(addr, var) \
     71 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
     72 
     73 static const char *
     74 fmtydhmsf(char *b, size_t l, time_t t, long nsec)
     75 {
     76 	time_t s, m, h, d, M, y;
     77 	int z;
     78 	size_t o;
     79 
     80 	s = t % 60;
     81 	t /= 60;
     82 
     83 	m = t % 60;
     84 	t /= 60;
     85 
     86 	h = t % 24;
     87 	t /= 24;
     88 
     89 	d = t % 30;
     90 	t /= 30;
     91 
     92 	M = t % 12;
     93 	t /= 12;
     94 
     95 	y = t;
     96 
     97 	z = 0;
     98 	o = 0;
     99 
    100 #define APPENDFMT(fmt, ...)  \
    101     do { \
    102 	    z = snprintf(b + o, l - o, fmt, __VA_ARGS__); \
    103 	    if (z == -1) \
    104 		    return b; \
    105 	    o += (size_t)z; \
    106 	    if (o >= l) \
    107 		    return b; \
    108     } while (/*CONSTCOND*/0)
    109 
    110 #define APPEND(a) \
    111     do if (a) \
    112     APPENDFMT("%jd%c", (intmax_t)a, toupper((unsigned char)__STRING(a)[0])); \
    113     while (/*CONSTCOND*/0)
    114 #define APPENDS(a, s) \
    115     APPENDFMT("%jd.%ld%c", (intmax_t)a, s, \
    116 	toupper((unsigned char)__STRING(a)[0]))
    117 
    118 	APPENDFMT("%s", "P");
    119 	APPEND(y);
    120 	APPEND(M);
    121 	APPEND(d);
    122 	APPENDFMT("%s", "T");
    123 	APPEND(h);
    124 	APPEND(m);
    125 	if (nsec)
    126 		nsec = nsec / 1000000;
    127 	if (nsec)
    128 		APPENDS(s, nsec);
    129 	else
    130 		APPEND(s);
    131 	return b;
    132 }
    133 
    134 static void
    135 pnsec(long nsec, long fsec, int scale)
    136 {
    137 	if (scale > 6)
    138 		printf("%6.6ld", (nsec + 499) / 1000);
    139 	else
    140 		printf("%*.*ld%.*s", scale, scale, fsec, 6 - scale, "000000");
    141 }
    142 #endif
    143 
    144 int
    145 main(int argc, char *argv[])
    146 {
    147 	struct kern_msgbuf cur;
    148 	int ch, newl, log, i;
    149 	size_t tstamp, size;
    150 	char *p, *bufdata;
    151 	char buf[5];
    152 #ifndef SMALL
    153 	char tbuf[64];
    154 	char *memf, *nlistf;
    155 	struct timeval boottime;
    156 	struct timespec lasttime;
    157 	intmax_t sec;
    158 	long nsec, fsec;
    159 	int scale;
    160 	int deltas, quiet, humantime;
    161 	bool frac;
    162 
    163 	static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
    164 	size = sizeof(boottime);
    165 
    166 	boottime.tv_sec = 0;
    167 	boottime.tv_usec = 0;
    168 	lasttime.tv_sec = 0;
    169 	lasttime.tv_nsec = 0;
    170 	deltas = quiet = humantime = 0;
    171 
    172         (void)sysctl(bmib, 2, &boottime, &size, NULL, 0);
    173 
    174 	memf = nlistf = NULL;
    175 	while ((ch = getopt(argc, argv, "dM:N:tT")) != -1)
    176 		switch(ch) {
    177 		case 'd':
    178 			deltas = 1;
    179 			break;
    180 		case 'M':
    181 			memf = optarg;
    182 			break;
    183 		case 'N':
    184 			nlistf = optarg;
    185 			break;
    186 		case 't':
    187 			quiet = 1;
    188 			break;
    189 		case 'T':
    190 			humantime++;
    191 			break;
    192 		case '?':
    193 		default:
    194 			usage();
    195 		}
    196 	argc -= optind;
    197 	argv += optind;
    198 	if (quiet && humantime)
    199 		err(EXIT_FAILURE, "-t cannot be used with -T");
    200 
    201 	if (memf == NULL) {
    202 #endif
    203 		static const int mmib[2] = { CTL_KERN, KERN_MSGBUF };
    204 
    205 		if (sysctl(mmib, 2, NULL, &size, NULL, 0) == -1 ||
    206 		    (bufdata = malloc(size)) == NULL ||
    207 		    sysctl(mmib, 2, bufdata, &size, NULL, 0) == -1)
    208 			err(1, "can't get msgbuf");
    209 
    210 		/* make a dummy struct msgbuf for the display logic */
    211 		cur.msg_bufx = 0;
    212 		cur.msg_bufs = size;
    213 #ifndef SMALL
    214 	} else {
    215 		kvm_t *kd;
    216 		struct kern_msgbuf *bufp;
    217 
    218 		/*
    219 		 * Read in message buffer header and data, and do sanity
    220 		 * checks.
    221 		 */
    222 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
    223 		if (kd == NULL)
    224 			exit (1);
    225 		if (kvm_nlist(kd, nl) == -1)
    226 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    227 		if (nl[X_MSGBUF].n_type == 0)
    228 			errx(1, "%s: msgbufp not found", nlistf ? nlistf :
    229 			    "namelist");
    230 		if (KREAD(nl[X_MSGBUF].n_value, bufp))
    231 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    232 			    nl[X_MSGBUF].n_value);
    233 		if (kvm_read(kd, (long)bufp, &cur,
    234 		    offsetof(struct kern_msgbuf, msg_bufc)) !=
    235 		    offsetof(struct kern_msgbuf, msg_bufc))
    236 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    237 			    (unsigned long)bufp);
    238 		if (cur.msg_magic != MSG_MAGIC)
    239 			errx(1, "magic number incorrect");
    240 		bufdata = malloc(cur.msg_bufs);
    241 		if (bufdata == NULL)
    242 			errx(1, "couldn't allocate space for buffer data");
    243 		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
    244 		    cur.msg_bufs) != cur.msg_bufs)
    245 			errx(1, "kvm_read: %s", kvm_geterr(kd));
    246 		kvm_close(kd);
    247 		if (cur.msg_bufx >= cur.msg_bufs)
    248 			cur.msg_bufx = 0;
    249 	}
    250 #endif
    251 
    252 	/*
    253 	 * The message buffer is circular; start at the write pointer
    254 	 * (which points the oldest character), and go to the write
    255 	 * pointer - 1 (which points the newest character).  I.e, loop
    256 	 * over cur.msg_bufs times.  Unused area is skipped since it
    257 	 * contains nul.
    258 	 */
    259 #ifndef SMALL
    260 	frac = false;
    261 	scale = 0;
    262 #endif
    263 	for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
    264 	    i < cur.msg_bufs; i++, p++) {
    265 
    266 #ifndef SMALL
    267 		if (p == bufdata + cur.msg_bufs)
    268 			p = bufdata;
    269 #define ADDC(c)				\
    270     do {				\
    271 	if (tstamp < sizeof(tbuf) - 1)	\
    272 		tbuf[tstamp++] = (c);	\
    273 	if (frac)			\
    274 		scale++;		\
    275     } while (/*CONSTCOND*/0)
    276 #else
    277 #define ADDC(c)
    278 #endif
    279 		ch = *p;
    280 		if (ch == '\0')
    281 			continue;
    282 		/* Skip "\n<.*>" syslog sequences. */
    283 		/* Gather timestamp sequences */
    284 		if (newl) {
    285 #ifndef SMALL
    286 			int j;
    287 #endif
    288 
    289 			switch (ch) {
    290 			case '[':
    291 #ifndef SMALL
    292 				frac = false;
    293 				scale = 0;
    294 #endif
    295 				ADDC(ch);
    296 				continue;
    297 			case '<':
    298 				log = 1;
    299 				continue;
    300 			case '>':
    301 				log = 0;
    302 				continue;
    303 			case ']':
    304 #ifndef SMALL
    305 				frac = false;
    306 #endif
    307 				ADDC(ch);
    308 				ADDC('\0');
    309 				tstamp = 0;
    310 #ifndef SMALL
    311 				sec = fsec = 0;
    312 				switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
    313 				case EOF:
    314 				case 0:
    315 					/*???*/
    316 					continue;
    317 				case 1:
    318 					fsec = 0;
    319 					break;
    320 				case 2:
    321 					break;
    322 				default:
    323 					/* Help */
    324 					continue;
    325 				}
    326 
    327 				for (nsec = fsec, j = 9 - scale; --j >= 0; )
    328 					nsec *= 10;
    329 				if (!quiet || deltas)
    330 					printf("[");
    331 				if (humantime == 1) {
    332 					time_t t;
    333 					struct tm tm;
    334 
    335 					t = boottime.tv_sec + sec;
    336 					if (localtime_r(&t, &tm) != NULL) {
    337 						strftime(tbuf, sizeof(tbuf),
    338 						    "%a %b %e %H:%M:%S %Z %Y",
    339 						     &tm);
    340 						printf("%s", tbuf);
    341 					}
    342 				} else if (humantime > 1) {
    343 					const char *fp = fmtydhmsf(tbuf,
    344 					    sizeof(tbuf), sec, fsec);
    345 					if (fp) {
    346 						printf("%s", fp);
    347 					}
    348 				} else if (!quiet) {
    349 					printf(" %5jd.", sec);
    350 					pnsec(nsec, fsec, scale);
    351 				}
    352 				if (deltas) {
    353 					struct timespec nt = { sec, nsec };
    354 					struct timespec dt;
    355 
    356 					timespecsub(&nt, &lasttime, &dt);
    357 					if (humantime || !quiet)
    358 						printf(" ");
    359 					printf("<% 4jd.%06ld>", (intmax_t)
    360 					    dt.tv_sec, (dt.tv_nsec+499) / 1000);
    361 					lasttime = nt;
    362 				}
    363 				if (!quiet || deltas)
    364 					printf("] ");
    365 #endif
    366 				continue;
    367 			case ' ':
    368 				if (!tstamp)
    369 					continue;
    370 				/*FALLTHROUGH*/
    371 			default:
    372 				if (tstamp) {
    373 				    ADDC(ch);
    374 #ifndef SMALL
    375 				    if (ch == '.')
    376 					frac = true;
    377 #endif
    378 				    continue;
    379 				}
    380 				if (log)
    381 					continue;
    382 				break;
    383 			}
    384 			newl = 0;
    385 		}
    386 		newl = ch == '\n';
    387 		(void)vis(buf, ch, VIS_NOSLASH, 0);
    388 #ifndef SMALL
    389 		if (buf[1] == 0)
    390 			(void)putchar(buf[0]);
    391 		else
    392 #endif
    393 			(void)printf("%s", buf);
    394 	}
    395 	if (!newl)
    396 		(void)putchar('\n');
    397 	return EXIT_SUCCESS;
    398 }
    399 
    400 #ifndef SMALL
    401 static void
    402 usage(void)
    403 {
    404 
    405 	(void)fprintf(stderr, "Usage: %s [-dTt] [-M core] [-N system]\n",
    406 		getprogname());
    407 	exit(EXIT_FAILURE);
    408 }
    409 #endif
    410