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