blob: c21f84c9a02cb6e9bd8347bde2c6bb4c7ccb0d1c [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 Andersen3570a342000-09-25 21:45:58 +000018#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000019#include <stdio.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000020#include <stdlib.h>
Eric Andersened3ef502001-01-27 08:24:39 +000021#include <getopt.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000022
Eric Andersene77ae3a1999-10-19 20:03:34 +000023#if __GNU_LIBRARY__ < 5
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <sys/syscall.h>
25#include <linux/unistd.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000026#ifndef __alpha__
27# define __NR_klogctl __NR_syslog
Erik Andersene49d5ec2000-02-08 19:58:47 +000028static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
29#else /* __alpha__ */
Eric Andersene77ae3a1999-10-19 20:03:34 +000030#define klogctl syslog
31#endif
32
Eric Andersencc8ed391999-10-05 16:24:54 +000033#else
Eric Andersene77ae3a1999-10-19 20:03:34 +000034# include <sys/klog.h>
35#endif
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)
56 usage(dmesg_usage);
57 level = atoi(optarg);
58 break;
59 case 's':
60 if (optarg == NULL)
61 usage(dmesg_usage);
62 bufsize = atoi(optarg);
63 break;
64 default:
65 usage(dmesg_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) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 goto end;
71 }
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;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 end:
101 usage(dmesg_usage);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000102 return EXIT_FAILURE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000103}