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