blob: df2bce7133b6afb76ed25940560c7cfce63b7f2d [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 Andersen59b9e872000-05-10 05:05:45 +000035static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n"
36#ifndef BB_FEATURE_TRIVIAL_HELP
37 "\nPrints or controls the kernel ring buffer\n\n"
38 "Options:\n"
39 "\t-c\t\tClears the ring buffer's contents after printing\n"
40 "\t-n LEVEL\tSets console logging level\n"
41 "\t-s SIZE\t\tUse a buffer of size SIZE\n"
42#endif
43 ;
Eric Andersencc8ed391999-10-05 16:24:54 +000044
Erik Andersene49d5ec2000-02-08 19:58:47 +000045int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000046{
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 char *buf;
48 int bufsize = 8196;
49 int i;
50 int n;
51 int level = 0;
52 int lastc;
53 int cmd = 3;
54 int stopDoingThat;
Erik Andersen1266a131999-12-29 22:19:46 +000055
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 argc--;
57 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 /* Parse any options */
60 while (argc && **argv == '-') {
61 stopDoingThat = FALSE;
62 while (stopDoingThat == FALSE && *++(*argv)) {
63 switch (**argv) {
64 case 'c':
65 cmd = 4;
66 break;
67 case 'n':
68 cmd = 8;
69 if (--argc == 0)
70 goto end;
71 level = atoi(*(++argv));
72 if (--argc > 0)
73 ++argv;
74 stopDoingThat = TRUE;
75 break;
76 case 's':
77 if (--argc == 0)
78 goto end;
79 bufsize = atoi(*(++argv));
80 if (--argc > 0)
81 ++argv;
82 stopDoingThat = TRUE;
83 break;
84 default:
85 goto end;
86 }
87 }
Erik Andersen1266a131999-12-29 22:19:46 +000088 }
Eric Andersencc8ed391999-10-05 16:24:54 +000089
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 if (argc > 1) {
91 goto end;
92 }
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 if (cmd == 8) {
95 n = klogctl(cmd, NULL, level);
96 if (n < 0) {
97 goto klogctl_error;
98 }
99 exit(TRUE);
100 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000101
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 if (bufsize < 4096)
103 bufsize = 4096;
Erik Andersen0d068a22000-03-21 22:32:57 +0000104 buf = (char *) xmalloc(bufsize);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 n = klogctl(cmd, buf, bufsize);
106 if (n < 0) {
107 goto klogctl_error;
108 }
109
110 lastc = '\n';
111 for (i = 0; i < n; i++) {
112 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
113 i++;
114 while (buf[i] >= '0' && buf[i] <= '9')
115 i++;
116 if (buf[i] == '>')
117 i++;
118 }
119 lastc = buf[i];
120 putchar(lastc);
121 }
122 if (lastc != '\n')
123 putchar('\n');
124 exit(TRUE);
125 end:
126 usage(dmesg_usage);
127 exit(FALSE);
128 klogctl_error:
129 perror("klogctl");
Eric Andersenb6106152000-06-19 17:25:40 +0000130 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000131}