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