]>
git.ipfire.org Git - people/ms/systemd.git/blob - logger.c
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>
39 #define STREAM_BUFFER 2048
40 #define STREAMS_MAX 256
41 #define SERVER_FD_START 3
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
{
65 typedef enum LogTarget
{
83 char buffer
[STREAM_BUFFER
];
86 LIST_FIELDS(Stream
, stream
);
89 #define IOVEC_SET_STRING(iovec, s) \
91 (iovec).iov_base = s; \
92 (iovec).iov_len = strlen(s); \
95 static int stream_log(Stream
*s
, char *p
, usec_t timestamp
) {
97 char header_priority
[16], header_time
[64], header_pid
[16];
99 struct iovec iovec
[5];
108 * The format glibc uses to talk to the syslog daemon is:
110 * <priority>time process[pid]: msg
112 * The format the kernel uses is:
116 * We extend the latter to include the process name and pid.
119 snprintf(header_priority
, sizeof(header_priority
), "<%i>",
120 s
->target
== LOG_TARGET_SYSLOG
? s
->priority
: LOG_PRI(s
->priority
));
121 char_array_0(header_priority
);
123 if (s
->target
== LOG_TARGET_SYSLOG
) {
127 t
= (time_t) (timestamp
/ USEC_PER_SEC
);
128 if (!(tm
= localtime(&t
)))
131 if (strftime(header_time
, sizeof(header_time
), "%h %e %T ", tm
) <= 0)
135 snprintf(header_pid
, sizeof(header_pid
), "[%llu]: ", (unsigned long long) s
->pid
);
136 char_array_0(header_pid
);
139 IOVEC_SET_STRING(iovec
[0], header_priority
);
141 if (s
->target
== LOG_TARGET_SYSLOG
) {
142 IOVEC_SET_STRING(iovec
[1], header_time
);
143 IOVEC_SET_STRING(iovec
[2], s
->process
);
144 IOVEC_SET_STRING(iovec
[3], header_pid
);
145 IOVEC_SET_STRING(iovec
[4], p
);
148 msghdr
.msg_iov
= iovec
;
149 msghdr
.msg_iovlen
= ELEMENTSOF(iovec
);
151 if (sendmsg(s
->server
->syslog_fd
, &msghdr
, MSG_NOSIGNAL
) < 0)
154 } else if (s
->target
== LOG_TARGET_KMSG
) {
155 IOVEC_SET_STRING(iovec
[1], s
->process
);
156 IOVEC_SET_STRING(iovec
[2], header_pid
);
157 IOVEC_SET_STRING(iovec
[3], p
);
158 IOVEC_SET_STRING(iovec
[4], (char*) "\n");
160 if (writev(s
->server
->kmsg_fd
, iovec
, ELEMENTSOF(iovec
)) < 0)
163 assert_not_reached("Unknown log target");
168 static int stream_line(Stream
*s
, char *p
, usec_t timestamp
) {
178 case STREAM_LOG_TARGET
:
179 if (streq(p
, "syslog"))
180 s
->target
= LOG_TARGET_SYSLOG
;
181 else if (streq(p
, "kmsg")) {
183 if (s
->server
->kmsg_fd
>= 0 && s
->uid
== 0)
184 s
->target
= LOG_TARGET_KMSG
;
186 log_warning("/dev/kmsg logging not available.");
190 log_warning("Failed to parse log target line.");
193 s
->state
= STREAM_PRIORITY
;
196 case STREAM_PRIORITY
:
197 if ((r
= safe_atoi(p
, &s
->priority
)) < 0) {
198 log_warning("Failed to parse log priority line: %s", strerror(errno
));
202 if (s
->priority
< 0) {
203 log_warning("Log priority negative: %s", strerror(errno
));
207 s
->state
= STREAM_PROCESS
;
211 if (!(s
->process
= strdup(p
)))
214 s
->state
= STREAM_RUNNING
;
218 return stream_log(s
, p
, timestamp
);
221 assert_not_reached("Unknown stream state");
224 static int stream_scan(Stream
*s
, usec_t timestamp
) {
232 remaining
= s
->length
;
236 if (!(newline
= memchr(p
, '\n', remaining
)))
241 if ((r
= stream_line(s
, p
, timestamp
)) >= 0) {
242 remaining
-= newline
-p
+1;
248 memmove(s
->buffer
, p
, remaining
);
249 s
->length
= remaining
;
255 static int stream_process(Stream
*s
, usec_t timestamp
) {
260 if ((l
= read(s
->fd
, s
->buffer
+s
->length
, STREAM_BUFFER
-s
->length
)) < 0) {
265 log_warning("Failed to read from stream: %s", strerror(errno
));
274 r
= stream_scan(s
, timestamp
);
282 static void stream_free(Stream
*s
) {
286 assert(s
->server
->n_streams
> 0);
287 s
->server
->n_streams
--;
288 LIST_REMOVE(Stream
, stream
, s
->server
->streams
, s
);
294 epoll_ctl(s
->server
->epoll_fd
, EPOLL_CTL_DEL
, s
->fd
, NULL
);
296 assert_se(close_nointr(s
->fd
) == 0);
303 static int stream_new(Server
*s
, int server_fd
) {
307 socklen_t len
= sizeof(ucred
);
308 struct epoll_event ev
;
313 if ((fd
= accept4(server_fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
)) < 0)
316 if (s
->n_streams
>= STREAMS_MAX
) {
317 log_warning("Too many connections, refusing connection.");
318 assert_se(close_nointr(fd
) == 0);
322 if (!(stream
= new0(Stream
, 1))) {
323 assert_se(close_nointr(fd
) == 0);
329 if (getsockopt(stream
->fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &len
) < 0) {
334 if (shutdown(fd
, SHUT_WR
) < 0) {
340 ev
.data
.ptr
= stream
;
342 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0) {
347 stream
->pid
= ucred
.pid
;
348 stream
->uid
= ucred
.uid
;
351 LIST_PREPEND(Stream
, stream
, s
->streams
, stream
);
361 static int verify_environment(unsigned *n_sockets
) {
362 unsigned long long pid
;
367 assert_se(n_sockets
);
369 if (!(e
= getenv("LISTEN_PID"))) {
370 log_error("Missing $LISTEN_PID environment variable.");
374 if ((r
= safe_atollu(e
, &pid
)) < 0) {
375 log_error("Failed to parse $LISTEN_PID: %s", strerror(-r
));
379 if (pid
!= (unsigned long long) getpid()) {
380 log_error("Socket nor for me.");
384 if (!(e
= getenv("LISTEN_FDS"))) {
385 log_error("Missing $LISTEN_FDS environment variable.");
389 if ((r
= safe_atou(e
, &ns
)) < 0) {
390 log_error("Failed to parse $LISTEN_FDS: %s", strerror(-r
));
394 if (ns
<= 0 || ns
> SERVER_FD_MAX
) {
395 log_error("Wrong number of file descriptors passed: %s", e
);
404 static void server_done(Server
*s
) {
409 stream_free(s
->streams
);
411 for (i
= 0; i
< s
->n_server_fd
; i
++)
412 assert_se(close_nointr(SERVER_FD_START
+i
) == 0);
414 if (s
->syslog_fd
>= 0)
415 assert_se(close_nointr(s
->syslog_fd
) == 0);
417 if (s
->epoll_fd
>= 0)
418 assert_se(close_nointr(s
->epoll_fd
) == 0);
421 assert_se(close_nointr(s
->kmsg_fd
) == 0);
424 static int server_init(Server
*s
, unsigned n_sockets
) {
429 struct sockaddr_un un
;
433 assert(n_sockets
> 0);
437 s
->n_server_fd
= n_sockets
;
441 if ((s
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0) {
443 log_error("Failed to create epoll object: %s", strerror(errno
));
447 for (i
= 0; i
< n_sockets
; i
++) {
448 struct epoll_event ev
;
452 ev
.data
.ptr
= UINT_TO_PTR(SERVER_FD_START
+i
);
453 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, SERVER_FD_START
+i
, &ev
) < 0) {
455 log_error("Failed to add server fd to epoll object: %s", strerror(errno
));
460 if ((s
->syslog_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0)) < 0) {
462 log_error("Failed to create log fd: %s", strerror(errno
));
467 sa
.un
.sun_family
= AF_UNIX
;
468 strncpy(sa
.un
.sun_path
, "/dev/log", sizeof(sa
.un
.sun_path
));
470 if (connect(s
->syslog_fd
, &sa
.sa
, sizeof(sa
)) < 0) {
472 log_error("Failed to connect log socket to /dev/log: %s", strerror(errno
));
476 /* /dev/kmsg logging is strictly optional */
477 if ((s
->kmsg_fd
= open("/dev/kmsg", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
)) < 0)
478 log_debug("Failed to open /dev/kmsg for logging, disabling kernel log buffer support: %s", strerror(errno
));
487 static int process_event(Server
*s
, struct epoll_event
*ev
) {
492 /* Yes, this is a bit ugly, we assume that that valid pointers
493 * are > SERVER_FD_START+SERVER_FD_MAX. Which is certainly
494 * true on Linux (and probably most other OSes, too, since the
495 * first 4k usually are part of a seperate null pointer
496 * dereference page. */
498 if (PTR_TO_UINT(ev
->data
.ptr
) >= SERVER_FD_START
&&
499 PTR_TO_UINT(ev
->data
.ptr
) < SERVER_FD_START
+s
->n_server_fd
) {
501 if (ev
->events
!= EPOLLIN
) {
502 log_info("Got invalid event from epoll. (1)");
506 if ((r
= stream_new(s
, PTR_TO_UINT(ev
->data
.ptr
))) < 0) {
507 log_info("Failed to accept new connection: %s", strerror(-r
));
513 Stream
*stream
= ev
->data
.ptr
;
515 timestamp
= now(CLOCK_REALTIME
);
517 if (!(ev
->events
& EPOLLIN
)) {
518 log_info("Got invalid event from epoll. (3)");
523 if ((r
= stream_process(stream
, timestamp
)) <= 0) {
526 log_info("Got error on stream: %s", strerror(-r
));
536 int main(int argc
, char *argv
[]) {
541 log_info("systemd-logger running as pid %llu", (unsigned long long) getpid());
543 if (verify_environment(&n
) < 0)
546 if (server_init(&server
, n
) < 0)
550 struct epoll_event event
;
553 if ((k
= epoll_wait(server
.epoll_fd
,
555 server
.n_streams
<= 0 ? TIMEOUT
: -1)) < 0) {
560 log_error("epoll_wait() failed: %s", strerror(errno
));
567 if ((r
= process_event(&server
, &event
)) < 0)
573 server_done(&server
);
575 log_info("systemd-logger stopped as pid %llu", (unsigned long long) getpid());