aboutsummaryrefslogtreecommitdiff
path: root/klogd.c
blob: 8db1d50fc157318d9aabc17b4fbcbfe5cf157116 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* SPDX-License-Identifier: ISC */
#include <sys/klog.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
#include <errno.h>

#include "config.h"

enum {
	KLOG_CLOSE = 0,
	KLOG_OPEN = 1,
	KLOG_READ = 2,
	KLOG_CONSOLE_OFF = 6,
	KLOG_CONSOLE_ON = 7,
	KLOG_CONSOLE_LEVEL = 8,
};

static char log_buffer[4096];
static sig_atomic_t running = 1;
static int level = 0;

static const struct option options[] = {
	{ "help", no_argument, NULL, 'h' },
	{ "version", no_argument, NULL, 'V' },
	{ "level", required_argument, NULL, 'l' },
	{ NULL, 0, NULL, 0 },
};

static const char *shortopt = "hVl:";

static const char *helptext =
"Usage: klogd [OPTION]... \n\n"
"Collect printk() messages from the kernel and forward them to syslogd.\n"
"\n"
"The following OPTIONSs can be used:\n"
"  -l, --level <level>  Minimum log level that should be printed to console.\n"
"                       If not set, logging to console is turned off.\n"
"  -h, --help           Print this help text and exit\n"
"  -V, --version        Print version information and exit\n\n";

static const char *version_string =
"klogd (usyslog) " PACKAGE_VERSION "\n"
"Copyright (C) 2018 David Oberhollenzer\n\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n";

static void process_options(int argc, char **argv)
{
	int c;

	for (;;) {
		c = getopt_long(argc, argv, shortopt, options, NULL);
		if (c == -1)
			break;

		switch (c) {
		case 'l':
			level = strtoul(optarg, NULL, 10);
			break;
		case 'h':
			fputs(helptext, stdout);
			exit(EXIT_SUCCESS);
		case 'V':
			fputs(version_string, stdout);
			exit(EXIT_SUCCESS);
		default:
			fputs("Try `klogd --help' for more information\n",
			      stderr);
			exit(EXIT_FAILURE);
		}
	}
}

static void sighandler(int signo)
{
	if (signo == SIGTERM || signo == SIGINT)
		running = 0;
}

static void sigsetup(void)
{
	struct sigaction act;
	sigset_t mask;

	memset(&act, 0, sizeof(act));
	act.sa_handler = sighandler;
	sigaction(SIGTERM, &act, NULL);
	sigaction(SIGINT, &act, NULL);

	sigfillset(&mask);
	sigdelset(&mask, SIGTERM);
	sigdelset(&mask, SIGINT);
	sigprocmask(SIG_SETMASK, &mask, NULL);
}

static void log_open(void)
{
	klogctl(KLOG_OPEN, NULL, 0);

	if (level) {
		klogctl(KLOG_CONSOLE_LEVEL, NULL, level);
	} else {
		klogctl(KLOG_CONSOLE_OFF, NULL, 0);
	}

	openlog("kernel", 0, LOG_KERN);
}

static void log_close(void)
{
	klogctl(KLOG_CONSOLE_ON, NULL, 0);
	klogctl(KLOG_CLOSE, NULL, 0);
	syslog(LOG_NOTICE, "-- klogd terminating --");
}

int main(int argc, char **argv)
{
	int diff, count = 0, priority, ret = EXIT_SUCCESS;
	char *ptr, *end;

	process_options(argc, argv);
	sigsetup();
	log_open();

	/* TODO: seccomp lockdown? */

	while (running) {
		diff = klogctl(KLOG_READ, log_buffer + count,
			       sizeof(log_buffer) - 1 - count);

		if (diff < 0) {
			if (errno == EINTR)
				continue;
			syslog(LOG_CRIT, "klogctl read: %s", strerror(errno));
			ret = EXIT_FAILURE;
			break;
		}

		count += diff;
		log_buffer[count] = '\0';
		ptr = log_buffer;

		for (;;) {
			end = strchr(ptr, '\n');
			if (end == NULL) {
				count = strlen(ptr);
				memmove(log_buffer, ptr, count);
				break;
			}

			*(end++) = '\0';
			priority = LOG_INFO;

			if (*ptr == '<') {
				++ptr;
				if (*ptr)
					priority = strtoul(ptr, &ptr, 10);
				if (*ptr == '>')
					++ptr;
			}

			if (*ptr)
				syslog(priority, "%s", ptr);
			ptr = end;
		}
	}

	log_close();
	return ret;
}