1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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>
38 #include "sd-daemon.h"
40 #define STREAM_BUFFER 2048
41 #define STREAMS_MAX 256
42 #define SERVER_FD_MAX 16
43 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
45 typedef struct Stream Stream;
47 typedef struct Server {
54 LIST_HEAD(Stream, streams);
58 typedef enum StreamState {
78 char buffer[STREAM_BUFFER];
81 LIST_FIELDS(Stream, stream);
84 static int stream_log(Stream *s, char *p, usec_t timestamp) {
86 char header_priority[16], header_time[64], header_pid[16];
87 struct iovec iovec[5];
96 * The format glibc uses to talk to the syslog daemon is:
98 * <priority>time process[pid]: msg
100 * The format the kernel uses is:
104 * We extend the latter to include the process name and pid.
107 snprintf(header_priority, sizeof(header_priority), "<%i>",
108 s->target == LOG_TARGET_SYSLOG ? s->priority : LOG_PRI(s->priority));
109 char_array_0(header_priority);
111 if (s->target == LOG_TARGET_SYSLOG) {
115 t = (time_t) (timestamp / USEC_PER_SEC);
116 if (!(tm = localtime(&t)))
119 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
123 snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) s->pid);
124 char_array_0(header_pid);
127 IOVEC_SET_STRING(iovec[0], header_priority);
129 if (s->target == LOG_TARGET_SYSLOG) {
130 struct msghdr msghdr;
132 IOVEC_SET_STRING(iovec[1], header_time);
133 IOVEC_SET_STRING(iovec[2], s->process);
134 IOVEC_SET_STRING(iovec[3], header_pid);
135 IOVEC_SET_STRING(iovec[4], p);
138 msghdr.msg_iov = iovec;
139 msghdr.msg_iovlen = ELEMENTSOF(iovec);
141 if (sendmsg(s->server->syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
144 } else if (s->target == LOG_TARGET_KMSG) {
145 IOVEC_SET_STRING(iovec[1], s->process);
146 IOVEC_SET_STRING(iovec[2], header_pid);
147 IOVEC_SET_STRING(iovec[3], p);
148 IOVEC_SET_STRING(iovec[4], (char*) "\n");
150 if (writev(s->server->kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
153 assert_not_reached("Unknown log target");
158 static int stream_line(Stream *s, char *p, usec_t timestamp) {
168 case STREAM_LOG_TARGET:
169 if (streq(p, "syslog"))
170 s->target = LOG_TARGET_SYSLOG;
171 else if (streq(p, "kmsg")) {
173 if (s->server->kmsg_fd >= 0 && s->uid == 0)
174 s->target = LOG_TARGET_KMSG;
176 log_warning("/dev/kmsg logging not available.");
180 log_warning("Failed to parse log target line.");
183 s->state = STREAM_PRIORITY;
186 case STREAM_PRIORITY:
187 if ((r = safe_atoi(p, &s->priority)) < 0) {
188 log_warning("Failed to parse log priority line: %s", strerror(errno));
192 if (s->priority < 0) {
193 log_warning("Log priority negative: %s", strerror(errno));
197 s->state = STREAM_PROCESS;
201 if (!(s->process = strdup(p)))
204 s->state = STREAM_RUNNING;
208 return stream_log(s, p, timestamp);
211 assert_not_reached("Unknown stream state");
214 static int stream_scan(Stream *s, usec_t timestamp) {
222 remaining = s->length;
226 if (!(newline = memchr(p, '\n', remaining)))
231 if ((r = stream_line(s, p, timestamp)) >= 0) {
232 remaining -= newline-p+1;
238 memmove(s->buffer, p, remaining);
239 s->length = remaining;
245 static int stream_process(Stream *s, usec_t timestamp) {
250 if ((l = read(s->fd, s->buffer+s->length, STREAM_BUFFER-s->length)) < 0) {
255 log_warning("Failed to read from stream: %s", strerror(errno));
264 r = stream_scan(s, timestamp);
272 static void stream_free(Stream *s) {
276 assert(s->server->n_streams > 0);
277 s->server->n_streams--;
278 LIST_REMOVE(Stream, stream, s->server->streams, s);
284 epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
286 close_nointr_nofail(s->fd);
293 static int stream_new(Server *s, int server_fd) {
297 socklen_t len = sizeof(ucred);
298 struct epoll_event ev;
303 if ((fd = accept4(server_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC)) < 0)
306 if (s->n_streams >= STREAMS_MAX) {
307 log_warning("Too many connections, refusing connection.");
308 close_nointr_nofail(fd);
312 if (!(stream = new0(Stream, 1))) {
313 close_nointr_nofail(fd);
319 if (getsockopt(stream->fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
324 if (shutdown(fd, SHUT_WR) < 0) {
330 ev.data.ptr = stream;
332 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
337 stream->pid = ucred.pid;
338 stream->uid = ucred.uid;
341 LIST_PREPEND(Stream, stream, s->streams, stream);
351 static void server_done(Server *s) {
356 stream_free(s->streams);
358 for (i = 0; i < s->n_server_fd; i++)
359 close_nointr_nofail(SD_LISTEN_FDS_START+i);
361 if (s->syslog_fd >= 0)
362 close_nointr_nofail(s->syslog_fd);
364 if (s->epoll_fd >= 0)
365 close_nointr_nofail(s->epoll_fd);
368 close_nointr_nofail(s->kmsg_fd);
371 static int server_init(Server *s, unsigned n_sockets) {
376 struct sockaddr_un un;
380 assert(n_sockets > 0);
384 s->n_server_fd = n_sockets;
388 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
390 log_error("Failed to create epoll object: %s", strerror(errno));
394 for (i = 0; i < n_sockets; i++) {
395 struct epoll_event ev;
399 ev.data.ptr = UINT_TO_PTR(SD_LISTEN_FDS_START+i);
400 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SD_LISTEN_FDS_START+i, &ev) < 0) {
402 log_error("Failed to add server fd to epoll object: %s", strerror(errno));
407 if ((s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
409 log_error("Failed to create log fd: %s", strerror(errno));
414 sa.un.sun_family = AF_UNIX;
415 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
417 if (connect(s->syslog_fd, &sa.sa, sizeof(sa)) < 0) {
419 log_error("Failed to connect log socket to /dev/log: %s", strerror(errno));
423 /* /dev/kmsg logging is strictly optional */
424 if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
425 log_debug("Failed to open /dev/kmsg for logging, disabling kernel log buffer support: %s", strerror(errno));
434 static int process_event(Server *s, struct epoll_event *ev) {
439 /* Yes, this is a bit ugly, we assume that that valid pointers
440 * are > SD_LISTEN_FDS_START+SERVER_FD_MAX. Which is certainly
441 * true on Linux (and probably most other OSes, too, since the
442 * first 4k usually are part of a seperate null pointer
443 * dereference page. */
445 if (PTR_TO_UINT(ev->data.ptr) >= SD_LISTEN_FDS_START &&
446 PTR_TO_UINT(ev->data.ptr) < SD_LISTEN_FDS_START+s->n_server_fd) {
448 if (ev->events != EPOLLIN) {
449 log_info("Got invalid event from epoll. (1)");
453 if ((r = stream_new(s, PTR_TO_UINT(ev->data.ptr))) < 0) {
454 log_info("Failed to accept new connection: %s", strerror(-r));
460 Stream *stream = ev->data.ptr;
462 timestamp = now(CLOCK_REALTIME);
464 if (!(ev->events & EPOLLIN)) {
465 log_info("Got invalid event from epoll. (2)");
470 if ((r = stream_process(stream, timestamp)) <= 0) {
473 log_info("Got error on stream: %s", strerror(-r));
483 int main(int argc, char *argv[]) {
487 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
488 log_parse_environment();
490 log_info("systemd-logger running as pid %llu", (unsigned long long) getpid());
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)
506 struct epoll_event event;
509 if ((k = epoll_wait(server.epoll_fd,
511 server.n_streams <= 0 ? TIMEOUT : -1)) < 0) {
516 log_error("epoll_wait() failed: %s", strerror(errno));
523 if ((k = process_event(&server, &event)) < 0)
529 server_done(&server);
531 log_info("systemd-logger stopped as pid %llu", (unsigned long long) getpid());