Mercurial > hg > toybox
comparison toys/lsb/dmesg.c @ 1575:d26f235323db draft
Update dmesg, loosely based on a patch from Elliott Hughes.
Probe the default buffer size, replace the constants with FLAG_x macros, add -r,
replace the byte at a time output with a single xwrite(), more comments.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 25 Nov 2014 21:45:18 -0600 |
parents | 786841fdb1e0 |
children |
comparison
equal
deleted
inserted
replaced
1574:991f4d613388 | 1575:d26f235323db |
---|---|
2 * | 2 * |
3 * Copyright 2006, 2007 Rob Landley <rob@landley.net> | 3 * Copyright 2006, 2007 Rob Landley <rob@landley.net> |
4 * | 4 * |
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/dmesg.html | 5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/dmesg.html |
6 | 6 |
7 USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN)) | 7 // We care that FLAG_c is 1, so keep c at the end. |
8 USE_DMESG(NEWTOY(dmesg, "rs#<1n#c", TOYFLAG_BIN)) | |
8 | 9 |
9 config DMESG | 10 config DMESG |
10 bool "dmesg" | 11 bool "dmesg" |
11 default y | 12 default y |
12 help | 13 help |
13 usage: dmesg [-n level] [-s bufsize] | -c | 14 usage: dmesg [-n LEVEL] [-s SIZE] | -c |
14 | 15 |
15 Print or control the kernel ring buffer. | 16 Print or control the kernel ring buffer. |
16 | 17 |
17 -n Set kernel logging level (1-9). | 18 -c Clear the ring buffer after printing |
18 -s Size of buffer to read (in bytes), default 16384. | 19 -n Set kernel logging LEVEL (1-9) |
19 -c Clear the ring buffer after printing. | 20 -r Raw output (with <level markers>) |
21 -s Show the last SIZE many bytes | |
20 */ | 22 */ |
21 | 23 |
22 #define FOR_dmesg | 24 #define FOR_dmesg |
23 #include "toys.h" | 25 #include "toys.h" |
24 #include <sys/klog.h> | 26 #include <sys/klog.h> |
29 ) | 31 ) |
30 | 32 |
31 void dmesg_main(void) | 33 void dmesg_main(void) |
32 { | 34 { |
33 // For -n just tell kernel to which messages to keep. | 35 // For -n just tell kernel to which messages to keep. |
34 if (toys.optflags & 2) { | 36 if (toys.optflags & FLAG_n) { |
35 if (klogctl(8, NULL, TT.level)) error_exit("klogctl"); | 37 if (klogctl(8, NULL, TT.level)) perror_exit("klogctl"); |
36 } else { | 38 } else { |
37 int size, i, last = '\n'; | 39 char *data, *to, *from; |
38 char *data; | 40 int size; |
39 | 41 |
40 // Figure out how much data we need, and fetch it. | 42 // Figure out how much data we need, and fetch it. |
41 size = TT.size; | 43 size = TT.size; |
42 if (size<2) size = 16384; | 44 if (!size && 1>(size = klogctl(10, 0, 0))) perror_exit("klogctl");; |
43 data = xmalloc(size); | 45 data = to = from = xmalloc(size+1); |
44 size = klogctl(3 + (toys.optflags&1), data, size); | 46 size = klogctl(3 + (toys.optflags & FLAG_c), data, size); |
45 if (size < 0) error_exit("klogctl"); | 47 if (size < 0) error_exit("klogctl"); |
48 data[size] = 0; | |
46 | 49 |
47 // Display data, filtering out level markers. | 50 // Filter out level markers. |
48 for (i=0; i<size; ) { | 51 if (!(toys.optflags & FLAG_r)) while ((from - data) < size) { |
49 if (last=='\n' && data[i]=='<') i += 3; | 52 if ((from == data || from[-1] == '\n') && *from == '<') { |
50 else xputc(last = data[i++]); | 53 int i = stridx(from, '>'); |
54 | |
55 if (i>0) from += i+1; | |
56 } | |
57 *(to++) = *(from++); | |
58 } else to = data+size; | |
59 | |
60 // Write result. The odds of somebody requesting a buffer of size 3 and | |
61 // getting "<1>" are remote, but don't segfault if they do. | |
62 if (to != data) { | |
63 xwrite(1, data, to-data); | |
64 if (to[-1] != '\n') xputc('\n'); | |
51 } | 65 } |
52 if (last!='\n') xputc('\n'); | |
53 if (CFG_TOYBOX_FREE) free(data); | 66 if (CFG_TOYBOX_FREE) free(data); |
54 } | 67 } |
55 } | 68 } |