chiark / gitweb /
journal-upload: add watchdog support
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 2 Apr 2014 00:30:13 +0000 (20:30 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Jul 2014 02:23:48 +0000 (22:23 -0400)
src/journal-remote/journal-remote.c
src/journal-remote/journal-upload.c
src/journal-remote/journal-upload.h

index 906759b..063d6df 100644 (file)
@@ -1252,6 +1252,8 @@ int main(int argc, char **argv) {
         if (remoteserver_init(&s) < 0)
                 return EXIT_FAILURE;
 
+        sd_event_set_watchdog(s.events, true);
+
         log_debug("%s running as pid "PID_FMT,
                   program_invocation_short_name, getpid());
         sd_notify(false,
index 7685537..264f915 100644 (file)
@@ -340,6 +340,43 @@ static int open_file_for_upload(Uploader *u, const char *filename) {
         return r;
 }
 
+static int dispatch_sigterm(sd_event_source *event,
+                            const struct signalfd_siginfo *si,
+                            void *userdata) {
+        Uploader *u = userdata;
+
+        assert(u);
+
+        log_received_signal(LOG_INFO, si);
+
+        close_fd_input(u);
+        close_journal_input(u);
+
+        sd_event_exit(u->events, 0);
+        return 0;
+}
+
+static int setup_signals(Uploader *u) {
+        sigset_t mask;
+        int r;
+
+        assert(u);
+
+        assert_se(sigemptyset(&mask) == 0);
+        sigset_add_many(&mask, SIGINT, SIGTERM, -1);
+        assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
+
+        r = sd_event_add_signal(u->events, &u->sigterm_event, SIGTERM, dispatch_sigterm, u);
+        if (r < 0)
+                return r;
+
+        r = sd_event_add_signal(u->events, &u->sigint_event, SIGINT, dispatch_sigterm, u);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 static int setup_uploader(Uploader *u, const char *url, const char *state_file) {
         int r;
 
@@ -358,6 +395,12 @@ static int setup_uploader(Uploader *u, const char *url, const char *state_file)
                 return r;
         }
 
+        r = setup_signals(u);
+        if (r < 0) {
+                log_error("Failed to set up signals: %s", strerror(-r));
+                return r;
+        }
+
         return load_cursor_state(u);
 }
 
@@ -376,6 +419,8 @@ static void destroy_uploader(Uploader *u) {
         close_fd_input(u);
         close_journal_input(u);
 
+        sd_event_source_unref(u->sigterm_event);
+        sd_event_source_unref(u->sigint_event);
         sd_event_unref(u->events);
 }
 
@@ -668,6 +713,8 @@ int main(int argc, char **argv) {
         if (r < 0)
                 goto cleanup;
 
+        sd_event_set_watchdog(u.events, true);
+
         log_debug("%s running as pid "PID_FMT,
                   program_invocation_short_name, getpid());
 
index 0adb915..9ccad10 100644 (file)
@@ -21,6 +21,7 @@ typedef enum {
 
 typedef struct Uploader {
         sd_event *events;
+        sd_event_source *sigint_event, *sigterm_event;
 
         const char *url;
         CURL *easy;