923a60
From 652a44f9a9948a023fd7b26f72044fea0b13c25d Mon Sep 17 00:00:00 2001
923a60
From: Lennart Poettering <lennart@poettering.net>
923a60
Date: Tue, 3 Nov 2015 12:28:19 +0100
923a60
Subject: [PATCH] journal: restore watchdog support
923a60
923a60
(cherry picked from commit 119e9655dc36f18ed74f9a256d5c693b5aeb43ab)
923a60
923a60
Conflicts:
923a60
	src/journal/journald-server.h
923a60
	units/systemd-journald.service.in
923a60
923a60
Related: #1511565
923a60
---
923a60
 src/journal/journald-server.c     | 62 ++++++++++++++++++++++++++++---
923a60
 src/journal/journald-server.h     | 13 ++++---
923a60
 units/systemd-journald.service.in |  1 +
923a60
 3 files changed, 66 insertions(+), 10 deletions(-)
923a60
923a60
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
923a60
index a810829b24..6e7568b60b 100644
923a60
--- a/src/journal/journald-server.c
923a60
+++ b/src/journal/journald-server.c
923a60
@@ -1572,10 +1572,10 @@ static int dispatch_notify_event(sd_event_source *es, int fd, uint32_t revents,
923a60
         }
923a60
 
923a60
         /* The $NOTIFY_SOCKET is writable again, now send exactly one
923a60
-         * message on it. Either it's the initial READY=1 event or an
923a60
-         * stdout stream event. If there's nothing to write anymore,
923a60
-         * turn our event source off. The next time there's something
923a60
-         * to send it will be turned on again. */
923a60
+         * message on it. Either it's the wtachdog event, the initial
923a60
+         * READY=1 event or an stdout stream event. If there's nothing
923a60
+         * to write anymore, turn our event source off. The next time
923a60
+         * there's something to send it will be turned on again. */
923a60
 
923a60
         if (!s->sent_notify_ready) {
923a60
                 static const char p[] =
923a60
@@ -1594,12 +1594,30 @@ static int dispatch_notify_event(sd_event_source *es, int fd, uint32_t revents,
923a60
                 s->sent_notify_ready = true;
923a60
                 log_debug("Sent READY=1 notification.");
923a60
 
923a60
+        } else if (s->send_watchdog) {
923a60
+
923a60
+                static const char p[] =
923a60
+                        "WATCHDOG=1";
923a60
+
923a60
+                ssize_t l;
923a60
+
923a60
+                l = send(s->notify_fd, p, strlen(p), MSG_DONTWAIT);
923a60
+                if (l < 0) {
923a60
+                        if (errno == EAGAIN)
923a60
+                                return 0;
923a60
+
923a60
+                        return log_error_errno(errno, "Failed to send WATCHDOG=1 notification message: %m");
923a60
+                }
923a60
+
923a60
+                s->send_watchdog = false;
923a60
+                log_debug("Sent WATCHDOG=1 notification.");
923a60
+
923a60
         } else if (s->stdout_streams_notify_queue)
923a60
                 /* Dispatch one stream notification event */
923a60
                 stdout_stream_send_notify(s->stdout_streams_notify_queue);
923a60
 
923a60
         /* Leave us enabled if there's still more to to do. */
923a60
-        if (s->stdout_streams_notify_queue)
923a60
+        if (s->send_watchdog || s->stdout_streams_notify_queue)
923a60
                 return 0;
923a60
 
923a60
         /* There was nothing to do anymore, let's turn ourselves off. */
923a60
@@ -1610,6 +1628,29 @@ static int dispatch_notify_event(sd_event_source *es, int fd, uint32_t revents,
923a60
         return 0;
923a60
 }
923a60
 
923a60
+static int dispatch_watchdog(sd_event_source *es, uint64_t usec, void *userdata) {
923a60
+        Server *s = userdata;
923a60
+        int r;
923a60
+
923a60
+        assert(s);
923a60
+
923a60
+        s->send_watchdog = true;
923a60
+
923a60
+        r = sd_event_source_set_enabled(s->notify_event_source, SD_EVENT_ON);
923a60
+        if (r < 0)
923a60
+                log_warning_errno(r, "Failed to turn on notify event source: %m");
923a60
+
923a60
+        r = sd_event_source_set_time(s->watchdog_event_source, usec + s->watchdog_usec / 2);
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to restart watchdog event source: %m");
923a60
+
923a60
+        r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ON);
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to enable watchdog event source: %m");
923a60
+
923a60
+        return 0;
923a60
+}
923a60
+
923a60
 static int server_connect_notify(Server *s) {
923a60
         union sockaddr_union sa = {
923a60
                 .un.sun_family = AF_UNIX,
923a60
@@ -1672,6 +1713,14 @@ static int server_connect_notify(Server *s) {
923a60
         if (r < 0)
923a60
                 return log_error_errno(r, "Failed to watch notification socket: %m");
923a60
 
923a60
+        if (sd_watchdog_enabled(false, &s->watchdog_usec) > 0) {
923a60
+                s->send_watchdog = true;
923a60
+
923a60
+                r = sd_event_add_time(s->event, &s->watchdog_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + s->watchdog_usec/2, s->watchdog_usec*3/4, dispatch_watchdog, s);
923a60
+                if (r < 0)
923a60
+                        return log_error_errno(r, "Failed to add watchdog time event: %m");
923a60
+        }
923a60
+
923a60
         /* This should fire pretty soon, which we'll use to send the
923a60
          * READY=1 event. */
923a60
 
923a60
@@ -1689,6 +1738,8 @@ int server_init(Server *s) {
923a60
         s->compress = true;
923a60
         s->seal = true;
923a60
 
923a60
+        s->watchdog_usec = USEC_INFINITY;
923a60
+
923a60
         s->sync_interval_usec = DEFAULT_SYNC_INTERVAL_USEC;
923a60
         s->sync_scheduled = false;
923a60
 
923a60
@@ -1893,6 +1944,7 @@ void server_done(Server *s) {
923a60
         sd_event_source_unref(s->sigint_event_source);
923a60
         sd_event_source_unref(s->hostname_event_source);
923a60
         sd_event_source_unref(s->notify_event_source);
923a60
+        sd_event_source_unref(s->watchdog_event_source);
923a60
         sd_event_unref(s->event);
923a60
 
923a60
         safe_close(s->syslog_fd);
923a60
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
923a60
index e59ff35e22..f046fde834 100644
923a60
--- a/src/journal/journald-server.h
923a60
+++ b/src/journal/journald-server.h
923a60
@@ -78,6 +78,7 @@ struct Server {
923a60
         sd_event_source *sigint_event_source;
923a60
         sd_event_source *hostname_event_source;
923a60
         sd_event_source *notify_event_source;
923a60
+        sd_event_source *watchdog_event_source;
923a60
 
923a60
         JournalFile *runtime_journal;
923a60
         JournalFile *system_journal;
923a60
@@ -133,14 +134,14 @@ struct Server {
923a60
 
923a60
         MMapCache *mmap;
923a60
 
923a60
-        bool dev_kmsg_readable;
923a60
+        struct udev *udev;
923a60
 
923a60
         uint64_t *kernel_seqnum;
923a60
+        bool dev_kmsg_readable:1;
923a60
 
923a60
-        struct udev *udev;
923a60
-
923a60
-        bool sent_notify_ready;
923a60
-        bool sync_scheduled;
923a60
+        bool send_watchdog:1;
923a60
+        bool sent_notify_ready:1;
923a60
+        bool sync_scheduled:1;
923a60
 
923a60
         char machine_id_field[sizeof("_MACHINE_ID=") + 32];
923a60
         char boot_id_field[sizeof("_BOOT_ID=") + 32];
923a60
@@ -149,6 +150,8 @@ struct Server {
923a60
         /* Cached cgroup root, so that we don't have to query that all the time */
923a60
         char *cgroup_root;
923a60
 
923a60
+        usec_t watchdog_usec;
923a60
+
923a60
         size_t line_max;
923a60
 };
923a60
 
923a60
diff --git a/units/systemd-journald.service.in b/units/systemd-journald.service.in
923a60
index c94c0bfba1..0d1ea61fe8 100644
923a60
--- a/units/systemd-journald.service.in
923a60
+++ b/units/systemd-journald.service.in
923a60
@@ -22,6 +22,7 @@ RestartSec=0
923a60
 StandardOutput=null
923a60
 FileDescriptorStoreMax=4224
923a60
 CapabilityBoundingSet=CAP_SYS_ADMIN CAP_DAC_OVERRIDE CAP_SYS_PTRACE CAP_SYSLOG CAP_AUDIT_CONTROL CAP_AUDIT_READ CAP_CHOWN CAP_DAC_READ_SEARCH CAP_FOWNER CAP_SETUID CAP_SETGID CAP_MAC_OVERRIDE
923a60
+WatchdogSec=3min
923a60
 
923a60
 # Increase the default a bit in order to allow many simultaneous
923a60
 # services being run since we keep one fd open per service. Also, when