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