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 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)dmesg.c 8.1 (Berkeley) 6/5/93 35 * $FreeBSD: src/sbin/dmesg/dmesg.c,v 1.11.2.3 2001/08/08 22:32:15 obrien Exp $ 36 * $DragonFly: src/sbin/dmesg/dmesg.c,v 1.2 2003/06/17 04:27:32 dillon Exp $ 37 */ 38 39 #include <sys/types.h> 40 #include <sys/msgbuf.h> 41 #include <sys/sysctl.h> 42 43 #include <err.h> 44 #include <fcntl.h> 45 #include <kvm.h> 46 #include <locale.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <unistd.h> 50 #include <vis.h> 51 #include <sys/syslog.h> 52 53 struct nlist nl[] = { 54 #define X_MSGBUF 0 55 { "_msgbufp" }, 56 { NULL }, 57 }; 58 59 void usage __P((void)); 60 61 #define KREAD(addr, var) \ 62 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 63 64 int 65 main(argc, argv) 66 int argc; 67 char *argv[]; 68 { 69 int ch, newl, skip; 70 char *p, *ep; 71 struct msgbuf *bufp, cur; 72 char *bp, *memf, *nlistf; 73 kvm_t *kd; 74 char buf[5]; 75 int all = 0; 76 int pri; 77 size_t buflen; 78 int bufpos; 79 80 (void) setlocale(LC_CTYPE, ""); 81 memf = nlistf = NULL; 82 while ((ch = getopt(argc, argv, "aM:N:")) != -1) 83 switch(ch) { 84 case 'a': 85 all++; 86 break; 87 case 'M': 88 memf = optarg; 89 break; 90 case 'N': 91 nlistf = optarg; 92 break; 93 case '?': 94 default: 95 usage(); 96 } 97 argc -= optind; 98 argv += optind; 99 100 if (memf == NULL && nlistf == NULL) { 101 /* Running kernel. Use sysctl. */ 102 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) 103 err(1, "sysctl kern.msgbuf"); 104 if ((bp = malloc(buflen)) == NULL) 105 errx(1, "malloc failed"); 106 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) 107 err(1, "sysctl kern.msgbuf"); 108 /* We get a dewrapped buffer using sysctl. */ 109 bufpos = 0; 110 } else { 111 /* Read in kernel message buffer, do sanity checks. */ 112 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 113 if (kd == 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", 119 nlistf ? nlistf : "namelist"); 120 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 121 errx(1, "kvm_read: %s", kvm_geterr(kd)); 122 if (cur.msg_magic != MSG_MAGIC) 123 errx(1, "kernel message buffer has different magic " 124 "number"); 125 bp = malloc(cur.msg_size); 126 if (!bp) 127 errx(1, "malloc failed"); 128 if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) != 129 cur.msg_size) 130 errx(1, "kvm_read: %s", kvm_geterr(kd)); 131 kvm_close(kd); 132 buflen = cur.msg_size; 133 bufpos = cur.msg_bufx; 134 if (bufpos >= buflen) 135 bufpos = 0; 136 } 137 138 /* 139 * The message buffer is circular. If the buffer has wrapped, the 140 * write pointer points to the oldest data. Otherwise, the write 141 * pointer points to \0's following the data. Read the entire 142 * buffer starting at the write pointer and ignore nulls so that 143 * we effectively start at the oldest data. 144 */ 145 p = bp + bufpos; 146 ep = (bufpos == 0 ? bp + buflen : p); 147 newl = skip = 0; 148 do { 149 if (p == bp + buflen) 150 p = bp; 151 ch = *p; 152 /* Skip "\n<.*>" syslog sequences. */ 153 if (skip) { 154 if (ch == '\n') { 155 skip = 0; 156 newl = 1; 157 } if (ch == '>') { 158 if (LOG_FAC(pri) == LOG_KERN || all) 159 newl = skip = 0; 160 } else if (ch >= '0' && ch <= '9') { 161 pri *= 10; 162 pri += ch - '0'; 163 } 164 continue; 165 } 166 if (newl && ch == '<') { 167 pri = 0; 168 skip = 1; 169 continue; 170 } 171 if (ch == '\0') 172 continue; 173 newl = ch == '\n'; 174 (void)vis(buf, ch, 0, 0); 175 if (buf[1] == 0) 176 (void)putchar(buf[0]); 177 else 178 (void)printf("%s", buf); 179 } while (++p != ep); 180 if (!newl) 181 (void)putchar('\n'); 182 exit(0); 183 } 184 185 void 186 usage() 187 { 188 (void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n"); 189 exit(1); 190 } 191