]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/swap.c
Merge pull request #3111 from poettering/nspawn-remove-veth
[thirdparty/systemd.git] / src / core / swap.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <sys/epoll.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "libudev.h"
26
27 #include "alloc-util.h"
28 #include "dbus-swap.h"
29 #include "escape.h"
30 #include "exit-status.h"
31 #include "fd-util.h"
32 #include "formats-util.h"
33 #include "fstab-util.h"
34 #include "parse-util.h"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "special.h"
38 #include "string-table.h"
39 #include "string-util.h"
40 #include "swap.h"
41 #include "udev-util.h"
42 #include "unit-name.h"
43 #include "unit.h"
44 #include "virt.h"
45
46 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
47 [SWAP_DEAD] = UNIT_INACTIVE,
48 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
49 [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
50 [SWAP_ACTIVE] = UNIT_ACTIVE,
51 [SWAP_DEACTIVATING] = UNIT_DEACTIVATING,
52 [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
53 [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
54 [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING,
55 [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING,
56 [SWAP_FAILED] = UNIT_FAILED
57 };
58
59 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
60 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
61
62 static void swap_unset_proc_swaps(Swap *s) {
63 assert(s);
64
65 if (!s->from_proc_swaps)
66 return;
67
68 s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what);
69
70 s->from_proc_swaps = false;
71 }
72
73 static int swap_set_devnode(Swap *s, const char *devnode) {
74 Hashmap *swaps;
75 Swap *first;
76 int r;
77
78 assert(s);
79
80 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &string_hash_ops);
81 if (r < 0)
82 return r;
83
84 swaps = UNIT(s)->manager->swaps_by_devnode;
85
86 if (s->devnode) {
87 first = hashmap_get(swaps, s->devnode);
88
89 LIST_REMOVE(same_devnode, first, s);
90 if (first)
91 hashmap_replace(swaps, first->devnode, first);
92 else
93 hashmap_remove(swaps, s->devnode);
94
95 s->devnode = mfree(s->devnode);
96 }
97
98 if (devnode) {
99 s->devnode = strdup(devnode);
100 if (!s->devnode)
101 return -ENOMEM;
102
103 first = hashmap_get(swaps, s->devnode);
104 LIST_PREPEND(same_devnode, first, s);
105
106 return hashmap_replace(swaps, first->devnode, first);
107 }
108
109 return 0;
110 }
111
112 static void swap_init(Unit *u) {
113 Swap *s = SWAP(u);
114
115 assert(s);
116 assert(UNIT(s)->load_state == UNIT_STUB);
117
118 s->timeout_usec = u->manager->default_timeout_start_usec;
119
120 s->exec_context.std_output = u->manager->default_std_output;
121 s->exec_context.std_error = u->manager->default_std_error;
122
123 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
124
125 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
126
127 u->ignore_on_isolate = true;
128 }
129
130 static void swap_unwatch_control_pid(Swap *s) {
131 assert(s);
132
133 if (s->control_pid <= 0)
134 return;
135
136 unit_unwatch_pid(UNIT(s), s->control_pid);
137 s->control_pid = 0;
138 }
139
140 static void swap_done(Unit *u) {
141 Swap *s = SWAP(u);
142
143 assert(s);
144
145 swap_unset_proc_swaps(s);
146 swap_set_devnode(s, NULL);
147
148 s->what = mfree(s->what);
149 s->parameters_fragment.what = mfree(s->parameters_fragment.what);
150 s->parameters_fragment.options = mfree(s->parameters_fragment.options);
151
152 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
153 exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
154 s->control_command = NULL;
155
156 swap_unwatch_control_pid(s);
157
158 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
159 }
160
161 static int swap_arm_timer(Swap *s, usec_t usec) {
162 int r;
163
164 assert(s);
165
166 if (s->timer_event_source) {
167 r = sd_event_source_set_time(s->timer_event_source, usec);
168 if (r < 0)
169 return r;
170
171 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
172 }
173
174 if (usec == USEC_INFINITY)
175 return 0;
176
177 r = sd_event_add_time(
178 UNIT(s)->manager->event,
179 &s->timer_event_source,
180 CLOCK_MONOTONIC,
181 usec, 0,
182 swap_dispatch_timer, s);
183 if (r < 0)
184 return r;
185
186 (void) sd_event_source_set_description(s->timer_event_source, "swap-timer");
187
188 return 0;
189 }
190
191 static int swap_add_device_links(Swap *s) {
192 assert(s);
193
194 if (!s->what)
195 return 0;
196
197 if (!s->from_fragment)
198 return 0;
199
200 if (is_device_path(s->what))
201 return unit_add_node_link(UNIT(s), s->what, MANAGER_IS_SYSTEM(UNIT(s)->manager), UNIT_BINDS_TO);
202 else
203 /* File based swap devices need to be ordered after
204 * systemd-remount-fs.service, since they might need a
205 * writable file system. */
206 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
207 }
208
209 static int swap_add_default_dependencies(Swap *s) {
210 int r;
211
212 assert(s);
213
214 if (!UNIT(s)->default_dependencies)
215 return 0;
216
217 if (!MANAGER_IS_SYSTEM(UNIT(s)->manager))
218 return 0;
219
220 if (detect_container() > 0)
221 return 0;
222
223 /* swap units generated for the swap dev links are missing the
224 * ordering dep against the swap target. */
225 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SWAP_TARGET, NULL, true);
226 if (r < 0)
227 return r;
228
229 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
230 }
231
232 static int swap_verify(Swap *s) {
233 _cleanup_free_ char *e = NULL;
234 int r;
235
236 if (UNIT(s)->load_state != UNIT_LOADED)
237 return 0;
238
239 r = unit_name_from_path(s->what, ".swap", &e);
240 if (r < 0)
241 return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m");
242
243 if (!unit_has_name(UNIT(s), e)) {
244 log_unit_error(UNIT(s), "Value of What= and unit name do not match, not loading.");
245 return -EINVAL;
246 }
247
248 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
249 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.");
250 return -EINVAL;
251 }
252
253 return 0;
254 }
255
256 static int swap_load_devnode(Swap *s) {
257 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
258 struct stat st;
259 const char *p;
260
261 assert(s);
262
263 if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode))
264 return 0;
265
266 d = udev_device_new_from_devnum(UNIT(s)->manager->udev, 'b', st.st_rdev);
267 if (!d)
268 return 0;
269
270 p = udev_device_get_devnode(d);
271 if (!p)
272 return 0;
273
274 return swap_set_devnode(s, p);
275 }
276
277 static int swap_load(Unit *u) {
278 int r;
279 Swap *s = SWAP(u);
280
281 assert(s);
282 assert(u->load_state == UNIT_STUB);
283
284 /* Load a .swap file */
285 r = unit_load_fragment_and_dropin_optional(u);
286 if (r < 0)
287 return r;
288
289 if (u->load_state == UNIT_LOADED) {
290
291 if (UNIT(s)->fragment_path)
292 s->from_fragment = true;
293
294 if (!s->what) {
295 if (s->parameters_fragment.what)
296 s->what = strdup(s->parameters_fragment.what);
297 else if (s->parameters_proc_swaps.what)
298 s->what = strdup(s->parameters_proc_swaps.what);
299 else {
300 r = unit_name_to_path(u->id, &s->what);
301 if (r < 0)
302 return r;
303 }
304
305 if (!s->what)
306 return -ENOMEM;
307 }
308
309 path_kill_slashes(s->what);
310
311 if (!UNIT(s)->description) {
312 r = unit_set_description(u, s->what);
313 if (r < 0)
314 return r;
315 }
316
317 r = unit_require_mounts_for(UNIT(s), s->what);
318 if (r < 0)
319 return r;
320
321 r = swap_add_device_links(s);
322 if (r < 0)
323 return r;
324
325 r = swap_load_devnode(s);
326 if (r < 0)
327 return r;
328
329 r = unit_patch_contexts(u);
330 if (r < 0)
331 return r;
332
333 r = unit_add_exec_dependencies(u, &s->exec_context);
334 if (r < 0)
335 return r;
336
337 r = unit_set_default_slice(u);
338 if (r < 0)
339 return r;
340
341 r = swap_add_default_dependencies(s);
342 if (r < 0)
343 return r;
344 }
345
346 return swap_verify(s);
347 }
348
349 static int swap_setup_unit(
350 Manager *m,
351 const char *what,
352 const char *what_proc_swaps,
353 int priority,
354 bool set_flags) {
355
356 _cleanup_free_ char *e = NULL;
357 bool delete = false;
358 Unit *u = NULL;
359 int r;
360 SwapParameters *p;
361
362 assert(m);
363 assert(what);
364 assert(what_proc_swaps);
365
366 r = unit_name_from_path(what, ".swap", &e);
367 if (r < 0)
368 return log_unit_error_errno(u, r, "Failed to generate unit name from path: %m");
369
370 u = manager_get_unit(m, e);
371
372 if (u &&
373 SWAP(u)->from_proc_swaps &&
374 !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps)) {
375 log_error("Swap %s appeared twice with different device paths %s and %s", e, SWAP(u)->parameters_proc_swaps.what, what_proc_swaps);
376 return -EEXIST;
377 }
378
379 if (!u) {
380 delete = true;
381
382 u = unit_new(m, sizeof(Swap));
383 if (!u)
384 return log_oom();
385
386 r = unit_add_name(u, e);
387 if (r < 0)
388 goto fail;
389
390 SWAP(u)->what = strdup(what);
391 if (!SWAP(u)->what) {
392 r = -ENOMEM;
393 goto fail;
394 }
395
396 unit_add_to_load_queue(u);
397 } else
398 delete = false;
399
400 p = &SWAP(u)->parameters_proc_swaps;
401
402 if (!p->what) {
403 p->what = strdup(what_proc_swaps);
404 if (!p->what) {
405 r = -ENOMEM;
406 goto fail;
407 }
408 }
409
410 if (set_flags) {
411 SWAP(u)->is_active = true;
412 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
413 }
414
415 SWAP(u)->from_proc_swaps = true;
416
417 p->priority = priority;
418
419 unit_add_to_dbus_queue(u);
420 return 0;
421
422 fail:
423 log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
424
425 if (delete && u)
426 unit_free(u);
427
428 return r;
429 }
430
431 static int swap_process_new(Manager *m, const char *device, int prio, bool set_flags) {
432 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
433 struct udev_list_entry *item = NULL, *first = NULL;
434 const char *dn;
435 struct stat st;
436 int r;
437
438 assert(m);
439
440 r = swap_setup_unit(m, device, device, prio, set_flags);
441 if (r < 0)
442 return r;
443
444 /* If this is a block device, then let's add duplicates for
445 * all other names of this block device */
446 if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode))
447 return 0;
448
449 d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev);
450 if (!d)
451 return 0;
452
453 /* Add the main device node */
454 dn = udev_device_get_devnode(d);
455 if (dn && !streq(dn, device))
456 swap_setup_unit(m, dn, device, prio, set_flags);
457
458 /* Add additional units for all symlinks */
459 first = udev_device_get_devlinks_list_entry(d);
460 udev_list_entry_foreach(item, first) {
461 const char *p;
462
463 /* Don't bother with the /dev/block links */
464 p = udev_list_entry_get_name(item);
465
466 if (streq(p, device))
467 continue;
468
469 if (path_startswith(p, "/dev/block/"))
470 continue;
471
472 if (stat(p, &st) >= 0)
473 if (!S_ISBLK(st.st_mode) ||
474 st.st_rdev != udev_device_get_devnum(d))
475 continue;
476
477 swap_setup_unit(m, p, device, prio, set_flags);
478 }
479
480 return r;
481 }
482
483 static void swap_set_state(Swap *s, SwapState state) {
484 SwapState old_state;
485 Swap *other;
486
487 assert(s);
488
489 old_state = s->state;
490 s->state = state;
491
492 if (state != SWAP_ACTIVATING &&
493 state != SWAP_ACTIVATING_SIGTERM &&
494 state != SWAP_ACTIVATING_SIGKILL &&
495 state != SWAP_ACTIVATING_DONE &&
496 state != SWAP_DEACTIVATING &&
497 state != SWAP_DEACTIVATING_SIGTERM &&
498 state != SWAP_DEACTIVATING_SIGKILL) {
499 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
500 swap_unwatch_control_pid(s);
501 s->control_command = NULL;
502 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
503 }
504
505 if (state != old_state)
506 log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state));
507
508 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
509
510 /* If there other units for the same device node have a job
511 queued it might be worth checking again if it is runnable
512 now. This is necessary, since swap_start() refuses
513 operation with EAGAIN if there's already another job for
514 the same device node queued. */
515 LIST_FOREACH_OTHERS(same_devnode, other, s)
516 if (UNIT(other)->job)
517 job_add_to_run_queue(UNIT(other)->job);
518 }
519
520 static int swap_coldplug(Unit *u) {
521 Swap *s = SWAP(u);
522 SwapState new_state = SWAP_DEAD;
523 int r;
524
525 assert(s);
526 assert(s->state == SWAP_DEAD);
527
528 if (s->deserialized_state != s->state)
529 new_state = s->deserialized_state;
530 else if (s->from_proc_swaps)
531 new_state = SWAP_ACTIVE;
532
533 if (new_state == s->state)
534 return 0;
535
536 if (s->control_pid > 0 &&
537 pid_is_unwaited(s->control_pid) &&
538 IN_SET(new_state,
539 SWAP_ACTIVATING,
540 SWAP_ACTIVATING_SIGTERM,
541 SWAP_ACTIVATING_SIGKILL,
542 SWAP_ACTIVATING_DONE,
543 SWAP_DEACTIVATING,
544 SWAP_DEACTIVATING_SIGTERM,
545 SWAP_DEACTIVATING_SIGKILL)) {
546
547 r = unit_watch_pid(UNIT(s), s->control_pid);
548 if (r < 0)
549 return r;
550
551 r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
552 if (r < 0)
553 return r;
554 }
555
556 swap_set_state(s, new_state);
557 return 0;
558 }
559
560 static void swap_dump(Unit *u, FILE *f, const char *prefix) {
561 Swap *s = SWAP(u);
562 SwapParameters *p;
563
564 assert(s);
565 assert(f);
566
567 if (s->from_proc_swaps)
568 p = &s->parameters_proc_swaps;
569 else if (s->from_fragment)
570 p = &s->parameters_fragment;
571 else
572 p = NULL;
573
574 fprintf(f,
575 "%sSwap State: %s\n"
576 "%sResult: %s\n"
577 "%sWhat: %s\n"
578 "%sFrom /proc/swaps: %s\n"
579 "%sFrom fragment: %s\n",
580 prefix, swap_state_to_string(s->state),
581 prefix, swap_result_to_string(s->result),
582 prefix, s->what,
583 prefix, yes_no(s->from_proc_swaps),
584 prefix, yes_no(s->from_fragment));
585
586 if (s->devnode)
587 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
588
589 if (p)
590 fprintf(f,
591 "%sPriority: %i\n"
592 "%sOptions: %s\n",
593 prefix, p->priority,
594 prefix, strempty(p->options));
595
596 if (s->control_pid > 0)
597 fprintf(f,
598 "%sControl PID: "PID_FMT"\n",
599 prefix, s->control_pid);
600
601 exec_context_dump(&s->exec_context, f, prefix);
602 kill_context_dump(&s->kill_context, f, prefix);
603 }
604
605 static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
606 pid_t pid;
607 int r;
608 ExecParameters exec_params = {
609 .apply_permissions = true,
610 .apply_chroot = true,
611 .apply_tty_stdin = true,
612 .stdin_fd = -1,
613 .stdout_fd = -1,
614 .stderr_fd = -1,
615 };
616
617 assert(s);
618 assert(c);
619 assert(_pid);
620
621 (void) unit_realize_cgroup(UNIT(s));
622 if (s->reset_cpu_usage) {
623 (void) unit_reset_cpu_usage(UNIT(s));
624 s->reset_cpu_usage = false;
625 }
626
627 r = unit_setup_exec_runtime(UNIT(s));
628 if (r < 0)
629 goto fail;
630
631 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
632 if (r < 0)
633 goto fail;
634
635 exec_params.environment = UNIT(s)->manager->environment;
636 exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
637 exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
638 exec_params.cgroup_path = UNIT(s)->cgroup_path;
639 exec_params.cgroup_delegate = s->cgroup_context.delegate;
640 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
641
642 r = exec_spawn(UNIT(s),
643 c,
644 &s->exec_context,
645 &exec_params,
646 s->exec_runtime,
647 &pid);
648 if (r < 0)
649 goto fail;
650
651 r = unit_watch_pid(UNIT(s), pid);
652 if (r < 0)
653 /* FIXME: we need to do something here */
654 goto fail;
655
656 *_pid = pid;
657
658 return 0;
659
660 fail:
661 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
662 return r;
663 }
664
665 static void swap_enter_dead(Swap *s, SwapResult f) {
666 assert(s);
667
668 if (f != SWAP_SUCCESS)
669 s->result = f;
670
671 exec_runtime_destroy(s->exec_runtime);
672 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
673
674 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
675
676 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
677 }
678
679 static void swap_enter_active(Swap *s, SwapResult f) {
680 assert(s);
681
682 if (f != SWAP_SUCCESS)
683 s->result = f;
684
685 swap_set_state(s, SWAP_ACTIVE);
686 }
687
688 static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
689 int r;
690
691 assert(s);
692
693 if (f != SWAP_SUCCESS)
694 s->result = f;
695
696 r = unit_kill_context(
697 UNIT(s),
698 &s->kill_context,
699 (state != SWAP_ACTIVATING_SIGTERM && state != SWAP_DEACTIVATING_SIGTERM) ?
700 KILL_KILL : KILL_TERMINATE,
701 -1,
702 s->control_pid,
703 false);
704 if (r < 0)
705 goto fail;
706
707 if (r > 0) {
708 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
709 if (r < 0)
710 goto fail;
711
712 swap_set_state(s, state);
713 } else if (state == SWAP_ACTIVATING_SIGTERM)
714 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_SUCCESS);
715 else if (state == SWAP_DEACTIVATING_SIGTERM)
716 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS);
717 else
718 swap_enter_dead(s, SWAP_SUCCESS);
719
720 return;
721
722 fail:
723 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
724 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
725 }
726
727 static void swap_enter_activating(Swap *s) {
728 _cleanup_free_ char *opts = NULL;
729 int r;
730
731 assert(s);
732
733 s->control_command_id = SWAP_EXEC_ACTIVATE;
734 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
735
736 if (s->from_fragment) {
737 int priority = -1;
738
739 r = fstab_find_pri(s->parameters_fragment.options, &priority);
740 if (r < 0)
741 log_warning_errno(r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options);
742 else if (r == 1 && s->parameters_fragment.priority >= 0)
743 log_warning("Duplicate swap priority configuration by Priority and Options fields.");
744
745 if (r <= 0 && s->parameters_fragment.priority >= 0) {
746 if (s->parameters_fragment.options)
747 r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority);
748 else
749 r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority);
750 if (r < 0)
751 goto fail;
752 }
753 }
754
755 r = exec_command_set(s->control_command, "/sbin/swapon", NULL);
756 if (r < 0)
757 goto fail;
758
759 if (s->parameters_fragment.options || opts) {
760 r = exec_command_append(s->control_command, "-o",
761 opts ? : s->parameters_fragment.options, NULL);
762 if (r < 0)
763 goto fail;
764 }
765
766 r = exec_command_append(s->control_command, s->what, NULL);
767 if (r < 0)
768 goto fail;
769
770 swap_unwatch_control_pid(s);
771
772 r = swap_spawn(s, s->control_command, &s->control_pid);
773 if (r < 0)
774 goto fail;
775
776 swap_set_state(s, SWAP_ACTIVATING);
777
778 return;
779
780 fail:
781 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m");
782 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
783 }
784
785 static void swap_enter_deactivating(Swap *s) {
786 int r;
787
788 assert(s);
789
790 s->control_command_id = SWAP_EXEC_DEACTIVATE;
791 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
792
793 r = exec_command_set(s->control_command,
794 "/sbin/swapoff",
795 s->what,
796 NULL);
797 if (r < 0)
798 goto fail;
799
800 swap_unwatch_control_pid(s);
801
802 r = swap_spawn(s, s->control_command, &s->control_pid);
803 if (r < 0)
804 goto fail;
805
806 swap_set_state(s, SWAP_DEACTIVATING);
807
808 return;
809
810 fail:
811 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m");
812 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
813 }
814
815 static int swap_start(Unit *u) {
816 Swap *s = SWAP(u), *other;
817
818 assert(s);
819
820 /* We cannot fulfill this request right now, try again later
821 * please! */
822
823 if (s->state == SWAP_DEACTIVATING ||
824 s->state == SWAP_DEACTIVATING_SIGTERM ||
825 s->state == SWAP_DEACTIVATING_SIGKILL ||
826 s->state == SWAP_ACTIVATING_SIGTERM ||
827 s->state == SWAP_ACTIVATING_SIGKILL)
828 return -EAGAIN;
829
830 if (s->state == SWAP_ACTIVATING)
831 return 0;
832
833 assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED);
834
835 if (detect_container() > 0)
836 return -EPERM;
837
838 /* If there's a job for another swap unit for the same node
839 * running, then let's not dispatch this one for now, and wait
840 * until that other job has finished. */
841 LIST_FOREACH_OTHERS(same_devnode, other, s)
842 if (UNIT(other)->job && UNIT(other)->job->state == JOB_RUNNING)
843 return -EAGAIN;
844
845 s->result = SWAP_SUCCESS;
846 s->reset_cpu_usage = true;
847
848 swap_enter_activating(s);
849 return 1;
850 }
851
852 static int swap_stop(Unit *u) {
853 Swap *s = SWAP(u);
854
855 assert(s);
856
857 if (s->state == SWAP_DEACTIVATING ||
858 s->state == SWAP_DEACTIVATING_SIGTERM ||
859 s->state == SWAP_DEACTIVATING_SIGKILL ||
860 s->state == SWAP_ACTIVATING_SIGTERM ||
861 s->state == SWAP_ACTIVATING_SIGKILL)
862 return 0;
863
864 assert(s->state == SWAP_ACTIVATING ||
865 s->state == SWAP_ACTIVATING_DONE ||
866 s->state == SWAP_ACTIVE);
867
868 if (detect_container() > 0)
869 return -EPERM;
870
871 swap_enter_deactivating(s);
872 return 1;
873 }
874
875 static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
876 Swap *s = SWAP(u);
877
878 assert(s);
879 assert(f);
880 assert(fds);
881
882 unit_serialize_item(u, f, "state", swap_state_to_string(s->state));
883 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
884
885 if (s->control_pid > 0)
886 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
887
888 if (s->control_command_id >= 0)
889 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
890
891 return 0;
892 }
893
894 static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
895 Swap *s = SWAP(u);
896
897 assert(s);
898 assert(fds);
899
900 if (streq(key, "state")) {
901 SwapState state;
902
903 state = swap_state_from_string(value);
904 if (state < 0)
905 log_unit_debug(u, "Failed to parse state value: %s", value);
906 else
907 s->deserialized_state = state;
908 } else if (streq(key, "result")) {
909 SwapResult f;
910
911 f = swap_result_from_string(value);
912 if (f < 0)
913 log_unit_debug(u, "Failed to parse result value: %s", value);
914 else if (f != SWAP_SUCCESS)
915 s->result = f;
916 } else if (streq(key, "control-pid")) {
917 pid_t pid;
918
919 if (parse_pid(value, &pid) < 0)
920 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
921 else
922 s->control_pid = pid;
923
924 } else if (streq(key, "control-command")) {
925 SwapExecCommand id;
926
927 id = swap_exec_command_from_string(value);
928 if (id < 0)
929 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
930 else {
931 s->control_command_id = id;
932 s->control_command = s->exec_command + id;
933 }
934 } else
935 log_unit_debug(u, "Unknown serialization key: %s", key);
936
937 return 0;
938 }
939
940 _pure_ static UnitActiveState swap_active_state(Unit *u) {
941 assert(u);
942
943 return state_translation_table[SWAP(u)->state];
944 }
945
946 _pure_ static const char *swap_sub_state_to_string(Unit *u) {
947 assert(u);
948
949 return swap_state_to_string(SWAP(u)->state);
950 }
951
952 _pure_ static bool swap_check_gc(Unit *u) {
953 Swap *s = SWAP(u);
954
955 assert(s);
956
957 return s->from_proc_swaps;
958 }
959
960 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
961 Swap *s = SWAP(u);
962 SwapResult f;
963
964 assert(s);
965 assert(pid >= 0);
966
967 if (pid != s->control_pid)
968 return;
969
970 s->control_pid = 0;
971
972 if (is_clean_exit(code, status, NULL))
973 f = SWAP_SUCCESS;
974 else if (code == CLD_EXITED)
975 f = SWAP_FAILURE_EXIT_CODE;
976 else if (code == CLD_KILLED)
977 f = SWAP_FAILURE_SIGNAL;
978 else if (code == CLD_DUMPED)
979 f = SWAP_FAILURE_CORE_DUMP;
980 else
981 assert_not_reached("Unknown code");
982
983 if (f != SWAP_SUCCESS)
984 s->result = f;
985
986 if (s->control_command) {
987 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
988
989 s->control_command = NULL;
990 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
991 }
992
993 log_unit_full(u, f == SWAP_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
994 "Swap process exited, code=%s status=%i", sigchld_code_to_string(code), status);
995
996 switch (s->state) {
997
998 case SWAP_ACTIVATING:
999 case SWAP_ACTIVATING_DONE:
1000 case SWAP_ACTIVATING_SIGTERM:
1001 case SWAP_ACTIVATING_SIGKILL:
1002
1003 if (f == SWAP_SUCCESS)
1004 swap_enter_active(s, f);
1005 else
1006 swap_enter_dead(s, f);
1007 break;
1008
1009 case SWAP_DEACTIVATING:
1010 case SWAP_DEACTIVATING_SIGKILL:
1011 case SWAP_DEACTIVATING_SIGTERM:
1012
1013 swap_enter_dead(s, f);
1014 break;
1015
1016 default:
1017 assert_not_reached("Uh, control process died at wrong time.");
1018 }
1019
1020 /* Notify clients about changed exit status */
1021 unit_add_to_dbus_queue(u);
1022 }
1023
1024 static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1025 Swap *s = SWAP(userdata);
1026
1027 assert(s);
1028 assert(s->timer_event_source == source);
1029
1030 switch (s->state) {
1031
1032 case SWAP_ACTIVATING:
1033 case SWAP_ACTIVATING_DONE:
1034 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
1035 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1036 break;
1037
1038 case SWAP_DEACTIVATING:
1039 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
1040 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
1041 break;
1042
1043 case SWAP_ACTIVATING_SIGTERM:
1044 if (s->kill_context.send_sigkill) {
1045 log_unit_warning(UNIT(s), "Activation timed out. Killing.");
1046 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1047 } else {
1048 log_unit_warning(UNIT(s), "Activation timed out. Skipping SIGKILL. Ignoring.");
1049 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1050 }
1051 break;
1052
1053 case SWAP_DEACTIVATING_SIGTERM:
1054 if (s->kill_context.send_sigkill) {
1055 log_unit_warning(UNIT(s), "Deactivation timed out. Killing.");
1056 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
1057 } else {
1058 log_unit_warning(UNIT(s), "Deactivation timed out. Skipping SIGKILL. Ignoring.");
1059 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1060 }
1061 break;
1062
1063 case SWAP_ACTIVATING_SIGKILL:
1064 case SWAP_DEACTIVATING_SIGKILL:
1065 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
1066 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
1067 break;
1068
1069 default:
1070 assert_not_reached("Timeout at wrong time.");
1071 }
1072
1073 return 0;
1074 }
1075
1076 static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1077 unsigned i;
1078 int r = 0;
1079
1080 assert(m);
1081
1082 rewind(m->proc_swaps);
1083
1084 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
1085
1086 for (i = 1;; i++) {
1087 _cleanup_free_ char *dev = NULL, *d = NULL;
1088 int prio = 0, k;
1089
1090 k = fscanf(m->proc_swaps,
1091 "%ms " /* device/file */
1092 "%*s " /* type of swap */
1093 "%*s " /* swap size */
1094 "%*s " /* used */
1095 "%i\n", /* priority */
1096 &dev, &prio);
1097 if (k != 2) {
1098 if (k == EOF)
1099 break;
1100
1101 log_warning("Failed to parse /proc/swaps:%u.", i);
1102 continue;
1103 }
1104
1105 if (cunescape(dev, UNESCAPE_RELAX, &d) < 0)
1106 return log_oom();
1107
1108 device_found_node(m, d, true, DEVICE_FOUND_SWAP, set_flags);
1109
1110 k = swap_process_new(m, d, prio, set_flags);
1111 if (k < 0)
1112 r = k;
1113 }
1114
1115 return r;
1116 }
1117
1118 static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1119 Manager *m = userdata;
1120 Unit *u;
1121 int r;
1122
1123 assert(m);
1124 assert(revents & EPOLLPRI);
1125
1126 r = swap_load_proc_swaps(m, true);
1127 if (r < 0) {
1128 log_error_errno(r, "Failed to reread /proc/swaps: %m");
1129
1130 /* Reset flags, just in case, for late calls */
1131 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1132 Swap *swap = SWAP(u);
1133
1134 swap->is_active = swap->just_activated = false;
1135 }
1136
1137 return 0;
1138 }
1139
1140 manager_dispatch_load_queue(m);
1141
1142 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1143 Swap *swap = SWAP(u);
1144
1145 if (!swap->is_active) {
1146 /* This has just been deactivated */
1147
1148 swap_unset_proc_swaps(swap);
1149
1150 switch (swap->state) {
1151
1152 case SWAP_ACTIVE:
1153 swap_enter_dead(swap, SWAP_SUCCESS);
1154 break;
1155
1156 default:
1157 /* Fire again */
1158 swap_set_state(swap, swap->state);
1159 break;
1160 }
1161
1162 if (swap->what)
1163 device_found_node(m, swap->what, false, DEVICE_FOUND_SWAP, true);
1164
1165 } else if (swap->just_activated) {
1166
1167 /* New swap entry */
1168
1169 switch (swap->state) {
1170
1171 case SWAP_DEAD:
1172 case SWAP_FAILED:
1173 swap_enter_active(swap, SWAP_SUCCESS);
1174 break;
1175
1176 case SWAP_ACTIVATING:
1177 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1178 break;
1179
1180 default:
1181 /* Nothing really changed, but let's
1182 * issue an notification call
1183 * nonetheless, in case somebody is
1184 * waiting for this. */
1185 swap_set_state(swap, swap->state);
1186 break;
1187 }
1188 }
1189
1190 /* Reset the flags for later calls */
1191 swap->is_active = swap->just_activated = false;
1192 }
1193
1194 return 1;
1195 }
1196
1197 static Unit *swap_following(Unit *u) {
1198 Swap *s = SWAP(u);
1199 Swap *other, *first = NULL;
1200
1201 assert(s);
1202
1203 /* If the user configured the swap through /etc/fstab or
1204 * a device unit, follow that. */
1205
1206 if (s->from_fragment)
1207 return NULL;
1208
1209 LIST_FOREACH_OTHERS(same_devnode, other, s)
1210 if (other->from_fragment)
1211 return UNIT(other);
1212
1213 /* Otherwise, make everybody follow the unit that's named after
1214 * the swap device in the kernel */
1215
1216 if (streq_ptr(s->what, s->devnode))
1217 return NULL;
1218
1219 LIST_FOREACH_AFTER(same_devnode, other, s)
1220 if (streq_ptr(other->what, other->devnode))
1221 return UNIT(other);
1222
1223 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1224 if (streq_ptr(other->what, other->devnode))
1225 return UNIT(other);
1226
1227 first = other;
1228 }
1229
1230 /* Fall back to the first on the list */
1231 return UNIT(first);
1232 }
1233
1234 static int swap_following_set(Unit *u, Set **_set) {
1235 Swap *s = SWAP(u), *other;
1236 Set *set;
1237 int r;
1238
1239 assert(s);
1240 assert(_set);
1241
1242 if (LIST_JUST_US(same_devnode, s)) {
1243 *_set = NULL;
1244 return 0;
1245 }
1246
1247 set = set_new(NULL);
1248 if (!set)
1249 return -ENOMEM;
1250
1251 LIST_FOREACH_OTHERS(same_devnode, other, s) {
1252 r = set_put(set, other);
1253 if (r < 0)
1254 goto fail;
1255 }
1256
1257 *_set = set;
1258 return 1;
1259
1260 fail:
1261 set_free(set);
1262 return r;
1263 }
1264
1265 static void swap_shutdown(Manager *m) {
1266 assert(m);
1267
1268 m->swap_event_source = sd_event_source_unref(m->swap_event_source);
1269
1270 m->proc_swaps = safe_fclose(m->proc_swaps);
1271
1272 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
1273 }
1274
1275 static void swap_enumerate(Manager *m) {
1276 int r;
1277
1278 assert(m);
1279
1280 if (!m->proc_swaps) {
1281 m->proc_swaps = fopen("/proc/swaps", "re");
1282 if (!m->proc_swaps) {
1283 if (errno == ENOENT)
1284 log_debug("Not swap enabled, skipping enumeration");
1285 else
1286 log_error_errno(errno, "Failed to open /proc/swaps: %m");
1287
1288 return;
1289 }
1290
1291 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
1292 if (r < 0) {
1293 log_error_errno(r, "Failed to watch /proc/swaps: %m");
1294 goto fail;
1295 }
1296
1297 /* Dispatch this before we dispatch SIGCHLD, so that
1298 * we always get the events from /proc/swaps before
1299 * the SIGCHLD of /sbin/swapon. */
1300 r = sd_event_source_set_priority(m->swap_event_source, -10);
1301 if (r < 0) {
1302 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
1303 goto fail;
1304 }
1305
1306 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
1307 }
1308
1309 r = swap_load_proc_swaps(m, false);
1310 if (r < 0)
1311 goto fail;
1312
1313 return;
1314
1315 fail:
1316 swap_shutdown(m);
1317 }
1318
1319 int swap_process_device_new(Manager *m, struct udev_device *dev) {
1320 struct udev_list_entry *item = NULL, *first = NULL;
1321 _cleanup_free_ char *e = NULL;
1322 const char *dn;
1323 Swap *s;
1324 int r = 0;
1325
1326 assert(m);
1327 assert(dev);
1328
1329 dn = udev_device_get_devnode(dev);
1330 if (!dn)
1331 return 0;
1332
1333 r = unit_name_from_path(dn, ".swap", &e);
1334 if (r < 0)
1335 return r;
1336
1337 s = hashmap_get(m->units, e);
1338 if (s)
1339 r = swap_set_devnode(s, dn);
1340
1341 first = udev_device_get_devlinks_list_entry(dev);
1342 udev_list_entry_foreach(item, first) {
1343 _cleanup_free_ char *n = NULL;
1344 int q;
1345
1346 q = unit_name_from_path(udev_list_entry_get_name(item), ".swap", &n);
1347 if (q < 0)
1348 return q;
1349
1350 s = hashmap_get(m->units, n);
1351 if (s) {
1352 q = swap_set_devnode(s, dn);
1353 if (q < 0)
1354 r = q;
1355 }
1356 }
1357
1358 return r;
1359 }
1360
1361 int swap_process_device_remove(Manager *m, struct udev_device *dev) {
1362 const char *dn;
1363 int r = 0;
1364 Swap *s;
1365
1366 dn = udev_device_get_devnode(dev);
1367 if (!dn)
1368 return 0;
1369
1370 while ((s = hashmap_get(m->swaps_by_devnode, dn))) {
1371 int q;
1372
1373 q = swap_set_devnode(s, NULL);
1374 if (q < 0)
1375 r = q;
1376 }
1377
1378 return r;
1379 }
1380
1381 static void swap_reset_failed(Unit *u) {
1382 Swap *s = SWAP(u);
1383
1384 assert(s);
1385
1386 if (s->state == SWAP_FAILED)
1387 swap_set_state(s, SWAP_DEAD);
1388
1389 s->result = SWAP_SUCCESS;
1390 }
1391
1392 static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
1393 return unit_kill_common(u, who, signo, -1, SWAP(u)->control_pid, error);
1394 }
1395
1396 static int swap_get_timeout(Unit *u, usec_t *timeout) {
1397 Swap *s = SWAP(u);
1398 usec_t t;
1399 int r;
1400
1401 if (!s->timer_event_source)
1402 return 0;
1403
1404 r = sd_event_source_get_time(s->timer_event_source, &t);
1405 if (r < 0)
1406 return r;
1407 if (t == USEC_INFINITY)
1408 return 0;
1409
1410 *timeout = t;
1411 return 1;
1412 }
1413
1414 static bool swap_supported(void) {
1415 static int supported = -1;
1416
1417 /* If swap support is not available in the kernel, or we are
1418 * running in a container we don't support swap units, and any
1419 * attempts to starting one should fail immediately. */
1420
1421 if (supported < 0)
1422 supported =
1423 access("/proc/swaps", F_OK) >= 0 &&
1424 detect_container() <= 0;
1425
1426 return supported;
1427 }
1428
1429 static int swap_control_pid(Unit *u) {
1430 Swap *s = SWAP(u);
1431
1432 assert(s);
1433
1434 return s->control_pid;
1435 }
1436
1437 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1438 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1439 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1440 };
1441
1442 DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1443
1444 static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1445 [SWAP_SUCCESS] = "success",
1446 [SWAP_FAILURE_RESOURCES] = "resources",
1447 [SWAP_FAILURE_TIMEOUT] = "timeout",
1448 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1449 [SWAP_FAILURE_SIGNAL] = "signal",
1450 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1451 };
1452
1453 DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1454
1455 const UnitVTable swap_vtable = {
1456 .object_size = sizeof(Swap),
1457 .exec_context_offset = offsetof(Swap, exec_context),
1458 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1459 .kill_context_offset = offsetof(Swap, kill_context),
1460 .exec_runtime_offset = offsetof(Swap, exec_runtime),
1461
1462 .sections =
1463 "Unit\0"
1464 "Swap\0"
1465 "Install\0",
1466 .private_section = "Swap",
1467
1468 .init = swap_init,
1469 .load = swap_load,
1470 .done = swap_done,
1471
1472 .coldplug = swap_coldplug,
1473
1474 .dump = swap_dump,
1475
1476 .start = swap_start,
1477 .stop = swap_stop,
1478
1479 .kill = swap_kill,
1480
1481 .get_timeout = swap_get_timeout,
1482
1483 .serialize = swap_serialize,
1484 .deserialize_item = swap_deserialize_item,
1485
1486 .active_state = swap_active_state,
1487 .sub_state_to_string = swap_sub_state_to_string,
1488
1489 .check_gc = swap_check_gc,
1490
1491 .sigchld_event = swap_sigchld_event,
1492
1493 .reset_failed = swap_reset_failed,
1494
1495 .control_pid = swap_control_pid,
1496
1497 .bus_vtable = bus_swap_vtable,
1498 .bus_set_property = bus_swap_set_property,
1499 .bus_commit_properties = bus_swap_commit_properties,
1500
1501 .following = swap_following,
1502 .following_set = swap_following_set,
1503
1504 .enumerate = swap_enumerate,
1505 .shutdown = swap_shutdown,
1506 .supported = swap_supported,
1507
1508 .status_message_formats = {
1509 .starting_stopping = {
1510 [0] = "Activating swap %s...",
1511 [1] = "Deactivating swap %s...",
1512 },
1513 .finished_start_job = {
1514 [JOB_DONE] = "Activated swap %s.",
1515 [JOB_FAILED] = "Failed to activate swap %s.",
1516 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1517 },
1518 .finished_stop_job = {
1519 [JOB_DONE] = "Deactivated swap %s.",
1520 [JOB_FAILED] = "Failed deactivating swap %s.",
1521 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",
1522 },
1523 },
1524 };