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