comparison toys/dmesg.c @ 169:56034b162074

Implement dmesg.
author Rob Landley <rob@landley.net>
date Tue, 20 Nov 2007 01:06:29 -0600
parents
children 25447caf1b4b
comparison
equal deleted inserted replaced
168:14fa82969ea3 169:56034b162074
1 /* vi: set sw=4 ts=4: */
2 /*
3 * dmesg.c - display/control kernel ring buffer.
4 *
5 * Copyright 2006 Rob Landley <rob@landley.net>
6 */
7
8 #include "toys.h"
9 #include <sys/klog.h>
10
11 #define TT toy.dmesg
12
13 int dmesg_main(void)
14 {
15 // For -n just tell kernel to which messages to keep.
16 if (toys.optflags & 2) {
17 if (klogctl(8, NULL, TT.level))
18 error_exit("klogctl");
19 } else {
20 int size, i, last = '\n';
21 char *data;
22
23 // Figure out how much data we need, and fetch it.
24 size = TT.size;
25 if (size<2) size = 16384;
26 data = xmalloc(size);
27 size = klogctl(3 + (toys.optflags&1), data, size);
28 if (size < 0) error_exit("klogctl");
29
30 // Display data, filtering out level markers.
31 for (i=0; i<size; ) {
32 if (last=='\n' && data[i]=='<') i += 3;
33 else putchar(last = data[i++]);
34 }
35 if (last!='\n') putchar('\n');
36 if (CFG_TOYBOX_FREE) free(data);
37 }
38
39 return 0;
40 }