Mercurial > hg > toybox
annotate toys/dmesg.c @ 396:7ca3bef07f0d
Implement truncate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 11 Dec 2011 03:58:43 -0600 |
parents | 7cb15eae1664 |
children |
rev | line source |
---|---|
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
2 * |
169 | 3 * dmesg.c - display/control kernel ring buffer. |
4 * | |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
5 * 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
|
6 * |
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
7 * Not in SUSv3. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
8 |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
9 USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN)) |
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
10 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
11 config DMESG |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
12 bool "dmesg" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
13 default y |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
14 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
15 usage: dmesg [-n level] [-s bufsize] | -c |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
16 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
17 Print or control the kernel ring buffer. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
18 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
19 -n Set kernel logging level (1-9). |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
20 -s Size of buffer to read (in bytes), default 16384. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
21 -c Clear the ring buffer after printing. |
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 |
24 #include "toys.h" | |
25 #include <sys/klog.h> | |
26 | |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
27 DEFINE_GLOBALS( |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
28 long level; |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
29 long size; |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
30 ) |
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 #define TT this.dmesg |
169 | 33 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
169
diff
changeset
|
34 void dmesg_main(void) |
169 | 35 { |
36 // For -n just tell kernel to which messages to keep. | |
37 if (toys.optflags & 2) { | |
38 if (klogctl(8, NULL, TT.level)) | |
39 error_exit("klogctl"); | |
40 } else { | |
41 int size, i, last = '\n'; | |
42 char *data; | |
43 | |
44 // Figure out how much data we need, and fetch it. | |
45 size = TT.size; | |
46 if (size<2) size = 16384; | |
47 data = xmalloc(size); | |
48 size = klogctl(3 + (toys.optflags&1), data, size); | |
49 if (size < 0) error_exit("klogctl"); | |
50 | |
51 // Display data, filtering out level markers. | |
52 for (i=0; i<size; ) { | |
53 if (last=='\n' && data[i]=='<') i += 3; | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
169
diff
changeset
|
54 else xputc(last = data[i++]); |
169 | 55 } |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
169
diff
changeset
|
56 if (last!='\n') xputc('\n'); |
169 | 57 if (CFG_TOYBOX_FREE) free(data); |
58 } | |
59 } |