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