]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/swap.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / core / swap.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
07b0b134
ML
2
3#include <errno.h>
07b0b134
ML
4#include <sys/epoll.h>
5#include <sys/stat.h>
4f5dd394 6#include <unistd.h>
07b0b134 7
b4bbcaa9
TA
8#include "libudev.h"
9
b5efdb8a 10#include "alloc-util.h"
07b0b134 11#include "dbus-swap.h"
57b7a260 12#include "device.h"
4f5dd394 13#include "escape.h"
9a57c629 14#include "exit-status.h"
3ffd4af2 15#include "fd-util.h"
f97b34a6 16#include "format-util.h"
4f5dd394 17#include "fstab-util.h"
6bedfcbb 18#include "parse-util.h"
9eb977db 19#include "path-util.h"
7b3e062c 20#include "process-util.h"
4f5dd394 21#include "special.h"
8b43440b 22#include "string-table.h"
07630cea 23#include "string-util.h"
3ffd4af2 24#include "swap.h"
5bcb0f2b 25#include "udev-util.h"
4f5dd394
LP
26#include "unit-name.h"
27#include "unit.h"
28#include "virt.h"
07b0b134
ML
29
30static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
31 [SWAP_DEAD] = UNIT_INACTIVE,
e04aad61 32 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
5bcb0f2b 33 [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
07b0b134 34 [SWAP_ACTIVE] = UNIT_ACTIVE,
e04aad61 35 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
e04aad61
LP
36 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
37 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 38 [SWAP_FAILED] = UNIT_FAILED
07b0b134
ML
39};
40
718db961
LP
41static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
42static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
43
dedf3719
LP
44static bool SWAP_STATE_WITH_PROCESS(SwapState state) {
45 return IN_SET(state,
46 SWAP_ACTIVATING,
47 SWAP_ACTIVATING_DONE,
48 SWAP_DEACTIVATING,
49 SWAP_DEACTIVATING_SIGTERM,
50 SWAP_DEACTIVATING_SIGKILL);
51}
52
e04aad61 53static void swap_unset_proc_swaps(Swap *s) {
9670d583
LP
54 assert(s);
55
56 if (!s->from_proc_swaps)
57 return;
58
a1e58e8e 59 s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what);
9670d583
LP
60
61 s->from_proc_swaps = false;
62}
63
64static int swap_set_devnode(Swap *s, const char *devnode) {
df326b84 65 Hashmap *swaps;
5bcb0f2b 66 Swap *first;
9670d583 67 int r;
e04aad61
LP
68
69 assert(s);
70
548f6937 71 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &path_hash_ops);
9670d583
LP
72 if (r < 0)
73 return r;
e04aad61 74
9670d583 75 swaps = UNIT(s)->manager->swaps_by_devnode;
e04aad61 76
9670d583
LP
77 if (s->devnode) {
78 first = hashmap_get(swaps, s->devnode);
e04aad61 79
9670d583
LP
80 LIST_REMOVE(same_devnode, first, s);
81 if (first)
82 hashmap_replace(swaps, first->devnode, first);
83 else
84 hashmap_remove(swaps, s->devnode);
5bcb0f2b 85
a1e58e8e 86 s->devnode = mfree(s->devnode);
9670d583
LP
87 }
88
89 if (devnode) {
90 s->devnode = strdup(devnode);
91 if (!s->devnode)
92 return -ENOMEM;
93
94 first = hashmap_get(swaps, s->devnode);
95 LIST_PREPEND(same_devnode, first, s);
96
97 return hashmap_replace(swaps, first->devnode, first);
98 }
99
100 return 0;
e04aad61
LP
101}
102
f6cebb3b 103static void swap_init(Unit *u) {
6e620bec
LP
104 Swap *s = SWAP(u);
105
106 assert(s);
1124fe6f 107 assert(UNIT(s)->load_state == UNIT_STUB);
6e620bec 108
1f19a534 109 s->timeout_usec = u->manager->default_timeout_start_usec;
e04aad61 110
ac155bb8
MS
111 s->exec_context.std_output = u->manager->default_std_output;
112 s->exec_context.std_error = u->manager->default_std_error;
085afe36 113
6b1dc2bd 114 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
e04aad61 115
e1770af8 116 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
c8f4d764 117
5bcb0f2b 118 u->ignore_on_isolate = true;
e04aad61
LP
119}
120
121static void swap_unwatch_control_pid(Swap *s) {
122 assert(s);
123
124 if (s->control_pid <= 0)
125 return;
126
127 unit_unwatch_pid(UNIT(s), s->control_pid);
128 s->control_pid = 0;
6e620bec
LP
129}
130
4e85aff4
LP
131static void swap_done(Unit *u) {
132 Swap *s = SWAP(u);
07b0b134 133
4e85aff4 134 assert(s);
07b0b134 135
e04aad61 136 swap_unset_proc_swaps(s);
9670d583 137 swap_set_devnode(s, NULL);
e04aad61 138
a1e58e8e
LP
139 s->what = mfree(s->what);
140 s->parameters_fragment.what = mfree(s->parameters_fragment.what);
141 s->parameters_fragment.options = mfree(s->parameters_fragment.options);
86b23b07 142
e8a565cb 143 s->exec_runtime = exec_runtime_unref(s->exec_runtime, false);
e04aad61
LP
144 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
145 s->control_command = NULL;
146
29206d46
LP
147 dynamic_creds_unref(&s->dynamic_creds);
148
e04aad61
LP
149 swap_unwatch_control_pid(s);
150
718db961
LP
151 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
152}
153
36c16a7c 154static int swap_arm_timer(Swap *s, usec_t usec) {
718db961
LP
155 int r;
156
157 assert(s);
158
718db961 159 if (s->timer_event_source) {
36c16a7c 160 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
161 if (r < 0)
162 return r;
163
164 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
165 }
166
36c16a7c
LP
167 if (usec == USEC_INFINITY)
168 return 0;
169
7dfbe2e3 170 r = sd_event_add_time(
6a0f1f6d
LP
171 UNIT(s)->manager->event,
172 &s->timer_event_source,
173 CLOCK_MONOTONIC,
36c16a7c 174 usec, 0,
6a0f1f6d 175 swap_dispatch_timer, s);
7dfbe2e3
TG
176 if (r < 0)
177 return r;
178
179 (void) sd_event_source_set_description(s->timer_event_source, "swap-timer");
180
181 return 0;
07b0b134
ML
182}
183
eef85c4a 184static int swap_add_device_dependencies(Swap *s) {
173a8d04
LP
185 assert(s);
186
187 if (!s->what)
188 return 0;
189
f10af76d
LP
190 if (!s->from_fragment)
191 return 0;
192
cfcfd4ae 193 if (is_device_path(s->what))
eef85c4a 194 return unit_add_node_dependency(UNIT(s), s->what, MANAGER_IS_SYSTEM(UNIT(s)->manager), UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE);
cfcfd4ae
LP
195 else
196 /* File based swap devices need to be ordered after
2b93b027 197 * systemd-remount-fs.service, since they might need a
cfcfd4ae 198 * writable file system. */
eef85c4a 199 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true, UNIT_DEPENDENCY_FILE);
173a8d04
LP
200}
201
2edd4434 202static int swap_add_default_dependencies(Swap *s) {
8bf23dc7
FB
203 int r;
204
2edd4434
LP
205 assert(s);
206
4c9ea260
LP
207 if (!UNIT(s)->default_dependencies)
208 return 0;
209
463d0d15 210 if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
6b1dc2bd 211 return 0;
2edd4434 212
75f86906 213 if (detect_container() > 0)
c0387ebf
LP
214 return 0;
215
8bf23dc7
FB
216 /* swap units generated for the swap dev links are missing the
217 * ordering dep against the swap target. */
eef85c4a 218 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SWAP_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT);
8bf23dc7
FB
219 if (r < 0)
220 return r;
221
eef85c4a 222 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT);
2edd4434
LP
223}
224
4e85aff4 225static int swap_verify(Swap *s) {
7fd1b19b 226 _cleanup_free_ char *e = NULL;
7410616c 227 int r;
4e85aff4 228
1124fe6f 229 if (UNIT(s)->load_state != UNIT_LOADED)
5bcb0f2b 230 return 0;
4e85aff4 231
7410616c
LP
232 r = unit_name_from_path(s->what, ".swap", &e);
233 if (r < 0)
f2341e0a 234 return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m");
4e85aff4 235
7410616c 236 if (!unit_has_name(UNIT(s), e)) {
f2341e0a 237 log_unit_error(UNIT(s), "Value of What= and unit name do not match, not loading.");
6f40aa45 238 return -ENOEXEC;
4e85aff4
LP
239 }
240
4819ff03 241 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
f2341e0a 242 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.");
6f40aa45 243 return -ENOEXEC;
e04aad61
LP
244 }
245
4e85aff4
LP
246 return 0;
247}
248
9670d583 249static int swap_load_devnode(Swap *s) {
8e766630 250 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
9670d583
LP
251 struct stat st;
252 const char *p;
e5ca27b7 253 int r;
9670d583
LP
254
255 assert(s);
256
257 if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
258 return 0;
259
e5ca27b7
LP
260 r = udev_device_new_from_stat_rdev(UNIT(s)->manager->udev, &st, &d);
261 if (r < 0) {
262 log_unit_full(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
263 "Failed to allocate udev device for swap %s: %m", s->what);
9670d583 264 return 0;
e5ca27b7 265 }
9670d583
LP
266
267 p = udev_device_get_devnode(d);
268 if (!p)
269 return 0;
270
271 return swap_set_devnode(s, p);
272}
273
07b0b134
ML
274static int swap_load(Unit *u) {
275 int r;
276 Swap *s = SWAP(u);
277
278 assert(s);
ac155bb8 279 assert(u->load_state == UNIT_STUB);
07b0b134
ML
280
281 /* Load a .swap file */
3000f850
YW
282 if (SWAP(u)->from_proc_swaps)
283 r = unit_load_fragment_and_dropin_optional(u);
284 else
285 r = unit_load_fragment_and_dropin(u);
646134dc 286 if (r < 0)
07b0b134
ML
287 return r;
288
ac155bb8 289 if (u->load_state == UNIT_LOADED) {
4e85aff4 290
1124fe6f 291 if (UNIT(s)->fragment_path)
4e85aff4
LP
292 s->from_fragment = true;
293
294 if (!s->what) {
295 if (s->parameters_fragment.what)
296 s->what = strdup(s->parameters_fragment.what);
4e85aff4
LP
297 else if (s->parameters_proc_swaps.what)
298 s->what = strdup(s->parameters_proc_swaps.what);
7410616c
LP
299 else {
300 r = unit_name_to_path(u->id, &s->what);
301 if (r < 0)
302 return r;
303 }
4e85aff4
LP
304
305 if (!s->what)
07b0b134 306 return -ENOMEM;
4e85aff4 307 }
07b0b134 308
858d36c1 309 path_simplify(s->what, false);
07b0b134 310
5bcb0f2b
LP
311 if (!UNIT(s)->description) {
312 r = unit_set_description(u, s->what);
313 if (r < 0)
4e85aff4 314 return r;
5bcb0f2b 315 }
4e85aff4 316
eef85c4a 317 r = unit_require_mounts_for(UNIT(s), s->what, UNIT_DEPENDENCY_IMPLICIT);
646134dc 318 if (r < 0)
07b0b134
ML
319 return r;
320
eef85c4a 321 r = swap_add_device_dependencies(s);
646134dc 322 if (r < 0)
6e2ef85b 323 return r;
07b0b134 324
9670d583
LP
325 r = swap_load_devnode(s);
326 if (r < 0)
327 return r;
328
598459ce
LP
329 r = unit_patch_contexts(u);
330 if (r < 0)
331 return r;
332
333 r = unit_add_exec_dependencies(u, &s->exec_context);
334 if (r < 0)
335 return r;
336
d79200e2 337 r = unit_set_default_slice(u);
a016b922
LP
338 if (r < 0)
339 return r;
340
4c9ea260
LP
341 r = swap_add_default_dependencies(s);
342 if (r < 0)
343 return r;
07b0b134
ML
344 }
345
346 return swap_verify(s);
347}
348
628c89cc 349static int swap_setup_unit(
4e85aff4
LP
350 Manager *m,
351 const char *what,
e04aad61 352 const char *what_proc_swaps,
4e85aff4 353 int priority,
e04aad61
LP
354 bool set_flags) {
355
7fd1b19b 356 _cleanup_free_ char *e = NULL;
2c7c6144 357 bool delete = false;
5bcb0f2b 358 Unit *u = NULL;
07b0b134 359 int r;
4e85aff4
LP
360 SwapParameters *p;
361
362 assert(m);
363 assert(what);
6b1dc2bd 364 assert(what_proc_swaps);
07b0b134 365
7410616c
LP
366 r = unit_name_from_path(what, ".swap", &e);
367 if (r < 0)
f2341e0a 368 return log_unit_error_errno(u, r, "Failed to generate unit name from path: %m");
07b0b134 369
e04aad61
LP
370 u = manager_get_unit(m, e);
371
6b1dc2bd 372 if (u &&
e04aad61 373 SWAP(u)->from_proc_swaps &&
628c89cc
LP
374 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps)) {
375 log_error("Swap %s appeared twice with different device paths %s and %s", e, SWAP(u)->parameters_proc_swaps.what, what_proc_swaps);
e04aad61 376 return -EEXIST;
628c89cc 377 }
4e85aff4
LP
378
379 if (!u) {
07b0b134
ML
380 delete = true;
381
a581e45a 382 r = unit_new_for_name(m, sizeof(Swap), e, &u);
7d17cfbc 383 if (r < 0)
e04aad61
LP
384 goto fail;
385
7d17cfbc
MS
386 SWAP(u)->what = strdup(what);
387 if (!SWAP(u)->what) {
628c89cc 388 r = -ENOMEM;
e04aad61
LP
389 goto fail;
390 }
391
392 unit_add_to_load_queue(u);
4e85aff4
LP
393 } else
394 delete = false;
07b0b134 395
6b1dc2bd 396 p = &SWAP(u)->parameters_proc_swaps;
07b0b134 397
6b1dc2bd 398 if (!p->what) {
5bcb0f2b
LP
399 p->what = strdup(what_proc_swaps);
400 if (!p->what) {
401 r = -ENOMEM;
6b1dc2bd
LP
402 goto fail;
403 }
6b1dc2bd 404 }
e04aad61 405
6b1dc2bd
LP
406 if (set_flags) {
407 SWAP(u)->is_active = true;
408 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
4e85aff4 409 }
07b0b134 410
6b1dc2bd
LP
411 SWAP(u)->from_proc_swaps = true;
412
4e85aff4 413 p->priority = priority;
07b0b134 414
4e85aff4 415 unit_add_to_dbus_queue(u);
07b0b134
ML
416 return 0;
417
418fail:
f2341e0a 419 log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
e04aad61 420
c9d5c9c0 421 if (delete)
07b0b134
ML
422 unit_free(u);
423
4e85aff4 424 return r;
07b0b134
ML
425}
426
628c89cc 427static int swap_process_new(Manager *m, const char *device, int prio, bool set_flags) {
8e766630 428 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
5bcb0f2b
LP
429 struct udev_list_entry *item = NULL, *first = NULL;
430 const char *dn;
e04aad61 431 struct stat st;
5bcb0f2b 432 int r;
e04aad61
LP
433
434 assert(m);
435
628c89cc 436 r = swap_setup_unit(m, device, device, prio, set_flags);
5bcb0f2b
LP
437 if (r < 0)
438 return r;
e04aad61 439
5bcb0f2b
LP
440 /* If this is a block device, then let's add duplicates for
441 * all other names of this block device */
442 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
443 return 0;
e04aad61 444
e5ca27b7
LP
445 r = udev_device_new_from_stat_rdev(m->udev, &st, &d);
446 if (r < 0) {
447 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
448 "Failed to allocate udev device for swap %s: %m", device);
5bcb0f2b 449 return 0;
e5ca27b7 450 }
e04aad61 451
5bcb0f2b
LP
452 /* Add the main device node */
453 dn = udev_device_get_devnode(d);
454 if (dn && !streq(dn, device))
628c89cc 455 swap_setup_unit(m, dn, device, prio, set_flags);
e04aad61 456
5bcb0f2b
LP
457 /* Add additional units for all symlinks */
458 first = udev_device_get_devlinks_list_entry(d);
459 udev_list_entry_foreach(item, first) {
460 const char *p;
e04aad61 461
5bcb0f2b
LP
462 /* Don't bother with the /dev/block links */
463 p = udev_list_entry_get_name(item);
e04aad61 464
5bcb0f2b
LP
465 if (streq(p, device))
466 continue;
e04aad61 467
5bcb0f2b
LP
468 if (path_startswith(p, "/dev/block/"))
469 continue;
e04aad61 470
5bcb0f2b
LP
471 if (stat(p, &st) >= 0)
472 if (!S_ISBLK(st.st_mode) ||
473 st.st_rdev != udev_device_get_devnum(d))
474 continue;
e04aad61 475
628c89cc 476 swap_setup_unit(m, p, device, prio, set_flags);
e04aad61
LP
477 }
478
e04aad61
LP
479 return r;
480}
481
07b0b134
ML
482static void swap_set_state(Swap *s, SwapState state) {
483 SwapState old_state;
37cf8fee 484 Swap *other;
e04aad61 485
07b0b134
ML
486 assert(s);
487
488 old_state = s->state;
489 s->state = state;
490
dedf3719 491 if (!SWAP_STATE_WITH_PROCESS(state)) {
718db961 492 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
e04aad61
LP
493 swap_unwatch_control_pid(s);
494 s->control_command = NULL;
495 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
496 }
497
07b0b134 498 if (state != old_state)
f2341e0a 499 log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state));
07b0b134 500
2ad2e41a 501 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
37cf8fee
LP
502
503 /* If there other units for the same device node have a job
504 queued it might be worth checking again if it is runnable
505 now. This is necessary, since swap_start() refuses
506 operation with EAGAIN if there's already another job for
507 the same device node queued. */
508 LIST_FOREACH_OTHERS(same_devnode, other, s)
509 if (UNIT(other)->job)
510 job_add_to_run_queue(UNIT(other)->job);
07b0b134
ML
511}
512
be847e82 513static int swap_coldplug(Unit *u) {
07b0b134
ML
514 Swap *s = SWAP(u);
515 SwapState new_state = SWAP_DEAD;
e04aad61 516 int r;
07b0b134
ML
517
518 assert(s);
519 assert(s->state == SWAP_DEAD);
520
521 if (s->deserialized_state != s->state)
522 new_state = s->deserialized_state;
4e85aff4 523 else if (s->from_proc_swaps)
07b0b134
ML
524 new_state = SWAP_ACTIVE;
525
5bcb0f2b
LP
526 if (new_state == s->state)
527 return 0;
e04aad61 528
c386f588
LP
529 if (s->control_pid > 0 &&
530 pid_is_unwaited(s->control_pid) &&
dedf3719 531 SWAP_STATE_WITH_PROCESS(new_state)) {
e04aad61 532
5bcb0f2b
LP
533 r = unit_watch_pid(UNIT(s), s->control_pid);
534 if (r < 0)
535 return r;
e04aad61 536
36c16a7c 537 r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
5bcb0f2b
LP
538 if (r < 0)
539 return r;
e04aad61 540 }
07b0b134 541
e8a565cb 542 if (!IN_SET(new_state, SWAP_DEAD, SWAP_FAILED)) {
29206d46 543 (void) unit_setup_dynamic_creds(u);
e8a565cb
YW
544 (void) unit_setup_exec_runtime(u);
545 }
29206d46 546
5bcb0f2b 547 swap_set_state(s, new_state);
07b0b134
ML
548 return 0;
549}
550
551static void swap_dump(Unit *u, FILE *f, const char *prefix) {
f2e18ef1 552 char buf[FORMAT_TIMESPAN_MAX];
07b0b134 553 Swap *s = SWAP(u);
4e85aff4 554 SwapParameters *p;
07b0b134
ML
555
556 assert(s);
4e85aff4
LP
557 assert(f);
558
559 if (s->from_proc_swaps)
560 p = &s->parameters_proc_swaps;
561 else if (s->from_fragment)
562 p = &s->parameters_fragment;
b6bfc7bb
LP
563 else
564 p = NULL;
07b0b134
ML
565
566 fprintf(f,
4e85aff4 567 "%sSwap State: %s\n"
e1770af8 568 "%sResult: %s\n"
07b0b134 569 "%sWhat: %s\n"
4e85aff4
LP
570 "%sFrom /proc/swaps: %s\n"
571 "%sFrom fragment: %s\n",
07b0b134 572 prefix, swap_state_to_string(s->state),
e1770af8 573 prefix, swap_result_to_string(s->result),
07b0b134 574 prefix, s->what,
4e85aff4
LP
575 prefix, yes_no(s->from_proc_swaps),
576 prefix, yes_no(s->from_fragment));
e04aad61 577
9670d583
LP
578 if (s->devnode)
579 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
580
b6bfc7bb
LP
581 if (p)
582 fprintf(f,
583 "%sPriority: %i\n"
47cb901e 584 "%sOptions: %s\n",
b6bfc7bb 585 prefix, p->priority,
47cb901e 586 prefix, strempty(p->options));
b6bfc7bb 587
9bd0e1b8
YW
588 fprintf(f,
589 "%sTimeoutSec: %s\n",
590 prefix, format_timespan(buf, sizeof(buf), s->timeout_usec, USEC_PER_SEC));
591
e04aad61
LP
592 if (s->control_pid > 0)
593 fprintf(f,
de0671ee
ZJS
594 "%sControl PID: "PID_FMT"\n",
595 prefix, s->control_pid);
e04aad61
LP
596
597 exec_context_dump(&s->exec_context, f, prefix);
4819ff03 598 kill_context_dump(&s->kill_context, f, prefix);
18f573aa 599 cgroup_context_dump(&s->cgroup_context, f, prefix);
e04aad61
LP
600}
601
602static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
3c7416b6 603
9fa95f85 604 ExecParameters exec_params = {
1703fa41 605 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
c39f1ce2
LP
606 .stdin_fd = -1,
607 .stdout_fd = -1,
608 .stderr_fd = -1,
9fa95f85 609 };
3c7416b6
LP
610 pid_t pid;
611 int r;
e04aad61
LP
612
613 assert(s);
614 assert(c);
615 assert(_pid);
616
3c7416b6 617 r = unit_prepare_exec(UNIT(s));
29206d46 618 if (r < 0)
3c7416b6 619 return r;
29206d46 620
36c16a7c 621 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
646134dc 622 if (r < 0)
e04aad61
LP
623 goto fail;
624
f0d47797 625 unit_set_exec_params(UNIT(s), &exec_params);
9fa95f85 626
f2341e0a
LP
627 r = exec_spawn(UNIT(s),
628 c,
646134dc 629 &s->exec_context,
9fa95f85 630 &exec_params,
613b411c 631 s->exec_runtime,
29206d46 632 &s->dynamic_creds,
646134dc
ZJS
633 &pid);
634 if (r < 0)
e04aad61
LP
635 goto fail;
636
646134dc
ZJS
637 r = unit_watch_pid(UNIT(s), pid);
638 if (r < 0)
e04aad61
LP
639 /* FIXME: we need to do something here */
640 goto fail;
641
642 *_pid = pid;
643
644 return 0;
645
646fail:
718db961 647 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
3536f49e 648
e04aad61 649 return r;
07b0b134
ML
650}
651
e1770af8 652static void swap_enter_dead(Swap *s, SwapResult f) {
07b0b134
ML
653 assert(s);
654
a0fef983 655 if (s->result == SWAP_SUCCESS)
e1770af8 656 s->result = f;
e04aad61 657
ed77d407
LP
658 if (s->result != SWAP_SUCCESS)
659 log_unit_warning(UNIT(s), "Failed with result '%s'.", swap_result_to_string(s->result));
660
29206d46
LP
661 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
662
e8a565cb 663 s->exec_runtime = exec_runtime_unref(s->exec_runtime, true);
613b411c 664
3536f49e 665 exec_context_destroy_runtime_directory(&s->exec_context, UNIT(s)->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
e66cf1a3 666
00d9ef85
LP
667 unit_unref_uid_gid(UNIT(s), true);
668
29206d46 669 dynamic_creds_destroy(&s->dynamic_creds);
07b0b134
ML
670}
671
e1770af8 672static void swap_enter_active(Swap *s, SwapResult f) {
e04aad61
LP
673 assert(s);
674
a0fef983 675 if (s->result == SWAP_SUCCESS)
e1770af8 676 s->result = f;
e04aad61
LP
677
678 swap_set_state(s, SWAP_ACTIVE);
679}
680
50864457
LP
681static void swap_enter_dead_or_active(Swap *s, SwapResult f) {
682 assert(s);
683
684 if (s->from_proc_swaps)
685 swap_enter_active(s, f);
686 else
687 swap_enter_dead(s, f);
688}
689
e1770af8 690static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
07b0b134 691 int r;
d31ae548 692 KillOperation kop;
07b0b134
ML
693
694 assert(s);
e04aad61 695
a0fef983 696 if (s->result == SWAP_SUCCESS)
e1770af8 697 s->result = f;
e04aad61 698
50864457 699 if (state == SWAP_DEACTIVATING_SIGTERM)
d31ae548
FB
700 kop = KILL_TERMINATE;
701 else
702 kop = KILL_KILL;
703
704 r = unit_kill_context(UNIT(s), &s->kill_context, kop, -1, s->control_pid, false);
cd2086fe
LP
705 if (r < 0)
706 goto fail;
e04aad61 707
cd2086fe 708 if (r > 0) {
36c16a7c 709 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
646134dc 710 if (r < 0)
e04aad61
LP
711 goto fail;
712
713 swap_set_state(s, state);
50864457 714 } else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill)
ac84d1fb
LP
715 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
716 else
50864457 717 swap_enter_dead_or_active(s, SWAP_SUCCESS);
e04aad61
LP
718
719 return;
720
721fail:
f2341e0a 722 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
50864457 723 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
e04aad61
LP
724}
725
726static void swap_enter_activating(Swap *s) {
bf1d7ba7
KZ
727 _cleanup_free_ char *opts = NULL;
728 int r;
e04aad61
LP
729
730 assert(s);
731
a4634b21
LP
732 unit_warn_leftover_processes(UNIT(s));
733
e04aad61
LP
734 s->control_command_id = SWAP_EXEC_ACTIVATE;
735 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
07b0b134 736
86b23b07 737 if (s->from_fragment) {
bf1d7ba7 738 int priority = -1;
47cb901e 739
bf1d7ba7
KZ
740 r = fstab_find_pri(s->parameters_fragment.options, &priority);
741 if (r < 0)
742 log_warning_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
743 else if (r == 1 && s->parameters_fragment.priority >= 0)
744 log_warning("Duplicate swap priority configuration by Priority and Options fields.");
745
746 if (r <= 0 && s->parameters_fragment.priority >= 0) {
747 if (s->parameters_fragment.options)
748 r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority);
749 else
750 r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority);
e64d5235 751 if (r < 0)
bf1d7ba7 752 goto fail;
e64d5235 753 }
86b23b07
JS
754 }
755
756 r = exec_command_set(s->control_command, "/sbin/swapon", NULL);
757 if (r < 0)
758 goto fail;
4e85aff4 759
bf1d7ba7
KZ
760 if (s->parameters_fragment.options || opts) {
761 r = exec_command_append(s->control_command, "-o",
762 opts ? : s->parameters_fragment.options, NULL);
86b23b07
JS
763 if (r < 0)
764 goto fail;
765 }
e04aad61 766
86b23b07 767 r = exec_command_append(s->control_command, s->what, NULL);
e04aad61
LP
768 if (r < 0)
769 goto fail;
770
771 swap_unwatch_control_pid(s);
772
646134dc
ZJS
773 r = swap_spawn(s, s->control_command, &s->control_pid);
774 if (r < 0)
e04aad61
LP
775 goto fail;
776
777 swap_set_state(s, SWAP_ACTIVATING);
778
779 return;
780
781fail:
f2341e0a 782 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m");
50864457 783 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
e04aad61
LP
784}
785
e1770af8 786static void swap_enter_deactivating(Swap *s) {
e04aad61
LP
787 int r;
788
789 assert(s);
790
e04aad61
LP
791 s->control_command_id = SWAP_EXEC_DEACTIVATE;
792 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
793
646134dc 794 r = exec_command_set(s->control_command,
e04aad61
LP
795 "/sbin/swapoff",
796 s->what,
646134dc
ZJS
797 NULL);
798 if (r < 0)
e04aad61
LP
799 goto fail;
800
801 swap_unwatch_control_pid(s);
802
646134dc
ZJS
803 r = swap_spawn(s, s->control_command, &s->control_pid);
804 if (r < 0)
e04aad61
LP
805 goto fail;
806
807 swap_set_state(s, SWAP_DEACTIVATING);
808
809 return;
810
811fail:
f2341e0a 812 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m");
50864457 813 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
e04aad61
LP
814}
815
816static int swap_start(Unit *u) {
37cf8fee 817 Swap *s = SWAP(u), *other;
07299350 818 int r;
e04aad61
LP
819
820 assert(s);
821
50864457 822 /* We cannot fulfill this request right now, try again later please! */
d31ae548
FB
823 if (IN_SET(s->state,
824 SWAP_DEACTIVATING,
825 SWAP_DEACTIVATING_SIGTERM,
50864457 826 SWAP_DEACTIVATING_SIGKILL))
e04aad61
LP
827 return -EAGAIN;
828
50864457 829 /* Already on it! */
e04aad61
LP
830 if (s->state == SWAP_ACTIVATING)
831 return 0;
832
d31ae548 833 assert(IN_SET(s->state, SWAP_DEAD, SWAP_FAILED));
e04aad61 834
75f86906 835 if (detect_container() > 0)
a5c3034f
LP
836 return -EPERM;
837
37cf8fee
LP
838 /* If there's a job for another swap unit for the same node
839 * running, then let's not dispatch this one for now, and wait
840 * until that other job has finished. */
841 LIST_FOREACH_OTHERS(same_devnode, other, s)
842 if (UNIT(other)->job && UNIT(other)->job->state == JOB_RUNNING)
843 return -EAGAIN;
844
07299350
LP
845 r = unit_start_limit_test(u);
846 if (r < 0) {
847 swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT);
848 return r;
849 }
850
4b58153d
LP
851 r = unit_acquire_invocation_id(u);
852 if (r < 0)
853 return r;
854
e1770af8 855 s->result = SWAP_SUCCESS;
3c7416b6
LP
856
857 u->reset_accounting = true;
5ad096b3 858
e04aad61 859 swap_enter_activating(s);
82a2b6bb 860 return 1;
07b0b134
ML
861}
862
863static int swap_stop(Unit *u) {
864 Swap *s = SWAP(u);
07b0b134
ML
865
866 assert(s);
867
50864457
LP
868 switch (s->state) {
869
870 case SWAP_DEACTIVATING:
871 case SWAP_DEACTIVATING_SIGTERM:
872 case SWAP_DEACTIVATING_SIGKILL:
873 /* Already on it */
e04aad61 874 return 0;
07b0b134 875
50864457
LP
876 case SWAP_ACTIVATING:
877 case SWAP_ACTIVATING_DONE:
878 /* There's a control process pending, directly enter kill mode */
879 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS);
880 return 0;
07b0b134 881
50864457
LP
882 case SWAP_ACTIVE:
883 if (detect_container() > 0)
884 return -EPERM;
a5c3034f 885
50864457
LP
886 swap_enter_deactivating(s);
887 return 1;
888
889 default:
890 assert_not_reached("Unexpected state.");
891 }
07b0b134
ML
892}
893
894static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
895 Swap *s = SWAP(u);
896
897 assert(s);
898 assert(f);
899 assert(fds);
900
901 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
e1770af8 902 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
e04aad61
LP
903
904 if (s->control_pid > 0)
de0671ee 905 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
e04aad61
LP
906
907 if (s->control_command_id >= 0)
908 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
07b0b134
ML
909
910 return 0;
911}
912
913static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
914 Swap *s = SWAP(u);
915
916 assert(s);
917 assert(fds);
918
919 if (streq(key, "state")) {
920 SwapState state;
921
646134dc
ZJS
922 state = swap_state_from_string(value);
923 if (state < 0)
f2341e0a 924 log_unit_debug(u, "Failed to parse state value: %s", value);
07b0b134
ML
925 else
926 s->deserialized_state = state;
e1770af8
LP
927 } else if (streq(key, "result")) {
928 SwapResult f;
929
930 f = swap_result_from_string(value);
931 if (f < 0)
f2341e0a 932 log_unit_debug(u, "Failed to parse result value: %s", value);
e1770af8
LP
933 else if (f != SWAP_SUCCESS)
934 s->result = f;
e04aad61
LP
935 } else if (streq(key, "control-pid")) {
936 pid_t pid;
937
938 if (parse_pid(value, &pid) < 0)
f2341e0a 939 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
e04aad61
LP
940 else
941 s->control_pid = pid;
942
943 } else if (streq(key, "control-command")) {
944 SwapExecCommand id;
945
646134dc
ZJS
946 id = swap_exec_command_from_string(value);
947 if (id < 0)
f2341e0a 948 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
e04aad61
LP
949 else {
950 s->control_command_id = id;
951 s->control_command = s->exec_command + id;
952 }
07b0b134 953 } else
f2341e0a 954 log_unit_debug(u, "Unknown serialization key: %s", key);
07b0b134
ML
955
956 return 0;
957}
958
44a6b1b6 959_pure_ static UnitActiveState swap_active_state(Unit *u) {
07b0b134
ML
960 assert(u);
961
962 return state_translation_table[SWAP(u)->state];
963}
964
44a6b1b6 965_pure_ static const char *swap_sub_state_to_string(Unit *u) {
07b0b134
ML
966 assert(u);
967
968 return swap_state_to_string(SWAP(u)->state);
969}
970
f2f725e5 971_pure_ static bool swap_may_gc(Unit *u) {
07b0b134
ML
972 Swap *s = SWAP(u);
973
974 assert(s);
975
f2f725e5
ZJS
976 if (s->from_proc_swaps)
977 return false;
978
979 return true;
07b0b134
ML
980}
981
e04aad61
LP
982static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
983 Swap *s = SWAP(u);
e1770af8 984 SwapResult f;
e04aad61
LP
985
986 assert(s);
987 assert(pid >= 0);
988
989 if (pid != s->control_pid)
990 return;
991
992 s->control_pid = 0;
993
1f0958f6 994 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
e1770af8
LP
995 f = SWAP_SUCCESS;
996 else if (code == CLD_EXITED)
997 f = SWAP_FAILURE_EXIT_CODE;
998 else if (code == CLD_KILLED)
999 f = SWAP_FAILURE_SIGNAL;
1000 else if (code == CLD_DUMPED)
1001 f = SWAP_FAILURE_CORE_DUMP;
1002 else
1003 assert_not_reached("Unknown code");
1004
a0fef983 1005 if (s->result == SWAP_SUCCESS)
e1770af8 1006 s->result = f;
e04aad61
LP
1007
1008 if (s->control_command) {
6ea832a2 1009 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
e1770af8 1010
e04aad61
LP
1011 s->control_command = NULL;
1012 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1013 }
1014
f2341e0a
LP
1015 log_unit_full(u, f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
1016 "Swap process exited, code=%s status=%i", sigchld_code_to_string(code), status);
e04aad61
LP
1017
1018 switch (s->state) {
1019
1020 case SWAP_ACTIVATING:
5bcb0f2b 1021 case SWAP_ACTIVATING_DONE:
e04aad61 1022
50864457 1023 if (f == SWAP_SUCCESS || s->from_proc_swaps)
e1770af8 1024 swap_enter_active(s, f);
e04aad61 1025 else
e1770af8 1026 swap_enter_dead(s, f);
e04aad61
LP
1027 break;
1028
1029 case SWAP_DEACTIVATING:
1030 case SWAP_DEACTIVATING_SIGKILL:
1031 case SWAP_DEACTIVATING_SIGTERM:
1032
50864457 1033 swap_enter_dead_or_active(s, f);
e04aad61
LP
1034 break;
1035
1036 default:
1037 assert_not_reached("Uh, control process died at wrong time.");
1038 }
1039
1040 /* Notify clients about changed exit status */
1041 unit_add_to_dbus_queue(u);
e04aad61
LP
1042}
1043
718db961
LP
1044static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1045 Swap *s = SWAP(userdata);
e04aad61
LP
1046
1047 assert(s);
718db961 1048 assert(s->timer_event_source == source);
e04aad61
LP
1049
1050 switch (s->state) {
1051
1052 case SWAP_ACTIVATING:
5bcb0f2b 1053 case SWAP_ACTIVATING_DONE:
f2341e0a 1054 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
50864457 1055 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1056 break;
1057
1058 case SWAP_DEACTIVATING:
f2341e0a 1059 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
e1770af8 1060 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1061 break;
1062
e04aad61 1063 case SWAP_DEACTIVATING_SIGTERM:
4819ff03 1064 if (s->kill_context.send_sigkill) {
50864457 1065 log_unit_warning(UNIT(s), "Swap process timed out. Killing.");
e1770af8 1066 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
ba035df2 1067 } else {
50864457
LP
1068 log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring.");
1069 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
ba035df2 1070 }
e04aad61
LP
1071 break;
1072
e04aad61 1073 case SWAP_DEACTIVATING_SIGKILL:
f2341e0a 1074 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
50864457 1075 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1076 break;
1077
1078 default:
1079 assert_not_reached("Timeout at wrong time.");
1080 }
718db961
LP
1081
1082 return 0;
e04aad61
LP
1083}
1084
1085static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1086 unsigned i;
1087 int r = 0;
1088
1089 assert(m);
1090
07b0b134 1091 rewind(m->proc_swaps);
bab45044 1092
4e85aff4 1093 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
07b0b134 1094
e04aad61 1095 for (i = 1;; i++) {
9fb3675e 1096 _cleanup_free_ char *dev = NULL, *d = NULL;
07b0b134
ML
1097 int prio = 0, k;
1098
646134dc
ZJS
1099 k = fscanf(m->proc_swaps,
1100 "%ms " /* device/file */
1101 "%*s " /* type of swap */
1102 "%*s " /* swap size */
1103 "%*s " /* used */
1104 "%i\n", /* priority */
1105 &dev, &prio);
1106 if (k != 2) {
07b0b134 1107 if (k == EOF)
4e85aff4 1108 break;
07b0b134 1109
628c89cc 1110 log_warning("Failed to parse /proc/swaps:%u.", i);
e04aad61 1111 continue;
07b0b134 1112 }
07b0b134 1113
527b7a42 1114 if (cunescape(dev, UNESCAPE_RELAX, &d) < 0)
628c89cc
LP
1115 return log_oom();
1116
485ae697 1117 device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
4e85aff4 1118
628c89cc 1119 k = swap_process_new(m, d, prio, set_flags);
07b0b134 1120 if (k < 0)
e04aad61 1121 r = k;
07b0b134
ML
1122 }
1123
e04aad61
LP
1124 return r;
1125}
1126
718db961
LP
1127static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1128 Manager *m = userdata;
595ed347 1129 Unit *u;
4e434314
LP
1130 int r;
1131
1132 assert(m);
718db961 1133 assert(revents & EPOLLPRI);
4e434314 1134
646134dc
ZJS
1135 r = swap_load_proc_swaps(m, true);
1136 if (r < 0) {
da927ba9 1137 log_error_errno(r, "Failed to reread /proc/swaps: %m");
e04aad61
LP
1138
1139 /* Reset flags, just in case, for late calls */
595ed347
MS
1140 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1141 Swap *swap = SWAP(u);
e04aad61
LP
1142
1143 swap->is_active = swap->just_activated = false;
1144 }
1145
1146 return 0;
1147 }
1148
1149 manager_dispatch_load_queue(m);
1150
595ed347
MS
1151 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1152 Swap *swap = SWAP(u);
e04aad61
LP
1153
1154 if (!swap->is_active) {
1155 /* This has just been deactivated */
1156
e04aad61
LP
1157 swap_unset_proc_swaps(swap);
1158
1159 switch (swap->state) {
1160
1161 case SWAP_ACTIVE:
e1770af8 1162 swap_enter_dead(swap, SWAP_SUCCESS);
e04aad61
LP
1163 break;
1164
1165 default:
5bcb0f2b 1166 /* Fire again */
e04aad61
LP
1167 swap_set_state(swap, swap->state);
1168 break;
1169 }
1170
628c89cc 1171 if (swap->what)
485ae697 1172 device_found_node(m, swap->what, 0, DEVICE_FOUND_SWAP);
628c89cc 1173
e04aad61
LP
1174 } else if (swap->just_activated) {
1175
1176 /* New swap entry */
1177
1178 switch (swap->state) {
1179
1180 case SWAP_DEAD:
1181 case SWAP_FAILED:
4b58153d 1182 (void) unit_acquire_invocation_id(UNIT(swap));
e1770af8 1183 swap_enter_active(swap, SWAP_SUCCESS);
e04aad61
LP
1184 break;
1185
5bcb0f2b
LP
1186 case SWAP_ACTIVATING:
1187 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1188 break;
1189
e04aad61
LP
1190 default:
1191 /* Nothing really changed, but let's
1192 * issue an notification call
1193 * nonetheless, in case somebody is
1194 * waiting for this. */
1195 swap_set_state(swap, swap->state);
1196 break;
1197 }
1198 }
1199
1200 /* Reset the flags for later calls */
1201 swap->is_active = swap->just_activated = false;
1202 }
1203
1204 return 1;
1205}
1206
1207static Unit *swap_following(Unit *u) {
1208 Swap *s = SWAP(u);
1209 Swap *other, *first = NULL;
1210
1211 assert(s);
1212
cdc89820
ZJS
1213 /* If the user configured the swap through /etc/fstab or
1214 * a device unit, follow that. */
1215
1216 if (s->from_fragment)
e04aad61
LP
1217 return NULL;
1218
caac2704 1219 LIST_FOREACH_OTHERS(same_devnode, other, s)
cdc89820
ZJS
1220 if (other->from_fragment)
1221 return UNIT(other);
1222
b938cb90 1223 /* Otherwise, make everybody follow the unit that's named after
cdc89820
ZJS
1224 * the swap device in the kernel */
1225
1226 if (streq_ptr(s->what, s->devnode))
1227 return NULL;
e04aad61 1228
9670d583
LP
1229 LIST_FOREACH_AFTER(same_devnode, other, s)
1230 if (streq_ptr(other->what, other->devnode))
e04aad61
LP
1231 return UNIT(other);
1232
9670d583
LP
1233 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1234 if (streq_ptr(other->what, other->devnode))
e04aad61
LP
1235 return UNIT(other);
1236
1237 first = other;
1238 }
1239
cdc89820 1240 /* Fall back to the first on the list */
e04aad61 1241 return UNIT(first);
07b0b134
ML
1242}
1243
6210e7fc 1244static int swap_following_set(Unit *u, Set **_set) {
5bcb0f2b 1245 Swap *s = SWAP(u), *other;
af4fa99d 1246 _cleanup_set_free_ Set *set = NULL;
6210e7fc
LP
1247 int r;
1248
1249 assert(s);
1250 assert(_set);
1251
9670d583 1252 if (LIST_JUST_US(same_devnode, s)) {
6210e7fc
LP
1253 *_set = NULL;
1254 return 0;
1255 }
1256
d5099efc 1257 set = set_new(NULL);
5bcb0f2b 1258 if (!set)
6210e7fc
LP
1259 return -ENOMEM;
1260
caac2704 1261 LIST_FOREACH_OTHERS(same_devnode, other, s) {
5bcb0f2b
LP
1262 r = set_put(set, other);
1263 if (r < 0)
95f14a3e 1264 return r;
5bcb0f2b 1265 }
6210e7fc 1266
95f14a3e 1267 *_set = TAKE_PTR(set);
6210e7fc 1268 return 1;
6210e7fc
LP
1269}
1270
07b0b134
ML
1271static void swap_shutdown(Manager *m) {
1272 assert(m);
1273
718db961 1274 m->swap_event_source = sd_event_source_unref(m->swap_event_source);
74ca738f 1275 m->proc_swaps = safe_fclose(m->proc_swaps);
525d3cc7 1276 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
07b0b134
ML
1277}
1278
ba64af90 1279static void swap_enumerate(Manager *m) {
07b0b134 1280 int r;
9670d583 1281
07b0b134
ML
1282 assert(m);
1283
4e434314 1284 if (!m->proc_swaps) {
646134dc 1285 m->proc_swaps = fopen("/proc/swaps", "re");
ba64af90
LP
1286 if (!m->proc_swaps) {
1287 if (errno == ENOENT)
cb209a04 1288 log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
ba64af90 1289 else
cb209a04 1290 log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m");
ba64af90
LP
1291
1292 return;
1293 }
4e434314 1294
151b9b96 1295 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
ba64af90
LP
1296 if (r < 0) {
1297 log_error_errno(r, "Failed to watch /proc/swaps: %m");
29083707 1298 goto fail;
ba64af90 1299 }
29083707
LP
1300
1301 /* Dispatch this before we dispatch SIGCHLD, so that
1302 * we always get the events from /proc/swaps before
1303 * the SIGCHLD of /sbin/swapon. */
83231637 1304 r = sd_event_source_set_priority(m->swap_event_source, SD_EVENT_PRIORITY_NORMAL-10);
ba64af90
LP
1305 if (r < 0) {
1306 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
718db961 1307 goto fail;
ba64af90 1308 }
7dfbe2e3
TG
1309
1310 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
4e434314
LP
1311 }
1312
646134dc
ZJS
1313 r = swap_load_proc_swaps(m, false);
1314 if (r < 0)
718db961
LP
1315 goto fail;
1316
ba64af90 1317 return;
07b0b134 1318
718db961
LP
1319fail:
1320 swap_shutdown(m);
07b0b134
ML
1321}
1322
628c89cc 1323int swap_process_device_new(Manager *m, struct udev_device *dev) {
9670d583
LP
1324 struct udev_list_entry *item = NULL, *first = NULL;
1325 _cleanup_free_ char *e = NULL;
1326 const char *dn;
cabf58b2 1327 Unit *u;
9670d583
LP
1328 int r = 0;
1329
1330 assert(m);
1331 assert(dev);
1332
1333 dn = udev_device_get_devnode(dev);
1334 if (!dn)
1335 return 0;
1336
7410616c
LP
1337 r = unit_name_from_path(dn, ".swap", &e);
1338 if (r < 0)
1339 return r;
9670d583 1340
cabf58b2
FB
1341 u = manager_get_unit(m, e);
1342 if (u)
1343 r = swap_set_devnode(SWAP(u), dn);
9670d583
LP
1344
1345 first = udev_device_get_devlinks_list_entry(dev);
1346 udev_list_entry_foreach(item, first) {
1347 _cleanup_free_ char *n = NULL;
7410616c 1348 int q;
9670d583 1349
7410616c
LP
1350 q = unit_name_from_path(udev_list_entry_get_name(item), ".swap", &n);
1351 if (q < 0)
1352 return q;
9670d583 1353
cabf58b2
FB
1354 u = manager_get_unit(m, n);
1355 if (u) {
1356 q = swap_set_devnode(SWAP(u), dn);
9670d583
LP
1357 if (q < 0)
1358 r = q;
1359 }
1360 }
1361
1362 return r;
1363}
1364
628c89cc 1365int swap_process_device_remove(Manager *m, struct udev_device *dev) {
9670d583
LP
1366 const char *dn;
1367 int r = 0;
1368 Swap *s;
1369
1370 dn = udev_device_get_devnode(dev);
1371 if (!dn)
1372 return 0;
1373
1374 while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1375 int q;
1376
1377 q = swap_set_devnode(s, NULL);
1378 if (q < 0)
1379 r = q;
1380 }
1381
1382 return r;
1383}
1384
fdf20a31 1385static void swap_reset_failed(Unit *u) {
5632e374
LP
1386 Swap *s = SWAP(u);
1387
1388 assert(s);
1389
fdf20a31 1390 if (s->state == SWAP_FAILED)
5632e374 1391 swap_set_state(s, SWAP_DEAD);
e04aad61 1392
e1770af8 1393 s->result = SWAP_SUCCESS;
5632e374
LP
1394}
1395
718db961 1396static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
814cc562 1397 return unit_kill_common(u, who, signo, -1, SWAP(u)->control_pid, error);
8a0867d6
LP
1398}
1399
7a7821c8 1400static int swap_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1401 Swap *s = SWAP(u);
7a7821c8 1402 usec_t t;
68db7a3b
ZJS
1403 int r;
1404
1405 if (!s->timer_event_source)
1406 return 0;
1407
7a7821c8 1408 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
1409 if (r < 0)
1410 return r;
7a7821c8
LP
1411 if (t == USEC_INFINITY)
1412 return 0;
68db7a3b 1413
7a7821c8 1414 *timeout = t;
68db7a3b
ZJS
1415 return 1;
1416}
1417
1c2e9646 1418static bool swap_supported(void) {
0faacd47
LP
1419 static int supported = -1;
1420
1421 /* If swap support is not available in the kernel, or we are
1422 * running in a container we don't support swap units, and any
1423 * attempts to starting one should fail immediately. */
1424
1425 if (supported < 0)
1426 supported =
1427 access("/proc/swaps", F_OK) >= 0 &&
75f86906 1428 detect_container() <= 0;
0faacd47
LP
1429
1430 return supported;
1431}
1432
291d565a
LP
1433static int swap_control_pid(Unit *u) {
1434 Swap *s = SWAP(u);
1435
1436 assert(s);
1437
1438 return s->control_pid;
1439}
1440
e04aad61
LP
1441static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1442 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1443 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1444};
1445
1446DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1447
e1770af8
LP
1448static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1449 [SWAP_SUCCESS] = "success",
1450 [SWAP_FAILURE_RESOURCES] = "resources",
1451 [SWAP_FAILURE_TIMEOUT] = "timeout",
1452 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1453 [SWAP_FAILURE_SIGNAL] = "signal",
07299350
LP
1454 [SWAP_FAILURE_CORE_DUMP] = "core-dump",
1455 [SWAP_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
e1770af8
LP
1456};
1457
1458DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1459
07b0b134 1460const UnitVTable swap_vtable = {
7d17cfbc 1461 .object_size = sizeof(Swap),
718db961
LP
1462 .exec_context_offset = offsetof(Swap, exec_context),
1463 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1464 .kill_context_offset = offsetof(Swap, kill_context),
613b411c 1465 .exec_runtime_offset = offsetof(Swap, exec_runtime),
29206d46 1466 .dynamic_creds_offset = offsetof(Swap, dynamic_creds),
3ef63c31 1467
f975e971
LP
1468 .sections =
1469 "Unit\0"
1470 "Swap\0"
1471 "Install\0",
4ad49000 1472 .private_section = "Swap",
71645aca 1473
4e85aff4 1474 .init = swap_init,
07b0b134 1475 .load = swap_load,
6e620bec 1476 .done = swap_done,
07b0b134
ML
1477
1478 .coldplug = swap_coldplug,
1479
1480 .dump = swap_dump,
1481
1482 .start = swap_start,
1483 .stop = swap_stop,
1484
8a0867d6
LP
1485 .kill = swap_kill,
1486
68db7a3b
ZJS
1487 .get_timeout = swap_get_timeout,
1488
07b0b134
ML
1489 .serialize = swap_serialize,
1490 .deserialize_item = swap_deserialize_item,
1491
1492 .active_state = swap_active_state,
1493 .sub_state_to_string = swap_sub_state_to_string,
1494
f2f725e5 1495 .may_gc = swap_may_gc,
07b0b134 1496
e04aad61 1497 .sigchld_event = swap_sigchld_event,
e04aad61
LP
1498
1499 .reset_failed = swap_reset_failed,
1500
291d565a
LP
1501 .control_pid = swap_control_pid,
1502
718db961 1503 .bus_vtable = bus_swap_vtable,
74c964d3
LP
1504 .bus_set_property = bus_swap_set_property,
1505 .bus_commit_properties = bus_swap_commit_properties,
07b0b134 1506
e04aad61 1507 .following = swap_following,
6210e7fc 1508 .following_set = swap_following_set,
5632e374 1509
6e620bec 1510 .enumerate = swap_enumerate,
c6918296 1511 .shutdown = swap_shutdown,
0faacd47 1512 .supported = swap_supported,
c6918296
MS
1513
1514 .status_message_formats = {
1515 .starting_stopping = {
1516 [0] = "Activating swap %s...",
1517 [1] = "Deactivating swap %s...",
1518 },
1519 .finished_start_job = {
1520 [JOB_DONE] = "Activated swap %s.",
1521 [JOB_FAILED] = "Failed to activate swap %s.",
c6918296
MS
1522 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1523 },
1524 .finished_stop_job = {
1525 [JOB_DONE] = "Deactivated swap %s.",
1526 [JOB_FAILED] = "Failed deactivating swap %s.",
1527 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",
1528 },
1529 },
07b0b134 1530};