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