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