chiark / gitweb /
bootchart: switch to log_* helpers
[elogind.git] / src / journal-remote / journal-gatewayd.c
index 7a99430a63c951e9b1556cb92bf3b60e7ba0f190..bba0d12ee78000af5fee8f8ff34dc05901deb71f 100644 (file)
 #include <gnutls/gnutls.h>
 #endif
 
-#include "log.h"
-#include "util.h"
 #include "sd-journal.h"
 #include "sd-daemon.h"
 #include "sd-bus.h"
+#include "log.h"
+#include "util.h"
 #include "bus-util.h"
 #include "logs-show.h"
 #include "microhttpd-util.h"
 #include "build.h"
 #include "fileio.h"
+#include "sigbus.h"
 
-static char *key_pem = NULL;
-static char *cert_pem = NULL;
-static char *trust_pem = NULL;
+static char *arg_key_pem = NULL;
+static char *arg_cert_pem = NULL;
+static char *arg_trust_pem = NULL;
 
 typedef struct RequestMeta {
         sd_journal *journal;
@@ -120,6 +121,26 @@ static int open_journal(RequestMeta *m) {
         return sd_journal_open(&m->journal, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM);
 }
 
+static int request_meta_ensure_tmp(RequestMeta *m) {
+        if (m->tmp)
+                rewind(m->tmp);
+        else {
+                int fd;
+
+                fd = open_tmpfile("/tmp", O_RDWR|O_CLOEXEC);
+                if (fd < 0)
+                        return fd;
+
+                m->tmp = fdopen(fd, "rw");
+                if (!m->tmp) {
+                        safe_close(fd);
+                        return -errno;
+                }
+        }
+
+        return 0;
+}
+
 static ssize_t request_reader_entries(
                 void *cls,
                 uint64_t pos,
@@ -193,14 +214,10 @@ static ssize_t request_reader_entries(
 
                 m->n_skip = 0;
 
-                if (m->tmp)
-                        rewind(m->tmp);
-                else {
-                        m->tmp = tmpfile();
-                        if (!m->tmp) {
-                                log_error_errno(errno, "Failed to create temporary file: %m");
-                                return MHD_CONTENT_READER_END_WITH_ERROR;
-                        }
+                r = request_meta_ensure_tmp(m);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to create temporary file: %m");
+                        return MHD_CONTENT_READER_END_WITH_ERROR;
                 }
 
                 r = output_journal(m->tmp, m->journal, m->mode, 0, OUTPUT_FULL_WIDTH, NULL);
@@ -554,14 +571,10 @@ static ssize_t request_reader_fields(
                 if (m->n_fields_set)
                         m->n_fields -= 1;
 
-                if (m->tmp)
-                        rewind(m->tmp);
-                else {
-                        m->tmp = tmpfile();
-                        if (!m->tmp) {
-                                log_error_errno(errno, "Failed to create temporary file: %m");
-                                return MHD_CONTENT_READER_END_WITH_ERROR;
-                        }
+                r = request_meta_ensure_tmp(m);
+                if (r < 0) {
+                        log_error_errno(r, "Failed to create temporary file: %m");
+                        return MHD_CONTENT_READER_END_WITH_ERROR;
                 }
 
                 r = output_field(m->tmp, m->mode, d, l);
@@ -735,7 +748,7 @@ static int request_handler_machine(
         RequestMeta *m = connection_cls;
         int r;
         _cleanup_free_ char* hostname = NULL, *os_name = NULL;
-        uint64_t cutoff_from = 0, cutoff_to = 0, usage;
+        uint64_t cutoff_from = 0, cutoff_to = 0, usage = 0;
         char *json;
         sd_id128_t mid, bid;
         _cleanup_free_ char *v = NULL;
@@ -833,7 +846,7 @@ static int request_handler(
                 return MHD_YES;
         }
 
-        if (trust_pem) {
+        if (arg_trust_pem) {
                 r = check_permissions(connection, &code, NULL);
                 if (r < 0)
                         return code;
@@ -904,37 +917,37 @@ static int parse_argv(int argc, char *argv[]) {
                         return 0;
 
                 case ARG_KEY:
-                        if (key_pem) {
+                        if (arg_key_pem) {
                                 log_error("Key file specified twice");
                                 return -EINVAL;
                         }
-                        r = read_full_file(optarg, &key_pem, NULL);
+                        r = read_full_file(optarg, &arg_key_pem, NULL);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to read key file: %m");
-                        assert(key_pem);
+                        assert(arg_key_pem);
                         break;
 
                 case ARG_CERT:
-                        if (cert_pem) {
+                        if (arg_cert_pem) {
                                 log_error("Certificate file specified twice");
                                 return -EINVAL;
                         }
-                        r = read_full_file(optarg, &cert_pem, NULL);
+                        r = read_full_file(optarg, &arg_cert_pem, NULL);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to read certificate file: %m");
-                        assert(cert_pem);
+                        assert(arg_cert_pem);
                         break;
 
                 case ARG_TRUST:
 #ifdef HAVE_GNUTLS
-                        if (trust_pem) {
+                        if (arg_trust_pem) {
                                 log_error("CA certificate file specified twice");
                                 return -EINVAL;
                         }
-                        r = read_full_file(optarg, &trust_pem, NULL);
+                        r = read_full_file(optarg, &arg_trust_pem, NULL);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to read CA certificate file: %m");
-                        assert(trust_pem);
+                        assert(arg_trust_pem);
                         break;
 #else
                         log_error("Option --trust is not available.");
@@ -952,12 +965,12 @@ static int parse_argv(int argc, char *argv[]) {
                 return -EINVAL;
         }
 
-        if (!!key_pem != !!cert_pem) {
+        if (!!arg_key_pem != !!arg_cert_pem) {
                 log_error("Certificate and key files must be specified together");
                 return -EINVAL;
         }
 
-        if (trust_pem && !key_pem) {
+        if (arg_trust_pem && !arg_key_pem) {
                 log_error("CA certificate can only be used with certificate file");
                 return -EINVAL;
         }
@@ -979,10 +992,11 @@ int main(int argc, char *argv[]) {
         if (r == 0)
                 return EXIT_SUCCESS;
 
-#ifdef HAVE_GNUTLS
-        gnutls_global_set_log_function(log_func_gnutls);
-        log_reset_gnutls_level();
-#endif
+        sigbus_install();
+
+        r = setup_gnutls_logger(NULL);
+        if (r < 0)
+                return EXIT_FAILURE;
 
         n = sd_listen_fds(1);
         if (n < 0) {
@@ -1008,18 +1022,18 @@ int main(int argc, char *argv[]) {
                 if (n > 0)
                         opts[opts_pos++] = (struct MHD_OptionItem)
                                 {MHD_OPTION_LISTEN_SOCKET, SD_LISTEN_FDS_START};
-                if (key_pem) {
-                        assert(cert_pem);
+                if (arg_key_pem) {
+                        assert(arg_cert_pem);
                         opts[opts_pos++] = (struct MHD_OptionItem)
-                                {MHD_OPTION_HTTPS_MEM_KEY, 0, key_pem};
+                                {MHD_OPTION_HTTPS_MEM_KEY, 0, arg_key_pem};
                         opts[opts_pos++] = (struct MHD_OptionItem)
-                                {MHD_OPTION_HTTPS_MEM_CERT, 0, cert_pem};
+                                {MHD_OPTION_HTTPS_MEM_CERT, 0, arg_cert_pem};
                         flags |= MHD_USE_SSL;
                 }
-                if (trust_pem) {
+                if (arg_trust_pem) {
                         assert(flags & MHD_USE_SSL);
                         opts[opts_pos++] = (struct MHD_OptionItem)
-                                {MHD_OPTION_HTTPS_MEM_TRUST, 0, trust_pem};
+                                {MHD_OPTION_HTTPS_MEM_TRUST, 0, arg_trust_pem};
                 }
 
                 d = MHD_start_daemon(flags, 19531,