aboutsummaryrefslogtreecommitdiff
path: root/toys/dmesg.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-11-20 01:06:29 -0600
committerRob Landley <rob@landley.net>2007-11-20 01:06:29 -0600
commitb5b82d92d26934b3d49adb146e8a4bd542ab8dfb (patch)
treecbfdb3b566c0e244ef53c4e5cd014174564a379b /toys/dmesg.c
parent126f9c5c6f4da006dc389b6eb91d9c8f0d02970e (diff)
downloadtoybox-b5b82d92d26934b3d49adb146e8a4bd542ab8dfb.tar.gz
Implement dmesg.
Diffstat (limited to 'toys/dmesg.c')
-rw-r--r--toys/dmesg.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/toys/dmesg.c b/toys/dmesg.c
new file mode 100644
index 00000000..7a150121
--- /dev/null
+++ b/toys/dmesg.c
@@ -0,0 +1,40 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * dmesg.c - display/control kernel ring buffer.
+ *
+ * Copyright 2006 Rob Landley <rob@landley.net>
+ */
+
+#include "toys.h"
+#include <sys/klog.h>
+
+#define TT toy.dmesg
+
+int dmesg_main(void)
+{
+ // For -n just tell kernel to which messages to keep.
+ if (toys.optflags & 2) {
+ if (klogctl(8, NULL, TT.level))
+ error_exit("klogctl");
+ } else {
+ int size, i, last = '\n';
+ char *data;
+
+ // Figure out how much data we need, and fetch it.
+ size = TT.size;
+ if (size<2) size = 16384;
+ data = xmalloc(size);
+ size = klogctl(3 + (toys.optflags&1), data, size);
+ if (size < 0) error_exit("klogctl");
+
+ // Display data, filtering out level markers.
+ for (i=0; i<size; ) {
+ if (last=='\n' && data[i]=='<') i += 3;
+ else putchar(last = data[i++]);
+ }
+ if (last!='\n') putchar('\n');
+ if (CFG_TOYBOX_FREE) free(data);
+ }
+
+ return 0;
+}