diff --git a/Makefile b/Makefile index 92a9b8b..4484d67 100644 --- 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 new file mode 100644 index 0000000..5d61182 --- /dev/null +++ b/dmesg.c @@ -0,0 +1,38 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#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 new file mode 100644 index 0000000..d649933 --- /dev/null +++ b/linux/syslog.c @@ -0,0 +1,19 @@ +#include +#include + +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 new file mode 100644 index 0000000..fb61686 --- /dev/null +++ b/openbsd/syslog.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +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 index 3c4f263..3196f30 100644 --- 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);