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