journal-upload.c revision 1fa2f38f0f011010bf57522b42fcc168856a7003
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2014 Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <stdio.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <curl/curl.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <sys/stat.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <fcntl.h>
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include <getopt.h>
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "sd-daemon.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "log.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "util.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "build.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "fileio.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "mkdir.h"
a5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7Harald Hoyer#include "conf-parser.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "sigbus.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "journal-upload.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define CERT_FILE CERTIFICATE_ROOT "/certs/journal-upload.pem"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define TRUST_FILE CERTIFICATE_ROOT "/ca/trusted.pem"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define DEFAULT_PORT 19532
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poetteringstatic const char* arg_url = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_key = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_cert = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_trust = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_directory = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic char **arg_file = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_cursor = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool arg_after_cursor = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int arg_journal_type = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_machine = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic bool arg_merge = false;
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poetteringstatic int arg_follow = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic const char *arg_save_state = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void close_fd_input(Uploader *u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define SERVER_ANSWER_KEEP 2048
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define STATE_FILE "/var/lib/systemd/journal-upload/state"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define easy_setopt(curl, opt, value, level, cmd) \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek do { \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek code = curl_easy_setopt(curl, opt, value); \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (code) { \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_full(level, \
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "curl_easy_setopt " #opt " failed: %s", \
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering curl_easy_strerror(code)); \
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering cmd; \
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz } \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } while(0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic size_t output_callback(char *buf,
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack size_t size,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t nmemb,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *userp) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Uploader *u = userp;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("The server answers (%zu bytes): %.*s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size*nmemb, (int)(size*nmemb), buf);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack if (nmemb && !u->answer) {
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack u->answer = strndup(buf, size*nmemb);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!u->answer)
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack log_warning_errno(ENOMEM, "Failed to store server answer (%zu bytes): %m",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size*nmemb);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return size * nmemb;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmekstatic int check_cursor_updating(Uploader *u) {
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer _cleanup_free_ char *temp_path = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_fclose_ FILE *f = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!u->state_file)
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek r = mkdir_parents(u->state_file, 0755);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Cannot create parent directory of state file %s: %m",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->state_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek r = fopen_temporary(u->state_file, &f, &temp_path);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Cannot save state to %s: %m",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->state_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unlink(temp_path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int update_cursor_state(Uploader *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *temp_path = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_fclose_ FILE *f = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!u->state_file || !u->last_cursor)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = fopen_temporary(u->state_file, &f, &temp_path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fprintf(f,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "# This is private data. Do not parse.\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "LAST_CURSOR=%s\n",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->last_cursor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fflush(f);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (ferror(f) || rename(temp_path, u->state_file) < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unlink(u->state_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unlink(temp_path);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimerfinish:
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer if (r < 0)
0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671Florian Weimer log_error_errno(r, "Failed to save state %s: %m", u->state_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int load_cursor_state(Uploader *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!u->state_file)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = parse_env_file(u->state_file, NEWLINE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "LAST_CURSOR", &u->last_cursor,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r == -ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("State file %s is not present.", u->state_file);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to read state file %s: %m",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->state_file);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek else
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek log_debug("Last cursor was %s", u->last_cursor);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek return 0;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek}
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmekint start_upload(Uploader *u,
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek size_t (*input_callback)(void *ptr,
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek size_t size,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek size_t nmemb,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *userdata),
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek void *data) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek CURLcode code;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert(u);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert(input_callback);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek if (!u->header) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek struct curl_slist *h;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek h = curl_slist_append(NULL, "Content-Type: application/vnd.fdo.journal");
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (!h)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek return log_oom();
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek h = curl_slist_append(h, "Transfer-Encoding: chunked");
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (!h) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek curl_slist_free_all(h);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_oom();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek h = curl_slist_append(h, "Accept: text/plain");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!h) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek curl_slist_free_all(h);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_oom();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->header = h;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering if (!u->easy) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek CURL *curl;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek curl = curl_easy_init();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!curl) {
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering log_error("Call to curl_easy_init failed.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOSR;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* tell it to POST to the URL */
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt easy_setopt(curl, CURLOPT_POST, 1L,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_ERRORBUFFER, u->error,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* set where to write to */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_WRITEFUNCTION, output_callback,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt easy_setopt(curl, CURLOPT_WRITEDATA, data,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* set where to read from */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_READFUNCTION, input_callback,
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) LOG_ERR, return -EXFULL);
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_READDATA, data,
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) LOG_ERR, return -EXFULL);
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig)
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt /* use our special own mime type and chunked transfer */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_HTTPHEADER, u->header,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (_unlikely_(log_get_max_level() >= LOG_DEBUG))
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt /* enable verbose for easier tracing */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_VERBOSE, 1L, LOG_WARNING, );
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_USERAGENT,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "systemd-journal-upload " PACKAGE_STRING,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_WARNING, );
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (arg_key || startswith(u->url, "https://")) {
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_SSLKEY, arg_key ?: PRIV_KEY_FILE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_SSLCERT, arg_cert ?: CERT_FILE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq_ptr(arg_trust, "all"))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EUCLEAN);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else if (arg_trust || startswith(u->url, "https://"))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_CAINFO, arg_trust ?: TRUST_FILE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LOG_ERR, return -EXFULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (arg_key || arg_trust)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1,
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering LOG_WARNING, );
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->easy = curl;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* truncate the potential old error message */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->error[0] = '\0';
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt free(u->answer);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->answer = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek /* upload to this place */
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek code = curl_easy_setopt(u->easy, CURLOPT_URL, u->url);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (code) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("curl_easy_setopt CURLOPT_URL failed: %s",
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt curl_easy_strerror(code));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EXFULL;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->uploading = true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic size_t fd_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Uploader *u = userp;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ssize_t r;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(nmemb <= SSIZE_MAX / size);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (u->input < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = read(u->input, buf, size * nmemb);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("%s: allowed %zu, read %zd", __func__, size*nmemb, r);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r > 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return r;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek u->uploading = false;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r == 0) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_debug("Reached EOF");
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek close_fd_input(u);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return 0;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek } else {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Aborting transfer after read error on input: %m.");
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt return CURL_READFUNC_ABORT;
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek}
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidt
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidtstatic void close_fd_input(Uploader *u) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(u);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (u->input >= 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek close_nointr(u->input);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek u->input = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->timeout = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int dispatch_fd_input(sd_event_source *event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int fd,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint32_t revents,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek void *userp) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Uploader *u = userp;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(fd >= 0);
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (revents & EPOLLHUP) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_debug("Received HUP");
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt close_fd_input(u);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return 0;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!(revents & EPOLLIN)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Unexpected poll event %"PRIu32".", revents);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (u->uploading) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk log_warning("dispatch_fd_input called when uploading, ignoring.");
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return 0;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return start_upload(u, fd_input_callback, u);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk}
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int open_file_for_upload(Uploader *u, const char *filename) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk int fd, r = 0;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (streq(filename, "-"))
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk fd = STDIN_FILENO;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt else {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (fd < 0)
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering return log_error_errno(errno, "Failed to open %s: %m", filename);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering }
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt u->input = fd;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (arg_follow) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk r = sd_event_add_io(u->events, &u->input_event,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk fd, EPOLLIN, dispatch_fd_input, u);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (r < 0) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek if (r != -EPERM || arg_follow > 0)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to register input event: %m");
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek /* Normal files should just be consumed without polling. */
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = start_upload(u, fd_input_callback, u);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek }
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek }
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return r;
dbd2a83fbf051fc51bdca3aa7536c78479488c5bLennart Poettering}
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidtstatic int dispatch_sigterm(sd_event_source *event,
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek const struct signalfd_siginfo *si,
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek void *userdata) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Uploader *u = userdata;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_received_signal(LOG_INFO, si);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_fd_input(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_journal_input(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_exit(u->events, 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int setup_signals(Uploader *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sigset_t mask;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(sigemptyset(&mask) == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sigset_add_many(&mask, SIGINT, SIGTERM, -1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_event_add_signal(u->events, &u->sigterm_event, SIGTERM, dispatch_sigterm, u);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = sd_event_add_signal(u->events, &u->sigint_event, SIGINT, dispatch_sigterm, u);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return 0;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringstatic int setup_uploader(Uploader *u, const char *url, const char *state_file) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering int r;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering const char *host, *proto = "";
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(u);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(url);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering memzero(u, sizeof(Uploader));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering u->input = -1;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!(host = startswith(url, "http://")) && !(host = startswith(url, "https://"))) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering host = url;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering proto = "https://";
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (strchr(host, ':'))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering u->url = strjoin(proto, url, "/upload", NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering else {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering char *t;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering size_t x;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering t = strdupa(url);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering x = strlen(t);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering while (x > 0 && t[x - 1] == '/')
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering t[x - 1] = '\0';
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering u->url = strjoin(proto, t, ":" STRINGIFY(DEFAULT_PORT), "/upload", NULL);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (!u->url)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return log_oom();
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering u->state_file = state_file;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_default(&u->events);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_error_errno(r, "sd_event_default failed: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = setup_signals(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to set up signals: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return load_cursor_state(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void destroy_uploader(Uploader *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek curl_easy_cleanup(u->easy);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek curl_slist_free_all(u->header);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(u->answer);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(u->last_cursor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(u->current_cursor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(u->url);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->input_event = sd_event_source_unref(u->input_event);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_fd_input(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek close_journal_input(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering sd_event_source_unref(u->sigterm_event);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_source_unref(u->sigint_event);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_unref(u->events);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int perform_upload(Uploader *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek CURLcode code;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek long status;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek code = curl_easy_perform(u->easy);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (code) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (u->error[0])
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Upload to %s failed: %.*s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->url, (int) sizeof(u->error), u->error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Upload to %s failed: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->url, curl_easy_strerror(code));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek code = curl_easy_getinfo(u->easy, CURLINFO_RESPONSE_CODE, &status);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (code) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to retrieve response code: %s",
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk curl_easy_strerror(code));
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering return -EUCLEAN;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (status >= 300) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Upload to %s failed with code %ld: %s",
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek u->url, status, strna(u->answer));
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek return -EIO;
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek } else if (status < 200) {
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek log_error("Upload to %s finished with unexpected code %ld: %s",
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek u->url, status, strna(u->answer));
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt return -EIO;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Upload finished successfully with code %ld: %s",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek status, strna(u->answer));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(u->last_cursor);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->last_cursor = u->current_cursor;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek u->current_cursor = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return update_cursor_state(u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int parse_config(void) {
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek const ConfigTableItem items[] = {
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek { "Upload", "URL", config_parse_string, 0, &arg_url },
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek { "Upload", "ServerKeyFile", config_parse_path, 0, &arg_key },
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek { "Upload", "ServerCertificateFile", config_parse_path, 0, &arg_cert },
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek { "Upload", "TrustedCertificateFile", config_parse_path, 0, &arg_trust },
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek {}};
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering return config_parse_many(PKGSYSCONFDIR "/journal-upload.conf",
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering CONF_DIRS_NULSTR("systemd/journal-upload.conf"),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "Upload\0", config_item_table_lookup, items,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek false, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void help(void) {
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering printf("%s -u URL {FILE|-}...\n\n"
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering "Upload journal events to a remote server.\n\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " -h --help Show this help\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " --version Show package version\n"
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering " -u --url=URL Upload to this address (default port "
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek STRINGIFY(DEFAULT_PORT) ")\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " --key=FILENAME Specify key in PEM format (default:\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " \"" PRIV_KEY_FILE "\")\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " --cert=FILENAME Specify certificate in PEM format (default:\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " \"" CERT_FILE "\")\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " --trust=FILENAME|all Specify CA certificate or disable checking (default:\n"
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler " \"" TRUST_FILE "\")\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " --system Use the system journal\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " --user Use the user journal for the current user\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " -m --merge Use all available journals\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " -M --machine=CONTAINER Operate on local container\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " -D --directory=PATH Use journal files from directory\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " --file=PATH Use this journal file\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " --cursor=CURSOR Start at the specified cursor\n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " --after-cursor=CURSOR Start after the specified cursor\n"
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering " --follow[=BOOL] Do [not] wait for input\n"
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering " --save-state[=FILE] Save uploaded cursors (default \n"
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering " " STATE_FILE ")\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " -h --help Show this help and exit\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek " --version Print version string and exit\n"
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek , program_invocation_short_name);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering enum {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering ARG_VERSION = 0x100,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_KEY,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_CERT,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_TRUST,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_USER,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek ARG_SYSTEM,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_FILE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_CURSOR,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_AFTER_CURSOR,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ARG_FOLLOW,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek ARG_SAVE_STATE,
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek static const struct option options[] = {
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther { "help", no_argument, NULL, 'h' },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "version", no_argument, NULL, ARG_VERSION },
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek { "url", required_argument, NULL, 'u' },
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther { "key", required_argument, NULL, ARG_KEY },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "cert", required_argument, NULL, ARG_CERT },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "trust", required_argument, NULL, ARG_TRUST },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "system", no_argument, NULL, ARG_SYSTEM },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek { "user", no_argument, NULL, ARG_USER },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "merge", no_argument, NULL, 'm' },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek { "machine", required_argument, NULL, 'M' },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "directory", required_argument, NULL, 'D' },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "file", required_argument, NULL, ARG_FILE },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "cursor", required_argument, NULL, ARG_CURSOR },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "after-cursor", required_argument, NULL, ARG_AFTER_CURSOR },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek { "follow", optional_argument, NULL, ARG_FOLLOW },
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "save-state", optional_argument, NULL, ARG_SAVE_STATE },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek {}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykryn int c, r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(argc >= 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(argv);
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden opterr = 0;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden while ((c = getopt_long(argc, argv, "hu:mM:D:", options, NULL)) >= 0)
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden switch(c) {
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden case 'h':
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden help();
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0 /* done */;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case ARG_VERSION:
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther puts(PACKAGE_STRING);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek puts(SYSTEMD_FEATURES);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return 0 /* done */;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering case 'u':
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (arg_url) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("cannot use more than one --url");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering arg_url = optarg;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case ARG_KEY:
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (arg_key) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering log_error("cannot use more than one --key");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_key = optarg;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering case ARG_CERT:
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (arg_cert) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("cannot use more than one --cert");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering arg_cert = optarg;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek case ARG_TRUST:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (arg_trust) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("cannot use more than one --trust");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering arg_trust = optarg;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering break;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering case ARG_SYSTEM:
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering arg_journal_type |= SD_JOURNAL_SYSTEM;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering break;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering case ARG_USER:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_journal_type |= SD_JOURNAL_CURRENT_USER;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering case 'm':
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering arg_merge = true;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering break;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering case 'M':
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering if (arg_machine) {
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_error("cannot use more than one --machine/-M");
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering return -EINVAL;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering }
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering arg_machine = optarg;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering break;
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee case 'D':
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee if (arg_directory) {
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler log_error("cannot use more than one --directory/-D");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
6baa7db00812437bbc87e73faa1a11b6cf631958Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering arg_directory = optarg;
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett break;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering case ARG_FILE:
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = glob_extend(&arg_file, optarg);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (r < 0)
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering return log_error_errno(r, "Failed to add paths: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering case ARG_CURSOR:
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman if (arg_cursor) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_error("cannot use more than one --cursor/--after-cursor");
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering return -EINVAL;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek arg_cursor = optarg;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek case ARG_AFTER_CURSOR:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (arg_cursor) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("cannot use more than one --cursor/--after-cursor");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_cursor = optarg;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_after_cursor = true;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek case ARG_FOLLOW:
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (optarg) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = parse_boolean(optarg);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to parse --follow= parameter.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_follow = !!r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek } else
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_follow = true;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek case ARG_SAVE_STATE:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek arg_save_state = optarg ?: STATE_FILE;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek break;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek case '?':
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Unknown option %s.", argv[optind-1]);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek case ':':
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Missing argument to %s.", argv[optind-1]);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek default:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_not_reached("Unhandled option code.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (!arg_url) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Required --url/-u option missing.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (!!arg_key != !!arg_cert) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Options --key and --cert must be used together.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering if (optind < argc && (arg_directory || arg_file || arg_machine || arg_journal_type)) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Input arguments make no sense with journal input.");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return -EINVAL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 1;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int open_journal(sd_journal **j) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (arg_directory)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_journal_open_directory(j, arg_directory, arg_journal_type);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek else if (arg_file)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_journal_open_files(j, (const char**) arg_file, 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek else if (arg_machine)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_journal_open_container(j, arg_machine, 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek else
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = sd_journal_open(j, !arg_merge*SD_JOURNAL_LOCAL_ONLY + arg_journal_type);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering log_error_errno(r, "Failed to open %s: %m",
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering arg_directory ? arg_directory : arg_file ? "files" : "journal");
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering return r;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekint main(int argc, char **argv) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek Uploader u;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering int r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek bool use_journal;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_show_color(true);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_parse_environment();
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = parse_config();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering goto finish;
a569398925430de1f8479262e8ab39502054f2e9Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = parse_argv(argc, argv);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r <= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sigbus_install();
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = setup_uploader(&u, arg_url, arg_save_state);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering goto cleanup;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_event_set_watchdog(u.events, true);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = check_cursor_updating(&u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto cleanup;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers log_debug("%s running as pid "PID_FMT,
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering program_invocation_short_name, getpid());
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering use_journal = optind >= argc;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering if (use_journal) {
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering sd_journal *j;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering r = open_journal(&j);
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering if (r < 0)
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering goto finish;
8a0889dfdafa3054c894e54852d8a9e3a7e8390bLennart Poettering r = open_journal_for_upload(&u, j,
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers arg_cursor ?: u.last_cursor,
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers arg_cursor ? arg_after_cursor : true,
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters !!arg_follow);
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto finish;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sd_notify(false,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "READY=1\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "STATUS=Processing input...");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (true) {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek r = sd_event_get_state(u.events);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r == SD_EVENT_FINISHED)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (use_journal) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!u.journal)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = check_journal_input(&u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (u.input < 0 && !use_journal) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (optind >= argc)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Using %s as input.", argv[optind]);
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering r = open_file_for_upload(&u, argv[optind++]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek goto cleanup;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (u.uploading) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = perform_upload(&u);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_run(u.events, u.timeout);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to run event loop: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering }
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekcleanup:
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek sd_notify(false,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek "STOPPING=1\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "STATUS=Shutting down...");
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer destroy_uploader(&u);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekfinish:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek