"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> |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 7 | * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at> |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame^] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | */ |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 11 | #include <sys/klog.h> |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 14 | int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 15 | int dmesg_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 16 | { |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 17 | int len, level; |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 18 | char *buf; |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 19 | unsigned opts; |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 20 | enum { |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 21 | OPT_c = 1 << 0, |
| 22 | OPT_s = 1 << 1, |
| 23 | OPT_n = 1 << 2 |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 24 | }; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 25 | |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 26 | opt_complementary = "s+:n+"; /* numeric */ |
| 27 | opts = getopt32(argv, "cs:n:", &len, &level); |
| 28 | if (opts & OPT_n) { |
| 29 | if (klogctl(8, NULL, (long) level)) |
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 | |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 34 | if (!(opts & OPT_s)) |
| 35 | len = klogctl(10, NULL, 0); /* read ring buffer size */ |
| 36 | if (len < 16*1024) |
| 37 | len = 16*1024; |
| 38 | if (len > 16*1024*1024) |
| 39 | len = 16*1024*1024; |
| 40 | |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 41 | buf = xmalloc(len); |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 42 | len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 43 | if (len < 0) |
| 44 | bb_perror_msg_and_die("klogctl"); |
| 45 | if (len == 0) |
| 46 | return EXIT_SUCCESS; |
| 47 | |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 48 | /* Skip <#> at the start of lines, and make sure we end with a newline */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 49 | |
| 50 | if (ENABLE_FEATURE_DMESG_PRETTY) { |
| 51 | int last = '\n'; |
| 52 | int in = 0; |
| 53 | |
| 54 | do { |
| 55 | if (last == '\n' && buf[in] == '<') |
| 56 | in += 3; |
| 57 | else { |
| 58 | last = buf[in++]; |
| 59 | bb_putchar(last); |
| 60 | } |
| 61 | } while (in < len); |
| 62 | if (last != '\n') |
| 63 | bb_putchar('\n'); |
| 64 | } else { |
| 65 | full_write(STDOUT_FILENO, buf, len); |
| 66 | if (buf[len-1] != '\n') |
| 67 | bb_putchar('\n'); |
| 68 | } |
| 69 | |
| 70 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); |
| 71 | |
| 72 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | } |