8 |
* |
* |
9 |
* Licensed under GPLv2, see file LICENSE in this tarball for details. |
* Licensed under GPLv2, see file LICENSE in this tarball for details. |
10 |
*/ |
*/ |
|
|
|
11 |
#include <sys/klog.h> |
#include <sys/klog.h> |
12 |
#include "libbb.h" |
#include "libbb.h" |
13 |
|
|
14 |
int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
15 |
int dmesg_main(int argc UNUSED_PARAM, char **argv) |
int dmesg_main(int argc UNUSED_PARAM, char **argv) |
16 |
{ |
{ |
17 |
int len; |
int len, level; |
18 |
char *buf; |
char *buf; |
19 |
char *size, *level; |
unsigned opts; |
|
unsigned flags = getopt32(argv, "cs:n:", &size, &level); |
|
20 |
enum { |
enum { |
21 |
OPT_c = 1<<0, |
OPT_c = 1 << 0, |
22 |
OPT_s = 1<<1, |
OPT_s = 1 << 1, |
23 |
OPT_n = 1<<2 |
OPT_n = 1 << 2 |
24 |
}; |
}; |
25 |
|
|
26 |
if (flags & OPT_n) { |
opt_complementary = "s+:n+"; /* numeric */ |
27 |
if (klogctl(8, NULL, xatoul_range(level, 0, 10))) |
opts = getopt32(argv, "cs:n:", &len, &level); |
28 |
|
if (opts & OPT_n) { |
29 |
|
if (klogctl(8, NULL, (long) level)) |
30 |
bb_perror_msg_and_die("klogctl"); |
bb_perror_msg_and_die("klogctl"); |
31 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
32 |
} |
} |
33 |
|
|
34 |
len = (flags & OPT_s) ? xatoul_range(size, 2, INT_MAX) : 16384; |
if (!(opts & OPT_s)) |
35 |
|
len = klogctl(10, NULL, 0); /* read ring buffer size */ |
36 |
|
if (len < 16*1024) |
37 |
|
len = 16*1024; |
38 |
|
if (len > 16*1024*1024) |
39 |
|
len = 16*1024*1024; |
40 |
|
|
41 |
buf = xmalloc(len); |
buf = xmalloc(len); |
42 |
len = klogctl(3 + (flags & OPT_c), buf, len); |
len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */ |
43 |
if (len < 0) |
if (len < 0) |
44 |
bb_perror_msg_and_die("klogctl"); |
bb_perror_msg_and_die("klogctl"); |
45 |
if (len == 0) |
if (len == 0) |
46 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
47 |
|
|
48 |
/* Skip <#> at the start of lines, and make sure we end with a newline. */ |
/* Skip <#> at the start of lines, and make sure we end with a newline */ |
49 |
|
|
50 |
if (ENABLE_FEATURE_DMESG_PRETTY) { |
if (ENABLE_FEATURE_DMESG_PRETTY) { |
51 |
int last = '\n'; |
int last = '\n'; |