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