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