[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | *
|
---|
[821] | 4 | * dmesg - display/control kernel ring buffer.
|
---|
| 5 | *
|
---|
[1765] | 6 | * Copyright 2006 Rob Landley <rob@landley.net>
|
---|
[2725] | 7 | * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at>
|
---|
[821] | 8 | *
|
---|
[2725] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
[821] | 10 | */
|
---|
[3232] | 11 |
|
---|
| 12 | //usage:#define dmesg_trivial_usage
|
---|
| 13 | //usage: "[-c] [-n LEVEL] [-s SIZE]"
|
---|
| 14 | //usage:#define dmesg_full_usage "\n\n"
|
---|
| 15 | //usage: "Print or control the kernel ring buffer\n"
|
---|
| 16 | //usage: "\n -c Clear ring buffer after printing"
|
---|
| 17 | //usage: "\n -n LEVEL Set console logging level"
|
---|
| 18 | //usage: "\n -s SIZE Buffer size"
|
---|
[3621] | 19 | //usage: "\n -r Print raw message buffer"
|
---|
[3232] | 20 |
|
---|
[821] | 21 | #include <sys/klog.h>
|
---|
[1765] | 22 | #include "libbb.h"
|
---|
[821] | 23 |
|
---|
[2725] | 24 | int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 25 | int dmesg_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 26 | {
|
---|
[2725] | 27 | int len, level;
|
---|
| 28 | char *buf;
|
---|
| 29 | unsigned opts;
|
---|
| 30 | enum {
|
---|
| 31 | OPT_c = 1 << 0,
|
---|
| 32 | OPT_s = 1 << 1,
|
---|
[3621] | 33 | OPT_n = 1 << 2,
|
---|
| 34 | OPT_r = 1 << 3
|
---|
[2725] | 35 | };
|
---|
[821] | 36 |
|
---|
[2725] | 37 | opt_complementary = "s+:n+"; /* numeric */
|
---|
[3621] | 38 | opts = getopt32(argv, "cs:n:r", &len, &level);
|
---|
[2725] | 39 | if (opts & OPT_n) {
|
---|
| 40 | if (klogctl(8, NULL, (long) level))
|
---|
[821] | 41 | bb_perror_msg_and_die("klogctl");
|
---|
[2725] | 42 | return EXIT_SUCCESS;
|
---|
| 43 | }
|
---|
[821] | 44 |
|
---|
[2725] | 45 | if (!(opts & OPT_s))
|
---|
| 46 | len = klogctl(10, NULL, 0); /* read ring buffer size */
|
---|
| 47 | if (len < 16*1024)
|
---|
| 48 | len = 16*1024;
|
---|
| 49 | if (len > 16*1024*1024)
|
---|
| 50 | len = 16*1024*1024;
|
---|
[1765] | 51 |
|
---|
[2725] | 52 | buf = xmalloc(len);
|
---|
| 53 | len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
|
---|
| 54 | if (len < 0)
|
---|
| 55 | bb_perror_msg_and_die("klogctl");
|
---|
| 56 | if (len == 0)
|
---|
| 57 | return EXIT_SUCCESS;
|
---|
[1765] | 58 |
|
---|
| 59 |
|
---|
[3621] | 60 | if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
|
---|
[2725] | 61 | int last = '\n';
|
---|
| 62 | int in = 0;
|
---|
| 63 |
|
---|
[3232] | 64 | /* Skip <[0-9]+> at the start of lines */
|
---|
[2725] | 65 | while (1) {
|
---|
| 66 | if (last == '\n' && buf[in] == '<') {
|
---|
[3232] | 67 | while (buf[in++] != '>' && in < len)
|
---|
| 68 | ;
|
---|
| 69 | } else {
|
---|
| 70 | last = buf[in++];
|
---|
| 71 | putchar(last);
|
---|
[1765] | 72 | }
|
---|
[2725] | 73 | if (in >= len)
|
---|
| 74 | break;
|
---|
[1765] | 75 | }
|
---|
[2725] | 76 | /* Make sure we end with a newline */
|
---|
| 77 | if (last != '\n')
|
---|
| 78 | bb_putchar('\n');
|
---|
| 79 | } else {
|
---|
| 80 | full_write(STDOUT_FILENO, buf, len);
|
---|
| 81 | if (buf[len-1] != '\n')
|
---|
| 82 | bb_putchar('\n');
|
---|
[821] | 83 | }
|
---|
| 84 |
|
---|
[2725] | 85 | if (ENABLE_FEATURE_CLEAN_UP) free(buf);
|
---|
| 86 |
|
---|
| 87 | return EXIT_SUCCESS;
|
---|
[821] | 88 | }
|
---|