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