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