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