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