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"
41 #define STREAM_BUFFER 2048
42 #define STREAMS_MAX 256
43 #define SERVER_FD_MAX 16
44 #define TIMEOUT ((int) (10*MSEC_PER_SEC))
46 typedef struct Stream Stream
;
48 typedef struct Server
{
55 LIST_HEAD(Stream
, streams
);
59 typedef enum StreamTarget
{
64 typedef enum StreamState
{
87 char buffer
[STREAM_BUFFER
];
90 LIST_FIELDS(Stream
, stream
);
93 static int stream_log(Stream
*s
, char *p
, usec_t ts
) {
95 char header_priority
[16], header_time
[64], header_pid
[16];
96 struct iovec iovec
[5];
102 priority
= s
->priority
;
106 p
[1] >= '0' && p
[1] <= '7' &&
109 /* Detected priority prefix */
110 priority
= LOG_MAKEPRI(LOG_FAC(priority
), (p
[1] - '0'));
119 * The format glibc uses to talk to the syslog daemon is:
121 * <priority>time process[pid]: msg
123 * The format the kernel uses is:
127 * We extend the latter to include the process name and pid.
130 snprintf(header_priority
, sizeof(header_priority
), "<%i>",
131 s
->target
== STREAM_SYSLOG
? priority
: LOG_PRI(priority
));
132 char_array_0(header_priority
);
134 if (s
->target
== STREAM_SYSLOG
) {
138 t
= (time_t) (ts
/ USEC_PER_SEC
);
139 if (!(tm
= localtime(&t
)))
142 if (strftime(header_time
, sizeof(header_time
), "%h %e %T ", tm
) <= 0)
146 snprintf(header_pid
, sizeof(header_pid
), "[%lu]: ", (unsigned long) s
->pid
);
147 char_array_0(header_pid
);
150 IOVEC_SET_STRING(iovec
[0], header_priority
);
152 if (s
->target
== STREAM_SYSLOG
) {
153 struct msghdr msghdr
;
155 IOVEC_SET_STRING(iovec
[1], header_time
);
156 IOVEC_SET_STRING(iovec
[2], s
->process
);
157 IOVEC_SET_STRING(iovec
[3], header_pid
);
158 IOVEC_SET_STRING(iovec
[4], p
);
161 msghdr
.msg_iov
= iovec
;
162 msghdr
.msg_iovlen
= ELEMENTSOF(iovec
);
164 if (sendmsg(s
->server
->syslog_fd
, &msghdr
, MSG_NOSIGNAL
) < 0)
167 } else if (s
->target
== STREAM_KMSG
) {
168 IOVEC_SET_STRING(iovec
[1], s
->process
);
169 IOVEC_SET_STRING(iovec
[2], header_pid
);
170 IOVEC_SET_STRING(iovec
[3], p
);
171 IOVEC_SET_STRING(iovec
[4], (char*) "\n");
173 if (writev(s
->server
->kmsg_fd
, iovec
, ELEMENTSOF(iovec
)) < 0)
176 assert_not_reached("Unknown log target");
181 static int stream_line(Stream
*s
, char *p
, usec_t ts
) {
192 if (streq(p
, "syslog"))
193 s
->target
= STREAM_SYSLOG
;
194 else if (streq(p
, "kmsg")) {
196 if (s
->server
->kmsg_fd
>= 0 && s
->uid
== 0)
197 s
->target
= STREAM_KMSG
;
199 log_warning("/dev/kmsg logging not available.");
203 log_warning("Failed to parse log target line.");
206 s
->state
= STREAM_PRIORITY
;
209 case STREAM_PRIORITY
:
210 if ((r
= safe_atoi(p
, &s
->priority
)) < 0) {
211 log_warning("Failed to parse log priority line: %s", strerror(errno
));
215 if (s
->priority
< 0) {
216 log_warning("Log priority negative: %s", strerror(errno
));
220 s
->state
= STREAM_PROCESS
;
224 if (!(s
->process
= strdup(p
)))
227 s
->state
= STREAM_PREFIX
;
232 if ((r
= parse_boolean(p
)) < 0)
236 s
->state
= STREAM_RUNNING
;
240 return stream_log(s
, p
, ts
);
243 assert_not_reached("Unknown stream state");
246 static int stream_scan(Stream
*s
, usec_t ts
) {
254 remaining
= s
->length
;
258 if (!(newline
= memchr(p
, '\n', remaining
)))
263 if ((r
= stream_line(s
, p
, ts
)) >= 0) {
264 remaining
-= newline
-p
+1;
270 memmove(s
->buffer
, p
, remaining
);
271 s
->length
= remaining
;
277 static int stream_process(Stream
*s
, usec_t ts
) {
282 if ((l
= read(s
->fd
, s
->buffer
+s
->length
, STREAM_BUFFER
-s
->length
)) < 0) {
287 log_warning("Failed to read from stream: %s", strerror(errno
));
296 r
= stream_scan(s
, ts
);
304 static void stream_free(Stream
*s
) {
308 assert(s
->server
->n_streams
> 0);
309 s
->server
->n_streams
--;
310 LIST_REMOVE(Stream
, stream
, s
->server
->streams
, s
);
316 epoll_ctl(s
->server
->epoll_fd
, EPOLL_CTL_DEL
, s
->fd
, NULL
);
318 close_nointr_nofail(s
->fd
);
325 static int stream_new(Server
*s
, int server_fd
) {
329 socklen_t len
= sizeof(ucred
);
330 struct epoll_event ev
;
335 if ((fd
= accept4(server_fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
)) < 0)
338 if (s
->n_streams
>= STREAMS_MAX
) {
339 log_warning("Too many connections, refusing connection.");
340 close_nointr_nofail(fd
);
344 if (!socket_tcpwrap(fd
, "systemd-logger")) {
345 close_nointr_nofail(fd
);
349 if (!(stream
= new0(Stream
, 1))) {
350 close_nointr_nofail(fd
);
356 if (getsockopt(stream
->fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &len
) < 0) {
361 if (shutdown(fd
, SHUT_WR
) < 0) {
367 ev
.data
.ptr
= stream
;
369 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0) {
374 stream
->pid
= ucred
.pid
;
375 stream
->uid
= ucred
.uid
;
378 LIST_PREPEND(Stream
, stream
, s
->streams
, stream
);
388 static void server_done(Server
*s
) {
393 stream_free(s
->streams
);
395 for (i
= 0; i
< s
->n_server_fd
; i
++)
396 close_nointr_nofail(SD_LISTEN_FDS_START
+i
);
398 if (s
->syslog_fd
>= 0)
399 close_nointr_nofail(s
->syslog_fd
);
401 if (s
->epoll_fd
>= 0)
402 close_nointr_nofail(s
->epoll_fd
);
405 close_nointr_nofail(s
->kmsg_fd
);
408 static int server_init(Server
*s
, unsigned n_sockets
) {
413 struct sockaddr_un un
;
417 assert(n_sockets
> 0);
421 s
->n_server_fd
= n_sockets
;
425 if ((s
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0) {
427 log_error("Failed to create epoll object: %s", strerror(errno
));
431 for (i
= 0; i
< n_sockets
; i
++) {
432 struct epoll_event ev
;
435 fd
= SD_LISTEN_FDS_START
+i
;
437 if ((r
= sd_is_socket(fd
, AF_UNSPEC
, SOCK_STREAM
, 1)) < 0) {
438 log_error("Failed to determine file descriptor type: %s", strerror(-r
));
443 log_error("Wrong file descriptor type.");
450 ev
.data
.ptr
= UINT_TO_PTR(fd
);
451 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0) {
453 log_error("Failed to add server fd to epoll object: %s", strerror(errno
));
458 if ((s
->syslog_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0)) < 0) {
460 log_error("Failed to create log fd: %s", strerror(errno
));
465 sa
.un
.sun_family
= AF_UNIX
;
466 strncpy(sa
.un
.sun_path
, "/dev/log", sizeof(sa
.un
.sun_path
));
468 if (connect(s
->syslog_fd
, &sa
.sa
, sizeof(sa
)) < 0) {
470 log_error("Failed to connect log socket to /dev/log: %s", strerror(errno
));
474 /* /dev/kmsg logging is strictly optional */
475 if ((s
->kmsg_fd
= open("/dev/kmsg", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
)) < 0)
476 log_debug("Failed to open /dev/kmsg for logging, disabling kernel log buffer support: %s", strerror(errno
));
485 static int process_event(Server
*s
, struct epoll_event
*ev
) {
490 /* Yes, this is a bit ugly, we assume that that valid pointers
491 * are > SD_LISTEN_FDS_START+SERVER_FD_MAX. Which is certainly
492 * true on Linux (and probably most other OSes, too, since the
493 * first 4k usually are part of a seperate null pointer
494 * dereference page. */
496 if (PTR_TO_UINT(ev
->data
.ptr
) >= SD_LISTEN_FDS_START
&&
497 PTR_TO_UINT(ev
->data
.ptr
) < SD_LISTEN_FDS_START
+s
->n_server_fd
) {
499 if (ev
->events
!= EPOLLIN
) {
500 log_info("Got invalid event from epoll. (1)");
504 if ((r
= stream_new(s
, PTR_TO_UINT(ev
->data
.ptr
))) < 0) {
505 log_info("Failed to accept new connection: %s", strerror(-r
));
511 Stream
*stream
= ev
->data
.ptr
;
513 ts
= now(CLOCK_REALTIME
);
515 if (!(ev
->events
& EPOLLIN
)) {
516 log_info("Got invalid event from epoll. (2)");
521 if ((r
= stream_process(stream
, ts
)) <= 0) {
524 log_info("Got error on stream: %s", strerror(-r
));
534 int main(int argc
, char *argv
[]) {
538 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
539 log_parse_environment();
541 log_info("systemd-logger running as pid %lu", (unsigned long) getpid());
543 if ((n
= sd_listen_fds(true)) < 0) {
544 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r
));
548 if (n
<= 0 || n
> SERVER_FD_MAX
) {
549 log_error("No or too many file descriptors passed.");
553 if (server_init(&server
, (unsigned) n
) < 0)
558 "STATUS=Processing requests...");
561 struct epoll_event event
;
564 if ((k
= epoll_wait(server
.epoll_fd
,
566 server
.n_streams
<= 0 ? TIMEOUT
: -1)) < 0) {
571 log_error("epoll_wait() failed: %s", strerror(errno
));
578 if ((k
= process_event(&server
, &event
)) < 0)
585 "STATUS=Shutting down...");
587 server_done(&server
);
589 log_info("systemd-logger stopped as pid %lu", (unsigned long) getpid());