ryantimwilson / rpms / systemd

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