chiark / gitweb /
core: introduce new Delegate=yes/no property controlling creation of cgroup subhierar...
[elogind.git] / src / core / swap.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/epoll.h>
27 #include <sys/stat.h>
28 #include <sys/swap.h>
29 #include <libudev.h>
30
31 #include "unit.h"
32 #include "swap.h"
33 #include "load-fragment.h"
34 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "dbus-swap.h"
37 #include "special.h"
38 #include "bus-errors.h"
39 #include "exit-status.h"
40 #include "def.h"
41 #include "path-util.h"
42 #include "virt.h"
43 #include "udev-util.h"
44
45 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
46         [SWAP_DEAD] = UNIT_INACTIVE,
47         [SWAP_ACTIVATING] = UNIT_ACTIVATING,
48         [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
49         [SWAP_ACTIVE] = UNIT_ACTIVE,
50         [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
51         [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
52         [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
53         [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
54         [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
55         [SWAP_FAILED] = UNIT_FAILED
56 };
57
58 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
59 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
60
61 static void swap_unset_proc_swaps(Swap *s) {
62         assert(s);
63
64         if (!s->from_proc_swaps)
65                 return;
66
67         free(s->parameters_proc_swaps.what);
68         s->parameters_proc_swaps.what = NULL;
69
70         s->from_proc_swaps = false;
71 }
72
73 static int swap_set_devnode(Swap *s, const char *devnode) {
74         Hashmap *swaps;
75         Swap *first;
76         int r;
77
78         assert(s);
79
80         r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &string_hash_ops);
81         if (r < 0)
82                 return r;
83
84         swaps = UNIT(s)->manager->swaps_by_devnode;
85
86         if (s->devnode) {
87                 first = hashmap_get(swaps, s->devnode);
88
89                 LIST_REMOVE(same_devnode, first, s);
90                 if (first)
91                         hashmap_replace(swaps, first->devnode, first);
92                 else
93                         hashmap_remove(swaps, s->devnode);
94
95                 free(s->devnode);
96                 s->devnode = NULL;
97         }
98
99         if (devnode) {
100                 s->devnode = strdup(devnode);
101                 if (!s->devnode)
102                         return -ENOMEM;
103
104                 first = hashmap_get(swaps, s->devnode);
105                 LIST_PREPEND(same_devnode, first, s);
106
107                 return hashmap_replace(swaps, first->devnode, first);
108         }
109
110         return 0;
111 }
112
113 static void swap_init(Unit *u) {
114         Swap *s = SWAP(u);
115
116         assert(s);
117         assert(UNIT(s)->load_state == UNIT_STUB);
118
119         s->timeout_usec = u->manager->default_timeout_start_usec;
120
121         s->exec_context.std_output = u->manager->default_std_output;
122         s->exec_context.std_error = u->manager->default_std_error;
123
124         s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
125
126         s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
127
128         u->ignore_on_isolate = true;
129 }
130
131 static void swap_unwatch_control_pid(Swap *s) {
132         assert(s);
133
134         if (s->control_pid <= 0)
135                 return;
136
137         unit_unwatch_pid(UNIT(s), s->control_pid);
138         s->control_pid = 0;
139 }
140
141 static void swap_done(Unit *u) {
142         Swap *s = SWAP(u);
143
144         assert(s);
145
146         swap_unset_proc_swaps(s);
147         swap_set_devnode(s, NULL);
148
149         free(s->what);
150         s->what = NULL;
151
152         free(s->parameters_fragment.what);
153         s->parameters_fragment.what = NULL;
154
155         free(s->parameters_fragment.options);
156         s->parameters_fragment.options = NULL;
157
158         s->exec_runtime = exec_runtime_unref(s->exec_runtime);
159         exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
160         s->control_command = NULL;
161
162         swap_unwatch_control_pid(s);
163
164         s->timer_event_source = sd_event_source_unref(s->timer_event_source);
165 }
166
167 static int swap_arm_timer(Swap *s) {
168         int r;
169
170         assert(s);
171
172         if (s->timeout_usec <= 0) {
173                 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
174                 return 0;
175         }
176
177         if (s->timer_event_source) {
178                 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + s->timeout_usec);
179                 if (r < 0)
180                         return r;
181
182                 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
183         }
184
185         return sd_event_add_time(
186                         UNIT(s)->manager->event,
187                         &s->timer_event_source,
188                         CLOCK_MONOTONIC,
189                         now(CLOCK_MONOTONIC) + s->timeout_usec, 0,
190                         swap_dispatch_timer, s);
191 }
192
193 static int swap_add_device_links(Swap *s) {
194         assert(s);
195
196         if (!s->what)
197                 return 0;
198
199         if (!s->from_fragment)
200                 return 0;
201
202         if (is_device_path(s->what))
203                 return unit_add_node_link(UNIT(s), s->what, UNIT(s)->manager->running_as == SYSTEMD_SYSTEM);
204         else
205                 /* File based swap devices need to be ordered after
206                  * systemd-remount-fs.service, since they might need a
207                  * writable file system. */
208                 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
209 }
210
211 static int swap_add_default_dependencies(Swap *s) {
212         int r;
213
214         assert(s);
215
216         if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
217                 return 0;
218
219         if (detect_container(NULL) > 0)
220                 return 0;
221
222         r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
223         if (r < 0)
224                 return r;
225
226         if (!s->from_fragment)
227                 /* The swap unit can either be for an alternative device name, in which
228                  * case we don't need to add the dependency on swap.target because this unit
229                  * is following a different unit which will have this dependency added,
230                  * or it can be derived from /proc/swaps, in which case it was started
231                  * manually, and should not become a dependency of swap.target. */
232                 return 0;
233
234         return unit_add_two_dependencies_by_name_inverse(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SWAP_TARGET, NULL, true);
235 }
236
237 static int swap_verify(Swap *s) {
238         bool b;
239         _cleanup_free_ char *e = NULL;
240
241         if (UNIT(s)->load_state != UNIT_LOADED)
242                 return 0;
243
244         e = unit_name_from_path(s->what, ".swap");
245         if (!e)
246                 return log_oom();
247
248         b = unit_has_name(UNIT(s), e);
249         if (!b) {
250                 log_error_unit(UNIT(s)->id, "%s: Value of \"What\" and unit name do not match, not loading.", UNIT(s)->id);
251                 return -EINVAL;
252         }
253
254         if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
255                 log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.", UNIT(s)->id);
256                 return -EINVAL;
257         }
258
259         return 0;
260 }
261
262 static int swap_load_devnode(Swap *s) {
263         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
264         struct stat st;
265         const char *p;
266
267         assert(s);
268
269         if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
270                 return 0;
271
272         d = udev_device_new_from_devnum(UNIT(s)->manager->udev, 'b', st.st_rdev);
273         if (!d)
274                 return 0;
275
276         p = udev_device_get_devnode(d);
277         if (!p)
278                 return 0;
279
280         return swap_set_devnode(s, p);
281 }
282
283 static int swap_load(Unit *u) {
284         int r;
285         Swap *s = SWAP(u);
286
287         assert(s);
288         assert(u->load_state == UNIT_STUB);
289
290         /* Load a .swap file */
291         r = unit_load_fragment_and_dropin_optional(u);
292         if (r < 0)
293                 return r;
294
295         if (u->load_state == UNIT_LOADED) {
296
297                 if (UNIT(s)->fragment_path)
298                         s->from_fragment = true;
299
300                 if (!s->what) {
301                         if (s->parameters_fragment.what)
302                                 s->what = strdup(s->parameters_fragment.what);
303                         else if (s->parameters_proc_swaps.what)
304                                 s->what = strdup(s->parameters_proc_swaps.what);
305                         else
306                                 s->what = unit_name_to_path(u->id);
307
308                         if (!s->what)
309                                 return -ENOMEM;
310                 }
311
312                 path_kill_slashes(s->what);
313
314                 if (!UNIT(s)->description) {
315                         r = unit_set_description(u, s->what);
316                         if (r < 0)
317                                 return r;
318                 }
319
320                 r = unit_require_mounts_for(UNIT(s), s->what);
321                 if (r < 0)
322                         return r;
323
324                 r = swap_add_device_links(s);
325                 if (r < 0)
326                         return r;
327
328                 r = swap_load_devnode(s);
329                 if (r < 0)
330                         return r;
331
332                 r = unit_patch_contexts(u);
333                 if (r < 0)
334                         return r;
335
336                 r = unit_add_exec_dependencies(u, &s->exec_context);
337                 if (r < 0)
338                         return r;
339
340                 r = unit_add_default_slice(u, &s->cgroup_context);
341                 if (r < 0)
342                         return r;
343
344                 if (UNIT(s)->default_dependencies) {
345                         r = swap_add_default_dependencies(s);
346                         if (r < 0)
347                                 return r;
348                 }
349         }
350
351         return swap_verify(s);
352 }
353
354 static int swap_add_one(
355                 Manager *m,
356                 const char *what,
357                 const char *what_proc_swaps,
358                 int priority,
359                 bool set_flags) {
360
361         _cleanup_free_ char *e = NULL;
362         bool delete = false;
363         Unit *u = NULL;
364         int r;
365         SwapParameters *p;
366
367         assert(m);
368         assert(what);
369         assert(what_proc_swaps);
370
371         e = unit_name_from_path(what, ".swap");
372         if (!e)
373                 return log_oom();
374
375         u = manager_get_unit(m, e);
376
377         if (u &&
378             SWAP(u)->from_proc_swaps &&
379             !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
380                 return -EEXIST;
381
382         if (!u) {
383                 delete = true;
384
385                 u = unit_new(m, sizeof(Swap));
386                 if (!u)
387                         return log_oom();
388
389                 r = unit_add_name(u, e);
390                 if (r < 0)
391                         goto fail;
392
393                 SWAP(u)->what = strdup(what);
394                 if (!SWAP(u)->what) {
395                         r = log_oom();
396                         goto fail;
397                 }
398
399                 unit_add_to_load_queue(u);
400         } else
401                 delete = false;
402
403         p = &SWAP(u)->parameters_proc_swaps;
404
405         if (!p->what) {
406                 p->what = strdup(what_proc_swaps);
407                 if (!p->what) {
408                         r = -ENOMEM;
409                         goto fail;
410                 }
411         }
412
413         if (set_flags) {
414                 SWAP(u)->is_active = true;
415                 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
416         }
417
418         SWAP(u)->from_proc_swaps = true;
419
420         p->priority = priority;
421
422         unit_add_to_dbus_queue(u);
423
424         return 0;
425
426 fail:
427         log_warning_unit(e, "Failed to load swap unit: %s", strerror(-r));
428
429         if (delete && u)
430                 unit_free(u);
431
432         return r;
433 }
434
435 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
436         _cleanup_udev_device_unref_ struct udev_device *d = NULL;
437         struct udev_list_entry *item = NULL, *first = NULL;
438         const char *dn;
439         struct stat st;
440         int r;
441
442         assert(m);
443
444         r = swap_add_one(m, device, device, prio, set_flags);
445         if (r < 0)
446                 return r;
447
448         /* If this is a block device, then let's add duplicates for
449          * all other names of this block device */
450         if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
451                 return 0;
452
453         d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev);
454         if (!d)
455                 return 0;
456
457         /* Add the main device node */
458         dn = udev_device_get_devnode(d);
459         if (dn && !streq(dn, device))
460                 swap_add_one(m, dn, device, prio, set_flags);
461
462         /* Add additional units for all symlinks */
463         first = udev_device_get_devlinks_list_entry(d);
464         udev_list_entry_foreach(item, first) {
465                 const char *p;
466
467                 /* Don't bother with the /dev/block links */
468                 p = udev_list_entry_get_name(item);
469
470                 if (streq(p, device))
471                         continue;
472
473                 if (path_startswith(p, "/dev/block/"))
474                         continue;
475
476                 if (stat(p, &st) >= 0)
477                         if (!S_ISBLK(st.st_mode) ||
478                             st.st_rdev != udev_device_get_devnum(d))
479                                 continue;
480
481                 swap_add_one(m, p, device, prio, set_flags);
482         }
483
484         return r;
485 }
486
487 static void swap_set_state(Swap *s, SwapState state) {
488         SwapState old_state;
489
490         assert(s);
491
492         old_state = s->state;
493         s->state = state;
494
495         if (state != SWAP_ACTIVATING &&
496             state != SWAP_ACTIVATING_SIGTERM &&
497             state != SWAP_ACTIVATING_SIGKILL &&
498             state != SWAP_ACTIVATING_DONE &&
499             state != SWAP_DEACTIVATING &&
500             state != SWAP_DEACTIVATING_SIGTERM &&
501             state != SWAP_DEACTIVATING_SIGKILL) {
502                 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
503                 swap_unwatch_control_pid(s);
504                 s->control_command = NULL;
505                 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
506         }
507
508         if (state != old_state)
509                 log_debug_unit(UNIT(s)->id,
510                                "%s changed %s -> %s",
511                                UNIT(s)->id,
512                                swap_state_to_string(old_state),
513                                swap_state_to_string(state));
514
515         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
516 }
517
518 static int swap_coldplug(Unit *u) {
519         Swap *s = SWAP(u);
520         SwapState new_state = SWAP_DEAD;
521         int r;
522
523         assert(s);
524         assert(s->state == SWAP_DEAD);
525
526         if (s->deserialized_state != s->state)
527                 new_state = s->deserialized_state;
528         else if (s->from_proc_swaps)
529                 new_state = SWAP_ACTIVE;
530
531         if (new_state == s->state)
532                 return 0;
533
534         if (new_state == SWAP_ACTIVATING ||
535             new_state == SWAP_ACTIVATING_SIGTERM ||
536             new_state == SWAP_ACTIVATING_SIGKILL ||
537             new_state == SWAP_ACTIVATING_DONE ||
538             new_state == SWAP_DEACTIVATING ||
539             new_state == SWAP_DEACTIVATING_SIGTERM ||
540             new_state == SWAP_DEACTIVATING_SIGKILL) {
541
542                 if (s->control_pid <= 0)
543                         return -EBADMSG;
544
545                 r = unit_watch_pid(UNIT(s), s->control_pid);
546                 if (r < 0)
547                         return r;
548
549                 r = swap_arm_timer(s);
550                 if (r < 0)
551                         return r;
552         }
553
554         swap_set_state(s, new_state);
555         return 0;
556 }
557
558 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
559         Swap *s = SWAP(u);
560         SwapParameters *p;
561
562         assert(s);
563         assert(f);
564
565         if (s->from_proc_swaps)
566                 p = &s->parameters_proc_swaps;
567         else if (s->from_fragment)
568                 p = &s->parameters_fragment;
569         else
570                 p = NULL;
571
572         fprintf(f,
573                 "%sSwap State: %s\n"
574                 "%sResult: %s\n"
575                 "%sWhat: %s\n"
576                 "%sFrom /proc/swaps: %s\n"
577                 "%sFrom fragment: %s\n",
578                 prefix, swap_state_to_string(s->state),
579                 prefix, swap_result_to_string(s->result),
580                 prefix, s->what,
581                 prefix, yes_no(s->from_proc_swaps),
582                 prefix, yes_no(s->from_fragment));
583
584         if (s->devnode)
585                 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
586
587         if (p)
588                 fprintf(f,
589                         "%sPriority: %i\n"
590                         "%sOptions: %s\n",
591                         prefix, p->priority,
592                         prefix, strempty(p->options));
593
594         if (s->control_pid > 0)
595                 fprintf(f,
596                         "%sControl PID: "PID_FMT"\n",
597                         prefix, s->control_pid);
598
599         exec_context_dump(&s->exec_context, f, prefix);
600         kill_context_dump(&s->kill_context, f, prefix);
601 }
602
603 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
604         pid_t pid;
605         int r;
606         ExecParameters exec_params = {
607                 .apply_permissions = true,
608                 .apply_chroot      = true,
609                 .apply_tty_stdin   = true,
610         };
611
612         assert(s);
613         assert(c);
614         assert(_pid);
615
616         unit_realize_cgroup(UNIT(s));
617
618         r = unit_setup_exec_runtime(UNIT(s));
619         if (r < 0)
620                 goto fail;
621
622         r = swap_arm_timer(s);
623         if (r < 0)
624                 goto fail;
625
626         exec_params.environment = UNIT(s)->manager->environment;
627         exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
628         exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
629         exec_params.cgroup_path = UNIT(s)->cgroup_path;
630         exec_params.cgroup_delegate = s->cgroup_context.delegate;
631         exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
632         exec_params.unit_id = UNIT(s)->id;
633
634         r = exec_spawn(c,
635                        &s->exec_context,
636                        &exec_params,
637                        s->exec_runtime,
638                        &pid);
639         if (r < 0)
640                 goto fail;
641
642         r = unit_watch_pid(UNIT(s), pid);
643         if (r < 0)
644                 /* FIXME: we need to do something here */
645                 goto fail;
646
647         *_pid = pid;
648
649         return 0;
650
651 fail:
652         s->timer_event_source = sd_event_source_unref(s->timer_event_source);
653
654         return r;
655 }
656
657 static void swap_enter_dead(Swap *s, SwapResult f) {
658         assert(s);
659
660         if (f != SWAP_SUCCESS)
661                 s->result = f;
662
663         exec_runtime_destroy(s->exec_runtime);
664         s->exec_runtime = exec_runtime_unref(s->exec_runtime);
665
666         exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
667
668         swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
669 }
670
671 static void swap_enter_active(Swap *s, SwapResult f) {
672         assert(s);
673
674         if (f != SWAP_SUCCESS)
675                 s->result = f;
676
677         swap_set_state(s, SWAP_ACTIVE);
678 }
679
680 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
681         int r;
682
683         assert(s);
684
685         if (f != SWAP_SUCCESS)
686                 s->result = f;
687
688         r = unit_kill_context(
689                         UNIT(s),
690                         &s->kill_context,
691                         (state != SWAP_ACTIVATING_SIGTERM && state != SWAP_DEACTIVATING_SIGTERM) ?
692                         KILL_KILL : KILL_TERMINATE,
693                         -1,
694                         s->control_pid,
695                         false);
696         if (r < 0)
697                 goto fail;
698
699         if (r > 0) {
700                 r = swap_arm_timer(s);
701                 if (r < 0)
702                         goto fail;
703
704                 swap_set_state(s, state);
705         } else if (state == SWAP_ACTIVATING_SIGTERM)
706                 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_SUCCESS);
707         else if (state == SWAP_DEACTIVATING_SIGTERM)
708                 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
709         else
710                 swap_enter_dead(s, SWAP_SUCCESS);
711
712         return;
713
714 fail:
715         log_warning_unit(UNIT(s)->id,
716                          "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
717
718         swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
719 }
720
721 static int mount_find_pri(const char *options, int *ret) {
722         const char *opt;
723         char *end;
724         unsigned long r;
725
726         assert(ret);
727
728         if (!options)
729                 return 0;
730
731         opt = mount_test_option(options, "pri");
732         if (!opt)
733                 return 0;
734
735         opt += strlen("pri");
736         if (*opt != '=')
737                 return -EINVAL;
738
739         errno = 0;
740         r = strtoul(opt + 1, &end, 10);
741         if (errno > 0)
742                 return -errno;
743
744         if (end == opt + 1 || (*end != ',' && *end != 0))
745                 return -EINVAL;
746
747         *ret = (int) r;
748         return 1;
749 }
750
751 static int mount_find_discard(const char *options, char **ret) {
752         const char *opt;
753         char *ans;
754         size_t len;
755
756         assert(ret);
757
758         if (!options)
759                 return 0;
760
761         opt = mount_test_option(options, "discard");
762         if (!opt)
763                 return 0;
764
765         opt += strlen("discard");
766         if (*opt == ',' || *opt == '\0')
767                 ans = strdup("all");
768         else {
769                 if (*opt != '=')
770                         return -EINVAL;
771
772                 len = strcspn(opt + 1, ",");
773                 if (len == 0)
774                         return -EINVAL;
775
776                 ans = strndup(opt + 1, len);
777         }
778
779         if (!ans)
780                 return -ENOMEM;
781
782         *ret = ans;
783         return 1;
784 }
785
786 static void swap_enter_activating(Swap *s) {
787         _cleanup_free_ char *discard = NULL;
788         int r, priority = -1;
789
790         assert(s);
791
792         s->control_command_id = SWAP_EXEC_ACTIVATE;
793         s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
794
795         if (s->from_fragment) {
796                 mount_find_discard(s->parameters_fragment.options, &discard);
797
798                 priority = s->parameters_fragment.priority;
799                 if (priority < 0)
800                         mount_find_pri(s->parameters_fragment.options, &priority);
801         }
802
803         r = exec_command_set(s->control_command, "/sbin/swapon", NULL);
804         if (r < 0)
805                 goto fail;
806
807         if (priority >= 0) {
808                 char p[DECIMAL_STR_MAX(int)];
809
810                 sprintf(p, "%i", priority);
811                 r = exec_command_append(s->control_command, "-p", p, NULL);
812                 if (r < 0)
813                         goto fail;
814         }
815
816         if (discard && !streq(discard, "none")) {
817                 const char *discard_arg;
818
819                 if (streq(discard, "all"))
820                         discard_arg = "--discard";
821                 else
822                         discard_arg = strappenda("--discard=", discard);
823
824                 r = exec_command_append(s->control_command, discard_arg, NULL);
825                 if (r < 0)
826                         goto fail;
827         }
828
829         r = exec_command_append(s->control_command, s->what, NULL);
830         if (r < 0)
831                 goto fail;
832
833         swap_unwatch_control_pid(s);
834
835         r = swap_spawn(s, s->control_command, &s->control_pid);
836         if (r < 0)
837                 goto fail;
838
839         swap_set_state(s, SWAP_ACTIVATING);
840
841         return;
842
843 fail:
844         log_warning_unit(UNIT(s)->id,
845                          "%s failed to run 'swapon' task: %s",
846                          UNIT(s)->id, strerror(-r));
847         swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
848 }
849
850 static void swap_enter_deactivating(Swap *s) {
851         int r;
852
853         assert(s);
854
855         s->control_command_id = SWAP_EXEC_DEACTIVATE;
856         s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
857
858         r = exec_command_set(s->control_command,
859                              "/sbin/swapoff",
860                              s->what,
861                              NULL);
862         if (r < 0)
863                 goto fail;
864
865         swap_unwatch_control_pid(s);
866
867         r = swap_spawn(s, s->control_command, &s->control_pid);
868         if (r < 0)
869                 goto fail;
870
871         swap_set_state(s, SWAP_DEACTIVATING);
872
873         return;
874
875 fail:
876         log_warning_unit(UNIT(s)->id,
877                          "%s failed to run 'swapoff' task: %s",
878                          UNIT(s)->id, strerror(-r));
879         swap_enter_active(s, SWAP_FAILURE_RESOURCES);
880 }
881
882 static int swap_start(Unit *u) {
883         Swap *s = SWAP(u);
884
885         assert(s);
886
887         /* We cannot fulfill this request right now, try again later
888          * please! */
889
890         if (s->state == SWAP_DEACTIVATING ||
891             s->state == SWAP_DEACTIVATING_SIGTERM ||
892             s->state == SWAP_DEACTIVATING_SIGKILL ||
893             s->state == SWAP_ACTIVATING_SIGTERM ||
894             s->state == SWAP_ACTIVATING_SIGKILL)
895                 return -EAGAIN;
896
897         if (s->state == SWAP_ACTIVATING)
898                 return 0;
899
900         assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
901
902         if (detect_container(NULL) > 0)
903                 return -EPERM;
904
905         s->result = SWAP_SUCCESS;
906         swap_enter_activating(s);
907         return 0;
908 }
909
910 static int swap_stop(Unit *u) {
911         Swap *s = SWAP(u);
912
913         assert(s);
914
915         if (s->state == SWAP_DEACTIVATING ||
916             s->state == SWAP_DEACTIVATING_SIGTERM ||
917             s->state == SWAP_DEACTIVATING_SIGKILL ||
918             s->state == SWAP_ACTIVATING_SIGTERM ||
919             s->state == SWAP_ACTIVATING_SIGKILL)
920                 return 0;
921
922         assert(s->state == SWAP_ACTIVATING ||
923                s->state == SWAP_ACTIVATING_DONE ||
924                s->state == SWAP_ACTIVE);
925
926         if (detect_container(NULL) > 0)
927                 return -EPERM;
928
929         swap_enter_deactivating(s);
930         return 0;
931 }
932
933 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
934         Swap *s = SWAP(u);
935
936         assert(s);
937         assert(f);
938         assert(fds);
939
940         unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
941         unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
942
943         if (s->control_pid > 0)
944                 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
945
946         if (s->control_command_id >= 0)
947                 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
948
949         return 0;
950 }
951
952 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
953         Swap *s = SWAP(u);
954
955         assert(s);
956         assert(fds);
957
958         if (streq(key, "state")) {
959                 SwapState state;
960
961                 state = swap_state_from_string(value);
962                 if (state < 0)
963                         log_debug_unit(u->id, "Failed to parse state value %s", value);
964                 else
965                         s->deserialized_state = state;
966         } else if (streq(key, "result")) {
967                 SwapResult f;
968
969                 f = swap_result_from_string(value);
970                 if (f < 0)
971                         log_debug_unit(u->id, "Failed to parse result value %s", value);
972                 else if (f != SWAP_SUCCESS)
973                         s->result = f;
974         } else if (streq(key, "control-pid")) {
975                 pid_t pid;
976
977                 if (parse_pid(value, &pid) < 0)
978                         log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
979                 else
980                         s->control_pid = pid;
981
982         } else if (streq(key, "control-command")) {
983                 SwapExecCommand id;
984
985                 id = swap_exec_command_from_string(value);
986                 if (id < 0)
987                         log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
988                 else {
989                         s->control_command_id = id;
990                         s->control_command = s->exec_command + id;
991                 }
992         } else
993                 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
994
995         return 0;
996 }
997
998 _pure_ static UnitActiveState swap_active_state(Unit *u) {
999         assert(u);
1000
1001         return state_translation_table[SWAP(u)->state];
1002 }
1003
1004 _pure_ static const char *swap_sub_state_to_string(Unit *u) {
1005         assert(u);
1006
1007         return swap_state_to_string(SWAP(u)->state);
1008 }
1009
1010 _pure_ static bool swap_check_gc(Unit *u) {
1011         Swap *s = SWAP(u);
1012
1013         assert(s);
1014
1015         return s->from_proc_swaps;
1016 }
1017
1018 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1019         Swap *s = SWAP(u);
1020         SwapResult f;
1021
1022         assert(s);
1023         assert(pid >= 0);
1024
1025         if (pid != s->control_pid)
1026                 return;
1027
1028         s->control_pid = 0;
1029
1030         if (is_clean_exit(code, status, NULL))
1031                 f = SWAP_SUCCESS;
1032         else if (code == CLD_EXITED)
1033                 f = SWAP_FAILURE_EXIT_CODE;
1034         else if (code == CLD_KILLED)
1035                 f = SWAP_FAILURE_SIGNAL;
1036         else if (code == CLD_DUMPED)
1037                 f = SWAP_FAILURE_CORE_DUMP;
1038         else
1039                 assert_not_reached("Unknown code");
1040
1041         if (f != SWAP_SUCCESS)
1042                 s->result = f;
1043
1044         if (s->control_command) {
1045                 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
1046
1047                 s->control_command = NULL;
1048                 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
1049         }
1050
1051         log_full_unit(f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
1052                       u->id,
1053                       "%s swap process exited, code=%s status=%i",
1054                       u->id, sigchld_code_to_string(code), status);
1055
1056         switch (s->state) {
1057
1058         case SWAP_ACTIVATING:
1059         case SWAP_ACTIVATING_DONE:
1060         case SWAP_ACTIVATING_SIGTERM:
1061         case SWAP_ACTIVATING_SIGKILL:
1062
1063                 if (f == SWAP_SUCCESS)
1064                         swap_enter_active(s, f);
1065                 else
1066                         swap_enter_dead(s, f);
1067                 break;
1068
1069         case SWAP_DEACTIVATING:
1070         case SWAP_DEACTIVATING_SIGKILL:
1071         case SWAP_DEACTIVATING_SIGTERM:
1072
1073                 swap_enter_dead(s, f);
1074                 break;
1075
1076         default:
1077                 assert_not_reached("Uh, control process died at wrong time.");
1078         }
1079
1080         /* Notify clients about changed exit status */
1081         unit_add_to_dbus_queue(u);
1082 }
1083
1084 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1085         Swap *s = SWAP(userdata);
1086
1087         assert(s);
1088         assert(s->timer_event_source == source);
1089
1090         switch (s->state) {
1091
1092         case SWAP_ACTIVATING:
1093         case SWAP_ACTIVATING_DONE:
1094                 log_warning_unit(UNIT(s)->id, "%s activation timed out. Stopping.", UNIT(s)->id);
1095                 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1096                 break;
1097
1098         case SWAP_DEACTIVATING:
1099                 log_warning_unit(UNIT(s)->id, "%s deactivation timed out. Stopping.", UNIT(s)->id);
1100                 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1101                 break;
1102
1103         case SWAP_ACTIVATING_SIGTERM:
1104                 if (s->kill_context.send_sigkill) {
1105                         log_warning_unit(UNIT(s)->id, "%s activation timed out. Killing.", UNIT(s)->id);
1106                         swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1107                 } else {
1108                         log_warning_unit(UNIT(s)->id, "%s activation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
1109                         swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1110                 }
1111                 break;
1112
1113         case SWAP_DEACTIVATING_SIGTERM:
1114                 if (s->kill_context.send_sigkill) {
1115                         log_warning_unit(UNIT(s)->id, "%s deactivation timed out. Killing.", UNIT(s)->id);
1116                         swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1117                 } else {
1118                         log_warning_unit(UNIT(s)->id, "%s deactivation timed out. Skipping SIGKILL. Ignoring.", UNIT(s)->id);
1119                         swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1120                 }
1121                 break;
1122
1123         case SWAP_ACTIVATING_SIGKILL:
1124         case SWAP_DEACTIVATING_SIGKILL:
1125                 log_warning_unit(UNIT(s)->id, "%s swap process still around after SIGKILL. Ignoring.", UNIT(s)->id);
1126                 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1127                 break;
1128
1129         default:
1130                 assert_not_reached("Timeout at wrong time.");
1131         }
1132
1133         return 0;
1134 }
1135
1136 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1137         unsigned i;
1138         int r = 0;
1139
1140         assert(m);
1141
1142         rewind(m->proc_swaps);
1143
1144         (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1145
1146         for (i = 1;; i++) {
1147                 _cleanup_free_ char *dev = NULL, *d = NULL;
1148                 int prio = 0, k;
1149
1150                 k = fscanf(m->proc_swaps,
1151                            "%ms "  /* device/file */
1152                            "%*s "  /* type of swap */
1153                            "%*s "  /* swap size */
1154                            "%*s "  /* used */
1155                            "%i\n", /* priority */
1156                            &dev, &prio);
1157                 if (k != 2) {
1158                         if (k == EOF)
1159                                 break;
1160
1161                         log_warning("Failed to parse /proc/swaps:%u", i);
1162                         continue;
1163                 }
1164
1165                 d = cunescape(dev);
1166                 if (!d)
1167                         return -ENOMEM;
1168
1169                 k = swap_process_new_swap(m, d, prio, set_flags);
1170                 if (k < 0)
1171                         r = k;
1172         }
1173
1174         return r;
1175 }
1176
1177 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1178         Manager *m = userdata;
1179         Unit *u;
1180         int r;
1181
1182         assert(m);
1183         assert(revents & EPOLLPRI);
1184
1185         r = swap_load_proc_swaps(m, true);
1186         if (r < 0) {
1187                 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1188
1189                 /* Reset flags, just in case, for late calls */
1190                 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1191                         Swap *swap = SWAP(u);
1192
1193                         swap->is_active = swap->just_activated = false;
1194                 }
1195
1196                 return 0;
1197         }
1198
1199         manager_dispatch_load_queue(m);
1200
1201         LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1202                 Swap *swap = SWAP(u);
1203
1204                 if (!swap->is_active) {
1205                         /* This has just been deactivated */
1206
1207                         swap_unset_proc_swaps(swap);
1208
1209                         switch (swap->state) {
1210
1211                         case SWAP_ACTIVE:
1212                                 swap_enter_dead(swap, SWAP_SUCCESS);
1213                                 break;
1214
1215                         default:
1216                                 /* Fire again */
1217                                 swap_set_state(swap, swap->state);
1218                                 break;
1219                         }
1220
1221                 } else if (swap->just_activated) {
1222
1223                         /* New swap entry */
1224
1225                         switch (swap->state) {
1226
1227                         case SWAP_DEAD:
1228                         case SWAP_FAILED:
1229                                 swap_enter_active(swap, SWAP_SUCCESS);
1230                                 break;
1231
1232                         case SWAP_ACTIVATING:
1233                                 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1234                                 break;
1235
1236                         default:
1237                                 /* Nothing really changed, but let's
1238                                  * issue an notification call
1239                                  * nonetheless, in case somebody is
1240                                  * waiting for this. */
1241                                 swap_set_state(swap, swap->state);
1242                                 break;
1243                         }
1244                 }
1245
1246                 /* Reset the flags for later calls */
1247                 swap->is_active = swap->just_activated = false;
1248         }
1249
1250         return 1;
1251 }
1252
1253 static Unit *swap_following(Unit *u) {
1254         Swap *s = SWAP(u);
1255         Swap *other, *first = NULL;
1256
1257         assert(s);
1258
1259         /* If the user configured the swap through /etc/fstab or
1260          * a device unit, follow that. */
1261
1262         if (s->from_fragment)
1263                 return NULL;
1264
1265         LIST_FOREACH_AFTER(same_devnode, other, s)
1266                 if (other->from_fragment)
1267                         return UNIT(other);
1268
1269         LIST_FOREACH_BEFORE(same_devnode, other, s)
1270                 if (other->from_fragment)
1271                         return UNIT(other);
1272
1273         /* Otherwise make everybody follow the unit that's named after
1274          * the swap device in the kernel */
1275
1276         if (streq_ptr(s->what, s->devnode))
1277                 return NULL;
1278
1279         LIST_FOREACH_AFTER(same_devnode, other, s)
1280                 if (streq_ptr(other->what, other->devnode))
1281                         return UNIT(other);
1282
1283         LIST_FOREACH_BEFORE(same_devnode, other, s) {
1284                 if (streq_ptr(other->what, other->devnode))
1285                         return UNIT(other);
1286
1287                 first = other;
1288         }
1289
1290         /* Fall back to the first on the list */
1291         return UNIT(first);
1292 }
1293
1294 static int swap_following_set(Unit *u, Set **_set) {
1295         Swap *s = SWAP(u), *other;
1296         Set *set;
1297         int r;
1298
1299         assert(s);
1300         assert(_set);
1301
1302         if (LIST_JUST_US(same_devnode, s)) {
1303                 *_set = NULL;
1304                 return 0;
1305         }
1306
1307         set = set_new(NULL);
1308         if (!set)
1309                 return -ENOMEM;
1310
1311         LIST_FOREACH_AFTER(same_devnode, other, s) {
1312                 r = set_put(set, other);
1313                 if (r < 0)
1314                         goto fail;
1315         }
1316
1317         LIST_FOREACH_BEFORE(same_devnode, other, s) {
1318                 r = set_put(set, other);
1319                 if (r < 0)
1320                         goto fail;
1321         }
1322
1323         *_set = set;
1324         return 1;
1325
1326 fail:
1327         set_free(set);
1328         return r;
1329 }
1330
1331 static void swap_shutdown(Manager *m) {
1332         assert(m);
1333
1334         m->swap_event_source = sd_event_source_unref(m->swap_event_source);
1335
1336         if (m->proc_swaps) {
1337                 fclose(m->proc_swaps);
1338                 m->proc_swaps = NULL;
1339         }
1340
1341         hashmap_free(m->swaps_by_devnode);
1342         m->swaps_by_devnode = NULL;
1343 }
1344
1345 static int 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                         return errno == ENOENT ? 0 : -errno;
1354
1355                 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
1356                 if (r < 0)
1357                         goto fail;
1358
1359                 /* Dispatch this before we dispatch SIGCHLD, so that
1360                  * we always get the events from /proc/swaps before
1361                  * the SIGCHLD of /sbin/swapon. */
1362                 r = sd_event_source_set_priority(m->swap_event_source, -10);
1363                 if (r < 0)
1364                         goto fail;
1365         }
1366
1367         r = swap_load_proc_swaps(m, false);
1368         if (r < 0)
1369                 goto fail;
1370
1371         return 0;
1372
1373 fail:
1374         swap_shutdown(m);
1375         return r;
1376 }
1377
1378 int swap_process_new_device(Manager *m, struct udev_device *dev) {
1379         struct udev_list_entry *item = NULL, *first = NULL;
1380         _cleanup_free_ char *e = NULL;
1381         const char *dn;
1382         Swap *s;
1383         int r = 0;
1384
1385         assert(m);
1386         assert(dev);
1387
1388         dn = udev_device_get_devnode(dev);
1389         if (!dn)
1390                 return 0;
1391
1392         e = unit_name_from_path(dn, ".swap");
1393         if (!e)
1394                 return -ENOMEM;
1395
1396         s = hashmap_get(m->units, e);
1397         if (s)
1398                 r = swap_set_devnode(s, dn);
1399
1400         first = udev_device_get_devlinks_list_entry(dev);
1401         udev_list_entry_foreach(item, first) {
1402                 _cleanup_free_ char *n = NULL;
1403
1404                 n = unit_name_from_path(udev_list_entry_get_name(item), ".swap");
1405                 if (!n)
1406                         return -ENOMEM;
1407
1408                 s = hashmap_get(m->units, n);
1409                 if (s) {
1410                         int q;
1411
1412                         q = swap_set_devnode(s, dn);
1413                         if (q < 0)
1414                                 r = q;
1415                 }
1416         }
1417
1418         return r;
1419 }
1420
1421 int swap_process_removed_device(Manager *m, struct udev_device *dev) {
1422         const char *dn;
1423         int r = 0;
1424         Swap *s;
1425
1426         dn = udev_device_get_devnode(dev);
1427         if (!dn)
1428                 return 0;
1429
1430         while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1431                 int q;
1432
1433                 q = swap_set_devnode(s, NULL);
1434                 if (q < 0)
1435                         r = q;
1436         }
1437
1438         return r;
1439 }
1440
1441 static void swap_reset_failed(Unit *u) {
1442         Swap *s = SWAP(u);
1443
1444         assert(s);
1445
1446         if (s->state == SWAP_FAILED)
1447                 swap_set_state(s, SWAP_DEAD);
1448
1449         s->result = SWAP_SUCCESS;
1450 }
1451
1452 static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1453         return unit_kill_common(u, who, signo, -1, SWAP(u)->control_pid, error);
1454 }
1455
1456 static int swap_get_timeout(Unit *u, uint64_t *timeout) {
1457         Swap *s = SWAP(u);
1458         int r;
1459
1460         if (!s->timer_event_source)
1461                 return 0;
1462
1463         r = sd_event_source_get_time(s->timer_event_source, timeout);
1464         if (r < 0)
1465                 return r;
1466
1467         return 1;
1468 }
1469
1470 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1471         [SWAP_DEAD] = "dead",
1472         [SWAP_ACTIVATING] = "activating",
1473         [SWAP_ACTIVATING_DONE] = "activating-done",
1474         [SWAP_ACTIVE] = "active",
1475         [SWAP_DEACTIVATING] = "deactivating",
1476         [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1477         [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1478         [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1479         [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1480         [SWAP_FAILED] = "failed"
1481 };
1482
1483 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1484
1485 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1486         [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1487         [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1488 };
1489
1490 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1491
1492 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1493         [SWAP_SUCCESS] = "success",
1494         [SWAP_FAILURE_RESOURCES] = "resources",
1495         [SWAP_FAILURE_TIMEOUT] = "timeout",
1496         [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1497         [SWAP_FAILURE_SIGNAL] = "signal",
1498         [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1499 };
1500
1501 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1502
1503 const UnitVTable swap_vtable = {
1504         .object_size = sizeof(Swap),
1505         .exec_context_offset = offsetof(Swap, exec_context),
1506         .cgroup_context_offset = offsetof(Swap, cgroup_context),
1507         .kill_context_offset = offsetof(Swap, kill_context),
1508         .exec_runtime_offset = offsetof(Swap, exec_runtime),
1509
1510         .sections =
1511                 "Unit\0"
1512                 "Swap\0"
1513                 "Install\0",
1514         .private_section = "Swap",
1515
1516         .no_alias = true,
1517         .no_instances = true,
1518
1519         .init = swap_init,
1520         .load = swap_load,
1521         .done = swap_done,
1522
1523         .coldplug = swap_coldplug,
1524
1525         .dump = swap_dump,
1526
1527         .start = swap_start,
1528         .stop = swap_stop,
1529
1530         .kill = swap_kill,
1531
1532         .get_timeout = swap_get_timeout,
1533
1534         .serialize = swap_serialize,
1535         .deserialize_item = swap_deserialize_item,
1536
1537         .active_state = swap_active_state,
1538         .sub_state_to_string = swap_sub_state_to_string,
1539
1540         .check_gc = swap_check_gc,
1541
1542         .sigchld_event = swap_sigchld_event,
1543
1544         .reset_failed = swap_reset_failed,
1545
1546         .bus_interface = "org.freedesktop.systemd1.Swap",
1547         .bus_vtable = bus_swap_vtable,
1548         .bus_set_property = bus_swap_set_property,
1549         .bus_commit_properties = bus_swap_commit_properties,
1550
1551         .following = swap_following,
1552         .following_set = swap_following_set,
1553
1554         .enumerate = swap_enumerate,
1555         .shutdown = swap_shutdown,
1556
1557         .status_message_formats = {
1558                 .starting_stopping = {
1559                         [0] = "Activating swap %s...",
1560                         [1] = "Deactivating swap %s...",
1561                 },
1562                 .finished_start_job = {
1563                         [JOB_DONE]       = "Activated swap %s.",
1564                         [JOB_FAILED]     = "Failed to activate swap %s.",
1565                         [JOB_DEPENDENCY] = "Dependency failed for %s.",
1566                         [JOB_TIMEOUT]    = "Timed out activating swap %s.",
1567                 },
1568                 .finished_stop_job = {
1569                         [JOB_DONE]       = "Deactivated swap %s.",
1570                         [JOB_FAILED]     = "Failed deactivating swap %s.",
1571                         [JOB_TIMEOUT]    = "Timed out deactivating swap %s.",
1572                 },
1573         },
1574 };