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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#if 0 31#ifndef lint 32static const char copyright[] = 33"@(#) Copyright (c) 1991, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35#endif /* not lint */ 36 37#ifndef lint 38static const char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93"; 39#endif /* not lint */ 40#endif 41#include <sys/cdefs.h>
|
42__FBSDID("$FreeBSD: head/sbin/dmesg/dmesg.c 227081 2011-11-04 13:36:02Z ed $");
|
42__FBSDID("$FreeBSD: head/sbin/dmesg/dmesg.c 250430 2013-05-10 03:42:48Z eadler $"); |
43 44#include <sys/types.h> 45#include <sys/msgbuf.h> 46#include <sys/sysctl.h> 47 48#include <ctype.h> 49#include <err.h> 50#include <errno.h> 51#include <fcntl.h> 52#include <kvm.h> 53#include <limits.h> 54#include <locale.h> 55#include <nlist.h> 56#include <stdio.h>
|
57#include <stdbool.h> |
58#include <stdlib.h> 59#include <string.h> 60#include <unistd.h> 61#include <vis.h> 62#include <sys/syslog.h> 63 64static struct nlist nl[] = { 65#define X_MSGBUF 0 66 { "_msgbufp", 0, 0, 0, 0 }, 67 { NULL, 0, 0, 0, 0 }, 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, *visbp; 80 kvm_t *kd; 81 size_t buflen, bufpos; 82 long pri;
|
82 int all, ch;
|
83 int ch, clear; 84 bool all; |
85
|
84 all = 0;
|
86 all = false; 87 clear = false; |
88 (void) setlocale(LC_CTYPE, ""); 89 memf = nlistf = NULL;
|
87 while ((ch = getopt(argc, argv, "aM:N:")) != -1)
|
90 while ((ch = getopt(argc, argv, "acM:N:")) != -1) |
91 switch(ch) { 92 case 'a':
|
90 all++;
|
93 all = true; |
94 break;
|
95 case 'c': 96 clear = true; 97 break; |
98 case 'M': 99 memf = optarg; 100 break; 101 case 'N': 102 nlistf = optarg; 103 break; 104 case '?': 105 default: 106 usage(); 107 } 108 argc -= optind; 109 if (argc != 0) 110 usage(); 111 112 if (memf == NULL) { 113 /* 114 * Running kernel. Use sysctl. This gives an unwrapped 115 * buffer as a side effect. 116 */ 117 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) 118 err(1, "sysctl kern.msgbuf"); 119 if ((bp = malloc(buflen + 2)) == NULL) 120 errx(1, "malloc failed"); 121 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) 122 err(1, "sysctl kern.msgbuf"); 123 } else { 124 /* Read in kernel message buffer and do sanity checks. */ 125 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 126 if (kd == NULL) 127 exit (1); 128 if (kvm_nlist(kd, nl) == -1) 129 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 130 if (nl[X_MSGBUF].n_type == 0) 131 errx(1, "%s: msgbufp not found", 132 nlistf ? nlistf : "namelist"); 133 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 134 errx(1, "kvm_read: %s", kvm_geterr(kd)); 135 if (cur.msg_magic != MSG_MAGIC) 136 errx(1, "kernel message buffer has different magic " 137 "number"); 138 if ((bp = malloc(cur.msg_size + 2)) == NULL) 139 errx(1, "malloc failed"); 140 141 /* Unwrap the circular buffer to start from the oldest data. */ 142 bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq); 143 if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp, 144 cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos)) 145 errx(1, "kvm_read: %s", kvm_geterr(kd)); 146 if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr, 147 &bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos) 148 errx(1, "kvm_read: %s", kvm_geterr(kd)); 149 kvm_close(kd); 150 buflen = cur.msg_size; 151 } 152 153 /* 154 * Ensure that the buffer ends with a newline and a \0 to avoid 155 * complications below. We left space above. 156 */ 157 if (buflen == 0 || bp[buflen - 1] != '\n') 158 bp[buflen++] = '\n'; 159 bp[buflen] = '\0'; 160 161 if ((visbp = malloc(4 * buflen + 1)) == NULL) 162 errx(1, "malloc failed"); 163 164 /* 165 * The message buffer is circular, but has been unwrapped so that 166 * the oldest data comes first. The data will be preceded by \0's 167 * if the message buffer was not full. 168 */ 169 p = bp; 170 ep = &bp[buflen]; 171 if (*p == '\0') { 172 /* Strip leading \0's */ 173 while (*p == '\0') 174 p++; 175 } else if (!all) { 176 /* Skip the first line, since it is probably incomplete. */ 177 p = memchr(p, '\n', ep - p); 178 p++; 179 } 180 for (; p < ep; p = nextp) { 181 nextp = memchr(p, '\n', ep - p); 182 nextp++; 183 184 /* Skip ^<[0-9]+> syslog sequences. */ 185 if (*p == '<' && isdigit(*(p+1))) { 186 errno = 0; 187 pri = strtol(p + 1, &q, 10); 188 if (*q == '>' && pri >= 0 && pri < INT_MAX && 189 errno == 0) { 190 if (LOG_FAC(pri) != LOG_KERN && !all) 191 continue; 192 p = q + 1; 193 } 194 } 195 196 (void)strvisx(visbp, p, nextp - p, 0); 197 (void)printf("%s", visbp); 198 }
|
199 if (clear) 200 if (sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof(int))) 201 err(1, "sysctl kern.msgbuf_clear"); 202 |
203 exit(0); 204} 205 206void 207usage(void) 208{
|
199 (void)fprintf(stderr, "usage: dmesg [-a] [-M core [-N system]]\n");
|
209 fprintf(stderr, "usage: dmesg [-ac] [-M core [-N system]]\n"); |
210 exit(1); 211}
|