]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/swap.c
Merge pull request #2594 from keszybz/spelling
[thirdparty/systemd.git] / src / core / swap.c
CommitLineData
07b0b134
ML
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
5430f7f2
LP
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
07b0b134
ML
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
5430f7f2 14 Lesser General Public License for more details.
07b0b134 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
07b0b134
ML
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
07b0b134
ML
21#include <sys/epoll.h>
22#include <sys/stat.h>
4f5dd394 23#include <unistd.h>
07b0b134 24
b4bbcaa9
TA
25#include "libudev.h"
26
b5efdb8a 27#include "alloc-util.h"
07b0b134 28#include "dbus-swap.h"
4f5dd394 29#include "escape.h"
9a57c629 30#include "exit-status.h"
3ffd4af2 31#include "fd-util.h"
4f5dd394
LP
32#include "formats-util.h"
33#include "fstab-util.h"
6bedfcbb 34#include "parse-util.h"
9eb977db 35#include "path-util.h"
7b3e062c 36#include "process-util.h"
4f5dd394 37#include "special.h"
8b43440b 38#include "string-table.h"
07630cea 39#include "string-util.h"
3ffd4af2 40#include "swap.h"
5bcb0f2b 41#include "udev-util.h"
4f5dd394
LP
42#include "unit-name.h"
43#include "unit.h"
44#include "virt.h"
07b0b134
ML
45
46static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
47 [SWAP_DEAD] = UNIT_INACTIVE,
e04aad61 48 [SWAP_ACTIVATING] = UNIT_ACTIVATING,
5bcb0f2b 49 [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE,
07b0b134 50 [SWAP_ACTIVE] = UNIT_ACTIVE,
e04aad61
LP
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,
fdf20a31 56 [SWAP_FAILED] = UNIT_FAILED
07b0b134
ML
57};
58
718db961
LP
59static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
60static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
61
e04aad61 62static void swap_unset_proc_swaps(Swap *s) {
9670d583
LP
63 assert(s);
64
65 if (!s->from_proc_swaps)
66 return;
67
a1e58e8e 68 s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what);
9670d583
LP
69
70 s->from_proc_swaps = false;
71}
72
73static int swap_set_devnode(Swap *s, const char *devnode) {
df326b84 74 Hashmap *swaps;
5bcb0f2b 75 Swap *first;
9670d583 76 int r;
e04aad61
LP
77
78 assert(s);
79
d5099efc 80 r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &string_hash_ops);
9670d583
LP
81 if (r < 0)
82 return r;
e04aad61 83
9670d583 84 swaps = UNIT(s)->manager->swaps_by_devnode;
e04aad61 85
9670d583
LP
86 if (s->devnode) {
87 first = hashmap_get(swaps, s->devnode);
e04aad61 88
9670d583
LP
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);
5bcb0f2b 94
a1e58e8e 95 s->devnode = mfree(s->devnode);
9670d583
LP
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;
e04aad61
LP
110}
111
f6cebb3b 112static void swap_init(Unit *u) {
6e620bec
LP
113 Swap *s = SWAP(u);
114
115 assert(s);
1124fe6f 116 assert(UNIT(s)->load_state == UNIT_STUB);
6e620bec 117
1f19a534 118 s->timeout_usec = u->manager->default_timeout_start_usec;
e04aad61 119
ac155bb8
MS
120 s->exec_context.std_output = u->manager->default_std_output;
121 s->exec_context.std_error = u->manager->default_std_error;
085afe36 122
6b1dc2bd 123 s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
e04aad61 124
e1770af8 125 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
c8f4d764 126
5bcb0f2b 127 u->ignore_on_isolate = true;
e04aad61
LP
128}
129
130static 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;
6e620bec
LP
138}
139
4e85aff4
LP
140static void swap_done(Unit *u) {
141 Swap *s = SWAP(u);
07b0b134 142
4e85aff4 143 assert(s);
07b0b134 144
e04aad61 145 swap_unset_proc_swaps(s);
9670d583 146 swap_set_devnode(s, NULL);
e04aad61 147
a1e58e8e
LP
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);
86b23b07 151
613b411c 152 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
e04aad61
LP
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
718db961
LP
158 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
159}
160
36c16a7c 161static int swap_arm_timer(Swap *s, usec_t usec) {
718db961
LP
162 int r;
163
164 assert(s);
165
718db961 166 if (s->timer_event_source) {
36c16a7c 167 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
168 if (r < 0)
169 return r;
170
171 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
172 }
173
36c16a7c
LP
174 if (usec == USEC_INFINITY)
175 return 0;
176
7dfbe2e3 177 r = sd_event_add_time(
6a0f1f6d
LP
178 UNIT(s)->manager->event,
179 &s->timer_event_source,
180 CLOCK_MONOTONIC,
36c16a7c 181 usec, 0,
6a0f1f6d 182 swap_dispatch_timer, s);
7dfbe2e3
TG
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;
07b0b134
ML
189}
190
173a8d04 191static int swap_add_device_links(Swap *s) {
173a8d04
LP
192 assert(s);
193
194 if (!s->what)
195 return 0;
196
f10af76d
LP
197 if (!s->from_fragment)
198 return 0;
199
cfcfd4ae 200 if (is_device_path(s->what))
9d06297e 201 return unit_add_node_link(UNIT(s), s->what, UNIT(s)->manager->running_as == MANAGER_SYSTEM, UNIT_BINDS_TO);
cfcfd4ae
LP
202 else
203 /* File based swap devices need to be ordered after
2b93b027 204 * systemd-remount-fs.service, since they might need a
cfcfd4ae 205 * writable file system. */
2b93b027 206 return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
173a8d04
LP
207}
208
2edd4434 209static int swap_add_default_dependencies(Swap *s) {
8bf23dc7
FB
210 int r;
211
2edd4434
LP
212 assert(s);
213
4c9ea260
LP
214 if (!UNIT(s)->default_dependencies)
215 return 0;
216
b2c23da8 217 if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
6b1dc2bd 218 return 0;
2edd4434 219
75f86906 220 if (detect_container() > 0)
c0387ebf
LP
221 return 0;
222
8bf23dc7
FB
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
5607d856 229 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
2edd4434
LP
230}
231
4e85aff4 232static int swap_verify(Swap *s) {
7fd1b19b 233 _cleanup_free_ char *e = NULL;
7410616c 234 int r;
4e85aff4 235
1124fe6f 236 if (UNIT(s)->load_state != UNIT_LOADED)
5bcb0f2b 237 return 0;
4e85aff4 238
7410616c
LP
239 r = unit_name_from_path(s->what, ".swap", &e);
240 if (r < 0)
f2341e0a 241 return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m");
4e85aff4 242
7410616c 243 if (!unit_has_name(UNIT(s), e)) {
f2341e0a 244 log_unit_error(UNIT(s), "Value of What= and unit name do not match, not loading.");
4e85aff4
LP
245 return -EINVAL;
246 }
247
4819ff03 248 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
f2341e0a 249 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load.");
e04aad61
LP
250 return -EINVAL;
251 }
252
4e85aff4
LP
253 return 0;
254}
255
9670d583
LP
256static 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
07b0b134
ML
277static int swap_load(Unit *u) {
278 int r;
279 Swap *s = SWAP(u);
280
281 assert(s);
ac155bb8 282 assert(u->load_state == UNIT_STUB);
07b0b134
ML
283
284 /* Load a .swap file */
646134dc
ZJS
285 r = unit_load_fragment_and_dropin_optional(u);
286 if (r < 0)
07b0b134
ML
287 return r;
288
ac155bb8 289 if (u->load_state == UNIT_LOADED) {
4e85aff4 290
1124fe6f 291 if (UNIT(s)->fragment_path)
4e85aff4
LP
292 s->from_fragment = true;
293
294 if (!s->what) {
295 if (s->parameters_fragment.what)
296 s->what = strdup(s->parameters_fragment.what);
4e85aff4
LP
297 else if (s->parameters_proc_swaps.what)
298 s->what = strdup(s->parameters_proc_swaps.what);
7410616c
LP
299 else {
300 r = unit_name_to_path(u->id, &s->what);
301 if (r < 0)
302 return r;
303 }
4e85aff4
LP
304
305 if (!s->what)
07b0b134 306 return -ENOMEM;
4e85aff4 307 }
07b0b134
ML
308
309 path_kill_slashes(s->what);
310
5bcb0f2b
LP
311 if (!UNIT(s)->description) {
312 r = unit_set_description(u, s->what);
313 if (r < 0)
4e85aff4 314 return r;
5bcb0f2b 315 }
4e85aff4 316
a57f7e2c 317 r = unit_require_mounts_for(UNIT(s), s->what);
646134dc 318 if (r < 0)
07b0b134
ML
319 return r;
320
a57f7e2c 321 r = swap_add_device_links(s);
646134dc 322 if (r < 0)
6e2ef85b 323 return r;
07b0b134 324
9670d583
LP
325 r = swap_load_devnode(s);
326 if (r < 0)
327 return r;
328
598459ce
LP
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
d79200e2 337 r = unit_set_default_slice(u);
a016b922
LP
338 if (r < 0)
339 return r;
340
4c9ea260
LP
341 r = swap_add_default_dependencies(s);
342 if (r < 0)
343 return r;
07b0b134
ML
344 }
345
346 return swap_verify(s);
347}
348
628c89cc 349static int swap_setup_unit(
4e85aff4
LP
350 Manager *m,
351 const char *what,
e04aad61 352 const char *what_proc_swaps,
4e85aff4 353 int priority,
e04aad61
LP
354 bool set_flags) {
355
7fd1b19b 356 _cleanup_free_ char *e = NULL;
2c7c6144 357 bool delete = false;
5bcb0f2b 358 Unit *u = NULL;
07b0b134 359 int r;
4e85aff4
LP
360 SwapParameters *p;
361
362 assert(m);
363 assert(what);
6b1dc2bd 364 assert(what_proc_swaps);
07b0b134 365
7410616c
LP
366 r = unit_name_from_path(what, ".swap", &e);
367 if (r < 0)
f2341e0a 368 return log_unit_error_errno(u, r, "Failed to generate unit name from path: %m");
07b0b134 369
e04aad61
LP
370 u = manager_get_unit(m, e);
371
6b1dc2bd 372 if (u &&
e04aad61 373 SWAP(u)->from_proc_swaps &&
628c89cc
LP
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);
e04aad61 376 return -EEXIST;
628c89cc 377 }
4e85aff4
LP
378
379 if (!u) {
07b0b134
ML
380 delete = true;
381
7d17cfbc 382 u = unit_new(m, sizeof(Swap));
170ca19e
ZJS
383 if (!u)
384 return log_oom();
e04aad61 385
7d17cfbc
MS
386 r = unit_add_name(u, e);
387 if (r < 0)
e04aad61
LP
388 goto fail;
389
7d17cfbc
MS
390 SWAP(u)->what = strdup(what);
391 if (!SWAP(u)->what) {
628c89cc 392 r = -ENOMEM;
e04aad61
LP
393 goto fail;
394 }
395
396 unit_add_to_load_queue(u);
4e85aff4
LP
397 } else
398 delete = false;
07b0b134 399
6b1dc2bd 400 p = &SWAP(u)->parameters_proc_swaps;
07b0b134 401
6b1dc2bd 402 if (!p->what) {
5bcb0f2b
LP
403 p->what = strdup(what_proc_swaps);
404 if (!p->what) {
405 r = -ENOMEM;
6b1dc2bd
LP
406 goto fail;
407 }
6b1dc2bd 408 }
e04aad61 409
6b1dc2bd
LP
410 if (set_flags) {
411 SWAP(u)->is_active = true;
412 SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
4e85aff4 413 }
07b0b134 414
6b1dc2bd
LP
415 SWAP(u)->from_proc_swaps = true;
416
4e85aff4 417 p->priority = priority;
07b0b134 418
4e85aff4 419 unit_add_to_dbus_queue(u);
07b0b134
ML
420 return 0;
421
422fail:
f2341e0a 423 log_unit_warning_errno(u, r, "Failed to load swap unit: %m");
e04aad61 424
4e85aff4 425 if (delete && u)
07b0b134
ML
426 unit_free(u);
427
4e85aff4 428 return r;
07b0b134
ML
429}
430
628c89cc 431static int swap_process_new(Manager *m, const char *device, int prio, bool set_flags) {
5bcb0f2b
LP
432 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
433 struct udev_list_entry *item = NULL, *first = NULL;
434 const char *dn;
e04aad61 435 struct stat st;
5bcb0f2b 436 int r;
e04aad61
LP
437
438 assert(m);
439
628c89cc 440 r = swap_setup_unit(m, device, device, prio, set_flags);
5bcb0f2b
LP
441 if (r < 0)
442 return r;
e04aad61 443
5bcb0f2b
LP
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;
e04aad61 448
5bcb0f2b
LP
449 d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev);
450 if (!d)
451 return 0;
e04aad61 452
5bcb0f2b
LP
453 /* Add the main device node */
454 dn = udev_device_get_devnode(d);
455 if (dn && !streq(dn, device))
628c89cc 456 swap_setup_unit(m, dn, device, prio, set_flags);
e04aad61 457
5bcb0f2b
LP
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;
e04aad61 462
5bcb0f2b
LP
463 /* Don't bother with the /dev/block links */
464 p = udev_list_entry_get_name(item);
e04aad61 465
5bcb0f2b
LP
466 if (streq(p, device))
467 continue;
e04aad61 468
5bcb0f2b
LP
469 if (path_startswith(p, "/dev/block/"))
470 continue;
e04aad61 471
5bcb0f2b
LP
472 if (stat(p, &st) >= 0)
473 if (!S_ISBLK(st.st_mode) ||
474 st.st_rdev != udev_device_get_devnum(d))
475 continue;
e04aad61 476
628c89cc 477 swap_setup_unit(m, p, device, prio, set_flags);
e04aad61
LP
478 }
479
e04aad61
LP
480 return r;
481}
482
07b0b134
ML
483static void swap_set_state(Swap *s, SwapState state) {
484 SwapState old_state;
37cf8fee 485 Swap *other;
e04aad61 486
07b0b134
ML
487 assert(s);
488
489 old_state = s->state;
490 s->state = state;
491
e04aad61
LP
492 if (state != SWAP_ACTIVATING &&
493 state != SWAP_ACTIVATING_SIGTERM &&
494 state != SWAP_ACTIVATING_SIGKILL &&
5bcb0f2b 495 state != SWAP_ACTIVATING_DONE &&
e04aad61
LP
496 state != SWAP_DEACTIVATING &&
497 state != SWAP_DEACTIVATING_SIGTERM &&
498 state != SWAP_DEACTIVATING_SIGKILL) {
718db961 499 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
e04aad61
LP
500 swap_unwatch_control_pid(s);
501 s->control_command = NULL;
502 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
503 }
504
07b0b134 505 if (state != old_state)
f2341e0a 506 log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state));
07b0b134 507
5bcb0f2b 508 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
37cf8fee
LP
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);
07b0b134
ML
518}
519
be847e82 520static int swap_coldplug(Unit *u) {
07b0b134
ML
521 Swap *s = SWAP(u);
522 SwapState new_state = SWAP_DEAD;
e04aad61 523 int r;
07b0b134
ML
524
525 assert(s);
526 assert(s->state == SWAP_DEAD);
527
528 if (s->deserialized_state != s->state)
529 new_state = s->deserialized_state;
4e85aff4 530 else if (s->from_proc_swaps)
07b0b134
ML
531 new_state = SWAP_ACTIVE;
532
5bcb0f2b
LP
533 if (new_state == s->state)
534 return 0;
e04aad61 535
c386f588
LP
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)) {
e04aad61 546
5bcb0f2b
LP
547 r = unit_watch_pid(UNIT(s), s->control_pid);
548 if (r < 0)
549 return r;
e04aad61 550
36c16a7c 551 r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
5bcb0f2b
LP
552 if (r < 0)
553 return r;
e04aad61 554 }
07b0b134 555
5bcb0f2b 556 swap_set_state(s, new_state);
07b0b134
ML
557 return 0;
558}
559
560static void swap_dump(Unit *u, FILE *f, const char *prefix) {
561 Swap *s = SWAP(u);
4e85aff4 562 SwapParameters *p;
07b0b134
ML
563
564 assert(s);
4e85aff4
LP
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;
b6bfc7bb
LP
571 else
572 p = NULL;
07b0b134
ML
573
574 fprintf(f,
4e85aff4 575 "%sSwap State: %s\n"
e1770af8 576 "%sResult: %s\n"
07b0b134 577 "%sWhat: %s\n"
4e85aff4
LP
578 "%sFrom /proc/swaps: %s\n"
579 "%sFrom fragment: %s\n",
07b0b134 580 prefix, swap_state_to_string(s->state),
e1770af8 581 prefix, swap_result_to_string(s->result),
07b0b134 582 prefix, s->what,
4e85aff4
LP
583 prefix, yes_no(s->from_proc_swaps),
584 prefix, yes_no(s->from_fragment));
e04aad61 585
9670d583
LP
586 if (s->devnode)
587 fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode);
588
b6bfc7bb
LP
589 if (p)
590 fprintf(f,
591 "%sPriority: %i\n"
47cb901e 592 "%sOptions: %s\n",
b6bfc7bb 593 prefix, p->priority,
47cb901e 594 prefix, strempty(p->options));
b6bfc7bb 595
e04aad61
LP
596 if (s->control_pid > 0)
597 fprintf(f,
de0671ee
ZJS
598 "%sControl PID: "PID_FMT"\n",
599 prefix, s->control_pid);
e04aad61
LP
600
601 exec_context_dump(&s->exec_context, f, prefix);
4819ff03 602 kill_context_dump(&s->kill_context, f, prefix);
e04aad61
LP
603}
604
605static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
606 pid_t pid;
607 int r;
9fa95f85
DM
608 ExecParameters exec_params = {
609 .apply_permissions = true,
610 .apply_chroot = true,
611 .apply_tty_stdin = true,
a34ceba6
LP
612 .stdin_fd = -1,
613 .stdout_fd = -1,
614 .stderr_fd = -1,
9fa95f85 615 };
e04aad61
LP
616
617 assert(s);
618 assert(c);
619 assert(_pid);
620
5ad096b3
LP
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 }
4ad49000 626
613b411c
LP
627 r = unit_setup_exec_runtime(UNIT(s));
628 if (r < 0)
629 goto fail;
630
36c16a7c 631 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
646134dc 632 if (r < 0)
e04aad61
LP
633 goto fail;
634
9fa95f85
DM
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;
a931ad47 639 exec_params.cgroup_delegate = s->cgroup_context.delegate;
9fa95f85 640 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
9fa95f85 641
f2341e0a
LP
642 r = exec_spawn(UNIT(s),
643 c,
646134dc 644 &s->exec_context,
9fa95f85 645 &exec_params,
613b411c 646 s->exec_runtime,
646134dc
ZJS
647 &pid);
648 if (r < 0)
e04aad61
LP
649 goto fail;
650
646134dc
ZJS
651 r = unit_watch_pid(UNIT(s), pid);
652 if (r < 0)
e04aad61
LP
653 /* FIXME: we need to do something here */
654 goto fail;
655
656 *_pid = pid;
657
658 return 0;
659
660fail:
718db961 661 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
e04aad61 662 return r;
07b0b134
ML
663}
664
e1770af8 665static void swap_enter_dead(Swap *s, SwapResult f) {
07b0b134
ML
666 assert(s);
667
e1770af8
LP
668 if (f != SWAP_SUCCESS)
669 s->result = f;
e04aad61 670
613b411c
LP
671 exec_runtime_destroy(s->exec_runtime);
672 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
673
e66cf1a3
LP
674 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
675
e1770af8 676 swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
07b0b134
ML
677}
678
e1770af8 679static void swap_enter_active(Swap *s, SwapResult f) {
e04aad61
LP
680 assert(s);
681
e1770af8
LP
682 if (f != SWAP_SUCCESS)
683 s->result = f;
e04aad61
LP
684
685 swap_set_state(s, SWAP_ACTIVE);
686}
687
e1770af8 688static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
07b0b134
ML
689 int r;
690
691 assert(s);
e04aad61 692
e1770af8
LP
693 if (f != SWAP_SUCCESS)
694 s->result = f;
e04aad61 695
cd2086fe
LP
696 r = unit_kill_context(
697 UNIT(s),
698 &s->kill_context,
db2cb23b
UTL
699 (state != SWAP_ACTIVATING_SIGTERM && state != SWAP_DEACTIVATING_SIGTERM) ?
700 KILL_KILL : KILL_TERMINATE,
cd2086fe
LP
701 -1,
702 s->control_pid,
703 false);
704 if (r < 0)
705 goto fail;
e04aad61 706
cd2086fe 707 if (r > 0) {
36c16a7c 708 r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
646134dc 709 if (r < 0)
e04aad61
LP
710 goto fail;
711
712 swap_set_state(s, state);
ac84d1fb
LP
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
e1770af8 718 swap_enter_dead(s, SWAP_SUCCESS);
e04aad61
LP
719
720 return;
721
722fail:
f2341e0a 723 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
e1770af8 724 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
e04aad61
LP
725}
726
727static void swap_enter_activating(Swap *s) {
bf1d7ba7
KZ
728 _cleanup_free_ char *opts = NULL;
729 int r;
e04aad61
LP
730
731 assert(s);
732
733 s->control_command_id = SWAP_EXEC_ACTIVATE;
734 s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE;
07b0b134 735
86b23b07 736 if (s->from_fragment) {
bf1d7ba7 737 int priority = -1;
47cb901e 738
bf1d7ba7
KZ
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);
e64d5235 750 if (r < 0)
bf1d7ba7 751 goto fail;
e64d5235 752 }
86b23b07
JS
753 }
754
755 r = exec_command_set(s->control_command, "/sbin/swapon", NULL);
756 if (r < 0)
757 goto fail;
4e85aff4 758
bf1d7ba7
KZ
759 if (s->parameters_fragment.options || opts) {
760 r = exec_command_append(s->control_command, "-o",
761 opts ? : s->parameters_fragment.options, NULL);
86b23b07
JS
762 if (r < 0)
763 goto fail;
764 }
e04aad61 765
86b23b07 766 r = exec_command_append(s->control_command, s->what, NULL);
e04aad61
LP
767 if (r < 0)
768 goto fail;
769
770 swap_unwatch_control_pid(s);
771
646134dc
ZJS
772 r = swap_spawn(s, s->control_command, &s->control_pid);
773 if (r < 0)
e04aad61
LP
774 goto fail;
775
776 swap_set_state(s, SWAP_ACTIVATING);
777
778 return;
779
780fail:
f2341e0a 781 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m");
e1770af8 782 swap_enter_dead(s, SWAP_FAILURE_RESOURCES);
e04aad61
LP
783}
784
e1770af8 785static void swap_enter_deactivating(Swap *s) {
e04aad61
LP
786 int r;
787
788 assert(s);
789
e04aad61
LP
790 s->control_command_id = SWAP_EXEC_DEACTIVATE;
791 s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE;
792
646134dc 793 r = exec_command_set(s->control_command,
e04aad61
LP
794 "/sbin/swapoff",
795 s->what,
646134dc
ZJS
796 NULL);
797 if (r < 0)
e04aad61
LP
798 goto fail;
799
800 swap_unwatch_control_pid(s);
801
646134dc
ZJS
802 r = swap_spawn(s, s->control_command, &s->control_pid);
803 if (r < 0)
e04aad61
LP
804 goto fail;
805
806 swap_set_state(s, SWAP_DEACTIVATING);
807
808 return;
809
810fail:
f2341e0a 811 log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m");
e1770af8 812 swap_enter_active(s, SWAP_FAILURE_RESOURCES);
e04aad61
LP
813}
814
815static int swap_start(Unit *u) {
37cf8fee 816 Swap *s = SWAP(u), *other;
e04aad61
LP
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
75f86906 835 if (detect_container() > 0)
a5c3034f
LP
836 return -EPERM;
837
37cf8fee
LP
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
e1770af8 845 s->result = SWAP_SUCCESS;
5ad096b3
LP
846 s->reset_cpu_usage = true;
847
e04aad61 848 swap_enter_activating(s);
82a2b6bb 849 return 1;
07b0b134
ML
850}
851
852static int swap_stop(Unit *u) {
853 Swap *s = SWAP(u);
07b0b134
ML
854
855 assert(s);
856
e04aad61
LP
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;
07b0b134 863
e04aad61 864 assert(s->state == SWAP_ACTIVATING ||
5bcb0f2b 865 s->state == SWAP_ACTIVATING_DONE ||
e04aad61 866 s->state == SWAP_ACTIVE);
07b0b134 867
75f86906 868 if (detect_container() > 0)
a5c3034f
LP
869 return -EPERM;
870
e1770af8 871 swap_enter_deactivating(s);
82a2b6bb 872 return 1;
07b0b134
ML
873}
874
875static 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));
e1770af8 883 unit_serialize_item(u, f, "result", swap_result_to_string(s->result));
e04aad61
LP
884
885 if (s->control_pid > 0)
de0671ee 886 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
e04aad61
LP
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));
07b0b134
ML
890
891 return 0;
892}
893
894static 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
646134dc
ZJS
903 state = swap_state_from_string(value);
904 if (state < 0)
f2341e0a 905 log_unit_debug(u, "Failed to parse state value: %s", value);
07b0b134
ML
906 else
907 s->deserialized_state = state;
e1770af8
LP
908 } else if (streq(key, "result")) {
909 SwapResult f;
910
911 f = swap_result_from_string(value);
912 if (f < 0)
f2341e0a 913 log_unit_debug(u, "Failed to parse result value: %s", value);
e1770af8
LP
914 else if (f != SWAP_SUCCESS)
915 s->result = f;
e04aad61
LP
916 } else if (streq(key, "control-pid")) {
917 pid_t pid;
918
919 if (parse_pid(value, &pid) < 0)
f2341e0a 920 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
e04aad61
LP
921 else
922 s->control_pid = pid;
923
924 } else if (streq(key, "control-command")) {
925 SwapExecCommand id;
926
646134dc
ZJS
927 id = swap_exec_command_from_string(value);
928 if (id < 0)
f2341e0a 929 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
e04aad61
LP
930 else {
931 s->control_command_id = id;
932 s->control_command = s->exec_command + id;
933 }
07b0b134 934 } else
f2341e0a 935 log_unit_debug(u, "Unknown serialization key: %s", key);
07b0b134
ML
936
937 return 0;
938}
939
44a6b1b6 940_pure_ static UnitActiveState swap_active_state(Unit *u) {
07b0b134
ML
941 assert(u);
942
943 return state_translation_table[SWAP(u)->state];
944}
945
44a6b1b6 946_pure_ static const char *swap_sub_state_to_string(Unit *u) {
07b0b134
ML
947 assert(u);
948
949 return swap_state_to_string(SWAP(u)->state);
950}
951
44a6b1b6 952_pure_ static bool swap_check_gc(Unit *u) {
07b0b134
ML
953 Swap *s = SWAP(u);
954
955 assert(s);
956
6b1dc2bd 957 return s->from_proc_swaps;
07b0b134
ML
958}
959
e04aad61
LP
960static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
961 Swap *s = SWAP(u);
e1770af8 962 SwapResult f;
e04aad61
LP
963
964 assert(s);
965 assert(pid >= 0);
966
967 if (pid != s->control_pid)
968 return;
969
970 s->control_pid = 0;
971
96342de6 972 if (is_clean_exit(code, status, NULL))
e1770af8
LP
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;
e04aad61
LP
985
986 if (s->control_command) {
6ea832a2 987 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
e1770af8 988
e04aad61
LP
989 s->control_command = NULL;
990 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
991 }
992
f2341e0a
LP
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);
e04aad61
LP
995
996 switch (s->state) {
997
998 case SWAP_ACTIVATING:
5bcb0f2b 999 case SWAP_ACTIVATING_DONE:
e04aad61
LP
1000 case SWAP_ACTIVATING_SIGTERM:
1001 case SWAP_ACTIVATING_SIGKILL:
1002
e1770af8
LP
1003 if (f == SWAP_SUCCESS)
1004 swap_enter_active(s, f);
e04aad61 1005 else
e1770af8 1006 swap_enter_dead(s, f);
e04aad61
LP
1007 break;
1008
1009 case SWAP_DEACTIVATING:
1010 case SWAP_DEACTIVATING_SIGKILL:
1011 case SWAP_DEACTIVATING_SIGTERM:
1012
7bb01eff 1013 swap_enter_dead(s, f);
e04aad61
LP
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);
e04aad61
LP
1022}
1023
718db961
LP
1024static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
1025 Swap *s = SWAP(userdata);
e04aad61
LP
1026
1027 assert(s);
718db961 1028 assert(s->timer_event_source == source);
e04aad61
LP
1029
1030 switch (s->state) {
1031
1032 case SWAP_ACTIVATING:
5bcb0f2b 1033 case SWAP_ACTIVATING_DONE:
f2341e0a 1034 log_unit_warning(UNIT(s), "Activation timed out. Stopping.");
e1770af8 1035 swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1036 break;
1037
1038 case SWAP_DEACTIVATING:
f2341e0a 1039 log_unit_warning(UNIT(s), "Deactivation timed out. Stopping.");
e1770af8 1040 swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1041 break;
1042
1043 case SWAP_ACTIVATING_SIGTERM:
4819ff03 1044 if (s->kill_context.send_sigkill) {
f2341e0a 1045 log_unit_warning(UNIT(s), "Activation timed out. Killing.");
e1770af8 1046 swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
ba035df2 1047 } else {
f2341e0a 1048 log_unit_warning(UNIT(s), "Activation timed out. Skipping SIGKILL. Ignoring.");
e1770af8 1049 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
ba035df2 1050 }
e04aad61
LP
1051 break;
1052
1053 case SWAP_DEACTIVATING_SIGTERM:
4819ff03 1054 if (s->kill_context.send_sigkill) {
f2341e0a 1055 log_unit_warning(UNIT(s), "Deactivation timed out. Killing.");
e1770af8 1056 swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT);
ba035df2 1057 } else {
f2341e0a 1058 log_unit_warning(UNIT(s), "Deactivation timed out. Skipping SIGKILL. Ignoring.");
e1770af8 1059 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
ba035df2 1060 }
e04aad61
LP
1061 break;
1062
1063 case SWAP_ACTIVATING_SIGKILL:
1064 case SWAP_DEACTIVATING_SIGKILL:
f2341e0a 1065 log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring.");
e1770af8 1066 swap_enter_dead(s, SWAP_FAILURE_TIMEOUT);
e04aad61
LP
1067 break;
1068
1069 default:
1070 assert_not_reached("Timeout at wrong time.");
1071 }
718db961
LP
1072
1073 return 0;
e04aad61
LP
1074}
1075
1076static int swap_load_proc_swaps(Manager *m, bool set_flags) {
1077 unsigned i;
1078 int r = 0;
1079
1080 assert(m);
1081
07b0b134 1082 rewind(m->proc_swaps);
bab45044 1083
4e85aff4 1084 (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
07b0b134 1085
e04aad61 1086 for (i = 1;; i++) {
9fb3675e 1087 _cleanup_free_ char *dev = NULL, *d = NULL;
07b0b134
ML
1088 int prio = 0, k;
1089
646134dc
ZJS
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) {
07b0b134 1098 if (k == EOF)
4e85aff4 1099 break;
07b0b134 1100
628c89cc 1101 log_warning("Failed to parse /proc/swaps:%u.", i);
e04aad61 1102 continue;
07b0b134 1103 }
07b0b134 1104
527b7a42 1105 if (cunescape(dev, UNESCAPE_RELAX, &d) < 0)
628c89cc
LP
1106 return log_oom();
1107
1108 device_found_node(m, d, true, DEVICE_FOUND_SWAP, set_flags);
4e85aff4 1109
628c89cc 1110 k = swap_process_new(m, d, prio, set_flags);
07b0b134 1111 if (k < 0)
e04aad61 1112 r = k;
07b0b134
ML
1113 }
1114
e04aad61
LP
1115 return r;
1116}
1117
718db961
LP
1118static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
1119 Manager *m = userdata;
595ed347 1120 Unit *u;
4e434314
LP
1121 int r;
1122
1123 assert(m);
718db961 1124 assert(revents & EPOLLPRI);
4e434314 1125
646134dc
ZJS
1126 r = swap_load_proc_swaps(m, true);
1127 if (r < 0) {
da927ba9 1128 log_error_errno(r, "Failed to reread /proc/swaps: %m");
e04aad61
LP
1129
1130 /* Reset flags, just in case, for late calls */
595ed347
MS
1131 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1132 Swap *swap = SWAP(u);
e04aad61
LP
1133
1134 swap->is_active = swap->just_activated = false;
1135 }
1136
1137 return 0;
1138 }
1139
1140 manager_dispatch_load_queue(m);
1141
595ed347
MS
1142 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) {
1143 Swap *swap = SWAP(u);
e04aad61
LP
1144
1145 if (!swap->is_active) {
1146 /* This has just been deactivated */
1147
e04aad61
LP
1148 swap_unset_proc_swaps(swap);
1149
1150 switch (swap->state) {
1151
1152 case SWAP_ACTIVE:
e1770af8 1153 swap_enter_dead(swap, SWAP_SUCCESS);
e04aad61
LP
1154 break;
1155
1156 default:
5bcb0f2b 1157 /* Fire again */
e04aad61
LP
1158 swap_set_state(swap, swap->state);
1159 break;
1160 }
1161
628c89cc
LP
1162 if (swap->what)
1163 device_found_node(m, swap->what, false, DEVICE_FOUND_SWAP, true);
1164
e04aad61
LP
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:
e1770af8 1173 swap_enter_active(swap, SWAP_SUCCESS);
e04aad61
LP
1174 break;
1175
5bcb0f2b
LP
1176 case SWAP_ACTIVATING:
1177 swap_set_state(swap, SWAP_ACTIVATING_DONE);
1178 break;
1179
e04aad61
LP
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
1197static Unit *swap_following(Unit *u) {
1198 Swap *s = SWAP(u);
1199 Swap *other, *first = NULL;
1200
1201 assert(s);
1202
cdc89820
ZJS
1203 /* If the user configured the swap through /etc/fstab or
1204 * a device unit, follow that. */
1205
1206 if (s->from_fragment)
e04aad61
LP
1207 return NULL;
1208
caac2704 1209 LIST_FOREACH_OTHERS(same_devnode, other, s)
cdc89820
ZJS
1210 if (other->from_fragment)
1211 return UNIT(other);
1212
b938cb90 1213 /* Otherwise, make everybody follow the unit that's named after
cdc89820
ZJS
1214 * the swap device in the kernel */
1215
1216 if (streq_ptr(s->what, s->devnode))
1217 return NULL;
e04aad61 1218
9670d583
LP
1219 LIST_FOREACH_AFTER(same_devnode, other, s)
1220 if (streq_ptr(other->what, other->devnode))
e04aad61
LP
1221 return UNIT(other);
1222
9670d583
LP
1223 LIST_FOREACH_BEFORE(same_devnode, other, s) {
1224 if (streq_ptr(other->what, other->devnode))
e04aad61
LP
1225 return UNIT(other);
1226
1227 first = other;
1228 }
1229
cdc89820 1230 /* Fall back to the first on the list */
e04aad61 1231 return UNIT(first);
07b0b134
ML
1232}
1233
6210e7fc 1234static int swap_following_set(Unit *u, Set **_set) {
5bcb0f2b 1235 Swap *s = SWAP(u), *other;
6210e7fc
LP
1236 Set *set;
1237 int r;
1238
1239 assert(s);
1240 assert(_set);
1241
9670d583 1242 if (LIST_JUST_US(same_devnode, s)) {
6210e7fc
LP
1243 *_set = NULL;
1244 return 0;
1245 }
1246
d5099efc 1247 set = set_new(NULL);
5bcb0f2b 1248 if (!set)
6210e7fc
LP
1249 return -ENOMEM;
1250
caac2704 1251 LIST_FOREACH_OTHERS(same_devnode, other, s) {
5bcb0f2b
LP
1252 r = set_put(set, other);
1253 if (r < 0)
6210e7fc 1254 goto fail;
5bcb0f2b 1255 }
6210e7fc
LP
1256
1257 *_set = set;
1258 return 1;
1259
1260fail:
1261 set_free(set);
1262 return r;
1263}
1264
07b0b134
ML
1265static void swap_shutdown(Manager *m) {
1266 assert(m);
1267
718db961
LP
1268 m->swap_event_source = sd_event_source_unref(m->swap_event_source);
1269
74ca738f 1270 m->proc_swaps = safe_fclose(m->proc_swaps);
e04aad61 1271
525d3cc7 1272 m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode);
07b0b134
ML
1273}
1274
ba64af90 1275static void swap_enumerate(Manager *m) {
07b0b134 1276 int r;
9670d583 1277
07b0b134
ML
1278 assert(m);
1279
4e434314 1280 if (!m->proc_swaps) {
646134dc 1281 m->proc_swaps = fopen("/proc/swaps", "re");
ba64af90
LP
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 }
4e434314 1290
151b9b96 1291 r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m);
ba64af90
LP
1292 if (r < 0) {
1293 log_error_errno(r, "Failed to watch /proc/swaps: %m");
29083707 1294 goto fail;
ba64af90 1295 }
29083707
LP
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);
ba64af90
LP
1301 if (r < 0) {
1302 log_error_errno(r, "Failed to change /proc/swaps priority: %m");
718db961 1303 goto fail;
ba64af90 1304 }
7dfbe2e3
TG
1305
1306 (void) sd_event_source_set_description(m->swap_event_source, "swap-proc");
4e434314
LP
1307 }
1308
646134dc
ZJS
1309 r = swap_load_proc_swaps(m, false);
1310 if (r < 0)
718db961
LP
1311 goto fail;
1312
ba64af90 1313 return;
07b0b134 1314
718db961
LP
1315fail:
1316 swap_shutdown(m);
07b0b134
ML
1317}
1318
628c89cc 1319int swap_process_device_new(Manager *m, struct udev_device *dev) {
9670d583
LP
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
7410616c
LP
1333 r = unit_name_from_path(dn, ".swap", &e);
1334 if (r < 0)
1335 return r;
9670d583
LP
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;
7410616c 1344 int q;
9670d583 1345
7410616c
LP
1346 q = unit_name_from_path(udev_list_entry_get_name(item), ".swap", &n);
1347 if (q < 0)
1348 return q;
9670d583
LP
1349
1350 s = hashmap_get(m->units, n);
1351 if (s) {
9670d583
LP
1352 q = swap_set_devnode(s, dn);
1353 if (q < 0)
1354 r = q;
1355 }
1356 }
1357
1358 return r;
1359}
1360
628c89cc 1361int swap_process_device_remove(Manager *m, struct udev_device *dev) {
9670d583
LP
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
fdf20a31 1381static void swap_reset_failed(Unit *u) {
5632e374
LP
1382 Swap *s = SWAP(u);
1383
1384 assert(s);
1385
fdf20a31 1386 if (s->state == SWAP_FAILED)
5632e374 1387 swap_set_state(s, SWAP_DEAD);
e04aad61 1388
e1770af8 1389 s->result = SWAP_SUCCESS;
5632e374
LP
1390}
1391
718db961 1392static int swap_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
814cc562 1393 return unit_kill_common(u, who, signo, -1, SWAP(u)->control_pid, error);
8a0867d6
LP
1394}
1395
7a7821c8 1396static int swap_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 1397 Swap *s = SWAP(u);
7a7821c8 1398 usec_t t;
68db7a3b
ZJS
1399 int r;
1400
1401 if (!s->timer_event_source)
1402 return 0;
1403
7a7821c8 1404 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
1405 if (r < 0)
1406 return r;
7a7821c8
LP
1407 if (t == USEC_INFINITY)
1408 return 0;
68db7a3b 1409
7a7821c8 1410 *timeout = t;
68db7a3b
ZJS
1411 return 1;
1412}
1413
1c2e9646 1414static bool swap_supported(void) {
0faacd47
LP
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 &&
75f86906 1424 detect_container() <= 0;
0faacd47
LP
1425
1426 return supported;
1427}
1428
e04aad61
LP
1429static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
1430 [SWAP_EXEC_ACTIVATE] = "ExecActivate",
1431 [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
1432};
1433
1434DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
1435
e1770af8
LP
1436static const char* const swap_result_table[_SWAP_RESULT_MAX] = {
1437 [SWAP_SUCCESS] = "success",
1438 [SWAP_FAILURE_RESOURCES] = "resources",
1439 [SWAP_FAILURE_TIMEOUT] = "timeout",
1440 [SWAP_FAILURE_EXIT_CODE] = "exit-code",
1441 [SWAP_FAILURE_SIGNAL] = "signal",
1442 [SWAP_FAILURE_CORE_DUMP] = "core-dump"
1443};
1444
1445DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult);
1446
07b0b134 1447const UnitVTable swap_vtable = {
7d17cfbc 1448 .object_size = sizeof(Swap),
718db961
LP
1449 .exec_context_offset = offsetof(Swap, exec_context),
1450 .cgroup_context_offset = offsetof(Swap, cgroup_context),
1451 .kill_context_offset = offsetof(Swap, kill_context),
613b411c 1452 .exec_runtime_offset = offsetof(Swap, exec_runtime),
3ef63c31 1453
f975e971
LP
1454 .sections =
1455 "Unit\0"
1456 "Swap\0"
1457 "Install\0",
4ad49000 1458 .private_section = "Swap",
71645aca 1459
e04aad61 1460 .no_alias = true,
07b0b134
ML
1461 .no_instances = true,
1462
4e85aff4 1463 .init = swap_init,
07b0b134 1464 .load = swap_load,
6e620bec 1465 .done = swap_done,
07b0b134
ML
1466
1467 .coldplug = swap_coldplug,
1468
1469 .dump = swap_dump,
1470
1471 .start = swap_start,
1472 .stop = swap_stop,
1473
8a0867d6
LP
1474 .kill = swap_kill,
1475
68db7a3b
ZJS
1476 .get_timeout = swap_get_timeout,
1477
07b0b134
ML
1478 .serialize = swap_serialize,
1479 .deserialize_item = swap_deserialize_item,
1480
1481 .active_state = swap_active_state,
1482 .sub_state_to_string = swap_sub_state_to_string,
1483
1484 .check_gc = swap_check_gc,
1485
e04aad61 1486 .sigchld_event = swap_sigchld_event,
e04aad61
LP
1487
1488 .reset_failed = swap_reset_failed,
1489
718db961 1490 .bus_vtable = bus_swap_vtable,
74c964d3
LP
1491 .bus_set_property = bus_swap_set_property,
1492 .bus_commit_properties = bus_swap_commit_properties,
07b0b134 1493
e04aad61 1494 .following = swap_following,
6210e7fc 1495 .following_set = swap_following_set,
5632e374 1496
6e620bec 1497 .enumerate = swap_enumerate,
c6918296 1498 .shutdown = swap_shutdown,
0faacd47 1499 .supported = swap_supported,
c6918296
MS
1500
1501 .status_message_formats = {
1502 .starting_stopping = {
1503 [0] = "Activating swap %s...",
1504 [1] = "Deactivating swap %s...",
1505 },
1506 .finished_start_job = {
1507 [JOB_DONE] = "Activated swap %s.",
1508 [JOB_FAILED] = "Failed to activate swap %s.",
c6918296
MS
1509 [JOB_TIMEOUT] = "Timed out activating swap %s.",
1510 },
1511 .finished_stop_job = {
1512 [JOB_DONE] = "Deactivated swap %s.",
1513 [JOB_FAILED] = "Failed deactivating swap %s.",
1514 [JOB_TIMEOUT] = "Timed out deactivating swap %s.",
1515 },
1516 },
07b0b134 1517};