| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fnmatch.h> |
| 4 |
#include <linux/capability.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-id128.h" |
| 9 |
#include "sd-messages.h" |
| 10 |
#include "sd-varlink.h" |
| 11 |
|
| 12 |
#include "all-units.h" |
| 13 |
#include "alloc-util.h" |
| 14 |
#include "ansi-color.h" |
| 15 |
#include "bitfield.h" |
| 16 |
#include "bpf-restrict-fs.h" |
| 17 |
#include "bus-common-errors.h" |
| 18 |
#include "bus-internal.h" |
| 19 |
#include "bus-util.h" |
| 20 |
#include "cgroup-setup.h" |
| 21 |
#include "cgroup-util.h" |
| 22 |
#include "chase.h" |
| 23 |
#include "chattr-util.h" |
| 24 |
#include "condition.h" |
| 25 |
#include "dbus-unit.h" |
| 26 |
#include "dropin.h" |
| 27 |
#include "dynamic-user.h" |
| 28 |
#include "env-util.h" |
| 29 |
#include "escape.h" |
| 30 |
#include "exec-credential.h" |
| 31 |
#include "execute.h" |
| 32 |
#include "fd-util.h" |
| 33 |
#include "fileio.h" |
| 34 |
#include "format-util.h" |
| 35 |
#include "fs-util.h" |
| 36 |
#include "id128-util.h" |
| 37 |
#include "install.h" |
| 38 |
#include "iovec-util.h" |
| 39 |
#include "load-dropin.h" |
| 40 |
#include "load-fragment.h" |
| 41 |
#include "log.h" |
| 42 |
#include "logarithm.h" |
| 43 |
#include "mkdir.h" |
| 44 |
#include "manager.h" |
| 45 |
#include "mount-util.h" |
| 46 |
#include "mountpoint-util.h" |
| 47 |
#include "netlink-internal.h" |
| 48 |
#include "path-util.h" |
| 49 |
#include "process-util.h" |
| 50 |
#include "quota-util.h" |
| 51 |
#include "rm-rf.h" |
| 52 |
#include "serialize.h" |
| 53 |
#include "set.h" |
| 54 |
#include "signal-util.h" |
| 55 |
#include "siphash24.h" |
| 56 |
#include "sparse-endian.h" |
| 57 |
#include "special.h" |
| 58 |
#include "specifier.h" |
| 59 |
#include "stat-util.h" |
| 60 |
#include "string-table.h" |
| 61 |
#include "string-util.h" |
| 62 |
#include "strv.h" |
| 63 |
#include "tmpfile-util.h" |
| 64 |
#include "umask-util.h" |
| 65 |
#include "unit.h" |
| 66 |
#include "unit-name.h" |
| 67 |
#include "user-util.h" |
| 68 |
#include "varlink.h" |
| 69 |
|
| 70 |
/* Thresholds for logging at INFO level about resource consumption */ |
| 71 |
#define MENTIONWORTHY_CPU_NSEC (1 * NSEC_PER_SEC) |
| 72 |
#define MENTIONWORTHY_MEMORY_BYTES (64 * U64_MB) |
| 73 |
#define MENTIONWORTHY_IO_BYTES (1 * U64_MB) |
| 74 |
#define MENTIONWORTHY_IP_BYTES UINT64_C(0) |
| 75 |
|
| 76 |
/* Thresholds for logging at NOTICE level about resource consumption */ |
| 77 |
#define NOTICEWORTHY_CPU_NSEC (10 * NSEC_PER_MINUTE) |
| 78 |
#define NOTICEWORTHY_MEMORY_BYTES (512 * U64_MB) |
| 79 |
#define NOTICEWORTHY_IO_BYTES (10 * U64_MB) |
| 80 |
#define NOTICEWORTHY_IP_BYTES (128 * U64_MB) |
| 81 |
|
| 82 |
const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = { |
| 83 |
[UNIT_SERVICE] = &service_vtable, |
| 84 |
[UNIT_SOCKET] = &socket_vtable, |
| 85 |
[UNIT_TARGET] = &target_vtable, |
| 86 |
[UNIT_DEVICE] = &device_vtable, |
| 87 |
[UNIT_MOUNT] = &mount_vtable, |
| 88 |
[UNIT_AUTOMOUNT] = &automount_vtable, |
| 89 |
[UNIT_SWAP] = &swap_vtable, |
| 90 |
[UNIT_TIMER] = &timer_vtable, |
| 91 |
[UNIT_PATH] = &path_vtable, |
| 92 |
[UNIT_SLICE] = &slice_vtable, |
| 93 |
[UNIT_SCOPE] = &scope_vtable, |
| 94 |
}; |
| 95 |
|
| 96 |
Unit* unit_new(Manager *m, size_t size) { |
| 97 |
Unit *u; |
| 98 |
|
| 99 |
assert(m); |
| 100 |
assert(size >= sizeof(Unit)); |
| 101 |
|
| 102 |
u = malloc0(size); |
| 103 |
if (!u) |
| 104 |
return NULL; |
| 105 |
|
| 106 |
u->manager = m; |
| 107 |
u->type = _UNIT_TYPE_INVALID; |
| 108 |
u->default_dependencies = true; |
| 109 |
u->unit_file_state = _UNIT_FILE_STATE_INVALID; |
| 110 |
u->unit_file_preset = _PRESET_ACTION_INVALID; |
| 111 |
u->on_failure_job_mode = JOB_REPLACE; |
| 112 |
u->on_success_job_mode = JOB_FAIL; |
| 113 |
u->job_timeout = USEC_INFINITY; |
| 114 |
u->job_running_timeout = USEC_INFINITY; |
| 115 |
u->ref_uid = UID_INVALID; |
| 116 |
u->ref_gid = GID_INVALID; |
| 117 |
|
| 118 |
u->failure_action_exit_status = u->success_action_exit_status = -1; |
| 119 |
|
| 120 |
u->last_section_private = -1; |
| 121 |
|
| 122 |
u->start_ratelimit = m->defaults.start_limit; |
| 123 |
|
| 124 |
u->auto_start_stop_ratelimit = (const RateLimit) { |
| 125 |
.interval = 10 * USEC_PER_SEC, |
| 126 |
.burst = 16 |
| 127 |
}; |
| 128 |
|
| 129 |
return u; |
| 130 |
} |
| 131 |
|
| 132 |
int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) { |
| 133 |
_cleanup_(unit_freep) Unit *u = NULL; |
| 134 |
int r; |
| 135 |
|
| 136 |
assert(ret); |
| 137 |
|
| 138 |
u = unit_new(m, size); |
| 139 |
if (!u) |
| 140 |
return -ENOMEM; |
| 141 |
|
| 142 |
r = unit_add_name(u, name); |
| 143 |
if (r < 0) |
| 144 |
return r; |
| 145 |
|
| 146 |
*ret = TAKE_PTR(u); |
| 147 |
|
| 148 |
return r; |
| 149 |
} |
| 150 |
|
| 151 |
bool unit_has_name(const Unit *u, const char *name) { |
| 152 |
assert(u); |
| 153 |
assert(name); |
| 154 |
|
| 155 |
return streq_ptr(name, u->id) || |
| 156 |
set_contains(u->aliases, name); |
| 157 |
} |
| 158 |
|
| 159 |
static void unit_init(Unit *u) { |
| 160 |
CGroupContext *cc; |
| 161 |
ExecContext *ec; |
| 162 |
KillContext *kc; |
| 163 |
|
| 164 |
assert(u); |
| 165 |
assert(u->manager); |
| 166 |
assert(u->type >= 0); |
| 167 |
|
| 168 |
cc = unit_get_cgroup_context(u); |
| 169 |
if (cc) { |
| 170 |
cgroup_context_init(cc); |
| 171 |
|
| 172 |
/* Copy in the manager defaults into the cgroup |
| 173 |
* context, _before_ the rest of the settings have |
| 174 |
* been initialized */ |
| 175 |
|
| 176 |
cc->io_accounting = u->manager->defaults.io_accounting; |
| 177 |
cc->memory_accounting = u->manager->defaults.memory_accounting; |
| 178 |
cc->tasks_accounting = u->manager->defaults.tasks_accounting; |
| 179 |
cc->ip_accounting = u->manager->defaults.ip_accounting; |
| 180 |
|
| 181 |
if (u->type != UNIT_SLICE) |
| 182 |
cc->tasks_max = u->manager->defaults.tasks_max; |
| 183 |
|
| 184 |
cc->memory_zswap_writeback = u->manager->defaults.memory_zswap_writeback; |
| 185 |
|
| 186 |
memcpy(cc->pressure, u->manager->defaults.pressure, sizeof(cc->pressure)); |
| 187 |
} |
| 188 |
|
| 189 |
ec = unit_get_exec_context(u); |
| 190 |
if (ec) { |
| 191 |
exec_context_init(ec); |
| 192 |
|
| 193 |
if (u->manager->defaults.oom_score_adjust_set) { |
| 194 |
ec->oom_score_adjust = u->manager->defaults.oom_score_adjust; |
| 195 |
ec->oom_score_adjust_set = true; |
| 196 |
} |
| 197 |
|
| 198 |
ec->restrict_suid_sgid = u->manager->defaults.restrict_suid_sgid; |
| 199 |
|
| 200 |
if (MANAGER_IS_SYSTEM(u->manager)) |
| 201 |
ec->keyring_mode = EXEC_KEYRING_SHARED; |
| 202 |
else { |
| 203 |
ec->keyring_mode = EXEC_KEYRING_INHERIT; |
| 204 |
|
| 205 |
/* User manager might have its umask redefined by PAM or UMask=. In this |
| 206 |
* case let the units it manages inherit this value by default. They can |
| 207 |
* still tune this value through their own unit file */ |
| 208 |
(void) get_process_umask(0, &ec->umask); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
kc = unit_get_kill_context(u); |
| 213 |
if (kc) |
| 214 |
kill_context_init(kc); |
| 215 |
|
| 216 |
if (UNIT_VTABLE(u)->init) |
| 217 |
UNIT_VTABLE(u)->init(u); |
| 218 |
} |
| 219 |
|
| 220 |
static int unit_add_alias(Unit *u, char *donated_name) { |
| 221 |
int r; |
| 222 |
|
| 223 |
/* Make sure that u->names is allocated. We may leave u->names |
| 224 |
* empty if we fail later, but this is not a problem. */ |
| 225 |
r = set_ensure_put(&u->aliases, &string_hash_ops_free, donated_name); |
| 226 |
if (r < 0) |
| 227 |
return r; |
| 228 |
assert(r > 0); |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
int unit_add_name(Unit *u, const char *text) { |
| 234 |
_cleanup_free_ char *name = NULL, *instance = NULL; |
| 235 |
UnitType t; |
| 236 |
int r; |
| 237 |
|
| 238 |
assert(u); |
| 239 |
assert(text); |
| 240 |
|
| 241 |
if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) { |
| 242 |
if (!u->instance) |
| 243 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 244 |
"Instance is not set when adding name '%s'.", text); |
| 245 |
|
| 246 |
r = unit_name_replace_instance(text, u->instance, &name); |
| 247 |
if (r < 0) |
| 248 |
return log_unit_debug_errno(u, r, |
| 249 |
"Failed to build instance name from '%s': %m", text); |
| 250 |
} else { |
| 251 |
name = strdup(text); |
| 252 |
if (!name) |
| 253 |
return -ENOMEM; |
| 254 |
} |
| 255 |
|
| 256 |
if (unit_has_name(u, name)) |
| 257 |
return 0; |
| 258 |
|
| 259 |
if (hashmap_contains(u->manager->units, name)) |
| 260 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST), |
| 261 |
"Unit already exist when adding name '%s'.", name); |
| 262 |
|
| 263 |
if (!unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) |
| 264 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 265 |
"Name '%s' is invalid.", name); |
| 266 |
|
| 267 |
t = unit_name_to_type(name); |
| 268 |
if (t < 0) |
| 269 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 270 |
"failed to derive unit type from name '%s'.", name); |
| 271 |
|
| 272 |
if (u->type != _UNIT_TYPE_INVALID && t != u->type) |
| 273 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 274 |
"Unit type is illegal: u->type(%d) and t(%d) for name '%s'.", |
| 275 |
u->type, t, name); |
| 276 |
|
| 277 |
r = unit_name_to_instance(name, &instance); |
| 278 |
if (r < 0) |
| 279 |
return log_unit_debug_errno(u, r, "Failed to extract instance from name '%s': %m", name); |
| 280 |
|
| 281 |
if (instance && !unit_type_may_template(t)) |
| 282 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), "Templates are not allowed for name '%s'.", name); |
| 283 |
|
| 284 |
/* Ensure that this unit either has no instance, or that the instance matches. */ |
| 285 |
if (u->type != _UNIT_TYPE_INVALID && !streq_ptr(u->instance, instance)) |
| 286 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 287 |
"Cannot add name %s, the instances don't match (\"%s\" != \"%s\").", |
| 288 |
name, instance, u->instance); |
| 289 |
|
| 290 |
if (u->id && !unit_type_may_alias(t)) |
| 291 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST), |
| 292 |
"Cannot add name %s, aliases are not allowed for %s units.", |
| 293 |
name, unit_type_to_string(t)); |
| 294 |
|
| 295 |
if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES) |
| 296 |
return log_unit_warning_errno(u, SYNTHETIC_ERRNO(E2BIG), "Cannot add name, manager has too many units."); |
| 297 |
|
| 298 |
/* Add name to the global hashmap first, because that's easier to undo */ |
| 299 |
r = hashmap_put(u->manager->units, name, u); |
| 300 |
if (r < 0) |
| 301 |
return log_unit_debug_errno(u, r, "Add unit to hashmap failed for name '%s': %m", text); |
| 302 |
|
| 303 |
if (u->id) { |
| 304 |
r = unit_add_alias(u, name); /* unit_add_alias() takes ownership of the name on success */ |
| 305 |
if (r < 0) { |
| 306 |
hashmap_remove(u->manager->units, name); |
| 307 |
return r; |
| 308 |
} |
| 309 |
TAKE_PTR(name); |
| 310 |
|
| 311 |
} else { |
| 312 |
/* A new name, we don't need the set yet. */ |
| 313 |
assert(u->type == _UNIT_TYPE_INVALID); |
| 314 |
assert(!u->instance); |
| 315 |
|
| 316 |
u->type = t; |
| 317 |
u->id = TAKE_PTR(name); |
| 318 |
u->instance = TAKE_PTR(instance); |
| 319 |
|
| 320 |
LIST_PREPEND(units_by_type, u->manager->units_by_type[t], u); |
| 321 |
unit_init(u); |
| 322 |
} |
| 323 |
|
| 324 |
unit_add_to_dbus_queue(u); |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
int unit_choose_id(Unit *u, const char *name) { |
| 329 |
_cleanup_free_ char *t = NULL; |
| 330 |
char *s; |
| 331 |
int r; |
| 332 |
|
| 333 |
assert(u); |
| 334 |
assert(name); |
| 335 |
|
| 336 |
if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
| 337 |
if (!u->instance) |
| 338 |
return -EINVAL; |
| 339 |
|
| 340 |
r = unit_name_replace_instance(name, u->instance, &t); |
| 341 |
if (r < 0) |
| 342 |
return r; |
| 343 |
|
| 344 |
name = t; |
| 345 |
} |
| 346 |
|
| 347 |
if (streq_ptr(u->id, name)) |
| 348 |
return 0; /* Nothing to do. */ |
| 349 |
|
| 350 |
/* Selects one of the aliases of this unit as the id */ |
| 351 |
s = set_get(u->aliases, (char*) name); |
| 352 |
if (!s) |
| 353 |
return -ENOENT; |
| 354 |
|
| 355 |
if (u->id) { |
| 356 |
r = set_remove_and_put(u->aliases, name, u->id); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
} else |
| 360 |
assert_se(set_remove(u->aliases, name)); /* see set_get() above… */ |
| 361 |
|
| 362 |
u->id = s; /* Old u->id is now stored in the set, and s is not stored anywhere */ |
| 363 |
unit_add_to_dbus_queue(u); |
| 364 |
|
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
int unit_set_description(Unit *u, const char *description) { |
| 369 |
int r; |
| 370 |
|
| 371 |
assert(u); |
| 372 |
|
| 373 |
r = free_and_strdup(&u->description, empty_to_null(description)); |
| 374 |
if (r < 0) |
| 375 |
return r; |
| 376 |
if (r > 0) |
| 377 |
unit_add_to_dbus_queue(u); |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
static bool unit_success_failure_handler_has_jobs(Unit *unit) { |
| 383 |
Unit *other; |
| 384 |
|
| 385 |
UNIT_FOREACH_DEPENDENCY(other, unit, UNIT_ATOM_ON_SUCCESS) |
| 386 |
if (other->job || other->nop_job) |
| 387 |
return true; |
| 388 |
|
| 389 |
UNIT_FOREACH_DEPENDENCY(other, unit, UNIT_ATOM_ON_FAILURE) |
| 390 |
if (other->job || other->nop_job) |
| 391 |
return true; |
| 392 |
|
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
void unit_release_resources(Unit *u) { |
| 397 |
UnitActiveState state; |
| 398 |
ExecContext *ec; |
| 399 |
|
| 400 |
assert(u); |
| 401 |
|
| 402 |
if (u->job || u->nop_job) |
| 403 |
return; |
| 404 |
|
| 405 |
if (u->perpetual) |
| 406 |
return; |
| 407 |
|
| 408 |
state = unit_active_state(u); |
| 409 |
if (!UNIT_IS_INACTIVE_OR_FAILED(state)) |
| 410 |
return; |
| 411 |
|
| 412 |
if (unit_will_restart(u)) |
| 413 |
return; |
| 414 |
|
| 415 |
ec = unit_get_exec_context(u); |
| 416 |
if (ec && ec->runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART) |
| 417 |
exec_context_destroy_runtime_directory(ec, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); |
| 418 |
|
| 419 |
if (UNIT_VTABLE(u)->release_resources) |
| 420 |
UNIT_VTABLE(u)->release_resources(u); |
| 421 |
} |
| 422 |
|
| 423 |
bool unit_may_gc(Unit *u) { |
| 424 |
UnitActiveState state; |
| 425 |
int r; |
| 426 |
|
| 427 |
assert(u); |
| 428 |
|
| 429 |
/* Checks whether the unit is ready to be unloaded for garbage collection. Returns true when the |
| 430 |
* unit may be collected, and false if there's some reason to keep it loaded. |
| 431 |
* |
| 432 |
* References from other units are *not* checked here. Instead, this is done in unit_gc_sweep(), but |
| 433 |
* using markers to properly collect dependency loops. |
| 434 |
*/ |
| 435 |
|
| 436 |
if (u->job || u->nop_job) |
| 437 |
return false; |
| 438 |
|
| 439 |
if (u->perpetual) |
| 440 |
return false; |
| 441 |
|
| 442 |
/* if we saw a cgroup empty event for this unit, stay around until we processed it so that we remove |
| 443 |
* the empty cgroup if possible. Similar, process any pending OOM events if they are already queued |
| 444 |
* before we release the unit. */ |
| 445 |
if (u->in_cgroup_empty_queue || u->in_cgroup_oom_queue) |
| 446 |
return false; |
| 447 |
|
| 448 |
/* Make sure to send out D-Bus events before we unload the unit */ |
| 449 |
if (u->in_dbus_queue) |
| 450 |
return false; |
| 451 |
|
| 452 |
if (sd_bus_track_count(u->bus_track) > 0) |
| 453 |
return false; |
| 454 |
|
| 455 |
state = unit_active_state(u); |
| 456 |
|
| 457 |
/* But we keep the unit object around for longer when it is referenced or configured to not be |
| 458 |
* gc'ed */ |
| 459 |
switch (u->collect_mode) { |
| 460 |
|
| 461 |
case COLLECT_INACTIVE: |
| 462 |
if (state != UNIT_INACTIVE) |
| 463 |
return false; |
| 464 |
|
| 465 |
break; |
| 466 |
|
| 467 |
case COLLECT_INACTIVE_OR_FAILED: |
| 468 |
if (!UNIT_IS_INACTIVE_OR_FAILED(state)) |
| 469 |
return false; |
| 470 |
|
| 471 |
break; |
| 472 |
|
| 473 |
default: |
| 474 |
assert_not_reached(); |
| 475 |
} |
| 476 |
|
| 477 |
/* Check if any OnFailure= or on Success= jobs may be pending */ |
| 478 |
if (unit_success_failure_handler_has_jobs(u)) |
| 479 |
return false; |
| 480 |
|
| 481 |
/* If the unit has a cgroup, then check whether there's anything in it. If so, we should stay |
| 482 |
* around. Units with active processes should never be collected. */ |
| 483 |
r = unit_cgroup_is_empty(u); |
| 484 |
if (r <= 0 && !IN_SET(r, -ENXIO, -EOWNERDEAD)) |
| 485 |
return false; /* ENXIO/EOWNERDEAD means: currently not realized */ |
| 486 |
|
| 487 |
if (unit_can_start(u) && BIT_SET(u->markers, UNIT_MARKER_NEEDS_START)) |
| 488 |
return false; |
| 489 |
|
| 490 |
if (!UNIT_VTABLE(u)->may_gc) |
| 491 |
return true; |
| 492 |
|
| 493 |
return UNIT_VTABLE(u)->may_gc(u); |
| 494 |
} |
| 495 |
|
| 496 |
void unit_add_to_load_queue(Unit *u) { |
| 497 |
assert(u); |
| 498 |
assert(u->type != _UNIT_TYPE_INVALID); |
| 499 |
|
| 500 |
if (u->load_state != UNIT_STUB || u->in_load_queue) |
| 501 |
return; |
| 502 |
|
| 503 |
LIST_PREPEND(load_queue, u->manager->load_queue, u); |
| 504 |
u->in_load_queue = true; |
| 505 |
} |
| 506 |
|
| 507 |
void unit_add_to_cleanup_queue(Unit *u) { |
| 508 |
assert(u); |
| 509 |
|
| 510 |
if (u->in_cleanup_queue) |
| 511 |
return; |
| 512 |
|
| 513 |
LIST_PREPEND(cleanup_queue, u->manager->cleanup_queue, u); |
| 514 |
u->in_cleanup_queue = true; |
| 515 |
} |
| 516 |
|
| 517 |
void unit_add_to_gc_queue(Unit *u) { |
| 518 |
assert(u); |
| 519 |
|
| 520 |
if (u->in_gc_queue || u->in_cleanup_queue) |
| 521 |
return; |
| 522 |
|
| 523 |
if (!unit_may_gc(u)) |
| 524 |
return; |
| 525 |
|
| 526 |
LIST_PREPEND(gc_queue, u->manager->gc_unit_queue, u); |
| 527 |
u->in_gc_queue = true; |
| 528 |
} |
| 529 |
|
| 530 |
void unit_add_to_dbus_queue(Unit *u) { |
| 531 |
assert(u); |
| 532 |
assert(u->type != _UNIT_TYPE_INVALID); |
| 533 |
|
| 534 |
if (u->load_state == UNIT_STUB || u->in_dbus_queue) |
| 535 |
return; |
| 536 |
|
| 537 |
/* Shortcut things if nobody cares */ |
| 538 |
if (sd_bus_track_count(u->manager->subscribed) <= 0 && |
| 539 |
sd_bus_track_count(u->bus_track) <= 0 && |
| 540 |
set_isempty(u->manager->private_buses)) { |
| 541 |
u->sent_dbus_new_signal = true; |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
LIST_PREPEND(dbus_queue, u->manager->dbus_unit_queue, u); |
| 546 |
u->in_dbus_queue = true; |
| 547 |
} |
| 548 |
|
| 549 |
void unit_submit_to_stop_when_unneeded_queue(Unit *u) { |
| 550 |
assert(u); |
| 551 |
|
| 552 |
if (u->in_stop_when_unneeded_queue) |
| 553 |
return; |
| 554 |
|
| 555 |
if (!u->stop_when_unneeded) |
| 556 |
return; |
| 557 |
|
| 558 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) |
| 559 |
return; |
| 560 |
|
| 561 |
LIST_PREPEND(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u); |
| 562 |
u->in_stop_when_unneeded_queue = true; |
| 563 |
} |
| 564 |
|
| 565 |
void unit_submit_to_start_when_upheld_queue(Unit *u) { |
| 566 |
assert(u); |
| 567 |
|
| 568 |
if (u->in_start_when_upheld_queue) |
| 569 |
return; |
| 570 |
|
| 571 |
if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u))) |
| 572 |
return; |
| 573 |
|
| 574 |
if (!unit_has_dependency(u, UNIT_ATOM_START_STEADILY, NULL)) |
| 575 |
return; |
| 576 |
|
| 577 |
LIST_PREPEND(start_when_upheld_queue, u->manager->start_when_upheld_queue, u); |
| 578 |
u->in_start_when_upheld_queue = true; |
| 579 |
} |
| 580 |
|
| 581 |
void unit_submit_to_stop_when_bound_queue(Unit *u) { |
| 582 |
assert(u); |
| 583 |
|
| 584 |
if (u->in_stop_when_bound_queue) |
| 585 |
return; |
| 586 |
|
| 587 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) |
| 588 |
return; |
| 589 |
|
| 590 |
if (!unit_has_dependency(u, UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT, NULL)) |
| 591 |
return; |
| 592 |
|
| 593 |
LIST_PREPEND(stop_when_bound_queue, u->manager->stop_when_bound_queue, u); |
| 594 |
u->in_stop_when_bound_queue = true; |
| 595 |
} |
| 596 |
|
| 597 |
static bool unit_can_release_resources(Unit *u) { |
| 598 |
ExecContext *ec; |
| 599 |
|
| 600 |
assert(u); |
| 601 |
|
| 602 |
if (UNIT_VTABLE(u)->release_resources) |
| 603 |
return true; |
| 604 |
|
| 605 |
ec = unit_get_exec_context(u); |
| 606 |
if (ec && ec->runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART) |
| 607 |
return true; |
| 608 |
|
| 609 |
return false; |
| 610 |
} |
| 611 |
|
| 612 |
void unit_submit_to_release_resources_queue(Unit *u) { |
| 613 |
assert(u); |
| 614 |
|
| 615 |
if (u->in_release_resources_queue) |
| 616 |
return; |
| 617 |
|
| 618 |
if (u->job || u->nop_job) |
| 619 |
return; |
| 620 |
|
| 621 |
if (u->perpetual) |
| 622 |
return; |
| 623 |
|
| 624 |
if (!unit_can_release_resources(u)) |
| 625 |
return; |
| 626 |
|
| 627 |
LIST_PREPEND(release_resources_queue, u->manager->release_resources_queue, u); |
| 628 |
u->in_release_resources_queue = true; |
| 629 |
} |
| 630 |
|
| 631 |
void unit_add_to_stop_notify_queue(Unit *u) { |
| 632 |
assert(u); |
| 633 |
|
| 634 |
if (u->in_stop_notify_queue) |
| 635 |
return; |
| 636 |
|
| 637 |
assert(UNIT_VTABLE(u)->stop_notify); |
| 638 |
|
| 639 |
LIST_PREPEND(stop_notify_queue, u->manager->stop_notify_queue, u); |
| 640 |
u->in_stop_notify_queue = true; |
| 641 |
} |
| 642 |
|
| 643 |
void unit_remove_from_stop_notify_queue(Unit *u) { |
| 644 |
assert(u); |
| 645 |
|
| 646 |
if (!u->in_stop_notify_queue) |
| 647 |
return; |
| 648 |
|
| 649 |
LIST_REMOVE(stop_notify_queue, u->manager->stop_notify_queue, u); |
| 650 |
u->in_stop_notify_queue = false; |
| 651 |
} |
| 652 |
|
| 653 |
static void unit_clear_dependencies(Unit *u) { |
| 654 |
assert(u); |
| 655 |
|
| 656 |
/* Removes all dependencies configured on u and their reverse dependencies. */ |
| 657 |
|
| 658 |
for (Hashmap *deps; (deps = hashmap_steal_first(u->dependencies));) { |
| 659 |
|
| 660 |
for (Unit *other; (other = hashmap_steal_first_key(deps));) { |
| 661 |
Hashmap *other_deps; |
| 662 |
|
| 663 |
HASHMAP_FOREACH(other_deps, other->dependencies) |
| 664 |
hashmap_remove(other_deps, u); |
| 665 |
|
| 666 |
unit_add_to_gc_queue(other); |
| 667 |
other->dependency_generation++; |
| 668 |
} |
| 669 |
|
| 670 |
hashmap_free(deps); |
| 671 |
} |
| 672 |
|
| 673 |
u->dependencies = hashmap_free(u->dependencies); |
| 674 |
u->dependency_generation++; |
| 675 |
} |
| 676 |
|
| 677 |
static void unit_remove_transient(Unit *u) { |
| 678 |
assert(u); |
| 679 |
assert(u->manager); |
| 680 |
|
| 681 |
if (!u->transient) |
| 682 |
return; |
| 683 |
|
| 684 |
const char *dropin_directory = strjoina(u->id, ".d"); |
| 685 |
|
| 686 |
STRV_FOREACH(i, u->dropin_paths) { |
| 687 |
_cleanup_free_ char *p = NULL, *pp = NULL; |
| 688 |
|
| 689 |
if (path_extract_directory(*i, &p) < 0) /* Get the drop-in directory from the drop-in file */ |
| 690 |
continue; |
| 691 |
|
| 692 |
if (path_extract_directory(p, &pp) < 0) /* Get the config directory from the drop-in directory */ |
| 693 |
continue; |
| 694 |
|
| 695 |
/* Only drop transient drop-ins */ |
| 696 |
if (!path_equal(u->manager->lookup_paths.transient, pp)) |
| 697 |
continue; |
| 698 |
|
| 699 |
/* Drop the transient drop-in directory also from unit path cache. */ |
| 700 |
if (path_equal(last_path_component(p), dropin_directory)) |
| 701 |
free(set_remove(u->manager->unit_path_cache, p)); |
| 702 |
|
| 703 |
(void) unlink(*i); |
| 704 |
(void) rmdir(p); |
| 705 |
} |
| 706 |
|
| 707 |
if (u->fragment_path) { |
| 708 |
(void) unlink(u->fragment_path); |
| 709 |
(void) unit_file_remove_from_name_map( |
| 710 |
&u->manager->lookup_paths, |
| 711 |
&u->manager->unit_cache_timestamp_hash, |
| 712 |
&u->manager->unit_id_map, |
| 713 |
&u->manager->unit_name_map, |
| 714 |
&u->manager->unit_path_cache, |
| 715 |
u->fragment_path); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
static void unit_free_mounts_for(Unit *u) { |
| 720 |
assert(u); |
| 721 |
|
| 722 |
for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; ++t) { |
| 723 |
for (;;) { |
| 724 |
_cleanup_free_ char *path = NULL; |
| 725 |
|
| 726 |
path = hashmap_steal_first_key(u->mounts_for[t]); |
| 727 |
if (!path) |
| 728 |
break; |
| 729 |
|
| 730 |
char s[strlen(path) + 1]; |
| 731 |
|
| 732 |
PATH_FOREACH_PREFIX_MORE(s, path) { |
| 733 |
char *y; |
| 734 |
Set *x; |
| 735 |
|
| 736 |
x = hashmap_get2(u->manager->units_needing_mounts_for[t], s, (void**) &y); |
| 737 |
if (!x) |
| 738 |
continue; |
| 739 |
|
| 740 |
(void) set_remove(x, u); |
| 741 |
|
| 742 |
if (set_isempty(x)) { |
| 743 |
assert_se(hashmap_remove(u->manager->units_needing_mounts_for[t], y)); |
| 744 |
free(y); |
| 745 |
set_free(x); |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
u->mounts_for[t] = hashmap_free(u->mounts_for[t]); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
static void unit_done(Unit *u) { |
| 755 |
ExecContext *ec; |
| 756 |
CGroupContext *cc; |
| 757 |
|
| 758 |
assert(u); |
| 759 |
|
| 760 |
if (u->type < 0) |
| 761 |
return; |
| 762 |
|
| 763 |
if (UNIT_VTABLE(u)->done) |
| 764 |
UNIT_VTABLE(u)->done(u); |
| 765 |
|
| 766 |
ec = unit_get_exec_context(u); |
| 767 |
if (ec) |
| 768 |
exec_context_done(ec); |
| 769 |
|
| 770 |
cc = unit_get_cgroup_context(u); |
| 771 |
if (cc) |
| 772 |
cgroup_context_done(cc); |
| 773 |
} |
| 774 |
|
| 775 |
Unit* unit_free(Unit *u) { |
| 776 |
Unit *slice; |
| 777 |
char *t; |
| 778 |
|
| 779 |
if (!u) |
| 780 |
return NULL; |
| 781 |
|
| 782 |
sd_event_source_disable_unref(u->auto_start_stop_event_source); |
| 783 |
|
| 784 |
u->transient_file = safe_fclose(u->transient_file); |
| 785 |
|
| 786 |
if (!MANAGER_IS_RELOADING(u->manager)) |
| 787 |
unit_remove_transient(u); |
| 788 |
|
| 789 |
bus_unit_send_removed_signal(u); |
| 790 |
|
| 791 |
unit_done(u); |
| 792 |
|
| 793 |
u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
| 794 |
u->bus_track = sd_bus_track_unref(u->bus_track); |
| 795 |
u->varlink_unit_change = sd_varlink_unref(u->varlink_unit_change); |
| 796 |
u->deserialized_refs = strv_free(u->deserialized_refs); |
| 797 |
u->pending_freezer_invocation = sd_bus_message_unref(u->pending_freezer_invocation); |
| 798 |
|
| 799 |
unit_free_mounts_for(u); |
| 800 |
|
| 801 |
SET_FOREACH(t, u->aliases) |
| 802 |
hashmap_remove_value(u->manager->units, t, u); |
| 803 |
if (u->id) |
| 804 |
hashmap_remove_value(u->manager->units, u->id, u); |
| 805 |
|
| 806 |
if (!sd_id128_is_null(u->invocation_id)) |
| 807 |
hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); |
| 808 |
|
| 809 |
if (u->job) { |
| 810 |
Job *j = u->job; |
| 811 |
job_uninstall(j); |
| 812 |
job_free(j); |
| 813 |
} |
| 814 |
|
| 815 |
if (u->nop_job) { |
| 816 |
Job *j = u->nop_job; |
| 817 |
job_uninstall(j); |
| 818 |
job_free(j); |
| 819 |
} |
| 820 |
|
| 821 |
/* A unit is being dropped from the tree, make sure our family is realized properly. Do this after we |
| 822 |
* detach the unit from slice tree in order to eliminate its effect on controller masks. */ |
| 823 |
slice = UNIT_GET_SLICE(u); |
| 824 |
unit_clear_dependencies(u); |
| 825 |
if (slice) |
| 826 |
unit_add_family_to_cgroup_realize_queue(slice); |
| 827 |
|
| 828 |
if (u->on_console) |
| 829 |
manager_unref_console(u->manager); |
| 830 |
|
| 831 |
unit_release_cgroup(u, /* drop_cgroup_runtime= */ true); |
| 832 |
|
| 833 |
if (!MANAGER_IS_RELOADING(u->manager)) |
| 834 |
unit_unlink_state_files(u); |
| 835 |
|
| 836 |
unit_unref_uid_gid(u, false); |
| 837 |
|
| 838 |
(void) manager_update_failed_units(u->manager, u, false); |
| 839 |
set_remove(u->manager->startup_units, u); |
| 840 |
|
| 841 |
unit_unwatch_all_pids(u); |
| 842 |
|
| 843 |
while (u->refs_by_target) |
| 844 |
unit_ref_unset(u->refs_by_target); |
| 845 |
|
| 846 |
if (u->type != _UNIT_TYPE_INVALID) |
| 847 |
LIST_REMOVE(units_by_type, u->manager->units_by_type[u->type], u); |
| 848 |
|
| 849 |
if (u->in_load_queue) |
| 850 |
LIST_REMOVE(load_queue, u->manager->load_queue, u); |
| 851 |
|
| 852 |
if (u->in_dbus_queue) |
| 853 |
LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u); |
| 854 |
|
| 855 |
if (u->in_cleanup_queue) |
| 856 |
LIST_REMOVE(cleanup_queue, u->manager->cleanup_queue, u); |
| 857 |
|
| 858 |
if (u->in_gc_queue) |
| 859 |
LIST_REMOVE(gc_queue, u->manager->gc_unit_queue, u); |
| 860 |
|
| 861 |
if (u->in_cgroup_realize_queue) |
| 862 |
LIST_REMOVE(cgroup_realize_queue, u->manager->cgroup_realize_queue, u); |
| 863 |
|
| 864 |
if (u->in_cgroup_empty_queue) |
| 865 |
LIST_REMOVE(cgroup_empty_queue, u->manager->cgroup_empty_queue, u); |
| 866 |
|
| 867 |
if (u->in_cgroup_oom_queue) |
| 868 |
LIST_REMOVE(cgroup_oom_queue, u->manager->cgroup_oom_queue, u); |
| 869 |
|
| 870 |
if (u->in_target_deps_queue) |
| 871 |
LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u); |
| 872 |
|
| 873 |
if (u->in_stop_when_unneeded_queue) |
| 874 |
LIST_REMOVE(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u); |
| 875 |
|
| 876 |
if (u->in_start_when_upheld_queue) |
| 877 |
LIST_REMOVE(start_when_upheld_queue, u->manager->start_when_upheld_queue, u); |
| 878 |
|
| 879 |
if (u->in_stop_when_bound_queue) |
| 880 |
LIST_REMOVE(stop_when_bound_queue, u->manager->stop_when_bound_queue, u); |
| 881 |
|
| 882 |
if (u->in_release_resources_queue) |
| 883 |
LIST_REMOVE(release_resources_queue, u->manager->release_resources_queue, u); |
| 884 |
|
| 885 |
unit_remove_from_stop_notify_queue(u); |
| 886 |
|
| 887 |
condition_free_list(u->conditions); |
| 888 |
condition_free_list(u->asserts); |
| 889 |
|
| 890 |
free(u->description); |
| 891 |
strv_free(u->documentation); |
| 892 |
free(u->fragment_path); |
| 893 |
free(u->source_path); |
| 894 |
strv_free(u->dropin_paths); |
| 895 |
free(u->instance); |
| 896 |
|
| 897 |
free(u->job_timeout_reboot_arg); |
| 898 |
free(u->reboot_arg); |
| 899 |
|
| 900 |
free(u->access_selinux_context); |
| 901 |
|
| 902 |
set_free(u->aliases); |
| 903 |
free(u->id); |
| 904 |
|
| 905 |
activation_details_unref(u->activation_details); |
| 906 |
|
| 907 |
return mfree(u); |
| 908 |
} |
| 909 |
|
| 910 |
UnitActiveState unit_active_state(Unit *u) { |
| 911 |
assert(u); |
| 912 |
|
| 913 |
if (u->load_state == UNIT_MERGED) |
| 914 |
return unit_active_state(unit_follow_merge(u)); |
| 915 |
|
| 916 |
/* After a reload it might happen that a unit is not correctly |
| 917 |
* loaded but still has a process around. That's why we won't |
| 918 |
* shortcut failed loading to UNIT_INACTIVE_FAILED. */ |
| 919 |
|
| 920 |
return UNIT_VTABLE(u)->active_state(u); |
| 921 |
} |
| 922 |
|
| 923 |
const char* unit_sub_state_to_string(Unit *u) { |
| 924 |
assert(u); |
| 925 |
|
| 926 |
return UNIT_VTABLE(u)->sub_state_to_string(u); |
| 927 |
} |
| 928 |
|
| 929 |
static int unit_merge_names(Unit *u, Unit *other) { |
| 930 |
char *name; |
| 931 |
int r; |
| 932 |
|
| 933 |
assert(u); |
| 934 |
assert(other); |
| 935 |
|
| 936 |
r = unit_add_alias(u, other->id); |
| 937 |
if (r < 0) |
| 938 |
return r; |
| 939 |
|
| 940 |
r = set_move(u->aliases, other->aliases); |
| 941 |
if (r < 0) { |
| 942 |
set_remove(u->aliases, other->id); |
| 943 |
return r; |
| 944 |
} |
| 945 |
|
| 946 |
TAKE_PTR(other->id); |
| 947 |
other->aliases = set_free(other->aliases); |
| 948 |
|
| 949 |
SET_FOREACH(name, u->aliases) |
| 950 |
assert_se(hashmap_replace(u->manager->units, name, u) == 0); |
| 951 |
|
| 952 |
return 0; |
| 953 |
} |
| 954 |
|
| 955 |
static int unit_reserve_dependencies(Unit *u, Unit *other) { |
| 956 |
size_t n_reserve; |
| 957 |
Hashmap* deps; |
| 958 |
void *d; |
| 959 |
int r; |
| 960 |
|
| 961 |
assert(u); |
| 962 |
assert(other); |
| 963 |
|
| 964 |
/* Let's reserve some space in the dependency hashmaps so that later on merging the units cannot |
| 965 |
* fail. |
| 966 |
* |
| 967 |
* First make some room in the per dependency type hashmaps. Using the summed size of both units' |
| 968 |
* hashmaps is an estimate that is likely too high since they probably use some of the same |
| 969 |
* types. But it's never too low, and that's all we need. */ |
| 970 |
|
| 971 |
n_reserve = MIN(hashmap_size(other->dependencies), LESS_BY((size_t) _UNIT_DEPENDENCY_MAX, hashmap_size(u->dependencies))); |
| 972 |
if (n_reserve > 0) { |
| 973 |
r = hashmap_ensure_allocated(&u->dependencies, NULL); |
| 974 |
if (r < 0) |
| 975 |
return r; |
| 976 |
|
| 977 |
r = hashmap_reserve(u->dependencies, n_reserve); |
| 978 |
if (r < 0) |
| 979 |
return r; |
| 980 |
} |
| 981 |
|
| 982 |
/* Now, enlarge our per dependency type hashmaps by the number of entries in the same hashmap of the |
| 983 |
* other unit's dependencies. |
| 984 |
* |
| 985 |
* NB: If u does not have a dependency set allocated for some dependency type, there is no need to |
| 986 |
* reserve anything for. In that case other's set will be transferred as a whole to u by |
| 987 |
* complete_move(). */ |
| 988 |
|
| 989 |
HASHMAP_FOREACH_KEY(deps, d, u->dependencies) { |
| 990 |
Hashmap *other_deps; |
| 991 |
|
| 992 |
other_deps = hashmap_get(other->dependencies, d); |
| 993 |
|
| 994 |
r = hashmap_reserve(deps, hashmap_size(other_deps)); |
| 995 |
if (r < 0) |
| 996 |
return r; |
| 997 |
} |
| 998 |
|
| 999 |
return 0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
static bool unit_should_warn_about_dependency(UnitDependency dependency) { |
| 1003 |
/* Only warn about some unit types */ |
| 1004 |
return IN_SET(dependency, |
| 1005 |
UNIT_CONFLICTS, |
| 1006 |
UNIT_CONFLICTED_BY, |
| 1007 |
UNIT_BEFORE, |
| 1008 |
UNIT_AFTER, |
| 1009 |
UNIT_ON_SUCCESS, |
| 1010 |
UNIT_ON_FAILURE, |
| 1011 |
UNIT_TRIGGERS, |
| 1012 |
UNIT_TRIGGERED_BY); |
| 1013 |
} |
| 1014 |
|
| 1015 |
static int unit_per_dependency_type_hashmap_update( |
| 1016 |
Hashmap *per_type, |
| 1017 |
Unit *other, |
| 1018 |
UnitDependencyMask origin_mask, |
| 1019 |
UnitDependencyMask destination_mask) { |
| 1020 |
|
| 1021 |
UnitDependencyInfo info; |
| 1022 |
int r; |
| 1023 |
|
| 1024 |
assert(other); |
| 1025 |
assert_cc(sizeof(void*) == sizeof(info)); |
| 1026 |
|
| 1027 |
/* Acquire the UnitDependencyInfo entry for the Unit* we are interested in, and update it if it |
| 1028 |
* exists, or insert it anew if not. */ |
| 1029 |
|
| 1030 |
info.data = hashmap_get(per_type, other); |
| 1031 |
if (info.data) { |
| 1032 |
/* Entry already exists. Add in our mask. */ |
| 1033 |
|
| 1034 |
if (FLAGS_SET(origin_mask, info.origin_mask) && |
| 1035 |
FLAGS_SET(destination_mask, info.destination_mask)) |
| 1036 |
return 0; /* NOP */ |
| 1037 |
|
| 1038 |
info.origin_mask |= origin_mask; |
| 1039 |
info.destination_mask |= destination_mask; |
| 1040 |
|
| 1041 |
r = hashmap_update(per_type, other, info.data); |
| 1042 |
} else { |
| 1043 |
info = (UnitDependencyInfo) { |
| 1044 |
.origin_mask = origin_mask, |
| 1045 |
.destination_mask = destination_mask, |
| 1046 |
}; |
| 1047 |
|
| 1048 |
r = hashmap_put(per_type, other, info.data); |
| 1049 |
} |
| 1050 |
if (r < 0) |
| 1051 |
return r; |
| 1052 |
|
| 1053 |
return 1; |
| 1054 |
} |
| 1055 |
|
| 1056 |
static void unit_merge_dependencies(Unit *u, Unit *other) { |
| 1057 |
Hashmap *deps; |
| 1058 |
void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here, |
| 1059 |
* since the hashmaps all want it as void pointer. */ |
| 1060 |
|
| 1061 |
assert(u); |
| 1062 |
assert(other); |
| 1063 |
|
| 1064 |
if (u == other) |
| 1065 |
return; |
| 1066 |
|
| 1067 |
/* First, remove dependency to other. */ |
| 1068 |
HASHMAP_FOREACH_KEY(deps, dt, u->dependencies) { |
| 1069 |
if (hashmap_remove(deps, other) && unit_should_warn_about_dependency(UNIT_DEPENDENCY_FROM_PTR(dt))) |
| 1070 |
log_unit_warning(u, "Dependency %s=%s is dropped, as %s is merged into %s.", |
| 1071 |
unit_dependency_to_string(UNIT_DEPENDENCY_FROM_PTR(dt)), |
| 1072 |
other->id, other->id, u->id); |
| 1073 |
|
| 1074 |
if (hashmap_isempty(deps)) |
| 1075 |
hashmap_free(hashmap_remove(u->dependencies, dt)); |
| 1076 |
} |
| 1077 |
|
| 1078 |
for (;;) { |
| 1079 |
_cleanup_hashmap_free_ Hashmap *other_deps = NULL; |
| 1080 |
UnitDependencyInfo di_back; |
| 1081 |
Unit *back; |
| 1082 |
|
| 1083 |
/* Let's focus on one dependency type at a time, that 'other' has defined. */ |
| 1084 |
other_deps = hashmap_steal_first_key_and_value(other->dependencies, &dt); |
| 1085 |
if (!other_deps) |
| 1086 |
break; /* done! */ |
| 1087 |
|
| 1088 |
deps = hashmap_get(u->dependencies, dt); |
| 1089 |
|
| 1090 |
/* Now iterate through all dependencies of this dependency type, of 'other'. We refer to the |
| 1091 |
* referenced units as 'back'. */ |
| 1092 |
HASHMAP_FOREACH_KEY(di_back.data, back, other_deps) { |
| 1093 |
Hashmap *back_deps; |
| 1094 |
void *back_dt; |
| 1095 |
|
| 1096 |
if (back == u) { |
| 1097 |
/* This is a dependency pointing back to the unit we want to merge with? |
| 1098 |
* Suppress it (but warn) */ |
| 1099 |
if (unit_should_warn_about_dependency(UNIT_DEPENDENCY_FROM_PTR(dt))) |
| 1100 |
log_unit_warning(u, "Dependency %s=%s in %s is dropped, as %s is merged into %s.", |
| 1101 |
unit_dependency_to_string(UNIT_DEPENDENCY_FROM_PTR(dt)), |
| 1102 |
u->id, other->id, other->id, u->id); |
| 1103 |
|
| 1104 |
hashmap_remove(other_deps, back); |
| 1105 |
continue; |
| 1106 |
} |
| 1107 |
|
| 1108 |
/* Now iterate through all deps of 'back', and fix the ones pointing to 'other' to |
| 1109 |
* point to 'u' instead. */ |
| 1110 |
HASHMAP_FOREACH_KEY(back_deps, back_dt, back->dependencies) { |
| 1111 |
UnitDependencyInfo di_move; |
| 1112 |
|
| 1113 |
di_move.data = hashmap_remove(back_deps, other); |
| 1114 |
if (!di_move.data) |
| 1115 |
continue; |
| 1116 |
|
| 1117 |
assert_se(unit_per_dependency_type_hashmap_update( |
| 1118 |
back_deps, |
| 1119 |
u, |
| 1120 |
di_move.origin_mask, |
| 1121 |
di_move.destination_mask) >= 0); |
| 1122 |
} |
| 1123 |
|
| 1124 |
/* The target unit already has dependencies of this type, let's then merge this individually. */ |
| 1125 |
if (deps) |
| 1126 |
assert_se(unit_per_dependency_type_hashmap_update( |
| 1127 |
deps, |
| 1128 |
back, |
| 1129 |
di_back.origin_mask, |
| 1130 |
di_back.destination_mask) >= 0); |
| 1131 |
} |
| 1132 |
|
| 1133 |
/* Now all references towards 'other' of the current type 'dt' are corrected to point to 'u'. |
| 1134 |
* Lets's now move the deps of type 'dt' from 'other' to 'u'. If the unit does not have |
| 1135 |
* dependencies of this type, let's move them per type wholesale. */ |
| 1136 |
if (!deps) |
| 1137 |
assert_se(hashmap_put(u->dependencies, dt, TAKE_PTR(other_deps)) >= 0); |
| 1138 |
} |
| 1139 |
|
| 1140 |
other->dependencies = hashmap_free(other->dependencies); |
| 1141 |
|
| 1142 |
u->dependency_generation++; |
| 1143 |
other->dependency_generation++; |
| 1144 |
} |
| 1145 |
|
| 1146 |
int unit_merge(Unit *u, Unit *other) { |
| 1147 |
int r; |
| 1148 |
|
| 1149 |
assert(u); |
| 1150 |
assert(other); |
| 1151 |
assert(u->manager == other->manager); |
| 1152 |
assert(u->type != _UNIT_TYPE_INVALID); |
| 1153 |
|
| 1154 |
other = unit_follow_merge(other); |
| 1155 |
|
| 1156 |
if (other == u) |
| 1157 |
return 0; |
| 1158 |
|
| 1159 |
if (u->type != other->type) |
| 1160 |
return -EINVAL; |
| 1161 |
|
| 1162 |
if (!unit_type_may_alias(u->type)) /* Merging only applies to unit names that support aliases */ |
| 1163 |
return -EEXIST; |
| 1164 |
|
| 1165 |
if (!IN_SET(other->load_state, UNIT_STUB, UNIT_NOT_FOUND)) |
| 1166 |
return -EEXIST; |
| 1167 |
|
| 1168 |
if (!streq_ptr(u->instance, other->instance)) |
| 1169 |
return -EINVAL; |
| 1170 |
|
| 1171 |
if (other->job) |
| 1172 |
return -EEXIST; |
| 1173 |
|
| 1174 |
if (other->nop_job) |
| 1175 |
return -EEXIST; |
| 1176 |
|
| 1177 |
if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) |
| 1178 |
return -EEXIST; |
| 1179 |
|
| 1180 |
/* Make reservations to ensure merge_dependencies() won't fail. We don't rollback reservations if we |
| 1181 |
* fail. We don't have a way to undo reservations. A reservation is not a leak. */ |
| 1182 |
r = unit_reserve_dependencies(u, other); |
| 1183 |
if (r < 0) |
| 1184 |
return r; |
| 1185 |
|
| 1186 |
/* Redirect all references */ |
| 1187 |
while (other->refs_by_target) |
| 1188 |
unit_ref_set(other->refs_by_target, other->refs_by_target->source, u); |
| 1189 |
|
| 1190 |
/* Merge dependencies */ |
| 1191 |
unit_merge_dependencies(u, other); |
| 1192 |
|
| 1193 |
/* Merge names. It is better to do that after merging deps, otherwise the log message contains n/a. */ |
| 1194 |
r = unit_merge_names(u, other); |
| 1195 |
if (r < 0) |
| 1196 |
return r; |
| 1197 |
|
| 1198 |
other->load_state = UNIT_MERGED; |
| 1199 |
other->merged_into = u; |
| 1200 |
|
| 1201 |
if (!u->activation_details) |
| 1202 |
u->activation_details = activation_details_ref(other->activation_details); |
| 1203 |
|
| 1204 |
/* If there is still some data attached to the other node, we |
| 1205 |
* don't need it anymore, and can free it. */ |
| 1206 |
if (other->load_state != UNIT_STUB) |
| 1207 |
if (UNIT_VTABLE(other)->done) |
| 1208 |
UNIT_VTABLE(other)->done(other); |
| 1209 |
|
| 1210 |
unit_add_to_dbus_queue(u); |
| 1211 |
unit_add_to_cleanup_queue(other); |
| 1212 |
|
| 1213 |
return 0; |
| 1214 |
} |
| 1215 |
|
| 1216 |
int unit_merge_by_name(Unit *u, const char *name) { |
| 1217 |
_cleanup_free_ char *s = NULL; |
| 1218 |
Unit *other; |
| 1219 |
int r; |
| 1220 |
|
| 1221 |
/* Either add name to u, or if a unit with name already exists, merge it with u. |
| 1222 |
* If name is a template, do the same for name@instance, where instance is u's instance. */ |
| 1223 |
|
| 1224 |
assert(u); |
| 1225 |
assert(name); |
| 1226 |
|
| 1227 |
if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
| 1228 |
if (!u->instance) |
| 1229 |
return -EINVAL; |
| 1230 |
|
| 1231 |
r = unit_name_replace_instance(name, u->instance, &s); |
| 1232 |
if (r < 0) |
| 1233 |
return r; |
| 1234 |
|
| 1235 |
name = s; |
| 1236 |
} |
| 1237 |
|
| 1238 |
other = manager_get_unit(u->manager, name); |
| 1239 |
if (other) |
| 1240 |
return unit_merge(u, other); |
| 1241 |
|
| 1242 |
return unit_add_name(u, name); |
| 1243 |
} |
| 1244 |
|
| 1245 |
Unit* unit_follow_merge(Unit *u) { |
| 1246 |
assert(u); |
| 1247 |
|
| 1248 |
while (u->load_state == UNIT_MERGED) |
| 1249 |
assert_se(u = u->merged_into); |
| 1250 |
|
| 1251 |
return u; |
| 1252 |
} |
| 1253 |
|
| 1254 |
int unit_add_exec_dependencies(Unit *u, ExecContext *c) { |
| 1255 |
int r; |
| 1256 |
|
| 1257 |
assert(u); |
| 1258 |
assert(c); |
| 1259 |
|
| 1260 |
/* Unlike unit_add_dependency() or friends, this always returns 0 on success. */ |
| 1261 |
|
| 1262 |
if (c->working_directory) { |
| 1263 |
r = unit_add_mounts_for( |
| 1264 |
u, |
| 1265 |
c->working_directory, |
| 1266 |
UNIT_DEPENDENCY_FILE, |
| 1267 |
c->working_directory_missing_ok ? UNIT_MOUNT_WANTS : UNIT_MOUNT_REQUIRES); |
| 1268 |
if (r < 0) |
| 1269 |
return r; |
| 1270 |
} |
| 1271 |
|
| 1272 |
if (c->root_directory) { |
| 1273 |
r = unit_add_mounts_for(u, c->root_directory, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_WANTS); |
| 1274 |
if (r < 0) |
| 1275 |
return r; |
| 1276 |
} |
| 1277 |
|
| 1278 |
if (c->root_image) { |
| 1279 |
r = unit_add_mounts_for(u, c->root_image, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_WANTS); |
| 1280 |
if (r < 0) |
| 1281 |
return r; |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (c->root_mstack) { |
| 1285 |
r = unit_add_mounts_for(u, c->root_mstack, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_WANTS); |
| 1286 |
if (r < 0) |
| 1287 |
return r; |
| 1288 |
} |
| 1289 |
|
| 1290 |
for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) { |
| 1291 |
if (!u->manager->prefix[dt]) |
| 1292 |
continue; |
| 1293 |
|
| 1294 |
FOREACH_ARRAY(i, c->directories[dt].items, c->directories[dt].n_items) { |
| 1295 |
_cleanup_free_ char *p = NULL; |
| 1296 |
|
| 1297 |
p = path_join(u->manager->prefix[dt], i->path); |
| 1298 |
if (!p) |
| 1299 |
return -ENOMEM; |
| 1300 |
|
| 1301 |
r = unit_add_mounts_for(u, p, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_REQUIRES); |
| 1302 |
if (r < 0) |
| 1303 |
return r; |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
if (!MANAGER_IS_SYSTEM(u->manager)) |
| 1308 |
return 0; |
| 1309 |
|
| 1310 |
/* For the following three directory types we need write access, and /var/ is possibly on the root |
| 1311 |
* fs. Hence order after systemd-remount-fs.service, to ensure things are writable. */ |
| 1312 |
if (c->directories[EXEC_DIRECTORY_STATE].n_items > 0 || |
| 1313 |
c->directories[EXEC_DIRECTORY_CACHE].n_items > 0 || |
| 1314 |
c->directories[EXEC_DIRECTORY_LOGS].n_items > 0) { |
| 1315 |
r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, true, UNIT_DEPENDENCY_FILE); |
| 1316 |
if (r < 0) |
| 1317 |
return r; |
| 1318 |
} |
| 1319 |
|
| 1320 |
/* This must be already set in unit_patch_contexts(). */ |
| 1321 |
assert(c->private_var_tmp >= 0 && c->private_var_tmp < _PRIVATE_TMP_MAX); |
| 1322 |
|
| 1323 |
if (c->private_tmp == PRIVATE_TMP_CONNECTED) { |
| 1324 |
r = unit_add_mounts_for(u, "/tmp/", UNIT_DEPENDENCY_FILE, UNIT_MOUNT_WANTS); |
| 1325 |
if (r < 0) |
| 1326 |
return r; |
| 1327 |
} |
| 1328 |
|
| 1329 |
if (c->private_var_tmp == PRIVATE_TMP_CONNECTED) { |
| 1330 |
r = unit_add_mounts_for(u, "/var/tmp/", UNIT_DEPENDENCY_FILE, UNIT_MOUNT_WANTS); |
| 1331 |
if (r < 0) |
| 1332 |
return r; |
| 1333 |
} else if (c->private_var_tmp == PRIVATE_TMP_DISCONNECTED && !exec_context_with_rootfs(c)) { |
| 1334 |
/* Even if PrivateTmp=disconnected, we still require /var/tmp/ mountpoint to be present, |
| 1335 |
* i.e. /var/ needs to be mounted. See comments in unit_patch_contexts(). */ |
| 1336 |
r = unit_add_mounts_for(u, "/var/", UNIT_DEPENDENCY_FILE, UNIT_MOUNT_WANTS); |
| 1337 |
if (r < 0) |
| 1338 |
return r; |
| 1339 |
} |
| 1340 |
|
| 1341 |
if (c->private_tmp == PRIVATE_TMP_CONNECTED || c->private_var_tmp == PRIVATE_TMP_CONNECTED) { |
| 1342 |
r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_TMPFILES_SETUP_SERVICE, true, UNIT_DEPENDENCY_FILE); |
| 1343 |
if (r < 0) |
| 1344 |
return r; |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (c->root_image || c->root_mstack) { |
| 1348 |
/* We need to wait for /dev/loopX to appear when doing RootImage=, hence let's add an |
| 1349 |
* implicit dependency on udev. (And for RootMStack= we might need it) */ |
| 1350 |
|
| 1351 |
r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_UDEVD_SERVICE, true, UNIT_DEPENDENCY_FILE); |
| 1352 |
if (r < 0) |
| 1353 |
return r; |
| 1354 |
} |
| 1355 |
|
| 1356 |
/* If syslog or kernel logging is requested (or log namespacing is), make sure our own logging daemon |
| 1357 |
* is run first. */ |
| 1358 |
if (c->log_namespace) { |
| 1359 |
static const struct { |
| 1360 |
const char *template; |
| 1361 |
UnitType type; |
| 1362 |
} deps[] = { |
| 1363 |
{ "systemd-journald", UNIT_SOCKET, }, |
| 1364 |
{ "systemd-journald-varlink", UNIT_SOCKET, }, |
| 1365 |
{ "systemd-journald-sync", UNIT_SERVICE, }, |
| 1366 |
}; |
| 1367 |
|
| 1368 |
FOREACH_ELEMENT(i, deps) { |
| 1369 |
_cleanup_free_ char *unit = NULL; |
| 1370 |
|
| 1371 |
r = unit_name_build_from_type(i->template, c->log_namespace, i->type, &unit); |
| 1372 |
if (r < 0) |
| 1373 |
return r; |
| 1374 |
|
| 1375 |
r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, unit, true, UNIT_DEPENDENCY_FILE); |
| 1376 |
if (r < 0) |
| 1377 |
return r; |
| 1378 |
} |
| 1379 |
} else if (IN_SET(c->std_output, EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, |
| 1380 |
EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE) || |
| 1381 |
IN_SET(c->std_error, EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE, |
| 1382 |
EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE)) { |
| 1383 |
|
| 1384 |
r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, true, UNIT_DEPENDENCY_FILE); |
| 1385 |
if (r < 0) |
| 1386 |
return r; |
| 1387 |
} |
| 1388 |
|
| 1389 |
return 0; |
| 1390 |
} |
| 1391 |
|
| 1392 |
const char* unit_description(Unit *u) { |
| 1393 |
assert(u); |
| 1394 |
|
| 1395 |
if (u->description) |
| 1396 |
return u->description; |
| 1397 |
|
| 1398 |
return strna(u->id); |
| 1399 |
} |
| 1400 |
|
| 1401 |
const char* unit_status_string(Unit *u, char **ret_combined_buffer) { |
| 1402 |
assert(u); |
| 1403 |
assert(u->id); |
| 1404 |
|
| 1405 |
/* Return u->id, u->description, or "{u->id} - {u->description}". |
| 1406 |
* Versions with u->description are only used if it is set. |
| 1407 |
* The last option is used if configured and the caller provided the 'ret_combined_buffer' |
| 1408 |
* pointer. |
| 1409 |
* |
| 1410 |
* Note that *ret_combined_buffer may be set to NULL. */ |
| 1411 |
|
| 1412 |
if (!u->description || |
| 1413 |
u->manager->status_unit_format == STATUS_UNIT_FORMAT_NAME || |
| 1414 |
(u->manager->status_unit_format == STATUS_UNIT_FORMAT_COMBINED && !ret_combined_buffer) || |
| 1415 |
streq(u->description, u->id)) { |
| 1416 |
|
| 1417 |
if (ret_combined_buffer) |
| 1418 |
*ret_combined_buffer = NULL; |
| 1419 |
return u->id; |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (ret_combined_buffer) { |
| 1423 |
if (u->manager->status_unit_format == STATUS_UNIT_FORMAT_COMBINED) { |
| 1424 |
*ret_combined_buffer = strjoin(u->id, " - ", u->description); |
| 1425 |
if (*ret_combined_buffer) |
| 1426 |
return *ret_combined_buffer; |
| 1427 |
log_oom(); /* Fall back to ->description */ |
| 1428 |
} else |
| 1429 |
*ret_combined_buffer = NULL; |
| 1430 |
} |
| 1431 |
|
| 1432 |
return u->description; |
| 1433 |
} |
| 1434 |
|
| 1435 |
/* Common implementation for multiple backends */ |
| 1436 |
int unit_load_fragment_and_dropin(Unit *u, bool fragment_required) { |
| 1437 |
int r; |
| 1438 |
|
| 1439 |
assert(u); |
| 1440 |
|
| 1441 |
/* Load a .{service,socket,...} file */ |
| 1442 |
r = unit_load_fragment(u); |
| 1443 |
if (r < 0) |
| 1444 |
return r; |
| 1445 |
|
| 1446 |
if (u->load_state == UNIT_MASKED) |
| 1447 |
return 0; |
| 1448 |
|
| 1449 |
if (u->load_state == UNIT_STUB) { |
| 1450 |
if (fragment_required) |
| 1451 |
return -ENOENT; |
| 1452 |
|
| 1453 |
u->load_state = UNIT_LOADED; |
| 1454 |
} |
| 1455 |
|
| 1456 |
u = unit_follow_merge(u); |
| 1457 |
|
| 1458 |
/* Load drop-in directory data. If u is an alias, we might be reloading the |
| 1459 |
* target unit needlessly. But we cannot be sure which drops-ins have already |
| 1460 |
* been loaded and which not, at least without doing complicated book-keeping, |
| 1461 |
* so let's always reread all drop-ins. */ |
| 1462 |
r = unit_load_dropin(u); |
| 1463 |
if (r < 0) |
| 1464 |
return r; |
| 1465 |
|
| 1466 |
if (u->source_path) { |
| 1467 |
struct stat st; |
| 1468 |
|
| 1469 |
if (stat(u->source_path, &st) >= 0) |
| 1470 |
u->source_mtime = timespec_load(&st.st_mtim); |
| 1471 |
else |
| 1472 |
u->source_mtime = 0; |
| 1473 |
} |
| 1474 |
|
| 1475 |
return 0; |
| 1476 |
} |
| 1477 |
|
| 1478 |
void unit_add_to_target_deps_queue(Unit *u) { |
| 1479 |
Manager *m = ASSERT_PTR(ASSERT_PTR(u)->manager); |
| 1480 |
|
| 1481 |
if (u->in_target_deps_queue) |
| 1482 |
return; |
| 1483 |
|
| 1484 |
LIST_PREPEND(target_deps_queue, m->target_deps_queue, u); |
| 1485 |
u->in_target_deps_queue = true; |
| 1486 |
} |
| 1487 |
|
| 1488 |
int unit_add_default_target_dependency(Unit *u, Unit *target) { |
| 1489 |
assert(u); |
| 1490 |
assert(target); |
| 1491 |
|
| 1492 |
if (target->type != UNIT_TARGET) |
| 1493 |
return 0; |
| 1494 |
|
| 1495 |
/* Only add the dependency if both units are loaded, so that |
| 1496 |
* that loop check below is reliable */ |
| 1497 |
if (u->load_state != UNIT_LOADED || |
| 1498 |
target->load_state != UNIT_LOADED) |
| 1499 |
return 0; |
| 1500 |
|
| 1501 |
/* If either side wants no automatic dependencies, then let's |
| 1502 |
* skip this */ |
| 1503 |
if (!u->default_dependencies || |
| 1504 |
!target->default_dependencies) |
| 1505 |
return 0; |
| 1506 |
|
| 1507 |
/* Don't create loops */ |
| 1508 |
if (unit_has_dependency(target, UNIT_ATOM_BEFORE, u)) |
| 1509 |
return 0; |
| 1510 |
|
| 1511 |
return unit_add_dependency(target, UNIT_AFTER, u, true, UNIT_DEPENDENCY_DEFAULT); |
| 1512 |
} |
| 1513 |
|
| 1514 |
static int unit_add_slice_dependencies(Unit *u) { |
| 1515 |
Unit *slice; |
| 1516 |
|
| 1517 |
assert(u); |
| 1518 |
|
| 1519 |
if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
| 1520 |
return 0; |
| 1521 |
|
| 1522 |
/* Slice units are implicitly ordered against their parent slices (as this relationship is encoded in the |
| 1523 |
name), while all other units are ordered based on configuration (as in their case Slice= configures the |
| 1524 |
relationship). */ |
| 1525 |
UnitDependencyMask mask = u->type == UNIT_SLICE ? UNIT_DEPENDENCY_IMPLICIT : UNIT_DEPENDENCY_FILE; |
| 1526 |
|
| 1527 |
slice = UNIT_GET_SLICE(u); |
| 1528 |
if (slice) { |
| 1529 |
if (!IN_SET(slice->freezer_state, FREEZER_RUNNING, FREEZER_THAWING)) |
| 1530 |
u->freezer_state = FREEZER_FROZEN_BY_PARENT; |
| 1531 |
|
| 1532 |
return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, slice, true, mask); |
| 1533 |
} |
| 1534 |
|
| 1535 |
if (unit_has_name(u, SPECIAL_ROOT_SLICE)) |
| 1536 |
return 0; |
| 1537 |
|
| 1538 |
return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_ROOT_SLICE, true, mask); |
| 1539 |
} |
| 1540 |
|
| 1541 |
static int unit_add_mount_dependencies(Unit *u) { |
| 1542 |
bool changed = false; |
| 1543 |
int r; |
| 1544 |
|
| 1545 |
assert(u); |
| 1546 |
|
| 1547 |
for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; ++t) { |
| 1548 |
UnitDependencyInfo di; |
| 1549 |
const char *path; |
| 1550 |
|
| 1551 |
HASHMAP_FOREACH_KEY(di.data, path, u->mounts_for[t]) { |
| 1552 |
|
| 1553 |
char prefix[strlen(ASSERT_PTR(path)) + 1]; |
| 1554 |
|
| 1555 |
PATH_FOREACH_PREFIX_MORE(prefix, path) { |
| 1556 |
_cleanup_free_ char *p = NULL; |
| 1557 |
Unit *m; |
| 1558 |
|
| 1559 |
r = unit_name_from_path(prefix, ".mount", &p); |
| 1560 |
if (r == -EINVAL) |
| 1561 |
continue; /* If the path cannot be converted to a mount unit name, |
| 1562 |
* then it's not manageable as a unit by systemd, and |
| 1563 |
* hence we don't need a dependency on it. Let's thus |
| 1564 |
* silently ignore the issue. */ |
| 1565 |
if (r < 0) |
| 1566 |
return r; |
| 1567 |
|
| 1568 |
m = manager_get_unit(u->manager, p); |
| 1569 |
if (!m) { |
| 1570 |
/* Make sure to load the mount unit if it exists. If so the |
| 1571 |
* dependencies on this unit will be added later during the loading |
| 1572 |
* of the mount unit. */ |
| 1573 |
(void) manager_load_unit_prepare( |
| 1574 |
u->manager, |
| 1575 |
p, |
| 1576 |
/* path= */ NULL, |
| 1577 |
/* e= */ NULL, |
| 1578 |
&m); |
| 1579 |
continue; |
| 1580 |
} |
| 1581 |
if (m == u) |
| 1582 |
continue; |
| 1583 |
|
| 1584 |
if (m->load_state != UNIT_LOADED) |
| 1585 |
continue; |
| 1586 |
|
| 1587 |
r = unit_add_dependency( |
| 1588 |
u, |
| 1589 |
UNIT_AFTER, |
| 1590 |
m, |
| 1591 |
/* add_reference= */ true, |
| 1592 |
di.origin_mask); |
| 1593 |
if (r < 0) |
| 1594 |
return r; |
| 1595 |
changed = changed || r > 0; |
| 1596 |
|
| 1597 |
if (m->fragment_path) { |
| 1598 |
r = unit_add_dependency( |
| 1599 |
u, |
| 1600 |
unit_mount_dependency_type_to_dependency_type(t), |
| 1601 |
m, |
| 1602 |
/* add_reference= */ true, |
| 1603 |
di.origin_mask); |
| 1604 |
if (r < 0) |
| 1605 |
return r; |
| 1606 |
changed = changed || r > 0; |
| 1607 |
} |
| 1608 |
} |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
return changed; |
| 1613 |
} |
| 1614 |
|
| 1615 |
static int unit_add_oomd_dependencies(Unit *u) { |
| 1616 |
CGroupContext *c; |
| 1617 |
CGroupMask mask; |
| 1618 |
int r; |
| 1619 |
|
| 1620 |
assert(u); |
| 1621 |
|
| 1622 |
if (!u->default_dependencies) |
| 1623 |
return 0; |
| 1624 |
|
| 1625 |
c = unit_get_cgroup_context(u); |
| 1626 |
if (!c) |
| 1627 |
return 0; |
| 1628 |
|
| 1629 |
bool wants_oomd = c->moom_swap == MANAGED_OOM_KILL || c->moom_mem_pressure == MANAGED_OOM_KILL || !strv_isempty(c->moom_rules); |
| 1630 |
if (!wants_oomd) |
| 1631 |
return 0; |
| 1632 |
|
| 1633 |
r = cg_mask_supported(&mask); |
| 1634 |
if (r < 0) |
| 1635 |
return log_debug_errno(r, "Failed to determine supported controllers: %m"); |
| 1636 |
|
| 1637 |
if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY)) |
| 1638 |
return 0; |
| 1639 |
|
| 1640 |
return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, "systemd-oomd.service", true, UNIT_DEPENDENCY_FILE); |
| 1641 |
} |
| 1642 |
|
| 1643 |
static int unit_add_startup_units(Unit *u) { |
| 1644 |
if (!unit_has_startup_cgroup_constraints(u)) |
| 1645 |
return 0; |
| 1646 |
|
| 1647 |
return set_ensure_put(&u->manager->startup_units, NULL, u); |
| 1648 |
} |
| 1649 |
|
| 1650 |
static const struct { |
| 1651 |
UnitDependencyAtom atom; |
| 1652 |
size_t job_mode_offset; |
| 1653 |
const char *dependency_name; |
| 1654 |
const char *job_mode_setting_name; |
| 1655 |
} on_termination_settings[] = { |
| 1656 |
{ UNIT_ATOM_ON_SUCCESS, offsetof(Unit, on_success_job_mode), "OnSuccess=", "OnSuccessJobMode=" }, |
| 1657 |
{ UNIT_ATOM_ON_FAILURE, offsetof(Unit, on_failure_job_mode), "OnFailure=", "OnFailureJobMode=" }, |
| 1658 |
}; |
| 1659 |
|
| 1660 |
static int unit_validate_on_termination_job_modes(Unit *u) { |
| 1661 |
assert(u); |
| 1662 |
|
| 1663 |
/* Verify that if On{Success,Failure}JobMode=isolate, only one unit gets specified. */ |
| 1664 |
|
| 1665 |
FOREACH_ELEMENT(setting, on_termination_settings) { |
| 1666 |
JobMode job_mode = *(JobMode*) ((uint8_t*) u + setting->job_mode_offset); |
| 1667 |
|
| 1668 |
if (job_mode != JOB_ISOLATE) |
| 1669 |
continue; |
| 1670 |
|
| 1671 |
Unit *other, *found = NULL; |
| 1672 |
UNIT_FOREACH_DEPENDENCY(other, u, setting->atom) { |
| 1673 |
if (!found) |
| 1674 |
found = other; |
| 1675 |
else if (found != other) |
| 1676 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), |
| 1677 |
"More than one %s dependencies specified but %sisolate set. Refusing.", |
| 1678 |
setting->dependency_name, setting->job_mode_setting_name); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
return 0; |
| 1683 |
} |
| 1684 |
|
| 1685 |
int unit_load(Unit *u) { |
| 1686 |
int r; |
| 1687 |
|
| 1688 |
assert(u); |
| 1689 |
|
| 1690 |
if (u->in_load_queue) { |
| 1691 |
LIST_REMOVE(load_queue, u->manager->load_queue, u); |
| 1692 |
u->in_load_queue = false; |
| 1693 |
} |
| 1694 |
|
| 1695 |
if (u->type == _UNIT_TYPE_INVALID) |
| 1696 |
return -EINVAL; |
| 1697 |
|
| 1698 |
if (u->load_state != UNIT_STUB) |
| 1699 |
return 0; |
| 1700 |
|
| 1701 |
if (u->transient_file) { |
| 1702 |
/* Finalize transient file: if this is a transient unit file, as soon as we reach unit_load() the setup |
| 1703 |
* is complete, hence let's synchronize the unit file we just wrote to disk. */ |
| 1704 |
|
| 1705 |
r = fflush_and_check(u->transient_file); |
| 1706 |
if (r < 0) |
| 1707 |
goto fail; |
| 1708 |
|
| 1709 |
u->transient_file = safe_fclose(u->transient_file); |
| 1710 |
u->fragment_mtime = now(CLOCK_REALTIME); |
| 1711 |
} |
| 1712 |
|
| 1713 |
r = UNIT_VTABLE(u)->load(u); |
| 1714 |
if (r < 0) |
| 1715 |
goto fail; |
| 1716 |
|
| 1717 |
assert(u->load_state != UNIT_STUB); |
| 1718 |
|
| 1719 |
if (u->load_state == UNIT_LOADED) { |
| 1720 |
unit_add_to_target_deps_queue(u); |
| 1721 |
|
| 1722 |
r = unit_add_slice_dependencies(u); |
| 1723 |
if (r < 0) |
| 1724 |
goto fail; |
| 1725 |
|
| 1726 |
r = unit_add_mount_dependencies(u); |
| 1727 |
if (r < 0) |
| 1728 |
goto fail; |
| 1729 |
|
| 1730 |
r = unit_add_oomd_dependencies(u); |
| 1731 |
if (r < 0) |
| 1732 |
goto fail; |
| 1733 |
|
| 1734 |
r = unit_add_startup_units(u); |
| 1735 |
if (r < 0) |
| 1736 |
goto fail; |
| 1737 |
|
| 1738 |
r = unit_validate_on_termination_job_modes(u); |
| 1739 |
if (r < 0) |
| 1740 |
goto fail; |
| 1741 |
|
| 1742 |
if (u->job_running_timeout != USEC_INFINITY && u->job_running_timeout > u->job_timeout) |
| 1743 |
log_unit_warning(u, "JobRunningTimeoutSec= is greater than JobTimeoutSec=, it has no effect."); |
| 1744 |
|
| 1745 |
/* We finished loading, let's ensure our parents recalculate the members mask */ |
| 1746 |
unit_invalidate_cgroup_members_masks(u); |
| 1747 |
} |
| 1748 |
|
| 1749 |
assert((u->load_state != UNIT_MERGED) == !u->merged_into); |
| 1750 |
|
| 1751 |
unit_add_to_dbus_queue(unit_follow_merge(u)); |
| 1752 |
unit_add_to_gc_queue(u); |
| 1753 |
(void) manager_varlink_send_managed_oom_update(u); |
| 1754 |
|
| 1755 |
return 0; |
| 1756 |
|
| 1757 |
fail: |
| 1758 |
/* We convert ENOEXEC errors to the UNIT_BAD_SETTING load state here. Configuration parsing code |
| 1759 |
* should hence return ENOEXEC to ensure units are placed in this state after loading. */ |
| 1760 |
|
| 1761 |
u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND : |
| 1762 |
r == -ENOEXEC ? UNIT_BAD_SETTING : |
| 1763 |
UNIT_ERROR; |
| 1764 |
u->load_error = r; |
| 1765 |
|
| 1766 |
/* Record the timestamp on the cache, so that if the cache gets updated between now and the next time |
| 1767 |
* an attempt is made to load this unit, we know we need to check again. */ |
| 1768 |
if (u->load_state == UNIT_NOT_FOUND) |
| 1769 |
u->fragment_not_found_timestamp_hash = u->manager->unit_cache_timestamp_hash; |
| 1770 |
|
| 1771 |
unit_add_to_dbus_queue(u); |
| 1772 |
unit_add_to_gc_queue(u); |
| 1773 |
|
| 1774 |
return log_unit_debug_errno(u, r, "Failed to load configuration: %m"); |
| 1775 |
} |
| 1776 |
|
| 1777 |
_printf_(7, 8) |
| 1778 |
static int log_unit_internal(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) { |
| 1779 |
Unit *u = userdata; |
| 1780 |
va_list ap; |
| 1781 |
int r; |
| 1782 |
|
| 1783 |
if (u && !unit_log_level_test(u, level)) |
| 1784 |
return -ERRNO_VALUE(error); |
| 1785 |
|
| 1786 |
va_start(ap, format); |
| 1787 |
if (u) |
| 1788 |
r = log_object_internalv(level, error, file, line, func, |
| 1789 |
unit_log_field(u), |
| 1790 |
u->id, |
| 1791 |
unit_invocation_log_field(u), |
| 1792 |
u->invocation_id_string, |
| 1793 |
format, ap); |
| 1794 |
else |
| 1795 |
r = log_internalv(level, error, file, line, func, format, ap); |
| 1796 |
va_end(ap); |
| 1797 |
|
| 1798 |
return r; |
| 1799 |
} |
| 1800 |
|
| 1801 |
static bool unit_test_condition(Unit *u) { |
| 1802 |
_cleanup_strv_free_ char **env = NULL; |
| 1803 |
int r; |
| 1804 |
|
| 1805 |
assert(u); |
| 1806 |
|
| 1807 |
dual_timestamp_now(&u->condition_timestamp); |
| 1808 |
|
| 1809 |
r = manager_get_effective_environment(u->manager, &env); |
| 1810 |
if (r < 0) { |
| 1811 |
log_unit_error_errno(u, r, "Failed to determine effective environment: %m"); |
| 1812 |
u->condition_result = true; |
| 1813 |
} else |
| 1814 |
u->condition_result = condition_test_list( |
| 1815 |
u->conditions, |
| 1816 |
env, |
| 1817 |
condition_type_to_string, |
| 1818 |
log_unit_internal, |
| 1819 |
u); |
| 1820 |
|
| 1821 |
unit_add_to_dbus_queue(u); |
| 1822 |
return u->condition_result; |
| 1823 |
} |
| 1824 |
|
| 1825 |
static bool unit_test_assert(Unit *u) { |
| 1826 |
_cleanup_strv_free_ char **env = NULL; |
| 1827 |
int r; |
| 1828 |
|
| 1829 |
assert(u); |
| 1830 |
|
| 1831 |
dual_timestamp_now(&u->assert_timestamp); |
| 1832 |
|
| 1833 |
r = manager_get_effective_environment(u->manager, &env); |
| 1834 |
if (r < 0) { |
| 1835 |
log_unit_error_errno(u, r, "Failed to determine effective environment: %m"); |
| 1836 |
u->assert_result = CONDITION_ERROR; |
| 1837 |
} else |
| 1838 |
u->assert_result = condition_test_list( |
| 1839 |
u->asserts, |
| 1840 |
env, |
| 1841 |
assert_type_to_string, |
| 1842 |
log_unit_internal, |
| 1843 |
u); |
| 1844 |
|
| 1845 |
unit_add_to_dbus_queue(u); |
| 1846 |
return u->assert_result; |
| 1847 |
} |
| 1848 |
|
| 1849 |
void unit_status_printf( |
| 1850 |
Unit *u, |
| 1851 |
StatusType status_type, |
| 1852 |
const char *status, |
| 1853 |
const char *format, |
| 1854 |
const char *ident) { |
| 1855 |
|
| 1856 |
assert(u); |
| 1857 |
|
| 1858 |
if (log_get_show_color()) { |
| 1859 |
if (u->manager->status_unit_format == STATUS_UNIT_FORMAT_COMBINED && strchr(ident, ' ')) |
| 1860 |
ident = strjoina(ANSI_HIGHLIGHT, u->id, ANSI_NORMAL, " - ", u->description); |
| 1861 |
else |
| 1862 |
ident = strjoina(ANSI_HIGHLIGHT, ident, ANSI_NORMAL); |
| 1863 |
} |
| 1864 |
|
| 1865 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 1866 |
manager_status_printf(u->manager, status_type, status, format, ident); |
| 1867 |
REENABLE_WARNING; |
| 1868 |
} |
| 1869 |
|
| 1870 |
int unit_test_start_limit(Unit *u) { |
| 1871 |
const char *reason; |
| 1872 |
|
| 1873 |
assert(u); |
| 1874 |
|
| 1875 |
if (ratelimit_below(&u->start_ratelimit)) { |
| 1876 |
u->start_limit_hit = false; |
| 1877 |
return 0; |
| 1878 |
} |
| 1879 |
|
| 1880 |
log_unit_warning(u, "Start request repeated too quickly."); |
| 1881 |
u->start_limit_hit = true; |
| 1882 |
|
| 1883 |
reason = strjoina("unit ", u->id, " failed"); |
| 1884 |
|
| 1885 |
emergency_action( |
| 1886 |
u->manager, |
| 1887 |
u->start_limit_action, |
| 1888 |
EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN|EMERGENCY_ACTION_SLEEP_5S, |
| 1889 |
u->reboot_arg, |
| 1890 |
/* exit_status= */ -1, |
| 1891 |
reason); |
| 1892 |
|
| 1893 |
return -ECANCELED; |
| 1894 |
} |
| 1895 |
|
| 1896 |
static bool unit_verify_deps(Unit *u) { |
| 1897 |
Unit *other; |
| 1898 |
|
| 1899 |
assert(u); |
| 1900 |
|
| 1901 |
/* Checks whether all BindsTo= dependencies of this unit are fulfilled — if they are also combined |
| 1902 |
* with After=. We do not check Requires= or Requisite= here as they only should have an effect on |
| 1903 |
* the job processing, but do not have any effect afterwards. We don't check BindsTo= dependencies |
| 1904 |
* that are not used in conjunction with After= as for them any such check would make things entirely |
| 1905 |
* racy. */ |
| 1906 |
|
| 1907 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT) { |
| 1908 |
|
| 1909 |
if (!unit_has_dependency(u, UNIT_ATOM_AFTER, other)) |
| 1910 |
continue; |
| 1911 |
|
| 1912 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(other))) { |
| 1913 |
log_unit_notice(u, "Bound to unit %s, but unit isn't active.", other->id); |
| 1914 |
return false; |
| 1915 |
} |
| 1916 |
} |
| 1917 |
|
| 1918 |
return true; |
| 1919 |
} |
| 1920 |
|
| 1921 |
/* Errors that aren't really errors: |
| 1922 |
* -EALREADY: Unit is already started. |
| 1923 |
* -ECOMM: Condition failed |
| 1924 |
* -EAGAIN: An operation is already in progress. Retry later. |
| 1925 |
* |
| 1926 |
* Errors that are real errors: |
| 1927 |
* -EBADR: This unit type does not support starting. |
| 1928 |
* -ECANCELED: Start limit hit, too many requests for now |
| 1929 |
* -EPROTO: Assert failed |
| 1930 |
* -EINVAL: Unit not loaded |
| 1931 |
* -EOPNOTSUPP: Unit type not supported |
| 1932 |
* -ENOLINK: The necessary dependencies are not fulfilled. |
| 1933 |
* -ESTALE: This unit has been started before and can't be started a second time |
| 1934 |
* -EDEADLK: This unit is frozen |
| 1935 |
* -ENOENT: This is a triggering unit and unit to trigger is not loaded |
| 1936 |
* -ETOOMANYREFS: The hard concurrency limit of at least one of the slices the unit is contained in has been reached |
| 1937 |
*/ |
| 1938 |
int unit_start(Unit *u, ActivationDetails *details) { |
| 1939 |
UnitActiveState state; |
| 1940 |
Unit *following; |
| 1941 |
int r; |
| 1942 |
|
| 1943 |
assert(u); |
| 1944 |
|
| 1945 |
/* Let's hold off running start jobs for mount units when /proc/self/mountinfo monitor is ratelimited. */ |
| 1946 |
if (UNIT_VTABLE(u)->subsystem_ratelimited) { |
| 1947 |
r = UNIT_VTABLE(u)->subsystem_ratelimited(u->manager); |
| 1948 |
if (r < 0) |
| 1949 |
return r; |
| 1950 |
if (r > 0) |
| 1951 |
return -EAGAIN; |
| 1952 |
} |
| 1953 |
|
| 1954 |
/* If this is already started, then this will succeed. Note that this will even succeed if this unit |
| 1955 |
* is not startable by the user. This is relied on to detect when we need to wait for units and when |
| 1956 |
* waiting is finished. */ |
| 1957 |
state = unit_active_state(u); |
| 1958 |
if (UNIT_IS_ACTIVE_OR_RELOADING(state)) |
| 1959 |
return -EALREADY; |
| 1960 |
if (IN_SET(state, UNIT_DEACTIVATING, UNIT_MAINTENANCE)) |
| 1961 |
return -EAGAIN; |
| 1962 |
|
| 1963 |
/* Units that aren't loaded cannot be started */ |
| 1964 |
if (u->load_state != UNIT_LOADED) |
| 1965 |
return -EINVAL; |
| 1966 |
|
| 1967 |
/* Refuse starting scope units more than once */ |
| 1968 |
if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_enter_timestamp)) |
| 1969 |
return -ESTALE; |
| 1970 |
|
| 1971 |
/* If the conditions were unmet, don't do anything at all. If we already are activating this call might |
| 1972 |
* still be useful to speed up activation in case there is some hold-off time, but we don't want to |
| 1973 |
* recheck the condition in that case. */ |
| 1974 |
if (state != UNIT_ACTIVATING && |
| 1975 |
!unit_test_condition(u)) |
| 1976 |
return log_unit_debug_errno(u, SYNTHETIC_ERRNO(ECOMM), "Starting requested but condition not met. Not starting unit."); |
| 1977 |
|
| 1978 |
/* If the asserts failed, fail the entire job */ |
| 1979 |
if (state != UNIT_ACTIVATING && |
| 1980 |
!unit_test_assert(u)) |
| 1981 |
return log_unit_notice_errno(u, SYNTHETIC_ERRNO(EPROTO), "Starting requested but asserts failed."); |
| 1982 |
|
| 1983 |
/* Units of types that aren't supported cannot be started. Note that we do this test only after the |
| 1984 |
* condition checks, so that we rather return condition check errors (which are usually not |
| 1985 |
* considered a true failure) than "not supported" errors (which are considered a failure). |
| 1986 |
*/ |
| 1987 |
if (!unit_type_supported(u->type)) |
| 1988 |
return -EOPNOTSUPP; |
| 1989 |
|
| 1990 |
/* Let's make sure that the deps really are in order before we start this. Normally the job engine |
| 1991 |
* should have taken care of this already, but let's check this here again. After all, our |
| 1992 |
* dependencies might not be in effect anymore, due to a reload or due to an unmet condition. */ |
| 1993 |
if (!unit_verify_deps(u)) |
| 1994 |
return -ENOLINK; |
| 1995 |
|
| 1996 |
/* Forward to the main object, if we aren't it. */ |
| 1997 |
following = unit_following(u); |
| 1998 |
if (following) { |
| 1999 |
log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id); |
| 2000 |
return unit_start(following, details); |
| 2001 |
} |
| 2002 |
|
| 2003 |
/* Check to make sure the unit isn't frozen */ |
| 2004 |
if (u->freezer_state != FREEZER_RUNNING) |
| 2005 |
return -EDEADLK; |
| 2006 |
|
| 2007 |
/* Check our ability to start early so that ratelimited or already starting/started units don't |
| 2008 |
* cause us to enter a busy loop. */ |
| 2009 |
if (UNIT_VTABLE(u)->test_startable) { |
| 2010 |
r = UNIT_VTABLE(u)->test_startable(u); |
| 2011 |
if (r <= 0) |
| 2012 |
return r; |
| 2013 |
} |
| 2014 |
|
| 2015 |
/* If it is stopped, but we cannot start it, then fail */ |
| 2016 |
if (!UNIT_VTABLE(u)->start) |
| 2017 |
return -EBADR; |
| 2018 |
|
| 2019 |
if (UNIT_IS_INACTIVE_OR_FAILED(state)) { |
| 2020 |
Slice *slice = SLICE(UNIT_GET_SLICE(u)); |
| 2021 |
|
| 2022 |
if (slice) { |
| 2023 |
/* Check hard concurrency limit. Note this is partially redundant, we already checked |
| 2024 |
* this when enqueuing jobs. However, between the time when we enqueued this and the |
| 2025 |
* time we are dispatching the queue the configuration might have changed, hence |
| 2026 |
* check here again */ |
| 2027 |
if (slice_concurrency_hard_max_reached(slice, u)) |
| 2028 |
return -ETOOMANYREFS; |
| 2029 |
|
| 2030 |
/* Also check soft concurrenty limit, and return EAGAIN so that the job is kept in |
| 2031 |
* the queue */ |
| 2032 |
if (slice_concurrency_soft_max_reached(slice, u)) |
| 2033 |
return -EAGAIN; /* Try again, keep in queue */ |
| 2034 |
} |
| 2035 |
} |
| 2036 |
|
| 2037 |
/* We don't suppress calls to ->start() here when we are already starting, to allow this request to |
| 2038 |
* be used as a "hurry up" call, for example when the unit is in some "auto restart" state where it |
| 2039 |
* waits for a holdoff timer to elapse before it will start again. */ |
| 2040 |
|
| 2041 |
unit_add_to_dbus_queue(u); |
| 2042 |
|
| 2043 |
if (!u->activation_details) /* Older details object wins */ |
| 2044 |
u->activation_details = activation_details_ref(details); |
| 2045 |
|
| 2046 |
return UNIT_VTABLE(u)->start(u); |
| 2047 |
} |
| 2048 |
|
| 2049 |
bool unit_can_start(Unit *u) { |
| 2050 |
assert(u); |
| 2051 |
|
| 2052 |
if (u->load_state != UNIT_LOADED) |
| 2053 |
return false; |
| 2054 |
|
| 2055 |
if (!unit_type_supported(u->type)) |
| 2056 |
return false; |
| 2057 |
|
| 2058 |
/* Scope units may be started only once */ |
| 2059 |
if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_exit_timestamp)) |
| 2060 |
return false; |
| 2061 |
|
| 2062 |
return !!UNIT_VTABLE(u)->start; |
| 2063 |
} |
| 2064 |
|
| 2065 |
bool unit_can_isolate(Unit *u) { |
| 2066 |
assert(u); |
| 2067 |
|
| 2068 |
return unit_can_start(u) && |
| 2069 |
u->allow_isolate; |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* Errors: |
| 2073 |
* -EBADR: This unit type does not support stopping. |
| 2074 |
* -EALREADY: Unit is already stopped. |
| 2075 |
* -EAGAIN: An operation is already in progress. Retry later. |
| 2076 |
* -EDEADLK: Unit is frozen |
| 2077 |
*/ |
| 2078 |
int unit_stop(Unit *u) { |
| 2079 |
UnitActiveState state; |
| 2080 |
Unit *following; |
| 2081 |
|
| 2082 |
assert(u); |
| 2083 |
|
| 2084 |
state = unit_active_state(u); |
| 2085 |
if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
| 2086 |
return -EALREADY; |
| 2087 |
|
| 2088 |
following = unit_following(u); |
| 2089 |
if (following) { |
| 2090 |
log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id); |
| 2091 |
return unit_stop(following); |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* Check to make sure the unit isn't frozen */ |
| 2095 |
if (u->freezer_state != FREEZER_RUNNING) |
| 2096 |
return -EDEADLK; |
| 2097 |
|
| 2098 |
if (!UNIT_VTABLE(u)->stop) |
| 2099 |
return -EBADR; |
| 2100 |
|
| 2101 |
unit_add_to_dbus_queue(u); |
| 2102 |
|
| 2103 |
return UNIT_VTABLE(u)->stop(u); |
| 2104 |
} |
| 2105 |
|
| 2106 |
bool unit_can_stop(Unit *u) { |
| 2107 |
assert(u); |
| 2108 |
|
| 2109 |
/* Note: if we return true here, it does not mean that the unit may be successfully stopped. |
| 2110 |
* Extrinsic units follow external state and they may stop following external state changes |
| 2111 |
* (hence we return true here), but an attempt to do this through the manager will fail. */ |
| 2112 |
|
| 2113 |
if (!unit_type_supported(u->type)) |
| 2114 |
return false; |
| 2115 |
|
| 2116 |
if (u->perpetual) |
| 2117 |
return false; |
| 2118 |
|
| 2119 |
return !!UNIT_VTABLE(u)->stop; |
| 2120 |
} |
| 2121 |
|
| 2122 |
/* Errors: |
| 2123 |
* -EBADR: This unit type does not support reloading. |
| 2124 |
* -ENOEXEC: Unit is not started. |
| 2125 |
* -EAGAIN: An operation is already in progress. Retry later. |
| 2126 |
* -EDEADLK: Unit is frozen. |
| 2127 |
*/ |
| 2128 |
int unit_reload(Unit *u) { |
| 2129 |
UnitActiveState state; |
| 2130 |
Unit *following; |
| 2131 |
|
| 2132 |
assert(u); |
| 2133 |
|
| 2134 |
if (u->load_state != UNIT_LOADED) |
| 2135 |
return -EINVAL; |
| 2136 |
|
| 2137 |
if (!unit_can_reload(u)) |
| 2138 |
return -EBADR; |
| 2139 |
|
| 2140 |
state = unit_active_state(u); |
| 2141 |
if (IN_SET(state, UNIT_RELOADING, UNIT_REFRESHING)) |
| 2142 |
/* "refreshing" means some resources in the unit namespace is being updated. Unlike reload, |
| 2143 |
* the unit processes aren't made aware of refresh. Let's put the job back to queue |
| 2144 |
* in both cases, as refresh typically takes place before reload and it's better to wait |
| 2145 |
* for it rather than failing. */ |
| 2146 |
return -EAGAIN; |
| 2147 |
|
| 2148 |
if (state != UNIT_ACTIVE) |
| 2149 |
return log_unit_warning_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "Unit cannot be reloaded because it is inactive."); |
| 2150 |
|
| 2151 |
following = unit_following(u); |
| 2152 |
if (following) { |
| 2153 |
log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id); |
| 2154 |
return unit_reload(following); |
| 2155 |
} |
| 2156 |
|
| 2157 |
/* Check to make sure the unit isn't frozen */ |
| 2158 |
if (u->freezer_state != FREEZER_RUNNING) |
| 2159 |
return -EDEADLK; |
| 2160 |
|
| 2161 |
unit_add_to_dbus_queue(u); |
| 2162 |
|
| 2163 |
if (!UNIT_VTABLE(u)->reload) { |
| 2164 |
/* Unit doesn't have a reload function, but we need to propagate the reload anyway */ |
| 2165 |
unit_notify(u, unit_active_state(u), unit_active_state(u), /* reload_success= */ true); |
| 2166 |
return 0; |
| 2167 |
} |
| 2168 |
|
| 2169 |
return UNIT_VTABLE(u)->reload(u); |
| 2170 |
} |
| 2171 |
|
| 2172 |
bool unit_can_reload(Unit *u) { |
| 2173 |
assert(u); |
| 2174 |
|
| 2175 |
if (UNIT_VTABLE(u)->can_reload) |
| 2176 |
return UNIT_VTABLE(u)->can_reload(u); |
| 2177 |
|
| 2178 |
if (unit_has_dependency(u, UNIT_ATOM_PROPAGATES_RELOAD_TO, NULL)) |
| 2179 |
return true; |
| 2180 |
|
| 2181 |
return UNIT_VTABLE(u)->reload; |
| 2182 |
} |
| 2183 |
|
| 2184 |
bool unit_is_unneeded(Unit *u) { |
| 2185 |
Unit *other; |
| 2186 |
assert(u); |
| 2187 |
|
| 2188 |
if (!u->stop_when_unneeded) |
| 2189 |
return false; |
| 2190 |
|
| 2191 |
/* Don't clean up while the unit is transitioning or is even inactive. */ |
| 2192 |
if (unit_active_state(u) != UNIT_ACTIVE) |
| 2193 |
return false; |
| 2194 |
if (u->job) |
| 2195 |
return false; |
| 2196 |
|
| 2197 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED) { |
| 2198 |
/* If a dependent unit has a job queued, is active or transitioning, or is marked for |
| 2199 |
* restart, then don't clean this one up. */ |
| 2200 |
|
| 2201 |
if (other->job) |
| 2202 |
return false; |
| 2203 |
|
| 2204 |
if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) |
| 2205 |
return false; |
| 2206 |
|
| 2207 |
if (unit_will_restart(other)) |
| 2208 |
return false; |
| 2209 |
} |
| 2210 |
|
| 2211 |
return true; |
| 2212 |
} |
| 2213 |
|
| 2214 |
bool unit_is_upheld_by_active(Unit *u, Unit **ret_culprit) { |
| 2215 |
Unit *other; |
| 2216 |
|
| 2217 |
assert(u); |
| 2218 |
|
| 2219 |
/* Checks if the unit needs to be started because it currently is not running, but some other unit |
| 2220 |
* that is active declared an Uphold= dependencies on it */ |
| 2221 |
|
| 2222 |
if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) || u->job) { |
| 2223 |
if (ret_culprit) |
| 2224 |
*ret_culprit = NULL; |
| 2225 |
return false; |
| 2226 |
} |
| 2227 |
|
| 2228 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_START_STEADILY) { |
| 2229 |
if (other->job) |
| 2230 |
continue; |
| 2231 |
|
| 2232 |
if (UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(other))) { |
| 2233 |
if (ret_culprit) |
| 2234 |
*ret_culprit = other; |
| 2235 |
return true; |
| 2236 |
} |
| 2237 |
} |
| 2238 |
|
| 2239 |
if (ret_culprit) |
| 2240 |
*ret_culprit = NULL; |
| 2241 |
return false; |
| 2242 |
} |
| 2243 |
|
| 2244 |
bool unit_is_bound_by_inactive(Unit *u, Unit **ret_culprit) { |
| 2245 |
Unit *other; |
| 2246 |
|
| 2247 |
assert(u); |
| 2248 |
|
| 2249 |
/* Checks whether this unit is bound to another unit that is inactive, i.e. whether we should stop |
| 2250 |
* because the other unit is down. */ |
| 2251 |
|
| 2252 |
if (unit_active_state(u) != UNIT_ACTIVE || u->job) { |
| 2253 |
/* Don't clean up while the unit is transitioning or is even inactive. */ |
| 2254 |
if (ret_culprit) |
| 2255 |
*ret_culprit = NULL; |
| 2256 |
return false; |
| 2257 |
} |
| 2258 |
|
| 2259 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT) { |
| 2260 |
if (other->job) |
| 2261 |
continue; |
| 2262 |
|
| 2263 |
if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) { |
| 2264 |
if (ret_culprit) |
| 2265 |
*ret_culprit = other; |
| 2266 |
|
| 2267 |
return true; |
| 2268 |
} |
| 2269 |
} |
| 2270 |
|
| 2271 |
if (ret_culprit) |
| 2272 |
*ret_culprit = NULL; |
| 2273 |
return false; |
| 2274 |
} |
| 2275 |
|
| 2276 |
static void check_unneeded_dependencies(Unit *u) { |
| 2277 |
Unit *other; |
| 2278 |
assert(u); |
| 2279 |
|
| 2280 |
/* Add all units this unit depends on to the queue that processes StopWhenUnneeded= behaviour. */ |
| 2281 |
|
| 2282 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE) |
| 2283 |
unit_submit_to_stop_when_unneeded_queue(other); |
| 2284 |
} |
| 2285 |
|
| 2286 |
static void check_uphold_dependencies(Unit *u) { |
| 2287 |
Unit *other; |
| 2288 |
assert(u); |
| 2289 |
|
| 2290 |
/* Add all units this unit depends on to the queue that processes Uphold= behaviour. */ |
| 2291 |
|
| 2292 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_ADD_START_WHEN_UPHELD_QUEUE) |
| 2293 |
unit_submit_to_start_when_upheld_queue(other); |
| 2294 |
} |
| 2295 |
|
| 2296 |
static void check_bound_by_dependencies(Unit *u) { |
| 2297 |
Unit *other; |
| 2298 |
assert(u); |
| 2299 |
|
| 2300 |
/* Add all units this unit depends on to the queue that processes BindsTo= stop behaviour. */ |
| 2301 |
|
| 2302 |
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_ADD_CANNOT_BE_ACTIVE_WITHOUT_QUEUE) |
| 2303 |
unit_submit_to_stop_when_bound_queue(other); |
| 2304 |
} |
| 2305 |
|
| 2306 |
static void retroactively_start_dependencies(Unit *u) { |
| 2307 |
Unit *other; |
| 2308 |
|
| 2309 |
assert(u); |
| 2310 |
assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))); |
| 2311 |
|
| 2312 |
UNIT_FOREACH_DEPENDENCY_SAFE(other, u, UNIT_ATOM_RETROACTIVE_START_REPLACE) /* Requires= + BindsTo= */ |
| 2313 |
if (!unit_has_dependency(u, UNIT_ATOM_AFTER, other) && |
| 2314 |
!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
| 2315 |
(void) manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, /* reterr_error= */ NULL, /* ret= */ NULL); |
| 2316 |
|
| 2317 |
UNIT_FOREACH_DEPENDENCY_SAFE(other, u, UNIT_ATOM_RETROACTIVE_START_FAIL) /* Wants= */ |
| 2318 |
if (!unit_has_dependency(u, UNIT_ATOM_AFTER, other) && |
| 2319 |
!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other))) |
| 2320 |
(void) manager_add_job(u->manager, JOB_START, other, JOB_FAIL, /* reterr_error= */ NULL, /* ret= */ NULL); |
| 2321 |
|
| 2322 |
UNIT_FOREACH_DEPENDENCY_SAFE(other, u, UNIT_ATOM_RETROACTIVE_STOP_ON_START) /* Conflicts= (and inverse) */ |
| 2323 |
if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
| 2324 |
(void) manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, /* reterr_error= */ NULL, /* ret= */ NULL); |
| 2325 |
} |
| 2326 |
|
| 2327 |
static void retroactively_stop_dependencies(Unit *u) { |
| 2328 |
Unit *other; |
| 2329 |
|
| 2330 |
assert(u); |
| 2331 |
assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))); |
| 2332 |
|
| 2333 |
/* Pull down units which are bound to us recursively if enabled */ |
| 2334 |
UNIT_FOREACH_DEPENDENCY_SAFE(other, u, UNIT_ATOM_RETROACTIVE_STOP_ON_STOP) /* BoundBy= */ |
| 2335 |
if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other))) |
| 2336 |
(void) manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, /* reterr_error= */ NULL, /* ret= */ NULL); |
| 2337 |
} |
| 2338 |
|
| 2339 |
void unit_start_on_termination_deps(Unit *u, UnitDependencyAtom atom) { |
| 2340 |
const char *dependency_name = NULL; |
| 2341 |
JobMode job_mode = _JOB_MODE_INVALID; |
| 2342 |
unsigned n_jobs = 0; |
| 2343 |
int r; |
| 2344 |
|
| 2345 |
/* Act on OnFailure= and OnSuccess= dependencies */ |
| 2346 |
|
| 2347 |
assert(u); |
| 2348 |
assert(u->manager); |
| 2349 |
assert(IN_SET(atom, UNIT_ATOM_ON_SUCCESS, UNIT_ATOM_ON_FAILURE)); |
| 2350 |
|
| 2351 |
FOREACH_ELEMENT(setting, on_termination_settings) |
| 2352 |
if (atom == setting->atom) { |
| 2353 |
job_mode = *(JobMode*) ((uint8_t*) u + setting->job_mode_offset); |
| 2354 |
dependency_name = setting->dependency_name; |
| 2355 |
break; |
| 2356 |
} |
| 2357 |
|
| 2358 |
assert(dependency_name); |
| 2359 |
|
| 2360 |
Unit *other; |
| 2361 |
UNIT_FOREACH_DEPENDENCY_SAFE(other, u, atom) { |
| 2362 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 2363 |
|
| 2364 |
if (n_jobs == 0) |
| 2365 |
log_unit_info(u, "Triggering %s dependencies.", dependency_name); |
| 2366 |
|
| 2367 |
r = manager_add_job(u->manager, JOB_START, other, job_mode, &error, /* ret= */ NULL); |
| 2368 |
if (r < 0) |
| 2369 |
log_unit_warning_errno(u, r, "Failed to enqueue %s%s job, ignoring: %s", |
| 2370 |
dependency_name, other->id, bus_error_message(&error, r)); |
| 2371 |
n_jobs++; |
| 2372 |
} |
| 2373 |
|
| 2374 |
if (n_jobs > 0) |
| 2375 |
log_unit_debug(u, "Triggering %s dependencies done (%u %s).", |
| 2376 |
dependency_name, n_jobs, n_jobs == 1 ? "job" : "jobs"); |
| 2377 |
} |
| 2378 |
|
| 2379 |
void unit_trigger_notify(Unit *u) { |
| 2380 |
Unit *other; |
| 2381 |
|
| 2382 |
assert(u); |
| 2383 |
|
| 2384 |
UNIT_FOREACH_DEPENDENCY_SAFE(other, u, UNIT_ATOM_TRIGGERED_BY) |
| 2385 |
if (UNIT_VTABLE(other)->trigger_notify) |
| 2386 |
UNIT_VTABLE(other)->trigger_notify(other, u); |
| 2387 |
} |
| 2388 |
|
| 2389 |
static int raise_level(int log_level, bool condition_info, bool condition_notice) { |
| 2390 |
if (condition_notice && log_level > LOG_NOTICE) |
| 2391 |
return LOG_NOTICE; |
| 2392 |
if (condition_info && log_level > LOG_INFO) |
| 2393 |
return LOG_INFO; |
| 2394 |
return log_level; |
| 2395 |
} |
| 2396 |
|
| 2397 |
static int unit_log_resources(Unit *u) { |
| 2398 |
|
| 2399 |
static const struct { |
| 2400 |
const char *journal_field; |
| 2401 |
const char *message_suffix; |
| 2402 |
} memory_fields[_CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST + 1] = { |
| 2403 |
[CGROUP_MEMORY_PEAK] = { "MEMORY_PEAK", "memory peak" }, |
| 2404 |
[CGROUP_MEMORY_SWAP_PEAK] = { "MEMORY_SWAP_PEAK", "memory swap peak" }, |
| 2405 |
}, ip_fields[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = { |
| 2406 |
[CGROUP_IP_INGRESS_BYTES] = { "IP_METRIC_INGRESS_BYTES", "incoming IP traffic" }, |
| 2407 |
[CGROUP_IP_EGRESS_BYTES] = { "IP_METRIC_EGRESS_BYTES", "outgoing IP traffic" }, |
| 2408 |
[CGROUP_IP_INGRESS_PACKETS] = { "IP_METRIC_INGRESS_PACKETS", NULL }, |
| 2409 |
[CGROUP_IP_EGRESS_PACKETS] = { "IP_METRIC_EGRESS_PACKETS", NULL }, |
| 2410 |
}, io_fields[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = { |
| 2411 |
[CGROUP_IO_READ_BYTES] = { "IO_METRIC_READ_BYTES", "read from disk" }, |
| 2412 |
[CGROUP_IO_WRITE_BYTES] = { "IO_METRIC_WRITE_BYTES", "written to disk" }, |
| 2413 |
[CGROUP_IO_READ_OPERATIONS] = { "IO_METRIC_READ_OPERATIONS", NULL }, |
| 2414 |
[CGROUP_IO_WRITE_OPERATIONS] = { "IO_METRIC_WRITE_OPERATIONS", NULL }, |
| 2415 |
}; |
| 2416 |
|
| 2417 |
struct iovec *iovec = NULL; |
| 2418 |
size_t n_iovec = 0; |
| 2419 |
_cleanup_free_ char *message = NULL, *t = NULL; |
| 2420 |
nsec_t cpu_nsec = NSEC_INFINITY; |
| 2421 |
int log_level = LOG_DEBUG; /* May be raised if resources consumed over a threshold */ |
| 2422 |
|
| 2423 |
assert(u); |
| 2424 |
|
| 2425 |
CLEANUP_ARRAY(iovec, n_iovec, iovec_array_free); |
| 2426 |
|
| 2427 |
iovec = new(struct iovec, 1 + (_CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST + 1) + |
| 2428 |
_CGROUP_IP_ACCOUNTING_METRIC_MAX + _CGROUP_IO_ACCOUNTING_METRIC_MAX + 4); |
| 2429 |
if (!iovec) |
| 2430 |
return log_oom(); |
| 2431 |
|
| 2432 |
/* Invoked whenever a unit enters failed or dead state. Logs information about consumed resources if resource |
| 2433 |
* accounting was enabled for a unit. It does this in two ways: a friendly human-readable string with reduced |
| 2434 |
* information and the complete data in structured fields. */ |
| 2435 |
|
| 2436 |
(void) unit_get_cpu_usage(u, &cpu_nsec); |
| 2437 |
if (cpu_nsec != NSEC_INFINITY) { |
| 2438 |
/* Format the CPU time for inclusion in the structured log message */ |
| 2439 |
if (asprintf(&t, "CPU_USAGE_NSEC=%" PRIu64, cpu_nsec) < 0) |
| 2440 |
return log_oom(); |
| 2441 |
iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); |
| 2442 |
|
| 2443 |
/* Format the CPU time for inclusion in the human language message string */ |
| 2444 |
if (dual_timestamp_is_set(&u->inactive_exit_timestamp) && |
| 2445 |
dual_timestamp_is_set(&u->inactive_enter_timestamp)) { |
| 2446 |
usec_t wall_clock_usec = usec_sub_unsigned(u->inactive_enter_timestamp.monotonic, u->inactive_exit_timestamp.monotonic); |
| 2447 |
if (strextendf_with_separator(&message, ", ", |
| 2448 |
"Consumed %s CPU time over %s wall clock time", |
| 2449 |
FORMAT_TIMESPAN(cpu_nsec / NSEC_PER_USEC, USEC_PER_MSEC), |
| 2450 |
FORMAT_TIMESPAN(wall_clock_usec, USEC_PER_MSEC)) < 0) |
| 2451 |
return log_oom(); |
| 2452 |
} else { |
| 2453 |
if (strextendf_with_separator(&message, ", ", |
| 2454 |
"Consumed %s CPU time", |
| 2455 |
FORMAT_TIMESPAN(cpu_nsec / NSEC_PER_USEC, USEC_PER_MSEC)) < 0) |
| 2456 |
return log_oom(); |
| 2457 |
} |
| 2458 |
|
| 2459 |
log_level = raise_level(log_level, |
| 2460 |
cpu_nsec > MENTIONWORTHY_CPU_NSEC, |
| 2461 |
cpu_nsec > NOTICEWORTHY_CPU_NSEC); |
| 2462 |
} |
| 2463 |
|
| 2464 |
for (CGroupMemoryAccountingMetric metric = 0; metric <= _CGROUP_MEMORY_ACCOUNTING_METRIC_CACHED_LAST; metric++) { |
| 2465 |
uint64_t value = UINT64_MAX; |
| 2466 |
|
| 2467 |
assert(memory_fields[metric].journal_field); |
| 2468 |
assert(memory_fields[metric].message_suffix); |
| 2469 |
|
| 2470 |
(void) unit_get_memory_accounting(u, metric, &value); |
| 2471 |
if (value == UINT64_MAX) |
| 2472 |
continue; |
| 2473 |
|
| 2474 |
if (asprintf(&t, "%s=%" PRIu64, memory_fields[metric].journal_field, value) < 0) |
| 2475 |
return log_oom(); |
| 2476 |
iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); |
| 2477 |
|
| 2478 |
/* If value is 0, we don't log it in the MESSAGE= field. */ |
| 2479 |
if (value == 0) |
| 2480 |
continue; |
| 2481 |
|
| 2482 |
if (strextendf_with_separator(&message, ", ", "%s %s", |
| 2483 |
FORMAT_BYTES(value), memory_fields[metric].message_suffix) < 0) |
| 2484 |
return log_oom(); |
| 2485 |
|
| 2486 |
log_level = raise_level(log_level, |
| 2487 |
value > MENTIONWORTHY_MEMORY_BYTES, |
| 2488 |
value > NOTICEWORTHY_MEMORY_BYTES); |
| 2489 |
} |
| 2490 |
|
| 2491 |
for (CGroupIOAccountingMetric k = 0; k < _CGROUP_IO_ACCOUNTING_METRIC_MAX; k++) { |
| 2492 |
uint64_t value = UINT64_MAX; |
| 2493 |
|
| 2494 |
assert(io_fields[k].journal_field); |
| 2495 |
|
| 2496 |
(void) unit_get_io_accounting(u, k, &value); |
| 2497 |
if (value == UINT64_MAX) |
| 2498 |
continue; |
| 2499 |
|
| 2500 |
/* Format IO accounting data for inclusion in the structured log message */ |
| 2501 |
if (asprintf(&t, "%s=%" PRIu64, io_fields[k].journal_field, value) < 0) |
| 2502 |
return log_oom(); |
| 2503 |
iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); |
| 2504 |
|
| 2505 |
/* If value is 0, we don't log it in the MESSAGE= field. */ |
| 2506 |
if (value == 0) |
| 2507 |
continue; |
| 2508 |
|
| 2509 |
/* Format the IO accounting data for inclusion in the human language message string, but only |
| 2510 |
* for the bytes counters (and not for the operations counters) */ |
| 2511 |
if (io_fields[k].message_suffix) { |
| 2512 |
if (strextendf_with_separator(&message, ", ", "%s %s", |
| 2513 |
FORMAT_BYTES(value), io_fields[k].message_suffix) < 0) |
| 2514 |
return log_oom(); |
| 2515 |
|
| 2516 |
log_level = raise_level(log_level, |
| 2517 |
value > MENTIONWORTHY_IO_BYTES, |
| 2518 |
value > NOTICEWORTHY_IO_BYTES); |
| 2519 |
} |
| 2520 |
} |
| 2521 |
|
| 2522 |
for (CGroupIPAccountingMetric m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) { |
| 2523 |
uint64_t value = UINT64_MAX; |
| 2524 |
|
| 2525 |
assert(ip_fields[m].journal_field); |
| 2526 |
|
| 2527 |
(void) unit_get_ip_accounting(u, m, &value); |
| 2528 |
if (value == UINT64_MAX) |
| 2529 |
continue; |
| 2530 |
|
| 2531 |
/* Format IP accounting data for inclusion in the structured log message */ |
| 2532 |
if (asprintf(&t, "%s=%" PRIu64, ip_fields[m].journal_field, value) < 0) |
| 2533 |
return log_oom(); |
| 2534 |
iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); |
| 2535 |
|
| 2536 |
/* If value is 0, we don't log it in the MESSAGE= field. */ |
| 2537 |
if (value == 0) |
| 2538 |
continue; |
| 2539 |
|
| 2540 |
/* Format the IP accounting data for inclusion in the human language message string, but only |
| 2541 |
* for the bytes counters (and not for the packets counters) */ |
| 2542 |
if (ip_fields[m].message_suffix) { |
| 2543 |
if (strextendf_with_separator(&message, ", ", "%s %s", |
| 2544 |
FORMAT_BYTES(value), ip_fields[m].message_suffix) < 0) |
| 2545 |
return log_oom(); |
| 2546 |
|
| 2547 |
log_level = raise_level(log_level, |
| 2548 |
value > MENTIONWORTHY_IP_BYTES, |
| 2549 |
value > NOTICEWORTHY_IP_BYTES); |
| 2550 |
} |
| 2551 |
} |
| 2552 |
|
| 2553 |
/* This check is here because it is the earliest point following all possible log_level assignments. |
| 2554 |
* (If log_level is assigned anywhere after this point, move this check.) */ |
| 2555 |
if (!unit_log_level_test(u, log_level)) |
| 2556 |
return 0; |
| 2557 |
|
| 2558 |
/* Is there any accounting data available at all? */ |
| 2559 |
if (n_iovec == 0) { |
| 2560 |
assert(!message); |
| 2561 |
return 0; |
| 2562 |
} |
| 2563 |
|
| 2564 |
t = strjoin("MESSAGE=", u->id, ": ", message ?: "Completed", "."); |
| 2565 |
if (!t) |
| 2566 |
return log_oom(); |
| 2567 |
iovec[n_iovec++] = IOVEC_MAKE_STRING(TAKE_PTR(t)); |
| 2568 |
|
| 2569 |
if (!set_iovec_string_field(iovec, &n_iovec, "MESSAGE_ID=", SD_MESSAGE_UNIT_RESOURCES_STR)) |
| 2570 |
return log_oom(); |
| 2571 |
|
| 2572 |
if (!set_iovec_string_field(iovec, &n_iovec, unit_log_field(u), u->id)) |
| 2573 |
return log_oom(); |
| 2574 |
|
| 2575 |
if (!set_iovec_string_field(iovec, &n_iovec, unit_invocation_log_field(u), u->invocation_id_string)) |
| 2576 |
return log_oom(); |
| 2577 |
|
| 2578 |
log_unit_struct_iovec(u, log_level, iovec, n_iovec); |
| 2579 |
|
| 2580 |
return 0; |
| 2581 |
} |
| 2582 |
|
| 2583 |
static void unit_update_on_console(Unit *u) { |
| 2584 |
bool b; |
| 2585 |
|
| 2586 |
assert(u); |
| 2587 |
|
| 2588 |
b = unit_needs_console(u); |
| 2589 |
if (u->on_console == b) |
| 2590 |
return; |
| 2591 |
|
| 2592 |
u->on_console = b; |
| 2593 |
if (b) |
| 2594 |
manager_ref_console(u->manager); |
| 2595 |
else |
| 2596 |
manager_unref_console(u->manager); |
| 2597 |
} |
| 2598 |
|
| 2599 |
static void unit_emit_audit_start(Unit *u) { |
| 2600 |
assert(u); |
| 2601 |
|
| 2602 |
if (UNIT_VTABLE(u)->audit_start_message_type <= 0) |
| 2603 |
return; |
| 2604 |
|
| 2605 |
/* Write audit record if we have just finished starting up */ |
| 2606 |
manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_start_message_type, /* success= */ true); |
| 2607 |
u->in_audit = true; |
| 2608 |
} |
| 2609 |
|
| 2610 |
static void unit_emit_audit_stop(Unit *u, UnitActiveState state) { |
| 2611 |
assert(u); |
| 2612 |
|
| 2613 |
if (UNIT_VTABLE(u)->audit_start_message_type <= 0) |
| 2614 |
return; |
| 2615 |
|
| 2616 |
if (u->in_audit) { |
| 2617 |
/* Write audit record if we have just finished shutting down */ |
| 2618 |
manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_stop_message_type, /* success= */ state == UNIT_INACTIVE); |
| 2619 |
u->in_audit = false; |
| 2620 |
} else { |
| 2621 |
/* Hmm, if there was no start record written write it now, so that we always have a nice pair */ |
| 2622 |
manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_start_message_type, /* success= */ state == UNIT_INACTIVE); |
| 2623 |
|
| 2624 |
if (state == UNIT_INACTIVE) |
| 2625 |
manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_stop_message_type, /* success= */ true); |
| 2626 |
} |
| 2627 |
} |
| 2628 |
|
| 2629 |
static bool unit_process_job(Job *j, UnitActiveState ns, bool reload_success) { |
| 2630 |
bool unexpected = false; |
| 2631 |
|
| 2632 |
assert(j); |
| 2633 |
assert(j->installed); |
| 2634 |
|
| 2635 |
if (j->state == JOB_WAITING) |
| 2636 |
/* So we reached a different state for this job. Let's see if we can run it now if it failed previously |
| 2637 |
* due to EAGAIN. */ |
| 2638 |
job_add_to_run_queue(j); |
| 2639 |
|
| 2640 |
/* Let's check whether the unit's new state constitutes a finished job, or maybe contradicts a running job and |
| 2641 |
* hence needs to invalidate jobs. */ |
| 2642 |
|
| 2643 |
switch (j->type) { |
| 2644 |
|
| 2645 |
case JOB_START: |
| 2646 |
case JOB_VERIFY_ACTIVE: |
| 2647 |
|
| 2648 |
if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
| 2649 |
job_finish_and_invalidate(j, JOB_DONE, /* recursive= */ true, /* already= */ false); |
| 2650 |
else if (j->state == JOB_RUNNING && ns != UNIT_ACTIVATING) { |
| 2651 |
unexpected = true; |
| 2652 |
|
| 2653 |
if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
| 2654 |
job_finish_and_invalidate(j, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, /* recursive= */ true, /* already= */ false); |
| 2655 |
} |
| 2656 |
|
| 2657 |
break; |
| 2658 |
|
| 2659 |
case JOB_RELOAD: |
| 2660 |
|
| 2661 |
if (j->state == JOB_RUNNING) { |
| 2662 |
if (ns == UNIT_ACTIVE) |
| 2663 |
job_finish_and_invalidate(j, reload_success ? JOB_DONE : JOB_FAILED, /* recursive= */ true, /* already= */ false); |
| 2664 |
else if (!IN_SET(ns, UNIT_ACTIVATING, UNIT_RELOADING, UNIT_REFRESHING)) { |
| 2665 |
unexpected = true; |
| 2666 |
job_finish_and_invalidate(j, reload_success ? JOB_CANCELED : JOB_FAILED, /* recursive= */ true, /* already= */ false); |
| 2667 |
} |
| 2668 |
} |
| 2669 |
|
| 2670 |
break; |
| 2671 |
|
| 2672 |
case JOB_STOP: |
| 2673 |
case JOB_RESTART: |
| 2674 |
|
| 2675 |
if (UNIT_IS_INACTIVE_OR_FAILED(ns)) |
| 2676 |
job_finish_and_invalidate(j, JOB_DONE, /* recursive= */ true, /* already= */ false); |
| 2677 |
else if (j->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) { |
| 2678 |
unexpected = true; |
| 2679 |
job_finish_and_invalidate(j, JOB_FAILED, /* recursive= */ true, /* already= */ false); |
| 2680 |
} |
| 2681 |
|
| 2682 |
break; |
| 2683 |
|
| 2684 |
default: |
| 2685 |
assert_not_reached(); |
| 2686 |
} |
| 2687 |
|
| 2688 |
return unexpected; |
| 2689 |
} |
| 2690 |
|
| 2691 |
static void unit_recursive_add_to_run_queue(Unit *u) { |
| 2692 |
assert(u); |
| 2693 |
|
| 2694 |
if (u->job) |
| 2695 |
job_add_to_run_queue(u->job); |
| 2696 |
|
| 2697 |
Unit *child; |
| 2698 |
UNIT_FOREACH_DEPENDENCY(child, u, UNIT_ATOM_SLICE_OF) { |
| 2699 |
|
| 2700 |
if (!child->job) |
| 2701 |
continue; |
| 2702 |
|
| 2703 |
unit_recursive_add_to_run_queue(child); |
| 2704 |
} |
| 2705 |
} |
| 2706 |
|
| 2707 |
static void unit_check_concurrency_limit(Unit *u) { |
| 2708 |
assert(u); |
| 2709 |
|
| 2710 |
Unit *slice = UNIT_GET_SLICE(u); |
| 2711 |
if (!slice) |
| 2712 |
return; |
| 2713 |
|
| 2714 |
/* If a unit was stopped, maybe it has pending siblings (or children thereof) that can be started now */ |
| 2715 |
|
| 2716 |
if (SLICE(slice)->concurrency_soft_max != UINT_MAX) { |
| 2717 |
Unit *sibling; |
| 2718 |
UNIT_FOREACH_DEPENDENCY(sibling, slice, UNIT_ATOM_SLICE_OF) { |
| 2719 |
if (sibling == u) |
| 2720 |
continue; |
| 2721 |
|
| 2722 |
unit_recursive_add_to_run_queue(sibling); |
| 2723 |
} |
| 2724 |
} |
| 2725 |
|
| 2726 |
/* Also go up the tree. */ |
| 2727 |
unit_check_concurrency_limit(slice); |
| 2728 |
} |
| 2729 |
|
| 2730 |
void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) { |
| 2731 |
assert(u); |
| 2732 |
assert(os < _UNIT_ACTIVE_STATE_MAX); |
| 2733 |
assert(ns < _UNIT_ACTIVE_STATE_MAX); |
| 2734 |
|
| 2735 |
/* Note that this is called for all low-level state changes, even if they might map to the same high-level |
| 2736 |
* UnitActiveState! That means that ns == os is an expected behavior here. For example: if a mount point is |
| 2737 |
* remounted this function will be called too! */ |
| 2738 |
|
| 2739 |
Manager *m = ASSERT_PTR(u->manager); |
| 2740 |
|
| 2741 |
/* Let's enqueue the change signal early. In case this unit has a job associated we want that this unit is in |
| 2742 |
* the bus queue, so that any job change signal queued will force out the unit change signal first. */ |
| 2743 |
unit_add_to_dbus_queue(u); |
| 2744 |
|
| 2745 |
/* Update systemd-oomd on the property/state change. |
| 2746 |
* |
| 2747 |
* Always send an update if the unit is going into an inactive state so systemd-oomd knows to |
| 2748 |
* stop monitoring. |
| 2749 |
* Also send an update whenever the unit goes active; this is to handle a case where an override file |
| 2750 |
* sets one of the ManagedOOM*= properties to "kill", then later removes it. systemd-oomd needs to |
| 2751 |
* know to stop monitoring when the unit changes from "kill" -> "auto" on daemon-reload, but we don't |
| 2752 |
* have the information on the property. Thus, indiscriminately send an update. */ |
| 2753 |
if (os != ns && (UNIT_IS_INACTIVE_OR_FAILED(ns) || UNIT_IS_ACTIVE_OR_RELOADING(ns))) |
| 2754 |
(void) manager_varlink_send_managed_oom_update(u); |
| 2755 |
|
| 2756 |
/* Update timestamps for state changes */ |
| 2757 |
if (!MANAGER_IS_RELOADING(m)) { |
| 2758 |
dual_timestamp_now(&u->state_change_timestamp); |
| 2759 |
|
| 2760 |
if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns)) |
| 2761 |
u->inactive_exit_timestamp = u->state_change_timestamp; |
| 2762 |
else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns)) |
| 2763 |
u->inactive_enter_timestamp = u->state_change_timestamp; |
| 2764 |
|
| 2765 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
| 2766 |
u->active_enter_timestamp = u->state_change_timestamp; |
| 2767 |
else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) |
| 2768 |
u->active_exit_timestamp = u->state_change_timestamp; |
| 2769 |
} |
| 2770 |
|
| 2771 |
/* Keep track of failed units */ |
| 2772 |
(void) manager_update_failed_units(m, u, ns == UNIT_FAILED); |
| 2773 |
|
| 2774 |
/* Make sure the cgroup and state files are always removed when we become inactive */ |
| 2775 |
if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
| 2776 |
SET_FLAG(u->markers, |
| 2777 |
(1u << UNIT_MARKER_NEEDS_RELOAD)|(1u << UNIT_MARKER_NEEDS_RESTART)|(1u << UNIT_MARKER_NEEDS_STOP), |
| 2778 |
false); |
| 2779 |
unit_prune_cgroup(u); |
| 2780 |
unit_unlink_state_files(u); |
| 2781 |
} else if (UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) |
| 2782 |
SET_FLAG(u->markers, 1u << UNIT_MARKER_NEEDS_START, false); |
| 2783 |
else if (ns != os && ns == UNIT_RELOADING) |
| 2784 |
SET_FLAG(u->markers, 1u << UNIT_MARKER_NEEDS_RELOAD, false); |
| 2785 |
|
| 2786 |
unit_update_on_console(u); |
| 2787 |
|
| 2788 |
if (!MANAGER_IS_RELOADING(m)) { |
| 2789 |
bool unexpected; |
| 2790 |
|
| 2791 |
/* Let's propagate state changes to the job */ |
| 2792 |
if (u->job) |
| 2793 |
unexpected = unit_process_job(u->job, ns, reload_success); |
| 2794 |
else |
| 2795 |
unexpected = true; |
| 2796 |
|
| 2797 |
/* If this state change happened without being requested by a job, then let's retroactively start or |
| 2798 |
* stop dependencies. We skip that step when deserializing, since we don't want to create any |
| 2799 |
* additional jobs just because something is already activated. */ |
| 2800 |
|
| 2801 |
if (unexpected) { |
| 2802 |
if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns)) |
| 2803 |
retroactively_start_dependencies(u); |
| 2804 |
else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) |
| 2805 |
retroactively_stop_dependencies(u); |
| 2806 |
} |
| 2807 |
|
| 2808 |
if (UNIT_IS_ACTIVE_OR_RELOADING(ns) && !UNIT_IS_ACTIVE_OR_RELOADING(os)) { |
| 2809 |
/* This unit just finished starting up */ |
| 2810 |
|
| 2811 |
unit_emit_audit_start(u); |
| 2812 |
manager_send_unit_plymouth(m, u); |
| 2813 |
manager_send_unit_supervisor(m, u, /* active= */ true); |
| 2814 |
|
| 2815 |
} else if (UNIT_IS_INACTIVE_OR_FAILED(ns) && !UNIT_IS_INACTIVE_OR_FAILED(os)) { |
| 2816 |
/* This unit just stopped/failed. */ |
| 2817 |
|
| 2818 |
unit_emit_audit_stop(u, ns); |
| 2819 |
manager_send_unit_supervisor(m, u, /* active= */ false); |
| 2820 |
unit_log_resources(u); |
| 2821 |
} |
| 2822 |
|
| 2823 |
if (ns == UNIT_INACTIVE && !IN_SET(os, UNIT_FAILED, UNIT_INACTIVE, UNIT_MAINTENANCE)) |
| 2824 |
unit_start_on_termination_deps(u, UNIT_ATOM_ON_SUCCESS); |
| 2825 |
else if (ns != os && ns == UNIT_FAILED) |
| 2826 |
unit_start_on_termination_deps(u, UNIT_ATOM_ON_FAILURE); |
| 2827 |
} |
| 2828 |
|
| 2829 |
manager_recheck_journal(m); |
| 2830 |
manager_recheck_dbus(m); |
| 2831 |
|
| 2832 |
unit_trigger_notify(u); |
| 2833 |
|
| 2834 |
if (!MANAGER_IS_RELOADING(m)) { |
| 2835 |
const char *reason; |
| 2836 |
|
| 2837 |
if (os != UNIT_FAILED && ns == UNIT_FAILED) { |
| 2838 |
reason = strjoina("unit ", u->id, " failed"); |
| 2839 |
emergency_action(m, u->failure_action, EMERGENCY_ACTION_WARN|EMERGENCY_ACTION_SLEEP_5S, u->reboot_arg, unit_failure_action_exit_status(u), reason); |
| 2840 |
} else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && ns == UNIT_INACTIVE) { |
| 2841 |
reason = strjoina("unit ", u->id, " succeeded"); |
| 2842 |
emergency_action(m, u->success_action, /* flags= */ 0, u->reboot_arg, unit_success_action_exit_status(u), reason); |
| 2843 |
} |
| 2844 |
} |
| 2845 |
|
| 2846 |
/* And now, add the unit or depending units to various queues that will act on the new situation if |
| 2847 |
* needed. These queues generally check for continuous state changes rather than events (like most of |
| 2848 |
* the state propagation above), and do work deferred instead of instantly, since they typically |
| 2849 |
* don't want to run during reloading, and usually involve checking combined state of multiple units |
| 2850 |
* at once. */ |
| 2851 |
|
| 2852 |
if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { |
| 2853 |
/* Stop unneeded units and bound-by units regardless if going down was expected or not */ |
| 2854 |
check_unneeded_dependencies(u); |
| 2855 |
check_bound_by_dependencies(u); |
| 2856 |
|
| 2857 |
/* Maybe someone wants us to remain up? */ |
| 2858 |
unit_submit_to_start_when_upheld_queue(u); |
| 2859 |
|
| 2860 |
/* Maybe the unit should be GC'ed now? */ |
| 2861 |
unit_add_to_gc_queue(u); |
| 2862 |
|
| 2863 |
/* Maybe we can release some resources now? */ |
| 2864 |
unit_submit_to_release_resources_queue(u); |
| 2865 |
|
| 2866 |
/* Maybe the concurrency limits now allow dispatching of another start job in this slice? */ |
| 2867 |
unit_check_concurrency_limit(u); |
| 2868 |
|
| 2869 |
/* Maybe someone else has been waiting for us to stop? */ |
| 2870 |
m->may_dispatch_stop_notify_queue = true; |
| 2871 |
|
| 2872 |
} else if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) { |
| 2873 |
/* Start uphold units regardless if going up was expected or not */ |
| 2874 |
check_uphold_dependencies(u); |
| 2875 |
|
| 2876 |
/* Maybe we finished startup and are now ready for being stopped because unneeded? */ |
| 2877 |
unit_submit_to_stop_when_unneeded_queue(u); |
| 2878 |
|
| 2879 |
/* Maybe we finished startup, but something we needed has vanished? Let's die then. (This happens |
| 2880 |
* when something BindsTo= to a Type=oneshot unit, as these units go directly from starting to |
| 2881 |
* inactive, without ever entering started.) */ |
| 2882 |
unit_submit_to_stop_when_bound_queue(u); |
| 2883 |
} |
| 2884 |
} |
| 2885 |
|
| 2886 |
int unit_watch_pidref(Unit *u, const PidRef *pid, bool exclusive) { |
| 2887 |
_cleanup_(pidref_freep) PidRef *pid_dup = NULL; |
| 2888 |
int r; |
| 2889 |
|
| 2890 |
/* Adds a specific PID to the set of PIDs this unit watches. */ |
| 2891 |
|
| 2892 |
assert(u); |
| 2893 |
assert(pidref_is_set(pid)); |
| 2894 |
|
| 2895 |
/* Caller might be sure that this PID belongs to this unit only. Let's take this |
| 2896 |
* opportunity to remove any stalled references to this PID as they can be created |
| 2897 |
* easily (when watching a process which is not our direct child). */ |
| 2898 |
if (exclusive) |
| 2899 |
manager_unwatch_pidref(u->manager, pid); |
| 2900 |
|
| 2901 |
if (set_contains(u->pids, pid)) { /* early exit if already being watched */ |
| 2902 |
assert(!exclusive); |
| 2903 |
return 0; |
| 2904 |
} |
| 2905 |
|
| 2906 |
r = pidref_dup(pid, &pid_dup); |
| 2907 |
if (r < 0) |
| 2908 |
return r; |
| 2909 |
|
| 2910 |
/* First, insert into the set of PIDs maintained by the unit */ |
| 2911 |
r = set_ensure_put(&u->pids, &pidref_hash_ops_free, pid_dup); |
| 2912 |
if (r < 0) |
| 2913 |
return r; |
| 2914 |
|
| 2915 |
pid = TAKE_PTR(pid_dup); /* continue with our copy now that we have installed it properly in our set */ |
| 2916 |
|
| 2917 |
/* Second, insert it into the simple global table, see if that works */ |
| 2918 |
r = hashmap_ensure_put(&u->manager->watch_pids, &pidref_hash_ops, pid, u); |
| 2919 |
if (r != -EEXIST) |
| 2920 |
return r; |
| 2921 |
|
| 2922 |
/* OK, the key is already assigned to a different unit. That's fine, then add us via the second |
| 2923 |
* hashmap that points to an array. */ |
| 2924 |
|
| 2925 |
PidRef *old_pid = NULL; |
| 2926 |
Unit **array = hashmap_get2(u->manager->watch_pids_more, pid, (void**) &old_pid); |
| 2927 |
|
| 2928 |
/* Count entries in array */ |
| 2929 |
size_t n = 0; |
| 2930 |
for (; array && array[n]; n++) |
| 2931 |
; |
| 2932 |
|
| 2933 |
/* Allocate a new array */ |
| 2934 |
_cleanup_free_ Unit **new_array = new(Unit*, n + 2); |
| 2935 |
if (!new_array) |
| 2936 |
return -ENOMEM; |
| 2937 |
|
| 2938 |
/* Append us to the end */ |
| 2939 |
memcpy_safe(new_array, array, sizeof(Unit*) * n); |
| 2940 |
new_array[n] = u; |
| 2941 |
new_array[n+1] = NULL; |
| 2942 |
|
| 2943 |
/* Add or replace the old array */ |
| 2944 |
r = hashmap_ensure_replace(&u->manager->watch_pids_more, &pidref_hash_ops, old_pid ?: pid, new_array); |
| 2945 |
if (r < 0) |
| 2946 |
return r; |
| 2947 |
|
| 2948 |
TAKE_PTR(new_array); /* Now part of the hash table */ |
| 2949 |
free(array); /* Which means we can now delete the old version */ |
| 2950 |
return 0; |
| 2951 |
} |
| 2952 |
|
| 2953 |
void unit_unwatch_pidref(Unit *u, const PidRef *pid) { |
| 2954 |
assert(u); |
| 2955 |
assert(pidref_is_set(pid)); |
| 2956 |
|
| 2957 |
/* Remove from the set we maintain for this unit. (And destroy the returned pid eventually) */ |
| 2958 |
_cleanup_(pidref_freep) PidRef *pid1 = set_remove(u->pids, pid); |
| 2959 |
if (!pid1) |
| 2960 |
return; /* Early exit if this PID was never watched by us */ |
| 2961 |
|
| 2962 |
/* First let's drop the unit from the simple hash table, if it is included there */ |
| 2963 |
PidRef *pid2 = NULL; |
| 2964 |
Unit *uu = hashmap_get2(u->manager->watch_pids, pid, (void**) &pid2); |
| 2965 |
|
| 2966 |
/* Quick validation: iff we are in the watch_pids table then the PidRef object must be the same as in our local pids set */ |
| 2967 |
assert((uu == u) == (pid1 == pid2)); |
| 2968 |
|
| 2969 |
if (uu == u) |
| 2970 |
/* OK, we are in the first table. Let's remove it there then, and we are done already. */ |
| 2971 |
assert_se(hashmap_remove_value(u->manager->watch_pids, pid2, uu)); |
| 2972 |
else { |
| 2973 |
/* We weren't in the first table, then let's consult the 2nd table that points to an array */ |
| 2974 |
PidRef *pid3 = NULL; |
| 2975 |
Unit **array = hashmap_get2(u->manager->watch_pids_more, pid, (void**) &pid3); |
| 2976 |
|
| 2977 |
/* Let's iterate through the array, dropping our own entry */ |
| 2978 |
size_t m = 0, n = 0; |
| 2979 |
for (; array && array[n]; n++) |
| 2980 |
if (array[n] != u) |
| 2981 |
array[m++] = array[n]; |
| 2982 |
if (n == m) |
| 2983 |
return; /* Not there */ |
| 2984 |
|
| 2985 |
array[m] = NULL; /* set trailing NULL marker on the new end */ |
| 2986 |
|
| 2987 |
if (m == 0) { |
| 2988 |
/* The array is now empty, remove the entire entry */ |
| 2989 |
assert_se(hashmap_remove_value(u->manager->watch_pids_more, pid3, array)); |
| 2990 |
free(array); |
| 2991 |
} else { |
| 2992 |
/* The array is not empty, but let's make sure the entry is not keyed by the PidRef |
| 2993 |
* we will delete, but by the PidRef object of the Unit that is now first in the |
| 2994 |
* array. */ |
| 2995 |
|
| 2996 |
PidRef *new_pid3 = ASSERT_PTR(set_get(array[0]->pids, pid)); |
| 2997 |
assert_se(hashmap_replace(u->manager->watch_pids_more, new_pid3, array) >= 0); |
| 2998 |
} |
| 2999 |
} |
| 3000 |
} |
| 3001 |
|
| 3002 |
void unit_unwatch_all_pids(Unit *u) { |
| 3003 |
assert(u); |
| 3004 |
|
| 3005 |
while (!set_isempty(u->pids)) |
| 3006 |
unit_unwatch_pidref(u, set_first(u->pids)); |
| 3007 |
|
| 3008 |
u->pids = set_free(u->pids); |
| 3009 |
} |
| 3010 |
|
| 3011 |
void unit_unwatch_pidref_done(Unit *u, PidRef *pidref) { |
| 3012 |
assert(u); |
| 3013 |
|
| 3014 |
if (!pidref_is_set(pidref)) |
| 3015 |
return; |
| 3016 |
|
| 3017 |
unit_unwatch_pidref(u, pidref); |
| 3018 |
pidref_done(pidref); |
| 3019 |
} |
| 3020 |
|
| 3021 |
bool unit_job_is_applicable(Unit *u, JobType j) { |
| 3022 |
assert(u); |
| 3023 |
assert(j >= 0 && j < _JOB_TYPE_MAX); |
| 3024 |
|
| 3025 |
switch (j) { |
| 3026 |
|
| 3027 |
case JOB_VERIFY_ACTIVE: |
| 3028 |
case JOB_START: |
| 3029 |
case JOB_NOP: |
| 3030 |
/* Note that we don't check unit_can_start() here. That's because .device units and suchlike are not |
| 3031 |
* startable by us but may appear due to external events, and it thus makes sense to permit enqueuing |
| 3032 |
* jobs for it. */ |
| 3033 |
return true; |
| 3034 |
|
| 3035 |
case JOB_STOP: |
| 3036 |
/* Similar as above. However, perpetual units can never be stopped (neither explicitly nor due to |
| 3037 |
* external events), hence it makes no sense to permit enqueuing such a request either. */ |
| 3038 |
return !u->perpetual; |
| 3039 |
|
| 3040 |
case JOB_RESTART: |
| 3041 |
case JOB_TRY_RESTART: |
| 3042 |
return unit_can_stop(u) && unit_can_start(u); |
| 3043 |
|
| 3044 |
case JOB_RELOAD: |
| 3045 |
case JOB_TRY_RELOAD: |
| 3046 |
return unit_can_reload(u); |
| 3047 |
|
| 3048 |
case JOB_RELOAD_OR_START: |
| 3049 |
return unit_can_reload(u) && unit_can_start(u); |
| 3050 |
|
| 3051 |
default: |
| 3052 |
assert_not_reached(); |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
static Hashmap *unit_get_dependency_hashmap_per_type(Unit *u, UnitDependency d) { |
| 3057 |
Hashmap *deps; |
| 3058 |
|
| 3059 |
assert(u); |
| 3060 |
assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); |
| 3061 |
|
| 3062 |
deps = hashmap_get(u->dependencies, UNIT_DEPENDENCY_TO_PTR(d)); |
| 3063 |
if (!deps) { |
| 3064 |
_cleanup_hashmap_free_ Hashmap *h = NULL; |
| 3065 |
|
| 3066 |
h = hashmap_new(NULL); |
| 3067 |
if (!h) |
| 3068 |
return NULL; |
| 3069 |
|
| 3070 |
if (hashmap_ensure_put(&u->dependencies, NULL, UNIT_DEPENDENCY_TO_PTR(d), h) < 0) |
| 3071 |
return NULL; |
| 3072 |
|
| 3073 |
deps = TAKE_PTR(h); |
| 3074 |
} |
| 3075 |
|
| 3076 |
return deps; |
| 3077 |
} |
| 3078 |
|
| 3079 |
typedef enum NotifyDependencyFlags { |
| 3080 |
NOTIFY_DEPENDENCY_UPDATE_FROM = 1 << 0, |
| 3081 |
NOTIFY_DEPENDENCY_UPDATE_TO = 1 << 1, |
| 3082 |
} NotifyDependencyFlags; |
| 3083 |
|
| 3084 |
static int unit_add_dependency_impl( |
| 3085 |
Unit *u, |
| 3086 |
UnitDependency d, |
| 3087 |
Unit *other, |
| 3088 |
UnitDependencyMask mask) { |
| 3089 |
|
| 3090 |
static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = { |
| 3091 |
[UNIT_REQUIRES] = UNIT_REQUIRED_BY, |
| 3092 |
[UNIT_REQUISITE] = UNIT_REQUISITE_OF, |
| 3093 |
[UNIT_WANTS] = UNIT_WANTED_BY, |
| 3094 |
[UNIT_BINDS_TO] = UNIT_BOUND_BY, |
| 3095 |
[UNIT_PART_OF] = UNIT_CONSISTS_OF, |
| 3096 |
[UNIT_UPHOLDS] = UNIT_UPHELD_BY, |
| 3097 |
[UNIT_REQUIRED_BY] = UNIT_REQUIRES, |
| 3098 |
[UNIT_REQUISITE_OF] = UNIT_REQUISITE, |
| 3099 |
[UNIT_WANTED_BY] = UNIT_WANTS, |
| 3100 |
[UNIT_BOUND_BY] = UNIT_BINDS_TO, |
| 3101 |
[UNIT_CONSISTS_OF] = UNIT_PART_OF, |
| 3102 |
[UNIT_UPHELD_BY] = UNIT_UPHOLDS, |
| 3103 |
[UNIT_CONFLICTS] = UNIT_CONFLICTED_BY, |
| 3104 |
[UNIT_CONFLICTED_BY] = UNIT_CONFLICTS, |
| 3105 |
[UNIT_BEFORE] = UNIT_AFTER, |
| 3106 |
[UNIT_AFTER] = UNIT_BEFORE, |
| 3107 |
[UNIT_ON_SUCCESS] = UNIT_ON_SUCCESS_OF, |
| 3108 |
[UNIT_ON_SUCCESS_OF] = UNIT_ON_SUCCESS, |
| 3109 |
[UNIT_ON_FAILURE] = UNIT_ON_FAILURE_OF, |
| 3110 |
[UNIT_ON_FAILURE_OF] = UNIT_ON_FAILURE, |
| 3111 |
[UNIT_TRIGGERS] = UNIT_TRIGGERED_BY, |
| 3112 |
[UNIT_TRIGGERED_BY] = UNIT_TRIGGERS, |
| 3113 |
[UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM, |
| 3114 |
[UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO, |
| 3115 |
[UNIT_PROPAGATES_STOP_TO] = UNIT_STOP_PROPAGATED_FROM, |
| 3116 |
[UNIT_STOP_PROPAGATED_FROM] = UNIT_PROPAGATES_STOP_TO, |
| 3117 |
[UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, /* symmetric! 👓 */ |
| 3118 |
[UNIT_REFERENCES] = UNIT_REFERENCED_BY, |
| 3119 |
[UNIT_REFERENCED_BY] = UNIT_REFERENCES, |
| 3120 |
[UNIT_IN_SLICE] = UNIT_SLICE_OF, |
| 3121 |
[UNIT_SLICE_OF] = UNIT_IN_SLICE, |
| 3122 |
}; |
| 3123 |
|
| 3124 |
Hashmap *u_deps, *other_deps; |
| 3125 |
UnitDependencyInfo u_info, u_info_old, other_info, other_info_old; |
| 3126 |
NotifyDependencyFlags flags = 0; |
| 3127 |
int r; |
| 3128 |
|
| 3129 |
assert(u); |
| 3130 |
assert(other); |
| 3131 |
assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); |
| 3132 |
assert(inverse_table[d] >= 0 && inverse_table[d] < _UNIT_DEPENDENCY_MAX); |
| 3133 |
assert(mask > 0 && mask < _UNIT_DEPENDENCY_MASK_FULL); |
| 3134 |
|
| 3135 |
/* Ensure the following two hashmaps for each unit exist: |
| 3136 |
* - the top-level dependency hashmap that maps UnitDependency → Hashmap(Unit* → UnitDependencyInfo), |
| 3137 |
* - the inner hashmap, that maps Unit* → UnitDependencyInfo, for the specified dependency type. */ |
| 3138 |
u_deps = unit_get_dependency_hashmap_per_type(u, d); |
| 3139 |
if (!u_deps) |
| 3140 |
return -ENOMEM; |
| 3141 |
|
| 3142 |
other_deps = unit_get_dependency_hashmap_per_type(other, inverse_table[d]); |
| 3143 |
if (!other_deps) |
| 3144 |
return -ENOMEM; |
| 3145 |
|
| 3146 |
/* Save the original dependency info. */ |
| 3147 |
u_info.data = u_info_old.data = hashmap_get(u_deps, other); |
| 3148 |
other_info.data = other_info_old.data = hashmap_get(other_deps, u); |
| 3149 |
|
| 3150 |
/* Update dependency info. */ |
| 3151 |
u_info.origin_mask |= mask; |
| 3152 |
other_info.destination_mask |= mask; |
| 3153 |
|
| 3154 |
/* Save updated dependency info. */ |
| 3155 |
if (u_info.data != u_info_old.data) { |
| 3156 |
r = hashmap_replace(u_deps, other, u_info.data); |
| 3157 |
if (r < 0) |
| 3158 |
return r; |
| 3159 |
|
| 3160 |
flags = NOTIFY_DEPENDENCY_UPDATE_FROM; |
| 3161 |
u->dependency_generation++; |
| 3162 |
} |
| 3163 |
|
| 3164 |
if (other_info.data != other_info_old.data) { |
| 3165 |
r = hashmap_replace(other_deps, u, other_info.data); |
| 3166 |
if (r < 0) { |
| 3167 |
if (u_info.data != u_info_old.data) { |
| 3168 |
/* Restore the old dependency. */ |
| 3169 |
if (u_info_old.data) |
| 3170 |
(void) hashmap_update(u_deps, other, u_info_old.data); |
| 3171 |
else |
| 3172 |
hashmap_remove(u_deps, other); |
| 3173 |
} |
| 3174 |
return r; |
| 3175 |
} |
| 3176 |
|
| 3177 |
flags |= NOTIFY_DEPENDENCY_UPDATE_TO; |
| 3178 |
other->dependency_generation++; |
| 3179 |
} |
| 3180 |
|
| 3181 |
return flags; |
| 3182 |
} |
| 3183 |
|
| 3184 |
int unit_add_dependency( |
| 3185 |
Unit *u, |
| 3186 |
UnitDependency d, |
| 3187 |
Unit *other, |
| 3188 |
bool add_reference, |
| 3189 |
UnitDependencyMask mask) { |
| 3190 |
|
| 3191 |
UnitDependencyAtom a; |
| 3192 |
int r; |
| 3193 |
|
| 3194 |
/* Helper to know whether sending a notification is necessary or not: if the dependency is already |
| 3195 |
* there, no need to notify! */ |
| 3196 |
NotifyDependencyFlags notify_flags; |
| 3197 |
|
| 3198 |
assert(u); |
| 3199 |
assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX); |
| 3200 |
assert(other); |
| 3201 |
|
| 3202 |
u = unit_follow_merge(u); |
| 3203 |
other = unit_follow_merge(other); |
| 3204 |
a = unit_dependency_to_atom(d); |
| 3205 |
assert(a >= 0); |
| 3206 |
|
| 3207 |
/* We won't allow dependencies on ourselves. We will not consider them an error however. */ |
| 3208 |
if (u == other) { |
| 3209 |
if (unit_should_warn_about_dependency(d)) |
| 3210 |
log_unit_warning(u, "Dependency %s=%s is dropped.", |
| 3211 |
unit_dependency_to_string(d), u->id); |
| 3212 |
return 0; |
| 3213 |
} |
| 3214 |
|
| 3215 |
if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES)) |
| 3216 |
return 0; |
| 3217 |
|
| 3218 |
/* Note that ordering a device unit after a unit is permitted since it allows its job running |
| 3219 |
* timeout to be started at a specific time. */ |
| 3220 |
if (FLAGS_SET(a, UNIT_ATOM_BEFORE) && other->type == UNIT_DEVICE) { |
| 3221 |
log_unit_warning(u, "Dependency Before=%s ignored (.device units cannot be delayed)", other->id); |
| 3222 |
return 0; |
| 3223 |
} |
| 3224 |
|
| 3225 |
if (FLAGS_SET(a, UNIT_ATOM_ON_FAILURE) && !UNIT_VTABLE(u)->can_fail) { |
| 3226 |
log_unit_warning(u, "Requested dependency OnFailure=%s ignored (%s units cannot fail).", other->id, unit_type_to_string(u->type)); |
| 3227 |
return 0; |
| 3228 |
} |
| 3229 |
|
| 3230 |
if (FLAGS_SET(a, UNIT_ATOM_TRIGGERS) && !UNIT_VTABLE(u)->can_trigger) |
| 3231 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 3232 |
"Requested dependency Triggers=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(u->type)); |
| 3233 |
if (FLAGS_SET(a, UNIT_ATOM_TRIGGERED_BY) && !UNIT_VTABLE(other)->can_trigger) |
| 3234 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 3235 |
"Requested dependency TriggeredBy=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(other->type)); |
| 3236 |
|
| 3237 |
if (FLAGS_SET(a, UNIT_ATOM_IN_SLICE) && other->type != UNIT_SLICE) |
| 3238 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 3239 |
"Requested dependency Slice=%s refused (%s is not a slice unit).", other->id, other->id); |
| 3240 |
if (FLAGS_SET(a, UNIT_ATOM_SLICE_OF) && u->type != UNIT_SLICE) |
| 3241 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 3242 |
"Requested dependency SliceOf=%s refused (%s is not a slice unit).", other->id, u->id); |
| 3243 |
|
| 3244 |
if (FLAGS_SET(a, UNIT_ATOM_IN_SLICE) && !UNIT_HAS_CGROUP_CONTEXT(u)) |
| 3245 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 3246 |
"Requested dependency Slice=%s refused (%s is not a cgroup unit).", other->id, u->id); |
| 3247 |
|
| 3248 |
if (FLAGS_SET(a, UNIT_ATOM_SLICE_OF) && !UNIT_HAS_CGROUP_CONTEXT(other)) |
| 3249 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL), |
| 3250 |
"Requested dependency SliceOf=%s refused (%s is not a cgroup unit).", other->id, other->id); |
| 3251 |
|
| 3252 |
r = unit_add_dependency_impl(u, d, other, mask); |
| 3253 |
if (r < 0) |
| 3254 |
return r; |
| 3255 |
notify_flags = r; |
| 3256 |
|
| 3257 |
if (add_reference) { |
| 3258 |
r = unit_add_dependency_impl(u, UNIT_REFERENCES, other, mask); |
| 3259 |
if (r < 0) |
| 3260 |
return r; |
| 3261 |
notify_flags |= r; |
| 3262 |
} |
| 3263 |
|
| 3264 |
if (FLAGS_SET(notify_flags, NOTIFY_DEPENDENCY_UPDATE_FROM)) |
| 3265 |
unit_add_to_dbus_queue(u); |
| 3266 |
if (FLAGS_SET(notify_flags, NOTIFY_DEPENDENCY_UPDATE_TO)) |
| 3267 |
unit_add_to_dbus_queue(other); |
| 3268 |
|
| 3269 |
return notify_flags != 0; |
| 3270 |
} |
| 3271 |
|
| 3272 |
int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask) { |
| 3273 |
int r = 0, s = 0; |
| 3274 |
|
| 3275 |
assert(u); |
| 3276 |
assert(d >= 0 || e >= 0); |
| 3277 |
|
| 3278 |
if (d >= 0) { |
| 3279 |
r = unit_add_dependency(u, d, other, add_reference, mask); |
| 3280 |
if (r < 0) |
| 3281 |
return r; |
| 3282 |
} |
| 3283 |
|
| 3284 |
if (e >= 0) { |
| 3285 |
s = unit_add_dependency(u, e, other, add_reference, mask); |
| 3286 |
if (s < 0) |
| 3287 |
return s; |
| 3288 |
} |
| 3289 |
|
| 3290 |
return r > 0 || s > 0; |
| 3291 |
} |
| 3292 |
|
| 3293 |
static int resolve_template(Unit *u, const char *name, char **buf, const char **ret) { |
| 3294 |
int r; |
| 3295 |
|
| 3296 |
assert(u); |
| 3297 |
assert(name); |
| 3298 |
assert(buf); |
| 3299 |
assert(ret); |
| 3300 |
|
| 3301 |
if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) { |
| 3302 |
*buf = NULL; |
| 3303 |
*ret = name; |
| 3304 |
return 0; |
| 3305 |
} |
| 3306 |
|
| 3307 |
if (u->instance) |
| 3308 |
r = unit_name_replace_instance(name, u->instance, buf); |
| 3309 |
else { |
| 3310 |
_cleanup_free_ char *i = NULL; |
| 3311 |
|
| 3312 |
r = unit_name_to_prefix(u->id, &i); |
| 3313 |
if (r < 0) |
| 3314 |
return r; |
| 3315 |
|
| 3316 |
r = unit_name_replace_instance(name, i, buf); |
| 3317 |
} |
| 3318 |
if (r < 0) |
| 3319 |
return r; |
| 3320 |
|
| 3321 |
*ret = *buf; |
| 3322 |
return 0; |
| 3323 |
} |
| 3324 |
|
| 3325 |
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, bool add_reference, UnitDependencyMask mask) { |
| 3326 |
_cleanup_free_ char *buf = NULL; |
| 3327 |
Unit *other; |
| 3328 |
int r; |
| 3329 |
|
| 3330 |
assert(u); |
| 3331 |
assert(name); |
| 3332 |
|
| 3333 |
r = resolve_template(u, name, &buf, &name); |
| 3334 |
if (r < 0) |
| 3335 |
return r; |
| 3336 |
|
| 3337 |
if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES)) |
| 3338 |
return 0; |
| 3339 |
|
| 3340 |
r = manager_load_unit(u->manager, name, NULL, NULL, &other); |
| 3341 |
if (r < 0) |
| 3342 |
return r; |
| 3343 |
|
| 3344 |
return unit_add_dependency(u, d, other, add_reference, mask); |
| 3345 |
} |
| 3346 |
|
| 3347 |
int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, bool add_reference, UnitDependencyMask mask) { |
| 3348 |
_cleanup_free_ char *buf = NULL; |
| 3349 |
Unit *other; |
| 3350 |
int r; |
| 3351 |
|
| 3352 |
assert(u); |
| 3353 |
assert(name); |
| 3354 |
|
| 3355 |
r = resolve_template(u, name, &buf, &name); |
| 3356 |
if (r < 0) |
| 3357 |
return r; |
| 3358 |
|
| 3359 |
if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES)) |
| 3360 |
return 0; |
| 3361 |
|
| 3362 |
r = manager_load_unit(u->manager, name, NULL, NULL, &other); |
| 3363 |
if (r < 0) |
| 3364 |
return r; |
| 3365 |
|
| 3366 |
return unit_add_two_dependencies(u, d, e, other, add_reference, mask); |
| 3367 |
} |
| 3368 |
|
| 3369 |
int setenv_unit_path(const char *p) { |
| 3370 |
assert(p); |
| 3371 |
|
| 3372 |
/* This is mostly for debug purposes */ |
| 3373 |
return RET_NERRNO(setenv("SYSTEMD_UNIT_PATH", p, /* overwrite= */ true)); |
| 3374 |
} |
| 3375 |
|
| 3376 |
char* unit_dbus_path(Unit *u) { |
| 3377 |
assert(u); |
| 3378 |
|
| 3379 |
if (!u->id) |
| 3380 |
return NULL; |
| 3381 |
|
| 3382 |
return unit_dbus_path_from_name(u->id); |
| 3383 |
} |
| 3384 |
|
| 3385 |
char* unit_dbus_path_invocation_id(Unit *u) { |
| 3386 |
assert(u); |
| 3387 |
|
| 3388 |
if (sd_id128_is_null(u->invocation_id)) |
| 3389 |
return NULL; |
| 3390 |
|
| 3391 |
return unit_dbus_path_from_name(u->invocation_id_string); |
| 3392 |
} |
| 3393 |
|
| 3394 |
int unit_set_invocation_id(Unit *u, sd_id128_t id) { |
| 3395 |
int r; |
| 3396 |
|
| 3397 |
assert(u); |
| 3398 |
|
| 3399 |
/* Set the invocation ID for this unit. If we cannot, this will not roll back, but reset the whole thing. */ |
| 3400 |
|
| 3401 |
if (sd_id128_equal(u->invocation_id, id)) |
| 3402 |
return 0; |
| 3403 |
|
| 3404 |
if (!sd_id128_is_null(u->invocation_id)) |
| 3405 |
(void) hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u); |
| 3406 |
|
| 3407 |
if (sd_id128_is_null(id)) { |
| 3408 |
r = 0; |
| 3409 |
goto reset; |
| 3410 |
} |
| 3411 |
|
| 3412 |
r = hashmap_ensure_allocated(&u->manager->units_by_invocation_id, &id128_hash_ops); |
| 3413 |
if (r < 0) |
| 3414 |
goto reset; |
| 3415 |
|
| 3416 |
u->invocation_id = id; |
| 3417 |
sd_id128_to_string(id, u->invocation_id_string); |
| 3418 |
|
| 3419 |
r = hashmap_put(u->manager->units_by_invocation_id, &u->invocation_id, u); |
| 3420 |
if (r < 0) |
| 3421 |
goto reset; |
| 3422 |
|
| 3423 |
return 0; |
| 3424 |
|
| 3425 |
reset: |
| 3426 |
u->invocation_id = SD_ID128_NULL; |
| 3427 |
u->invocation_id_string[0] = 0; |
| 3428 |
return r; |
| 3429 |
} |
| 3430 |
|
| 3431 |
int unit_set_slice(Unit *u, Unit *slice) { |
| 3432 |
int r; |
| 3433 |
|
| 3434 |
assert(u); |
| 3435 |
assert(slice); |
| 3436 |
|
| 3437 |
/* Sets the unit slice if it has not been set before. Is extra careful, to only allow this for units |
| 3438 |
* that actually have a cgroup context. Also, we don't allow to set this for slices (since the parent |
| 3439 |
* slice is derived from the name). Make sure the unit we set is actually a slice. */ |
| 3440 |
|
| 3441 |
if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
| 3442 |
return -EOPNOTSUPP; |
| 3443 |
|
| 3444 |
if (u->type == UNIT_SLICE) |
| 3445 |
return -EINVAL; |
| 3446 |
|
| 3447 |
if (unit_active_state(u) != UNIT_INACTIVE) |
| 3448 |
return -EBUSY; |
| 3449 |
|
| 3450 |
if (slice->type != UNIT_SLICE) |
| 3451 |
return -EINVAL; |
| 3452 |
|
| 3453 |
if (unit_has_name(u, SPECIAL_INIT_SCOPE) && |
| 3454 |
!unit_has_name(slice, SPECIAL_ROOT_SLICE)) |
| 3455 |
return -EPERM; |
| 3456 |
|
| 3457 |
if (UNIT_GET_SLICE(u) == slice) |
| 3458 |
return 0; |
| 3459 |
|
| 3460 |
/* Disallow slice changes if @u is already bound to cgroups */ |
| 3461 |
if (UNIT_GET_SLICE(u)) { |
| 3462 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 3463 |
if (crt && crt->cgroup_path) |
| 3464 |
return -EBUSY; |
| 3465 |
} |
| 3466 |
|
| 3467 |
/* Remove any slices assigned prior; we should only have one UNIT_IN_SLICE dependency */ |
| 3468 |
if (UNIT_GET_SLICE(u)) |
| 3469 |
unit_remove_dependencies(u, UNIT_DEPENDENCY_SLICE_PROPERTY); |
| 3470 |
|
| 3471 |
r = unit_add_dependency(u, UNIT_IN_SLICE, slice, true, UNIT_DEPENDENCY_SLICE_PROPERTY); |
| 3472 |
if (r < 0) |
| 3473 |
return r; |
| 3474 |
|
| 3475 |
return 1; |
| 3476 |
} |
| 3477 |
|
| 3478 |
int unit_set_default_slice(Unit *u) { |
| 3479 |
const char *slice_name; |
| 3480 |
Unit *slice; |
| 3481 |
int r; |
| 3482 |
|
| 3483 |
assert(u); |
| 3484 |
|
| 3485 |
if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES)) |
| 3486 |
return 0; |
| 3487 |
|
| 3488 |
if (UNIT_GET_SLICE(u)) |
| 3489 |
return 0; |
| 3490 |
|
| 3491 |
if (u->instance) { |
| 3492 |
_cleanup_free_ char *prefix = NULL, *escaped = NULL; |
| 3493 |
|
| 3494 |
/* Implicitly place all instantiated units in their |
| 3495 |
* own per-template slice */ |
| 3496 |
|
| 3497 |
r = unit_name_to_prefix(u->id, &prefix); |
| 3498 |
if (r < 0) |
| 3499 |
return r; |
| 3500 |
|
| 3501 |
/* The prefix is already escaped, but it might include |
| 3502 |
* "-" which has a special meaning for slice units, |
| 3503 |
* hence escape it here extra. */ |
| 3504 |
escaped = unit_name_escape(prefix); |
| 3505 |
if (!escaped) |
| 3506 |
return -ENOMEM; |
| 3507 |
|
| 3508 |
if (MANAGER_IS_SYSTEM(u->manager)) |
| 3509 |
slice_name = strjoina("system-", escaped, ".slice"); |
| 3510 |
else |
| 3511 |
slice_name = strjoina("app-", escaped, ".slice"); |
| 3512 |
|
| 3513 |
} else if (unit_is_extrinsic(u)) |
| 3514 |
/* Keep all extrinsic units (e.g. perpetual units and swap and mount units in user mode) in |
| 3515 |
* the root slice. They don't really belong in one of the subslices. */ |
| 3516 |
slice_name = SPECIAL_ROOT_SLICE; |
| 3517 |
|
| 3518 |
else if (MANAGER_IS_SYSTEM(u->manager)) |
| 3519 |
slice_name = SPECIAL_SYSTEM_SLICE; |
| 3520 |
else |
| 3521 |
slice_name = SPECIAL_APP_SLICE; |
| 3522 |
|
| 3523 |
r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice); |
| 3524 |
if (r < 0) |
| 3525 |
return r; |
| 3526 |
|
| 3527 |
return unit_set_slice(u, slice); |
| 3528 |
} |
| 3529 |
|
| 3530 |
const char* unit_slice_name(Unit *u) { |
| 3531 |
Unit *slice; |
| 3532 |
assert(u); |
| 3533 |
|
| 3534 |
slice = UNIT_GET_SLICE(u); |
| 3535 |
if (!slice) |
| 3536 |
return NULL; |
| 3537 |
|
| 3538 |
return slice->id; |
| 3539 |
} |
| 3540 |
|
| 3541 |
int unit_load_related_unit(Unit *u, const char *type, Unit **_found) { |
| 3542 |
_cleanup_free_ char *t = NULL; |
| 3543 |
int r; |
| 3544 |
|
| 3545 |
assert(u); |
| 3546 |
assert(type); |
| 3547 |
assert(_found); |
| 3548 |
|
| 3549 |
r = unit_name_change_suffix(u->id, type, &t); |
| 3550 |
if (r < 0) |
| 3551 |
return r; |
| 3552 |
if (unit_has_name(u, t)) |
| 3553 |
return -EINVAL; |
| 3554 |
|
| 3555 |
r = manager_load_unit(u->manager, t, NULL, NULL, _found); |
| 3556 |
assert(r < 0 || *_found != u); |
| 3557 |
return r; |
| 3558 |
} |
| 3559 |
|
| 3560 |
static int signal_name_owner_changed_install_handler(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 3561 |
Unit *u = ASSERT_PTR(userdata); |
| 3562 |
const sd_bus_error *e; |
| 3563 |
int r; |
| 3564 |
|
| 3565 |
e = sd_bus_message_get_error(message); |
| 3566 |
if (!e) { |
| 3567 |
log_unit_trace(u, "Successfully installed NameOwnerChanged signal match."); |
| 3568 |
return 0; |
| 3569 |
} |
| 3570 |
|
| 3571 |
r = sd_bus_error_get_errno(e); |
| 3572 |
log_unit_error_errno(u, r, |
| 3573 |
"Unexpected error response on installing NameOwnerChanged signal match: %s", |
| 3574 |
bus_error_message(e, r)); |
| 3575 |
|
| 3576 |
/* If we failed to install NameOwnerChanged signal, also unref the bus slot of GetNameOwner(). */ |
| 3577 |
u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
| 3578 |
u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
| 3579 |
|
| 3580 |
if (UNIT_VTABLE(u)->bus_name_owner_change) |
| 3581 |
UNIT_VTABLE(u)->bus_name_owner_change(u, NULL); |
| 3582 |
|
| 3583 |
return 0; |
| 3584 |
} |
| 3585 |
|
| 3586 |
static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 3587 |
const char *new_owner; |
| 3588 |
Unit *u = ASSERT_PTR(userdata); |
| 3589 |
int r; |
| 3590 |
|
| 3591 |
assert(message); |
| 3592 |
|
| 3593 |
r = sd_bus_message_read(message, "sss", NULL, NULL, &new_owner); |
| 3594 |
if (r < 0) { |
| 3595 |
bus_log_parse_error(r); |
| 3596 |
return 0; |
| 3597 |
} |
| 3598 |
|
| 3599 |
if (UNIT_VTABLE(u)->bus_name_owner_change) |
| 3600 |
UNIT_VTABLE(u)->bus_name_owner_change(u, empty_to_null(new_owner)); |
| 3601 |
|
| 3602 |
return 0; |
| 3603 |
} |
| 3604 |
|
| 3605 |
static int get_name_owner_handler(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) { |
| 3606 |
const sd_bus_error *e; |
| 3607 |
const char *new_owner; |
| 3608 |
Unit *u = ASSERT_PTR(userdata); |
| 3609 |
int r; |
| 3610 |
|
| 3611 |
assert(message); |
| 3612 |
|
| 3613 |
u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
| 3614 |
|
| 3615 |
e = sd_bus_message_get_error(message); |
| 3616 |
if (e) { |
| 3617 |
if (!sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) { |
| 3618 |
r = sd_bus_error_get_errno(e); |
| 3619 |
log_unit_error_errno(u, r, |
| 3620 |
"Unexpected error response from GetNameOwner(): %s", |
| 3621 |
bus_error_message(e, r)); |
| 3622 |
} |
| 3623 |
|
| 3624 |
new_owner = NULL; |
| 3625 |
} else { |
| 3626 |
r = sd_bus_message_read(message, "s", &new_owner); |
| 3627 |
if (r < 0) |
| 3628 |
return bus_log_parse_error(r); |
| 3629 |
|
| 3630 |
assert(!isempty(new_owner)); |
| 3631 |
} |
| 3632 |
|
| 3633 |
if (UNIT_VTABLE(u)->bus_name_owner_change) |
| 3634 |
UNIT_VTABLE(u)->bus_name_owner_change(u, new_owner); |
| 3635 |
|
| 3636 |
return 0; |
| 3637 |
} |
| 3638 |
|
| 3639 |
int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) { |
| 3640 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 3641 |
const char *match; |
| 3642 |
usec_t timeout_usec = 0; |
| 3643 |
int r; |
| 3644 |
|
| 3645 |
assert(u); |
| 3646 |
assert(bus); |
| 3647 |
assert(name); |
| 3648 |
|
| 3649 |
if (u->match_bus_slot || u->get_name_owner_slot) |
| 3650 |
return -EBUSY; |
| 3651 |
|
| 3652 |
/* NameOwnerChanged and GetNameOwner is used to detect when a service finished starting up. The dbus |
| 3653 |
* call timeout shouldn't be earlier than that. If we couldn't get the start timeout, use the default |
| 3654 |
* value defined above. */ |
| 3655 |
if (UNIT_VTABLE(u)->get_timeout_start_usec) |
| 3656 |
timeout_usec = UNIT_VTABLE(u)->get_timeout_start_usec(u); |
| 3657 |
|
| 3658 |
match = strjoina("type='signal'," |
| 3659 |
"sender='org.freedesktop.DBus'," |
| 3660 |
"path='/org/freedesktop/DBus'," |
| 3661 |
"interface='org.freedesktop.DBus'," |
| 3662 |
"member='NameOwnerChanged'," |
| 3663 |
"arg0='", name, "'"); |
| 3664 |
|
| 3665 |
r = bus_add_match_full( |
| 3666 |
bus, |
| 3667 |
&u->match_bus_slot, |
| 3668 |
/* asynchronous= */ true, |
| 3669 |
match, |
| 3670 |
signal_name_owner_changed, |
| 3671 |
signal_name_owner_changed_install_handler, |
| 3672 |
u, |
| 3673 |
timeout_usec); |
| 3674 |
if (r < 0) |
| 3675 |
return r; |
| 3676 |
|
| 3677 |
r = sd_bus_message_new_method_call( |
| 3678 |
bus, |
| 3679 |
&m, |
| 3680 |
"org.freedesktop.DBus", |
| 3681 |
"/org/freedesktop/DBus", |
| 3682 |
"org.freedesktop.DBus", |
| 3683 |
"GetNameOwner"); |
| 3684 |
if (r < 0) |
| 3685 |
return r; |
| 3686 |
|
| 3687 |
r = sd_bus_message_append(m, "s", name); |
| 3688 |
if (r < 0) |
| 3689 |
return r; |
| 3690 |
|
| 3691 |
r = sd_bus_call_async( |
| 3692 |
bus, |
| 3693 |
&u->get_name_owner_slot, |
| 3694 |
m, |
| 3695 |
get_name_owner_handler, |
| 3696 |
u, |
| 3697 |
timeout_usec); |
| 3698 |
if (r < 0) { |
| 3699 |
u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
| 3700 |
return r; |
| 3701 |
} |
| 3702 |
|
| 3703 |
log_unit_debug(u, "Watching D-Bus name '%s'.", name); |
| 3704 |
return 0; |
| 3705 |
} |
| 3706 |
|
| 3707 |
int unit_watch_bus_name(Unit *u, const char *name) { |
| 3708 |
int r; |
| 3709 |
|
| 3710 |
assert(u); |
| 3711 |
assert(name); |
| 3712 |
|
| 3713 |
/* Watch a specific name on the bus. We only support one unit |
| 3714 |
* watching each name for now. */ |
| 3715 |
|
| 3716 |
if (u->manager->api_bus) { |
| 3717 |
/* If the bus is already available, install the match directly. |
| 3718 |
* Otherwise, just put the name in the list. bus_setup_api() will take care later. */ |
| 3719 |
r = unit_install_bus_match(u, u->manager->api_bus, name); |
| 3720 |
if (r < 0) |
| 3721 |
return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name); |
| 3722 |
} |
| 3723 |
|
| 3724 |
r = hashmap_put(u->manager->watch_bus, name, u); |
| 3725 |
if (r < 0) { |
| 3726 |
u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
| 3727 |
u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
| 3728 |
return log_warning_errno(r, "Failed to put bus name to hashmap: %m"); |
| 3729 |
} |
| 3730 |
|
| 3731 |
return 0; |
| 3732 |
} |
| 3733 |
|
| 3734 |
void unit_unwatch_bus_name(Unit *u, const char *name) { |
| 3735 |
assert(u); |
| 3736 |
assert(name); |
| 3737 |
|
| 3738 |
(void) hashmap_remove_value(u->manager->watch_bus, name, u); |
| 3739 |
u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot); |
| 3740 |
u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot); |
| 3741 |
} |
| 3742 |
|
| 3743 |
int unit_add_node_dependency(Unit *u, const char *what, UnitDependency dep, UnitDependencyMask mask) { |
| 3744 |
_cleanup_free_ char *e = NULL; |
| 3745 |
Unit *device; |
| 3746 |
int r; |
| 3747 |
|
| 3748 |
assert(u); |
| 3749 |
|
| 3750 |
/* Adds in links to the device node that this unit is based on */ |
| 3751 |
if (isempty(what)) |
| 3752 |
return 0; |
| 3753 |
|
| 3754 |
if (!is_device_path(what)) |
| 3755 |
return 0; |
| 3756 |
|
| 3757 |
/* When device units aren't supported (such as in a container), don't create dependencies on them. */ |
| 3758 |
if (!unit_type_supported(UNIT_DEVICE)) |
| 3759 |
return 0; |
| 3760 |
|
| 3761 |
r = unit_name_from_path(what, ".device", &e); |
| 3762 |
if (r < 0) |
| 3763 |
return r; |
| 3764 |
|
| 3765 |
r = manager_load_unit(u->manager, e, NULL, NULL, &device); |
| 3766 |
if (r < 0) |
| 3767 |
return r; |
| 3768 |
|
| 3769 |
if (dep == UNIT_REQUIRES && device_shall_be_bound_by(device, u)) |
| 3770 |
dep = UNIT_BINDS_TO; |
| 3771 |
|
| 3772 |
return unit_add_two_dependencies(u, UNIT_AFTER, |
| 3773 |
MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS, |
| 3774 |
device, true, mask); |
| 3775 |
} |
| 3776 |
|
| 3777 |
int unit_add_blockdev_dependency(Unit *u, const char *what, UnitDependencyMask mask) { |
| 3778 |
_cleanup_free_ char *escaped = NULL, *target = NULL; |
| 3779 |
int r; |
| 3780 |
|
| 3781 |
assert(u); |
| 3782 |
|
| 3783 |
if (isempty(what)) |
| 3784 |
return 0; |
| 3785 |
|
| 3786 |
if (!path_startswith(what, "/dev/")) |
| 3787 |
return 0; |
| 3788 |
|
| 3789 |
/* If we don't support devices, then also don't bother with [email protected] */ |
| 3790 |
if (!unit_type_supported(UNIT_DEVICE)) |
| 3791 |
return 0; |
| 3792 |
|
| 3793 |
r = unit_name_path_escape(what, &escaped); |
| 3794 |
if (r < 0) |
| 3795 |
return r; |
| 3796 |
|
| 3797 |
r = unit_name_build("blockdev", escaped, ".target", &target); |
| 3798 |
if (r < 0) |
| 3799 |
return r; |
| 3800 |
|
| 3801 |
return unit_add_dependency_by_name(u, UNIT_AFTER, target, true, mask); |
| 3802 |
} |
| 3803 |
|
| 3804 |
int unit_coldplug(Unit *u) { |
| 3805 |
int r = 0; |
| 3806 |
|
| 3807 |
assert(u); |
| 3808 |
|
| 3809 |
/* Make sure we don't enter a loop, when coldplugging recursively. */ |
| 3810 |
if (u->coldplugged) |
| 3811 |
return 0; |
| 3812 |
|
| 3813 |
u->coldplugged = true; |
| 3814 |
|
| 3815 |
STRV_FOREACH(i, u->deserialized_refs) |
| 3816 |
RET_GATHER(r, bus_unit_track_add_name(u, *i)); |
| 3817 |
|
| 3818 |
u->deserialized_refs = strv_free(u->deserialized_refs); |
| 3819 |
|
| 3820 |
if (UNIT_VTABLE(u)->coldplug) |
| 3821 |
RET_GATHER(r, UNIT_VTABLE(u)->coldplug(u)); |
| 3822 |
|
| 3823 |
if (u->job) |
| 3824 |
RET_GATHER(r, job_coldplug(u->job)); |
| 3825 |
if (u->nop_job) |
| 3826 |
RET_GATHER(r, job_coldplug(u->nop_job)); |
| 3827 |
|
| 3828 |
return r; |
| 3829 |
} |
| 3830 |
|
| 3831 |
void unit_catchup(Unit *u) { |
| 3832 |
assert(u); |
| 3833 |
|
| 3834 |
if (UNIT_VTABLE(u)->catchup) |
| 3835 |
UNIT_VTABLE(u)->catchup(u); |
| 3836 |
|
| 3837 |
unit_cgroup_catchup(u); |
| 3838 |
} |
| 3839 |
|
| 3840 |
static bool fragment_mtime_newer(const char *path, usec_t mtime, bool path_masked) { |
| 3841 |
struct stat st; |
| 3842 |
|
| 3843 |
if (!path) |
| 3844 |
return false; |
| 3845 |
|
| 3846 |
/* If the source is some virtual kernel file system, then we assume we watch it anyway, and hence pretend we |
| 3847 |
* are never out-of-date. */ |
| 3848 |
if (PATH_STARTSWITH_SET(path, "/proc", "/sys")) |
| 3849 |
return false; |
| 3850 |
|
| 3851 |
if (stat(path, &st) < 0) |
| 3852 |
/* What, cannot access this anymore? */ |
| 3853 |
return true; |
| 3854 |
|
| 3855 |
if (path_masked) |
| 3856 |
/* For masked files check if they are still so */ |
| 3857 |
return !null_or_empty(&st); |
| 3858 |
else |
| 3859 |
/* For non-empty files check the mtime */ |
| 3860 |
return timespec_load(&st.st_mtim) > mtime; |
| 3861 |
|
| 3862 |
return false; |
| 3863 |
} |
| 3864 |
|
| 3865 |
bool unit_need_daemon_reload(Unit *u) { |
| 3866 |
assert(u); |
| 3867 |
assert(u->manager); |
| 3868 |
|
| 3869 |
if (u->manager->unit_file_state_outdated) |
| 3870 |
return true; |
| 3871 |
|
| 3872 |
/* For unit files, we allow masking… */ |
| 3873 |
if (fragment_mtime_newer(u->fragment_path, u->fragment_mtime, |
| 3874 |
u->load_state == UNIT_MASKED)) |
| 3875 |
return true; |
| 3876 |
|
| 3877 |
/* Source paths should not be masked… */ |
| 3878 |
if (fragment_mtime_newer(u->source_path, u->source_mtime, false)) |
| 3879 |
return true; |
| 3880 |
|
| 3881 |
if (u->load_state == UNIT_LOADED) { |
| 3882 |
_cleanup_strv_free_ char **dropins = NULL; |
| 3883 |
|
| 3884 |
(void) unit_find_dropin_paths(u, /* use_unit_path_cache= */ false, &dropins); |
| 3885 |
|
| 3886 |
if (!strv_equal(u->dropin_paths, dropins)) |
| 3887 |
return true; |
| 3888 |
|
| 3889 |
/* … any drop-ins that are masked are simply omitted from the list. */ |
| 3890 |
STRV_FOREACH(path, u->dropin_paths) |
| 3891 |
if (fragment_mtime_newer(*path, u->dropin_mtime, false)) |
| 3892 |
return true; |
| 3893 |
} |
| 3894 |
|
| 3895 |
return false; |
| 3896 |
} |
| 3897 |
|
| 3898 |
void unit_reset_failed(Unit *u) { |
| 3899 |
assert(u); |
| 3900 |
|
| 3901 |
if (UNIT_VTABLE(u)->reset_failed) |
| 3902 |
UNIT_VTABLE(u)->reset_failed(u); |
| 3903 |
|
| 3904 |
ratelimit_reset(&u->start_ratelimit); |
| 3905 |
u->start_limit_hit = false; |
| 3906 |
|
| 3907 |
(void) unit_set_debug_invocation(u, /* enable= */ false); |
| 3908 |
} |
| 3909 |
|
| 3910 |
Unit *unit_following(Unit *u) { |
| 3911 |
assert(u); |
| 3912 |
|
| 3913 |
if (UNIT_VTABLE(u)->following) |
| 3914 |
return UNIT_VTABLE(u)->following(u); |
| 3915 |
|
| 3916 |
return NULL; |
| 3917 |
} |
| 3918 |
|
| 3919 |
bool unit_stop_pending(Unit *u) { |
| 3920 |
assert(u); |
| 3921 |
|
| 3922 |
/* This call does check the current state of the unit. It's |
| 3923 |
* hence useful to be called from state change calls of the |
| 3924 |
* unit itself, where the state isn't updated yet. This is |
| 3925 |
* different from unit_inactive_or_pending() which checks both |
| 3926 |
* the current state and for a queued job. */ |
| 3927 |
|
| 3928 |
return unit_has_job_type(u, JOB_STOP); |
| 3929 |
} |
| 3930 |
|
| 3931 |
bool unit_inactive_or_pending(Unit *u) { |
| 3932 |
assert(u); |
| 3933 |
|
| 3934 |
/* Returns true if the unit is inactive or going down */ |
| 3935 |
|
| 3936 |
if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u))) |
| 3937 |
return true; |
| 3938 |
|
| 3939 |
if (unit_stop_pending(u)) |
| 3940 |
return true; |
| 3941 |
|
| 3942 |
return false; |
| 3943 |
} |
| 3944 |
|
| 3945 |
bool unit_active_or_pending(Unit *u) { |
| 3946 |
assert(u); |
| 3947 |
|
| 3948 |
/* Returns true if the unit is active or going up */ |
| 3949 |
|
| 3950 |
if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) |
| 3951 |
return true; |
| 3952 |
|
| 3953 |
if (u->job && IN_SET(u->job->type, JOB_START, JOB_RESTART)) |
| 3954 |
return true; |
| 3955 |
|
| 3956 |
return false; |
| 3957 |
} |
| 3958 |
|
| 3959 |
bool unit_will_restart_default(Unit *u) { |
| 3960 |
assert(u); |
| 3961 |
|
| 3962 |
return unit_has_job_type(u, JOB_START); |
| 3963 |
} |
| 3964 |
|
| 3965 |
bool unit_will_restart(Unit *u) { |
| 3966 |
assert(u); |
| 3967 |
|
| 3968 |
if (!UNIT_VTABLE(u)->will_restart) |
| 3969 |
return false; |
| 3970 |
|
| 3971 |
return UNIT_VTABLE(u)->will_restart(u); |
| 3972 |
} |
| 3973 |
|
| 3974 |
void unit_notify_cgroup_oom(Unit *u, bool managed_oom) { |
| 3975 |
assert(u); |
| 3976 |
|
| 3977 |
if (UNIT_VTABLE(u)->notify_cgroup_oom) |
| 3978 |
UNIT_VTABLE(u)->notify_cgroup_oom(u, managed_oom); |
| 3979 |
} |
| 3980 |
|
| 3981 |
static int unit_pid_set(Unit *u, Set **pid_set) { |
| 3982 |
int r; |
| 3983 |
|
| 3984 |
assert(u); |
| 3985 |
assert(pid_set); |
| 3986 |
|
| 3987 |
set_clear(*pid_set); /* This updates input. */ |
| 3988 |
|
| 3989 |
/* Exclude the main/control pids from being killed via the cgroup */ |
| 3990 |
|
| 3991 |
PidRef *pid; |
| 3992 |
FOREACH_ARGUMENT(pid, unit_main_pid(u), unit_control_pid(u)) |
| 3993 |
if (pidref_is_set(pid)) { |
| 3994 |
r = set_ensure_put(pid_set, NULL, PID_TO_PTR(pid->pid)); |
| 3995 |
if (r < 0) |
| 3996 |
return r; |
| 3997 |
} |
| 3998 |
|
| 3999 |
return 0; |
| 4000 |
} |
| 4001 |
|
| 4002 |
static int kill_common_log(const PidRef *pid, int signo, void *userdata) { |
| 4003 |
_cleanup_free_ char *comm = NULL; |
| 4004 |
Unit *u = ASSERT_PTR(userdata); |
| 4005 |
|
| 4006 |
(void) pidref_get_comm(pid, &comm); |
| 4007 |
|
| 4008 |
log_unit_info(u, "Sending signal SIG%s to process " PID_FMT " (%s) on client request.", |
| 4009 |
signal_to_string(signo), pid->pid, strna(comm)); |
| 4010 |
|
| 4011 |
return 1; |
| 4012 |
} |
| 4013 |
|
| 4014 |
static int kill_or_sigqueue(PidRef *pidref, int signo, int code, int value) { |
| 4015 |
assert(pidref_is_set(pidref)); |
| 4016 |
assert(SIGNAL_VALID(signo)); |
| 4017 |
|
| 4018 |
switch (code) { |
| 4019 |
|
| 4020 |
case SI_USER: |
| 4021 |
log_debug("Killing " PID_FMT " with signal SIG%s.", pidref->pid, signal_to_string(signo)); |
| 4022 |
return pidref_kill(pidref, signo); |
| 4023 |
|
| 4024 |
case SI_QUEUE: |
| 4025 |
log_debug("Enqueuing value %i to " PID_FMT " on signal SIG%s.", value, pidref->pid, signal_to_string(signo)); |
| 4026 |
return pidref_sigqueue(pidref, signo, value); |
| 4027 |
|
| 4028 |
default: |
| 4029 |
assert_not_reached(); |
| 4030 |
} |
| 4031 |
} |
| 4032 |
|
| 4033 |
static int unit_kill_one( |
| 4034 |
Unit *u, |
| 4035 |
PidRef *pidref, |
| 4036 |
const char *type, |
| 4037 |
int signo, |
| 4038 |
int code, |
| 4039 |
int value, |
| 4040 |
sd_bus_error *ret_error) { |
| 4041 |
|
| 4042 |
int r; |
| 4043 |
|
| 4044 |
assert(u); |
| 4045 |
assert(type); |
| 4046 |
|
| 4047 |
if (!pidref_is_set(pidref)) |
| 4048 |
return 0; |
| 4049 |
|
| 4050 |
_cleanup_free_ char *comm = NULL; |
| 4051 |
(void) pidref_get_comm(pidref, &comm); |
| 4052 |
|
| 4053 |
r = kill_or_sigqueue(pidref, signo, code, value); |
| 4054 |
if (r == -ESRCH) |
| 4055 |
return 0; |
| 4056 |
if (r < 0) { |
| 4057 |
/* Report this failure both to the logs and to the client */ |
| 4058 |
if (ret_error) |
| 4059 |
sd_bus_error_set_errnof( |
| 4060 |
ret_error, r, |
| 4061 |
"Failed to send signal SIG%s to %s process " PID_FMT " (%s): %m", |
| 4062 |
signal_to_string(signo), type, pidref->pid, strna(comm)); |
| 4063 |
|
| 4064 |
return log_unit_warning_errno( |
| 4065 |
u, r, |
| 4066 |
"Failed to send signal SIG%s to %s process " PID_FMT " (%s) on client request: %m", |
| 4067 |
signal_to_string(signo), type, pidref->pid, strna(comm)); |
| 4068 |
} |
| 4069 |
|
| 4070 |
log_unit_info(u, "Sent signal SIG%s to %s process " PID_FMT " (%s) on client request.", |
| 4071 |
signal_to_string(signo), type, pidref->pid, strna(comm)); |
| 4072 |
return 1; /* killed */ |
| 4073 |
} |
| 4074 |
|
| 4075 |
int unit_kill( |
| 4076 |
Unit *u, |
| 4077 |
KillWhom whom, |
| 4078 |
const char *subgroup, |
| 4079 |
int signo, |
| 4080 |
int code, |
| 4081 |
int value, |
| 4082 |
sd_bus_error *ret_error) { |
| 4083 |
|
| 4084 |
PidRef *main_pid, *control_pid; |
| 4085 |
bool killed = false; |
| 4086 |
int ret = 0, r; |
| 4087 |
|
| 4088 |
/* This is the common implementation for explicit user-requested killing of unit processes, shared by |
| 4089 |
* various unit types. Do not confuse with unit_kill_context(), which is what we use when we want to |
| 4090 |
* stop a service ourselves. */ |
| 4091 |
|
| 4092 |
assert(u); |
| 4093 |
assert(whom >= 0); |
| 4094 |
assert(whom < _KILL_WHOM_MAX); |
| 4095 |
assert(SIGNAL_VALID(signo)); |
| 4096 |
assert(IN_SET(code, SI_USER, SI_QUEUE)); |
| 4097 |
|
| 4098 |
if (subgroup) { |
| 4099 |
if (!IN_SET(whom, KILL_CGROUP, KILL_CGROUP_FAIL)) |
| 4100 |
return sd_bus_error_set(ret_error, SD_BUS_ERROR_NOT_SUPPORTED, |
| 4101 |
"Killing by subgroup is only supported for 'cgroup' or 'cgroup-kill' modes."); |
| 4102 |
|
| 4103 |
if (!unit_cgroup_delegate(u)) |
| 4104 |
return sd_bus_error_set(ret_error, SD_BUS_ERROR_NOT_SUPPORTED, |
| 4105 |
"Killing by subgroup is only available for units with control group delegation enabled."); |
| 4106 |
} |
| 4107 |
|
| 4108 |
main_pid = unit_main_pid(u); |
| 4109 |
control_pid = unit_control_pid(u); |
| 4110 |
|
| 4111 |
if (!UNIT_HAS_CGROUP_CONTEXT(u) && !main_pid && !control_pid) |
| 4112 |
return sd_bus_error_set(ret_error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit type does not support process killing."); |
| 4113 |
|
| 4114 |
if (IN_SET(whom, KILL_MAIN, KILL_MAIN_FAIL)) { |
| 4115 |
if (!main_pid) |
| 4116 |
return sd_bus_error_setf(ret_error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type)); |
| 4117 |
if (!pidref_is_set(main_pid)) |
| 4118 |
return sd_bus_error_set_const(ret_error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); |
| 4119 |
} |
| 4120 |
|
| 4121 |
if (IN_SET(whom, KILL_CONTROL, KILL_CONTROL_FAIL)) { |
| 4122 |
if (!control_pid) |
| 4123 |
return sd_bus_error_setf(ret_error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type)); |
| 4124 |
if (!pidref_is_set(control_pid)) |
| 4125 |
return sd_bus_error_set_const(ret_error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); |
| 4126 |
} |
| 4127 |
|
| 4128 |
if (IN_SET(whom, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) { |
| 4129 |
r = unit_kill_one(u, control_pid, "control", signo, code, value, ret_error); |
| 4130 |
RET_GATHER(ret, r); |
| 4131 |
killed = killed || r > 0; |
| 4132 |
} |
| 4133 |
|
| 4134 |
if (IN_SET(whom, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) { |
| 4135 |
r = unit_kill_one(u, main_pid, "main", signo, code, value, ret >= 0 ? ret_error : NULL); |
| 4136 |
RET_GATHER(ret, r); |
| 4137 |
killed = killed || r > 0; |
| 4138 |
} |
| 4139 |
|
| 4140 |
/* Note: if we shall enqueue rather than kill we won't do this via the cgroup mechanism, since it |
| 4141 |
* doesn't really make much sense (and given that enqueued values are a relatively expensive |
| 4142 |
* resource, and we shouldn't allow us to be subjects for such allocation sprees) */ |
| 4143 |
if (IN_SET(whom, KILL_ALL, KILL_ALL_FAIL, KILL_CGROUP, KILL_CGROUP_FAIL) && code == SI_USER) { |
| 4144 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 4145 |
if (crt && crt->cgroup_path) { |
| 4146 |
_cleanup_set_free_ Set *pid_set = NULL; |
| 4147 |
_cleanup_free_ char *joined = NULL; |
| 4148 |
const char *p; |
| 4149 |
|
| 4150 |
if (empty_or_root(subgroup)) |
| 4151 |
p = crt->cgroup_path; |
| 4152 |
else { |
| 4153 |
joined = path_join(crt->cgroup_path, subgroup); |
| 4154 |
if (!joined) |
| 4155 |
return -ENOMEM; |
| 4156 |
|
| 4157 |
p = joined; |
| 4158 |
} |
| 4159 |
|
| 4160 |
if (signo == SIGKILL) { |
| 4161 |
r = cg_kill_kernel_sigkill(p); |
| 4162 |
if (r >= 0) { |
| 4163 |
killed = true; |
| 4164 |
log_unit_info(u, "Killed unit cgroup '%s' with SIGKILL on client request.", p); |
| 4165 |
goto finish; |
| 4166 |
} |
| 4167 |
if (r != -EOPNOTSUPP) { |
| 4168 |
if (ret >= 0) |
| 4169 |
sd_bus_error_set_errnof(ret_error, r, |
| 4170 |
"Failed to kill unit cgroup: %m"); |
| 4171 |
RET_GATHER(ret, log_unit_warning_errno(u, r, "Failed to kill unit cgroup '%s': %m", p)); |
| 4172 |
goto finish; |
| 4173 |
} |
| 4174 |
/* Fall back to manual enumeration */ |
| 4175 |
} else if (IN_SET(whom, KILL_ALL, KILL_ALL_FAIL)) { |
| 4176 |
/* Exclude the main/control pids from being killed via the cgroup if not |
| 4177 |
* SIGKILL */ |
| 4178 |
r = unit_pid_set(u, &pid_set); |
| 4179 |
if (r < 0) |
| 4180 |
return log_oom(); |
| 4181 |
} |
| 4182 |
|
| 4183 |
r = cg_kill_recursive(p, signo, /* flags= */ 0, pid_set, kill_common_log, u); |
| 4184 |
if (r < 0 && !IN_SET(r, -ESRCH, -ENOENT)) { |
| 4185 |
if (ret >= 0) |
| 4186 |
sd_bus_error_set_errnof( |
| 4187 |
ret_error, r, |
| 4188 |
"Failed to send signal SIG%s to processes in unit cgroup '%s': %m", |
| 4189 |
signal_to_string(signo), p); |
| 4190 |
|
| 4191 |
RET_GATHER(ret, log_unit_warning_errno( |
| 4192 |
u, r, |
| 4193 |
"Failed to send signal SIG%s to processes in unit cgroup '%s' on client request: %m", |
| 4194 |
signal_to_string(signo), p)); |
| 4195 |
} |
| 4196 |
killed = killed || r > 0; |
| 4197 |
} |
| 4198 |
} |
| 4199 |
|
| 4200 |
finish: |
| 4201 |
/* If the "fail" versions of the operation are requested, then complain if the set of processes we killed is empty */ |
| 4202 |
if (ret >= 0 && !killed && IN_SET(whom, KILL_ALL_FAIL, KILL_CONTROL_FAIL, KILL_MAIN_FAIL, KILL_CGROUP_FAIL)) |
| 4203 |
return sd_bus_error_set_const(ret_error, BUS_ERROR_NO_SUCH_PROCESS, "No matching processes to kill"); |
| 4204 |
|
| 4205 |
return ret; |
| 4206 |
} |
| 4207 |
|
| 4208 |
int unit_following_set(Unit *u, Set **s) { |
| 4209 |
assert(u); |
| 4210 |
assert(s); |
| 4211 |
|
| 4212 |
if (UNIT_VTABLE(u)->following_set) |
| 4213 |
return UNIT_VTABLE(u)->following_set(u, s); |
| 4214 |
|
| 4215 |
*s = NULL; |
| 4216 |
return 0; |
| 4217 |
} |
| 4218 |
|
| 4219 |
UnitFileState unit_get_unit_file_state(Unit *u) { |
| 4220 |
int r; |
| 4221 |
|
| 4222 |
assert(u); |
| 4223 |
|
| 4224 |
if (u->unit_file_state >= 0 || !u->fragment_path) |
| 4225 |
return u->unit_file_state; |
| 4226 |
|
| 4227 |
/* If we know this is a transient unit no need to ask the unit file state for details. Let's bypass |
| 4228 |
* the more expensive on-disk check. */ |
| 4229 |
if (u->transient) |
| 4230 |
return (u->unit_file_state = UNIT_FILE_TRANSIENT); |
| 4231 |
|
| 4232 |
r = unit_file_get_state( |
| 4233 |
u->manager->runtime_scope, |
| 4234 |
/* root_dir= */ NULL, |
| 4235 |
u->id, |
| 4236 |
&u->unit_file_state); |
| 4237 |
if (r < 0) |
| 4238 |
u->unit_file_state = UNIT_FILE_BAD; |
| 4239 |
|
| 4240 |
return u->unit_file_state; |
| 4241 |
} |
| 4242 |
|
| 4243 |
PresetAction unit_get_unit_file_preset(Unit *u) { |
| 4244 |
int r; |
| 4245 |
|
| 4246 |
assert(u); |
| 4247 |
|
| 4248 |
if (u->unit_file_preset >= 0) |
| 4249 |
return u->unit_file_preset; |
| 4250 |
|
| 4251 |
/* If this is a transient or perpetual unit file it doesn't make much sense to ask the preset |
| 4252 |
* database about this, because enabling/disabling makes no sense for either. Hence don't. */ |
| 4253 |
if (!u->fragment_path || u->transient || u->perpetual) |
| 4254 |
return (u->unit_file_preset = -ENOEXEC); |
| 4255 |
|
| 4256 |
_cleanup_free_ char *bn = NULL; |
| 4257 |
r = path_extract_filename(u->fragment_path, &bn); |
| 4258 |
if (r < 0) |
| 4259 |
return (u->unit_file_preset = r); |
| 4260 |
if (r == O_DIRECTORY) |
| 4261 |
return (u->unit_file_preset = -EISDIR); |
| 4262 |
|
| 4263 |
return (u->unit_file_preset = unit_file_query_preset( |
| 4264 |
u->manager->runtime_scope, |
| 4265 |
/* root_dir= */ NULL, |
| 4266 |
bn, |
| 4267 |
/* cached= */ NULL)); |
| 4268 |
} |
| 4269 |
|
| 4270 |
Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target) { |
| 4271 |
assert(ref); |
| 4272 |
assert(source); |
| 4273 |
assert(target); |
| 4274 |
|
| 4275 |
if (ref->target) |
| 4276 |
unit_ref_unset(ref); |
| 4277 |
|
| 4278 |
ref->source = source; |
| 4279 |
ref->target = target; |
| 4280 |
LIST_PREPEND(refs_by_target, target->refs_by_target, ref); |
| 4281 |
return target; |
| 4282 |
} |
| 4283 |
|
| 4284 |
void unit_ref_unset(UnitRef *ref) { |
| 4285 |
assert(ref); |
| 4286 |
|
| 4287 |
if (!ref->target) |
| 4288 |
return; |
| 4289 |
|
| 4290 |
/* We are about to drop a reference to the unit, make sure the garbage collection has a look at it as it might |
| 4291 |
* be unreferenced now. */ |
| 4292 |
unit_add_to_gc_queue(ref->target); |
| 4293 |
|
| 4294 |
LIST_REMOVE(refs_by_target, ref->target->refs_by_target, ref); |
| 4295 |
ref->source = ref->target = NULL; |
| 4296 |
} |
| 4297 |
|
| 4298 |
static int user_from_unit_name(Unit *u, char **ret) { |
| 4299 |
|
| 4300 |
static const uint8_t hash_key[] = { |
| 4301 |
0x58, 0x1a, 0xaf, 0xe6, 0x28, 0x58, 0x4e, 0x96, |
| 4302 |
0xb4, 0x4e, 0xf5, 0x3b, 0x8c, 0x92, 0x07, 0xec |
| 4303 |
}; |
| 4304 |
|
| 4305 |
_cleanup_free_ char *n = NULL; |
| 4306 |
int r; |
| 4307 |
|
| 4308 |
assert(u); |
| 4309 |
assert(ret); |
| 4310 |
|
| 4311 |
r = unit_name_to_prefix(u->id, &n); |
| 4312 |
if (r < 0) |
| 4313 |
return r; |
| 4314 |
|
| 4315 |
if (valid_user_group_name(n, 0)) { |
| 4316 |
*ret = TAKE_PTR(n); |
| 4317 |
return 0; |
| 4318 |
} |
| 4319 |
|
| 4320 |
/* If we can't use the unit name as a user name, then let's hash it and use that */ |
| 4321 |
if (asprintf(ret, "_du%016" PRIx64, siphash24(n, strlen(n), hash_key)) < 0) |
| 4322 |
return -ENOMEM; |
| 4323 |
|
| 4324 |
return 0; |
| 4325 |
} |
| 4326 |
|
| 4327 |
static int unit_verify_contexts(const Unit *u) { |
| 4328 |
assert(u); |
| 4329 |
|
| 4330 |
const ExecContext *ec = unit_get_exec_context(u); |
| 4331 |
if (!ec) |
| 4332 |
return 0; |
| 4333 |
|
| 4334 |
if (MANAGER_IS_USER(u->manager) && ec->dynamic_user) |
| 4335 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "DynamicUser= enabled for user unit, which is not supported. Refusing."); |
| 4336 |
|
| 4337 |
if (ec->dynamic_user && ec->working_directory_home) |
| 4338 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "WorkingDirectory=~ is not allowed under DynamicUser=yes. Refusing."); |
| 4339 |
|
| 4340 |
if (ec->working_directory && path_below_api_vfs(ec->working_directory) && |
| 4341 |
exec_needs_mount_namespace(ec, /* params= */ NULL)) |
| 4342 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "WorkingDirectory= may not be below /proc/, /sys/ or /dev/ when using mount namespacing. Refusing."); |
| 4343 |
|
| 4344 |
if (exec_needs_pid_namespace(ec, /* params= */ NULL) && !UNIT_VTABLE(u)->notify_pidref) |
| 4345 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "PrivatePIDs= setting is only supported for service units. Refusing."); |
| 4346 |
|
| 4347 |
if ((ec->user || ec->dynamic_user || ec->group || ec->pam_name) && ec->private_users == PRIVATE_USERS_MANAGED) |
| 4348 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "PrivateUsers=managed may not be used in combination with User=/DynamicUser=/Group=/PAMName=, refusing."); |
| 4349 |
|
| 4350 |
if (ec->user_namespace_path && ec->private_users != PRIVATE_USERS_NO) |
| 4351 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "PrivateUsers= may not be used with custom UserNamespacePath=, refusing."); |
| 4352 |
|
| 4353 |
const KillContext *kc = unit_get_kill_context(u); |
| 4354 |
|
| 4355 |
if (ec->pam_name && kc && !IN_SET(kc->kill_mode, KILL_CONTROL_GROUP, KILL_MIXED)) |
| 4356 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); |
| 4357 |
|
| 4358 |
return 0; |
| 4359 |
} |
| 4360 |
|
| 4361 |
static PrivateTmp unit_get_private_var_tmp(const Unit *u, const ExecContext *c) { |
| 4362 |
assert(u); |
| 4363 |
assert(c); |
| 4364 |
assert(c->private_tmp >= 0 && c->private_tmp < _PRIVATE_TMP_MAX); |
| 4365 |
|
| 4366 |
/* Disable disconnected private tmpfs on /var/tmp/ when DefaultDependencies=no and |
| 4367 |
* RootImage=/RootDirectory= are not set, as /var/ may be a separate partition. |
| 4368 |
* See https://github.com/systemd/systemd/issues/37258. */ |
| 4369 |
|
| 4370 |
/* PrivateTmp=yes/no also enables/disables private tmpfs on /var/tmp/. */ |
| 4371 |
if (c->private_tmp != PRIVATE_TMP_DISCONNECTED) |
| 4372 |
return c->private_tmp; |
| 4373 |
|
| 4374 |
/* When DefaultDependencies=yes, disconnected tmpfs is also enabled on /var/tmp/, and an explicit |
| 4375 |
* dependency to the mount on /var/ will be added in unit_add_exec_dependencies(). */ |
| 4376 |
if (u->default_dependencies) |
| 4377 |
return PRIVATE_TMP_DISCONNECTED; |
| 4378 |
|
| 4379 |
/* When RootImage=/RootDirectory= is enabled, /var/ should be prepared by the image or directory, |
| 4380 |
* hence we can mount a disconnected tmpfs on /var/tmp/. */ |
| 4381 |
if (exec_context_with_rootfs(c)) |
| 4382 |
return PRIVATE_TMP_DISCONNECTED; |
| 4383 |
|
| 4384 |
/* Even if DefaultDependencies=no, enable disconnected tmpfs when |
| 4385 |
* RequiresMountsFor=/WantsMountsFor=/var/ is explicitly set. */ |
| 4386 |
for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; t++) |
| 4387 |
if (hashmap_contains(u->mounts_for[t], "/var/")) |
| 4388 |
return PRIVATE_TMP_DISCONNECTED; |
| 4389 |
|
| 4390 |
/* Check the same but for After=. */ |
| 4391 |
Unit *m = manager_get_unit(u->manager, "var.mount"); |
| 4392 |
if (m && unit_has_dependency(u, UNIT_ATOM_AFTER, m)) |
| 4393 |
return PRIVATE_TMP_DISCONNECTED; |
| 4394 |
|
| 4395 |
return PRIVATE_TMP_NO; |
| 4396 |
} |
| 4397 |
|
| 4398 |
static PrivateTmp unit_get_private_tmp(const Unit *u, const ExecContext *c) { |
| 4399 |
assert(u); |
| 4400 |
assert(c); |
| 4401 |
assert(c->private_tmp >= 0 && c->private_tmp < _PRIVATE_TMP_MAX); |
| 4402 |
|
| 4403 |
/* Upgrade "PrivateTmp=yes" (a.k.a. 'connected') to 'disconnected' when |
| 4404 |
* DefaultDependencies=no and RootImage=/RootDirectory= are not set, as /tmp/ may be a |
| 4405 |
* separate partition. See https://github.com/systemd/systemd/issues/28515. |
| 4406 |
* |
| 4407 |
* Note that the change goes in the opposite direction than unit_get_private_var_tmp() |
| 4408 |
* above. For /var/tmp/, we need to disable the setting, because we don't want to create |
| 4409 |
* the /var/tmp/ directory if /var/ is a mount point. We don't have this problem with |
| 4410 |
* /tmp/ because there is no nesting. */ |
| 4411 |
|
| 4412 |
if (c->private_tmp != PRIVATE_TMP_CONNECTED || |
| 4413 |
u->default_dependencies || |
| 4414 |
exec_context_with_rootfs(c)) |
| 4415 |
return c->private_tmp; |
| 4416 |
|
| 4417 |
/* Even if DefaultDependencies=no, honour tmpfs setting when |
| 4418 |
* RequiresMountsFor=/WantsMountsFor=/tmp/ is explicitly set. */ |
| 4419 |
for (UnitMountDependencyType t = 0; t < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX; t++) |
| 4420 |
if (hashmap_contains(u->mounts_for[t], "/tmp/")) |
| 4421 |
return c->private_tmp; |
| 4422 |
|
| 4423 |
/* Check the same but for After=. */ |
| 4424 |
Unit *m = manager_get_unit(u->manager, "tmp.mount"); |
| 4425 |
if (!m) |
| 4426 |
return c->private_tmp; |
| 4427 |
|
| 4428 |
if (unit_has_dependency(u, UNIT_ATOM_AFTER, m)) |
| 4429 |
return c->private_tmp; |
| 4430 |
|
| 4431 |
return PRIVATE_TMP_DISCONNECTED; |
| 4432 |
} |
| 4433 |
|
| 4434 |
int unit_patch_contexts(Unit *u) { |
| 4435 |
CGroupContext *cc; |
| 4436 |
ExecContext *ec; |
| 4437 |
int r; |
| 4438 |
|
| 4439 |
assert(u); |
| 4440 |
|
| 4441 |
/* Patch in the manager defaults into the exec and cgroup |
| 4442 |
* contexts, _after_ the rest of the settings have been |
| 4443 |
* initialized */ |
| 4444 |
|
| 4445 |
ec = unit_get_exec_context(u); |
| 4446 |
if (ec) { |
| 4447 |
/* This only copies in the ones that need memory */ |
| 4448 |
for (unsigned i = 0; i < _RLIMIT_MAX; i++) |
| 4449 |
if (u->manager->defaults.rlimit[i] && !ec->rlimit[i]) { |
| 4450 |
ec->rlimit[i] = newdup(struct rlimit, u->manager->defaults.rlimit[i], 1); |
| 4451 |
if (!ec->rlimit[i]) |
| 4452 |
return -ENOMEM; |
| 4453 |
} |
| 4454 |
|
| 4455 |
if (MANAGER_IS_USER(u->manager) && !ec->working_directory) { |
| 4456 |
r = get_home_dir(&ec->working_directory); |
| 4457 |
if (r < 0) |
| 4458 |
return r; |
| 4459 |
|
| 4460 |
if (!ec->working_directory_home) |
| 4461 |
/* If home directory is implied by us, allow it to be missing. */ |
| 4462 |
ec->working_directory_missing_ok = true; |
| 4463 |
} |
| 4464 |
|
| 4465 |
if (ec->private_devices) |
| 4466 |
ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) | (UINT64_C(1) << CAP_SYS_RAWIO)); |
| 4467 |
|
| 4468 |
if (ec->protect_kernel_modules) |
| 4469 |
ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE); |
| 4470 |
|
| 4471 |
if (ec->protect_kernel_logs) |
| 4472 |
ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYSLOG); |
| 4473 |
|
| 4474 |
if (ec->protect_clock) |
| 4475 |
ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_SYS_TIME) | (UINT64_C(1) << CAP_WAKE_ALARM)); |
| 4476 |
|
| 4477 |
if (ec->dynamic_user) { |
| 4478 |
if (!ec->user) { |
| 4479 |
r = user_from_unit_name(u, &ec->user); |
| 4480 |
if (r < 0) |
| 4481 |
return r; |
| 4482 |
} |
| 4483 |
|
| 4484 |
if (!ec->group) { |
| 4485 |
ec->group = strdup(ec->user); |
| 4486 |
if (!ec->group) |
| 4487 |
return -ENOMEM; |
| 4488 |
} |
| 4489 |
|
| 4490 |
/* If the dynamic user option is on, let's make sure that the unit can't leave its |
| 4491 |
* UID/GID around in the file system or on IPC objects. Hence enforce a strict |
| 4492 |
* sandbox. */ |
| 4493 |
|
| 4494 |
/* With DynamicUser= we want private directories, so if the user hasn't manually |
| 4495 |
* selected PrivateTmp=, enable it, but to a fully private (disconnected) tmpfs |
| 4496 |
* instance. */ |
| 4497 |
if (ec->private_tmp == PRIVATE_TMP_NO) |
| 4498 |
ec->private_tmp = PRIVATE_TMP_DISCONNECTED; |
| 4499 |
ec->remove_ipc = true; |
| 4500 |
ec->protect_system = PROTECT_SYSTEM_STRICT; |
| 4501 |
if (ec->protect_home == PROTECT_HOME_NO) |
| 4502 |
ec->protect_home = PROTECT_HOME_READ_ONLY; |
| 4503 |
|
| 4504 |
/* Make sure this service can neither benefit from SUID/SGID binaries nor create |
| 4505 |
* them. */ |
| 4506 |
ec->no_new_privileges = true; |
| 4507 |
ec->restrict_suid_sgid = true; |
| 4508 |
} |
| 4509 |
|
| 4510 |
/* Table of possible combinations: |
| 4511 |
* /var/tmp /tmp |
| 4512 |
* PrivateTmp=no no no |
| 4513 |
* PrivateTmp=connected connected connected,disconnected |
| 4514 |
* PrivateTmp=disconnected disconnected,no disconnected |
| 4515 |
*/ |
| 4516 |
ec->private_var_tmp = unit_get_private_var_tmp(u, ec); |
| 4517 |
ec->private_tmp = unit_get_private_tmp(u, ec); |
| 4518 |
|
| 4519 |
FOREACH_ARRAY(d, ec->directories, _EXEC_DIRECTORY_TYPE_MAX) |
| 4520 |
exec_directory_sort(d); |
| 4521 |
} |
| 4522 |
|
| 4523 |
cc = unit_get_cgroup_context(u); |
| 4524 |
if (cc && ec) { |
| 4525 |
|
| 4526 |
if (ec->private_devices && |
| 4527 |
cc->device_policy == CGROUP_DEVICE_POLICY_AUTO) |
| 4528 |
cc->device_policy = CGROUP_DEVICE_POLICY_CLOSED; |
| 4529 |
|
| 4530 |
/* Only add these if needed, as they imply that everything else is blocked. */ |
| 4531 |
if (cgroup_context_has_device_policy(cc)) { |
| 4532 |
if (ec->root_image || ec->mount_images || ec->root_mstack) { |
| 4533 |
|
| 4534 |
/* When RootImage= or MountImages= is specified, the following devices are |
| 4535 |
* touched. For RootMStack= there's the possibility the are touched. */ |
| 4536 |
FOREACH_STRING(p, "/dev/loop-control", "/dev/mapper/control") { |
| 4537 |
r = cgroup_context_add_device_allow(cc, p, CGROUP_DEVICE_READ|CGROUP_DEVICE_WRITE); |
| 4538 |
if (r < 0) |
| 4539 |
return r; |
| 4540 |
} |
| 4541 |
FOREACH_STRING(p, "block-loop", "block-blkext", "block-device-mapper") { |
| 4542 |
r = cgroup_context_add_device_allow(cc, p, CGROUP_DEVICE_READ|CGROUP_DEVICE_WRITE|CGROUP_DEVICE_MKNOD); |
| 4543 |
if (r < 0) |
| 4544 |
return r; |
| 4545 |
} |
| 4546 |
|
| 4547 |
/* Make sure "block-loop" can be resolved, i.e. make sure "loop" shows up in /proc/devices. |
| 4548 |
* Same for mapper and verity. */ |
| 4549 |
FOREACH_STRING(p, "[email protected]", "modprobe@dm_mod.service", "modprobe@dm_verity.service") { |
| 4550 |
r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, p, true, UNIT_DEPENDENCY_FILE); |
| 4551 |
if (r < 0) |
| 4552 |
return r; |
| 4553 |
} |
| 4554 |
} |
| 4555 |
|
| 4556 |
if (ec->protect_clock) { |
| 4557 |
r = cgroup_context_add_device_allow(cc, "char-rtc", CGROUP_DEVICE_READ); |
| 4558 |
if (r < 0) |
| 4559 |
return r; |
| 4560 |
} |
| 4561 |
} |
| 4562 |
} |
| 4563 |
|
| 4564 |
return unit_verify_contexts(u); |
| 4565 |
} |
| 4566 |
|
| 4567 |
ExecContext *unit_get_exec_context(const Unit *u) { |
| 4568 |
size_t offset; |
| 4569 |
assert(u); |
| 4570 |
|
| 4571 |
if (u->type < 0) |
| 4572 |
return NULL; |
| 4573 |
|
| 4574 |
offset = UNIT_VTABLE(u)->exec_context_offset; |
| 4575 |
if (offset <= 0) |
| 4576 |
return NULL; |
| 4577 |
|
| 4578 |
return (ExecContext*) ((uint8_t*) u + offset); |
| 4579 |
} |
| 4580 |
|
| 4581 |
KillContext *unit_get_kill_context(const Unit *u) { |
| 4582 |
size_t offset; |
| 4583 |
assert(u); |
| 4584 |
|
| 4585 |
if (u->type < 0) |
| 4586 |
return NULL; |
| 4587 |
|
| 4588 |
offset = UNIT_VTABLE(u)->kill_context_offset; |
| 4589 |
if (offset <= 0) |
| 4590 |
return NULL; |
| 4591 |
|
| 4592 |
return (KillContext*) ((uint8_t*) u + offset); |
| 4593 |
} |
| 4594 |
|
| 4595 |
CGroupContext *unit_get_cgroup_context(const Unit *u) { |
| 4596 |
size_t offset; |
| 4597 |
|
| 4598 |
if (u->type < 0) |
| 4599 |
return NULL; |
| 4600 |
|
| 4601 |
offset = UNIT_VTABLE(u)->cgroup_context_offset; |
| 4602 |
if (offset <= 0) |
| 4603 |
return NULL; |
| 4604 |
|
| 4605 |
return (CGroupContext*) ((uint8_t*) u + offset); |
| 4606 |
} |
| 4607 |
|
| 4608 |
ExecRuntime *unit_get_exec_runtime(const Unit *u) { |
| 4609 |
size_t offset; |
| 4610 |
|
| 4611 |
if (u->type < 0) |
| 4612 |
return NULL; |
| 4613 |
|
| 4614 |
offset = UNIT_VTABLE(u)->exec_runtime_offset; |
| 4615 |
if (offset <= 0) |
| 4616 |
return NULL; |
| 4617 |
|
| 4618 |
return *(ExecRuntime**) ((uint8_t*) u + offset); |
| 4619 |
} |
| 4620 |
|
| 4621 |
CGroupRuntime *unit_get_cgroup_runtime(const Unit *u) { |
| 4622 |
size_t offset; |
| 4623 |
|
| 4624 |
if (u->type < 0) |
| 4625 |
return NULL; |
| 4626 |
|
| 4627 |
offset = UNIT_VTABLE(u)->cgroup_runtime_offset; |
| 4628 |
if (offset <= 0) |
| 4629 |
return NULL; |
| 4630 |
|
| 4631 |
return *(CGroupRuntime**) ((uint8_t*) u + offset); |
| 4632 |
} |
| 4633 |
|
| 4634 |
static const char* unit_drop_in_dir(Unit *u, UnitWriteFlags flags) { |
| 4635 |
assert(u); |
| 4636 |
|
| 4637 |
if (UNIT_WRITE_FLAGS_NOOP(flags)) |
| 4638 |
return NULL; |
| 4639 |
|
| 4640 |
if (u->transient) /* Redirect drop-ins for transient units always into the transient directory. */ |
| 4641 |
return u->manager->lookup_paths.transient; |
| 4642 |
|
| 4643 |
if (flags & UNIT_PERSISTENT) |
| 4644 |
return u->manager->lookup_paths.persistent_control; |
| 4645 |
|
| 4646 |
if (flags & UNIT_RUNTIME) |
| 4647 |
return u->manager->lookup_paths.runtime_control; |
| 4648 |
|
| 4649 |
return NULL; |
| 4650 |
} |
| 4651 |
|
| 4652 |
const char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) { |
| 4653 |
assert(s); |
| 4654 |
assert(popcount(flags & (UNIT_ESCAPE_EXEC_SYNTAX_ENV | UNIT_ESCAPE_EXEC_SYNTAX | UNIT_ESCAPE_C)) <= 1); |
| 4655 |
assert(buf); |
| 4656 |
|
| 4657 |
_cleanup_free_ char *t = NULL; |
| 4658 |
|
| 4659 |
/* Returns a string with any escaping done. If no escaping was necessary, *buf is set to NULL, and |
| 4660 |
* the input pointer is returned as-is. If an allocation was needed, the return buffer pointer is |
| 4661 |
* written to *buf. This means the return value always contains a properly escaped version, but *buf |
| 4662 |
* only contains a pointer if an allocation was made. Callers can use this to optimize memory |
| 4663 |
* allocations. */ |
| 4664 |
|
| 4665 |
if (flags & UNIT_ESCAPE_SPECIFIERS) { |
| 4666 |
t = specifier_escape(s); |
| 4667 |
if (!t) |
| 4668 |
return NULL; |
| 4669 |
|
| 4670 |
s = t; |
| 4671 |
} |
| 4672 |
|
| 4673 |
/* We either do C-escaping or shell-escaping, to additionally escape characters that we parse for |
| 4674 |
* ExecStart= and friends, i.e. '$' and quotes. */ |
| 4675 |
|
| 4676 |
if (flags & (UNIT_ESCAPE_EXEC_SYNTAX_ENV | UNIT_ESCAPE_EXEC_SYNTAX)) { |
| 4677 |
char *t2; |
| 4678 |
|
| 4679 |
if (flags & UNIT_ESCAPE_EXEC_SYNTAX_ENV) { |
| 4680 |
t2 = strreplace(s, "$", "$$"); |
| 4681 |
if (!t2) |
| 4682 |
return NULL; |
| 4683 |
free_and_replace(t, t2); |
| 4684 |
} |
| 4685 |
|
| 4686 |
t2 = shell_escape(t ?: s, "\""); |
| 4687 |
if (!t2) |
| 4688 |
return NULL; |
| 4689 |
free_and_replace(t, t2); |
| 4690 |
|
| 4691 |
s = t; |
| 4692 |
|
| 4693 |
} else if (flags & UNIT_ESCAPE_C) { |
| 4694 |
char *t2; |
| 4695 |
|
| 4696 |
t2 = cescape(s); |
| 4697 |
if (!t2) |
| 4698 |
return NULL; |
| 4699 |
free_and_replace(t, t2); |
| 4700 |
|
| 4701 |
s = t; |
| 4702 |
} |
| 4703 |
|
| 4704 |
*buf = TAKE_PTR(t); |
| 4705 |
return s; |
| 4706 |
} |
| 4707 |
|
| 4708 |
char* unit_concat_strv(char **l, UnitWriteFlags flags) { |
| 4709 |
_cleanup_free_ char *result = NULL; |
| 4710 |
size_t n = 0; |
| 4711 |
|
| 4712 |
/* Takes a list of strings, escapes them, and concatenates them. This may be used to format command |
| 4713 |
* lines in a way suitable for ExecStart= stanzas. */ |
| 4714 |
|
| 4715 |
STRV_FOREACH(i, l) { |
| 4716 |
_cleanup_free_ char *buf = NULL; |
| 4717 |
const char *p; |
| 4718 |
size_t a; |
| 4719 |
char *q; |
| 4720 |
|
| 4721 |
p = unit_escape_setting(*i, flags, &buf); |
| 4722 |
if (!p) |
| 4723 |
return NULL; |
| 4724 |
|
| 4725 |
a = (n > 0) + 1 + strlen(p) + 1; /* separating space + " + entry + " */ |
| 4726 |
if (!GREEDY_REALLOC(result, n + a + 1)) |
| 4727 |
return NULL; |
| 4728 |
|
| 4729 |
q = result + n; |
| 4730 |
if (n > 0) |
| 4731 |
*(q++) = ' '; |
| 4732 |
|
| 4733 |
*(q++) = '"'; |
| 4734 |
q = stpcpy(q, p); |
| 4735 |
*(q++) = '"'; |
| 4736 |
|
| 4737 |
n += a; |
| 4738 |
} |
| 4739 |
|
| 4740 |
if (!GREEDY_REALLOC(result, n + 1)) |
| 4741 |
return NULL; |
| 4742 |
|
| 4743 |
result[n] = 0; |
| 4744 |
|
| 4745 |
return TAKE_PTR(result); |
| 4746 |
} |
| 4747 |
|
| 4748 |
int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) { |
| 4749 |
_cleanup_free_ char *p = NULL, *q = NULL, *escaped = NULL; |
| 4750 |
const char *dir, *wrapped; |
| 4751 |
int r; |
| 4752 |
|
| 4753 |
assert(u); |
| 4754 |
assert(name); |
| 4755 |
assert(data); |
| 4756 |
|
| 4757 |
if (UNIT_WRITE_FLAGS_NOOP(flags)) |
| 4758 |
return 0; |
| 4759 |
|
| 4760 |
data = unit_escape_setting(data, flags, &escaped); |
| 4761 |
if (!data) |
| 4762 |
return -ENOMEM; |
| 4763 |
|
| 4764 |
/* Prefix the section header. If we are writing this out as transient file, then let's suppress this if the |
| 4765 |
* previous section header is the same */ |
| 4766 |
|
| 4767 |
if (flags & UNIT_PRIVATE) { |
| 4768 |
if (!UNIT_VTABLE(u)->private_section) |
| 4769 |
return -EINVAL; |
| 4770 |
|
| 4771 |
if (!u->transient_file || u->last_section_private < 0) |
| 4772 |
data = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data); |
| 4773 |
else if (u->last_section_private == 0) |
| 4774 |
data = strjoina("\n[", UNIT_VTABLE(u)->private_section, "]\n", data); |
| 4775 |
} else { |
| 4776 |
if (!u->transient_file || u->last_section_private < 0) |
| 4777 |
data = strjoina("[Unit]\n", data); |
| 4778 |
else if (u->last_section_private > 0) |
| 4779 |
data = strjoina("\n[Unit]\n", data); |
| 4780 |
} |
| 4781 |
|
| 4782 |
if (u->transient_file) { |
| 4783 |
/* When this is a transient unit file in creation, then let's not create a new drop-in, |
| 4784 |
* but instead write to the transient unit file. */ |
| 4785 |
fputs_with_newline(u->transient_file, data); |
| 4786 |
|
| 4787 |
/* Remember which section we wrote this entry to */ |
| 4788 |
u->last_section_private = !!(flags & UNIT_PRIVATE); |
| 4789 |
return 0; |
| 4790 |
} |
| 4791 |
|
| 4792 |
dir = unit_drop_in_dir(u, flags); |
| 4793 |
if (!dir) |
| 4794 |
return -EINVAL; |
| 4795 |
|
| 4796 |
wrapped = strjoina("# This is a drop-in unit file extension, created via \"systemctl set-property\"\n" |
| 4797 |
"# or an equivalent operation. Do not edit.\n", |
| 4798 |
data, |
| 4799 |
"\n"); |
| 4800 |
|
| 4801 |
r = drop_in_file(dir, u->id, 50, name, &p, &q); |
| 4802 |
if (r < 0) |
| 4803 |
return r; |
| 4804 |
|
| 4805 |
(void) mkdir_p_label(p, 0755); |
| 4806 |
|
| 4807 |
/* Make sure the drop-in dir is registered in our path cache. This way we don't need to stupidly |
| 4808 |
* recreate the cache after every drop-in we write. */ |
| 4809 |
if (u->manager->unit_path_cache) { |
| 4810 |
r = set_put_strdup_full(&u->manager->unit_path_cache, &path_hash_ops_free, p); |
| 4811 |
if (r < 0) |
| 4812 |
return r; |
| 4813 |
} |
| 4814 |
|
| 4815 |
r = write_string_file(q, wrapped, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL); |
| 4816 |
if (r < 0) |
| 4817 |
return r; |
| 4818 |
|
| 4819 |
_cleanup_strv_free_ char **dropins = NULL; |
| 4820 |
r = unit_find_dropin_paths(u, /* use_unit_path_cache= */ true, &dropins); |
| 4821 |
if (r < 0) |
| 4822 |
return r; |
| 4823 |
strv_free_and_replace(u->dropin_paths, dropins); |
| 4824 |
|
| 4825 |
u->dropin_mtime = now(CLOCK_REALTIME); |
| 4826 |
|
| 4827 |
return 0; |
| 4828 |
} |
| 4829 |
|
| 4830 |
int unit_write_settingf(Unit *u, UnitWriteFlags flags, const char *name, const char *format, ...) { |
| 4831 |
_cleanup_free_ char *p = NULL; |
| 4832 |
va_list ap; |
| 4833 |
int r; |
| 4834 |
|
| 4835 |
assert(u); |
| 4836 |
assert(name); |
| 4837 |
assert(format); |
| 4838 |
|
| 4839 |
if (UNIT_WRITE_FLAGS_NOOP(flags)) |
| 4840 |
return 0; |
| 4841 |
|
| 4842 |
va_start(ap, format); |
| 4843 |
r = vasprintf(&p, format, ap); |
| 4844 |
va_end(ap); |
| 4845 |
|
| 4846 |
if (r < 0) |
| 4847 |
return -ENOMEM; |
| 4848 |
|
| 4849 |
return unit_write_setting(u, flags, name, p); |
| 4850 |
} |
| 4851 |
|
| 4852 |
int unit_make_transient(Unit *u) { |
| 4853 |
_cleanup_free_ char *path = NULL; |
| 4854 |
FILE *f; |
| 4855 |
|
| 4856 |
assert(u); |
| 4857 |
|
| 4858 |
if (!UNIT_VTABLE(u)->can_transient) |
| 4859 |
return -EOPNOTSUPP; |
| 4860 |
|
| 4861 |
(void) mkdir_p_label(u->manager->lookup_paths.transient, 0755); |
| 4862 |
|
| 4863 |
path = path_join(u->manager->lookup_paths.transient, u->id); |
| 4864 |
if (!path) |
| 4865 |
return -ENOMEM; |
| 4866 |
|
| 4867 |
/* Let's open the file we'll write the transient settings into. This file is kept open as long as we are |
| 4868 |
* creating the transient, and is closed in unit_load(), as soon as we start loading the file. */ |
| 4869 |
|
| 4870 |
WITH_UMASK(0022) { |
| 4871 |
f = fopen(path, "we"); |
| 4872 |
if (!f) |
| 4873 |
return -errno; |
| 4874 |
} |
| 4875 |
|
| 4876 |
safe_fclose(u->transient_file); |
| 4877 |
u->transient_file = f; |
| 4878 |
|
| 4879 |
free_and_replace(u->fragment_path, path); |
| 4880 |
|
| 4881 |
u->source_path = mfree(u->source_path); |
| 4882 |
u->dropin_paths = strv_free(u->dropin_paths); |
| 4883 |
u->fragment_mtime = u->source_mtime = u->dropin_mtime = 0; |
| 4884 |
|
| 4885 |
u->load_state = UNIT_STUB; |
| 4886 |
u->load_error = 0; |
| 4887 |
u->transient = true; |
| 4888 |
|
| 4889 |
unit_add_to_dbus_queue(u); |
| 4890 |
unit_add_to_gc_queue(u); |
| 4891 |
|
| 4892 |
fputs("# This is a transient unit file, created programmatically via the systemd API. Do not edit.\n", |
| 4893 |
u->transient_file); |
| 4894 |
|
| 4895 |
return 0; |
| 4896 |
} |
| 4897 |
|
| 4898 |
int manager_setup_transient_unit(Manager *m, const char *name, Unit **ret, sd_bus_error *reterr_error) { |
| 4899 |
Unit *u; |
| 4900 |
int r; |
| 4901 |
|
| 4902 |
assert(m); |
| 4903 |
assert(name); |
| 4904 |
assert(ret); |
| 4905 |
|
| 4906 |
UnitType t = unit_name_to_type(name); |
| 4907 |
if (t < 0) |
| 4908 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, |
| 4909 |
"Invalid unit name or type: %s", name); |
| 4910 |
|
| 4911 |
if (!unit_vtable[t]->can_transient) |
| 4912 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, |
| 4913 |
"Unit type %s does not support transient units.", |
| 4914 |
unit_type_to_string(t)); |
| 4915 |
|
| 4916 |
r = manager_load_unit(m, name, /* path= */ NULL, reterr_error, &u); |
| 4917 |
if (r < 0) |
| 4918 |
return r; |
| 4919 |
|
| 4920 |
if (!unit_is_pristine(u)) |
| 4921 |
return sd_bus_error_setf(reterr_error, BUS_ERROR_UNIT_EXISTS, |
| 4922 |
"Unit %s was already loaded or has a fragment file.", name); |
| 4923 |
|
| 4924 |
/* OK, the unit failed to load and is unreferenced, now let's |
| 4925 |
* fill in the transient data instead */ |
| 4926 |
r = unit_make_transient(u); |
| 4927 |
if (r < 0) |
| 4928 |
return r; |
| 4929 |
|
| 4930 |
*ret = u; |
| 4931 |
return 0; |
| 4932 |
} |
| 4933 |
|
| 4934 |
static bool ignore_leftover_process(const char *comm) { |
| 4935 |
return comm && comm[0] == '('; /* Most likely our own helper process (PAM?), ignore */ |
| 4936 |
} |
| 4937 |
|
| 4938 |
static int log_kill(const PidRef *pid, int sig, void *userdata) { |
| 4939 |
const Unit *u = ASSERT_PTR(userdata); |
| 4940 |
_cleanup_free_ char *comm = NULL; |
| 4941 |
|
| 4942 |
assert(pidref_is_set(pid)); |
| 4943 |
|
| 4944 |
(void) pidref_get_comm(pid, &comm); |
| 4945 |
|
| 4946 |
if (ignore_leftover_process(comm)) |
| 4947 |
/* Although we didn't log anything, as this callback is used in unit_kill_context we must return 1 |
| 4948 |
* here to let the manager know that a process was killed. */ |
| 4949 |
return 1; |
| 4950 |
|
| 4951 |
log_unit_notice(u, |
| 4952 |
"Killing process " PID_FMT " (%s) with signal SIG%s.", |
| 4953 |
pid->pid, |
| 4954 |
strna(comm), |
| 4955 |
signal_to_string(sig)); |
| 4956 |
|
| 4957 |
return 1; |
| 4958 |
} |
| 4959 |
|
| 4960 |
static int operation_to_signal( |
| 4961 |
const KillContext *c, |
| 4962 |
KillOperation k, |
| 4963 |
bool *ret_noteworthy) { |
| 4964 |
|
| 4965 |
assert(c); |
| 4966 |
assert(ret_noteworthy); |
| 4967 |
|
| 4968 |
switch (k) { |
| 4969 |
|
| 4970 |
case KILL_TERMINATE: |
| 4971 |
case KILL_TERMINATE_AND_LOG: |
| 4972 |
*ret_noteworthy = k == KILL_TERMINATE_AND_LOG; |
| 4973 |
return c->kill_signal; |
| 4974 |
|
| 4975 |
case KILL_RESTART: |
| 4976 |
*ret_noteworthy = false; |
| 4977 |
return restart_kill_signal(c); |
| 4978 |
|
| 4979 |
case KILL_KILL: |
| 4980 |
*ret_noteworthy = true; |
| 4981 |
return c->final_kill_signal; |
| 4982 |
|
| 4983 |
case KILL_WATCHDOG: |
| 4984 |
*ret_noteworthy = true; |
| 4985 |
return c->watchdog_signal; |
| 4986 |
|
| 4987 |
default: |
| 4988 |
assert_not_reached(); |
| 4989 |
} |
| 4990 |
} |
| 4991 |
|
| 4992 |
static int unit_kill_context_one( |
| 4993 |
Unit *u, |
| 4994 |
const PidRef *pidref, |
| 4995 |
const char *type, |
| 4996 |
bool is_alien, |
| 4997 |
int sig, |
| 4998 |
bool send_sighup, |
| 4999 |
cg_kill_log_func_t log_func) { |
| 5000 |
|
| 5001 |
int r; |
| 5002 |
|
| 5003 |
assert(u); |
| 5004 |
assert(type); |
| 5005 |
|
| 5006 |
/* This returns > 0 if it makes sense to wait for SIGCHLD for the process, == 0 if not. */ |
| 5007 |
|
| 5008 |
if (!pidref_is_set(pidref)) |
| 5009 |
return 0; |
| 5010 |
|
| 5011 |
if (log_func) |
| 5012 |
log_func(pidref, sig, u); |
| 5013 |
|
| 5014 |
r = pidref_kill_and_sigcont(pidref, sig); |
| 5015 |
if (r == -ESRCH) |
| 5016 |
return !is_alien; |
| 5017 |
if (r < 0) { |
| 5018 |
_cleanup_free_ char *comm = NULL; |
| 5019 |
|
| 5020 |
(void) pidref_get_comm(pidref, &comm); |
| 5021 |
return log_unit_warning_errno(u, r, "Failed to kill %s process " PID_FMT " (%s), ignoring: %m", type, pidref->pid, strna(comm)); |
| 5022 |
} |
| 5023 |
|
| 5024 |
if (send_sighup) |
| 5025 |
(void) pidref_kill(pidref, SIGHUP); |
| 5026 |
|
| 5027 |
return !is_alien; |
| 5028 |
} |
| 5029 |
|
| 5030 |
int unit_kill_context(Unit *u, KillOperation k) { |
| 5031 |
bool wait_for_exit = false, send_sighup; |
| 5032 |
cg_kill_log_func_t log_func = NULL; |
| 5033 |
int sig, r; |
| 5034 |
|
| 5035 |
assert(u); |
| 5036 |
|
| 5037 |
/* Kill the processes belonging to this unit, in preparation for shutting the unit down. Returns > 0 |
| 5038 |
* if we killed something worth waiting for, 0 otherwise. Do not confuse with unit_kill_common() |
| 5039 |
* which is used for user-requested killing of unit processes. */ |
| 5040 |
|
| 5041 |
KillContext *c = unit_get_kill_context(u); |
| 5042 |
if (!c || c->kill_mode == KILL_NONE) |
| 5043 |
return 0; |
| 5044 |
|
| 5045 |
bool noteworthy; |
| 5046 |
sig = operation_to_signal(c, k, ¬eworthy); |
| 5047 |
if (noteworthy) |
| 5048 |
log_func = log_kill; |
| 5049 |
|
| 5050 |
send_sighup = |
| 5051 |
c->send_sighup && |
| 5052 |
IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) && |
| 5053 |
sig != SIGHUP; |
| 5054 |
|
| 5055 |
bool is_alien; |
| 5056 |
PidRef *main_pid = unit_main_pid_full(u, &is_alien); |
| 5057 |
r = unit_kill_context_one(u, main_pid, "main", is_alien, sig, send_sighup, log_func); |
| 5058 |
wait_for_exit = wait_for_exit || r > 0; |
| 5059 |
|
| 5060 |
r = unit_kill_context_one(u, unit_control_pid(u), "control", /* is_alien= */ false, sig, send_sighup, log_func); |
| 5061 |
wait_for_exit = wait_for_exit || r > 0; |
| 5062 |
|
| 5063 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 5064 |
if (crt && crt->cgroup_path && |
| 5065 |
(c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) { |
| 5066 |
_cleanup_set_free_ Set *pid_set = NULL; |
| 5067 |
|
| 5068 |
/* Exclude the main/control pids from being killed via the cgroup */ |
| 5069 |
r = unit_pid_set(u, &pid_set); |
| 5070 |
if (r < 0) |
| 5071 |
return r; |
| 5072 |
|
| 5073 |
r = cg_kill_recursive( |
| 5074 |
crt->cgroup_path, |
| 5075 |
sig, |
| 5076 |
CGROUP_SIGCONT|CGROUP_IGNORE_SELF, |
| 5077 |
pid_set, |
| 5078 |
log_func, u); |
| 5079 |
if (r < 0) { |
| 5080 |
if (!IN_SET(r, -EAGAIN, -ESRCH, -ENOENT)) |
| 5081 |
log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", empty_to_root(crt->cgroup_path)); |
| 5082 |
|
| 5083 |
} else if (r > 0) { |
| 5084 |
|
| 5085 |
wait_for_exit = true; |
| 5086 |
|
| 5087 |
if (send_sighup) { |
| 5088 |
r = unit_pid_set(u, &pid_set); |
| 5089 |
if (r < 0) |
| 5090 |
return r; |
| 5091 |
|
| 5092 |
(void) cg_kill_recursive( |
| 5093 |
crt->cgroup_path, |
| 5094 |
SIGHUP, |
| 5095 |
CGROUP_IGNORE_SELF, |
| 5096 |
pid_set, |
| 5097 |
/* log_kill= */ NULL, |
| 5098 |
/* userdata= */ NULL); |
| 5099 |
} |
| 5100 |
} |
| 5101 |
} |
| 5102 |
|
| 5103 |
return wait_for_exit; |
| 5104 |
} |
| 5105 |
|
| 5106 |
int unit_add_mounts_for(Unit *u, const char *path, UnitDependencyMask mask, UnitMountDependencyType type) { |
| 5107 |
Hashmap **unit_map, **manager_map; |
| 5108 |
int r; |
| 5109 |
|
| 5110 |
assert(u); |
| 5111 |
assert(path); |
| 5112 |
assert(type >= 0 && type < _UNIT_MOUNT_DEPENDENCY_TYPE_MAX); |
| 5113 |
|
| 5114 |
unit_map = &u->mounts_for[type]; |
| 5115 |
manager_map = &u->manager->units_needing_mounts_for[type]; |
| 5116 |
|
| 5117 |
/* Registers a unit for requiring a certain path and all its prefixes. We keep a hashtable of these |
| 5118 |
* paths in the unit (from the path to the UnitDependencyInfo structure indicating how to the |
| 5119 |
* dependency came to be). However, we build a prefix table for all possible prefixes so that new |
| 5120 |
* appearing mount units can easily determine which units to make themselves a dependency of. */ |
| 5121 |
|
| 5122 |
if (!path_is_absolute(path)) |
| 5123 |
return -EINVAL; |
| 5124 |
|
| 5125 |
if (hashmap_contains(*unit_map, path)) /* Exit quickly if the path is already covered. */ |
| 5126 |
return 0; |
| 5127 |
|
| 5128 |
if (!unit_type_supported(UNIT_MOUNT)) { |
| 5129 |
log_once(LOG_NOTICE, "Mount unit not supported, skipping *MountsFor= dependencies."); |
| 5130 |
return 0; |
| 5131 |
} |
| 5132 |
|
| 5133 |
/* Use the canonical form of the path as the stored key. We call path_is_normalized() |
| 5134 |
* only after simplification, since path_is_normalized() rejects paths with '.'. |
| 5135 |
* path_is_normalized() also verifies that the path fits in PATH_MAX. */ |
| 5136 |
_cleanup_free_ char *p = NULL; |
| 5137 |
r = path_simplify_alloc(path, &p); |
| 5138 |
if (r < 0) |
| 5139 |
return r; |
| 5140 |
path = p; |
| 5141 |
|
| 5142 |
if (!path_is_normalized(path)) |
| 5143 |
return -EPERM; |
| 5144 |
|
| 5145 |
UnitDependencyInfo di = { |
| 5146 |
.origin_mask = mask |
| 5147 |
}; |
| 5148 |
|
| 5149 |
r = hashmap_ensure_put(unit_map, &path_hash_ops, p, di.data); |
| 5150 |
if (r < 0) |
| 5151 |
return r; |
| 5152 |
assert(r > 0); |
| 5153 |
TAKE_PTR(p); /* path remains a valid pointer to the string stored in the hashmap */ |
| 5154 |
|
| 5155 |
char prefix[strlen(path) + 1]; |
| 5156 |
PATH_FOREACH_PREFIX_MORE(prefix, path) { |
| 5157 |
Set *x; |
| 5158 |
|
| 5159 |
x = hashmap_get(*manager_map, prefix); |
| 5160 |
if (!x) { |
| 5161 |
_cleanup_free_ char *q = NULL; |
| 5162 |
|
| 5163 |
r = hashmap_ensure_allocated(manager_map, &path_hash_ops); |
| 5164 |
if (r < 0) |
| 5165 |
return r; |
| 5166 |
|
| 5167 |
q = strdup(prefix); |
| 5168 |
if (!q) |
| 5169 |
return -ENOMEM; |
| 5170 |
|
| 5171 |
x = set_new(NULL); |
| 5172 |
if (!x) |
| 5173 |
return -ENOMEM; |
| 5174 |
|
| 5175 |
r = hashmap_put(*manager_map, q, x); |
| 5176 |
if (r < 0) { |
| 5177 |
set_free(x); |
| 5178 |
return r; |
| 5179 |
} |
| 5180 |
q = NULL; |
| 5181 |
} |
| 5182 |
|
| 5183 |
r = set_put(x, u); |
| 5184 |
if (r < 0) |
| 5185 |
return r; |
| 5186 |
} |
| 5187 |
|
| 5188 |
return 0; |
| 5189 |
} |
| 5190 |
|
| 5191 |
int unit_setup_exec_runtime(Unit *u) { |
| 5192 |
_cleanup_(exec_shared_runtime_unrefp) ExecSharedRuntime *esr = NULL; |
| 5193 |
_cleanup_(dynamic_creds_unrefp) DynamicCreds *dcreds = NULL; |
| 5194 |
_cleanup_set_free_ Set *units = NULL; |
| 5195 |
ExecRuntime **rt; |
| 5196 |
ExecContext *ec; |
| 5197 |
size_t offset; |
| 5198 |
Unit *other; |
| 5199 |
int r; |
| 5200 |
|
| 5201 |
offset = UNIT_VTABLE(u)->exec_runtime_offset; |
| 5202 |
assert(offset > 0); |
| 5203 |
|
| 5204 |
/* Check if there already is an ExecRuntime for this unit? */ |
| 5205 |
rt = (ExecRuntime**) ((uint8_t*) u + offset); |
| 5206 |
if (*rt) |
| 5207 |
return 0; |
| 5208 |
|
| 5209 |
ec = ASSERT_PTR(unit_get_exec_context(u)); |
| 5210 |
|
| 5211 |
r = unit_get_transitive_dependency_set(u, UNIT_ATOM_JOINS_NAMESPACE_OF, &units); |
| 5212 |
if (r < 0) |
| 5213 |
return r; |
| 5214 |
|
| 5215 |
/* Try to get it from somebody else */ |
| 5216 |
SET_FOREACH(other, units) { |
| 5217 |
r = exec_shared_runtime_acquire(u->manager, NULL, other->id, false, &esr); |
| 5218 |
if (r < 0) |
| 5219 |
return r; |
| 5220 |
if (r > 0) |
| 5221 |
break; |
| 5222 |
} |
| 5223 |
|
| 5224 |
if (!esr) { |
| 5225 |
r = exec_shared_runtime_acquire(u->manager, ec, u->id, true, &esr); |
| 5226 |
if (r < 0) |
| 5227 |
return r; |
| 5228 |
} |
| 5229 |
|
| 5230 |
if (ec->dynamic_user) { |
| 5231 |
r = dynamic_creds_make(u->manager, ec->user, ec->group, &dcreds); |
| 5232 |
if (r < 0) |
| 5233 |
return r; |
| 5234 |
} |
| 5235 |
|
| 5236 |
r = exec_runtime_make(u, ec, esr, dcreds, rt); |
| 5237 |
if (r < 0) |
| 5238 |
return r; |
| 5239 |
|
| 5240 |
TAKE_PTR(esr); |
| 5241 |
TAKE_PTR(dcreds); |
| 5242 |
|
| 5243 |
return r; |
| 5244 |
} |
| 5245 |
|
| 5246 |
CGroupRuntime* unit_setup_cgroup_runtime(Unit *u) { |
| 5247 |
assert(u); |
| 5248 |
assert(UNIT_HAS_CGROUP_CONTEXT(u)); |
| 5249 |
|
| 5250 |
size_t offset = UNIT_VTABLE(u)->cgroup_runtime_offset; |
| 5251 |
assert(offset > 0); |
| 5252 |
|
| 5253 |
CGroupRuntime **rt = (CGroupRuntime**) ((uint8_t*) u + offset); |
| 5254 |
if (*rt) |
| 5255 |
return *rt; |
| 5256 |
|
| 5257 |
return (*rt = cgroup_runtime_new()); |
| 5258 |
} |
| 5259 |
|
| 5260 |
bool unit_type_supported(UnitType t) { |
| 5261 |
static int8_t cache[_UNIT_TYPE_MAX] = {}; /* -1: disabled, 1: enabled: 0: don't know */ |
| 5262 |
int r; |
| 5263 |
|
| 5264 |
assert(t >= 0 && t < _UNIT_TYPE_MAX); |
| 5265 |
|
| 5266 |
if (cache[t] == 0) { |
| 5267 |
char *e; |
| 5268 |
|
| 5269 |
e = strjoina("SYSTEMD_SUPPORT_", unit_type_to_string(t)); |
| 5270 |
|
| 5271 |
r = getenv_bool(ascii_strupper(e)); |
| 5272 |
if (r < 0 && r != -ENXIO) |
| 5273 |
log_debug_errno(r, "Failed to parse $%s, ignoring: %m", e); |
| 5274 |
|
| 5275 |
cache[t] = r == 0 ? -1 : 1; |
| 5276 |
} |
| 5277 |
if (cache[t] < 0) |
| 5278 |
return false; |
| 5279 |
|
| 5280 |
if (!unit_vtable[t]->supported) |
| 5281 |
return true; |
| 5282 |
|
| 5283 |
return unit_vtable[t]->supported(); |
| 5284 |
} |
| 5285 |
|
| 5286 |
void unit_warn_if_dir_nonempty(Unit *u, const char* where) { |
| 5287 |
int r; |
| 5288 |
|
| 5289 |
assert(u); |
| 5290 |
assert(where); |
| 5291 |
|
| 5292 |
if (!unit_log_level_test(u, LOG_NOTICE)) |
| 5293 |
return; |
| 5294 |
|
| 5295 |
r = dir_is_empty(where, /* ignore_hidden_or_backup= */ false); |
| 5296 |
if (r > 0 || r == -ENOTDIR) |
| 5297 |
return; |
| 5298 |
if (r < 0) { |
| 5299 |
log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where); |
| 5300 |
return; |
| 5301 |
} |
| 5302 |
|
| 5303 |
log_unit_struct(u, LOG_NOTICE, |
| 5304 |
LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING_STR), |
| 5305 |
LOG_UNIT_INVOCATION_ID(u), |
| 5306 |
LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where), |
| 5307 |
LOG_ITEM("WHERE=%s", where)); |
| 5308 |
} |
| 5309 |
|
| 5310 |
int unit_log_noncanonical_mount_path(Unit *u, const char *where) { |
| 5311 |
assert(u); |
| 5312 |
assert(where); |
| 5313 |
|
| 5314 |
/* No need to mention "." or "..", they would already have been rejected by unit_name_from_path() */ |
| 5315 |
log_unit_struct(u, LOG_ERR, |
| 5316 |
LOG_MESSAGE_ID(SD_MESSAGE_NON_CANONICAL_MOUNT_STR), |
| 5317 |
LOG_UNIT_INVOCATION_ID(u), |
| 5318 |
LOG_UNIT_MESSAGE(u, "Mount path %s is not canonical (contains a symlink).", where), |
| 5319 |
LOG_ITEM("WHERE=%s", where)); |
| 5320 |
|
| 5321 |
return -ELOOP; |
| 5322 |
} |
| 5323 |
|
| 5324 |
int unit_fail_if_noncanonical_mount_path(Unit *u, const char* where) { |
| 5325 |
int r; |
| 5326 |
|
| 5327 |
assert(u); |
| 5328 |
assert(where); |
| 5329 |
|
| 5330 |
_cleanup_free_ char *canonical_where = NULL; |
| 5331 |
r = chase(where, /* root= */ NULL, CHASE_NONEXISTENT, &canonical_where, /* ret_fd= */ NULL); |
| 5332 |
if (r < 0) { |
| 5333 |
log_unit_debug_errno(u, r, "Failed to check %s for symlinks, ignoring: %m", where); |
| 5334 |
return 0; |
| 5335 |
} |
| 5336 |
|
| 5337 |
/* We will happily ignore a trailing slash (or any redundant slashes) */ |
| 5338 |
if (path_equal(where, canonical_where)) |
| 5339 |
return 0; |
| 5340 |
|
| 5341 |
return unit_log_noncanonical_mount_path(u, where); |
| 5342 |
} |
| 5343 |
|
| 5344 |
bool unit_is_pristine(Unit *u) { |
| 5345 |
assert(u); |
| 5346 |
|
| 5347 |
/* Check if the unit already exists or is already around, in a number of different ways. Note that to |
| 5348 |
* cater for unit types such as slice, we are generally fine with units that are marked UNIT_LOADED |
| 5349 |
* even though nothing was actually loaded, as those unit types don't require a file on disk. |
| 5350 |
* |
| 5351 |
* Note that we don't check for drop-ins here, because we allow drop-ins for transient units |
| 5352 |
* identically to non-transient units, both unit-specific and hierarchical. E.g. for a-b-c.service: |
| 5353 |
* service.d/….conf, a-.service.d/….conf, a-b-.service.d/….conf, a-b-c.service.d/….conf. |
| 5354 |
*/ |
| 5355 |
|
| 5356 |
return IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_LOADED) && |
| 5357 |
!u->fragment_path && |
| 5358 |
!u->source_path && |
| 5359 |
!u->job && |
| 5360 |
!u->merged_into; |
| 5361 |
} |
| 5362 |
|
| 5363 |
PidRef* unit_control_pid(Unit *u) { |
| 5364 |
assert(u); |
| 5365 |
|
| 5366 |
if (UNIT_VTABLE(u)->control_pid) |
| 5367 |
return UNIT_VTABLE(u)->control_pid(u); |
| 5368 |
|
| 5369 |
return NULL; |
| 5370 |
} |
| 5371 |
|
| 5372 |
PidRef* unit_main_pid_full(Unit *u, bool *ret_is_alien) { |
| 5373 |
assert(u); |
| 5374 |
|
| 5375 |
if (UNIT_VTABLE(u)->main_pid) |
| 5376 |
return UNIT_VTABLE(u)->main_pid(u, ret_is_alien); |
| 5377 |
|
| 5378 |
if (ret_is_alien) |
| 5379 |
*ret_is_alien = false; |
| 5380 |
return NULL; |
| 5381 |
} |
| 5382 |
|
| 5383 |
static void unit_modify_user_nft_set(Unit *u, bool add, NFTSetSource source, uint32_t element) { |
| 5384 |
int r; |
| 5385 |
|
| 5386 |
assert(u); |
| 5387 |
|
| 5388 |
if (!MANAGER_IS_SYSTEM(u->manager)) |
| 5389 |
return; |
| 5390 |
|
| 5391 |
CGroupContext *c; |
| 5392 |
c = unit_get_cgroup_context(u); |
| 5393 |
if (!c) |
| 5394 |
return; |
| 5395 |
|
| 5396 |
FOREACH_ARRAY(nft_set, c->nft_set_context.sets, c->nft_set_context.n_sets) { |
| 5397 |
if (nft_set->source != source) |
| 5398 |
continue; |
| 5399 |
|
| 5400 |
if (!u->manager->nfnl) { |
| 5401 |
r = sd_nfnl_socket_open(&u->manager->nfnl); |
| 5402 |
if (r < 0) |
| 5403 |
return (void) log_once_errno(LOG_WARNING, r, "Failed to open NETLINK_NETFILTER socket, ignoring: %m"); |
| 5404 |
} |
| 5405 |
|
| 5406 |
r = nft_set_element_modify_any(u->manager->nfnl, add, nft_set->nfproto, nft_set->table, nft_set->set, &element, sizeof(element)); |
| 5407 |
if (r < 0) |
| 5408 |
log_warning_errno(r, "Failed to %s NFT set entry: family %s, table %s, set %s, ID %u, ignoring: %m", |
| 5409 |
add? "add" : "delete", nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set, element); |
| 5410 |
else |
| 5411 |
log_debug("%s NFT set entry: family %s, table %s, set %s, ID %u", |
| 5412 |
add? "Added" : "Deleted", nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set, element); |
| 5413 |
} |
| 5414 |
} |
| 5415 |
|
| 5416 |
static void unit_unref_uid_internal( |
| 5417 |
Unit *u, |
| 5418 |
uid_t *ref_uid, |
| 5419 |
bool destroy_now, |
| 5420 |
void (*_manager_unref_uid)(Manager *m, uid_t uid, bool destroy_now)) { |
| 5421 |
|
| 5422 |
assert(u); |
| 5423 |
assert(ref_uid); |
| 5424 |
assert(_manager_unref_uid); |
| 5425 |
|
| 5426 |
/* Generic implementation of both unit_unref_uid() and unit_unref_gid(), under the assumption that uid_t and |
| 5427 |
* gid_t are actually the same time, with the same validity rules. |
| 5428 |
* |
| 5429 |
* Drops a reference to UID/GID from a unit. */ |
| 5430 |
|
| 5431 |
assert_cc(sizeof(uid_t) == sizeof(gid_t)); |
| 5432 |
assert_cc(UID_INVALID == (uid_t) GID_INVALID); |
| 5433 |
|
| 5434 |
if (!uid_is_valid(*ref_uid)) |
| 5435 |
return; |
| 5436 |
|
| 5437 |
_manager_unref_uid(u->manager, *ref_uid, destroy_now); |
| 5438 |
*ref_uid = UID_INVALID; |
| 5439 |
} |
| 5440 |
|
| 5441 |
static void unit_unref_uid(Unit *u, bool destroy_now) { |
| 5442 |
assert(u); |
| 5443 |
|
| 5444 |
unit_modify_user_nft_set(u, /* add= */ false, NFT_SET_SOURCE_USER, u->ref_uid); |
| 5445 |
|
| 5446 |
unit_unref_uid_internal(u, &u->ref_uid, destroy_now, manager_unref_uid); |
| 5447 |
} |
| 5448 |
|
| 5449 |
static void unit_unref_gid(Unit *u, bool destroy_now) { |
| 5450 |
assert(u); |
| 5451 |
|
| 5452 |
unit_modify_user_nft_set(u, /* add= */ false, NFT_SET_SOURCE_GROUP, u->ref_gid); |
| 5453 |
|
| 5454 |
unit_unref_uid_internal(u, (uid_t*) &u->ref_gid, destroy_now, manager_unref_gid); |
| 5455 |
} |
| 5456 |
|
| 5457 |
void unit_unref_uid_gid(Unit *u, bool destroy_now) { |
| 5458 |
assert(u); |
| 5459 |
|
| 5460 |
unit_unref_uid(u, destroy_now); |
| 5461 |
unit_unref_gid(u, destroy_now); |
| 5462 |
} |
| 5463 |
|
| 5464 |
static int unit_ref_uid_internal( |
| 5465 |
Unit *u, |
| 5466 |
uid_t *ref_uid, |
| 5467 |
uid_t uid, |
| 5468 |
bool clean_ipc, |
| 5469 |
int (*_manager_ref_uid)(Manager *m, uid_t uid, bool clean_ipc)) { |
| 5470 |
|
| 5471 |
int r; |
| 5472 |
|
| 5473 |
assert(u); |
| 5474 |
assert(ref_uid); |
| 5475 |
assert(uid_is_valid(uid)); |
| 5476 |
assert(_manager_ref_uid); |
| 5477 |
|
| 5478 |
/* Generic implementation of both unit_ref_uid() and unit_ref_guid(), under the assumption that uid_t and gid_t |
| 5479 |
* are actually the same type, and have the same validity rules. |
| 5480 |
* |
| 5481 |
* Adds a reference on a specific UID/GID to this unit. Each unit referencing the same UID/GID maintains a |
| 5482 |
* reference so that we can destroy the UID/GID's IPC resources as soon as this is requested and the counter |
| 5483 |
* drops to zero. */ |
| 5484 |
|
| 5485 |
assert_cc(sizeof(uid_t) == sizeof(gid_t)); |
| 5486 |
assert_cc(UID_INVALID == (uid_t) GID_INVALID); |
| 5487 |
|
| 5488 |
if (*ref_uid == uid) |
| 5489 |
return 0; |
| 5490 |
|
| 5491 |
if (uid_is_valid(*ref_uid)) /* Already set? */ |
| 5492 |
return -EBUSY; |
| 5493 |
|
| 5494 |
r = _manager_ref_uid(u->manager, uid, clean_ipc); |
| 5495 |
if (r < 0) |
| 5496 |
return r; |
| 5497 |
|
| 5498 |
*ref_uid = uid; |
| 5499 |
return 1; |
| 5500 |
} |
| 5501 |
|
| 5502 |
static int unit_ref_uid(Unit *u, uid_t uid, bool clean_ipc) { |
| 5503 |
return unit_ref_uid_internal(u, &u->ref_uid, uid, clean_ipc, manager_ref_uid); |
| 5504 |
} |
| 5505 |
|
| 5506 |
static int unit_ref_gid(Unit *u, gid_t gid, bool clean_ipc) { |
| 5507 |
return unit_ref_uid_internal(u, (uid_t*) &u->ref_gid, (uid_t) gid, clean_ipc, manager_ref_gid); |
| 5508 |
} |
| 5509 |
|
| 5510 |
static int unit_ref_uid_gid_internal(Unit *u, uid_t uid, gid_t gid, bool clean_ipc) { |
| 5511 |
int r = 0, q = 0; |
| 5512 |
|
| 5513 |
assert(u); |
| 5514 |
|
| 5515 |
/* Reference both a UID and a GID in one go. Either references both, or neither. */ |
| 5516 |
|
| 5517 |
if (uid_is_valid(uid)) { |
| 5518 |
r = unit_ref_uid(u, uid, clean_ipc); |
| 5519 |
if (r < 0) |
| 5520 |
return r; |
| 5521 |
} |
| 5522 |
|
| 5523 |
if (gid_is_valid(gid)) { |
| 5524 |
q = unit_ref_gid(u, gid, clean_ipc); |
| 5525 |
if (q < 0) { |
| 5526 |
if (r > 0) |
| 5527 |
unit_unref_uid(u, false); |
| 5528 |
|
| 5529 |
return q; |
| 5530 |
} |
| 5531 |
} |
| 5532 |
|
| 5533 |
return r > 0 || q > 0; |
| 5534 |
} |
| 5535 |
|
| 5536 |
int unit_ref_uid_gid(Unit *u, uid_t uid, gid_t gid) { |
| 5537 |
ExecContext *c; |
| 5538 |
int r; |
| 5539 |
|
| 5540 |
assert(u); |
| 5541 |
|
| 5542 |
c = unit_get_exec_context(u); |
| 5543 |
|
| 5544 |
r = unit_ref_uid_gid_internal(u, uid, gid, c ? c->remove_ipc : false); |
| 5545 |
if (r < 0) |
| 5546 |
return log_unit_warning_errno(u, r, "Couldn't add UID/GID reference to unit, proceeding without: %m"); |
| 5547 |
|
| 5548 |
unit_modify_user_nft_set(u, /* add= */ true, NFT_SET_SOURCE_USER, uid); |
| 5549 |
unit_modify_user_nft_set(u, /* add= */ true, NFT_SET_SOURCE_GROUP, gid); |
| 5550 |
|
| 5551 |
return r; |
| 5552 |
} |
| 5553 |
|
| 5554 |
void unit_notify_user_lookup(Unit *u, uid_t uid, gid_t gid) { |
| 5555 |
int r; |
| 5556 |
|
| 5557 |
assert(u); |
| 5558 |
|
| 5559 |
/* This is invoked whenever one of the forked off processes let's us know the UID/GID its user name/group names |
| 5560 |
* resolved to. We keep track of which UID/GID is currently assigned in order to be able to destroy its IPC |
| 5561 |
* objects when no service references the UID/GID anymore. */ |
| 5562 |
|
| 5563 |
r = unit_ref_uid_gid(u, uid, gid); |
| 5564 |
if (r > 0) |
| 5565 |
unit_add_to_dbus_queue(u); |
| 5566 |
} |
| 5567 |
|
| 5568 |
int unit_acquire_invocation_id(Unit *u) { |
| 5569 |
sd_id128_t id; |
| 5570 |
int r; |
| 5571 |
|
| 5572 |
assert(u); |
| 5573 |
|
| 5574 |
r = sd_id128_randomize(&id); |
| 5575 |
if (r < 0) |
| 5576 |
return log_unit_error_errno(u, r, "Failed to generate invocation ID for unit: %m"); |
| 5577 |
|
| 5578 |
r = unit_set_invocation_id(u, id); |
| 5579 |
if (r < 0) |
| 5580 |
return log_unit_error_errno(u, r, "Failed to set invocation ID for unit: %m"); |
| 5581 |
|
| 5582 |
unit_add_to_dbus_queue(u); |
| 5583 |
return 0; |
| 5584 |
} |
| 5585 |
|
| 5586 |
int unit_set_exec_params(Unit *u, ExecParameters *p) { |
| 5587 |
int r; |
| 5588 |
|
| 5589 |
assert(u); |
| 5590 |
assert(p); |
| 5591 |
|
| 5592 |
/* Copy parameters from manager */ |
| 5593 |
r = manager_get_effective_environment(u->manager, &p->environment); |
| 5594 |
if (r < 0) |
| 5595 |
return r; |
| 5596 |
|
| 5597 |
p->runtime_scope = u->manager->runtime_scope; |
| 5598 |
|
| 5599 |
r = strdup_to(&p->confirm_spawn, manager_get_confirm_spawn(u->manager)); |
| 5600 |
if (r < 0) |
| 5601 |
return r; |
| 5602 |
|
| 5603 |
p->prefix = u->manager->prefix; |
| 5604 |
SET_FLAG(p->flags, EXEC_PASS_LOG_UNIT|EXEC_CHOWN_DIRECTORIES, MANAGER_IS_SYSTEM(u->manager)); |
| 5605 |
|
| 5606 |
/* Copy parameters from unit */ |
| 5607 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 5608 |
p->cgroup_path = crt ? crt->cgroup_path : NULL; |
| 5609 |
SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, unit_cgroup_delegate(u)); |
| 5610 |
|
| 5611 |
p->received_credentials_directory = u->manager->received_credentials_directory; |
| 5612 |
p->received_encrypted_credentials_directory = u->manager->received_encrypted_credentials_directory; |
| 5613 |
|
| 5614 |
p->shall_confirm_spawn = u->manager->confirm_spawn; |
| 5615 |
|
| 5616 |
p->fallback_smack_process_label = u->manager->defaults.smack_process_label; |
| 5617 |
|
| 5618 |
if (u->manager->restrict_fs && p->bpf_restrict_fs_map_fd < 0) { |
| 5619 |
int fd = bpf_restrict_fs_map_fd(u); |
| 5620 |
if (fd < 0) |
| 5621 |
return fd; |
| 5622 |
|
| 5623 |
p->bpf_restrict_fs_map_fd = fd; |
| 5624 |
} |
| 5625 |
|
| 5626 |
p->user_lookup_fd = u->manager->user_lookup_fds[1]; |
| 5627 |
p->handoff_timestamp_fd = u->manager->handoff_timestamp_fds[1]; |
| 5628 |
if (UNIT_VTABLE(u)->notify_pidref) |
| 5629 |
p->pidref_transport_fd = u->manager->pidref_transport_fds[1]; |
| 5630 |
|
| 5631 |
p->cgroup_id = crt ? crt->cgroup_id : 0; |
| 5632 |
p->invocation_id = u->invocation_id; |
| 5633 |
sd_id128_to_string(p->invocation_id, p->invocation_id_string); |
| 5634 |
p->unit_id = strdup(u->id); |
| 5635 |
if (!p->unit_id) |
| 5636 |
return -ENOMEM; |
| 5637 |
|
| 5638 |
p->debug_invocation = u->debug_invocation; |
| 5639 |
|
| 5640 |
return 0; |
| 5641 |
} |
| 5642 |
|
| 5643 |
int unit_fork_helper_process_full(Unit *u, const char *name, bool into_cgroup, ForkFlags flags, PidRef *ret) { |
| 5644 |
CGroupRuntime *crt = NULL; |
| 5645 |
int r; |
| 5646 |
|
| 5647 |
assert(u); |
| 5648 |
assert((flags & (FORK_RESET_SIGNALS|FORK_DETACH|FORK_WAIT)) == 0); /* these don't really make sense for manager */ |
| 5649 |
assert(ret); |
| 5650 |
|
| 5651 |
/* Forks off a helper process and makes sure it is a member of the unit's cgroup, if configured to |
| 5652 |
* do so. Returns == 0 in the child, and > 0 in the parent. The pidref parameter is always filled in |
| 5653 |
* with the child's PID reference. */ |
| 5654 |
|
| 5655 |
if (into_cgroup) { |
| 5656 |
r = unit_realize_cgroup(u); |
| 5657 |
if (r < 0) |
| 5658 |
return r; |
| 5659 |
|
| 5660 |
crt = unit_get_cgroup_runtime(u); |
| 5661 |
} |
| 5662 |
|
| 5663 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 5664 |
r = pidref_safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG_SIGTERM|flags, &pidref); |
| 5665 |
if (r < 0) |
| 5666 |
return r; |
| 5667 |
if (r > 0) { |
| 5668 |
*ret = TAKE_PIDREF(pidref); |
| 5669 |
return r; |
| 5670 |
} |
| 5671 |
|
| 5672 |
/* Child */ |
| 5673 |
|
| 5674 |
(void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE); |
| 5675 |
(void) ignore_signals(SIGPIPE); |
| 5676 |
|
| 5677 |
if (crt && crt->cgroup_path) { |
| 5678 |
r = cg_attach(crt->cgroup_path, 0); |
| 5679 |
if (r < 0) { |
| 5680 |
log_unit_error_errno(u, r, "Failed to join unit cgroup %s: %m", empty_to_root(crt->cgroup_path)); |
| 5681 |
_exit(EXIT_CGROUP); |
| 5682 |
} |
| 5683 |
} |
| 5684 |
|
| 5685 |
*ret = TAKE_PIDREF(pidref); |
| 5686 |
return 0; |
| 5687 |
} |
| 5688 |
|
| 5689 |
int unit_fork_helper_process(Unit *u, const char *name, bool into_cgroup, PidRef *ret) { |
| 5690 |
return unit_fork_helper_process_full(u, name, into_cgroup, /* flags= */ 0, ret); |
| 5691 |
} |
| 5692 |
|
| 5693 |
int unit_fork_and_watch_rm_rf(Unit *u, char **paths, PidRef *ret_pid) { |
| 5694 |
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL; |
| 5695 |
int r; |
| 5696 |
|
| 5697 |
assert(u); |
| 5698 |
assert(ret_pid); |
| 5699 |
|
| 5700 |
r = unit_fork_helper_process(u, "(sd-rmrf)", /* into_cgroup= */ true, &pid); |
| 5701 |
if (r < 0) |
| 5702 |
return r; |
| 5703 |
if (r == 0) { |
| 5704 |
int ret = EXIT_SUCCESS; |
| 5705 |
|
| 5706 |
STRV_FOREACH(i, paths) { |
| 5707 |
r = rm_rf(*i, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK); |
| 5708 |
if (r < 0) { |
| 5709 |
log_error_errno(r, "Failed to remove '%s': %m", *i); |
| 5710 |
ret = EXIT_FAILURE; |
| 5711 |
} |
| 5712 |
} |
| 5713 |
|
| 5714 |
_exit(ret); |
| 5715 |
} |
| 5716 |
|
| 5717 |
r = unit_watch_pidref(u, &pid, /* exclusive= */ true); |
| 5718 |
if (r < 0) |
| 5719 |
return r; |
| 5720 |
|
| 5721 |
*ret_pid = TAKE_PIDREF(pid); |
| 5722 |
return 0; |
| 5723 |
} |
| 5724 |
|
| 5725 |
static void unit_update_dependency_mask(Hashmap *deps, Unit *other, UnitDependencyInfo di) { |
| 5726 |
assert(deps); |
| 5727 |
assert(other); |
| 5728 |
|
| 5729 |
if (di.origin_mask == 0 && di.destination_mask == 0) |
| 5730 |
/* No bit set anymore, let's drop the whole entry */ |
| 5731 |
assert_se(hashmap_remove(deps, other)); |
| 5732 |
else |
| 5733 |
/* Mask was reduced, let's update the entry */ |
| 5734 |
assert_se(hashmap_update(deps, other, di.data) == 0); |
| 5735 |
} |
| 5736 |
|
| 5737 |
void unit_remove_dependencies(Unit *u, UnitDependencyMask mask) { |
| 5738 |
Hashmap *deps; |
| 5739 |
assert(u); |
| 5740 |
|
| 5741 |
/* Removes all dependencies u has on other units marked for ownership by 'mask'. */ |
| 5742 |
|
| 5743 |
if (mask == 0) |
| 5744 |
return; |
| 5745 |
|
| 5746 |
HASHMAP_FOREACH(deps, u->dependencies) { |
| 5747 |
bool done; |
| 5748 |
|
| 5749 |
do { |
| 5750 |
UnitDependencyInfo di; |
| 5751 |
Unit *other; |
| 5752 |
|
| 5753 |
done = true; |
| 5754 |
|
| 5755 |
HASHMAP_FOREACH_KEY(di.data, other, deps) { |
| 5756 |
Hashmap *other_deps; |
| 5757 |
|
| 5758 |
if (FLAGS_SET(~mask, di.origin_mask)) |
| 5759 |
continue; |
| 5760 |
|
| 5761 |
di.origin_mask &= ~mask; |
| 5762 |
unit_update_dependency_mask(deps, other, di); |
| 5763 |
|
| 5764 |
/* We updated the dependency from our unit to the other unit now. But most |
| 5765 |
* dependencies imply a reverse dependency. Hence, let's delete that one |
| 5766 |
* too. For that we go through all dependency types on the other unit and |
| 5767 |
* delete all those which point to us and have the right mask set. */ |
| 5768 |
|
| 5769 |
HASHMAP_FOREACH(other_deps, other->dependencies) { |
| 5770 |
UnitDependencyInfo dj; |
| 5771 |
|
| 5772 |
dj.data = hashmap_get(other_deps, u); |
| 5773 |
if (FLAGS_SET(~mask, dj.destination_mask)) |
| 5774 |
continue; |
| 5775 |
|
| 5776 |
dj.destination_mask &= ~mask; |
| 5777 |
unit_update_dependency_mask(other_deps, u, dj); |
| 5778 |
} |
| 5779 |
|
| 5780 |
unit_add_to_gc_queue(other); |
| 5781 |
|
| 5782 |
/* The unit 'other' may not be wanted by the unit 'u'. */ |
| 5783 |
unit_submit_to_stop_when_unneeded_queue(other); |
| 5784 |
|
| 5785 |
u->dependency_generation++; |
| 5786 |
other->dependency_generation++; |
| 5787 |
|
| 5788 |
done = false; |
| 5789 |
break; |
| 5790 |
} |
| 5791 |
|
| 5792 |
} while (!done); |
| 5793 |
} |
| 5794 |
} |
| 5795 |
|
| 5796 |
static int unit_get_invocation_path(Unit *u, char **ret) { |
| 5797 |
char *p; |
| 5798 |
int r; |
| 5799 |
|
| 5800 |
assert(u); |
| 5801 |
assert(ret); |
| 5802 |
|
| 5803 |
if (MANAGER_IS_SYSTEM(u->manager)) |
| 5804 |
p = strjoin("/run/systemd/units/invocation:", u->id); |
| 5805 |
else { |
| 5806 |
_cleanup_free_ char *user_path = NULL; |
| 5807 |
|
| 5808 |
r = xdg_user_runtime_dir("/systemd/units/invocation:", &user_path); |
| 5809 |
if (r < 0) |
| 5810 |
return r; |
| 5811 |
|
| 5812 |
p = strjoin(user_path, u->id); |
| 5813 |
} |
| 5814 |
if (!p) |
| 5815 |
return -ENOMEM; |
| 5816 |
|
| 5817 |
*ret = p; |
| 5818 |
return 0; |
| 5819 |
} |
| 5820 |
|
| 5821 |
static int unit_export_invocation_id(Unit *u) { |
| 5822 |
_cleanup_free_ char *p = NULL; |
| 5823 |
int r; |
| 5824 |
|
| 5825 |
assert(u); |
| 5826 |
|
| 5827 |
if (u->exported_invocation_id) |
| 5828 |
return 0; |
| 5829 |
|
| 5830 |
if (sd_id128_is_null(u->invocation_id)) |
| 5831 |
return 0; |
| 5832 |
|
| 5833 |
r = unit_get_invocation_path(u, &p); |
| 5834 |
if (r < 0) |
| 5835 |
return log_unit_debug_errno(u, r, "Failed to get invocation path: %m"); |
| 5836 |
|
| 5837 |
r = symlinkat_atomic_full(u->invocation_id_string, AT_FDCWD, p, SYMLINK_LABEL); |
| 5838 |
if (r < 0) |
| 5839 |
return log_unit_debug_errno(u, r, "Failed to create invocation ID symlink %s: %m", p); |
| 5840 |
|
| 5841 |
u->exported_invocation_id = true; |
| 5842 |
return 0; |
| 5843 |
} |
| 5844 |
|
| 5845 |
static int unit_export_log_level_max(Unit *u, int log_level_max, bool overwrite) { |
| 5846 |
const char *p; |
| 5847 |
char buf[2]; |
| 5848 |
int r; |
| 5849 |
|
| 5850 |
assert(u); |
| 5851 |
|
| 5852 |
/* When the debug_invocation logic runs, overwrite will be true as we always want to switch the max |
| 5853 |
* log level that the journal applies, and we want to always restore the previous level once done */ |
| 5854 |
|
| 5855 |
if (!overwrite && u->exported_log_level_max) |
| 5856 |
return 0; |
| 5857 |
|
| 5858 |
if (log_level_max < 0) |
| 5859 |
return 0; |
| 5860 |
|
| 5861 |
assert(log_level_max <= 7); |
| 5862 |
|
| 5863 |
buf[0] = '0' + log_level_max; |
| 5864 |
buf[1] = 0; |
| 5865 |
|
| 5866 |
p = strjoina("/run/systemd/units/log-level-max:", u->id); |
| 5867 |
r = symlink_atomic(buf, p); |
| 5868 |
if (r < 0) |
| 5869 |
return log_unit_debug_errno(u, r, "Failed to create maximum log level symlink %s: %m", p); |
| 5870 |
|
| 5871 |
u->exported_log_level_max = true; |
| 5872 |
return 0; |
| 5873 |
} |
| 5874 |
|
| 5875 |
static int unit_export_log_extra_fields(Unit *u, const ExecContext *c) { |
| 5876 |
int r; |
| 5877 |
|
| 5878 |
if (u->exported_log_extra_fields) |
| 5879 |
return 0; |
| 5880 |
|
| 5881 |
if (c->n_log_extra_fields <= 0) |
| 5882 |
return 0; |
| 5883 |
|
| 5884 |
assert(c->n_log_extra_fields <= LOG_EXTRA_FIELDS_MAX); |
| 5885 |
|
| 5886 |
le64_t *sizes = newa(le64_t, c->n_log_extra_fields); |
| 5887 |
struct iovec *iovec = newa(struct iovec, c->n_log_extra_fields * 2); |
| 5888 |
|
| 5889 |
for (size_t i = 0; i < c->n_log_extra_fields; i++) { |
| 5890 |
sizes[i] = htole64(c->log_extra_fields[i].iov_len); |
| 5891 |
|
| 5892 |
iovec[i*2] = IOVEC_MAKE(sizes + i, sizeof(le64_t)); |
| 5893 |
iovec[i*2+1] = c->log_extra_fields[i]; |
| 5894 |
} |
| 5895 |
|
| 5896 |
const char *p = strjoina("/run/systemd/units/log-extra-fields:", u->id); |
| 5897 |
char *pattern = strjoina(p, ".XXXXXX"); |
| 5898 |
|
| 5899 |
_cleanup_close_ int fd = mkostemp_safe(pattern); |
| 5900 |
if (fd < 0) |
| 5901 |
return log_unit_debug_errno(u, fd, "Failed to create extra fields file %s: %m", p); |
| 5902 |
|
| 5903 |
if (writev(fd, iovec, c->n_log_extra_fields * 2) < 0) { |
| 5904 |
r = log_unit_debug_errno(u, errno, "Failed to write extra fields: %m"); |
| 5905 |
goto fail; |
| 5906 |
} |
| 5907 |
|
| 5908 |
(void) fchmod(fd, 0644); |
| 5909 |
|
| 5910 |
if (rename(pattern, p) < 0) { |
| 5911 |
r = log_unit_debug_errno(u, errno, "Failed to rename extra fields file: %m"); |
| 5912 |
goto fail; |
| 5913 |
} |
| 5914 |
|
| 5915 |
u->exported_log_extra_fields = true; |
| 5916 |
return 0; |
| 5917 |
|
| 5918 |
fail: |
| 5919 |
(void) unlink(pattern); |
| 5920 |
return r; |
| 5921 |
} |
| 5922 |
|
| 5923 |
static int unit_export_log_ratelimit_interval(Unit *u, const ExecContext *c) { |
| 5924 |
int r; |
| 5925 |
|
| 5926 |
assert(u); |
| 5927 |
assert(c); |
| 5928 |
|
| 5929 |
if (u->exported_log_ratelimit_interval) |
| 5930 |
return 0; |
| 5931 |
|
| 5932 |
if (c->log_ratelimit.interval == 0) |
| 5933 |
return 0; |
| 5934 |
|
| 5935 |
const char *p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id); |
| 5936 |
|
| 5937 |
char buf[DECIMAL_STR_MAX(c->log_ratelimit.interval)]; |
| 5938 |
xsprintf(buf, "%" PRIu64, c->log_ratelimit.interval); |
| 5939 |
|
| 5940 |
r = symlink_atomic(buf, p); |
| 5941 |
if (r < 0) |
| 5942 |
return log_unit_debug_errno(u, r, "Failed to create log rate limit interval symlink %s: %m", p); |
| 5943 |
|
| 5944 |
u->exported_log_ratelimit_interval = true; |
| 5945 |
return 0; |
| 5946 |
} |
| 5947 |
|
| 5948 |
static int unit_export_log_ratelimit_burst(Unit *u, const ExecContext *c) { |
| 5949 |
int r; |
| 5950 |
|
| 5951 |
assert(u); |
| 5952 |
assert(c); |
| 5953 |
|
| 5954 |
if (u->exported_log_ratelimit_burst) |
| 5955 |
return 0; |
| 5956 |
|
| 5957 |
if (c->log_ratelimit.burst == 0) |
| 5958 |
return 0; |
| 5959 |
|
| 5960 |
const char *p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id); |
| 5961 |
|
| 5962 |
char buf[DECIMAL_STR_MAX(c->log_ratelimit.burst)]; |
| 5963 |
xsprintf(buf, "%u", c->log_ratelimit.burst); |
| 5964 |
|
| 5965 |
r = symlink_atomic(buf, p); |
| 5966 |
if (r < 0) |
| 5967 |
return log_unit_debug_errno(u, r, "Failed to create log rate limit burst symlink %s: %m", p); |
| 5968 |
|
| 5969 |
u->exported_log_ratelimit_burst = true; |
| 5970 |
return 0; |
| 5971 |
} |
| 5972 |
|
| 5973 |
void unit_export_state_files(Unit *u) { |
| 5974 |
const ExecContext *c; |
| 5975 |
|
| 5976 |
assert(u); |
| 5977 |
|
| 5978 |
if (!u->id) |
| 5979 |
return; |
| 5980 |
|
| 5981 |
if (MANAGER_IS_TEST_RUN(u->manager)) |
| 5982 |
return; |
| 5983 |
|
| 5984 |
/* Exports a couple of unit properties to /run/systemd/units/, so that journald can quickly query this data |
| 5985 |
* from there. Ideally, journald would use IPC to query this, like everybody else, but that's hard, as long as |
| 5986 |
* the IPC system itself and PID 1 also log to the journal. |
| 5987 |
* |
| 5988 |
* Note that these files really shouldn't be considered API for anyone else, as use a runtime file system as |
| 5989 |
* IPC replacement is not compatible with today's world of file system namespaces. However, this doesn't really |
| 5990 |
* apply to communication between the journal and systemd, as we assume that these two daemons live in the same |
| 5991 |
* namespace at least. |
| 5992 |
* |
| 5993 |
* Note that some of the "files" exported here are actually symlinks and not regular files. Symlinks work |
| 5994 |
* better for storing small bits of data, in particular as we can write them with two system calls, and read |
| 5995 |
* them with one. */ |
| 5996 |
|
| 5997 |
(void) unit_export_invocation_id(u); |
| 5998 |
|
| 5999 |
if (!MANAGER_IS_SYSTEM(u->manager)) |
| 6000 |
return; |
| 6001 |
|
| 6002 |
c = unit_get_exec_context(u); |
| 6003 |
if (c) { |
| 6004 |
(void) unit_export_log_level_max(u, c->log_level_max, /* overwrite= */ false); |
| 6005 |
(void) unit_export_log_extra_fields(u, c); |
| 6006 |
(void) unit_export_log_ratelimit_interval(u, c); |
| 6007 |
(void) unit_export_log_ratelimit_burst(u, c); |
| 6008 |
} |
| 6009 |
} |
| 6010 |
|
| 6011 |
void unit_unlink_state_files(Unit *u) { |
| 6012 |
const char *p; |
| 6013 |
|
| 6014 |
assert(u); |
| 6015 |
|
| 6016 |
if (!u->id) |
| 6017 |
return; |
| 6018 |
|
| 6019 |
/* Undoes the effect of unit_export_state() */ |
| 6020 |
|
| 6021 |
if (u->exported_invocation_id) { |
| 6022 |
_cleanup_free_ char *invocation_path = NULL; |
| 6023 |
int r = unit_get_invocation_path(u, &invocation_path); |
| 6024 |
if (r >= 0) { |
| 6025 |
(void) unlink(invocation_path); |
| 6026 |
u->exported_invocation_id = false; |
| 6027 |
} |
| 6028 |
} |
| 6029 |
|
| 6030 |
if (!MANAGER_IS_SYSTEM(u->manager)) |
| 6031 |
return; |
| 6032 |
|
| 6033 |
if (u->exported_log_level_max) { |
| 6034 |
p = strjoina("/run/systemd/units/log-level-max:", u->id); |
| 6035 |
(void) unlink(p); |
| 6036 |
|
| 6037 |
u->exported_log_level_max = false; |
| 6038 |
} |
| 6039 |
|
| 6040 |
if (u->exported_log_extra_fields) { |
| 6041 |
p = strjoina("/run/systemd/units/extra-fields:", u->id); |
| 6042 |
(void) unlink(p); |
| 6043 |
|
| 6044 |
u->exported_log_extra_fields = false; |
| 6045 |
} |
| 6046 |
|
| 6047 |
if (u->exported_log_ratelimit_interval) { |
| 6048 |
p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id); |
| 6049 |
(void) unlink(p); |
| 6050 |
|
| 6051 |
u->exported_log_ratelimit_interval = false; |
| 6052 |
} |
| 6053 |
|
| 6054 |
if (u->exported_log_ratelimit_burst) { |
| 6055 |
p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id); |
| 6056 |
(void) unlink(p); |
| 6057 |
|
| 6058 |
u->exported_log_ratelimit_burst = false; |
| 6059 |
} |
| 6060 |
} |
| 6061 |
|
| 6062 |
int unit_set_debug_invocation(Unit *u, bool enable) { |
| 6063 |
int r; |
| 6064 |
|
| 6065 |
assert(u); |
| 6066 |
|
| 6067 |
if (u->debug_invocation == enable) |
| 6068 |
return 0; /* Nothing to do */ |
| 6069 |
|
| 6070 |
u->debug_invocation = enable; |
| 6071 |
|
| 6072 |
/* Ensure that the new log level is exported for the journal, in place of the previous one */ |
| 6073 |
if (u->exported_log_level_max) { |
| 6074 |
const ExecContext *ec = unit_get_exec_context(u); |
| 6075 |
if (ec) { |
| 6076 |
r = unit_export_log_level_max(u, enable ? LOG_PRI(LOG_DEBUG) : ec->log_level_max, /* overwrite= */ true); |
| 6077 |
if (r < 0) |
| 6078 |
return r; |
| 6079 |
} |
| 6080 |
} |
| 6081 |
|
| 6082 |
return 1; |
| 6083 |
} |
| 6084 |
|
| 6085 |
int unit_prepare_exec(Unit *u) { |
| 6086 |
int r; |
| 6087 |
|
| 6088 |
assert(u); |
| 6089 |
|
| 6090 |
/* Prepares everything so that we can fork of a process for this unit */ |
| 6091 |
|
| 6092 |
r = unit_realize_cgroup(u); |
| 6093 |
if (r < 0) |
| 6094 |
return r; |
| 6095 |
|
| 6096 |
CGroupRuntime *crt = unit_get_cgroup_runtime(u); |
| 6097 |
if (crt && crt->reset_accounting) { |
| 6098 |
(void) unit_reset_accounting(u); |
| 6099 |
crt->reset_accounting = false; |
| 6100 |
} |
| 6101 |
|
| 6102 |
unit_export_state_files(u); |
| 6103 |
|
| 6104 |
r = unit_setup_exec_runtime(u); |
| 6105 |
if (r < 0) |
| 6106 |
return r; |
| 6107 |
|
| 6108 |
return 0; |
| 6109 |
} |
| 6110 |
|
| 6111 |
static int unit_log_leftover_process_start(const PidRef *pid, int sig, void *userdata) { |
| 6112 |
const Unit *u = ASSERT_PTR(userdata); |
| 6113 |
_cleanup_free_ char *comm = NULL; |
| 6114 |
|
| 6115 |
assert(pidref_is_set(pid)); |
| 6116 |
|
| 6117 |
(void) pidref_get_comm(pid, &comm); |
| 6118 |
|
| 6119 |
if (ignore_leftover_process(comm)) |
| 6120 |
return 0; |
| 6121 |
|
| 6122 |
/* During start we print a warning */ |
| 6123 |
|
| 6124 |
log_unit_warning(u, |
| 6125 |
"Found left-over process " PID_FMT " (%s) in control group while starting unit. Ignoring.\n" |
| 6126 |
"This usually indicates unclean termination of a previous run, or service implementation deficiencies.", |
| 6127 |
pid->pid, strna(comm)); |
| 6128 |
|
| 6129 |
return 1; |
| 6130 |
} |
| 6131 |
|
| 6132 |
static int unit_log_leftover_process_stop(const PidRef *pid, int sig, void *userdata) { |
| 6133 |
const Unit *u = ASSERT_PTR(userdata); |
| 6134 |
_cleanup_free_ char *comm = NULL; |
| 6135 |
|
| 6136 |
assert(pidref_is_set(pid)); |
| 6137 |
|
| 6138 |
(void) pidref_get_comm(pid, &comm); |
| 6139 |
|
| 6140 |
if (ignore_leftover_process(comm)) |
| 6141 |
return 0; |
| 6142 |
|
| 6143 |
/* During stop we only print an informational message */ |
| 6144 |
|
| 6145 |
log_unit_info(u, |
| 6146 |
"Unit process " PID_FMT " (%s) remains running after unit stopped.", |
| 6147 |
pid->pid, strna(comm)); |
| 6148 |
|
| 6149 |
return 1; |
| 6150 |
} |
| 6151 |
|
| 6152 |
int unit_warn_leftover_processes(Unit *u, bool start) { |
| 6153 |
_cleanup_free_ char *cgroup = NULL; |
| 6154 |
int r; |
| 6155 |
|
| 6156 |
assert(u); |
| 6157 |
|
| 6158 |
r = unit_get_cgroup_path_with_fallback(u, &cgroup); |
| 6159 |
if (r < 0) |
| 6160 |
return r; |
| 6161 |
|
| 6162 |
return cg_kill_recursive( |
| 6163 |
cgroup, |
| 6164 |
/* sig= */ 0, |
| 6165 |
/* flags= */ 0, |
| 6166 |
/* killed_pids= */ NULL, |
| 6167 |
start ? unit_log_leftover_process_start : unit_log_leftover_process_stop, |
| 6168 |
u); |
| 6169 |
} |
| 6170 |
|
| 6171 |
bool unit_needs_console(Unit *u) { |
| 6172 |
ExecContext *ec; |
| 6173 |
UnitActiveState state; |
| 6174 |
|
| 6175 |
assert(u); |
| 6176 |
|
| 6177 |
state = unit_active_state(u); |
| 6178 |
|
| 6179 |
if (UNIT_IS_INACTIVE_OR_FAILED(state)) |
| 6180 |
return false; |
| 6181 |
|
| 6182 |
if (UNIT_VTABLE(u)->needs_console) |
| 6183 |
return UNIT_VTABLE(u)->needs_console(u); |
| 6184 |
|
| 6185 |
/* If this unit type doesn't implement this call, let's use a generic fallback implementation: */ |
| 6186 |
ec = unit_get_exec_context(u); |
| 6187 |
if (!ec) |
| 6188 |
return false; |
| 6189 |
|
| 6190 |
return exec_context_may_touch_console(ec); |
| 6191 |
} |
| 6192 |
|
| 6193 |
int unit_pid_attachable(Unit *u, PidRef *pid, sd_bus_error *reterr_error) { |
| 6194 |
int r; |
| 6195 |
|
| 6196 |
assert(u); |
| 6197 |
|
| 6198 |
/* Checks whether the specified PID is generally good for attaching, i.e. a valid PID, not our manager itself, |
| 6199 |
* and not a kernel thread either */ |
| 6200 |
|
| 6201 |
/* First, a simple range check */ |
| 6202 |
if (!pidref_is_set(pid)) |
| 6203 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Process identifier is not valid."); |
| 6204 |
|
| 6205 |
/* Some extra safety check */ |
| 6206 |
if (pid->pid == 1 || pidref_is_self(pid)) |
| 6207 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a manager process, refusing.", pid->pid); |
| 6208 |
|
| 6209 |
/* Don't even begin to bother with kernel threads */ |
| 6210 |
r = pidref_is_kernel_thread(pid); |
| 6211 |
if (r == -ESRCH) |
| 6212 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "Process with ID " PID_FMT " does not exist.", pid->pid); |
| 6213 |
if (r < 0) |
| 6214 |
return sd_bus_error_set_errnof(reterr_error, r, "Failed to determine whether process " PID_FMT " is a kernel thread: %m", pid->pid); |
| 6215 |
if (r > 0) |
| 6216 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a kernel thread, refusing.", pid->pid); |
| 6217 |
|
| 6218 |
return 0; |
| 6219 |
} |
| 6220 |
|
| 6221 |
int unit_get_log_level_max(const Unit *u) { |
| 6222 |
if (u) { |
| 6223 |
if (u->debug_invocation) |
| 6224 |
return LOG_DEBUG; |
| 6225 |
|
| 6226 |
ExecContext *ec = unit_get_exec_context(u); |
| 6227 |
if (ec && ec->log_level_max >= 0) |
| 6228 |
return ec->log_level_max; |
| 6229 |
} |
| 6230 |
|
| 6231 |
return log_get_max_level(); |
| 6232 |
} |
| 6233 |
|
| 6234 |
bool unit_log_level_test(const Unit *u, int level) { |
| 6235 |
assert(u); |
| 6236 |
return LOG_PRI(level) <= unit_get_log_level_max(u); |
| 6237 |
} |
| 6238 |
|
| 6239 |
void unit_log_success(Unit *u) { |
| 6240 |
assert(u); |
| 6241 |
|
| 6242 |
/* Let's show message "Deactivated successfully" in debug mode (when manager is user) rather than in info mode. |
| 6243 |
* This message has low information value for regular users and it might be a bit overwhelming on a system with |
| 6244 |
* a lot of devices. */ |
| 6245 |
log_unit_struct(u, |
| 6246 |
MANAGER_IS_USER(u->manager) ? LOG_DEBUG : LOG_INFO, |
| 6247 |
LOG_MESSAGE_ID(SD_MESSAGE_UNIT_SUCCESS_STR), |
| 6248 |
LOG_UNIT_INVOCATION_ID(u), |
| 6249 |
LOG_UNIT_MESSAGE(u, "Deactivated successfully.")); |
| 6250 |
} |
| 6251 |
|
| 6252 |
void unit_log_failure(Unit *u, const char *result) { |
| 6253 |
assert(u); |
| 6254 |
assert(result); |
| 6255 |
|
| 6256 |
log_unit_struct(u, LOG_WARNING, |
| 6257 |
LOG_MESSAGE_ID(SD_MESSAGE_UNIT_FAILURE_RESULT_STR), |
| 6258 |
LOG_UNIT_INVOCATION_ID(u), |
| 6259 |
LOG_UNIT_MESSAGE(u, "Failed with result '%s'.", result), |
| 6260 |
LOG_ITEM("UNIT_RESULT=%s", result)); |
| 6261 |
} |
| 6262 |
|
| 6263 |
void unit_log_skip(Unit *u, const char *result) { |
| 6264 |
assert(u); |
| 6265 |
assert(result); |
| 6266 |
|
| 6267 |
log_unit_struct(u, LOG_INFO, |
| 6268 |
LOG_MESSAGE_ID(SD_MESSAGE_UNIT_SKIPPED_STR), |
| 6269 |
LOG_UNIT_INVOCATION_ID(u), |
| 6270 |
LOG_UNIT_MESSAGE(u, "Skipped due to '%s'.", result), |
| 6271 |
LOG_ITEM("UNIT_RESULT=%s", result)); |
| 6272 |
} |
| 6273 |
|
| 6274 |
void unit_log_process_exit( |
| 6275 |
Unit *u, |
| 6276 |
const char *kind, |
| 6277 |
const char *command, |
| 6278 |
bool success, |
| 6279 |
int code, |
| 6280 |
int status) { |
| 6281 |
|
| 6282 |
int level; |
| 6283 |
|
| 6284 |
assert(u); |
| 6285 |
assert(kind); |
| 6286 |
|
| 6287 |
/* If this is a successful exit, let's log about the exit code on DEBUG level. If this is a failure |
| 6288 |
* and the process exited on its own via exit(), then let's make this a NOTICE, under the assumption |
| 6289 |
* that the service already logged the reason at a higher log level on its own. Otherwise, make it a |
| 6290 |
* WARNING. */ |
| 6291 |
if (success) |
| 6292 |
level = LOG_DEBUG; |
| 6293 |
else if (code == CLD_EXITED) |
| 6294 |
level = LOG_NOTICE; |
| 6295 |
else |
| 6296 |
level = LOG_WARNING; |
| 6297 |
|
| 6298 |
log_unit_struct(u, level, |
| 6299 |
LOG_MESSAGE_ID(SD_MESSAGE_UNIT_PROCESS_EXIT_STR), |
| 6300 |
LOG_UNIT_MESSAGE(u, "%s exited, code=%s, status=%i/%s%s", |
| 6301 |
kind, |
| 6302 |
sigchld_code_to_string(code), status, |
| 6303 |
strna(code == CLD_EXITED |
| 6304 |
? exit_status_to_string(status, EXIT_STATUS_FULL) |
| 6305 |
: signal_to_string(status)), |
| 6306 |
success ? " (success)" : ""), |
| 6307 |
LOG_ITEM("EXIT_CODE=%s", sigchld_code_to_string(code)), |
| 6308 |
LOG_ITEM("EXIT_STATUS=%i", status), |
| 6309 |
LOG_ITEM("COMMAND=%s", strna(command)), |
| 6310 |
LOG_UNIT_INVOCATION_ID(u)); |
| 6311 |
} |
| 6312 |
|
| 6313 |
int unit_exit_status(Unit *u) { |
| 6314 |
assert(u); |
| 6315 |
|
| 6316 |
/* Returns the exit status to propagate for the most recent cycle of this unit. Returns a value in the range |
| 6317 |
* 0…255 if there's something to propagate. EOPNOTSUPP if the concept does not apply to this unit type, ENODATA |
| 6318 |
* if no data is currently known (for example because the unit hasn't deactivated yet) and EBADE if the main |
| 6319 |
* service process has exited abnormally (signal/coredump). */ |
| 6320 |
|
| 6321 |
if (!UNIT_VTABLE(u)->exit_status) |
| 6322 |
return -EOPNOTSUPP; |
| 6323 |
|
| 6324 |
return UNIT_VTABLE(u)->exit_status(u); |
| 6325 |
} |
| 6326 |
|
| 6327 |
int unit_failure_action_exit_status(Unit *u) { |
| 6328 |
int r; |
| 6329 |
|
| 6330 |
assert(u); |
| 6331 |
|
| 6332 |
/* Returns the exit status to propagate on failure, or an error if there's nothing to propagate */ |
| 6333 |
|
| 6334 |
if (u->failure_action_exit_status >= 0) |
| 6335 |
return u->failure_action_exit_status; |
| 6336 |
|
| 6337 |
r = unit_exit_status(u); |
| 6338 |
if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */ |
| 6339 |
return 255; |
| 6340 |
|
| 6341 |
return r; |
| 6342 |
} |
| 6343 |
|
| 6344 |
int unit_success_action_exit_status(Unit *u) { |
| 6345 |
int r; |
| 6346 |
|
| 6347 |
assert(u); |
| 6348 |
|
| 6349 |
/* Returns the exit status to propagate on success, or an error if there's nothing to propagate */ |
| 6350 |
|
| 6351 |
if (u->success_action_exit_status >= 0) |
| 6352 |
return u->success_action_exit_status; |
| 6353 |
|
| 6354 |
r = unit_exit_status(u); |
| 6355 |
if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */ |
| 6356 |
return 255; |
| 6357 |
|
| 6358 |
return r; |
| 6359 |
} |
| 6360 |
|
| 6361 |
int unit_test_trigger_loaded(Unit *u) { |
| 6362 |
Unit *trigger; |
| 6363 |
|
| 6364 |
/* Tests whether the unit to trigger is loaded */ |
| 6365 |
|
| 6366 |
trigger = UNIT_TRIGGER(u); |
| 6367 |
if (!trigger) |
| 6368 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), |
| 6369 |
"Refusing to start, no unit to trigger."); |
| 6370 |
if (trigger->load_state != UNIT_LOADED) |
| 6371 |
return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), |
| 6372 |
"Refusing to start, unit %s to trigger not loaded.", trigger->id); |
| 6373 |
|
| 6374 |
return 0; |
| 6375 |
} |
| 6376 |
|
| 6377 |
void unit_destroy_runtime_data(Unit *u, const ExecContext *context, bool destroy_runtime_dir) { |
| 6378 |
assert(u); |
| 6379 |
assert(u->manager); |
| 6380 |
assert(context); |
| 6381 |
|
| 6382 |
/* EXEC_PRESERVE_RESTART is handled via unit_release_resources()! */ |
| 6383 |
if (destroy_runtime_dir && context->runtime_directory_preserve_mode == EXEC_PRESERVE_NO) |
| 6384 |
exec_context_destroy_runtime_directory(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); |
| 6385 |
|
| 6386 |
exec_context_destroy_credentials(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME], u->id); |
| 6387 |
exec_context_destroy_mount_ns_dir(u); |
| 6388 |
} |
| 6389 |
|
| 6390 |
int unit_clean(Unit *u, ExecCleanMask mask) { |
| 6391 |
UnitActiveState state; |
| 6392 |
|
| 6393 |
assert(u); |
| 6394 |
|
| 6395 |
/* Special return values: |
| 6396 |
* |
| 6397 |
* -EOPNOTSUPP → cleaning not supported for this unit type |
| 6398 |
* -EUNATCH → cleaning not defined for this resource type |
| 6399 |
* -EBUSY → unit currently can't be cleaned since it's running or not properly loaded, or has |
| 6400 |
* a job queued or similar |
| 6401 |
*/ |
| 6402 |
|
| 6403 |
if (!UNIT_VTABLE(u)->clean) |
| 6404 |
return -EOPNOTSUPP; |
| 6405 |
|
| 6406 |
if (mask == 0) |
| 6407 |
return -EUNATCH; |
| 6408 |
|
| 6409 |
if (u->load_state != UNIT_LOADED) |
| 6410 |
return -EBUSY; |
| 6411 |
|
| 6412 |
if (u->job) |
| 6413 |
return -EBUSY; |
| 6414 |
|
| 6415 |
state = unit_active_state(u); |
| 6416 |
if (state != UNIT_INACTIVE) |
| 6417 |
return -EBUSY; |
| 6418 |
|
| 6419 |
return UNIT_VTABLE(u)->clean(u, mask); |
| 6420 |
} |
| 6421 |
|
| 6422 |
int unit_can_clean(Unit *u, ExecCleanMask *ret) { |
| 6423 |
assert(u); |
| 6424 |
assert(ret); |
| 6425 |
|
| 6426 |
if (!UNIT_VTABLE(u)->clean || |
| 6427 |
u->load_state != UNIT_LOADED) { |
| 6428 |
*ret = 0; |
| 6429 |
return 0; |
| 6430 |
} |
| 6431 |
|
| 6432 |
/* When the clean() method is set, can_clean() really should be set too */ |
| 6433 |
assert(UNIT_VTABLE(u)->can_clean); |
| 6434 |
|
| 6435 |
return UNIT_VTABLE(u)->can_clean(u, ret); |
| 6436 |
} |
| 6437 |
|
| 6438 |
bool unit_can_start_refuse_manual(Unit *u) { |
| 6439 |
return unit_can_start(u) && !u->refuse_manual_start; |
| 6440 |
} |
| 6441 |
|
| 6442 |
bool unit_can_stop_refuse_manual(Unit *u) { |
| 6443 |
return unit_can_stop(u) && !u->refuse_manual_stop; |
| 6444 |
} |
| 6445 |
|
| 6446 |
bool unit_can_isolate_refuse_manual(Unit *u) { |
| 6447 |
return unit_can_isolate(u) && !u->refuse_manual_start; |
| 6448 |
} |
| 6449 |
|
| 6450 |
void unit_next_freezer_state(Unit *u, FreezerAction action, FreezerState *ret_next, FreezerState *ret_objective) { |
| 6451 |
FreezerState current, parent, next, objective; |
| 6452 |
|
| 6453 |
assert(u); |
| 6454 |
assert(action >= 0); |
| 6455 |
assert(action < _FREEZER_ACTION_MAX); |
| 6456 |
assert(ret_next); |
| 6457 |
assert(ret_objective); |
| 6458 |
|
| 6459 |
/* This function determines the correct freezer state transitions for a unit |
| 6460 |
* given the action being requested. It returns the next state, and also the "objective", |
| 6461 |
* which is either FREEZER_FROZEN or FREEZER_RUNNING, depending on what actual state we |
| 6462 |
* ultimately want to achieve. */ |
| 6463 |
|
| 6464 |
current = u->freezer_state; |
| 6465 |
|
| 6466 |
Unit *slice = UNIT_GET_SLICE(u); |
| 6467 |
if (slice) |
| 6468 |
parent = slice->freezer_state; |
| 6469 |
else |
| 6470 |
parent = FREEZER_RUNNING; |
| 6471 |
|
| 6472 |
switch (action) { |
| 6473 |
|
| 6474 |
case FREEZER_FREEZE: |
| 6475 |
/* We always "promote" a freeze initiated by parent into a normal freeze */ |
| 6476 |
if (IN_SET(current, FREEZER_FROZEN, FREEZER_FROZEN_BY_PARENT)) |
| 6477 |
next = FREEZER_FROZEN; |
| 6478 |
else |
| 6479 |
next = FREEZER_FREEZING; |
| 6480 |
break; |
| 6481 |
|
| 6482 |
case FREEZER_THAW: |
| 6483 |
/* Thawing is the most complicated operation here, because we can't thaw a unit |
| 6484 |
* if its parent is frozen. So we instead "demote" a normal freeze into a freeze |
| 6485 |
* initiated by parent if the parent is frozen */ |
| 6486 |
if (IN_SET(current, FREEZER_RUNNING, FREEZER_THAWING, |
| 6487 |
FREEZER_FREEZING_BY_PARENT, FREEZER_FROZEN_BY_PARENT)) /* Should usually be refused by unit_freezer_action */ |
| 6488 |
next = current; |
| 6489 |
else if (current == FREEZER_FREEZING) { |
| 6490 |
if (IN_SET(parent, FREEZER_RUNNING, FREEZER_THAWING)) |
| 6491 |
next = FREEZER_THAWING; |
| 6492 |
else |
| 6493 |
next = FREEZER_FREEZING_BY_PARENT; |
| 6494 |
} else if (current == FREEZER_FROZEN) { |
| 6495 |
if (IN_SET(parent, FREEZER_RUNNING, FREEZER_THAWING)) |
| 6496 |
next = FREEZER_THAWING; |
| 6497 |
else |
| 6498 |
next = FREEZER_FROZEN_BY_PARENT; |
| 6499 |
} else |
| 6500 |
assert_not_reached(); |
| 6501 |
break; |
| 6502 |
|
| 6503 |
case FREEZER_PARENT_FREEZE: |
| 6504 |
/* We need to avoid accidentally demoting units frozen manually */ |
| 6505 |
if (IN_SET(current, FREEZER_FREEZING, FREEZER_FROZEN, FREEZER_FROZEN_BY_PARENT)) |
| 6506 |
next = current; |
| 6507 |
else |
| 6508 |
next = FREEZER_FREEZING_BY_PARENT; |
| 6509 |
break; |
| 6510 |
|
| 6511 |
case FREEZER_PARENT_THAW: |
| 6512 |
/* We don't want to thaw units from a parent if they were frozen |
| 6513 |
* manually, so for such units this action is a no-op */ |
| 6514 |
if (IN_SET(current, FREEZER_RUNNING, FREEZER_FREEZING, FREEZER_FROZEN)) |
| 6515 |
next = current; |
| 6516 |
else |
| 6517 |
next = FREEZER_THAWING; |
| 6518 |
break; |
| 6519 |
|
| 6520 |
default: |
| 6521 |
assert_not_reached(); |
| 6522 |
} |
| 6523 |
|
| 6524 |
objective = freezer_state_objective(next); |
| 6525 |
assert(IN_SET(objective, FREEZER_RUNNING, FREEZER_FROZEN)); |
| 6526 |
|
| 6527 |
*ret_next = next; |
| 6528 |
*ret_objective = objective; |
| 6529 |
} |
| 6530 |
|
| 6531 |
bool unit_can_freeze(const Unit *u) { |
| 6532 |
assert(u); |
| 6533 |
|
| 6534 |
if (unit_has_name(u, SPECIAL_ROOT_SLICE) || unit_has_name(u, SPECIAL_INIT_SCOPE)) |
| 6535 |
return false; |
| 6536 |
|
| 6537 |
if (UNIT_VTABLE(u)->can_freeze) |
| 6538 |
return UNIT_VTABLE(u)->can_freeze(u); |
| 6539 |
|
| 6540 |
return UNIT_VTABLE(u)->freezer_action; |
| 6541 |
} |
| 6542 |
|
| 6543 |
void unit_set_freezer_state(Unit *u, FreezerState state) { |
| 6544 |
assert(u); |
| 6545 |
assert(state >= 0); |
| 6546 |
assert(state < _FREEZER_STATE_MAX); |
| 6547 |
|
| 6548 |
if (u->freezer_state == state) |
| 6549 |
return; |
| 6550 |
|
| 6551 |
log_unit_debug(u, "Freezer state changed %s -> %s", |
| 6552 |
freezer_state_to_string(u->freezer_state), freezer_state_to_string(state)); |
| 6553 |
|
| 6554 |
u->freezer_state = state; |
| 6555 |
|
| 6556 |
unit_add_to_dbus_queue(u); |
| 6557 |
} |
| 6558 |
|
| 6559 |
void unit_freezer_complete(Unit *u, FreezerState kernel_state) { |
| 6560 |
bool expected; |
| 6561 |
|
| 6562 |
assert(u); |
| 6563 |
assert(IN_SET(kernel_state, FREEZER_RUNNING, FREEZER_FROZEN)); |
| 6564 |
|
| 6565 |
expected = IN_SET(u->freezer_state, FREEZER_RUNNING, FREEZER_THAWING) == (kernel_state == FREEZER_RUNNING); |
| 6566 |
|
| 6567 |
unit_set_freezer_state(u, expected ? freezer_state_finish(u->freezer_state) : kernel_state); |
| 6568 |
log_unit_info(u, "Unit now %s.", u->freezer_state == FREEZER_RUNNING ? "thawed" : |
| 6569 |
freezer_state_to_string(u->freezer_state)); |
| 6570 |
|
| 6571 |
/* If the cgroup's final state is against what's requested by us, report as canceled. */ |
| 6572 |
bus_unit_send_pending_freezer_message(u, /* canceled= */ !expected); |
| 6573 |
} |
| 6574 |
|
| 6575 |
int unit_freezer_action(Unit *u, FreezerAction action) { |
| 6576 |
UnitActiveState s; |
| 6577 |
int r; |
| 6578 |
|
| 6579 |
assert(u); |
| 6580 |
assert(IN_SET(action, FREEZER_FREEZE, FREEZER_THAW)); |
| 6581 |
|
| 6582 |
if (!unit_can_freeze(u)) |
| 6583 |
return -EOPNOTSUPP; |
| 6584 |
|
| 6585 |
if (u->job) |
| 6586 |
return -EBUSY; |
| 6587 |
|
| 6588 |
if (u->load_state != UNIT_LOADED) |
| 6589 |
return -EHOSTDOWN; |
| 6590 |
|
| 6591 |
s = unit_active_state(u); |
| 6592 |
if (s != UNIT_ACTIVE) |
| 6593 |
return -EHOSTDOWN; |
| 6594 |
|
| 6595 |
if (action == FREEZER_FREEZE && IN_SET(u->freezer_state, FREEZER_FREEZING, FREEZER_FREEZING_BY_PARENT)) |
| 6596 |
return -EALREADY; |
| 6597 |
if (action == FREEZER_THAW && u->freezer_state == FREEZER_THAWING) |
| 6598 |
return -EALREADY; |
| 6599 |
if (action == FREEZER_THAW && IN_SET(u->freezer_state, FREEZER_FREEZING_BY_PARENT, FREEZER_FROZEN_BY_PARENT)) |
| 6600 |
return -EDEADLK; |
| 6601 |
|
| 6602 |
r = UNIT_VTABLE(u)->freezer_action(u, action); |
| 6603 |
if (r <= 0) |
| 6604 |
return r; |
| 6605 |
|
| 6606 |
assert(IN_SET(u->freezer_state, FREEZER_FREEZING, FREEZER_FREEZING_BY_PARENT, FREEZER_THAWING)); |
| 6607 |
return 1; |
| 6608 |
} |
| 6609 |
|
| 6610 |
Condition *unit_find_failed_condition(Unit *u) { |
| 6611 |
Condition *failed_trigger = NULL; |
| 6612 |
bool has_succeeded_trigger = false; |
| 6613 |
|
| 6614 |
if (u->condition_result) |
| 6615 |
return NULL; |
| 6616 |
|
| 6617 |
LIST_FOREACH(conditions, c, u->conditions) |
| 6618 |
if (c->trigger) { |
| 6619 |
if (c->result == CONDITION_SUCCEEDED) |
| 6620 |
has_succeeded_trigger = true; |
| 6621 |
else if (!failed_trigger) |
| 6622 |
failed_trigger = c; |
| 6623 |
} else if (c->result != CONDITION_SUCCEEDED) |
| 6624 |
return c; |
| 6625 |
|
| 6626 |
return failed_trigger && !has_succeeded_trigger ? failed_trigger : NULL; |
| 6627 |
} |
| 6628 |
|
| 6629 |
int unit_can_live_mount(Unit *u, sd_bus_error *reterr_error) { |
| 6630 |
assert(u); |
| 6631 |
|
| 6632 |
if (!UNIT_VTABLE(u)->live_mount) |
| 6633 |
return sd_bus_error_setf( |
| 6634 |
reterr_error, |
| 6635 |
SD_BUS_ERROR_NOT_SUPPORTED, |
| 6636 |
"Live mounting not supported by unit type '%s'", |
| 6637 |
unit_type_to_string(u->type)); |
| 6638 |
|
| 6639 |
if (u->load_state != UNIT_LOADED) |
| 6640 |
return sd_bus_error_setf( |
| 6641 |
reterr_error, |
| 6642 |
BUS_ERROR_NO_SUCH_UNIT, |
| 6643 |
"Unit '%s' not loaded, cannot live mount", |
| 6644 |
u->id); |
| 6645 |
|
| 6646 |
if (!UNIT_VTABLE(u)->can_live_mount) |
| 6647 |
return 0; |
| 6648 |
|
| 6649 |
return UNIT_VTABLE(u)->can_live_mount(u, reterr_error); |
| 6650 |
} |
| 6651 |
|
| 6652 |
int unit_live_mount( |
| 6653 |
Unit *u, |
| 6654 |
const char *src, |
| 6655 |
const char *dst, |
| 6656 |
sd_bus_message *message, |
| 6657 |
MountInNamespaceFlags flags, |
| 6658 |
const MountOptions *options, |
| 6659 |
sd_bus_error *reterr_error) { |
| 6660 |
|
| 6661 |
assert(u); |
| 6662 |
assert(UNIT_VTABLE(u)->live_mount); |
| 6663 |
|
| 6664 |
if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) { |
| 6665 |
log_unit_debug(u, "Unit not active, cannot perform live mount."); |
| 6666 |
return sd_bus_error_setf( |
| 6667 |
reterr_error, |
| 6668 |
BUS_ERROR_UNIT_INACTIVE, |
| 6669 |
"Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: unit not active", |
| 6670 |
src, |
| 6671 |
dst, |
| 6672 |
u->id); |
| 6673 |
} |
| 6674 |
|
| 6675 |
if (unit_active_state(u) == UNIT_REFRESHING) { |
| 6676 |
log_unit_debug(u, "Unit already live mounting, refusing further requests."); |
| 6677 |
return sd_bus_error_setf( |
| 6678 |
reterr_error, |
| 6679 |
BUS_ERROR_UNIT_BUSY, |
| 6680 |
"Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: another live mount in progress", |
| 6681 |
src, |
| 6682 |
dst, |
| 6683 |
u->id); |
| 6684 |
} |
| 6685 |
|
| 6686 |
if (u->job) { |
| 6687 |
log_unit_debug(u, "Unit already has a job in progress, cannot live mount"); |
| 6688 |
return sd_bus_error_setf( |
| 6689 |
reterr_error, |
| 6690 |
BUS_ERROR_UNIT_BUSY, |
| 6691 |
"Live mounting '%s' on '%s' for unit '%s' cannot be scheduled: another operation in progress", |
| 6692 |
src, |
| 6693 |
dst, |
| 6694 |
u->id); |
| 6695 |
} |
| 6696 |
|
| 6697 |
return UNIT_VTABLE(u)->live_mount(u, src, dst, message, flags, options, reterr_error); |
| 6698 |
} |
| 6699 |
|
| 6700 |
static const char* const collect_mode_table[_COLLECT_MODE_MAX] = { |
| 6701 |
[COLLECT_INACTIVE] = "inactive", |
| 6702 |
[COLLECT_INACTIVE_OR_FAILED] = "inactive-or-failed", |
| 6703 |
}; |
| 6704 |
|
| 6705 |
DEFINE_STRING_TABLE_LOOKUP(collect_mode, CollectMode); |
| 6706 |
|
| 6707 |
Unit* unit_has_dependency(const Unit *u, UnitDependencyAtom atom, Unit *other) { |
| 6708 |
Unit *i; |
| 6709 |
|
| 6710 |
assert(u); |
| 6711 |
|
| 6712 |
/* Checks if the unit has a dependency on 'other' with the specified dependency atom. If 'other' is |
| 6713 |
* NULL checks if the unit has *any* dependency of that atom. Returns 'other' if found (or if 'other' |
| 6714 |
* is NULL the first entry found), or NULL if not found. */ |
| 6715 |
|
| 6716 |
UNIT_FOREACH_DEPENDENCY(i, u, atom) |
| 6717 |
if (!other || other == i) |
| 6718 |
return i; |
| 6719 |
|
| 6720 |
return NULL; |
| 6721 |
} |
| 6722 |
|
| 6723 |
int unit_get_dependency_array(const Unit *u, UnitDependencyAtom atom, Unit ***ret_array) { |
| 6724 |
_cleanup_free_ Unit **array = NULL; |
| 6725 |
size_t n = 0; |
| 6726 |
Unit *other; |
| 6727 |
|
| 6728 |
assert(u); |
| 6729 |
assert(ret_array); |
| 6730 |
|
| 6731 |
/* Gets a list of units matching a specific atom as array. This is useful when iterating through |
| 6732 |
* dependencies while modifying them: the array is an "atomic snapshot" of sorts, that can be read |
| 6733 |
* while the dependency table is continuously updated. */ |
| 6734 |
|
| 6735 |
UNIT_FOREACH_DEPENDENCY(other, u, atom) { |
| 6736 |
if (!GREEDY_REALLOC(array, n + 1)) |
| 6737 |
return -ENOMEM; |
| 6738 |
|
| 6739 |
array[n++] = other; |
| 6740 |
} |
| 6741 |
|
| 6742 |
*ret_array = TAKE_PTR(array); |
| 6743 |
|
| 6744 |
assert(n <= INT_MAX); |
| 6745 |
return (int) n; |
| 6746 |
} |
| 6747 |
|
| 6748 |
int unit_get_transitive_dependency_set(Unit *u, UnitDependencyAtom atom, Set **ret) { |
| 6749 |
_cleanup_set_free_ Set *units = NULL, *queue = NULL; |
| 6750 |
Unit *other; |
| 6751 |
int r; |
| 6752 |
|
| 6753 |
assert(u); |
| 6754 |
assert(ret); |
| 6755 |
|
| 6756 |
/* Similar to unit_get_dependency_array(), but also search the same dependency in other units. */ |
| 6757 |
|
| 6758 |
do { |
| 6759 |
UNIT_FOREACH_DEPENDENCY(other, u, atom) { |
| 6760 |
r = set_ensure_put(&units, NULL, other); |
| 6761 |
if (r < 0) |
| 6762 |
return r; |
| 6763 |
if (r == 0) |
| 6764 |
continue; |
| 6765 |
r = set_ensure_put(&queue, NULL, other); |
| 6766 |
if (r < 0) |
| 6767 |
return r; |
| 6768 |
} |
| 6769 |
} while ((u = set_steal_first(queue))); |
| 6770 |
|
| 6771 |
*ret = TAKE_PTR(units); |
| 6772 |
return 0; |
| 6773 |
} |
| 6774 |
|
| 6775 |
int unit_arm_timer( |
| 6776 |
Unit *u, |
| 6777 |
sd_event_source **source, |
| 6778 |
bool relative, |
| 6779 |
usec_t usec, |
| 6780 |
sd_event_time_handler_t handler) { |
| 6781 |
|
| 6782 |
int r; |
| 6783 |
|
| 6784 |
assert(u); |
| 6785 |
assert(source); |
| 6786 |
assert(handler); |
| 6787 |
|
| 6788 |
if (*source) { |
| 6789 |
if (usec == USEC_INFINITY) |
| 6790 |
return sd_event_source_set_enabled(*source, SD_EVENT_OFF); |
| 6791 |
|
| 6792 |
r = (relative ? sd_event_source_set_time_relative : sd_event_source_set_time)(*source, usec); |
| 6793 |
if (r < 0) |
| 6794 |
return r; |
| 6795 |
|
| 6796 |
return sd_event_source_set_enabled(*source, SD_EVENT_ONESHOT); |
| 6797 |
} |
| 6798 |
|
| 6799 |
if (usec == USEC_INFINITY) |
| 6800 |
return 0; |
| 6801 |
|
| 6802 |
r = (relative ? sd_event_add_time_relative : sd_event_add_time)( |
| 6803 |
u->manager->event, |
| 6804 |
source, |
| 6805 |
CLOCK_MONOTONIC, |
| 6806 |
usec, 0, |
| 6807 |
handler, |
| 6808 |
u); |
| 6809 |
if (r < 0) |
| 6810 |
return r; |
| 6811 |
|
| 6812 |
const char *d = strjoina(unit_type_to_string(u->type), "-timer"); |
| 6813 |
(void) sd_event_source_set_description(*source, d); |
| 6814 |
|
| 6815 |
return 0; |
| 6816 |
} |
| 6817 |
|
| 6818 |
bool unit_passes_filter(Unit *u, char * const *states, char * const *patterns) { |
| 6819 |
assert(u); |
| 6820 |
|
| 6821 |
if (!strv_isempty(states)) { |
| 6822 |
char * const *unit_states = STRV_MAKE( |
| 6823 |
unit_load_state_to_string(u->load_state), |
| 6824 |
unit_active_state_to_string(unit_active_state(u)), |
| 6825 |
unit_sub_state_to_string(u)); |
| 6826 |
|
| 6827 |
if (!strv_overlap(states, unit_states)) |
| 6828 |
return false; |
| 6829 |
} |
| 6830 |
|
| 6831 |
return strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE); |
| 6832 |
} |
| 6833 |
|
| 6834 |
static int unit_get_nice(Unit *u) { |
| 6835 |
ExecContext *ec; |
| 6836 |
|
| 6837 |
ec = unit_get_exec_context(u); |
| 6838 |
return ec ? ec->nice : 0; |
| 6839 |
} |
| 6840 |
|
| 6841 |
static uint64_t unit_get_cpu_weight(Unit *u) { |
| 6842 |
CGroupContext *cc; |
| 6843 |
|
| 6844 |
cc = unit_get_cgroup_context(u); |
| 6845 |
return cc ? cgroup_context_cpu_weight(cc, manager_state(u->manager)) : CGROUP_WEIGHT_DEFAULT; |
| 6846 |
} |
| 6847 |
|
| 6848 |
int unit_get_exec_quota_stats(Unit *u, ExecContext *c, ExecDirectoryType dt, uint64_t *ret_usage, uint64_t *ret_limit) { |
| 6849 |
int r; |
| 6850 |
_cleanup_close_ int fd = -EBADF; |
| 6851 |
_cleanup_free_ char *p = NULL, *pp = NULL; |
| 6852 |
|
| 6853 |
assert(u); |
| 6854 |
assert(c); |
| 6855 |
|
| 6856 |
if (c->directories[dt].n_items == 0) { |
| 6857 |
*ret_usage = UINT64_MAX; |
| 6858 |
*ret_limit = UINT64_MAX; |
| 6859 |
return 0; |
| 6860 |
} |
| 6861 |
|
| 6862 |
ExecDirectoryItem *i = &c->directories[dt].items[0]; |
| 6863 |
p = path_join(u->manager->prefix[dt], i->path); |
| 6864 |
if (!p) |
| 6865 |
return log_oom_debug(); |
| 6866 |
|
| 6867 |
if (exec_directory_is_private(c, dt)) { |
| 6868 |
pp = path_join(u->manager->prefix[dt], "private", i->path); |
| 6869 |
if (!pp) |
| 6870 |
return log_oom_debug(); |
| 6871 |
} |
| 6872 |
|
| 6873 |
const char *target_dir = pp ?: p; |
| 6874 |
fd = open(target_dir, O_PATH | O_CLOEXEC | O_DIRECTORY); |
| 6875 |
if (fd < 0) |
| 6876 |
return log_unit_debug_errno(u, errno, "Failed to get exec quota stats: %m"); |
| 6877 |
|
| 6878 |
uint32_t proj_id; |
| 6879 |
r = read_fs_xattr_fd(fd, /* ret_xflags= */ NULL, &proj_id); |
| 6880 |
if (r < 0) |
| 6881 |
return log_unit_debug_errno(u, r, "Failed to get project ID for exec quota stats: %m"); |
| 6882 |
|
| 6883 |
struct dqblk req; |
| 6884 |
r = quota_query_proj_id(fd, proj_id, &req); |
| 6885 |
if (r <= 0) |
| 6886 |
return log_unit_debug_errno(u, r, "Failed to query project ID for exec quota stats: %m"); |
| 6887 |
|
| 6888 |
*ret_usage = req.dqb_curspace; |
| 6889 |
*ret_limit = req.dqb_bhardlimit * QIF_DQBLKSIZE; |
| 6890 |
|
| 6891 |
return r; |
| 6892 |
} |
| 6893 |
|
| 6894 |
int unit_compare_priority(Unit *a, Unit *b) { |
| 6895 |
int ret; |
| 6896 |
|
| 6897 |
ret = CMP(a->type, b->type); |
| 6898 |
if (ret != 0) |
| 6899 |
return -ret; |
| 6900 |
|
| 6901 |
ret = CMP(unit_get_cpu_weight(a), unit_get_cpu_weight(b)); |
| 6902 |
if (ret != 0) |
| 6903 |
return -ret; |
| 6904 |
|
| 6905 |
ret = CMP(unit_get_nice(a), unit_get_nice(b)); |
| 6906 |
if (ret != 0) |
| 6907 |
return ret; |
| 6908 |
|
| 6909 |
return strcmp(a->id, b->id); |
| 6910 |
} |
| 6911 |
|
| 6912 |
const char* unit_log_field(const Unit *u) { |
| 6913 |
return MANAGER_IS_SYSTEM(ASSERT_PTR(u)->manager) ? "UNIT=" : "USER_UNIT="; |
| 6914 |
} |
| 6915 |
|
| 6916 |
const char* unit_invocation_log_field(const Unit *u) { |
| 6917 |
return MANAGER_IS_SYSTEM(ASSERT_PTR(u)->manager) ? "INVOCATION_ID=" : "USER_INVOCATION_ID="; |
| 6918 |
} |
| 6919 |
|
| 6920 |
const ActivationDetailsVTable * const activation_details_vtable[_UNIT_TYPE_MAX] = { |
| 6921 |
[UNIT_PATH] = &activation_details_path_vtable, |
| 6922 |
[UNIT_TIMER] = &activation_details_timer_vtable, |
| 6923 |
}; |
| 6924 |
|
| 6925 |
ActivationDetails *activation_details_new(Unit *trigger_unit) { |
| 6926 |
_cleanup_free_ ActivationDetails *details = NULL; |
| 6927 |
|
| 6928 |
assert(trigger_unit); |
| 6929 |
assert(trigger_unit->type != _UNIT_TYPE_INVALID); |
| 6930 |
assert(trigger_unit->id); |
| 6931 |
|
| 6932 |
details = malloc0(activation_details_vtable[trigger_unit->type]->object_size); |
| 6933 |
if (!details) |
| 6934 |
return NULL; |
| 6935 |
|
| 6936 |
*details = (ActivationDetails) { |
| 6937 |
.n_ref = 1, |
| 6938 |
.trigger_unit_type = trigger_unit->type, |
| 6939 |
}; |
| 6940 |
|
| 6941 |
details->trigger_unit_name = strdup(trigger_unit->id); |
| 6942 |
if (!details->trigger_unit_name) |
| 6943 |
return NULL; |
| 6944 |
|
| 6945 |
if (ACTIVATION_DETAILS_VTABLE(details)->init) |
| 6946 |
ACTIVATION_DETAILS_VTABLE(details)->init(details, trigger_unit); |
| 6947 |
|
| 6948 |
return TAKE_PTR(details); |
| 6949 |
} |
| 6950 |
|
| 6951 |
static ActivationDetails *activation_details_free(ActivationDetails *details) { |
| 6952 |
if (!details) |
| 6953 |
return NULL; |
| 6954 |
|
| 6955 |
if (ACTIVATION_DETAILS_VTABLE(details)->done) |
| 6956 |
ACTIVATION_DETAILS_VTABLE(details)->done(details); |
| 6957 |
|
| 6958 |
free(details->trigger_unit_name); |
| 6959 |
|
| 6960 |
return mfree(details); |
| 6961 |
} |
| 6962 |
|
| 6963 |
void activation_details_serialize(const ActivationDetails *details, FILE *f) { |
| 6964 |
if (!details || details->trigger_unit_type == _UNIT_TYPE_INVALID) |
| 6965 |
return; |
| 6966 |
|
| 6967 |
(void) serialize_item(f, "activation-details-unit-type", unit_type_to_string(details->trigger_unit_type)); |
| 6968 |
if (details->trigger_unit_name) |
| 6969 |
(void) serialize_item(f, "activation-details-unit-name", details->trigger_unit_name); |
| 6970 |
if (ACTIVATION_DETAILS_VTABLE(details)->serialize) |
| 6971 |
ACTIVATION_DETAILS_VTABLE(details)->serialize(details, f); |
| 6972 |
} |
| 6973 |
|
| 6974 |
int activation_details_deserialize(const char *key, const char *value, ActivationDetails **details) { |
| 6975 |
int r; |
| 6976 |
|
| 6977 |
assert(key); |
| 6978 |
assert(value); |
| 6979 |
assert(details); |
| 6980 |
|
| 6981 |
if (!*details) { |
| 6982 |
UnitType t; |
| 6983 |
|
| 6984 |
if (!streq(key, "activation-details-unit-type")) |
| 6985 |
return -EINVAL; |
| 6986 |
|
| 6987 |
t = unit_type_from_string(value); |
| 6988 |
if (t < 0) |
| 6989 |
return t; |
| 6990 |
|
| 6991 |
/* The activation details vtable has defined ops only for path and timer units */ |
| 6992 |
if (!activation_details_vtable[t]) |
| 6993 |
return -EINVAL; |
| 6994 |
|
| 6995 |
*details = malloc0(activation_details_vtable[t]->object_size); |
| 6996 |
if (!*details) |
| 6997 |
return -ENOMEM; |
| 6998 |
|
| 6999 |
**details = (ActivationDetails) { |
| 7000 |
.n_ref = 1, |
| 7001 |
.trigger_unit_type = t, |
| 7002 |
}; |
| 7003 |
|
| 7004 |
return 0; |
| 7005 |
} |
| 7006 |
|
| 7007 |
if (streq(key, "activation-details-unit-name")) { |
| 7008 |
r = free_and_strdup(&(*details)->trigger_unit_name, value); |
| 7009 |
if (r < 0) |
| 7010 |
return r; |
| 7011 |
|
| 7012 |
return 0; |
| 7013 |
} |
| 7014 |
|
| 7015 |
if (ACTIVATION_DETAILS_VTABLE(*details)->deserialize) |
| 7016 |
return ACTIVATION_DETAILS_VTABLE(*details)->deserialize(key, value, details); |
| 7017 |
|
| 7018 |
return -EINVAL; |
| 7019 |
} |
| 7020 |
|
| 7021 |
int activation_details_append_env(const ActivationDetails *details, char ***strv) { |
| 7022 |
int r = 0; |
| 7023 |
|
| 7024 |
assert(strv); |
| 7025 |
|
| 7026 |
if (!details) |
| 7027 |
return 0; |
| 7028 |
|
| 7029 |
if (!isempty(details->trigger_unit_name)) { |
| 7030 |
char *s = strjoin("TRIGGER_UNIT=", details->trigger_unit_name); |
| 7031 |
if (!s) |
| 7032 |
return -ENOMEM; |
| 7033 |
|
| 7034 |
r = strv_consume(strv, TAKE_PTR(s)); |
| 7035 |
if (r < 0) |
| 7036 |
return r; |
| 7037 |
} |
| 7038 |
|
| 7039 |
if (ACTIVATION_DETAILS_VTABLE(details)->append_env) { |
| 7040 |
r = ACTIVATION_DETAILS_VTABLE(details)->append_env(details, strv); |
| 7041 |
if (r < 0) |
| 7042 |
return r; |
| 7043 |
} |
| 7044 |
|
| 7045 |
return r + !isempty(details->trigger_unit_name); /* Return the number of variables added to the env block */ |
| 7046 |
} |
| 7047 |
|
| 7048 |
int activation_details_append_pair(const ActivationDetails *details, char ***strv) { |
| 7049 |
int r = 0; |
| 7050 |
|
| 7051 |
assert(strv); |
| 7052 |
|
| 7053 |
if (!details) |
| 7054 |
return 0; |
| 7055 |
|
| 7056 |
if (!isempty(details->trigger_unit_name)) { |
| 7057 |
r = strv_extend_many(strv, "trigger_unit", details->trigger_unit_name); |
| 7058 |
if (r < 0) |
| 7059 |
return r; |
| 7060 |
} |
| 7061 |
|
| 7062 |
if (ACTIVATION_DETAILS_VTABLE(details)->append_pair) { |
| 7063 |
r = ACTIVATION_DETAILS_VTABLE(details)->append_pair(details, strv); |
| 7064 |
if (r < 0) |
| 7065 |
return r; |
| 7066 |
} |
| 7067 |
|
| 7068 |
return r + !isempty(details->trigger_unit_name); /* Return the number of pairs added to the strv */ |
| 7069 |
} |
| 7070 |
|
| 7071 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(ActivationDetails, activation_details, activation_details_free); |
| 7072 |
|
| 7073 |
static const char* const unit_mount_dependency_type_table[_UNIT_MOUNT_DEPENDENCY_TYPE_MAX] = { |
| 7074 |
[UNIT_MOUNT_WANTS] = "WantsMountsFor", |
| 7075 |
[UNIT_MOUNT_REQUIRES] = "RequiresMountsFor", |
| 7076 |
}; |
| 7077 |
|
| 7078 |
DEFINE_STRING_TABLE_LOOKUP(unit_mount_dependency_type, UnitMountDependencyType); |
| 7079 |
|
| 7080 |
static const char* const oom_policy_table[_OOM_POLICY_MAX] = { |
| 7081 |
[OOM_CONTINUE] = "continue", |
| 7082 |
[OOM_STOP] = "stop", |
| 7083 |
[OOM_KILL] = "kill", |
| 7084 |
}; |
| 7085 |
|
| 7086 |
DEFINE_STRING_TABLE_LOOKUP(oom_policy, OOMPolicy); |
| 7087 |
|
| 7088 |
UnitDependency unit_mount_dependency_type_to_dependency_type(UnitMountDependencyType t) { |
| 7089 |
switch (t) { |
| 7090 |
|
| 7091 |
case UNIT_MOUNT_WANTS: |
| 7092 |
return UNIT_WANTS; |
| 7093 |
|
| 7094 |
case UNIT_MOUNT_REQUIRES: |
| 7095 |
return UNIT_REQUIRES; |
| 7096 |
|
| 7097 |
default: |
| 7098 |
assert_not_reached(); |
| 7099 |
} |
| 7100 |
} |
| 7101 |
|
| 7102 |
int unit_queue_job_check_and_mangle_type( |
| 7103 |
Unit *u, |
| 7104 |
JobType *type, /* input and output */ |
| 7105 |
bool reload_if_possible, |
| 7106 |
sd_bus_error *reterr_error) { |
| 7107 |
|
| 7108 |
JobType t; |
| 7109 |
|
| 7110 |
assert(u); |
| 7111 |
assert(type); |
| 7112 |
|
| 7113 |
t = *type; |
| 7114 |
|
| 7115 |
if (reload_if_possible && unit_can_reload(u)) { |
| 7116 |
if (t == JOB_RESTART) |
| 7117 |
t = JOB_RELOAD_OR_START; |
| 7118 |
else if (t == JOB_TRY_RESTART) |
| 7119 |
t = JOB_TRY_RELOAD; |
| 7120 |
} |
| 7121 |
|
| 7122 |
/* Our transaction logic allows units not properly loaded to be stopped. But if already dead |
| 7123 |
* let's return clear error to caller. */ |
| 7124 |
if (t == JOB_STOP && UNIT_IS_LOAD_ERROR(u->load_state) && unit_active_state(u) == UNIT_INACTIVE) |
| 7125 |
return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id); |
| 7126 |
|
| 7127 |
if ((t == JOB_START && u->refuse_manual_start) || |
| 7128 |
(t == JOB_STOP && u->refuse_manual_stop) || |
| 7129 |
(IN_SET(t, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) || |
| 7130 |
(t == JOB_RELOAD_OR_START && job_type_collapse(t, u) == JOB_START && u->refuse_manual_start)) |
| 7131 |
return sd_bus_error_setf(reterr_error, |
| 7132 |
BUS_ERROR_ONLY_BY_DEPENDENCY, |
| 7133 |
"Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).", |
| 7134 |
u->id); |
| 7135 |
|
| 7136 |
/* dbus-broker issues StartUnit for activation requests, and Type=dbus services automatically |
| 7137 |
* gain dependency on dbus.socket. Therefore, if dbus has a pending stop job, the new start |
| 7138 |
* job that pulls in dbus again would cause job type conflict. Let's avoid that by rejecting |
| 7139 |
* job enqueuing early. |
| 7140 |
* |
| 7141 |
* Note that unlike signal_activation_request(), we can't use unit_inactive_or_pending() |
| 7142 |
* here. StartUnit is a more generic interface, and thus users are allowed to use e.g. systemctl |
| 7143 |
* to start Type=dbus services even when dbus is inactive. */ |
| 7144 |
if (t == JOB_START && u->type == UNIT_SERVICE && SERVICE(u)->type == SERVICE_DBUS) |
| 7145 |
FOREACH_STRING(dbus_unit, SPECIAL_DBUS_SOCKET, SPECIAL_DBUS_SERVICE) { |
| 7146 |
Unit *dbus = manager_get_unit(u->manager, dbus_unit); |
| 7147 |
if (dbus && unit_stop_pending(dbus)) |
| 7148 |
return sd_bus_error_setf(reterr_error, |
| 7149 |
BUS_ERROR_SHUTTING_DOWN, |
| 7150 |
"Operation for unit %s refused, D-Bus is shutting down.", |
| 7151 |
u->id); |
| 7152 |
} |
| 7153 |
|
| 7154 |
*type = t; |
| 7155 |
|
| 7156 |
return 0; |
| 7157 |
} |
| 7158 |
|
| 7159 |
int parse_unit_marker(const char *marker, unsigned *settings, unsigned *mask) { |
| 7160 |
bool some_plus_minus = false, b = true; |
| 7161 |
|
| 7162 |
assert(marker); |
| 7163 |
assert(settings); |
| 7164 |
assert(mask); |
| 7165 |
|
| 7166 |
if (IN_SET(marker[0], '+', '-')) { |
| 7167 |
b = marker[0] == '+'; |
| 7168 |
marker++; |
| 7169 |
some_plus_minus = true; |
| 7170 |
} |
| 7171 |
|
| 7172 |
UnitMarker m = unit_marker_from_string(marker); |
| 7173 |
if (m < 0) |
| 7174 |
return -EINVAL; |
| 7175 |
|
| 7176 |
/* When +- are not used, last one wins, so reset the bitmask before storing the new result */ |
| 7177 |
if (!some_plus_minus) |
| 7178 |
*settings = 0; |
| 7179 |
|
| 7180 |
SET_FLAG(*settings, 1u << m, b); |
| 7181 |
SET_FLAG(*mask, 1u << m, true); |
| 7182 |
|
| 7183 |
return some_plus_minus; |
| 7184 |
} |
| 7185 |
|
| 7186 |
unsigned unit_normalize_markers(unsigned existing_markers, unsigned new_markers) { |
| 7187 |
/* Follow the job merging logic: when new markers conflict with existing ones, the new marker |
| 7188 |
* takes precedence and clears out conflicting existing markers. Then standard normalization |
| 7189 |
* resolves any remaining conflicts. */ |
| 7190 |
|
| 7191 |
/* New stop wins against all existing markers */ |
| 7192 |
if (BIT_SET(new_markers, UNIT_MARKER_NEEDS_STOP)) |
| 7193 |
CLEAR_BITS(existing_markers, UNIT_MARKER_NEEDS_RESTART, UNIT_MARKER_NEEDS_START, UNIT_MARKER_NEEDS_RELOAD); |
| 7194 |
/* New start wins against existing stop */ |
| 7195 |
if (BIT_SET(new_markers, UNIT_MARKER_NEEDS_START)) |
| 7196 |
CLEAR_BIT(existing_markers, UNIT_MARKER_NEEDS_STOP); |
| 7197 |
/* New restart wins against existing start and reload */ |
| 7198 |
if (BIT_SET(new_markers, UNIT_MARKER_NEEDS_RESTART)) |
| 7199 |
CLEAR_BITS(existing_markers, UNIT_MARKER_NEEDS_START, UNIT_MARKER_NEEDS_RELOAD); |
| 7200 |
|
| 7201 |
unsigned markers = existing_markers | new_markers; |
| 7202 |
|
| 7203 |
/* Standard normalization: reload loses against everything */ |
| 7204 |
if (BIT_SET(markers, UNIT_MARKER_NEEDS_RESTART) || BIT_SET(markers, UNIT_MARKER_NEEDS_START) || BIT_SET(markers, UNIT_MARKER_NEEDS_STOP)) |
| 7205 |
CLEAR_BIT(markers, UNIT_MARKER_NEEDS_RELOAD); |
| 7206 |
/* Stop wins against restart and reload */ |
| 7207 |
if (BIT_SET(markers, UNIT_MARKER_NEEDS_STOP)) |
| 7208 |
CLEAR_BITS(markers, UNIT_MARKER_NEEDS_RESTART, UNIT_MARKER_NEEDS_RELOAD); |
| 7209 |
/* Start wins against stop */ |
| 7210 |
if (BIT_SET(markers, UNIT_MARKER_NEEDS_START)) |
| 7211 |
CLEAR_BIT(markers, UNIT_MARKER_NEEDS_STOP); |
| 7212 |
/* Restart wins against start */ |
| 7213 |
if (BITS_SET(markers, UNIT_MARKER_NEEDS_RESTART, UNIT_MARKER_NEEDS_START)) |
| 7214 |
CLEAR_BIT(markers, UNIT_MARKER_NEEDS_START); |
| 7215 |
|
| 7216 |
return markers; |
| 7217 |
} |