journal-upload.c revision a83f403760cb63b1bf7787e9ff325ffb6d891d39
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Zbigniew Jędrzejewski-Szmek
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include "sd-daemon.h"
#include "log.h"
#include "util.h"
#include "build.h"
#include "fileio.h"
#include "journal-upload.h"
static const char* arg_url;
static void close_fd_input(Uploader *u);
static const char *arg_directory = NULL;
static const char *arg_cursor = NULL;
static bool arg_after_cursor = false;
static int arg_journal_type = 0;
static const char *arg_machine = NULL;
static bool arg_merge = false;
static int arg_follow = -1;
static const char *arg_save_state = NULL;
#define SERVER_ANSWER_KEEP 2048
#define STATE_FILE "/var/lib/systemd/journal-upload/state"
{ \
if (code) { \
curl_easy_strerror(code)); \
cmd; \
} \
}
void *userp) {
assert(u);
log_debug("The server answers (%zu bytes): %.*s",
if (!u->answer)
log_warning("Failed to store server answer (%zu bytes): %s",
}
}
static int update_cursor_state(Uploader *u) {
int r;
if (!u->state_file || !u->last_cursor)
return 0;
if (r < 0)
goto finish;
fprintf(f,
"# This is private data. Do not parse.\n"
"LAST_CURSOR=%s\n",
u->last_cursor);
fflush(f);
r = -errno;
unlink(u->state_file);
}
if (r < 0)
return r;
}
static int load_cursor_state(Uploader *u) {
int r;
if (!u->state_file)
return 0;
"LAST_CURSOR", &u->last_cursor,
NULL);
if (r < 0 && r != -ENOENT) {
log_error("Failed to read state file %s: %s",
u->state_file, strerror(-r));
return r;
}
return 0;
}
int start_upload(Uploader *u,
void *userdata),
void *data) {
assert(u);
if (!u->header) {
struct curl_slist *h;
if (!h)
return log_oom();
h = curl_slist_append(h, "Transfer-Encoding: chunked");
if (!h) {
return log_oom();
}
h = curl_slist_append(h, "Accept: text/plain");
if (!h) {
return log_oom();
}
u->header = h;
}
if (!u->easy) {
curl = curl_easy_init();
if (!curl) {
log_error("Call to curl_easy_init failed.");
return -ENOSR;
}
/* tell it to POST to the URL */
/* set where to write to */
/* set where to read from */
/* use our special own mime type and chunked transfer */
/* enable verbose for easier tracing */
"systemd-journal-upload " PACKAGE_STRING,
LOG_WARNING, );
if (arg_key) {
}
if (arg_trust)
LOG_WARNING, );
} else {
/* truncate the potential old error message */
u->error[0] = '\0';
u->answer = 0;
}
/* upload to this place */
if (code) {
log_error("curl_easy_setopt CURLOPT_URL failed: %s",
return -EXFULL;
}
u->uploading = true;
return 0;
}
ssize_t r;
assert(u);
if (u->input < 0)
return 0;
if (r > 0)
return r;
u->uploading = false;
if (r == 0) {
log_debug("Reached EOF");
close_fd_input(u);
return 0;
} else {
log_error("Aborting transfer after read error on input: %m.");
return CURL_READFUNC_ABORT;
}
}
static void close_fd_input(Uploader *u) {
assert(u);
if (u->input >= 0)
close_nointr(u->input);
u->input = -1;
u->timeout = 0;
}
int fd,
void *userp) {
assert(u);
log_debug("Received HUP");
close_fd_input(u);
return 0;
}
return -EINVAL;
}
if (u->uploading) {
log_warning("dispatch_fd_input called when uploading, ignoring.");
return 0;
}
return start_upload(u, fd_input_callback, u);
}
int fd, r;
fd = STDIN_FILENO;
else {
if (fd < 0) {
return -errno;
}
}
if (arg_follow) {
if (r < 0) {
if (r != -EPERM || arg_follow > 0) {
return r;
}
/* Normal files should just be consumed without polling. */
r = start_upload(u, fd_input_callback, u);
}
}
return r;
}
const struct signalfd_siginfo *si,
void *userdata) {
assert(u);
close_fd_input(u);
sd_event_exit(u->events, 0);
return 0;
}
static int setup_signals(Uploader *u) {
int r;
assert(u);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int r;
assert(u);
u->input = -1;
u->state_file = state_file;
r = sd_event_default(&u->events);
if (r < 0) {
return r;
}
r = setup_signals(u);
if (r < 0) {
return r;
}
return load_cursor_state(u);
}
static void destroy_uploader(Uploader *u) {
assert(u);
curl_easy_cleanup(u->easy);
free(u->last_cursor);
free(u->current_cursor);
close_fd_input(u);
sd_event_unref(u->events);
}
static int perform_upload(Uploader *u) {
long status;
assert(u);
if (code) {
log_error("Upload to %s failed: %.*s",
u->url,
return -EIO;
}
if (code) {
log_error("Failed to retrieve response code: %s",
return -EUCLEAN;
}
if (status >= 300) {
log_error("Upload to %s failed with code %lu: %s",
return -EIO;
} else if (status < 200) {
log_error("Upload to %s finished with unexpected code %lu: %s",
return -EIO;
} else
log_debug("Upload finished successfully with code %lu: %s",
free(u->last_cursor);
u->last_cursor = u->current_cursor;
u->current_cursor = NULL;
return update_cursor_state(u);
}
static void help(void) {
printf("%s -u URL {FILE|-}...\n\n"
"Upload journal events to a remote server.\n\n"
"Options:\n"
" -u --url=URL Upload to this address\n"
" --key=FILENAME Specify key in PEM format\n"
" --cert=FILENAME Specify certificate in PEM format\n"
" --trust=FILENAME Specify CA certificate in PEM format\n"
" --system Use the system journal\n"
" --user Use the user journal for the current user\n"
" -m --merge Use all available journals\n"
" -M --machine=CONTAINER Operate on local container\n"
" -D --directory=PATH Use journal files from directory\n"
" --file=PATH Use this journal file\n"
" --cursor=CURSOR Start at the specified cursor\n"
" --after-cursor=CURSOR Start after the specified cursor\n"
" --[no-]follow Do [not] wait for input\n"
" --save-state[=FILE] Save uploaded cursors (default \n"
" -h --help Show this help and exit\n"
" --version Print version string and exit\n"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c, r;
opterr = 0;
switch(c) {
case 'h':
help();
return 0 /* done */;
case ARG_VERSION:
return 0 /* done */;
case 'u':
if (arg_url) {
log_error("cannot use more than one --url");
return -EINVAL;
}
break;
case ARG_KEY:
if (arg_key) {
log_error("cannot use more than one --key");
return -EINVAL;
}
break;
case ARG_CERT:
if (arg_cert) {
log_error("cannot use more than one --cert");
return -EINVAL;
}
break;
case ARG_TRUST:
if (arg_trust) {
log_error("cannot use more than one --trust");
return -EINVAL;
}
break;
case ARG_SYSTEM:
break;
case ARG_USER:
break;
case 'm':
arg_merge = true;
break;
case 'M':
if (arg_machine) {
log_error("cannot use more than one --machine/-M");
return -EINVAL;
}
break;
case 'D':
if (arg_directory) {
log_error("cannot use more than one --directory/-D");
return -EINVAL;
}
break;
case ARG_FILE:
if (r < 0) {
return r;
};
break;
case ARG_CURSOR:
if (arg_cursor) {
log_error("cannot use more than one --cursor/--after-cursor");
return -EINVAL;
}
arg_cursor = optarg;
break;
case ARG_AFTER_CURSOR:
if (arg_cursor) {
log_error("cannot use more than one --cursor/--after-cursor");
return -EINVAL;
}
arg_cursor = optarg;
arg_after_cursor = true;
break;
case ARG_FOLLOW:
arg_follow = true;
break;
case ARG_NO_FOLLOW:
arg_follow = false;
break;
case ARG_SAVE_STATE:
break;
case '?':
return -EINVAL;
case ':':
return -EINVAL;
default:
assert_not_reached("Unhandled option code.");
}
if (!arg_url) {
log_error("Required --url/-u option missing.");
return -EINVAL;
}
log_error("Options --key and --cert must be used together.");
return -EINVAL;
}
log_error("Input arguments make no sense with journal input.");
return -EINVAL;
}
return 1;
}
static int open_journal(sd_journal **j) {
int r;
if (arg_directory)
else if (arg_file)
r = sd_journal_open_files(j, (const char**) arg_file, 0);
else if (arg_machine)
r = sd_journal_open_container(j, arg_machine, 0);
else
if (r < 0)
log_error("Failed to open %s: %s",
strerror(-r));
return r;
}
Uploader u;
int r;
bool use_journal;
log_show_color(true);
if (r <= 0)
goto finish;
if (r < 0)
goto cleanup;
sd_event_set_watchdog(u.events, true);
if (use_journal) {
sd_journal *j;
r = open_journal(&j);
if (r < 0)
goto finish;
r = open_journal_for_upload(&u, j,
arg_cursor ?: u.last_cursor,
arg_cursor ? arg_after_cursor : true,
!!arg_follow);
if (r < 0)
goto finish;
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing input...");
while (true) {
if (use_journal) {
if (!u.journal)
break;
r = check_journal_input(&u);
} else if (u.input < 0 && !use_journal) {
break;
}
if (r < 0)
goto cleanup;
r = sd_event_get_state(u.events);
if (r < 0)
break;
if (r == SD_EVENT_FINISHED)
break;
if (u.uploading) {
r = perform_upload(&u);
if (r < 0)
break;
}
if (r < 0) {
break;
}
}
sd_notify(false, "STATUS=Shutting down...");
destroy_uploader(&u);
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}