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