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 int read_priority(const char **buf) {
169 if (n < 3 || p[0] != '<')
176 } else if (n >= 4 && p[3] == '>') {
181 } else if (n >= 5 && p[4] == '>') {
189 if (a < 0 || b < 0 || c < 0)
194 priority = 100*a + 10*b + c;
195 return LOG_PRI(priority);
201 static void skip_date(const char **buf) {
209 LETTER, LETTER, LETTER,
211 SPACE_OR_NUMBER, NUMBER,
213 SPACE_OR_NUMBER, NUMBER,
215 SPACE_OR_NUMBER, NUMBER,
217 SPACE_OR_NUMBER, NUMBER,
229 for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
234 switch (sequence[i]) {
241 case SPACE_OR_NUMBER:
248 if (*p < '0' || *p > '9')
254 if (!(*p >= 'A' && *p <= 'Z') &&
255 !(*p >= 'a' && *p <= 'z'))
271 static int read_process(const char **buf, struct iovec *iovec) {
281 p += strspn(p, WHITESPACE);
282 l = strcspn(p, WHITESPACE);
307 iovec->iov_base = (char*) p;
313 static void skip_pid(const char **buf) {
325 p += strspn(p, "0123456789");
335 static int write_message(Server *s, const char *buf, struct ucred *ucred) {
337 char priority[4], pid[16];
338 struct iovec iovec[5];
340 char *process = NULL;
346 /* First, set priority field */
347 snprintf(priority, sizeof(priority), "<%i>", read_priority(&buf));
348 char_array_0(priority);
349 IOVEC_SET_STRING(iovec[i++], priority);
351 /* Second, skip date */
354 /* Then, add process if set */
355 if (read_process(&buf, &iovec[i]) > 0)
359 get_process_name(ucred->pid, &process) >= 0)
360 IOVEC_SET_STRING(iovec[i++], process);
362 /* Skip the stored PID if we have a better one */
364 snprintf(pid, sizeof(pid), "[%lu]: ", (unsigned long) ucred->pid);
366 IOVEC_SET_STRING(iovec[i++], pid);
373 buf += strspn(buf, WHITESPACE);
376 /* Is the remaining message empty? */
379 /* And the rest is the message */
380 IOVEC_SET_STRING(iovec[i++], buf);
381 IOVEC_SET_STRING(iovec[i++], "\n");
383 if ((k = writev(s->kmsg_fd, iovec, i)) <= 0) {
384 log_error("Failed to write log message to kmsg: %s", k < 0 ? strerror(errno) : "short write");
385 r = k < 0 ? -errno : -EIO;
394 static int process_event(Server *s, struct epoll_event *ev) {
397 if (ev->events != EPOLLIN) {
398 log_info("Got invalid event from epoll.");
402 if (ev->data.fd == s->signal_fd) {
403 struct signalfd_siginfo sfsi;
406 if ((n = read(s->signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
411 if (errno == EINTR || errno == EAGAIN)
417 log_debug("Received SIG%s", strna(signal_to_string(sfsi.ssi_signo)));
422 char buf[LINE_MAX+1];
423 struct msghdr msghdr;
427 struct cmsghdr cmsghdr;
428 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
435 iovec.iov_base = buf;
436 iovec.iov_len = sizeof(buf)-1;
440 msghdr.msg_iov = &iovec;
441 msghdr.msg_iovlen = 1;
442 msghdr.msg_control = &control;
443 msghdr.msg_controllen = sizeof(control);
445 if ((n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT)) < 0) {
447 if (errno == EINTR || errno == EAGAIN)
450 log_error("recvmsg() failed: %m");
454 if (msghdr.msg_controllen >= CMSG_LEN(sizeof(struct ucred)) &&
455 control.cmsghdr.cmsg_level == SOL_SOCKET &&
456 control.cmsghdr.cmsg_type == SCM_CREDENTIALS &&
457 control.cmsghdr.cmsg_len == CMSG_LEN(sizeof(struct ucred)))
458 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
462 if ((e = memchr(buf, '\n', n)))
467 if ((k = write_message(s, strstrip(buf), ucred)) < 0)
475 int main(int argc, char *argv[]) {
477 int r = EXIT_FAILURE, n;
479 if (getppid() != 1) {
480 log_error("This program should be invoked by init only.");
485 log_error("This program does not take arguments.");
489 log_set_target(LOG_TARGET_KMSG);
490 log_parse_environment();
493 if ((n = sd_listen_fds(true)) < 0) {
494 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
498 if (n <= 0 || n > SERVER_FD_MAX) {
499 log_error("No or too many file descriptors passed.");
503 if (server_init(&server, (unsigned) n) < 0)
506 log_debug("systemd-kmsg-syslogd running as pid %lu", (unsigned long) getpid());
510 "STATUS=Processing messages...");
513 struct epoll_event event;
516 if ((k = epoll_wait(server.epoll_fd, &event, 1, -1)) < 0) {
521 log_error("epoll_wait() failed: %m");
528 if ((k = process_event(&server, &event)) < 0)
537 log_debug("systemd-kmsg-syslogd stopped as pid %lu", (unsigned long) getpid());
541 "STATUS=Shutting down...");
543 server_done(&server);