blob: 840358c85771173052a9d2de171291ac8b6a7557 [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>
7 * Copyright 2006 Bernhard Fischer <rep.nop@aon.at>
Eric Andersen2afcbe42003-03-07 17:33:40 +00008 *
Bernhard Reutner-Fischer09533362008-05-20 08:27:26 +00009 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +000010 */
11
Eric Andersen85e5e722003-07-22 08:56:55 +000012#include <sys/klog.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000013#include "libbb.h"
Eric Andersene76c3b02001-04-05 03:14:39 +000014
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000016int dmesg_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000017{
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000018 int len;
19 char *buf;
Rob Landley7c7b0d72006-06-13 18:31:04 +000020 char *size, *level;
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000021 unsigned flags = getopt32(argv, "cs:n:", &size, &level);
22 enum {
23 OPT_c = 1<<0,
24 OPT_s = 1<<1,
25 OPT_n = 1<<2
26 };
Erik Andersen1266a131999-12-29 22:19:46 +000027
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000028 if (flags & OPT_n) {
Denis Vlasenko13858992006-10-08 12:49:22 +000029 if (klogctl(8, NULL, xatoul_range(level, 0, 10)))
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
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000034 len = (flags & OPT_s) ? xatoul_range(size, 2, INT_MAX) : 16384;
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000035 buf = xmalloc(len);
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000036 len = klogctl(3 + (flags & OPT_c), buf, len);
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000037 if (len < 0)
38 bb_perror_msg_and_die("klogctl");
39 if (len == 0)
40 return EXIT_SUCCESS;
41
42 /* Skip <#> at the start of lines, and make sure we end with a newline. */
43
44 if (ENABLE_FEATURE_DMESG_PRETTY) {
45 int last = '\n';
46 int in = 0;
47
48 do {
49 if (last == '\n' && buf[in] == '<')
50 in += 3;
51 else {
52 last = buf[in++];
53 bb_putchar(last);
54 }
55 } while (in < len);
56 if (last != '\n')
57 bb_putchar('\n');
58 } else {
59 full_write(STDOUT_FILENO, buf, len);
60 if (buf[len-1] != '\n')
61 bb_putchar('\n');
62 }
63
64 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
65
66 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000067}