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