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