journal-upload.c revision 7449bc1f34c206e3ff8e274cd74e2db950d492a1
/*-*- 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 "journal-upload.h"
static const char* arg_url;
static void close_fd_input(Uploader *u);
{ \
if (code) { \
curl_easy_strerror(code)); \
cmd; \
} \
}
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 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, );
}
/* 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;
}
int fd,
void *userp) {
assert(u);
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 (r < 0) {
if (r != -EPERM) {
return r;
}
/* Normal files should just be consumed without polling. */
r = start_upload(u, fd_input_callback, u);
}
return r;
}
int r;
assert(u);
u->input = -1;
r = sd_event_default(&u->events);
if (r < 0) {
return r;
}
return 0;
}
static void destroy_uploader(Uploader *u) {
assert(u);
curl_easy_cleanup(u->easy);
close_fd_input(u);
sd_event_unref(u->events);
}
static void help(void) {
printf("%s -u URL {FILE|-}...\n\n"
"Upload journal events to a remote server.\n\n"
"Options:\n"
" --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"
" -h --help Show this help and exit\n"
" --version Print version string and exit\n"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
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 '?':
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 argument missing.");
return -EINVAL;
}
return 1;
}
Uploader u;
int r;
log_show_color(true);
if (r <= 0)
goto finish;
r = setup_uploader(&u, arg_url);
if (r < 0)
goto cleanup;
sd_notify(false,
"READY=1\n"
"STATUS=Processing input...");
while (true) {
if (u.input < 0) {
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) {
if (code) {
log_error("Upload to %s failed: %s",
r = -EIO;
break;
} else
log_debug("Upload finished successfully.");
}
if (r < 0) {
break;
}
}
sd_notify(false, "STATUS=Shutting down...");
destroy_uploader(&u);
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}