blob: ed908ede93cd66af1b53d71d80e81ed275494b73 [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 *
Rob Landleye9a7a622006-09-22 02:52:41 +00009 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +000010 */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Rob Landley7c7b0d72006-06-13 18:31:04 +000013#include <unistd.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000014#include <sys/klog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000015
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000016int dmesg_main(int argc, char **argv);
17int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000018{
Rob Landley7c7b0d72006-06-13 18:31:04 +000019 char *size, *level;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000020 int flags = getopt32(argc, argv, "cs:n:", &size, &level);
Erik Andersen1266a131999-12-29 22:19:46 +000021
Rob Landley7c7b0d72006-06-13 18:31:04 +000022 if (flags & 4) {
Denis Vlasenko13858992006-10-08 12:49:22 +000023 if (klogctl(8, NULL, xatoul_range(level, 0, 10)))
Rob Landley7c7b0d72006-06-13 18:31:04 +000024 bb_perror_msg_and_die("klogctl");
25 } else {
26 int len;
27 char *buf;
Eric Andersencc8ed391999-10-05 16:24:54 +000028
Denis Vlasenko13858992006-10-08 12:49:22 +000029 len = (flags & 2) ? xatoul_range(size, 2, INT_MAX) : 16384;
Rob Landley7c7b0d72006-06-13 18:31:04 +000030 buf = xmalloc(len);
31 if (0 > (len = klogctl(3 + (flags & 1), buf, len)))
32 bb_perror_msg_and_die("klogctl");
Rob Landley446129a2006-07-27 16:40:55 +000033
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000034 // Skip <#> at the start of lines, and make sure we end with a newline.
Rob Landley446129a2006-07-27 16:40:55 +000035
36 if (ENABLE_FEATURE_DMESG_PRETTY) {
37 int last = '\n';
38 int in;
39
40 for (in = 0; in<len;) {
41 if (last == '\n' && buf[in] == '<') in += 3;
42 else putchar(last = buf[in++]);
43 }
44 if (last != '\n') putchar('\n');
45 } else {
46 write(1,buf,len);
47 if (len && buf[len-1]!='\n') putchar('\n');
48 }
49
50 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000052
Rob Landley7c7b0d72006-06-13 18:31:04 +000053 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000054}