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