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 | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 20 | #include <getopt.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 22 | #if __GNU_LIBRARY__ < 5 |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 23 | #include <sys/syscall.h> |
| 24 | #include <linux/unistd.h> |
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 | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | int dmesg_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | { |
Pavel Roskin | 47d4926 | 2000-07-17 16:17:19 +0000 | [diff] [blame] | 39 | char *buf; |
| 40 | int c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | int bufsize = 8196; |
| 42 | int i; |
| 43 | int n; |
| 44 | int level = 0; |
| 45 | int lastc; |
| 46 | int cmd = 3; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 47 | |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 48 | while ((c = getopt(argc, argv, "cn:s:")) != EOF) { |
| 49 | switch (c) { |
| 50 | case 'c': |
| 51 | cmd = 4; |
| 52 | break; |
| 53 | case 'n': |
| 54 | cmd = 8; |
| 55 | if (optarg == NULL) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 56 | show_usage(); |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 57 | level = atoi(optarg); |
| 58 | break; |
| 59 | case 's': |
| 60 | if (optarg == NULL) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 61 | show_usage(); |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 62 | bufsize = atoi(optarg); |
| 63 | break; |
| 64 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 65 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | } |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 67 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | |
Eric Andersen | add09fd | 2000-07-14 18:39:08 +0000 | [diff] [blame] | 69 | if (optind < argc) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 70 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | if (cmd == 8) { |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 74 | if (klogctl(cmd, NULL, level) < 0) |
| 75 | perror_msg_and_die("klogctl"); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 76 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 78 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | if (bufsize < 4096) |
| 80 | bufsize = 4096; |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 81 | buf = (char *) xmalloc(bufsize); |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 82 | if ((n = klogctl(cmd, buf, bufsize)) < 0) |
| 83 | perror_msg_and_die("klogctl"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | |
| 85 | lastc = '\n'; |
| 86 | for (i = 0; i < n; i++) { |
Eric Andersen | afdde3e | 2000-12-09 16:41:42 +0000 | [diff] [blame] | 87 | if (lastc == '\n' && buf[i] == '<') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | i++; |
| 89 | while (buf[i] >= '0' && buf[i] <= '9') |
| 90 | i++; |
| 91 | if (buf[i] == '>') |
| 92 | i++; |
| 93 | } |
| 94 | lastc = buf[i]; |
| 95 | putchar(lastc); |
| 96 | } |
| 97 | if (lastc != '\n') |
| 98 | putchar('\n'); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 99 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | } |