blob: 06a03d3fb76af17c29b101ca56a87de0bdd9feae [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 */
Eric Andersen85e5e722003-07-22 08:56:55 +000011#include <sys/klog.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000012#include "libbb.h"
Eric Andersene76c3b02001-04-05 03:14:39 +000013
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000014int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000015int dmesg_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000016{
Denys Vlasenko874201f2009-07-10 14:14:14 +020017 int len, level;
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000018 char *buf;
Denys Vlasenko874201f2009-07-10 14:14:14 +020019 unsigned opts;
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000020 enum {
Denys Vlasenko874201f2009-07-10 14:14:14 +020021 OPT_c = 1 << 0,
22 OPT_s = 1 << 1,
23 OPT_n = 1 << 2
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000024 };
Erik Andersen1266a131999-12-29 22:19:46 +000025
Denys Vlasenko874201f2009-07-10 14:14:14 +020026 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 Landley7c7b0d72006-06-13 18:31:04 +000030 bb_perror_msg_and_die("klogctl");
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000031 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000032 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000033
Denys Vlasenko874201f2009-07-10 14:14:14 +020034 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 Vlasenko7e3a5f52007-11-16 20:18:54 +000041 buf = xmalloc(len);
Denys Vlasenko874201f2009-07-10 14:14:14 +020042 len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000043 if (len < 0)
44 bb_perror_msg_and_die("klogctl");
45 if (len == 0)
46 return EXIT_SUCCESS;
47
Denys Vlasenko874201f2009-07-10 14:14:14 +020048 /* Skip <#> at the start of lines, and make sure we end with a newline */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000049
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 Andersencc8ed391999-10-05 16:24:54 +000073}