commit 7b228e69b7c5dfd03f3f3bbf1f9e5bf7792c8da4
parent 353b8a2c0b0ac320d3792d10524abdab33ceca9c
Author: sin <sin@2f30.org>
Date: Tue, 6 Aug 2013 20:08:41 +0100
Add dmesg(1)
No manpage yet.
Diffstat:
5 files changed, 103 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -6,11 +6,13 @@ include config.mk
HDR = util.h arg.h ubase.h
LIB = \
$(OS)/grabmntinfo.o \
+ $(OS)/syslog.o \
util/eprintf.o \
util/estrtol.o
SRC = \
- df.c
+ df.c \
+ dmesg.c
OBJ = $(SRC:.c=.o) $(LIB)
BIN = $(SRC:.c=)
diff --git a/dmesg.c b/dmesg.c
@@ -0,0 +1,38 @@
+/* See LICENSE file for copyright and license details. */
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "ubase.h"
+#include "util.h"
+
+static void
+usage(void)
+{
+ eprintf("usage: %s\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int n;
+ char *buf;
+
+ ARGBEGIN {
+ default:
+ usage();
+ } ARGEND;
+
+ n = syslog_size();
+ if (n < 0)
+ eprintf("syslog_size:");
+ buf = malloc(n);
+ if (!buf)
+ eprintf("malloc:");
+ n = syslog_read(buf, n);
+ if (n < 0)
+ eprintf("syslog_read:");
+ if (write(STDOUT_FILENO, buf, n) != n)
+ eprintf("write:");
+ free(buf);
+ return 0;
+}
diff --git a/linux/syslog.c b/linux/syslog.c
@@ -0,0 +1,19 @@
+#include <sys/klog.h>
+#include <stdio.h>
+
+enum {
+ SYSLOG_ACTION_READ_ALL = 3,
+ SYSLOG_ACTION_SIZE_BUFFER = 10
+};
+
+int
+syslog_size(void)
+{
+ return klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
+}
+
+int
+syslog_read(void *buf, size_t n)
+{
+ return klogctl(SYSLOG_ACTION_READ_ALL, buf, n);
+}
diff --git a/openbsd/syslog.c b/openbsd/syslog.c
@@ -0,0 +1,39 @@
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/msgbuf.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+syslog_size(void)
+{
+ int mib[2], msgbufsize;
+ size_t len;
+ int ret;
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_MSGBUFSIZE;
+ len = sizeof(msgbufsize);
+ ret = sysctl(mib, 2, &msgbufsize, &len, NULL, 0);
+ if (ret < 0)
+ return ret;
+ msgbufsize += sizeof(struct msgbuf) - 1;
+ return msgbufsize;
+}
+
+int
+syslog_read(void *buf, size_t n)
+{
+ int mib[2];
+ int ret;
+
+ memset(buf, 0, n);
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_MSGBUF;
+ ret = sysctl(mib, 2, buf, &n, NULL, 0);
+ if (ret < 0)
+ return ret;
+ memmove(buf, ((struct msgbuf *)buf)->msg_bufc, n);
+ return n;
+}
diff --git a/ubase.h b/ubase.h
@@ -5,3 +5,7 @@ struct mntinfo {
};
int grabmntinfo(struct mntinfo **minfo);
+
+/* syslog.c */
+int syslog_size(void);
+int syslog_read(void *buf, size_t n);