chiark / gitweb /
exec: optionally apply cgroup attributes to the cgroups we create
[elogind.git] / src / 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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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
42 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
43         [SWAP_DEAD] = UNIT_INACTIVE,
44         [SWAP_ACTIVATING] = UNIT_ACTIVATING,
45         [SWAP_ACTIVE] = UNIT_ACTIVE,
46         [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
47         [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
48         [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
49         [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
50         [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
51         [SWAP_FAILED] = UNIT_FAILED
52 };
53
54 static void swap_unset_proc_swaps(Swap *s) {
55         Swap *first;
56
57         assert(s);
58
59         if (!s->parameters_proc_swaps.what)
60                 return;
61
62         /* Remove this unit from the chain of swaps which share the
63          * same kernel swap device. */
64
65         first = hashmap_get(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
66         LIST_REMOVE(Swap, same_proc_swaps, first, s);
67
68         if (first)
69                 hashmap_remove_and_replace(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first);
70         else
71                 hashmap_remove(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what);
72
73         free(s->parameters_proc_swaps.what);
74         s->parameters_proc_swaps.what = NULL;
75 }
76
77  static void swap_init(Unit *u) {
78         Swap *s = SWAP(u);
79
80         assert(s);
81         assert(s->meta.load_state == UNIT_STUB);
82
83         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
84
85         exec_context_init(&s->exec_context);
86         s->exec_context.std_output = EXEC_OUTPUT_KMSG;
87
88         s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
89
90         s->timer_watch.type = WATCH_INVALID;
91
92         s->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
93
94         s->meta.ignore_on_isolate = true;
95 }
96
97 static void swap_unwatch_control_pid(Swap *s) {
98         assert(s);
99
100         if (s->control_pid <= 0)
101                 return;
102
103         unit_unwatch_pid(UNIT(s), s->control_pid);
104         s->control_pid = 0;
105 }
106
107 static void swap_done(Unit *u) {
108         Swap *s = SWAP(u);
109
110         assert(s);
111
112         swap_unset_proc_swaps(s);
113
114         free(s->what);
115         s->what = NULL;
116
117         free(s->parameters_etc_fstab.what);
118         free(s->parameters_fragment.what);
119         s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL;
120
121         exec_context_done(&s->exec_context);
122         exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
123         s->control_command = NULL;
124
125         swap_unwatch_control_pid(s);
126
127         unit_unwatch_timer(u, &s->timer_watch);
128 }
129
130 int swap_add_one_mount_link(Swap *s, Mount *m) {
131          int r;
132
133         assert(s);
134         assert(m);
135
136         if (s->meta.load_state != UNIT_LOADED ||
137             m->meta.load_state != UNIT_LOADED)
138                 return 0;
139
140         if (is_device_path(s->what))
141                 return 0;
142
143         if (!path_startswith(s->what, m->where))
144                 return 0;
145
146         if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
147                 return r;
148
149         return 0;
150 }
151
152 static int swap_add_mount_links(Swap *s) {
153         Meta *other;
154         int r;
155
156         assert(s);
157
158         LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_MOUNT])
159                 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
160                         return r;
161
162         return 0;
163 }
164
165 static int swap_add_target_links(Swap *s) {
166         Unit *tu;
167         SwapParameters *p;
168         int r;
169
170         assert(s);
171
172         if (s->from_fragment)
173                 p = &s->parameters_fragment;
174         else if (s->from_etc_fstab)
175                 p = &s->parameters_etc_fstab;
176         else
177                 return 0;
178
179         if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
180                 return r;
181
182         if (!p->noauto &&
183             !p->nofail &&
184             (p->handle || s->meta.manager->swap_auto) &&
185             s->from_etc_fstab &&
186             s->meta.manager->running_as == MANAGER_SYSTEM)
187                 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
188                         return r;
189
190         return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
191 }
192
193 static int swap_add_device_links(Swap *s) {
194         SwapParameters *p;
195
196         assert(s);
197
198         if (!s->what)
199                 return 0;
200
201         if (s->from_fragment)
202                 p = &s->parameters_fragment;
203         else if (s->from_etc_fstab)
204                 p = &s->parameters_etc_fstab;
205         else
206                 return 0;
207
208         if (is_device_path(s->what))
209                 return unit_add_node_link(UNIT(s), s->what,
210                                           !p->noauto && p->nofail &&
211                                           s->meta.manager->running_as == MANAGER_SYSTEM);
212         else
213                 /* File based swap devices need to be ordered after
214                  * remount-rootfs.service, since they might need a
215                  * writable file system. */
216                 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true);
217 }
218
219 static int swap_add_default_dependencies(Swap *s) {
220         int r;
221
222         assert(s);
223
224         if (s->meta.manager->running_as == MANAGER_SYSTEM) {
225
226                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
227                         return r;
228         }
229
230         return 0;
231 }
232
233 static int swap_verify(Swap *s) {
234         bool b;
235         char *e;
236
237         if (s->meta.load_state != UNIT_LOADED)
238                   return 0;
239
240         if (!(e = unit_name_from_path(s->what, ".swap")))
241                   return -ENOMEM;
242
243         b = unit_has_name(UNIT(s), e);
244         free(e);
245
246         if (!b) {
247                 log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id);
248                 return -EINVAL;
249         }
250
251         if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
252                 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
253                 return -EINVAL;
254         }
255
256         return 0;
257 }
258
259 static int swap_load(Unit *u) {
260         int r;
261         Swap *s = SWAP(u);
262
263         assert(s);
264         assert(u->meta.load_state == UNIT_STUB);
265
266         /* Load a .swap file */
267         if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
268                 return r;
269
270         if (u->meta.load_state == UNIT_LOADED) {
271                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
272                         return r;
273
274                 if (s->meta.fragment_path)
275                         s->from_fragment = true;
276
277                 if (!s->what) {
278                         if (s->parameters_fragment.what)
279                                 s->what = strdup(s->parameters_fragment.what);
280                         else if (s->parameters_etc_fstab.what)
281                                 s->what = strdup(s->parameters_etc_fstab.what);
282                         else if (s->parameters_proc_swaps.what)
283                                 s->what = strdup(s->parameters_proc_swaps.what);
284                         else
285                                 s->what = unit_name_to_path(u->meta.id);
286
287                         if (!s->what)
288                                 return -ENOMEM;
289                 }
290
291                 path_kill_slashes(s->what);
292
293                 if (!s->meta.description)
294                         if ((r = unit_set_description(u, s->what)) < 0)
295                                 return r;
296
297                 if ((r = swap_add_device_links(s)) < 0)
298                         return r;
299
300                 if ((r = swap_add_mount_links(s)) < 0)
301                         return r;
302
303                 if ((r = swap_add_target_links(s)) < 0)
304                         return r;
305
306                 if ((r = unit_add_default_cgroups(u)) < 0)
307                         return r;
308
309                 if (s->meta.default_dependencies)
310                         if ((r = swap_add_default_dependencies(s)) < 0)
311                                 return r;
312         }
313
314         return swap_verify(s);
315 }
316
317 int swap_add_one(
318                 Manager *m,
319                 const char *what,
320                 const char *what_proc_swaps,
321                 int priority,
322                 bool noauto,
323                 bool nofail,
324                 bool handle,
325                 bool set_flags) {
326
327         Unit *u = NULL;
328         char *e = NULL, *wp = NULL;
329         bool delete = false;
330         int r;
331         SwapParameters *p;
332
333         assert(m);
334         assert(what);
335
336         if (!(e = unit_name_from_path(what, ".swap")))
337                 return -ENOMEM;
338
339         u = manager_get_unit(m, e);
340
341         if (what_proc_swaps &&
342             u &&
343             SWAP(u)->from_proc_swaps &&
344             !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
345                 return -EEXIST;
346
347         if (!u) {
348                 delete = true;
349
350                 if (!(u = unit_new(m))) {
351                         free(e);
352                         return -ENOMEM;
353                 }
354
355                 if ((r = unit_add_name(u, e)) < 0)
356                         goto fail;
357
358                 if (!(SWAP(u)->what = strdup(what))) {
359                         r = -ENOMEM;
360                         goto fail;
361                 }
362
363                 unit_add_to_load_queue(u);
364         } else
365                 delete = false;
366
367         if (what_proc_swaps) {
368                 Swap *first;
369
370                 p = &SWAP(u)->parameters_proc_swaps;
371
372                 if (!p->what) {
373                         if (!(wp = strdup(what_proc_swaps))) {
374                                 r = -ENOMEM;
375                                 goto fail;
376                         }
377
378                         if (!m->swaps_by_proc_swaps)
379                                 if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
380                                         r = -ENOMEM;
381                                         goto fail;
382                                 }
383
384                         free(p->what);
385                         p->what = wp;
386
387                         first = hashmap_get(m->swaps_by_proc_swaps, wp);
388                         LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
389
390                         if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
391                                 goto fail;
392                 }
393
394                 if (set_flags) {
395                         SWAP(u)->is_active = true;
396                         SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
397                 }
398
399                 SWAP(u)->from_proc_swaps = true;
400
401         } else {
402                 p = &SWAP(u)->parameters_etc_fstab;
403
404                 if (!(wp = strdup(what))) {
405                         r = -ENOMEM;
406                         goto fail;
407                 }
408
409                 free(p->what);
410                 p->what = wp;
411
412                 SWAP(u)->from_etc_fstab = true;
413         }
414
415         p->priority = priority;
416         p->noauto = noauto;
417         p->nofail = nofail;
418         p->handle = handle;
419
420         unit_add_to_dbus_queue(u);
421
422         free(e);
423
424         return 0;
425
426 fail:
427         log_warning("Failed to load swap unit: %s", strerror(-r));
428
429         free(wp);
430         free(e);
431
432         if (delete && u)
433                 unit_free(u);
434
435         return r;
436 }
437
438 static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) {
439         struct stat st;
440         int r = 0, k;
441
442         assert(m);
443
444         if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) {
445                 struct udev_device *d;
446                 const char *dn;
447                 struct udev_list_entry *item = NULL, *first = NULL;
448
449                 /* So this is a proper swap device. Create swap units
450                  * for all names this swap device is known under */
451
452                 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
453                         return -ENOMEM;
454
455                 if ((dn = udev_device_get_devnode(d)))
456                         r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
457
458                 /* Add additional units for all symlinks */
459                 first = udev_device_get_devlinks_list_entry(d);
460                 udev_list_entry_foreach(item, first) {
461                         const char *p;
462
463                         /* Don't bother with the /dev/block links */
464                         p = udev_list_entry_get_name(item);
465
466                         if (path_startswith(p, "/dev/block/"))
467                                 continue;
468
469                         if (stat(p, &st) >= 0)
470                                 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
471                                         continue;
472
473                         if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
474                                 r = k;
475                 }
476
477                 udev_device_unref(d);
478         }
479
480         if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
481                 r = k;
482
483         return r;
484 }
485
486 static void swap_set_state(Swap *s, SwapState state) {
487         SwapState old_state;
488
489         assert(s);
490
491         old_state = s->state;
492         s->state = state;
493
494         if (state != SWAP_ACTIVATING &&
495             state != SWAP_ACTIVATING_SIGTERM &&
496             state != SWAP_ACTIVATING_SIGKILL &&
497             state != SWAP_DEACTIVATING &&
498             state != SWAP_DEACTIVATING_SIGTERM &&
499             state != SWAP_DEACTIVATING_SIGKILL) {
500                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
501                 swap_unwatch_control_pid(s);
502                 s->control_command = NULL;
503                 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
504         }
505
506         if (state != old_state)
507                 log_debug("%s changed %s -> %s",
508                           s->meta.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
530                 if (new_state == SWAP_ACTIVATING ||
531                     new_state == SWAP_ACTIVATING_SIGTERM ||
532                     new_state == SWAP_ACTIVATING_SIGKILL ||
533                     new_state == SWAP_DEACTIVATING ||
534                     new_state == SWAP_DEACTIVATING_SIGTERM ||
535                     new_state == SWAP_DEACTIVATING_SIGKILL) {
536
537                         if (s->control_pid <= 0)
538                                 return -EBADMSG;
539
540                         if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
541                                 return r;
542
543                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
544                                 return r;
545                 }
546
547                 swap_set_state(s, new_state);
548         }
549
550         return 0;
551 }
552
553 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
554         Swap *s = SWAP(u);
555         SwapParameters *p;
556
557         assert(s);
558         assert(f);
559
560         if (s->from_proc_swaps)
561                 p = &s->parameters_proc_swaps;
562         else if (s->from_fragment)
563                 p = &s->parameters_fragment;
564         else
565                 p = &s->parameters_etc_fstab;
566
567         fprintf(f,
568                 "%sSwap State: %s\n"
569                 "%sWhat: %s\n"
570                 "%sPriority: %i\n"
571                 "%sNoAuto: %s\n"
572                 "%sNoFail: %s\n"
573                 "%sHandle: %s\n"
574                 "%sFrom /etc/fstab: %s\n"
575                 "%sFrom /proc/swaps: %s\n"
576                 "%sFrom fragment: %s\n",
577                 prefix, swap_state_to_string(s->state),
578                 prefix, s->what,
579                 prefix, p->priority,
580                 prefix, yes_no(p->noauto),
581                 prefix, yes_no(p->nofail),
582                 prefix, yes_no(p->handle),
583                 prefix, yes_no(s->from_etc_fstab),
584                 prefix, yes_no(s->from_proc_swaps),
585                 prefix, yes_no(s->from_fragment));
586
587         if (s->control_pid > 0)
588                 fprintf(f,
589                         "%sControl PID: %lu\n",
590                         prefix, (unsigned long) s->control_pid);
591
592         exec_context_dump(&s->exec_context, f, prefix);
593 }
594
595 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
596         pid_t pid;
597         int r;
598
599         assert(s);
600         assert(c);
601         assert(_pid);
602
603         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
604                 goto fail;
605
606         if ((r = exec_spawn(c,
607                             NULL,
608                             &s->exec_context,
609                             NULL, 0,
610                             s->meta.manager->environment,
611                             true,
612                             true,
613                             true,
614                             s->meta.manager->confirm_spawn,
615                             s->meta.cgroup_bondings,
616                             s->meta.cgroup_attributes,
617                             &pid)) < 0)
618                 goto fail;
619
620         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
621                 /* FIXME: we need to do something here */
622                 goto fail;
623
624         *_pid = pid;
625
626         return 0;
627
628 fail:
629         unit_unwatch_timer(UNIT(s), &s->timer_watch);
630
631         return r;
632 }
633
634 static void swap_enter_dead(Swap *s, bool success) {
635         assert(s);
636
637         if (!success)
638                 s->failure = true;
639
640         swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD);
641 }
642
643 static void swap_enter_active(Swap *s, bool success) {
644         assert(s);
645
646         if (!success)
647                 s->failure = true;
648
649         swap_set_state(s, SWAP_ACTIVE);
650 }
651
652 static void swap_enter_signal(Swap *s, SwapState state, bool success) {
653         int r;
654         Set *pid_set = NULL;
655         bool wait_for_exit = false;
656
657         assert(s);
658
659         if (!success)
660                 s->failure = true;
661
662         if (s->exec_context.kill_mode != KILL_NONE) {
663                 int sig = (state == SWAP_ACTIVATING_SIGTERM ||
664                            state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
665
666                 if (s->control_pid > 0) {
667                         if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
668
669                                 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
670                         else
671                                 wait_for_exit = true;
672                 }
673
674                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
675
676                         if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
677                                 r = -ENOMEM;
678                                 goto fail;
679                         }
680
681                         /* Exclude the control pid from being killed via the cgroup */
682                         if (s->control_pid > 0)
683                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
684                                         goto fail;
685
686                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
687                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
688                                         log_warning("Failed to kill control group: %s", strerror(-r));
689                         } else if (r > 0)
690                                 wait_for_exit = true;
691
692                         set_free(pid_set);
693                         pid_set = NULL;
694                 }
695         }
696
697         if (wait_for_exit) {
698                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
699                         goto fail;
700
701                 swap_set_state(s, state);
702         } else
703                 swap_enter_dead(s, true);
704
705         return;
706
707 fail:
708         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
709
710         swap_enter_dead(s, false);
711
712         if (pid_set)
713                 set_free(pid_set);
714 }
715
716 static void swap_enter_activating(Swap *s) {
717         int r, priority;
718
719         assert(s);
720
721         s->control_command_id = SWAP_EXEC_ACTIVATE;
722         s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
723
724         if (s->from_fragment)
725                 priority = s->parameters_fragment.priority;
726         else if (s->from_etc_fstab)
727                 priority = s->parameters_etc_fstab.priority;
728         else
729                 priority = -1;
730
731         if (priority >= 0) {
732                 char p[LINE_MAX];
733
734                 snprintf(p, sizeof(p), "%i", priority);
735                 char_array_0(p);
736
737                 r = exec_command_set(
738                                 s->control_command,
739                                 "/sbin/swapon",
740                                 "-p",
741                                 p,
742                                 s->what,
743                                 NULL);
744         } else
745                 r = exec_command_set(
746                                 s->control_command,
747                                 "/sbin/swapon",
748                                 s->what,
749                                 NULL);
750
751         if (r < 0)
752                 goto fail;
753
754         swap_unwatch_control_pid(s);
755
756         if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
757                 goto fail;
758
759         swap_set_state(s, SWAP_ACTIVATING);
760
761         return;
762
763 fail:
764         log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r));
765         swap_enter_dead(s, false);
766 }
767
768 static void swap_enter_deactivating(Swap *s, bool success) {
769         int r;
770
771         assert(s);
772
773         if (!success)
774                 s->failure = true;
775
776         s->control_command_id = SWAP_EXEC_DEACTIVATE;
777         s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
778
779         if ((r = exec_command_set(
780                              s->control_command,
781                              "/sbin/swapoff",
782                              s->what,
783                              NULL)) < 0)
784                 goto fail;
785
786         swap_unwatch_control_pid(s);
787
788         if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0)
789                 goto fail;
790
791         swap_set_state(s, SWAP_DEACTIVATING);
792
793         return;
794
795 fail:
796         log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r));
797         swap_enter_active(s, false);
798 }
799
800 static int swap_start(Unit *u) {
801         Swap *s = SWAP(u);
802
803         assert(s);
804
805         /* We cannot fulfill this request right now, try again later
806          * please! */
807
808         if (s->state == SWAP_DEACTIVATING ||
809             s->state == SWAP_DEACTIVATING_SIGTERM ||
810             s->state == SWAP_DEACTIVATING_SIGKILL ||
811             s->state == SWAP_ACTIVATING_SIGTERM ||
812             s->state == SWAP_ACTIVATING_SIGKILL)
813                 return -EAGAIN;
814
815         if (s->state == SWAP_ACTIVATING)
816                 return 0;
817
818         assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
819
820         s->failure = false;
821         swap_enter_activating(s);
822         return 0;
823 }
824
825 static int swap_stop(Unit *u) {
826         Swap *s = SWAP(u);
827
828         assert(s);
829
830         if (s->state == SWAP_DEACTIVATING ||
831             s->state == SWAP_DEACTIVATING_SIGTERM ||
832             s->state == SWAP_DEACTIVATING_SIGKILL ||
833             s->state == SWAP_ACTIVATING_SIGTERM ||
834             s->state == SWAP_ACTIVATING_SIGKILL)
835                 return 0;
836
837         assert(s->state == SWAP_ACTIVATING ||
838                s->state == SWAP_ACTIVE);
839
840         swap_enter_deactivating(s, true);
841         return 0;
842 }
843
844 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
845         Swap *s = SWAP(u);
846
847         assert(s);
848         assert(f);
849         assert(fds);
850
851         unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
852         unit_serialize_item(u, f, "failure", yes_no(s->failure));
853
854         if (s->control_pid > 0)
855                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
856
857         if (s->control_command_id >= 0)
858                 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
859
860         return 0;
861 }
862
863 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
864         Swap *s = SWAP(u);
865
866         assert(s);
867         assert(fds);
868
869         if (streq(key, "state")) {
870                 SwapState state;
871
872                 if ((state = swap_state_from_string(value)) < 0)
873                         log_debug("Failed to parse state value %s", value);
874                 else
875                         s->deserialized_state = state;
876         } else if (streq(key, "failure")) {
877                 int b;
878
879                 if ((b = parse_boolean(value)) < 0)
880                         log_debug("Failed to parse failure value %s", value);
881                 else
882                         s->failure = b || s->failure;
883
884         } else if (streq(key, "control-pid")) {
885                 pid_t pid;
886
887                 if (parse_pid(value, &pid) < 0)
888                         log_debug("Failed to parse control-pid value %s", value);
889                 else
890                         s->control_pid = pid;
891
892         } else if (streq(key, "control-command")) {
893                 SwapExecCommand id;
894
895                 if ((id = swap_exec_command_from_string(value)) < 0)
896                         log_debug("Failed to parse exec-command value %s", value);
897                 else {
898                         s->control_command_id = id;
899                         s->control_command = s->exec_command + id;
900                 }
901
902         } else
903                 log_debug("Unknown serialization key '%s'", key);
904
905         return 0;
906 }
907
908 static UnitActiveState swap_active_state(Unit *u) {
909         assert(u);
910
911         return state_translation_table[SWAP(u)->state];
912 }
913
914 static const char *swap_sub_state_to_string(Unit *u) {
915         assert(u);
916
917         return swap_state_to_string(SWAP(u)->state);
918 }
919
920 static bool swap_check_gc(Unit *u) {
921         Swap *s = SWAP(u);
922
923         assert(s);
924
925         return s->from_etc_fstab || s->from_proc_swaps;
926 }
927
928 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
929         Swap *s = SWAP(u);
930         bool success;
931
932         assert(s);
933         assert(pid >= 0);
934
935         if (pid != s->control_pid)
936                 return;
937
938         s->control_pid = 0;
939
940         success = is_clean_exit(code, status);
941         s->failure = s->failure || !success;
942
943         if (s->control_command) {
944                 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
945                 s->control_command = NULL;
946                 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
947         }
948
949         log_full(success ? LOG_DEBUG : LOG_NOTICE,
950                  "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
951
952         switch (s->state) {
953
954         case SWAP_ACTIVATING:
955         case SWAP_ACTIVATING_SIGTERM:
956         case SWAP_ACTIVATING_SIGKILL:
957
958                 if (success)
959                         swap_enter_active(s, true);
960                 else
961                         swap_enter_dead(s, false);
962                 break;
963
964         case SWAP_DEACTIVATING:
965         case SWAP_DEACTIVATING_SIGKILL:
966         case SWAP_DEACTIVATING_SIGTERM:
967
968                 if (success)
969                         swap_enter_dead(s, true);
970                 else
971                         swap_enter_dead(s, false);
972                 break;
973
974         default:
975                 assert_not_reached("Uh, control process died at wrong time.");
976         }
977
978         /* Notify clients about changed exit status */
979         unit_add_to_dbus_queue(u);
980
981         /* Request a reload of /proc/swaps, so that following units
982          * can follow our state change */
983         u->meta.manager->request_reload = true;
984 }
985
986 static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
987         Swap *s = SWAP(u);
988
989         assert(s);
990         assert(elapsed == 1);
991         assert(w == &s->timer_watch);
992
993         switch (s->state) {
994
995         case SWAP_ACTIVATING:
996                 log_warning("%s activation timed out. Stopping.", u->meta.id);
997                 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false);
998                 break;
999
1000         case SWAP_DEACTIVATING:
1001                 log_warning("%s deactivation timed out. Stopping.", u->meta.id);
1002                 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false);
1003                 break;
1004
1005         case SWAP_ACTIVATING_SIGTERM:
1006                 if (s->exec_context.send_sigkill) {
1007                         log_warning("%s activation timed out. Killing.", u->meta.id);
1008                         swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
1009                 } else {
1010                         log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1011                         swap_enter_dead(s, false);
1012                 }
1013                 break;
1014
1015         case SWAP_DEACTIVATING_SIGTERM:
1016                 if (s->exec_context.send_sigkill) {
1017                         log_warning("%s deactivation timed out. Killing.", u->meta.id);
1018                         swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
1019                 } else {
1020                         log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
1021                         swap_enter_dead(s, false);
1022                 }
1023                 break;
1024
1025         case SWAP_ACTIVATING_SIGKILL:
1026         case SWAP_DEACTIVATING_SIGKILL:
1027                 log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id);
1028                 swap_enter_dead(s, false);
1029                 break;
1030
1031         default:
1032                 assert_not_reached("Timeout at wrong time.");
1033         }
1034 }
1035
1036 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1037         unsigned i;
1038         int r = 0;
1039
1040         assert(m);
1041
1042         rewind(m->proc_swaps);
1043
1044         (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1045
1046         for (i = 1;; i++) {
1047                 char *dev = NULL, *d;
1048                 int prio = 0, k;
1049
1050                 if ((k = fscanf(m->proc_swaps,
1051                                 "%ms "  /* device/file */
1052                                 "%*s "  /* type of swap */
1053                                 "%*s "  /* swap size */
1054                                 "%*s "  /* used */
1055                                 "%i\n", /* priority */
1056                                 &dev, &prio)) != 2) {
1057
1058                         if (k == EOF)
1059                                 break;
1060
1061                         log_warning("Failed to parse /proc/swaps:%u.", i);
1062                         free(dev);
1063                         continue;
1064                 }
1065
1066                 d = cunescape(dev);
1067                 free(dev);
1068
1069                 if (!d)
1070                         return -ENOMEM;
1071
1072                 k = swap_process_new_swap(m, d, prio, set_flags);
1073                 free(d);
1074
1075                 if (k < 0)
1076                         r = k;
1077         }
1078
1079         return r;
1080 }
1081
1082 int swap_dispatch_reload(Manager *m) {
1083         /* This function should go as soon as the kernel properly notifies us */
1084
1085         if (_likely_(!m->request_reload))
1086                 return 0;
1087
1088         m->request_reload = false;
1089
1090         return swap_fd_event(m, EPOLLPRI);
1091 }
1092
1093 int swap_fd_event(Manager *m, int events) {
1094         Meta *meta;
1095         int r;
1096
1097         assert(m);
1098         assert(events & EPOLLPRI);
1099
1100         if ((r = swap_load_proc_swaps(m, true)) < 0) {
1101                 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
1102
1103                 /* Reset flags, just in case, for late calls */
1104                 LIST_FOREACH(units_by_type, meta, m->units_by_type[UNIT_SWAP]) {
1105                         Swap *swap = (Swap*) meta;
1106
1107                         swap->is_active = swap->just_activated = false;
1108                 }
1109
1110                 return 0;
1111         }
1112
1113         manager_dispatch_load_queue(m);
1114
1115         LIST_FOREACH(units_by_type, meta, m->units_by_type[UNIT_SWAP]) {
1116                 Swap *swap = (Swap*) meta;
1117
1118                 if (!swap->is_active) {
1119                         /* This has just been deactivated */
1120
1121                         swap->from_proc_swaps = false;
1122                         swap_unset_proc_swaps(swap);
1123
1124                         switch (swap->state) {
1125
1126                         case SWAP_ACTIVE:
1127                                 swap_enter_dead(swap, true);
1128                                 break;
1129
1130                         default:
1131                                 swap_set_state(swap, swap->state);
1132                                 break;
1133                         }
1134
1135                 } else if (swap->just_activated) {
1136
1137                         /* New swap entry */
1138
1139                         switch (swap->state) {
1140
1141                         case SWAP_DEAD:
1142                         case SWAP_FAILED:
1143                                 swap_enter_active(swap, true);
1144                                 break;
1145
1146                         default:
1147                                 /* Nothing really changed, but let's
1148                                  * issue an notification call
1149                                  * nonetheless, in case somebody is
1150                                  * waiting for this. */
1151                                 swap_set_state(swap, swap->state);
1152                                 break;
1153                         }
1154                 }
1155
1156                 /* Reset the flags for later calls */
1157                 swap->is_active = swap->just_activated = false;
1158         }
1159
1160         return 1;
1161 }
1162
1163 static Unit *swap_following(Unit *u) {
1164         Swap *s = SWAP(u);
1165         Swap *other, *first = NULL;
1166
1167         assert(s);
1168
1169         if (streq_ptr(s->what, s->parameters_proc_swaps.what))
1170                 return NULL;
1171
1172         /* Make everybody follow the unit that's named after the swap
1173          * device in the kernel */
1174
1175         LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1176                 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1177                         return UNIT(other);
1178
1179         LIST_FOREACH_BEFORE(same_proc_swaps, other, s) {
1180                 if (streq_ptr(other->what, other->parameters_proc_swaps.what))
1181                         return UNIT(other);
1182
1183                 first = other;
1184         }
1185
1186         return UNIT(first);
1187 }
1188
1189 static int swap_following_set(Unit *u, Set **_set) {
1190         Swap *s = SWAP(u);
1191         Swap *other;
1192         Set *set;
1193         int r;
1194
1195         assert(s);
1196         assert(_set);
1197
1198         if (LIST_JUST_US(same_proc_swaps, s)) {
1199                 *_set = NULL;
1200                 return 0;
1201         }
1202
1203         if (!(set = set_new(NULL, NULL)))
1204                 return -ENOMEM;
1205
1206         LIST_FOREACH_AFTER(same_proc_swaps, other, s)
1207                 if ((r = set_put(set, other)) < 0)
1208                         goto fail;
1209
1210         LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
1211                 if ((r = set_put(set, other)) < 0)
1212                         goto fail;
1213
1214         *_set = set;
1215         return 1;
1216
1217 fail:
1218         set_free(set);
1219         return r;
1220 }
1221
1222 static void swap_shutdown(Manager *m) {
1223         assert(m);
1224
1225         if (m->proc_swaps) {
1226                 fclose(m->proc_swaps);
1227                 m->proc_swaps = NULL;
1228         }
1229
1230         hashmap_free(m->swaps_by_proc_swaps);
1231         m->swaps_by_proc_swaps = NULL;
1232 }
1233
1234 static int swap_enumerate(Manager *m) {
1235         int r;
1236         struct epoll_event ev;
1237         assert(m);
1238
1239         if (!m->proc_swaps) {
1240                 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
1241                         return (errno == ENOENT) ? 0 : -errno;
1242
1243                 m->swap_watch.type = WATCH_SWAP;
1244                 m->swap_watch.fd = fileno(m->proc_swaps);
1245
1246                 zero(ev);
1247                 ev.events = EPOLLPRI;
1248                 ev.data.ptr = &m->swap_watch;
1249
1250                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0)
1251                         return -errno;
1252         }
1253
1254         /* We rely on mount.c to load /etc/fstab for us */
1255
1256         if ((r = swap_load_proc_swaps(m, false)) < 0)
1257                 swap_shutdown(m);
1258
1259         return r;
1260 }
1261
1262 static void swap_reset_failed(Unit *u) {
1263         Swap *s = SWAP(u);
1264
1265         assert(s);
1266
1267         if (s->state == SWAP_FAILED)
1268                 swap_set_state(s, SWAP_DEAD);
1269
1270         s->failure = false;
1271 }
1272
1273 static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
1274         Swap *s = SWAP(u);
1275         int r = 0;
1276         Set *pid_set = NULL;
1277
1278         assert(s);
1279
1280         if (who == KILL_MAIN) {
1281                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
1282                 return -ESRCH;
1283         }
1284
1285         if (s->control_pid <= 0 && who == KILL_CONTROL) {
1286                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
1287                 return -ESRCH;
1288         }
1289
1290         if (who == KILL_CONTROL || who == KILL_ALL)
1291                 if (s->control_pid > 0)
1292                         if (kill(s->control_pid, signo) < 0)
1293                                 r = -errno;
1294
1295         if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
1296                 int q;
1297
1298                 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
1299                         return -ENOMEM;
1300
1301                 /* Exclude the control pid from being killed via the cgroup */
1302                 if (s->control_pid > 0)
1303                         if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
1304                                 r = q;
1305                                 goto finish;
1306                         }
1307
1308                 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
1309                         if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
1310                                 r = q;
1311         }
1312
1313 finish:
1314         if (pid_set)
1315                 set_free(pid_set);
1316
1317         return r;
1318 }
1319
1320 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
1321         [SWAP_DEAD] = "dead",
1322         [SWAP_ACTIVATING] = "activating",
1323         [SWAP_ACTIVE] = "active",
1324         [SWAP_DEACTIVATING] = "deactivating",
1325         [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1326         [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1327         [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1328         [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1329         [SWAP_FAILED] = "failed"
1330 };
1331
1332 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1333
1334 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1335         [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1336         [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1337 };
1338
1339 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1340
1341 const UnitVTable swap_vtable = {
1342         .suffix = ".swap",
1343         .sections =
1344                 "Unit\0"
1345                 "Swap\0"
1346                 "Install\0",
1347
1348         .no_alias = true,
1349         .no_instances = true,
1350         .show_status = true,
1351
1352         .init = swap_init,
1353         .load = swap_load,
1354         .done = swap_done,
1355
1356         .coldplug = swap_coldplug,
1357
1358         .dump = swap_dump,
1359
1360         .start = swap_start,
1361         .stop = swap_stop,
1362
1363         .kill = swap_kill,
1364
1365         .serialize = swap_serialize,
1366         .deserialize_item = swap_deserialize_item,
1367
1368         .active_state = swap_active_state,
1369         .sub_state_to_string = swap_sub_state_to_string,
1370
1371         .check_gc = swap_check_gc,
1372
1373         .sigchld_event = swap_sigchld_event,
1374         .timer_event = swap_timer_event,
1375
1376         .reset_failed = swap_reset_failed,
1377
1378         .bus_interface = "org.freedesktop.systemd1.Swap",
1379         .bus_message_handler = bus_swap_message_handler,
1380         .bus_invalidating_properties =  bus_swap_invalidating_properties,
1381
1382         .following = swap_following,
1383         .following_set = swap_following_set,
1384
1385         .enumerate = swap_enumerate,
1386         .shutdown = swap_shutdown
1387 };