blob: e543446c11c87e02c6d92c28518eeda796c1130c [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
13//usage: "[-c] [-n LEVEL] [-s SIZE]"
14//usage:#define dmesg_full_usage "\n\n"
15//usage: "Print or control the kernel ring buffer\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -c Clear ring buffer after printing"
17//usage: "\n -n LEVEL Set console logging level"
18//usage: "\n -s SIZE Buffer size"
Peter Korsgaard864d1b72015-08-24 15:54:49 +020019//usage: "\n -r Print raw message buffer"
Pere Orga5bc8c002011-04-11 03:29:49 +020020
Eric Andersen85e5e722003-07-22 08:56:55 +000021#include <sys/klog.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000022#include "libbb.h"
Eric Andersene76c3b02001-04-05 03:14:39 +000023
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000024int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000025int dmesg_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000026{
Denys Vlasenko874201f2009-07-10 14:14:14 +020027 int len, level;
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000028 char *buf;
Denys Vlasenko874201f2009-07-10 14:14:14 +020029 unsigned opts;
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000030 enum {
Denys Vlasenko874201f2009-07-10 14:14:14 +020031 OPT_c = 1 << 0,
32 OPT_s = 1 << 1,
Peter Korsgaard864d1b72015-08-24 15:54:49 +020033 OPT_n = 1 << 2,
34 OPT_r = 1 << 3
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000035 };
Erik Andersen1266a131999-12-29 22:19:46 +000036
Denys Vlasenko874201f2009-07-10 14:14:14 +020037 opt_complementary = "s+:n+"; /* numeric */
Peter Korsgaard864d1b72015-08-24 15:54:49 +020038 opts = getopt32(argv, "cs:n:r", &len, &level);
Denys Vlasenko874201f2009-07-10 14:14:14 +020039 if (opts & OPT_n) {
40 if (klogctl(8, NULL, (long) level))
Rob Landley7c7b0d72006-06-13 18:31:04 +000041 bb_perror_msg_and_die("klogctl");
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000042 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000044
Denys Vlasenko874201f2009-07-10 14:14:14 +020045 if (!(opts & OPT_s))
46 len = klogctl(10, NULL, 0); /* read ring buffer size */
47 if (len < 16*1024)
48 len = 16*1024;
49 if (len > 16*1024*1024)
50 len = 16*1024*1024;
51
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000052 buf = xmalloc(len);
Denys Vlasenko874201f2009-07-10 14:14:14 +020053 len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000054 if (len < 0)
55 bb_perror_msg_and_die("klogctl");
56 if (len == 0)
57 return EXIT_SUCCESS;
58
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000059
Peter Korsgaard864d1b72015-08-24 15:54:49 +020060 if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000061 int last = '\n';
62 int in = 0;
63
Peter Korsgaard3917fa32013-01-06 00:07:17 +010064 /* Skip <[0-9]+> at the start of lines */
Denys Vlasenkof04ca742010-10-19 23:08:33 +020065 while (1) {
66 if (last == '\n' && buf[in] == '<') {
Peter Korsgaard3917fa32013-01-06 00:07:17 +010067 while (buf[in++] != '>' && in < len)
68 ;
69 } else {
70 last = buf[in++];
71 putchar(last);
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000072 }
Denys Vlasenkof04ca742010-10-19 23:08:33 +020073 if (in >= len)
74 break;
75 }
76 /* Make sure we end with a newline */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000077 if (last != '\n')
78 bb_putchar('\n');
79 } else {
80 full_write(STDOUT_FILENO, buf, len);
81 if (buf[len-1] != '\n')
82 bb_putchar('\n');
83 }
84
85 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
86
87 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000088}