]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal-remote/journal-upload.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / journal-remote / journal-upload.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3d090cc6 2/***
96b2fb93 3 Copyright © 2014 Zbigniew Jędrzejewski-Szmek
3d090cc6
ZJS
4***/
5
07630cea 6#include <curl/curl.h>
3d090cc6
ZJS
7#include <fcntl.h>
8#include <getopt.h>
3f6fd1ba
LP
9#include <stdio.h>
10#include <sys/stat.h>
3d090cc6
ZJS
11
12#include "sd-daemon.h"
3f6fd1ba 13
b5efdb8a 14#include "alloc-util.h"
3f6fd1ba 15#include "conf-parser.h"
a0f29c76 16#include "def.h"
3ffd4af2 17#include "fd-util.h"
722b6795 18#include "fileio.h"
f97b34a6 19#include "format-util.h"
7d50b32a 20#include "glob-util.h"
3ffd4af2 21#include "journal-upload.h"
3f6fd1ba 22#include "log.h"
d71839af 23#include "mkdir.h"
6bedfcbb 24#include "parse-util.h"
dccca82b 25#include "process-util.h"
2cf4172a 26#include "sigbus.h"
24882e06 27#include "signal-util.h"
07630cea 28#include "string-util.h"
3f6fd1ba 29#include "util.h"
3d090cc6 30
799a8f39
ZJS
31#define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
32#define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
33#define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
50a0b071 34#define DEFAULT_PORT 19532
29fc0ddc 35
2cf4172a 36static const char* arg_url = NULL;
7449bc1f
ZJS
37static const char *arg_key = NULL;
38static const char *arg_cert = NULL;
39static const char *arg_trust = NULL;
eacbb4d3
ZJS
40static const char *arg_directory = NULL;
41static char **arg_file = NULL;
42static const char *arg_cursor = NULL;
43static bool arg_after_cursor = false;
44static int arg_journal_type = 0;
45static const char *arg_machine = NULL;
46static bool arg_merge = false;
47static int arg_follow = -1;
722b6795 48static const char *arg_save_state = NULL;
eacbb4d3 49
2cf4172a
LP
50static void close_fd_input(Uploader *u);
51
eacbb4d3
ZJS
52#define SERVER_ANSWER_KEEP 2048
53
722b6795
ZJS
54#define STATE_FILE "/var/lib/systemd/journal-upload/state"
55
3d090cc6 56#define easy_setopt(curl, opt, value, level, cmd) \
8847551b 57 do { \
3d090cc6
ZJS
58 code = curl_easy_setopt(curl, opt, value); \
59 if (code) { \
60 log_full(level, \
61 "curl_easy_setopt " #opt " failed: %s", \
62 curl_easy_strerror(code)); \
63 cmd; \
64 } \
9ed794a3 65 } while (0)
3d090cc6 66
eacbb4d3
ZJS
67static size_t output_callback(char *buf,
68 size_t size,
69 size_t nmemb,
70 void *userp) {
71 Uploader *u = userp;
72
73 assert(u);
74
75 log_debug("The server answers (%zu bytes): %.*s",
76 size*nmemb, (int)(size*nmemb), buf);
77
78 if (nmemb && !u->answer) {
79 u->answer = strndup(buf, size*nmemb);
80 if (!u->answer)
c33b3297
MS
81 log_warning_errno(ENOMEM, "Failed to store server answer (%zu bytes): %m",
82 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
1a5a177e 153 r = parse_env_file(NULL, u->state_file, NEWLINE,
722b6795
ZJS
154 "LAST_CURSOR", &u->last_cursor,
155 NULL);
156
36d4739a
ZJS
157 if (r == -ENOENT)
158 log_debug("State file %s is not present.", u->state_file);
eb56eb9b
MS
159 else if (r < 0)
160 return log_error_errno(r, "Failed to read state file %s: %m",
161 u->state_file);
162 else
36d4739a 163 log_debug("Last cursor was %s", u->last_cursor);
722b6795
ZJS
164
165 return 0;
166}
167
3d090cc6
ZJS
168int start_upload(Uploader *u,
169 size_t (*input_callback)(void *ptr,
170 size_t size,
171 size_t nmemb,
172 void *userdata),
173 void *data) {
174 CURLcode code;
175
176 assert(u);
177 assert(input_callback);
178
179 if (!u->header) {
180 struct curl_slist *h;
181
182 h = curl_slist_append(NULL, "Content-Type: application/vnd.fdo.journal");
183 if (!h)
184 return log_oom();
185
186 h = curl_slist_append(h, "Transfer-Encoding: chunked");
187 if (!h) {
188 curl_slist_free_all(h);
189 return log_oom();
190 }
191
192 h = curl_slist_append(h, "Accept: text/plain");
193 if (!h) {
194 curl_slist_free_all(h);
195 return log_oom();
196 }
197
198 u->header = h;
199 }
200
201 if (!u->easy) {
202 CURL *curl;
203
204 curl = curl_easy_init();
205 if (!curl) {
206 log_error("Call to curl_easy_init failed.");
207 return -ENOSR;
208 }
209
210 /* tell it to POST to the URL */
211 easy_setopt(curl, CURLOPT_POST, 1L,
212 LOG_ERR, return -EXFULL);
213
b88a40a7 214 easy_setopt(curl, CURLOPT_ERRORBUFFER, u->error,
eacbb4d3
ZJS
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
3d090cc6
ZJS
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
f1d34068 235 if (DEBUG_LOGGING)
5dabb1e0
ZJS
236 /* enable verbose for easier tracing */
237 easy_setopt(curl, CURLOPT_VERBOSE, 1L, LOG_WARNING, );
3d090cc6
ZJS
238
239 easy_setopt(curl, CURLOPT_USERAGENT,
240 "systemd-journal-upload " PACKAGE_STRING,
241 LOG_WARNING, );
242
29fc0ddc 243 if (arg_key || startswith(u->url, "https://")) {
799a8f39 244 easy_setopt(curl, CURLOPT_SSLKEY, arg_key ?: PRIV_KEY_FILE,
7449bc1f 245 LOG_ERR, return -EXFULL);
29fc0ddc 246 easy_setopt(curl, CURLOPT_SSLCERT, arg_cert ?: CERT_FILE,
7449bc1f
ZJS
247 LOG_ERR, return -EXFULL);
248 }
249
8847551b
ZJS
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://"))
29fc0ddc 254 easy_setopt(curl, CURLOPT_CAINFO, arg_trust ?: TRUST_FILE,
7449bc1f
ZJS
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
3d090cc6 261 u->easy = curl;
eacbb4d3
ZJS
262 } else {
263 /* truncate the potential old error message */
264 u->error[0] = '\0';
265
266 free(u->answer);
267 u->answer = 0;
3d090cc6
ZJS
268 }
269
270 /* upload to this place */
271 code = curl_easy_setopt(u->easy, CURLOPT_URL, u->url);
272 if (code) {
273 log_error("curl_easy_setopt CURLOPT_URL failed: %s",
274 curl_easy_strerror(code));
275 return -EXFULL;
276 }
277
278 u->uploading = true;
279
280 return 0;
281}
282
283static size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
284 Uploader *u = userp;
285
286 ssize_t r;
287
288 assert(u);
289 assert(nmemb <= SSIZE_MAX / size);
290
291 if (u->input < 0)
292 return 0;
293
294 r = read(u->input, buf, size * nmemb);
1fa2f38f 295 log_debug("%s: allowed %zu, read %zd", __func__, size*nmemb, r);
3d090cc6
ZJS
296
297 if (r > 0)
298 return r;
299
300 u->uploading = false;
301 if (r == 0) {
302 log_debug("Reached EOF");
303 close_fd_input(u);
304 return 0;
305 } else {
56f64d95 306 log_error_errno(errno, "Aborting transfer after read error on input: %m.");
3d090cc6
ZJS
307 return CURL_READFUNC_ABORT;
308 }
309}
310
311static void close_fd_input(Uploader *u) {
312 assert(u);
313
3ccf323d 314 u->input = safe_close(u->input);
eacbb4d3 315 u->timeout = 0;
3d090cc6
ZJS
316}
317
318static int dispatch_fd_input(sd_event_source *event,
319 int fd,
320 uint32_t revents,
321 void *userp) {
322 Uploader *u = userp;
323
324 assert(u);
3d090cc6
ZJS
325 assert(fd >= 0);
326
8201af08
ZJS
327 if (revents & EPOLLHUP) {
328 log_debug("Received HUP");
329 close_fd_input(u);
330 return 0;
331 }
332
333 if (!(revents & EPOLLIN)) {
334 log_warning("Unexpected poll event %"PRIu32".", revents);
335 return -EINVAL;
336 }
337
3d090cc6
ZJS
338 if (u->uploading) {
339 log_warning("dispatch_fd_input called when uploading, ignoring.");
340 return 0;
341 }
342
343 return start_upload(u, fd_input_callback, u);
344}
345
346static int open_file_for_upload(Uploader *u, const char *filename) {
e1ad6e24 347 int fd, r = 0;
3d090cc6
ZJS
348
349 if (streq(filename, "-"))
350 fd = STDIN_FILENO;
351 else {
352 fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
4a62c710
MS
353 if (fd < 0)
354 return log_error_errno(errno, "Failed to open %s: %m", filename);
3d090cc6
ZJS
355 }
356
357 u->input = fd;
358
eacbb4d3
ZJS
359 if (arg_follow) {
360 r = sd_event_add_io(u->events, &u->input_event,
361 fd, EPOLLIN, dispatch_fd_input, u);
362 if (r < 0) {
eb56eb9b
MS
363 if (r != -EPERM || arg_follow > 0)
364 return log_error_errno(r, "Failed to register input event: %m");
3d090cc6 365
eacbb4d3
ZJS
366 /* Normal files should just be consumed without polling. */
367 r = start_upload(u, fd_input_callback, u);
368 }
3d090cc6 369 }
eacbb4d3 370
3d090cc6
ZJS
371 return r;
372}
373
a3152e76
ZJS
374static int dispatch_sigterm(sd_event_source *event,
375 const struct signalfd_siginfo *si,
376 void *userdata) {
377 Uploader *u = userdata;
378
379 assert(u);
380
381 log_received_signal(LOG_INFO, si);
382
383 close_fd_input(u);
384 close_journal_input(u);
385
386 sd_event_exit(u->events, 0);
387 return 0;
388}
389
390static int setup_signals(Uploader *u) {
a3152e76
ZJS
391 int r;
392
393 assert(u);
394
72c0a2c2 395 assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, -1) >= 0);
a3152e76
ZJS
396
397 r = sd_event_add_signal(u->events, &u->sigterm_event, SIGTERM, dispatch_sigterm, u);
398 if (r < 0)
399 return r;
400
401 r = sd_event_add_signal(u->events, &u->sigint_event, SIGINT, dispatch_sigterm, u);
402 if (r < 0)
403 return r;
404
405 return 0;
406}
407
722b6795 408static int setup_uploader(Uploader *u, const char *url, const char *state_file) {
3d090cc6 409 int r;
50a0b071 410 const char *host, *proto = "";
3d090cc6
ZJS
411
412 assert(u);
413 assert(url);
414
415 memzero(u, sizeof(Uploader));
416 u->input = -1;
417
50a0b071
ZJS
418 if (!(host = startswith(url, "http://")) && !(host = startswith(url, "https://"))) {
419 host = url;
420 proto = "https://";
421 }
422
423 if (strchr(host, ':'))
605405c6 424 u->url = strjoin(proto, url, "/upload");
50a0b071
ZJS
425 else {
426 char *t;
427 size_t x;
5bc89120 428
50a0b071
ZJS
429 t = strdupa(url);
430 x = strlen(t);
431 while (x > 0 && t[x - 1] == '/')
432 t[x - 1] = '\0';
433
605405c6 434 u->url = strjoin(proto, t, ":" STRINGIFY(DEFAULT_PORT), "/upload");
50a0b071 435 }
5bc89120
ZJS
436 if (!u->url)
437 return log_oom();
438
722b6795 439 u->state_file = state_file;
3d090cc6
ZJS
440
441 r = sd_event_default(&u->events);
eb56eb9b
MS
442 if (r < 0)
443 return log_error_errno(r, "sd_event_default failed: %m");
3d090cc6 444
a3152e76 445 r = setup_signals(u);
eb56eb9b
MS
446 if (r < 0)
447 return log_error_errno(r, "Failed to set up signals: %m");
a3152e76 448
0aa176a7
ZJS
449 (void) sd_watchdog_enabled(false, &u->watchdog_usec);
450
722b6795 451 return load_cursor_state(u);
3d090cc6
ZJS
452}
453
454static void destroy_uploader(Uploader *u) {
455 assert(u);
456
457 curl_easy_cleanup(u->easy);
458 curl_slist_free_all(u->header);
eacbb4d3
ZJS
459 free(u->answer);
460
461 free(u->last_cursor);
722b6795 462 free(u->current_cursor);
3d090cc6 463
5bc89120
ZJS
464 free(u->url);
465
3d090cc6
ZJS
466 u->input_event = sd_event_source_unref(u->input_event);
467
468 close_fd_input(u);
eacbb4d3 469 close_journal_input(u);
3d090cc6 470
a3152e76
ZJS
471 sd_event_source_unref(u->sigterm_event);
472 sd_event_source_unref(u->sigint_event);
3d090cc6
ZJS
473 sd_event_unref(u->events);
474}
475
eacbb4d3
ZJS
476static int perform_upload(Uploader *u) {
477 CURLcode code;
478 long status;
479
480 assert(u);
481
0aa176a7 482 u->watchdog_timestamp = now(CLOCK_MONOTONIC);
eacbb4d3
ZJS
483 code = curl_easy_perform(u->easy);
484 if (code) {
30776485
ZJS
485 if (u->error[0])
486 log_error("Upload to %s failed: %.*s",
487 u->url, (int) sizeof(u->error), u->error);
488 else
489 log_error("Upload to %s failed: %s",
490 u->url, curl_easy_strerror(code));
eacbb4d3
ZJS
491 return -EIO;
492 }
493
494 code = curl_easy_getinfo(u->easy, CURLINFO_RESPONSE_CODE, &status);
495 if (code) {
496 log_error("Failed to retrieve response code: %s",
497 curl_easy_strerror(code));
498 return -EUCLEAN;
499 }
500
501 if (status >= 300) {
1fa2f38f 502 log_error("Upload to %s failed with code %ld: %s",
eacbb4d3
ZJS
503 u->url, status, strna(u->answer));
504 return -EIO;
505 } else if (status < 200) {
1fa2f38f 506 log_error("Upload to %s finished with unexpected code %ld: %s",
eacbb4d3
ZJS
507 u->url, status, strna(u->answer));
508 return -EIO;
509 } else
1fa2f38f 510 log_debug("Upload finished successfully with code %ld: %s",
eacbb4d3 511 status, strna(u->answer));
722b6795 512
3b319885 513 free_and_replace(u->last_cursor, u->current_cursor);
722b6795
ZJS
514
515 return update_cursor_state(u);
eacbb4d3
ZJS
516}
517
29fc0ddc
ZJS
518static 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 {}};
29fc0ddc 525
43688c49 526 return config_parse_many_nulstr(PKGSYSCONFDIR "/journal-upload.conf",
da412854
YW
527 CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
528 "Upload\0", config_item_table_lookup, items,
bcde742e 529 CONFIG_PARSE_WARN, NULL);
29fc0ddc
ZJS
530}
531
3d090cc6
ZJS
532static void help(void) {
533 printf("%s -u URL {FILE|-}...\n\n"
534 "Upload journal events to a remote server.\n\n"
dad29dff
LP
535 " -h --help Show this help\n"
536 " --version Show package version\n"
50a0b071
ZJS
537 " -u --url=URL Upload to this address (default port "
538 STRINGIFY(DEFAULT_PORT) ")\n"
1af719ed
ZJS
539 " --key=FILENAME Specify key in PEM format (default:\n"
540 " \"" PRIV_KEY_FILE "\")\n"
541 " --cert=FILENAME Specify certificate in PEM format (default:\n"
542 " \"" CERT_FILE "\")\n"
543 " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
544 " \"" TRUST_FILE "\")\n"
dad29dff
LP
545 " --system Use the system journal\n"
546 " --user Use the user journal for the current user\n"
547 " -m --merge Use all available journals\n"
548 " -M --machine=CONTAINER Operate on local container\n"
549 " -D --directory=PATH Use journal files from directory\n"
550 " --file=PATH Use this journal file\n"
551 " --cursor=CURSOR Start at the specified cursor\n"
552 " --after-cursor=CURSOR Start after the specified cursor\n"
553 " --follow[=BOOL] Do [not] wait for input\n"
554 " --save-state[=FILE] Save uploaded cursors (default \n"
555 " " STATE_FILE ")\n"
3d090cc6
ZJS
556 , program_invocation_short_name);
557}
558
559static int parse_argv(int argc, char *argv[]) {
560 enum {
561 ARG_VERSION = 0x100,
7449bc1f
ZJS
562 ARG_KEY,
563 ARG_CERT,
564 ARG_TRUST,
eacbb4d3
ZJS
565 ARG_USER,
566 ARG_SYSTEM,
567 ARG_FILE,
568 ARG_CURSOR,
569 ARG_AFTER_CURSOR,
570 ARG_FOLLOW,
722b6795 571 ARG_SAVE_STATE,
3d090cc6
ZJS
572 };
573
574 static const struct option options[] = {
575 { "help", no_argument, NULL, 'h' },
576 { "version", no_argument, NULL, ARG_VERSION },
577 { "url", required_argument, NULL, 'u' },
7449bc1f
ZJS
578 { "key", required_argument, NULL, ARG_KEY },
579 { "cert", required_argument, NULL, ARG_CERT },
580 { "trust", required_argument, NULL, ARG_TRUST },
eacbb4d3
ZJS
581 { "system", no_argument, NULL, ARG_SYSTEM },
582 { "user", no_argument, NULL, ARG_USER },
583 { "merge", no_argument, NULL, 'm' },
584 { "machine", required_argument, NULL, 'M' },
585 { "directory", required_argument, NULL, 'D' },
586 { "file", required_argument, NULL, ARG_FILE },
587 { "cursor", required_argument, NULL, ARG_CURSOR },
588 { "after-cursor", required_argument, NULL, ARG_AFTER_CURSOR },
dad29dff 589 { "follow", optional_argument, NULL, ARG_FOLLOW },
722b6795 590 { "save-state", optional_argument, NULL, ARG_SAVE_STATE },
3d090cc6
ZJS
591 {}
592 };
593
eacbb4d3 594 int c, r;
3d090cc6
ZJS
595
596 assert(argc >= 0);
597 assert(argv);
598
599 opterr = 0;
600
eacbb4d3 601 while ((c = getopt_long(argc, argv, "hu:mM:D:", options, NULL)) >= 0)
3d090cc6
ZJS
602 switch(c) {
603 case 'h':
604 help();
605 return 0 /* done */;
606
607 case ARG_VERSION:
3f6fd1ba 608 return version();
3d090cc6
ZJS
609
610 case 'u':
611 if (arg_url) {
612 log_error("cannot use more than one --url");
613 return -EINVAL;
614 }
615
616 arg_url = optarg;
617 break;
618
7449bc1f
ZJS
619 case ARG_KEY:
620 if (arg_key) {
621 log_error("cannot use more than one --key");
622 return -EINVAL;
623 }
624
625 arg_key = optarg;
626 break;
627
628 case ARG_CERT:
629 if (arg_cert) {
630 log_error("cannot use more than one --cert");
631 return -EINVAL;
632 }
633
634 arg_cert = optarg;
635 break;
636
637 case ARG_TRUST:
638 if (arg_trust) {
639 log_error("cannot use more than one --trust");
640 return -EINVAL;
641 }
642
643 arg_trust = optarg;
644 break;
645
eacbb4d3
ZJS
646 case ARG_SYSTEM:
647 arg_journal_type |= SD_JOURNAL_SYSTEM;
648 break;
649
650 case ARG_USER:
651 arg_journal_type |= SD_JOURNAL_CURRENT_USER;
652 break;
653
654 case 'm':
655 arg_merge = true;
656 break;
657
658 case 'M':
659 if (arg_machine) {
660 log_error("cannot use more than one --machine/-M");
661 return -EINVAL;
662 }
663
664 arg_machine = optarg;
665 break;
666
667 case 'D':
668 if (arg_directory) {
669 log_error("cannot use more than one --directory/-D");
670 return -EINVAL;
671 }
672
673 arg_directory = optarg;
674 break;
675
676 case ARG_FILE:
677 r = glob_extend(&arg_file, optarg);
eb56eb9b
MS
678 if (r < 0)
679 return log_error_errno(r, "Failed to add paths: %m");
eacbb4d3
ZJS
680 break;
681
682 case ARG_CURSOR:
683 if (arg_cursor) {
684 log_error("cannot use more than one --cursor/--after-cursor");
685 return -EINVAL;
686 }
687
688 arg_cursor = optarg;
689 break;
690
691 case ARG_AFTER_CURSOR:
692 if (arg_cursor) {
693 log_error("cannot use more than one --cursor/--after-cursor");
694 return -EINVAL;
695 }
696
697 arg_cursor = optarg;
698 arg_after_cursor = true;
699 break;
700
701 case ARG_FOLLOW:
dad29dff
LP
702 if (optarg) {
703 r = parse_boolean(optarg);
704 if (r < 0) {
705 log_error("Failed to parse --follow= parameter.");
706 return -EINVAL;
707 }
708
709 arg_follow = !!r;
710 } else
711 arg_follow = true;
eacbb4d3 712
eacbb4d3
ZJS
713 break;
714
722b6795
ZJS
715 case ARG_SAVE_STATE:
716 arg_save_state = optarg ?: STATE_FILE;
717 break;
718
3d090cc6
ZJS
719 case '?':
720 log_error("Unknown option %s.", argv[optind-1]);
721 return -EINVAL;
722
723 case ':':
724 log_error("Missing argument to %s.", argv[optind-1]);
725 return -EINVAL;
726
727 default:
728 assert_not_reached("Unhandled option code.");
729 }
730
731 if (!arg_url) {
732 log_error("Required --url/-u option missing.");
733 return -EINVAL;
734 }
735
7449bc1f
ZJS
736 if (!!arg_key != !!arg_cert) {
737 log_error("Options --key and --cert must be used together.");
738 return -EINVAL;
739 }
740
eacbb4d3
ZJS
741 if (optind < argc && (arg_directory || arg_file || arg_machine || arg_journal_type)) {
742 log_error("Input arguments make no sense with journal input.");
3d090cc6
ZJS
743 return -EINVAL;
744 }
745
746 return 1;
747}
748
eacbb4d3
ZJS
749static int open_journal(sd_journal **j) {
750 int r;
751
752 if (arg_directory)
753 r = sd_journal_open_directory(j, arg_directory, arg_journal_type);
754 else if (arg_file)
755 r = sd_journal_open_files(j, (const char**) arg_file, 0);
756 else if (arg_machine)
757 r = sd_journal_open_container(j, arg_machine, 0);
758 else
759 r = sd_journal_open(j, !arg_merge*SD_JOURNAL_LOCAL_ONLY + arg_journal_type);
760 if (r < 0)
c33b3297
MS
761 log_error_errno(r, "Failed to open %s: %m",
762 arg_directory ? arg_directory : arg_file ? "files" : "journal");
eacbb4d3
ZJS
763 return r;
764}
3d090cc6
ZJS
765
766int main(int argc, char **argv) {
767 Uploader u;
768 int r;
eacbb4d3 769 bool use_journal;
3d090cc6
ZJS
770
771 log_show_color(true);
772 log_parse_environment();
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}