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