Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* 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 Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 8 | * 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 18 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | #include <linux/unistd.h> |
| 20 | #include <stdio.h> |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 21 | #include <getopt.h> |
| 22 | #include <stdlib.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 24 | #if __GNU_LIBRARY__ < 5 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 26 | #ifndef __alpha__ |
| 27 | # define __NR_klogctl __NR_syslog |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 28 | static inline _syscall3(int, klogctl, int, type, char *, b, int, len); |
| 29 | #else /* __alpha__ */ |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 30 | #define klogctl syslog |
| 31 | #endif |
| 32 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | #else |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 34 | # include <sys/klog.h> |
| 35 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 37 | static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n" |
| 38 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 39 | "\nPrints or controls the kernel ring buffer\n\n" |
| 40 | "Options:\n" |
| 41 | "\t-c\t\tClears the ring buffer's contents after printing\n" |
| 42 | "\t-n LEVEL\tSets console logging level\n" |
| 43 | "\t-s SIZE\t\tUse a buffer of size SIZE\n" |
| 44 | #endif |
| 45 | ; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | int dmesg_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | char *buf; |
| 50 | int bufsize = 8196; |
| 51 | int i; |
| 52 | int n; |
| 53 | int level = 0; |
| 54 | int lastc; |
| 55 | int cmd = 3; |
| 56 | int stopDoingThat; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 57 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | argc--; |
| 59 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | /* Parse any options */ |
| 62 | while (argc && **argv == '-') { |
| 63 | stopDoingThat = FALSE; |
| 64 | while (stopDoingThat == FALSE && *++(*argv)) { |
| 65 | switch (**argv) { |
| 66 | case 'c': |
| 67 | cmd = 4; |
| 68 | break; |
| 69 | case 'n': |
| 70 | cmd = 8; |
| 71 | if (--argc == 0) |
| 72 | goto end; |
| 73 | level = atoi(*(++argv)); |
| 74 | if (--argc > 0) |
| 75 | ++argv; |
| 76 | stopDoingThat = TRUE; |
| 77 | break; |
| 78 | case 's': |
| 79 | if (--argc == 0) |
| 80 | goto end; |
| 81 | bufsize = atoi(*(++argv)); |
| 82 | if (--argc > 0) |
| 83 | ++argv; |
| 84 | stopDoingThat = TRUE; |
| 85 | break; |
| 86 | default: |
| 87 | goto end; |
| 88 | } |
| 89 | } |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 90 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | if (argc > 1) { |
| 93 | goto end; |
| 94 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | if (cmd == 8) { |
| 97 | n = klogctl(cmd, NULL, level); |
| 98 | if (n < 0) { |
| 99 | goto klogctl_error; |
| 100 | } |
| 101 | exit(TRUE); |
| 102 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | if (bufsize < 4096) |
| 105 | bufsize = 4096; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 106 | buf = (char *) xmalloc(bufsize); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | n = klogctl(cmd, buf, bufsize); |
| 108 | if (n < 0) { |
| 109 | goto klogctl_error; |
| 110 | } |
| 111 | |
| 112 | lastc = '\n'; |
| 113 | for (i = 0; i < n; i++) { |
| 114 | if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') { |
| 115 | i++; |
| 116 | while (buf[i] >= '0' && buf[i] <= '9') |
| 117 | i++; |
| 118 | if (buf[i] == '>') |
| 119 | i++; |
| 120 | } |
| 121 | lastc = buf[i]; |
| 122 | putchar(lastc); |
| 123 | } |
| 124 | if (lastc != '\n') |
| 125 | putchar('\n'); |
| 126 | exit(TRUE); |
| 127 | end: |
| 128 | usage(dmesg_usage); |
| 129 | exit(FALSE); |
| 130 | klogctl_error: |
| 131 | perror("klogctl"); |
| 132 | exit(FALSE); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 133 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 134 | } |