Add dmesg(1)

No manpage yet.
This commit is contained in:
sin 2013-08-06 20:08:41 +01:00
parent 353b8a2c0b
commit 7b228e69b7
5 changed files with 103 additions and 1 deletions

View File

@ -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=)

38
dmesg.c Normal file
View File

@ -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;
}

19
linux/syslog.c Normal file
View File

@ -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);
}

39
openbsd/syslog.c Normal file
View File

@ -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;
}

View File

@ -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);