-
Notifications
You must be signed in to change notification settings - Fork 4
/
dmesg.c
81 lines (67 loc) · 1.29 KB
/
dmesg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* See LICENSE file for copyright and license details. */
#include <sys/klog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "util.h"
enum {
SYSLOG_ACTION_READ_ALL = 3,
SYSLOG_ACTION_CLEAR = 5,
SYSLOG_ACTION_CONSOLE_LEVEL = 8,
SYSLOG_ACTION_SIZE_BUFFER = 10
};
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');
}
static void
usage(void)
{
eprintf("usage: %s [-Ccr] [-n level]\n", argv0);
}
int
main(int argc, char *argv[])
{
int n;
char *buf;
int cflag = 0;
long level;
ARGBEGIN {
case 'C':
if (klogctl(SYSLOG_ACTION_CLEAR, NULL, 0) < 0)
eprintf("klogctl:");
return 0;
case 'c':
cflag = 1;
break;
case 'r':
break;
case 'n':
level = estrtol(EARGF(usage()), 10);
if (klogctl(SYSLOG_ACTION_CONSOLE_LEVEL, NULL, level) < 0)
eprintf("klogctl:");
return 0;
default:
usage();
} ARGEND;
n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
if (n < 0)
eprintf("klogctl:");
buf = emalloc(n);
n = klogctl(SYSLOG_ACTION_READ_ALL, buf, n);
if (n < 0)
eprintf("klogctl:");
dmesg_show(buf, n);
if (cflag && klogctl(SYSLOG_ACTION_CLEAR, NULL, 0) < 0)
eprintf("klogctl:");
free(buf);
return 0;
}