"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 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 11 | |
| 12 | //usage:#define dmesg_trivial_usage |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 13 | //usage: "[-c] [-n LEVEL] [-s SIZE]" |
| 14 | //usage: IF_FEATURE_DMESG_PRETTY(" [-r]") |
| 15 | //usage: IF_FEATURE_DMESG_COLOR(" [-C]") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 16 | //usage:#define dmesg_full_usage "\n\n" |
| 17 | //usage: "Print or control the kernel ring buffer\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 18 | //usage: "\n -c Clear ring buffer after printing" |
| 19 | //usage: "\n -n LEVEL Set console logging level" |
| 20 | //usage: "\n -s SIZE Buffer size" |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 21 | //usage: IF_FEATURE_DMESG_PRETTY( |
| 22 | //usage: "\n -r Show level prefix") |
| 23 | //usage: IF_FEATURE_DMESG_COLOR( |
| 24 | //usage: "\n -C Colored output") |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 26 | #include <sys/klog.h> |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 28 | |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 29 | #if ENABLE_FEATURE_DMESG_COLOR |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 30 | #define COLOR_DEFAULT 0 |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 31 | #define COLOR_WHITE 97 |
| 32 | #define COLOR_YELLOW 93 |
| 33 | #define COLOR_ORANGE 33 |
| 34 | #define COLOR_RED 91 |
| 35 | |
| 36 | static void set_color(int color) |
| 37 | { |
| 38 | printf("%c[%dm", 0x1B, color); |
| 39 | } |
| 40 | |
| 41 | #else |
| 42 | #define set_color(c) {} |
| 43 | #endif |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 44 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 45 | int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 46 | int dmesg_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | { |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 48 | int len, level; |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 49 | char *buf; |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 50 | unsigned opts; |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 51 | int color = 0; |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 52 | enum { |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 53 | OPT_c = 1 << 0, |
| 54 | OPT_s = 1 << 1, |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 55 | OPT_n = 1 << 2, |
| 56 | OPT_r = 1 << 3, |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 57 | OPT_C = 1 << 4, |
| 58 | OPT_end |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 59 | }; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 61 | opt_complementary = "s+:n+"; /* numeric */ |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 62 | opts = getopt32(argv, "cs:n:rC", &len, &level); |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 63 | if (opts & OPT_n) { |
| 64 | if (klogctl(8, NULL, (long) level)) |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 65 | bb_perror_msg_and_die("klogctl"); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 66 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 68 | |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 69 | if (!(opts & OPT_s)) |
| 70 | len = klogctl(10, NULL, 0); /* read ring buffer size */ |
| 71 | if (len < 16*1024) |
| 72 | len = 16*1024; |
| 73 | if (len > 16*1024*1024) |
| 74 | len = 16*1024*1024; |
| 75 | |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 76 | buf = xmalloc(len); |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 77 | len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 78 | if (len < 0) |
| 79 | bb_perror_msg_and_die("klogctl"); |
| 80 | if (len == 0) |
| 81 | return EXIT_SUCCESS; |
| 82 | |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 83 | |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 84 | if ((ENABLE_FEATURE_DMESG_PRETTY || (opts & OPT_C)) && !(opts & OPT_r)) { |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 85 | int last = '\n'; |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 86 | int in = 0; |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 87 | |
Tanguy Pruvot | 823694d | 2012-11-18 13:20:29 +0100 | [diff] [blame] | 88 | /* Skip <[0-9]+> at the start of lines */ |
Denys Vlasenko | f04ca74 | 2010-10-19 23:08:33 +0200 | [diff] [blame] | 89 | while (1) { |
| 90 | if (last == '\n' && buf[in] == '<') { |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 91 | |
| 92 | #if ENABLE_FEATURE_DMESG_COLOR |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 93 | if (opts & OPT_C) { |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 94 | char *lvl = buf + in + 1; |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 95 | sscanf(lvl, "%d", &level); |
| 96 | |
| 97 | switch (level) { |
| 98 | case 1: |
| 99 | case 2: |
| 100 | case 3: color = COLOR_RED; break; |
| 101 | case 4: color = COLOR_ORANGE; break; |
| 102 | case 5: color = COLOR_YELLOW; break; |
| 103 | case 7: color = COLOR_WHITE; break; |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 104 | case 6: /* common dmesg info */ |
| 105 | default: |
| 106 | color = COLOR_DEFAULT; |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 107 | } |
| 108 | |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 109 | set_color(color); |
Tanguy Pruvot | a0c20e4 | 2012-01-06 16:40:09 +0100 | [diff] [blame] | 110 | } |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 111 | #endif |
Tanguy Pruvot | 823694d | 2012-11-18 13:20:29 +0100 | [diff] [blame] | 112 | while (buf[in++] != '>' && in < len) |
| 113 | ; |
| 114 | } else { |
| 115 | last = buf[in++]; |
| 116 | putchar(last); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 117 | } |
Denys Vlasenko | f04ca74 | 2010-10-19 23:08:33 +0200 | [diff] [blame] | 118 | if (in >= len) |
| 119 | break; |
| 120 | } |
| 121 | /* Make sure we end with a newline */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 122 | if (last != '\n') |
| 123 | bb_putchar('\n'); |
| 124 | } else { |
| 125 | full_write(STDOUT_FILENO, buf, len); |
| 126 | if (buf[len-1] != '\n') |
| 127 | bb_putchar('\n'); |
| 128 | } |
| 129 | |
| 130 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); |
| 131 | |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 132 | /* Reset default terminal color */ |
| 133 | if (color) |
| 134 | set_color(0); |
| 135 | |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 136 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 137 | } |