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 36 static 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 42 static 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$"); 47 48 #include <sys/types.h> 49 #include <sys/msgbuf.h> 50 #include <sys/sysctl.h> 51 52 #include <err.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <kvm.h> 56 #include <limits.h> 57 #include <locale.h> 58 #include <nlist.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 #include <vis.h> 64 #include <sys/syslog.h> 65 66 struct nlist nl[] = { 67 #define X_MSGBUF 0 68 { "_msgbufp" }, 69 { NULL }, 70 }; 71 72 void usage(void) __dead2; 73 74 #define KREAD(addr, var) \ 75 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 76 77 int 78 main(int argc, char *argv[]) 79 { 80 struct msgbuf *bufp, cur; 81 char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp; 82 kvm_t *kd; 83 size_t buflen, bufpos; 84 long pri; 85 int all, ch; 86 87 all = 0; 88 (void) setlocale(LC_CTYPE, ""); 89 memf = nlistf = NULL; 90 while ((ch = getopt(argc, argv, "aM:N:")) != -1) 91 switch(ch) { 92 case 'a': 93 all++; 94 break; 95 case 'M': 96 memf = optarg; 97 break; 98 case 'N': 99 nlistf = optarg; 100 break; 101 case '?': 102 default: 103 usage(); 104 } 105 argc -= optind; 106 argv += optind; 107 108 if (memf == NULL) { 109 /* 110 * Running kernel. Use sysctl. This gives an unwrapped 111 * buffer as a side effect. 112 */ 113 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) 114 err(1, "sysctl kern.msgbuf"); 115 if ((bp = malloc(buflen + 2)) == NULL) 116 errx(1, "malloc failed"); 117 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) 118 err(1, "sysctl kern.msgbuf"); 119 } else { 120 /* Read in kernel message buffer and do sanity checks. */ 121 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 122 if (kd == NULL) 123 exit (1); 124 if (kvm_nlist(kd, nl) == -1) 125 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 126 if (nl[X_MSGBUF].n_type == 0) 127 errx(1, "%s: msgbufp not found", 128 nlistf ? nlistf : "namelist"); 129 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 130 errx(1, "kvm_read: %s", kvm_geterr(kd)); 131 if (cur.msg_magic != MSG_MAGIC) 132 errx(1, "kernel message buffer has different magic " 133 "number"); 134 if ((bp = malloc(cur.msg_size + 2)) == NULL) 135 errx(1, "malloc failed"); 136 137 /* Unwrap the circular buffer to start from the oldest data. */ 138 bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq); 139 if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp, 140 cur.msg_size - bufpos) != cur.msg_size - bufpos) 141 errx(1, "kvm_read: %s", kvm_geterr(kd)); 142 if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr, 143 &bp[cur.msg_size - bufpos], bufpos) != bufpos) 144 errx(1, "kvm_read: %s", kvm_geterr(kd)); 145 kvm_close(kd); 146 buflen = cur.msg_size; 147 } 148 149 /* 150 * Ensure that the buffer ends with a newline and a \0 to avoid 151 * complications below. We left space above. 152 */ 153 if (buflen == 0 || bp[buflen - 1] != '\n') 154 bp[buflen++] = '\n'; 155 bp[buflen] = '\0'; 156 157 if ((visbp = malloc(4 * buflen + 1)) == NULL) 158 errx(1, "malloc failed"); 159 160 /* 161 * The message buffer is circular, but has been unwrapped so that 162 * the oldest data comes first. The data will be preceded by \0's 163 * if the message buffer was not full. 164 */ 165 p = bp; 166 ep = &bp[buflen]; 167 if (*p == '\0') { 168 /* Strip leading \0's */ 169 while (*p == '\0') 170 p++; 171 } else if (!all) { 172 /* Skip the first line, since it is probably incomplete. */ 173 p = memchr(p, '\n', ep - p) + 1; 174 } 175 for (; p < ep; p = nextp) { 176 nextp = memchr(p, '\n', ep - p) + 1; 177 178 /* Skip ^<[0-9]+> syslog sequences. */ 179 if (*p == '<') { 180 errno = 0; 181 pri = strtol(p + 1, &q, 10); 182 if (*q == '>' && pri >= 0 && pri < INT_MAX && 183 errno == 0) { 184 if (LOG_FAC(pri) != LOG_KERN && !all) 185 continue; 186 p = q + 1; 187 } 188 } 189 190 (void)strvisx(visbp, p, nextp - p, 0); 191 (void)printf("%s", visbp); 192 } 193 exit(0); 194 } 195 196 void 197 usage(void) 198 { 199 (void)fprintf(stderr, "usage: dmesg [-a] [-M core [-N system]]\n"); 200 exit(1); 201 } 202