Blame SOURCES/0577-journal-restore-watchdog-support.patch

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