"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> |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 7 | * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at> |
Eric Andersen | 2afcbe4 | 2003-03-07 17:33:40 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config:config DMESG |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 12 | //config: bool "dmesg (3.7 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 13 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: dmesg is used to examine or control the kernel ring buffer. When the |
| 16 | //config: Linux kernel prints messages to the system log, they are stored in |
| 17 | //config: the kernel ring buffer. You can use dmesg to print the kernel's ring |
| 18 | //config: buffer, clear the kernel ring buffer, change the size of the kernel |
| 19 | //config: ring buffer, and change the priority level at which kernel messages |
| 20 | //config: are also logged to the system console. Enable this option if you |
| 21 | //config: wish to enable the 'dmesg' utility. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 22 | //config: |
| 23 | //config:config FEATURE_DMESG_PRETTY |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 24 | //config: bool "Pretty output" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: depends on DMESG |
| 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: If you wish to scrub the syslog level from the output, say 'Y' here. |
| 29 | //config: The syslog level is a string prefixed to every line with the form |
| 30 | //config: "<#>". |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 31 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 32 | //config: With this option you will see: |
| 33 | //config: # dmesg |
| 34 | //config: Linux version 2.6.17.4 ..... |
| 35 | //config: BIOS-provided physical RAM map: |
| 36 | //config: BIOS-e820: 0000000000000000 - 000000000009f000 (usable) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 37 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 38 | //config: Without this option you will see: |
| 39 | //config: # dmesg |
| 40 | //config: <5>Linux version 2.6.17.4 ..... |
| 41 | //config: <6>BIOS-provided physical RAM map: |
| 42 | //config: <6> BIOS-e820: 0000000000000000 - 000000000009f000 (usable) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 43 | |
| 44 | //applet:IF_DMESG(APPLET(dmesg, BB_DIR_BIN, BB_SUID_DROP)) |
| 45 | |
| 46 | //kbuild:lib-$(CONFIG_DMESG) += dmesg.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 47 | |
| 48 | //usage:#define dmesg_trivial_usage |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame^] | 49 | //usage: "[-cr] [-n LEVEL] [-s SIZE]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 50 | //usage:#define dmesg_full_usage "\n\n" |
| 51 | //usage: "Print or control the kernel ring buffer\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 52 | //usage: "\n -c Clear ring buffer after printing" |
| 53 | //usage: "\n -n LEVEL Set console logging level" |
| 54 | //usage: "\n -s SIZE Buffer size" |
Peter Korsgaard | 864d1b7 | 2015-08-24 15:54:49 +0200 | [diff] [blame] | 55 | //usage: "\n -r Print raw message buffer" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 56 | |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 57 | #include <sys/klog.h> |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 58 | #include "libbb.h" |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 60 | int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 61 | int dmesg_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | { |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 63 | int len, level; |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 64 | char *buf; |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 65 | unsigned opts; |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 66 | enum { |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 67 | OPT_c = 1 << 0, |
| 68 | OPT_s = 1 << 1, |
Peter Korsgaard | 864d1b7 | 2015-08-24 15:54:49 +0200 | [diff] [blame] | 69 | OPT_n = 1 << 2, |
| 70 | OPT_r = 1 << 3 |
Bernhard Reutner-Fischer | f02efd1 | 2008-05-19 10:28:32 +0000 | [diff] [blame] | 71 | }; |
Erik Andersen | 1266a13 | 1999-12-29 22:19:46 +0000 | [diff] [blame] | 72 | |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 73 | opts = getopt32(argv, "cs:+n:+r", &len, &level); |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 74 | if (opts & OPT_n) { |
| 75 | if (klogctl(8, NULL, (long) level)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 76 | bb_simple_perror_msg_and_die("klogctl"); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 77 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 79 | |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 80 | if (!(opts & OPT_s)) |
| 81 | len = klogctl(10, NULL, 0); /* read ring buffer size */ |
| 82 | if (len < 16*1024) |
| 83 | len = 16*1024; |
| 84 | if (len > 16*1024*1024) |
| 85 | len = 16*1024*1024; |
| 86 | |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 87 | buf = xmalloc(len); |
Denys Vlasenko | 874201f | 2009-07-10 14:14:14 +0200 | [diff] [blame] | 88 | len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 89 | if (len < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 90 | bb_simple_perror_msg_and_die("klogctl"); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 91 | if (len == 0) |
| 92 | return EXIT_SUCCESS; |
| 93 | |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 94 | |
Peter Korsgaard | 864d1b7 | 2015-08-24 15:54:49 +0200 | [diff] [blame] | 95 | if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) { |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 96 | int last = '\n'; |
| 97 | int in = 0; |
| 98 | |
Peter Korsgaard | 3917fa3 | 2013-01-06 00:07:17 +0100 | [diff] [blame] | 99 | /* Skip <[0-9]+> at the start of lines */ |
Denys Vlasenko | f04ca74 | 2010-10-19 23:08:33 +0200 | [diff] [blame] | 100 | while (1) { |
| 101 | if (last == '\n' && buf[in] == '<') { |
Peter Korsgaard | 3917fa3 | 2013-01-06 00:07:17 +0100 | [diff] [blame] | 102 | while (buf[in++] != '>' && in < len) |
| 103 | ; |
| 104 | } else { |
| 105 | last = buf[in++]; |
| 106 | putchar(last); |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 107 | } |
Denys Vlasenko | f04ca74 | 2010-10-19 23:08:33 +0200 | [diff] [blame] | 108 | if (in >= len) |
| 109 | break; |
| 110 | } |
| 111 | /* Make sure we end with a newline */ |
Denis Vlasenko | 7e3a5f5 | 2007-11-16 20:18:54 +0000 | [diff] [blame] | 112 | if (last != '\n') |
| 113 | bb_putchar('\n'); |
| 114 | } else { |
| 115 | full_write(STDOUT_FILENO, buf, len); |
| 116 | if (buf[len-1] != '\n') |
| 117 | bb_putchar('\n'); |
| 118 | } |
| 119 | |
| 120 | if (ENABLE_FEATURE_CLEAN_UP) free(buf); |
| 121 | |
| 122 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | } |