1 /* $OpenBSD: dmesg.c,v 1.27 2015/10/09 01:37:06 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/msgbuf.h> 35 #include <sys/sysctl.h> 36 37 #include <err.h> 38 #include <fcntl.h> 39 #include <kvm.h> 40 #include <limits.h> 41 #include <nlist.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <time.h> 46 #include <unistd.h> 47 #include <vis.h> 48 49 #ifndef NOKVM 50 struct nlist nl[] = { 51 #define X_MSGBUF 0 52 { "_msgbufp" }, 53 { NULL }, 54 }; 55 #endif 56 57 void usage(void); 58 59 #define KREAD(addr, var) \ 60 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 61 62 int 63 main(int argc, char *argv[]) 64 { 65 int ch, newl, skip, i; 66 char *p; 67 struct msgbuf cur; 68 char *memf, *nlistf, *bufdata = NULL; 69 int startupmsgs = 0; 70 char buf[5]; 71 72 memf = nlistf = NULL; 73 while ((ch = getopt(argc, argv, "sM:N:")) != -1) 74 switch(ch) { 75 case 's': 76 startupmsgs = 1; 77 break; 78 case 'M': 79 memf = optarg; 80 break; 81 case 'N': 82 nlistf = optarg; 83 break; 84 case '?': 85 default: 86 usage(); 87 } 88 argc -= optind; 89 argv += optind; 90 91 if (memf == NULL && nlistf == NULL) { 92 int mib[2], msgbufsize; 93 size_t len; 94 95 mib[0] = CTL_KERN; 96 mib[1] = startupmsgs ? KERN_CONSBUFSIZE : KERN_MSGBUFSIZE; 97 len = sizeof(msgbufsize); 98 if (sysctl(mib, 2, &msgbufsize, &len, NULL, 0)) 99 err(1, "sysctl: KERN_MSGBUFSIZE"); 100 101 msgbufsize += sizeof(struct msgbuf) - 1; 102 bufdata = calloc(1, msgbufsize); 103 if (bufdata == NULL) 104 errx(1, "couldn't allocate space for buffer data"); 105 106 mib[1] = startupmsgs ? KERN_CONSBUF : KERN_MSGBUF; 107 len = msgbufsize; 108 if (sysctl(mib, 2, bufdata, &len, NULL, 0)) 109 err(1, "sysctl: KERN_MSGBUF"); 110 111 if (pledge("stdio", NULL) == -1) 112 err(1, "pledge"); 113 114 memcpy(&cur, bufdata, sizeof(cur)); 115 bufdata = ((struct msgbuf *)bufdata)->msg_bufc; 116 } else { 117 #ifndef NOKVM 118 struct msgbuf *bufp; 119 kvm_t *kd; 120 121 /* Read in kernel message buffer, do sanity checks. */ 122 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, 123 "dmesg")) == NULL) 124 return (1); 125 126 if (pledge("stdio", NULL) == -1) 127 err(1, "pledge"); 128 129 if (kvm_nlist(kd, nl) == -1) 130 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 131 if (nl[X_MSGBUF].n_type == 0) 132 errx(1, "%s: msgbufp not found", 133 nlistf ? nlistf : "namelist"); 134 if (KREAD(nl[X_MSGBUF].n_value, bufp)) 135 errx(1, "kvm_read: %s: (0x%lx)", kvm_geterr(kd), 136 nl[X_MSGBUF].n_value); 137 if (KREAD((long)bufp, cur)) 138 errx(1, "kvm_read: %s (%0lx)", kvm_geterr(kd), 139 (unsigned long)bufp); 140 if (cur.msg_magic != MSG_MAGIC) 141 errx(1, "magic number incorrect"); 142 bufdata = malloc(cur.msg_bufs); 143 if (bufdata == NULL) 144 errx(1, "couldn't allocate space for buffer data"); 145 if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, 146 cur.msg_bufs) != cur.msg_bufs) 147 errx(1, "kvm_read: %s", kvm_geterr(kd)); 148 kvm_close(kd); 149 #endif 150 } 151 152 if (cur.msg_bufx >= cur.msg_bufs) 153 cur.msg_bufx = 0; 154 /* 155 * The message buffer is circular; start at the read pointer, and 156 * go to the write pointer - 1. 157 */ 158 for (newl = skip = i = 0, p = bufdata + cur.msg_bufx; 159 i < cur.msg_bufs; i++, p++) { 160 if (p == bufdata + cur.msg_bufs) 161 p = bufdata; 162 ch = *p; 163 /* Skip "\n<.*>" syslog sequences. */ 164 if (skip) { 165 if (ch == '>') 166 newl = skip = 0; 167 continue; 168 } 169 if (newl && ch == '<') { 170 skip = 1; 171 continue; 172 } 173 if (ch == '\0') 174 continue; 175 newl = ch == '\n'; 176 vis(buf, ch, 0, 0); 177 if (buf[1] == 0) 178 putchar(buf[0]); 179 else 180 printf("%s", buf); 181 } 182 if (!newl) 183 putchar('\n'); 184 return (0); 185 } 186 187 void 188 usage(void) 189 { 190 extern char *__progname; 191 192 fprintf(stderr, "usage: %s [-s] [-M core] [-N system]\n", __progname); 193 exit(1); 194 } 195