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