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