1 /* $OpenBSD: dmesg.c,v 1.5 1997/03/29 20:28:20 tholo Exp $ */ 2 /* $NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1991, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93"; 46 #else 47 static char rcsid[] = "$OpenBSD: dmesg.c,v 1.5 1997/03/29 20:28:20 tholo Exp $"; 48 #endif 49 #endif /* not lint */ 50 51 #include <sys/cdefs.h> 52 #include <sys/msgbuf.h> 53 54 #include <err.h> 55 #include <fcntl.h> 56 #include <kvm.h> 57 #include <limits.h> 58 #include <nlist.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <time.h> 62 #include <unistd.h> 63 #include <vis.h> 64 65 struct nlist nl[] = { 66 #define X_MSGBUF 0 67 { "_msgbufp" }, 68 { NULL }, 69 }; 70 71 void usage __P((void)); 72 73 #define KREAD(addr, var) \ 74 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 75 76 int 77 main(argc, argv) 78 int argc; 79 char *argv[]; 80 { 81 register int ch, newl, skip; 82 register char *p, *ep; 83 struct msgbuf *bufp, cur; 84 char *memf, *nlistf; 85 kvm_t *kd; 86 char buf[5]; 87 88 memf = nlistf = NULL; 89 while ((ch = getopt(argc, argv, "M:N:")) != -1) 90 switch(ch) { 91 case 'M': 92 memf = optarg; 93 break; 94 case 'N': 95 nlistf = optarg; 96 break; 97 case '?': 98 default: 99 usage(); 100 } 101 argc -= optind; 102 argv += optind; 103 104 /* 105 * Discard setgid privileges if not the running kernel so that bad 106 * guys can't print interesting stuff from kernel memory. 107 */ 108 if (memf != NULL || nlistf != NULL) { 109 setegid(getgid()); 110 setgid(getgid()); 111 } 112 113 /* Read in kernel message buffer, do sanity checks. */ 114 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL) 115 exit (1); 116 if (kvm_nlist(kd, nl) == -1) 117 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 118 if (nl[X_MSGBUF].n_type == 0) 119 errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist"); 120 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 121 errx(1, "kvm_read: %s", kvm_geterr(kd)); 122 kvm_close(kd); 123 if (cur.msg_magic != MSG_MAGIC) 124 errx(1, "magic number incorrect"); 125 if (cur.msg_bufx >= MSG_BSIZE) 126 cur.msg_bufx = 0; 127 128 /* 129 * The message buffer is circular; start at the read pointer, and 130 * go to the write pointer - 1. 131 */ 132 p = ep = cur.msg_bufc + (cur.msg_bufx - 1 + MSG_BSIZE) % MSG_BSIZE; 133 newl = skip = 0; 134 do { 135 if (++p == cur.msg_bufc + MSG_BSIZE) 136 p = cur.msg_bufc; 137 ch = *p; 138 /* Skip "\n<.*>" syslog sequences. */ 139 if (skip) { 140 if (ch == '>') 141 newl = skip = 0; 142 continue; 143 } 144 if (newl && ch == '<') { 145 skip = 1; 146 continue; 147 } 148 if (ch == '\0') 149 continue; 150 newl = ch == '\n'; 151 (void)vis(buf, ch, 0, 0); 152 if (buf[1] == 0) 153 (void)putchar(buf[0]); 154 else 155 (void)printf("%s", buf); 156 } while (p != ep); 157 if (!newl) 158 (void)putchar('\n'); 159 exit(0); 160 } 161 162 void 163 usage() 164 { 165 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); 166 exit(1); 167 } 168