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