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