]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-upload.c
3952c0fac83498cf15ed442a206a145b488de6ad
[thirdparty/systemd.git] / src / journal-remote / journal-upload.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2014 Zbigniew Jędrzejewski-Szmek
4 ***/
5
6 #include <curl/curl.h>
7 #include <fcntl.h>
8 #include <getopt.h>
9 #include <stdio.h>
10 #include <sys/stat.h>
11
12 #include "sd-daemon.h"
13
14 #include "alloc-util.h"
15 #include "conf-parser.h"
16 #include "def.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "format-util.h"
20 #include "glob-util.h"
21 #include "journal-upload.h"
22 #include "log.h"
23 #include "mkdir.h"
24 #include "parse-util.h"
25 #include "process-util.h"
26 #include "sigbus.h"
27 #include "signal-util.h"
28 #include "string-util.h"
29 #include "util.h"
30
31 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
32 #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
33 #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
34 #define DEFAULT_PORT 19532
35
36 static const char* arg_url = NULL;
37 static const char *arg_key = NULL;
38 static const char *arg_cert = NULL;
39 static const char *arg_trust = NULL;
40 static const char *arg_directory = NULL;
41 static char **arg_file = NULL;
42 static const char *arg_cursor = NULL;
43 static bool arg_after_cursor = false;
44 static int arg_journal_type = 0;
45 static const char *arg_machine = NULL;
46 static bool arg_merge = false;
47 static int arg_follow = -1;
48 static const char *arg_save_state = NULL;
49
50 static void close_fd_input(Uploader *u);
51
52 #define SERVER_ANSWER_KEEP 2048
53
54 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
55
56 #define easy_setopt(curl, opt, value, level, cmd) \
57 do { \
58 code = curl_easy_setopt(curl, opt, value); \
59 if (code) { \
60 log_full(level, \
61 "curl_easy_setopt " #opt " failed: %s", \
62 curl_easy_strerror(code)); \
63 cmd; \
64 } \
65 } while (0)
66
67 static size_t output_callback(char *buf,
68 size_t size,
69 size_t nmemb,
70 void *userp) {
71 Uploader *u = userp;
72
73 assert(u);
74
75 log_debug("The server answers (%zu bytes): %.*s",
76 size*nmemb, (int)(size*nmemb), buf);
77
78 if (nmemb && !u->answer) {
79 u->answer = strndup(buf, size*nmemb);
80 if (!u->answer)
81 log_warning_errno(ENOMEM, "Failed to store server answer (%zu bytes): %m",
82 size*nmemb);
83 }
84
85 return size * nmemb;
86 }
87
88 static int check_cursor_updating(Uploader *u) {
89 _cleanup_free_ char *temp_path = NULL;
90 _cleanup_fclose_ FILE *f = NULL;
91 int r;
92
93 if (!u->state_file)
94 return 0;
95
96 r = mkdir_parents(u->state_file, 0755);
97 if (r < 0)
98 return log_error_errno(r, "Cannot create parent directory of state file %s: %m",
99 u->state_file);
100
101 r = fopen_temporary(u->state_file, &f, &temp_path);
102 if (r < 0)
103 return log_error_errno(r, "Cannot save state to %s: %m",
104 u->state_file);
105 unlink(temp_path);
106
107 return 0;
108 }
109
110 static int update_cursor_state(Uploader *u) {
111 _cleanup_free_ char *temp_path = NULL;
112 _cleanup_fclose_ FILE *f = NULL;
113 int r;
114
115 if (!u->state_file || !u->last_cursor)
116 return 0;
117
118 r = fopen_temporary(u->state_file, &f, &temp_path);
119 if (r < 0)
120 goto fail;
121
122 fprintf(f,
123 "# This is private data. Do not parse.\n"
124 "LAST_CURSOR=%s\n",
125 u->last_cursor);
126
127 r = fflush_and_check(f);
128 if (r < 0)
129 goto fail;
130
131 if (rename(temp_path, u->state_file) < 0) {
132 r = -errno;
133 goto fail;
134 }
135
136 return 0;
137
138 fail:
139 if (temp_path)
140 (void) unlink(temp_path);
141
142 (void) unlink(u->state_file);
143
144 return log_error_errno(r, "Failed to save state %s: %m", u->state_file);
145 }
146
147 static int load_cursor_state(Uploader *u) {
148 int r;
149
150 if (!u->state_file)
151 return 0;
152
153 r = parse_env_file(NULL, u->state_file, NEWLINE,
154 "LAST_CURSOR", &u->last_cursor,
155 NULL);
156
157 if (r == -ENOENT)
158 log_debug("State file %s is not present.", u->state_file);
159 else if (r < 0)
160 return log_error_errno(r, "Failed to read state file %s: %m",
161 u->state_file);
162 else
163 log_debug("Last cursor was %s", u->last_cursor);
164
165 return 0;
166 }
167
168 int start_upload(Uploader *u,
169 size_t (*input_callback)(void *ptr,
170 size_t size,
171 size_t nmemb,
172 void *userdata),
173 void *data) {
174 CURLcode code;
175
176 assert(u);
177 assert(input_callback);
178
179 if (!u->header) {
180 struct curl_slist *h;
181
182 h = curl_slist_append(NULL, "Content-Type: application/vnd.fdo.journal");
183 if (!h)
184 return log_oom();
185
186 h = curl_slist_append(h, "Transfer-Encoding: chunked");
187 if (!h) {
188 curl_slist_free_all(h);
189 return log_oom();
190 }
191
192 h = curl_slist_append(h, "Accept: text/plain");
193 if (!h) {
194 curl_slist_free_all(h);
195 return log_oom();
196 }
197
198 u->header = h;
199 }
200
201 if (!u->easy) {
202 CURL *curl;
203
204 curl = curl_easy_init();
205 if (!curl) {
206 log_error("Call to curl_easy_init failed.");
207 return -ENOSR;
208 }
209
210 /* tell it to POST to the URL */
211 easy_setopt(curl, CURLOPT_POST, 1L,
212 LOG_ERR, return -EXFULL);
213
214 easy_setopt(curl, CURLOPT_ERRORBUFFER, u->error,
215 LOG_ERR, return -EXFULL);
216
217 /* set where to write to */
218 easy_setopt(curl, CURLOPT_WRITEFUNCTION, output_callback,
219 LOG_ERR, return -EXFULL);
220
221 easy_setopt(curl, CURLOPT_WRITEDATA, data,
222 LOG_ERR, return -EXFULL);
223
224 /* set where to read from */
225 easy_setopt(curl, CURLOPT_READFUNCTION, input_callback,
226 LOG_ERR, return -EXFULL);
227
228 easy_setopt(curl, CURLOPT_READDATA, data,
229 LOG_ERR, return -EXFULL);
230
231 /* use our special own mime type and chunked transfer */
232 easy_setopt(curl, CURLOPT_HTTPHEADER, u->header,
233 LOG_ERR, return -EXFULL);
234
235 if (DEBUG_LOGGING)
236 /* enable verbose for easier tracing */
237 easy_setopt(curl, CURLOPT_VERBOSE, 1L, LOG_WARNING, );
238
239 easy_setopt(curl, CURLOPT_USERAGENT,
240 "systemd-journal-upload " PACKAGE_STRING,
241 LOG_WARNING, );
242
243 if (arg_key || startswith(u->url, "https://")) {
244 easy_setopt(curl, CURLOPT_SSLKEY, arg_key ?: PRIV_KEY_FILE,
245 LOG_ERR, return -EXFULL);
246 easy_setopt(curl, CURLOPT_SSLCERT, arg_cert ?: CERT_FILE,
247 LOG_ERR, return -EXFULL);
248 }
249
250 if (streq_ptr(arg_trust, "all"))
251 easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0,
252 LOG_ERR, return -EUCLEAN);
253 else if (arg_trust || startswith(u->url, "https://"))
254 easy_setopt(curl, CURLOPT_CAINFO, arg_trust ?: TRUST_FILE,
255 LOG_ERR, return -EXFULL);
256
257 if (arg_key || arg_trust)
258 easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1,
259 LOG_WARNING, );
260
261 u->easy = curl;
262 } else {
263 /* truncate the potential old error message */
264 u->error[0] = '\0';
265
266 free(u->answer);
267 u->answer = 0;
268 }
269
270 /* upload to this place */
271 code = curl_easy_setopt(u->easy, CURLOPT_URL, u->url);
272 if (code) {
273 log_error("curl_easy_setopt CURLOPT_URL failed: %s",
274 curl_easy_strerror(code));
275 return -EXFULL;
276 }
277
278 u->uploading = true;
279
280 return 0;
281 }
282
283 static size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
284 Uploader *u = userp;
285
286 ssize_t r;
287
288 assert(u);
289 assert(nmemb <= SSIZE_MAX / size);
290
291 if (u->input < 0)
292 return 0;
293
294 r = read(u->input, buf, size * nmemb);
295 log_debug("%s: allowed %zu, read %zd", __func__, size*nmemb, r);
296
297 if (r > 0)
298 return r;
299
300 u->uploading = false;
301 if (r == 0) {
302 log_debug("Reached EOF");
303 close_fd_input(u);
304 return 0;
305 } else {
306 log_error_errno(errno, "Aborting transfer after read error on input: %m.");
307 return CURL_READFUNC_ABORT;
308 }
309 }
310
311 static void close_fd_input(Uploader *u) {
312 assert(u);
313
314 u->input = safe_close(u->input);
315 u->timeout = 0;
316 }
317
318 static int dispatch_fd_input(sd_event_source *event,
319 int fd,
320 uint32_t revents,
321 void *userp) {
322 Uploader *u = userp;
323
324 assert(u);
325 assert(fd >= 0);
326
327 if (revents & EPOLLHUP) {
328 log_debug("Received HUP");
329 close_fd_input(u);
330 return 0;
331 }
332
333 if (!(revents & EPOLLIN)) {
334 log_warning("Unexpected poll event %"PRIu32".", revents);
335 return -EINVAL;
336 }
337
338 if (u->uploading) {
339 log_warning("dispatch_fd_input called when uploading, ignoring.");
340 return 0;
341 }
342
343 return start_upload(u, fd_input_callback, u);
344 }
345
346 static int open_file_for_upload(Uploader *u, const char *filename) {
347 int fd, r = 0;
348
349 if (streq(filename, "-"))
350 fd = STDIN_FILENO;
351 else {
352 fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
353 if (fd < 0)
354 return log_error_errno(errno, "Failed to open %s: %m", filename);
355 }
356
357 u->input = fd;
358
359 if (arg_follow) {
360 r = sd_event_add_io(u->events, &u->input_event,
361 fd, EPOLLIN, dispatch_fd_input, u);
362 if (r < 0) {
363 if (r != -EPERM || arg_follow > 0)
364 return log_error_errno(r, "Failed to register input event: %m");
365
366 /* Normal files should just be consumed without polling. */
367 r = start_upload(u, fd_input_callback, u);
368 }
369 }
370
371 return r;
372 }
373
374 static int dispatch_sigterm(sd_event_source *event,
375 const struct signalfd_siginfo *si,
376 void *userdata) {
377 Uploader *u = userdata;
378
379 assert(u);
380
381 log_received_signal(LOG_INFO, si);
382
383 close_fd_input(u);
384 close_journal_input(u);
385
386 sd_event_exit(u->events, 0);
387 return 0;
388 }
389
390 static int setup_signals(Uploader *u) {
391 int r;
392
393 assert(u);
394
395 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, -1) >= 0);
396
397 r = sd_event_add_signal(u->events, &u->sigterm_event, SIGTERM, dispatch_sigterm, u);
398 if (r < 0)
399 return r;
400
401 r = sd_event_add_signal(u->events, &u->sigint_event, SIGINT, dispatch_sigterm, u);
402 if (r < 0)
403 return r;
404
405 return 0;
406 }
407
408 static int setup_uploader(Uploader *u, const char *url, const char *state_file) {
409 int r;
410 const char *host, *proto = "";
411
412 assert(u);
413 assert(url);
414
415 memzero(u, sizeof(Uploader));
416 u->input = -1;
417
418 if (!(host = startswith(url, "http://")) && !(host = startswith(url, "https://"))) {
419 host = url;
420 proto = "https://";
421 }
422
423 if (strchr(host, ':'))
424 u->url = strjoin(proto, url, "/upload");
425 else {
426 char *t;
427 size_t x;
428
429 t = strdupa(url);
430 x = strlen(t);
431 while (x > 0 && t[x - 1] == '/')
432 t[x - 1] = '\0';
433
434 u->url = strjoin(proto, t, ":" STRINGIFY(DEFAULT_PORT), "/upload");
435 }
436 if (!u->url)
437 return log_oom();
438
439 u->state_file = state_file;
440
441 r = sd_event_default(&u->events);
442 if (r < 0)
443 return log_error_errno(r, "sd_event_default failed: %m");
444
445 r = setup_signals(u);
446 if (r < 0)
447 return log_error_errno(r, "Failed to set up signals: %m");
448
449 (void) sd_watchdog_enabled(false, &u->watchdog_usec);
450
451 return load_cursor_state(u);
452 }
453
454 static void destroy_uploader(Uploader *u) {
455 assert(u);
456
457 curl_easy_cleanup(u->easy);
458 curl_slist_free_all(u->header);
459 free(u->answer);
460
461 free(u->last_cursor);
462 free(u->current_cursor);
463
464 free(u->url);
465
466 u->input_event = sd_event_source_unref(u->input_event);
467
468 close_fd_input(u);
469 close_journal_input(u);
470
471 sd_event_source_unref(u->sigterm_event);
472 sd_event_source_unref(u->sigint_event);
473 sd_event_unref(u->events);
474 }
475
476 static int perform_upload(Uploader *u) {
477 CURLcode code;
478 long status;
479
480 assert(u);
481
482 u->watchdog_timestamp = now(CLOCK_MONOTONIC);
483 code = curl_easy_perform(u->easy);
484 if (code) {
485 if (u->error[0])
486 log_error("Upload to %s failed: %.*s",
487 u->url, (int) sizeof(u->error), u->error);
488 else
489 log_error("Upload to %s failed: %s",
490 u->url, curl_easy_strerror(code));
491 return -EIO;
492 }
493
494 code = curl_easy_getinfo(u->easy, CURLINFO_RESPONSE_CODE, &status);
495 if (code) {
496 log_error("Failed to retrieve response code: %s",
497 curl_easy_strerror(code));
498 return -EUCLEAN;
499 }
500
501 if (status >= 300) {
502 log_error("Upload to %s failed with code %ld: %s",
503 u->url, status, strna(u->answer));
504 return -EIO;
505 } else if (status < 200) {
506 log_error("Upload to %s finished with unexpected code %ld: %s",
507 u->url, status, strna(u->answer));
508 return -EIO;
509 } else
510 log_debug("Upload finished successfully with code %ld: %s",
511 status, strna(u->answer));
512
513 free_and_replace(u->last_cursor, u->current_cursor);
514
515 return update_cursor_state(u);
516 }
517
518 static int parse_config(void) {
519 const ConfigTableItem items[] = {
520 { "Upload", "URL", config_parse_string, 0, &arg_url },
521 { "Upload", "ServerKeyFile", config_parse_path, 0, &arg_key },
522 { "Upload", "ServerCertificateFile", config_parse_path, 0, &arg_cert },
523 { "Upload", "TrustedCertificateFile", config_parse_path, 0, &arg_trust },
524 {}};
525
526 return config_parse_many_nulstr(PKGSYSCONFDIR "/journal-upload.conf",
527 CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
528 "Upload\0", config_item_table_lookup, items,
529 CONFIG_PARSE_WARN, NULL);
530 }
531
532 static void help(void) {
533 printf("%s -u URL {FILE|-}...\n\n"
534 "Upload journal events to a remote server.\n\n"
535 " -h --help Show this help\n"
536 " --version Show package version\n"
537 " -u --url=URL Upload to this address (default port "
538 STRINGIFY(DEFAULT_PORT) ")\n"
539 " --key=FILENAME Specify key in PEM format (default:\n"
540 " \"" PRIV_KEY_FILE "\")\n"
541 " --cert=FILENAME Specify certificate in PEM format (default:\n"
542 " \"" CERT_FILE "\")\n"
543 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
544 " \"" TRUST_FILE "\")\n"
545 " --system Use the system journal\n"
546 " --user Use the user journal for the current user\n"
547 " -m --merge Use all available journals\n"
548 " -M --machine=CONTAINER Operate on local container\n"
549 " -D --directory=PATH Use journal files from directory\n"
550 " --file=PATH Use this journal file\n"
551 " --cursor=CURSOR Start at the specified cursor\n"
552 " --after-cursor=CURSOR Start after the specified cursor\n"
553 " --follow[=BOOL] Do [not] wait for input\n"
554 " --save-state[=FILE] Save uploaded cursors (default \n"
555 " " STATE_FILE ")\n"
556 , program_invocation_short_name);
557 }
558
559 static int parse_argv(int argc, char *argv[]) {
560 enum {
561 ARG_VERSION = 0x100,
562 ARG_KEY,
563 ARG_CERT,
564 ARG_TRUST,
565 ARG_USER,
566 ARG_SYSTEM,
567 ARG_FILE,
568 ARG_CURSOR,
569 ARG_AFTER_CURSOR,
570 ARG_FOLLOW,
571 ARG_SAVE_STATE,
572 };
573
574 static const struct option options[] = {
575 { "help", no_argument, NULL, 'h' },
576 { "version", no_argument, NULL, ARG_VERSION },
577 { "url", required_argument, NULL, 'u' },
578 { "key", required_argument, NULL, ARG_KEY },
579 { "cert", required_argument, NULL, ARG_CERT },
580 { "trust", required_argument, NULL, ARG_TRUST },
581 { "system", no_argument, NULL, ARG_SYSTEM },
582 { "user", no_argument, NULL, ARG_USER },
583 { "merge", no_argument, NULL, 'm' },
584 { "machine", required_argument, NULL, 'M' },
585 { "directory", required_argument, NULL, 'D' },
586 { "file", required_argument, NULL, ARG_FILE },
587 { "cursor", required_argument, NULL, ARG_CURSOR },
588 { "after-cursor", required_argument, NULL, ARG_AFTER_CURSOR },
589 { "follow", optional_argument, NULL, ARG_FOLLOW },
590 { "save-state", optional_argument, NULL, ARG_SAVE_STATE },
591 {}
592 };
593
594 int c, r;
595
596 assert(argc >= 0);
597 assert(argv);
598
599 opterr = 0;
600
601 while ((c = getopt_long(argc, argv, "hu:mM:D:", options, NULL)) >= 0)
602 switch(c) {
603 case 'h':
604 help();
605 return 0 /* done */;
606
607 case ARG_VERSION:
608 return version();
609
610 case 'u':
611 if (arg_url) {
612 log_error("cannot use more than one --url");
613 return -EINVAL;
614 }
615
616 arg_url = optarg;
617 break;
618
619 case ARG_KEY:
620 if (arg_key) {
621 log_error("cannot use more than one --key");
622 return -EINVAL;
623 }
624
625 arg_key = optarg;
626 break;
627
628 case ARG_CERT:
629 if (arg_cert) {
630 log_error("cannot use more than one --cert");
631 return -EINVAL;
632 }
633
634 arg_cert = optarg;
635 break;
636
637 case ARG_TRUST:
638 if (arg_trust) {
639 log_error("cannot use more than one --trust");
640 return -EINVAL;
641 }
642
643 arg_trust = optarg;
644 break;
645
646 case ARG_SYSTEM:
647 arg_journal_type |= SD_JOURNAL_SYSTEM;
648 break;
649
650 case ARG_USER:
651 arg_journal_type |= SD_JOURNAL_CURRENT_USER;
652 break;
653
654 case 'm':
655 arg_merge = true;
656 break;
657
658 case 'M':
659 if (arg_machine) {
660 log_error("cannot use more than one --machine/-M");
661 return -EINVAL;
662 }
663
664 arg_machine = optarg;
665 break;
666
667 case 'D':
668 if (arg_directory) {
669 log_error("cannot use more than one --directory/-D");
670 return -EINVAL;
671 }
672
673 arg_directory = optarg;
674 break;
675
676 case ARG_FILE:
677 r = glob_extend(&arg_file, optarg);
678 if (r < 0)
679 return log_error_errno(r, "Failed to add paths: %m");
680 break;
681
682 case ARG_CURSOR:
683 if (arg_cursor) {
684 log_error("cannot use more than one --cursor/--after-cursor");
685 return -EINVAL;
686 }
687
688 arg_cursor = optarg;
689 break;
690
691 case ARG_AFTER_CURSOR:
692 if (arg_cursor) {
693 log_error("cannot use more than one --cursor/--after-cursor");
694 return -EINVAL;
695 }
696
697 arg_cursor = optarg;
698 arg_after_cursor = true;
699 break;
700
701 case ARG_FOLLOW:
702 if (optarg) {
703 r = parse_boolean(optarg);
704 if (r < 0) {
705 log_error("Failed to parse --follow= parameter.");
706 return -EINVAL;
707 }
708
709 arg_follow = !!r;
710 } else
711 arg_follow = true;
712
713 break;
714
715 case ARG_SAVE_STATE:
716 arg_save_state = optarg ?: STATE_FILE;
717 break;
718
719 case '?':
720 log_error("Unknown option %s.", argv[optind-1]);
721 return -EINVAL;
722
723 case ':':
724 log_error("Missing argument to %s.", argv[optind-1]);
725 return -EINVAL;
726
727 default:
728 assert_not_reached("Unhandled option code.");
729 }
730
731 if (!arg_url) {
732 log_error("Required --url/-u option missing.");
733 return -EINVAL;
734 }
735
736 if (!!arg_key != !!arg_cert) {
737 log_error("Options --key and --cert must be used together.");
738 return -EINVAL;
739 }
740
741 if (optind < argc && (arg_directory || arg_file || arg_machine || arg_journal_type)) {
742 log_error("Input arguments make no sense with journal input.");
743 return -EINVAL;
744 }
745
746 return 1;
747 }
748
749 static int open_journal(sd_journal **j) {
750 int r;
751
752 if (arg_directory)
753 r = sd_journal_open_directory(j, arg_directory, arg_journal_type);
754 else if (arg_file)
755 r = sd_journal_open_files(j, (const char**) arg_file, 0);
756 else if (arg_machine)
757 r = sd_journal_open_container(j, arg_machine, 0);
758 else
759 r = sd_journal_open(j, !arg_merge*SD_JOURNAL_LOCAL_ONLY + arg_journal_type);
760 if (r < 0)
761 log_error_errno(r, "Failed to open %s: %m",
762 arg_directory ? arg_directory : arg_file ? "files" : "journal");
763 return r;
764 }
765
766 int main(int argc, char **argv) {
767 Uploader u;
768 int r;
769 bool use_journal;
770
771 log_show_color(true);
772 log_parse_environment();
773
774 r = parse_config();
775 if (r < 0)
776 goto finish;
777
778 r = parse_argv(argc, argv);
779 if (r <= 0)
780 goto finish;
781
782 sigbus_install();
783
784 r = setup_uploader(&u, arg_url, arg_save_state);
785 if (r < 0)
786 goto cleanup;
787
788 sd_event_set_watchdog(u.events, true);
789
790 r = check_cursor_updating(&u);
791 if (r < 0)
792 goto cleanup;
793
794 log_debug("%s running as pid "PID_FMT,
795 program_invocation_short_name, getpid_cached());
796
797 use_journal = optind >= argc;
798 if (use_journal) {
799 sd_journal *j;
800 r = open_journal(&j);
801 if (r < 0)
802 goto finish;
803 r = open_journal_for_upload(&u, j,
804 arg_cursor ?: u.last_cursor,
805 arg_cursor ? arg_after_cursor : true,
806 !!arg_follow);
807 if (r < 0)
808 goto finish;
809 }
810
811 sd_notify(false,
812 "READY=1\n"
813 "STATUS=Processing input...");
814
815 for (;;) {
816 r = sd_event_get_state(u.events);
817 if (r < 0)
818 break;
819 if (r == SD_EVENT_FINISHED)
820 break;
821
822 if (use_journal) {
823 if (!u.journal)
824 break;
825
826 r = check_journal_input(&u);
827 } else if (u.input < 0 && !use_journal) {
828 if (optind >= argc)
829 break;
830
831 log_debug("Using %s as input.", argv[optind]);
832 r = open_file_for_upload(&u, argv[optind++]);
833 }
834 if (r < 0)
835 goto cleanup;
836
837 if (u.uploading) {
838 r = perform_upload(&u);
839 if (r < 0)
840 break;
841 }
842
843 r = sd_event_run(u.events, u.timeout);
844 if (r < 0) {
845 log_error_errno(r, "Failed to run event loop: %m");
846 break;
847 }
848 }
849
850 cleanup:
851 sd_notify(false,
852 "STOPPING=1\n"
853 "STATUS=Shutting down...");
854
855 destroy_uploader(&u);
856
857 finish:
858 return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
859 }