]>
Commit | Line | Data |
---|---|---|
fdfccdbc ZJS |
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> | |
3f6fd1ba | 22 | #include <getopt.h> |
fdfccdbc ZJS |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/prctl.h> | |
27 | #include <sys/socket.h> | |
fdfccdbc | 28 | #include <unistd.h> |
3f6fd1ba LP |
29 | |
30 | #ifdef HAVE_GNUTLS | |
31 | #include <gnutls/gnutls.h> | |
32 | #endif | |
fdfccdbc ZJS |
33 | |
34 | #include "sd-daemon.h" | |
3f6fd1ba | 35 | |
b5efdb8a | 36 | #include "alloc-util.h" |
3f6fd1ba | 37 | #include "conf-parser.h" |
a0f29c76 | 38 | #include "def.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" | |
8fcde012 | 50 | #include "stat-util.h" |
15a5e950 | 51 | #include "stdio-util.h" |
8b43440b | 52 | #include "string-table.h" |
07630cea | 53 | #include "string-util.h" |
fdfccdbc | 54 | #include "strv.h" |
fdfccdbc | 55 | |
8201af08 | 56 | #define REMOTE_JOURNAL_PATH "/var/log/journal/remote" |
fdfccdbc | 57 | |
799a8f39 ZJS |
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" | |
ad95fd1d | 61 | |
fdfccdbc ZJS |
62 | static char* arg_url = NULL; |
63 | static char* arg_getter = NULL; | |
fdfccdbc | 64 | static char* arg_listen_raw = NULL; |
cc64d017 ZJS |
65 | static char* arg_listen_http = NULL; |
66 | static char* arg_listen_https = NULL; | |
8a8d55f2 | 67 | static char** arg_files = NULL; |
fdfccdbc ZJS |
68 | static int arg_compress = true; |
69 | static int arg_seal = false; | |
8a8d55f2 | 70 | static int http_socket = -1, https_socket = -1; |
5937a4d4 | 71 | static char** arg_gnutls_log = NULL; |
fdfccdbc | 72 | |
9ff48d09 | 73 | static JournalWriteSplitMode arg_split_mode = JOURNAL_WRITE_SPLIT_HOST; |
8201af08 ZJS |
74 | static char* arg_output = NULL; |
75 | ||
ad95fd1d ZJS |
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; | |
cc64d017 | 80 | |
fdfccdbc ZJS |
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 | ||
4a62c710 MS |
90 | if (pipe(fd) < 0) |
91 | return log_error_errno(errno, "Failed to create pager pipe: %m"); | |
fdfccdbc ZJS |
92 | |
93 | parent_pid = getpid(); | |
94 | ||
95 | child_pid = fork(); | |
96 | if (child_pid < 0) { | |
76ef789d | 97 | r = log_error_errno(errno, "Failed to fork: %m"); |
3d94f76c | 98 | safe_close_pair(fd); |
fdfccdbc ZJS |
99 | return r; |
100 | } | |
101 | ||
102 | /* In the child */ | |
103 | if (child_pid == 0) { | |
ce30c8dc LP |
104 | |
105 | (void) reset_all_signal_handlers(); | |
106 | (void) reset_signal_mask(); | |
107 | ||
fdfccdbc ZJS |
108 | r = dup2(fd[1], STDOUT_FILENO); |
109 | if (r < 0) { | |
56f64d95 | 110 | log_error_errno(errno, "Failed to dup pipe to stdout: %m"); |
fdfccdbc ZJS |
111 | _exit(EXIT_FAILURE); |
112 | } | |
113 | ||
3d94f76c | 114 | safe_close_pair(fd); |
fdfccdbc ZJS |
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); | |
56f64d95 | 126 | log_error_errno(errno, "Failed to exec child %s: %m", child); |
fdfccdbc ZJS |
127 | _exit(EXIT_FAILURE); |
128 | } | |
129 | ||
130 | r = close(fd[1]); | |
131 | if (r < 0) | |
56f64d95 | 132 | log_warning_errno(errno, "Failed to close write end of pipe: %m"); |
fdfccdbc ZJS |
133 | |
134 | return fd[0]; | |
135 | } | |
136 | ||
8201af08 | 137 | static int spawn_curl(const char* url) { |
fdfccdbc ZJS |
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) | |
709f6e46 | 147 | log_error_errno(r, "Failed to spawn curl: %m"); |
fdfccdbc ZJS |
148 | return r; |
149 | } | |
150 | ||
2f1acf6f | 151 | static int spawn_getter(const char *getter) { |
fdfccdbc | 152 | int r; |
c8b32e11 | 153 | _cleanup_strv_free_ char **words = NULL; |
fdfccdbc ZJS |
154 | |
155 | assert(getter); | |
8adaf7bd | 156 | r = strv_split_extract(&words, getter, WHITESPACE, EXTRACT_QUOTES); |
23bbb0de MS |
157 | if (r < 0) |
158 | return log_error_errno(r, "Failed to split getter option: %m"); | |
fdfccdbc ZJS |
159 | |
160 | r = spawn_child(words[0], words); | |
161 | if (r < 0) | |
709f6e46 | 162 | log_error_errno(r, "Failed to spawn getter %s: %m", getter); |
fdfccdbc ZJS |
163 | |
164 | return r; | |
165 | } | |
166 | ||
9ff48d09 | 167 | #define filename_escape(s) xescape((s), "/ ") |
8201af08 ZJS |
168 | |
169 | static int open_output(Writer *w, const char* host) { | |
170 | _cleanup_free_ char *_output = NULL; | |
171 | const char *output; | |
fdfccdbc ZJS |
172 | int r; |
173 | ||
8201af08 ZJS |
174 | switch (arg_split_mode) { |
175 | case JOURNAL_WRITE_SPLIT_NONE: | |
176 | output = arg_output ?: REMOTE_JOURNAL_PATH "/remote.journal"; | |
177 | break; | |
fdfccdbc | 178 | |
8201af08 ZJS |
179 | case JOURNAL_WRITE_SPLIT_HOST: { |
180 | _cleanup_free_ char *name; | |
fdfccdbc | 181 | |
8201af08 | 182 | assert(host); |
fdfccdbc | 183 | |
8201af08 ZJS |
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); | |
fdfccdbc ZJS |
191 | if (r < 0) |
192 | return log_oom(); | |
8201af08 ZJS |
193 | |
194 | output = _output; | |
195 | break; | |
196 | } | |
197 | ||
198 | default: | |
199 | assert_not_reached("what?"); | |
fdfccdbc ZJS |
200 | } |
201 | ||
202 | r = journal_file_open_reliably(output, | |
203 | O_RDWR|O_CREAT, 0640, | |
204 | arg_compress, arg_seal, | |
8201af08 ZJS |
205 | &w->metrics, |
206 | w->mmap, | |
207 | NULL, &w->journal); | |
fdfccdbc | 208 | if (r < 0) |
c33b3297 MS |
209 | log_error_errno(r, "Failed to open output journal %s: %m", |
210 | output); | |
fdfccdbc | 211 | else |
0e72da6f | 212 | log_debug("Opened output file %s", w->journal->path); |
fdfccdbc ZJS |
213 | return r; |
214 | } | |
215 | ||
cc64d017 ZJS |
216 | /********************************************************************** |
217 | ********************************************************************** | |
218 | **********************************************************************/ | |
219 | ||
9ff48d09 | 220 | static int init_writer_hashmap(RemoteServer *s) { |
d5099efc MS |
221 | static const struct hash_ops *hash_ops[] = { |
222 | [JOURNAL_WRITE_SPLIT_NONE] = NULL, | |
223 | [JOURNAL_WRITE_SPLIT_HOST] = &string_hash_ops, | |
9ff48d09 ZJS |
224 | }; |
225 | ||
d5099efc | 226 | assert(arg_split_mode >= 0 && arg_split_mode < (int) ELEMENTSOF(hash_ops)); |
cc64d017 | 227 | |
d5099efc | 228 | s->writers = hashmap_new(hash_ops[arg_split_mode]); |
9ff48d09 ZJS |
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 | } | |
cc64d017 | 268 | |
9ff48d09 ZJS |
269 | r = open_output(w, host); |
270 | if (r < 0) | |
271 | return r; | |
fdfccdbc | 272 | |
9ff48d09 ZJS |
273 | r = hashmap_put(s->writers, w->hashmap_key ?: key, w); |
274 | if (r < 0) | |
275 | return r; | |
276 | } | |
fdfccdbc | 277 | |
9ff48d09 ZJS |
278 | *writer = w; |
279 | w = NULL; | |
280 | return 0; | |
281 | } | |
cc64d017 | 282 | |
9ff48d09 ZJS |
283 | /********************************************************************** |
284 | ********************************************************************** | |
285 | **********************************************************************/ | |
fdfccdbc | 286 | |
cc64d017 ZJS |
287 | /* This should go away as soon as µhttpd allows state to be passed around. */ |
288 | static RemoteServer *server; | |
289 | ||
fdfccdbc ZJS |
290 | static int dispatch_raw_source_event(sd_event_source *event, |
291 | int fd, | |
292 | uint32_t revents, | |
293 | void *userdata); | |
043945b9 ZJS |
294 | static int dispatch_raw_source_until_block(sd_event_source *event, |
295 | void *userdata); | |
70f1b2dd ZJS |
296 | static int dispatch_blocking_source_event(sd_event_source *event, |
297 | void *userdata); | |
fdfccdbc ZJS |
298 | static int dispatch_raw_connection_event(sd_event_source *event, |
299 | int fd, | |
300 | uint32_t revents, | |
301 | void *userdata); | |
cc64d017 ZJS |
302 | static int dispatch_http_event(sd_event_source *event, |
303 | int fd, | |
304 | uint32_t revents, | |
305 | void *userdata); | |
fdfccdbc | 306 | |
9ff48d09 ZJS |
307 | static int get_source_for_fd(RemoteServer *s, |
308 | int fd, char *name, RemoteSource **source) { | |
309 | Writer *writer; | |
310 | int r; | |
311 | ||
1f8af042 ZJS |
312 | /* This takes ownership of name, but only on success. */ |
313 | ||
fdfccdbc ZJS |
314 | assert(fd >= 0); |
315 | assert(source); | |
316 | ||
ca2d3784 | 317 | if (!GREEDY_REALLOC0(s->sources, s->sources_size, fd + 1)) |
fdfccdbc ZJS |
318 | return log_oom(); |
319 | ||
9ff48d09 | 320 | r = get_writer(s, name, &writer); |
eb56eb9b MS |
321 | if (r < 0) |
322 | return log_warning_errno(r, "Failed to get writer for source %s: %m", | |
323 | name); | |
9ff48d09 | 324 | |
fdfccdbc | 325 | if (s->sources[fd] == NULL) { |
9ff48d09 ZJS |
326 | s->sources[fd] = source_new(fd, false, name, writer); |
327 | if (!s->sources[fd]) { | |
328 | writer_unref(writer); | |
fdfccdbc | 329 | return log_oom(); |
9ff48d09 ZJS |
330 | } |
331 | ||
fdfccdbc ZJS |
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); | |
ca2d3784 | 343 | assert(fd >= 0 && fd < (ssize_t) s->sources_size); |
fdfccdbc ZJS |
344 | |
345 | source = s->sources[fd]; | |
346 | if (source) { | |
8201af08 | 347 | /* this closes fd too */ |
fdfccdbc ZJS |
348 | source_free(source); |
349 | s->sources[fd] = NULL; | |
350 | s->active--; | |
351 | } | |
352 | ||
fdfccdbc ZJS |
353 | return 0; |
354 | } | |
355 | ||
9ff48d09 | 356 | static int add_source(RemoteServer *s, int fd, char* name, bool own_name) { |
8201af08 | 357 | |
a7f7d1bd | 358 | RemoteSource *source = NULL; |
fdfccdbc ZJS |
359 | int r; |
360 | ||
1f8af042 ZJS |
361 | /* This takes ownership of name, even on failure, if own_name is true. */ |
362 | ||
fdfccdbc ZJS |
363 | assert(s); |
364 | assert(fd >= 0); | |
9ff48d09 | 365 | assert(name); |
fdfccdbc | 366 | |
9ff48d09 ZJS |
367 | if (!own_name) { |
368 | name = strdup(name); | |
369 | if (!name) | |
370 | return log_oom(); | |
371 | } | |
fdfccdbc | 372 | |
9ff48d09 | 373 | r = get_source_for_fd(s, fd, name, &source); |
fdfccdbc | 374 | if (r < 0) { |
c33b3297 MS |
375 | log_error_errno(r, "Failed to create source for fd:%d (%s): %m", |
376 | fd, name); | |
1f8af042 | 377 | free(name); |
fdfccdbc ZJS |
378 | return r; |
379 | } | |
8201af08 | 380 | |
fdfccdbc | 381 | r = sd_event_add_io(s->events, &source->event, |
8201af08 | 382 | fd, EPOLLIN|EPOLLRDHUP|EPOLLPRI, |
043945b9 ZJS |
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) { | |
70f1b2dd ZJS |
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 | } | |
fdfccdbc | 398 | if (r < 0) { |
c33b3297 MS |
399 | log_error_errno(r, "Failed to register event source for fd:%d: %m", |
400 | fd); | |
fdfccdbc ZJS |
401 | goto error; |
402 | } | |
403 | ||
356779df | 404 | r = sd_event_source_set_description(source->event, name); |
43300d9d | 405 | if (r < 0) { |
da927ba9 | 406 | log_error_errno(r, "Failed to set source name for fd:%d: %m", fd); |
43300d9d ZJS |
407 | goto error; |
408 | } | |
409 | ||
fdfccdbc ZJS |
410 | return 1; /* work to do */ |
411 | ||
412 | error: | |
413 | remove_source(s, fd); | |
414 | return r; | |
415 | } | |
416 | ||
8a8d55f2 ZJS |
417 | static int add_raw_socket(RemoteServer *s, int fd) { |
418 | int r; | |
43300d9d | 419 | _cleanup_close_ int fd_ = fd; |
5ffa8c81 | 420 | char name[sizeof("raw-socket-")-1 + DECIMAL_STR_MAX(int) + 1]; |
43300d9d ZJS |
421 | |
422 | assert(fd >= 0); | |
fdfccdbc | 423 | |
8201af08 ZJS |
424 | r = sd_event_add_io(s->events, &s->listen_event, |
425 | fd, EPOLLIN, | |
fdfccdbc | 426 | dispatch_raw_connection_event, s); |
43300d9d | 427 | if (r < 0) |
fdfccdbc | 428 | return r; |
fdfccdbc | 429 | |
5ffa8c81 | 430 | xsprintf(name, "raw-socket-%d", fd); |
43300d9d | 431 | |
356779df | 432 | r = sd_event_source_set_description(s->listen_event, name); |
43300d9d ZJS |
433 | if (r < 0) |
434 | return r; | |
435 | ||
436 | fd_ = -1; | |
fdfccdbc ZJS |
437 | s->active ++; |
438 | return 0; | |
439 | } | |
440 | ||
8a8d55f2 ZJS |
441 | static int setup_raw_socket(RemoteServer *s, const char *address) { |
442 | int fd; | |
443 | ||
7b7afdfc | 444 | fd = make_socket_fd(LOG_INFO, address, SOCK_STREAM, SOCK_CLOEXEC); |
8a8d55f2 ZJS |
445 | if (fd < 0) |
446 | return fd; | |
447 | ||
448 | return add_raw_socket(s, fd); | |
449 | } | |
450 | ||
8201af08 ZJS |
451 | /********************************************************************** |
452 | ********************************************************************** | |
453 | **********************************************************************/ | |
454 | ||
9ce998b9 | 455 | static int request_meta(void **connection_cls, int fd, char *hostname) { |
cc64d017 | 456 | RemoteSource *source; |
9ff48d09 ZJS |
457 | Writer *writer; |
458 | int r; | |
cc64d017 ZJS |
459 | |
460 | assert(connection_cls); | |
9ff48d09 | 461 | if (*connection_cls) |
9ce998b9 | 462 | return 0; |
9ff48d09 ZJS |
463 | |
464 | r = get_writer(server, hostname, &writer); | |
eb56eb9b MS |
465 | if (r < 0) |
466 | return log_warning_errno(r, "Failed to get writer for source %s: %m", | |
467 | hostname); | |
cc64d017 | 468 | |
9ff48d09 | 469 | source = source_new(fd, true, hostname, writer); |
8201af08 | 470 | if (!source) { |
9ff48d09 | 471 | writer_unref(writer); |
9ce998b9 | 472 | return log_oom(); |
8201af08 ZJS |
473 | } |
474 | ||
cc64d017 ZJS |
475 | log_debug("Added RemoteSource as connection metadata %p", source); |
476 | ||
477 | *connection_cls = source; | |
9ce998b9 | 478 | return 0; |
cc64d017 ZJS |
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 | ||
9ce998b9 ZJS |
490 | if (s) { |
491 | log_debug("Cleaning up connection metadata %p", s); | |
492 | source_free(s); | |
493 | *connection_cls = NULL; | |
494 | } | |
cc64d017 ZJS |
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 | ||
8201af08 | 503 | bool finished = false; |
4a0a6ac0 | 504 | size_t remaining; |
9ff48d09 | 505 | int r; |
cc64d017 ZJS |
506 | |
507 | assert(source); | |
508 | ||
cb41ff29 ZJS |
509 | log_trace("%s: connection %p, %zu bytes", |
510 | __func__, connection, *upload_data_size); | |
cc64d017 ZJS |
511 | |
512 | if (*upload_data_size) { | |
cb41ff29 | 513 | log_trace("Received %zu bytes", *upload_data_size); |
cc64d017 ZJS |
514 | |
515 | r = push_data(source, upload_data, *upload_data_size); | |
851d4e2a | 516 | if (r < 0) |
e7216d11 | 517 | return mhd_respond_oom(connection); |
851d4e2a | 518 | |
cc64d017 ZJS |
519 | *upload_data_size = 0; |
520 | } else | |
521 | finished = true; | |
522 | ||
57255510 | 523 | for (;;) { |
9ff48d09 | 524 | r = process_source(source, arg_compress, arg_seal); |
ff55c3c7 | 525 | if (r == -EAGAIN) |
cc64d017 ZJS |
526 | break; |
527 | else if (r < 0) { | |
528 | log_warning("Failed to process data for connection %p", connection); | |
851d4e2a ZJS |
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)); | |
cc64d017 ZJS |
538 | } |
539 | } | |
540 | ||
541 | if (!finished) | |
542 | return MHD_YES; | |
543 | ||
544 | /* The upload is finished */ | |
545 | ||
4a0a6ac0 ZJS |
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); | |
cc64d017 ZJS |
552 | } |
553 | ||
e7216d11 | 554 | return mhd_respond(connection, MHD_HTTP_ACCEPTED, "OK.\n"); |
cc64d017 ZJS |
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; | |
8201af08 | 568 | int r, code, fd; |
9ff48d09 | 569 | _cleanup_free_ char *hostname = NULL; |
cc64d017 ZJS |
570 | |
571 | assert(connection); | |
572 | assert(connection_cls); | |
573 | assert(url); | |
574 | assert(method); | |
575 | ||
cb41ff29 | 576 | log_trace("Handling a connection %s %s %s", method, url, version); |
cc64d017 ZJS |
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")) | |
ce7229a2 | 584 | return mhd_respond(connection, MHD_HTTP_NOT_ACCEPTABLE, |
e7216d11 | 585 | "Unsupported method.\n"); |
cc64d017 ZJS |
586 | |
587 | if (!streq(url, "/upload")) | |
e7216d11 ZJS |
588 | return mhd_respond(connection, MHD_HTTP_NOT_FOUND, |
589 | "Not found.\n"); | |
cc64d017 ZJS |
590 | |
591 | header = MHD_lookup_connection_value(connection, | |
592 | MHD_HEADER_KIND, "Content-Type"); | |
593 | if (!header || !streq(header, "application/vnd.fdo.journal")) | |
e7216d11 ZJS |
594 | return mhd_respond(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE, |
595 | "Content-Type: application/vnd.fdo.journal" | |
596 | " is required.\n"); | |
cc64d017 | 597 | |
8201af08 ZJS |
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"); | |
8201af08 ZJS |
607 | } |
608 | ||
609 | fd = ci->connect_fd; | |
610 | assert(fd >= 0); | |
611 | } | |
612 | ||
ad95fd1d | 613 | if (server->check_trust) { |
8201af08 | 614 | r = check_permissions(connection, &code, &hostname); |
cc64d017 ZJS |
615 | if (r < 0) |
616 | return code; | |
8201af08 | 617 | } else { |
366b7db4 | 618 | r = getpeername_pretty(fd, false, &hostname); |
1f6b4113 | 619 | if (r < 0) |
8201af08 ZJS |
620 | return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, |
621 | "Cannot check remote hostname"); | |
cc64d017 ZJS |
622 | } |
623 | ||
8201af08 ZJS |
624 | assert(hostname); |
625 | ||
9ce998b9 ZJS |
626 | r = request_meta(connection_cls, fd, hostname); |
627 | if (r == -ENOMEM) | |
cc64d017 | 628 | return respond_oom(connection); |
9ce998b9 ZJS |
629 | else if (r < 0) |
630 | return mhd_respond(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, | |
631 | strerror(-r)); | |
632 | ||
9ff48d09 | 633 | hostname = NULL; |
cc64d017 ZJS |
634 | return MHD_YES; |
635 | } | |
636 | ||
ad95fd1d ZJS |
637 | static int setup_microhttpd_server(RemoteServer *s, |
638 | int fd, | |
639 | const char *key, | |
640 | const char *cert, | |
641 | const char *trust) { | |
cc64d017 ZJS |
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}, | |
8c6bcbf8 | 646 | { MHD_OPTION_CONNECTION_MEMORY_LIMIT, 128*1024}, |
cc64d017 ZJS |
647 | { MHD_OPTION_END}, |
648 | { MHD_OPTION_END}, | |
649 | { MHD_OPTION_END}, | |
650 | { MHD_OPTION_END}}; | |
5e3efccf | 651 | int opts_pos = 4; |
cc64d017 ZJS |
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 | ||
7b7afdfc | 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 | 875 | |
366b7db4 | 876 | r = getpeername_pretty(fd, false, &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 | ||
2f1acf6f YW |
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 | } | |
8201af08 | 904 | |
fdfccdbc | 905 | if (arg_url) { |
d10accb0 YW |
906 | const char *url; |
907 | char *hostname, *p; | |
fdfccdbc | 908 | |
b68f6b0a YW |
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"); | |
fdfccdbc | 914 | } |
b68f6b0a YW |
915 | else |
916 | url = strdupa(arg_url); | |
fdfccdbc | 917 | |
2f1acf6f YW |
918 | log_info("Spawning curl %s...", url); |
919 | fd = spawn_curl(url); | |
fdfccdbc ZJS |
920 | if (fd < 0) |
921 | return fd; | |
922 | ||
8201af08 ZJS |
923 | hostname = |
924 | startswith(arg_url, "https://") ?: | |
925 | startswith(arg_url, "http://") ?: | |
926 | arg_url; | |
927 | ||
d10accb0 | 928 | hostname = strdupa(hostname); |
d10accb0 YW |
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); | |
fdfccdbc ZJS |
935 | if (r < 0) |
936 | return r; | |
fdfccdbc ZJS |
937 | } |
938 | ||
939 | if (arg_listen_raw) { | |
0e72da6f | 940 | log_debug("Listening on a socket..."); |
fdfccdbc ZJS |
941 | r = setup_raw_socket(s, arg_listen_raw); |
942 | if (r < 0) | |
943 | return r; | |
fdfccdbc ZJS |
944 | } |
945 | ||
cc64d017 | 946 | if (arg_listen_http) { |
ad95fd1d | 947 | r = setup_microhttpd_socket(s, arg_listen_http, NULL, NULL, NULL); |
cc64d017 ZJS |
948 | if (r < 0) |
949 | return r; | |
cc64d017 ZJS |
950 | } |
951 | ||
952 | if (arg_listen_https) { | |
ad95fd1d | 953 | r = setup_microhttpd_socket(s, arg_listen_https, key, cert, trust); |
cc64d017 ZJS |
954 | if (r < 0) |
955 | return r; | |
cc64d017 ZJS |
956 | } |
957 | ||
8a8d55f2 | 958 | STRV_FOREACH(file, arg_files) { |
40780a62 ZJS |
959 | const char *output_name; |
960 | ||
8a8d55f2 | 961 | if (streq(*file, "-")) { |
0e72da6f | 962 | log_debug("Using standard input as source."); |
8a8d55f2 ZJS |
963 | |
964 | fd = STDIN_FILENO; | |
965 | output_name = "stdin"; | |
966 | } else { | |
0e72da6f | 967 | log_debug("Reading file %s...", *file); |
8a8d55f2 ZJS |
968 | |
969 | fd = open(*file, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
4a62c710 MS |
970 | if (fd < 0) |
971 | return log_error_errno(errno, "Failed to open %s: %m", *file); | |
8a8d55f2 ZJS |
972 | output_name = *file; |
973 | } | |
974 | ||
9ff48d09 | 975 | r = add_source(s, fd, (char*) output_name, false); |
fdfccdbc ZJS |
976 | if (r < 0) |
977 | return r; | |
fdfccdbc ZJS |
978 | } |
979 | ||
980 | if (s->active == 0) { | |
f823da67 | 981 | log_error("Zero sources specified"); |
fdfccdbc ZJS |
982 | return -EINVAL; |
983 | } | |
984 | ||
8201af08 ZJS |
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. */ | |
9ff48d09 | 989 | r = get_writer(s, NULL, &s->_single_writer); |
8201af08 ZJS |
990 | if (r < 0) |
991 | return r; | |
992 | } | |
993 | ||
994 | return 0; | |
fdfccdbc ZJS |
995 | } |
996 | ||
9ff48d09 | 997 | static void server_destroy(RemoteServer *s) { |
ca2d3784 | 998 | size_t i; |
cc64d017 | 999 | MHDDaemonWrapper *d; |
fdfccdbc | 1000 | |
cc64d017 ZJS |
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 | ||
fdfccdbc | 1009 | assert(s->sources_size == 0 || s->sources); |
cc64d017 | 1010 | for (i = 0; i < s->sources_size; i++) |
fdfccdbc | 1011 | remove_source(s, i); |
fdfccdbc ZJS |
1012 | free(s->sources); |
1013 | ||
9ff48d09 ZJS |
1014 | writer_unref(s->_single_writer); |
1015 | hashmap_free(s->writers); | |
1016 | ||
fdfccdbc ZJS |
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... */ | |
fdfccdbc ZJS |
1023 | } |
1024 | ||
1025 | /********************************************************************** | |
1026 | ********************************************************************** | |
1027 | **********************************************************************/ | |
1028 | ||
043945b9 ZJS |
1029 | static int handle_raw_source(sd_event_source *event, |
1030 | int fd, | |
1031 | uint32_t revents, | |
1032 | RemoteServer *s) { | |
fdfccdbc | 1033 | |
fdfccdbc ZJS |
1034 | RemoteSource *source; |
1035 | int r; | |
1036 | ||
043945b9 ZJS |
1037 | /* Returns 1 if there might be more data pending, |
1038 | * 0 if data is currently exhausted, negative on error. | |
1039 | */ | |
1040 | ||
ca2d3784 | 1041 | assert(fd >= 0 && fd < (ssize_t) s->sources_size); |
fdfccdbc ZJS |
1042 | source = s->sources[fd]; |
1043 | assert(source->fd == fd); | |
1044 | ||
9ff48d09 | 1045 | r = process_source(source, arg_compress, arg_seal); |
fdfccdbc | 1046 | if (source->state == STATE_EOF) { |
4a0a6ac0 ZJS |
1047 | size_t remaining; |
1048 | ||
0e72da6f ZJS |
1049 | log_debug("EOF reached with source fd:%d (%s)", |
1050 | source->fd, source->name); | |
4a0a6ac0 ZJS |
1051 | |
1052 | remaining = source_non_empty(source); | |
1053 | if (remaining > 0) | |
0e72da6f | 1054 | log_notice("Premature EOF. %zu bytes lost.", remaining); |
fdfccdbc | 1055 | remove_source(s, source->fd); |
0e72da6f | 1056 | log_debug("%zu active sources remaining", s->active); |
8201af08 | 1057 | return 0; |
fdfccdbc | 1058 | } else if (r == -E2BIG) { |
0e72da6f | 1059 | log_notice_errno(E2BIG, "Entry too big, skipped"); |
8201af08 | 1060 | return 1; |
ff55c3c7 | 1061 | } else if (r == -EAGAIN) { |
8201af08 ZJS |
1062 | return 0; |
1063 | } else if (r < 0) { | |
0e72da6f | 1064 | log_debug_errno(r, "Closing connection: %m"); |
8201af08 ZJS |
1065 | remove_source(server, fd); |
1066 | return 0; | |
1067 | } else | |
1068 | return 1; | |
fdfccdbc ZJS |
1069 | } |
1070 | ||
043945b9 ZJS |
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 | ||
70f1b2dd ZJS |
1108 | static int dispatch_blocking_source_event(sd_event_source *event, |
1109 | void *userdata) { | |
1110 | RemoteSource *source = userdata; | |
1111 | ||
043945b9 | 1112 | return handle_raw_source(event, source->fd, EPOLLIN, server); |
70f1b2dd ZJS |
1113 | } |
1114 | ||
8201af08 ZJS |
1115 | static int accept_connection(const char* type, int fd, |
1116 | SocketAddress *addr, char **hostname) { | |
fdfccdbc ZJS |
1117 | int fd2, r; |
1118 | ||
cc64d017 ZJS |
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); | |
4a62c710 MS |
1121 | if (fd2 < 0) |
1122 | return log_error_errno(errno, "accept() on fd:%d failed: %m", fd); | |
fdfccdbc | 1123 | |
cc64d017 | 1124 | switch(socket_address_family(addr)) { |
fdfccdbc ZJS |
1125 | case AF_INET: |
1126 | case AF_INET6: { | |
8201af08 ZJS |
1127 | _cleanup_free_ char *a = NULL; |
1128 | char *b; | |
fdfccdbc | 1129 | |
cc64d017 | 1130 | r = socket_address_print(addr, &a); |
fdfccdbc | 1131 | if (r < 0) { |
da927ba9 | 1132 | log_error_errno(r, "socket_address_print(): %m"); |
fdfccdbc ZJS |
1133 | close(fd2); |
1134 | return r; | |
1135 | } | |
1136 | ||
8201af08 ZJS |
1137 | r = socknameinfo_pretty(&addr->sockaddr, addr->size, &b); |
1138 | if (r < 0) { | |
1938ac51 | 1139 | log_error_errno(r, "Resolving hostname failed: %m"); |
8201af08 ZJS |
1140 | close(fd2); |
1141 | return r; | |
1142 | } | |
1143 | ||
0e72da6f ZJS |
1144 | log_debug("Accepted %s %s connection from %s", |
1145 | type, | |
1146 | socket_address_family(addr) == AF_INET ? "IP" : "IPv6", | |
1147 | a); | |
cc64d017 | 1148 | |
8201af08 ZJS |
1149 | *hostname = b; |
1150 | ||
cc64d017 | 1151 | return fd2; |
fdfccdbc ZJS |
1152 | }; |
1153 | default: | |
cc64d017 ZJS |
1154 | log_error("Rejected %s connection with unsupported family %d", |
1155 | type, socket_address_family(addr)); | |
fdfccdbc | 1156 | close(fd2); |
cc64d017 | 1157 | |
fdfccdbc ZJS |
1158 | return -EINVAL; |
1159 | } | |
cc64d017 | 1160 | } |
fdfccdbc | 1161 | |
cc64d017 ZJS |
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; | |
1f8af042 | 1167 | int fd2; |
cc64d017 ZJS |
1168 | SocketAddress addr = { |
1169 | .size = sizeof(union sockaddr_union), | |
1170 | .type = SOCK_STREAM, | |
1171 | }; | |
a7f7d1bd | 1172 | char *hostname = NULL; |
fdfccdbc | 1173 | |
8201af08 | 1174 | fd2 = accept_connection("raw", fd, &addr, &hostname); |
cc64d017 ZJS |
1175 | if (fd2 < 0) |
1176 | return fd2; | |
fdfccdbc | 1177 | |
1f8af042 | 1178 | return add_source(s, fd2, hostname, true); |
cc64d017 | 1179 | } |
fdfccdbc ZJS |
1180 | |
1181 | /********************************************************************** | |
1182 | ********************************************************************** | |
1183 | **********************************************************************/ | |
1184 | ||
8201af08 ZJS |
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 | ||
ad95fd1d ZJS |
1196 | static int parse_config(void) { |
1197 | const ConfigTableItem items[] = { | |
9d3737f1 | 1198 | { "Remote", "Seal", config_parse_bool, 0, &arg_seal }, |
8201af08 ZJS |
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 }, | |
ad95fd1d | 1203 | {}}; |
ad95fd1d | 1204 | |
bf257aed | 1205 | return config_parse_many(PKGSYSCONFDIR "/journal-remote.conf", |
75eb6154 | 1206 | CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"), |
bf257aed JT |
1207 | "Remote\0", config_item_table_lookup, items, |
1208 | false, NULL); | |
ad95fd1d ZJS |
1209 | } |
1210 | ||
5c879495 | 1211 | static void help(void) { |
8a8d55f2 | 1212 | printf("%s [OPTIONS...] {FILE|-}...\n\n" |
a83f4037 | 1213 | "Write external journal events to journal file(s).\n\n" |
a7736b14 ZJS |
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" | |
dad29dff | 1230 | " --gnutls-log=CATEGORY...\n" |
a7736b14 ZJS |
1231 | " Specify a list of gnutls logging categories\n" |
1232 | " --split-mode=none|host How many output files to create\n" | |
fdfccdbc ZJS |
1233 | "\n" |
1234 | "Note: file descriptors from sd_listen_fds() will be consumed, too.\n" | |
1235 | , program_invocation_short_name); | |
fdfccdbc ZJS |
1236 | } |
1237 | ||
1238 | static int parse_argv(int argc, char *argv[]) { | |
1239 | enum { | |
1240 | ARG_VERSION = 0x100, | |
1241 | ARG_URL, | |
1242 | ARG_LISTEN_RAW, | |
cc64d017 ZJS |
1243 | ARG_LISTEN_HTTP, |
1244 | ARG_LISTEN_HTTPS, | |
fdfccdbc | 1245 | ARG_GETTER, |
8201af08 | 1246 | ARG_SPLIT_MODE, |
fdfccdbc | 1247 | ARG_COMPRESS, |
fdfccdbc | 1248 | ARG_SEAL, |
cc64d017 ZJS |
1249 | ARG_KEY, |
1250 | ARG_CERT, | |
1251 | ARG_TRUST, | |
5937a4d4 | 1252 | ARG_GNUTLS_LOG, |
fdfccdbc ZJS |
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 }, | |
cc64d017 ZJS |
1261 | { "listen-http", required_argument, NULL, ARG_LISTEN_HTTP }, |
1262 | { "listen-https", required_argument, NULL, ARG_LISTEN_HTTPS }, | |
fdfccdbc | 1263 | { "output", required_argument, NULL, 'o' }, |
8201af08 | 1264 | { "split-mode", required_argument, NULL, ARG_SPLIT_MODE }, |
dad29dff LP |
1265 | { "compress", optional_argument, NULL, ARG_COMPRESS }, |
1266 | { "seal", optional_argument, NULL, ARG_SEAL }, | |
cc64d017 ZJS |
1267 | { "key", required_argument, NULL, ARG_KEY }, |
1268 | { "cert", required_argument, NULL, ARG_CERT }, | |
1269 | { "trust", required_argument, NULL, ARG_TRUST }, | |
5937a4d4 | 1270 | { "gnutls-log", required_argument, NULL, ARG_GNUTLS_LOG }, |
fdfccdbc ZJS |
1271 | {} |
1272 | }; | |
1273 | ||
cc64d017 | 1274 | int c, r; |
8201af08 | 1275 | bool type_a, type_b; |
fdfccdbc ZJS |
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: | |
3f6fd1ba | 1287 | return version(); |
fdfccdbc ZJS |
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 | ||
cc64d017 | 1316 | case ARG_LISTEN_HTTP: |
8a8d55f2 | 1317 | if (arg_listen_http || http_socket >= 0) { |
cc64d017 ZJS |
1318 | log_error("cannot currently use --listen-http more than once"); |
1319 | return -EINVAL; | |
1320 | } | |
1321 | ||
42b6bf75 | 1322 | r = negative_fd(optarg); |
8a8d55f2 ZJS |
1323 | if (r >= 0) |
1324 | http_socket = r; | |
24739b7b | 1325 | else |
8a8d55f2 | 1326 | arg_listen_http = optarg; |
cc64d017 ZJS |
1327 | break; |
1328 | ||
1329 | case ARG_LISTEN_HTTPS: | |
8a8d55f2 | 1330 | if (arg_listen_https || https_socket >= 0) { |
cc64d017 ZJS |
1331 | log_error("cannot currently use --listen-https more than once"); |
1332 | return -EINVAL; | |
1333 | } | |
1334 | ||
42b6bf75 | 1335 | r = negative_fd(optarg); |
8a8d55f2 ZJS |
1336 | if (r >= 0) |
1337 | https_socket = r; | |
24739b7b | 1338 | else |
8a8d55f2 | 1339 | arg_listen_https = optarg; |
8a8d55f2 | 1340 | |
cc64d017 ZJS |
1341 | break; |
1342 | ||
1343 | case ARG_KEY: | |
ad95fd1d | 1344 | if (arg_key) { |
cc64d017 ZJS |
1345 | log_error("Key file specified twice"); |
1346 | return -EINVAL; | |
1347 | } | |
ad95fd1d ZJS |
1348 | |
1349 | arg_key = strdup(optarg); | |
1350 | if (!arg_key) | |
1351 | return log_oom(); | |
1352 | ||
cc64d017 ZJS |
1353 | break; |
1354 | ||
1355 | case ARG_CERT: | |
ad95fd1d | 1356 | if (arg_cert) { |
cc64d017 ZJS |
1357 | log_error("Certificate file specified twice"); |
1358 | return -EINVAL; | |
1359 | } | |
ad95fd1d ZJS |
1360 | |
1361 | arg_cert = strdup(optarg); | |
1362 | if (!arg_cert) | |
1363 | return log_oom(); | |
1364 | ||
cc64d017 ZJS |
1365 | break; |
1366 | ||
1367 | case ARG_TRUST: | |
ad95fd1d ZJS |
1368 | if (arg_trust || arg_trust_all) { |
1369 | log_error("Confusing trusted CA configuration"); | |
cc64d017 ZJS |
1370 | return -EINVAL; |
1371 | } | |
ad95fd1d ZJS |
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(); | |
cc64d017 | 1380 | #else |
ad95fd1d ZJS |
1381 | log_error("Option --trust is not available."); |
1382 | return -EINVAL; | |
cc64d017 | 1383 | #endif |
ad95fd1d ZJS |
1384 | } |
1385 | ||
1386 | break; | |
cc64d017 | 1387 | |
fdfccdbc ZJS |
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 | ||
8201af08 ZJS |
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 | ||
fdfccdbc | 1405 | case ARG_COMPRESS: |
dad29dff LP |
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 | ||
fdfccdbc | 1417 | break; |
dad29dff | 1418 | |
fdfccdbc | 1419 | case ARG_SEAL: |
dad29dff LP |
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 | ||
fdfccdbc ZJS |
1431 | break; |
1432 | ||
5937a4d4 ZJS |
1433 | case ARG_GNUTLS_LOG: { |
1434 | #ifdef HAVE_GNUTLS | |
39583258 | 1435 | const char* p = optarg; |
b1f48e3c SS |
1436 | for (;;) { |
1437 | _cleanup_free_ char *word = NULL; | |
5937a4d4 | 1438 | |
b1f48e3c SS |
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"); | |
5937a4d4 | 1442 | |
b1f48e3c SS |
1443 | if (r == 0) |
1444 | break; | |
5937a4d4 | 1445 | |
b1f48e3c | 1446 | if (strv_push(&arg_gnutls_log, word) < 0) |
5937a4d4 | 1447 | return log_oom(); |
b1f48e3c SS |
1448 | |
1449 | word = NULL; | |
5937a4d4 ZJS |
1450 | } |
1451 | break; | |
1452 | #else | |
1453 | log_error("Option --gnutls-log is not available."); | |
1454 | return -EINVAL; | |
1455 | #endif | |
1456 | } | |
1457 | ||
fdfccdbc ZJS |
1458 | case '?': |
1459 | return -EINVAL; | |
1460 | ||
1461 | default: | |
601185b4 | 1462 | assert_not_reached("Unknown option code."); |
fdfccdbc ZJS |
1463 | } |
1464 | ||
8a8d55f2 ZJS |
1465 | if (optind < argc) |
1466 | arg_files = argv + optind; | |
fdfccdbc | 1467 | |
8201af08 ZJS |
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 | ||
fdfccdbc ZJS |
1505 | return 1 /* work to do */; |
1506 | } | |
1507 | ||
ad95fd1d ZJS |
1508 | static int load_certificates(char **key, char **cert, char **trust) { |
1509 | int r; | |
1510 | ||
799a8f39 | 1511 | r = read_full_file(arg_key ?: PRIV_KEY_FILE, key, NULL); |
eb56eb9b MS |
1512 | if (r < 0) |
1513 | return log_error_errno(r, "Failed to read key from file '%s': %m", | |
1514 | arg_key ?: PRIV_KEY_FILE); | |
ad95fd1d ZJS |
1515 | |
1516 | r = read_full_file(arg_cert ?: CERT_FILE, cert, NULL); | |
eb56eb9b MS |
1517 | if (r < 0) |
1518 | return log_error_errno(r, "Failed to read certificate from file '%s': %m", | |
1519 | arg_cert ?: CERT_FILE); | |
ad95fd1d ZJS |
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); | |
eb56eb9b MS |
1525 | if (r < 0) |
1526 | return log_error_errno(r, "Failed to read CA certificate file '%s': %m", | |
1527 | arg_trust ?: TRUST_FILE); | |
ad95fd1d ZJS |
1528 | } |
1529 | ||
1530 | return 0; | |
1531 | } | |
1532 | ||
fdfccdbc ZJS |
1533 | int main(int argc, char **argv) { |
1534 | RemoteServer s = {}; | |
9ff48d09 | 1535 | int r; |
ad95fd1d | 1536 | _cleanup_free_ char *key = NULL, *cert = NULL, *trust = NULL; |
fdfccdbc | 1537 | |
fdfccdbc ZJS |
1538 | log_show_color(true); |
1539 | log_parse_environment(); | |
1540 | ||
ad95fd1d ZJS |
1541 | r = parse_config(); |
1542 | if (r < 0) | |
1543 | return EXIT_FAILURE; | |
1544 | ||
fdfccdbc ZJS |
1545 | r = parse_argv(argc, argv); |
1546 | if (r <= 0) | |
1547 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
1548 | ||
d357562c ZJS |
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 | } | |
cc64d017 | 1555 | |
8201af08 ZJS |
1556 | if (arg_listen_https || https_socket >= 0) |
1557 | if (load_certificates(&key, &cert, &trust) < 0) | |
1558 | return EXIT_FAILURE; | |
ad95fd1d ZJS |
1559 | |
1560 | if (remoteserver_init(&s, key, cert, trust) < 0) | |
fdfccdbc ZJS |
1561 | return EXIT_FAILURE; |
1562 | ||
a7736b14 ZJS |
1563 | r = sd_event_set_watchdog(s.events, true); |
1564 | if (r < 0) | |
da927ba9 | 1565 | log_error_errno(r, "Failed to enable watchdog: %m"); |
a7736b14 ZJS |
1566 | else |
1567 | log_debug("Watchdog is %s.", r > 0 ? "enabled" : "disabled"); | |
a3152e76 | 1568 | |
de0671ee ZJS |
1569 | log_debug("%s running as pid "PID_FMT, |
1570 | program_invocation_short_name, getpid()); | |
fdfccdbc ZJS |
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) { | |
da927ba9 | 1584 | log_error_errno(r, "Failed to run event loop: %m"); |
fdfccdbc ZJS |
1585 | break; |
1586 | } | |
1587 | } | |
1588 | ||
af4ec430 LP |
1589 | sd_notifyf(false, |
1590 | "STOPPING=1\n" | |
1591 | "STATUS=Shutting down after writing %" PRIu64 " entries...", s.event_count); | |
9ff48d09 | 1592 | log_info("Finishing after writing %" PRIu64 " entries", s.event_count); |
fdfccdbc | 1593 | |
af4ec430 | 1594 | server_destroy(&s); |
fdfccdbc | 1595 | |
ad95fd1d ZJS |
1596 | free(arg_key); |
1597 | free(arg_cert); | |
1598 | free(arg_trust); | |
1599 | ||
9ff48d09 | 1600 | return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
fdfccdbc | 1601 | } |