1 /* $OpenBSD: dmesg.c,v 1.13 2001/07/07 18:26:11 deraadt 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.13 2001/07/07 18:26:11 deraadt Exp $"; 48 #endif 49 #endif /* not lint */ 50 51 #include <sys/param.h> 52 #include <sys/msgbuf.h> 53 #include <sys/sysctl.h> 54 55 #include <err.h> 56 #include <fcntl.h> 57 #include <kvm.h> 58 #include <limits.h> 59 #include <nlist.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <time.h> 64 #include <unistd.h> 65 #include <vis.h> 66 67 struct nlist nl[] = { 68 #define X_MSGBUF 0 69 { "_msgbufp" }, 70 { NULL }, 71 }; 72 73 void usage __P((void)); 74 75 #define KREAD(addr, var) \ 76 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 77 78 int 79 main(argc, argv) 80 int argc; 81 char *argv[]; 82 { 83 register int ch, newl, skip, i; 84 register char *p; 85 struct msgbuf cur; 86 char *memf, *nlistf, *bufdata; 87 char buf[5]; 88 89 memf = nlistf = NULL; 90 while ((ch = getopt(argc, argv, "M:N:")) != -1) 91 switch(ch) { 92 case 'M': 93 memf = optarg; 94 break; 95 case 'N': 96 nlistf = optarg; 97 break; 98 case '?': 99 default: 100 usage(); 101 } 102 argc -= optind; 103 argv += optind; 104 105 /* 106 * Discard setgid privileges if not the running kernel so that bad 107 * guys can't print interesting stuff from kernel memory. 108 */ 109 if (memf == NULL && nlistf == NULL) { 110 int mib[2], msgbufsize; 111 size_t len; 112 113 mib[0] = CTL_KERN; 114 mib[1] = KERN_MSGBUFSIZE; 115 len = sizeof(msgbufsize); 116 if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0)) 117 err(1, "sysctl: KERN_MSGBUFSIZE"); 118 119 msgbufsize += sizeof(struct msgbuf) - 1; 120 bufdata = malloc(msgbufsize); 121 if (bufdata == NULL) 122 errx(1, "couldn't allocate space for buffer data"); 123 124 memset(bufdata, 0, msgbufsize); 125 mib[1] = KERN_MSGBUF; 126 len = msgbufsize; 127 if (sysctl(mib, 2, bufdata, &len, NULL, 0)) 128 err(1, "sysctl: KERN_MSGBUF"); 129 130 memcpy(&cur, bufdata, sizeof(cur)); 131 bufdata = ((struct msgbuf *)bufdata)->msg_bufc; 132 } else { 133 #ifndef NOKVM 134 struct msgbuf *bufp; 135 kvm_t *kd; 136 137 /* Read in kernel message buffer, do sanity checks. */ 138 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, 139 "dmesg")) == NULL) 140 return (1); 141 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", 146 nlistf ? nlistf : "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 (KREAD((long)bufp, cur)) 151 errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd), 152 (unsigned long)bufp); 153 if (cur.msg_magic != MSG_MAGIC) 154 errx(1, "magic number incorrect"); 155 bufdata = malloc(cur.msg_bufs); 156 if (bufdata == NULL) 157 errx(1, "couldn't allocate space for buffer data"); 158 if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, 159 cur.msg_bufs) != cur.msg_bufs) 160 errx(1, "kvm_read: %s", kvm_geterr(kd)); 161 kvm_close(kd); 162 #endif 163 } 164 165 if (cur.msg_bufx >= cur.msg_bufs) 166 cur.msg_bufx = 0; 167 /* 168 * The message buffer is circular; start at the read pointer, and 169 * go to the write pointer - 1. 170 */ 171 for (newl = skip = i = 0, p = bufdata + cur.msg_bufx; 172 i < cur.msg_bufs; i++, p++) { 173 if (p == bufdata + cur.msg_bufs) 174 p = bufdata; 175 ch = *p; 176 /* Skip "\n<.*>" syslog sequences. */ 177 if (skip) { 178 if (ch == '>') 179 newl = skip = 0; 180 continue; 181 } 182 if (newl && ch == '<') { 183 skip = 1; 184 continue; 185 } 186 if (ch == '\0') 187 continue; 188 newl = ch == '\n'; 189 (void)vis(buf, ch, 0, 0); 190 if (buf[1] == 0) 191 (void)putchar(buf[0]); 192 else 193 (void)printf("%s", buf); 194 } 195 if (!newl) 196 (void)putchar('\n'); 197 return (0); 198 } 199 200 void 201 usage() 202 { 203 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); 204 exit(1); 205 } 206