"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 3 | * |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 4 | * dmesg - display/control kernel ring buffer. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 5 | * |
Rob Landley | 446129a | 2006-07-27 16:40:55 +0000 | [diff] [blame] | 6 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 7 | * Copyright 2006 Bernhard Fischer <rep.nop@aon.at> |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 8 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
| 13 | #include <unistd.h> |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 14 | #include <sys/klog.h> |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 15 | |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 16 | int dmesg_main(int argc, char **argv); |
| 17 | int dmesg_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | { |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 19 | char *size, *level; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 20 | int flags = getopt32(argc, argv, "cs:n:", &size, &level); |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 21 | |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 22 | if (flags & 4) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 23 | if (klogctl(8, NULL, xatoul_range(level, 0, 10))) |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 24 | bb_perror_msg_and_die("klogctl"); |
| 25 | } else { |
| 26 | int len; |
| 27 | char *buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 29 | len = (flags & 2) ? xatoul_range(size, 2, INT_MAX) : 16384; |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 30 | buf = xmalloc(len); |
| 31 | if (0 > (len = klogctl(3 + (flags & 1), buf, len))) |
| 32 | bb_perror_msg_and_die("klogctl"); |
Rob Landley | 446129a | 2006-07-27 16:40:55 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 34 | // Skip <#> at the start of lines, and make sure we end with a newline. |
Rob Landley | 446129a | 2006-07-27 16:40:55 +0000 | [diff] [blame] | 35 | |
| 36 | if (ENABLE_FEATURE_DMESG_PRETTY) { |
| 37 | int last = '\n'; |
| 38 | int in; |
| 39 | |
| 40 | for (in = 0; in<len;) { |
| 41 | if (last == '\n' && buf[in] == '<') in += 3; |
| 42 | else putchar(last = buf[in++]); |
| 43 | } |
| 44 | if (last != '\n') putchar('\n'); |
| 45 | } else { |
| 46 | write(1,buf,len); |
| 47 | if (len && buf[len-1]!='\n') putchar('\n'); |
| 48 | } |
| 49 | |
| 50 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 52 | |
Rob Landley | 7c7b0d7 | 2006-06-13 18:31:04 +0000 | [diff] [blame] | 53 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | } |