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