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