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