blob: 73de6d1ae9ba84bf65f72f34b1f713b97fd208be [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 Andersencc8ed391999-10-05 16:24:54 +000016 */
17
Eric Andersencc8ed391999-10-05 16:24:54 +000018#include <stdio.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000019#include <stdlib.h>
Eric Andersened3ef502001-01-27 08:24:39 +000020#include <getopt.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000021
Eric Andersene77ae3a1999-10-19 20:03:34 +000022#if __GNU_LIBRARY__ < 5
Eric Andersene76c3b02001-04-05 03:14:39 +000023# ifdef __alpha__
24# define klogctl syslog
25# endif
Eric Andersencc8ed391999-10-05 16:24:54 +000026#else
Eric Andersene77ae3a1999-10-19 20:03:34 +000027# include <sys/klog.h>
28#endif
Eric Andersene76c3b02001-04-05 03:14:39 +000029
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Erik Andersene49d5ec2000-02-08 19:58:47 +000032int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000033{
Pavel Roskin47d49262000-07-17 16:17:19 +000034 char *buf;
35 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 int bufsize = 8196;
37 int i;
38 int n;
39 int level = 0;
40 int lastc;
41 int cmd = 3;
Erik Andersen1266a131999-12-29 22:19:46 +000042
Eric Andersenadd09fd2000-07-14 18:39:08 +000043 while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
44 switch (c) {
45 case 'c':
46 cmd = 4;
47 break;
48 case 'n':
49 cmd = 8;
50 if (optarg == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +000051 show_usage();
Eric Andersenadd09fd2000-07-14 18:39:08 +000052 level = atoi(optarg);
53 break;
54 case 's':
55 if (optarg == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +000056 show_usage();
Eric Andersenadd09fd2000-07-14 18:39:08 +000057 bufsize = atoi(optarg);
58 break;
59 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000060 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 }
Eric Andersenadd09fd2000-07-14 18:39:08 +000062 }
Eric Andersencc8ed391999-10-05 16:24:54 +000063
Eric Andersenadd09fd2000-07-14 18:39:08 +000064 if (optind < argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +000065 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 }
Eric Andersencc8ed391999-10-05 16:24:54 +000067
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 if (cmd == 8) {
Matt Kraaia9819b22000-12-22 01:48:07 +000069 if (klogctl(cmd, NULL, level) < 0)
70 perror_msg_and_die("klogctl");
Matt Kraai3e856ce2000-12-01 02:55:13 +000071 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 if (bufsize < 4096)
75 bufsize = 4096;
Erik Andersen0d068a22000-03-21 22:32:57 +000076 buf = (char *) xmalloc(bufsize);
Matt Kraaia9819b22000-12-22 01:48:07 +000077 if ((n = klogctl(cmd, buf, bufsize)) < 0)
78 perror_msg_and_die("klogctl");
Erik Andersene49d5ec2000-02-08 19:58:47 +000079
80 lastc = '\n';
81 for (i = 0; i < n; i++) {
Eric Andersenafdde3e2000-12-09 16:41:42 +000082 if (lastc == '\n' && buf[i] == '<') {
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 i++;
84 while (buf[i] >= '0' && buf[i] <= '9')
85 i++;
86 if (buf[i] == '>')
87 i++;
88 }
89 lastc = buf[i];
90 putchar(lastc);
91 }
92 if (lastc != '\n')
93 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +000094 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000095}