]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
07b0b134 ML |
2 | |
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
23 | #include <limits.h> | |
24 | #include <unistd.h> | |
25 | #include <fcntl.h> | |
26 | #include <sys/epoll.h> | |
27 | #include <sys/stat.h> | |
28 | #include <sys/swap.h> | |
e04aad61 | 29 | #include <libudev.h> |
07b0b134 ML |
30 | |
31 | #include "unit.h" | |
32 | #include "swap.h" | |
33 | #include "load-fragment.h" | |
34 | #include "load-dropin.h" | |
35 | #include "unit-name.h" | |
36 | #include "dbus-swap.h" | |
514f4ef5 | 37 | #include "special.h" |
8a0867d6 | 38 | #include "bus-errors.h" |
9a57c629 | 39 | #include "exit-status.h" |
f6a6225e | 40 | #include "def.h" |
07b0b134 ML |
41 | |
42 | static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = { | |
43 | [SWAP_DEAD] = UNIT_INACTIVE, | |
e04aad61 | 44 | [SWAP_ACTIVATING] = UNIT_ACTIVATING, |
07b0b134 | 45 | [SWAP_ACTIVE] = UNIT_ACTIVE, |
e04aad61 LP |
46 | [SWAP_DEACTIVATING] = UNIT_DEACTIVATING, |
47 | [SWAP_ACTIVATING_SIGTERM] = UNIT_DEACTIVATING, | |
48 | [SWAP_ACTIVATING_SIGKILL] = UNIT_DEACTIVATING, | |
49 | [SWAP_DEACTIVATING_SIGTERM] = UNIT_DEACTIVATING, | |
50 | [SWAP_DEACTIVATING_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 51 | [SWAP_FAILED] = UNIT_FAILED |
07b0b134 ML |
52 | }; |
53 | ||
e04aad61 LP |
54 | static void swap_unset_proc_swaps(Swap *s) { |
55 | Swap *first; | |
56 | ||
57 | assert(s); | |
58 | ||
59 | if (!s->parameters_proc_swaps.what) | |
60 | return; | |
61 | ||
62 | /* Remove this unit from the chain of swaps which share the | |
63 | * same kernel swap device. */ | |
64 | ||
65 | first = hashmap_get(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what); | |
66 | LIST_REMOVE(Swap, same_proc_swaps, first, s); | |
67 | ||
68 | if (first) | |
69 | hashmap_remove_and_replace(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what, first->parameters_proc_swaps.what, first); | |
70 | else | |
71 | hashmap_remove(s->meta.manager->swaps_by_proc_swaps, s->parameters_proc_swaps.what); | |
72 | ||
73 | free(s->parameters_proc_swaps.what); | |
74 | s->parameters_proc_swaps.what = NULL; | |
75 | } | |
76 | ||
77 | static void swap_init(Unit *u) { | |
6e620bec LP |
78 | Swap *s = SWAP(u); |
79 | ||
80 | assert(s); | |
4e85aff4 | 81 | assert(s->meta.load_state == UNIT_STUB); |
6e620bec | 82 | |
e04aad61 LP |
83 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; |
84 | ||
85 | exec_context_init(&s->exec_context); | |
0a494f1f | 86 | s->exec_context.std_output = EXEC_OUTPUT_KMSG; |
e04aad61 | 87 | |
4e85aff4 | 88 | s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1; |
e04aad61 LP |
89 | |
90 | s->timer_watch.type = WATCH_INVALID; | |
91 | ||
92 | s->control_command_id = _MOUNT_EXEC_COMMAND_INVALID; | |
93 | } | |
94 | ||
95 | static void swap_unwatch_control_pid(Swap *s) { | |
96 | assert(s); | |
97 | ||
98 | if (s->control_pid <= 0) | |
99 | return; | |
100 | ||
101 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
102 | s->control_pid = 0; | |
6e620bec LP |
103 | } |
104 | ||
4e85aff4 LP |
105 | static void swap_done(Unit *u) { |
106 | Swap *s = SWAP(u); | |
07b0b134 | 107 | |
4e85aff4 | 108 | assert(s); |
07b0b134 | 109 | |
e04aad61 LP |
110 | swap_unset_proc_swaps(s); |
111 | ||
4e85aff4 | 112 | free(s->what); |
e04aad61 LP |
113 | s->what = NULL; |
114 | ||
4e85aff4 | 115 | free(s->parameters_etc_fstab.what); |
4e85aff4 | 116 | free(s->parameters_fragment.what); |
e04aad61 LP |
117 | s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL; |
118 | ||
119 | exec_context_done(&s->exec_context); | |
120 | exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX); | |
121 | s->control_command = NULL; | |
122 | ||
123 | swap_unwatch_control_pid(s); | |
124 | ||
125 | unit_unwatch_timer(u, &s->timer_watch); | |
07b0b134 ML |
126 | } |
127 | ||
6e2ef85b LP |
128 | int swap_add_one_mount_link(Swap *s, Mount *m) { |
129 | int r; | |
130 | ||
131 | assert(s); | |
132 | assert(m); | |
133 | ||
134 | if (s->meta.load_state != UNIT_LOADED || | |
135 | m->meta.load_state != UNIT_LOADED) | |
136 | return 0; | |
137 | ||
8407a5d0 LP |
138 | if (is_device_path(s->what)) |
139 | return 0; | |
140 | ||
6e2ef85b LP |
141 | if (!path_startswith(s->what, m->where)) |
142 | return 0; | |
143 | ||
2c966c03 | 144 | if ((r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0) |
6e2ef85b LP |
145 | return r; |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static int swap_add_mount_links(Swap *s) { | |
151 | Meta *other; | |
152 | int r; | |
153 | ||
154 | assert(s); | |
155 | ||
156 | LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT]) | |
157 | if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0) | |
158 | return r; | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
07b0b134 | 163 | static int swap_add_target_links(Swap *s) { |
07b0b134 | 164 | Unit *tu; |
4e85aff4 | 165 | SwapParameters *p; |
07b0b134 ML |
166 | int r; |
167 | ||
4e85aff4 LP |
168 | assert(s); |
169 | ||
170 | if (s->from_fragment) | |
171 | p = &s->parameters_fragment; | |
172 | else if (s->from_etc_fstab) | |
173 | p = &s->parameters_etc_fstab; | |
174 | else | |
175 | return 0; | |
176 | ||
398ef8ba | 177 | if ((r = manager_load_unit(s->meta.manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0) |
07b0b134 ML |
178 | return r; |
179 | ||
81bf310e | 180 | if (!p->noauto && |
85385610 | 181 | !p->nofail && |
81bf310e | 182 | (p->handle || s->meta.manager->swap_auto) && |
510051fc | 183 | s->from_etc_fstab && |
81bf310e | 184 | s->meta.manager->running_as == MANAGER_SYSTEM) |
6e2ef85b LP |
185 | if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0) |
186 | return r; | |
07b0b134 ML |
187 | |
188 | return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true); | |
189 | } | |
190 | ||
173a8d04 LP |
191 | static int swap_add_device_links(Swap *s) { |
192 | SwapParameters *p; | |
193 | ||
194 | assert(s); | |
195 | ||
196 | if (!s->what) | |
197 | return 0; | |
198 | ||
199 | if (s->from_fragment) | |
200 | p = &s->parameters_fragment; | |
201 | else if (s->from_etc_fstab) | |
202 | p = &s->parameters_etc_fstab; | |
203 | else | |
204 | return 0; | |
205 | ||
cfcfd4ae LP |
206 | if (is_device_path(s->what)) |
207 | return unit_add_node_link(UNIT(s), s->what, | |
208 | !p->noauto && p->nofail && | |
209 | s->meta.manager->running_as == MANAGER_SYSTEM); | |
210 | else | |
211 | /* File based swap devices need to be ordered after | |
212 | * remount-rootfs.service, since they might need a | |
213 | * writable file system. */ | |
214 | return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true); | |
173a8d04 LP |
215 | } |
216 | ||
2edd4434 LP |
217 | static int swap_add_default_dependencies(Swap *s) { |
218 | int r; | |
219 | ||
220 | assert(s); | |
221 | ||
222 | if (s->meta.manager->running_as == MANAGER_SYSTEM) { | |
223 | ||
ead8e478 | 224 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0) |
e04aad61 | 225 | return r; |
2edd4434 LP |
226 | } |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
4e85aff4 LP |
231 | static int swap_verify(Swap *s) { |
232 | bool b; | |
233 | char *e; | |
234 | ||
4cd1fbcc | 235 | if (s->meta.load_state != UNIT_LOADED) |
4e85aff4 LP |
236 | return 0; |
237 | ||
238 | if (!(e = unit_name_from_path(s->what, ".swap"))) | |
239 | return -ENOMEM; | |
240 | ||
241 | b = unit_has_name(UNIT(s), e); | |
242 | free(e); | |
243 | ||
244 | if (!b) { | |
4cd1fbcc | 245 | log_error("%s: Value of \"What\" and unit name do not match, not loading.\n", s->meta.id); |
4e85aff4 LP |
246 | return -EINVAL; |
247 | } | |
248 | ||
e04aad61 LP |
249 | if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) { |
250 | log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); | |
251 | return -EINVAL; | |
252 | } | |
253 | ||
4e85aff4 LP |
254 | return 0; |
255 | } | |
256 | ||
07b0b134 ML |
257 | static int swap_load(Unit *u) { |
258 | int r; | |
259 | Swap *s = SWAP(u); | |
260 | ||
261 | assert(s); | |
262 | assert(u->meta.load_state == UNIT_STUB); | |
263 | ||
264 | /* Load a .swap file */ | |
265 | if ((r = unit_load_fragment_and_dropin_optional(u)) < 0) | |
266 | return r; | |
267 | ||
268 | if (u->meta.load_state == UNIT_LOADED) { | |
27abbe82 LP |
269 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) |
270 | return r; | |
4e85aff4 LP |
271 | |
272 | if (s->meta.fragment_path) | |
273 | s->from_fragment = true; | |
274 | ||
275 | if (!s->what) { | |
276 | if (s->parameters_fragment.what) | |
277 | s->what = strdup(s->parameters_fragment.what); | |
278 | else if (s->parameters_etc_fstab.what) | |
279 | s->what = strdup(s->parameters_etc_fstab.what); | |
280 | else if (s->parameters_proc_swaps.what) | |
281 | s->what = strdup(s->parameters_proc_swaps.what); | |
282 | else | |
283 | s->what = unit_name_to_path(u->meta.id); | |
284 | ||
285 | if (!s->what) | |
07b0b134 | 286 | return -ENOMEM; |
4e85aff4 | 287 | } |
07b0b134 ML |
288 | |
289 | path_kill_slashes(s->what); | |
290 | ||
4e85aff4 LP |
291 | if (!s->meta.description) |
292 | if ((r = unit_set_description(u, s->what)) < 0) | |
293 | return r; | |
294 | ||
173a8d04 | 295 | if ((r = swap_add_device_links(s)) < 0) |
07b0b134 ML |
296 | return r; |
297 | ||
6e2ef85b LP |
298 | if ((r = swap_add_mount_links(s)) < 0) |
299 | return r; | |
07b0b134 ML |
300 | |
301 | if ((r = swap_add_target_links(s)) < 0) | |
302 | return r; | |
2edd4434 | 303 | |
d686d8a9 | 304 | if ((r = unit_add_default_cgroups(u)) < 0) |
f1a1cd64 LP |
305 | return r; |
306 | ||
2edd4434 LP |
307 | if (s->meta.default_dependencies) |
308 | if ((r = swap_add_default_dependencies(s)) < 0) | |
309 | return r; | |
07b0b134 ML |
310 | } |
311 | ||
312 | return swap_verify(s); | |
313 | } | |
314 | ||
4e85aff4 LP |
315 | int swap_add_one( |
316 | Manager *m, | |
317 | const char *what, | |
e04aad61 | 318 | const char *what_proc_swaps, |
4e85aff4 LP |
319 | int priority, |
320 | bool noauto, | |
173a8d04 | 321 | bool nofail, |
4e85aff4 | 322 | bool handle, |
e04aad61 LP |
323 | bool set_flags) { |
324 | ||
4e85aff4 | 325 | Unit *u = NULL; |
e04aad61 | 326 | char *e = NULL, *wp = NULL; |
2c7c6144 | 327 | bool delete = false; |
07b0b134 | 328 | int r; |
4e85aff4 LP |
329 | SwapParameters *p; |
330 | ||
331 | assert(m); | |
332 | assert(what); | |
07b0b134 ML |
333 | |
334 | if (!(e = unit_name_from_path(what, ".swap"))) | |
335 | return -ENOMEM; | |
336 | ||
e04aad61 LP |
337 | u = manager_get_unit(m, e); |
338 | ||
339 | if (what_proc_swaps && | |
340 | u && | |
341 | SWAP(u)->from_proc_swaps && | |
342 | !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps)) | |
343 | return -EEXIST; | |
4e85aff4 LP |
344 | |
345 | if (!u) { | |
07b0b134 ML |
346 | delete = true; |
347 | ||
348 | if (!(u = unit_new(m))) { | |
349 | free(e); | |
350 | return -ENOMEM; | |
351 | } | |
e04aad61 LP |
352 | |
353 | if ((r = unit_add_name(u, e)) < 0) | |
354 | goto fail; | |
355 | ||
356 | if (!(SWAP(u)->what = strdup(what))) { | |
357 | r = -ENOMEM; | |
358 | goto fail; | |
359 | } | |
360 | ||
361 | unit_add_to_load_queue(u); | |
4e85aff4 LP |
362 | } else |
363 | delete = false; | |
07b0b134 | 364 | |
e04aad61 LP |
365 | if (what_proc_swaps) { |
366 | Swap *first; | |
07b0b134 | 367 | |
4e85aff4 | 368 | p = &SWAP(u)->parameters_proc_swaps; |
e04aad61 LP |
369 | |
370 | if (!p->what) { | |
371 | if (!(wp = strdup(what_proc_swaps))) { | |
372 | r = -ENOMEM; | |
373 | goto fail; | |
374 | } | |
375 | ||
376 | if (!m->swaps_by_proc_swaps) | |
377 | if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) { | |
378 | r = -ENOMEM; | |
379 | goto fail; | |
380 | } | |
381 | ||
382 | free(p->what); | |
383 | p->what = wp; | |
384 | ||
385 | first = hashmap_get(m->swaps_by_proc_swaps, wp); | |
386 | LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u)); | |
387 | ||
388 | if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0) | |
389 | goto fail; | |
390 | } | |
391 | ||
392 | if (set_flags) { | |
393 | SWAP(u)->is_active = true; | |
394 | SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps; | |
395 | } | |
396 | ||
4e85aff4 | 397 | SWAP(u)->from_proc_swaps = true; |
e04aad61 | 398 | |
4e85aff4 LP |
399 | } else { |
400 | p = &SWAP(u)->parameters_etc_fstab; | |
e04aad61 LP |
401 | |
402 | if (!(wp = strdup(what))) { | |
403 | r = -ENOMEM; | |
404 | goto fail; | |
405 | } | |
406 | ||
407 | free(p->what); | |
408 | p->what = wp; | |
409 | ||
4e85aff4 LP |
410 | SWAP(u)->from_etc_fstab = true; |
411 | } | |
07b0b134 | 412 | |
4e85aff4 LP |
413 | p->priority = priority; |
414 | p->noauto = noauto; | |
173a8d04 | 415 | p->nofail = nofail; |
4e85aff4 | 416 | p->handle = handle; |
07b0b134 | 417 | |
4e85aff4 | 418 | unit_add_to_dbus_queue(u); |
07b0b134 | 419 | |
4e85aff4 | 420 | free(e); |
07b0b134 ML |
421 | |
422 | return 0; | |
423 | ||
424 | fail: | |
e04aad61 LP |
425 | log_warning("Failed to load swap unit: %s", strerror(-r)); |
426 | ||
427 | free(wp); | |
4e85aff4 LP |
428 | free(e); |
429 | ||
430 | if (delete && u) | |
07b0b134 ML |
431 | unit_free(u); |
432 | ||
4e85aff4 | 433 | return r; |
07b0b134 ML |
434 | } |
435 | ||
e04aad61 LP |
436 | static int swap_process_new_swap(Manager *m, const char *device, int prio, bool set_flags) { |
437 | struct stat st; | |
438 | int r = 0, k; | |
439 | ||
440 | assert(m); | |
441 | ||
442 | if (stat(device, &st) >= 0 && S_ISBLK(st.st_mode)) { | |
443 | struct udev_device *d; | |
444 | const char *dn; | |
445 | struct udev_list_entry *item = NULL, *first = NULL; | |
446 | ||
447 | /* So this is a proper swap device. Create swap units | |
448 | * for all names this swap device is known under */ | |
449 | ||
450 | if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev))) | |
451 | return -ENOMEM; | |
452 | ||
453 | if ((dn = udev_device_get_devnode(d))) | |
454 | r = swap_add_one(m, dn, device, prio, false, false, false, set_flags); | |
455 | ||
456 | /* Add additional units for all symlinks */ | |
457 | first = udev_device_get_devlinks_list_entry(d); | |
458 | udev_list_entry_foreach(item, first) { | |
459 | const char *p; | |
460 | ||
461 | /* Don't bother with the /dev/block links */ | |
462 | p = udev_list_entry_get_name(item); | |
463 | ||
464 | if (path_startswith(p, "/dev/block/")) | |
465 | continue; | |
466 | ||
467 | if (stat(p, &st) >= 0) | |
468 | if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d)) | |
469 | continue; | |
470 | ||
471 | if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0) | |
472 | r = k; | |
473 | } | |
474 | ||
475 | udev_device_unref(d); | |
476 | } | |
477 | ||
478 | if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0) | |
479 | r = k; | |
480 | ||
481 | return r; | |
482 | } | |
483 | ||
07b0b134 ML |
484 | static void swap_set_state(Swap *s, SwapState state) { |
485 | SwapState old_state; | |
e04aad61 | 486 | |
07b0b134 ML |
487 | assert(s); |
488 | ||
489 | old_state = s->state; | |
490 | s->state = state; | |
491 | ||
e04aad61 LP |
492 | if (state != SWAP_ACTIVATING && |
493 | state != SWAP_ACTIVATING_SIGTERM && | |
494 | state != SWAP_ACTIVATING_SIGKILL && | |
495 | state != SWAP_DEACTIVATING && | |
496 | state != SWAP_DEACTIVATING_SIGTERM && | |
497 | state != SWAP_DEACTIVATING_SIGKILL) { | |
498 | unit_unwatch_timer(UNIT(s), &s->timer_watch); | |
499 | swap_unwatch_control_pid(s); | |
500 | s->control_command = NULL; | |
501 | s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; | |
502 | } | |
503 | ||
07b0b134 ML |
504 | if (state != old_state) |
505 | log_debug("%s changed %s -> %s", | |
4cd1fbcc | 506 | s->meta.id, |
07b0b134 ML |
507 | swap_state_to_string(old_state), |
508 | swap_state_to_string(state)); | |
509 | ||
e2f3b44c | 510 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true); |
07b0b134 ML |
511 | } |
512 | ||
513 | static int swap_coldplug(Unit *u) { | |
514 | Swap *s = SWAP(u); | |
515 | SwapState new_state = SWAP_DEAD; | |
e04aad61 | 516 | int r; |
07b0b134 ML |
517 | |
518 | assert(s); | |
519 | assert(s->state == SWAP_DEAD); | |
520 | ||
521 | if (s->deserialized_state != s->state) | |
522 | new_state = s->deserialized_state; | |
4e85aff4 | 523 | else if (s->from_proc_swaps) |
07b0b134 ML |
524 | new_state = SWAP_ACTIVE; |
525 | ||
e04aad61 LP |
526 | if (new_state != s->state) { |
527 | ||
528 | if (new_state == SWAP_ACTIVATING || | |
529 | new_state == SWAP_ACTIVATING_SIGTERM || | |
530 | new_state == SWAP_ACTIVATING_SIGKILL || | |
531 | new_state == SWAP_DEACTIVATING || | |
532 | new_state == SWAP_DEACTIVATING_SIGTERM || | |
533 | new_state == SWAP_DEACTIVATING_SIGKILL) { | |
534 | ||
535 | if (s->control_pid <= 0) | |
536 | return -EBADMSG; | |
537 | ||
538 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
539 | return r; | |
540 | ||
541 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
542 | return r; | |
543 | } | |
544 | ||
4e85aff4 | 545 | swap_set_state(s, new_state); |
e04aad61 | 546 | } |
07b0b134 ML |
547 | |
548 | return 0; | |
549 | } | |
550 | ||
551 | static void swap_dump(Unit *u, FILE *f, const char *prefix) { | |
552 | Swap *s = SWAP(u); | |
4e85aff4 | 553 | SwapParameters *p; |
07b0b134 ML |
554 | |
555 | assert(s); | |
4e85aff4 LP |
556 | assert(f); |
557 | ||
558 | if (s->from_proc_swaps) | |
559 | p = &s->parameters_proc_swaps; | |
560 | else if (s->from_fragment) | |
561 | p = &s->parameters_fragment; | |
562 | else | |
563 | p = &s->parameters_etc_fstab; | |
07b0b134 ML |
564 | |
565 | fprintf(f, | |
4e85aff4 | 566 | "%sSwap State: %s\n" |
07b0b134 ML |
567 | "%sWhat: %s\n" |
568 | "%sPriority: %i\n" | |
4e85aff4 | 569 | "%sNoAuto: %s\n" |
173a8d04 | 570 | "%sNoFail: %s\n" |
4e85aff4 LP |
571 | "%sHandle: %s\n" |
572 | "%sFrom /etc/fstab: %s\n" | |
573 | "%sFrom /proc/swaps: %s\n" | |
574 | "%sFrom fragment: %s\n", | |
07b0b134 ML |
575 | prefix, swap_state_to_string(s->state), |
576 | prefix, s->what, | |
4e85aff4 LP |
577 | prefix, p->priority, |
578 | prefix, yes_no(p->noauto), | |
173a8d04 | 579 | prefix, yes_no(p->nofail), |
4e85aff4 LP |
580 | prefix, yes_no(p->handle), |
581 | prefix, yes_no(s->from_etc_fstab), | |
582 | prefix, yes_no(s->from_proc_swaps), | |
583 | prefix, yes_no(s->from_fragment)); | |
e04aad61 LP |
584 | |
585 | if (s->control_pid > 0) | |
586 | fprintf(f, | |
587 | "%sControl PID: %lu\n", | |
588 | prefix, (unsigned long) s->control_pid); | |
589 | ||
590 | exec_context_dump(&s->exec_context, f, prefix); | |
591 | } | |
592 | ||
593 | static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) { | |
594 | pid_t pid; | |
595 | int r; | |
596 | ||
597 | assert(s); | |
598 | assert(c); | |
599 | assert(_pid); | |
600 | ||
601 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
602 | goto fail; | |
603 | ||
604 | if ((r = exec_spawn(c, | |
605 | NULL, | |
606 | &s->exec_context, | |
607 | NULL, 0, | |
608 | s->meta.manager->environment, | |
609 | true, | |
610 | true, | |
611 | true, | |
612 | s->meta.manager->confirm_spawn, | |
613 | s->meta.cgroup_bondings, | |
614 | &pid)) < 0) | |
615 | goto fail; | |
616 | ||
617 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) | |
618 | /* FIXME: we need to do something here */ | |
619 | goto fail; | |
620 | ||
621 | *_pid = pid; | |
622 | ||
623 | return 0; | |
624 | ||
625 | fail: | |
626 | unit_unwatch_timer(UNIT(s), &s->timer_watch); | |
627 | ||
628 | return r; | |
07b0b134 ML |
629 | } |
630 | ||
631 | static void swap_enter_dead(Swap *s, bool success) { | |
632 | assert(s); | |
633 | ||
e04aad61 LP |
634 | if (!success) |
635 | s->failure = true; | |
636 | ||
637 | swap_set_state(s, s->failure ? SWAP_FAILED : SWAP_DEAD); | |
07b0b134 ML |
638 | } |
639 | ||
e04aad61 LP |
640 | static void swap_enter_active(Swap *s, bool success) { |
641 | assert(s); | |
642 | ||
643 | if (!success) | |
644 | s->failure = true; | |
645 | ||
646 | swap_set_state(s, SWAP_ACTIVE); | |
647 | } | |
648 | ||
649 | static void swap_enter_signal(Swap *s, SwapState state, bool success) { | |
07b0b134 | 650 | int r; |
e04aad61 LP |
651 | Set *pid_set = NULL; |
652 | bool wait_for_exit = false; | |
07b0b134 ML |
653 | |
654 | assert(s); | |
e04aad61 LP |
655 | |
656 | if (!success) | |
657 | s->failure = true; | |
658 | ||
659 | if (s->exec_context.kill_mode != KILL_NONE) { | |
660 | int sig = (state == SWAP_ACTIVATING_SIGTERM || | |
661 | state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
662 | ||
663 | if (s->control_pid > 0) { | |
cd25cce9 | 664 | if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH) |
e04aad61 LP |
665 | |
666 | log_warning("Failed to kill control process %li: %m", (long) s->control_pid); | |
667 | else | |
668 | wait_for_exit = true; | |
669 | } | |
670 | ||
671 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { | |
672 | ||
673 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) { | |
674 | r = -ENOMEM; | |
675 | goto fail; | |
676 | } | |
677 | ||
678 | /* Exclude the control pid from being killed via the cgroup */ | |
679 | if (s->control_pid > 0) | |
680 | if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) | |
681 | goto fail; | |
682 | ||
430c18ed | 683 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) { |
e04aad61 LP |
684 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) |
685 | log_warning("Failed to kill control group: %s", strerror(-r)); | |
686 | } else if (r > 0) | |
687 | wait_for_exit = true; | |
688 | ||
689 | set_free(pid_set); | |
da19d5c1 | 690 | pid_set = NULL; |
e04aad61 LP |
691 | } |
692 | } | |
693 | ||
694 | if (wait_for_exit) { | |
695 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
696 | goto fail; | |
697 | ||
698 | swap_set_state(s, state); | |
699 | } else | |
700 | swap_enter_dead(s, true); | |
701 | ||
702 | return; | |
703 | ||
704 | fail: | |
705 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); | |
706 | ||
707 | swap_enter_dead(s, false); | |
708 | ||
709 | if (pid_set) | |
710 | set_free(pid_set); | |
711 | } | |
712 | ||
713 | static void swap_enter_activating(Swap *s) { | |
714 | int r, priority; | |
715 | ||
716 | assert(s); | |
717 | ||
718 | s->control_command_id = SWAP_EXEC_ACTIVATE; | |
719 | s->control_command = s->exec_command + SWAP_EXEC_ACTIVATE; | |
07b0b134 | 720 | |
4e85aff4 LP |
721 | if (s->from_fragment) |
722 | priority = s->parameters_fragment.priority; | |
723 | else if (s->from_etc_fstab) | |
724 | priority = s->parameters_etc_fstab.priority; | |
e04aad61 LP |
725 | else |
726 | priority = -1; | |
4e85aff4 | 727 | |
e04aad61 LP |
728 | if (priority >= 0) { |
729 | char p[LINE_MAX]; | |
07b0b134 | 730 | |
e04aad61 LP |
731 | snprintf(p, sizeof(p), "%i", priority); |
732 | char_array_0(p); | |
07b0b134 | 733 | |
e04aad61 LP |
734 | r = exec_command_set( |
735 | s->control_command, | |
736 | "/sbin/swapon", | |
737 | "-p", | |
738 | p, | |
739 | s->what, | |
740 | NULL); | |
741 | } else | |
742 | r = exec_command_set( | |
743 | s->control_command, | |
744 | "/sbin/swapon", | |
745 | s->what, | |
746 | NULL); | |
747 | ||
748 | if (r < 0) | |
749 | goto fail; | |
750 | ||
751 | swap_unwatch_control_pid(s); | |
752 | ||
753 | if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0) | |
754 | goto fail; | |
755 | ||
756 | swap_set_state(s, SWAP_ACTIVATING); | |
757 | ||
758 | return; | |
759 | ||
760 | fail: | |
761 | log_warning("%s failed to run 'swapon' task: %s", s->meta.id, strerror(-r)); | |
762 | swap_enter_dead(s, false); | |
763 | } | |
764 | ||
765 | static void swap_enter_deactivating(Swap *s, bool success) { | |
766 | int r; | |
767 | ||
768 | assert(s); | |
769 | ||
770 | if (!success) | |
771 | s->failure = true; | |
772 | ||
773 | s->control_command_id = SWAP_EXEC_DEACTIVATE; | |
774 | s->control_command = s->exec_command + SWAP_EXEC_DEACTIVATE; | |
775 | ||
776 | if ((r = exec_command_set( | |
777 | s->control_command, | |
778 | "/sbin/swapoff", | |
779 | s->what, | |
780 | NULL)) < 0) | |
781 | goto fail; | |
782 | ||
783 | swap_unwatch_control_pid(s); | |
784 | ||
785 | if ((r = swap_spawn(s, s->control_command, &s->control_pid)) < 0) | |
786 | goto fail; | |
787 | ||
788 | swap_set_state(s, SWAP_DEACTIVATING); | |
789 | ||
790 | return; | |
791 | ||
792 | fail: | |
793 | log_warning("%s failed to run 'swapoff' task: %s", s->meta.id, strerror(-r)); | |
794 | swap_enter_active(s, false); | |
795 | } | |
796 | ||
797 | static int swap_start(Unit *u) { | |
798 | Swap *s = SWAP(u); | |
799 | ||
800 | assert(s); | |
801 | ||
802 | /* We cannot fulfill this request right now, try again later | |
803 | * please! */ | |
804 | ||
805 | if (s->state == SWAP_DEACTIVATING || | |
806 | s->state == SWAP_DEACTIVATING_SIGTERM || | |
807 | s->state == SWAP_DEACTIVATING_SIGKILL || | |
808 | s->state == SWAP_ACTIVATING_SIGTERM || | |
809 | s->state == SWAP_ACTIVATING_SIGKILL) | |
810 | return -EAGAIN; | |
811 | ||
812 | if (s->state == SWAP_ACTIVATING) | |
813 | return 0; | |
814 | ||
815 | assert(s->state == SWAP_DEAD || s->state == SWAP_FAILED); | |
816 | ||
817 | s->failure = false; | |
818 | swap_enter_activating(s); | |
07b0b134 ML |
819 | return 0; |
820 | } | |
821 | ||
822 | static int swap_stop(Unit *u) { | |
823 | Swap *s = SWAP(u); | |
07b0b134 ML |
824 | |
825 | assert(s); | |
826 | ||
e04aad61 LP |
827 | if (s->state == SWAP_DEACTIVATING || |
828 | s->state == SWAP_DEACTIVATING_SIGTERM || | |
829 | s->state == SWAP_DEACTIVATING_SIGKILL || | |
830 | s->state == SWAP_ACTIVATING_SIGTERM || | |
831 | s->state == SWAP_ACTIVATING_SIGKILL) | |
832 | return 0; | |
07b0b134 | 833 | |
e04aad61 LP |
834 | assert(s->state == SWAP_ACTIVATING || |
835 | s->state == SWAP_ACTIVE); | |
07b0b134 | 836 | |
e04aad61 | 837 | swap_enter_deactivating(s, true); |
07b0b134 ML |
838 | return 0; |
839 | } | |
840 | ||
841 | static int swap_serialize(Unit *u, FILE *f, FDSet *fds) { | |
842 | Swap *s = SWAP(u); | |
843 | ||
844 | assert(s); | |
845 | assert(f); | |
846 | assert(fds); | |
847 | ||
848 | unit_serialize_item(u, f, "state", swap_state_to_string(s->state)); | |
e04aad61 LP |
849 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); |
850 | ||
851 | if (s->control_pid > 0) | |
852 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); | |
853 | ||
854 | if (s->control_command_id >= 0) | |
855 | unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id)); | |
07b0b134 ML |
856 | |
857 | return 0; | |
858 | } | |
859 | ||
860 | static int swap_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
861 | Swap *s = SWAP(u); | |
862 | ||
863 | assert(s); | |
864 | assert(fds); | |
865 | ||
866 | if (streq(key, "state")) { | |
867 | SwapState state; | |
868 | ||
869 | if ((state = swap_state_from_string(value)) < 0) | |
870 | log_debug("Failed to parse state value %s", value); | |
871 | else | |
872 | s->deserialized_state = state; | |
e04aad61 LP |
873 | } else if (streq(key, "failure")) { |
874 | int b; | |
875 | ||
876 | if ((b = parse_boolean(value)) < 0) | |
877 | log_debug("Failed to parse failure value %s", value); | |
878 | else | |
879 | s->failure = b || s->failure; | |
880 | ||
881 | } else if (streq(key, "control-pid")) { | |
882 | pid_t pid; | |
883 | ||
884 | if (parse_pid(value, &pid) < 0) | |
885 | log_debug("Failed to parse control-pid value %s", value); | |
886 | else | |
887 | s->control_pid = pid; | |
888 | ||
889 | } else if (streq(key, "control-command")) { | |
890 | SwapExecCommand id; | |
891 | ||
892 | if ((id = swap_exec_command_from_string(value)) < 0) | |
893 | log_debug("Failed to parse exec-command value %s", value); | |
894 | else { | |
895 | s->control_command_id = id; | |
896 | s->control_command = s->exec_command + id; | |
897 | } | |
898 | ||
07b0b134 ML |
899 | } else |
900 | log_debug("Unknown serialization key '%s'", key); | |
901 | ||
902 | return 0; | |
903 | } | |
904 | ||
905 | static UnitActiveState swap_active_state(Unit *u) { | |
906 | assert(u); | |
907 | ||
908 | return state_translation_table[SWAP(u)->state]; | |
909 | } | |
910 | ||
911 | static const char *swap_sub_state_to_string(Unit *u) { | |
912 | assert(u); | |
913 | ||
914 | return swap_state_to_string(SWAP(u)->state); | |
915 | } | |
916 | ||
917 | static bool swap_check_gc(Unit *u) { | |
918 | Swap *s = SWAP(u); | |
919 | ||
920 | assert(s); | |
921 | ||
4e85aff4 | 922 | return s->from_etc_fstab || s->from_proc_swaps; |
07b0b134 ML |
923 | } |
924 | ||
e04aad61 LP |
925 | static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
926 | Swap *s = SWAP(u); | |
927 | bool success; | |
928 | ||
929 | assert(s); | |
930 | assert(pid >= 0); | |
931 | ||
932 | if (pid != s->control_pid) | |
933 | return; | |
934 | ||
935 | s->control_pid = 0; | |
936 | ||
937 | success = is_clean_exit(code, status); | |
938 | s->failure = s->failure || !success; | |
939 | ||
940 | if (s->control_command) { | |
941 | exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id); | |
942 | s->control_command = NULL; | |
943 | s->control_command_id = _SWAP_EXEC_COMMAND_INVALID; | |
944 | } | |
945 | ||
946 | log_full(success ? LOG_DEBUG : LOG_NOTICE, | |
947 | "%s swap process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
948 | ||
949 | switch (s->state) { | |
950 | ||
951 | case SWAP_ACTIVATING: | |
952 | case SWAP_ACTIVATING_SIGTERM: | |
953 | case SWAP_ACTIVATING_SIGKILL: | |
954 | ||
955 | if (success) | |
956 | swap_enter_active(s, true); | |
957 | else | |
958 | swap_enter_dead(s, false); | |
959 | break; | |
960 | ||
961 | case SWAP_DEACTIVATING: | |
962 | case SWAP_DEACTIVATING_SIGKILL: | |
963 | case SWAP_DEACTIVATING_SIGTERM: | |
964 | ||
965 | if (success) | |
966 | swap_enter_dead(s, true); | |
967 | else | |
968 | swap_enter_dead(s, false); | |
969 | break; | |
970 | ||
971 | default: | |
972 | assert_not_reached("Uh, control process died at wrong time."); | |
973 | } | |
974 | ||
975 | /* Notify clients about changed exit status */ | |
976 | unit_add_to_dbus_queue(u); | |
977 | ||
978 | /* Request a reload of /proc/swaps, so that following units | |
979 | * can follow our state change */ | |
980 | u->meta.manager->request_reload = true; | |
981 | } | |
982 | ||
983 | static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) { | |
984 | Swap *s = SWAP(u); | |
985 | ||
986 | assert(s); | |
987 | assert(elapsed == 1); | |
988 | assert(w == &s->timer_watch); | |
989 | ||
990 | switch (s->state) { | |
991 | ||
992 | case SWAP_ACTIVATING: | |
993 | log_warning("%s activation timed out. Stopping.", u->meta.id); | |
994 | swap_enter_signal(s, SWAP_ACTIVATING_SIGTERM, false); | |
995 | break; | |
996 | ||
997 | case SWAP_DEACTIVATING: | |
998 | log_warning("%s deactivation timed out. Stopping.", u->meta.id); | |
999 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGTERM, false); | |
1000 | break; | |
1001 | ||
1002 | case SWAP_ACTIVATING_SIGTERM: | |
ba035df2 LP |
1003 | if (s->exec_context.send_sigkill) { |
1004 | log_warning("%s activation timed out. Killing.", u->meta.id); | |
1005 | swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false); | |
1006 | } else { | |
1007 | log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->meta.id); | |
1008 | swap_enter_dead(s, false); | |
1009 | } | |
e04aad61 LP |
1010 | break; |
1011 | ||
1012 | case SWAP_DEACTIVATING_SIGTERM: | |
ba035df2 LP |
1013 | if (s->exec_context.send_sigkill) { |
1014 | log_warning("%s deactivation timed out. Killing.", u->meta.id); | |
1015 | swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false); | |
1016 | } else { | |
1017 | log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->meta.id); | |
1018 | swap_enter_dead(s, false); | |
1019 | } | |
e04aad61 LP |
1020 | break; |
1021 | ||
1022 | case SWAP_ACTIVATING_SIGKILL: | |
1023 | case SWAP_DEACTIVATING_SIGKILL: | |
1024 | log_warning("%s swap process still around after SIGKILL. Ignoring.", u->meta.id); | |
1025 | swap_enter_dead(s, false); | |
1026 | break; | |
1027 | ||
1028 | default: | |
1029 | assert_not_reached("Timeout at wrong time."); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | static int swap_load_proc_swaps(Manager *m, bool set_flags) { | |
1034 | unsigned i; | |
1035 | int r = 0; | |
1036 | ||
1037 | assert(m); | |
1038 | ||
07b0b134 | 1039 | rewind(m->proc_swaps); |
bab45044 | 1040 | |
4e85aff4 | 1041 | (void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n"); |
07b0b134 | 1042 | |
e04aad61 | 1043 | for (i = 1;; i++) { |
07b0b134 ML |
1044 | char *dev = NULL, *d; |
1045 | int prio = 0, k; | |
1046 | ||
4e85aff4 | 1047 | if ((k = fscanf(m->proc_swaps, |
e04aad61 LP |
1048 | "%ms " /* device/file */ |
1049 | "%*s " /* type of swap */ | |
1050 | "%*s " /* swap size */ | |
1051 | "%*s " /* used */ | |
4e85aff4 LP |
1052 | "%i\n", /* priority */ |
1053 | &dev, &prio)) != 2) { | |
07b0b134 | 1054 | |
07b0b134 | 1055 | if (k == EOF) |
4e85aff4 | 1056 | break; |
07b0b134 | 1057 | |
e04aad61 | 1058 | log_warning("Failed to parse /proc/swaps:%u.", i); |
07b0b134 | 1059 | free(dev); |
e04aad61 | 1060 | continue; |
07b0b134 | 1061 | } |
07b0b134 | 1062 | |
4e85aff4 | 1063 | d = cunescape(dev); |
07b0b134 | 1064 | free(dev); |
4e85aff4 LP |
1065 | |
1066 | if (!d) | |
1067 | return -ENOMEM; | |
1068 | ||
e04aad61 | 1069 | k = swap_process_new_swap(m, d, prio, set_flags); |
07b0b134 ML |
1070 | free(d); |
1071 | ||
1072 | if (k < 0) | |
e04aad61 | 1073 | r = k; |
07b0b134 ML |
1074 | } |
1075 | ||
e04aad61 LP |
1076 | return r; |
1077 | } | |
1078 | ||
1079 | int swap_dispatch_reload(Manager *m) { | |
4e434314 | 1080 | /* This function should go as soon as the kernel properly notifies us */ |
e04aad61 LP |
1081 | |
1082 | if (_likely_(!m->request_reload)) | |
1083 | return 0; | |
1084 | ||
1085 | m->request_reload = false; | |
1086 | ||
4e434314 LP |
1087 | return swap_fd_event(m, EPOLLPRI); |
1088 | } | |
1089 | ||
1090 | int swap_fd_event(Manager *m, int events) { | |
1091 | Meta *meta; | |
1092 | int r; | |
1093 | ||
1094 | assert(m); | |
1095 | assert(events & EPOLLPRI); | |
1096 | ||
bd40a2d8 | 1097 | if ((r = swap_load_proc_swaps(m, true)) < 0) { |
e04aad61 LP |
1098 | log_error("Failed to reread /proc/swaps: %s", strerror(-r)); |
1099 | ||
1100 | /* Reset flags, just in case, for late calls */ | |
1101 | LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) { | |
1102 | Swap *swap = (Swap*) meta; | |
1103 | ||
1104 | swap->is_active = swap->just_activated = false; | |
1105 | } | |
1106 | ||
1107 | return 0; | |
1108 | } | |
1109 | ||
1110 | manager_dispatch_load_queue(m); | |
1111 | ||
1112 | LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) { | |
1113 | Swap *swap = (Swap*) meta; | |
1114 | ||
1115 | if (!swap->is_active) { | |
1116 | /* This has just been deactivated */ | |
1117 | ||
1118 | swap->from_proc_swaps = false; | |
1119 | swap_unset_proc_swaps(swap); | |
1120 | ||
1121 | switch (swap->state) { | |
1122 | ||
1123 | case SWAP_ACTIVE: | |
1124 | swap_enter_dead(swap, true); | |
1125 | break; | |
1126 | ||
1127 | default: | |
1128 | swap_set_state(swap, swap->state); | |
1129 | break; | |
1130 | } | |
1131 | ||
1132 | } else if (swap->just_activated) { | |
1133 | ||
1134 | /* New swap entry */ | |
1135 | ||
1136 | switch (swap->state) { | |
1137 | ||
1138 | case SWAP_DEAD: | |
1139 | case SWAP_FAILED: | |
1140 | swap_enter_active(swap, true); | |
1141 | break; | |
1142 | ||
1143 | default: | |
1144 | /* Nothing really changed, but let's | |
1145 | * issue an notification call | |
1146 | * nonetheless, in case somebody is | |
1147 | * waiting for this. */ | |
1148 | swap_set_state(swap, swap->state); | |
1149 | break; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | /* Reset the flags for later calls */ | |
1154 | swap->is_active = swap->just_activated = false; | |
1155 | } | |
1156 | ||
1157 | return 1; | |
1158 | } | |
1159 | ||
1160 | static Unit *swap_following(Unit *u) { | |
1161 | Swap *s = SWAP(u); | |
1162 | Swap *other, *first = NULL; | |
1163 | ||
1164 | assert(s); | |
1165 | ||
1166 | if (streq_ptr(s->what, s->parameters_proc_swaps.what)) | |
1167 | return NULL; | |
1168 | ||
1169 | /* Make everybody follow the unit that's named after the swap | |
1170 | * device in the kernel */ | |
1171 | ||
1172 | LIST_FOREACH_AFTER(same_proc_swaps, other, s) | |
1173 | if (streq_ptr(other->what, other->parameters_proc_swaps.what)) | |
1174 | return UNIT(other); | |
1175 | ||
1176 | LIST_FOREACH_BEFORE(same_proc_swaps, other, s) { | |
1177 | if (streq_ptr(other->what, other->parameters_proc_swaps.what)) | |
1178 | return UNIT(other); | |
1179 | ||
1180 | first = other; | |
1181 | } | |
1182 | ||
1183 | return UNIT(first); | |
07b0b134 ML |
1184 | } |
1185 | ||
6210e7fc LP |
1186 | static int swap_following_set(Unit *u, Set **_set) { |
1187 | Swap *s = SWAP(u); | |
1188 | Swap *other; | |
1189 | Set *set; | |
1190 | int r; | |
1191 | ||
1192 | assert(s); | |
1193 | assert(_set); | |
1194 | ||
1195 | if (LIST_JUST_US(same_proc_swaps, s)) { | |
1196 | *_set = NULL; | |
1197 | return 0; | |
1198 | } | |
1199 | ||
1200 | if (!(set = set_new(NULL, NULL))) | |
1201 | return -ENOMEM; | |
1202 | ||
1203 | LIST_FOREACH_AFTER(same_proc_swaps, other, s) | |
1204 | if ((r = set_put(set, other)) < 0) | |
1205 | goto fail; | |
1206 | ||
1207 | LIST_FOREACH_BEFORE(same_proc_swaps, other, s) | |
1208 | if ((r = set_put(set, other)) < 0) | |
1209 | goto fail; | |
1210 | ||
1211 | *_set = set; | |
1212 | return 1; | |
1213 | ||
1214 | fail: | |
1215 | set_free(set); | |
1216 | return r; | |
1217 | } | |
1218 | ||
07b0b134 ML |
1219 | static void swap_shutdown(Manager *m) { |
1220 | assert(m); | |
1221 | ||
1222 | if (m->proc_swaps) { | |
1223 | fclose(m->proc_swaps); | |
1224 | m->proc_swaps = NULL; | |
1225 | } | |
e04aad61 LP |
1226 | |
1227 | hashmap_free(m->swaps_by_proc_swaps); | |
1228 | m->swaps_by_proc_swaps = NULL; | |
07b0b134 ML |
1229 | } |
1230 | ||
07b0b134 ML |
1231 | static int swap_enumerate(Manager *m) { |
1232 | int r; | |
4e434314 | 1233 | struct epoll_event ev; |
07b0b134 ML |
1234 | assert(m); |
1235 | ||
4e434314 | 1236 | if (!m->proc_swaps) { |
4e85aff4 LP |
1237 | if (!(m->proc_swaps = fopen("/proc/swaps", "re"))) |
1238 | return -errno; | |
07b0b134 | 1239 | |
4e434314 LP |
1240 | m->swap_watch.type = WATCH_SWAP; |
1241 | m->swap_watch.fd = fileno(m->proc_swaps); | |
1242 | ||
1243 | zero(ev); | |
1244 | ev.events = EPOLLPRI; | |
1245 | ev.data.ptr = &m->swap_watch; | |
1246 | ||
1247 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->swap_watch.fd, &ev) < 0) | |
1248 | return -errno; | |
1249 | } | |
1250 | ||
e04aad61 LP |
1251 | /* We rely on mount.c to load /etc/fstab for us */ |
1252 | ||
1253 | if ((r = swap_load_proc_swaps(m, false)) < 0) | |
07b0b134 ML |
1254 | swap_shutdown(m); |
1255 | ||
1256 | return r; | |
1257 | } | |
1258 | ||
fdf20a31 | 1259 | static void swap_reset_failed(Unit *u) { |
5632e374 LP |
1260 | Swap *s = SWAP(u); |
1261 | ||
1262 | assert(s); | |
1263 | ||
fdf20a31 | 1264 | if (s->state == SWAP_FAILED) |
5632e374 | 1265 | swap_set_state(s, SWAP_DEAD); |
e04aad61 LP |
1266 | |
1267 | s->failure = false; | |
5632e374 LP |
1268 | } |
1269 | ||
8a0867d6 LP |
1270 | static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) { |
1271 | Swap *s = SWAP(u); | |
1272 | int r = 0; | |
1273 | Set *pid_set = NULL; | |
1274 | ||
1275 | assert(s); | |
1276 | ||
1277 | if (who == KILL_MAIN) { | |
1278 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes"); | |
1279 | return -EINVAL; | |
1280 | } | |
1281 | ||
1282 | if (s->control_pid <= 0 && who == KILL_CONTROL) { | |
1283 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); | |
1284 | return -ENOENT; | |
1285 | } | |
1286 | ||
1287 | if (s->control_pid > 0) | |
cd25cce9 | 1288 | if (kill(s->control_pid, signo) < 0) |
8a0867d6 LP |
1289 | r = -errno; |
1290 | ||
1291 | if (mode == KILL_CONTROL_GROUP) { | |
1292 | int q; | |
1293 | ||
1294 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) | |
1295 | return -ENOMEM; | |
1296 | ||
1297 | /* Exclude the control pid from being killed via the cgroup */ | |
1298 | if (s->control_pid > 0) | |
1299 | if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) { | |
1300 | r = q; | |
1301 | goto finish; | |
1302 | } | |
1303 | ||
430c18ed | 1304 | if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0) |
8a0867d6 LP |
1305 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) |
1306 | r = q; | |
1307 | } | |
1308 | ||
1309 | finish: | |
1310 | if (pid_set) | |
1311 | set_free(pid_set); | |
1312 | ||
1313 | return r; | |
1314 | } | |
1315 | ||
5632e374 LP |
1316 | static const char* const swap_state_table[_SWAP_STATE_MAX] = { |
1317 | [SWAP_DEAD] = "dead", | |
e04aad61 | 1318 | [SWAP_ACTIVATING] = "activating", |
5632e374 | 1319 | [SWAP_ACTIVE] = "active", |
e04aad61 LP |
1320 | [SWAP_DEACTIVATING] = "deactivating", |
1321 | [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm", | |
1322 | [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill", | |
1323 | [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm", | |
1324 | [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill", | |
fdf20a31 | 1325 | [SWAP_FAILED] = "failed" |
5632e374 LP |
1326 | }; |
1327 | ||
1328 | DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState); | |
1329 | ||
e04aad61 LP |
1330 | static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = { |
1331 | [SWAP_EXEC_ACTIVATE] = "ExecActivate", | |
1332 | [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate", | |
1333 | }; | |
1334 | ||
1335 | DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand); | |
1336 | ||
07b0b134 ML |
1337 | const UnitVTable swap_vtable = { |
1338 | .suffix = ".swap", | |
1339 | ||
e04aad61 | 1340 | .no_alias = true, |
07b0b134 | 1341 | .no_instances = true, |
6e620bec | 1342 | .no_isolate = true, |
9e58ff9c | 1343 | .show_status = true, |
07b0b134 | 1344 | |
4e85aff4 | 1345 | .init = swap_init, |
07b0b134 | 1346 | .load = swap_load, |
6e620bec | 1347 | .done = swap_done, |
07b0b134 ML |
1348 | |
1349 | .coldplug = swap_coldplug, | |
1350 | ||
1351 | .dump = swap_dump, | |
1352 | ||
1353 | .start = swap_start, | |
1354 | .stop = swap_stop, | |
1355 | ||
8a0867d6 LP |
1356 | .kill = swap_kill, |
1357 | ||
07b0b134 ML |
1358 | .serialize = swap_serialize, |
1359 | .deserialize_item = swap_deserialize_item, | |
1360 | ||
1361 | .active_state = swap_active_state, | |
1362 | .sub_state_to_string = swap_sub_state_to_string, | |
1363 | ||
1364 | .check_gc = swap_check_gc, | |
1365 | ||
e04aad61 LP |
1366 | .sigchld_event = swap_sigchld_event, |
1367 | .timer_event = swap_timer_event, | |
1368 | ||
1369 | .reset_failed = swap_reset_failed, | |
1370 | ||
c4e2ceae | 1371 | .bus_interface = "org.freedesktop.systemd1.Swap", |
07b0b134 | 1372 | .bus_message_handler = bus_swap_message_handler, |
c4e2ceae | 1373 | .bus_invalidating_properties = bus_swap_invalidating_properties, |
07b0b134 | 1374 | |
e04aad61 | 1375 | .following = swap_following, |
6210e7fc | 1376 | .following_set = swap_following_set, |
5632e374 | 1377 | |
6e620bec LP |
1378 | .enumerate = swap_enumerate, |
1379 | .shutdown = swap_shutdown | |
07b0b134 | 1380 | }; |