148128Sbostic /*- 248966Sbostic * Copyright (c) 1991 The Regents of the University of California. 348128Sbostic * All rights reserved. 448128Sbostic * 548966Sbostic * %sccs.include.redist.c% 622058Sdist */ 722058Sdist 822058Sdist #ifndef lint 948128Sbostic char copyright[] = 1048966Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 1148128Sbostic All rights reserved.\n"; 1248128Sbostic #endif /* not lint */ 1322058Sdist 1448128Sbostic #ifndef lint 15*59445Sbostic static char sccsid[] = "@(#)dmesg.c 5.15 (Berkeley) 04/28/93"; 1648128Sbostic #endif /* not lint */ 1748128Sbostic 1848966Sbostic #include <sys/cdefs.h> 196019Swnj #include <sys/msgbuf.h> 20*59445Sbostic 2153665Sbostic #include <fcntl.h> 22*59445Sbostic #include <kvm.h> 2353665Sbostic #include <limits.h> 2437263Sbostic #include <nlist.h> 25*59445Sbostic #include <stdio.h> 2648966Sbostic #include <stdlib.h> 27*59445Sbostic #include <time.h> 2853665Sbostic #include <unistd.h> 2953665Sbostic #include <vis.h> 306019Swnj 3148966Sbostic struct nlist nl[] = { 3248966Sbostic #define X_MSGBUF 0 3352479Skarels { "_msgbufp" }, 3448966Sbostic { NULL }, 356019Swnj }; 366019Swnj 3753665Sbostic void usage __P((void)); 3848966Sbostic 3953665Sbostic #define KREAD(addr, var) \ 4053677Sbostic kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 4153665Sbostic 4253665Sbostic int 436019Swnj main(argc, argv) 4448966Sbostic int argc; 4553665Sbostic char *argv[]; 466019Swnj { 4748966Sbostic register int ch, newl, skip; 4848966Sbostic register char *p, *ep; 4952479Skarels struct msgbuf *bufp, cur; 5052241Sbostic char *memf, *nlistf; 5153665Sbostic kvm_t *kd; 5253665Sbostic char buf[_POSIX2_LINE_MAX]; 536019Swnj 5452241Sbostic memf = nlistf = NULL; 5548966Sbostic while ((ch = getopt(argc, argv, "M:N:")) != EOF) 5648966Sbostic switch(ch) { 5748966Sbostic case 'M': 5852241Sbostic memf = optarg; 596019Swnj break; 6048966Sbostic case 'N': 6152241Sbostic nlistf = optarg; 6248966Sbostic break; 6348966Sbostic case '?': 6448966Sbostic default: 6548966Sbostic usage(); 666019Swnj } 6748966Sbostic argc -= optind; 6848966Sbostic argv += optind; 6948966Sbostic 7052241Sbostic /* 7152241Sbostic * Discard setgid privileges if not the running kernel so that bad 7252241Sbostic * guys can't print interesting stuff from kernel memory. 7352241Sbostic */ 7452241Sbostic if (memf != NULL || nlistf != NULL) 7552241Sbostic setgid(getgid()); 7652241Sbostic 7748966Sbostic /* Read in kernel message buffer, do sanity checks. */ 7853665Sbostic buf[0] = 0; 7953665Sbostic kd = kvm_open(nlistf, memf, NULL, O_RDONLY, buf); 8053665Sbostic if (kd == NULL) 81*59445Sbostic errx(1, "kvm_open: %s", buf); 8253665Sbostic if (kvm_nlist(kd, nl) == -1) 83*59445Sbostic errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 8448966Sbostic if (nl[X_MSGBUF].n_type == 0) 85*59445Sbostic errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist"); 8653665Sbostic if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 87*59445Sbostic errx(1, "kvm_read: %s", kvm_geterr(kd)); 8853665Sbostic kvm_close(kd); 8948966Sbostic if (cur.msg_magic != MSG_MAGIC) 90*59445Sbostic errx(1, "magic number incorrect"); 9148966Sbostic if (cur.msg_bufx >= MSG_BSIZE) 9248966Sbostic cur.msg_bufx = 0; 9348966Sbostic 9448966Sbostic /* 9548966Sbostic * The message buffer is circular; start at the read pointer, and 9648966Sbostic * go to the write pointer - 1. 9748966Sbostic */ 9848966Sbostic p = cur.msg_bufc + cur.msg_bufx; 9948966Sbostic ep = cur.msg_bufc + cur.msg_bufx - 1; 10048966Sbostic for (newl = skip = 0; p != ep; ++p) { 10148966Sbostic if (p == cur.msg_bufc + MSG_BSIZE) 10248966Sbostic p = cur.msg_bufc; 10348966Sbostic ch = *p; 10448966Sbostic /* Skip "\n<.*>" syslog sequences. */ 10548966Sbostic if (skip) { 10648966Sbostic if (ch == '>') 10748966Sbostic newl = skip = 0; 10848966Sbostic continue; 10948966Sbostic } 11048966Sbostic if (newl && ch == '<') { 11148966Sbostic skip = 1; 11248966Sbostic continue; 11348966Sbostic } 11448966Sbostic if (ch == '\0') 11548966Sbostic continue; 11653665Sbostic newl = ch == '\n'; 117*59445Sbostic (void)vis(buf, ch, 0, 0); 11853665Sbostic if (buf[1] == 0) 119*59445Sbostic (void)putchar(buf[0]); 12053665Sbostic else 121*59445Sbostic (void)printf("%s", buf); 12248966Sbostic } 12348966Sbostic if (!newl) 12448966Sbostic (void)putchar('\n'); 12548966Sbostic exit(0); 1266019Swnj } 1276019Swnj 12848966Sbostic void 12948966Sbostic usage() 13048966Sbostic { 13148966Sbostic (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); 13248966Sbostic exit(1); 1336019Swnj } 134