/***
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 <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include "sd-daemon.h"
#include "alloc-util.h"
#include "conf-parser.h"
#include "def.h"
#include "fd-util.h"
#include "fileio.h"
#include "formats-util.h"
#include "glob-util.h"
#include "journal-upload.h"
#include "log.h"
#include "mkdir.h"
#include "parse-util.h"
#include "sigbus.h"
#include "signal-util.h"
#include "string-util.h"
#include "util.h"
static bool arg_after_cursor = false;
static int arg_journal_type = 0;
static bool arg_merge = false;
static void close_fd_input(Uploader *u);
do { \
if (code) { \
curl_easy_strerror(code)); \
cmd; \
} \
} while(0)
void *userp) {
assert(u);
log_debug("The server answers (%zu bytes): %.*s",
if (!u->answer)
}
}
int r;
if (!u->state_file)
return 0;
if (r < 0)
return log_error_errno(r, "Cannot create parent directory of state file %s: %m",
u->state_file);
if (r < 0)
return log_error_errno(r, "Cannot save state to %s: %m",
u->state_file);
return 0;
}
int r;
if (!u->state_file || !u->last_cursor)
return 0;
if (r < 0)
goto fail;
fprintf(f,
"# This is private data. Do not parse.\n"
"LAST_CURSOR=%s\n",
u->last_cursor);
r = fflush_and_check(f);
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
return 0;
fail:
if (temp_path)
(void) unlink(u->state_file);
}
int r;
if (!u->state_file)
return 0;
"LAST_CURSOR", &u->last_cursor,
NULL);
if (r == -ENOENT)
else if (r < 0)
return log_error_errno(r, "Failed to read state file %s: %m",
u->state_file);
else
return 0;
}
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, );
}
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 {
return CURL_READFUNC_ABORT;
}
}
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 = 0;
fd = STDIN_FILENO;
else {
if (fd < 0)
}
if (arg_follow) {
if (r < 0) {
if (r != -EPERM || arg_follow > 0)
return log_error_errno(r, "Failed to register input event: %m");
/* 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;
}
int r;
assert(u);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
int r;
assert(u);
u->input = -1;
proto = "https://";
}
else {
char *t;
size_t x;
x = strlen(t);
while (x > 0 && t[x - 1] == '/')
t[x - 1] = '\0';
}
if (!u->url)
return log_oom();
u->state_file = state_file;
r = sd_event_default(&u->events);
if (r < 0)
return log_error_errno(r, "sd_event_default failed: %m");
r = setup_signals(u);
if (r < 0)
return log_error_errno(r, "Failed to set up signals: %m");
return load_cursor_state(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);
}
long status;
assert(u);
if (code) {
if (u->error[0])
log_error("Upload to %s failed: %.*s",
else
log_error("Upload to %s failed: %s",
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 %ld: %s",
return -EIO;
} else if (status < 200) {
log_error("Upload to %s finished with unexpected code %ld: %s",
return -EIO;
} else
log_debug("Upload finished successfully with code %ld: %s",
free(u->last_cursor);
u->last_cursor = u->current_cursor;
u->current_cursor = NULL;
return update_cursor_state(u);
}
static int parse_config(void) {
{}};
false, NULL);
}
static void help(void) {
printf("%s -u URL {FILE|-}...\n\n"
"Upload journal events to a remote server.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" -u --url=URL Upload to this address (default port "
" --key=FILENAME Specify key in PEM format (default:\n"
" --cert=FILENAME Specify certificate in PEM format (default:\n"
" --trust=FILENAME|all Specify CA certificate or disable checking (default:\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"
" --follow[=BOOL] 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 {
};
{}
};
int c, r;
opterr = 0;
switch(c) {
case 'h':
help();
return 0 /* done */;
case ARG_VERSION:
return version();
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 log_error_errno(r, "Failed to add paths: %m");
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:
if (optarg) {
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --follow= parameter.");
return -EINVAL;
}
arg_follow = !!r;
} else
arg_follow = true;
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;
}
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_errno(r, "Failed to open %s: %m",
return r;
}
Uploader u;
int r;
bool use_journal;
log_show_color(true);
r = parse_config();
if (r < 0)
goto finish;
if (r <= 0)
goto finish;
if (r < 0)
goto cleanup;
sd_event_set_watchdog(u.events, true);
r = check_cursor_updating(&u);
if (r < 0)
goto cleanup;
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...");
for (;;) {
r = sd_event_get_state(u.events);
if (r < 0)
break;
if (r == SD_EVENT_FINISHED)
break;
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;
if (u.uploading) {
r = perform_upload(&u);
if (r < 0)
break;
}
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
break;
}
}
sd_notify(false,
"STOPPING=1\n"
"STATUS=Shutting down...");
destroy_uploader(&u);
return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}