blob: 99d053a72640e5fe887eb31fe17cae699ec5af7a [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 Andersene77ae3a1999-10-19 20:03:34 +000018#include "internal.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) {
72 n = klogctl(cmd, NULL, level);
73 if (n < 0) {
74 goto klogctl_error;
75 }
76 exit(TRUE);
77 }
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);
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 n = klogctl(cmd, buf, bufsize);
83 if (n < 0) {
84 goto klogctl_error;
85 }
86
87 lastc = '\n';
88 for (i = 0; i < n; i++) {
89 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
90 i++;
91 while (buf[i] >= '0' && buf[i] <= '9')
92 i++;
93 if (buf[i] == '>')
94 i++;
95 }
96 lastc = buf[i];
97 putchar(lastc);
98 }
99 if (lastc != '\n')
100 putchar('\n');
101 exit(TRUE);
102 end:
103 usage(dmesg_usage);
104 exit(FALSE);
105 klogctl_error:
106 perror("klogctl");
Eric Andersenb6106152000-06-19 17:25:40 +0000107 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000108}