blob: 56e0771f0facb4fd111ab799701b3e75b5d7473a [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/* dmesg.c -- Print out the contents of the kernel ring buffer
3 * Created: Sat Oct 9 16:19:47 1993
4 * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
5 * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
6 * This program comes with ABSOLUTELY NO WARRANTY.
7 * Modifications by Rick Sladkey (jrs@world.std.com)
Eric Andersene77ae3a1999-10-19 20:03:34 +00008 * Larger buffersize 3 June 1998 by Nicolai Langfeldt, based on a patch
9 * by Peeter Joot. This was also suggested by John Hudson.
10 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
11 * - added Native Language Support
12 *
13 * from util-linux -- adapted for busybox by
14 * Erik Andersen <andersee@debian.org>. I ripped out Native Language
15 * Support, replaced getopt, added some gotos for redundant stuff.
Eric Andersen2afcbe42003-03-07 17:33:40 +000016 *
17 * Audited and cleaned up on 7 March 2003 to reduce size of
18 * check error handling by Erik Andersen <andersee@debian.org>
Eric Andersencc8ed391999-10-05 16:24:54 +000019 */
20
Eric Andersencc8ed391999-10-05 16:24:54 +000021#include <stdio.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000022#include <stdlib.h>
Eric Andersened3ef502001-01-27 08:24:39 +000023#include <getopt.h>
Eric Andersen2afcbe42003-03-07 17:33:40 +000024#include <errno.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000025
Eric Andersene77ae3a1999-10-19 20:03:34 +000026#if __GNU_LIBRARY__ < 5
Eric Andersene76c3b02001-04-05 03:14:39 +000027# ifdef __alpha__
28# define klogctl syslog
29# endif
Eric Andersencc8ed391999-10-05 16:24:54 +000030#else
Eric Andersene77ae3a1999-10-19 20:03:34 +000031# include <sys/klog.h>
32#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000033
Eric Andersencbe31da2001-02-20 06:14:08 +000034#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Erik Andersene49d5ec2000-02-08 19:58:47 +000036int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000037{
Pavel Roskin47d49262000-07-17 16:17:19 +000038 char *buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 int bufsize = 8196;
Eric Andersen2afcbe42003-03-07 17:33:40 +000040 int i, n;
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 int level = 0;
42 int lastc;
43 int cmd = 3;
Erik Andersen1266a131999-12-29 22:19:46 +000044
Eric Andersen2afcbe42003-03-07 17:33:40 +000045 while ((i = getopt(argc, argv, "cn:s:")) != EOF) {
46 switch (i) {
47 case 'c':
48 cmd = 4;
49 break;
50 case 'n':
51 cmd = 8;
52 level = bb_xgetlarg(optarg, 10, 0, 10);
53 break;
54 case 's':
55 /* I think a 512k max kernel ring buffer is big enough for
56 * anybody, as the default is 16k... Could be wrong though.
57 * If so I'm sure I'll hear about it by the enraged masses*/
58 bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024);
59 break;
60 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000061 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 }
Eric Andersen2afcbe42003-03-07 17:33:40 +000063 }
Eric Andersencc8ed391999-10-05 16:24:54 +000064
Eric Andersenadd09fd2000-07-14 18:39:08 +000065 if (optind < argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +000066 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 }
Eric Andersencc8ed391999-10-05 16:24:54 +000068
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 if (cmd == 8) {
Matt Kraaia9819b22000-12-22 01:48:07 +000070 if (klogctl(cmd, NULL, level) < 0)
Eric Andersen2afcbe42003-03-07 17:33:40 +000071 goto die_the_death;
72 goto all_done;
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000074
Eric Andersen2afcbe42003-03-07 17:33:40 +000075 buf = xmalloc(bufsize);
Matt Kraaia9819b22000-12-22 01:48:07 +000076 if ((n = klogctl(cmd, buf, bufsize)) < 0)
Eric Andersen2afcbe42003-03-07 17:33:40 +000077 goto die_the_death;
Erik Andersene49d5ec2000-02-08 19:58:47 +000078
79 lastc = '\n';
80 for (i = 0; i < n; i++) {
Eric Andersenafdde3e2000-12-09 16:41:42 +000081 if (lastc == '\n' && buf[i] == '<') {
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 i++;
83 while (buf[i] >= '0' && buf[i] <= '9')
84 i++;
85 if (buf[i] == '>')
86 i++;
87 }
88 lastc = buf[i];
89 putchar(lastc);
90 }
91 if (lastc != '\n')
92 putchar('\n');
Eric Andersen2afcbe42003-03-07 17:33:40 +000093all_done:
94#ifdef CONFIG_FEATURE_CLEAN_UP
95 if (buf) {
96 free(buf);
97 }
98#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000099 return EXIT_SUCCESS;
Eric Andersen2afcbe42003-03-07 17:33:40 +0000100die_the_death:
101 perror_msg_and_die("klogctl");
Eric Andersencc8ed391999-10-05 16:24:54 +0000102}