dmesg.c revision 125497
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#if 0 35#ifndef lint 36static const char copyright[] = 37"@(#) Copyright (c) 1991, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39#endif /* not lint */ 40 41#ifndef lint 42static const char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93"; 43#endif /* not lint */ 44#endif 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: head/sbin/dmesg/dmesg.c 125497 2004-02-05 21:07:50Z iedowse $"); 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 <nlist.h> 57#include <stdio.h> 58#include <stdlib.h> 59#include <string.h> 60#include <unistd.h> 61#include <vis.h> 62#include <sys/syslog.h> 63 64struct nlist nl[] = { 65#define X_MSGBUF 0 66 { "_msgbufp" }, 67 { NULL }, 68}; 69 70void usage(void) __dead2; 71 72#define KREAD(addr, var) \ 73 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 74 75int 76main(int argc, char *argv[]) 77{ 78 struct msgbuf *bufp, cur; 79 char *bp, *ep, *memf, *nextp, *nlistf, *p, *q; 80 kvm_t *kd; 81 size_t buflen, bufpos; 82 int all, ch, pri; 83 char buf[5]; 84 85 all = 0; 86 (void) setlocale(LC_CTYPE, ""); 87 memf = nlistf = NULL; 88 while ((ch = getopt(argc, argv, "aM:N:")) != -1) 89 switch(ch) { 90 case 'a': 91 all++; 92 break; 93 case 'M': 94 memf = optarg; 95 break; 96 case 'N': 97 nlistf = optarg; 98 break; 99 case '?': 100 default: 101 usage(); 102 } 103 argc -= optind; 104 argv += optind; 105 106 if (memf == NULL && nlistf == NULL) { 107 /* Running kernel. Use sysctl to get an unwrapped buffer. */ 108 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) 109 err(1, "sysctl kern.msgbuf"); 110 if ((bp = malloc(buflen)) == NULL) 111 errx(1, "malloc failed"); 112 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) 113 err(1, "sysctl kern.msgbuf"); 114 } else { 115 /* Read in kernel message buffer, do sanity checks. */ 116 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 117 if (kd == NULL) 118 exit (1); 119 if (kvm_nlist(kd, nl) == -1) 120 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 121 if (nl[X_MSGBUF].n_type == 0) 122 errx(1, "%s: msgbufp not found", 123 nlistf ? nlistf : "namelist"); 124 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 125 errx(1, "kvm_read: %s", kvm_geterr(kd)); 126 if (cur.msg_magic != MSG_MAGIC) 127 errx(1, "kernel message buffer has different magic " 128 "number"); 129 bp = malloc(cur.msg_size); 130 if (!bp) 131 errx(1, "malloc failed"); 132 /* Unwrap the circular buffer to start from the oldest data. */ 133 bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq); 134 if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp, 135 cur.msg_size - bufpos) != cur.msg_size - bufpos) 136 errx(1, "kvm_read: %s", kvm_geterr(kd)); 137 if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr, 138 &bp[cur.msg_size - bufpos], bufpos) != bufpos) 139 errx(1, "kvm_read: %s", kvm_geterr(kd)); 140 kvm_close(kd); 141 buflen = cur.msg_size; 142 } 143 144 /* 145 * The message buffer is circular, but has been unwrapped so that 146 * the oldest data comes first. The data will be preceded by \0's 147 * if the message buffer was not full. 148 */ 149 p = bp; 150 ep = &bp[buflen]; 151 if (*p == '\0') { 152 /* Strip leading \0's */ 153 while (p != ep && *p == '\0') 154 p++; 155 } else if (!all) { 156 /* Skip the first line, since it is probably incomplete. */ 157 p = memchr(p, '\n', ep - p); 158 if (p == NULL) 159 p = ep; 160 else 161 p++; 162 } 163 for (; p != ep; p = nextp) { 164 nextp = memchr(p, '\n', ep - p); 165 if (nextp == NULL) 166 nextp = ep; 167 else 168 nextp++; 169 /* Skip ^<[0-9]+> syslog sequences. */ 170 if (*p == '<') { 171 pri = 0; 172 for (q = p + 1; q != ep && *q >= '0' && *q <= '9'; q++) 173 pri = pri * 10 + (*q - '0'); 174 if (q != ep && *q == '>' && q != p + 1) { 175 if (LOG_FAC(pri) != LOG_KERN && !all) 176 continue; 177 p = q + 1; 178 } 179 } 180 for (; p != nextp; p++) { 181 (void)vis(buf, *p, 0, 0); 182 if (buf[1] == 0) 183 (void)putchar(buf[0]); 184 else 185 (void)printf("%s", buf); 186 } 187 if (nextp == ep && ep[-1] != '\n') 188 (void)putchar('\n'); 189 } 190 exit(0); 191} 192 193void 194usage(void) 195{ 196 (void)fprintf(stderr, "usage: dmesg [-a] [-M core] [-N system]\n"); 197 exit(1); 198} 199