1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Zbigniew Jędrzejewski-Szmek
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <curl/curl.h>
28 #include "sd-daemon.h"
34 #include "journal-upload.h"
36 static const char* arg_url;
38 static void close_fd_input(Uploader *u);
40 static const char *arg_key = NULL;
41 static const char *arg_cert = NULL;
42 static const char *arg_trust = NULL;
44 static const char *arg_directory = NULL;
45 static char **arg_file = NULL;
46 static const char *arg_cursor = NULL;
47 static bool arg_after_cursor = false;
48 static int arg_journal_type = 0;
49 static const char *arg_machine = NULL;
50 static bool arg_merge = false;
51 static int arg_follow = -1;
52 static const char *arg_save_state = NULL;
54 #define SERVER_ANSWER_KEEP 2048
56 #define STATE_FILE "/var/lib/systemd/journal-upload/state"
58 #define easy_setopt(curl, opt, value, level, cmd) \
60 code = curl_easy_setopt(curl, opt, value); \
63 "curl_easy_setopt " #opt " failed: %s", \
64 curl_easy_strerror(code)); \
69 static size_t output_callback(char *buf,
77 log_debug("The server answers (%zu bytes): %.*s",
78 size*nmemb, (int)(size*nmemb), buf);
80 if (nmemb && !u->answer) {
81 u->answer = strndup(buf, size*nmemb);
83 log_warning("Failed to store server answer (%zu bytes): %s",
84 size*nmemb, strerror(ENOMEM));
90 static int update_cursor_state(Uploader *u) {
91 _cleanup_free_ char *temp_path = NULL;
92 _cleanup_fclose_ FILE *f = NULL;
95 if (!u->state_file || !u->last_cursor)
98 r = fopen_temporary(u->state_file, &f, &temp_path);
103 "# This is private data. Do not parse.\n"
109 if (ferror(f) || rename(temp_path, u->state_file) < 0) {
111 unlink(u->state_file);
117 log_error("Failed to save state %s: %s", u->state_file, strerror(-r));
122 static int load_cursor_state(Uploader *u) {
128 r = parse_env_file(u->state_file, NEWLINE,
129 "LAST_CURSOR", &u->last_cursor,
132 if (r < 0 && r != -ENOENT) {
133 log_error("Failed to read state file %s: %s",
134 u->state_file, strerror(-r));
143 int start_upload(Uploader *u,
144 size_t (*input_callback)(void *ptr,
152 assert(input_callback);
155 struct curl_slist *h;
157 h = curl_slist_append(NULL, "Content-Type: application/vnd.fdo.journal");
161 h = curl_slist_append(h, "Transfer-Encoding: chunked");
163 curl_slist_free_all(h);
167 h = curl_slist_append(h, "Accept: text/plain");
169 curl_slist_free_all(h);
179 curl = curl_easy_init();
181 log_error("Call to curl_easy_init failed.");
185 /* tell it to POST to the URL */
186 easy_setopt(curl, CURLOPT_POST, 1L,
187 LOG_ERR, return -EXFULL);
189 easy_setopt(curl, CURLOPT_ERRORBUFFER, &u->error,
190 LOG_ERR, return -EXFULL);
192 /* set where to write to */
193 easy_setopt(curl, CURLOPT_WRITEFUNCTION, output_callback,
194 LOG_ERR, return -EXFULL);
196 easy_setopt(curl, CURLOPT_WRITEDATA, data,
197 LOG_ERR, return -EXFULL);
199 /* set where to read from */
200 easy_setopt(curl, CURLOPT_READFUNCTION, input_callback,
201 LOG_ERR, return -EXFULL);
203 easy_setopt(curl, CURLOPT_READDATA, data,
204 LOG_ERR, return -EXFULL);
206 /* use our special own mime type and chunked transfer */
207 easy_setopt(curl, CURLOPT_HTTPHEADER, u->header,
208 LOG_ERR, return -EXFULL);
210 /* enable verbose for easier tracing */
211 easy_setopt(curl, CURLOPT_VERBOSE, 1L, LOG_WARNING, );
213 easy_setopt(curl, CURLOPT_USERAGENT,
214 "systemd-journal-upload " PACKAGE_STRING,
220 easy_setopt(curl, CURLOPT_SSLKEY, arg_key,
221 LOG_ERR, return -EXFULL);
222 easy_setopt(curl, CURLOPT_SSLCERT, arg_cert,
223 LOG_ERR, return -EXFULL);
227 easy_setopt(curl, CURLOPT_CAINFO, arg_trust,
228 LOG_ERR, return -EXFULL);
230 if (arg_key || arg_trust)
231 easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1,
236 /* truncate the potential old error message */
243 /* upload to this place */
244 code = curl_easy_setopt(u->easy, CURLOPT_URL, u->url);
246 log_error("curl_easy_setopt CURLOPT_URL failed: %s",
247 curl_easy_strerror(code));
256 static size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
262 assert(nmemb <= SSIZE_MAX / size);
267 r = read(u->input, buf, size * nmemb);
268 log_debug("%s: allowed %zu, read %zu", __func__, size*nmemb, r);
273 u->uploading = false;
275 log_debug("Reached EOF");
279 log_error("Aborting transfer after read error on input: %m.");
280 return CURL_READFUNC_ABORT;
284 static void close_fd_input(Uploader *u) {
288 close_nointr(u->input);
293 static int dispatch_fd_input(sd_event_source *event,
300 assert(revents & EPOLLIN);
304 log_warning("dispatch_fd_input called when uploading, ignoring.");
308 return start_upload(u, fd_input_callback, u);
311 static int open_file_for_upload(Uploader *u, const char *filename) {
314 if (streq(filename, "-"))
317 fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
319 log_error("Failed to open %s: %m", filename);
327 r = sd_event_add_io(u->events, &u->input_event,
328 fd, EPOLLIN, dispatch_fd_input, u);
330 if (r != -EPERM || arg_follow > 0) {
331 log_error("Failed to register input event: %s", strerror(-r));
335 /* Normal files should just be consumed without polling. */
336 r = start_upload(u, fd_input_callback, u);
343 static int dispatch_sigterm(sd_event_source *event,
344 const struct signalfd_siginfo *si,
346 Uploader *u = userdata;
350 log_received_signal(LOG_INFO, si);
353 close_journal_input(u);
355 sd_event_exit(u->events, 0);
359 static int setup_signals(Uploader *u) {
365 assert_se(sigemptyset(&mask) == 0);
366 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
367 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
369 r = sd_event_add_signal(u->events, &u->sigterm_event, SIGTERM, dispatch_sigterm, u);
373 r = sd_event_add_signal(u->events, &u->sigint_event, SIGINT, dispatch_sigterm, u);
380 static int setup_uploader(Uploader *u, const char *url, const char *state_file) {
386 memzero(u, sizeof(Uploader));
390 u->state_file = state_file;
392 r = sd_event_default(&u->events);
394 log_error("sd_event_default failed: %s", strerror(-r));
398 r = setup_signals(u);
400 log_error("Failed to set up signals: %s", strerror(-r));
404 return load_cursor_state(u);
407 static void destroy_uploader(Uploader *u) {
410 curl_easy_cleanup(u->easy);
411 curl_slist_free_all(u->header);
414 free(u->last_cursor);
415 free(u->current_cursor);
417 u->input_event = sd_event_source_unref(u->input_event);
420 close_journal_input(u);
422 sd_event_source_unref(u->sigterm_event);
423 sd_event_source_unref(u->sigint_event);
424 sd_event_unref(u->events);
427 static int perform_upload(Uploader *u) {
433 code = curl_easy_perform(u->easy);
435 log_error("Upload to %s failed: %.*s",
437 u->error[0] ? (int) sizeof(u->error) : INT_MAX,
438 u->error[0] ? u->error : curl_easy_strerror(code));
442 code = curl_easy_getinfo(u->easy, CURLINFO_RESPONSE_CODE, &status);
444 log_error("Failed to retrieve response code: %s",
445 curl_easy_strerror(code));
450 log_error("Upload to %s failed with code %lu: %s",
451 u->url, status, strna(u->answer));
453 } else if (status < 200) {
454 log_error("Upload to %s finished with unexpected code %lu: %s",
455 u->url, status, strna(u->answer));
458 log_debug("Upload finished successfully with code %lu: %s",
459 status, strna(u->answer));
461 free(u->last_cursor);
462 u->last_cursor = u->current_cursor;
463 u->current_cursor = NULL;
465 return update_cursor_state(u);
468 static void help(void) {
469 printf("%s -u URL {FILE|-}...\n\n"
470 "Upload journal events to a remote server.\n\n"
472 " --url=URL Upload to this address\n"
473 " --key=FILENAME Specify key in PEM format\n"
474 " --cert=FILENAME Specify certificate in PEM format\n"
475 " --trust=FILENAME Specify CA certificate in PEM format\n"
476 " --system Use the system journal\n"
477 " --user Use the user journal for the current user\n"
478 " -m --merge Use all available journals\n"
479 " -M --machine=CONTAINER Operate on local container\n"
480 " -D --directory=PATH Use journal files from directory\n"
481 " --file=PATH Use this journal file\n"
482 " --cursor=CURSOR Start at the specified cursor\n"
483 " --after-cursor=CURSOR Start after the specified cursor\n"
484 " --[no-]follow Do [not] wait for input\n"
485 " --save-state[=FILE] Save uploaded cursors (default \n"
487 " -h --help Show this help and exit\n"
488 " --version Print version string and exit\n"
489 , program_invocation_short_name);
492 static int parse_argv(int argc, char *argv[]) {
508 static const struct option options[] = {
509 { "help", no_argument, NULL, 'h' },
510 { "version", no_argument, NULL, ARG_VERSION },
511 { "url", required_argument, NULL, 'u' },
512 { "key", required_argument, NULL, ARG_KEY },
513 { "cert", required_argument, NULL, ARG_CERT },
514 { "trust", required_argument, NULL, ARG_TRUST },
515 { "system", no_argument, NULL, ARG_SYSTEM },
516 { "user", no_argument, NULL, ARG_USER },
517 { "merge", no_argument, NULL, 'm' },
518 { "machine", required_argument, NULL, 'M' },
519 { "directory", required_argument, NULL, 'D' },
520 { "file", required_argument, NULL, ARG_FILE },
521 { "cursor", required_argument, NULL, ARG_CURSOR },
522 { "after-cursor", required_argument, NULL, ARG_AFTER_CURSOR },
523 { "follow", no_argument, NULL, ARG_FOLLOW },
524 { "no-follow", no_argument, NULL, ARG_NO_FOLLOW },
525 { "save-state", optional_argument, NULL, ARG_SAVE_STATE },
536 while ((c = getopt_long(argc, argv, "hu:mM:D:", options, NULL)) >= 0)
543 puts(PACKAGE_STRING);
544 puts(SYSTEMD_FEATURES);
549 log_error("cannot use more than one --url");
558 log_error("cannot use more than one --key");
567 log_error("cannot use more than one --cert");
576 log_error("cannot use more than one --trust");
584 arg_journal_type |= SD_JOURNAL_SYSTEM;
588 arg_journal_type |= SD_JOURNAL_CURRENT_USER;
597 log_error("cannot use more than one --machine/-M");
601 arg_machine = optarg;
606 log_error("cannot use more than one --directory/-D");
610 arg_directory = optarg;
614 r = glob_extend(&arg_file, optarg);
616 log_error("Failed to add paths: %s", strerror(-r));
623 log_error("cannot use more than one --cursor/--after-cursor");
630 case ARG_AFTER_CURSOR:
632 log_error("cannot use more than one --cursor/--after-cursor");
637 arg_after_cursor = true;
649 arg_save_state = optarg ?: STATE_FILE;
653 log_error("Unknown option %s.", argv[optind-1]);
657 log_error("Missing argument to %s.", argv[optind-1]);
661 assert_not_reached("Unhandled option code.");
665 log_error("Required --url/-u option missing.");
669 if (!!arg_key != !!arg_cert) {
670 log_error("Options --key and --cert must be used together.");
674 if (optind < argc && (arg_directory || arg_file || arg_machine || arg_journal_type)) {
675 log_error("Input arguments make no sense with journal input.");
682 static int open_journal(sd_journal **j) {
686 r = sd_journal_open_directory(j, arg_directory, arg_journal_type);
688 r = sd_journal_open_files(j, (const char**) arg_file, 0);
689 else if (arg_machine)
690 r = sd_journal_open_container(j, arg_machine, 0);
692 r = sd_journal_open(j, !arg_merge*SD_JOURNAL_LOCAL_ONLY + arg_journal_type);
694 log_error("Failed to open %s: %s",
695 arg_directory ? arg_directory : arg_file ? "files" : "journal",
700 int main(int argc, char **argv) {
705 log_show_color(true);
706 log_parse_environment();
708 r = parse_argv(argc, argv);
712 r = setup_uploader(&u, arg_url, arg_save_state);
716 sd_event_set_watchdog(u.events, true);
718 log_debug("%s running as pid "PID_FMT,
719 program_invocation_short_name, getpid());
721 use_journal = optind >= argc;
724 r = open_journal(&j);
727 r = open_journal_for_upload(&u, j,
728 arg_cursor ?: u.last_cursor,
729 arg_cursor ? arg_after_cursor : true,
737 "STATUS=Processing input...");
744 r = check_journal_input(&u);
745 } else if (u.input < 0 && !use_journal) {
749 log_debug("Using %s as input.", argv[optind]);
750 r = open_file_for_upload(&u, argv[optind++]);
755 r = sd_event_get_state(u.events);
758 if (r == SD_EVENT_FINISHED)
762 r = perform_upload(&u);
767 r = sd_event_run(u.events, u.timeout);
769 log_error("Failed to run event loop: %s", strerror(-r));
775 sd_notify(false, "STATUS=Shutting down...");
776 destroy_uploader(&u);
779 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;