ubase/dmesg.c

81 lines
1.3 KiB
C
Raw Normal View History

2013-08-06 15:08:41 -04:00
/* See LICENSE file for copyright and license details. */
#include <sys/klog.h>
2014-06-30 14:03:41 -04:00
2013-08-06 15:08:41 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-06-30 14:03:41 -04:00
#include <unistd.h>
2013-08-06 15:08:41 -04:00
#include "util.h"
enum {
SYSLOG_ACTION_READ_ALL = 3,
2013-08-14 09:34:57 -04:00
SYSLOG_ACTION_CLEAR = 5,
SYSLOG_ACTION_CONSOLE_LEVEL = 8,
SYSLOG_ACTION_SIZE_BUFFER = 10
};
2015-02-06 09:10:34 -05:00
static void
dmesg_show(const void *buf, size_t n)
{
const char *p = buf;
ssize_t r;
r = write(1, p, n);
if (r < 0)
eprintf("write:");
if (r > 0 && p[r - 1] != '\n')
putchar('\n');
}
2013-08-06 15:08:41 -04:00
static void
usage(void)
{
2015-02-06 09:10:34 -05:00
eprintf("usage: %s [-Ccr] [-n level]\n", argv0);
2013-08-06 15:08:41 -04:00
}
int
main(int argc, char *argv[])
{
int n;
char *buf;
2013-08-16 09:59:53 -04:00
int cflag = 0;
long level;
2013-08-06 15:08:41 -04:00
ARGBEGIN {
2013-08-14 09:34:57 -04:00
case 'C':
if (klogctl(SYSLOG_ACTION_CLEAR, NULL, 0) < 0)
eprintf("klogctl:");
2014-10-02 18:45:25 -04:00
return 0;
2013-08-16 09:59:53 -04:00
case 'c':
cflag = 1;
break;
2013-08-17 12:22:30 -04:00
case 'r':
break;
case 'n':
level = estrtol(EARGF(usage()), 10);
if (klogctl(SYSLOG_ACTION_CONSOLE_LEVEL, NULL, level) < 0)
eprintf("klogctl:");
2014-10-02 18:45:25 -04:00
return 0;
2013-08-06 15:08:41 -04:00
default:
usage();
} ARGEND;
n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
2013-08-06 15:08:41 -04:00
if (n < 0)
eprintf("klogctl:");
2013-08-12 06:29:21 -04:00
buf = emalloc(n);
2013-08-12 06:29:21 -04:00
n = klogctl(SYSLOG_ACTION_READ_ALL, buf, n);
2013-08-06 15:08:41 -04:00
if (n < 0)
eprintf("klogctl:");
2013-08-12 06:29:21 -04:00
2014-11-30 08:28:01 -05:00
dmesg_show(buf, n);
2013-08-12 06:29:21 -04:00
2013-08-16 09:59:53 -04:00
if (cflag && klogctl(SYSLOG_ACTION_CLEAR, NULL, 0) < 0)
eprintf("klogctl:");
2013-08-06 15:08:41 -04:00
free(buf);
2014-10-02 18:45:25 -04:00
return 0;
2015-02-06 09:10:34 -05:00
}