Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | #include "internal.h" |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <time.h> |
| 5 | |
| 6 | /* dmesg.c -- Print out the contents of the kernel ring buffer |
| 7 | * Created: Sat Oct 9 16:19:47 1993 |
| 8 | * Revised: Thu Oct 28 21:52:17 1993 by faith@cs.unc.edu |
| 9 | * Copyright 1993 Theodore Ts'o (tytso@athena.mit.edu) |
| 10 | * This program comes with ABSOLUTELY NO WARRANTY. |
| 11 | * Modifications by Rick Sladkey (jrs@world.std.com) |
| 12 | * from util-linux; adapted for busybox |
| 13 | */ |
| 14 | |
| 15 | #include <linux/unistd.h> |
| 16 | #include <stdio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | |
| 18 | #define __NR_klog __NR_syslog |
| 19 | |
| 20 | #if defined(__GLIBC__) |
| 21 | #include <sys/klog.h> |
| 22 | #define klog klogctl |
| 23 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 24 | static inline _syscall3 (int, klog, int, type, char *, b, int, len) |
| 25 | #endif /* __GLIBC__ */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 28 | |
| 29 | static const char dmesg_usage[] = "dmesg [-c] [-n level]\n"; |
| 30 | |
| 31 | int dmesg_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | { |
| 33 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 34 | char buf[4096]; |
| 35 | int i; |
| 36 | int n; |
| 37 | int level = 0; |
| 38 | int lastc; |
| 39 | int cmd = 3; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 40 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 41 | argc--; |
| 42 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 44 | /* Parse any options */ |
| 45 | while (argc && **argv == '-') { |
| 46 | while (*++(*argv)) |
| 47 | switch (**argv) { |
| 48 | case 'c': |
| 49 | cmd = 4; |
| 50 | break; |
| 51 | case 'n': |
| 52 | cmd = 8; |
| 53 | if (--argc == 0) |
| 54 | goto end; |
| 55 | level = atoi (*(++argv)); |
| 56 | --argc; |
| 57 | ++argv; |
| 58 | break; |
| 59 | default: |
| 60 | goto end; |
| 61 | } |
| 62 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 64 | if (cmd == 8) { |
| 65 | n = klog (cmd, NULL, level); |
| 66 | if (n < 0) { |
| 67 | perror ("klog"); |
| 68 | exit (FALSE); |
| 69 | } |
| 70 | exit (TRUE); |
| 71 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 73 | n = klog (cmd, buf, sizeof (buf)); |
| 74 | if (n < 0) { |
| 75 | perror ("klog"); |
| 76 | exit (FALSE); |
| 77 | } |
| 78 | |
| 79 | lastc = '\n'; |
| 80 | for (i = 0; i < n; i++) { |
| 81 | if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | i++; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 83 | while (buf[i] >= '0' && buf[i] <= '9') |
| 84 | i++; |
| 85 | if (buf[i] == '>') |
| 86 | i++; |
| 87 | } |
| 88 | lastc = buf[i]; |
| 89 | putchar (lastc); |
| 90 | } |
| 91 | if (lastc != '\n') |
| 92 | putchar ('\n'); |
| 93 | exit (TRUE); |
| 94 | |
| 95 | end: |
| 96 | fprintf (stderr, "Usage: %s\n", dmesg_usage); |
| 97 | exit (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | } |