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/types.h> 49 #include <sys/msgbuf.h> 50 #include <sys/sysctl.h> 51 52 #include <err.h> 53 #include <fcntl.h> 54 #include <kvm.h> 55 #include <locale.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 #include <vis.h> 60 #include <sys/syslog.h> 61 62 struct nlist nl[] = { 63 #define X_MSGBUF 0 64 { "_msgbufp" }, 65 { NULL }, 66 }; 67 68 void usage(void) __dead2; 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 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 size_t buflen; 85 int bufpos; 86 87 (void) setlocale(LC_CTYPE, ""); 88 memf = nlistf = NULL; 89 while ((ch = getopt(argc, argv, "aM:N:")) != -1) 90 switch(ch) { 91 case 'a': 92 all++; 93 break; 94 case 'M': 95 memf = optarg; 96 break; 97 case 'N': 98 nlistf = optarg; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 if (memf == NULL && nlistf == NULL) { 108 /* Running kernel. Use sysctl. */ 109 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) 110 err(1, "sysctl kern.msgbuf"); 111 if ((bp = malloc(buflen)) == NULL) 112 errx(1, "malloc failed"); 113 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) 114 err(1, "sysctl kern.msgbuf"); 115 /* We get a dewrapped buffer using sysctl. */ 116 bufpos = 0; 117 } else { 118 /* Read in kernel message buffer, do sanity checks. */ 119 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 120 if (kd == NULL) 121 exit (1); 122 if (kvm_nlist(kd, nl) == -1) 123 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 124 if (nl[X_MSGBUF].n_type == 0) 125 errx(1, "%s: msgbufp not found", 126 nlistf ? nlistf : "namelist"); 127 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 128 errx(1, "kvm_read: %s", kvm_geterr(kd)); 129 if (cur.msg_magic != MSG_MAGIC) 130 errx(1, "kernel message buffer has different magic " 131 "number"); 132 bp = malloc(cur.msg_size); 133 if (!bp) 134 errx(1, "malloc failed"); 135 if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) != 136 cur.msg_size) 137 errx(1, "kvm_read: %s", kvm_geterr(kd)); 138 kvm_close(kd); 139 buflen = cur.msg_size; 140 bufpos = cur.msg_bufx; 141 if (bufpos >= buflen) 142 bufpos = 0; 143 } 144 145 /* 146 * The message buffer is circular. If the buffer has wrapped, the 147 * write pointer points to the oldest data. Otherwise, the write 148 * pointer points to \0's following the data. Read the entire 149 * buffer starting at the write pointer and ignore nulls so that 150 * we effectively start at the oldest data. 151 */ 152 p = bp + bufpos; 153 ep = (bufpos == 0 ? bp + buflen : p); 154 newl = skip = 0; 155 do { 156 if (p == bp + buflen) 157 p = bp; 158 ch = *p; 159 /* Skip "\n<.*>" syslog sequences. */ 160 if (skip) { 161 if (ch == '\n') { 162 skip = 0; 163 newl = 1; 164 } if (ch == '>') { 165 if (LOG_FAC(pri) == LOG_KERN || all) 166 newl = skip = 0; 167 } else if (ch >= '0' && ch <= '9') { 168 pri *= 10; 169 pri += ch - '0'; 170 } 171 continue; 172 } 173 if (newl && ch == '<') { 174 pri = 0; 175 skip = 1; 176 continue; 177 } 178 if (ch == '\0') 179 continue; 180 newl = ch == '\n'; 181 (void)vis(buf, ch, 0, 0); 182 if (buf[1] == 0) 183 (void)putchar(buf[0]); 184 else 185 (void)printf("%s", buf); 186 } while (++p != ep); 187 if (!newl) 188 (void)putchar('\n'); 189 exit(0); 190 } 191 192 void 193 usage(void) 194 { 195 (void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n"); 196 exit(1); 197 } 198