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