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)
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@pld.ORG.PL>
10 * - added Native Language Support
15 * Commands to sys_syslog:
17 * 0 -- Close the log. Currently a NOP.
18 * 1 -- Open the log. Currently a NOP.
19 * 2 -- Read from the log.
20 * 3 -- Read all messages remaining in the ring buffer.
21 * 4 -- Read and clear all messages remaining in the ring buffer
22 * 5 -- Clear ring buffer.
23 * 6 -- Disable printk's to console
24 * 7 -- Enable printk's to console
25 * 8 -- Set level of messages printed to console
26 * 9 -- Return number of unread characters in the log buffer
27 * [supported since 2.4.10]
29 * Only function 3 is allowed to non-root processes.
32 #include <linux/unistd.h>
36 # include <sys/klog.h>
40 static char *progname;
45 _("Usage: %s [-c] [-n level] [-r] [-s bufsize]\n"), progname);
49 main(int argc, char *argv[]) {
58 int cmd = 3; /* Read all messages in the ring buffer */
61 setlocale(LC_ALL, "");
62 bindtextdomain(PACKAGE, LOCALEDIR);
66 while ((c = getopt(argc, argv, "crn:s:")) != -1) {
69 cmd = 4; /* Read and clear all messages */
72 cmd = 8; /* Set level of messages */
79 bufsize = atoi(optarg);
98 n = klogctl(cmd, NULL, level);
107 n = klogctl(10, NULL, 0); /* read ringbuffer size */
114 buf = (char *) malloc(sz);
115 n = klogctl(cmd, buf, sz);
119 buf = (char *) malloc(sz);
120 n = klogctl(3, buf, sz); /* read only */
121 if (n != sz || sz > (1<<28))
127 if (n > 0 && cmd == 4)
128 n = klogctl(cmd, buf, sz); /* read and clear */
137 for (i = 0; i < n; i++) {
138 if (!raw && (i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
140 while (buf[i] >= '0' && buf[i] <= '9')