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