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