Mercurial > hg > toybox
annotate 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 |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
1 /* dmesg.c - display/control kernel ring buffer. |
169 | 2 * |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
3 * Copyright 2006, 2007 Rob Landley <rob@landley.net> |
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
4 * |
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/dmesg.html |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
6 |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
7 // We care that FLAG_c is 1, so keep c at the end. |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
8 USE_DMESG(NEWTOY(dmesg, "rs#<1n#c", TOYFLAG_BIN)) |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
9 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
10 config DMESG |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
11 bool "dmesg" |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
12 default y |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
13 help |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
14 usage: dmesg [-n LEVEL] [-s SIZE] | -c |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
15 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
16 Print or control the kernel ring buffer. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
17 |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
18 -c Clear the ring buffer after printing |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
19 -n Set kernel logging LEVEL (1-9) |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
20 -r Raw output (with <level markers>) |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
21 -s Show the last SIZE many bytes |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
22 */ |
169 | 23 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
24 #define FOR_dmesg |
169 | 25 #include "toys.h" |
26 #include <sys/klog.h> | |
27 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
28 GLOBALS( |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
29 long level; |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
30 long size; |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
31 ) |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
32 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
169
diff
changeset
|
33 void dmesg_main(void) |
169 | 34 { |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
35 // For -n just tell kernel to which messages to keep. |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
36 if (toys.optflags & FLAG_n) { |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
37 if (klogctl(8, NULL, TT.level)) perror_exit("klogctl"); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
38 } else { |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
39 char *data, *to, *from; |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
40 int size; |
169 | 41 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
42 // Figure out how much data we need, and fetch it. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
43 size = TT.size; |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
44 if (!size && 1>(size = klogctl(10, 0, 0))) perror_exit("klogctl");; |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
45 data = to = from = xmalloc(size+1); |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
46 size = klogctl(3 + (toys.optflags & FLAG_c), data, size); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
47 if (size < 0) error_exit("klogctl"); |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
48 data[size] = 0; |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
49 |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
50 // Filter out level markers. |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
51 if (!(toys.optflags & FLAG_r)) while ((from - data) < size) { |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
52 if ((from == data || from[-1] == '\n') && *from == '<') { |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
53 int i = stridx(from, '>'); |
169 | 54 |
1575
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
55 if (i>0) from += i+1; |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
56 } |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
57 *(to++) = *(from++); |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
58 } else to = data+size; |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
59 |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
60 // Write result. The odds of somebody requesting a buffer of size 3 and |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
61 // getting "<1>" are remote, but don't segfault if they do. |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
62 if (to != data) { |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
63 xwrite(1, data, to-data); |
d26f235323db
Update dmesg, loosely based on a patch from Elliott Hughes.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
64 if (to[-1] != '\n') xputc('\n'); |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
65 } |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
66 if (CFG_TOYBOX_FREE) free(data); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
67 } |
169 | 68 } |