blob: 34519dfd16fc9e5bfc5a3867e64171e9b1ced5d6 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Rob Landleyfc367f92006-07-27 15:02:58 +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 Landley7c7b0d72006-06-13 18:31:04 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +000010 */
11
Rob Landley7c7b0d72006-06-13 18:31:04 +000012#include "busybox.h"
13#include <unistd.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000014#include <sys/klog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000015
Rob Landley7c7b0d72006-06-13 18:31:04 +000016int dmesg_main(int argc, char *argv[])
Eric Andersencc8ed391999-10-05 16:24:54 +000017{
Rob Landley7c7b0d72006-06-13 18:31:04 +000018 char *size, *level;
Rob Landleyaa516a62006-07-06 16:41:56 +000019 int flags = bb_getopt_ulflags(argc, argv, "cs:n:", &size, &level);
Erik Andersen1266a131999-12-29 22:19:46 +000020
Rob Landley7c7b0d72006-06-13 18:31:04 +000021 if (flags & 4) {
Mike Frysingera7881112006-07-27 03:00:49 +000022 if (klogctl(8, NULL, bb_xgetlarg(level, 10, 0, 10)))
Rob Landley7c7b0d72006-06-13 18:31:04 +000023 bb_perror_msg_and_die("klogctl");
24 } else {
25 int len;
26 char *buf;
Eric Andersencc8ed391999-10-05 16:24:54 +000027
Rob Landleyaa516a62006-07-06 16:41:56 +000028 len = (flags & 2) ? bb_xgetlarg(size, 10, 2, INT_MAX) : 16384;
Rob Landley7c7b0d72006-06-13 18:31:04 +000029 buf = xmalloc(len);
30 if (0 > (len = klogctl(3 + (flags & 1), buf, len)))
31 bb_perror_msg_and_die("klogctl");
Rob Landley446129a2006-07-27 16:40:55 +000032
33 // Skip <#> at the start of lines, and make sure we end with a newline.
34
35 if (ENABLE_FEATURE_DMESG_PRETTY) {
36 int last = '\n';
37 int in;
38
39 for (in = 0; in<len;) {
40 if (last == '\n' && buf[in] == '<') in += 3;
41 else putchar(last = buf[in++]);
42 }
43 if (last != '\n') putchar('\n');
44 } else {
45 write(1,buf,len);
46 if (len && buf[len-1]!='\n') putchar('\n');
47 }
48
49 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000051
Rob Landley7c7b0d72006-06-13 18:31:04 +000052 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000053}