]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/swap.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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
47 static bool SWAP_STATE_WITH_PROCESS(SwapState state) {
48 return IN_SET(state,
49 SWAP_ACTIVATING,
50 SWAP_ACTIVATING_DONE,
51 SWAP_DEACTIVATING,
52 SWAP_DEACTIVATING_SIGTERM,
53 SWAP_DEACTIVATING_SIGKILL);
54 }
55
56 static void swap_unset_proc_swaps(Swap *s) {
57 assert(s);
58
59 if (!s->from_proc_swaps)
60 return;
61
62 s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what);
63
64 s->from_proc_swaps = false;
65 }
66
67 static int swap_set_devnode(Swap *s, const char *devnode) {
68 Hashmap *swaps;
69 Swap *first;
70 int r;
71
72 assert(s);
73
74 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &path_hash_ops);
75 if (r < 0)
76 return r;
77
78 swaps = UNIT(s)->manager->swaps_by_devnode;
79
80 if (s->devnode) {
81 first = hashmap_get(swaps, s->devnode);
82
83 LIST_REMOVE(same_devnode, first, s);
84 if (first)
85 hashmap_replace(swaps, first->devnode, first);
86 else
87 hashmap_remove(swaps, s->devnode);
88
89 s->devnode = mfree(s->devnode);
90 }
91
92 if (devnode) {
93 s->devnode = strdup(devnode);
94 if (!s->devnode)
95 return -ENOMEM;
96
97 first = hashmap_get(swaps, s->devnode);
98 LIST_PREPEND(same_devnode, first, s);
99
100 return hashmap_replace(swaps, first->devnode, first);
101 }
102
103 return 0;
104 }
105
106 static void swap_init(Unit *u) {
107 Swap *s = SWAP(u);
108
109 assert(s);
110 assert(UNIT(s)->load_state == UNIT_STUB);
111
112 s->timeout_usec = u->manager->default_timeout_start_usec;
113
114 s->exec_context.std_output = u->manager->default_std_output;
115 s->exec_context.std_error = u->manager->default_std_error;
116
117 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
118
119 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
120
121 u->ignore_on_isolate = true;
122 }
123
124 static void swap_unwatch_control_pid(Swap *s) {
125 assert(s);
126
127 if (s->control_pid <= 0)
128 return;
129
130 unit_unwatch_pid(UNIT(s), s->control_pid);
131 s->control_pid = 0;
132 }
133
134 static void swap_done(Unit *u) {
135 Swap *s = SWAP(u);
136
137 assert(s);
138
139 swap_unset_proc_swaps(s);
140 swap_set_devnode(s, NULL);
141
142 s->what = mfree(s->what);
143 s->parameters_fragment.what = mfree(s->parameters_fragment.what);
144 s->parameters_fragment.options = mfree(s->parameters_fragment.options);
145
146 s->exec_runtime = exec_runtime_unref(s->exec_runtime, false);
147 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
148 s->control_command = NULL;
149
150 dynamic_creds_unref(&s->dynamic_creds);
151
152 swap_unwatch_control_pid(s);
153
154 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
155 }
156
157 static int swap_arm_timer(Swap *s, usec_t usec) {
158 int r;
159
160 assert(s);
161
162 if (s->timer_event_source) {
163 r = sd_event_source_set_time(s->timer_event_source, usec);
164 if (r < 0)
165 return r;
166
167 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
168 }
169
170 if (usec == USEC_INFINITY)
171 return 0;
172
173 r = sd_event_add_time(
174 UNIT(s)->manager->event,
175 &s->timer_event_source,
176 CLOCK_MONOTONIC,
177 usec, 0,
178 swap_dispatch_timer, s);
179 if (r < 0)
180 return r;
181
182 (void) sd_event_source_set_description(s->timer_event_source, "swap-timer");
183
184 return 0;
185 }
186
187 static int swap_add_device_dependencies(Swap *s) {
188 assert(s);
189
190 if (!s->what)
191 return 0;
192
193 if (!s->from_fragment)
194 return 0;
195
196 if (is_device_path(s->what))
197 return unit_add_node_dependency(UNIT(s), s->what, MANAGER_IS_SYSTEM(UNIT(s)->manager), UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE);
198 else
199 /* File based swap devices need to be ordered after
200 * systemd-remount-fs.service, since they might need a
201 * writable file system. */
202 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, true, UNIT_DEPENDENCY_FILE);
203 }
204
205 static int swap_add_default_dependencies(Swap *s) {
206 int r;
207
208 assert(s);
209
210 if (!UNIT(s)->default_dependencies)
211 return 0;
212
213 if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
214 return 0;
215
216 if (detect_container() > 0)
217 return 0;
218
219 /* swap units generated for the swap dev links are missing the
220 * ordering dep against the swap target. */
221 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SWAP_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
222 if (r < 0)
223 return r;
224
225 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
226 }
227
228 static int swap_verify(Swap *s) {
229 _cleanup_free_ char *e = NULL;
230 int r;
231
232 if (UNIT(s)->load_state != UNIT_LOADED)
233 return 0;
234
235 r = unit_name_from_path(s->what, ".swap", &e);
236 if (r < 0)
237 return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m");
238
239 if (!unit_has_name(UNIT(s), e)) {
240 log_unit_error(UNIT(s), "Value of What= and unit name do not match, not loading.");
241 return -ENOEXEC;
242 }
243
244 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
245 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.");
246 return -ENOEXEC;
247 }
248
249 return 0;
250 }
251
252 static int swap_load_devnode(Swap *s) {
253 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
254 struct stat st;
255 const char *p;
256 int r;
257
258 assert(s);
259
260 if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
261 return 0;
262
263 r = device_new_from_stat_rdev(&d, &st);
264 if (r < 0) {
265 log_unit_full(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
266 "Failed to allocate device for swap %s: %m", s->what);
267 return 0;
268 }
269
270 if (sd_device_get_devname(d, &p) < 0)
271 return 0;
272
273 return swap_set_devnode(s, p);
274 }
275
276 static int swap_add_extras(Swap *s) {
277 int r;
278
279 assert(s);
280
281 if (UNIT(s)->fragment_path)
282 s->from_fragment = true;
283
284 if (!s->what) {
285 if (s->parameters_fragment.what)
286 s->what = strdup(s->parameters_fragment.what);
287 else if (s->parameters_proc_swaps.what)
288 s->what = strdup(s->parameters_proc_swaps.what);
289 else {
290 r = unit_name_to_path(UNIT(s)->id, &s->what);
291 if (r < 0)
292 return r;
293 }
294
295 if (!s->what)
296 return -ENOMEM;
297 }
298
299 path_simplify(s->what, false);
300
301 if (!UNIT(s)->description) {
302 r = unit_set_description(UNIT(s), s->what);
303 if (r < 0)
304 return r;
305 }
306
307 r = unit_require_mounts_for(UNIT(s), s->what, UNIT_DEPENDENCY_IMPLICIT);
308 if (r < 0)
309 return r;
310
311 r = swap_add_device_dependencies(s);
312 if (r < 0)
313 return r;
314
315 r = swap_load_devnode(s);
316 if (r < 0)
317 return r;
318
319 r = unit_patch_contexts(UNIT(s));
320 if (r < 0)
321 return r;
322
323 r = unit_add_exec_dependencies(UNIT(s), &s->exec_context);
324 if (r < 0)
325 return r;
326
327 r = unit_set_default_slice(UNIT(s));
328 if (r < 0)
329 return r;
330
331 r = swap_add_default_dependencies(s);
332 if (r < 0)
333 return r;
334
335 return 0;
336 }
337
338 static int swap_load(Unit *u) {
339 Swap *s = SWAP(u);
340 int r, q;
341
342 assert(s);
343 assert(u->load_state == UNIT_STUB);
344
345 /* Load a .swap file */
346 if (SWAP(u)->from_proc_swaps)
347 r = unit_load_fragment_and_dropin_optional(u);
348 else
349 r = unit_load_fragment_and_dropin(u);
350
351 /* Add in some extras, and do so either when we successfully loaded something or when /proc/swaps is already
352 * active. */
353 if (u->load_state == UNIT_LOADED || s->from_proc_swaps)
354 q = swap_add_extras(s);
355 else
356 q = 0;
357
358 if (r < 0)
359 return r;
360 if (q < 0)
361 return q;
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
438 unit_add_to_dbus_queue(u);
439 return 0;
440
441 fail:
442 log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
443
444 if (delete)
445 unit_free(u);
446
447 return r;
448 }
449
450 static int swap_process_new(Manager *m, const char *device, int prio, bool set_flags) {
451 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
452 const char *dn, *devlink;
453 struct stat st, st_link;
454 int r;
455
456 assert(m);
457
458 r = swap_setup_unit(m, device, device, prio, set_flags);
459 if (r < 0)
460 return r;
461
462 /* If this is a block device, then let's add duplicates for
463 * all other names of this block device */
464 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
465 return 0;
466
467 r = device_new_from_stat_rdev(&d, &st);
468 if (r < 0) {
469 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
470 "Failed to allocate device for swap %s: %m", device);
471 return 0;
472 }
473
474 /* Add the main device node */
475 if (sd_device_get_devname(d, &dn) >= 0 && !streq(dn, device))
476 swap_setup_unit(m, dn, device, prio, set_flags);
477
478 /* Add additional units for all symlinks */
479 FOREACH_DEVICE_DEVLINK(d, devlink) {
480
481 /* Don't bother with the /dev/block links */
482 if (streq(devlink, device))
483 continue;
484
485 if (path_startswith(devlink, "/dev/block/"))
486 continue;
487
488 if (stat(devlink, &st_link) >= 0 &&
489 (!S_ISBLK(st_link.st_mode) ||
490 st_link.st_rdev != st.st_rdev))
491 continue;
492
493 swap_setup_unit(m, devlink, device, prio, set_flags);
494 }
495
496 return 0;
497 }
498
499 static void swap_set_state(Swap *s, SwapState state) {
500 SwapState old_state;
501 Swap *other;
502
503 assert(s);
504
505 if (s->state != state)
506 bus_unit_send_pending_change_signal(UNIT(s), false);
507
508 old_state = s->state;
509 s->state = state;
510
511 if (!SWAP_STATE_WITH_PROCESS(state)) {
512 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
513 swap_unwatch_control_pid(s);
514 s->control_command = NULL;
515 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
516 }
517
518 if (state != old_state)
519 log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state));
520
521 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
522
523 /* If there other units for the same device node have a job
524 queued it might be worth checking again if it is runnable
525 now. This is necessary, since swap_start() refuses
526 operation with EAGAIN if there's already another job for
527 the same device node queued. */
528 LIST_FOREACH_OTHERS(same_devnode, other, s)
529 if (UNIT(other)->job)
530 job_add_to_run_queue(UNIT(other)->job);
531 }
532
533 static int swap_coldplug(Unit *u) {
534 Swap *s = SWAP(u);
535 SwapState new_state = SWAP_DEAD;
536 int r;
537
538 assert(s);
539 assert(s->state == SWAP_DEAD);
540
541 if (s->deserialized_state != s->state)
542 new_state = s->deserialized_state;
543 else if (s->from_proc_swaps)
544 new_state = SWAP_ACTIVE;
545
546 if (new_state == s->state)
547 return 0;
548
549 if (s->control_pid > 0 &&
550 pid_is_unwaited(s->control_pid) &&
551 SWAP_STATE_WITH_PROCESS(new_state)) {
552
553 r = unit_watch_pid(UNIT(s), s->control_pid);
554 if (r < 0)
555 return r;
556
557 r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
558 if (r < 0)
559 return r;
560 }
561
562 if (!IN_SET(new_state, SWAP_DEAD, SWAP_FAILED)) {
563 (void) unit_setup_dynamic_creds(u);
564 (void) unit_setup_exec_runtime(u);
565 }
566
567 swap_set_state(s, new_state);
568 return 0;
569 }
570
571 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
572 char buf[FORMAT_TIMESPAN_MAX];
573 Swap *s = SWAP(u);
574 SwapParameters *p;
575
576 assert(s);
577 assert(f);
578
579 if (s->from_proc_swaps)
580 p = &s->parameters_proc_swaps;
581 else if (s->from_fragment)
582 p = &s->parameters_fragment;
583 else
584 p = NULL;
585
586 fprintf(f,
587 "%sSwap State: %s\n"
588 "%sResult: %s\n"
589 "%sWhat: %s\n"
590 "%sFrom /proc/swaps: %s\n"
591 "%sFrom fragment: %s\n",
592 prefix, swap_state_to_string(s->state),
593 prefix, swap_result_to_string(s->result),
594 prefix, s->what,
595 prefix, yes_no(s->from_proc_swaps),
596 prefix, yes_no(s->from_fragment));
597
598 if (s->devnode)
599 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
600
601 if (p)
602 fprintf(f,
603 "%sPriority: %i\n"
604 "%sOptions: %s\n",
605 prefix, p->priority,
606 prefix, strempty(p->options));
607
608 fprintf(f,
609 "%sTimeoutSec: %s\n",
610 prefix, format_timespan(buf, sizeof(buf), s->timeout_usec, USEC_PER_SEC));
611
612 if (s->control_pid > 0)
613 fprintf(f,
614 "%sControl PID: "PID_FMT"\n",
615 prefix, s->control_pid);
616
617 exec_context_dump(&s->exec_context, f, prefix);
618 kill_context_dump(&s->kill_context, f, prefix);
619 cgroup_context_dump(&s->cgroup_context, f, prefix);
620 }
621
622 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
623
624 _cleanup_(exec_params_clear) ExecParameters exec_params = {
625 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
626 .stdin_fd = -1,
627 .stdout_fd = -1,
628 .stderr_fd = -1,
629 .exec_fd = -1,
630 };
631 pid_t pid;
632 int r;
633
634 assert(s);
635 assert(c);
636 assert(_pid);
637
638 r = unit_prepare_exec(UNIT(s));
639 if (r < 0)
640 return r;
641
642 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
643 if (r < 0)
644 goto fail;
645
646 r = unit_set_exec_params(UNIT(s), &exec_params);
647 if (r < 0)
648 goto fail;
649
650 r = exec_spawn(UNIT(s),
651 c,
652 &s->exec_context,
653 &exec_params,
654 s->exec_runtime,
655 &s->dynamic_creds,
656 &pid);
657 if (r < 0)
658 goto fail;
659
660 r = unit_watch_pid(UNIT(s), pid);
661 if (r < 0)
662 /* FIXME: we need to do something here */
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_start_limit_test(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 s->control_pid = 0;
1019
1020 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
1021 f = SWAP_SUCCESS;
1022 else if (code == CLD_EXITED)
1023 f = SWAP_FAILURE_EXIT_CODE;
1024 else if (code == CLD_KILLED)
1025 f = SWAP_FAILURE_SIGNAL;
1026 else if (code == CLD_DUMPED)
1027 f = SWAP_FAILURE_CORE_DUMP;
1028 else
1029 assert_not_reached("Unknown code");
1030
1031 if (s->result == SWAP_SUCCESS)
1032 s->result = f;
1033
1034 if (s->control_command) {
1035 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
1036
1037 s->control_command = NULL;
1038 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1039 }
1040
1041 unit_log_process_exit(
1042 u, f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1043 "Swap process",
1044 swap_exec_command_to_string(s->control_command_id),
1045 code, status);
1046
1047 switch (s->state) {
1048
1049 case SWAP_ACTIVATING:
1050 case SWAP_ACTIVATING_DONE:
1051
1052 if (f == SWAP_SUCCESS || s->from_proc_swaps)
1053 swap_enter_active(s, f);
1054 else
1055 swap_enter_dead(s, f);
1056 break;
1057
1058 case SWAP_DEACTIVATING:
1059 case SWAP_DEACTIVATING_SIGKILL:
1060 case SWAP_DEACTIVATING_SIGTERM:
1061
1062 swap_enter_dead_or_active(s, f);
1063 break;
1064
1065 default:
1066 assert_not_reached("Uh, control process died at wrong time.");
1067 }
1068
1069 /* Notify clients about changed exit status */
1070 unit_add_to_dbus_queue(u);
1071 }
1072
1073 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1074 Swap *s = SWAP(userdata);
1075
1076 assert(s);
1077 assert(s->timer_event_source == source);
1078
1079 switch (s->state) {
1080
1081 case SWAP_ACTIVATING:
1082 case SWAP_ACTIVATING_DONE:
1083 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
1084 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1085 break;
1086
1087 case SWAP_DEACTIVATING:
1088 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
1089 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1090 break;
1091
1092 case SWAP_DEACTIVATING_SIGTERM:
1093 if (s->kill_context.send_sigkill) {
1094 log_unit_warning(UNIT(s), "Swap process timed out. Killing.");
1095 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1096 } else {
1097 log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring.");
1098 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
1099 }
1100 break;
1101
1102 case SWAP_DEACTIVATING_SIGKILL:
1103 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
1104 swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT);
1105 break;
1106
1107 default:
1108 assert_not_reached("Timeout at wrong time.");
1109 }
1110
1111 return 0;
1112 }
1113
1114 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1115 unsigned i;
1116
1117 assert(m);
1118
1119 rewind(m->proc_swaps);
1120
1121 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1122
1123 for (i = 1;; i++) {
1124 _cleanup_free_ char *dev = NULL, *d = NULL;
1125 int prio = 0, k;
1126
1127 k = fscanf(m->proc_swaps,
1128 "%ms " /* device/file */
1129 "%*s " /* type of swap */
1130 "%*s " /* swap size */
1131 "%*s " /* used */
1132 "%i\n", /* priority */
1133 &dev, &prio);
1134 if (k != 2) {
1135 if (k == EOF)
1136 break;
1137
1138 log_warning("Failed to parse /proc/swaps:%u.", i);
1139 continue;
1140 }
1141
1142 if (cunescape(dev, UNESCAPE_RELAX, &d) < 0)
1143 return log_oom();
1144
1145 device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP);
1146
1147 (void) swap_process_new(m, d, prio, set_flags);
1148 }
1149
1150 return 0;
1151 }
1152
1153 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1154 Manager *m = userdata;
1155 Unit *u;
1156 int r;
1157
1158 assert(m);
1159 assert(revents & EPOLLPRI);
1160
1161 r = swap_load_proc_swaps(m, true);
1162 if (r < 0) {
1163 log_error_errno(r, "Failed to reread /proc/swaps: %m");
1164
1165 /* Reset flags, just in case, for late calls */
1166 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1167 Swap *swap = SWAP(u);
1168
1169 swap->is_active = swap->just_activated = false;
1170 }
1171
1172 return 0;
1173 }
1174
1175 manager_dispatch_load_queue(m);
1176
1177 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1178 Swap *swap = SWAP(u);
1179
1180 if (!swap->is_active) {
1181
1182 swap_unset_proc_swaps(swap);
1183
1184 switch (swap->state) {
1185
1186 case SWAP_ACTIVE:
1187 /* This has just been deactivated */
1188 swap_enter_dead(swap, SWAP_SUCCESS);
1189 break;
1190
1191 default:
1192 /* Fire again */
1193 swap_set_state(swap, swap->state);
1194 break;
1195 }
1196
1197 if (swap->what)
1198 device_found_node(m, swap->what, 0, DEVICE_FOUND_SWAP);
1199
1200 } else if (swap->just_activated) {
1201
1202 /* New swap entry */
1203
1204 switch (swap->state) {
1205
1206 case SWAP_DEAD:
1207 case SWAP_FAILED:
1208 (void) unit_acquire_invocation_id(u);
1209 swap_cycle_clear(swap);
1210 swap_enter_active(swap, SWAP_SUCCESS);
1211 break;
1212
1213 case SWAP_ACTIVATING:
1214 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1215 break;
1216
1217 default:
1218 /* Nothing really changed, but let's
1219 * issue an notification call
1220 * nonetheless, in case somebody is
1221 * waiting for this. */
1222 swap_set_state(swap, swap->state);
1223 break;
1224 }
1225 }
1226
1227 /* Reset the flags for later calls */
1228 swap->is_active = swap->just_activated = false;
1229 }
1230
1231 return 1;
1232 }
1233
1234 static Unit *swap_following(Unit *u) {
1235 Swap *s = SWAP(u);
1236 Swap *other, *first = NULL;
1237
1238 assert(s);
1239
1240 /* If the user configured the swap through /etc/fstab or
1241 * a device unit, follow that. */
1242
1243 if (s->from_fragment)
1244 return NULL;
1245
1246 LIST_FOREACH_OTHERS(same_devnode, other, s)
1247 if (other->from_fragment)
1248 return UNIT(other);
1249
1250 /* Otherwise, make everybody follow the unit that's named after
1251 * the swap device in the kernel */
1252
1253 if (streq_ptr(s->what, s->devnode))
1254 return NULL;
1255
1256 LIST_FOREACH_AFTER(same_devnode, other, s)
1257 if (streq_ptr(other->what, other->devnode))
1258 return UNIT(other);
1259
1260 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1261 if (streq_ptr(other->what, other->devnode))
1262 return UNIT(other);
1263
1264 first = other;
1265 }
1266
1267 /* Fall back to the first on the list */
1268 return UNIT(first);
1269 }
1270
1271 static int swap_following_set(Unit *u, Set **_set) {
1272 Swap *s = SWAP(u), *other;
1273 _cleanup_set_free_ Set *set = NULL;
1274 int r;
1275
1276 assert(s);
1277 assert(_set);
1278
1279 if (LIST_JUST_US(same_devnode, s)) {
1280 *_set = NULL;
1281 return 0;
1282 }
1283
1284 set = set_new(NULL);
1285 if (!set)
1286 return -ENOMEM;
1287
1288 LIST_FOREACH_OTHERS(same_devnode, other, s) {
1289 r = set_put(set, other);
1290 if (r < 0)
1291 return r;
1292 }
1293
1294 *_set = TAKE_PTR(set);
1295 return 1;
1296 }
1297
1298 static void swap_shutdown(Manager *m) {
1299 assert(m);
1300
1301 m->swap_event_source = sd_event_source_unref(m->swap_event_source);
1302 m->proc_swaps = safe_fclose(m->proc_swaps);
1303 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
1304 }
1305
1306 static void swap_enumerate(Manager *m) {
1307 int r;
1308
1309 assert(m);
1310
1311 if (!m->proc_swaps) {
1312 m->proc_swaps = fopen("/proc/swaps", "re");
1313 if (!m->proc_swaps) {
1314 if (errno == ENOENT)
1315 log_debug_errno(errno, "Not swap enabled, skipping enumeration.");
1316 else
1317 log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m");
1318
1319 return;
1320 }
1321
1322 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
1323 if (r < 0) {
1324 log_error_errno(r, "Failed to watch /proc/swaps: %m");
1325 goto fail;
1326 }
1327
1328 /* Dispatch this before we dispatch SIGCHLD, so that
1329 * we always get the events from /proc/swaps before
1330 * the SIGCHLD of /sbin/swapon. */
1331 r = sd_event_source_set_priority(m->swap_event_source, SD_EVENT_PRIORITY_NORMAL-10);
1332 if (r < 0) {
1333 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
1334 goto fail;
1335 }
1336
1337 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
1338 }
1339
1340 r = swap_load_proc_swaps(m, false);
1341 if (r < 0)
1342 goto fail;
1343
1344 return;
1345
1346 fail:
1347 swap_shutdown(m);
1348 }
1349
1350 int swap_process_device_new(Manager *m, sd_device *dev) {
1351 _cleanup_free_ char *e = NULL;
1352 const char *dn, *devlink;
1353 Unit *u;
1354 int r = 0;
1355
1356 assert(m);
1357 assert(dev);
1358
1359 r = sd_device_get_devname(dev, &dn);
1360 if (r < 0)
1361 return 0;
1362
1363 r = unit_name_from_path(dn, ".swap", &e);
1364 if (r < 0)
1365 return r;
1366
1367 u = manager_get_unit(m, e);
1368 if (u)
1369 r = swap_set_devnode(SWAP(u), dn);
1370
1371 FOREACH_DEVICE_DEVLINK(dev, devlink) {
1372 _cleanup_free_ char *n = NULL;
1373 int q;
1374
1375 q = unit_name_from_path(devlink, ".swap", &n);
1376 if (q < 0)
1377 return q;
1378
1379 u = manager_get_unit(m, n);
1380 if (u) {
1381 q = swap_set_devnode(SWAP(u), dn);
1382 if (q < 0)
1383 r = q;
1384 }
1385 }
1386
1387 return r;
1388 }
1389
1390 int swap_process_device_remove(Manager *m, sd_device *dev) {
1391 const char *dn;
1392 int r = 0;
1393 Swap *s;
1394
1395 r = sd_device_get_devname(dev, &dn);
1396 if (r < 0)
1397 return 0;
1398
1399 while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1400 int q;
1401
1402 q = swap_set_devnode(s, NULL);
1403 if (q < 0)
1404 r = q;
1405 }
1406
1407 return r;
1408 }
1409
1410 static void swap_reset_failed(Unit *u) {
1411 Swap *s = SWAP(u);
1412
1413 assert(s);
1414
1415 if (s->state == SWAP_FAILED)
1416 swap_set_state(s, SWAP_DEAD);
1417
1418 s->result = SWAP_SUCCESS;
1419 }
1420
1421 static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1422 return unit_kill_common(u, who, signo, -1, SWAP(u)->control_pid, error);
1423 }
1424
1425 static int swap_get_timeout(Unit *u, usec_t *timeout) {
1426 Swap *s = SWAP(u);
1427 usec_t t;
1428 int r;
1429
1430 if (!s->timer_event_source)
1431 return 0;
1432
1433 r = sd_event_source_get_time(s->timer_event_source, &t);
1434 if (r < 0)
1435 return r;
1436 if (t == USEC_INFINITY)
1437 return 0;
1438
1439 *timeout = t;
1440 return 1;
1441 }
1442
1443 static bool swap_supported(void) {
1444 static int supported = -1;
1445
1446 /* If swap support is not available in the kernel, or we are
1447 * running in a container we don't support swap units, and any
1448 * attempts to starting one should fail immediately. */
1449
1450 if (supported < 0)
1451 supported =
1452 access("/proc/swaps", F_OK) >= 0 &&
1453 detect_container() <= 0;
1454
1455 return supported;
1456 }
1457
1458 static int swap_control_pid(Unit *u) {
1459 Swap *s = SWAP(u);
1460
1461 assert(s);
1462
1463 return s->control_pid;
1464 }
1465
1466 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1467 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1468 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1469 };
1470
1471 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1472
1473 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1474 [SWAP_SUCCESS] = "success",
1475 [SWAP_FAILURE_RESOURCES] = "resources",
1476 [SWAP_FAILURE_TIMEOUT] = "timeout",
1477 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1478 [SWAP_FAILURE_SIGNAL] = "signal",
1479 [SWAP_FAILURE_CORE_DUMP] = "core-dump",
1480 [SWAP_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
1481 };
1482
1483 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1484
1485 const UnitVTable swap_vtable = {
1486 .object_size = sizeof(Swap),
1487 .exec_context_offset = offsetof(Swap, exec_context),
1488 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1489 .kill_context_offset = offsetof(Swap, kill_context),
1490 .exec_runtime_offset = offsetof(Swap, exec_runtime),
1491 .dynamic_creds_offset = offsetof(Swap, dynamic_creds),
1492
1493 .sections =
1494 "Unit\0"
1495 "Swap\0"
1496 "Install\0",
1497 .private_section = "Swap",
1498
1499 .init = swap_init,
1500 .load = swap_load,
1501 .done = swap_done,
1502
1503 .coldplug = swap_coldplug,
1504
1505 .dump = swap_dump,
1506
1507 .start = swap_start,
1508 .stop = swap_stop,
1509
1510 .kill = swap_kill,
1511
1512 .get_timeout = swap_get_timeout,
1513
1514 .serialize = swap_serialize,
1515 .deserialize_item = swap_deserialize_item,
1516
1517 .active_state = swap_active_state,
1518 .sub_state_to_string = swap_sub_state_to_string,
1519
1520 .may_gc = swap_may_gc,
1521
1522 .sigchld_event = swap_sigchld_event,
1523
1524 .reset_failed = swap_reset_failed,
1525
1526 .control_pid = swap_control_pid,
1527
1528 .bus_vtable = bus_swap_vtable,
1529 .bus_set_property = bus_swap_set_property,
1530 .bus_commit_properties = bus_swap_commit_properties,
1531
1532 .following = swap_following,
1533 .following_set = swap_following_set,
1534
1535 .enumerate = swap_enumerate,
1536 .shutdown = swap_shutdown,
1537 .supported = swap_supported,
1538
1539 .status_message_formats = {
1540 .starting_stopping = {
1541 [0] = "Activating swap %s...",
1542 [1] = "Deactivating swap %s...",
1543 },
1544 .finished_start_job = {
1545 [JOB_DONE] = "Activated swap %s.",
1546 [JOB_FAILED] = "Failed to activate swap %s.",
1547 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1548 },
1549 .finished_stop_job = {
1550 [JOB_DONE] = "Deactivated swap %s.",
1551 [JOB_FAILED] = "Failed deactivating swap %s.",
1552 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",
1553 },
1554 },
1555 };