Don't print the oldest line in the message buffer if the buffer is
full, since that line is almost always incomplete. Make the parsing of <%d> lines more strict. Also simplify the logic a little: - Start off by making the buffer linear so that we don't have to deal with it wrapping around (suggested by bde). - Process line by line rather than byte at a time.
This commit is contained in:
parent
4c3558aa82
commit
59f256ec35
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=125497
@ -56,6 +56,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <nlist.h>
|
#include <nlist.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <vis.h>
|
#include <vis.h>
|
||||||
#include <sys/syslog.h>
|
#include <sys/syslog.h>
|
||||||
@ -74,16 +75,14 @@ void usage(void) __dead2;
|
|||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ch, newl, skip;
|
|
||||||
char *p, *ep;
|
|
||||||
struct msgbuf *bufp, cur;
|
struct msgbuf *bufp, cur;
|
||||||
char *bp, *memf, *nlistf;
|
char *bp, *ep, *memf, *nextp, *nlistf, *p, *q;
|
||||||
kvm_t *kd;
|
kvm_t *kd;
|
||||||
char buf[5];
|
|
||||||
int all = 0;
|
|
||||||
int pri;
|
|
||||||
size_t buflen, bufpos;
|
size_t buflen, bufpos;
|
||||||
|
int all, ch, pri;
|
||||||
|
char buf[5];
|
||||||
|
|
||||||
|
all = 0;
|
||||||
(void) setlocale(LC_CTYPE, "");
|
(void) setlocale(LC_CTYPE, "");
|
||||||
memf = nlistf = NULL;
|
memf = nlistf = NULL;
|
||||||
while ((ch = getopt(argc, argv, "aM:N:")) != -1)
|
while ((ch = getopt(argc, argv, "aM:N:")) != -1)
|
||||||
@ -105,15 +104,13 @@ main(int argc, char *argv[])
|
|||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (memf == NULL && nlistf == NULL) {
|
if (memf == NULL && nlistf == NULL) {
|
||||||
/* Running kernel. Use sysctl. */
|
/* Running kernel. Use sysctl to get an unwrapped buffer. */
|
||||||
if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
|
if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
|
||||||
err(1, "sysctl kern.msgbuf");
|
err(1, "sysctl kern.msgbuf");
|
||||||
if ((bp = malloc(buflen)) == NULL)
|
if ((bp = malloc(buflen)) == NULL)
|
||||||
errx(1, "malloc failed");
|
errx(1, "malloc failed");
|
||||||
if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
|
if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
|
||||||
err(1, "sysctl kern.msgbuf");
|
err(1, "sysctl kern.msgbuf");
|
||||||
/* We get a dewrapped buffer using sysctl. */
|
|
||||||
bufpos = 0;
|
|
||||||
} else {
|
} else {
|
||||||
/* Read in kernel message buffer, do sanity checks. */
|
/* Read in kernel message buffer, do sanity checks. */
|
||||||
kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
|
kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
|
||||||
@ -132,58 +129,64 @@ main(int argc, char *argv[])
|
|||||||
bp = malloc(cur.msg_size);
|
bp = malloc(cur.msg_size);
|
||||||
if (!bp)
|
if (!bp)
|
||||||
errx(1, "malloc failed");
|
errx(1, "malloc failed");
|
||||||
if (kvm_read(kd, (long)cur.msg_ptr, bp, cur.msg_size) !=
|
/* Unwrap the circular buffer to start from the oldest data. */
|
||||||
cur.msg_size)
|
bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
|
||||||
|
if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
|
||||||
|
cur.msg_size - bufpos) != 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) != bufpos)
|
||||||
errx(1, "kvm_read: %s", kvm_geterr(kd));
|
errx(1, "kvm_read: %s", kvm_geterr(kd));
|
||||||
kvm_close(kd);
|
kvm_close(kd);
|
||||||
buflen = cur.msg_size;
|
buflen = cur.msg_size;
|
||||||
bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The message buffer is circular. If the buffer has wrapped, the
|
* The message buffer is circular, but has been unwrapped so that
|
||||||
* write pointer points to the oldest data. Otherwise, the write
|
* the oldest data comes first. The data will be preceded by \0's
|
||||||
* pointer points to \0's following the data. Read the entire
|
* if the message buffer was not full.
|
||||||
* buffer starting at the write pointer and ignore nulls so that
|
|
||||||
* we effectively start at the oldest data.
|
|
||||||
*/
|
*/
|
||||||
p = bp + bufpos;
|
|
||||||
ep = (bufpos == 0 ? bp + buflen : p);
|
|
||||||
newl = skip = 0;
|
|
||||||
do {
|
|
||||||
if (p == bp + buflen)
|
|
||||||
p = bp;
|
p = bp;
|
||||||
ch = *p;
|
ep = &bp[buflen];
|
||||||
/* Skip "\n<.*>" syslog sequences. */
|
if (*p == '\0') {
|
||||||
if (skip) {
|
/* Strip leading \0's */
|
||||||
if (ch == '\n') {
|
while (p != ep && *p == '\0')
|
||||||
skip = 0;
|
p++;
|
||||||
newl = 1;
|
} else if (!all) {
|
||||||
} if (ch == '>') {
|
/* Skip the first line, since it is probably incomplete. */
|
||||||
if (LOG_FAC(pri) == LOG_KERN || all)
|
p = memchr(p, '\n', ep - p);
|
||||||
newl = skip = 0;
|
if (p == NULL)
|
||||||
} else if (ch >= '0' && ch <= '9') {
|
p = ep;
|
||||||
pri *= 10;
|
else
|
||||||
pri += ch - '0';
|
p++;
|
||||||
}
|
}
|
||||||
continue;
|
for (; p != ep; p = nextp) {
|
||||||
}
|
nextp = memchr(p, '\n', ep - p);
|
||||||
if (newl && ch == '<') {
|
if (nextp == NULL)
|
||||||
|
nextp = ep;
|
||||||
|
else
|
||||||
|
nextp++;
|
||||||
|
/* Skip ^<[0-9]+> syslog sequences. */
|
||||||
|
if (*p == '<') {
|
||||||
pri = 0;
|
pri = 0;
|
||||||
skip = 1;
|
for (q = p + 1; q != ep && *q >= '0' && *q <= '9'; q++)
|
||||||
|
pri = pri * 10 + (*q - '0');
|
||||||
|
if (q != ep && *q == '>' && q != p + 1) {
|
||||||
|
if (LOG_FAC(pri) != LOG_KERN && !all)
|
||||||
continue;
|
continue;
|
||||||
|
p = q + 1;
|
||||||
}
|
}
|
||||||
if (ch == '\0')
|
}
|
||||||
continue;
|
for (; p != nextp; p++) {
|
||||||
newl = ch == '\n';
|
(void)vis(buf, *p, 0, 0);
|
||||||
(void)vis(buf, ch, 0, 0);
|
|
||||||
if (buf[1] == 0)
|
if (buf[1] == 0)
|
||||||
(void)putchar(buf[0]);
|
(void)putchar(buf[0]);
|
||||||
else
|
else
|
||||||
(void)printf("%s", buf);
|
(void)printf("%s", buf);
|
||||||
} while (++p != ep);
|
}
|
||||||
if (!newl)
|
if (nextp == ep && ep[-1] != '\n')
|
||||||
(void)putchar('\n');
|
(void)putchar('\n');
|
||||||
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user