| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
| Rob Landley | b5b82d9 | 2007-11-20 01:06:29 -0600 | [diff] [blame] | 3 | * dmesg.c - display/control kernel ring buffer. |
| 4 | * |
| Rob Landley | fece5cb | 2007-12-03 20:05:57 -0600 | [diff] [blame] | 5 | * Copyright 2006, 2007 Rob Landley <rob@landley.net> |
| 6 | * |
| 7 | * Not in SUSv3. |
| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 8 | |
| Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame^] | 9 | USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN)) |
| 10 | |
| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 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 | */ |
| Rob Landley | b5b82d9 | 2007-11-20 01:06:29 -0600 | [diff] [blame] | 23 | |
| 24 | #include "toys.h" |
| 25 | #include <sys/klog.h> |
| 26 | |
| 27 | #define TT toy.dmesg |
| 28 | |
| Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 29 | void dmesg_main(void) |
| Rob Landley | b5b82d9 | 2007-11-20 01:06:29 -0600 | [diff] [blame] | 30 | { |
| 31 | // For -n just tell kernel to which messages to keep. |
| 32 | if (toys.optflags & 2) { |
| 33 | if (klogctl(8, NULL, TT.level)) |
| 34 | error_exit("klogctl"); |
| 35 | } else { |
| 36 | int size, i, last = '\n'; |
| 37 | char *data; |
| 38 | |
| 39 | // Figure out how much data we need, and fetch it. |
| 40 | size = TT.size; |
| 41 | if (size<2) size = 16384; |
| 42 | data = xmalloc(size); |
| 43 | size = klogctl(3 + (toys.optflags&1), data, size); |
| 44 | if (size < 0) error_exit("klogctl"); |
| 45 | |
| 46 | // Display data, filtering out level markers. |
| 47 | for (i=0; i<size; ) { |
| 48 | if (last=='\n' && data[i]=='<') i += 3; |
| Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 49 | else xputc(last = data[i++]); |
| Rob Landley | b5b82d9 | 2007-11-20 01:06:29 -0600 | [diff] [blame] | 50 | } |
| Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 51 | if (last!='\n') xputc('\n'); |
| Rob Landley | b5b82d9 | 2007-11-20 01:06:29 -0600 | [diff] [blame] | 52 | if (CFG_TOYBOX_FREE) free(data); |
| 53 | } |
| Rob Landley | b5b82d9 | 2007-11-20 01:06:29 -0600 | [diff] [blame] | 54 | } |