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