]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote.c
journal: defer journal closes on rotate
[thirdparty/systemd.git] / src / journal-remote / journal-remote.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Zbigniew Jędrzejewski-Szmek
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/prctl.h>
27 #include <sys/socket.h>
28 #include <unistd.h>
29
30 #ifdef HAVE_GNUTLS
31 #include <gnutls/gnutls.h>
32 #endif
33
34 #include "sd-daemon.h"
35
36 #include "alloc-util.h"
37 #include "conf-parser.h"
38 #include "def.h"
39 #include "escape.h"
40 #include "fd-util.h"
41 #include "fileio.h"
42 #include "journal-file.h"
43 #include "journal-remote-write.h"
44 #include "journal-remote.h"
45 #include "journald-native.h"
46 #include "macro.h"
47 #include "parse-util.h"
48 #include "signal-util.h"
49 #include "socket-util.h"
50 #include "stat-util.h"
51 #include "stdio-util.h"
52 #include "string-table.h"
53 #include "string-util.h"
54 #include "strv.h"
55
56 #define REMOTE_JOURNAL_PATH "/var/log/journal/remote"
57
58 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-remote.pem"
59 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-remote.pem"
60 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
61
62 static char* arg_url = NULL;
63 static char* arg_getter = NULL;
64 static char* arg_listen_raw = NULL;
65 static char* arg_listen_http = NULL;
66 static char* arg_listen_https = NULL;
67 static char** arg_files = NULL;
68 static int arg_compress = true;
69 static int arg_seal = false;
70 static int http_socket = -1, https_socket = -1;
71 static char** arg_gnutls_log = NULL;
72
73 static JournalWriteSplitMode arg_split_mode = JOURNAL_WRITE_SPLIT_HOST;
74 static char* arg_output = NULL;
75
76 static char *arg_key = NULL;
77 static char *arg_cert = NULL;
78 static char *arg_trust = NULL;
79 static bool arg_trust_all = false;
80
81 /**********************************************************************
82 **********************************************************************
83 **********************************************************************/
84
85 static int spawn_child(const char* child, char** argv) {
86 int fd[2];
87 pid_t parent_pid, child_pid;
88 int r;
89
90 if (pipe(fd) < 0)
91 return log_error_errno(errno, "Failed to create pager pipe: %m");
92
93 parent_pid = getpid();
94
95 child_pid = fork();
96 if (child_pid < 0) {
97 r = log_error_errno(errno, "Failed to fork: %m");
98 safe_close_pair(fd);
99 return r;
100 }
101
102 /* In the child */
103 if (child_pid == 0) {
104
105 (void) reset_all_signal_handlers();
106 (void) reset_signal_mask();
107
108 r = dup2(fd[1], STDOUT_FILENO);
109 if (r < 0) {
110 log_error_errno(errno, "Failed to dup pipe to stdout: %m");
111 _exit(EXIT_FAILURE);
112 }
113
114 safe_close_pair(fd);
115
116 /* Make sure the child goes away when the parent dies */
117 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
118 _exit(EXIT_FAILURE);
119
120 /* Check whether our parent died before we were able
121 * to set the death signal */
122 if (getppid() != parent_pid)
123 _exit(EXIT_SUCCESS);
124
125 execvp(child, argv);
126 log_error_errno(errno, "Failed to exec child %s: %m", child);
127 _exit(EXIT_FAILURE);
128 }
129
130 r = close(fd[1]);
131 if (r < 0)
132 log_warning_errno(errno, "Failed to close write end of pipe: %m");
133
134 return fd[0];
135 }
136
137 static int spawn_curl(const char* url) {
138 char **argv = STRV_MAKE("curl",
139 "-HAccept: application/vnd.fdo.journal",
140 "--silent",
141 "--show-error",
142 url);
143 int r;
144
145 r = spawn_child("curl", argv);
146 if (r < 0)
147 log_error_errno(r, "Failed to spawn curl: %m");
148 return r;
149 }
150
151 static int spawn_getter(const char *getter) {
152 int r;
153 _cleanup_strv_free_ char **words = NULL;
154
155 assert(getter);
156 r = strv_split_extract(&words, getter, WHITESPACE, EXTRACT_QUOTES);
157 if (r < 0)
158 return log_error_errno(r, "Failed to split getter option: %m");
159
160 r = spawn_child(words[0], words);
161 if (r < 0)
162 log_error_errno(r, "Failed to spawn getter %s: %m", getter);
163
164 return r;
165 }
166
167 #define filename_escape(s) xescape((s), "/ ")
168
169 static int open_output(Writer *w, const char* host) {
170 _cleanup_free_ char *_output = NULL;
171 const char *output;
172 int r;
173
174 switch (arg_split_mode) {
175 case JOURNAL_WRITE_SPLIT_NONE:
176 output = arg_output ?: REMOTE_JOURNAL_PATH "/remote.journal";
177 break;
178
179 case JOURNAL_WRITE_SPLIT_HOST: {
180 _cleanup_free_ char *name;
181
182 assert(host);
183
184 name = filename_escape(host);
185 if (!name)
186 return log_oom();
187
188 r = asprintf(&_output, "%s/remote-%s.journal",
189 arg_output ?: REMOTE_JOURNAL_PATH,
190 name);
191 if (r < 0)
192 return log_oom();
193
194 output = _output;
195 break;
196 }
197
198 default:
199 assert_not_reached("what?");
200 }
201
202 r = journal_file_open_reliably(output,
203 O_RDWR|O_CREAT, 0640,
204 arg_compress, arg_seal,
205 &w->metrics,
206 w->mmap, NULL,
207 NULL, &w->journal);
208 if (r < 0)
209 log_error_errno(r, "Failed to open output journal %s: %m",
210 output);
211 else
212 log_debug("Opened output file %s", w->journal->path);
213 return r;
214 }
215
216 /**********************************************************************
217 **********************************************************************
218 **********************************************************************/
219
220 static int init_writer_hashmap(RemoteServer *s) {
221 static const struct hash_ops *hash_ops[] = {
222 [JOURNAL_WRITE_SPLIT_NONE] = NULL,
223 [JOURNAL_WRITE_SPLIT_HOST] = &string_hash_ops,
224 };
225
226 assert(arg_split_mode >= 0 && arg_split_mode < (int) ELEMENTSOF(hash_ops));
227
228 s->writers = hashmap_new(hash_ops[arg_split_mode]);
229 if (!s->writers)
230 return log_oom();
231
232 return 0;
233 }
234
235 static int get_writer(RemoteServer *s, const char *host,
236 Writer **writer) {
237 const void *key;
238 _cleanup_writer_unref_ Writer *w = NULL;
239 int r;
240
241 switch(arg_split_mode) {
242 case JOURNAL_WRITE_SPLIT_NONE:
243 key = "one and only";
244 break;
245
246 case JOURNAL_WRITE_SPLIT_HOST:
247 assert(host);
248 key = host;
249 break;
250
251 default:
252 assert_not_reached("what split mode?");
253 }
254
255 w = hashmap_get(s->writers, key);
256 if (w)
257 writer_ref(w);
258 else {
259 w = writer_new(s);
260 if (!w)
261 return log_oom();
262
263 if (arg_split_mode == JOURNAL_WRITE_SPLIT_HOST) {
264 w->hashmap_key = strdup(key);
265 if (!w->hashmap_key)
266 return log_oom();
267 }
268
269 r = open_output(w, host);
270 if (r < 0)
271 return r;
272
273 r = hashmap_put(s->writers, w->hashmap_key ?: key, w);
274 if (r < 0)
275 return r;
276 }
277
278 *writer = w;
279 w = NULL;
280 return 0;
281 }
282
283 /**********************************************************************
284 **********************************************************************
285 **********************************************************************/
286
287 /* This should go away as soon as µhttpd allows state to be passed around. */
288 static RemoteServer *server;
289
290 static int dispatch_raw_source_event(sd_event_source *event,
291 int fd,
292 uint32_t revents,
293 void *userdata);
294 static int dispatch_raw_source_until_block(sd_event_source *event,
295 void *userdata);
296 static int dispatch_blocking_source_event(sd_event_source *event,
297 void *userdata);
298 static int dispatch_raw_connection_event(sd_event_source *event,
299 int fd,
300 uint32_t revents,
301 void *userdata);
302 static int dispatch_http_event(sd_event_source *event,
303 int fd,
304 uint32_t revents,
305 void *userdata);
306
307 static int get_source_for_fd(RemoteServer *s,
308 int fd, char *name, RemoteSource **source) {
309 Writer *writer;
310 int r;
311
312 /* This takes ownership of name, but only on success. */
313
314 assert(fd >= 0);
315 assert(source);
316
317 if (!GREEDY_REALLOC0(s->sources, s->sources_size, fd + 1))
318 return log_oom();
319
320 r = get_writer(s, name, &writer);
321 if (r < 0)
322 return log_warning_errno(r, "Failed to get writer for source %s: %m",
323 name);
324
325 if (s->sources[fd] == NULL) {
326 s->sources[fd] = source_new(fd, false, name, writer);
327 if (!s->sources[fd]) {
328 writer_unref(writer);
329 return log_oom();
330 }
331
332 s->active++;
333 }
334
335 *source = s->sources[fd];
336 return 0;
337 }
338
339 static int remove_source(RemoteServer *s, int fd) {
340 RemoteSource *source;
341
342 assert(s);
343 assert(fd >= 0 && fd < (ssize_t) s->sources_size);
344
345 source = s->sources[fd];
346 if (source) {
347 /* this closes fd too */
348 source_free(source);
349 s->sources[fd] = NULL;
350 s->active--;
351 }
352
353 return 0;
354 }
355
356 static int add_source(RemoteServer *s, int fd, char* name, bool own_name) {
357
358 RemoteSource *source = NULL;
359 int r;
360
361 /* This takes ownership of name, even on failure, if own_name is true. */
362
363 assert(s);
364 assert(fd >= 0);
365 assert(name);
366
367 if (!own_name) {
368 name = strdup(name);
369 if (!name)
370 return log_oom();
371 }
372
373 r = get_source_for_fd(s, fd, name, &source);
374 if (r < 0) {
375 log_error_errno(r, "Failed to create source for fd:%d (%s): %m",
376 fd, name);
377 free(name);
378 return r;
379 }
380
381 r = sd_event_add_io(s->events, &source->event,
382 fd, EPOLLIN|EPOLLRDHUP|EPOLLPRI,
383 dispatch_raw_source_event, source);
384 if (r == 0) {
385 /* Add additional source for buffer processing. It will be
386 * enabled later. */
387 r = sd_event_add_defer(s->events, &source->buffer_event,
388 dispatch_raw_source_until_block, source);
389 if (r == 0)
390 sd_event_source_set_enabled(source->buffer_event, SD_EVENT_OFF);
391 } else if (r == -EPERM) {
392 log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd, name);
393 r = sd_event_add_defer(s->events, &source->event,
394 dispatch_blocking_source_event, source);
395 if (r == 0)
396 sd_event_source_set_enabled(source->event, SD_EVENT_ON);
397 }
398 if (r < 0) {
399 log_error_errno(r, "Failed to register event source for fd:%d: %m",
400 fd);
401 goto error;
402 }
403
404 r = sd_event_source_set_description(source->event, name);
405 if (r < 0) {
406 log_error_errno(r, "Failed to set source name for fd:%d: %m", fd);
407 goto error;
408 }
409
410 return 1; /* work to do */
411
412 error:
413 remove_source(s, fd);
414 return r;
415 }
416
417 static int add_raw_socket(RemoteServer *s, int fd) {
418 int r;
419 _cleanup_close_ int fd_ = fd;
420 char name[sizeof("raw-socket-")-1 + DECIMAL_STR_MAX(int) + 1];
421
422 assert(fd >= 0);
423
424 r = sd_event_add_io(s->events, &s->listen_event,
425 fd, EPOLLIN,
426 dispatch_raw_connection_event, s);
427 if (r < 0)
428 return r;
429
430 xsprintf(name, "raw-socket-%d", fd);
431
432 r = sd_event_source_set_description(s->listen_event, name);
433 if (r < 0)
434 return r;
435
436 fd_ = -1;
437 s->active ++;
438 return 0;
439 }
440
441 static int setup_raw_socket(RemoteServer *s, const char *address) {
442 int fd;
443
444 fd = make_socket_fd(LOG_INFO, address, SOCK_STREAM, SOCK_CLOEXEC);
445 if (fd < 0)
446 return fd;
447
448 return add_raw_socket(s, fd);
449 }
450
451 /**********************************************************************
452 **********************************************************************
453 **********************************************************************/
454
455 static int request_meta(void **connection_cls, int fd, char *hostname) {
456 RemoteSource *source;
457 Writer *writer;
458 int r;
459
460 assert(connection_cls);
461 if (*connection_cls)
462 return 0;
463
464 r = get_writer(server, hostname, &writer);
465 if (r < 0)
466 return log_warning_errno(r, "Failed to get writer for source %s: %m",
467 hostname);
468
469 source = source_new(fd, true, hostname, writer);
470 if (!source) {
471 writer_unref(writer);
472 return log_oom();
473 }
474
475 log_debug("Added RemoteSource as connection metadata %p", source);
476
477 *connection_cls = source;
478 return 0;
479 }
480
481 static void request_meta_free(void *cls,
482 struct MHD_Connection *connection,
483 void **connection_cls,
484 enum MHD_RequestTerminationCode toe) {
485 RemoteSource *s;
486
487 assert(connection_cls);
488 s = *connection_cls;
489
490 if (s) {
491 log_debug("Cleaning up connection metadata %p", s);
492 source_free(s);
493 *connection_cls = NULL;
494 }
495 }
496
497 static int process_http_upload(
498 struct MHD_Connection *connection,
499 const char *upload_data,
500 size_t *upload_data_size,
501 RemoteSource *source) {
502
503 bool finished = false;
504 size_t remaining;
505 int r;
506
507 assert(source);
508
509 log_trace("%s: connection %p, %zu bytes",
510 __func__, connection, *upload_data_size);
511
512 if (*upload_data_size) {
513 log_trace("Received %zu bytes", *upload_data_size);
514
515 r = push_data(source, upload_data, *upload_data_size);
516 if (r < 0)
517 return mhd_respond_oom(connection);
518
519 *upload_data_size = 0;
520 } else
521 finished = true;
522
523 for (;;) {
524 r = process_source(source, arg_compress, arg_seal);
525 if (r == -EAGAIN)
526 break;
527 else if (r < 0) {
528 log_warning("Failed to process data for connection %p", connection);
529 if (r == -E2BIG)
530 return mhd_respondf(connection,
531 MHD_HTTP_REQUEST_ENTITY_TOO_LARGE,
532 "Entry is too large, maximum is %u bytes.\n",
533 DATA_SIZE_MAX);
534 else
535 return mhd_respondf(connection,
536 MHD_HTTP_UNPROCESSABLE_ENTITY,
537 "Processing failed: %s.", strerror(-r));
538 }
539 }
540
541 if (!finished)
542 return MHD_YES;
543
544 /* The upload is finished */
545
546 remaining = source_non_empty(source);
547 if (remaining > 0) {
548 log_warning("Premature EOFbyte. %zu bytes lost.", remaining);
549 return mhd_respondf(connection, MHD_HTTP_EXPECTATION_FAILED,
550 "Premature EOF. %zu bytes of trailing data not processed.",
551 remaining);
552 }
553
554 return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.\n");
555 };
556
557 static int request_handler(
558 void *cls,
559 struct MHD_Connection *connection,
560 const char *url,
561 const char *method,
562 const char *version,
563 const char *upload_data,
564 size_t *upload_data_size,
565 void **connection_cls) {
566
567 const char *header;
568 int r, code, fd;
569 _cleanup_free_ char *hostname = NULL;
570
571 assert(connection);
572 assert(connection_cls);
573 assert(url);
574 assert(method);
575
576 log_trace("Handling a connection %s %s %s", method, url, version);
577
578 if (*connection_cls)
579 return process_http_upload(connection,
580 upload_data, upload_data_size,
581 *connection_cls);
582
583 if (!streq(method, "POST"))
584 return mhd_respond(connection, MHD_HTTP_NOT_ACCEPTABLE,
585 "Unsupported method.\n");
586
587 if (!streq(url, "/upload"))
588 return mhd_respond(connection, MHD_HTTP_NOT_FOUND,
589 "Not found.\n");
590
591 header = MHD_lookup_connection_value(connection,
592 MHD_HEADER_KIND, "Content-Type");
593 if (!header || !streq(header, "application/vnd.fdo.journal"))
594 return mhd_respond(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE,
595 "Content-Type: application/vnd.fdo.journal"
596 " is required.\n");
597
598 {
599 const union MHD_ConnectionInfo *ci;
600
601 ci = MHD_get_connection_info(connection,
602 MHD_CONNECTION_INFO_CONNECTION_FD);
603 if (!ci) {
604 log_error("MHD_get_connection_info failed: cannot get remote fd");
605 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
606 "Cannot check remote address");
607 }
608
609 fd = ci->connect_fd;
610 assert(fd >= 0);
611 }
612
613 if (server->check_trust) {
614 r = check_permissions(connection, &code, &hostname);
615 if (r < 0)
616 return code;
617 } else {
618 r = getpeername_pretty(fd, false, &hostname);
619 if (r < 0)
620 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
621 "Cannot check remote hostname");
622 }
623
624 assert(hostname);
625
626 r = request_meta(connection_cls, fd, hostname);
627 if (r == -ENOMEM)
628 return respond_oom(connection);
629 else if (r < 0)
630 return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR,
631 strerror(-r));
632
633 hostname = NULL;
634 return MHD_YES;
635 }
636
637 static int setup_microhttpd_server(RemoteServer *s,
638 int fd,
639 const char *key,
640 const char *cert,
641 const char *trust) {
642 struct MHD_OptionItem opts[] = {
643 { MHD_OPTION_NOTIFY_COMPLETED, (intptr_t) request_meta_free},
644 { MHD_OPTION_EXTERNAL_LOGGER, (intptr_t) microhttpd_logger},
645 { MHD_OPTION_LISTEN_SOCKET, fd},
646 { MHD_OPTION_CONNECTION_MEMORY_LIMIT, 128*1024},
647 { MHD_OPTION_END},
648 { MHD_OPTION_END},
649 { MHD_OPTION_END},
650 { MHD_OPTION_END}};
651 int opts_pos = 4;
652 int flags =
653 MHD_USE_DEBUG |
654 MHD_USE_DUAL_STACK |
655 MHD_USE_EPOLL_LINUX_ONLY |
656 MHD_USE_PEDANTIC_CHECKS |
657 MHD_USE_PIPE_FOR_SHUTDOWN;
658
659 const union MHD_DaemonInfo *info;
660 int r, epoll_fd;
661 MHDDaemonWrapper *d;
662
663 assert(fd >= 0);
664
665 r = fd_nonblock(fd, true);
666 if (r < 0)
667 return log_error_errno(r, "Failed to make fd:%d nonblocking: %m", fd);
668
669 if (key) {
670 assert(cert);
671
672 opts[opts_pos++] = (struct MHD_OptionItem)
673 {MHD_OPTION_HTTPS_MEM_KEY, 0, (char*) key};
674 opts[opts_pos++] = (struct MHD_OptionItem)
675 {MHD_OPTION_HTTPS_MEM_CERT, 0, (char*) cert};
676
677 flags |= MHD_USE_SSL;
678
679 if (trust)
680 opts[opts_pos++] = (struct MHD_OptionItem)
681 {MHD_OPTION_HTTPS_MEM_TRUST, 0, (char*) trust};
682 }
683
684 d = new(MHDDaemonWrapper, 1);
685 if (!d)
686 return log_oom();
687
688 d->fd = (uint64_t) fd;
689
690 d->daemon = MHD_start_daemon(flags, 0,
691 NULL, NULL,
692 request_handler, NULL,
693 MHD_OPTION_ARRAY, opts,
694 MHD_OPTION_END);
695 if (!d->daemon) {
696 log_error("Failed to start µhttp daemon");
697 r = -EINVAL;
698 goto error;
699 }
700
701 log_debug("Started MHD %s daemon on fd:%d (wrapper @ %p)",
702 key ? "HTTPS" : "HTTP", fd, d);
703
704
705 info = MHD_get_daemon_info(d->daemon, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
706 if (!info) {
707 log_error("µhttp returned NULL daemon info");
708 r = -EOPNOTSUPP;
709 goto error;
710 }
711
712 epoll_fd = info->listen_fd;
713 if (epoll_fd < 0) {
714 log_error("µhttp epoll fd is invalid");
715 r = -EUCLEAN;
716 goto error;
717 }
718
719 r = sd_event_add_io(s->events, &d->event,
720 epoll_fd, EPOLLIN,
721 dispatch_http_event, d);
722 if (r < 0) {
723 log_error_errno(r, "Failed to add event callback: %m");
724 goto error;
725 }
726
727 r = sd_event_source_set_description(d->event, "epoll-fd");
728 if (r < 0) {
729 log_error_errno(r, "Failed to set source name: %m");
730 goto error;
731 }
732
733 r = hashmap_ensure_allocated(&s->daemons, &uint64_hash_ops);
734 if (r < 0) {
735 log_oom();
736 goto error;
737 }
738
739 r = hashmap_put(s->daemons, &d->fd, d);
740 if (r < 0) {
741 log_error_errno(r, "Failed to add daemon to hashmap: %m");
742 goto error;
743 }
744
745 s->active ++;
746 return 0;
747
748 error:
749 MHD_stop_daemon(d->daemon);
750 free(d->daemon);
751 free(d);
752 return r;
753 }
754
755 static int setup_microhttpd_socket(RemoteServer *s,
756 const char *address,
757 const char *key,
758 const char *cert,
759 const char *trust) {
760 int fd;
761
762 fd = make_socket_fd(LOG_DEBUG, address, SOCK_STREAM, SOCK_CLOEXEC);
763 if (fd < 0)
764 return fd;
765
766 return setup_microhttpd_server(s, fd, key, cert, trust);
767 }
768
769 static int dispatch_http_event(sd_event_source *event,
770 int fd,
771 uint32_t revents,
772 void *userdata) {
773 MHDDaemonWrapper *d = userdata;
774 int r;
775
776 assert(d);
777
778 r = MHD_run(d->daemon);
779 if (r == MHD_NO) {
780 log_error("MHD_run failed!");
781 // XXX: unregister daemon
782 return -EINVAL;
783 }
784
785 return 1; /* work to do */
786 }
787
788 /**********************************************************************
789 **********************************************************************
790 **********************************************************************/
791
792 static int setup_signals(RemoteServer *s) {
793 int r;
794
795 assert(s);
796
797 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, -1) >= 0);
798
799 r = sd_event_add_signal(s->events, &s->sigterm_event, SIGTERM, NULL, s);
800 if (r < 0)
801 return r;
802
803 r = sd_event_add_signal(s->events, &s->sigint_event, SIGINT, NULL, s);
804 if (r < 0)
805 return r;
806
807 return 0;
808 }
809
810 static int negative_fd(const char *spec) {
811 /* Return a non-positive number as its inverse, -EINVAL otherwise. */
812
813 int fd, r;
814
815 r = safe_atoi(spec, &fd);
816 if (r < 0)
817 return r;
818
819 if (fd > 0)
820 return -EINVAL;
821 else
822 return -fd;
823 }
824
825 static int remoteserver_init(RemoteServer *s,
826 const char* key,
827 const char* cert,
828 const char* trust) {
829 int r, n, fd;
830 char **file;
831
832 assert(s);
833
834 if ((arg_listen_raw || arg_listen_http) && trust) {
835 log_error("Option --trust makes all non-HTTPS connections untrusted.");
836 return -EINVAL;
837 }
838
839 r = sd_event_default(&s->events);
840 if (r < 0)
841 return log_error_errno(r, "Failed to allocate event loop: %m");
842
843 setup_signals(s);
844
845 assert(server == NULL);
846 server = s;
847
848 r = init_writer_hashmap(s);
849 if (r < 0)
850 return r;
851
852 n = sd_listen_fds(true);
853 if (n < 0)
854 return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
855 else
856 log_debug("Received %d descriptors", n);
857
858 if (MAX(http_socket, https_socket) >= SD_LISTEN_FDS_START + n) {
859 log_error("Received fewer sockets than expected");
860 return -EBADFD;
861 }
862
863 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
864 if (sd_is_socket(fd, AF_UNSPEC, 0, true)) {
865 log_debug("Received a listening socket (fd:%d)", fd);
866
867 if (fd == http_socket)
868 r = setup_microhttpd_server(s, fd, NULL, NULL, NULL);
869 else if (fd == https_socket)
870 r = setup_microhttpd_server(s, fd, key, cert, trust);
871 else
872 r = add_raw_socket(s, fd);
873 } else if (sd_is_socket(fd, AF_UNSPEC, 0, false)) {
874 char *hostname;
875
876 r = getpeername_pretty(fd, false, &hostname);
877 if (r < 0)
878 return log_error_errno(r, "Failed to retrieve remote name: %m");
879
880 log_debug("Received a connection socket (fd:%d) from %s", fd, hostname);
881
882 r = add_source(s, fd, hostname, true);
883 } else {
884 log_error("Unknown socket passed on fd:%d", fd);
885
886 return -EINVAL;
887 }
888
889 if (r < 0)
890 return log_error_errno(r, "Failed to register socket (fd:%d): %m",
891 fd);
892 }
893
894 if (arg_getter) {
895 log_info("Spawning getter %s...", arg_getter);
896 fd = spawn_getter(arg_getter);
897 if (fd < 0)
898 return fd;
899
900 r = add_source(s, fd, (char*) arg_output, false);
901 if (r < 0)
902 return r;
903 }
904
905 if (arg_url) {
906 const char *url;
907 char *hostname, *p;
908
909 if (!strstr(arg_url, "/entries")) {
910 if (endswith(arg_url, "/"))
911 url = strjoina(arg_url, "entries");
912 else
913 url = strjoina(arg_url, "/entries");
914 }
915 else
916 url = strdupa(arg_url);
917
918 log_info("Spawning curl %s...", url);
919 fd = spawn_curl(url);
920 if (fd < 0)
921 return fd;
922
923 hostname =
924 startswith(arg_url, "https://") ?:
925 startswith(arg_url, "http://") ?:
926 arg_url;
927
928 hostname = strdupa(hostname);
929 if ((p = strchr(hostname, '/')))
930 *p = '\0';
931 if ((p = strchr(hostname, ':')))
932 *p = '\0';
933
934 r = add_source(s, fd, hostname, false);
935 if (r < 0)
936 return r;
937 }
938
939 if (arg_listen_raw) {
940 log_debug("Listening on a socket...");
941 r = setup_raw_socket(s, arg_listen_raw);
942 if (r < 0)
943 return r;
944 }
945
946 if (arg_listen_http) {
947 r = setup_microhttpd_socket(s, arg_listen_http, NULL, NULL, NULL);
948 if (r < 0)
949 return r;
950 }
951
952 if (arg_listen_https) {
953 r = setup_microhttpd_socket(s, arg_listen_https, key, cert, trust);
954 if (r < 0)
955 return r;
956 }
957
958 STRV_FOREACH(file, arg_files) {
959 const char *output_name;
960
961 if (streq(*file, "-")) {
962 log_debug("Using standard input as source.");
963
964 fd = STDIN_FILENO;
965 output_name = "stdin";
966 } else {
967 log_debug("Reading file %s...", *file);
968
969 fd = open(*file, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
970 if (fd < 0)
971 return log_error_errno(errno, "Failed to open %s: %m", *file);
972 output_name = *file;
973 }
974
975 r = add_source(s, fd, (char*) output_name, false);
976 if (r < 0)
977 return r;
978 }
979
980 if (s->active == 0) {
981 log_error("Zero sources specified");
982 return -EINVAL;
983 }
984
985 if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE) {
986 /* In this case we know what the writer will be
987 called, so we can create it and verify that we can
988 create output as expected. */
989 r = get_writer(s, NULL, &s->_single_writer);
990 if (r < 0)
991 return r;
992 }
993
994 return 0;
995 }
996
997 static void server_destroy(RemoteServer *s) {
998 size_t i;
999 MHDDaemonWrapper *d;
1000
1001 while ((d = hashmap_steal_first(s->daemons))) {
1002 MHD_stop_daemon(d->daemon);
1003 sd_event_source_unref(d->event);
1004 free(d);
1005 }
1006
1007 hashmap_free(s->daemons);
1008
1009 assert(s->sources_size == 0 || s->sources);
1010 for (i = 0; i < s->sources_size; i++)
1011 remove_source(s, i);
1012 free(s->sources);
1013
1014 writer_unref(s->_single_writer);
1015 hashmap_free(s->writers);
1016
1017 sd_event_source_unref(s->sigterm_event);
1018 sd_event_source_unref(s->sigint_event);
1019 sd_event_source_unref(s->listen_event);
1020 sd_event_unref(s->events);
1021
1022 /* fds that we're listening on remain open... */
1023 }
1024
1025 /**********************************************************************
1026 **********************************************************************
1027 **********************************************************************/
1028
1029 static int handle_raw_source(sd_event_source *event,
1030 int fd,
1031 uint32_t revents,
1032 RemoteServer *s) {
1033
1034 RemoteSource *source;
1035 int r;
1036
1037 /* Returns 1 if there might be more data pending,
1038 * 0 if data is currently exhausted, negative on error.
1039 */
1040
1041 assert(fd >= 0 && fd < (ssize_t) s->sources_size);
1042 source = s->sources[fd];
1043 assert(source->fd == fd);
1044
1045 r = process_source(source, arg_compress, arg_seal);
1046 if (source->state == STATE_EOF) {
1047 size_t remaining;
1048
1049 log_debug("EOF reached with source fd:%d (%s)",
1050 source->fd, source->name);
1051
1052 remaining = source_non_empty(source);
1053 if (remaining > 0)
1054 log_notice("Premature EOF. %zu bytes lost.", remaining);
1055 remove_source(s, source->fd);
1056 log_debug("%zu active sources remaining", s->active);
1057 return 0;
1058 } else if (r == -E2BIG) {
1059 log_notice_errno(E2BIG, "Entry too big, skipped");
1060 return 1;
1061 } else if (r == -EAGAIN) {
1062 return 0;
1063 } else if (r < 0) {
1064 log_debug_errno(r, "Closing connection: %m");
1065 remove_source(server, fd);
1066 return 0;
1067 } else
1068 return 1;
1069 }
1070
1071 static int dispatch_raw_source_until_block(sd_event_source *event,
1072 void *userdata) {
1073 RemoteSource *source = userdata;
1074 int r;
1075
1076 /* Make sure event stays around even if source is destroyed */
1077 sd_event_source_ref(event);
1078
1079 r = handle_raw_source(event, source->fd, EPOLLIN, server);
1080 if (r != 1)
1081 /* No more data for now */
1082 sd_event_source_set_enabled(event, SD_EVENT_OFF);
1083
1084 sd_event_source_unref(event);
1085
1086 return r;
1087 }
1088
1089 static int dispatch_raw_source_event(sd_event_source *event,
1090 int fd,
1091 uint32_t revents,
1092 void *userdata) {
1093 RemoteSource *source = userdata;
1094 int r;
1095
1096 assert(source->event);
1097 assert(source->buffer_event);
1098
1099 r = handle_raw_source(event, fd, EPOLLIN, server);
1100 if (r == 1)
1101 /* Might have more data. We need to rerun the handler
1102 * until we are sure the buffer is exhausted. */
1103 sd_event_source_set_enabled(source->buffer_event, SD_EVENT_ON);
1104
1105 return r;
1106 }
1107
1108 static int dispatch_blocking_source_event(sd_event_source *event,
1109 void *userdata) {
1110 RemoteSource *source = userdata;
1111
1112 return handle_raw_source(event, source->fd, EPOLLIN, server);
1113 }
1114
1115 static int accept_connection(const char* type, int fd,
1116 SocketAddress *addr, char **hostname) {
1117 int fd2, r;
1118
1119 log_debug("Accepting new %s connection on fd:%d", type, fd);
1120 fd2 = accept4(fd, &addr->sockaddr.sa, &addr->size, SOCK_NONBLOCK|SOCK_CLOEXEC);
1121 if (fd2 < 0)
1122 return log_error_errno(errno, "accept() on fd:%d failed: %m", fd);
1123
1124 switch(socket_address_family(addr)) {
1125 case AF_INET:
1126 case AF_INET6: {
1127 _cleanup_free_ char *a = NULL;
1128 char *b;
1129
1130 r = socket_address_print(addr, &a);
1131 if (r < 0) {
1132 log_error_errno(r, "socket_address_print(): %m");
1133 close(fd2);
1134 return r;
1135 }
1136
1137 r = socknameinfo_pretty(&addr->sockaddr, addr->size, &b);
1138 if (r < 0) {
1139 log_error_errno(r, "Resolving hostname failed: %m");
1140 close(fd2);
1141 return r;
1142 }
1143
1144 log_debug("Accepted %s %s connection from %s",
1145 type,
1146 socket_address_family(addr) == AF_INET ? "IP" : "IPv6",
1147 a);
1148
1149 *hostname = b;
1150
1151 return fd2;
1152 };
1153 default:
1154 log_error("Rejected %s connection with unsupported family %d",
1155 type, socket_address_family(addr));
1156 close(fd2);
1157
1158 return -EINVAL;
1159 }
1160 }
1161
1162 static int dispatch_raw_connection_event(sd_event_source *event,
1163 int fd,
1164 uint32_t revents,
1165 void *userdata) {
1166 RemoteServer *s = userdata;
1167 int fd2;
1168 SocketAddress addr = {
1169 .size = sizeof(union sockaddr_union),
1170 .type = SOCK_STREAM,
1171 };
1172 char *hostname = NULL;
1173
1174 fd2 = accept_connection("raw", fd, &addr, &hostname);
1175 if (fd2 < 0)
1176 return fd2;
1177
1178 return add_source(s, fd2, hostname, true);
1179 }
1180
1181 /**********************************************************************
1182 **********************************************************************
1183 **********************************************************************/
1184
1185 static const char* const journal_write_split_mode_table[_JOURNAL_WRITE_SPLIT_MAX] = {
1186 [JOURNAL_WRITE_SPLIT_NONE] = "none",
1187 [JOURNAL_WRITE_SPLIT_HOST] = "host",
1188 };
1189
1190 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(journal_write_split_mode, JournalWriteSplitMode);
1191 static DEFINE_CONFIG_PARSE_ENUM(config_parse_write_split_mode,
1192 journal_write_split_mode,
1193 JournalWriteSplitMode,
1194 "Failed to parse split mode setting");
1195
1196 static int parse_config(void) {
1197 const ConfigTableItem items[] = {
1198 { "Remote", "Seal", config_parse_bool, 0, &arg_seal },
1199 { "Remote", "SplitMode", config_parse_write_split_mode, 0, &arg_split_mode },
1200 { "Remote", "ServerKeyFile", config_parse_path, 0, &arg_key },
1201 { "Remote", "ServerCertificateFile", config_parse_path, 0, &arg_cert },
1202 { "Remote", "TrustedCertificateFile", config_parse_path, 0, &arg_trust },
1203 {}};
1204
1205 return config_parse_many(PKGSYSCONFDIR "/journal-remote.conf",
1206 CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"),
1207 "Remote\0", config_item_table_lookup, items,
1208 false, NULL);
1209 }
1210
1211 static void help(void) {
1212 printf("%s [OPTIONS...] {FILE|-}...\n\n"
1213 "Write external journal events to journal file(s).\n\n"
1214 " -h --help Show this help\n"
1215 " --version Show package version\n"
1216 " --url=URL Read events from systemd-journal-gatewayd at URL\n"
1217 " --getter=COMMAND Read events from the output of COMMAND\n"
1218 " --listen-raw=ADDR Listen for connections at ADDR\n"
1219 " --listen-http=ADDR Listen for HTTP connections at ADDR\n"
1220 " --listen-https=ADDR Listen for HTTPS connections at ADDR\n"
1221 " -o --output=FILE|DIR Write output to FILE or DIR/external-*.journal\n"
1222 " --compress[=BOOL] XZ-compress the output journal (default: yes)\n"
1223 " --seal[=BOOL] Use event sealing (default: no)\n"
1224 " --key=FILENAME SSL key in PEM format (default:\n"
1225 " \"" PRIV_KEY_FILE "\")\n"
1226 " --cert=FILENAME SSL certificate in PEM format (default:\n"
1227 " \"" CERT_FILE "\")\n"
1228 " --trust=FILENAME|all SSL CA certificate or disable checking (default:\n"
1229 " \"" TRUST_FILE "\")\n"
1230 " --gnutls-log=CATEGORY...\n"
1231 " Specify a list of gnutls logging categories\n"
1232 " --split-mode=none|host How many output files to create\n"
1233 "\n"
1234 "Note: file descriptors from sd_listen_fds() will be consumed, too.\n"
1235 , program_invocation_short_name);
1236 }
1237
1238 static int parse_argv(int argc, char *argv[]) {
1239 enum {
1240 ARG_VERSION = 0x100,
1241 ARG_URL,
1242 ARG_LISTEN_RAW,
1243 ARG_LISTEN_HTTP,
1244 ARG_LISTEN_HTTPS,
1245 ARG_GETTER,
1246 ARG_SPLIT_MODE,
1247 ARG_COMPRESS,
1248 ARG_SEAL,
1249 ARG_KEY,
1250 ARG_CERT,
1251 ARG_TRUST,
1252 ARG_GNUTLS_LOG,
1253 };
1254
1255 static const struct option options[] = {
1256 { "help", no_argument, NULL, 'h' },
1257 { "version", no_argument, NULL, ARG_VERSION },
1258 { "url", required_argument, NULL, ARG_URL },
1259 { "getter", required_argument, NULL, ARG_GETTER },
1260 { "listen-raw", required_argument, NULL, ARG_LISTEN_RAW },
1261 { "listen-http", required_argument, NULL, ARG_LISTEN_HTTP },
1262 { "listen-https", required_argument, NULL, ARG_LISTEN_HTTPS },
1263 { "output", required_argument, NULL, 'o' },
1264 { "split-mode", required_argument, NULL, ARG_SPLIT_MODE },
1265 { "compress", optional_argument, NULL, ARG_COMPRESS },
1266 { "seal", optional_argument, NULL, ARG_SEAL },
1267 { "key", required_argument, NULL, ARG_KEY },
1268 { "cert", required_argument, NULL, ARG_CERT },
1269 { "trust", required_argument, NULL, ARG_TRUST },
1270 { "gnutls-log", required_argument, NULL, ARG_GNUTLS_LOG },
1271 {}
1272 };
1273
1274 int c, r;
1275 bool type_a, type_b;
1276
1277 assert(argc >= 0);
1278 assert(argv);
1279
1280 while ((c = getopt_long(argc, argv, "ho:", options, NULL)) >= 0)
1281 switch(c) {
1282 case 'h':
1283 help();
1284 return 0 /* done */;
1285
1286 case ARG_VERSION:
1287 return version();
1288
1289 case ARG_URL:
1290 if (arg_url) {
1291 log_error("cannot currently set more than one --url");
1292 return -EINVAL;
1293 }
1294
1295 arg_url = optarg;
1296 break;
1297
1298 case ARG_GETTER:
1299 if (arg_getter) {
1300 log_error("cannot currently use --getter more than once");
1301 return -EINVAL;
1302 }
1303
1304 arg_getter = optarg;
1305 break;
1306
1307 case ARG_LISTEN_RAW:
1308 if (arg_listen_raw) {
1309 log_error("cannot currently use --listen-raw more than once");
1310 return -EINVAL;
1311 }
1312
1313 arg_listen_raw = optarg;
1314 break;
1315
1316 case ARG_LISTEN_HTTP:
1317 if (arg_listen_http || http_socket >= 0) {
1318 log_error("cannot currently use --listen-http more than once");
1319 return -EINVAL;
1320 }
1321
1322 r = negative_fd(optarg);
1323 if (r >= 0)
1324 http_socket = r;
1325 else
1326 arg_listen_http = optarg;
1327 break;
1328
1329 case ARG_LISTEN_HTTPS:
1330 if (arg_listen_https || https_socket >= 0) {
1331 log_error("cannot currently use --listen-https more than once");
1332 return -EINVAL;
1333 }
1334
1335 r = negative_fd(optarg);
1336 if (r >= 0)
1337 https_socket = r;
1338 else
1339 arg_listen_https = optarg;
1340
1341 break;
1342
1343 case ARG_KEY:
1344 if (arg_key) {
1345 log_error("Key file specified twice");
1346 return -EINVAL;
1347 }
1348
1349 arg_key = strdup(optarg);
1350 if (!arg_key)
1351 return log_oom();
1352
1353 break;
1354
1355 case ARG_CERT:
1356 if (arg_cert) {
1357 log_error("Certificate file specified twice");
1358 return -EINVAL;
1359 }
1360
1361 arg_cert = strdup(optarg);
1362 if (!arg_cert)
1363 return log_oom();
1364
1365 break;
1366
1367 case ARG_TRUST:
1368 if (arg_trust || arg_trust_all) {
1369 log_error("Confusing trusted CA configuration");
1370 return -EINVAL;
1371 }
1372
1373 if (streq(optarg, "all"))
1374 arg_trust_all = true;
1375 else {
1376 #ifdef HAVE_GNUTLS
1377 arg_trust = strdup(optarg);
1378 if (!arg_trust)
1379 return log_oom();
1380 #else
1381 log_error("Option --trust is not available.");
1382 return -EINVAL;
1383 #endif
1384 }
1385
1386 break;
1387
1388 case 'o':
1389 if (arg_output) {
1390 log_error("cannot use --output/-o more than once");
1391 return -EINVAL;
1392 }
1393
1394 arg_output = optarg;
1395 break;
1396
1397 case ARG_SPLIT_MODE:
1398 arg_split_mode = journal_write_split_mode_from_string(optarg);
1399 if (arg_split_mode == _JOURNAL_WRITE_SPLIT_INVALID) {
1400 log_error("Invalid split mode: %s", optarg);
1401 return -EINVAL;
1402 }
1403 break;
1404
1405 case ARG_COMPRESS:
1406 if (optarg) {
1407 r = parse_boolean(optarg);
1408 if (r < 0) {
1409 log_error("Failed to parse --compress= parameter.");
1410 return -EINVAL;
1411 }
1412
1413 arg_compress = !!r;
1414 } else
1415 arg_compress = true;
1416
1417 break;
1418
1419 case ARG_SEAL:
1420 if (optarg) {
1421 r = parse_boolean(optarg);
1422 if (r < 0) {
1423 log_error("Failed to parse --seal= parameter.");
1424 return -EINVAL;
1425 }
1426
1427 arg_seal = !!r;
1428 } else
1429 arg_seal = true;
1430
1431 break;
1432
1433 case ARG_GNUTLS_LOG: {
1434 #ifdef HAVE_GNUTLS
1435 const char* p = optarg;
1436 for (;;) {
1437 _cleanup_free_ char *word = NULL;
1438
1439 r = extract_first_word(&p, &word, ",", 0);
1440 if (r < 0)
1441 return log_error_errno(r, "Failed to parse --gnutls-log= argument: %m");
1442
1443 if (r == 0)
1444 break;
1445
1446 if (strv_push(&arg_gnutls_log, word) < 0)
1447 return log_oom();
1448
1449 word = NULL;
1450 }
1451 break;
1452 #else
1453 log_error("Option --gnutls-log is not available.");
1454 return -EINVAL;
1455 #endif
1456 }
1457
1458 case '?':
1459 return -EINVAL;
1460
1461 default:
1462 assert_not_reached("Unknown option code.");
1463 }
1464
1465 if (optind < argc)
1466 arg_files = argv + optind;
1467
1468 type_a = arg_getter || !strv_isempty(arg_files);
1469 type_b = arg_url
1470 || arg_listen_raw
1471 || arg_listen_http || arg_listen_https
1472 || sd_listen_fds(false) > 0;
1473 if (type_a && type_b) {
1474 log_error("Cannot use file input or --getter with "
1475 "--arg-listen-... or socket activation.");
1476 return -EINVAL;
1477 }
1478 if (type_a) {
1479 if (!arg_output) {
1480 log_error("Option --output must be specified with file input or --getter.");
1481 return -EINVAL;
1482 }
1483
1484 arg_split_mode = JOURNAL_WRITE_SPLIT_NONE;
1485 }
1486
1487 if (arg_split_mode == JOURNAL_WRITE_SPLIT_NONE
1488 && arg_output && is_dir(arg_output, true) > 0) {
1489 log_error("For SplitMode=none, output must be a file.");
1490 return -EINVAL;
1491 }
1492
1493 if (arg_split_mode == JOURNAL_WRITE_SPLIT_HOST
1494 && arg_output && is_dir(arg_output, true) <= 0) {
1495 log_error("For SplitMode=host, output must be a directory.");
1496 return -EINVAL;
1497 }
1498
1499 log_debug("Full config: SplitMode=%s Key=%s Cert=%s Trust=%s",
1500 journal_write_split_mode_to_string(arg_split_mode),
1501 strna(arg_key),
1502 strna(arg_cert),
1503 strna(arg_trust));
1504
1505 return 1 /* work to do */;
1506 }
1507
1508 static int load_certificates(char **key, char **cert, char **trust) {
1509 int r;
1510
1511 r = read_full_file(arg_key ?: PRIV_KEY_FILE, key, NULL);
1512 if (r < 0)
1513 return log_error_errno(r, "Failed to read key from file '%s': %m",
1514 arg_key ?: PRIV_KEY_FILE);
1515
1516 r = read_full_file(arg_cert ?: CERT_FILE, cert, NULL);
1517 if (r < 0)
1518 return log_error_errno(r, "Failed to read certificate from file '%s': %m",
1519 arg_cert ?: CERT_FILE);
1520
1521 if (arg_trust_all)
1522 log_info("Certificate checking disabled.");
1523 else {
1524 r = read_full_file(arg_trust ?: TRUST_FILE, trust, NULL);
1525 if (r < 0)
1526 return log_error_errno(r, "Failed to read CA certificate file '%s': %m",
1527 arg_trust ?: TRUST_FILE);
1528 }
1529
1530 return 0;
1531 }
1532
1533 int main(int argc, char **argv) {
1534 RemoteServer s = {};
1535 int r;
1536 _cleanup_free_ char *key = NULL, *cert = NULL, *trust = NULL;
1537
1538 log_show_color(true);
1539 log_parse_environment();
1540
1541 r = parse_config();
1542 if (r < 0)
1543 return EXIT_FAILURE;
1544
1545 r = parse_argv(argc, argv);
1546 if (r <= 0)
1547 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1548
1549
1550 if (arg_listen_http || arg_listen_https) {
1551 r = setup_gnutls_logger(arg_gnutls_log);
1552 if (r < 0)
1553 return EXIT_FAILURE;
1554 }
1555
1556 if (arg_listen_https || https_socket >= 0)
1557 if (load_certificates(&key, &cert, &trust) < 0)
1558 return EXIT_FAILURE;
1559
1560 if (remoteserver_init(&s, key, cert, trust) < 0)
1561 return EXIT_FAILURE;
1562
1563 r = sd_event_set_watchdog(s.events, true);
1564 if (r < 0)
1565 log_error_errno(r, "Failed to enable watchdog: %m");
1566 else
1567 log_debug("Watchdog is %s.", r > 0 ? "enabled" : "disabled");
1568
1569 log_debug("%s running as pid "PID_FMT,
1570 program_invocation_short_name, getpid());
1571 sd_notify(false,
1572 "READY=1\n"
1573 "STATUS=Processing requests...");
1574
1575 while (s.active) {
1576 r = sd_event_get_state(s.events);
1577 if (r < 0)
1578 break;
1579 if (r == SD_EVENT_FINISHED)
1580 break;
1581
1582 r = sd_event_run(s.events, -1);
1583 if (r < 0) {
1584 log_error_errno(r, "Failed to run event loop: %m");
1585 break;
1586 }
1587 }
1588
1589 sd_notifyf(false,
1590 "STOPPING=1\n"
1591 "STATUS=Shutting down after writing %" PRIu64 " entries...", s.event_count);
1592 log_info("Finishing after writing %" PRIu64 " entries", s.event_count);
1593
1594 server_destroy(&s);
1595
1596 free(arg_key);
1597 free(arg_cert);
1598 free(arg_trust);
1599
1600 return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1601 }