blob: 95b023d6dc7b1f8c5a07ffce1d83611cb6ae3ed7 [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001/* vi: set sw=4 ts=4:
2 *
Rob Landleyb5b82d92007-11-20 01:06:29 -06003 * dmesg.c - display/control kernel ring buffer.
4 *
Rob Landleyfece5cb2007-12-03 20:05:57 -06005 * Copyright 2006, 2007 Rob Landley <rob@landley.net>
6 *
7 * Not in SUSv3.
Rob Landley28964802008-01-19 17:08:39 -06008
Rob Landley55928b12008-01-19 17:43:27 -06009USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN))
10
Rob Landley28964802008-01-19 17:08:39 -060011config 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*/
Rob Landleyb5b82d92007-11-20 01:06:29 -060023
24#include "toys.h"
25#include <sys/klog.h>
26
Rob Landleyb1aaba12008-01-20 17:25:44 -060027DEFINE_GLOBALS(
28 long level;
29 long size;
30)
31
32#define TT this.dmesg
Rob Landleyb5b82d92007-11-20 01:06:29 -060033
Rob Landleyefda21c2007-11-29 18:14:37 -060034void dmesg_main(void)
Rob Landleyb5b82d92007-11-20 01:06:29 -060035{
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;
Rob Landleyefda21c2007-11-29 18:14:37 -060054 else xputc(last = data[i++]);
Rob Landleyb5b82d92007-11-20 01:06:29 -060055 }
Rob Landleyefda21c2007-11-29 18:14:37 -060056 if (last!='\n') xputc('\n');
Rob Landleyb5b82d92007-11-20 01:06:29 -060057 if (CFG_TOYBOX_FREE) free(data);
58 }
Rob Landleyb5b82d92007-11-20 01:06:29 -060059}