blob: ba52036a701f744a88bbcd834053b4ebb43d5927
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: IF_FEATURE_DMESG_PRETTY(" [-r]") |
15 | //usage: IF_FEATURE_DMESG_COLOR(" [-C]") |
16 | //usage:#define dmesg_full_usage "\n\n" |
17 | //usage: "Print or control the kernel ring buffer\n" |
18 | //usage: "\n -c Clear ring buffer after printing" |
19 | //usage: "\n -n LEVEL Set console logging level" |
20 | //usage: "\n -s SIZE Buffer size" |
21 | //usage: IF_FEATURE_DMESG_PRETTY( |
22 | //usage: "\n -r Show level prefix") |
23 | //usage: IF_FEATURE_DMESG_COLOR( |
24 | //usage: "\n -C Colored output") |
25 | |
26 | #include <sys/klog.h> |
27 | #include "libbb.h" |
28 | |
29 | #if ENABLE_FEATURE_DMESG_COLOR |
30 | #define COLOR_DEFAULT 0 |
31 | #define COLOR_WHITE 97 |
32 | #define COLOR_YELLOW 93 |
33 | #define COLOR_ORANGE 33 |
34 | #define COLOR_RED 91 |
35 | |
36 | static void set_color(int color) |
37 | { |
38 | printf("%c[%dm", 0x1B, color); |
39 | } |
40 | |
41 | #else |
42 | #define set_color(c) {} |
43 | #endif |
44 | |
45 | int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
46 | int dmesg_main(int argc UNUSED_PARAM, char **argv) |
47 | { |
48 | int len, level; |
49 | char *buf; |
50 | unsigned opts; |
51 | int color = 0; |
52 | enum { |
53 | OPT_c = 1 << 0, |
54 | OPT_s = 1 << 1, |
55 | OPT_n = 1 << 2, |
56 | OPT_r = 1 << 3, |
57 | OPT_C = 1 << 4, |
58 | OPT_end |
59 | }; |
60 | |
61 | opt_complementary = "s+:n+"; /* numeric */ |
62 | opts = getopt32(argv, "cs:n:rC", &len, &level); |
63 | if (opts & OPT_n) { |
64 | if (klogctl(8, NULL, (long) level)) |
65 | bb_perror_msg_and_die("klogctl"); |
66 | return EXIT_SUCCESS; |
67 | } |
68 | |
69 | if (!(opts & OPT_s)) |
70 | len = klogctl(10, NULL, 0); /* read ring buffer size */ |
71 | if (len < 16*1024) |
72 | len = 16*1024; |
73 | if (len > 16*1024*1024) |
74 | len = 16*1024*1024; |
75 | |
76 | buf = xmalloc(len); |
77 | len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */ |
78 | if (len < 0) |
79 | bb_perror_msg_and_die("klogctl"); |
80 | if (len == 0) |
81 | return EXIT_SUCCESS; |
82 | |
83 | |
84 | if ((ENABLE_FEATURE_DMESG_PRETTY || (opts & OPT_C)) && !(opts & OPT_r)) { |
85 | int last = '\n'; |
86 | int in = 0; |
87 | |
88 | /* Skip <[0-9]+> at the start of lines */ |
89 | while (1) { |
90 | if (last == '\n' && buf[in] == '<') { |
91 | |
92 | #if ENABLE_FEATURE_DMESG_COLOR |
93 | if (opts & OPT_C) { |
94 | char *lvl = buf + in + 1; |
95 | sscanf(lvl, "%d", &level); |
96 | |
97 | switch (level) { |
98 | case 1: |
99 | case 2: |
100 | case 3: color = COLOR_RED; break; |
101 | case 4: color = COLOR_ORANGE; break; |
102 | case 5: color = COLOR_YELLOW; break; |
103 | case 7: color = COLOR_WHITE; break; |
104 | case 6: /* common dmesg info */ |
105 | default: |
106 | color = COLOR_DEFAULT; |
107 | } |
108 | |
109 | set_color(color); |
110 | } |
111 | #endif |
112 | while (buf[in++] != '>' && in < len) |
113 | ; |
114 | } else { |
115 | last = buf[in++]; |
116 | putchar(last); |
117 | } |
118 | if (in >= len) |
119 | break; |
120 | } |
121 | /* Make sure we end with a newline */ |
122 | if (last != '\n') |
123 | bb_putchar('\n'); |
124 | } else { |
125 | full_write(STDOUT_FILENO, buf, len); |
126 | if (buf[len-1] != '\n') |
127 | bb_putchar('\n'); |
128 | } |
129 | |
130 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); |
131 | |
132 | /* Reset default terminal color */ |
133 | if (color) |
134 | set_color(0); |
135 | |
136 | return EXIT_SUCCESS; |
137 | } |
138 |