blob: b7d17840f697dfff271094a18f796c04ab88a803 [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
Eric Andersencb81e642003-07-14 21:21:08 +000014 * Erik Andersen <andersen@codepoet.org>. I ripped out Native Language
Eric Andersene77ae3a1999-10-19 20:03:34 +000015 * 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
Eric Andersencb81e642003-07-14 21:21:08 +000018 * check error handling by Erik Andersen <andersen@codepoet.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 Andersen85e5e722003-07-22 08:56:55 +000025#include <sys/klog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000026
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000028
Erik Andersene49d5ec2000-02-08 19:58:47 +000029int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000030{
Eric Anderseneef23172003-07-30 06:52:33 +000031 char *buf
32#ifdef CONFIG_FEATURE_CLEAN_UP
33 = NULL
34#endif
35 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 int bufsize = 8196;
Eric Andersen2afcbe42003-03-07 17:33:40 +000037 int i, n;
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 int level = 0;
39 int lastc;
40 int cmd = 3;
Erik Andersen1266a131999-12-29 22:19:46 +000041
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 while ((i = getopt(argc, argv, "cn:s:")) > 0) {
Eric Andersen2afcbe42003-03-07 17:33:40 +000043 switch (i) {
44 case 'c':
45 cmd = 4;
46 break;
47 case 'n':
48 cmd = 8;
49 level = bb_xgetlarg(optarg, 10, 0, 10);
50 break;
51 case 's':
52 /* I think a 512k max kernel ring buffer is big enough for
53 * anybody, as the default is 16k... Could be wrong though.
54 * If so I'm sure I'll hear about it by the enraged masses*/
55 bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024);
56 break;
57 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 }
Eric Andersen2afcbe42003-03-07 17:33:40 +000060 }
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Eric Andersenadd09fd2000-07-14 18:39:08 +000062 if (optind < argc) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 }
Eric Andersencc8ed391999-10-05 16:24:54 +000065
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 if (cmd == 8) {
Matt Kraaia9819b22000-12-22 01:48:07 +000067 if (klogctl(cmd, NULL, level) < 0)
Eric Andersen2afcbe42003-03-07 17:33:40 +000068 goto die_the_death;
69 goto all_done;
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000071
Eric Andersen2afcbe42003-03-07 17:33:40 +000072 buf = xmalloc(bufsize);
Matt Kraaia9819b22000-12-22 01:48:07 +000073 if ((n = klogctl(cmd, buf, bufsize)) < 0)
Eric Andersen2afcbe42003-03-07 17:33:40 +000074 goto die_the_death;
Erik Andersene49d5ec2000-02-08 19:58:47 +000075
76 lastc = '\n';
77 for (i = 0; i < n; i++) {
Eric Andersenafdde3e2000-12-09 16:41:42 +000078 if (lastc == '\n' && buf[i] == '<') {
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 i++;
80 while (buf[i] >= '0' && buf[i] <= '9')
81 i++;
82 if (buf[i] == '>')
83 i++;
84 }
85 lastc = buf[i];
86 putchar(lastc);
87 }
88 if (lastc != '\n')
89 putchar('\n');
Eric Andersen2afcbe42003-03-07 17:33:40 +000090all_done:
91#ifdef CONFIG_FEATURE_CLEAN_UP
92 if (buf) {
93 free(buf);
94 }
95#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000096 return EXIT_SUCCESS;
Eric Andersen2afcbe42003-03-07 17:33:40 +000097die_the_death:
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_perror_nomsg_and_die();
Eric Andersencc8ed391999-10-05 16:24:54 +000099}