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