blob: ba52036a701f744a88bbcd834053b4ebb43d5927 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00003 *
Rob Landley7c7b0d72006-06-13 18:31:04 +00004 * dmesg - display/control kernel ring buffer.
Eric Andersene77ae3a1999-10-19 20:03:34 +00005 *
Rob Landley446129a2006-07-27 16:40:55 +00006 * Copyright 2006 Rob Landley <rob@landley.net>
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00007 * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at>
Eric Andersen2afcbe42003-03-07 17:33:40 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +000010 */
Pere Orga5bc8c002011-04-11 03:29:49 +020011
12//usage:#define dmesg_trivial_usage
maxwen27116ba2015-08-14 21:41:28 +020013//usage: "[-c] [-n LEVEL] [-s SIZE]"
14//usage: IF_FEATURE_DMESG_PRETTY(" [-r]")
15//usage: IF_FEATURE_DMESG_COLOR(" [-C]")
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage:#define dmesg_full_usage "\n\n"
17//usage: "Print or control the kernel ring buffer\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage: "\n -c Clear ring buffer after printing"
19//usage: "\n -n LEVEL Set console logging level"
20//usage: "\n -s SIZE Buffer size"
maxwen27116ba2015-08-14 21:41:28 +020021//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 Orga5bc8c002011-04-11 03:29:49 +020025
Eric Andersen85e5e722003-07-22 08:56:55 +000026#include <sys/klog.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000027#include "libbb.h"
Eric Andersene76c3b02001-04-05 03:14:39 +000028
maxwen27116ba2015-08-14 21:41:28 +020029#if ENABLE_FEATURE_DMESG_COLOR
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010030#define COLOR_DEFAULT 0
maxwen27116ba2015-08-14 21:41:28 +020031#define COLOR_WHITE 97
32#define COLOR_YELLOW 93
33#define COLOR_ORANGE 33
34#define COLOR_RED 91
35
36static void set_color(int color)
37{
38 printf("%c[%dm", 0x1B, color);
39}
40
41#else
42#define set_color(c) {}
43#endif
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010044
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000045int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000046int dmesg_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000047{
Denys Vlasenko874201f2009-07-10 14:14:14 +020048 int len, level;
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000049 char *buf;
Denys Vlasenko874201f2009-07-10 14:14:14 +020050 unsigned opts;
maxwen27116ba2015-08-14 21:41:28 +020051 int color = 0;
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000052 enum {
Denys Vlasenko874201f2009-07-10 14:14:14 +020053 OPT_c = 1 << 0,
54 OPT_s = 1 << 1,
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010055 OPT_n = 1 << 2,
56 OPT_r = 1 << 3,
maxwen27116ba2015-08-14 21:41:28 +020057 OPT_C = 1 << 4,
58 OPT_end
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000059 };
Erik Andersen1266a131999-12-29 22:19:46 +000060
Denys Vlasenko874201f2009-07-10 14:14:14 +020061 opt_complementary = "s+:n+"; /* numeric */
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010062 opts = getopt32(argv, "cs:n:rC", &len, &level);
Denys Vlasenko874201f2009-07-10 14:14:14 +020063 if (opts & OPT_n) {
64 if (klogctl(8, NULL, (long) level))
Rob Landley7c7b0d72006-06-13 18:31:04 +000065 bb_perror_msg_and_die("klogctl");
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000066 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000068
Denys Vlasenko874201f2009-07-10 14:14:14 +020069 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 Vlasenko7e3a5f52007-11-16 20:18:54 +000076 buf = xmalloc(len);
Denys Vlasenko874201f2009-07-10 14:14:14 +020077 len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000078 if (len < 0)
79 bb_perror_msg_and_die("klogctl");
80 if (len == 0)
81 return EXIT_SUCCESS;
82
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000083
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010084 if ((ENABLE_FEATURE_DMESG_PRETTY || (opts & OPT_C)) && !(opts & OPT_r)) {
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000085 int last = '\n';
maxwen27116ba2015-08-14 21:41:28 +020086 int in = 0;
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000087
Tanguy Pruvot823694d2012-11-18 13:20:29 +010088 /* Skip <[0-9]+> at the start of lines */
Denys Vlasenkof04ca742010-10-19 23:08:33 +020089 while (1) {
90 if (last == '\n' && buf[in] == '<') {
maxwen27116ba2015-08-14 21:41:28 +020091
92#if ENABLE_FEATURE_DMESG_COLOR
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010093 if (opts & OPT_C) {
maxwen27116ba2015-08-14 21:41:28 +020094 char *lvl = buf + in + 1;
Tanguy Pruvota0c20e42012-01-06 16:40:09 +010095 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;
maxwen27116ba2015-08-14 21:41:28 +0200104 case 6: /* common dmesg info */
105 default:
106 color = COLOR_DEFAULT;
Tanguy Pruvota0c20e42012-01-06 16:40:09 +0100107 }
108
maxwen27116ba2015-08-14 21:41:28 +0200109 set_color(color);
Tanguy Pruvota0c20e42012-01-06 16:40:09 +0100110 }
maxwen27116ba2015-08-14 21:41:28 +0200111#endif
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100112 while (buf[in++] != '>' && in < len)
113 ;
114 } else {
115 last = buf[in++];
116 putchar(last);
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000117 }
Denys Vlasenkof04ca742010-10-19 23:08:33 +0200118 if (in >= len)
119 break;
120 }
121 /* Make sure we end with a newline */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000122 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
maxwen27116ba2015-08-14 21:41:28 +0200132 /* Reset default terminal color */
133 if (color)
134 set_color(0);
135
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000136 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000137}