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