Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* dmesg.c -- Print out the contents of the kernel ring buffer |
| 2 | * Created: Sat Oct 9 16:19:47 1993 |
| 3 | * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu |
| 4 | * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu) |
| 5 | * This program comes with ABSOLUTELY NO WARRANTY. |
| 6 | * Modifications by Rick Sladkey (jrs@world.std.com) |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 7 | * Larger buffersize 3 June 1998 by Nicolai Langfeldt, based on a patch |
| 8 | * by Peeter Joot. This was also suggested by John Hudson. |
| 9 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org> |
| 10 | * - added Native Language Support |
| 11 | * |
| 12 | * from util-linux -- adapted for busybox by |
| 13 | * Erik Andersen <andersee@debian.org>. I ripped out Native Language |
| 14 | * Support, replaced getopt, added some gotos for redundant stuff. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 17 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | #include <linux/unistd.h> |
| 19 | #include <stdio.h> |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 20 | #include <getopt.h> |
| 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 |
| 27 | static inline _syscall3(int, klogctl, int, type, char *, b, int, len); |
| 28 | #else /* __alpha__ */ |
| 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 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 36 | static const char dmesg_usage[] = "dmesg [-c] [-n level] [-s bufsize]\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 38 | int dmesg_main( int argc, char** argv ) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | { |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 40 | char *buf; |
| 41 | int bufsize=8196; |
| 42 | int i; |
| 43 | int n; |
| 44 | int level = 0; |
| 45 | int lastc; |
| 46 | int cmd = 3; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 48 | /* Parse any options */ |
| 49 | while (argc && **argv == '-') { |
| 50 | while (*++(*argv)) |
| 51 | switch (**argv) { |
| 52 | case 'c': |
| 53 | cmd = 4; |
| 54 | break; |
| 55 | case 'n': |
| 56 | cmd = 8; |
| 57 | if (--argc == 0) |
| 58 | goto end; |
| 59 | level = atoi (*(++argv)); |
| 60 | --argc; |
| 61 | ++argv; |
| 62 | break; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 63 | case 's': |
| 64 | if (--argc == 0) |
| 65 | goto end; |
| 66 | bufsize = atoi (*(++argv)); |
| 67 | --argc; |
| 68 | ++argv; |
| 69 | break; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 70 | default: |
| 71 | goto end; |
| 72 | } |
| 73 | } |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 74 | |
| 75 | if (argc > 1) { |
| 76 | goto end; |
| 77 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 79 | if (cmd == 8) { |
| 80 | n = klogctl( cmd, NULL, level ); |
| 81 | if (n < 0) { |
| 82 | goto klogctl_error; |
| 83 | } |
| 84 | exit( TRUE ); |
| 85 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 87 | if (bufsize < 4096) bufsize = 4096; |
| 88 | buf = (char*)malloc(bufsize); |
| 89 | n = klogctl( cmd, buf, bufsize ); |
| 90 | if (n < 0) { |
| 91 | goto klogctl_error; |
| 92 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 93 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 94 | lastc = '\n'; |
| 95 | for (i = 0; i < n; i++) { |
| 96 | if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') { |
| 97 | i++; |
| 98 | while (buf[i] >= '0' && buf[i] <= '9') |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | i++; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 100 | if (buf[i] == '>') |
| 101 | i++; |
| 102 | } |
| 103 | lastc = buf[i]; |
| 104 | putchar( lastc ); |
| 105 | } |
| 106 | if (lastc != '\n') |
| 107 | putchar( '\n' ); |
| 108 | exit( TRUE); |
| 109 | end: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 110 | usage( dmesg_usage); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 111 | exit (FALSE); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 112 | klogctl_error: |
| 113 | perror( "klogctl" ); |
| 114 | exit( FALSE ); |
| 115 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 116 | } |