572a44
From 80b37e95f732ab5de22fda0d8d14c7d58ac29877 Mon Sep 17 00:00:00 2001
572a44
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
572a44
Date: Sun, 12 Jan 2014 11:38:56 -0500
572a44
Subject: [PATCH] core: do not segfault if swap activity happens when
572a44
 /proc/swaps is not open
572a44
572a44
In https://bugzilla.redhat.com/show_bug.cgi?id=969795 systemd crashes
572a44
in swap_dispatch_reload called from manager_loop becuase m->proc_swaps
572a44
is NULL. It can legitimately be NULL if something went wrong when
572a44
initially enumerating swap devices when starting the manager. This
572a44
is probably a sign of significant trouble, but let's do our best
572a44
to recover.
572a44
---
572a44
 src/core/swap.c | 45 +++++++++++++++++++++++++++++----------------
572a44
 1 file changed, 29 insertions(+), 16 deletions(-)
572a44
572a44
diff --git a/src/core/swap.c b/src/core/swap.c
572a44
index 147f710..f295b65 100644
572a44
--- a/src/core/swap.c
572a44
+++ b/src/core/swap.c
572a44
@@ -1068,14 +1068,40 @@ static int swap_load_proc_swaps(Manager *m, bool set_flags) {
572a44
         return r;
572a44
 }
572a44
 
572a44
+static int open_proc_swaps(Manager *m) {
572a44
+        if (!m->proc_swaps) {
572a44
+                struct epoll_event ev = {
572a44
+                        .events = EPOLLPRI,
572a44
+                        .data.ptr = &m->swap_watch,
572a44
+                };
572a44
+
572a44
+                m->proc_swaps = fopen("/proc/swaps", "re");
572a44
+                if (!m->proc_swaps)
572a44
+                        return (errno == ENOENT) ? 0 : -errno;
572a44
+
572a44
+                m->swap_watch.type = WATCH_SWAP;
572a44
+                m->swap_watch.fd = fileno(m->proc_swaps);
572a44
+
572a44
+                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
572a44
+                        return -errno;
572a44
+        }
572a44
+
572a44
+        return 0;
572a44
+}
572a44
+
572a44
 int swap_dispatch_reload(Manager *m) {
572a44
         /* This function should go as soon as the kernel properly notifies us */
572a44
+        int r;
572a44
 
572a44
         if (_likely_(!m->request_reload))
572a44
                 return 0;
572a44
 
572a44
         m->request_reload = false;
572a44
 
572a44
+        r = open_proc_swaps(m);
572a44
+        if (r < 0)
572a44
+                return r;
572a44
+
572a44
         return swap_fd_event(m, EPOLLPRI);
572a44
 }
572a44
 
572a44
@@ -1225,22 +1251,9 @@ static int swap_enumerate(Manager *m) {
572a44
         int r;
572a44
         assert(m);
572a44
 
572a44
-        if (!m->proc_swaps) {
572a44
-                struct epoll_event ev = {
572a44
-                        .events = EPOLLPRI,
572a44
-                        .data.ptr = &m->swap_watch,
572a44
-                };
572a44
-
572a44
-                m->proc_swaps = fopen("/proc/swaps", "re");
572a44
-                if (!m->proc_swaps)
572a44
-                        return (errno == ENOENT) ? 0 : -errno;
572a44
-
572a44
-                m->swap_watch.type = WATCH_SWAP;
572a44
-                m->swap_watch.fd = fileno(m->proc_swaps);
572a44
-
572a44
-                if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
572a44
-                        return -errno;
572a44
-        }
572a44
+        r = open_proc_swaps(m);
572a44
+        if (r < 0)
572a44
+                return r;
572a44
 
572a44
         r = swap_load_proc_swaps(m, false);
572a44
         if (r < 0)