]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logger.c
units: Provide separate fsck.target for Debian
[thirdparty/systemd.git] / src / logger.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b52429d4 2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
b52429d4
LP
22#include <sys/socket.h>
23#include <sys/types.h>
24#include <assert.h>
25#include <time.h>
26#include <string.h>
27#include <stdio.h>
28#include <errno.h>
29#include <unistd.h>
30#include <sys/poll.h>
31#include <sys/epoll.h>
32#include <sys/un.h>
4901f972 33#include <fcntl.h>
b52429d4
LP
34
35#include "util.h"
36#include "log.h"
37#include "list.h"
8bfcc8ea 38#include "sd-daemon.h"
0213c3f8 39#include "tcpwrap.h"
b52429d4 40
b52429d4 41#define STREAMS_MAX 256
b52429d4
LP
42#define SERVER_FD_MAX 16
43#define TIMEOUT ((int) (10*MSEC_PER_SEC))
44
45typedef struct Stream Stream;
46
47typedef struct Server {
4901f972
LP
48 int syslog_fd;
49 int kmsg_fd;
b52429d4
LP
50 int epoll_fd;
51
52 unsigned n_server_fd;
53
54 LIST_HEAD(Stream, streams);
55 unsigned n_streams;
56} Server;
57
4f4a1dbf
LP
58typedef enum StreamTarget {
59 STREAM_SYSLOG,
60 STREAM_KMSG
61} StreamTarget;
62
b52429d4 63typedef enum StreamState {
4f4a1dbf 64 STREAM_TARGET,
b52429d4
LP
65 STREAM_PRIORITY,
66 STREAM_PROCESS,
4f4a1dbf 67 STREAM_PREFIX,
b52429d4
LP
68 STREAM_RUNNING
69} StreamState;
70
71struct Stream {
72 Server *server;
73
74 StreamState state;
75
76 int fd;
4901f972 77
9c5c00f9 78 StreamTarget target;
b52429d4
LP
79 int priority;
80 char *process;
4901f972
LP
81 pid_t pid;
82 uid_t uid;
63090775 83 gid_t gid;
b52429d4 84
4f4a1dbf
LP
85 bool prefix;
86
addab137 87 char buffer[LINE_MAX];
b52429d4
LP
88 size_t length;
89
b52429d4
LP
90 LIST_FIELDS(Stream, stream);
91};
92
871d7de4 93static int stream_log(Stream *s, char *p, usec_t ts) {
b52429d4
LP
94
95 char header_priority[16], header_time[64], header_pid[16];
b52429d4 96 struct iovec iovec[5];
4f4a1dbf 97 int priority;
b52429d4
LP
98
99 assert(s);
100 assert(p);
101
4f4a1dbf
LP
102 priority = s->priority;
103
104 if (s->prefix &&
105 p[0] == '<' &&
106 p[1] >= '0' && p[1] <= '7' &&
107 p[2] == '>') {
108
109 /* Detected priority prefix */
110 priority = LOG_MAKEPRI(LOG_FAC(priority), (p[1] - '0'));
111
112 p += 3;
113 }
114
b52429d4
LP
115 if (*p == 0)
116 return 0;
117
118 /*
4901f972
LP
119 * The format glibc uses to talk to the syslog daemon is:
120 *
121 * <priority>time process[pid]: msg
b52429d4 122 *
4901f972
LP
123 * The format the kernel uses is:
124 *
125 * <priority>msg\n
126 *
127 * We extend the latter to include the process name and pid.
b52429d4
LP
128 */
129
4901f972 130 snprintf(header_priority, sizeof(header_priority), "<%i>",
4f4a1dbf 131 s->target == STREAM_SYSLOG ? priority : LOG_PRI(priority));
b52429d4
LP
132 char_array_0(header_priority);
133
4f4a1dbf 134 if (s->target == STREAM_SYSLOG) {
4901f972
LP
135 time_t t;
136 struct tm *tm;
b52429d4 137
871d7de4 138 t = (time_t) (ts / USEC_PER_SEC);
4901f972
LP
139 if (!(tm = localtime(&t)))
140 return -EINVAL;
141
142 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
143 return -EINVAL;
144 }
b52429d4 145
bb00e604 146 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) s->pid);
b52429d4
LP
147 char_array_0(header_pid);
148
149 zero(iovec);
150 IOVEC_SET_STRING(iovec[0], header_priority);
b52429d4 151
4f4a1dbf 152 if (s->target == STREAM_SYSLOG) {
16801e90 153 struct msghdr msghdr;
63090775
LP
154 union {
155 struct cmsghdr cmsghdr;
156 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
157 } control;
158 struct ucred *ucred;
159
160 zero(control);
161 control.cmsghdr.cmsg_level = SOL_SOCKET;
162 control.cmsghdr.cmsg_type = SCM_CREDENTIALS;
163 control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
164
165 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
166 ucred->pid = s->pid;
167 ucred->uid = s->uid;
168 ucred->gid = s->gid;
16801e90 169
4901f972
LP
170 IOVEC_SET_STRING(iovec[1], header_time);
171 IOVEC_SET_STRING(iovec[2], s->process);
172 IOVEC_SET_STRING(iovec[3], header_pid);
173 IOVEC_SET_STRING(iovec[4], p);
b52429d4 174
4901f972
LP
175 zero(msghdr);
176 msghdr.msg_iov = iovec;
177 msghdr.msg_iovlen = ELEMENTSOF(iovec);
63090775
LP
178 msghdr.msg_control = &control;
179 msghdr.msg_controllen = control.cmsghdr.cmsg_len;
4901f972
LP
180
181 if (sendmsg(s->server->syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
182 return -errno;
183
4f4a1dbf 184 } else if (s->target == STREAM_KMSG) {
4901f972
LP
185 IOVEC_SET_STRING(iovec[1], s->process);
186 IOVEC_SET_STRING(iovec[2], header_pid);
187 IOVEC_SET_STRING(iovec[3], p);
47be870b 188 IOVEC_SET_STRING(iovec[4], (char*) "\n");
4901f972
LP
189
190 if (writev(s->server->kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
191 return -errno;
192 } else
193 assert_not_reached("Unknown log target");
b52429d4
LP
194
195 return 0;
196}
197
871d7de4 198static int stream_line(Stream *s, char *p, usec_t ts) {
b52429d4
LP
199 int r;
200
201 assert(s);
202 assert(p);
203
204 p = strstrip(p);
205
206 switch (s->state) {
207
4f4a1dbf 208 case STREAM_TARGET:
4901f972 209 if (streq(p, "syslog"))
4f4a1dbf 210 s->target = STREAM_SYSLOG;
4901f972
LP
211 else if (streq(p, "kmsg")) {
212
213 if (s->server->kmsg_fd >= 0 && s->uid == 0)
4f4a1dbf 214 s->target = STREAM_KMSG;
4901f972
LP
215 else {
216 log_warning("/dev/kmsg logging not available.");
217 return -EPERM;
218 }
219 } else {
220 log_warning("Failed to parse log target line.");
221 return -EBADMSG;
222 }
223 s->state = STREAM_PRIORITY;
224 return 0;
225
b52429d4 226 case STREAM_PRIORITY:
4901f972 227 if ((r = safe_atoi(p, &s->priority)) < 0) {
63090775 228 log_warning("Failed to parse log priority line: %m");
b52429d4 229 return r;
4901f972 230 }
b52429d4 231
4901f972 232 if (s->priority < 0) {
63090775 233 log_warning("Log priority negative: %m");
b52429d4 234 return -ERANGE;
4901f972 235 }
b52429d4
LP
236
237 s->state = STREAM_PROCESS;
238 return 0;
239
240 case STREAM_PROCESS:
241 if (!(s->process = strdup(p)))
242 return -ENOMEM;
243
4f4a1dbf
LP
244 s->state = STREAM_PREFIX;
245 return 0;
246
247 case STREAM_PREFIX:
248
249 if ((r = parse_boolean(p)) < 0)
250 return r;
251
252 s->prefix = r;
b52429d4
LP
253 s->state = STREAM_RUNNING;
254 return 0;
255
256 case STREAM_RUNNING:
871d7de4 257 return stream_log(s, p, ts);
b52429d4
LP
258 }
259
260 assert_not_reached("Unknown stream state");
261}
262
871d7de4 263static int stream_scan(Stream *s, usec_t ts) {
b52429d4
LP
264 char *p;
265 size_t remaining;
266 int r = 0;
267
268 assert(s);
269
270 p = s->buffer;
271 remaining = s->length;
272 for (;;) {
273 char *newline;
274
275 if (!(newline = memchr(p, '\n', remaining)))
276 break;
277
278 *newline = 0;
279
871d7de4 280 if ((r = stream_line(s, p, ts)) >= 0) {
b52429d4
LP
281 remaining -= newline-p+1;
282 p = newline+1;
283 }
284 }
285
286 if (p > s->buffer) {
287 memmove(s->buffer, p, remaining);
288 s->length = remaining;
289 }
290
291 return r;
292}
293
871d7de4 294static int stream_process(Stream *s, usec_t ts) {
b52429d4
LP
295 ssize_t l;
296 int r;
297 assert(s);
298
addab137 299 if ((l = read(s->fd, s->buffer+s->length, LINE_MAX-s->length)) < 0) {
b52429d4
LP
300
301 if (errno == EAGAIN)
302 return 0;
303
63090775 304 log_warning("Failed to read from stream: %m");
b52429d4
LP
305 return -1;
306 }
307
308
309 if (l == 0)
310 return 0;
311
312 s->length += l;
871d7de4 313 r = stream_scan(s, ts);
b52429d4
LP
314
315 if (r < 0)
316 return r;
317
318 return 1;
319}
320
321static void stream_free(Stream *s) {
322 assert(s);
323
324 if (s->server) {
325 assert(s->server->n_streams > 0);
326 s->server->n_streams--;
327 LIST_REMOVE(Stream, stream, s->server->streams, s);
328
329 }
330
331 if (s->fd >= 0) {
332 if (s->server)
333 epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
334
a16e1123 335 close_nointr_nofail(s->fd);
b52429d4
LP
336 }
337
338 free(s->process);
339 free(s);
340}
341
342static int stream_new(Server *s, int server_fd) {
343 Stream *stream;
344 int fd;
345 struct ucred ucred;
346 socklen_t len = sizeof(ucred);
347 struct epoll_event ev;
348 int r;
349
350 assert(s);
351
352 if ((fd = accept4(server_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC)) < 0)
353 return -errno;
354
355 if (s->n_streams >= STREAMS_MAX) {
356 log_warning("Too many connections, refusing connection.");
a16e1123 357 close_nointr_nofail(fd);
b52429d4
LP
358 return 0;
359 }
360
0213c3f8
LP
361 if (!socket_tcpwrap(fd, "systemd-logger")) {
362 close_nointr_nofail(fd);
363 return 0;
364 }
365
b52429d4 366 if (!(stream = new0(Stream, 1))) {
a16e1123 367 close_nointr_nofail(fd);
b52429d4
LP
368 return -ENOMEM;
369 }
370
371 stream->fd = fd;
372
373 if (getsockopt(stream->fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
374 r = -errno;
375 goto fail;
376 }
377
378 if (shutdown(fd, SHUT_WR) < 0) {
379 r = -errno;
380 goto fail;
381 }
382
383 zero(ev);
384 ev.data.ptr = stream;
f94ea366 385 ev.events = EPOLLIN;
b52429d4
LP
386 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
387 r = -errno;
388 goto fail;
389 }
390
391 stream->pid = ucred.pid;
4901f972 392 stream->uid = ucred.uid;
63090775 393 stream->gid = ucred.gid;
b52429d4
LP
394
395 stream->server = s;
396 LIST_PREPEND(Stream, stream, s->streams, stream);
397 s->n_streams ++;
398
399 return 0;
400
401fail:
402 stream_free(stream);
403 return r;
404}
405
b52429d4
LP
406static void server_done(Server *s) {
407 unsigned i;
408 assert(s);
409
410 while (s->streams)
411 stream_free(s->streams);
412
413 for (i = 0; i < s->n_server_fd; i++)
8bfcc8ea 414 close_nointr_nofail(SD_LISTEN_FDS_START+i);
b52429d4 415
4901f972 416 if (s->syslog_fd >= 0)
a16e1123 417 close_nointr_nofail(s->syslog_fd);
b52429d4
LP
418
419 if (s->epoll_fd >= 0)
a16e1123 420 close_nointr_nofail(s->epoll_fd);
4901f972
LP
421
422 if (s->kmsg_fd >= 0)
a16e1123 423 close_nointr_nofail(s->kmsg_fd);
b52429d4
LP
424}
425
426static int server_init(Server *s, unsigned n_sockets) {
427 int r;
428 unsigned i;
429 union {
430 struct sockaddr sa;
431 struct sockaddr_un un;
432 } sa;
433
434 assert(s);
435 assert(n_sockets > 0);
436
437 zero(*s);
438
439 s->n_server_fd = n_sockets;
4901f972
LP
440 s->syslog_fd = -1;
441 s->kmsg_fd = -1;
b52429d4
LP
442
443 if ((s->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
444 r = -errno;
63090775 445 log_error("Failed to create epoll object: %m");
b52429d4
LP
446 goto fail;
447 }
448
449 for (i = 0; i < n_sockets; i++) {
450 struct epoll_event ev;
7c394faa
LP
451 int fd;
452
453 fd = SD_LISTEN_FDS_START+i;
454
88ce42f6 455 if ((r = sd_is_socket(fd, AF_UNSPEC, SOCK_STREAM, 1)) < 0) {
7c394faa
LP
456 log_error("Failed to determine file descriptor type: %s", strerror(-r));
457 goto fail;
458 }
459
460 if (!r) {
461 log_error("Wrong file descriptor type.");
462 r = -EINVAL;
463 goto fail;
464 }
b52429d4 465
63090775
LP
466 /* We use ev.data.ptr instead of ev.data.fd here,
467 * since on 64bit archs fd is 32bit while a pointer is
468 * 64bit. To make sure we can easily distuingish fd
469 * values and pointer values we want to make sure to
470 * write the full field unconditionally. */
471
b52429d4 472 zero(ev);
f94ea366 473 ev.events = EPOLLIN;
63090775 474 ev.data.ptr = INT_TO_PTR(fd);
7c394faa 475 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
b52429d4 476 r = -errno;
63090775 477 log_error("Failed to add server fd to epoll object: %m");
b52429d4
LP
478 goto fail;
479 }
480 }
481
4901f972 482 if ((s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
b52429d4 483 r = -errno;
63090775 484 log_error("Failed to create log fd: %m");
b52429d4
LP
485 goto fail;
486 }
487
488 zero(sa);
489 sa.un.sun_family = AF_UNIX;
490 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
491
4901f972 492 if (connect(s->syslog_fd, &sa.sa, sizeof(sa)) < 0) {
b52429d4 493 r = -errno;
63090775 494 log_error("Failed to connect log socket to /dev/log: %m");
b52429d4
LP
495 goto fail;
496 }
497
4901f972
LP
498 /* /dev/kmsg logging is strictly optional */
499 if ((s->kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
63090775 500 log_warning("Failed to open /dev/kmsg for logging, disabling kernel log buffer support: %m");
4901f972 501
b52429d4
LP
502 return 0;
503
504fail:
505 server_done(s);
506 return r;
507}
508
509static int process_event(Server *s, struct epoll_event *ev) {
510 int r;
511
512 assert(s);
513
514 /* Yes, this is a bit ugly, we assume that that valid pointers
8bfcc8ea 515 * are > SD_LISTEN_FDS_START+SERVER_FD_MAX. Which is certainly
b52429d4
LP
516 * true on Linux (and probably most other OSes, too, since the
517 * first 4k usually are part of a seperate null pointer
518 * dereference page. */
519
63090775
LP
520 if (PTR_TO_INT(ev->data.ptr) >= SD_LISTEN_FDS_START &&
521 PTR_TO_INT(ev->data.ptr) < SD_LISTEN_FDS_START+(int)s->n_server_fd) {
b52429d4 522
f94ea366 523 if (ev->events != EPOLLIN) {
b52429d4
LP
524 log_info("Got invalid event from epoll. (1)");
525 return -EIO;
526 }
527
63090775 528 if ((r = stream_new(s, PTR_TO_INT(ev->data.ptr))) < 0) {
b52429d4
LP
529 log_info("Failed to accept new connection: %s", strerror(-r));
530 return r;
531 }
532
533 } else {
871d7de4 534 usec_t ts;
b52429d4
LP
535 Stream *stream = ev->data.ptr;
536
871d7de4 537 ts = now(CLOCK_REALTIME);
b52429d4 538
f94ea366 539 if (!(ev->events & EPOLLIN)) {
8bfcc8ea 540 log_info("Got invalid event from epoll. (2)");
b52429d4
LP
541 stream_free(stream);
542 return 0;
543 }
544
871d7de4 545 if ((r = stream_process(stream, ts)) <= 0) {
b52429d4
LP
546
547 if (r < 0)
548 log_info("Got error on stream: %s", strerror(-r));
549
550 stream_free(stream);
551 return 0;
552 }
553 }
554
555 return 0;
556}
557
558int main(int argc, char *argv[]) {
559 Server server;
22f4096c 560 int r = EXIT_FAILURE, n;
8bfcc8ea 561
0ca3f374
LP
562 if (getppid() != 1) {
563 log_error("This program should be invoked by init only.");
22f4096c 564 return EXIT_FAILURE;
0ca3f374
LP
565 }
566
567 if (argc > 1) {
568 log_error("This program does not take arguments.");
22f4096c 569 return EXIT_FAILURE;
0ca3f374
LP
570 }
571
8bfcc8ea
LP
572 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
573 log_parse_environment();
2396fb04 574 log_open();
b52429d4 575
8bfcc8ea
LP
576 if ((n = sd_listen_fds(true)) < 0) {
577 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r));
22f4096c 578 return EXIT_FAILURE;
8bfcc8ea 579 }
b52429d4 580
8bfcc8ea
LP
581 if (n <= 0 || n > SERVER_FD_MAX) {
582 log_error("No or too many file descriptors passed.");
22f4096c 583 return EXIT_FAILURE;
8bfcc8ea
LP
584 }
585
586 if (server_init(&server, (unsigned) n) < 0)
22f4096c 587 return EXIT_FAILURE;
b52429d4 588
cd6d0a45
LP
589 log_debug("systemd-logger running as pid %lu", (unsigned long) getpid());
590
8c47c732
LP
591 sd_notify(false,
592 "READY=1\n"
593 "STATUS=Processing requests...");
594
b52429d4
LP
595 for (;;) {
596 struct epoll_event event;
47be870b 597 int k;
b52429d4 598
47be870b 599 if ((k = epoll_wait(server.epoll_fd,
b52429d4
LP
600 &event, 1,
601 server.n_streams <= 0 ? TIMEOUT : -1)) < 0) {
602
603 if (errno == EINTR)
604 continue;
605
63090775 606 log_error("epoll_wait() failed: %m");
b52429d4
LP
607 goto fail;
608 }
609
47be870b 610 if (k <= 0)
b52429d4
LP
611 break;
612
e364ad06 613 if (process_event(&server, &event) < 0)
b52429d4
LP
614 goto fail;
615 }
cd6d0a45 616
22f4096c 617 r = EXIT_SUCCESS;
b52429d4 618
addab137 619 log_debug("systemd-logger stopped as pid %lu", (unsigned long) getpid());
cd6d0a45 620
b52429d4 621fail:
8c47c732
LP
622 sd_notify(false,
623 "STATUS=Shutting down...");
624
b52429d4
LP
625 server_done(&server);
626
b52429d4
LP
627 return r;
628}