2013-08-06 15:08:41 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2013-08-14 09:19:05 -04:00
|
|
|
#include <sys/klog.h>
|
2013-08-06 15:08:41 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-08-14 09:19:05 -04:00
|
|
|
#include <string.h>
|
2013-08-06 15:08:41 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-08-14 09:19:05 -04:00
|
|
|
static int dmesg_show(int fd, const void *buf, size_t n);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SYSLOG_ACTION_READ_ALL = 3,
|
2013-08-14 09:34:57 -04:00
|
|
|
SYSLOG_ACTION_CLEAR = 5,
|
2013-09-23 10:13:01 -04:00
|
|
|
SYSLOG_ACTION_CONSOLE_LEVEL = 8,
|
2013-08-14 09:19:05 -04:00
|
|
|
SYSLOG_ACTION_SIZE_BUFFER = 10
|
|
|
|
};
|
|
|
|
|
2013-08-06 15:08:41 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-04-12 08:53:32 -04:00
|
|
|
eprintf("usage: [-Ccr] [-n level] %s\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;
|
2013-08-17 12:22:30 -04:00
|
|
|
int rflag = 0;
|
2013-09-23 10:13:01 -04:00
|
|
|
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:");
|
2013-10-07 15:03:06 -04:00
|
|
|
return EXIT_SUCCESS;
|
2013-08-16 09:59:53 -04:00
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
2013-08-17 12:22:30 -04:00
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
2013-09-23 10:13:01 -04:00
|
|
|
case 'n':
|
|
|
|
level = estrtol(EARGF(usage()), 10);
|
|
|
|
if (klogctl(SYSLOG_ACTION_CONSOLE_LEVEL, NULL, level) < 0)
|
|
|
|
eprintf("klogctl:");
|
2013-10-07 15:03:06 -04:00
|
|
|
return EXIT_SUCCESS;
|
2013-08-06 15:08:41 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2013-08-14 09:19:05 -04:00
|
|
|
n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
|
2013-08-06 15:08:41 -04:00
|
|
|
if (n < 0)
|
2013-08-14 09:19:05 -04:00
|
|
|
eprintf("klogctl:");
|
2013-08-12 06:29:21 -04:00
|
|
|
|
2014-04-30 08:08:16 -04:00
|
|
|
buf = emalloc(n);
|
2013-08-12 06:29:21 -04:00
|
|
|
|
2013-08-14 09:19:05 -04:00
|
|
|
n = klogctl(SYSLOG_ACTION_READ_ALL, buf, n);
|
2013-08-06 15:08:41 -04:00
|
|
|
if (n < 0)
|
2013-08-14 09:19:05 -04:00
|
|
|
eprintf("klogctl:");
|
2013-08-12 06:29:21 -04:00
|
|
|
|
2013-08-17 12:22:30 -04:00
|
|
|
if (rflag) {
|
|
|
|
if (write(STDOUT_FILENO, buf, n) != n)
|
|
|
|
eprintf("write:");
|
|
|
|
} else {
|
|
|
|
n = dmesg_show(STDOUT_FILENO, buf, n);
|
|
|
|
if (n < 0)
|
|
|
|
eprintf("dmesg_show:");
|
|
|
|
}
|
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);
|
2013-10-07 14:11:40 -04:00
|
|
|
return EXIT_SUCCESS;
|
2013-08-06 15:08:41 -04:00
|
|
|
}
|
2013-08-14 09:19:05 -04:00
|
|
|
|
2013-08-16 05:12:21 -04:00
|
|
|
static int
|
2013-08-14 09:19:05 -04:00
|
|
|
dmesg_show(int fd, const void *buf, size_t n)
|
|
|
|
{
|
|
|
|
int last = '\n';
|
2014-06-05 06:28:08 -04:00
|
|
|
char *newbuf, *q;
|
2013-08-14 09:19:05 -04:00
|
|
|
const char *p = buf;
|
2014-02-14 08:52:04 -05:00
|
|
|
ssize_t r;
|
2013-08-14 09:19:05 -04:00
|
|
|
size_t i;
|
|
|
|
|
2014-06-05 06:28:08 -04:00
|
|
|
newbuf = calloc(n, sizeof(char));
|
|
|
|
q = newbuf;
|
2013-08-14 09:19:05 -04:00
|
|
|
for (i = 0; i < n; ) {
|
|
|
|
if (last == '\n' && p[i] == '<') {
|
|
|
|
i += 2;
|
|
|
|
if (i + 1 < n && p[i + 1] == '>')
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
*q++ = p[i];
|
|
|
|
}
|
|
|
|
last = p[i++];
|
|
|
|
}
|
2014-02-14 08:52:04 -05:00
|
|
|
r = write(fd, newbuf, n);
|
2014-06-05 06:28:08 -04:00
|
|
|
free(newbuf);
|
2014-02-14 08:52:04 -05:00
|
|
|
if(r < 0 || (size_t)r != n)
|
2013-08-14 09:19:05 -04:00
|
|
|
return -1;
|
|
|
|
if (last != '\n')
|
|
|
|
if (write(fd, "\n", 1) != 1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|