udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / swap.c
blob37642a64a61c97fc840f04dcbd1837b23a65ad39
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <sys/epoll.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
8 #include "sd-device.h"
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"
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,
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);
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);
58 _pure_ static UnitActiveState swap_active_state(Unit *u) {
59 assert(u);
61 return state_translation_table[SWAP(u)->state];
64 _pure_ static const char *swap_sub_state_to_string(Unit *u) {
65 assert(u);
67 return swap_state_to_string(SWAP(u)->state);
70 _pure_ static bool swap_may_gc(Unit *u) {
71 Swap *s = SWAP(u);
73 assert(s);
75 if (s->from_proc_swaps)
76 return false;
78 return true;
81 _pure_ static bool swap_is_extrinsic(Unit *u) {
82 assert(SWAP(u));
84 return MANAGER_IS_USER(u->manager);
87 static void swap_unset_proc_swaps(Swap *s) {
88 assert(s);
90 if (!s->from_proc_swaps)
91 return;
93 s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what);
94 s->from_proc_swaps = false;
97 static int swap_set_devnode(Swap *s, const char *devnode) {
98 Hashmap *swaps;
99 Swap *first;
100 int r;
102 assert(s);
104 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &path_hash_ops);
105 if (r < 0)
106 return r;
108 swaps = UNIT(s)->manager->swaps_by_devnode;
110 if (s->devnode) {
111 first = hashmap_get(swaps, s->devnode);
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);
119 s->devnode = mfree(s->devnode);
122 if (devnode) {
123 s->devnode = strdup(devnode);
124 if (!s->devnode)
125 return -ENOMEM;
127 first = hashmap_get(swaps, s->devnode);
128 LIST_PREPEND(same_devnode, first, s);
130 return hashmap_replace(swaps, first->devnode, first);
133 return 0;
136 static void swap_init(Unit *u) {
137 Swap *s = SWAP(u);
139 assert(s);
140 assert(UNIT(s)->load_state == UNIT_STUB);
142 s->timeout_usec = u->manager->default_timeout_start_usec;
144 s->exec_context.std_output = u->manager->default_std_output;
145 s->exec_context.std_error = u->manager->default_std_error;
147 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
149 u->ignore_on_isolate = true;
152 static void swap_unwatch_control_pid(Swap *s) {
153 assert(s);
155 if (s->control_pid <= 0)
156 return;
158 unit_unwatch_pid(UNIT(s), TAKE_PID(s->control_pid));
161 static void swap_done(Unit *u) {
162 Swap *s = SWAP(u);
164 assert(s);
166 swap_unset_proc_swaps(s);
167 swap_set_devnode(s, NULL);
169 s->what = mfree(s->what);
170 s->parameters_fragment.what = mfree(s->parameters_fragment.what);
171 s->parameters_fragment.options = mfree(s->parameters_fragment.options);
173 s->exec_runtime = exec_runtime_free(s->exec_runtime);
174 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
175 s->control_command = NULL;
177 swap_unwatch_control_pid(s);
179 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
182 static int swap_arm_timer(Swap *s, usec_t usec) {
183 int r;
185 assert(s);
187 if (s->timer_event_source) {
188 r = sd_event_source_set_time(s->timer_event_source, usec);
189 if (r < 0)
190 return r;
192 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
195 if (usec == USEC_INFINITY)
196 return 0;
198 r = sd_event_add_time(
199 UNIT(s)->manager->event,
200 &s->timer_event_source,
201 CLOCK_MONOTONIC,
202 usec, 0,
203 swap_dispatch_timer, s);
204 if (r < 0)
205 return r;
207 (void) sd_event_source_set_description(s->timer_event_source, "swap-timer");
209 return 0;
212 static SwapParameters* swap_get_parameters(Swap *s) {
213 assert(s);
215 if (s->from_proc_swaps)
216 return &s->parameters_proc_swaps;
218 if (s->from_fragment)
219 return &s->parameters_fragment;
221 return NULL;
224 static int swap_add_device_dependencies(Swap *s) {
225 UnitDependencyMask mask;
226 SwapParameters *p;
227 int r;
229 assert(s);
231 if (!s->what)
232 return 0;
234 p = swap_get_parameters(s);
235 if (!p || !p->what)
236 return 0;
238 mask = s->from_proc_swaps ? UNIT_DEPENDENCY_PROC_SWAP : UNIT_DEPENDENCY_FILE;
240 if (is_device_path(p->what)) {
241 r = unit_add_node_dependency(UNIT(s), p->what, UNIT_REQUIRES, mask);
242 if (r < 0)
243 return r;
245 return unit_add_blockdev_dependency(UNIT(s), p->what, mask);
248 /* File based swap devices need to be ordered after systemd-remount-fs.service, since they might need
249 * a writable file system. */
250 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, true, mask);
253 static int swap_add_default_dependencies(Swap *s) {
254 int r;
256 assert(s);
258 if (!UNIT(s)->default_dependencies)
259 return 0;
261 if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
262 return 0;
264 if (detect_container() > 0)
265 return 0;
267 /* swap units generated for the swap dev links are missing the
268 * ordering dep against the swap target. */
269 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SWAP_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
270 if (r < 0)
271 return r;
273 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
276 static int swap_verify(Swap *s) {
277 _cleanup_free_ char *e = NULL;
278 int r;
280 assert(UNIT(s)->load_state == UNIT_LOADED);
282 r = unit_name_from_path(s->what, ".swap", &e);
283 if (r < 0)
284 return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m");
286 if (!unit_has_name(UNIT(s), e))
287 return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Value of What= and unit name do not match, not loading.");
289 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP)
290 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.");
292 return 0;
295 static int swap_load_devnode(Swap *s) {
296 _cleanup_free_ char *p = NULL;
297 struct stat st;
298 int r;
300 assert(s);
302 if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
303 return 0;
305 r = devname_from_stat_rdev(&st, &p);
306 if (r < 0) {
307 log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
308 "Failed to get device node for swap %s: %m", s->what);
309 return 0;
312 return swap_set_devnode(s, p);
315 static int swap_add_extras(Swap *s) {
316 int r;
318 assert(s);
320 if (UNIT(s)->fragment_path)
321 s->from_fragment = true;
323 if (!s->what) {
324 if (s->parameters_fragment.what)
325 s->what = strdup(s->parameters_fragment.what);
326 else if (s->parameters_proc_swaps.what)
327 s->what = strdup(s->parameters_proc_swaps.what);
328 else {
329 r = unit_name_to_path(UNIT(s)->id, &s->what);
330 if (r < 0)
331 return r;
334 if (!s->what)
335 return -ENOMEM;
338 path_simplify(s->what);
340 if (!UNIT(s)->description) {
341 r = unit_set_description(UNIT(s), s->what);
342 if (r < 0)
343 return r;
346 r = unit_require_mounts_for(UNIT(s), s->what, UNIT_DEPENDENCY_IMPLICIT);
347 if (r < 0)
348 return r;
350 r = swap_add_device_dependencies(s);
351 if (r < 0)
352 return r;
354 r = swap_load_devnode(s);
355 if (r < 0)
356 return r;
358 r = unit_patch_contexts(UNIT(s));
359 if (r < 0)
360 return r;
362 r = unit_add_exec_dependencies(UNIT(s), &s->exec_context);
363 if (r < 0)
364 return r;
366 r = unit_set_default_slice(UNIT(s));
367 if (r < 0)
368 return r;
370 r = swap_add_default_dependencies(s);
371 if (r < 0)
372 return r;
374 return 0;
377 static int swap_load(Unit *u) {
378 Swap *s = SWAP(u);
379 int r, q = 0;
381 assert(s);
382 assert(u->load_state == UNIT_STUB);
384 /* Load a .swap file */
385 bool fragment_optional = s->from_proc_swaps;
386 r = unit_load_fragment_and_dropin(u, !fragment_optional);
388 /* Add in some extras, and do so either when we successfully loaded something or when /proc/swaps is
389 * already active. */
390 if (u->load_state == UNIT_LOADED || s->from_proc_swaps)
391 q = swap_add_extras(s);
393 if (r < 0)
394 return r;
395 if (q < 0)
396 return q;
397 if (u->load_state != UNIT_LOADED)
398 return 0;
400 return swap_verify(s);
403 static int swap_setup_unit(
404 Manager *m,
405 const char *what,
406 const char *what_proc_swaps,
407 int priority,
408 bool set_flags) {
410 _cleanup_free_ char *e = NULL;
411 bool delete = false;
412 Unit *u = NULL;
413 int r;
414 SwapParameters *p;
416 assert(m);
417 assert(what);
418 assert(what_proc_swaps);
420 r = unit_name_from_path(what, ".swap", &e);
421 if (r < 0)
422 return log_unit_error_errno(u, r, "Failed to generate unit name from path: %m");
424 u = manager_get_unit(m, e);
425 if (u &&
426 SWAP(u)->from_proc_swaps &&
427 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
428 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
429 "Swap %s appeared twice with different device paths %s and %s",
430 e, SWAP(u)->parameters_proc_swaps.what, what_proc_swaps);
432 if (!u) {
433 delete = true;
435 r = unit_new_for_name(m, sizeof(Swap), e, &u);
436 if (r < 0)
437 goto fail;
439 SWAP(u)->what = strdup(what);
440 if (!SWAP(u)->what) {
441 r = -ENOMEM;
442 goto fail;
445 unit_add_to_load_queue(u);
446 } else
447 delete = false;
449 p = &SWAP(u)->parameters_proc_swaps;
451 if (!p->what) {
452 p->what = strdup(what_proc_swaps);
453 if (!p->what) {
454 r = -ENOMEM;
455 goto fail;
459 /* The unit is definitely around now, mark it as loaded if it was previously referenced but could not be
460 * loaded. After all we can load it now, from the data in /proc/swaps. */
461 if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) {
462 u->load_state = UNIT_LOADED;
463 u->load_error = 0;
466 if (set_flags) {
467 SWAP(u)->is_active = true;
468 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
471 SWAP(u)->from_proc_swaps = true;
473 p->priority = priority;
474 p->priority_set = true;
476 unit_add_to_dbus_queue(u);
477 return 0;
479 fail:
480 log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
482 if (delete)
483 unit_free(u);
485 return r;
488 static void swap_process_new(Manager *m, const char *device, int prio, bool set_flags) {
489 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
490 const char *dn;
491 struct stat st, st_link;
492 int r;
494 assert(m);
496 if (swap_setup_unit(m, device, device, prio, set_flags) < 0)
497 return;
499 /* If this is a block device, then let's add duplicates for
500 * all other names of this block device */
501 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
502 return;
504 r = sd_device_new_from_stat_rdev(&d, &st);
505 if (r < 0)
506 return (void) log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
507 "Failed to allocate device for swap %s: %m", device);
509 /* Add the main device node */
510 if (sd_device_get_devname(d, &dn) >= 0 && !streq(dn, device))
511 (void) swap_setup_unit(m, dn, device, prio, set_flags);
513 /* Add additional units for all symlinks */
514 FOREACH_DEVICE_DEVLINK(d, devlink) {
516 /* Don't bother with the /dev/block links */
517 if (streq(devlink, device))
518 continue;
520 if (path_startswith(devlink, "/dev/block/"))
521 continue;
523 if (stat(devlink, &st_link) >= 0 &&
524 (!S_ISBLK(st_link.st_mode) ||
525 st_link.st_rdev != st.st_rdev))
526 continue;
528 (void) swap_setup_unit(m, devlink, device, prio, set_flags);
532 static void swap_set_state(Swap *s, SwapState state) {
533 SwapState old_state;
535 assert(s);
537 if (s->state != state)
538 bus_unit_send_pending_change_signal(UNIT(s), false);
540 old_state = s->state;
541 s->state = state;
543 if (!SWAP_STATE_WITH_PROCESS(state)) {
544 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
545 swap_unwatch_control_pid(s);
546 s->control_command = NULL;
547 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
550 if (state != old_state)
551 log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state));
553 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
555 /* If there other units for the same device node have a job
556 queued it might be worth checking again if it is runnable
557 now. This is necessary, since swap_start() refuses
558 operation with EAGAIN if there's already another job for
559 the same device node queued. */
560 LIST_FOREACH_OTHERS(same_devnode, other, s)
561 if (UNIT(other)->job)
562 job_add_to_run_queue(UNIT(other)->job);
565 static int swap_coldplug(Unit *u) {
566 Swap *s = SWAP(u);
567 SwapState new_state = SWAP_DEAD;
568 int r;
570 assert(s);
571 assert(s->state == SWAP_DEAD);
573 if (s->deserialized_state != s->state)
574 new_state = s->deserialized_state;
575 else if (s->from_proc_swaps)
576 new_state = SWAP_ACTIVE;
578 if (new_state == s->state)
579 return 0;
581 if (s->control_pid > 0 &&
582 pid_is_unwaited(s->control_pid) &&
583 SWAP_STATE_WITH_PROCESS(new_state)) {
585 r = unit_watch_pid(UNIT(s), s->control_pid, false);
586 if (r < 0)
587 return r;
589 r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
590 if (r < 0)
591 return r;
594 if (!IN_SET(new_state, SWAP_DEAD, SWAP_FAILED))
595 (void) unit_setup_exec_runtime(u);
597 swap_set_state(s, new_state);
598 return 0;
601 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
602 Swap *s = SWAP(u);
603 SwapParameters *p;
605 assert(s);
606 assert(f);
608 if (s->from_proc_swaps)
609 p = &s->parameters_proc_swaps;
610 else if (s->from_fragment)
611 p = &s->parameters_fragment;
612 else
613 p = NULL;
615 fprintf(f,
616 "%sSwap State: %s\n"
617 "%sResult: %s\n"
618 "%sClean Result: %s\n"
619 "%sWhat: %s\n"
620 "%sFrom /proc/swaps: %s\n"
621 "%sFrom fragment: %s\n"
622 "%sExtrinsic: %s\n",
623 prefix, swap_state_to_string(s->state),
624 prefix, swap_result_to_string(s->result),
625 prefix, swap_result_to_string(s->clean_result),
626 prefix, s->what,
627 prefix, yes_no(s->from_proc_swaps),
628 prefix, yes_no(s->from_fragment),
629 prefix, yes_no(swap_is_extrinsic(u)));
631 if (s->devnode)
632 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
634 if (p)
635 fprintf(f,
636 "%sPriority: %i\n"
637 "%sOptions: %s\n",
638 prefix, p->priority,
639 prefix, strempty(p->options));
641 fprintf(f,
642 "%sTimeoutSec: %s\n",
643 prefix, FORMAT_TIMESPAN(s->timeout_usec, USEC_PER_SEC));
645 if (s->control_pid > 0)
646 fprintf(f,
647 "%sControl PID: "PID_FMT"\n",
648 prefix, s->control_pid);
650 exec_context_dump(&s->exec_context, f, prefix);
651 kill_context_dump(&s->kill_context, f, prefix);
652 cgroup_context_dump(UNIT(s), f, prefix);
655 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
657 _cleanup_(exec_params_clear) ExecParameters exec_params = {
658 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
659 .stdin_fd = -EBADF,
660 .stdout_fd = -EBADF,
661 .stderr_fd = -EBADF,
662 .exec_fd = -EBADF,
664 pid_t pid;
665 int r;
667 assert(s);
668 assert(c);
669 assert(_pid);
671 r = unit_prepare_exec(UNIT(s));
672 if (r < 0)
673 return r;
675 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
676 if (r < 0)
677 goto fail;
679 r = unit_set_exec_params(UNIT(s), &exec_params);
680 if (r < 0)
681 goto fail;
683 r = exec_spawn(UNIT(s),
685 &s->exec_context,
686 &exec_params,
687 s->exec_runtime,
688 &s->cgroup_context,
689 &pid);
690 if (r < 0)
691 goto fail;
693 r = unit_watch_pid(UNIT(s), pid, true);
694 if (r < 0)
695 goto fail;
697 *_pid = pid;
699 return 0;
701 fail:
702 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
704 return r;
707 static void swap_enter_dead(Swap *s, SwapResult f) {
708 assert(s);
710 if (s->result == SWAP_SUCCESS)
711 s->result = f;
713 unit_log_result(UNIT(s), s->result == SWAP_SUCCESS, swap_result_to_string(s->result));
714 unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop);
715 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
717 s->exec_runtime = exec_runtime_destroy(s->exec_runtime);
719 unit_destroy_runtime_data(UNIT(s), &s->exec_context);
721 unit_unref_uid_gid(UNIT(s), true);
724 static void swap_enter_active(Swap *s, SwapResult f) {
725 assert(s);
727 if (s->result == SWAP_SUCCESS)
728 s->result = f;
730 swap_set_state(s, SWAP_ACTIVE);
733 static void swap_enter_dead_or_active(Swap *s, SwapResult f) {
734 assert(s);
736 if (s->from_proc_swaps) {
737 swap_enter_active(s, f);
739 LIST_FOREACH_OTHERS(same_devnode, other, s)
740 if (UNIT(other)->job)
741 swap_enter_dead_or_active(other, f);
742 } else
743 swap_enter_dead(s, f);
746 static int state_to_kill_operation(Swap *s, SwapState state) {
747 if (state == SWAP_DEACTIVATING_SIGTERM) {
748 if (unit_has_job_type(UNIT(s), JOB_RESTART))
749 return KILL_RESTART;
750 else
751 return KILL_TERMINATE;
754 return KILL_KILL;
757 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
758 int r;
760 assert(s);
762 if (s->result == SWAP_SUCCESS)
763 s->result = f;
765 r = unit_kill_context(UNIT(s),
766 &s->kill_context,
767 state_to_kill_operation(s, state),
769 s->control_pid,
770 false);
771 if (r < 0)
772 goto fail;
774 if (r > 0) {
775 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
776 if (r < 0)
777 goto fail;
779 swap_set_state(s, state);
780 } else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill)
781 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
782 else
783 swap_enter_dead_or_active(s, SWAP_SUCCESS);
785 return;
787 fail:
788 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
789 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
792 static void swap_enter_activating(Swap *s) {
793 _cleanup_free_ char *opts = NULL;
794 int r;
796 assert(s);
798 unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start);
800 s->control_command_id = SWAP_EXEC_ACTIVATE;
801 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
803 if (s->from_fragment) {
804 int priority = 0;
806 r = fstab_find_pri(s->parameters_fragment.options, &priority);
807 if (r < 0)
808 log_unit_warning_errno(UNIT(s), r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
809 else if (r > 0 && s->parameters_fragment.priority_set)
810 log_unit_warning(UNIT(s), "Duplicate swap priority configuration by Priority= and Options= fields.");
812 if (r <= 0 && s->parameters_fragment.priority_set) {
813 if (s->parameters_fragment.options)
814 r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority);
815 else
816 r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority);
817 if (r < 0) {
818 r = -ENOMEM;
819 goto fail;
824 r = exec_command_set(s->control_command, "/sbin/swapon", "--fixpgsz", NULL);
825 if (r < 0)
826 goto fail;
828 if (s->parameters_fragment.options || opts) {
829 r = exec_command_append(s->control_command, "-o",
830 opts ?: s->parameters_fragment.options, NULL);
831 if (r < 0)
832 goto fail;
835 r = exec_command_append(s->control_command, s->what, NULL);
836 if (r < 0)
837 goto fail;
839 swap_unwatch_control_pid(s);
841 r = swap_spawn(s, s->control_command, &s->control_pid);
842 if (r < 0)
843 goto fail;
845 swap_set_state(s, SWAP_ACTIVATING);
846 return;
848 fail:
849 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m");
850 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
853 static void swap_enter_deactivating(Swap *s) {
854 int r;
856 assert(s);
858 s->control_command_id = SWAP_EXEC_DEACTIVATE;
859 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
861 r = exec_command_set(s->control_command,
862 "/sbin/swapoff",
863 s->what,
864 NULL);
865 if (r < 0)
866 goto fail;
868 swap_unwatch_control_pid(s);
870 r = swap_spawn(s, s->control_command, &s->control_pid);
871 if (r < 0)
872 goto fail;
874 swap_set_state(s, SWAP_DEACTIVATING);
876 return;
878 fail:
879 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m");
880 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
883 static void swap_cycle_clear(Swap *s) {
884 assert(s);
886 s->result = SWAP_SUCCESS;
887 exec_command_reset_status_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
888 UNIT(s)->reset_accounting = true;
891 static int swap_start(Unit *u) {
892 Swap *s = SWAP(u);
893 int r;
895 assert(s);
897 /* We cannot fulfill this request right now, try again later please! */
898 if (IN_SET(s->state,
899 SWAP_DEACTIVATING,
900 SWAP_DEACTIVATING_SIGTERM,
901 SWAP_DEACTIVATING_SIGKILL,
902 SWAP_CLEANING))
903 return -EAGAIN;
905 /* Already on it! */
906 if (s->state == SWAP_ACTIVATING)
907 return 0;
909 assert(IN_SET(s->state, SWAP_DEAD, SWAP_FAILED));
911 if (detect_container() > 0)
912 return -EPERM;
914 /* If there's a job for another swap unit for the same node
915 * running, then let's not dispatch this one for now, and wait
916 * until that other job has finished. */
917 LIST_FOREACH_OTHERS(same_devnode, other, s)
918 if (UNIT(other)->job && UNIT(other)->job->state == JOB_RUNNING)
919 return -EAGAIN;
921 r = unit_acquire_invocation_id(u);
922 if (r < 0)
923 return r;
925 swap_cycle_clear(s);
926 swap_enter_activating(s);
927 return 1;
930 static int swap_stop(Unit *u) {
931 Swap *s = SWAP(u);
933 assert(s);
935 switch (s->state) {
937 case SWAP_DEACTIVATING:
938 case SWAP_DEACTIVATING_SIGTERM:
939 case SWAP_DEACTIVATING_SIGKILL:
940 /* Already on it */
941 return 0;
943 case SWAP_ACTIVATING:
944 case SWAP_ACTIVATING_DONE:
945 /* There's a control process pending, directly enter kill mode */
946 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS);
947 return 0;
949 case SWAP_ACTIVE:
950 if (detect_container() > 0)
951 return -EPERM;
953 swap_enter_deactivating(s);
954 return 1;
956 case SWAP_CLEANING:
957 /* If we are currently cleaning, then abort it, brutally. */
958 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
959 return 0;
961 default:
962 assert_not_reached();
966 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
967 Swap *s = SWAP(u);
969 assert(s);
970 assert(f);
971 assert(fds);
973 (void) serialize_item(f, "state", swap_state_to_string(s->state));
974 (void) serialize_item(f, "result", swap_result_to_string(s->result));
976 if (s->control_pid > 0)
977 (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid);
979 if (s->control_command_id >= 0)
980 (void) serialize_item(f, "control-command", swap_exec_command_to_string(s->control_command_id));
982 return 0;
985 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
986 Swap *s = SWAP(u);
988 assert(s);
989 assert(fds);
991 if (streq(key, "state")) {
992 SwapState state;
994 state = swap_state_from_string(value);
995 if (state < 0)
996 log_unit_debug(u, "Failed to parse state value: %s", value);
997 else
998 s->deserialized_state = state;
999 } else if (streq(key, "result")) {
1000 SwapResult f;
1002 f = swap_result_from_string(value);
1003 if (f < 0)
1004 log_unit_debug(u, "Failed to parse result value: %s", value);
1005 else if (f != SWAP_SUCCESS)
1006 s->result = f;
1007 } else if (streq(key, "control-pid")) {
1008 pid_t pid;
1010 if (parse_pid(value, &pid) < 0)
1011 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
1012 else
1013 s->control_pid = pid;
1015 } else if (streq(key, "control-command")) {
1016 SwapExecCommand id;
1018 id = swap_exec_command_from_string(value);
1019 if (id < 0)
1020 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
1021 else {
1022 s->control_command_id = id;
1023 s->control_command = s->exec_command + id;
1025 } else
1026 log_unit_debug(u, "Unknown serialization key: %s", key);
1028 return 0;
1031 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1032 Swap *s = SWAP(u);
1033 SwapResult f;
1035 assert(s);
1036 assert(pid >= 0);
1038 if (pid != s->control_pid)
1039 return;
1041 /* Let's scan /proc/swaps before we process SIGCHLD. For the reasoning see the similar code in
1042 * mount.c */
1043 (void) swap_process_proc_swaps(u->manager);
1045 s->control_pid = 0;
1047 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
1048 f = SWAP_SUCCESS;
1049 else if (code == CLD_EXITED)
1050 f = SWAP_FAILURE_EXIT_CODE;
1051 else if (code == CLD_KILLED)
1052 f = SWAP_FAILURE_SIGNAL;
1053 else if (code == CLD_DUMPED)
1054 f = SWAP_FAILURE_CORE_DUMP;
1055 else
1056 assert_not_reached();
1058 if (s->result == SWAP_SUCCESS)
1059 s->result = f;
1061 if (s->control_command) {
1062 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
1064 s->control_command = NULL;
1065 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1068 unit_log_process_exit(
1070 "Swap process",
1071 swap_exec_command_to_string(s->control_command_id),
1072 f == SWAP_SUCCESS,
1073 code, status);
1075 switch (s->state) {
1077 case SWAP_ACTIVATING:
1078 case SWAP_ACTIVATING_DONE:
1080 if (f == SWAP_SUCCESS || s->from_proc_swaps)
1081 swap_enter_active(s, f);
1082 else
1083 swap_enter_dead(s, f);
1084 break;
1086 case SWAP_DEACTIVATING:
1087 case SWAP_DEACTIVATING_SIGKILL:
1088 case SWAP_DEACTIVATING_SIGTERM:
1090 swap_enter_dead_or_active(s, f);
1091 break;
1093 case SWAP_CLEANING:
1094 if (s->clean_result == SWAP_SUCCESS)
1095 s->clean_result = f;
1097 swap_enter_dead(s, SWAP_SUCCESS);
1098 break;
1100 default:
1101 assert_not_reached();
1104 /* Notify clients about changed exit status */
1105 unit_add_to_dbus_queue(u);
1108 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1109 Swap *s = SWAP(userdata);
1111 assert(s);
1112 assert(s->timer_event_source == source);
1114 switch (s->state) {
1116 case SWAP_ACTIVATING:
1117 case SWAP_ACTIVATING_DONE:
1118 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
1119 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1120 break;
1122 case SWAP_DEACTIVATING:
1123 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
1124 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1125 break;
1127 case SWAP_DEACTIVATING_SIGTERM:
1128 if (s->kill_context.send_sigkill) {
1129 log_unit_warning(UNIT(s), "Swap process timed out. Killing.");
1130 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1131 } else {
1132 log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring.");
1133 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
1135 break;
1137 case SWAP_DEACTIVATING_SIGKILL:
1138 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
1139 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
1140 break;
1142 case SWAP_CLEANING:
1143 log_unit_warning(UNIT(s), "Cleaning timed out. killing.");
1145 if (s->clean_result == SWAP_SUCCESS)
1146 s->clean_result = SWAP_FAILURE_TIMEOUT;
1148 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, 0);
1149 break;
1151 default:
1152 assert_not_reached();
1155 return 0;
1158 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1159 assert(m);
1161 rewind(m->proc_swaps);
1163 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1165 for (unsigned i = 1;; i++) {
1166 _cleanup_free_ char *dev = NULL, *d = NULL;
1167 int prio = 0, k;
1169 k = fscanf(m->proc_swaps,
1170 "%ms " /* device/file */
1171 "%*s " /* type of swap */
1172 "%*s " /* swap size */
1173 "%*s " /* used */
1174 "%i\n", /* priority */
1175 &dev, &prio);
1176 if (k != 2) {
1177 if (k == EOF)
1178 break;
1180 log_warning("Failed to parse /proc/swaps:%u.", i);
1181 continue;
1184 ssize_t l = cunescape(dev, UNESCAPE_RELAX, &d);
1185 if (l < 0)
1186 return log_error_errno(l, "Failed to unescape device path: %m");
1188 device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
1190 (void) swap_process_new(m, d, prio, set_flags);
1193 return 0;
1196 static int swap_process_proc_swaps(Manager *m) {
1197 int r;
1199 assert(m);
1201 r = swap_load_proc_swaps(m, true);
1202 if (r < 0) {
1203 log_error_errno(r, "Failed to reread /proc/swaps: %m");
1205 /* Reset flags, just in case, for late calls */
1206 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1207 Swap *swap = SWAP(u);
1209 assert(swap);
1211 swap->is_active = swap->just_activated = false;
1214 return 0;
1217 manager_dispatch_load_queue(m);
1219 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1220 Swap *swap = SWAP(u);
1222 assert(swap);
1224 if (!swap->is_active) {
1226 swap_unset_proc_swaps(swap);
1228 switch (swap->state) {
1230 case SWAP_ACTIVE:
1231 /* This has just been deactivated */
1232 swap_enter_dead(swap, SWAP_SUCCESS);
1233 break;
1235 default:
1236 /* Fire again */
1237 swap_set_state(swap, swap->state);
1238 break;
1241 if (swap->what)
1242 device_found_node(m, swap->what, DEVICE_NOT_FOUND, DEVICE_FOUND_SWAP);
1244 } else if (swap->just_activated) {
1246 /* New swap entry */
1248 switch (swap->state) {
1250 case SWAP_DEAD:
1251 case SWAP_FAILED:
1252 (void) unit_acquire_invocation_id(u);
1253 swap_cycle_clear(swap);
1254 swap_enter_active(swap, SWAP_SUCCESS);
1255 break;
1257 case SWAP_ACTIVATING:
1258 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1259 break;
1261 default:
1262 /* Nothing really changed, but let's
1263 * issue an notification call
1264 * nonetheless, in case somebody is
1265 * waiting for this. */
1266 swap_set_state(swap, swap->state);
1267 break;
1271 /* Reset the flags for later calls */
1272 swap->is_active = swap->just_activated = false;
1275 return 1;
1278 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1279 Manager *m = ASSERT_PTR(userdata);
1281 assert(revents & EPOLLPRI);
1283 return swap_process_proc_swaps(m);
1286 static Unit *swap_following(Unit *u) {
1287 Swap *s = SWAP(u);
1288 Swap *first = NULL;
1290 assert(s);
1292 /* If the user configured the swap through /etc/fstab or
1293 * a device unit, follow that. */
1295 if (s->from_fragment)
1296 return NULL;
1298 LIST_FOREACH_OTHERS(same_devnode, other, s)
1299 if (other->from_fragment)
1300 return UNIT(other);
1302 /* Otherwise, make everybody follow the unit that's named after
1303 * the swap device in the kernel */
1305 if (streq_ptr(s->what, s->devnode))
1306 return NULL;
1308 LIST_FOREACH(same_devnode, other, s->same_devnode_next)
1309 if (streq_ptr(other->what, other->devnode))
1310 return UNIT(other);
1312 LIST_FOREACH_BACKWARDS(same_devnode, other, s->same_devnode_prev) {
1313 if (streq_ptr(other->what, other->devnode))
1314 return UNIT(other);
1316 first = other;
1319 /* Fall back to the first on the list */
1320 return UNIT(first);
1323 static int swap_following_set(Unit *u, Set **_set) {
1324 Swap *s = SWAP(u);
1325 _cleanup_set_free_ Set *set = NULL;
1326 int r;
1328 assert(s);
1329 assert(_set);
1331 if (LIST_JUST_US(same_devnode, s)) {
1332 *_set = NULL;
1333 return 0;
1336 set = set_new(NULL);
1337 if (!set)
1338 return -ENOMEM;
1340 LIST_FOREACH_OTHERS(same_devnode, other, s) {
1341 r = set_put(set, other);
1342 if (r < 0)
1343 return r;
1346 *_set = TAKE_PTR(set);
1347 return 1;
1350 static void swap_shutdown(Manager *m) {
1351 assert(m);
1353 m->swap_event_source = sd_event_source_disable_unref(m->swap_event_source);
1354 m->proc_swaps = safe_fclose(m->proc_swaps);
1355 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
1358 static void swap_enumerate(Manager *m) {
1359 int r;
1361 assert(m);
1363 if (!m->proc_swaps) {
1364 m->proc_swaps = fopen("/proc/swaps", "re");
1365 if (!m->proc_swaps) {
1366 if (errno == ENOENT)
1367 log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
1368 else
1369 log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m");
1371 return;
1374 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
1375 if (r < 0) {
1376 log_error_errno(r, "Failed to watch /proc/swaps: %m");
1377 goto fail;
1380 /* Dispatch this before we dispatch SIGCHLD, so that
1381 * we always get the events from /proc/swaps before
1382 * the SIGCHLD of /sbin/swapon. */
1383 r = sd_event_source_set_priority(m->swap_event_source, SD_EVENT_PRIORITY_NORMAL-10);
1384 if (r < 0) {
1385 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
1386 goto fail;
1389 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
1392 r = swap_load_proc_swaps(m, false);
1393 if (r < 0)
1394 goto fail;
1396 return;
1398 fail:
1399 swap_shutdown(m);
1402 int swap_process_device_new(Manager *m, sd_device *dev) {
1403 _cleanup_free_ char *e = NULL;
1404 const char *dn;
1405 Unit *u;
1406 int r;
1408 assert(m);
1409 assert(dev);
1411 if (sd_device_get_devname(dev, &dn) < 0)
1412 return 0;
1414 r = unit_name_from_path(dn, ".swap", &e);
1415 if (r < 0) {
1416 log_debug_errno(r, "Cannot convert device name '%s' to unit name, ignoring: %m", dn);
1417 return 0;
1420 u = manager_get_unit(m, e);
1421 if (u)
1422 r = swap_set_devnode(SWAP(u), dn);
1424 FOREACH_DEVICE_DEVLINK(dev, devlink) {
1425 _cleanup_free_ char *n = NULL;
1426 int q;
1428 q = unit_name_from_path(devlink, ".swap", &n);
1429 if (q == -EINVAL) /* If the name is not convertible to unit name, we can't manage it */
1430 continue;
1431 if (q < 0)
1432 return q;
1434 u = manager_get_unit(m, n);
1435 if (u) {
1436 q = swap_set_devnode(SWAP(u), dn);
1437 if (q < 0)
1438 r = q;
1442 return r;
1445 int swap_process_device_remove(Manager *m, sd_device *dev) {
1446 const char *dn;
1447 int r;
1448 Swap *s;
1450 r = sd_device_get_devname(dev, &dn);
1451 if (r < 0)
1452 return 0;
1454 while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1455 int q;
1457 q = swap_set_devnode(s, NULL);
1458 if (q < 0)
1459 r = q;
1462 return r;
1465 static void swap_reset_failed(Unit *u) {
1466 Swap *s = SWAP(u);
1468 assert(s);
1470 if (s->state == SWAP_FAILED)
1471 swap_set_state(s, SWAP_DEAD);
1473 s->result = SWAP_SUCCESS;
1474 s->clean_result = SWAP_SUCCESS;
1477 static int swap_kill(Unit *u, KillWho who, int signo, int code, int value, sd_bus_error *error) {
1478 return unit_kill_common(u, who, signo, code, value, -1, SWAP(u)->control_pid, error);
1481 static int swap_get_timeout(Unit *u, usec_t *timeout) {
1482 Swap *s = SWAP(u);
1483 usec_t t;
1484 int r;
1486 assert(s);
1487 assert(u);
1489 if (!s->timer_event_source)
1490 return 0;
1492 r = sd_event_source_get_time(s->timer_event_source, &t);
1493 if (r < 0)
1494 return r;
1495 if (t == USEC_INFINITY)
1496 return 0;
1498 *timeout = t;
1499 return 1;
1502 static bool swap_supported(void) {
1503 static int supported = -1;
1505 /* If swap support is not available in the kernel, or we are
1506 * running in a container we don't support swap units, and any
1507 * attempts to starting one should fail immediately. */
1509 if (supported < 0)
1510 supported =
1511 access("/proc/swaps", F_OK) >= 0 &&
1512 detect_container() <= 0;
1514 return supported;
1517 static int swap_control_pid(Unit *u) {
1518 Swap *s = SWAP(u);
1520 assert(s);
1522 return s->control_pid;
1525 static int swap_clean(Unit *u, ExecCleanMask mask) {
1526 _cleanup_strv_free_ char **l = NULL;
1527 Swap *s = SWAP(u);
1528 int r;
1530 assert(s);
1531 assert(mask != 0);
1533 if (s->state != SWAP_DEAD)
1534 return -EBUSY;
1536 r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l);
1537 if (r < 0)
1538 return r;
1540 if (strv_isempty(l))
1541 return -EUNATCH;
1543 swap_unwatch_control_pid(s);
1544 s->clean_result = SWAP_SUCCESS;
1545 s->control_command = NULL;
1546 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1548 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec));
1549 if (r < 0)
1550 goto fail;
1552 r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
1553 if (r < 0)
1554 goto fail;
1556 swap_set_state(s, SWAP_CLEANING);
1558 return 0;
1560 fail:
1561 log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
1562 s->clean_result = SWAP_FAILURE_RESOURCES;
1563 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
1564 return r;
1567 static int swap_can_clean(Unit *u, ExecCleanMask *ret) {
1568 Swap *s = SWAP(u);
1570 assert(s);
1572 return exec_context_get_clean_mask(&s->exec_context, ret);
1575 static int swap_can_start(Unit *u) {
1576 Swap *s = SWAP(u);
1577 int r;
1579 assert(s);
1581 r = unit_test_start_limit(u);
1582 if (r < 0) {
1583 swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT);
1584 return r;
1587 return 1;
1590 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1591 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1592 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1595 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1597 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1598 [SWAP_SUCCESS] = "success",
1599 [SWAP_FAILURE_RESOURCES] = "resources",
1600 [SWAP_FAILURE_TIMEOUT] = "timeout",
1601 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1602 [SWAP_FAILURE_SIGNAL] = "signal",
1603 [SWAP_FAILURE_CORE_DUMP] = "core-dump",
1604 [SWAP_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1607 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1609 const UnitVTable swap_vtable = {
1610 .object_size = sizeof(Swap),
1611 .exec_context_offset = offsetof(Swap, exec_context),
1612 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1613 .kill_context_offset = offsetof(Swap, kill_context),
1614 .exec_runtime_offset = offsetof(Swap, exec_runtime),
1616 .sections =
1617 "Unit\0"
1618 "Swap\0"
1619 "Install\0",
1620 .private_section = "Swap",
1622 .can_fail = true,
1624 .init = swap_init,
1625 .load = swap_load,
1626 .done = swap_done,
1628 .coldplug = swap_coldplug,
1630 .dump = swap_dump,
1632 .start = swap_start,
1633 .stop = swap_stop,
1635 .kill = swap_kill,
1636 .clean = swap_clean,
1637 .can_clean = swap_can_clean,
1639 .get_timeout = swap_get_timeout,
1641 .serialize = swap_serialize,
1642 .deserialize_item = swap_deserialize_item,
1644 .active_state = swap_active_state,
1645 .sub_state_to_string = swap_sub_state_to_string,
1647 .will_restart = unit_will_restart_default,
1649 .may_gc = swap_may_gc,
1650 .is_extrinsic = swap_is_extrinsic,
1652 .sigchld_event = swap_sigchld_event,
1654 .reset_failed = swap_reset_failed,
1656 .control_pid = swap_control_pid,
1658 .bus_set_property = bus_swap_set_property,
1659 .bus_commit_properties = bus_swap_commit_properties,
1661 .following = swap_following,
1662 .following_set = swap_following_set,
1664 .enumerate = swap_enumerate,
1665 .shutdown = swap_shutdown,
1666 .supported = swap_supported,
1668 .status_message_formats = {
1669 .starting_stopping = {
1670 [0] = "Activating swap %s...",
1671 [1] = "Deactivating swap %s...",
1673 .finished_start_job = {
1674 [JOB_DONE] = "Activated swap %s.",
1675 [JOB_FAILED] = "Failed to activate swap %s.",
1676 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1678 .finished_stop_job = {
1679 [JOB_DONE] = "Deactivated swap %s.",
1680 [JOB_FAILED] = "Failed deactivating swap %s.",
1681 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",
1685 .can_start = swap_can_start,
1687 .notify_plymouth = true,