]>
Commit | Line | Data |
---|---|---|
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" |
3dadb54f | 26 | #include "path-util.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" |
3d090cc6 | 36 | |
799a8f39 ZJS |
37 | #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem" |
38 | #define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem" | |
39 | #define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem" | |
50a0b071 | 40 | #define DEFAULT_PORT 19532 |
29fc0ddc | 41 | |
2cf4172a | 42 | static const char* arg_url = NULL; |
7449bc1f ZJS |
43 | static const char *arg_key = NULL; |
44 | static const char *arg_cert = NULL; | |
45 | static const char *arg_trust = NULL; | |
eacbb4d3 ZJS |
46 | static const char *arg_directory = NULL; |
47 | static char **arg_file = NULL; | |
48 | static const char *arg_cursor = NULL; | |
49 | static bool arg_after_cursor = false; | |
50 | static int arg_journal_type = 0; | |
51 | static const char *arg_machine = NULL; | |
52 | static bool arg_merge = false; | |
53 | static int arg_follow = -1; | |
722b6795 | 54 | static const char *arg_save_state = NULL; |
eacbb4d3 | 55 | |
2cf4172a LP |
56 | static void close_fd_input(Uploader *u); |
57 | ||
eacbb4d3 ZJS |
58 | #define SERVER_ANSWER_KEEP 2048 |
59 | ||
722b6795 ZJS |
60 | #define STATE_FILE "/var/lib/systemd/journal-upload/state" |
61 | ||
3d090cc6 | 62 | #define easy_setopt(curl, opt, value, level, cmd) \ |
8847551b | 63 | do { \ |
3d090cc6 ZJS |
64 | code = curl_easy_setopt(curl, opt, value); \ |
65 | if (code) { \ | |
66 | log_full(level, \ | |
67 | "curl_easy_setopt " #opt " failed: %s", \ | |
68 | curl_easy_strerror(code)); \ | |
69 | cmd; \ | |
70 | } \ | |
9ed794a3 | 71 | } while (0) |
3d090cc6 | 72 | |
eacbb4d3 ZJS |
73 | static size_t output_callback(char *buf, |
74 | size_t size, | |
75 | size_t nmemb, | |
76 | void *userp) { | |
77 | Uploader *u = userp; | |
78 | ||
79 | assert(u); | |
80 | ||
81 | log_debug("The server answers (%zu bytes): %.*s", | |
82 | size*nmemb, (int)(size*nmemb), buf); | |
83 | ||
84 | if (nmemb && !u->answer) { | |
85 | u->answer = strndup(buf, size*nmemb); | |
86 | if (!u->answer) | |
d4e98880 | 87 | log_warning("Failed to store server answer (%zu bytes): out of memory", size*nmemb); |
eacbb4d3 ZJS |
88 | } |
89 | ||
90 | return size * nmemb; | |
91 | } | |
92 | ||
d71839af ZJS |
93 | static int check_cursor_updating(Uploader *u) { |
94 | _cleanup_free_ char *temp_path = NULL; | |
95 | _cleanup_fclose_ FILE *f = NULL; | |
96 | int r; | |
97 | ||
98 | if (!u->state_file) | |
99 | return 0; | |
100 | ||
101 | r = mkdir_parents(u->state_file, 0755); | |
eb56eb9b MS |
102 | if (r < 0) |
103 | return log_error_errno(r, "Cannot create parent directory of state file %s: %m", | |
104 | u->state_file); | |
d71839af ZJS |
105 | |
106 | r = fopen_temporary(u->state_file, &f, &temp_path); | |
eb56eb9b MS |
107 | if (r < 0) |
108 | return log_error_errno(r, "Cannot save state to %s: %m", | |
109 | u->state_file); | |
6990fb6b | 110 | (void) unlink(temp_path); |
d71839af ZJS |
111 | |
112 | return 0; | |
113 | } | |
114 | ||
722b6795 ZJS |
115 | static int update_cursor_state(Uploader *u) { |
116 | _cleanup_free_ char *temp_path = NULL; | |
117 | _cleanup_fclose_ FILE *f = NULL; | |
118 | int r; | |
119 | ||
120 | if (!u->state_file || !u->last_cursor) | |
121 | return 0; | |
122 | ||
123 | r = fopen_temporary(u->state_file, &f, &temp_path); | |
124 | if (r < 0) | |
dacd6cee | 125 | goto fail; |
722b6795 ZJS |
126 | |
127 | fprintf(f, | |
128 | "# This is private data. Do not parse.\n" | |
129 | "LAST_CURSOR=%s\n", | |
130 | u->last_cursor); | |
131 | ||
dacd6cee LP |
132 | r = fflush_and_check(f); |
133 | if (r < 0) | |
134 | goto fail; | |
722b6795 | 135 | |
dacd6cee | 136 | if (rename(temp_path, u->state_file) < 0) { |
722b6795 | 137 | r = -errno; |
dacd6cee | 138 | goto fail; |
722b6795 ZJS |
139 | } |
140 | ||
dacd6cee LP |
141 | return 0; |
142 | ||
143 | fail: | |
144 | if (temp_path) | |
145 | (void) unlink(temp_path); | |
146 | ||
147 | (void) unlink(u->state_file); | |
722b6795 | 148 | |
dacd6cee | 149 | return log_error_errno(r, "Failed to save state %s: %m", u->state_file); |
722b6795 ZJS |
150 | } |
151 | ||
152 | static int load_cursor_state(Uploader *u) { | |
153 | int r; | |
154 | ||
155 | if (!u->state_file) | |
156 | return 0; | |
157 | ||
13df9c39 | 158 | r = parse_env_file(NULL, u->state_file, "LAST_CURSOR", &u->last_cursor); |
36d4739a ZJS |
159 | if (r == -ENOENT) |
160 | log_debug("State file %s is not present.", u->state_file); | |
eb56eb9b MS |
161 | else if (r < 0) |
162 | return log_error_errno(r, "Failed to read state file %s: %m", | |
163 | u->state_file); | |
164 | else | |
36d4739a | 165 | log_debug("Last cursor was %s", u->last_cursor); |
722b6795 ZJS |
166 | |
167 | return 0; | |
168 | } | |
169 | ||
3d090cc6 ZJS |
170 | int start_upload(Uploader *u, |
171 | size_t (*input_callback)(void *ptr, | |
172 | size_t size, | |
173 | size_t nmemb, | |
174 | void *userdata), | |
175 | void *data) { | |
176 | CURLcode code; | |
177 | ||
178 | assert(u); | |
179 | assert(input_callback); | |
180 | ||
181 | if (!u->header) { | |
182 | struct curl_slist *h; | |
183 | ||
184 | h = curl_slist_append(NULL, "Content-Type: application/vnd.fdo.journal"); | |
185 | if (!h) | |
186 | return log_oom(); | |
187 | ||
188 | h = curl_slist_append(h, "Transfer-Encoding: chunked"); | |
189 | if (!h) { | |
190 | curl_slist_free_all(h); | |
191 | return log_oom(); | |
192 | } | |
193 | ||
194 | h = curl_slist_append(h, "Accept: text/plain"); | |
195 | if (!h) { | |
196 | curl_slist_free_all(h); | |
197 | return log_oom(); | |
198 | } | |
199 | ||
200 | u->header = h; | |
201 | } | |
202 | ||
203 | if (!u->easy) { | |
204 | CURL *curl; | |
205 | ||
206 | curl = curl_easy_init(); | |
baaa35ad ZJS |
207 | if (!curl) |
208 | return log_error_errno(SYNTHETIC_ERRNO(ENOSR), | |
209 | "Call to curl_easy_init failed."); | |
3d090cc6 ZJS |
210 | |
211 | /* tell it to POST to the URL */ | |
212 | easy_setopt(curl, CURLOPT_POST, 1L, | |
213 | LOG_ERR, return -EXFULL); | |
214 | ||
b88a40a7 | 215 | easy_setopt(curl, CURLOPT_ERRORBUFFER, u->error, |
eacbb4d3 ZJS |
216 | LOG_ERR, return -EXFULL); |
217 | ||
218 | /* set where to write to */ | |
219 | easy_setopt(curl, CURLOPT_WRITEFUNCTION, output_callback, | |
220 | LOG_ERR, return -EXFULL); | |
221 | ||
222 | easy_setopt(curl, CURLOPT_WRITEDATA, data, | |
223 | LOG_ERR, return -EXFULL); | |
224 | ||
3d090cc6 ZJS |
225 | /* set where to read from */ |
226 | easy_setopt(curl, CURLOPT_READFUNCTION, input_callback, | |
227 | LOG_ERR, return -EXFULL); | |
228 | ||
229 | easy_setopt(curl, CURLOPT_READDATA, data, | |
230 | LOG_ERR, return -EXFULL); | |
231 | ||
232 | /* use our special own mime type and chunked transfer */ | |
233 | easy_setopt(curl, CURLOPT_HTTPHEADER, u->header, | |
234 | LOG_ERR, return -EXFULL); | |
235 | ||
f1d34068 | 236 | if (DEBUG_LOGGING) |
5dabb1e0 ZJS |
237 | /* enable verbose for easier tracing */ |
238 | easy_setopt(curl, CURLOPT_VERBOSE, 1L, LOG_WARNING, ); | |
3d090cc6 ZJS |
239 | |
240 | easy_setopt(curl, CURLOPT_USERAGENT, | |
681bd2c5 | 241 | "systemd-journal-upload " GIT_VERSION, |
3d090cc6 ZJS |
242 | LOG_WARNING, ); |
243 | ||
3dadb54f | 244 | if (!streq_ptr(arg_key, "-") && (arg_key || startswith(u->url, "https://"))) { |
799a8f39 | 245 | easy_setopt(curl, CURLOPT_SSLKEY, arg_key ?: PRIV_KEY_FILE, |
7449bc1f | 246 | LOG_ERR, return -EXFULL); |
29fc0ddc | 247 | easy_setopt(curl, CURLOPT_SSLCERT, arg_cert ?: CERT_FILE, |
7449bc1f ZJS |
248 | LOG_ERR, return -EXFULL); |
249 | } | |
250 | ||
3dadb54f | 251 | if (STRPTR_IN_SET(arg_trust, "-", "all")) |
8847551b ZJS |
252 | easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0, |
253 | LOG_ERR, return -EUCLEAN); | |
254 | else if (arg_trust || startswith(u->url, "https://")) | |
29fc0ddc | 255 | easy_setopt(curl, CURLOPT_CAINFO, arg_trust ?: TRUST_FILE, |
7449bc1f ZJS |
256 | LOG_ERR, return -EXFULL); |
257 | ||
258 | if (arg_key || arg_trust) | |
259 | easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1, | |
260 | LOG_WARNING, ); | |
261 | ||
3d090cc6 | 262 | u->easy = curl; |
eacbb4d3 ZJS |
263 | } else { |
264 | /* truncate the potential old error message */ | |
265 | u->error[0] = '\0'; | |
266 | ||
267 | free(u->answer); | |
268 | u->answer = 0; | |
3d090cc6 ZJS |
269 | } |
270 | ||
271 | /* upload to this place */ | |
272 | code = curl_easy_setopt(u->easy, CURLOPT_URL, u->url); | |
baaa35ad ZJS |
273 | if (code) |
274 | return log_error_errno(SYNTHETIC_ERRNO(EXFULL), | |
275 | "curl_easy_setopt CURLOPT_URL failed: %s", | |
276 | curl_easy_strerror(code)); | |
3d090cc6 ZJS |
277 | |
278 | u->uploading = true; | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | static size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *userp) { | |
284 | Uploader *u = userp; | |
c504106c | 285 | ssize_t n; |
3d090cc6 ZJS |
286 | |
287 | assert(u); | |
ccc0ec6f | 288 | assert(nmemb < SSIZE_MAX / size); |
3d090cc6 ZJS |
289 | |
290 | if (u->input < 0) | |
291 | return 0; | |
292 | ||
c504106c | 293 | assert(!size_multiply_overflow(size, nmemb)); |
3d090cc6 | 294 | |
c504106c LP |
295 | n = read(u->input, buf, size * nmemb); |
296 | log_debug("%s: allowed %zu, read %zd", __func__, size*nmemb, n); | |
297 | if (n > 0) | |
298 | return n; | |
3d090cc6 ZJS |
299 | |
300 | u->uploading = false; | |
c504106c | 301 | if (n < 0) { |
56f64d95 | 302 | log_error_errno(errno, "Aborting transfer after read error on input: %m."); |
3d090cc6 ZJS |
303 | return CURL_READFUNC_ABORT; |
304 | } | |
c504106c LP |
305 | |
306 | log_debug("Reached EOF"); | |
307 | close_fd_input(u); | |
308 | return 0; | |
3d090cc6 ZJS |
309 | } |
310 | ||
311 | static 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 | ||
318 | static 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 | ||
346 | static 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 |
374 | static 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 | ||
390 | static 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 | 408 | static 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 | ||
b48a0e09 LP |
415 | *u = (Uploader) { |
416 | .input = -1 | |
417 | }; | |
3d090cc6 | 418 | |
49fe5c09 LP |
419 | host = STARTSWITH_SET(url, "http://", "https://"); |
420 | if (!host) { | |
50a0b071 ZJS |
421 | host = url; |
422 | proto = "https://"; | |
423 | } | |
424 | ||
425 | if (strchr(host, ':')) | |
605405c6 | 426 | u->url = strjoin(proto, url, "/upload"); |
50a0b071 ZJS |
427 | else { |
428 | char *t; | |
429 | size_t x; | |
5bc89120 | 430 | |
50a0b071 ZJS |
431 | t = strdupa(url); |
432 | x = strlen(t); | |
433 | while (x > 0 && t[x - 1] == '/') | |
434 | t[x - 1] = '\0'; | |
435 | ||
605405c6 | 436 | u->url = strjoin(proto, t, ":" STRINGIFY(DEFAULT_PORT), "/upload"); |
50a0b071 | 437 | } |
5bc89120 ZJS |
438 | if (!u->url) |
439 | return log_oom(); | |
440 | ||
722b6795 | 441 | u->state_file = state_file; |
3d090cc6 ZJS |
442 | |
443 | r = sd_event_default(&u->events); | |
eb56eb9b MS |
444 | if (r < 0) |
445 | return log_error_errno(r, "sd_event_default failed: %m"); | |
3d090cc6 | 446 | |
a3152e76 | 447 | r = setup_signals(u); |
eb56eb9b MS |
448 | if (r < 0) |
449 | return log_error_errno(r, "Failed to set up signals: %m"); | |
a3152e76 | 450 | |
0aa176a7 ZJS |
451 | (void) sd_watchdog_enabled(false, &u->watchdog_usec); |
452 | ||
722b6795 | 453 | return load_cursor_state(u); |
3d090cc6 ZJS |
454 | } |
455 | ||
456 | static void destroy_uploader(Uploader *u) { | |
457 | assert(u); | |
458 | ||
459 | curl_easy_cleanup(u->easy); | |
460 | curl_slist_free_all(u->header); | |
eacbb4d3 ZJS |
461 | free(u->answer); |
462 | ||
463 | free(u->last_cursor); | |
722b6795 | 464 | free(u->current_cursor); |
3d090cc6 | 465 | |
5bc89120 ZJS |
466 | free(u->url); |
467 | ||
3d090cc6 ZJS |
468 | u->input_event = sd_event_source_unref(u->input_event); |
469 | ||
470 | close_fd_input(u); | |
eacbb4d3 | 471 | close_journal_input(u); |
3d090cc6 | 472 | |
a3152e76 ZJS |
473 | sd_event_source_unref(u->sigterm_event); |
474 | sd_event_source_unref(u->sigint_event); | |
3d090cc6 ZJS |
475 | sd_event_unref(u->events); |
476 | } | |
477 | ||
eacbb4d3 ZJS |
478 | static int perform_upload(Uploader *u) { |
479 | CURLcode code; | |
480 | long status; | |
481 | ||
482 | assert(u); | |
483 | ||
0aa176a7 | 484 | u->watchdog_timestamp = now(CLOCK_MONOTONIC); |
eacbb4d3 ZJS |
485 | code = curl_easy_perform(u->easy); |
486 | if (code) { | |
30776485 ZJS |
487 | if (u->error[0]) |
488 | log_error("Upload to %s failed: %.*s", | |
489 | u->url, (int) sizeof(u->error), u->error); | |
490 | else | |
491 | log_error("Upload to %s failed: %s", | |
492 | u->url, curl_easy_strerror(code)); | |
eacbb4d3 ZJS |
493 | return -EIO; |
494 | } | |
495 | ||
496 | code = curl_easy_getinfo(u->easy, CURLINFO_RESPONSE_CODE, &status); | |
baaa35ad ZJS |
497 | if (code) |
498 | return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), | |
499 | "Failed to retrieve response code: %s", | |
500 | curl_easy_strerror(code)); | |
501 | ||
502 | if (status >= 300) | |
503 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
504 | "Upload to %s failed with code %ld: %s", | |
505 | u->url, status, strna(u->answer)); | |
506 | else if (status < 200) | |
507 | return log_error_errno(SYNTHETIC_ERRNO(EIO), | |
508 | "Upload to %s finished with unexpected code %ld: %s", | |
509 | u->url, status, strna(u->answer)); | |
510 | else | |
1fa2f38f | 511 | log_debug("Upload finished successfully with code %ld: %s", |
eacbb4d3 | 512 | status, strna(u->answer)); |
722b6795 | 513 | |
3b319885 | 514 | free_and_replace(u->last_cursor, u->current_cursor); |
722b6795 ZJS |
515 | |
516 | return update_cursor_state(u); | |
eacbb4d3 ZJS |
517 | } |
518 | ||
3dadb54f CH |
519 | static int config_parse_path_or_ignore( |
520 | const char *unit, | |
521 | const char *filename, | |
522 | unsigned line, | |
523 | const char *section, | |
524 | unsigned section_line, | |
525 | const char *lvalue, | |
526 | int ltype, | |
527 | const char *rvalue, | |
528 | void *data, | |
529 | void *userdata) { | |
530 | ||
531 | _cleanup_free_ char *n = NULL; | |
532 | bool fatal = ltype; | |
533 | char **s = data; | |
534 | int r; | |
535 | ||
536 | assert(filename); | |
537 | assert(lvalue); | |
538 | assert(rvalue); | |
539 | assert(data); | |
540 | ||
541 | if (isempty(rvalue)) | |
542 | goto finalize; | |
543 | ||
544 | n = strdup(rvalue); | |
545 | if (!n) | |
546 | return log_oom(); | |
547 | ||
548 | if (streq(n, "-")) | |
549 | goto finalize; | |
550 | ||
551 | r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue); | |
552 | if (r < 0) | |
553 | return fatal ? -ENOEXEC : 0; | |
554 | ||
555 | finalize: | |
556 | return free_and_replace(*s, n); | |
557 | } | |
558 | ||
29fc0ddc ZJS |
559 | static int parse_config(void) { |
560 | const ConfigTableItem items[] = { | |
3dadb54f CH |
561 | { "Upload", "URL", config_parse_string, 0, &arg_url }, |
562 | { "Upload", "ServerKeyFile", config_parse_path_or_ignore, 0, &arg_key }, | |
563 | { "Upload", "ServerCertificateFile", config_parse_path_or_ignore, 0, &arg_cert }, | |
564 | { "Upload", "TrustedCertificateFile", config_parse_path_or_ignore, 0, &arg_trust }, | |
4f9ff96a LP |
565 | {} |
566 | }; | |
29fc0ddc | 567 | |
4f9ff96a LP |
568 | return config_parse_many_nulstr( |
569 | PKGSYSCONFDIR "/journal-upload.conf", | |
570 | CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"), | |
571 | "Upload\0", | |
572 | config_item_table_lookup, items, | |
573 | CONFIG_PARSE_WARN, | |
574 | NULL, | |
575 | NULL); | |
29fc0ddc ZJS |
576 | } |
577 | ||
37ec0fdd LP |
578 | static int help(void) { |
579 | _cleanup_free_ char *link = NULL; | |
580 | int r; | |
581 | ||
582 | r = terminal_urlify_man("systemd-journal-upload.service", "8", &link); | |
583 | if (r < 0) | |
584 | return log_oom(); | |
585 | ||
3d090cc6 ZJS |
586 | printf("%s -u URL {FILE|-}...\n\n" |
587 | "Upload journal events to a remote server.\n\n" | |
dad29dff LP |
588 | " -h --help Show this help\n" |
589 | " --version Show package version\n" | |
50a0b071 ZJS |
590 | " -u --url=URL Upload to this address (default port " |
591 | STRINGIFY(DEFAULT_PORT) ")\n" | |
1af719ed ZJS |
592 | " --key=FILENAME Specify key in PEM format (default:\n" |
593 | " \"" PRIV_KEY_FILE "\")\n" | |
594 | " --cert=FILENAME Specify certificate in PEM format (default:\n" | |
595 | " \"" CERT_FILE "\")\n" | |
596 | " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n" | |
597 | " \"" TRUST_FILE "\")\n" | |
dad29dff LP |
598 | " --system Use the system journal\n" |
599 | " --user Use the user journal for the current user\n" | |
600 | " -m --merge Use all available journals\n" | |
601 | " -M --machine=CONTAINER Operate on local container\n" | |
602 | " -D --directory=PATH Use journal files from directory\n" | |
603 | " --file=PATH Use this journal file\n" | |
604 | " --cursor=CURSOR Start at the specified cursor\n" | |
605 | " --after-cursor=CURSOR Start after the specified cursor\n" | |
606 | " --follow[=BOOL] Do [not] wait for input\n" | |
607 | " --save-state[=FILE] Save uploaded cursors (default \n" | |
608 | " " STATE_FILE ")\n" | |
37ec0fdd LP |
609 | "\nSee the %s for details.\n" |
610 | , program_invocation_short_name | |
611 | , link | |
612 | ); | |
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: | |
dad29dff LP |
751 | if (optarg) { |
752 | r = parse_boolean(optarg); | |
baaa35ad ZJS |
753 | if (r < 0) |
754 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
755 | "Failed to parse --follow= parameter."); | |
dad29dff LP |
756 | |
757 | arg_follow = !!r; | |
758 | } else | |
759 | arg_follow = true; | |
eacbb4d3 | 760 | |
eacbb4d3 ZJS |
761 | break; |
762 | ||
722b6795 ZJS |
763 | case ARG_SAVE_STATE: |
764 | arg_save_state = optarg ?: STATE_FILE; | |
765 | break; | |
766 | ||
3d090cc6 | 767 | case '?': |
baaa35ad ZJS |
768 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
769 | "Unknown option %s.", | |
770 | argv[optind - 1]); | |
3d090cc6 ZJS |
771 | |
772 | case ':': | |
baaa35ad ZJS |
773 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
774 | "Missing argument to %s.", | |
775 | argv[optind - 1]); | |
3d090cc6 ZJS |
776 | |
777 | default: | |
778 | assert_not_reached("Unhandled option code."); | |
779 | } | |
780 | ||
baaa35ad ZJS |
781 | if (!arg_url) |
782 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
783 | "Required --url/-u option missing."); | |
3d090cc6 | 784 | |
baaa35ad ZJS |
785 | if (!!arg_key != !!arg_cert) |
786 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
787 | "Options --key and --cert must be used together."); | |
7449bc1f | 788 | |
baaa35ad ZJS |
789 | if (optind < argc && (arg_directory || arg_file || arg_machine || arg_journal_type)) |
790 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
791 | "Input arguments make no sense with journal input."); | |
3d090cc6 ZJS |
792 | |
793 | return 1; | |
794 | } | |
795 | ||
eacbb4d3 ZJS |
796 | static int open_journal(sd_journal **j) { |
797 | int r; | |
798 | ||
799 | if (arg_directory) | |
800 | r = sd_journal_open_directory(j, arg_directory, arg_journal_type); | |
801 | else if (arg_file) | |
802 | r = sd_journal_open_files(j, (const char**) arg_file, 0); | |
68312977 LP |
803 | else if (arg_machine) { |
804 | #pragma GCC diagnostic push | |
805 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | |
806 | /* FIXME: replace with D-Bus call OpenMachineRootDirectory() so that things also work with raw disk images */ | |
eacbb4d3 | 807 | r = sd_journal_open_container(j, arg_machine, 0); |
68312977 LP |
808 | #pragma GCC diagnostic pop |
809 | } else | |
eacbb4d3 ZJS |
810 | r = sd_journal_open(j, !arg_merge*SD_JOURNAL_LOCAL_ONLY + arg_journal_type); |
811 | if (r < 0) | |
c33b3297 MS |
812 | log_error_errno(r, "Failed to open %s: %m", |
813 | arg_directory ? arg_directory : arg_file ? "files" : "journal"); | |
eacbb4d3 ZJS |
814 | return r; |
815 | } | |
3d090cc6 | 816 | |
f36bb1e1 | 817 | static int run(int argc, char **argv) { |
c9ed6086 | 818 | _cleanup_(destroy_uploader) Uploader u = {}; |
272ac70a | 819 | _cleanup_(notify_on_cleanup) const char *notify_message = NULL; |
eacbb4d3 | 820 | bool use_journal; |
f36bb1e1 | 821 | int r; |
3d090cc6 ZJS |
822 | |
823 | log_show_color(true); | |
41d1f469 | 824 | log_parse_environment_cli(); |
3d090cc6 | 825 | |
1abaf488 LP |
826 | /* The journal merging logic potentially needs a lot of fds. */ |
827 | (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); | |
828 | ||
29fc0ddc | 829 | r = parse_config(); |
4015ac5c | 830 | if (r < 0) |
f36bb1e1 | 831 | return r; |
29fc0ddc | 832 | |
3d090cc6 ZJS |
833 | r = parse_argv(argc, argv); |
834 | if (r <= 0) | |
f36bb1e1 | 835 | return r; |
3d090cc6 | 836 | |
2cf4172a LP |
837 | sigbus_install(); |
838 | ||
722b6795 | 839 | r = setup_uploader(&u, arg_url, arg_save_state); |
3d090cc6 | 840 | if (r < 0) |
f36bb1e1 | 841 | return r; |
3d090cc6 | 842 | |
a3152e76 ZJS |
843 | sd_event_set_watchdog(u.events, true); |
844 | ||
d71839af ZJS |
845 | r = check_cursor_updating(&u); |
846 | if (r < 0) | |
f36bb1e1 | 847 | return r; |
d71839af | 848 | |
3d090cc6 | 849 | log_debug("%s running as pid "PID_FMT, |
df0ff127 | 850 | program_invocation_short_name, getpid_cached()); |
eacbb4d3 ZJS |
851 | |
852 | use_journal = optind >= argc; | |
853 | if (use_journal) { | |
854 | sd_journal *j; | |
855 | r = open_journal(&j); | |
856 | if (r < 0) | |
f36bb1e1 | 857 | return r; |
eacbb4d3 | 858 | r = open_journal_for_upload(&u, j, |
722b6795 ZJS |
859 | arg_cursor ?: u.last_cursor, |
860 | arg_cursor ? arg_after_cursor : true, | |
eacbb4d3 ZJS |
861 | !!arg_follow); |
862 | if (r < 0) | |
f36bb1e1 | 863 | return r; |
eacbb4d3 ZJS |
864 | } |
865 | ||
f36bb1e1 YW |
866 | notify_message = notify_start("READY=1\n" |
867 | "STATUS=Processing input...", | |
868 | NOTIFY_STOPPING); | |
3d090cc6 | 869 | |
57255510 | 870 | for (;;) { |
36d4739a ZJS |
871 | r = sd_event_get_state(u.events); |
872 | if (r < 0) | |
f36bb1e1 | 873 | return r; |
36d4739a | 874 | if (r == SD_EVENT_FINISHED) |
f36bb1e1 | 875 | return 0; |
36d4739a | 876 | |
eacbb4d3 ZJS |
877 | if (use_journal) { |
878 | if (!u.journal) | |
f36bb1e1 | 879 | return 0; |
eacbb4d3 ZJS |
880 | |
881 | r = check_journal_input(&u); | |
882 | } else if (u.input < 0 && !use_journal) { | |
3d090cc6 | 883 | if (optind >= argc) |
f36bb1e1 | 884 | return 0; |
3d090cc6 ZJS |
885 | |
886 | log_debug("Using %s as input.", argv[optind]); | |
3d090cc6 | 887 | r = open_file_for_upload(&u, argv[optind++]); |
3d090cc6 | 888 | } |
eacbb4d3 | 889 | if (r < 0) |
f36bb1e1 | 890 | return r; |
3d090cc6 | 891 | |
3d090cc6 | 892 | if (u.uploading) { |
eacbb4d3 ZJS |
893 | r = perform_upload(&u); |
894 | if (r < 0) | |
f36bb1e1 | 895 | return r; |
3d090cc6 ZJS |
896 | } |
897 | ||
eacbb4d3 | 898 | r = sd_event_run(u.events, u.timeout); |
f36bb1e1 YW |
899 | if (r < 0) |
900 | return log_error_errno(r, "Failed to run event loop: %m"); | |
3d090cc6 | 901 | } |
3d090cc6 | 902 | } |
f36bb1e1 YW |
903 | |
904 | DEFINE_MAIN_FUNCTION(run); |