-
Notifications
You must be signed in to change notification settings - Fork 0
/
nologd.c
402 lines (313 loc) · 8.72 KB
/
nologd.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* nologd: consume all the logs without any processing
*
* Copyright (C) 2014, 2016 Karol Lewandowski
* Licensed under terms of GNU GPL v2 (or later). See LICENSE.
*
* Compile:
* cc -o nologd nologd.c
* with support for journald-like socket activation:
* cc -o nologd -DHAVE_SYSTEMD $(pkgconfig --cflags --libs libsystemd) nologd.c
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#include <sys/epoll.h>
#ifdef HAVE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
#define NELEMS(arr) (sizeof(arr)/sizeof(arr[0]))
/* Server context */
struct Server {
int epoll_fd;
int dev_log_fd;
int journal_fd;
int stdout_fd;
int log_fd;
int kernel_fd;
};
enum {
SOCK_DEV_LOG = 0,
SOCK_JOURNAL_SOCKET,
SOCK_JOURNAL_STDOUT,
};
struct {
int type;
char *path;
} sockets[] = {
[SOCK_DEV_LOG] = { SOCK_DGRAM, "/run/systemd/journal/dev-log" },
[SOCK_JOURNAL_SOCKET] = { SOCK_DGRAM, "/run/systemd/journal/socket" },
[SOCK_JOURNAL_STDOUT] = { SOCK_STREAM, "/run/systemd/journal/stdout" },
};
static const char dev_kmsg_path[] = "/dev/kmsg";
static char *progname;
static int terminate_signal;
void epoll_addwatch(struct Server *s, int fd)
{
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };
epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
}
void fd_set_nonblock(int fd)
{
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
}
void babble(struct Server *s, int priority, const char *fmt, ...)
{
int fd = s->kernel_fd != -1 ? s->kernel_fd : STDOUT_FILENO;
va_list ap;
static char buf[1024];
unsigned int end;
int n;
n = snprintf(buf, sizeof buf, "<%d>%s: ", priority & 7, progname);
if (n < 0)
return;
end = n;
va_start(ap, fmt);
n = vsnprintf(buf + end, sizeof(buf) - end, fmt, ap);
va_end(ap);
if (n < 0)
return;
end += n;
if (end + 1 <= sizeof buf) {
buf[end++] = '\n';
buf[end] = 0;
}
write(fd, buf, end);
}
int unix_open(struct Server *s, int type, const char *path)
{
int r;
int fd;
struct sockaddr_un sa = { .sun_family = AF_UNIX };
fd = socket(AF_UNIX, type | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (fd < 0)
return -errno;
unlink(path);
strncpy(&sa.sun_path[0], path, NELEMS(sa.sun_path));
r = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
if (r < 0) {
close(fd);
return -errno;
}
r = (type == SOCK_STREAM) ? listen(fd, SOMAXCONN) : 0;
if (r < 0) {
close(fd);
return -errno;
}
return fd;
}
int unix_accept(struct Server *s, int stdout_fd)
{
int fd;
struct sockaddr_un sa;
socklen_t slen = sizeof(sa);
fd = accept4(stdout_fd, (struct sockaddr *)&sa, &slen, SOCK_NONBLOCK | SOCK_CLOEXEC);
return fd;
}
typedef int (*process_fn)(struct Server *, char *, int);
void consume(struct Server *s, int fd, int do_close, process_fn fn)
{
int r;
static char buf[2048];
do {
r = read(fd, &buf[0], NELEMS(buf) - 1);
fn(s, buf, r);
} while (r > 0);
if (do_close && r == 0) {
close(fd);
epoll_ctl(s->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
}
}
/* Expected format:
* <13>Feb 7 23:34:43 MSG
*/
int process_syslog(struct Server *s, char *buf, int len)
{
int start = 0;
int end = len - 1;
if (s->log_fd == -1 || len == -1)
return 0;
/* Drop numerically coded log level and facility as we perform no
* filtering based on this information.
*/
if (start < len && buf[start] == '<') {
do {
++start;
} while (start < len && isdigit(buf[start]));
if (buf[start] == '>')
++start;
}
while (end > 0 && buf[end] == '\n')
buf[end--] = '\0';
write(s->log_fd, buf + start, end - start);
write(s->log_fd, "\n", 1);
return end - start + 1;
}
int process_journal(struct Server *s, char *buf, int len)
{
int pos;
if (s->log_fd == -1 || len == -1)
return 0;
for (pos = 0; pos < len; pos++)
if (buf[pos] == '\n')
buf[pos] = ' ';
write(s->log_fd, buf, len);
write(s->log_fd, "\n", 1);
return len + 1;
}
int process_stream(struct Server *s, char *buf, int len)
{
if (s->log_fd == -1 || len == -1)
return 0;
/* To be implemented */
return 0;
}
void usage(void)
{
printf("usage: %s [-d] [-f FILE] [-h]\n"
" -d daemonize\n"
" -f FILE drop logs to FILE\n"
" -h this help screen\n",
progname);
}
int systemd_sock_get(struct Server *s)
{
int n = 0;
#ifdef HAVE_SYSTEMD
int i;
n = sd_listen_fds(1);
if (n < 0)
return 0;
for (i = SD_LISTEN_FDS_START; i < SD_LISTEN_FDS_START + n; i++) {
if (sd_is_socket_unix(i, sockets[SOCK_DEV_LOG].type, -1, sockets[SOCK_DEV_LOG].path, 0) > 0) {
s->dev_log_fd = i;
continue;
}
if (sd_is_socket_unix(i, sockets[SOCK_JOURNAL_SOCKET].type, -1, sockets[SOCK_JOURNAL_SOCKET].path, 0) > 0) {
s->journal_fd = i;
continue;
}
if (sd_is_socket_unix(i, sockets[SOCK_JOURNAL_STDOUT].type, 1, sockets[SOCK_JOURNAL_STDOUT].path, 0) > 0) {
s->stdout_fd = i;
continue;
}
}
#endif
return n;
}
void terminate(int signo)
{
if (!terminate_signal)
terminate_signal = signo;
}
int main(int argc, char *argv[])
{
int c;
int r;
struct Server s = {
.log_fd = -1,
.dev_log_fd = -1,
.journal_fd = -1,
.stdout_fd = -1,
.kernel_fd = -1,
};
struct epoll_event ev;
int do_daemonize = 0;
int nwatching = 0;
progname = argv[0];
do {
c = getopt(argc, argv, "dhf:");
if (c == 'd')
do_daemonize = 1;
else if (c == 'f') {
int fd = open(optarg, O_WRONLY | O_CREAT | O_APPEND, 0640);
if (fd < 0) {
babble(&s, LOG_CRIT, "Unable to open %s: %m", optarg);
exit(EXIT_FAILURE);
}
s.log_fd = fd;
} else if (c == 'h' || c == '?') {
usage();
exit(c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE);
}
} while (c != -1);
/* Ignore flush request for time being */
signal(SIGUSR1, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGINT, terminate);
signal(SIGTERM, terminate);
s.epoll_fd = epoll_create1(EPOLL_CLOEXEC);
mkdir("/run/systemd", 0755);
mkdir("/run/systemd/journal", 0755);
systemd_sock_get(&s);
if (s.dev_log_fd < 0)
s.dev_log_fd = unix_open(&s, sockets[SOCK_DEV_LOG].type, sockets[SOCK_DEV_LOG].path);
if (s.journal_fd < 0)
s.journal_fd = unix_open(&s, sockets[SOCK_JOURNAL_SOCKET].type, sockets[SOCK_JOURNAL_SOCKET].path);
if (s.stdout_fd < 0)
s.stdout_fd = unix_open(&s, sockets[SOCK_JOURNAL_STDOUT].type, sockets[SOCK_JOURNAL_STDOUT].path);
if (s.dev_log_fd >= 0) {
fd_set_nonblock(s.dev_log_fd);
epoll_addwatch(&s, s.dev_log_fd);
symlink(sockets[SOCK_DEV_LOG].path, "/dev/log");
++ nwatching;
}
if (s.journal_fd >= 0) {
fd_set_nonblock(s.journal_fd);
epoll_addwatch(&s, s.journal_fd);
++ nwatching;
}
if (s.stdout_fd >= 0) {
epoll_addwatch(&s, s.stdout_fd);
++ nwatching;
}
if (!nwatching) {
babble(&s, LOG_CRIT, "Unable to watch on any of defined sockets. Exiting");
exit(EXIT_FAILURE);
}
s.kernel_fd = open(dev_kmsg_path, O_WRONLY);
if (s.kernel_fd == -1)
babble(&s, LOG_WARNING, "Unable to open %s for logging. Skipping", dev_kmsg_path);
if (do_daemonize)
daemon(0, 0);
babble(&s, LOG_INFO, "Started. Consuming logs for: %s %s %s",
s.dev_log_fd != -1 ? "syslog" : "",
s.journal_fd != -1 ? "journal-packets" : "",
s.stdout_fd != -1 ? "journal-streams" : "");
while (!terminate_signal) {
r = epoll_wait(s.epoll_fd, &ev, 1, -1);
if (r < 0 && errno != EINTR) {
babble(&s, LOG_CRIT, "epoll_wait failed: %s. Exiting", strerror(errno));
exit(EXIT_FAILURE);
}
if (ev.data.fd == s.stdout_fd) {
int newfd = unix_accept(&s, s.stdout_fd);
if (newfd >= 0) {
fd_set_nonblock(newfd);
epoll_addwatch(&s, newfd);
} else
babble(&s, LOG_ERR, "accept failed: %s. Ignoring", strerror(errno));
continue;
}
if (ev.data.fd == s.dev_log_fd) {
consume(&s, s.dev_log_fd, 0, process_syslog);
} else if (ev.data.fd == s.journal_fd) {
consume(&s, s.journal_fd, 0, process_journal);
} else {
/* pre-opened stdout fd */
consume(&s, ev.data.fd, 1, process_stream);
}
}
babble(&s, LOG_NOTICE, "Terminated by signal %d", terminate_signal);
return EXIT_SUCCESS;
}