dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
f4d4e0
From 6181124fac6fbfd83e12fbe5f0068da826ab5772 Mon Sep 17 00:00:00 2001
f4d4e0
From: Lennart Poettering <lennart@poettering.net>
f4d4e0
Date: Mon, 25 Sep 2017 19:53:19 +0200
f4d4e0
Subject: [PATCH] swap: adjust swap.c in a similar way to what we just did to
f4d4e0
 mount.c
f4d4e0
f4d4e0
Also drop the redundant states and make all similar changes too.
f4d4e0
Thankfully the swap.c state engine is much simpler than mount.c's, hence
f4d4e0
this should be easier to digest.
f4d4e0
f4d4e0
(cherry picked from commit 50864457e1bc5f7a4ab2fd02e1565bc5d135d2f3)
f4d4e0
(cherry picked from commit 714dbded2ac725e1e2e6b61a289c118dae555dee)
f4d4e0
f4d4e0
Related: #1821261
f4d4e0
---
f4d4e0
 src/core/swap.c | 96 +++++++++++++++++++++++--------------------------
f4d4e0
 src/core/swap.h |  2 --
f4d4e0
 2 files changed, 44 insertions(+), 54 deletions(-)
f4d4e0
f4d4e0
diff --git a/src/core/swap.c b/src/core/swap.c
f4d4e0
index 4a5e882332..c9cce3d945 100644
f4d4e0
--- a/src/core/swap.c
f4d4e0
+++ b/src/core/swap.c
f4d4e0
@@ -49,8 +49,6 @@ static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
f4d4e0
         [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
f4d4e0
         [SWAP_ACTIVE] = UNIT_ACTIVE,
f4d4e0
         [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
f4d4e0
-        [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
f4d4e0
-        [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
f4d4e0
         [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
f4d4e0
         [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
f4d4e0
         [SWAP_FAILED] = UNIT_FAILED
f4d4e0
@@ -490,8 +488,6 @@ static void swap_set_state(Swap *s, SwapState state) {
f4d4e0
         s->state = state;
f4d4e0
 
f4d4e0
         if (state != SWAP_ACTIVATING &&
f4d4e0
-            state != SWAP_ACTIVATING_SIGTERM &&
f4d4e0
-            state != SWAP_ACTIVATING_SIGKILL &&
f4d4e0
             state != SWAP_ACTIVATING_DONE &&
f4d4e0
             state != SWAP_DEACTIVATING &&
f4d4e0
             state != SWAP_DEACTIVATING_SIGTERM &&
f4d4e0
@@ -538,8 +534,6 @@ static int swap_coldplug(Unit *u, Hashmap *deferred_work) {
f4d4e0
                 return 0;
f4d4e0
 
f4d4e0
         if (new_state == SWAP_ACTIVATING ||
f4d4e0
-            new_state == SWAP_ACTIVATING_SIGTERM ||
f4d4e0
-            new_state == SWAP_ACTIVATING_SIGKILL ||
f4d4e0
             new_state == SWAP_ACTIVATING_DONE ||
f4d4e0
             new_state == SWAP_DEACTIVATING ||
f4d4e0
             new_state == SWAP_DEACTIVATING_SIGTERM ||
f4d4e0
@@ -682,6 +676,15 @@ static void swap_enter_active(Swap *s, SwapResult f) {
f4d4e0
         swap_set_state(s, SWAP_ACTIVE);
f4d4e0
 }
f4d4e0
 
f4d4e0
+static void swap_enter_dead_or_active(Swap *s, SwapResult f) {
f4d4e0
+        assert(s);
f4d4e0
+
f4d4e0
+        if (s->from_proc_swaps)
f4d4e0
+                swap_enter_active(s, f);
f4d4e0
+        else
f4d4e0
+                swap_enter_dead(s, f);
f4d4e0
+}
f4d4e0
+
f4d4e0
 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
f4d4e0
         int r;
f4d4e0
 
f4d4e0
@@ -693,8 +696,7 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
f4d4e0
         r = unit_kill_context(
f4d4e0
                         UNIT(s),
f4d4e0
                         &s->kill_context,
f4d4e0
-                        (state != SWAP_ACTIVATING_SIGTERM && state != SWAP_DEACTIVATING_SIGTERM) ?
f4d4e0
-                        KILL_KILL : KILL_TERMINATE,
f4d4e0
+                        state != SWAP_DEACTIVATING_SIGTERM ? KILL_KILL : KILL_TERMINATE,
f4d4e0
                         -1,
f4d4e0
                         s->control_pid,
f4d4e0
                         false);
f4d4e0
@@ -707,18 +709,16 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
f4d4e0
                         goto fail;
f4d4e0
 
f4d4e0
                 swap_set_state(s, state);
f4d4e0
-        } else if (state == SWAP_ACTIVATING_SIGTERM)
f4d4e0
-                swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_SUCCESS);
f4d4e0
-        else if (state == SWAP_DEACTIVATING_SIGTERM)
f4d4e0
+        } else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill)
f4d4e0
                 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
f4d4e0
         else
f4d4e0
-                swap_enter_dead(s, SWAP_SUCCESS);
f4d4e0
+                swap_enter_dead_or_active(s, SWAP_SUCCESS);
f4d4e0
 
f4d4e0
         return;
f4d4e0
 
f4d4e0
 fail:
f4d4e0
         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to kill processes: %m", UNIT(s)->id);
f4d4e0
-        swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
f4d4e0
+        swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
f4d4e0
 }
f4d4e0
 
f4d4e0
 static void swap_enter_activating(Swap *s) {
f4d4e0
@@ -781,7 +781,7 @@ static void swap_enter_activating(Swap *s) {
f4d4e0
 
f4d4e0
 fail:
f4d4e0
         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'swapon' task: %m", UNIT(s)->id);
f4d4e0
-        swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
f4d4e0
+        swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
f4d4e0
 }
f4d4e0
 
f4d4e0
 static void swap_enter_deactivating(Swap *s) {
f4d4e0
@@ -811,7 +811,7 @@ static void swap_enter_deactivating(Swap *s) {
f4d4e0
 
f4d4e0
 fail:
f4d4e0
         log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'swapoff' task: %m", UNIT(s)->id);
f4d4e0
-        swap_enter_active(s, SWAP_FAILURE_RESOURCES);
f4d4e0
+        swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
f4d4e0
 }
f4d4e0
 
f4d4e0
 static int swap_start(Unit *u) {
f4d4e0
@@ -824,11 +824,10 @@ static int swap_start(Unit *u) {
f4d4e0
 
f4d4e0
         if (s->state == SWAP_DEACTIVATING ||
f4d4e0
             s->state == SWAP_DEACTIVATING_SIGTERM ||
f4d4e0
-            s->state == SWAP_DEACTIVATING_SIGKILL ||
f4d4e0
-            s->state == SWAP_ACTIVATING_SIGTERM ||
f4d4e0
-            s->state == SWAP_ACTIVATING_SIGKILL)
f4d4e0
+            s->state == SWAP_DEACTIVATING_SIGKILL)
f4d4e0
                 return -EAGAIN;
f4d4e0
 
f4d4e0
+        /* Already on it! */
f4d4e0
         if (s->state == SWAP_ACTIVATING)
f4d4e0
                 return 0;
f4d4e0
 
f4d4e0
@@ -854,22 +853,30 @@ static int swap_stop(Unit *u) {
f4d4e0
 
f4d4e0
         assert(s);
f4d4e0
 
f4d4e0
-        if (s->state == SWAP_DEACTIVATING ||
f4d4e0
-            s->state == SWAP_DEACTIVATING_SIGTERM ||
f4d4e0
-            s->state == SWAP_DEACTIVATING_SIGKILL ||
f4d4e0
-            s->state == SWAP_ACTIVATING_SIGTERM ||
f4d4e0
-            s->state == SWAP_ACTIVATING_SIGKILL)
f4d4e0
+        switch (s->state) {
f4d4e0
+
f4d4e0
+        case SWAP_DEACTIVATING:
f4d4e0
+        case SWAP_DEACTIVATING_SIGTERM:
f4d4e0
+        case SWAP_DEACTIVATING_SIGKILL:
f4d4e0
+                /* Already on it */
f4d4e0
+                return 0;
f4d4e0
+
f4d4e0
+        case SWAP_ACTIVATING:
f4d4e0
+        case SWAP_ACTIVATING_DONE:
f4d4e0
+                /* There's a control process pending, directly enter kill mode */
f4d4e0
+                swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS);
f4d4e0
                 return 0;
f4d4e0
 
f4d4e0
-        assert(s->state == SWAP_ACTIVATING ||
f4d4e0
-               s->state == SWAP_ACTIVATING_DONE ||
f4d4e0
-               s->state == SWAP_ACTIVE);
f4d4e0
+        case SWAP_ACTIVE:
f4d4e0
+                if (detect_container(NULL) > 0)
f4d4e0
+                        return -EPERM;
f4d4e0
 
f4d4e0
-        if (detect_container(NULL) > 0)
f4d4e0
-                return -EPERM;
f4d4e0
+                swap_enter_deactivating(s);
f4d4e0
+                return 1;
f4d4e0
 
f4d4e0
-        swap_enter_deactivating(s);
f4d4e0
-        return 1;
f4d4e0
+        default:
f4d4e0
+                assert_not_reached("Unexpected state.");
f4d4e0
+        }
f4d4e0
 }
f4d4e0
 
f4d4e0
 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
f4d4e0
@@ -1002,10 +1009,8 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
f4d4e0
 
f4d4e0
         case SWAP_ACTIVATING:
f4d4e0
         case SWAP_ACTIVATING_DONE:
f4d4e0
-        case SWAP_ACTIVATING_SIGTERM:
f4d4e0
-        case SWAP_ACTIVATING_SIGKILL:
f4d4e0
 
f4d4e0
-                if (f == SWAP_SUCCESS)
f4d4e0
+                if (f == SWAP_SUCCESS || s->from_proc_swaps)
f4d4e0
                         swap_enter_active(s, f);
f4d4e0
                 else
f4d4e0
                         swap_enter_dead(s, f);
f4d4e0
@@ -1015,7 +1020,7 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
f4d4e0
         case SWAP_DEACTIVATING_SIGKILL:
f4d4e0
         case SWAP_DEACTIVATING_SIGTERM:
f4d4e0
 
f4d4e0
-                swap_enter_dead(s, f);
f4d4e0
+                swap_enter_dead_or_active(s, f);
f4d4e0
                 break;
f4d4e0
 
f4d4e0
         default:
f4d4e0
@@ -1037,7 +1042,7 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd
f4d4e0
         case SWAP_ACTIVATING:
f4d4e0
         case SWAP_ACTIVATING_DONE:
f4d4e0
                 log_unit_warning(UNIT(s)->id, "%s activation timed out. Stopping.", UNIT(s)->id);
f4d4e0
-                swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
f4d4e0
+                swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
f4d4e0
                 break;
f4d4e0
 
f4d4e0
         case SWAP_DEACTIVATING:
f4d4e0
@@ -1045,30 +1050,19 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd
f4d4e0
                 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
f4d4e0
                 break;
f4d4e0
 
f4d4e0
-        case SWAP_ACTIVATING_SIGTERM:
f4d4e0
-                if (s->kill_context.send_sigkill) {
f4d4e0
-                        log_unit_warning(UNIT(s)->id, "%s activation timed out. Killing.", UNIT(s)->id);
f4d4e0
-                        swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
f4d4e0
-                } else {
f4d4e0
-                        log_unit_warning(UNIT(s)->id, "%s activation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
f4d4e0
-                        swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
f4d4e0
-                }
f4d4e0
-                break;
f4d4e0
-
f4d4e0
         case SWAP_DEACTIVATING_SIGTERM:
f4d4e0
                 if (s->kill_context.send_sigkill) {
f4d4e0
-                        log_unit_warning(UNIT(s)->id, "%s deactivation timed out. Killing.", UNIT(s)->id);
f4d4e0
+                        log_unit_warning(UNIT(s)->id, "Swap process timed out. Killing.");
f4d4e0
                         swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
f4d4e0
                 } else {
f4d4e0
-                        log_unit_warning(UNIT(s)->id, "%s deactivation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
f4d4e0
-                        swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
f4d4e0
+                        log_unit_warning(UNIT(s)->id, "Swap process timed out. Skipping SIGKILL. Ignoring.");
f4d4e0
+                        swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
f4d4e0
                 }
f4d4e0
                 break;
f4d4e0
 
f4d4e0
-        case SWAP_ACTIVATING_SIGKILL:
f4d4e0
         case SWAP_DEACTIVATING_SIGKILL:
f4d4e0
                 log_unit_warning(UNIT(s)->id, "%s swap process still around after SIGKILL. Ignoring.", UNIT(s)->id);
f4d4e0
-                swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
f4d4e0
+                swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
f4d4e0
                 break;
f4d4e0
 
f4d4e0
         default:
f4d4e0
@@ -1428,8 +1422,6 @@ static const char* const swap_state_table[_SWAP_STATE_MAX] = {
f4d4e0
         [SWAP_ACTIVATING_DONE] = "activating-done",
f4d4e0
         [SWAP_ACTIVE] = "active",
f4d4e0
         [SWAP_DEACTIVATING] = "deactivating",
f4d4e0
-        [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
f4d4e0
-        [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
f4d4e0
         [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
f4d4e0
         [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
f4d4e0
         [SWAP_FAILED] = "failed"
f4d4e0
diff --git a/src/core/swap.h b/src/core/swap.h
f4d4e0
index 914a2dbccd..f46873b5e1 100644
f4d4e0
--- a/src/core/swap.h
f4d4e0
+++ b/src/core/swap.h
f4d4e0
@@ -34,8 +34,6 @@ typedef enum SwapState {
f4d4e0
         SWAP_ACTIVATING_DONE,          /* /sbin/swapon is running, and the swap is done. */
f4d4e0
         SWAP_ACTIVE,
f4d4e0
         SWAP_DEACTIVATING,
f4d4e0
-        SWAP_ACTIVATING_SIGTERM,
f4d4e0
-        SWAP_ACTIVATING_SIGKILL,
f4d4e0
         SWAP_DEACTIVATING_SIGTERM,
f4d4e0
         SWAP_DEACTIVATING_SIGKILL,
f4d4e0
         SWAP_FAILED,