]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/swap.c
core: make log messages about unit processes exiting recognizable
[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
4366e598 8#include "sd-device.h"
b4bbcaa9 9
b5efdb8a 10#include "alloc-util.h"
07b0b134 11#include "dbus-swap.h"
4366e598
YW
12#include "device-private.h"
13#include "device-util.h"
57b7a260 14#include "device.h"
4f5dd394 15#include "escape.h"
9a57c629 16#include "exit-status.h"
3ffd4af2 17#include "fd-util.h"
f97b34a6 18#include "format-util.h"
4f5dd394 19#include "fstab-util.h"
6bedfcbb 20#include "parse-util.h"
9eb977db 21#include "path-util.h"
7b3e062c 22#include "process-util.h"
d68c645b 23#include "serialize.h"
4f5dd394 24#include "special.h"
8b43440b 25#include "string-table.h"
07630cea 26#include "string-util.h"
3ffd4af2 27#include "swap.h"
4f5dd394
LP
28#include "unit-name.h"
29#include "unit.h"
30#include "virt.h"
07b0b134
ML
31
32static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
33 [SWAP_DEAD] = UNIT_INACTIVE,
e04aad61 34 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
5bcb0f2b 35 [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
07b0b134 36 [SWAP_ACTIVE] = UNIT_ACTIVE,
e04aad61 37 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
e04aad61
LP
38 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
39 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 40 [SWAP_FAILED] = UNIT_FAILED
07b0b134
ML
41};
42
718db961
LP
43static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
44static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
45
dedf3719
LP
46static bool SWAP_STATE_WITH_PROCESS(SwapState state) {
47 return IN_SET(state,
48 SWAP_ACTIVATING,
49 SWAP_ACTIVATING_DONE,
50 SWAP_DEACTIVATING,
51 SWAP_DEACTIVATING_SIGTERM,
52 SWAP_DEACTIVATING_SIGKILL);
53}
54
e04aad61 55static void swap_unset_proc_swaps(Swap *s) {
9670d583
LP
56 assert(s);
57
58 if (!s->from_proc_swaps)
59 return;
60
a1e58e8e 61 s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what);
9670d583
LP
62
63 s->from_proc_swaps = false;
64}
65
66static int swap_set_devnode(Swap *s, const char *devnode) {
df326b84 67 Hashmap *swaps;
5bcb0f2b 68 Swap *first;
9670d583 69 int r;
e04aad61
LP
70
71 assert(s);
72
548f6937 73 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &path_hash_ops);
9670d583
LP
74 if (r < 0)
75 return r;
e04aad61 76
9670d583 77 swaps = UNIT(s)->manager->swaps_by_devnode;
e04aad61 78
9670d583
LP
79 if (s->devnode) {
80 first = hashmap_get(swaps, s->devnode);
e04aad61 81
9670d583
LP
82 LIST_REMOVE(same_devnode, first, s);
83 if (first)
84 hashmap_replace(swaps, first->devnode, first);
85 else
86 hashmap_remove(swaps, s->devnode);
5bcb0f2b 87
a1e58e8e 88 s->devnode = mfree(s->devnode);
9670d583
LP
89 }
90
91 if (devnode) {
92 s->devnode = strdup(devnode);
93 if (!s->devnode)
94 return -ENOMEM;
95
96 first = hashmap_get(swaps, s->devnode);
97 LIST_PREPEND(same_devnode, first, s);
98
99 return hashmap_replace(swaps, first->devnode, first);
100 }
101
102 return 0;
e04aad61
LP
103}
104
f6cebb3b 105static void swap_init(Unit *u) {
6e620bec
LP
106 Swap *s = SWAP(u);
107
108 assert(s);
1124fe6f 109 assert(UNIT(s)->load_state == UNIT_STUB);
6e620bec 110
1f19a534 111 s->timeout_usec = u->manager->default_timeout_start_usec;
e04aad61 112
ac155bb8
MS
113 s->exec_context.std_output = u->manager->default_std_output;
114 s->exec_context.std_error = u->manager->default_std_error;
085afe36 115
6b1dc2bd 116 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
e04aad61 117
e1770af8 118 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
c8f4d764 119
5bcb0f2b 120 u->ignore_on_isolate = true;
e04aad61
LP
121}
122
123static void swap_unwatch_control_pid(Swap *s) {
124 assert(s);
125
126 if (s->control_pid <= 0)
127 return;
128
129 unit_unwatch_pid(UNIT(s), s->control_pid);
130 s->control_pid = 0;
6e620bec
LP
131}
132
4e85aff4
LP
133static void swap_done(Unit *u) {
134 Swap *s = SWAP(u);
07b0b134 135
4e85aff4 136 assert(s);
07b0b134 137
e04aad61 138 swap_unset_proc_swaps(s);
9670d583 139 swap_set_devnode(s, NULL);
e04aad61 140
a1e58e8e
LP
141 s->what = mfree(s->what);
142 s->parameters_fragment.what = mfree(s->parameters_fragment.what);
143 s->parameters_fragment.options = mfree(s->parameters_fragment.options);
86b23b07 144
e8a565cb 145 s->exec_runtime = exec_runtime_unref(s->exec_runtime, false);
e04aad61
LP
146 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
147 s->control_command = NULL;
148
29206d46
LP
149 dynamic_creds_unref(&s->dynamic_creds);
150
e04aad61
LP
151 swap_unwatch_control_pid(s);
152
718db961
LP
153 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
154}
155
36c16a7c 156static int swap_arm_timer(Swap *s, usec_t usec) {
718db961
LP
157 int r;
158
159 assert(s);
160
718db961 161 if (s->timer_event_source) {
36c16a7c 162 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
163 if (r < 0)
164 return r;
165
166 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
167 }
168
36c16a7c
LP
169 if (usec == USEC_INFINITY)
170 return 0;
171
7dfbe2e3 172 r = sd_event_add_time(
6a0f1f6d
LP
173 UNIT(s)->manager->event,
174 &s->timer_event_source,
175 CLOCK_MONOTONIC,
36c16a7c 176 usec, 0,
6a0f1f6d 177 swap_dispatch_timer, s);
7dfbe2e3
TG
178 if (r < 0)
179 return r;
180
181 (void) sd_event_source_set_description(s->timer_event_source, "swap-timer");
182
183 return 0;
07b0b134
ML
184}
185
eef85c4a 186static int swap_add_device_dependencies(Swap *s) {
173a8d04
LP
187 assert(s);
188
189 if (!s->what)
190 return 0;
191
f10af76d
LP
192 if (!s->from_fragment)
193 return 0;
194
cfcfd4ae 195 if (is_device_path(s->what))
eef85c4a 196 return unit_add_node_dependency(UNIT(s), s->what, MANAGER_IS_SYSTEM(UNIT(s)->manager), UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE);
cfcfd4ae
LP
197 else
198 /* File based swap devices need to be ordered after
2b93b027 199 * systemd-remount-fs.service, since they might need a
cfcfd4ae 200 * writable file system. */
35d8c19a 201 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, true, UNIT_DEPENDENCY_FILE);
173a8d04
LP
202}
203
2edd4434 204static int swap_add_default_dependencies(Swap *s) {
8bf23dc7
FB
205 int r;
206
2edd4434
LP
207 assert(s);
208
4c9ea260
LP
209 if (!UNIT(s)->default_dependencies)
210 return 0;
211
463d0d15 212 if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
6b1dc2bd 213 return 0;
2edd4434 214
75f86906 215 if (detect_container() > 0)
c0387ebf
LP
216 return 0;
217
8bf23dc7
FB
218 /* swap units generated for the swap dev links are missing the
219 * ordering dep against the swap target. */
35d8c19a 220 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SWAP_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
8bf23dc7
FB
221 if (r < 0)
222 return r;
223
5a724170 224 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
2edd4434
LP
225}
226
4e85aff4 227static int swap_verify(Swap *s) {
7fd1b19b 228 _cleanup_free_ char *e = NULL;
7410616c 229 int r;
4e85aff4 230
1124fe6f 231 if (UNIT(s)->load_state != UNIT_LOADED)
5bcb0f2b 232 return 0;
4e85aff4 233
7410616c
LP
234 r = unit_name_from_path(s->what, ".swap", &e);
235 if (r < 0)
f2341e0a 236 return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m");
4e85aff4 237
7410616c 238 if (!unit_has_name(UNIT(s), e)) {
f2341e0a 239 log_unit_error(UNIT(s), "Value of What= and unit name do not match, not loading.");
6f40aa45 240 return -ENOEXEC;
4e85aff4
LP
241 }
242
4819ff03 243 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
f2341e0a 244 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.");
6f40aa45 245 return -ENOEXEC;
e04aad61
LP
246 }
247
4e85aff4
LP
248 return 0;
249}
250
9670d583 251static int swap_load_devnode(Swap *s) {
4366e598 252 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
9670d583
LP
253 struct stat st;
254 const char *p;
e5ca27b7 255 int r;
9670d583
LP
256
257 assert(s);
258
259 if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
260 return 0;
261
4366e598 262 r = device_new_from_stat_rdev(&d, &st);
e5ca27b7
LP
263 if (r < 0) {
264 log_unit_full(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
4366e598 265 "Failed to allocate device for swap %s: %m", s->what);
9670d583 266 return 0;
e5ca27b7 267 }
9670d583 268
4366e598 269 if (sd_device_get_devname(d, &p) < 0)
9670d583
LP
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 309
858d36c1 310 path_simplify(s->what, false);
07b0b134 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) {
4366e598
YW
429 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
430 const char *dn, *devlink;
431 struct stat st, st_link;
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
4366e598 445 r = device_new_from_stat_rdev(&d, &st);
e5ca27b7
LP
446 if (r < 0) {
447 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
4366e598 448 "Failed to allocate device for swap %s: %m", device);
5bcb0f2b 449 return 0;
e5ca27b7 450 }
e04aad61 451
5bcb0f2b 452 /* Add the main device node */
4366e598 453 if (sd_device_get_devname(d, &dn) >= 0 && !streq(dn, device))
628c89cc 454 swap_setup_unit(m, dn, device, prio, set_flags);
e04aad61 455
5bcb0f2b 456 /* Add additional units for all symlinks */
4366e598 457 FOREACH_DEVICE_DEVLINK(d, devlink) {
e04aad61 458
5bcb0f2b 459 /* Don't bother with the /dev/block links */
4366e598 460 if (streq(devlink, device))
5bcb0f2b 461 continue;
e04aad61 462
4366e598 463 if (path_startswith(devlink, "/dev/block/"))
5bcb0f2b 464 continue;
e04aad61 465
4366e598
YW
466 if (stat(devlink, &st_link) >= 0 &&
467 (!S_ISBLK(st_link.st_mode) ||
468 st_link.st_rdev != st.st_rdev))
469 continue;
e04aad61 470
4366e598 471 swap_setup_unit(m, devlink, device, prio, set_flags);
e04aad61
LP
472 }
473
e04aad61
LP
474 return r;
475}
476
07b0b134
ML
477static void swap_set_state(Swap *s, SwapState state) {
478 SwapState old_state;
37cf8fee 479 Swap *other;
e04aad61 480
07b0b134
ML
481 assert(s);
482
483 old_state = s->state;
484 s->state = state;
485
dedf3719 486 if (!SWAP_STATE_WITH_PROCESS(state)) {
718db961 487 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
e04aad61
LP
488 swap_unwatch_control_pid(s);
489 s->control_command = NULL;
490 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
491 }
492
07b0b134 493 if (state != old_state)
f2341e0a 494 log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state));
07b0b134 495
2ad2e41a 496 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
37cf8fee
LP
497
498 /* If there other units for the same device node have a job
499 queued it might be worth checking again if it is runnable
500 now. This is necessary, since swap_start() refuses
501 operation with EAGAIN if there's already another job for
502 the same device node queued. */
503 LIST_FOREACH_OTHERS(same_devnode, other, s)
504 if (UNIT(other)->job)
505 job_add_to_run_queue(UNIT(other)->job);
07b0b134
ML
506}
507
be847e82 508static int swap_coldplug(Unit *u) {
07b0b134
ML
509 Swap *s = SWAP(u);
510 SwapState new_state = SWAP_DEAD;
e04aad61 511 int r;
07b0b134
ML
512
513 assert(s);
514 assert(s->state == SWAP_DEAD);
515
516 if (s->deserialized_state != s->state)
517 new_state = s->deserialized_state;
4e85aff4 518 else if (s->from_proc_swaps)
07b0b134
ML
519 new_state = SWAP_ACTIVE;
520
5bcb0f2b
LP
521 if (new_state == s->state)
522 return 0;
e04aad61 523
c386f588
LP
524 if (s->control_pid > 0 &&
525 pid_is_unwaited(s->control_pid) &&
dedf3719 526 SWAP_STATE_WITH_PROCESS(new_state)) {
e04aad61 527
5bcb0f2b
LP
528 r = unit_watch_pid(UNIT(s), s->control_pid);
529 if (r < 0)
530 return r;
e04aad61 531
36c16a7c 532 r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
5bcb0f2b
LP
533 if (r < 0)
534 return r;
e04aad61 535 }
07b0b134 536
e8a565cb 537 if (!IN_SET(new_state, SWAP_DEAD, SWAP_FAILED)) {
29206d46 538 (void) unit_setup_dynamic_creds(u);
e8a565cb
YW
539 (void) unit_setup_exec_runtime(u);
540 }
29206d46 541
5bcb0f2b 542 swap_set_state(s, new_state);
07b0b134
ML
543 return 0;
544}
545
546static void swap_dump(Unit *u, FILE *f, const char *prefix) {
f2e18ef1 547 char buf[FORMAT_TIMESPAN_MAX];
07b0b134 548 Swap *s = SWAP(u);
4e85aff4 549 SwapParameters *p;
07b0b134
ML
550
551 assert(s);
4e85aff4
LP
552 assert(f);
553
554 if (s->from_proc_swaps)
555 p = &s->parameters_proc_swaps;
556 else if (s->from_fragment)
557 p = &s->parameters_fragment;
b6bfc7bb
LP
558 else
559 p = NULL;
07b0b134
ML
560
561 fprintf(f,
4e85aff4 562 "%sSwap State: %s\n"
e1770af8 563 "%sResult: %s\n"
07b0b134 564 "%sWhat: %s\n"
4e85aff4
LP
565 "%sFrom /proc/swaps: %s\n"
566 "%sFrom fragment: %s\n",
07b0b134 567 prefix, swap_state_to_string(s->state),
e1770af8 568 prefix, swap_result_to_string(s->result),
07b0b134 569 prefix, s->what,
4e85aff4
LP
570 prefix, yes_no(s->from_proc_swaps),
571 prefix, yes_no(s->from_fragment));
e04aad61 572
9670d583
LP
573 if (s->devnode)
574 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
575
b6bfc7bb
LP
576 if (p)
577 fprintf(f,
578 "%sPriority: %i\n"
47cb901e 579 "%sOptions: %s\n",
b6bfc7bb 580 prefix, p->priority,
47cb901e 581 prefix, strempty(p->options));
b6bfc7bb 582
9bd0e1b8
YW
583 fprintf(f,
584 "%sTimeoutSec: %s\n",
585 prefix, format_timespan(buf, sizeof(buf), s->timeout_usec, USEC_PER_SEC));
586
e04aad61
LP
587 if (s->control_pid > 0)
588 fprintf(f,
de0671ee
ZJS
589 "%sControl PID: "PID_FMT"\n",
590 prefix, s->control_pid);
e04aad61
LP
591
592 exec_context_dump(&s->exec_context, f, prefix);
4819ff03 593 kill_context_dump(&s->kill_context, f, prefix);
18f573aa 594 cgroup_context_dump(&s->cgroup_context, f, prefix);
e04aad61
LP
595}
596
597static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
3c7416b6 598
b9c04eaf 599 _cleanup_(exec_params_clear) ExecParameters exec_params = {
1703fa41 600 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
c39f1ce2
LP
601 .stdin_fd = -1,
602 .stdout_fd = -1,
603 .stderr_fd = -1,
5686391b 604 .exec_fd = -1,
9fa95f85 605 };
3c7416b6
LP
606 pid_t pid;
607 int r;
e04aad61
LP
608
609 assert(s);
610 assert(c);
611 assert(_pid);
612
3c7416b6 613 r = unit_prepare_exec(UNIT(s));
29206d46 614 if (r < 0)
3c7416b6 615 return r;
29206d46 616
36c16a7c 617 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
646134dc 618 if (r < 0)
e04aad61
LP
619 goto fail;
620
1ad6e8b3
LP
621 r = unit_set_exec_params(UNIT(s), &exec_params);
622 if (r < 0)
623 goto fail;
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 656 if (s->result != SWAP_SUCCESS)
7c047d74 657 unit_log_failure(UNIT(s), swap_result_to_string(s->result));
ed77d407 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;
6a1d4d9f 854 exec_command_reset_status_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
3c7416b6
LP
855
856 u->reset_accounting = true;
5ad096b3 857
e04aad61 858 swap_enter_activating(s);
82a2b6bb 859 return 1;
07b0b134
ML
860}
861
862static int swap_stop(Unit *u) {
863 Swap *s = SWAP(u);
07b0b134
ML
864
865 assert(s);
866
50864457
LP
867 switch (s->state) {
868
869 case SWAP_DEACTIVATING:
870 case SWAP_DEACTIVATING_SIGTERM:
871 case SWAP_DEACTIVATING_SIGKILL:
872 /* Already on it */
e04aad61 873 return 0;
07b0b134 874
50864457
LP
875 case SWAP_ACTIVATING:
876 case SWAP_ACTIVATING_DONE:
877 /* There's a control process pending, directly enter kill mode */
878 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS);
879 return 0;
07b0b134 880
50864457
LP
881 case SWAP_ACTIVE:
882 if (detect_container() > 0)
883 return -EPERM;
a5c3034f 884
50864457
LP
885 swap_enter_deactivating(s);
886 return 1;
887
888 default:
889 assert_not_reached("Unexpected state.");
890 }
07b0b134
ML
891}
892
893static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
894 Swap *s = SWAP(u);
895
896 assert(s);
897 assert(f);
898 assert(fds);
899
d68c645b
LP
900 (void) serialize_item(f, "state", swap_state_to_string(s->state));
901 (void) serialize_item(f, "result", swap_result_to_string(s->result));
e04aad61
LP
902
903 if (s->control_pid > 0)
d68c645b 904 (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid);
e04aad61
LP
905
906 if (s->control_command_id >= 0)
d68c645b 907 (void) serialize_item(f, "control-command", swap_exec_command_to_string(s->control_command_id));
07b0b134
ML
908
909 return 0;
910}
911
912static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
913 Swap *s = SWAP(u);
914
915 assert(s);
916 assert(fds);
917
918 if (streq(key, "state")) {
919 SwapState state;
920
646134dc
ZJS
921 state = swap_state_from_string(value);
922 if (state < 0)
f2341e0a 923 log_unit_debug(u, "Failed to parse state value: %s", value);
07b0b134
ML
924 else
925 s->deserialized_state = state;
e1770af8
LP
926 } else if (streq(key, "result")) {
927 SwapResult f;
928
929 f = swap_result_from_string(value);
930 if (f < 0)
f2341e0a 931 log_unit_debug(u, "Failed to parse result value: %s", value);
e1770af8
LP
932 else if (f != SWAP_SUCCESS)
933 s->result = f;
e04aad61
LP
934 } else if (streq(key, "control-pid")) {
935 pid_t pid;
936
937 if (parse_pid(value, &pid) < 0)
f2341e0a 938 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
e04aad61
LP
939 else
940 s->control_pid = pid;
941
942 } else if (streq(key, "control-command")) {
943 SwapExecCommand id;
944
646134dc
ZJS
945 id = swap_exec_command_from_string(value);
946 if (id < 0)
f2341e0a 947 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
e04aad61
LP
948 else {
949 s->control_command_id = id;
950 s->control_command = s->exec_command + id;
951 }
07b0b134 952 } else
f2341e0a 953 log_unit_debug(u, "Unknown serialization key: %s", key);
07b0b134
ML
954
955 return 0;
956}
957
44a6b1b6 958_pure_ static UnitActiveState swap_active_state(Unit *u) {
07b0b134
ML
959 assert(u);
960
961 return state_translation_table[SWAP(u)->state];
962}
963
44a6b1b6 964_pure_ static const char *swap_sub_state_to_string(Unit *u) {
07b0b134
ML
965 assert(u);
966
967 return swap_state_to_string(SWAP(u)->state);
968}
969
f2f725e5 970_pure_ static bool swap_may_gc(Unit *u) {
07b0b134
ML
971 Swap *s = SWAP(u);
972
973 assert(s);
974
f2f725e5
ZJS
975 if (s->from_proc_swaps)
976 return false;
977
978 return true;
07b0b134
ML
979}
980
e04aad61
LP
981static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
982 Swap *s = SWAP(u);
e1770af8 983 SwapResult f;
e04aad61
LP
984
985 assert(s);
986 assert(pid >= 0);
987
988 if (pid != s->control_pid)
989 return;
990
991 s->control_pid = 0;
992
1f0958f6 993 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
e1770af8
LP
994 f = SWAP_SUCCESS;
995 else if (code == CLD_EXITED)
996 f = SWAP_FAILURE_EXIT_CODE;
997 else if (code == CLD_KILLED)
998 f = SWAP_FAILURE_SIGNAL;
999 else if (code == CLD_DUMPED)
1000 f = SWAP_FAILURE_CORE_DUMP;
1001 else
1002 assert_not_reached("Unknown code");
1003
a0fef983 1004 if (s->result == SWAP_SUCCESS)
e1770af8 1005 s->result = f;
e04aad61
LP
1006
1007 if (s->control_command) {
6ea832a2 1008 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
e1770af8 1009
e04aad61
LP
1010 s->control_command = NULL;
1011 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1012 }
1013
91bbd9b7
LP
1014 unit_log_process_exit(
1015 u, f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1016 "Swap process",
1017 swap_exec_command_to_string(s->control_command_id),
1018 code, status);
e04aad61
LP
1019
1020 switch (s->state) {
1021
1022 case SWAP_ACTIVATING:
5bcb0f2b 1023 case SWAP_ACTIVATING_DONE:
e04aad61 1024
50864457 1025 if (f == SWAP_SUCCESS || s->from_proc_swaps)
e1770af8 1026 swap_enter_active(s, f);
e04aad61 1027 else
e1770af8 1028 swap_enter_dead(s, f);
e04aad61
LP
1029 break;
1030
1031 case SWAP_DEACTIVATING:
1032 case SWAP_DEACTIVATING_SIGKILL:
1033 case SWAP_DEACTIVATING_SIGTERM:
1034
50864457 1035 swap_enter_dead_or_active(s, f);
e04aad61
LP
1036 break;
1037
1038 default:
1039 assert_not_reached("Uh, control process died at wrong time.");
1040 }
1041
1042 /* Notify clients about changed exit status */
1043 unit_add_to_dbus_queue(u);
e04aad61
LP
1044}
1045
718db961
LP
1046static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1047 Swap *s = SWAP(userdata);
e04aad61
LP
1048
1049 assert(s);
718db961 1050 assert(s->timer_event_source == source);
e04aad61
LP
1051
1052 switch (s->state) {
1053
1054 case SWAP_ACTIVATING:
5bcb0f2b 1055 case SWAP_ACTIVATING_DONE:
f2341e0a 1056 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
50864457 1057 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1058 break;
1059
1060 case SWAP_DEACTIVATING:
f2341e0a 1061 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
e1770af8 1062 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1063 break;
1064
e04aad61 1065 case SWAP_DEACTIVATING_SIGTERM:
4819ff03 1066 if (s->kill_context.send_sigkill) {
50864457 1067 log_unit_warning(UNIT(s), "Swap process timed out. Killing.");
e1770af8 1068 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
ba035df2 1069 } else {
50864457
LP
1070 log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring.");
1071 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
ba035df2 1072 }
e04aad61
LP
1073 break;
1074
e04aad61 1075 case SWAP_DEACTIVATING_SIGKILL:
f2341e0a 1076 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
50864457 1077 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1078 break;
1079
1080 default:
1081 assert_not_reached("Timeout at wrong time.");
1082 }
718db961
LP
1083
1084 return 0;
e04aad61
LP
1085}
1086
1087static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1088 unsigned i;
1089 int r = 0;
1090
1091 assert(m);
1092
07b0b134 1093 rewind(m->proc_swaps);
bab45044 1094
4e85aff4 1095 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
07b0b134 1096
e04aad61 1097 for (i = 1;; i++) {
9fb3675e 1098 _cleanup_free_ char *dev = NULL, *d = NULL;
07b0b134
ML
1099 int prio = 0, k;
1100
646134dc
ZJS
1101 k = fscanf(m->proc_swaps,
1102 "%ms " /* device/file */
1103 "%*s " /* type of swap */
1104 "%*s " /* swap size */
1105 "%*s " /* used */
1106 "%i\n", /* priority */
1107 &dev, &prio);
1108 if (k != 2) {
07b0b134 1109 if (k == EOF)
4e85aff4 1110 break;
07b0b134 1111
628c89cc 1112 log_warning("Failed to parse /proc/swaps:%u.", i);
e04aad61 1113 continue;
07b0b134 1114 }
07b0b134 1115
527b7a42 1116 if (cunescape(dev, UNESCAPE_RELAX, &d) < 0)
628c89cc
LP
1117 return log_oom();
1118
485ae697 1119 device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
4e85aff4 1120
628c89cc 1121 k = swap_process_new(m, d, prio, set_flags);
07b0b134 1122 if (k < 0)
e04aad61 1123 r = k;
07b0b134
ML
1124 }
1125
e04aad61
LP
1126 return r;
1127}
1128
718db961
LP
1129static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1130 Manager *m = userdata;
595ed347 1131 Unit *u;
4e434314
LP
1132 int r;
1133
1134 assert(m);
718db961 1135 assert(revents & EPOLLPRI);
4e434314 1136
646134dc
ZJS
1137 r = swap_load_proc_swaps(m, true);
1138 if (r < 0) {
da927ba9 1139 log_error_errno(r, "Failed to reread /proc/swaps: %m");
e04aad61
LP
1140
1141 /* Reset flags, just in case, for late calls */
595ed347
MS
1142 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1143 Swap *swap = SWAP(u);
e04aad61
LP
1144
1145 swap->is_active = swap->just_activated = false;
1146 }
1147
1148 return 0;
1149 }
1150
1151 manager_dispatch_load_queue(m);
1152
595ed347
MS
1153 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1154 Swap *swap = SWAP(u);
e04aad61
LP
1155
1156 if (!swap->is_active) {
1157 /* This has just been deactivated */
1158
e04aad61
LP
1159 swap_unset_proc_swaps(swap);
1160
1161 switch (swap->state) {
1162
1163 case SWAP_ACTIVE:
e1770af8 1164 swap_enter_dead(swap, SWAP_SUCCESS);
e04aad61
LP
1165 break;
1166
1167 default:
5bcb0f2b 1168 /* Fire again */
e04aad61
LP
1169 swap_set_state(swap, swap->state);
1170 break;
1171 }
1172
628c89cc 1173 if (swap->what)
485ae697 1174 device_found_node(m, swap->what, 0, DEVICE_FOUND_SWAP);
628c89cc 1175
e04aad61
LP
1176 } else if (swap->just_activated) {
1177
1178 /* New swap entry */
1179
1180 switch (swap->state) {
1181
1182 case SWAP_DEAD:
1183 case SWAP_FAILED:
4b58153d 1184 (void) unit_acquire_invocation_id(UNIT(swap));
e1770af8 1185 swap_enter_active(swap, SWAP_SUCCESS);
e04aad61
LP
1186 break;
1187
5bcb0f2b
LP
1188 case SWAP_ACTIVATING:
1189 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1190 break;
1191
e04aad61
LP
1192 default:
1193 /* Nothing really changed, but let's
1194 * issue an notification call
1195 * nonetheless, in case somebody is
1196 * waiting for this. */
1197 swap_set_state(swap, swap->state);
1198 break;
1199 }
1200 }
1201
1202 /* Reset the flags for later calls */
1203 swap->is_active = swap->just_activated = false;
1204 }
1205
1206 return 1;
1207}
1208
1209static Unit *swap_following(Unit *u) {
1210 Swap *s = SWAP(u);
1211 Swap *other, *first = NULL;
1212
1213 assert(s);
1214
cdc89820
ZJS
1215 /* If the user configured the swap through /etc/fstab or
1216 * a device unit, follow that. */
1217
1218 if (s->from_fragment)
e04aad61
LP
1219 return NULL;
1220
caac2704 1221 LIST_FOREACH_OTHERS(same_devnode, other, s)
cdc89820
ZJS
1222 if (other->from_fragment)
1223 return UNIT(other);
1224
b938cb90 1225 /* Otherwise, make everybody follow the unit that's named after
cdc89820
ZJS
1226 * the swap device in the kernel */
1227
1228 if (streq_ptr(s->what, s->devnode))
1229 return NULL;
e04aad61 1230
9670d583
LP
1231 LIST_FOREACH_AFTER(same_devnode, other, s)
1232 if (streq_ptr(other->what, other->devnode))
e04aad61
LP
1233 return UNIT(other);
1234
9670d583
LP
1235 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1236 if (streq_ptr(other->what, other->devnode))
e04aad61
LP
1237 return UNIT(other);
1238
1239 first = other;
1240 }
1241
cdc89820 1242 /* Fall back to the first on the list */
e04aad61 1243 return UNIT(first);
07b0b134
ML
1244}
1245
6210e7fc 1246static int swap_following_set(Unit *u, Set **_set) {
5bcb0f2b 1247 Swap *s = SWAP(u), *other;
af4fa99d 1248 _cleanup_set_free_ Set *set = NULL;
6210e7fc
LP
1249 int r;
1250
1251 assert(s);
1252 assert(_set);
1253
9670d583 1254 if (LIST_JUST_US(same_devnode, s)) {
6210e7fc
LP
1255 *_set = NULL;
1256 return 0;
1257 }
1258
d5099efc 1259 set = set_new(NULL);
5bcb0f2b 1260 if (!set)
6210e7fc
LP
1261 return -ENOMEM;
1262
caac2704 1263 LIST_FOREACH_OTHERS(same_devnode, other, s) {
5bcb0f2b
LP
1264 r = set_put(set, other);
1265 if (r < 0)
95f14a3e 1266 return r;
5bcb0f2b 1267 }
6210e7fc 1268
95f14a3e 1269 *_set = TAKE_PTR(set);
6210e7fc 1270 return 1;
6210e7fc
LP
1271}
1272
07b0b134
ML
1273static void swap_shutdown(Manager *m) {
1274 assert(m);
1275
718db961 1276 m->swap_event_source = sd_event_source_unref(m->swap_event_source);
74ca738f 1277 m->proc_swaps = safe_fclose(m->proc_swaps);
525d3cc7 1278 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
07b0b134
ML
1279}
1280
ba64af90 1281static void swap_enumerate(Manager *m) {
07b0b134 1282 int r;
9670d583 1283
07b0b134
ML
1284 assert(m);
1285
4e434314 1286 if (!m->proc_swaps) {
646134dc 1287 m->proc_swaps = fopen("/proc/swaps", "re");
ba64af90
LP
1288 if (!m->proc_swaps) {
1289 if (errno == ENOENT)
cb209a04 1290 log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
ba64af90 1291 else
cb209a04 1292 log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m");
ba64af90
LP
1293
1294 return;
1295 }
4e434314 1296
151b9b96 1297 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
ba64af90
LP
1298 if (r < 0) {
1299 log_error_errno(r, "Failed to watch /proc/swaps: %m");
29083707 1300 goto fail;
ba64af90 1301 }
29083707
LP
1302
1303 /* Dispatch this before we dispatch SIGCHLD, so that
1304 * we always get the events from /proc/swaps before
1305 * the SIGCHLD of /sbin/swapon. */
83231637 1306 r = sd_event_source_set_priority(m->swap_event_source, SD_EVENT_PRIORITY_NORMAL-10);
ba64af90
LP
1307 if (r < 0) {
1308 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
718db961 1309 goto fail;
ba64af90 1310 }
7dfbe2e3
TG
1311
1312 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
4e434314
LP
1313 }
1314
646134dc
ZJS
1315 r = swap_load_proc_swaps(m, false);
1316 if (r < 0)
718db961
LP
1317 goto fail;
1318
ba64af90 1319 return;
07b0b134 1320
718db961
LP
1321fail:
1322 swap_shutdown(m);
07b0b134
ML
1323}
1324
4366e598 1325int swap_process_device_new(Manager *m, sd_device *dev) {
9670d583 1326 _cleanup_free_ char *e = NULL;
4366e598 1327 const char *dn, *devlink;
cabf58b2 1328 Unit *u;
9670d583
LP
1329 int r = 0;
1330
1331 assert(m);
1332 assert(dev);
1333
4366e598
YW
1334 r = sd_device_get_devname(dev, &dn);
1335 if (r < 0)
9670d583
LP
1336 return 0;
1337
7410616c
LP
1338 r = unit_name_from_path(dn, ".swap", &e);
1339 if (r < 0)
1340 return r;
9670d583 1341
cabf58b2
FB
1342 u = manager_get_unit(m, e);
1343 if (u)
1344 r = swap_set_devnode(SWAP(u), dn);
9670d583 1345
4366e598 1346 FOREACH_DEVICE_DEVLINK(dev, devlink) {
9670d583 1347 _cleanup_free_ char *n = NULL;
7410616c 1348 int q;
9670d583 1349
4366e598 1350 q = unit_name_from_path(devlink, ".swap", &n);
7410616c
LP
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
4366e598 1365int swap_process_device_remove(Manager *m, sd_device *dev) {
9670d583
LP
1366 const char *dn;
1367 int r = 0;
1368 Swap *s;
1369
4366e598
YW
1370 r = sd_device_get_devname(dev, &dn);
1371 if (r < 0)
9670d583
LP
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};