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
42 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
44 typedef struct Stream Stream;
46 typedef struct Server {
53 static void server_done(Server *s) {
57 close_nointr_nofail(s->epoll_fd);
60 close_nointr_nofail(s->kmsg_fd);
62 if (s->signal_fd >= 0)
63 close_nointr_nofail(s->signal_fd);
66 fdset_free(s->syslog_fds);
69 static int server_init(Server *s, unsigned n_sockets) {
72 struct epoll_event ev;
76 assert(n_sockets > 0);
80 s->kmsg_fd = s->signal_fd = -1;
82 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
84 log_error("Failed to create epoll object: %s", strerror(errno));
88 if (!(s->syslog_fds = fdset_new())) {
90 log_error("Failed to allocate file descriptor set: %s", strerror(errno));
94 for (i = 0; i < n_sockets; i++) {
97 fd = SD_LISTEN_FDS_START+i;
99 if ((r = sd_is_socket(fd, AF_UNSPEC, SOCK_DGRAM, -1)) < 0) {
100 log_error("Failed to determine file descriptor type: %s", strerror(-r));
105 log_error("Wrong file descriptor type.");
110 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
111 log_error("SO_PASSCRED failed: %m");
116 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
118 log_error("Failed to add server fd to epoll object: %s", strerror(errno));
122 if ((r = fdset_put(s->syslog_fds, fd)) < 0) {
123 log_error("Failed to store file descriptor in set: %s", strerror(-r));
128 if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
129 log_error("Failed to open /dev/kmsg for logging: %m");
133 assert_se(sigemptyset(&mask) == 0);
134 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
135 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
137 if ((s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
138 log_error("signalfd(): %m");
144 ev.data.fd = s->signal_fd;
146 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
147 log_error("epoll_ctl(): %m");
158 static int read_priority(const char **buf) {
170 if (n < 3 || p[0] != '<')
177 } else if (n >= 4 && p[3] == '>') {
182 } else if (n >= 5 && p[4] == '>') {
190 if (a < 0 || b < 0 || c < 0)
195 priority = 100*a + 10*b + c;
196 return LOG_PRI(priority);
202 static void skip_date(const char **buf) {
210 LETTER, LETTER, LETTER,
212 SPACE_OR_NUMBER, NUMBER,
214 SPACE_OR_NUMBER, NUMBER,
216 SPACE_OR_NUMBER, NUMBER,
218 SPACE_OR_NUMBER, NUMBER,
230 for (i = 0; i < ELEMENTSOF(sequence); i++, p++) {
235 switch (sequence[i]) {
242 case SPACE_OR_NUMBER:
249 if (*p < '0' || *p > '9')
255 if (!(*p >= 'A' && *p <= 'Z') &&
256 !(*p >= 'a' && *p <= 'z'))
272 static int read_process(const char **buf, struct iovec *iovec) {
282 p += strspn(p, WHITESPACE);
283 l = strcspn(p, WHITESPACE);
308 iovec->iov_base = (char*) p;
314 static void skip_pid(const char **buf) {
326 p += strspn(p, "0123456789");
336 static int write_message(Server *s, const char *buf, struct ucred *ucred) {
338 char priority[4], pid[16];
339 struct iovec iovec[5];
341 char *process = NULL;
347 /* First, set priority field */
348 snprintf(priority, sizeof(priority), "<%i>", read_priority(&buf));
349 char_array_0(priority);
350 IOVEC_SET_STRING(iovec[i++], priority);
352 /* Second, skip date */
355 /* Then, add process if set */
356 if (read_process(&buf, &iovec[i]) > 0)
358 else if (ucred && get_process_name(ucred->pid, &process) >= 0)
359 IOVEC_SET_STRING(iovec[i++], process);
361 /* Skip the stored PID if we have a better one */
363 snprintf(pid, sizeof(pid), "[%lu]: ", (unsigned long) ucred->pid);
365 IOVEC_SET_STRING(iovec[i++], pid);
372 buf += strspn(buf, WHITESPACE);
375 /* Is the remaining message empty? */
378 /* And the rest is the message */
379 IOVEC_SET_STRING(iovec[i++], buf);
380 IOVEC_SET_STRING(iovec[i++], "\n");
382 if ((k = writev(s->kmsg_fd, iovec, i)) <= 0) {
383 log_error("Failed to write log message to kmsg: %s", k < 0 ? strerror(errno) : "short write");
384 r = k < 0 ? -errno : -EIO;
393 static int process_event(Server *s, struct epoll_event *ev) {
396 if (ev->events != EPOLLIN) {
397 log_info("Got invalid event from epoll.");
401 if (ev->data.fd == s->signal_fd) {
402 struct signalfd_siginfo sfsi;
405 if ((n = read(s->signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
410 if (errno == EINTR || errno == EAGAIN)
416 log_debug("Received SIG%s", strna(signal_to_string(sfsi.ssi_signo)));
421 char buf[LINE_MAX+1];
422 struct msghdr msghdr;
426 struct cmsghdr cmsghdr;
427 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
434 iovec.iov_base = buf;
435 iovec.iov_len = sizeof(buf)-1;
439 msghdr.msg_iov = &iovec;
440 msghdr.msg_iovlen = 1;
441 msghdr.msg_control = &control;
442 msghdr.msg_controllen = sizeof(control);
444 if ((n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT)) < 0) {
446 if (errno == EINTR || errno == EAGAIN)
449 log_error("recvmsg() failed: %m");
453 if (msghdr.msg_controllen >= CMSG_LEN(sizeof(struct ucred)) &&
454 control.cmsghdr.cmsg_level == SOL_SOCKET &&
455 control.cmsghdr.cmsg_type == SCM_CREDENTIALS &&
456 control.cmsghdr.cmsg_len == CMSG_LEN(sizeof(struct ucred)))
457 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
461 if ((e = memchr(buf, '\n', n)))
466 if ((k = write_message(s, strstrip(buf), ucred)) < 0)
474 int main(int argc, char *argv[]) {
476 int r = EXIT_FAILURE, n;
478 if (getppid() != 1) {
479 log_error("This program should be invoked by init only.");
484 log_error("This program does not take arguments.");
488 log_set_target(LOG_TARGET_KMSG);
489 log_parse_environment();
492 if ((n = sd_listen_fds(true)) < 0) {
493 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
497 if (n <= 0 || n > SERVER_FD_MAX) {
498 log_error("No or too many file descriptors passed.");
502 if (server_init(&server, (unsigned) n) < 0)
505 log_debug("systemd-kmsg-syslogd running as pid %lu", (unsigned long) getpid());
509 "STATUS=Processing messages...");
512 struct epoll_event event;
515 if ((k = epoll_wait(server.epoll_fd, &event, 1, TIMEOUT)) < 0) {
520 log_error("epoll_wait() failed: %m");
527 if ((k = process_event(&server, &event)) < 0)
536 log_debug("systemd-kmsg-syslogd stopped as pid %lu", (unsigned long) getpid());
540 "STATUS=Shutting down...");
542 server_done(&server);