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/msgbuf.h> 49 50 #include <err.h> 51 #include <fcntl.h> 52 #include <kvm.h> 53 #include <locale.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <unistd.h> 57 #include <vis.h> 58 #include <sys/syslog.h> 59 60 struct nlist nl[] = { 61 #define X_MSGBUF 0 62 { "_msgbufp" }, 63 { NULL }, 64 }; 65 66 void usage __P((void)); 67 68 #define KREAD(addr, var) \ 69 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 70 71 int 72 main(argc, argv) 73 int argc; 74 char *argv[]; 75 { 76 int ch, newl, skip; 77 char *p, *ep; 78 struct msgbuf *bufp, cur; 79 char *bp, *memf, *nlistf; 80 kvm_t *kd; 81 char buf[5]; 82 int all = 0; 83 int pri; 84 85 (void) setlocale(LC_CTYPE, ""); 86 memf = nlistf = NULL; 87 while ((ch = getopt(argc, argv, "aM:N:")) != -1) 88 switch(ch) { 89 case 'a': 90 all++; 91 break; 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 setgid(getgid()); 111 112 /* Read in kernel message buffer, do sanity checks. */ 113 if ((kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg")) == NULL) 114 exit (1); 115 if (kvm_nlist(kd, nl) == -1) 116 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 117 if (nl[X_MSGBUF].n_type == 0) 118 errx(1, "%s: msgbufp not found", nlistf ? nlistf : "namelist"); 119 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 120 errx(1, "kvm_read: %s", kvm_geterr(kd)); 121 if (cur.msg_magic != MSG_MAGIC) 122 errx(1, "kernel message buffer has different magic number"); 123 bp = malloc(cur.msg_size); 124 if (!bp) 125 errx(1, "malloc failed"); 126 if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) != 127 cur.msg_size) 128 errx(1, "kvm_read: %s", kvm_geterr(kd)); 129 if (cur.msg_bufx >= cur.msg_size) 130 cur.msg_bufx = 0; 131 kvm_close(kd); 132 133 /* 134 * The message buffer is circular. If the buffer has wrapped, the 135 * write pointer points to the oldest data. Otherwise, the write 136 * pointer points to \0's following the data. Read the entire 137 * buffer starting at the write pointer and ignore nulls so that 138 * we effectively start at the oldest data. 139 */ 140 p = bp + cur.msg_bufx; 141 ep = (cur.msg_bufx == 0 ? bp + cur.msg_size : p); 142 newl = skip = 0; 143 do { 144 if (p == bp + cur.msg_size) 145 p = bp; 146 ch = *p; 147 /* Skip "\n<.*>" syslog sequences. */ 148 if (skip) { 149 if (ch == '\n') { 150 skip = 0; 151 newl = 1; 152 } if (ch == '>') { 153 if (LOG_FAC(pri) == LOG_KERN || all) 154 newl = skip = 0; 155 } else if (ch >= '0' && ch <= '9') { 156 pri *= 10; 157 pri += ch - '0'; 158 } 159 continue; 160 } 161 if (newl && ch == '<') { 162 pri = 0; 163 skip = 1; 164 continue; 165 } 166 if (ch == '\0') 167 continue; 168 newl = ch == '\n'; 169 (void)vis(buf, ch, 0, 0); 170 if (buf[1] == 0) 171 (void)putchar(buf[0]); 172 else 173 (void)printf("%s", buf); 174 } while (++p != ep); 175 if (!newl) 176 (void)putchar('\n'); 177 exit(0); 178 } 179 180 void 181 usage() 182 { 183 (void)fprintf(stderr, "usage: dmesg [-M core] [-N system]\n"); 184 exit(1); 185 } 186