blob: 2166edba33defa4e322cf646fb9128dbe5ea9c2f [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 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000013 * from util-linux -- adapted for busybox by
14 * 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 Andersen2afcbe42003-03-07 17:33:40 +000023#include <errno.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000024#include <sys/klog.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000025
Eric Andersencbe31da2001-02-20 06:14:08 +000026#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000027
Erik Andersene49d5ec2000-02-08 19:58:47 +000028int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000029{
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000030 char *buf, *tmp;
Erik Andersene49d5ec2000-02-08 19:58:47 +000031 int bufsize = 8196;
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000032 int i, n = 0;
33 int c = 3;
Erik Andersen1266a131999-12-29 22:19:46 +000034
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000035 i = bb_getopt_ulflags(argc, argv, "cn:s:", &buf, &tmp);
36 if (i & 1)
37 c = 4;
38 if (i & 2) {
39 c = 8;
40 n = bb_xgetlarg(buf, 10, 0, 10);
41 }
42 if (i & 4)
Eric Andersen2afcbe42003-03-07 17:33:40 +000043 /* I think a 512k max kernel ring buffer is big enough for
44 * anybody, as the default is 16k... Could be wrong though.
45 * If so I'm sure I'll hear about it by the enraged masses*/
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000046 bufsize = bb_xgetlarg(tmp, 10, 4096, 512*1024);
Eric Andersencc8ed391999-10-05 16:24:54 +000047
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000048 if (c == 8) {
49 if (klogctl(c, NULL, n) < 0)
Eric Andersen2afcbe42003-03-07 17:33:40 +000050 goto die_the_death;
51 goto all_done;
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000053
Eric Andersen2afcbe42003-03-07 17:33:40 +000054 buf = xmalloc(bufsize);
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000055 if ((n = klogctl(c, buf, bufsize)) < 0)
Eric Andersen2afcbe42003-03-07 17:33:40 +000056 goto die_the_death;
Erik Andersene49d5ec2000-02-08 19:58:47 +000057
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000058 c = '\n';
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 for (i = 0; i < n; i++) {
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000060 if (c == '\n' && buf[i] == '<') {
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 i++;
62 while (buf[i] >= '0' && buf[i] <= '9')
63 i++;
64 if (buf[i] == '>')
65 i++;
66 }
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000067 c = buf[i];
68 putchar(c);
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 }
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000070 if (c != '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 putchar('\n');
Eric Andersen2afcbe42003-03-07 17:33:40 +000072all_done:
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000073 if (ENABLE_FEATURE_CLEAN_UP) {
Eric Andersen2afcbe42003-03-07 17:33:40 +000074 free(buf);
75 }
Bernhard Reutner-Fischerb256bd32006-03-29 17:26:14 +000076
Matt Kraai3e856ce2000-12-01 02:55:13 +000077 return EXIT_SUCCESS;
Eric Andersen2afcbe42003-03-07 17:33:40 +000078die_the_death:
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_perror_nomsg_and_die();
Eric Andersencc8ed391999-10-05 16:24:54 +000080}