diff --git a/sys-utils/dmesg.1 b/sys-utils/dmesg.1 index d7af1da..bbdd462 100644 --- a/sys-utils/dmesg.1 +++ b/sys-utils/dmesg.1 @@ -6,6 +6,7 @@ dmesg \- print or control the kernel ring buffer .SH SYNOPSIS .B dmesg .RB [ \-c ] +.RB [ \-f ] .RB [ \-r ] .RB [ \-n .IR level ] @@ -28,6 +29,9 @@ file to whoever can debug their problem. .B \-c Clear the ring buffer contents after printing. .TP +.B \-f +Output ring buffer contents as they are added. +.TP .B \-r Print the raw message buffer, i.e., don't strip the log level prefixes. .TP diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index c3e5659..e2f5933 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -44,7 +44,7 @@ static void __attribute__ ((noreturn)) usage(void) { fprintf(stderr, - _("Usage: %s [-c] [-n level] [-r] [-s bufsize]\n"), + _("Usage: %s [-c] [-f] [-n level] [-r] [-s bufsize]\n"), program_invocation_short_name); exit(EXIT_FAILURE); @@ -62,16 +62,21 @@ int main(int argc, char *argv[]) int lastc; int cmd = 3; /* Read all messages in the ring buffer */ int raw = 0; + int follow = 0; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - while ((c = getopt(argc, argv, "crn:s:")) != -1) { + while ((c = getopt(argc, argv, "cfrn:s:")) != -1) { switch (c) { case 'c': cmd = 4; /* Read and clear all messages */ break; + case 'f': + cmd = 2; + follow = 1; + break; case 'n': cmd = 8; /* Set level of messages */ level = strtol_or_err(optarg, _("failed to parse level")); @@ -109,43 +114,45 @@ int main(int argc, char *argv[]) bufsize = n; } - if (bufsize) { - sz = bufsize + 8; - buf = xmalloc(sz * sizeof(char)); - n = klogctl(cmd, buf, sz); - } else { - sz = 16392; - while (1) { + do { + if (bufsize) { + sz = bufsize + 8; buf = xmalloc(sz * sizeof(char)); - n = klogctl(3, buf, sz); /* read only */ - if (n != sz || sz > (1 << 28)) - break; - free(buf); - sz *= 4; + n = klogctl(cmd, buf, sz); + } else { + sz = 16392; + while (1) { + buf = xmalloc(sz * sizeof(char)); + n = klogctl(3, buf, sz); /* read only */ + if (n != sz || sz > (1 << 28)) + break; + free(buf); + sz *= 4; + } + + if (n > 0 && cmd == 4) + n = klogctl(cmd, buf, sz); /* read and clear */ } - if (n > 0 && cmd == 4) - n = klogctl(cmd, buf, sz); /* read and clear */ - } - - if (n < 0) - err(EXIT_FAILURE, _("klogctl failed")); + if (n < 0) + err(EXIT_FAILURE, _("klogctl failed")); - lastc = '\n'; - for (i = 0; i < n; i++) { - if (!raw && (i == 0 || buf[i - 1] == '\n') && buf[i] == '<') { - i++; - while (isdigit(buf[i])) - i++; - if (buf[i] == '>') + lastc = '\n'; + for (i = 0; i < n; i++) { + if (!raw && (i == 0 || buf[i - 1] == '\n') && buf[i] == '<') { i++; + while (isdigit(buf[i])) + i++; + if (buf[i] == '>') + i++; + } + lastc = buf[i]; + putchar(lastc); } - lastc = buf[i]; - putchar(lastc); - } - if (lastc != '\n') - putchar('\n'); - free(buf); + if (lastc != '\n') + putchar('\n'); + free(buf); + } while (follow); return EXIT_SUCCESS; }