"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 3 | * |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 4 | * dmesg - display/control kernel ring buffer. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 5 | * |
Rob Landley | 446129a | 2006-07-27 16:40:55 +0000 | [diff] [blame] | 6 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 7 | * Copyright 2006 Bernhard Fischer <rep.nop@aon.at> |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 8 | * |
Bernhard Reutner-Fischer | 0953336 | 2008-05-20 08:27:26 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 12 | #include <sys/klog.h> |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 15 | int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 16 | int dmesg_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | { |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 18 | int len; |
| 19 | char *buf; |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 20 | char *size, *level; |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 21 | unsigned flags = getopt32(argv, "cs:n:", &size, &level); |
| 22 | enum { |
| 23 | OPT_c = 1<<0, |
| 24 | OPT_s = 1<<1, |
| 25 | OPT_n = 1<<2 |
| 26 | }; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 27 | |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 28 | if (flags & OPT_n) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 29 | if (klogctl(8, NULL, xatoul_range(level, 0, 10))) |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 30 | bb_perror_msg_and_die("klogctl"); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 31 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 32 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 33 | |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 34 | len = (flags & OPT_s) ? xatoul_range(size, 2, INT_MAX) : 16384; |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 35 | buf = xmalloc(len); |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 36 | len = klogctl(3 + (flags & OPT_c), buf, len); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 37 | if (len < 0) |
| 38 | bb_perror_msg_and_die("klogctl"); |
| 39 | if (len == 0) |
| 40 | return EXIT_SUCCESS; |
| 41 | |
| 42 | /* Skip <#> at the start of lines, and make sure we end with a newline. */ |
| 43 | |
| 44 | if (ENABLE_FEATURE_DMESG_PRETTY) { |
| 45 | int last = '\n'; |
| 46 | int in = 0; |
| 47 | |
| 48 | do { |
| 49 | if (last == '\n' && buf[in] == '<') |
| 50 | in += 3; |
| 51 | else { |
| 52 | last = buf[in++]; |
| 53 | bb_putchar(last); |
| 54 | } |
| 55 | } while (in < len); |
| 56 | if (last != '\n') |
| 57 | bb_putchar('\n'); |
| 58 | } else { |
| 59 | full_write(STDOUT_FILENO, buf, len); |
| 60 | if (buf[len-1] != '\n') |
| 61 | bb_putchar('\n'); |
| 62 | } |
| 63 | |
| 64 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); |
| 65 | |
| 66 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | } |