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