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