blob: 2c85ed3e97c7706e65176c47930ea8fe0782ac63 [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 Andersened3ef502001-01-27 08:24:39 +000023#include <sys/syscall.h>
24#include <linux/unistd.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000025#ifndef __alpha__
26# define __NR_klogctl __NR_syslog
Erik Andersene49d5ec2000-02-08 19:58:47 +000027static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
28#else /* __alpha__ */
Eric Andersene77ae3a1999-10-19 20:03:34 +000029#define klogctl syslog
30#endif
31
Eric Andersencc8ed391999-10-05 16:24:54 +000032#else
Eric Andersene77ae3a1999-10-19 20:03:34 +000033# include <sys/klog.h>
34#endif
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Erik Andersene49d5ec2000-02-08 19:58:47 +000037int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Pavel Roskin47d49262000-07-17 16:17:19 +000039 char *buf;
40 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 int bufsize = 8196;
42 int i;
43 int n;
44 int level = 0;
45 int lastc;
46 int cmd = 3;
Erik Andersen1266a131999-12-29 22:19:46 +000047
Eric Andersenadd09fd2000-07-14 18:39:08 +000048 while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
49 switch (c) {
50 case 'c':
51 cmd = 4;
52 break;
53 case 'n':
54 cmd = 8;
55 if (optarg == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +000056 show_usage();
Eric Andersenadd09fd2000-07-14 18:39:08 +000057 level = atoi(optarg);
58 break;
59 case 's':
60 if (optarg == NULL)
Eric Andersen67991cf2001-02-14 21:23:06 +000061 show_usage();
Eric Andersenadd09fd2000-07-14 18:39:08 +000062 bufsize = atoi(optarg);
63 break;
64 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000065 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 }
Eric Andersenadd09fd2000-07-14 18:39:08 +000067 }
Eric Andersencc8ed391999-10-05 16:24:54 +000068
Eric Andersenadd09fd2000-07-14 18:39:08 +000069 if (optind < argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +000070 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 }
Eric Andersencc8ed391999-10-05 16:24:54 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 if (cmd == 8) {
Matt Kraaia9819b22000-12-22 01:48:07 +000074 if (klogctl(cmd, NULL, level) < 0)
75 perror_msg_and_die("klogctl");
Matt Kraai3e856ce2000-12-01 02:55:13 +000076 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000078
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 if (bufsize < 4096)
80 bufsize = 4096;
Erik Andersen0d068a22000-03-21 22:32:57 +000081 buf = (char *) xmalloc(bufsize);
Matt Kraaia9819b22000-12-22 01:48:07 +000082 if ((n = klogctl(cmd, buf, bufsize)) < 0)
83 perror_msg_and_die("klogctl");
Erik Andersene49d5ec2000-02-08 19:58:47 +000084
85 lastc = '\n';
86 for (i = 0; i < n; i++) {
Eric Andersenafdde3e2000-12-09 16:41:42 +000087 if (lastc == '\n' && buf[i] == '<') {
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 i++;
89 while (buf[i] >= '0' && buf[i] <= '9')
90 i++;
91 if (buf[i] == '>')
92 i++;
93 }
94 lastc = buf[i];
95 putchar(lastc);
96 }
97 if (lastc != '\n')
98 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +000099 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000100}