1 /* $NetBSD: dmesg.c,v 1.22 2003/08/07 10:04:13 agc 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\n\ 34 The Regents of the University of California. All rights reserved.\n"); 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.22 2003/08/07 10:04:13 agc 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 <fcntl.h> 51 #include <kvm.h> 52 #include <nlist.h> 53 #include <stdio.h> 54 #include <stddef.h> 55 #include <stdlib.h> 56 #include <unistd.h> 57 #include <vis.h> 58 59 #ifndef SMALL 60 struct nlist nl[] = { 61 #define X_MSGBUF 0 62 { "_msgbufp" }, 63 { NULL }, 64 }; 65 #endif 66 67 int main(int, char *[]); 68 void usage(void); 69 70 #define KREAD(addr, var) \ 71 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 72 73 int 74 main(int argc, char *argv[]) 75 { 76 struct kern_msgbuf cur; 77 int ch, newl, skip, i; 78 char *p, *bufdata; 79 #ifndef SMALL 80 char *memf, *nlistf; 81 #endif 82 char buf[5]; 83 84 #ifndef SMALL 85 memf = nlistf = NULL; 86 while ((ch = getopt(argc, argv, "M:N:")) != -1) 87 switch(ch) { 88 case 'M': 89 memf = optarg; 90 break; 91 case 'N': 92 nlistf = optarg; 93 break; 94 case '?': 95 default: 96 usage(); 97 } 98 argc -= optind; 99 argv += optind; 100 101 if (memf == NULL) { 102 #endif 103 size_t size; 104 int mib[2]; 105 106 mib[0] = CTL_KERN; 107 mib[1] = KERN_MSGBUF; 108 109 if (sysctl(mib, 2, NULL, &size, NULL, 0) == -1) 110 errx(1, "can't get size of msgbuf"); 111 112 if ((bufdata = malloc(size)) == NULL) 113 err(1, "couldn't allocate space for buffer data"); 114 115 if (sysctl(mib, 2, bufdata, &size, NULL, 0) == -1) 116 err(1, "can't get msgbuf"); 117 118 /* make a dummy struct msgbuf for the display logic */ 119 cur.msg_bufx = cur.msg_bufs = size; 120 #ifndef SMALL 121 } else { 122 kvm_t *kd; 123 struct kern_msgbuf *bufp; 124 125 /* 126 * Read in message buffer header and data, and do sanity 127 * checks. 128 */ 129 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 130 if (kd == NULL) 131 exit (1); 132 if (kvm_nlist(kd, nl) == -1) 133 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 134 if (nl[X_MSGBUF].n_type == 0) 135 errx(1, "%s: msgbufp not found", nlistf ? nlistf : 136 "namelist"); 137 if (KREAD(nl[X_MSGBUF].n_value, bufp)) 138 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd), 139 nl[X_MSGBUF].n_value); 140 if (kvm_read(kd, (long)bufp, &cur, 141 offsetof(struct kern_msgbuf, msg_bufc)) != 142 offsetof(struct kern_msgbuf, msg_bufc)) 143 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd), 144 (unsigned long)bufp); 145 if (cur.msg_magic != MSG_MAGIC) 146 errx(1, "magic number incorrect"); 147 bufdata = malloc(cur.msg_bufs); 148 if (bufdata == NULL) 149 errx(1, "couldn't allocate space for buffer data"); 150 if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, 151 cur.msg_bufs) != cur.msg_bufs) 152 errx(1, "kvm_read: %s", kvm_geterr(kd)); 153 kvm_close(kd); 154 if (cur.msg_bufx >= cur.msg_bufs) 155 cur.msg_bufx = 0; 156 } 157 #endif 158 159 /* 160 * The message buffer is circular; start at the write pointer 161 * (which points the oldest character), and go to the write 162 * pointer - 1 (which points the newest character). I.e, loop 163 * over cur.msg_bufs times. Unused area is skipped since it 164 * contains nul. 165 */ 166 for (newl = skip = i = 0, p = bufdata + cur.msg_bufx; 167 i < cur.msg_bufs; i++, p++) { 168 if (p == bufdata + cur.msg_bufs) 169 p = bufdata; 170 ch = *p; 171 /* Skip "\n<.*>" syslog sequences. */ 172 if (skip) { 173 if (ch == '>') 174 newl = skip = 0; 175 continue; 176 } 177 if (newl && ch == '<') { 178 skip = 1; 179 continue; 180 } 181 if (ch == '\0') 182 continue; 183 newl = ch == '\n'; 184 (void)vis(buf, ch, VIS_NOSLASH, 0); 185 if (buf[1] == 0) 186 (void)putchar(buf[0]); 187 else 188 (void)printf("%s", buf); 189 } 190 if (!newl) 191 (void)putchar('\n'); 192 exit(0); 193 } 194 195 void 196 usage(void) 197 { 198 199 #ifdef SMALL 200 (void)fprintf(stderr, "usage: dmesg\n"); 201 #else 202 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); 203 #endif 204 exit(1); 205 } 206