chiark / gitweb /
journal-upload: avoid calling printf with maximum precision
[elogind.git] / src / journal-remote / journal-upload.c
index bdeeff67788363d65a16e38fcab7094c08c026be..229bceeb806a4977d9dfe2a2a72de4ce93a80cef 100644 (file)
 #include "util.h"
 #include "build.h"
 #include "fileio.h"
+#include "mkdir.h"
 #include "conf-parser.h"
 #include "journal-upload.h"
 
 #define PRIV_KEY_FILE CERTIFICATE_ROOT "/private/journal-upload.pem"
 #define CERT_FILE     CERTIFICATE_ROOT "/certs/journal-upload.pem"
 #define TRUST_FILE    CERTIFICATE_ROOT "/ca/trusted.pem"
+#define DEFAULT_PORT  19532
 
 static const char* arg_url;
 
@@ -92,6 +94,32 @@ static size_t output_callback(char *buf,
         return size * nmemb;
 }
 
+static int check_cursor_updating(Uploader *u) {
+        _cleanup_free_ char *temp_path = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        int r;
+
+        if (!u->state_file)
+                return 0;
+
+        r = mkdir_parents(u->state_file, 0755);
+        if (r < 0) {
+                log_error("Cannot create parent directory of state file %s: %s",
+                          u->state_file, strerror(-r));
+                return r;
+        }
+
+        r = fopen_temporary(u->state_file, &f, &temp_path);
+        if (r < 0) {
+                log_error("Cannot save state to %s: %s",
+                          u->state_file, strerror(-r));
+                return r;
+        }
+        unlink(temp_path);
+
+        return 0;
+}
+
 static int update_cursor_state(Uploader *u) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
@@ -191,7 +219,7 @@ int start_upload(Uploader *u,
                 easy_setopt(curl, CURLOPT_POST, 1L,
                             LOG_ERR, return -EXFULL);
 
-                easy_setopt(curl, CURLOPT_ERRORBUFFER, &u->error,
+                easy_setopt(curl, CURLOPT_ERRORBUFFER, u->error,
                             LOG_ERR, return -EXFULL);
 
                 /* set where to write to */
@@ -220,8 +248,6 @@ int start_upload(Uploader *u,
                             LOG_WARNING, );
 
                 if (arg_key || startswith(u->url, "https://")) {
-                        assert(arg_cert);
-
                         easy_setopt(curl, CURLOPT_SSLKEY, arg_key ?: PRIV_KEY_FILE,
                                     LOG_ERR, return -EXFULL);
                         easy_setopt(curl, CURLOPT_SSLCERT, arg_cert ?: CERT_FILE,
@@ -394,6 +420,7 @@ static int setup_signals(Uploader *u) {
 
 static int setup_uploader(Uploader *u, const char *url, const char *state_file) {
         int r;
+        const char *host, *proto = "";
 
         assert(u);
         assert(url);
@@ -401,10 +428,24 @@ static int setup_uploader(Uploader *u, const char *url, const char *state_file)
         memzero(u, sizeof(Uploader));
         u->input = -1;
 
-        if (!startswith(url, "http://") && !startswith(url, "https://"))
-                url = strappenda("https://", url);
+        if (!(host = startswith(url, "http://")) && !(host = startswith(url, "https://"))) {
+                host = url;
+                proto = "https://";
+        }
+
+        if (strchr(host, ':'))
+                u->url = strjoin(proto, url, "/upload", NULL);
+        else {
+                char *t;
+                size_t x;
+
+                t = strdupa(url);
+                x = strlen(t);
+                while (x > 0 && t[x - 1] == '/')
+                        t[x - 1] = '\0';
 
-        u->url = strappend(url, "/upload");
+                u->url = strjoin(proto, t, ":" STRINGIFY(DEFAULT_PORT), "/upload", NULL);
+        }
         if (!u->url)
                 return log_oom();
 
@@ -455,10 +496,12 @@ static int perform_upload(Uploader *u) {
 
         code = curl_easy_perform(u->easy);
         if (code) {
-                log_error("Upload to %s failed: %.*s",
-                          u->url,
-                          u->error[0] ? (int) sizeof(u->error) : INT_MAX,
-                          u->error[0] ? u->error : curl_easy_strerror(code));
+                if (u->error[0])
+                        log_error("Upload to %s failed: %.*s",
+                                  u->url, (int) sizeof(u->error), u->error);
+                else
+                        log_error("Upload to %s failed: %s",
+                                  u->url, curl_easy_strerror(code));
                 return -EIO;
         }
 
@@ -507,10 +550,14 @@ static void help(void) {
                "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\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"
+               "  -u --url=URL              Upload to this address (default port "
+                                            STRINGIFY(DEFAULT_PORT) ")\n"
+               "     --key=FILENAME         Specify key in PEM format (default:\n"
+               "                            \"" PRIV_KEY_FILE "\")\n"
+               "     --cert=FILENAME        Specify certificate in PEM format (default:\n"
+               "                            \"" CERT_FILE "\")\n"
+               "     --trust=FILENAME|all   Specify CA certificate or disable checking (default:\n"
+               "                            \"" TRUST_FILE "\")\n"
                "     --system               Use the system journal\n"
                "     --user                 Use the user journal for the current user\n"
                "  -m --merge                Use  all available journals\n"
@@ -761,6 +808,10 @@ int main(int argc, char **argv) {
 
         sd_event_set_watchdog(u.events, true);
 
+        r = check_cursor_updating(&u);
+        if (r < 0)
+                goto cleanup;
+
         log_debug("%s running as pid "PID_FMT,
                   program_invocation_short_name, getpid());
 
@@ -818,7 +869,10 @@ int main(int argc, char **argv) {
         }
 
 cleanup:
-        sd_notify(false, "STATUS=Shutting down...");
+        sd_notify(false,
+                  "STOPPING=1\n"
+                  "STATUS=Shutting down...");
+
         destroy_uploader(&u);
 
 finish: