blob: e38fd55551cebfb6a87c862b0f84d73dc5e914ba [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/* dmesg.c -- Print out the contents of the kernel ring buffer
2 * Created: Sat Oct 9 16:19:47 1993
3 * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu
4 * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu)
5 * This program comes with ABSOLUTELY NO WARRANTY.
6 * Modifications by Rick Sladkey (jrs@world.std.com)
Eric Andersene77ae3a1999-10-19 20:03:34 +00007 * Larger buffersize 3 June 1998 by Nicolai Langfeldt, based on a patch
8 * by Peeter Joot. This was also suggested by John Hudson.
9 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10 * - added Native Language Support
11 *
12 * from util-linux -- adapted for busybox by
13 * Erik Andersen <andersee@debian.org>. I ripped out Native Language
14 * Support, replaced getopt, added some gotos for redundant stuff.
Eric Andersencc8ed391999-10-05 16:24:54 +000015 */
16
Eric Andersene77ae3a1999-10-19 20:03:34 +000017#include "internal.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000018#include <linux/unistd.h>
19#include <stdio.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000020#include <getopt.h>
21#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000022
Eric Andersene77ae3a1999-10-19 20:03:34 +000023#if __GNU_LIBRARY__ < 5
Eric Andersencc8ed391999-10-05 16:24:54 +000024
Eric Andersene77ae3a1999-10-19 20:03:34 +000025#ifndef __alpha__
26# define __NR_klogctl __NR_syslog
27 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
28#else /* __alpha__ */
29#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 Andersencc8ed391999-10-05 16:24:54 +000035
Eric Andersene77ae3a1999-10-19 20:03:34 +000036static const char dmesg_usage[] = "dmesg [-c] [-n level] [-s bufsize]\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Eric Andersene77ae3a1999-10-19 20:03:34 +000038int dmesg_main( int argc, char** argv )
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Eric Andersene77ae3a1999-10-19 20:03:34 +000040 char *buf;
41 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 int stopDoingThat;
48
49 argc--;
50 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Eric Andersen3cf52d11999-10-12 22:26:06 +000052 /* Parse any options */
53 while (argc && **argv == '-') {
Erik Andersen1266a131999-12-29 22:19:46 +000054 stopDoingThat = FALSE;
55 while (stopDoingThat == FALSE && *++(*argv)) {
Eric Andersen3cf52d11999-10-12 22:26:06 +000056 switch (**argv) {
57 case 'c':
58 cmd = 4;
59 break;
60 case 'n':
61 cmd = 8;
62 if (--argc == 0)
63 goto end;
64 level = atoi (*(++argv));
Erik Andersen1266a131999-12-29 22:19:46 +000065 if (--argc > 0)
66 ++argv;
67 stopDoingThat = TRUE;
Eric Andersen3cf52d11999-10-12 22:26:06 +000068 break;
Eric Andersene77ae3a1999-10-19 20:03:34 +000069 case 's':
70 if (--argc == 0)
71 goto end;
72 bufsize = atoi (*(++argv));
Erik Andersen1266a131999-12-29 22:19:46 +000073 if (--argc > 0)
74 ++argv;
75 stopDoingThat = TRUE;
Eric Andersene77ae3a1999-10-19 20:03:34 +000076 break;
Eric Andersen3cf52d11999-10-12 22:26:06 +000077 default:
78 goto end;
79 }
Erik Andersen1266a131999-12-29 22:19:46 +000080 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000081 }
Eric Andersene77ae3a1999-10-19 20:03:34 +000082
83 if (argc > 1) {
84 goto end;
85 }
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Eric Andersene77ae3a1999-10-19 20:03:34 +000087 if (cmd == 8) {
88 n = klogctl( cmd, NULL, level );
89 if (n < 0) {
90 goto klogctl_error;
91 }
92 exit( TRUE );
93 }
Eric Andersencc8ed391999-10-05 16:24:54 +000094
Eric Andersene77ae3a1999-10-19 20:03:34 +000095 if (bufsize < 4096) bufsize = 4096;
96 buf = (char*)malloc(bufsize);
97 n = klogctl( cmd, buf, bufsize );
98 if (n < 0) {
99 goto klogctl_error;
100 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000101
Eric Andersene77ae3a1999-10-19 20:03:34 +0000102 lastc = '\n';
103 for (i = 0; i < n; i++) {
104 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
105 i++;
106 while (buf[i] >= '0' && buf[i] <= '9')
Eric Andersencc8ed391999-10-05 16:24:54 +0000107 i++;
Eric Andersene77ae3a1999-10-19 20:03:34 +0000108 if (buf[i] == '>')
109 i++;
110 }
111 lastc = buf[i];
112 putchar( lastc );
113 }
114 if (lastc != '\n')
115 putchar( '\n' );
116 exit( TRUE);
117end:
Eric Andersenb0e9a701999-10-18 22:28:26 +0000118 usage( dmesg_usage);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000119 exit (FALSE);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000120klogctl_error:
121 perror( "klogctl" );
122 exit( FALSE );
123
Eric Andersencc8ed391999-10-05 16:24:54 +0000124}