]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/swap.c
8a6a32543a905c85d10c1a7fe12e5c233a4f7bf2
[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_require_mounts_for(UNIT(s), s->what, UNIT_DEPENDENCY_IMPLICIT);
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 pid_is_unwaited(s->control_pid.pid) &&
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_clear) ExecParameters exec_params = {
636 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
637 .stdin_fd = -EBADF,
638 .stdout_fd = -EBADF,
639 .stderr_fd = -EBADF,
640 .exec_fd = -EBADF,
641 };
642 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
643 pid_t pid;
644 int r;
645
646 assert(s);
647 assert(c);
648 assert(ret_pid);
649
650 r = unit_prepare_exec(UNIT(s));
651 if (r < 0)
652 return r;
653
654 r = swap_arm_timer(s, /* relative= */ true, s->timeout_usec);
655 if (r < 0)
656 return r;
657
658 r = unit_set_exec_params(UNIT(s), &exec_params);
659 if (r < 0)
660 return r;
661
662 r = exec_spawn(UNIT(s),
663 c,
664 &s->exec_context,
665 &exec_params,
666 s->exec_runtime,
667 &s->cgroup_context,
668 &pid);
669 if (r < 0)
670 return r;
671
672 r = pidref_set_pid(&pidref, pid);
673 if (r < 0)
674 return r;
675
676 r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true);
677 if (r < 0)
678 return r;
679
680 *ret_pid = TAKE_PIDREF(pidref);
681 return 0;
682 }
683
684 static void swap_enter_dead(Swap *s, SwapResult f) {
685 assert(s);
686
687 if (s->result == SWAP_SUCCESS)
688 s->result = f;
689
690 unit_log_result(UNIT(s), s->result == SWAP_SUCCESS, swap_result_to_string(s->result));
691 unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop);
692 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
693
694 s->exec_runtime = exec_runtime_destroy(s->exec_runtime);
695
696 unit_destroy_runtime_data(UNIT(s), &s->exec_context);
697
698 unit_unref_uid_gid(UNIT(s), true);
699 }
700
701 static void swap_enter_active(Swap *s, SwapResult f) {
702 assert(s);
703
704 if (s->result == SWAP_SUCCESS)
705 s->result = f;
706
707 swap_set_state(s, SWAP_ACTIVE);
708 }
709
710 static void swap_enter_dead_or_active(Swap *s, SwapResult f) {
711 assert(s);
712
713 if (s->from_proc_swaps) {
714 swap_enter_active(s, f);
715
716 LIST_FOREACH_OTHERS(same_devnode, other, s)
717 if (UNIT(other)->job)
718 swap_enter_dead_or_active(other, f);
719 } else
720 swap_enter_dead(s, f);
721 }
722
723 static int state_to_kill_operation(Swap *s, SwapState state) {
724 if (state == SWAP_DEACTIVATING_SIGTERM) {
725 if (unit_has_job_type(UNIT(s), JOB_RESTART))
726 return KILL_RESTART;
727 else
728 return KILL_TERMINATE;
729 }
730
731 return KILL_KILL;
732 }
733
734 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
735 int r;
736
737 assert(s);
738
739 if (s->result == SWAP_SUCCESS)
740 s->result = f;
741
742 r = unit_kill_context(
743 UNIT(s),
744 &s->kill_context,
745 state_to_kill_operation(s, state),
746 /* main_pid= */ NULL,
747 &s->control_pid,
748 /* main_pid_alien= */ false);
749 if (r < 0) {
750 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
751 goto fail;
752 }
753
754 if (r > 0) {
755 r = swap_arm_timer(s, /* relative= */ true, s->timeout_usec);
756 if (r < 0) {
757 log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m");
758 goto fail;
759 }
760
761 swap_set_state(s, state);
762 } else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill)
763 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
764 else
765 swap_enter_dead_or_active(s, SWAP_SUCCESS);
766
767 return;
768
769 fail:
770 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
771 }
772
773 static void swap_enter_activating(Swap *s) {
774 _cleanup_free_ char *opts = NULL;
775 int r;
776
777 assert(s);
778
779 unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start);
780
781 s->control_command_id = SWAP_EXEC_ACTIVATE;
782 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
783
784 if (s->from_fragment) {
785 int priority = 0;
786
787 r = fstab_find_pri(s->parameters_fragment.options, &priority);
788 if (r < 0)
789 log_unit_warning_errno(UNIT(s), r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
790 else if (r > 0 && s->parameters_fragment.priority_set)
791 log_unit_warning(UNIT(s), "Duplicate swap priority configuration by Priority= and Options= fields.");
792
793 if (r <= 0 && s->parameters_fragment.priority_set) {
794 if (s->parameters_fragment.options)
795 r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority);
796 else
797 r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority);
798 if (r < 0) {
799 r = log_oom();
800 goto fail;
801 }
802 }
803 }
804
805 r = exec_command_set(s->control_command, "/sbin/swapon", "--fixpgsz", NULL);
806 if (r < 0) {
807 log_unit_warning_errno(UNIT(s), r, "Failed to initialize swapon command line: %m");
808 goto fail;
809 }
810
811 if (s->parameters_fragment.options || opts) {
812 r = exec_command_append(s->control_command, "-o",
813 opts ?: s->parameters_fragment.options, NULL);
814 if (r < 0) {
815 log_unit_warning_errno(UNIT(s), r, "Failed to prepare swapon command line: %m");
816 goto fail;
817 }
818 }
819
820 r = exec_command_append(s->control_command, s->what, NULL);
821 if (r < 0) {
822 log_unit_warning_errno(UNIT(s), r, "Failed to prepare swapon command line: %m");
823 goto fail;
824 }
825
826 swap_unwatch_control_pid(s);
827
828 r = swap_spawn(s, s->control_command, &s->control_pid);
829 if (r < 0) {
830 log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'swapon' task: %m");
831 goto fail;
832 }
833
834 swap_set_state(s, SWAP_ACTIVATING);
835 return;
836
837 fail:
838 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
839 }
840
841 static void swap_enter_deactivating(Swap *s) {
842 int r;
843
844 assert(s);
845
846 s->control_command_id = SWAP_EXEC_DEACTIVATE;
847 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
848
849 r = exec_command_set(s->control_command,
850 "/sbin/swapoff",
851 s->what,
852 NULL);
853 if (r < 0) {
854 log_unit_warning_errno(UNIT(s), r, "Failed to prepare swapoff command line: %m");
855 goto fail;
856 }
857
858 swap_unwatch_control_pid(s);
859
860 r = swap_spawn(s, s->control_command, &s->control_pid);
861 if (r < 0) {
862 log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'swapoff' task: %m");
863 goto fail;
864 }
865
866 swap_set_state(s, SWAP_DEACTIVATING);
867 return;
868
869 fail:
870 swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES);
871 }
872
873 static void swap_cycle_clear(Swap *s) {
874 assert(s);
875
876 s->result = SWAP_SUCCESS;
877 exec_command_reset_status_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
878 UNIT(s)->reset_accounting = true;
879 }
880
881 static int swap_start(Unit *u) {
882 Swap *s = SWAP(u);
883 int r;
884
885 assert(s);
886
887 /* We cannot fulfill this request right now, try again later please! */
888 if (IN_SET(s->state,
889 SWAP_DEACTIVATING,
890 SWAP_DEACTIVATING_SIGTERM,
891 SWAP_DEACTIVATING_SIGKILL,
892 SWAP_CLEANING))
893 return -EAGAIN;
894
895 /* Already on it! */
896 if (s->state == SWAP_ACTIVATING)
897 return 0;
898
899 assert(IN_SET(s->state, SWAP_DEAD, SWAP_FAILED));
900
901 if (detect_container() > 0)
902 return -EPERM;
903
904 /* If there's a job for another swap unit for the same node
905 * running, then let's not dispatch this one for now, and wait
906 * until that other job has finished. */
907 LIST_FOREACH_OTHERS(same_devnode, other, s)
908 if (UNIT(other)->job && UNIT(other)->job->state == JOB_RUNNING)
909 return -EAGAIN;
910
911 r = unit_acquire_invocation_id(u);
912 if (r < 0)
913 return r;
914
915 swap_cycle_clear(s);
916 swap_enter_activating(s);
917 return 1;
918 }
919
920 static int swap_stop(Unit *u) {
921 Swap *s = SWAP(u);
922
923 assert(s);
924
925 switch (s->state) {
926
927 case SWAP_DEACTIVATING:
928 case SWAP_DEACTIVATING_SIGTERM:
929 case SWAP_DEACTIVATING_SIGKILL:
930 /* Already on it */
931 return 0;
932
933 case SWAP_ACTIVATING:
934 case SWAP_ACTIVATING_DONE:
935 /* There's a control process pending, directly enter kill mode */
936 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS);
937 return 0;
938
939 case SWAP_ACTIVE:
940 if (detect_container() > 0)
941 return -EPERM;
942
943 swap_enter_deactivating(s);
944 return 1;
945
946 case SWAP_CLEANING:
947 /* If we are currently cleaning, then abort it, brutally. */
948 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
949 return 0;
950
951 default:
952 assert_not_reached();
953 }
954 }
955
956 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
957 Swap *s = SWAP(u);
958
959 assert(s);
960 assert(f);
961 assert(fds);
962
963 (void) serialize_item(f, "state", swap_state_to_string(s->state));
964 (void) serialize_item(f, "result", swap_result_to_string(s->result));
965
966 if (pidref_is_set(&s->control_pid))
967 (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid.pid);
968
969 if (s->control_command_id >= 0)
970 (void) serialize_item(f, "control-command", swap_exec_command_to_string(s->control_command_id));
971
972 return 0;
973 }
974
975 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
976 Swap *s = SWAP(u);
977 int r;
978
979 assert(s);
980 assert(fds);
981
982 if (streq(key, "state")) {
983 SwapState state;
984
985 state = swap_state_from_string(value);
986 if (state < 0)
987 log_unit_debug(u, "Failed to parse state value: %s", value);
988 else
989 s->deserialized_state = state;
990 } else if (streq(key, "result")) {
991 SwapResult f;
992
993 f = swap_result_from_string(value);
994 if (f < 0)
995 log_unit_debug(u, "Failed to parse result value: %s", value);
996 else if (f != SWAP_SUCCESS)
997 s->result = f;
998 } else if (streq(key, "control-pid")) {
999
1000 pidref_done(&s->control_pid);
1001 r = pidref_set_pidstr(&s->control_pid, value);
1002 if (r < 0)
1003 log_debug_errno(r, "Failed to pin control PID '%s', ignoring: %m", value);
1004
1005 } else if (streq(key, "control-command")) {
1006 SwapExecCommand id;
1007
1008 id = swap_exec_command_from_string(value);
1009 if (id < 0)
1010 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
1011 else {
1012 s->control_command_id = id;
1013 s->control_command = s->exec_command + id;
1014 }
1015 } else
1016 log_unit_debug(u, "Unknown serialization key: %s", key);
1017
1018 return 0;
1019 }
1020
1021 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1022 Swap *s = SWAP(u);
1023 SwapResult f;
1024
1025 assert(s);
1026 assert(pid >= 0);
1027
1028 if (pid != s->control_pid.pid)
1029 return;
1030
1031 /* Let's scan /proc/swaps before we process SIGCHLD. For the reasoning see the similar code in
1032 * mount.c */
1033 (void) swap_process_proc_swaps(u->manager);
1034
1035 pidref_done(&s->control_pid);
1036
1037 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
1038 f = SWAP_SUCCESS;
1039 else if (code == CLD_EXITED)
1040 f = SWAP_FAILURE_EXIT_CODE;
1041 else if (code == CLD_KILLED)
1042 f = SWAP_FAILURE_SIGNAL;
1043 else if (code == CLD_DUMPED)
1044 f = SWAP_FAILURE_CORE_DUMP;
1045 else
1046 assert_not_reached();
1047
1048 if (s->result == SWAP_SUCCESS)
1049 s->result = f;
1050
1051 if (s->control_command) {
1052 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
1053
1054 s->control_command = NULL;
1055 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1056 }
1057
1058 unit_log_process_exit(
1059 u,
1060 "Swap process",
1061 swap_exec_command_to_string(s->control_command_id),
1062 f == SWAP_SUCCESS,
1063 code, status);
1064
1065 switch (s->state) {
1066
1067 case SWAP_ACTIVATING:
1068 case SWAP_ACTIVATING_DONE:
1069
1070 if (f == SWAP_SUCCESS || s->from_proc_swaps)
1071 swap_enter_active(s, f);
1072 else
1073 swap_enter_dead(s, f);
1074 break;
1075
1076 case SWAP_DEACTIVATING:
1077 case SWAP_DEACTIVATING_SIGKILL:
1078 case SWAP_DEACTIVATING_SIGTERM:
1079
1080 swap_enter_dead_or_active(s, f);
1081 break;
1082
1083 case SWAP_CLEANING:
1084 if (s->clean_result == SWAP_SUCCESS)
1085 s->clean_result = f;
1086
1087 swap_enter_dead(s, SWAP_SUCCESS);
1088 break;
1089
1090 default:
1091 assert_not_reached();
1092 }
1093
1094 /* Notify clients about changed exit status */
1095 unit_add_to_dbus_queue(u);
1096 }
1097
1098 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1099 Swap *s = SWAP(userdata);
1100
1101 assert(s);
1102 assert(s->timer_event_source == source);
1103
1104 switch (s->state) {
1105
1106 case SWAP_ACTIVATING:
1107 case SWAP_ACTIVATING_DONE:
1108 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
1109 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1110 break;
1111
1112 case SWAP_DEACTIVATING:
1113 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
1114 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1115 break;
1116
1117 case SWAP_DEACTIVATING_SIGTERM:
1118 if (s->kill_context.send_sigkill) {
1119 log_unit_warning(UNIT(s), "Swap process timed out. Killing.");
1120 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1121 } else {
1122 log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring.");
1123 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
1124 }
1125 break;
1126
1127 case SWAP_DEACTIVATING_SIGKILL:
1128 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
1129 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
1130 break;
1131
1132 case SWAP_CLEANING:
1133 log_unit_warning(UNIT(s), "Cleaning timed out. killing.");
1134
1135 if (s->clean_result == SWAP_SUCCESS)
1136 s->clean_result = SWAP_FAILURE_TIMEOUT;
1137
1138 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, 0);
1139 break;
1140
1141 default:
1142 assert_not_reached();
1143 }
1144
1145 return 0;
1146 }
1147
1148 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1149 assert(m);
1150
1151 rewind(m->proc_swaps);
1152
1153 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1154
1155 for (unsigned i = 1;; i++) {
1156 _cleanup_free_ char *dev = NULL, *d = NULL;
1157 int prio = 0, k;
1158
1159 k = fscanf(m->proc_swaps,
1160 "%ms " /* device/file */
1161 "%*s " /* type of swap */
1162 "%*s " /* swap size */
1163 "%*s " /* used */
1164 "%i\n", /* priority */
1165 &dev, &prio);
1166 if (k != 2) {
1167 if (k == EOF)
1168 break;
1169
1170 log_warning("Failed to parse /proc/swaps:%u, skipping.", i);
1171 continue;
1172 }
1173
1174 ssize_t l = cunescape(dev, UNESCAPE_RELAX, &d);
1175 if (l < 0)
1176 return log_error_errno(l, "Failed to unescape device path: %m");
1177
1178 device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
1179
1180 (void) swap_process_new(m, d, prio, set_flags);
1181 }
1182
1183 return 0;
1184 }
1185
1186 static int swap_process_proc_swaps(Manager *m) {
1187 int r;
1188
1189 assert(m);
1190
1191 r = swap_load_proc_swaps(m, true);
1192 if (r < 0) {
1193 /* Reset flags, just in case, for late calls */
1194 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1195 Swap *swap = SWAP(u);
1196
1197 assert(swap);
1198
1199 swap->is_active = swap->just_activated = false;
1200 }
1201
1202 return 0;
1203 }
1204
1205 manager_dispatch_load_queue(m);
1206
1207 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1208 Swap *swap = SWAP(u);
1209
1210 assert(swap);
1211
1212 if (!swap->is_active) {
1213
1214 swap_unset_proc_swaps(swap);
1215
1216 switch (swap->state) {
1217
1218 case SWAP_ACTIVE:
1219 /* This has just been deactivated */
1220 swap_enter_dead(swap, SWAP_SUCCESS);
1221 break;
1222
1223 default:
1224 /* Fire again */
1225 swap_set_state(swap, swap->state);
1226 break;
1227 }
1228
1229 if (swap->what)
1230 device_found_node(m, swap->what, DEVICE_NOT_FOUND, DEVICE_FOUND_SWAP);
1231
1232 } else if (swap->just_activated) {
1233
1234 /* New swap entry */
1235
1236 switch (swap->state) {
1237
1238 case SWAP_DEAD:
1239 case SWAP_FAILED:
1240 (void) unit_acquire_invocation_id(u);
1241 swap_cycle_clear(swap);
1242 swap_enter_active(swap, SWAP_SUCCESS);
1243 break;
1244
1245 case SWAP_ACTIVATING:
1246 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1247 break;
1248
1249 default:
1250 /* Nothing really changed, but let's
1251 * issue an notification call
1252 * nonetheless, in case somebody is
1253 * waiting for this. */
1254 swap_set_state(swap, swap->state);
1255 break;
1256 }
1257 }
1258
1259 /* Reset the flags for later calls */
1260 swap->is_active = swap->just_activated = false;
1261 }
1262
1263 return 1;
1264 }
1265
1266 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1267 Manager *m = ASSERT_PTR(userdata);
1268
1269 assert(revents & EPOLLPRI);
1270
1271 return swap_process_proc_swaps(m);
1272 }
1273
1274 static Unit *swap_following(Unit *u) {
1275 Swap *s = SWAP(u);
1276 Swap *first = NULL;
1277
1278 assert(s);
1279
1280 /* If the user configured the swap through /etc/fstab or
1281 * a device unit, follow that. */
1282
1283 if (s->from_fragment)
1284 return NULL;
1285
1286 LIST_FOREACH_OTHERS(same_devnode, other, s)
1287 if (other->from_fragment)
1288 return UNIT(other);
1289
1290 /* Otherwise, make everybody follow the unit that's named after
1291 * the swap device in the kernel */
1292
1293 if (streq_ptr(s->what, s->devnode))
1294 return NULL;
1295
1296 LIST_FOREACH(same_devnode, other, s->same_devnode_next)
1297 if (streq_ptr(other->what, other->devnode))
1298 return UNIT(other);
1299
1300 LIST_FOREACH_BACKWARDS(same_devnode, other, s->same_devnode_prev) {
1301 if (streq_ptr(other->what, other->devnode))
1302 return UNIT(other);
1303
1304 first = other;
1305 }
1306
1307 /* Fall back to the first on the list */
1308 return UNIT(first);
1309 }
1310
1311 static int swap_following_set(Unit *u, Set **_set) {
1312 Swap *s = SWAP(u);
1313 _cleanup_set_free_ Set *set = NULL;
1314 int r;
1315
1316 assert(s);
1317 assert(_set);
1318
1319 if (LIST_JUST_US(same_devnode, s)) {
1320 *_set = NULL;
1321 return 0;
1322 }
1323
1324 set = set_new(NULL);
1325 if (!set)
1326 return -ENOMEM;
1327
1328 LIST_FOREACH_OTHERS(same_devnode, other, s) {
1329 r = set_put(set, other);
1330 if (r < 0)
1331 return r;
1332 }
1333
1334 *_set = TAKE_PTR(set);
1335 return 1;
1336 }
1337
1338 static void swap_shutdown(Manager *m) {
1339 assert(m);
1340
1341 m->swap_event_source = sd_event_source_disable_unref(m->swap_event_source);
1342 m->proc_swaps = safe_fclose(m->proc_swaps);
1343 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
1344 }
1345
1346 static void swap_enumerate(Manager *m) {
1347 int r;
1348
1349 assert(m);
1350
1351 if (!m->proc_swaps) {
1352 m->proc_swaps = fopen("/proc/swaps", "re");
1353 if (!m->proc_swaps) {
1354 if (errno == ENOENT)
1355 log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
1356 else
1357 log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m");
1358
1359 return;
1360 }
1361
1362 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
1363 if (r < 0) {
1364 log_error_errno(r, "Failed to watch /proc/swaps: %m");
1365 goto fail;
1366 }
1367
1368 /* Dispatch this before we dispatch SIGCHLD, so that
1369 * we always get the events from /proc/swaps before
1370 * the SIGCHLD of /sbin/swapon. */
1371 r = sd_event_source_set_priority(m->swap_event_source, SD_EVENT_PRIORITY_NORMAL-10);
1372 if (r < 0) {
1373 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
1374 goto fail;
1375 }
1376
1377 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
1378 }
1379
1380 r = swap_load_proc_swaps(m, false);
1381 if (r < 0)
1382 goto fail;
1383
1384 return;
1385
1386 fail:
1387 swap_shutdown(m);
1388 }
1389
1390 int swap_process_device_new(Manager *m, sd_device *dev) {
1391 _cleanup_free_ char *e = NULL;
1392 const char *dn;
1393 Unit *u;
1394 int r;
1395
1396 assert(m);
1397 assert(dev);
1398
1399 if (sd_device_get_devname(dev, &dn) < 0)
1400 return 0;
1401
1402 r = unit_name_from_path(dn, ".swap", &e);
1403 if (r < 0) {
1404 log_debug_errno(r, "Cannot convert device name '%s' to unit name, ignoring: %m", dn);
1405 return 0;
1406 }
1407
1408 u = manager_get_unit(m, e);
1409 if (u)
1410 r = swap_set_devnode(SWAP(u), dn);
1411
1412 FOREACH_DEVICE_DEVLINK(dev, devlink) {
1413 _cleanup_free_ char *n = NULL;
1414 int q;
1415
1416 q = unit_name_from_path(devlink, ".swap", &n);
1417 if (q == -EINVAL) /* If the name is not convertible to unit name, we can't manage it */
1418 continue;
1419 if (q < 0)
1420 return q;
1421
1422 u = manager_get_unit(m, n);
1423 if (u) {
1424 q = swap_set_devnode(SWAP(u), dn);
1425 if (q < 0)
1426 r = q;
1427 }
1428 }
1429
1430 return r;
1431 }
1432
1433 int swap_process_device_remove(Manager *m, sd_device *dev) {
1434 const char *dn;
1435 int r;
1436 Swap *s;
1437
1438 r = sd_device_get_devname(dev, &dn);
1439 if (r < 0)
1440 return 0;
1441
1442 while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1443 int q;
1444
1445 q = swap_set_devnode(s, NULL);
1446 if (q < 0)
1447 r = q;
1448 }
1449
1450 return r;
1451 }
1452
1453 static void swap_reset_failed(Unit *u) {
1454 Swap *s = SWAP(u);
1455
1456 assert(s);
1457
1458 if (s->state == SWAP_FAILED)
1459 swap_set_state(s, SWAP_DEAD);
1460
1461 s->result = SWAP_SUCCESS;
1462 s->clean_result = SWAP_SUCCESS;
1463 }
1464
1465 static int swap_get_timeout(Unit *u, usec_t *timeout) {
1466 Swap *s = SWAP(u);
1467 usec_t t;
1468 int r;
1469
1470 assert(s);
1471 assert(u);
1472
1473 if (!s->timer_event_source)
1474 return 0;
1475
1476 r = sd_event_source_get_time(s->timer_event_source, &t);
1477 if (r < 0)
1478 return r;
1479 if (t == USEC_INFINITY)
1480 return 0;
1481
1482 *timeout = t;
1483 return 1;
1484 }
1485
1486 static bool swap_supported(void) {
1487 static int supported = -1;
1488
1489 /* If swap support is not available in the kernel, or we are
1490 * running in a container we don't support swap units, and any
1491 * attempts to starting one should fail immediately. */
1492
1493 if (supported < 0)
1494 supported =
1495 access("/proc/swaps", F_OK) >= 0 &&
1496 detect_container() <= 0;
1497
1498 return supported;
1499 }
1500
1501 static PidRef* swap_control_pid(Unit *u) {
1502 return &ASSERT_PTR(SWAP(u))->control_pid;
1503 }
1504
1505 static int swap_clean(Unit *u, ExecCleanMask mask) {
1506 _cleanup_strv_free_ char **l = NULL;
1507 Swap *s = SWAP(u);
1508 int r;
1509
1510 assert(s);
1511 assert(mask != 0);
1512
1513 if (s->state != SWAP_DEAD)
1514 return -EBUSY;
1515
1516 r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l);
1517 if (r < 0)
1518 return r;
1519
1520 if (strv_isempty(l))
1521 return -EUNATCH;
1522
1523 swap_unwatch_control_pid(s);
1524 s->clean_result = SWAP_SUCCESS;
1525 s->control_command = NULL;
1526 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1527
1528 r = swap_arm_timer(s, /* relative= */ true, s->exec_context.timeout_clean_usec);
1529 if (r < 0) {
1530 log_unit_warning_errno(u, r, "Failed to install timer: %m");
1531 goto fail;
1532 }
1533
1534 r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
1535 if (r < 0) {
1536 log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m");
1537 goto fail;
1538 }
1539
1540 swap_set_state(s, SWAP_CLEANING);
1541 return 0;
1542
1543 fail:
1544 s->clean_result = SWAP_FAILURE_RESOURCES;
1545 s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
1546 return r;
1547 }
1548
1549 static int swap_can_clean(Unit *u, ExecCleanMask *ret) {
1550 Swap *s = SWAP(u);
1551
1552 assert(s);
1553
1554 return exec_context_get_clean_mask(&s->exec_context, ret);
1555 }
1556
1557 static int swap_can_start(Unit *u) {
1558 Swap *s = SWAP(u);
1559 int r;
1560
1561 assert(s);
1562
1563 r = unit_test_start_limit(u);
1564 if (r < 0) {
1565 swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT);
1566 return r;
1567 }
1568
1569 return 1;
1570 }
1571
1572 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1573 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1574 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1575 };
1576
1577 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1578
1579 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1580 [SWAP_SUCCESS] = "success",
1581 [SWAP_FAILURE_RESOURCES] = "resources",
1582 [SWAP_FAILURE_TIMEOUT] = "timeout",
1583 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1584 [SWAP_FAILURE_SIGNAL] = "signal",
1585 [SWAP_FAILURE_CORE_DUMP] = "core-dump",
1586 [SWAP_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1587 };
1588
1589 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1590
1591 const UnitVTable swap_vtable = {
1592 .object_size = sizeof(Swap),
1593 .exec_context_offset = offsetof(Swap, exec_context),
1594 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1595 .kill_context_offset = offsetof(Swap, kill_context),
1596 .exec_runtime_offset = offsetof(Swap, exec_runtime),
1597
1598 .sections =
1599 "Unit\0"
1600 "Swap\0"
1601 "Install\0",
1602 .private_section = "Swap",
1603
1604 .can_fail = true,
1605
1606 .init = swap_init,
1607 .load = swap_load,
1608 .done = swap_done,
1609
1610 .coldplug = swap_coldplug,
1611
1612 .dump = swap_dump,
1613
1614 .start = swap_start,
1615 .stop = swap_stop,
1616
1617 .clean = swap_clean,
1618 .can_clean = swap_can_clean,
1619
1620 .get_timeout = swap_get_timeout,
1621
1622 .serialize = swap_serialize,
1623 .deserialize_item = swap_deserialize_item,
1624
1625 .active_state = swap_active_state,
1626 .sub_state_to_string = swap_sub_state_to_string,
1627
1628 .will_restart = unit_will_restart_default,
1629
1630 .may_gc = swap_may_gc,
1631 .is_extrinsic = swap_is_extrinsic,
1632
1633 .sigchld_event = swap_sigchld_event,
1634
1635 .reset_failed = swap_reset_failed,
1636
1637 .control_pid = swap_control_pid,
1638
1639 .bus_set_property = bus_swap_set_property,
1640 .bus_commit_properties = bus_swap_commit_properties,
1641
1642 .following = swap_following,
1643 .following_set = swap_following_set,
1644
1645 .enumerate = swap_enumerate,
1646 .shutdown = swap_shutdown,
1647 .supported = swap_supported,
1648
1649 .status_message_formats = {
1650 .starting_stopping = {
1651 [0] = "Activating swap %s...",
1652 [1] = "Deactivating swap %s...",
1653 },
1654 .finished_start_job = {
1655 [JOB_DONE] = "Activated swap %s.",
1656 [JOB_FAILED] = "Failed to activate swap %s.",
1657 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1658 },
1659 .finished_stop_job = {
1660 [JOB_DONE] = "Deactivated swap %s.",
1661 [JOB_FAILED] = "Failed deactivating swap %s.",
1662 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",
1663 },
1664 },
1665
1666 .can_start = swap_can_start,
1667
1668 .notify_plymouth = true,
1669 };