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