diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-09-25 19:53:19 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-09-26 16:17:22 +0200 |
commit | 50864457e1bc5f7a4ab2fd02e1565bc5d135d2f3 (patch) | |
tree | e56bff1d2d37982a77e7921089e37bcdca0e8a1b /src/core/swap.c | |
parent | mount: rename mount_state_active() → MOUNT_STATE_WITH_PROCESS() (diff) | |
download | systemd-50864457e1bc5f7a4ab2fd02e1565bc5d135d2f3.tar.gz systemd-50864457e1bc5f7a4ab2fd02e1565bc5d135d2f3.tar.bz2 systemd-50864457e1bc5f7a4ab2fd02e1565bc5d135d2f3.zip |
swap: adjust swap.c in a similar way to what we just did to mount.c
Also drop the redundant states and make all similar changes too.
Thankfully the swap.c state engine is much simpler than mount.c's, hence
this should be easier to digest.
Diffstat (limited to 'src/core/swap.c')
-rw-r--r-- | src/core/swap.c | 96 |
1 files changed, 45 insertions, 51 deletions
diff --git a/src/core/swap.c b/src/core/swap.c index d58f68458..0cf03fc35 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -49,8 +49,6 @@ static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = { [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE, [SWAP_ACTIVE] = UNIT_ACTIVE, [SWAP_DEACTIVATING] = UNIT_DEACTIVATING, - [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING, - [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING, [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING, [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING, [SWAP_FAILED] = UNIT_FAILED @@ -489,8 +487,6 @@ static void swap_set_state(Swap *s, SwapState state) { if (!IN_SET(state, SWAP_ACTIVATING, - SWAP_ACTIVATING_SIGTERM, - SWAP_ACTIVATING_SIGKILL, SWAP_ACTIVATING_DONE, SWAP_DEACTIVATING, SWAP_DEACTIVATING_SIGTERM, @@ -536,8 +532,6 @@ static int swap_coldplug(Unit *u) { pid_is_unwaited(s->control_pid) && IN_SET(new_state, SWAP_ACTIVATING, - SWAP_ACTIVATING_SIGTERM, - SWAP_ACTIVATING_SIGKILL, SWAP_ACTIVATING_DONE, SWAP_DEACTIVATING, SWAP_DEACTIVATING_SIGTERM, @@ -693,6 +687,15 @@ static void swap_enter_active(Swap *s, SwapResult f) { swap_set_state(s, SWAP_ACTIVE); } +static void swap_enter_dead_or_active(Swap *s, SwapResult f) { + assert(s); + + if (s->from_proc_swaps) + swap_enter_active(s, f); + else + swap_enter_dead(s, f); +} + static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) { int r; KillOperation kop; @@ -702,7 +705,7 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) { if (s->result == SWAP_SUCCESS) s->result = f; - if (IN_SET(state, SWAP_ACTIVATING_SIGTERM, SWAP_DEACTIVATING_SIGTERM)) + if (state == SWAP_DEACTIVATING_SIGTERM) kop = KILL_TERMINATE; else kop = KILL_KILL; @@ -717,18 +720,16 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) { goto fail; swap_set_state(s, state); - } else if (state == SWAP_ACTIVATING_SIGTERM) - swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_SUCCESS); - else if (state == SWAP_DEACTIVATING_SIGTERM) + } else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill) swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS); else - swap_enter_dead(s, SWAP_SUCCESS); + swap_enter_dead_or_active(s, SWAP_SUCCESS); return; fail: log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); - swap_enter_dead(s, SWAP_FAILURE_RESOURCES); + swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES); } static void swap_enter_activating(Swap *s) { @@ -786,7 +787,7 @@ static void swap_enter_activating(Swap *s) { fail: log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m"); - swap_enter_dead(s, SWAP_FAILURE_RESOURCES); + swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES); } static void swap_enter_deactivating(Swap *s) { @@ -816,7 +817,7 @@ static void swap_enter_deactivating(Swap *s) { fail: log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m"); - swap_enter_active(s, SWAP_FAILURE_RESOURCES); + swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES); } static int swap_start(Unit *u) { @@ -825,17 +826,14 @@ static int swap_start(Unit *u) { assert(s); - /* We cannot fulfill this request right now, try again later - * please! */ - + /* We cannot fulfill this request right now, try again later please! */ if (IN_SET(s->state, SWAP_DEACTIVATING, SWAP_DEACTIVATING_SIGTERM, - SWAP_DEACTIVATING_SIGKILL, - SWAP_ACTIVATING_SIGTERM, - SWAP_ACTIVATING_SIGKILL)) + SWAP_DEACTIVATING_SIGKILL)) return -EAGAIN; + /* Already on it! */ if (s->state == SWAP_ACTIVATING) return 0; @@ -873,21 +871,30 @@ static int swap_stop(Unit *u) { assert(s); - if (IN_SET(s->state, - SWAP_DEACTIVATING, - SWAP_DEACTIVATING_SIGTERM, - SWAP_DEACTIVATING_SIGKILL, - SWAP_ACTIVATING_SIGTERM, - SWAP_ACTIVATING_SIGKILL)) + switch (s->state) { + + case SWAP_DEACTIVATING: + case SWAP_DEACTIVATING_SIGTERM: + case SWAP_DEACTIVATING_SIGKILL: + /* Already on it */ return 0; - assert(IN_SET(s->state, SWAP_ACTIVATING, SWAP_ACTIVATING_DONE, SWAP_ACTIVE)); + case SWAP_ACTIVATING: + case SWAP_ACTIVATING_DONE: + /* There's a control process pending, directly enter kill mode */ + swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS); + return 0; - if (detect_container() > 0) - return -EPERM; + case SWAP_ACTIVE: + if (detect_container() > 0) + return -EPERM; - swap_enter_deactivating(s); - return 1; + swap_enter_deactivating(s); + return 1; + + default: + assert_not_reached("Unexpected state."); + } } static int swap_serialize(Unit *u, FILE *f, FDSet *fds) { @@ -1015,10 +1022,8 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) { case SWAP_ACTIVATING: case SWAP_ACTIVATING_DONE: - case SWAP_ACTIVATING_SIGTERM: - case SWAP_ACTIVATING_SIGKILL: - if (f == SWAP_SUCCESS) + if (f == SWAP_SUCCESS || s->from_proc_swaps) swap_enter_active(s, f); else swap_enter_dead(s, f); @@ -1028,7 +1033,7 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) { case SWAP_DEACTIVATING_SIGKILL: case SWAP_DEACTIVATING_SIGTERM: - swap_enter_dead(s, f); + swap_enter_dead_or_active(s, f); break; default: @@ -1050,7 +1055,7 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd case SWAP_ACTIVATING: case SWAP_ACTIVATING_DONE: log_unit_warning(UNIT(s), "Activation timed out. Stopping."); - swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT); + swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT); break; case SWAP_DEACTIVATING: @@ -1058,30 +1063,19 @@ static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userd swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT); break; - case SWAP_ACTIVATING_SIGTERM: - if (s->kill_context.send_sigkill) { - log_unit_warning(UNIT(s), "Activation timed out. Killing."); - swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT); - } else { - log_unit_warning(UNIT(s), "Activation timed out. Skipping SIGKILL. Ignoring."); - swap_enter_dead(s, SWAP_FAILURE_TIMEOUT); - } - break; - case SWAP_DEACTIVATING_SIGTERM: if (s->kill_context.send_sigkill) { - log_unit_warning(UNIT(s), "Deactivation timed out. Killing."); + log_unit_warning(UNIT(s), "Swap process timed out. Killing."); swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT); } else { - log_unit_warning(UNIT(s), "Deactivation timed out. Skipping SIGKILL. Ignoring."); - swap_enter_dead(s, SWAP_FAILURE_TIMEOUT); + log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring."); + swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT); } break; - case SWAP_ACTIVATING_SIGKILL: case SWAP_DEACTIVATING_SIGKILL: log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring."); - swap_enter_dead(s, SWAP_FAILURE_TIMEOUT); + swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT); break; default: |