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