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