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