1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
3 #include <sys/socket.h>
12 #include <sys/epoll.h>
19 #define STREAM_BUFFER 2048
20 #define STREAMS_MAX 256
21 #define SERVER_FD_START 3
22 #define SERVER_FD_MAX 16
23 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
25 typedef struct Stream Stream;
27 typedef struct Server {
33 LIST_HEAD(Stream, streams);
37 typedef enum StreamState {
52 char buffer[STREAM_BUFFER];
57 LIST_FIELDS(Stream, stream);
60 #define IOVEC_SET_STRING(iovec, s) \
62 (iovec).iov_base = s; \
63 (iovec).iov_len = strlen(s); \
66 static int stream_log(Stream *s, char *p, usec_t timestamp) {
68 char header_priority[16], header_time[64], header_pid[16];
72 struct iovec iovec[5];
81 * The format glibc uses is:
83 * <priority>time process[pid]: msg
86 snprintf(header_priority, sizeof(header_priority), "<%i>", s->priority);
87 char_array_0(header_priority);
89 t = (time_t) (timestamp / USEC_PER_SEC);
90 if (!(tm = localtime(&t)))
93 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
96 snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) s->pid);
97 char_array_0(header_pid);
100 IOVEC_SET_STRING(iovec[0], header_priority);
101 IOVEC_SET_STRING(iovec[1], header_time);
102 IOVEC_SET_STRING(iovec[2], s->process);
103 IOVEC_SET_STRING(iovec[3], header_pid);
104 IOVEC_SET_STRING(iovec[4], p);
107 msghdr.msg_iov = iovec;
108 msghdr.msg_iovlen = ELEMENTSOF(iovec);
110 if (sendmsg(s->server->log_fd, &msghdr, MSG_NOSIGNAL) < 0)
116 static int stream_line(Stream *s, char *p, usec_t timestamp) {
126 case STREAM_PRIORITY:
127 if ((r = safe_atoi(p, &s->priority)) < 0)
133 s->state = STREAM_PROCESS;
137 if (!(s->process = strdup(p)))
140 s->state = STREAM_RUNNING;
144 return stream_log(s, p, timestamp);
147 assert_not_reached("Unknown stream state");
150 static int stream_scan(Stream *s, usec_t timestamp) {
158 remaining = s->length;
162 if (!(newline = memchr(p, '\n', remaining)))
167 if ((r = stream_line(s, p, timestamp)) >= 0) {
168 remaining -= newline-p+1;
174 memmove(s->buffer, p, remaining);
175 s->length = remaining;
181 static int stream_process(Stream *s, usec_t timestamp) {
186 if ((l = read(s->fd, s->buffer+s->length, STREAM_BUFFER-s->length)) < 0) {
191 log_warning("Failed to read from stream: %s", strerror(errno));
200 r = stream_scan(s, timestamp);
208 static void stream_free(Stream *s) {
212 assert(s->server->n_streams > 0);
213 s->server->n_streams--;
214 LIST_REMOVE(Stream, stream, s->server->streams, s);
220 epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
222 assert_se(close_nointr(s->fd) == 0);
229 static int stream_new(Server *s, int server_fd) {
233 socklen_t len = sizeof(ucred);
234 struct epoll_event ev;
239 if ((fd = accept4(server_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC)) < 0)
242 if (s->n_streams >= STREAMS_MAX) {
243 log_warning("Too many connections, refusing connection.");
244 assert_se(close_nointr(fd) == 0);
248 if (!(stream = new0(Stream, 1))) {
249 assert_se(close_nointr(fd) == 0);
255 if (getsockopt(stream->fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
260 if (shutdown(fd, SHUT_WR) < 0) {
266 ev.data.ptr = stream;
268 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
273 stream->pid = ucred.pid;
276 LIST_PREPEND(Stream, stream, s->streams, stream);
286 static int verify_environment(unsigned *n_sockets) {
287 unsigned long long pid;
292 assert_se(n_sockets);
294 if (!(e = getenv("LISTEN_PID"))) {
295 log_error("Missing $LISTEN_PID environment variable.");
299 if ((r = safe_atollu(e, &pid)) < 0) {
300 log_error("Failed to parse $LISTEN_PID: %s", strerror(-r));
304 if (pid != (unsigned long long) getpid()) {
305 log_error("Socket nor for me.");
309 if (!(e = getenv("LISTEN_FDS"))) {
310 log_error("Missing $LISTEN_FDS environment variable.");
314 if ((r = safe_atou(e, &ns)) < 0) {
315 log_error("Failed to parse $LISTEN_FDS: %s", strerror(-r));
319 if (ns <= 0 || ns > SERVER_FD_MAX) {
320 log_error("Wrong number of file descriptors passed: %s", e);
329 static void server_done(Server *s) {
334 stream_free(s->streams);
336 for (i = 0; i < s->n_server_fd; i++)
337 assert_se(close_nointr(SERVER_FD_START+i) == 0);
340 assert_se(close_nointr(s->log_fd) == 0);
342 if (s->epoll_fd >= 0)
343 assert_se(close_nointr(s->epoll_fd) == 0);
346 static int server_init(Server *s, unsigned n_sockets) {
351 struct sockaddr_un un;
355 assert(n_sockets > 0);
359 s->n_server_fd = n_sockets;
362 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
364 log_error("Failed to create epoll object: %s", strerror(errno));
368 for (i = 0; i < n_sockets; i++) {
369 struct epoll_event ev;
373 ev.data.ptr = UINT_TO_PTR(SERVER_FD_START+i);
374 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, SERVER_FD_START+i, &ev) < 0) {
376 log_error("Failed to add server fd to epoll object: %s", strerror(errno));
381 if ((s->log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
383 log_error("Failed to create log fd: %s", strerror(errno));
388 sa.un.sun_family = AF_UNIX;
389 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
391 if (connect(s->log_fd, &sa.sa, sizeof(sa)) < 0) {
393 log_error("Failed to connect log socket to /dev/log: %s", strerror(errno));
404 static int process_event(Server *s, struct epoll_event *ev) {
409 /* Yes, this is a bit ugly, we assume that that valid pointers
410 * are > SERVER_FD_START+SERVER_FD_MAX. Which is certainly
411 * true on Linux (and probably most other OSes, too, since the
412 * first 4k usually are part of a seperate null pointer
413 * dereference page. */
415 if (PTR_TO_UINT(ev->data.ptr) >= SERVER_FD_START &&
416 PTR_TO_UINT(ev->data.ptr) < SERVER_FD_START+s->n_server_fd) {
418 if (ev->events != POLLIN) {
419 log_info("Got invalid event from epoll. (1)");
423 if ((r = stream_new(s, PTR_TO_UINT(ev->data.ptr))) < 0) {
424 log_info("Failed to accept new connection: %s", strerror(-r));
430 Stream *stream = ev->data.ptr;
432 timestamp = now(CLOCK_REALTIME);
434 if (!(ev->events & POLLIN)) {
435 log_info("Got invalid event from epoll. (3)");
440 if ((r = stream_process(stream, timestamp)) <= 0) {
443 log_info("Got error on stream: %s", strerror(-r));
453 int main(int argc, char *argv[]) {
458 log_info("systemd-logger running as pid %llu", (unsigned long long) getpid());
460 if (verify_environment(&n) < 0)
463 if (server_init(&server, n) < 0)
468 struct epoll_event event;
471 if ((n = epoll_wait(server.epoll_fd,
473 server.n_streams <= 0 ? TIMEOUT : -1)) < 0) {
478 log_error("epoll_wait() failed: %s", strerror(errno));
485 if ((r = process_event(&server, &event)) < 0)
491 server_done(&server);
493 log_info("systemd-logger stopped as pid %llu", (unsigned long long) getpid());