blob: 2bbf43a12b86dd98e4164c337a49a274808472d6 [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 <linux/unistd.h>
20#include <stdio.h>
Eric Andersene77ae3a1999-10-19 20:03:34 +000021#include <getopt.h>
22#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000023
Eric Andersene77ae3a1999-10-19 20:03:34 +000024#if __GNU_LIBRARY__ < 5
Eric Andersencc8ed391999-10-05 16:24:54 +000025
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
Eric Andersene77ae3a1999-10-19 20:03:34 +000037static const char dmesg_usage[] = "dmesg [-c] [-n level] [-s bufsize]\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Erik Andersene49d5ec2000-02-08 19:58:47 +000039int dmesg_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000040{
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 char *buf;
42 int bufsize = 8196;
43 int i;
44 int n;
45 int level = 0;
46 int lastc;
47 int cmd = 3;
48 int stopDoingThat;
Erik Andersen1266a131999-12-29 22:19:46 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 argc--;
51 argv++;
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 /* Parse any options */
54 while (argc && **argv == '-') {
55 stopDoingThat = FALSE;
56 while (stopDoingThat == FALSE && *++(*argv)) {
57 switch (**argv) {
58 case 'c':
59 cmd = 4;
60 break;
61 case 'n':
62 cmd = 8;
63 if (--argc == 0)
64 goto end;
65 level = atoi(*(++argv));
66 if (--argc > 0)
67 ++argv;
68 stopDoingThat = TRUE;
69 break;
70 case 's':
71 if (--argc == 0)
72 goto end;
73 bufsize = atoi(*(++argv));
74 if (--argc > 0)
75 ++argv;
76 stopDoingThat = TRUE;
77 break;
78 default:
79 goto end;
80 }
81 }
Erik Andersen1266a131999-12-29 22:19:46 +000082 }
Eric Andersencc8ed391999-10-05 16:24:54 +000083
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 if (argc > 1) {
85 goto end;
86 }
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 if (cmd == 8) {
89 n = klogctl(cmd, NULL, level);
90 if (n < 0) {
91 goto klogctl_error;
92 }
93 exit(TRUE);
94 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000095
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 if (bufsize < 4096)
97 bufsize = 4096;
Erik Andersen0d068a22000-03-21 22:32:57 +000098 buf = (char *) xmalloc(bufsize);
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 n = klogctl(cmd, buf, bufsize);
100 if (n < 0) {
101 goto klogctl_error;
102 }
103
104 lastc = '\n';
105 for (i = 0; i < n; i++) {
106 if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
107 i++;
108 while (buf[i] >= '0' && buf[i] <= '9')
109 i++;
110 if (buf[i] == '>')
111 i++;
112 }
113 lastc = buf[i];
114 putchar(lastc);
115 }
116 if (lastc != '\n')
117 putchar('\n');
118 exit(TRUE);
119 end:
120 usage(dmesg_usage);
121 exit(FALSE);
122 klogctl_error:
123 perror("klogctl");
124 exit(FALSE);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000125
Eric Andersencc8ed391999-10-05 16:24:54 +0000126}