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 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * from util-linux -- adapted for busybox by |
| 14 | * Erik Andersen <andersen@codepoet.org>. I ripped out Native Language |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 15 | * Support, replaced getopt, added some gotos for redundant stuff. |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 16 | * |
| 17 | * Audited and cleaned up on 7 March 2003 to reduce size of |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 18 | * check error handling by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 23 | #include <getopt.h> |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 24 | #include <errno.h> |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 25 | #include <sys/klog.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 26 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | int dmesg_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | { |
Eric Andersen | eef2317 | 2003-07-30 06:52:33 +0000 | [diff] [blame] | 31 | char *buf |
| 32 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 33 | = NULL |
| 34 | #endif |
| 35 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | int bufsize = 8196; |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 37 | int i, n; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | int level = 0; |
| 39 | int lastc; |
| 40 | int cmd = 3; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 41 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | while ((i = getopt(argc, argv, "cn:s:")) > 0) { |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 43 | switch (i) { |
| 44 | case 'c': |
| 45 | cmd = 4; |
| 46 | break; |
| 47 | case 'n': |
| 48 | cmd = 8; |
| 49 | level = bb_xgetlarg(optarg, 10, 0, 10); |
| 50 | break; |
| 51 | case 's': |
| 52 | /* I think a 512k max kernel ring buffer is big enough for |
| 53 | * anybody, as the default is 16k... Could be wrong though. |
| 54 | * If so I'm sure I'll hear about it by the enraged masses*/ |
| 55 | bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024); |
| 56 | break; |
| 57 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | } |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 60 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 62 | if (optind < argc) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 65 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | if (cmd == 8) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 67 | if (klogctl(cmd, NULL, level) < 0) |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 68 | goto die_the_death; |
| 69 | goto all_done; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 71 | |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 72 | buf = xmalloc(bufsize); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 73 | if ((n = klogctl(cmd, buf, bufsize)) < 0) |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 74 | goto die_the_death; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | |
| 76 | lastc = '\n'; |
| 77 | for (i = 0; i < n; i++) { |
Eric Andersen | afdde3e | 2000-12-09 16:41:42 +0000 | [diff] [blame] | 78 | if (lastc == '\n' && buf[i] == '<') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | i++; |
| 80 | while (buf[i] >= '0' && buf[i] <= '9') |
| 81 | i++; |
| 82 | if (buf[i] == '>') |
| 83 | i++; |
| 84 | } |
| 85 | lastc = buf[i]; |
| 86 | putchar(lastc); |
| 87 | } |
| 88 | if (lastc != '\n') |
| 89 | putchar('\n'); |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 90 | all_done: |
| 91 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 92 | if (buf) { |
| 93 | free(buf); |
| 94 | } |
| 95 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 96 | return EXIT_SUCCESS; |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 97 | die_the_death: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | bb_perror_nomsg_and_die(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | } |