]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
07b0b134 ML |
2 | |
3 | #include <errno.h> | |
07b0b134 ML |
4 | #include <sys/epoll.h> |
5 | #include <sys/stat.h> | |
4f5dd394 | 6 | #include <unistd.h> |
07b0b134 | 7 | |
4366e598 | 8 | #include "sd-device.h" |
b4bbcaa9 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
07b0b134 | 11 | #include "dbus-swap.h" |
6fcbec6f | 12 | #include "dbus-unit.h" |
4366e598 | 13 | #include "device-util.h" |
57b7a260 | 14 | #include "device.h" |
4f5dd394 | 15 | #include "escape.h" |
9a57c629 | 16 | #include "exit-status.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
f97b34a6 | 18 | #include "format-util.h" |
4f5dd394 | 19 | #include "fstab-util.h" |
6bedfcbb | 20 | #include "parse-util.h" |
9eb977db | 21 | #include "path-util.h" |
7b3e062c | 22 | #include "process-util.h" |
d68c645b | 23 | #include "serialize.h" |
4f5dd394 | 24 | #include "special.h" |
8b43440b | 25 | #include "string-table.h" |
07630cea | 26 | #include "string-util.h" |
3ffd4af2 | 27 | #include "swap.h" |
4f5dd394 LP |
28 | #include "unit-name.h" |
29 | #include "unit.h" | |
30 | #include "virt.h" | |
07b0b134 ML |
31 | |
32 | static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = { | |
33 | [SWAP_DEAD] = UNIT_INACTIVE, | |
e04aad61 | 34 | [SWAP_ACTIVATING] = UNIT_ACTIVATING, |
5bcb0f2b | 35 | [SWAP_ACTIVATING_DONE] = UNIT_ACTIVE, |
07b0b134 | 36 | [SWAP_ACTIVE] = UNIT_ACTIVE, |
e04aad61 | 37 | [SWAP_DEACTIVATING] = UNIT_DEACTIVATING, |
e04aad61 LP |
38 | [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING, |
39 | [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING, | |
a8b689b7 YW |
40 | [SWAP_FAILED] = UNIT_FAILED, |
41 | [SWAP_CLEANING] = UNIT_MAINTENANCE, | |
07b0b134 ML |
42 | }; |
43 | ||
718db961 LP |
44 | static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
45 | static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
bcce581d | 46 | static int swap_process_proc_swaps(Manager *m); |
718db961 | 47 | |
dedf3719 LP |
48 | static bool SWAP_STATE_WITH_PROCESS(SwapState state) { |
49 | return IN_SET(state, | |
50 | SWAP_ACTIVATING, | |
51 | SWAP_ACTIVATING_DONE, | |
52 | SWAP_DEACTIVATING, | |
53 | SWAP_DEACTIVATING_SIGTERM, | |
a8b689b7 YW |
54 | SWAP_DEACTIVATING_SIGKILL, |
55 | SWAP_CLEANING); | |
dedf3719 LP |
56 | } |
57 | ||
39c79477 ZJS |
58 | _pure_ static UnitActiveState swap_active_state(Unit *u) { |
59 | assert(u); | |
60 | ||
61 | return state_translation_table[SWAP(u)->state]; | |
62 | } | |
63 | ||
64 | _pure_ static const char *swap_sub_state_to_string(Unit *u) { | |
65 | assert(u); | |
66 | ||
67 | return swap_state_to_string(SWAP(u)->state); | |
68 | } | |
69 | ||
70 | _pure_ static bool swap_may_gc(Unit *u) { | |
71 | Swap *s = SWAP(u); | |
72 | ||
73 | assert(s); | |
74 | ||
75 | if (s->from_proc_swaps) | |
76 | return false; | |
77 | ||
78 | return true; | |
79 | } | |
80 | ||
81 | _pure_ static bool swap_is_extrinsic(Unit *u) { | |
82 | assert(SWAP(u)); | |
83 | ||
84 | return MANAGER_IS_USER(u->manager); | |
85 | } | |
86 | ||
e04aad61 | 87 | static void swap_unset_proc_swaps(Swap *s) { |
9670d583 LP |
88 | assert(s); |
89 | ||
90 | if (!s->from_proc_swaps) | |
91 | return; | |
92 | ||
a1e58e8e | 93 | s->parameters_proc_swaps.what = mfree(s->parameters_proc_swaps.what); |
9670d583 LP |
94 | s->from_proc_swaps = false; |
95 | } | |
96 | ||
97 | static int swap_set_devnode(Swap *s, const char *devnode) { | |
df326b84 | 98 | Hashmap *swaps; |
5bcb0f2b | 99 | Swap *first; |
9670d583 | 100 | int r; |
e04aad61 LP |
101 | |
102 | assert(s); | |
103 | ||
548f6937 | 104 | r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &path_hash_ops); |
9670d583 LP |
105 | if (r < 0) |
106 | return r; | |
e04aad61 | 107 | |
9670d583 | 108 | swaps = UNIT(s)->manager->swaps_by_devnode; |
e04aad61 | 109 | |
9670d583 LP |
110 | if (s->devnode) { |
111 | first = hashmap_get(swaps, s->devnode); | |
e04aad61 | 112 | |
9670d583 LP |
113 | LIST_REMOVE(same_devnode, first, s); |
114 | if (first) | |
115 | hashmap_replace(swaps, first->devnode, first); | |
116 | else | |
117 | hashmap_remove(swaps, s->devnode); | |
5bcb0f2b | 118 | |
a1e58e8e | 119 | s->devnode = mfree(s->devnode); |
9670d583 LP |
120 | } |
121 | ||
122 | if (devnode) { | |
123 | s->devnode = strdup(devnode); | |
124 | if (!s->devnode) | |
125 | return -ENOMEM; | |
126 | ||
127 | first = hashmap_get(swaps, s->devnode); | |
128 | LIST_PREPEND(same_devnode, first, s); | |
129 | ||
130 | return hashmap_replace(swaps, first->devnode, first); | |
131 | } | |
132 | ||
133 | return 0; | |
e04aad61 LP |
134 | } |
135 | ||
f6cebb3b | 136 | static void swap_init(Unit *u) { |
6e620bec LP |
137 | Swap *s = SWAP(u); |
138 | ||
139 | assert(s); | |
1124fe6f | 140 | assert(UNIT(s)->load_state == UNIT_STUB); |
6e620bec | 141 | |
1f19a534 | 142 | s->timeout_usec = u->manager->default_timeout_start_usec; |
e04aad61 | 143 | |
ac155bb8 MS |
144 | s->exec_context.std_output = u->manager->default_std_output; |
145 | s->exec_context.std_error = u->manager->default_std_error; | |
085afe36 | 146 | |
e1770af8 | 147 | s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; |
c8f4d764 | 148 | |
5bcb0f2b | 149 | u->ignore_on_isolate = true; |
e04aad61 LP |
150 | } |
151 | ||
152 | static void swap_unwatch_control_pid(Swap *s) { | |
153 | assert(s); | |
154 | ||
155 | if (s->control_pid <= 0) | |
156 | return; | |
157 | ||
8f03de53 | 158 | unit_unwatch_pid(UNIT(s), TAKE_PID(s->control_pid)); |
6e620bec LP |
159 | } |
160 | ||
4e85aff4 LP |
161 | static void swap_done(Unit *u) { |
162 | Swap *s = SWAP(u); | |
07b0b134 | 163 | |
4e85aff4 | 164 | assert(s); |
07b0b134 | 165 | |
e04aad61 | 166 | swap_unset_proc_swaps(s); |
9670d583 | 167 | swap_set_devnode(s, NULL); |
e04aad61 | 168 | |
a1e58e8e LP |
169 | s->what = mfree(s->what); |
170 | s->parameters_fragment.what = mfree(s->parameters_fragment.what); | |
171 | s->parameters_fragment.options = mfree(s->parameters_fragment.options); | |
86b23b07 | 172 | |
28135da3 | 173 | s->exec_runtime = exec_runtime_free(s->exec_runtime); |
e04aad61 LP |
174 | exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX); |
175 | s->control_command = NULL; | |
176 | ||
29206d46 LP |
177 | dynamic_creds_unref(&s->dynamic_creds); |
178 | ||
e04aad61 LP |
179 | swap_unwatch_control_pid(s); |
180 | ||
5dcadb4c | 181 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
718db961 LP |
182 | } |
183 | ||
36c16a7c | 184 | static int swap_arm_timer(Swap *s, usec_t usec) { |
718db961 LP |
185 | int r; |
186 | ||
187 | assert(s); | |
188 | ||
718db961 | 189 | if (s->timer_event_source) { |
36c16a7c | 190 | r = sd_event_source_set_time(s->timer_event_source, usec); |
718db961 LP |
191 | if (r < 0) |
192 | return r; | |
193 | ||
194 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
195 | } | |
196 | ||
36c16a7c LP |
197 | if (usec == USEC_INFINITY) |
198 | return 0; | |
199 | ||
7dfbe2e3 | 200 | r = sd_event_add_time( |
6a0f1f6d LP |
201 | UNIT(s)->manager->event, |
202 | &s->timer_event_source, | |
203 | CLOCK_MONOTONIC, | |
36c16a7c | 204 | usec, 0, |
6a0f1f6d | 205 | swap_dispatch_timer, s); |
7dfbe2e3 TG |
206 | if (r < 0) |
207 | return r; | |
208 | ||
209 | (void) sd_event_source_set_description(s->timer_event_source, "swap-timer"); | |
210 | ||
211 | return 0; | |
07b0b134 ML |
212 | } |
213 | ||
61f9cf4e LP |
214 | static SwapParameters* swap_get_parameters(Swap *s) { |
215 | assert(s); | |
216 | ||
217 | if (s->from_proc_swaps) | |
218 | return &s->parameters_proc_swaps; | |
219 | ||
220 | if (s->from_fragment) | |
221 | return &s->parameters_fragment; | |
222 | ||
223 | return NULL; | |
224 | } | |
225 | ||
eef85c4a | 226 | static int swap_add_device_dependencies(Swap *s) { |
61f9cf4e LP |
227 | UnitDependencyMask mask; |
228 | SwapParameters *p; | |
44b0d1fd | 229 | int r; |
61f9cf4e | 230 | |
173a8d04 LP |
231 | assert(s); |
232 | ||
233 | if (!s->what) | |
234 | return 0; | |
235 | ||
61f9cf4e | 236 | p = swap_get_parameters(s); |
d4a3494e | 237 | if (!p || !p->what) |
f10af76d LP |
238 | return 0; |
239 | ||
61f9cf4e LP |
240 | mask = s->from_proc_swaps ? UNIT_DEPENDENCY_PROC_SWAP : UNIT_DEPENDENCY_FILE; |
241 | ||
44b0d1fd LP |
242 | if (is_device_path(p->what)) { |
243 | r = unit_add_node_dependency(UNIT(s), p->what, UNIT_REQUIRES, mask); | |
244 | if (r < 0) | |
245 | return r; | |
246 | ||
247 | return unit_add_blockdev_dependency(UNIT(s), p->what, mask); | |
248 | } | |
9b88bb50 | 249 | |
61f9cf4e LP |
250 | /* File based swap devices need to be ordered after systemd-remount-fs.service, since they might need |
251 | * a writable file system. */ | |
252 | return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, true, mask); | |
173a8d04 LP |
253 | } |
254 | ||
2edd4434 | 255 | static int swap_add_default_dependencies(Swap *s) { |
8bf23dc7 FB |
256 | int r; |
257 | ||
2edd4434 LP |
258 | assert(s); |
259 | ||
4c9ea260 LP |
260 | if (!UNIT(s)->default_dependencies) |
261 | return 0; | |
262 | ||
463d0d15 | 263 | if (!MANAGER_IS_SYSTEM(UNIT(s)->manager)) |
6b1dc2bd | 264 | return 0; |
2edd4434 | 265 | |
75f86906 | 266 | if (detect_container() > 0) |
c0387ebf LP |
267 | return 0; |
268 | ||
8bf23dc7 FB |
269 | /* swap units generated for the swap dev links are missing the |
270 | * ordering dep against the swap target. */ | |
35d8c19a | 271 | r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SWAP_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
8bf23dc7 FB |
272 | if (r < 0) |
273 | return r; | |
274 | ||
5a724170 | 275 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
2edd4434 LP |
276 | } |
277 | ||
4e85aff4 | 278 | static int swap_verify(Swap *s) { |
7fd1b19b | 279 | _cleanup_free_ char *e = NULL; |
7410616c | 280 | int r; |
4e85aff4 | 281 | |
75193d41 | 282 | assert(UNIT(s)->load_state == UNIT_LOADED); |
4e85aff4 | 283 | |
7410616c LP |
284 | r = unit_name_from_path(s->what, ".swap", &e); |
285 | if (r < 0) | |
f2341e0a | 286 | return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m"); |
4e85aff4 | 287 | |
d85ff944 YW |
288 | if (!unit_has_name(UNIT(s), e)) |
289 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Value of What= and unit name do not match, not loading."); | |
4e85aff4 | 290 | |
d85ff944 YW |
291 | if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) |
292 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load."); | |
e04aad61 | 293 | |
4e85aff4 LP |
294 | return 0; |
295 | } | |
296 | ||
9670d583 | 297 | static int swap_load_devnode(Swap *s) { |
ca822829 | 298 | _cleanup_free_ char *p = NULL; |
9670d583 | 299 | struct stat st; |
e5ca27b7 | 300 | int r; |
9670d583 LP |
301 | |
302 | assert(s); | |
303 | ||
304 | if (stat(s->what, &st) < 0 || !S_ISBLK(st.st_mode)) | |
305 | return 0; | |
306 | ||
b8a3f619 | 307 | r = devname_from_stat_rdev(&st, &p); |
e5ca27b7 | 308 | if (r < 0) { |
8ed6f81b | 309 | log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
ca822829 | 310 | "Failed to get device node for swap %s: %m", s->what); |
9670d583 | 311 | return 0; |
e5ca27b7 | 312 | } |
9670d583 | 313 | |
9670d583 LP |
314 | return swap_set_devnode(s, p); |
315 | } | |
316 | ||
15332d73 | 317 | static int swap_add_extras(Swap *s) { |
07b0b134 | 318 | int r; |
07b0b134 ML |
319 | |
320 | assert(s); | |
07b0b134 | 321 | |
15332d73 LP |
322 | if (UNIT(s)->fragment_path) |
323 | s->from_fragment = true; | |
07b0b134 | 324 | |
15332d73 LP |
325 | if (!s->what) { |
326 | if (s->parameters_fragment.what) | |
327 | s->what = strdup(s->parameters_fragment.what); | |
328 | else if (s->parameters_proc_swaps.what) | |
329 | s->what = strdup(s->parameters_proc_swaps.what); | |
330 | else { | |
331 | r = unit_name_to_path(UNIT(s)->id, &s->what); | |
5bcb0f2b | 332 | if (r < 0) |
4e85aff4 | 333 | return r; |
5bcb0f2b | 334 | } |
4e85aff4 | 335 | |
15332d73 LP |
336 | if (!s->what) |
337 | return -ENOMEM; | |
338 | } | |
07b0b134 | 339 | |
4ff361cc | 340 | path_simplify(s->what); |
07b0b134 | 341 | |
15332d73 LP |
342 | if (!UNIT(s)->description) { |
343 | r = unit_set_description(UNIT(s), s->what); | |
9670d583 LP |
344 | if (r < 0) |
345 | return r; | |
15332d73 | 346 | } |
9670d583 | 347 | |
15332d73 LP |
348 | r = unit_require_mounts_for(UNIT(s), s->what, UNIT_DEPENDENCY_IMPLICIT); |
349 | if (r < 0) | |
350 | return r; | |
598459ce | 351 | |
15332d73 LP |
352 | r = swap_add_device_dependencies(s); |
353 | if (r < 0) | |
354 | return r; | |
598459ce | 355 | |
15332d73 LP |
356 | r = swap_load_devnode(s); |
357 | if (r < 0) | |
358 | return r; | |
a016b922 | 359 | |
15332d73 LP |
360 | r = unit_patch_contexts(UNIT(s)); |
361 | if (r < 0) | |
362 | return r; | |
363 | ||
364 | r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); | |
365 | if (r < 0) | |
366 | return r; | |
367 | ||
368 | r = unit_set_default_slice(UNIT(s)); | |
369 | if (r < 0) | |
370 | return r; | |
371 | ||
372 | r = swap_add_default_dependencies(s); | |
373 | if (r < 0) | |
374 | return r; | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
379 | static int swap_load(Unit *u) { | |
380 | Swap *s = SWAP(u); | |
75193d41 | 381 | int r, q = 0; |
15332d73 LP |
382 | |
383 | assert(s); | |
384 | assert(u->load_state == UNIT_STUB); | |
385 | ||
386 | /* Load a .swap file */ | |
c3620770 ZJS |
387 | bool fragment_optional = s->from_proc_swaps; |
388 | r = unit_load_fragment_and_dropin(u, !fragment_optional); | |
15332d73 | 389 | |
75193d41 ZJS |
390 | /* Add in some extras, and do so either when we successfully loaded something or when /proc/swaps is |
391 | * already active. */ | |
06721f39 LP |
392 | if (u->load_state == UNIT_LOADED || s->from_proc_swaps) |
393 | q = swap_add_extras(s); | |
15332d73 | 394 | |
06721f39 LP |
395 | if (r < 0) |
396 | return r; | |
397 | if (q < 0) | |
398 | return q; | |
75193d41 ZJS |
399 | if (u->load_state != UNIT_LOADED) |
400 | return 0; | |
07b0b134 ML |
401 | |
402 | return swap_verify(s); | |
403 | } | |
404 | ||
628c89cc | 405 | static int swap_setup_unit( |
4e85aff4 LP |
406 | Manager *m, |
407 | const char *what, | |
e04aad61 | 408 | const char *what_proc_swaps, |
4e85aff4 | 409 | int priority, |
e04aad61 LP |
410 | bool set_flags) { |
411 | ||
7fd1b19b | 412 | _cleanup_free_ char *e = NULL; |
2c7c6144 | 413 | bool delete = false; |
5bcb0f2b | 414 | Unit *u = NULL; |
07b0b134 | 415 | int r; |
4e85aff4 LP |
416 | SwapParameters *p; |
417 | ||
418 | assert(m); | |
419 | assert(what); | |
6b1dc2bd | 420 | assert(what_proc_swaps); |
07b0b134 | 421 | |
7410616c LP |
422 | r = unit_name_from_path(what, ".swap", &e); |
423 | if (r < 0) | |
f2341e0a | 424 | return log_unit_error_errno(u, r, "Failed to generate unit name from path: %m"); |
07b0b134 | 425 | |
e04aad61 | 426 | u = manager_get_unit(m, e); |
6b1dc2bd | 427 | if (u && |
e04aad61 | 428 | SWAP(u)->from_proc_swaps && |
baaa35ad ZJS |
429 | !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps)) |
430 | return log_error_errno(SYNTHETIC_ERRNO(EEXIST), | |
431 | "Swap %s appeared twice with different device paths %s and %s", | |
432 | e, SWAP(u)->parameters_proc_swaps.what, what_proc_swaps); | |
4e85aff4 LP |
433 | |
434 | if (!u) { | |
07b0b134 ML |
435 | delete = true; |
436 | ||
a581e45a | 437 | r = unit_new_for_name(m, sizeof(Swap), e, &u); |
7d17cfbc | 438 | if (r < 0) |
e04aad61 LP |
439 | goto fail; |
440 | ||
7d17cfbc MS |
441 | SWAP(u)->what = strdup(what); |
442 | if (!SWAP(u)->what) { | |
628c89cc | 443 | r = -ENOMEM; |
e04aad61 LP |
444 | goto fail; |
445 | } | |
446 | ||
447 | unit_add_to_load_queue(u); | |
4e85aff4 LP |
448 | } else |
449 | delete = false; | |
07b0b134 | 450 | |
6b1dc2bd | 451 | p = &SWAP(u)->parameters_proc_swaps; |
07b0b134 | 452 | |
6b1dc2bd | 453 | if (!p->what) { |
5bcb0f2b LP |
454 | p->what = strdup(what_proc_swaps); |
455 | if (!p->what) { | |
456 | r = -ENOMEM; | |
6b1dc2bd LP |
457 | goto fail; |
458 | } | |
6b1dc2bd | 459 | } |
e04aad61 | 460 | |
46f94480 LP |
461 | /* The unit is definitely around now, mark it as loaded if it was previously referenced but could not be |
462 | * loaded. After all we can load it now, from the data in /proc/swaps. */ | |
463 | if (IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_ERROR)) { | |
464 | u->load_state = UNIT_LOADED; | |
465 | u->load_error = 0; | |
466 | } | |
467 | ||
6b1dc2bd LP |
468 | if (set_flags) { |
469 | SWAP(u)->is_active = true; | |
470 | SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps; | |
4e85aff4 | 471 | } |
07b0b134 | 472 | |
6b1dc2bd LP |
473 | SWAP(u)->from_proc_swaps = true; |
474 | ||
4e85aff4 | 475 | p->priority = priority; |
7477451b | 476 | p->priority_set = true; |
07b0b134 | 477 | |
4e85aff4 | 478 | unit_add_to_dbus_queue(u); |
07b0b134 ML |
479 | return 0; |
480 | ||
481 | fail: | |
f2341e0a | 482 | log_unit_warning_errno(u, r, "Failed to load swap unit: %m"); |
e04aad61 | 483 | |
c9d5c9c0 | 484 | if (delete) |
07b0b134 ML |
485 | unit_free(u); |
486 | ||
4e85aff4 | 487 | return r; |
07b0b134 ML |
488 | } |
489 | ||
6aeb8c89 | 490 | static void swap_process_new(Manager *m, const char *device, int prio, bool set_flags) { |
4366e598 YW |
491 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
492 | const char *dn, *devlink; | |
493 | struct stat st, st_link; | |
5bcb0f2b | 494 | int r; |
e04aad61 LP |
495 | |
496 | assert(m); | |
497 | ||
6aeb8c89 LP |
498 | if (swap_setup_unit(m, device, device, prio, set_flags) < 0) |
499 | return; | |
e04aad61 | 500 | |
5bcb0f2b LP |
501 | /* If this is a block device, then let's add duplicates for |
502 | * all other names of this block device */ | |
503 | if (stat(device, &st) < 0 || !S_ISBLK(st.st_mode)) | |
6aeb8c89 | 504 | return; |
e04aad61 | 505 | |
a1130022 | 506 | r = sd_device_new_from_stat_rdev(&d, &st); |
6aeb8c89 LP |
507 | if (r < 0) |
508 | return (void) log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, | |
509 | "Failed to allocate device for swap %s: %m", device); | |
e04aad61 | 510 | |
5bcb0f2b | 511 | /* Add the main device node */ |
4366e598 | 512 | if (sd_device_get_devname(d, &dn) >= 0 && !streq(dn, device)) |
6aeb8c89 | 513 | (void) swap_setup_unit(m, dn, device, prio, set_flags); |
e04aad61 | 514 | |
5bcb0f2b | 515 | /* Add additional units for all symlinks */ |
4366e598 | 516 | FOREACH_DEVICE_DEVLINK(d, devlink) { |
e04aad61 | 517 | |
5bcb0f2b | 518 | /* Don't bother with the /dev/block links */ |
4366e598 | 519 | if (streq(devlink, device)) |
5bcb0f2b | 520 | continue; |
e04aad61 | 521 | |
4366e598 | 522 | if (path_startswith(devlink, "/dev/block/")) |
5bcb0f2b | 523 | continue; |
e04aad61 | 524 | |
4366e598 YW |
525 | if (stat(devlink, &st_link) >= 0 && |
526 | (!S_ISBLK(st_link.st_mode) || | |
527 | st_link.st_rdev != st.st_rdev)) | |
528 | continue; | |
e04aad61 | 529 | |
6aeb8c89 | 530 | (void) swap_setup_unit(m, devlink, device, prio, set_flags); |
e04aad61 | 531 | } |
e04aad61 LP |
532 | } |
533 | ||
07b0b134 ML |
534 | static void swap_set_state(Swap *s, SwapState state) { |
535 | SwapState old_state; | |
e04aad61 | 536 | |
07b0b134 ML |
537 | assert(s); |
538 | ||
6fcbec6f LP |
539 | if (s->state != state) |
540 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
541 | ||
07b0b134 ML |
542 | old_state = s->state; |
543 | s->state = state; | |
544 | ||
dedf3719 | 545 | if (!SWAP_STATE_WITH_PROCESS(state)) { |
5dcadb4c | 546 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
e04aad61 LP |
547 | swap_unwatch_control_pid(s); |
548 | s->control_command = NULL; | |
549 | s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; | |
550 | } | |
551 | ||
07b0b134 | 552 | if (state != old_state) |
f2341e0a | 553 | log_unit_debug(UNIT(s), "Changed %s -> %s", swap_state_to_string(old_state), swap_state_to_string(state)); |
07b0b134 | 554 | |
2ad2e41a | 555 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0); |
37cf8fee LP |
556 | |
557 | /* If there other units for the same device node have a job | |
558 | queued it might be worth checking again if it is runnable | |
559 | now. This is necessary, since swap_start() refuses | |
560 | operation with EAGAIN if there's already another job for | |
561 | the same device node queued. */ | |
562 | LIST_FOREACH_OTHERS(same_devnode, other, s) | |
563 | if (UNIT(other)->job) | |
564 | job_add_to_run_queue(UNIT(other)->job); | |
07b0b134 ML |
565 | } |
566 | ||
be847e82 | 567 | static int swap_coldplug(Unit *u) { |
07b0b134 ML |
568 | Swap *s = SWAP(u); |
569 | SwapState new_state = SWAP_DEAD; | |
e04aad61 | 570 | int r; |
07b0b134 ML |
571 | |
572 | assert(s); | |
573 | assert(s->state == SWAP_DEAD); | |
574 | ||
575 | if (s->deserialized_state != s->state) | |
576 | new_state = s->deserialized_state; | |
4e85aff4 | 577 | else if (s->from_proc_swaps) |
07b0b134 ML |
578 | new_state = SWAP_ACTIVE; |
579 | ||
5bcb0f2b LP |
580 | if (new_state == s->state) |
581 | return 0; | |
e04aad61 | 582 | |
c386f588 LP |
583 | if (s->control_pid > 0 && |
584 | pid_is_unwaited(s->control_pid) && | |
dedf3719 | 585 | SWAP_STATE_WITH_PROCESS(new_state)) { |
e04aad61 | 586 | |
f75f613d | 587 | r = unit_watch_pid(UNIT(s), s->control_pid, false); |
5bcb0f2b LP |
588 | if (r < 0) |
589 | return r; | |
e04aad61 | 590 | |
36c16a7c | 591 | r = swap_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec)); |
5bcb0f2b LP |
592 | if (r < 0) |
593 | return r; | |
e04aad61 | 594 | } |
07b0b134 | 595 | |
e8a565cb | 596 | if (!IN_SET(new_state, SWAP_DEAD, SWAP_FAILED)) { |
29206d46 | 597 | (void) unit_setup_dynamic_creds(u); |
e8a565cb YW |
598 | (void) unit_setup_exec_runtime(u); |
599 | } | |
29206d46 | 600 | |
5bcb0f2b | 601 | swap_set_state(s, new_state); |
07b0b134 ML |
602 | return 0; |
603 | } | |
604 | ||
605 | static void swap_dump(Unit *u, FILE *f, const char *prefix) { | |
606 | Swap *s = SWAP(u); | |
4e85aff4 | 607 | SwapParameters *p; |
07b0b134 ML |
608 | |
609 | assert(s); | |
4e85aff4 LP |
610 | assert(f); |
611 | ||
612 | if (s->from_proc_swaps) | |
613 | p = &s->parameters_proc_swaps; | |
614 | else if (s->from_fragment) | |
615 | p = &s->parameters_fragment; | |
b6bfc7bb LP |
616 | else |
617 | p = NULL; | |
07b0b134 ML |
618 | |
619 | fprintf(f, | |
4e85aff4 | 620 | "%sSwap State: %s\n" |
e1770af8 | 621 | "%sResult: %s\n" |
a8b689b7 | 622 | "%sClean Result: %s\n" |
07b0b134 | 623 | "%sWhat: %s\n" |
4e85aff4 | 624 | "%sFrom /proc/swaps: %s\n" |
39c79477 ZJS |
625 | "%sFrom fragment: %s\n" |
626 | "%sExtrinsic: %s\n", | |
07b0b134 | 627 | prefix, swap_state_to_string(s->state), |
e1770af8 | 628 | prefix, swap_result_to_string(s->result), |
a8b689b7 | 629 | prefix, swap_result_to_string(s->clean_result), |
07b0b134 | 630 | prefix, s->what, |
4e85aff4 | 631 | prefix, yes_no(s->from_proc_swaps), |
39c79477 ZJS |
632 | prefix, yes_no(s->from_fragment), |
633 | prefix, yes_no(swap_is_extrinsic(u))); | |
e04aad61 | 634 | |
9670d583 LP |
635 | if (s->devnode) |
636 | fprintf(f, "%sDevice Node: %s\n", prefix, s->devnode); | |
637 | ||
b6bfc7bb LP |
638 | if (p) |
639 | fprintf(f, | |
640 | "%sPriority: %i\n" | |
47cb901e | 641 | "%sOptions: %s\n", |
b6bfc7bb | 642 | prefix, p->priority, |
47cb901e | 643 | prefix, strempty(p->options)); |
b6bfc7bb | 644 | |
9bd0e1b8 YW |
645 | fprintf(f, |
646 | "%sTimeoutSec: %s\n", | |
5291f26d | 647 | prefix, FORMAT_TIMESPAN(s->timeout_usec, USEC_PER_SEC)); |
9bd0e1b8 | 648 | |
e04aad61 LP |
649 | if (s->control_pid > 0) |
650 | fprintf(f, | |
de0671ee ZJS |
651 | "%sControl PID: "PID_FMT"\n", |
652 | prefix, s->control_pid); | |
e04aad61 LP |
653 | |
654 | exec_context_dump(&s->exec_context, f, prefix); | |
4819ff03 | 655 | kill_context_dump(&s->kill_context, f, prefix); |
bc0623df | 656 | cgroup_context_dump(UNIT(s), f, prefix); |
e04aad61 LP |
657 | } |
658 | ||
659 | static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) { | |
3c7416b6 | 660 | |
b9c04eaf | 661 | _cleanup_(exec_params_clear) ExecParameters exec_params = { |
1703fa41 | 662 | .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN, |
254d1313 ZJS |
663 | .stdin_fd = -EBADF, |
664 | .stdout_fd = -EBADF, | |
665 | .stderr_fd = -EBADF, | |
666 | .exec_fd = -EBADF, | |
9fa95f85 | 667 | }; |
3c7416b6 LP |
668 | pid_t pid; |
669 | int r; | |
e04aad61 LP |
670 | |
671 | assert(s); | |
672 | assert(c); | |
673 | assert(_pid); | |
674 | ||
3c7416b6 | 675 | r = unit_prepare_exec(UNIT(s)); |
29206d46 | 676 | if (r < 0) |
3c7416b6 | 677 | return r; |
29206d46 | 678 | |
36c16a7c | 679 | r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec)); |
646134dc | 680 | if (r < 0) |
e04aad61 LP |
681 | goto fail; |
682 | ||
1ad6e8b3 LP |
683 | r = unit_set_exec_params(UNIT(s), &exec_params); |
684 | if (r < 0) | |
685 | goto fail; | |
9fa95f85 | 686 | |
f2341e0a LP |
687 | r = exec_spawn(UNIT(s), |
688 | c, | |
646134dc | 689 | &s->exec_context, |
9fa95f85 | 690 | &exec_params, |
613b411c | 691 | s->exec_runtime, |
29206d46 | 692 | &s->dynamic_creds, |
6bb00842 | 693 | &s->cgroup_context, |
646134dc ZJS |
694 | &pid); |
695 | if (r < 0) | |
e04aad61 LP |
696 | goto fail; |
697 | ||
f75f613d | 698 | r = unit_watch_pid(UNIT(s), pid, true); |
646134dc | 699 | if (r < 0) |
e04aad61 LP |
700 | goto fail; |
701 | ||
702 | *_pid = pid; | |
703 | ||
704 | return 0; | |
705 | ||
706 | fail: | |
5dcadb4c | 707 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
3536f49e | 708 | |
e04aad61 | 709 | return r; |
07b0b134 ML |
710 | } |
711 | ||
e1770af8 | 712 | static void swap_enter_dead(Swap *s, SwapResult f) { |
07b0b134 ML |
713 | assert(s); |
714 | ||
a0fef983 | 715 | if (s->result == SWAP_SUCCESS) |
e1770af8 | 716 | s->result = f; |
e04aad61 | 717 | |
aac99f30 | 718 | unit_log_result(UNIT(s), s->result == SWAP_SUCCESS, swap_result_to_string(s->result)); |
4c425434 | 719 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop); |
29206d46 LP |
720 | swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD); |
721 | ||
28135da3 | 722 | s->exec_runtime = exec_runtime_destroy(s->exec_runtime); |
613b411c | 723 | |
bb0c0d6f | 724 | unit_destroy_runtime_data(UNIT(s), &s->exec_context); |
e66cf1a3 | 725 | |
00d9ef85 LP |
726 | unit_unref_uid_gid(UNIT(s), true); |
727 | ||
29206d46 | 728 | dynamic_creds_destroy(&s->dynamic_creds); |
07b0b134 ML |
729 | } |
730 | ||
e1770af8 | 731 | static void swap_enter_active(Swap *s, SwapResult f) { |
e04aad61 LP |
732 | assert(s); |
733 | ||
a0fef983 | 734 | if (s->result == SWAP_SUCCESS) |
e1770af8 | 735 | s->result = f; |
e04aad61 LP |
736 | |
737 | swap_set_state(s, SWAP_ACTIVE); | |
738 | } | |
739 | ||
50864457 LP |
740 | static void swap_enter_dead_or_active(Swap *s, SwapResult f) { |
741 | assert(s); | |
742 | ||
9c1f969d | 743 | if (s->from_proc_swaps) { |
50864457 | 744 | swap_enter_active(s, f); |
9c1f969d HD |
745 | |
746 | LIST_FOREACH_OTHERS(same_devnode, other, s) | |
747 | if (UNIT(other)->job) | |
748 | swap_enter_dead_or_active(other, f); | |
749 | } else | |
50864457 LP |
750 | swap_enter_dead(s, f); |
751 | } | |
752 | ||
a232ebcc ZJS |
753 | static int state_to_kill_operation(Swap *s, SwapState state) { |
754 | if (state == SWAP_DEACTIVATING_SIGTERM) { | |
755 | if (unit_has_job_type(UNIT(s), JOB_RESTART)) | |
756 | return KILL_RESTART; | |
757 | else | |
758 | return KILL_TERMINATE; | |
759 | } | |
760 | ||
761 | return KILL_KILL; | |
762 | } | |
763 | ||
e1770af8 | 764 | static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) { |
07b0b134 ML |
765 | int r; |
766 | ||
767 | assert(s); | |
e04aad61 | 768 | |
a0fef983 | 769 | if (s->result == SWAP_SUCCESS) |
e1770af8 | 770 | s->result = f; |
e04aad61 | 771 | |
a232ebcc ZJS |
772 | r = unit_kill_context(UNIT(s), |
773 | &s->kill_context, | |
774 | state_to_kill_operation(s, state), | |
775 | -1, | |
776 | s->control_pid, | |
777 | false); | |
cd2086fe LP |
778 | if (r < 0) |
779 | goto fail; | |
e04aad61 | 780 | |
cd2086fe | 781 | if (r > 0) { |
36c16a7c | 782 | r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec)); |
646134dc | 783 | if (r < 0) |
e04aad61 LP |
784 | goto fail; |
785 | ||
786 | swap_set_state(s, state); | |
50864457 | 787 | } else if (state == SWAP_DEACTIVATING_SIGTERM && s->kill_context.send_sigkill) |
ac84d1fb LP |
788 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS); |
789 | else | |
50864457 | 790 | swap_enter_dead_or_active(s, SWAP_SUCCESS); |
e04aad61 LP |
791 | |
792 | return; | |
793 | ||
794 | fail: | |
f2341e0a | 795 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
50864457 | 796 | swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES); |
e04aad61 LP |
797 | } |
798 | ||
799 | static void swap_enter_activating(Swap *s) { | |
bf1d7ba7 KZ |
800 | _cleanup_free_ char *opts = NULL; |
801 | int r; | |
e04aad61 LP |
802 | |
803 | assert(s); | |
804 | ||
4c425434 | 805 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start); |
a4634b21 | 806 | |
e04aad61 LP |
807 | s->control_command_id = SWAP_EXEC_ACTIVATE; |
808 | s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE; | |
07b0b134 | 809 | |
86b23b07 | 810 | if (s->from_fragment) { |
7477451b | 811 | int priority = 0; |
47cb901e | 812 | |
bf1d7ba7 KZ |
813 | r = fstab_find_pri(s->parameters_fragment.options, &priority); |
814 | if (r < 0) | |
af4454cb LP |
815 | log_unit_warning_errno(UNIT(s), r, "Failed to parse swap priority \"%s\", ignoring: %m", s->parameters_fragment.options); |
816 | else if (r > 0 && s->parameters_fragment.priority_set) | |
817 | log_unit_warning(UNIT(s), "Duplicate swap priority configuration by Priority= and Options= fields."); | |
bf1d7ba7 | 818 | |
7477451b | 819 | if (r <= 0 && s->parameters_fragment.priority_set) { |
bf1d7ba7 KZ |
820 | if (s->parameters_fragment.options) |
821 | r = asprintf(&opts, "%s,pri=%i", s->parameters_fragment.options, s->parameters_fragment.priority); | |
822 | else | |
823 | r = asprintf(&opts, "pri=%i", s->parameters_fragment.priority); | |
6fca66a7 LP |
824 | if (r < 0) { |
825 | r = -ENOMEM; | |
bf1d7ba7 | 826 | goto fail; |
6fca66a7 | 827 | } |
e64d5235 | 828 | } |
86b23b07 JS |
829 | } |
830 | ||
cc137d53 | 831 | r = exec_command_set(s->control_command, "/sbin/swapon", "--fixpgsz", NULL); |
86b23b07 JS |
832 | if (r < 0) |
833 | goto fail; | |
4e85aff4 | 834 | |
bf1d7ba7 KZ |
835 | if (s->parameters_fragment.options || opts) { |
836 | r = exec_command_append(s->control_command, "-o", | |
af4454cb | 837 | opts ?: s->parameters_fragment.options, NULL); |
86b23b07 JS |
838 | if (r < 0) |
839 | goto fail; | |
840 | } | |
e04aad61 | 841 | |
86b23b07 | 842 | r = exec_command_append(s->control_command, s->what, NULL); |
e04aad61 LP |
843 | if (r < 0) |
844 | goto fail; | |
845 | ||
846 | swap_unwatch_control_pid(s); | |
847 | ||
646134dc ZJS |
848 | r = swap_spawn(s, s->control_command, &s->control_pid); |
849 | if (r < 0) | |
e04aad61 LP |
850 | goto fail; |
851 | ||
852 | swap_set_state(s, SWAP_ACTIVATING); | |
e04aad61 LP |
853 | return; |
854 | ||
855 | fail: | |
f2341e0a | 856 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapon' task: %m"); |
50864457 | 857 | swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES); |
e04aad61 LP |
858 | } |
859 | ||
e1770af8 | 860 | static void swap_enter_deactivating(Swap *s) { |
e04aad61 LP |
861 | int r; |
862 | ||
863 | assert(s); | |
864 | ||
e04aad61 LP |
865 | s->control_command_id = SWAP_EXEC_DEACTIVATE; |
866 | s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE; | |
867 | ||
646134dc | 868 | r = exec_command_set(s->control_command, |
e04aad61 LP |
869 | "/sbin/swapoff", |
870 | s->what, | |
646134dc ZJS |
871 | NULL); |
872 | if (r < 0) | |
e04aad61 LP |
873 | goto fail; |
874 | ||
875 | swap_unwatch_control_pid(s); | |
876 | ||
646134dc ZJS |
877 | r = swap_spawn(s, s->control_command, &s->control_pid); |
878 | if (r < 0) | |
e04aad61 LP |
879 | goto fail; |
880 | ||
881 | swap_set_state(s, SWAP_DEACTIVATING); | |
882 | ||
883 | return; | |
884 | ||
885 | fail: | |
f2341e0a | 886 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'swapoff' task: %m"); |
50864457 | 887 | swap_enter_dead_or_active(s, SWAP_FAILURE_RESOURCES); |
e04aad61 LP |
888 | } |
889 | ||
31135818 LP |
890 | static void swap_cycle_clear(Swap *s) { |
891 | assert(s); | |
892 | ||
893 | s->result = SWAP_SUCCESS; | |
894 | exec_command_reset_status_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX); | |
895 | UNIT(s)->reset_accounting = true; | |
896 | } | |
897 | ||
e04aad61 | 898 | static int swap_start(Unit *u) { |
03677889 | 899 | Swap *s = SWAP(u); |
07299350 | 900 | int r; |
e04aad61 LP |
901 | |
902 | assert(s); | |
903 | ||
50864457 | 904 | /* We cannot fulfill this request right now, try again later please! */ |
d31ae548 FB |
905 | if (IN_SET(s->state, |
906 | SWAP_DEACTIVATING, | |
907 | SWAP_DEACTIVATING_SIGTERM, | |
a8b689b7 YW |
908 | SWAP_DEACTIVATING_SIGKILL, |
909 | SWAP_CLEANING)) | |
e04aad61 LP |
910 | return -EAGAIN; |
911 | ||
50864457 | 912 | /* Already on it! */ |
e04aad61 LP |
913 | if (s->state == SWAP_ACTIVATING) |
914 | return 0; | |
915 | ||
d31ae548 | 916 | assert(IN_SET(s->state, SWAP_DEAD, SWAP_FAILED)); |
e04aad61 | 917 | |
75f86906 | 918 | if (detect_container() > 0) |
a5c3034f LP |
919 | return -EPERM; |
920 | ||
37cf8fee LP |
921 | /* If there's a job for another swap unit for the same node |
922 | * running, then let's not dispatch this one for now, and wait | |
923 | * until that other job has finished. */ | |
924 | LIST_FOREACH_OTHERS(same_devnode, other, s) | |
925 | if (UNIT(other)->job && UNIT(other)->job->state == JOB_RUNNING) | |
926 | return -EAGAIN; | |
927 | ||
4b58153d LP |
928 | r = unit_acquire_invocation_id(u); |
929 | if (r < 0) | |
930 | return r; | |
931 | ||
31135818 | 932 | swap_cycle_clear(s); |
e04aad61 | 933 | swap_enter_activating(s); |
82a2b6bb | 934 | return 1; |
07b0b134 ML |
935 | } |
936 | ||
937 | static int swap_stop(Unit *u) { | |
938 | Swap *s = SWAP(u); | |
07b0b134 ML |
939 | |
940 | assert(s); | |
941 | ||
50864457 LP |
942 | switch (s->state) { |
943 | ||
944 | case SWAP_DEACTIVATING: | |
945 | case SWAP_DEACTIVATING_SIGTERM: | |
946 | case SWAP_DEACTIVATING_SIGKILL: | |
947 | /* Already on it */ | |
e04aad61 | 948 | return 0; |
07b0b134 | 949 | |
50864457 LP |
950 | case SWAP_ACTIVATING: |
951 | case SWAP_ACTIVATING_DONE: | |
952 | /* There's a control process pending, directly enter kill mode */ | |
953 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_SUCCESS); | |
954 | return 0; | |
07b0b134 | 955 | |
50864457 LP |
956 | case SWAP_ACTIVE: |
957 | if (detect_container() > 0) | |
958 | return -EPERM; | |
a5c3034f | 959 | |
50864457 LP |
960 | swap_enter_deactivating(s); |
961 | return 1; | |
962 | ||
a8b689b7 YW |
963 | case SWAP_CLEANING: |
964 | /* If we are currently cleaning, then abort it, brutally. */ | |
965 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_SUCCESS); | |
966 | return 0; | |
967 | ||
50864457 | 968 | default: |
04499a70 | 969 | assert_not_reached(); |
50864457 | 970 | } |
07b0b134 ML |
971 | } |
972 | ||
973 | static int swap_serialize(Unit *u, FILE *f, FDSet *fds) { | |
974 | Swap *s = SWAP(u); | |
975 | ||
976 | assert(s); | |
977 | assert(f); | |
978 | assert(fds); | |
979 | ||
d68c645b LP |
980 | (void) serialize_item(f, "state", swap_state_to_string(s->state)); |
981 | (void) serialize_item(f, "result", swap_result_to_string(s->result)); | |
e04aad61 LP |
982 | |
983 | if (s->control_pid > 0) | |
d68c645b | 984 | (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid); |
e04aad61 LP |
985 | |
986 | if (s->control_command_id >= 0) | |
d68c645b | 987 | (void) serialize_item(f, "control-command", swap_exec_command_to_string(s->control_command_id)); |
07b0b134 ML |
988 | |
989 | return 0; | |
990 | } | |
991 | ||
992 | static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
993 | Swap *s = SWAP(u); | |
994 | ||
995 | assert(s); | |
996 | assert(fds); | |
997 | ||
998 | if (streq(key, "state")) { | |
999 | SwapState state; | |
1000 | ||
646134dc ZJS |
1001 | state = swap_state_from_string(value); |
1002 | if (state < 0) | |
f2341e0a | 1003 | log_unit_debug(u, "Failed to parse state value: %s", value); |
07b0b134 ML |
1004 | else |
1005 | s->deserialized_state = state; | |
e1770af8 LP |
1006 | } else if (streq(key, "result")) { |
1007 | SwapResult f; | |
1008 | ||
1009 | f = swap_result_from_string(value); | |
1010 | if (f < 0) | |
f2341e0a | 1011 | log_unit_debug(u, "Failed to parse result value: %s", value); |
e1770af8 LP |
1012 | else if (f != SWAP_SUCCESS) |
1013 | s->result = f; | |
e04aad61 LP |
1014 | } else if (streq(key, "control-pid")) { |
1015 | pid_t pid; | |
1016 | ||
1017 | if (parse_pid(value, &pid) < 0) | |
f2341e0a | 1018 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); |
e04aad61 LP |
1019 | else |
1020 | s->control_pid = pid; | |
1021 | ||
1022 | } else if (streq(key, "control-command")) { | |
1023 | SwapExecCommand id; | |
1024 | ||
646134dc ZJS |
1025 | id = swap_exec_command_from_string(value); |
1026 | if (id < 0) | |
f2341e0a | 1027 | log_unit_debug(u, "Failed to parse exec-command value: %s", value); |
e04aad61 LP |
1028 | else { |
1029 | s->control_command_id = id; | |
1030 | s->control_command = s->exec_command + id; | |
1031 | } | |
07b0b134 | 1032 | } else |
f2341e0a | 1033 | log_unit_debug(u, "Unknown serialization key: %s", key); |
07b0b134 ML |
1034 | |
1035 | return 0; | |
1036 | } | |
1037 | ||
e04aad61 LP |
1038 | static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
1039 | Swap *s = SWAP(u); | |
e1770af8 | 1040 | SwapResult f; |
e04aad61 LP |
1041 | |
1042 | assert(s); | |
1043 | assert(pid >= 0); | |
1044 | ||
1045 | if (pid != s->control_pid) | |
1046 | return; | |
1047 | ||
bcce581d LP |
1048 | /* Let's scan /proc/swaps before we process SIGCHLD. For the reasoning see the similar code in |
1049 | * mount.c */ | |
1050 | (void) swap_process_proc_swaps(u->manager); | |
1051 | ||
e04aad61 LP |
1052 | s->control_pid = 0; |
1053 | ||
1f0958f6 | 1054 | if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) |
e1770af8 LP |
1055 | f = SWAP_SUCCESS; |
1056 | else if (code == CLD_EXITED) | |
1057 | f = SWAP_FAILURE_EXIT_CODE; | |
1058 | else if (code == CLD_KILLED) | |
1059 | f = SWAP_FAILURE_SIGNAL; | |
1060 | else if (code == CLD_DUMPED) | |
1061 | f = SWAP_FAILURE_CORE_DUMP; | |
1062 | else | |
04499a70 | 1063 | assert_not_reached(); |
e1770af8 | 1064 | |
a0fef983 | 1065 | if (s->result == SWAP_SUCCESS) |
e1770af8 | 1066 | s->result = f; |
e04aad61 LP |
1067 | |
1068 | if (s->control_command) { | |
6ea832a2 | 1069 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
e1770af8 | 1070 | |
e04aad61 LP |
1071 | s->control_command = NULL; |
1072 | s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; | |
1073 | } | |
1074 | ||
91bbd9b7 | 1075 | unit_log_process_exit( |
5cc2cd1c | 1076 | u, |
91bbd9b7 LP |
1077 | "Swap process", |
1078 | swap_exec_command_to_string(s->control_command_id), | |
5cc2cd1c | 1079 | f == SWAP_SUCCESS, |
91bbd9b7 | 1080 | code, status); |
e04aad61 LP |
1081 | |
1082 | switch (s->state) { | |
1083 | ||
1084 | case SWAP_ACTIVATING: | |
5bcb0f2b | 1085 | case SWAP_ACTIVATING_DONE: |
e04aad61 | 1086 | |
50864457 | 1087 | if (f == SWAP_SUCCESS || s->from_proc_swaps) |
e1770af8 | 1088 | swap_enter_active(s, f); |
e04aad61 | 1089 | else |
e1770af8 | 1090 | swap_enter_dead(s, f); |
e04aad61 LP |
1091 | break; |
1092 | ||
1093 | case SWAP_DEACTIVATING: | |
1094 | case SWAP_DEACTIVATING_SIGKILL: | |
1095 | case SWAP_DEACTIVATING_SIGTERM: | |
1096 | ||
50864457 | 1097 | swap_enter_dead_or_active(s, f); |
e04aad61 LP |
1098 | break; |
1099 | ||
a8b689b7 YW |
1100 | case SWAP_CLEANING: |
1101 | if (s->clean_result == SWAP_SUCCESS) | |
1102 | s->clean_result = f; | |
1103 | ||
1104 | swap_enter_dead(s, SWAP_SUCCESS); | |
1105 | break; | |
1106 | ||
e04aad61 | 1107 | default: |
04499a70 | 1108 | assert_not_reached(); |
e04aad61 LP |
1109 | } |
1110 | ||
1111 | /* Notify clients about changed exit status */ | |
1112 | unit_add_to_dbus_queue(u); | |
e04aad61 LP |
1113 | } |
1114 | ||
718db961 LP |
1115 | static int swap_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
1116 | Swap *s = SWAP(userdata); | |
e04aad61 LP |
1117 | |
1118 | assert(s); | |
718db961 | 1119 | assert(s->timer_event_source == source); |
e04aad61 LP |
1120 | |
1121 | switch (s->state) { | |
1122 | ||
1123 | case SWAP_ACTIVATING: | |
5bcb0f2b | 1124 | case SWAP_ACTIVATING_DONE: |
f2341e0a | 1125 | log_unit_warning(UNIT(s), "Activation timed out. Stopping."); |
50864457 | 1126 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT); |
e04aad61 LP |
1127 | break; |
1128 | ||
1129 | case SWAP_DEACTIVATING: | |
f2341e0a | 1130 | log_unit_warning(UNIT(s), "Deactivation timed out. Stopping."); |
e1770af8 | 1131 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, SWAP_FAILURE_TIMEOUT); |
e04aad61 LP |
1132 | break; |
1133 | ||
e04aad61 | 1134 | case SWAP_DEACTIVATING_SIGTERM: |
4819ff03 | 1135 | if (s->kill_context.send_sigkill) { |
50864457 | 1136 | log_unit_warning(UNIT(s), "Swap process timed out. Killing."); |
e1770af8 | 1137 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, SWAP_FAILURE_TIMEOUT); |
ba035df2 | 1138 | } else { |
50864457 LP |
1139 | log_unit_warning(UNIT(s), "Swap process timed out. Skipping SIGKILL. Ignoring."); |
1140 | swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT); | |
ba035df2 | 1141 | } |
e04aad61 LP |
1142 | break; |
1143 | ||
e04aad61 | 1144 | case SWAP_DEACTIVATING_SIGKILL: |
f2341e0a | 1145 | log_unit_warning(UNIT(s), "Swap process still around after SIGKILL. Ignoring."); |
50864457 | 1146 | swap_enter_dead_or_active(s, SWAP_FAILURE_TIMEOUT); |
e04aad61 LP |
1147 | break; |
1148 | ||
a8b689b7 YW |
1149 | case SWAP_CLEANING: |
1150 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
1151 | ||
1152 | if (s->clean_result == SWAP_SUCCESS) | |
1153 | s->clean_result = SWAP_FAILURE_TIMEOUT; | |
1154 | ||
1155 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, 0); | |
1156 | break; | |
1157 | ||
e04aad61 | 1158 | default: |
04499a70 | 1159 | assert_not_reached(); |
e04aad61 | 1160 | } |
718db961 LP |
1161 | |
1162 | return 0; | |
e04aad61 LP |
1163 | } |
1164 | ||
1165 | static int swap_load_proc_swaps(Manager *m, bool set_flags) { | |
e04aad61 LP |
1166 | assert(m); |
1167 | ||
07b0b134 | 1168 | rewind(m->proc_swaps); |
bab45044 | 1169 | |
4e85aff4 | 1170 | (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n"); |
07b0b134 | 1171 | |
fe96c0f8 | 1172 | for (unsigned i = 1;; i++) { |
9fb3675e | 1173 | _cleanup_free_ char *dev = NULL, *d = NULL; |
07b0b134 ML |
1174 | int prio = 0, k; |
1175 | ||
646134dc ZJS |
1176 | k = fscanf(m->proc_swaps, |
1177 | "%ms " /* device/file */ | |
1178 | "%*s " /* type of swap */ | |
1179 | "%*s " /* swap size */ | |
1180 | "%*s " /* used */ | |
1181 | "%i\n", /* priority */ | |
1182 | &dev, &prio); | |
1183 | if (k != 2) { | |
07b0b134 | 1184 | if (k == EOF) |
4e85aff4 | 1185 | break; |
07b0b134 | 1186 | |
628c89cc | 1187 | log_warning("Failed to parse /proc/swaps:%u.", i); |
e04aad61 | 1188 | continue; |
07b0b134 | 1189 | } |
07b0b134 | 1190 | |
e437538f ZJS |
1191 | ssize_t l = cunescape(dev, UNESCAPE_RELAX, &d); |
1192 | if (l < 0) | |
1193 | return log_error_errno(l, "Failed to unescape device path: %m"); | |
628c89cc | 1194 | |
485ae697 | 1195 | device_found_node(m, d, DEVICE_FOUND_SWAP, DEVICE_FOUND_SWAP); |
4e85aff4 | 1196 | |
eb04385d | 1197 | (void) swap_process_new(m, d, prio, set_flags); |
07b0b134 ML |
1198 | } |
1199 | ||
eb04385d | 1200 | return 0; |
e04aad61 LP |
1201 | } |
1202 | ||
bcce581d | 1203 | static int swap_process_proc_swaps(Manager *m) { |
4e434314 LP |
1204 | int r; |
1205 | ||
1206 | assert(m); | |
4e434314 | 1207 | |
646134dc ZJS |
1208 | r = swap_load_proc_swaps(m, true); |
1209 | if (r < 0) { | |
da927ba9 | 1210 | log_error_errno(r, "Failed to reread /proc/swaps: %m"); |
e04aad61 LP |
1211 | |
1212 | /* Reset flags, just in case, for late calls */ | |
595ed347 MS |
1213 | LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) { |
1214 | Swap *swap = SWAP(u); | |
e04aad61 | 1215 | |
c73f413d FS |
1216 | assert(swap); |
1217 | ||
e04aad61 LP |
1218 | swap->is_active = swap->just_activated = false; |
1219 | } | |
1220 | ||
1221 | return 0; | |
1222 | } | |
1223 | ||
1224 | manager_dispatch_load_queue(m); | |
1225 | ||
595ed347 MS |
1226 | LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_SWAP]) { |
1227 | Swap *swap = SWAP(u); | |
e04aad61 | 1228 | |
c73f413d FS |
1229 | assert(swap); |
1230 | ||
e04aad61 | 1231 | if (!swap->is_active) { |
e04aad61 | 1232 | |
e04aad61 LP |
1233 | swap_unset_proc_swaps(swap); |
1234 | ||
1235 | switch (swap->state) { | |
1236 | ||
1237 | case SWAP_ACTIVE: | |
ba6fbb2c | 1238 | /* This has just been deactivated */ |
e1770af8 | 1239 | swap_enter_dead(swap, SWAP_SUCCESS); |
e04aad61 LP |
1240 | break; |
1241 | ||
1242 | default: | |
5bcb0f2b | 1243 | /* Fire again */ |
e04aad61 LP |
1244 | swap_set_state(swap, swap->state); |
1245 | break; | |
1246 | } | |
1247 | ||
628c89cc | 1248 | if (swap->what) |
b1ba0ce8 | 1249 | device_found_node(m, swap->what, DEVICE_NOT_FOUND, DEVICE_FOUND_SWAP); |
628c89cc | 1250 | |
e04aad61 LP |
1251 | } else if (swap->just_activated) { |
1252 | ||
1253 | /* New swap entry */ | |
1254 | ||
1255 | switch (swap->state) { | |
1256 | ||
1257 | case SWAP_DEAD: | |
1258 | case SWAP_FAILED: | |
31135818 LP |
1259 | (void) unit_acquire_invocation_id(u); |
1260 | swap_cycle_clear(swap); | |
e1770af8 | 1261 | swap_enter_active(swap, SWAP_SUCCESS); |
e04aad61 LP |
1262 | break; |
1263 | ||
5bcb0f2b LP |
1264 | case SWAP_ACTIVATING: |
1265 | swap_set_state(swap, SWAP_ACTIVATING_DONE); | |
1266 | break; | |
1267 | ||
e04aad61 LP |
1268 | default: |
1269 | /* Nothing really changed, but let's | |
1270 | * issue an notification call | |
1271 | * nonetheless, in case somebody is | |
1272 | * waiting for this. */ | |
1273 | swap_set_state(swap, swap->state); | |
1274 | break; | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | /* Reset the flags for later calls */ | |
1279 | swap->is_active = swap->just_activated = false; | |
1280 | } | |
1281 | ||
1282 | return 1; | |
1283 | } | |
1284 | ||
bcce581d | 1285 | static int swap_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 1286 | Manager *m = ASSERT_PTR(userdata); |
bcce581d | 1287 | |
bcce581d LP |
1288 | assert(revents & EPOLLPRI); |
1289 | ||
1290 | return swap_process_proc_swaps(m); | |
1291 | } | |
1292 | ||
e04aad61 LP |
1293 | static Unit *swap_following(Unit *u) { |
1294 | Swap *s = SWAP(u); | |
03677889 | 1295 | Swap *first = NULL; |
e04aad61 LP |
1296 | |
1297 | assert(s); | |
1298 | ||
cdc89820 ZJS |
1299 | /* If the user configured the swap through /etc/fstab or |
1300 | * a device unit, follow that. */ | |
1301 | ||
1302 | if (s->from_fragment) | |
e04aad61 LP |
1303 | return NULL; |
1304 | ||
caac2704 | 1305 | LIST_FOREACH_OTHERS(same_devnode, other, s) |
cdc89820 ZJS |
1306 | if (other->from_fragment) |
1307 | return UNIT(other); | |
1308 | ||
b938cb90 | 1309 | /* Otherwise, make everybody follow the unit that's named after |
cdc89820 ZJS |
1310 | * the swap device in the kernel */ |
1311 | ||
1312 | if (streq_ptr(s->what, s->devnode)) | |
1313 | return NULL; | |
e04aad61 | 1314 | |
bd335c96 | 1315 | LIST_FOREACH(same_devnode, other, s->same_devnode_next) |
9670d583 | 1316 | if (streq_ptr(other->what, other->devnode)) |
e04aad61 LP |
1317 | return UNIT(other); |
1318 | ||
bd335c96 | 1319 | LIST_FOREACH_BACKWARDS(same_devnode, other, s->same_devnode_prev) { |
9670d583 | 1320 | if (streq_ptr(other->what, other->devnode)) |
e04aad61 LP |
1321 | return UNIT(other); |
1322 | ||
1323 | first = other; | |
1324 | } | |
1325 | ||
cdc89820 | 1326 | /* Fall back to the first on the list */ |
e04aad61 | 1327 | return UNIT(first); |
07b0b134 ML |
1328 | } |
1329 | ||
6210e7fc | 1330 | static int swap_following_set(Unit *u, Set **_set) { |
03677889 | 1331 | Swap *s = SWAP(u); |
af4fa99d | 1332 | _cleanup_set_free_ Set *set = NULL; |
6210e7fc LP |
1333 | int r; |
1334 | ||
1335 | assert(s); | |
1336 | assert(_set); | |
1337 | ||
9670d583 | 1338 | if (LIST_JUST_US(same_devnode, s)) { |
6210e7fc LP |
1339 | *_set = NULL; |
1340 | return 0; | |
1341 | } | |
1342 | ||
d5099efc | 1343 | set = set_new(NULL); |
5bcb0f2b | 1344 | if (!set) |
6210e7fc LP |
1345 | return -ENOMEM; |
1346 | ||
caac2704 | 1347 | LIST_FOREACH_OTHERS(same_devnode, other, s) { |
5bcb0f2b LP |
1348 | r = set_put(set, other); |
1349 | if (r < 0) | |
95f14a3e | 1350 | return r; |
5bcb0f2b | 1351 | } |
6210e7fc | 1352 | |
95f14a3e | 1353 | *_set = TAKE_PTR(set); |
6210e7fc | 1354 | return 1; |
6210e7fc LP |
1355 | } |
1356 | ||
07b0b134 ML |
1357 | static void swap_shutdown(Manager *m) { |
1358 | assert(m); | |
1359 | ||
5dcadb4c | 1360 | m->swap_event_source = sd_event_source_disable_unref(m->swap_event_source); |
74ca738f | 1361 | m->proc_swaps = safe_fclose(m->proc_swaps); |
525d3cc7 | 1362 | m->swaps_by_devnode = hashmap_free(m->swaps_by_devnode); |
07b0b134 ML |
1363 | } |
1364 | ||
ba64af90 | 1365 | static void swap_enumerate(Manager *m) { |
07b0b134 | 1366 | int r; |
9670d583 | 1367 | |
07b0b134 ML |
1368 | assert(m); |
1369 | ||
4e434314 | 1370 | if (!m->proc_swaps) { |
646134dc | 1371 | m->proc_swaps = fopen("/proc/swaps", "re"); |
ba64af90 LP |
1372 | if (!m->proc_swaps) { |
1373 | if (errno == ENOENT) | |
cb209a04 | 1374 | log_debug_errno(errno, "Not swap enabled, skipping enumeration."); |
ba64af90 | 1375 | else |
cb209a04 | 1376 | log_warning_errno(errno, "Failed to open /proc/swaps, ignoring: %m"); |
ba64af90 LP |
1377 | |
1378 | return; | |
1379 | } | |
4e434314 | 1380 | |
151b9b96 | 1381 | r = sd_event_add_io(m->event, &m->swap_event_source, fileno(m->proc_swaps), EPOLLPRI, swap_dispatch_io, m); |
ba64af90 LP |
1382 | if (r < 0) { |
1383 | log_error_errno(r, "Failed to watch /proc/swaps: %m"); | |
29083707 | 1384 | goto fail; |
ba64af90 | 1385 | } |
29083707 LP |
1386 | |
1387 | /* Dispatch this before we dispatch SIGCHLD, so that | |
1388 | * we always get the events from /proc/swaps before | |
1389 | * the SIGCHLD of /sbin/swapon. */ | |
83231637 | 1390 | r = sd_event_source_set_priority(m->swap_event_source, SD_EVENT_PRIORITY_NORMAL-10); |
ba64af90 LP |
1391 | if (r < 0) { |
1392 | log_error_errno(r, "Failed to change /proc/swaps priority: %m"); | |
718db961 | 1393 | goto fail; |
ba64af90 | 1394 | } |
7dfbe2e3 TG |
1395 | |
1396 | (void) sd_event_source_set_description(m->swap_event_source, "swap-proc"); | |
4e434314 LP |
1397 | } |
1398 | ||
646134dc ZJS |
1399 | r = swap_load_proc_swaps(m, false); |
1400 | if (r < 0) | |
718db961 LP |
1401 | goto fail; |
1402 | ||
ba64af90 | 1403 | return; |
07b0b134 | 1404 | |
718db961 LP |
1405 | fail: |
1406 | swap_shutdown(m); | |
07b0b134 ML |
1407 | } |
1408 | ||
4366e598 | 1409 | int swap_process_device_new(Manager *m, sd_device *dev) { |
9670d583 | 1410 | _cleanup_free_ char *e = NULL; |
4366e598 | 1411 | const char *dn, *devlink; |
cabf58b2 | 1412 | Unit *u; |
c53aafb7 | 1413 | int r; |
9670d583 LP |
1414 | |
1415 | assert(m); | |
1416 | assert(dev); | |
1417 | ||
e82c6e8b | 1418 | if (sd_device_get_devname(dev, &dn) < 0) |
9670d583 LP |
1419 | return 0; |
1420 | ||
7410616c | 1421 | r = unit_name_from_path(dn, ".swap", &e); |
e82c6e8b LP |
1422 | if (r < 0) { |
1423 | log_debug_errno(r, "Cannot convert device name '%s' to unit name, ignoring: %m", dn); | |
1424 | return 0; | |
1425 | } | |
9670d583 | 1426 | |
cabf58b2 FB |
1427 | u = manager_get_unit(m, e); |
1428 | if (u) | |
1429 | r = swap_set_devnode(SWAP(u), dn); | |
9670d583 | 1430 | |
4366e598 | 1431 | FOREACH_DEVICE_DEVLINK(dev, devlink) { |
9670d583 | 1432 | _cleanup_free_ char *n = NULL; |
7410616c | 1433 | int q; |
9670d583 | 1434 | |
4366e598 | 1435 | q = unit_name_from_path(devlink, ".swap", &n); |
03e52e8c | 1436 | if (q == -EINVAL) /* If the name is not convertible to unit name, we can't manage it */ |
598a6a84 | 1437 | continue; |
7410616c LP |
1438 | if (q < 0) |
1439 | return q; | |
9670d583 | 1440 | |
cabf58b2 FB |
1441 | u = manager_get_unit(m, n); |
1442 | if (u) { | |
1443 | q = swap_set_devnode(SWAP(u), dn); | |
9670d583 LP |
1444 | if (q < 0) |
1445 | r = q; | |
1446 | } | |
1447 | } | |
1448 | ||
1449 | return r; | |
1450 | } | |
1451 | ||
4366e598 | 1452 | int swap_process_device_remove(Manager *m, sd_device *dev) { |
9670d583 | 1453 | const char *dn; |
c53aafb7 | 1454 | int r; |
9670d583 LP |
1455 | Swap *s; |
1456 | ||
4366e598 YW |
1457 | r = sd_device_get_devname(dev, &dn); |
1458 | if (r < 0) | |
9670d583 LP |
1459 | return 0; |
1460 | ||
1461 | while ((s = hashmap_get(m->swaps_by_devnode, dn))) { | |
1462 | int q; | |
1463 | ||
1464 | q = swap_set_devnode(s, NULL); | |
1465 | if (q < 0) | |
1466 | r = q; | |
1467 | } | |
1468 | ||
1469 | return r; | |
1470 | } | |
1471 | ||
fdf20a31 | 1472 | static void swap_reset_failed(Unit *u) { |
5632e374 LP |
1473 | Swap *s = SWAP(u); |
1474 | ||
1475 | assert(s); | |
1476 | ||
fdf20a31 | 1477 | if (s->state == SWAP_FAILED) |
5632e374 | 1478 | swap_set_state(s, SWAP_DEAD); |
e04aad61 | 1479 | |
e1770af8 | 1480 | s->result = SWAP_SUCCESS; |
a8b689b7 | 1481 | s->clean_result = SWAP_SUCCESS; |
5632e374 LP |
1482 | } |
1483 | ||
a721cd00 LP |
1484 | static int swap_kill(Unit *u, KillWho who, int signo, int code, int value, sd_bus_error *error) { |
1485 | return unit_kill_common(u, who, signo, code, value, -1, SWAP(u)->control_pid, error); | |
8a0867d6 LP |
1486 | } |
1487 | ||
7a7821c8 | 1488 | static int swap_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 1489 | Swap *s = SWAP(u); |
7a7821c8 | 1490 | usec_t t; |
68db7a3b ZJS |
1491 | int r; |
1492 | ||
c73f413d FS |
1493 | assert(s); |
1494 | assert(u); | |
1495 | ||
68db7a3b ZJS |
1496 | if (!s->timer_event_source) |
1497 | return 0; | |
1498 | ||
7a7821c8 | 1499 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
1500 | if (r < 0) |
1501 | return r; | |
7a7821c8 LP |
1502 | if (t == USEC_INFINITY) |
1503 | return 0; | |
68db7a3b | 1504 | |
7a7821c8 | 1505 | *timeout = t; |
68db7a3b ZJS |
1506 | return 1; |
1507 | } | |
1508 | ||
1c2e9646 | 1509 | static bool swap_supported(void) { |
0faacd47 LP |
1510 | static int supported = -1; |
1511 | ||
1512 | /* If swap support is not available in the kernel, or we are | |
1513 | * running in a container we don't support swap units, and any | |
1514 | * attempts to starting one should fail immediately. */ | |
1515 | ||
1516 | if (supported < 0) | |
1517 | supported = | |
1518 | access("/proc/swaps", F_OK) >= 0 && | |
75f86906 | 1519 | detect_container() <= 0; |
0faacd47 LP |
1520 | |
1521 | return supported; | |
1522 | } | |
1523 | ||
291d565a LP |
1524 | static int swap_control_pid(Unit *u) { |
1525 | Swap *s = SWAP(u); | |
1526 | ||
1527 | assert(s); | |
1528 | ||
1529 | return s->control_pid; | |
1530 | } | |
1531 | ||
a8b689b7 YW |
1532 | static int swap_clean(Unit *u, ExecCleanMask mask) { |
1533 | _cleanup_strv_free_ char **l = NULL; | |
1534 | Swap *s = SWAP(u); | |
1535 | int r; | |
1536 | ||
1537 | assert(s); | |
1538 | assert(mask != 0); | |
1539 | ||
1540 | if (s->state != SWAP_DEAD) | |
1541 | return -EBUSY; | |
1542 | ||
1543 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); | |
1544 | if (r < 0) | |
1545 | return r; | |
1546 | ||
1547 | if (strv_isempty(l)) | |
1548 | return -EUNATCH; | |
1549 | ||
1550 | swap_unwatch_control_pid(s); | |
1551 | s->clean_result = SWAP_SUCCESS; | |
1552 | s->control_command = NULL; | |
1553 | s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; | |
1554 | ||
1555 | r = swap_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec)); | |
1556 | if (r < 0) | |
1557 | goto fail; | |
1558 | ||
1559 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); | |
1560 | if (r < 0) | |
1561 | goto fail; | |
1562 | ||
1563 | swap_set_state(s, SWAP_CLEANING); | |
1564 | ||
1565 | return 0; | |
1566 | ||
1567 | fail: | |
1568 | log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m"); | |
1569 | s->clean_result = SWAP_FAILURE_RESOURCES; | |
5dcadb4c | 1570 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
a8b689b7 YW |
1571 | return r; |
1572 | } | |
1573 | ||
1574 | static int swap_can_clean(Unit *u, ExecCleanMask *ret) { | |
1575 | Swap *s = SWAP(u); | |
1576 | ||
1577 | assert(s); | |
1578 | ||
1579 | return exec_context_get_clean_mask(&s->exec_context, ret); | |
1580 | } | |
1581 | ||
705578c3 | 1582 | static int swap_can_start(Unit *u) { |
9727f242 DDM |
1583 | Swap *s = SWAP(u); |
1584 | int r; | |
1585 | ||
1586 | assert(s); | |
1587 | ||
1588 | r = unit_test_start_limit(u); | |
1589 | if (r < 0) { | |
1590 | swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT); | |
1591 | return r; | |
1592 | } | |
1593 | ||
705578c3 | 1594 | return 1; |
9727f242 DDM |
1595 | } |
1596 | ||
e04aad61 | 1597 | static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = { |
48d83e33 | 1598 | [SWAP_EXEC_ACTIVATE] = "ExecActivate", |
e04aad61 LP |
1599 | [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate", |
1600 | }; | |
1601 | ||
1602 | DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand); | |
1603 | ||
e1770af8 | 1604 | static const char* const swap_result_table[_SWAP_RESULT_MAX] = { |
48d83e33 ZJS |
1605 | [SWAP_SUCCESS] = "success", |
1606 | [SWAP_FAILURE_RESOURCES] = "resources", | |
1607 | [SWAP_FAILURE_TIMEOUT] = "timeout", | |
1608 | [SWAP_FAILURE_EXIT_CODE] = "exit-code", | |
1609 | [SWAP_FAILURE_SIGNAL] = "signal", | |
1610 | [SWAP_FAILURE_CORE_DUMP] = "core-dump", | |
07299350 | 1611 | [SWAP_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
e1770af8 LP |
1612 | }; |
1613 | ||
1614 | DEFINE_STRING_TABLE_LOOKUP(swap_result, SwapResult); | |
1615 | ||
07b0b134 | 1616 | const UnitVTable swap_vtable = { |
7d17cfbc | 1617 | .object_size = sizeof(Swap), |
718db961 LP |
1618 | .exec_context_offset = offsetof(Swap, exec_context), |
1619 | .cgroup_context_offset = offsetof(Swap, cgroup_context), | |
1620 | .kill_context_offset = offsetof(Swap, kill_context), | |
613b411c | 1621 | .exec_runtime_offset = offsetof(Swap, exec_runtime), |
29206d46 | 1622 | .dynamic_creds_offset = offsetof(Swap, dynamic_creds), |
3ef63c31 | 1623 | |
f975e971 LP |
1624 | .sections = |
1625 | "Unit\0" | |
1626 | "Swap\0" | |
1627 | "Install\0", | |
4ad49000 | 1628 | .private_section = "Swap", |
71645aca | 1629 | |
c80a9a33 LP |
1630 | .can_fail = true, |
1631 | ||
4e85aff4 | 1632 | .init = swap_init, |
07b0b134 | 1633 | .load = swap_load, |
6e620bec | 1634 | .done = swap_done, |
07b0b134 ML |
1635 | |
1636 | .coldplug = swap_coldplug, | |
1637 | ||
1638 | .dump = swap_dump, | |
1639 | ||
1640 | .start = swap_start, | |
1641 | .stop = swap_stop, | |
1642 | ||
8a0867d6 | 1643 | .kill = swap_kill, |
a8b689b7 YW |
1644 | .clean = swap_clean, |
1645 | .can_clean = swap_can_clean, | |
8a0867d6 | 1646 | |
68db7a3b ZJS |
1647 | .get_timeout = swap_get_timeout, |
1648 | ||
07b0b134 ML |
1649 | .serialize = swap_serialize, |
1650 | .deserialize_item = swap_deserialize_item, | |
1651 | ||
1652 | .active_state = swap_active_state, | |
1653 | .sub_state_to_string = swap_sub_state_to_string, | |
1654 | ||
52a12341 YW |
1655 | .will_restart = unit_will_restart_default, |
1656 | ||
f2f725e5 | 1657 | .may_gc = swap_may_gc, |
39c79477 | 1658 | .is_extrinsic = swap_is_extrinsic, |
07b0b134 | 1659 | |
e04aad61 | 1660 | .sigchld_event = swap_sigchld_event, |
e04aad61 LP |
1661 | |
1662 | .reset_failed = swap_reset_failed, | |
1663 | ||
291d565a LP |
1664 | .control_pid = swap_control_pid, |
1665 | ||
74c964d3 LP |
1666 | .bus_set_property = bus_swap_set_property, |
1667 | .bus_commit_properties = bus_swap_commit_properties, | |
07b0b134 | 1668 | |
e04aad61 | 1669 | .following = swap_following, |
6210e7fc | 1670 | .following_set = swap_following_set, |
5632e374 | 1671 | |
6e620bec | 1672 | .enumerate = swap_enumerate, |
c6918296 | 1673 | .shutdown = swap_shutdown, |
0faacd47 | 1674 | .supported = swap_supported, |
c6918296 MS |
1675 | |
1676 | .status_message_formats = { | |
1677 | .starting_stopping = { | |
1678 | [0] = "Activating swap %s...", | |
1679 | [1] = "Deactivating swap %s...", | |
1680 | }, | |
1681 | .finished_start_job = { | |
1682 | [JOB_DONE] = "Activated swap %s.", | |
1683 | [JOB_FAILED] = "Failed to activate swap %s.", | |
c6918296 MS |
1684 | [JOB_TIMEOUT] = "Timed out activating swap %s.", |
1685 | }, | |
1686 | .finished_stop_job = { | |
1687 | [JOB_DONE] = "Deactivated swap %s.", | |
1688 | [JOB_FAILED] = "Failed deactivating swap %s.", | |
1689 | [JOB_TIMEOUT] = "Timed out deactivating swap %s.", | |
1690 | }, | |
1691 | }, | |
9727f242 | 1692 | |
705578c3 | 1693 | .can_start = swap_can_start, |
07b0b134 | 1694 | }; |