comparison toys/lsb/dmesg.c @ 653:2986aa63a021

Move commands into "posix", "lsb", and "other" menus/directories.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 14:25:22 -0500
parents toys/dmesg.c@7cb15eae1664
children 6df4ccc0acbe
comparison
equal deleted inserted replaced
652:2d7c56913fda 653:2986aa63a021
1 /* vi: set sw=4 ts=4:
2 *
3 * dmesg.c - display/control kernel ring buffer.
4 *
5 * Copyright 2006, 2007 Rob Landley <rob@landley.net>
6 *
7 * Not in SUSv3.
8
9 USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN))
10
11 config DMESG
12 bool "dmesg"
13 default y
14 help
15 usage: dmesg [-n level] [-s bufsize] | -c
16
17 Print or control the kernel ring buffer.
18
19 -n Set kernel logging level (1-9).
20 -s Size of buffer to read (in bytes), default 16384.
21 -c Clear the ring buffer after printing.
22 */
23
24 #include "toys.h"
25 #include <sys/klog.h>
26
27 DEFINE_GLOBALS(
28 long level;
29 long size;
30 )
31
32 #define TT this.dmesg
33
34 void dmesg_main(void)
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;
54 else xputc(last = data[i++]);
55 }
56 if (last!='\n') xputc('\n');
57 if (CFG_TOYBOX_FREE) free(data);
58 }
59 }