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