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