blob: c220d90184f9e76d06a3401d1fd2e0dfde67ab10 [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 Andersencc8ed391999-10-05 16:24:54 +000021
Eric Andersene77ae3a1999-10-19 20:03:34 +000022#if __GNU_LIBRARY__ < 5
Eric Andersencc8ed391999-10-05 16:24:54 +000023
Eric Andersene77ae3a1999-10-19 20:03:34 +000024#ifndef __alpha__
25# define __NR_klogctl __NR_syslog
Erik Andersene49d5ec2000-02-08 19:58:47 +000026static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
27#else /* __alpha__ */
Eric Andersene77ae3a1999-10-19 20:03:34 +000028#define klogctl syslog
29#endif
30
Eric Andersencc8ed391999-10-05 16:24:54 +000031#else
Eric Andersene77ae3a1999-10-19 20:03:34 +000032# include <sys/klog.h>
33#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Erik Andersene49d5ec2000-02-08 19:58:47 +000035int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000036{
Pavel Roskin47d49262000-07-17 16:17:19 +000037 char *buf;
38 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 int bufsize = 8196;
40 int i;
41 int n;
42 int level = 0;
43 int lastc;
44 int cmd = 3;
Erik Andersen1266a131999-12-29 22:19:46 +000045
Eric Andersenadd09fd2000-07-14 18:39:08 +000046 while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
47 switch (c) {
48 case 'c':
49 cmd = 4;
50 break;
51 case 'n':
52 cmd = 8;
53 if (optarg == NULL)
54 usage(dmesg_usage);
55 level = atoi(optarg);
56 break;
57 case 's':
58 if (optarg == NULL)
59 usage(dmesg_usage);
60 bufsize = atoi(optarg);
61 break;
62 default:
63 usage(dmesg_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 }
Eric Andersenadd09fd2000-07-14 18:39:08 +000065 }
Eric Andersencc8ed391999-10-05 16:24:54 +000066
Eric Andersenadd09fd2000-07-14 18:39:08 +000067 if (optind < argc) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 goto end;
69 }
Eric Andersencc8ed391999-10-05 16:24:54 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 if (cmd == 8) {
Matt Kraaia9819b22000-12-22 01:48:07 +000072 if (klogctl(cmd, NULL, level) < 0)
73 perror_msg_and_die("klogctl");
Matt Kraai3e856ce2000-12-01 02:55:13 +000074 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000076
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 if (bufsize < 4096)
78 bufsize = 4096;
Erik Andersen0d068a22000-03-21 22:32:57 +000079 buf = (char *) xmalloc(bufsize);
Matt Kraaia9819b22000-12-22 01:48:07 +000080 if ((n = klogctl(cmd, buf, bufsize)) < 0)
81 perror_msg_and_die("klogctl");
Erik Andersene49d5ec2000-02-08 19:58:47 +000082
83 lastc = '\n';
84 for (i = 0; i < n; i++) {
Eric Andersenafdde3e2000-12-09 16:41:42 +000085 if (lastc == '\n' && buf[i] == '<') {
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 i++;
87 while (buf[i] >= '0' && buf[i] <= '9')
88 i++;
89 if (buf[i] == '>')
90 i++;
91 }
92 lastc = buf[i];
93 putchar(lastc);
94 }
95 if (lastc != '\n')
96 putchar('\n');
Matt Kraai3e856ce2000-12-01 02:55:13 +000097 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 end:
99 usage(dmesg_usage);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000100 return EXIT_FAILURE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000101}