1
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4
This file is part of systemd.
6
Copyright 2010 Lennart Poettering
8
systemd is free software; you can redistribute it and/or modify it
9
under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
systemd is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with systemd; If not, see <http://www.gnu.org/licenses/>.
22
#include <sys/socket.h>
23
#include <sys/types.h>
31
#include <sys/epoll.h>
34
#include <sys/signalfd.h>
38
#include "sd-daemon.h"
41
#define SERVER_FD_MAX 16
43
typedef struct Stream Stream;
45
typedef struct Server {
52
static void server_done(Server *s) {
56
close_nointr_nofail(s->epoll_fd);
59
close_nointr_nofail(s->kmsg_fd);
61
if (s->signal_fd >= 0)
62
close_nointr_nofail(s->signal_fd);
65
fdset_free(s->syslog_fds);
68
static int server_init(Server *s, unsigned n_sockets) {
71
struct epoll_event ev;
75
assert(n_sockets > 0);
79
s->kmsg_fd = s->signal_fd = -1;
81
if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
83
log_error("Failed to create epoll object: %s", strerror(errno));
87
if (!(s->syslog_fds = fdset_new())) {
89
log_error("Failed to allocate file descriptor set: %s", strerror(errno));
93
for (i = 0; i < n_sockets; i++) {
96
fd = SD_LISTEN_FDS_START+i;
98
if ((r = sd_is_socket(fd, AF_UNSPEC, SOCK_DGRAM, -1)) < 0) {
99
log_error("Failed to determine file descriptor type: %s", strerror(-r));
104
log_error("Wrong file descriptor type.");
109
if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
110
log_error("SO_PASSCRED failed: %m");
115
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
117
log_error("Failed to add server fd to epoll object: %s", strerror(errno));
121
if ((r = fdset_put(s->syslog_fds, fd)) < 0) {
122
log_error("Failed to store file descriptor in set: %s", strerror(-r));
127
if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
128
log_error("Failed to open /dev/kmsg for logging: %m");
132
assert_se(sigemptyset(&mask) == 0);
133
sigset_add_many(&mask, SIGINT, SIGTERM, -1);
134
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
136
if ((s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
137
log_error("signalfd(): %m");
143
ev.data.fd = s->signal_fd;
145
if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
146
log_error("epoll_ctl(): %m");
157
static void skip_date(const char **buf) {
165
LETTER, LETTER, LETTER,
167
SPACE_OR_NUMBER, NUMBER,
169
SPACE_OR_NUMBER, NUMBER,
171
SPACE_OR_NUMBER, NUMBER,
173
SPACE_OR_NUMBER, NUMBER,
185
for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
190
switch (sequence[i]) {
197
case SPACE_OR_NUMBER:
204
if (*p < '0' || *p > '9')
210
if (!(*p >= 'A' && *p <= 'Z') &&
211
!(*p >= 'a' && *p <= 'z'))
227
static int read_process(const char **buf, struct iovec *iovec) {
237
p += strspn(p, WHITESPACE);
238
l = strcspn(p, WHITESPACE);
263
iovec->iov_base = (char*) p;
269
static void skip_pid(const char **buf) {
281
p += strspn(p, "0123456789");
291
static int write_message(Server *s, const char *buf, struct ucred *ucred) {
293
char priority[6], pid[16];
294
struct iovec iovec[5];
296
char *process = NULL;
298
int prio = LOG_USER | LOG_INFO;
303
parse_syslog_priority((char**) &buf, &prio);
308
if ((prio & LOG_FACMASK) == 0)
309
prio = LOG_USER | LOG_PRI(prio);
311
/* First, set priority field */
312
snprintf(priority, sizeof(priority), "<%i>", prio);
313
char_array_0(priority);
314
IOVEC_SET_STRING(iovec[i++], priority);
316
/* Second, skip date */
319
/* Then, add process if set */
320
if (read_process(&buf, &iovec[i]) > 0)
324
get_process_name(ucred->pid, &process) >= 0)
325
IOVEC_SET_STRING(iovec[i++], process);
327
/* Skip the stored PID if we have a better one */
329
snprintf(pid, sizeof(pid), "[%lu]: ", (unsigned long) ucred->pid);
331
IOVEC_SET_STRING(iovec[i++], pid);
338
buf += strspn(buf, WHITESPACE);
341
/* Is the remaining message empty? */
344
/* And the rest is the message */
345
IOVEC_SET_STRING(iovec[i++], buf);
346
IOVEC_SET_STRING(iovec[i++], "\n");
348
if ((k = writev(s->kmsg_fd, iovec, i)) <= 0) {
349
log_error("Failed to write log message to kmsg: %s", k < 0 ? strerror(errno) : "short write");
350
r = k < 0 ? -errno : -EIO;
359
static int process_event(Server *s, struct epoll_event *ev) {
362
if (ev->events != EPOLLIN) {
363
log_info("Got invalid event from epoll.");
367
if (ev->data.fd == s->signal_fd) {
368
struct signalfd_siginfo sfsi;
371
if ((n = read(s->signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
376
if (errno == EINTR || errno == EAGAIN)
382
log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
387
char buf[LINE_MAX+1];
388
struct msghdr msghdr;
392
struct cmsghdr cmsghdr;
393
uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
400
iovec.iov_base = buf;
401
iovec.iov_len = sizeof(buf)-1;
405
msghdr.msg_iov = &iovec;
406
msghdr.msg_iovlen = 1;
407
msghdr.msg_control = &control;
408
msghdr.msg_controllen = sizeof(control);
410
if ((n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT)) < 0) {
412
if (errno == EINTR || errno == EAGAIN)
415
log_error("recvmsg() failed: %m");
419
if (msghdr.msg_controllen >= CMSG_LEN(sizeof(struct ucred)) &&
420
control.cmsghdr.cmsg_level == SOL_SOCKET &&
421
control.cmsghdr.cmsg_type == SCM_CREDENTIALS &&
422
control.cmsghdr.cmsg_len == CMSG_LEN(sizeof(struct ucred)))
423
ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
427
if ((e = memchr(buf, '\n', n)))
432
if ((k = write_message(s, strstrip(buf), ucred)) < 0)
440
int main(int argc, char *argv[]) {
442
int r = EXIT_FAILURE, n;
444
if (getppid() != 1) {
445
log_error("This program should be invoked by init only.");
450
log_error("This program does not take arguments.");
454
log_set_target(LOG_TARGET_KMSG);
455
log_parse_environment();
460
if ((n = sd_listen_fds(true)) < 0) {
461
log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
465
if (n <= 0 || n > SERVER_FD_MAX) {
466
log_error("No or too many file descriptors passed.");
470
if (server_init(&server, (unsigned) n) < 0)
473
log_debug("systemd-kmsg-syslogd running as pid %lu", (unsigned long) getpid());
477
"STATUS=Processing messages...");
480
struct epoll_event event;
483
if ((k = epoll_wait(server.epoll_fd, &event, 1, -1)) < 0) {
488
log_error("epoll_wait() failed: %m");
495
if ((k = process_event(&server, &event)) < 0)
504
log_debug("systemd-kmsg-syslogd stopped as pid %lu", (unsigned long) getpid());
508
"STATUS=Shutting down...");
510
server_done(&server);