udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / core / unit.c
blobf7ddc6dde04fed494abf2de0ec9639148a0918e3
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <sys/prctl.h>
6 #include <unistd.h>
8 #include "sd-id128.h"
9 #include "sd-messages.h"
11 #include "all-units.h"
12 #include "alloc-util.h"
13 #include "bpf-firewall.h"
14 #include "bpf-foreign.h"
15 #include "bpf-socket-bind.h"
16 #include "bus-common-errors.h"
17 #include "bus-internal.h"
18 #include "bus-util.h"
19 #include "cgroup-setup.h"
20 #include "cgroup-util.h"
21 #include "chase.h"
22 #include "core-varlink.h"
23 #include "dbus-unit.h"
24 #include "dbus.h"
25 #include "dropin.h"
26 #include "env-util.h"
27 #include "escape.h"
28 #include "execute.h"
29 #include "fd-util.h"
30 #include "fileio-label.h"
31 #include "fileio.h"
32 #include "format-util.h"
33 #include "id128-util.h"
34 #include "install.h"
35 #include "io-util.h"
36 #include "label-util.h"
37 #include "load-dropin.h"
38 #include "load-fragment.h"
39 #include "log.h"
40 #include "logarithm.h"
41 #include "macro.h"
42 #include "mkdir-label.h"
43 #include "path-util.h"
44 #include "process-util.h"
45 #include "rm-rf.h"
46 #include "serialize.h"
47 #include "set.h"
48 #include "signal-util.h"
49 #include "sparse-endian.h"
50 #include "special.h"
51 #include "specifier.h"
52 #include "stat-util.h"
53 #include "stdio-util.h"
54 #include "string-table.h"
55 #include "string-util.h"
56 #include "strv.h"
57 #include "terminal-util.h"
58 #include "tmpfile-util.h"
59 #include "umask-util.h"
60 #include "unit-name.h"
61 #include "unit.h"
62 #include "user-util.h"
63 #include "virt.h"
64 #if BPF_FRAMEWORK
65 #include "bpf-link.h"
66 #endif
68 /* Thresholds for logging at INFO level about resource consumption */
69 #define MENTIONWORTHY_CPU_NSEC (1 * NSEC_PER_SEC)
70 #define MENTIONWORTHY_IO_BYTES (1024 * 1024ULL)
71 #define MENTIONWORTHY_IP_BYTES (0ULL)
73 /* Thresholds for logging at INFO level about resource consumption */
74 #define NOTICEWORTHY_CPU_NSEC (10*60 * NSEC_PER_SEC) /* 10 minutes */
75 #define NOTICEWORTHY_IO_BYTES (10 * 1024 * 1024ULL) /* 10 MB */
76 #define NOTICEWORTHY_IP_BYTES (128 * 1024 * 1024ULL) /* 128 MB */
78 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
79 [UNIT_SERVICE] = &service_vtable,
80 [UNIT_SOCKET] = &socket_vtable,
81 [UNIT_TARGET] = &target_vtable,
82 [UNIT_DEVICE] = &device_vtable,
83 [UNIT_MOUNT] = &mount_vtable,
84 [UNIT_AUTOMOUNT] = &automount_vtable,
85 [UNIT_SWAP] = &swap_vtable,
86 [UNIT_TIMER] = &timer_vtable,
87 [UNIT_PATH] = &path_vtable,
88 [UNIT_SLICE] = &slice_vtable,
89 [UNIT_SCOPE] = &scope_vtable,
92 Unit* unit_new(Manager *m, size_t size) {
93 Unit *u;
95 assert(m);
96 assert(size >= sizeof(Unit));
98 u = malloc0(size);
99 if (!u)
100 return NULL;
102 u->manager = m;
103 u->type = _UNIT_TYPE_INVALID;
104 u->default_dependencies = true;
105 u->unit_file_state = _UNIT_FILE_STATE_INVALID;
106 u->unit_file_preset = -1;
107 u->on_failure_job_mode = JOB_REPLACE;
108 u->on_success_job_mode = JOB_FAIL;
109 u->cgroup_control_inotify_wd = -1;
110 u->cgroup_memory_inotify_wd = -1;
111 u->job_timeout = USEC_INFINITY;
112 u->job_running_timeout = USEC_INFINITY;
113 u->ref_uid = UID_INVALID;
114 u->ref_gid = GID_INVALID;
115 u->cpu_usage_last = NSEC_INFINITY;
116 u->cgroup_invalidated_mask |= CGROUP_MASK_BPF_FIREWALL;
117 u->failure_action_exit_status = u->success_action_exit_status = -1;
119 u->ip_accounting_ingress_map_fd = -EBADF;
120 u->ip_accounting_egress_map_fd = -EBADF;
121 for (CGroupIOAccountingMetric i = 0; i < _CGROUP_IO_ACCOUNTING_METRIC_MAX; i++)
122 u->io_accounting_last[i] = UINT64_MAX;
124 u->ipv4_allow_map_fd = -EBADF;
125 u->ipv6_allow_map_fd = -EBADF;
126 u->ipv4_deny_map_fd = -EBADF;
127 u->ipv6_deny_map_fd = -EBADF;
129 u->last_section_private = -1;
131 u->start_ratelimit = (RateLimit) { m->default_start_limit_interval, m->default_start_limit_burst };
132 u->auto_start_stop_ratelimit = (const RateLimit) { 10 * USEC_PER_SEC, 16 };
134 return u;
137 int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret) {
138 _cleanup_(unit_freep) Unit *u = NULL;
139 int r;
141 u = unit_new(m, size);
142 if (!u)
143 return -ENOMEM;
145 r = unit_add_name(u, name);
146 if (r < 0)
147 return r;
149 *ret = TAKE_PTR(u);
151 return r;
154 bool unit_has_name(const Unit *u, const char *name) {
155 assert(u);
156 assert(name);
158 return streq_ptr(name, u->id) ||
159 set_contains(u->aliases, name);
162 static void unit_init(Unit *u) {
163 CGroupContext *cc;
164 ExecContext *ec;
165 KillContext *kc;
167 assert(u);
168 assert(u->manager);
169 assert(u->type >= 0);
171 cc = unit_get_cgroup_context(u);
172 if (cc) {
173 cgroup_context_init(cc);
175 /* Copy in the manager defaults into the cgroup
176 * context, _before_ the rest of the settings have
177 * been initialized */
179 cc->cpu_accounting = u->manager->default_cpu_accounting;
180 cc->io_accounting = u->manager->default_io_accounting;
181 cc->blockio_accounting = u->manager->default_blockio_accounting;
182 cc->memory_accounting = u->manager->default_memory_accounting;
183 cc->tasks_accounting = u->manager->default_tasks_accounting;
184 cc->ip_accounting = u->manager->default_ip_accounting;
186 if (u->type != UNIT_SLICE)
187 cc->tasks_max = u->manager->default_tasks_max;
189 cc->memory_pressure_watch = u->manager->default_memory_pressure_watch;
190 cc->memory_pressure_threshold_usec = u->manager->default_memory_pressure_threshold_usec;
193 ec = unit_get_exec_context(u);
194 if (ec) {
195 exec_context_init(ec);
197 if (u->manager->default_oom_score_adjust_set) {
198 ec->oom_score_adjust = u->manager->default_oom_score_adjust;
199 ec->oom_score_adjust_set = true;
202 if (MANAGER_IS_SYSTEM(u->manager))
203 ec->keyring_mode = EXEC_KEYRING_SHARED;
204 else {
205 ec->keyring_mode = EXEC_KEYRING_INHERIT;
207 /* User manager might have its umask redefined by PAM or UMask=. In this
208 * case let the units it manages inherit this value by default. They can
209 * still tune this value through their own unit file */
210 (void) get_process_umask(getpid_cached(), &ec->umask);
214 kc = unit_get_kill_context(u);
215 if (kc)
216 kill_context_init(kc);
218 if (UNIT_VTABLE(u)->init)
219 UNIT_VTABLE(u)->init(u);
222 static int unit_add_alias(Unit *u, char *donated_name) {
223 int r;
225 /* Make sure that u->names is allocated. We may leave u->names
226 * empty if we fail later, but this is not a problem. */
227 r = set_ensure_put(&u->aliases, &string_hash_ops, donated_name);
228 if (r < 0)
229 return r;
230 assert(r > 0);
232 return 0;
235 int unit_add_name(Unit *u, const char *text) {
236 _cleanup_free_ char *name = NULL, *instance = NULL;
237 UnitType t;
238 int r;
240 assert(u);
241 assert(text);
243 if (unit_name_is_valid(text, UNIT_NAME_TEMPLATE)) {
244 if (!u->instance)
245 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL),
246 "instance is not set when adding name '%s': %m", text);
248 r = unit_name_replace_instance(text, u->instance, &name);
249 if (r < 0)
250 return log_unit_debug_errno(u, r,
251 "failed to build instance name from '%s': %m", text);
252 } else {
253 name = strdup(text);
254 if (!name)
255 return -ENOMEM;
258 if (unit_has_name(u, name))
259 return 0;
261 if (hashmap_contains(u->manager->units, name))
262 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST),
263 "unit already exist when adding name '%s': %m", name);
265 if (!unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
266 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL),
267 "name '%s' is invalid: %m", name);
269 t = unit_name_to_type(name);
270 if (t < 0)
271 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL),
272 "failed to derive unit type from name '%s': %m", name);
274 if (u->type != _UNIT_TYPE_INVALID && t != u->type)
275 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL),
276 "unit type is illegal: u->type(%d) and t(%d) for name '%s': %m",
277 u->type, t, name);
279 r = unit_name_to_instance(name, &instance);
280 if (r < 0)
281 return log_unit_debug_errno(u, r, "failed to extract instance from name '%s': %m", name);
283 if (instance && !unit_type_may_template(t))
284 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL), "templates are not allowed for name '%s': %m", name);
286 /* Ensure that this unit either has no instance, or that the instance matches. */
287 if (u->type != _UNIT_TYPE_INVALID && !streq_ptr(u->instance, instance))
288 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EINVAL),
289 "cannot add name %s, the instances don't match (\"%s\" != \"%s\").",
290 name, instance, u->instance);
292 if (u->id && !unit_type_may_alias(t))
293 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST),
294 "cannot add name %s, aliases are not allowed for %s units.",
295 name, unit_type_to_string(t));
297 if (hashmap_size(u->manager->units) >= MANAGER_MAX_NAMES)
298 return log_unit_warning_errno(u, SYNTHETIC_ERRNO(E2BIG), "cannot add name, manager has too many units: %m");
300 /* Add name to the global hashmap first, because that's easier to undo */
301 r = hashmap_put(u->manager->units, name, u);
302 if (r < 0)
303 return log_unit_debug_errno(u, r, "add unit to hashmap failed for name '%s': %m", text);
305 if (u->id) {
306 r = unit_add_alias(u, name); /* unit_add_alias() takes ownership of the name on success */
307 if (r < 0) {
308 hashmap_remove(u->manager->units, name);
309 return r;
311 TAKE_PTR(name);
313 } else {
314 /* A new name, we don't need the set yet. */
315 assert(u->type == _UNIT_TYPE_INVALID);
316 assert(!u->instance);
318 u->type = t;
319 u->id = TAKE_PTR(name);
320 u->instance = TAKE_PTR(instance);
322 LIST_PREPEND(units_by_type, u->manager->units_by_type[t], u);
323 unit_init(u);
326 unit_add_to_dbus_queue(u);
327 return 0;
330 int unit_choose_id(Unit *u, const char *name) {
331 _cleanup_free_ char *t = NULL;
332 char *s;
333 int r;
335 assert(u);
336 assert(name);
338 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
339 if (!u->instance)
340 return -EINVAL;
342 r = unit_name_replace_instance(name, u->instance, &t);
343 if (r < 0)
344 return r;
346 name = t;
349 if (streq_ptr(u->id, name))
350 return 0; /* Nothing to do. */
352 /* Selects one of the aliases of this unit as the id */
353 s = set_get(u->aliases, (char*) name);
354 if (!s)
355 return -ENOENT;
357 if (u->id) {
358 r = set_remove_and_put(u->aliases, name, u->id);
359 if (r < 0)
360 return r;
361 } else
362 assert_se(set_remove(u->aliases, name)); /* see set_get() above… */
364 u->id = s; /* Old u->id is now stored in the set, and s is not stored anywhere */
365 unit_add_to_dbus_queue(u);
367 return 0;
370 int unit_set_description(Unit *u, const char *description) {
371 int r;
373 assert(u);
375 r = free_and_strdup(&u->description, empty_to_null(description));
376 if (r < 0)
377 return r;
378 if (r > 0)
379 unit_add_to_dbus_queue(u);
381 return 0;
384 static bool unit_success_failure_handler_has_jobs(Unit *unit) {
385 Unit *other;
387 UNIT_FOREACH_DEPENDENCY(other, unit, UNIT_ATOM_ON_SUCCESS)
388 if (other->job || other->nop_job)
389 return true;
391 UNIT_FOREACH_DEPENDENCY(other, unit, UNIT_ATOM_ON_FAILURE)
392 if (other->job || other->nop_job)
393 return true;
395 return false;
398 void unit_release_resources(Unit *u) {
399 UnitActiveState state;
400 ExecContext *ec;
402 assert(u);
404 if (u->job || u->nop_job)
405 return;
407 if (u->perpetual)
408 return;
410 state = unit_active_state(u);
411 if (!IN_SET(state, UNIT_INACTIVE, UNIT_FAILED))
412 return;
414 if (unit_will_restart(u))
415 return;
417 ec = unit_get_exec_context(u);
418 if (ec && ec->runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART)
419 exec_context_destroy_runtime_directory(ec, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
421 if (UNIT_VTABLE(u)->release_resources)
422 UNIT_VTABLE(u)->release_resources(u);
425 bool unit_may_gc(Unit *u) {
426 UnitActiveState state;
427 int r;
429 assert(u);
431 /* Checks whether the unit is ready to be unloaded for garbage collection. Returns true when the
432 * unit may be collected, and false if there's some reason to keep it loaded.
434 * References from other units are *not* checked here. Instead, this is done in unit_gc_sweep(), but
435 * using markers to properly collect dependency loops.
438 if (u->job || u->nop_job)
439 return false;
441 if (u->perpetual)
442 return false;
444 /* if we saw a cgroup empty event for this unit, stay around until we processed it so that we remove
445 * the empty cgroup if possible. Similar, process any pending OOM events if they are already queued
446 * before we release the unit. */
447 if (u->in_cgroup_empty_queue || u->in_cgroup_oom_queue)
448 return false;
450 /* Make sure to send out D-Bus events before we unload the unit */
451 if (u->in_dbus_queue)
452 return false;
454 if (sd_bus_track_count(u->bus_track) > 0)
455 return false;
457 state = unit_active_state(u);
459 /* But we keep the unit object around for longer when it is referenced or configured to not be
460 * gc'ed */
461 switch (u->collect_mode) {
463 case COLLECT_INACTIVE:
464 if (state != UNIT_INACTIVE)
465 return false;
467 break;
469 case COLLECT_INACTIVE_OR_FAILED:
470 if (!IN_SET(state, UNIT_INACTIVE, UNIT_FAILED))
471 return false;
473 break;
475 default:
476 assert_not_reached();
479 /* Check if any OnFailure= or on Success= jobs may be pending */
480 if (unit_success_failure_handler_has_jobs(u))
481 return false;
483 if (u->cgroup_path) {
484 /* If the unit has a cgroup, then check whether there's anything in it. If so, we should stay
485 * around. Units with active processes should never be collected. */
487 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path);
488 if (r < 0)
489 log_unit_debug_errno(u, r, "Failed to determine whether cgroup %s is empty: %m", empty_to_root(u->cgroup_path));
490 if (r <= 0)
491 return false;
494 if (!UNIT_VTABLE(u)->may_gc)
495 return true;
497 return UNIT_VTABLE(u)->may_gc(u);
500 void unit_add_to_load_queue(Unit *u) {
501 assert(u);
502 assert(u->type != _UNIT_TYPE_INVALID);
504 if (u->load_state != UNIT_STUB || u->in_load_queue)
505 return;
507 LIST_PREPEND(load_queue, u->manager->load_queue, u);
508 u->in_load_queue = true;
511 void unit_add_to_cleanup_queue(Unit *u) {
512 assert(u);
514 if (u->in_cleanup_queue)
515 return;
517 LIST_PREPEND(cleanup_queue, u->manager->cleanup_queue, u);
518 u->in_cleanup_queue = true;
521 void unit_add_to_gc_queue(Unit *u) {
522 assert(u);
524 if (u->in_gc_queue || u->in_cleanup_queue)
525 return;
527 if (!unit_may_gc(u))
528 return;
530 LIST_PREPEND(gc_queue, u->manager->gc_unit_queue, u);
531 u->in_gc_queue = true;
534 void unit_add_to_dbus_queue(Unit *u) {
535 assert(u);
536 assert(u->type != _UNIT_TYPE_INVALID);
538 if (u->load_state == UNIT_STUB || u->in_dbus_queue)
539 return;
541 /* Shortcut things if nobody cares */
542 if (sd_bus_track_count(u->manager->subscribed) <= 0 &&
543 sd_bus_track_count(u->bus_track) <= 0 &&
544 set_isempty(u->manager->private_buses)) {
545 u->sent_dbus_new_signal = true;
546 return;
549 LIST_PREPEND(dbus_queue, u->manager->dbus_unit_queue, u);
550 u->in_dbus_queue = true;
553 void unit_submit_to_stop_when_unneeded_queue(Unit *u) {
554 assert(u);
556 if (u->in_stop_when_unneeded_queue)
557 return;
559 if (!u->stop_when_unneeded)
560 return;
562 if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
563 return;
565 LIST_PREPEND(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u);
566 u->in_stop_when_unneeded_queue = true;
569 void unit_submit_to_start_when_upheld_queue(Unit *u) {
570 assert(u);
572 if (u->in_start_when_upheld_queue)
573 return;
575 if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)))
576 return;
578 if (!unit_has_dependency(u, UNIT_ATOM_START_STEADILY, NULL))
579 return;
581 LIST_PREPEND(start_when_upheld_queue, u->manager->start_when_upheld_queue, u);
582 u->in_start_when_upheld_queue = true;
585 void unit_submit_to_stop_when_bound_queue(Unit *u) {
586 assert(u);
588 if (u->in_stop_when_bound_queue)
589 return;
591 if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u)))
592 return;
594 if (!unit_has_dependency(u, UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT, NULL))
595 return;
597 LIST_PREPEND(stop_when_bound_queue, u->manager->stop_when_bound_queue, u);
598 u->in_stop_when_bound_queue = true;
601 static bool unit_can_release_resources(Unit *u) {
602 ExecContext *ec;
604 assert(u);
606 if (UNIT_VTABLE(u)->release_resources)
607 return true;
609 ec = unit_get_exec_context(u);
610 if (ec && ec->runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART)
611 return true;
613 return false;
616 void unit_submit_to_release_resources_queue(Unit *u) {
617 assert(u);
619 if (u->in_release_resources_queue)
620 return;
622 if (u->job || u->nop_job)
623 return;
625 if (u->perpetual)
626 return;
628 if (!unit_can_release_resources(u))
629 return;
631 LIST_PREPEND(release_resources_queue, u->manager->release_resources_queue, u);
632 u->in_release_resources_queue = true;
635 static void unit_clear_dependencies(Unit *u) {
636 assert(u);
638 /* Removes all dependencies configured on u and their reverse dependencies. */
640 for (Hashmap *deps; (deps = hashmap_steal_first(u->dependencies));) {
642 for (Unit *other; (other = hashmap_steal_first_key(deps));) {
643 Hashmap *other_deps;
645 HASHMAP_FOREACH(other_deps, other->dependencies)
646 hashmap_remove(other_deps, u);
648 unit_add_to_gc_queue(other);
651 hashmap_free(deps);
654 u->dependencies = hashmap_free(u->dependencies);
657 static void unit_remove_transient(Unit *u) {
658 assert(u);
660 if (!u->transient)
661 return;
663 if (u->fragment_path)
664 (void) unlink(u->fragment_path);
666 STRV_FOREACH(i, u->dropin_paths) {
667 _cleanup_free_ char *p = NULL, *pp = NULL;
669 if (path_extract_directory(*i, &p) < 0) /* Get the drop-in directory from the drop-in file */
670 continue;
672 if (path_extract_directory(p, &pp) < 0) /* Get the config directory from the drop-in directory */
673 continue;
675 /* Only drop transient drop-ins */
676 if (!path_equal(u->manager->lookup_paths.transient, pp))
677 continue;
679 (void) unlink(*i);
680 (void) rmdir(p);
684 static void unit_free_requires_mounts_for(Unit *u) {
685 assert(u);
687 for (;;) {
688 _cleanup_free_ char *path = NULL;
690 path = hashmap_steal_first_key(u->requires_mounts_for);
691 if (!path)
692 break;
693 else {
694 char s[strlen(path) + 1];
696 PATH_FOREACH_PREFIX_MORE(s, path) {
697 char *y;
698 Set *x;
700 x = hashmap_get2(u->manager->units_requiring_mounts_for, s, (void**) &y);
701 if (!x)
702 continue;
704 (void) set_remove(x, u);
706 if (set_isempty(x)) {
707 (void) hashmap_remove(u->manager->units_requiring_mounts_for, y);
708 free(y);
709 set_free(x);
715 u->requires_mounts_for = hashmap_free(u->requires_mounts_for);
718 static void unit_done(Unit *u) {
719 ExecContext *ec;
720 CGroupContext *cc;
722 assert(u);
724 if (u->type < 0)
725 return;
727 if (UNIT_VTABLE(u)->done)
728 UNIT_VTABLE(u)->done(u);
730 ec = unit_get_exec_context(u);
731 if (ec)
732 exec_context_done(ec);
734 cc = unit_get_cgroup_context(u);
735 if (cc)
736 cgroup_context_done(cc);
739 Unit* unit_free(Unit *u) {
740 Unit *slice;
741 char *t;
743 if (!u)
744 return NULL;
746 sd_event_source_disable_unref(u->auto_start_stop_event_source);
748 u->transient_file = safe_fclose(u->transient_file);
750 if (!MANAGER_IS_RELOADING(u->manager))
751 unit_remove_transient(u);
753 bus_unit_send_removed_signal(u);
755 unit_done(u);
757 unit_dequeue_rewatch_pids(u);
759 u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
760 u->bus_track = sd_bus_track_unref(u->bus_track);
761 u->deserialized_refs = strv_free(u->deserialized_refs);
762 u->pending_freezer_invocation = sd_bus_message_unref(u->pending_freezer_invocation);
764 unit_free_requires_mounts_for(u);
766 SET_FOREACH(t, u->aliases)
767 hashmap_remove_value(u->manager->units, t, u);
768 if (u->id)
769 hashmap_remove_value(u->manager->units, u->id, u);
771 if (!sd_id128_is_null(u->invocation_id))
772 hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u);
774 if (u->job) {
775 Job *j = u->job;
776 job_uninstall(j);
777 job_free(j);
780 if (u->nop_job) {
781 Job *j = u->nop_job;
782 job_uninstall(j);
783 job_free(j);
786 /* A unit is being dropped from the tree, make sure our family is realized properly. Do this after we
787 * detach the unit from slice tree in order to eliminate its effect on controller masks. */
788 slice = UNIT_GET_SLICE(u);
789 unit_clear_dependencies(u);
790 if (slice)
791 unit_add_family_to_cgroup_realize_queue(slice);
793 if (u->on_console)
794 manager_unref_console(u->manager);
796 fdset_free(u->initial_socket_bind_link_fds);
797 #if BPF_FRAMEWORK
798 bpf_link_free(u->ipv4_socket_bind_link);
799 bpf_link_free(u->ipv6_socket_bind_link);
800 #endif
802 unit_release_cgroup(u);
804 if (!MANAGER_IS_RELOADING(u->manager))
805 unit_unlink_state_files(u);
807 unit_unref_uid_gid(u, false);
809 (void) manager_update_failed_units(u->manager, u, false);
810 set_remove(u->manager->startup_units, u);
812 unit_unwatch_all_pids(u);
814 while (u->refs_by_target)
815 unit_ref_unset(u->refs_by_target);
817 if (u->type != _UNIT_TYPE_INVALID)
818 LIST_REMOVE(units_by_type, u->manager->units_by_type[u->type], u);
820 if (u->in_load_queue)
821 LIST_REMOVE(load_queue, u->manager->load_queue, u);
823 if (u->in_dbus_queue)
824 LIST_REMOVE(dbus_queue, u->manager->dbus_unit_queue, u);
826 if (u->in_cleanup_queue)
827 LIST_REMOVE(cleanup_queue, u->manager->cleanup_queue, u);
829 if (u->in_gc_queue)
830 LIST_REMOVE(gc_queue, u->manager->gc_unit_queue, u);
832 if (u->in_cgroup_realize_queue)
833 LIST_REMOVE(cgroup_realize_queue, u->manager->cgroup_realize_queue, u);
835 if (u->in_cgroup_empty_queue)
836 LIST_REMOVE(cgroup_empty_queue, u->manager->cgroup_empty_queue, u);
838 if (u->in_cgroup_oom_queue)
839 LIST_REMOVE(cgroup_oom_queue, u->manager->cgroup_oom_queue, u);
841 if (u->in_target_deps_queue)
842 LIST_REMOVE(target_deps_queue, u->manager->target_deps_queue, u);
844 if (u->in_stop_when_unneeded_queue)
845 LIST_REMOVE(stop_when_unneeded_queue, u->manager->stop_when_unneeded_queue, u);
847 if (u->in_start_when_upheld_queue)
848 LIST_REMOVE(start_when_upheld_queue, u->manager->start_when_upheld_queue, u);
850 if (u->in_stop_when_bound_queue)
851 LIST_REMOVE(stop_when_bound_queue, u->manager->stop_when_bound_queue, u);
853 if (u->in_release_resources_queue)
854 LIST_REMOVE(release_resources_queue, u->manager->release_resources_queue, u);
856 bpf_firewall_close(u);
858 hashmap_free(u->bpf_foreign_by_key);
860 bpf_program_free(u->bpf_device_control_installed);
862 #if BPF_FRAMEWORK
863 bpf_link_free(u->restrict_ifaces_ingress_bpf_link);
864 bpf_link_free(u->restrict_ifaces_egress_bpf_link);
865 #endif
866 fdset_free(u->initial_restric_ifaces_link_fds);
868 condition_free_list(u->conditions);
869 condition_free_list(u->asserts);
871 free(u->description);
872 strv_free(u->documentation);
873 free(u->fragment_path);
874 free(u->source_path);
875 strv_free(u->dropin_paths);
876 free(u->instance);
878 free(u->job_timeout_reboot_arg);
879 free(u->reboot_arg);
881 free(u->access_selinux_context);
883 set_free_free(u->aliases);
884 free(u->id);
886 activation_details_unref(u->activation_details);
888 return mfree(u);
891 FreezerState unit_freezer_state(Unit *u) {
892 assert(u);
894 return u->freezer_state;
897 int unit_freezer_state_kernel(Unit *u, FreezerState *ret) {
898 char *values[1] = {};
899 int r;
901 assert(u);
903 r = cg_get_keyed_attribute(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "cgroup.events",
904 STRV_MAKE("frozen"), values);
905 if (r < 0)
906 return r;
908 r = _FREEZER_STATE_INVALID;
910 if (values[0]) {
911 if (streq(values[0], "0"))
912 r = FREEZER_RUNNING;
913 else if (streq(values[0], "1"))
914 r = FREEZER_FROZEN;
917 free(values[0]);
918 *ret = r;
920 return 0;
923 UnitActiveState unit_active_state(Unit *u) {
924 assert(u);
926 if (u->load_state == UNIT_MERGED)
927 return unit_active_state(unit_follow_merge(u));
929 /* After a reload it might happen that a unit is not correctly
930 * loaded but still has a process around. That's why we won't
931 * shortcut failed loading to UNIT_INACTIVE_FAILED. */
933 return UNIT_VTABLE(u)->active_state(u);
936 const char* unit_sub_state_to_string(Unit *u) {
937 assert(u);
939 return UNIT_VTABLE(u)->sub_state_to_string(u);
942 static int unit_merge_names(Unit *u, Unit *other) {
943 char *name;
944 int r;
946 assert(u);
947 assert(other);
949 r = unit_add_alias(u, other->id);
950 if (r < 0)
951 return r;
953 r = set_move(u->aliases, other->aliases);
954 if (r < 0) {
955 set_remove(u->aliases, other->id);
956 return r;
959 TAKE_PTR(other->id);
960 other->aliases = set_free_free(other->aliases);
962 SET_FOREACH(name, u->aliases)
963 assert_se(hashmap_replace(u->manager->units, name, u) == 0);
965 return 0;
968 static int unit_reserve_dependencies(Unit *u, Unit *other) {
969 size_t n_reserve;
970 Hashmap* deps;
971 void *d;
972 int r;
974 assert(u);
975 assert(other);
977 /* Let's reserve some space in the dependency hashmaps so that later on merging the units cannot
978 * fail.
980 * First make some room in the per dependency type hashmaps. Using the summed size of both units'
981 * hashmaps is an estimate that is likely too high since they probably use some of the same
982 * types. But it's never too low, and that's all we need. */
984 n_reserve = MIN(hashmap_size(other->dependencies), LESS_BY((size_t) _UNIT_DEPENDENCY_MAX, hashmap_size(u->dependencies)));
985 if (n_reserve > 0) {
986 r = hashmap_ensure_allocated(&u->dependencies, NULL);
987 if (r < 0)
988 return r;
990 r = hashmap_reserve(u->dependencies, n_reserve);
991 if (r < 0)
992 return r;
995 /* Now, enlarge our per dependency type hashmaps by the number of entries in the same hashmap of the
996 * other unit's dependencies.
998 * NB: If u does not have a dependency set allocated for some dependency type, there is no need to
999 * reserve anything for. In that case other's set will be transferred as a whole to u by
1000 * complete_move(). */
1002 HASHMAP_FOREACH_KEY(deps, d, u->dependencies) {
1003 Hashmap *other_deps;
1005 other_deps = hashmap_get(other->dependencies, d);
1007 r = hashmap_reserve(deps, hashmap_size(other_deps));
1008 if (r < 0)
1009 return r;
1012 return 0;
1015 static bool unit_should_warn_about_dependency(UnitDependency dependency) {
1016 /* Only warn about some unit types */
1017 return IN_SET(dependency,
1018 UNIT_CONFLICTS,
1019 UNIT_CONFLICTED_BY,
1020 UNIT_BEFORE,
1021 UNIT_AFTER,
1022 UNIT_ON_SUCCESS,
1023 UNIT_ON_FAILURE,
1024 UNIT_TRIGGERS,
1025 UNIT_TRIGGERED_BY);
1028 static int unit_per_dependency_type_hashmap_update(
1029 Hashmap *per_type,
1030 Unit *other,
1031 UnitDependencyMask origin_mask,
1032 UnitDependencyMask destination_mask) {
1034 UnitDependencyInfo info;
1035 int r;
1037 assert(other);
1038 assert_cc(sizeof(void*) == sizeof(info));
1040 /* Acquire the UnitDependencyInfo entry for the Unit* we are interested in, and update it if it
1041 * exists, or insert it anew if not. */
1043 info.data = hashmap_get(per_type, other);
1044 if (info.data) {
1045 /* Entry already exists. Add in our mask. */
1047 if (FLAGS_SET(origin_mask, info.origin_mask) &&
1048 FLAGS_SET(destination_mask, info.destination_mask))
1049 return 0; /* NOP */
1051 info.origin_mask |= origin_mask;
1052 info.destination_mask |= destination_mask;
1054 r = hashmap_update(per_type, other, info.data);
1055 } else {
1056 info = (UnitDependencyInfo) {
1057 .origin_mask = origin_mask,
1058 .destination_mask = destination_mask,
1061 r = hashmap_put(per_type, other, info.data);
1063 if (r < 0)
1064 return r;
1066 return 1;
1069 static void unit_merge_dependencies(Unit *u, Unit *other) {
1070 Hashmap *deps;
1071 void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
1072 * since the hashmaps all want it as void pointer. */
1074 assert(u);
1075 assert(other);
1077 if (u == other)
1078 return;
1080 /* First, remove dependency to other. */
1081 HASHMAP_FOREACH_KEY(deps, dt, u->dependencies) {
1082 if (hashmap_remove(deps, other) && unit_should_warn_about_dependency(UNIT_DEPENDENCY_FROM_PTR(dt)))
1083 log_unit_warning(u, "Dependency %s=%s is dropped, as %s is merged into %s.",
1084 unit_dependency_to_string(UNIT_DEPENDENCY_FROM_PTR(dt)),
1085 other->id, other->id, u->id);
1087 if (hashmap_isempty(deps))
1088 hashmap_free(hashmap_remove(u->dependencies, dt));
1091 for (;;) {
1092 _cleanup_hashmap_free_ Hashmap *other_deps = NULL;
1093 UnitDependencyInfo di_back;
1094 Unit *back;
1096 /* Let's focus on one dependency type at a time, that 'other' has defined. */
1097 other_deps = hashmap_steal_first_key_and_value(other->dependencies, &dt);
1098 if (!other_deps)
1099 break; /* done! */
1101 deps = hashmap_get(u->dependencies, dt);
1103 /* Now iterate through all dependencies of this dependency type, of 'other'. We refer to the
1104 * referenced units as 'back'. */
1105 HASHMAP_FOREACH_KEY(di_back.data, back, other_deps) {
1106 Hashmap *back_deps;
1107 void *back_dt;
1109 if (back == u) {
1110 /* This is a dependency pointing back to the unit we want to merge with?
1111 * Suppress it (but warn) */
1112 if (unit_should_warn_about_dependency(UNIT_DEPENDENCY_FROM_PTR(dt)))
1113 log_unit_warning(u, "Dependency %s=%s in %s is dropped, as %s is merged into %s.",
1114 unit_dependency_to_string(UNIT_DEPENDENCY_FROM_PTR(dt)),
1115 u->id, other->id, other->id, u->id);
1117 hashmap_remove(other_deps, back);
1118 continue;
1121 /* Now iterate through all deps of 'back', and fix the ones pointing to 'other' to
1122 * point to 'u' instead. */
1123 HASHMAP_FOREACH_KEY(back_deps, back_dt, back->dependencies) {
1124 UnitDependencyInfo di_move;
1126 di_move.data = hashmap_remove(back_deps, other);
1127 if (!di_move.data)
1128 continue;
1130 assert_se(unit_per_dependency_type_hashmap_update(
1131 back_deps,
1133 di_move.origin_mask,
1134 di_move.destination_mask) >= 0);
1137 /* The target unit already has dependencies of this type, let's then merge this individually. */
1138 if (deps)
1139 assert_se(unit_per_dependency_type_hashmap_update(
1140 deps,
1141 back,
1142 di_back.origin_mask,
1143 di_back.destination_mask) >= 0);
1146 /* Now all references towards 'other' of the current type 'dt' are corrected to point to 'u'.
1147 * Lets's now move the deps of type 'dt' from 'other' to 'u'. If the unit does not have
1148 * dependencies of this type, let's move them per type wholesale. */
1149 if (!deps)
1150 assert_se(hashmap_put(u->dependencies, dt, TAKE_PTR(other_deps)) >= 0);
1153 other->dependencies = hashmap_free(other->dependencies);
1156 int unit_merge(Unit *u, Unit *other) {
1157 int r;
1159 assert(u);
1160 assert(other);
1161 assert(u->manager == other->manager);
1162 assert(u->type != _UNIT_TYPE_INVALID);
1164 other = unit_follow_merge(other);
1166 if (other == u)
1167 return 0;
1169 if (u->type != other->type)
1170 return -EINVAL;
1172 if (!unit_type_may_alias(u->type)) /* Merging only applies to unit names that support aliases */
1173 return -EEXIST;
1175 if (!IN_SET(other->load_state, UNIT_STUB, UNIT_NOT_FOUND))
1176 return -EEXIST;
1178 if (!streq_ptr(u->instance, other->instance))
1179 return -EINVAL;
1181 if (other->job)
1182 return -EEXIST;
1184 if (other->nop_job)
1185 return -EEXIST;
1187 if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
1188 return -EEXIST;
1190 /* Make reservations to ensure merge_dependencies() won't fail. We don't rollback reservations if we
1191 * fail. We don't have a way to undo reservations. A reservation is not a leak. */
1192 r = unit_reserve_dependencies(u, other);
1193 if (r < 0)
1194 return r;
1196 /* Redirect all references */
1197 while (other->refs_by_target)
1198 unit_ref_set(other->refs_by_target, other->refs_by_target->source, u);
1200 /* Merge dependencies */
1201 unit_merge_dependencies(u, other);
1203 /* Merge names. It is better to do that after merging deps, otherwise the log message contains n/a. */
1204 r = unit_merge_names(u, other);
1205 if (r < 0)
1206 return r;
1208 other->load_state = UNIT_MERGED;
1209 other->merged_into = u;
1211 if (!u->activation_details)
1212 u->activation_details = activation_details_ref(other->activation_details);
1214 /* If there is still some data attached to the other node, we
1215 * don't need it anymore, and can free it. */
1216 if (other->load_state != UNIT_STUB)
1217 if (UNIT_VTABLE(other)->done)
1218 UNIT_VTABLE(other)->done(other);
1220 unit_add_to_dbus_queue(u);
1221 unit_add_to_cleanup_queue(other);
1223 return 0;
1226 int unit_merge_by_name(Unit *u, const char *name) {
1227 _cleanup_free_ char *s = NULL;
1228 Unit *other;
1229 int r;
1231 /* Either add name to u, or if a unit with name already exists, merge it with u.
1232 * If name is a template, do the same for name@instance, where instance is u's instance. */
1234 assert(u);
1235 assert(name);
1237 if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
1238 if (!u->instance)
1239 return -EINVAL;
1241 r = unit_name_replace_instance(name, u->instance, &s);
1242 if (r < 0)
1243 return r;
1245 name = s;
1248 other = manager_get_unit(u->manager, name);
1249 if (other)
1250 return unit_merge(u, other);
1252 return unit_add_name(u, name);
1255 Unit* unit_follow_merge(Unit *u) {
1256 assert(u);
1258 while (u->load_state == UNIT_MERGED)
1259 assert_se(u = u->merged_into);
1261 return u;
1264 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
1265 int r;
1267 assert(u);
1268 assert(c);
1270 /* Unlike unit_add_dependency() or friends, this always returns 0 on success. */
1272 if (c->working_directory && !c->working_directory_missing_ok) {
1273 r = unit_require_mounts_for(u, c->working_directory, UNIT_DEPENDENCY_FILE);
1274 if (r < 0)
1275 return r;
1278 if (c->root_directory) {
1279 r = unit_require_mounts_for(u, c->root_directory, UNIT_DEPENDENCY_FILE);
1280 if (r < 0)
1281 return r;
1284 if (c->root_image) {
1285 r = unit_require_mounts_for(u, c->root_image, UNIT_DEPENDENCY_FILE);
1286 if (r < 0)
1287 return r;
1290 for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++) {
1291 if (!u->manager->prefix[dt])
1292 continue;
1294 for (size_t i = 0; i < c->directories[dt].n_items; i++) {
1295 _cleanup_free_ char *p = NULL;
1297 p = path_join(u->manager->prefix[dt], c->directories[dt].items[i].path);
1298 if (!p)
1299 return -ENOMEM;
1301 r = unit_require_mounts_for(u, p, UNIT_DEPENDENCY_FILE);
1302 if (r < 0)
1303 return r;
1307 if (!MANAGER_IS_SYSTEM(u->manager))
1308 return 0;
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;
1320 if (c->private_tmp) {
1322 /* FIXME: for now we make a special case for /tmp and add a weak dependency on
1323 * tmp.mount so /tmp being masked is supported. However there's no reason to treat
1324 * /tmp specifically and masking other mount units should be handled more
1325 * gracefully too, see PR#16894. */
1326 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, "tmp.mount", true, UNIT_DEPENDENCY_FILE);
1327 if (r < 0)
1328 return r;
1330 r = unit_require_mounts_for(u, "/var/tmp", UNIT_DEPENDENCY_FILE);
1331 if (r < 0)
1332 return r;
1334 r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_TMPFILES_SETUP_SERVICE, true, UNIT_DEPENDENCY_FILE);
1335 if (r < 0)
1336 return r;
1339 if (c->root_image) {
1340 /* We need to wait for /dev/loopX to appear when doing RootImage=, hence let's add an
1341 * implicit dependency on udev */
1343 r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_UDEVD_SERVICE, true, UNIT_DEPENDENCY_FILE);
1344 if (r < 0)
1345 return r;
1348 if (!IN_SET(c->std_output,
1349 EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
1350 EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE) &&
1351 !IN_SET(c->std_error,
1352 EXEC_OUTPUT_JOURNAL, EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
1353 EXEC_OUTPUT_KMSG, EXEC_OUTPUT_KMSG_AND_CONSOLE) &&
1354 !c->log_namespace)
1355 return 0;
1357 /* If syslog or kernel logging is requested (or log namespacing is), make sure our own logging daemon
1358 * is run first. */
1360 if (c->log_namespace) {
1361 _cleanup_free_ char *socket_unit = NULL, *varlink_socket_unit = NULL;
1363 r = unit_name_build_from_type("systemd-journald", c->log_namespace, UNIT_SOCKET, &socket_unit);
1364 if (r < 0)
1365 return r;
1367 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, socket_unit, true, UNIT_DEPENDENCY_FILE);
1368 if (r < 0)
1369 return r;
1371 r = unit_name_build_from_type("systemd-journald-varlink", c->log_namespace, UNIT_SOCKET, &varlink_socket_unit);
1372 if (r < 0)
1373 return r;
1375 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, varlink_socket_unit, true, UNIT_DEPENDENCY_FILE);
1376 if (r < 0)
1377 return r;
1378 } else
1379 r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_JOURNALD_SOCKET, true, UNIT_DEPENDENCY_FILE);
1380 if (r < 0)
1381 return r;
1383 if (exec_context_has_credentials(c) && u->manager->prefix[EXEC_DIRECTORY_RUNTIME]) {
1384 _cleanup_free_ char *p = NULL, *m = NULL;
1386 /* Let's make sure the credentials directory of this service is unmounted *after* the service
1387 * itself shuts down. This only matters if mount namespacing is not used for the service, and
1388 * hence the credentials mount appears on the host. */
1390 p = path_join(u->manager->prefix[EXEC_DIRECTORY_RUNTIME], "credentials", u->id);
1391 if (!p)
1392 return -ENOMEM;
1394 r = unit_name_from_path(p, ".mount", &m);
1395 if (r < 0)
1396 return r;
1398 r = unit_add_dependency_by_name(u, UNIT_AFTER, m, /* add_reference= */ true, UNIT_DEPENDENCY_FILE);
1399 if (r < 0)
1400 return r;
1403 return 0;
1406 const char* unit_description(Unit *u) {
1407 assert(u);
1409 if (u->description)
1410 return u->description;
1412 return strna(u->id);
1415 const char* unit_status_string(Unit *u, char **ret_combined_buffer) {
1416 assert(u);
1417 assert(u->id);
1419 /* Return u->id, u->description, or "{u->id} - {u->description}".
1420 * Versions with u->description are only used if it is set.
1421 * The last option is used if configured and the caller provided the 'ret_combined_buffer'
1422 * pointer.
1424 * Note that *ret_combined_buffer may be set to NULL. */
1426 if (!u->description ||
1427 u->manager->status_unit_format == STATUS_UNIT_FORMAT_NAME ||
1428 (u->manager->status_unit_format == STATUS_UNIT_FORMAT_COMBINED && !ret_combined_buffer) ||
1429 streq(u->description, u->id)) {
1431 if (ret_combined_buffer)
1432 *ret_combined_buffer = NULL;
1433 return u->id;
1436 if (ret_combined_buffer) {
1437 if (u->manager->status_unit_format == STATUS_UNIT_FORMAT_COMBINED) {
1438 *ret_combined_buffer = strjoin(u->id, " - ", u->description);
1439 if (*ret_combined_buffer)
1440 return *ret_combined_buffer;
1441 log_oom(); /* Fall back to ->description */
1442 } else
1443 *ret_combined_buffer = NULL;
1446 return u->description;
1449 /* Common implementation for multiple backends */
1450 int unit_load_fragment_and_dropin(Unit *u, bool fragment_required) {
1451 int r;
1453 assert(u);
1455 /* Load a .{service,socket,...} file */
1456 r = unit_load_fragment(u);
1457 if (r < 0)
1458 return r;
1460 if (u->load_state == UNIT_STUB) {
1461 if (fragment_required)
1462 return -ENOENT;
1464 u->load_state = UNIT_LOADED;
1467 /* Load drop-in directory data. If u is an alias, we might be reloading the
1468 * target unit needlessly. But we cannot be sure which drops-ins have already
1469 * been loaded and which not, at least without doing complicated book-keeping,
1470 * so let's always reread all drop-ins. */
1471 r = unit_load_dropin(unit_follow_merge(u));
1472 if (r < 0)
1473 return r;
1475 if (u->source_path) {
1476 struct stat st;
1478 if (stat(u->source_path, &st) >= 0)
1479 u->source_mtime = timespec_load(&st.st_mtim);
1480 else
1481 u->source_mtime = 0;
1484 return 0;
1487 void unit_add_to_target_deps_queue(Unit *u) {
1488 Manager *m = ASSERT_PTR(ASSERT_PTR(u)->manager);
1490 if (u->in_target_deps_queue)
1491 return;
1493 LIST_PREPEND(target_deps_queue, m->target_deps_queue, u);
1494 u->in_target_deps_queue = true;
1497 int unit_add_default_target_dependency(Unit *u, Unit *target) {
1498 assert(u);
1499 assert(target);
1501 if (target->type != UNIT_TARGET)
1502 return 0;
1504 /* Only add the dependency if both units are loaded, so that
1505 * that loop check below is reliable */
1506 if (u->load_state != UNIT_LOADED ||
1507 target->load_state != UNIT_LOADED)
1508 return 0;
1510 /* If either side wants no automatic dependencies, then let's
1511 * skip this */
1512 if (!u->default_dependencies ||
1513 !target->default_dependencies)
1514 return 0;
1516 /* Don't create loops */
1517 if (unit_has_dependency(target, UNIT_ATOM_BEFORE, u))
1518 return 0;
1520 return unit_add_dependency(target, UNIT_AFTER, u, true, UNIT_DEPENDENCY_DEFAULT);
1523 static int unit_add_slice_dependencies(Unit *u) {
1524 Unit *slice;
1525 assert(u);
1527 if (!UNIT_HAS_CGROUP_CONTEXT(u))
1528 return 0;
1530 /* Slice units are implicitly ordered against their parent slices (as this relationship is encoded in the
1531 name), while all other units are ordered based on configuration (as in their case Slice= configures the
1532 relationship). */
1533 UnitDependencyMask mask = u->type == UNIT_SLICE ? UNIT_DEPENDENCY_IMPLICIT : UNIT_DEPENDENCY_FILE;
1535 slice = UNIT_GET_SLICE(u);
1536 if (slice)
1537 return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, slice, true, mask);
1539 if (unit_has_name(u, SPECIAL_ROOT_SLICE))
1540 return 0;
1542 return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_ROOT_SLICE, true, mask);
1545 static int unit_add_mount_dependencies(Unit *u) {
1546 UnitDependencyInfo di;
1547 const char *path;
1548 bool changed = false;
1549 int r;
1551 assert(u);
1553 HASHMAP_FOREACH_KEY(di.data, path, u->requires_mounts_for) {
1554 char prefix[strlen(path) + 1];
1556 PATH_FOREACH_PREFIX_MORE(prefix, path) {
1557 _cleanup_free_ char *p = NULL;
1558 Unit *m;
1560 r = unit_name_from_path(prefix, ".mount", &p);
1561 if (r == -EINVAL)
1562 continue; /* If the path cannot be converted to a mount unit name, then it's
1563 * not manageable as a unit by systemd, and hence we don't need a
1564 * dependency on it. Let's thus silently ignore the issue. */
1565 if (r < 0)
1566 return r;
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 dependencies on
1571 * this unit will be added later during the loading of the mount unit. */
1572 (void) manager_load_unit_prepare(u->manager, p, NULL, NULL, &m);
1573 continue;
1575 if (m == u)
1576 continue;
1578 if (m->load_state != UNIT_LOADED)
1579 continue;
1581 r = unit_add_dependency(u, UNIT_AFTER, m, true, di.origin_mask);
1582 if (r < 0)
1583 return r;
1584 changed = changed || r > 0;
1586 if (m->fragment_path) {
1587 r = unit_add_dependency(u, UNIT_REQUIRES, m, true, di.origin_mask);
1588 if (r < 0)
1589 return r;
1590 changed = changed || r > 0;
1595 return changed;
1598 static int unit_add_oomd_dependencies(Unit *u) {
1599 CGroupContext *c;
1600 CGroupMask mask;
1601 int r;
1603 assert(u);
1605 if (!u->default_dependencies)
1606 return 0;
1608 c = unit_get_cgroup_context(u);
1609 if (!c)
1610 return 0;
1612 bool wants_oomd = c->moom_swap == MANAGED_OOM_KILL || c->moom_mem_pressure == MANAGED_OOM_KILL;
1613 if (!wants_oomd)
1614 return 0;
1616 if (!cg_all_unified())
1617 return 0;
1619 r = cg_mask_supported(&mask);
1620 if (r < 0)
1621 return log_debug_errno(r, "Failed to determine supported controllers: %m");
1623 if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY))
1624 return 0;
1626 return unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, "systemd-oomd.service", true, UNIT_DEPENDENCY_FILE);
1629 static int unit_add_startup_units(Unit *u) {
1630 if (!unit_has_startup_cgroup_constraints(u))
1631 return 0;
1633 return set_ensure_put(&u->manager->startup_units, NULL, u);
1636 static int unit_validate_on_failure_job_mode(
1637 Unit *u,
1638 const char *job_mode_setting,
1639 JobMode job_mode,
1640 const char *dependency_name,
1641 UnitDependencyAtom atom) {
1643 Unit *other, *found = NULL;
1645 if (job_mode != JOB_ISOLATE)
1646 return 0;
1648 UNIT_FOREACH_DEPENDENCY(other, u, atom) {
1649 if (!found)
1650 found = other;
1651 else if (found != other)
1652 return log_unit_error_errno(
1653 u, SYNTHETIC_ERRNO(ENOEXEC),
1654 "More than one %s dependencies specified but %sisolate set. Refusing.",
1655 dependency_name, job_mode_setting);
1658 return 0;
1661 int unit_load(Unit *u) {
1662 int r;
1664 assert(u);
1666 if (u->in_load_queue) {
1667 LIST_REMOVE(load_queue, u->manager->load_queue, u);
1668 u->in_load_queue = false;
1671 if (u->type == _UNIT_TYPE_INVALID)
1672 return -EINVAL;
1674 if (u->load_state != UNIT_STUB)
1675 return 0;
1677 if (u->transient_file) {
1678 /* Finalize transient file: if this is a transient unit file, as soon as we reach unit_load() the setup
1679 * is complete, hence let's synchronize the unit file we just wrote to disk. */
1681 r = fflush_and_check(u->transient_file);
1682 if (r < 0)
1683 goto fail;
1685 u->transient_file = safe_fclose(u->transient_file);
1686 u->fragment_mtime = now(CLOCK_REALTIME);
1689 r = UNIT_VTABLE(u)->load(u);
1690 if (r < 0)
1691 goto fail;
1693 assert(u->load_state != UNIT_STUB);
1695 if (u->load_state == UNIT_LOADED) {
1696 unit_add_to_target_deps_queue(u);
1698 r = unit_add_slice_dependencies(u);
1699 if (r < 0)
1700 goto fail;
1702 r = unit_add_mount_dependencies(u);
1703 if (r < 0)
1704 goto fail;
1706 r = unit_add_oomd_dependencies(u);
1707 if (r < 0)
1708 goto fail;
1710 r = unit_add_startup_units(u);
1711 if (r < 0)
1712 goto fail;
1714 r = unit_validate_on_failure_job_mode(u, "OnSuccessJobMode=", u->on_success_job_mode, "OnSuccess=", UNIT_ATOM_ON_SUCCESS);
1715 if (r < 0)
1716 goto fail;
1718 r = unit_validate_on_failure_job_mode(u, "OnFailureJobMode=", u->on_failure_job_mode, "OnFailure=", UNIT_ATOM_ON_FAILURE);
1719 if (r < 0)
1720 goto fail;
1722 if (u->job_running_timeout != USEC_INFINITY && u->job_running_timeout > u->job_timeout)
1723 log_unit_warning(u, "JobRunningTimeoutSec= is greater than JobTimeoutSec=, it has no effect.");
1725 /* We finished loading, let's ensure our parents recalculate the members mask */
1726 unit_invalidate_cgroup_members_masks(u);
1729 assert((u->load_state != UNIT_MERGED) == !u->merged_into);
1731 unit_add_to_dbus_queue(unit_follow_merge(u));
1732 unit_add_to_gc_queue(u);
1733 (void) manager_varlink_send_managed_oom_update(u);
1735 return 0;
1737 fail:
1738 /* We convert ENOEXEC errors to the UNIT_BAD_SETTING load state here. Configuration parsing code
1739 * should hence return ENOEXEC to ensure units are placed in this state after loading. */
1741 u->load_state = u->load_state == UNIT_STUB ? UNIT_NOT_FOUND :
1742 r == -ENOEXEC ? UNIT_BAD_SETTING :
1743 UNIT_ERROR;
1744 u->load_error = r;
1746 /* Record the timestamp on the cache, so that if the cache gets updated between now and the next time
1747 * an attempt is made to load this unit, we know we need to check again. */
1748 if (u->load_state == UNIT_NOT_FOUND)
1749 u->fragment_not_found_timestamp_hash = u->manager->unit_cache_timestamp_hash;
1751 unit_add_to_dbus_queue(u);
1752 unit_add_to_gc_queue(u);
1754 return log_unit_debug_errno(u, r, "Failed to load configuration: %m");
1757 _printf_(7, 8)
1758 static int log_unit_internal(void *userdata, int level, int error, const char *file, int line, const char *func, const char *format, ...) {
1759 Unit *u = userdata;
1760 va_list ap;
1761 int r;
1763 if (u && !unit_log_level_test(u, level))
1764 return -ERRNO_VALUE(error);
1766 va_start(ap, format);
1767 if (u)
1768 r = log_object_internalv(level, error, file, line, func,
1769 u->manager->unit_log_field,
1770 u->id,
1771 u->manager->invocation_log_field,
1772 u->invocation_id_string,
1773 format, ap);
1774 else
1775 r = log_internalv(level, error, file, line, func, format, ap);
1776 va_end(ap);
1778 return r;
1781 static bool unit_test_condition(Unit *u) {
1782 _cleanup_strv_free_ char **env = NULL;
1783 int r;
1785 assert(u);
1787 dual_timestamp_get(&u->condition_timestamp);
1789 r = manager_get_effective_environment(u->manager, &env);
1790 if (r < 0) {
1791 log_unit_error_errno(u, r, "Failed to determine effective environment: %m");
1792 u->condition_result = true;
1793 } else
1794 u->condition_result = condition_test_list(
1795 u->conditions,
1796 env,
1797 condition_type_to_string,
1798 log_unit_internal,
1801 unit_add_to_dbus_queue(u);
1802 return u->condition_result;
1805 static bool unit_test_assert(Unit *u) {
1806 _cleanup_strv_free_ char **env = NULL;
1807 int r;
1809 assert(u);
1811 dual_timestamp_get(&u->assert_timestamp);
1813 r = manager_get_effective_environment(u->manager, &env);
1814 if (r < 0) {
1815 log_unit_error_errno(u, r, "Failed to determine effective environment: %m");
1816 u->assert_result = CONDITION_ERROR;
1817 } else
1818 u->assert_result = condition_test_list(
1819 u->asserts,
1820 env,
1821 assert_type_to_string,
1822 log_unit_internal,
1825 unit_add_to_dbus_queue(u);
1826 return u->assert_result;
1829 void unit_status_printf(Unit *u, StatusType status_type, const char *status, const char *format, const char *ident) {
1830 if (log_get_show_color()) {
1831 if (u->manager->status_unit_format == STATUS_UNIT_FORMAT_COMBINED && strchr(ident, ' '))
1832 ident = strjoina(ANSI_HIGHLIGHT, u->id, ANSI_NORMAL, " - ", u->description);
1833 else
1834 ident = strjoina(ANSI_HIGHLIGHT, ident, ANSI_NORMAL);
1837 DISABLE_WARNING_FORMAT_NONLITERAL;
1838 manager_status_printf(u->manager, status_type, status, format, ident);
1839 REENABLE_WARNING;
1842 int unit_test_start_limit(Unit *u) {
1843 const char *reason;
1845 assert(u);
1847 if (ratelimit_below(&u->start_ratelimit)) {
1848 u->start_limit_hit = false;
1849 return 0;
1852 log_unit_warning(u, "Start request repeated too quickly.");
1853 u->start_limit_hit = true;
1855 reason = strjoina("unit ", u->id, " failed");
1857 emergency_action(u->manager, u->start_limit_action,
1858 EMERGENCY_ACTION_IS_WATCHDOG|EMERGENCY_ACTION_WARN,
1859 u->reboot_arg, -1, reason);
1861 return -ECANCELED;
1864 bool unit_shall_confirm_spawn(Unit *u) {
1865 assert(u);
1867 if (manager_is_confirm_spawn_disabled(u->manager))
1868 return false;
1870 /* For some reasons units remaining in the same process group
1871 * as PID 1 fail to acquire the console even if it's not used
1872 * by any process. So skip the confirmation question for them. */
1873 return !unit_get_exec_context(u)->same_pgrp;
1876 static bool unit_verify_deps(Unit *u) {
1877 Unit *other;
1879 assert(u);
1881 /* Checks whether all BindsTo= dependencies of this unit are fulfilled — if they are also combined
1882 * with After=. We do not check Requires= or Requisite= here as they only should have an effect on
1883 * the job processing, but do not have any effect afterwards. We don't check BindsTo= dependencies
1884 * that are not used in conjunction with After= as for them any such check would make things entirely
1885 * racy. */
1887 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT) {
1889 if (!unit_has_dependency(u, UNIT_ATOM_AFTER, other))
1890 continue;
1892 if (!UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(other))) {
1893 log_unit_notice(u, "Bound to unit %s, but unit isn't active.", other->id);
1894 return false;
1898 return true;
1901 /* Errors that aren't really errors:
1902 * -EALREADY: Unit is already started.
1903 * -ECOMM: Condition failed
1904 * -EAGAIN: An operation is already in progress. Retry later.
1906 * Errors that are real errors:
1907 * -EBADR: This unit type does not support starting.
1908 * -ECANCELED: Start limit hit, too many requests for now
1909 * -EPROTO: Assert failed
1910 * -EINVAL: Unit not loaded
1911 * -EOPNOTSUPP: Unit type not supported
1912 * -ENOLINK: The necessary dependencies are not fulfilled.
1913 * -ESTALE: This unit has been started before and can't be started a second time
1914 * -ENOENT: This is a triggering unit and unit to trigger is not loaded
1916 int unit_start(Unit *u, ActivationDetails *details) {
1917 UnitActiveState state;
1918 Unit *following;
1919 int r;
1921 assert(u);
1923 /* Let's hold off running start jobs for mount units when /proc/self/mountinfo monitor is ratelimited. */
1924 if (UNIT_VTABLE(u)->subsystem_ratelimited) {
1925 r = UNIT_VTABLE(u)->subsystem_ratelimited(u->manager);
1926 if (r < 0)
1927 return r;
1928 if (r > 0)
1929 return -EAGAIN;
1932 /* If this is already started, then this will succeed. Note that this will even succeed if this unit
1933 * is not startable by the user. This is relied on to detect when we need to wait for units and when
1934 * waiting is finished. */
1935 state = unit_active_state(u);
1936 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
1937 return -EALREADY;
1938 if (state == UNIT_MAINTENANCE)
1939 return -EAGAIN;
1941 /* Units that aren't loaded cannot be started */
1942 if (u->load_state != UNIT_LOADED)
1943 return -EINVAL;
1945 /* Refuse starting scope units more than once */
1946 if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_enter_timestamp))
1947 return -ESTALE;
1949 /* If the conditions were unmet, don't do anything at all. If we already are activating this call might
1950 * still be useful to speed up activation in case there is some hold-off time, but we don't want to
1951 * recheck the condition in that case. */
1952 if (state != UNIT_ACTIVATING &&
1953 !unit_test_condition(u))
1954 return log_unit_debug_errno(u, SYNTHETIC_ERRNO(ECOMM), "Starting requested but condition not met. Not starting unit.");
1956 /* If the asserts failed, fail the entire job */
1957 if (state != UNIT_ACTIVATING &&
1958 !unit_test_assert(u))
1959 return log_unit_notice_errno(u, SYNTHETIC_ERRNO(EPROTO), "Starting requested but asserts failed.");
1961 /* Units of types that aren't supported cannot be started. Note that we do this test only after the
1962 * condition checks, so that we rather return condition check errors (which are usually not
1963 * considered a true failure) than "not supported" errors (which are considered a failure).
1965 if (!unit_type_supported(u->type))
1966 return -EOPNOTSUPP;
1968 /* Let's make sure that the deps really are in order before we start this. Normally the job engine
1969 * should have taken care of this already, but let's check this here again. After all, our
1970 * dependencies might not be in effect anymore, due to a reload or due to an unmet condition. */
1971 if (!unit_verify_deps(u))
1972 return -ENOLINK;
1974 /* Forward to the main object, if we aren't it. */
1975 following = unit_following(u);
1976 if (following) {
1977 log_unit_debug(u, "Redirecting start request from %s to %s.", u->id, following->id);
1978 return unit_start(following, details);
1981 /* Check our ability to start early so that failure conditions don't cause us to enter a busy loop. */
1982 if (UNIT_VTABLE(u)->can_start) {
1983 r = UNIT_VTABLE(u)->can_start(u);
1984 if (r < 0)
1985 return r;
1988 /* If it is stopped, but we cannot start it, then fail */
1989 if (!UNIT_VTABLE(u)->start)
1990 return -EBADR;
1992 /* We don't suppress calls to ->start() here when we are already starting, to allow this request to
1993 * be used as a "hurry up" call, for example when the unit is in some "auto restart" state where it
1994 * waits for a holdoff timer to elapse before it will start again. */
1996 unit_add_to_dbus_queue(u);
1997 unit_cgroup_freezer_action(u, FREEZER_THAW);
1999 if (!u->activation_details) /* Older details object wins */
2000 u->activation_details = activation_details_ref(details);
2002 return UNIT_VTABLE(u)->start(u);
2005 bool unit_can_start(Unit *u) {
2006 assert(u);
2008 if (u->load_state != UNIT_LOADED)
2009 return false;
2011 if (!unit_type_supported(u->type))
2012 return false;
2014 /* Scope units may be started only once */
2015 if (UNIT_VTABLE(u)->once_only && dual_timestamp_is_set(&u->inactive_exit_timestamp))
2016 return false;
2018 return !!UNIT_VTABLE(u)->start;
2021 bool unit_can_isolate(Unit *u) {
2022 assert(u);
2024 return unit_can_start(u) &&
2025 u->allow_isolate;
2028 /* Errors:
2029 * -EBADR: This unit type does not support stopping.
2030 * -EALREADY: Unit is already stopped.
2031 * -EAGAIN: An operation is already in progress. Retry later.
2033 int unit_stop(Unit *u) {
2034 UnitActiveState state;
2035 Unit *following;
2037 assert(u);
2039 state = unit_active_state(u);
2040 if (UNIT_IS_INACTIVE_OR_FAILED(state))
2041 return -EALREADY;
2043 following = unit_following(u);
2044 if (following) {
2045 log_unit_debug(u, "Redirecting stop request from %s to %s.", u->id, following->id);
2046 return unit_stop(following);
2049 if (!UNIT_VTABLE(u)->stop)
2050 return -EBADR;
2052 unit_add_to_dbus_queue(u);
2053 unit_cgroup_freezer_action(u, FREEZER_THAW);
2055 return UNIT_VTABLE(u)->stop(u);
2058 bool unit_can_stop(Unit *u) {
2059 assert(u);
2061 /* Note: if we return true here, it does not mean that the unit may be successfully stopped.
2062 * Extrinsic units follow external state and they may stop following external state changes
2063 * (hence we return true here), but an attempt to do this through the manager will fail. */
2065 if (!unit_type_supported(u->type))
2066 return false;
2068 if (u->perpetual)
2069 return false;
2071 return !!UNIT_VTABLE(u)->stop;
2074 /* Errors:
2075 * -EBADR: This unit type does not support reloading.
2076 * -ENOEXEC: Unit is not started.
2077 * -EAGAIN: An operation is already in progress. Retry later.
2079 int unit_reload(Unit *u) {
2080 UnitActiveState state;
2081 Unit *following;
2083 assert(u);
2085 if (u->load_state != UNIT_LOADED)
2086 return -EINVAL;
2088 if (!unit_can_reload(u))
2089 return -EBADR;
2091 state = unit_active_state(u);
2092 if (state == UNIT_RELOADING)
2093 return -EAGAIN;
2095 if (state != UNIT_ACTIVE)
2096 return log_unit_warning_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "Unit cannot be reloaded because it is inactive.");
2098 following = unit_following(u);
2099 if (following) {
2100 log_unit_debug(u, "Redirecting reload request from %s to %s.", u->id, following->id);
2101 return unit_reload(following);
2104 unit_add_to_dbus_queue(u);
2106 if (!UNIT_VTABLE(u)->reload) {
2107 /* Unit doesn't have a reload function, but we need to propagate the reload anyway */
2108 unit_notify(u, unit_active_state(u), unit_active_state(u), /* reload_success = */ true);
2109 return 0;
2112 unit_cgroup_freezer_action(u, FREEZER_THAW);
2114 return UNIT_VTABLE(u)->reload(u);
2117 bool unit_can_reload(Unit *u) {
2118 assert(u);
2120 if (UNIT_VTABLE(u)->can_reload)
2121 return UNIT_VTABLE(u)->can_reload(u);
2123 if (unit_has_dependency(u, UNIT_ATOM_PROPAGATES_RELOAD_TO, NULL))
2124 return true;
2126 return UNIT_VTABLE(u)->reload;
2129 bool unit_is_unneeded(Unit *u) {
2130 Unit *other;
2131 assert(u);
2133 if (!u->stop_when_unneeded)
2134 return false;
2136 /* Don't clean up while the unit is transitioning or is even inactive. */
2137 if (unit_active_state(u) != UNIT_ACTIVE)
2138 return false;
2139 if (u->job)
2140 return false;
2142 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_PINS_STOP_WHEN_UNNEEDED) {
2143 /* If a dependent unit has a job queued, is active or transitioning, or is marked for
2144 * restart, then don't clean this one up. */
2146 if (other->job)
2147 return false;
2149 if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other)))
2150 return false;
2152 if (unit_will_restart(other))
2153 return false;
2156 return true;
2159 bool unit_is_upheld_by_active(Unit *u, Unit **ret_culprit) {
2160 Unit *other;
2162 assert(u);
2164 /* Checks if the unit needs to be started because it currently is not running, but some other unit
2165 * that is active declared an Uphold= dependencies on it */
2167 if (!UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) || u->job) {
2168 if (ret_culprit)
2169 *ret_culprit = NULL;
2170 return false;
2173 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_START_STEADILY) {
2174 if (other->job)
2175 continue;
2177 if (UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(other))) {
2178 if (ret_culprit)
2179 *ret_culprit = other;
2180 return true;
2184 if (ret_culprit)
2185 *ret_culprit = NULL;
2186 return false;
2189 bool unit_is_bound_by_inactive(Unit *u, Unit **ret_culprit) {
2190 Unit *other;
2192 assert(u);
2194 /* Checks whether this unit is bound to another unit that is inactive, i.e. whether we should stop
2195 * because the other unit is down. */
2197 if (unit_active_state(u) != UNIT_ACTIVE || u->job) {
2198 /* Don't clean up while the unit is transitioning or is even inactive. */
2199 if (ret_culprit)
2200 *ret_culprit = NULL;
2201 return false;
2204 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_CANNOT_BE_ACTIVE_WITHOUT) {
2205 if (other->job)
2206 continue;
2208 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
2209 if (ret_culprit)
2210 *ret_culprit = other;
2212 return true;
2216 if (ret_culprit)
2217 *ret_culprit = NULL;
2218 return false;
2221 static void check_unneeded_dependencies(Unit *u) {
2222 Unit *other;
2223 assert(u);
2225 /* Add all units this unit depends on to the queue that processes StopWhenUnneeded= behaviour. */
2227 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_ADD_STOP_WHEN_UNNEEDED_QUEUE)
2228 unit_submit_to_stop_when_unneeded_queue(other);
2231 static void check_uphold_dependencies(Unit *u) {
2232 Unit *other;
2233 assert(u);
2235 /* Add all units this unit depends on to the queue that processes Uphold= behaviour. */
2237 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_ADD_START_WHEN_UPHELD_QUEUE)
2238 unit_submit_to_start_when_upheld_queue(other);
2241 static void check_bound_by_dependencies(Unit *u) {
2242 Unit *other;
2243 assert(u);
2245 /* Add all units this unit depends on to the queue that processes BindsTo= stop behaviour. */
2247 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_ADD_CANNOT_BE_ACTIVE_WITHOUT_QUEUE)
2248 unit_submit_to_stop_when_bound_queue(other);
2251 static void retroactively_start_dependencies(Unit *u) {
2252 Unit *other;
2254 assert(u);
2255 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
2257 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_RETROACTIVE_START_REPLACE) /* Requires= + BindsTo= */
2258 if (!unit_has_dependency(u, UNIT_ATOM_AFTER, other) &&
2259 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
2260 manager_add_job(u->manager, JOB_START, other, JOB_REPLACE, NULL, NULL, NULL);
2262 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_RETROACTIVE_START_FAIL) /* Wants= */
2263 if (!unit_has_dependency(u, UNIT_ATOM_AFTER, other) &&
2264 !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
2265 manager_add_job(u->manager, JOB_START, other, JOB_FAIL, NULL, NULL, NULL);
2267 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_RETROACTIVE_STOP_ON_START) /* Conflicts= (and inverse) */
2268 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
2269 manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL);
2272 static void retroactively_stop_dependencies(Unit *u) {
2273 Unit *other;
2275 assert(u);
2276 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
2278 /* Pull down units which are bound to us recursively if enabled */
2279 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_RETROACTIVE_STOP_ON_STOP) /* BoundBy= */
2280 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
2281 manager_add_job(u->manager, JOB_STOP, other, JOB_REPLACE, NULL, NULL, NULL);
2284 void unit_start_on_failure(
2285 Unit *u,
2286 const char *dependency_name,
2287 UnitDependencyAtom atom,
2288 JobMode job_mode) {
2290 int n_jobs = -1;
2291 Unit *other;
2292 int r;
2294 assert(u);
2295 assert(dependency_name);
2296 assert(IN_SET(atom, UNIT_ATOM_ON_SUCCESS, UNIT_ATOM_ON_FAILURE));
2298 /* Act on OnFailure= and OnSuccess= dependencies */
2300 UNIT_FOREACH_DEPENDENCY(other, u, atom) {
2301 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2303 if (n_jobs < 0) {
2304 log_unit_info(u, "Triggering %s dependencies.", dependency_name);
2305 n_jobs = 0;
2308 r = manager_add_job(u->manager, JOB_START, other, job_mode, NULL, &error, NULL);
2309 if (r < 0)
2310 log_unit_warning_errno(
2311 u, r, "Failed to enqueue %s job, ignoring: %s",
2312 dependency_name, bus_error_message(&error, r));
2313 n_jobs ++;
2316 if (n_jobs >= 0)
2317 log_unit_debug(u, "Triggering %s dependencies done (%i %s).",
2318 dependency_name, n_jobs, n_jobs == 1 ? "job" : "jobs");
2321 void unit_trigger_notify(Unit *u) {
2322 Unit *other;
2324 assert(u);
2326 UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_TRIGGERED_BY)
2327 if (UNIT_VTABLE(other)->trigger_notify)
2328 UNIT_VTABLE(other)->trigger_notify(other, u);
2331 static int raise_level(int log_level, bool condition_info, bool condition_notice) {
2332 if (condition_notice && log_level > LOG_NOTICE)
2333 return LOG_NOTICE;
2334 if (condition_info && log_level > LOG_INFO)
2335 return LOG_INFO;
2336 return log_level;
2339 static int unit_log_resources(Unit *u) {
2340 struct iovec iovec[1 + _CGROUP_IP_ACCOUNTING_METRIC_MAX + _CGROUP_IO_ACCOUNTING_METRIC_MAX + 4];
2341 bool any_traffic = false, have_ip_accounting = false, any_io = false, have_io_accounting = false;
2342 _cleanup_free_ char *igress = NULL, *egress = NULL, *rr = NULL, *wr = NULL;
2343 int log_level = LOG_DEBUG; /* May be raised if resources consumed over a threshold */
2344 size_t n_message_parts = 0, n_iovec = 0;
2345 char* message_parts[1 + 2 + 2 + 1], *t;
2346 nsec_t nsec = NSEC_INFINITY;
2347 int r;
2348 const char* const ip_fields[_CGROUP_IP_ACCOUNTING_METRIC_MAX] = {
2349 [CGROUP_IP_INGRESS_BYTES] = "IP_METRIC_INGRESS_BYTES",
2350 [CGROUP_IP_INGRESS_PACKETS] = "IP_METRIC_INGRESS_PACKETS",
2351 [CGROUP_IP_EGRESS_BYTES] = "IP_METRIC_EGRESS_BYTES",
2352 [CGROUP_IP_EGRESS_PACKETS] = "IP_METRIC_EGRESS_PACKETS",
2354 const char* const io_fields[_CGROUP_IO_ACCOUNTING_METRIC_MAX] = {
2355 [CGROUP_IO_READ_BYTES] = "IO_METRIC_READ_BYTES",
2356 [CGROUP_IO_WRITE_BYTES] = "IO_METRIC_WRITE_BYTES",
2357 [CGROUP_IO_READ_OPERATIONS] = "IO_METRIC_READ_OPERATIONS",
2358 [CGROUP_IO_WRITE_OPERATIONS] = "IO_METRIC_WRITE_OPERATIONS",
2361 assert(u);
2363 /* Invoked whenever a unit enters failed or dead state. Logs information about consumed resources if resource
2364 * accounting was enabled for a unit. It does this in two ways: a friendly human readable string with reduced
2365 * information and the complete data in structured fields. */
2367 (void) unit_get_cpu_usage(u, &nsec);
2368 if (nsec != NSEC_INFINITY) {
2369 /* Format the CPU time for inclusion in the structured log message */
2370 if (asprintf(&t, "CPU_USAGE_NSEC=%" PRIu64, nsec) < 0) {
2371 r = log_oom();
2372 goto finish;
2374 iovec[n_iovec++] = IOVEC_MAKE_STRING(t);
2376 /* Format the CPU time for inclusion in the human language message string */
2377 t = strjoin("consumed ", FORMAT_TIMESPAN(nsec / NSEC_PER_USEC, USEC_PER_MSEC), " CPU time");
2378 if (!t) {
2379 r = log_oom();
2380 goto finish;
2383 message_parts[n_message_parts++] = t;
2385 log_level = raise_level(log_level,
2386 nsec > MENTIONWORTHY_CPU_NSEC,
2387 nsec > NOTICEWORTHY_CPU_NSEC);
2390 for (CGroupIOAccountingMetric k = 0; k < _CGROUP_IO_ACCOUNTING_METRIC_MAX; k++) {
2391 uint64_t value = UINT64_MAX;
2393 assert(io_fields[k]);
2395 (void) unit_get_io_accounting(u, k, k > 0, &value);
2396 if (value == UINT64_MAX)
2397 continue;
2399 have_io_accounting = true;
2400 if (value > 0)
2401 any_io = true;
2403 /* Format IO accounting data for inclusion in the structured log message */
2404 if (asprintf(&t, "%s=%" PRIu64, io_fields[k], value) < 0) {
2405 r = log_oom();
2406 goto finish;
2408 iovec[n_iovec++] = IOVEC_MAKE_STRING(t);
2410 /* Format the IO accounting data for inclusion in the human language message string, but only
2411 * for the bytes counters (and not for the operations counters) */
2412 if (k == CGROUP_IO_READ_BYTES) {
2413 assert(!rr);
2414 rr = strjoin("read ", strna(FORMAT_BYTES(value)), " from disk");
2415 if (!rr) {
2416 r = log_oom();
2417 goto finish;
2419 } else if (k == CGROUP_IO_WRITE_BYTES) {
2420 assert(!wr);
2421 wr = strjoin("written ", strna(FORMAT_BYTES(value)), " to disk");
2422 if (!wr) {
2423 r = log_oom();
2424 goto finish;
2428 if (IN_SET(k, CGROUP_IO_READ_BYTES, CGROUP_IO_WRITE_BYTES))
2429 log_level = raise_level(log_level,
2430 value > MENTIONWORTHY_IO_BYTES,
2431 value > NOTICEWORTHY_IO_BYTES);
2434 if (have_io_accounting) {
2435 if (any_io) {
2436 if (rr)
2437 message_parts[n_message_parts++] = TAKE_PTR(rr);
2438 if (wr)
2439 message_parts[n_message_parts++] = TAKE_PTR(wr);
2441 } else {
2442 char *k;
2444 k = strdup("no IO");
2445 if (!k) {
2446 r = log_oom();
2447 goto finish;
2450 message_parts[n_message_parts++] = k;
2454 for (CGroupIPAccountingMetric m = 0; m < _CGROUP_IP_ACCOUNTING_METRIC_MAX; m++) {
2455 uint64_t value = UINT64_MAX;
2457 assert(ip_fields[m]);
2459 (void) unit_get_ip_accounting(u, m, &value);
2460 if (value == UINT64_MAX)
2461 continue;
2463 have_ip_accounting = true;
2464 if (value > 0)
2465 any_traffic = true;
2467 /* Format IP accounting data for inclusion in the structured log message */
2468 if (asprintf(&t, "%s=%" PRIu64, ip_fields[m], value) < 0) {
2469 r = log_oom();
2470 goto finish;
2472 iovec[n_iovec++] = IOVEC_MAKE_STRING(t);
2474 /* Format the IP accounting data for inclusion in the human language message string, but only for the
2475 * bytes counters (and not for the packets counters) */
2476 if (m == CGROUP_IP_INGRESS_BYTES) {
2477 assert(!igress);
2478 igress = strjoin("received ", strna(FORMAT_BYTES(value)), " IP traffic");
2479 if (!igress) {
2480 r = log_oom();
2481 goto finish;
2483 } else if (m == CGROUP_IP_EGRESS_BYTES) {
2484 assert(!egress);
2485 egress = strjoin("sent ", strna(FORMAT_BYTES(value)), " IP traffic");
2486 if (!egress) {
2487 r = log_oom();
2488 goto finish;
2492 if (IN_SET(m, CGROUP_IP_INGRESS_BYTES, CGROUP_IP_EGRESS_BYTES))
2493 log_level = raise_level(log_level,
2494 value > MENTIONWORTHY_IP_BYTES,
2495 value > NOTICEWORTHY_IP_BYTES);
2498 /* This check is here because it is the earliest point following all possible log_level assignments. If
2499 * log_level is assigned anywhere after this point, move this check. */
2500 if (!unit_log_level_test(u, log_level)) {
2501 r = 0;
2502 goto finish;
2505 if (have_ip_accounting) {
2506 if (any_traffic) {
2507 if (igress)
2508 message_parts[n_message_parts++] = TAKE_PTR(igress);
2509 if (egress)
2510 message_parts[n_message_parts++] = TAKE_PTR(egress);
2512 } else {
2513 char *k;
2515 k = strdup("no IP traffic");
2516 if (!k) {
2517 r = log_oom();
2518 goto finish;
2521 message_parts[n_message_parts++] = k;
2525 /* Is there any accounting data available at all? */
2526 if (n_iovec == 0) {
2527 r = 0;
2528 goto finish;
2531 if (n_message_parts == 0)
2532 t = strjoina("MESSAGE=", u->id, ": Completed.");
2533 else {
2534 _cleanup_free_ char *joined = NULL;
2536 message_parts[n_message_parts] = NULL;
2538 joined = strv_join(message_parts, ", ");
2539 if (!joined) {
2540 r = log_oom();
2541 goto finish;
2544 joined[0] = ascii_toupper(joined[0]);
2545 t = strjoina("MESSAGE=", u->id, ": ", joined, ".");
2548 /* The following four fields we allocate on the stack or are static strings, we hence don't want to free them,
2549 * and hence don't increase n_iovec for them */
2550 iovec[n_iovec] = IOVEC_MAKE_STRING(t);
2551 iovec[n_iovec + 1] = IOVEC_MAKE_STRING("MESSAGE_ID=" SD_MESSAGE_UNIT_RESOURCES_STR);
2553 t = strjoina(u->manager->unit_log_field, u->id);
2554 iovec[n_iovec + 2] = IOVEC_MAKE_STRING(t);
2556 t = strjoina(u->manager->invocation_log_field, u->invocation_id_string);
2557 iovec[n_iovec + 3] = IOVEC_MAKE_STRING(t);
2559 log_unit_struct_iovec(u, log_level, iovec, n_iovec + 4);
2560 r = 0;
2562 finish:
2563 for (size_t i = 0; i < n_message_parts; i++)
2564 free(message_parts[i]);
2566 for (size_t i = 0; i < n_iovec; i++)
2567 free(iovec[i].iov_base);
2569 return r;
2573 static void unit_update_on_console(Unit *u) {
2574 bool b;
2576 assert(u);
2578 b = unit_needs_console(u);
2579 if (u->on_console == b)
2580 return;
2582 u->on_console = b;
2583 if (b)
2584 manager_ref_console(u->manager);
2585 else
2586 manager_unref_console(u->manager);
2589 static void unit_emit_audit_start(Unit *u) {
2590 assert(u);
2592 if (UNIT_VTABLE(u)->audit_start_message_type <= 0)
2593 return;
2595 /* Write audit record if we have just finished starting up */
2596 manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_start_message_type, /* success= */ true);
2597 u->in_audit = true;
2600 static void unit_emit_audit_stop(Unit *u, UnitActiveState state) {
2601 assert(u);
2603 if (UNIT_VTABLE(u)->audit_start_message_type <= 0)
2604 return;
2606 if (u->in_audit) {
2607 /* Write audit record if we have just finished shutting down */
2608 manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_stop_message_type, /* success= */ state == UNIT_INACTIVE);
2609 u->in_audit = false;
2610 } else {
2611 /* Hmm, if there was no start record written write it now, so that we always have a nice pair */
2612 manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_start_message_type, /* success= */ state == UNIT_INACTIVE);
2614 if (state == UNIT_INACTIVE)
2615 manager_send_unit_audit(u->manager, u, UNIT_VTABLE(u)->audit_stop_message_type, /* success= */ true);
2619 static bool unit_process_job(Job *j, UnitActiveState ns, bool reload_success) {
2620 bool unexpected = false;
2621 JobResult result;
2623 assert(j);
2625 if (j->state == JOB_WAITING)
2626 /* So we reached a different state for this job. Let's see if we can run it now if it failed previously
2627 * due to EAGAIN. */
2628 job_add_to_run_queue(j);
2630 /* Let's check whether the unit's new state constitutes a finished job, or maybe contradicts a running job and
2631 * hence needs to invalidate jobs. */
2633 switch (j->type) {
2635 case JOB_START:
2636 case JOB_VERIFY_ACTIVE:
2638 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
2639 job_finish_and_invalidate(j, JOB_DONE, true, false);
2640 else if (j->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
2641 unexpected = true;
2643 if (UNIT_IS_INACTIVE_OR_FAILED(ns)) {
2644 if (ns == UNIT_FAILED)
2645 result = JOB_FAILED;
2646 else
2647 result = JOB_DONE;
2649 job_finish_and_invalidate(j, result, true, false);
2653 break;
2655 case JOB_RELOAD:
2656 case JOB_RELOAD_OR_START:
2657 case JOB_TRY_RELOAD:
2659 if (j->state == JOB_RUNNING) {
2660 if (ns == UNIT_ACTIVE)
2661 job_finish_and_invalidate(j, reload_success ? JOB_DONE : JOB_FAILED, true, false);
2662 else if (!IN_SET(ns, UNIT_ACTIVATING, UNIT_RELOADING)) {
2663 unexpected = true;
2665 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
2666 job_finish_and_invalidate(j, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
2670 break;
2672 case JOB_STOP:
2673 case JOB_RESTART:
2674 case JOB_TRY_RESTART:
2676 if (UNIT_IS_INACTIVE_OR_FAILED(ns))
2677 job_finish_and_invalidate(j, JOB_DONE, true, false);
2678 else if (j->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
2679 unexpected = true;
2680 job_finish_and_invalidate(j, JOB_FAILED, true, false);
2683 break;
2685 default:
2686 assert_not_reached();
2689 return unexpected;
2692 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success) {
2693 const char *reason;
2694 Manager *m;
2696 assert(u);
2697 assert(os < _UNIT_ACTIVE_STATE_MAX);
2698 assert(ns < _UNIT_ACTIVE_STATE_MAX);
2700 /* Note that this is called for all low-level state changes, even if they might map to the same high-level
2701 * UnitActiveState! That means that ns == os is an expected behavior here. For example: if a mount point is
2702 * remounted this function will be called too! */
2704 m = u->manager;
2706 /* Let's enqueue the change signal early. In case this unit has a job associated we want that this unit is in
2707 * the bus queue, so that any job change signal queued will force out the unit change signal first. */
2708 unit_add_to_dbus_queue(u);
2710 /* Update systemd-oomd on the property/state change */
2711 if (os != ns) {
2712 /* Always send an update if the unit is going into an inactive state so systemd-oomd knows to stop
2713 * monitoring.
2714 * Also send an update whenever the unit goes active; this is to handle a case where an override file
2715 * sets one of the ManagedOOM*= properties to "kill", then later removes it. systemd-oomd needs to
2716 * know to stop monitoring when the unit changes from "kill" -> "auto" on daemon-reload, but we don't
2717 * have the information on the property. Thus, indiscriminately send an update. */
2718 if (UNIT_IS_INACTIVE_OR_FAILED(ns) || UNIT_IS_ACTIVE_OR_RELOADING(ns))
2719 (void) manager_varlink_send_managed_oom_update(u);
2722 /* Update timestamps for state changes */
2723 if (!MANAGER_IS_RELOADING(m)) {
2724 dual_timestamp_get(&u->state_change_timestamp);
2726 if (UNIT_IS_INACTIVE_OR_FAILED(os) && !UNIT_IS_INACTIVE_OR_FAILED(ns))
2727 u->inactive_exit_timestamp = u->state_change_timestamp;
2728 else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_INACTIVE_OR_FAILED(ns))
2729 u->inactive_enter_timestamp = u->state_change_timestamp;
2731 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
2732 u->active_enter_timestamp = u->state_change_timestamp;
2733 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
2734 u->active_exit_timestamp = u->state_change_timestamp;
2737 /* Keep track of failed units */
2738 (void) manager_update_failed_units(m, u, ns == UNIT_FAILED);
2740 /* Make sure the cgroup and state files are always removed when we become inactive */
2741 if (UNIT_IS_INACTIVE_OR_FAILED(ns)) {
2742 SET_FLAG(u->markers,
2743 (1u << UNIT_MARKER_NEEDS_RELOAD)|(1u << UNIT_MARKER_NEEDS_RESTART),
2744 false);
2745 unit_prune_cgroup(u);
2746 unit_unlink_state_files(u);
2747 } else if (ns != os && ns == UNIT_RELOADING)
2748 SET_FLAG(u->markers, 1u << UNIT_MARKER_NEEDS_RELOAD, false);
2750 unit_update_on_console(u);
2752 if (!MANAGER_IS_RELOADING(m)) {
2753 bool unexpected;
2755 /* Let's propagate state changes to the job */
2756 if (u->job)
2757 unexpected = unit_process_job(u->job, ns, reload_success);
2758 else
2759 unexpected = true;
2761 /* If this state change happened without being requested by a job, then let's retroactively start or
2762 * stop dependencies. We skip that step when deserializing, since we don't want to create any
2763 * additional jobs just because something is already activated. */
2765 if (unexpected) {
2766 if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
2767 retroactively_start_dependencies(u);
2768 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
2769 retroactively_stop_dependencies(u);
2772 if (ns != os && ns == UNIT_FAILED) {
2773 log_unit_debug(u, "Unit entered failed state.");
2774 unit_start_on_failure(u, "OnFailure=", UNIT_ATOM_ON_FAILURE, u->on_failure_job_mode);
2777 if (UNIT_IS_ACTIVE_OR_RELOADING(ns) && !UNIT_IS_ACTIVE_OR_RELOADING(os)) {
2778 /* This unit just finished starting up */
2780 unit_emit_audit_start(u);
2781 manager_send_unit_plymouth(m, u);
2784 if (UNIT_IS_INACTIVE_OR_FAILED(ns) && !UNIT_IS_INACTIVE_OR_FAILED(os)) {
2785 /* This unit just stopped/failed. */
2787 unit_emit_audit_stop(u, ns);
2788 unit_log_resources(u);
2791 if (ns == UNIT_INACTIVE && !IN_SET(os, UNIT_FAILED, UNIT_INACTIVE, UNIT_MAINTENANCE))
2792 unit_start_on_failure(u, "OnSuccess=", UNIT_ATOM_ON_SUCCESS, u->on_success_job_mode);
2795 manager_recheck_journal(m);
2796 manager_recheck_dbus(m);
2798 unit_trigger_notify(u);
2800 if (!MANAGER_IS_RELOADING(m)) {
2801 if (os != UNIT_FAILED && ns == UNIT_FAILED) {
2802 reason = strjoina("unit ", u->id, " failed");
2803 emergency_action(m, u->failure_action, 0, u->reboot_arg, unit_failure_action_exit_status(u), reason);
2804 } else if (!UNIT_IS_INACTIVE_OR_FAILED(os) && ns == UNIT_INACTIVE) {
2805 reason = strjoina("unit ", u->id, " succeeded");
2806 emergency_action(m, u->success_action, 0, u->reboot_arg, unit_success_action_exit_status(u), reason);
2810 /* And now, add the unit or depending units to various queues that will act on the new situation if
2811 * needed. These queues generally check for continuous state changes rather than events (like most of
2812 * the state propagation above), and do work deferred instead of instantly, since they typically
2813 * don't want to run during reloading, and usually involve checking combined state of multiple units
2814 * at once. */
2816 if (UNIT_IS_INACTIVE_OR_FAILED(ns)) {
2817 /* Stop unneeded units and bound-by units regardless if going down was expected or not */
2818 check_unneeded_dependencies(u);
2819 check_bound_by_dependencies(u);
2821 /* Maybe someone wants us to remain up? */
2822 unit_submit_to_start_when_upheld_queue(u);
2824 /* Maybe the unit should be GC'ed now? */
2825 unit_add_to_gc_queue(u);
2827 /* Maybe we can release some resources now? */
2828 unit_submit_to_release_resources_queue(u);
2831 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
2832 /* Start uphold units regardless if going up was expected or not */
2833 check_uphold_dependencies(u);
2835 /* Maybe we finished startup and are now ready for being stopped because unneeded? */
2836 unit_submit_to_stop_when_unneeded_queue(u);
2838 /* Maybe we finished startup, but something we needed has vanished? Let's die then. (This happens
2839 * when something BindsTo= to a Type=oneshot unit, as these units go directly from starting to
2840 * inactive, without ever entering started.) */
2841 unit_submit_to_stop_when_bound_queue(u);
2845 int unit_watch_pid(Unit *u, pid_t pid, bool exclusive) {
2846 int r;
2848 assert(u);
2849 assert(pid_is_valid(pid));
2851 /* Watch a specific PID */
2853 /* Caller might be sure that this PID belongs to this unit only. Let's take this
2854 * opportunity to remove any stalled references to this PID as they can be created
2855 * easily (when watching a process which is not our direct child). */
2856 if (exclusive)
2857 manager_unwatch_pid(u->manager, pid);
2859 r = set_ensure_allocated(&u->pids, NULL);
2860 if (r < 0)
2861 return r;
2863 r = hashmap_ensure_allocated(&u->manager->watch_pids, NULL);
2864 if (r < 0)
2865 return r;
2867 /* First try, let's add the unit keyed by "pid". */
2868 r = hashmap_put(u->manager->watch_pids, PID_TO_PTR(pid), u);
2869 if (r == -EEXIST) {
2870 Unit **array;
2871 bool found = false;
2872 size_t n = 0;
2874 /* OK, the "pid" key is already assigned to a different unit. Let's see if the "-pid" key (which points
2875 * to an array of Units rather than just a Unit), lists us already. */
2877 array = hashmap_get(u->manager->watch_pids, PID_TO_PTR(-pid));
2878 if (array)
2879 for (; array[n]; n++)
2880 if (array[n] == u)
2881 found = true;
2883 if (!found) {
2884 Unit **new_array;
2886 /* Allocate a new array */
2887 new_array = new(Unit*, n + 2);
2888 if (!new_array)
2889 return -ENOMEM;
2891 memcpy_safe(new_array, array, sizeof(Unit*) * n);
2892 new_array[n] = u;
2893 new_array[n+1] = NULL;
2895 /* Add or replace the old array */
2896 r = hashmap_replace(u->manager->watch_pids, PID_TO_PTR(-pid), new_array);
2897 if (r < 0) {
2898 free(new_array);
2899 return r;
2902 free(array);
2904 } else if (r < 0)
2905 return r;
2907 r = set_put(u->pids, PID_TO_PTR(pid));
2908 if (r < 0)
2909 return r;
2911 return 0;
2914 void unit_unwatch_pid(Unit *u, pid_t pid) {
2915 Unit **array;
2917 assert(u);
2918 assert(pid_is_valid(pid));
2920 /* First let's drop the unit in case it's keyed as "pid". */
2921 (void) hashmap_remove_value(u->manager->watch_pids, PID_TO_PTR(pid), u);
2923 /* Then, let's also drop the unit, in case it's in the array keyed by -pid */
2924 array = hashmap_get(u->manager->watch_pids, PID_TO_PTR(-pid));
2925 if (array) {
2926 /* Let's iterate through the array, dropping our own entry */
2928 size_t m = 0;
2929 for (size_t n = 0; array[n]; n++)
2930 if (array[n] != u)
2931 array[m++] = array[n];
2932 array[m] = NULL;
2934 if (m == 0) {
2935 /* The array is now empty, remove the entire entry */
2936 assert_se(hashmap_remove(u->manager->watch_pids, PID_TO_PTR(-pid)) == array);
2937 free(array);
2941 (void) set_remove(u->pids, PID_TO_PTR(pid));
2944 void unit_unwatch_all_pids(Unit *u) {
2945 assert(u);
2947 while (!set_isempty(u->pids))
2948 unit_unwatch_pid(u, PTR_TO_PID(set_first(u->pids)));
2950 u->pids = set_free(u->pids);
2953 static void unit_tidy_watch_pids(Unit *u) {
2954 pid_t except1, except2;
2955 void *e;
2957 assert(u);
2959 /* Cleans dead PIDs from our list */
2961 except1 = unit_main_pid(u);
2962 except2 = unit_control_pid(u);
2964 SET_FOREACH(e, u->pids) {
2965 pid_t pid = PTR_TO_PID(e);
2967 if (pid == except1 || pid == except2)
2968 continue;
2970 if (!pid_is_unwaited(pid))
2971 unit_unwatch_pid(u, pid);
2975 static int on_rewatch_pids_event(sd_event_source *s, void *userdata) {
2976 Unit *u = ASSERT_PTR(userdata);
2978 assert(s);
2980 unit_tidy_watch_pids(u);
2981 unit_watch_all_pids(u);
2983 /* If the PID set is empty now, then let's finish this off. */
2984 unit_synthesize_cgroup_empty_event(u);
2986 return 0;
2989 int unit_enqueue_rewatch_pids(Unit *u) {
2990 int r;
2992 assert(u);
2994 if (!u->cgroup_path)
2995 return -ENOENT;
2997 r = cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER);
2998 if (r < 0)
2999 return r;
3000 if (r > 0) /* On unified we can use proper notifications */
3001 return 0;
3003 /* Enqueues a low-priority job that will clean up dead PIDs from our list of PIDs to watch and subscribe to new
3004 * PIDs that might have appeared. We do this in a delayed job because the work might be quite slow, as it
3005 * involves issuing kill(pid, 0) on all processes we watch. */
3007 if (!u->rewatch_pids_event_source) {
3008 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
3010 r = sd_event_add_defer(u->manager->event, &s, on_rewatch_pids_event, u);
3011 if (r < 0)
3012 return log_error_errno(r, "Failed to allocate event source for tidying watched PIDs: %m");
3014 r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IDLE);
3015 if (r < 0)
3016 return log_error_errno(r, "Failed to adjust priority of event source for tidying watched PIDs: %m");
3018 (void) sd_event_source_set_description(s, "tidy-watch-pids");
3020 u->rewatch_pids_event_source = TAKE_PTR(s);
3023 r = sd_event_source_set_enabled(u->rewatch_pids_event_source, SD_EVENT_ONESHOT);
3024 if (r < 0)
3025 return log_error_errno(r, "Failed to enable event source for tidying watched PIDs: %m");
3027 return 0;
3030 void unit_dequeue_rewatch_pids(Unit *u) {
3031 int r;
3032 assert(u);
3034 if (!u->rewatch_pids_event_source)
3035 return;
3037 r = sd_event_source_set_enabled(u->rewatch_pids_event_source, SD_EVENT_OFF);
3038 if (r < 0)
3039 log_warning_errno(r, "Failed to disable event source for tidying watched PIDs, ignoring: %m");
3041 u->rewatch_pids_event_source = sd_event_source_disable_unref(u->rewatch_pids_event_source);
3044 bool unit_job_is_applicable(Unit *u, JobType j) {
3045 assert(u);
3046 assert(j >= 0 && j < _JOB_TYPE_MAX);
3048 switch (j) {
3050 case JOB_VERIFY_ACTIVE:
3051 case JOB_START:
3052 case JOB_NOP:
3053 /* Note that we don't check unit_can_start() here. That's because .device units and suchlike are not
3054 * startable by us but may appear due to external events, and it thus makes sense to permit enqueuing
3055 * jobs for it. */
3056 return true;
3058 case JOB_STOP:
3059 /* Similar as above. However, perpetual units can never be stopped (neither explicitly nor due to
3060 * external events), hence it makes no sense to permit enqueuing such a request either. */
3061 return !u->perpetual;
3063 case JOB_RESTART:
3064 case JOB_TRY_RESTART:
3065 return unit_can_stop(u) && unit_can_start(u);
3067 case JOB_RELOAD:
3068 case JOB_TRY_RELOAD:
3069 return unit_can_reload(u);
3071 case JOB_RELOAD_OR_START:
3072 return unit_can_reload(u) && unit_can_start(u);
3074 default:
3075 assert_not_reached();
3079 static Hashmap *unit_get_dependency_hashmap_per_type(Unit *u, UnitDependency d) {
3080 Hashmap *deps;
3082 assert(u);
3083 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
3085 deps = hashmap_get(u->dependencies, UNIT_DEPENDENCY_TO_PTR(d));
3086 if (!deps) {
3087 _cleanup_hashmap_free_ Hashmap *h = NULL;
3089 h = hashmap_new(NULL);
3090 if (!h)
3091 return NULL;
3093 if (hashmap_ensure_put(&u->dependencies, NULL, UNIT_DEPENDENCY_TO_PTR(d), h) < 0)
3094 return NULL;
3096 deps = TAKE_PTR(h);
3099 return deps;
3102 typedef enum NotifyDependencyFlags {
3103 NOTIFY_DEPENDENCY_UPDATE_FROM = 1 << 0,
3104 NOTIFY_DEPENDENCY_UPDATE_TO = 1 << 1,
3105 } NotifyDependencyFlags;
3107 static int unit_add_dependency_impl(
3108 Unit *u,
3109 UnitDependency d,
3110 Unit *other,
3111 UnitDependencyMask mask) {
3113 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
3114 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
3115 [UNIT_REQUISITE] = UNIT_REQUISITE_OF,
3116 [UNIT_WANTS] = UNIT_WANTED_BY,
3117 [UNIT_BINDS_TO] = UNIT_BOUND_BY,
3118 [UNIT_PART_OF] = UNIT_CONSISTS_OF,
3119 [UNIT_UPHOLDS] = UNIT_UPHELD_BY,
3120 [UNIT_REQUIRED_BY] = UNIT_REQUIRES,
3121 [UNIT_REQUISITE_OF] = UNIT_REQUISITE,
3122 [UNIT_WANTED_BY] = UNIT_WANTS,
3123 [UNIT_BOUND_BY] = UNIT_BINDS_TO,
3124 [UNIT_CONSISTS_OF] = UNIT_PART_OF,
3125 [UNIT_UPHELD_BY] = UNIT_UPHOLDS,
3126 [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY,
3127 [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS,
3128 [UNIT_BEFORE] = UNIT_AFTER,
3129 [UNIT_AFTER] = UNIT_BEFORE,
3130 [UNIT_ON_SUCCESS] = UNIT_ON_SUCCESS_OF,
3131 [UNIT_ON_SUCCESS_OF] = UNIT_ON_SUCCESS,
3132 [UNIT_ON_FAILURE] = UNIT_ON_FAILURE_OF,
3133 [UNIT_ON_FAILURE_OF] = UNIT_ON_FAILURE,
3134 [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY,
3135 [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS,
3136 [UNIT_PROPAGATES_RELOAD_TO] = UNIT_RELOAD_PROPAGATED_FROM,
3137 [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO,
3138 [UNIT_PROPAGATES_STOP_TO] = UNIT_STOP_PROPAGATED_FROM,
3139 [UNIT_STOP_PROPAGATED_FROM] = UNIT_PROPAGATES_STOP_TO,
3140 [UNIT_JOINS_NAMESPACE_OF] = UNIT_JOINS_NAMESPACE_OF, /* symmetric! 👓 */
3141 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
3142 [UNIT_REFERENCED_BY] = UNIT_REFERENCES,
3143 [UNIT_IN_SLICE] = UNIT_SLICE_OF,
3144 [UNIT_SLICE_OF] = UNIT_IN_SLICE,
3147 Hashmap *u_deps, *other_deps;
3148 UnitDependencyInfo u_info, u_info_old, other_info, other_info_old;
3149 NotifyDependencyFlags flags = 0;
3150 int r;
3152 assert(u);
3153 assert(other);
3154 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
3155 assert(inverse_table[d] >= 0 && inverse_table[d] < _UNIT_DEPENDENCY_MAX);
3156 assert(mask > 0 && mask < _UNIT_DEPENDENCY_MASK_FULL);
3158 /* Ensure the following two hashmaps for each unit exist:
3159 * - the top-level dependency hashmap that maps UnitDependency → Hashmap(Unit* → UnitDependencyInfo),
3160 * - the inner hashmap, that maps Unit* → UnitDependencyInfo, for the specified dependency type. */
3161 u_deps = unit_get_dependency_hashmap_per_type(u, d);
3162 if (!u_deps)
3163 return -ENOMEM;
3165 other_deps = unit_get_dependency_hashmap_per_type(other, inverse_table[d]);
3166 if (!other_deps)
3167 return -ENOMEM;
3169 /* Save the original dependency info. */
3170 u_info.data = u_info_old.data = hashmap_get(u_deps, other);
3171 other_info.data = other_info_old.data = hashmap_get(other_deps, u);
3173 /* Update dependency info. */
3174 u_info.origin_mask |= mask;
3175 other_info.destination_mask |= mask;
3177 /* Save updated dependency info. */
3178 if (u_info.data != u_info_old.data) {
3179 r = hashmap_replace(u_deps, other, u_info.data);
3180 if (r < 0)
3181 return r;
3183 flags = NOTIFY_DEPENDENCY_UPDATE_FROM;
3186 if (other_info.data != other_info_old.data) {
3187 r = hashmap_replace(other_deps, u, other_info.data);
3188 if (r < 0) {
3189 if (u_info.data != u_info_old.data) {
3190 /* Restore the old dependency. */
3191 if (u_info_old.data)
3192 (void) hashmap_update(u_deps, other, u_info_old.data);
3193 else
3194 hashmap_remove(u_deps, other);
3196 return r;
3199 flags |= NOTIFY_DEPENDENCY_UPDATE_TO;
3202 return flags;
3205 int unit_add_dependency(
3206 Unit *u,
3207 UnitDependency d,
3208 Unit *other,
3209 bool add_reference,
3210 UnitDependencyMask mask) {
3212 UnitDependencyAtom a;
3213 int r;
3215 /* Helper to know whether sending a notification is necessary or not: if the dependency is already
3216 * there, no need to notify! */
3217 NotifyDependencyFlags notify_flags;
3219 assert(u);
3220 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
3221 assert(other);
3223 u = unit_follow_merge(u);
3224 other = unit_follow_merge(other);
3225 a = unit_dependency_to_atom(d);
3226 assert(a >= 0);
3228 /* We won't allow dependencies on ourselves. We will not consider them an error however. */
3229 if (u == other) {
3230 if (unit_should_warn_about_dependency(d))
3231 log_unit_warning(u, "Dependency %s=%s is dropped.",
3232 unit_dependency_to_string(d), u->id);
3233 return 0;
3236 if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
3237 return 0;
3239 /* Note that ordering a device unit after a unit is permitted since it allows to start its job
3240 * running timeout at a specific time. */
3241 if (FLAGS_SET(a, UNIT_ATOM_BEFORE) && other->type == UNIT_DEVICE) {
3242 log_unit_warning(u, "Dependency Before=%s ignored (.device units cannot be delayed)", other->id);
3243 return 0;
3246 if (FLAGS_SET(a, UNIT_ATOM_ON_FAILURE) && !UNIT_VTABLE(u)->can_fail) {
3247 log_unit_warning(u, "Requested dependency OnFailure=%s ignored (%s units cannot fail).", other->id, unit_type_to_string(u->type));
3248 return 0;
3251 if (FLAGS_SET(a, UNIT_ATOM_TRIGGERS) && !UNIT_VTABLE(u)->can_trigger)
3252 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
3253 "Requested dependency Triggers=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(u->type));
3254 if (FLAGS_SET(a, UNIT_ATOM_TRIGGERED_BY) && !UNIT_VTABLE(other)->can_trigger)
3255 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
3256 "Requested dependency TriggeredBy=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(other->type));
3258 if (FLAGS_SET(a, UNIT_ATOM_IN_SLICE) && other->type != UNIT_SLICE)
3259 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
3260 "Requested dependency Slice=%s refused (%s is not a slice unit).", other->id, other->id);
3261 if (FLAGS_SET(a, UNIT_ATOM_SLICE_OF) && u->type != UNIT_SLICE)
3262 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
3263 "Requested dependency SliceOf=%s refused (%s is not a slice unit).", other->id, u->id);
3265 if (FLAGS_SET(a, UNIT_ATOM_IN_SLICE) && !UNIT_HAS_CGROUP_CONTEXT(u))
3266 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
3267 "Requested dependency Slice=%s refused (%s is not a cgroup unit).", other->id, u->id);
3269 if (FLAGS_SET(a, UNIT_ATOM_SLICE_OF) && !UNIT_HAS_CGROUP_CONTEXT(other))
3270 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
3271 "Requested dependency SliceOf=%s refused (%s is not a cgroup unit).", other->id, other->id);
3273 r = unit_add_dependency_impl(u, d, other, mask);
3274 if (r < 0)
3275 return r;
3276 notify_flags = r;
3278 if (add_reference) {
3279 r = unit_add_dependency_impl(u, UNIT_REFERENCES, other, mask);
3280 if (r < 0)
3281 return r;
3282 notify_flags |= r;
3285 if (FLAGS_SET(notify_flags, NOTIFY_DEPENDENCY_UPDATE_FROM))
3286 unit_add_to_dbus_queue(u);
3287 if (FLAGS_SET(notify_flags, NOTIFY_DEPENDENCY_UPDATE_TO))
3288 unit_add_to_dbus_queue(other);
3290 return notify_flags != 0;
3293 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask) {
3294 int r, s;
3296 assert(u);
3298 r = unit_add_dependency(u, d, other, add_reference, mask);
3299 if (r < 0)
3300 return r;
3302 s = unit_add_dependency(u, e, other, add_reference, mask);
3303 if (s < 0)
3304 return s;
3306 return r > 0 || s > 0;
3309 static int resolve_template(Unit *u, const char *name, char **buf, const char **ret) {
3310 int r;
3312 assert(u);
3313 assert(name);
3314 assert(buf);
3315 assert(ret);
3317 if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
3318 *buf = NULL;
3319 *ret = name;
3320 return 0;
3323 if (u->instance)
3324 r = unit_name_replace_instance(name, u->instance, buf);
3325 else {
3326 _cleanup_free_ char *i = NULL;
3328 r = unit_name_to_prefix(u->id, &i);
3329 if (r < 0)
3330 return r;
3332 r = unit_name_replace_instance(name, i, buf);
3334 if (r < 0)
3335 return r;
3337 *ret = *buf;
3338 return 0;
3341 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, bool add_reference, UnitDependencyMask mask) {
3342 _cleanup_free_ char *buf = NULL;
3343 Unit *other;
3344 int r;
3346 assert(u);
3347 assert(name);
3349 r = resolve_template(u, name, &buf, &name);
3350 if (r < 0)
3351 return r;
3353 if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
3354 return 0;
3356 r = manager_load_unit(u->manager, name, NULL, NULL, &other);
3357 if (r < 0)
3358 return r;
3360 return unit_add_dependency(u, d, other, add_reference, mask);
3363 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, bool add_reference, UnitDependencyMask mask) {
3364 _cleanup_free_ char *buf = NULL;
3365 Unit *other;
3366 int r;
3368 assert(u);
3369 assert(name);
3371 r = resolve_template(u, name, &buf, &name);
3372 if (r < 0)
3373 return r;
3375 if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
3376 return 0;
3378 r = manager_load_unit(u->manager, name, NULL, NULL, &other);
3379 if (r < 0)
3380 return r;
3382 return unit_add_two_dependencies(u, d, e, other, add_reference, mask);
3385 int set_unit_path(const char *p) {
3386 /* This is mostly for debug purposes */
3387 return RET_NERRNO(setenv("SYSTEMD_UNIT_PATH", p, 1));
3390 char *unit_dbus_path(Unit *u) {
3391 assert(u);
3393 if (!u->id)
3394 return NULL;
3396 return unit_dbus_path_from_name(u->id);
3399 char *unit_dbus_path_invocation_id(Unit *u) {
3400 assert(u);
3402 if (sd_id128_is_null(u->invocation_id))
3403 return NULL;
3405 return unit_dbus_path_from_name(u->invocation_id_string);
3408 int unit_set_invocation_id(Unit *u, sd_id128_t id) {
3409 int r;
3411 assert(u);
3413 /* Set the invocation ID for this unit. If we cannot, this will not roll back, but reset the whole thing. */
3415 if (sd_id128_equal(u->invocation_id, id))
3416 return 0;
3418 if (!sd_id128_is_null(u->invocation_id))
3419 (void) hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u);
3421 if (sd_id128_is_null(id)) {
3422 r = 0;
3423 goto reset;
3426 r = hashmap_ensure_allocated(&u->manager->units_by_invocation_id, &id128_hash_ops);
3427 if (r < 0)
3428 goto reset;
3430 u->invocation_id = id;
3431 sd_id128_to_string(id, u->invocation_id_string);
3433 r = hashmap_put(u->manager->units_by_invocation_id, &u->invocation_id, u);
3434 if (r < 0)
3435 goto reset;
3437 return 0;
3439 reset:
3440 u->invocation_id = SD_ID128_NULL;
3441 u->invocation_id_string[0] = 0;
3442 return r;
3445 int unit_set_slice(Unit *u, Unit *slice) {
3446 int r;
3448 assert(u);
3449 assert(slice);
3451 /* Sets the unit slice if it has not been set before. Is extra careful, to only allow this for units
3452 * that actually have a cgroup context. Also, we don't allow to set this for slices (since the parent
3453 * slice is derived from the name). Make sure the unit we set is actually a slice. */
3455 if (!UNIT_HAS_CGROUP_CONTEXT(u))
3456 return -EOPNOTSUPP;
3458 if (u->type == UNIT_SLICE)
3459 return -EINVAL;
3461 if (unit_active_state(u) != UNIT_INACTIVE)
3462 return -EBUSY;
3464 if (slice->type != UNIT_SLICE)
3465 return -EINVAL;
3467 if (unit_has_name(u, SPECIAL_INIT_SCOPE) &&
3468 !unit_has_name(slice, SPECIAL_ROOT_SLICE))
3469 return -EPERM;
3471 if (UNIT_GET_SLICE(u) == slice)
3472 return 0;
3474 /* Disallow slice changes if @u is already bound to cgroups */
3475 if (UNIT_GET_SLICE(u) && u->cgroup_realized)
3476 return -EBUSY;
3478 /* Remove any slices assigned prior; we should only have one UNIT_IN_SLICE dependency */
3479 if (UNIT_GET_SLICE(u))
3480 unit_remove_dependencies(u, UNIT_DEPENDENCY_SLICE_PROPERTY);
3482 r = unit_add_dependency(u, UNIT_IN_SLICE, slice, true, UNIT_DEPENDENCY_SLICE_PROPERTY);
3483 if (r < 0)
3484 return r;
3486 return 1;
3489 int unit_set_default_slice(Unit *u) {
3490 const char *slice_name;
3491 Unit *slice;
3492 int r;
3494 assert(u);
3496 if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
3497 return 0;
3499 if (UNIT_GET_SLICE(u))
3500 return 0;
3502 if (u->instance) {
3503 _cleanup_free_ char *prefix = NULL, *escaped = NULL;
3505 /* Implicitly place all instantiated units in their
3506 * own per-template slice */
3508 r = unit_name_to_prefix(u->id, &prefix);
3509 if (r < 0)
3510 return r;
3512 /* The prefix is already escaped, but it might include
3513 * "-" which has a special meaning for slice units,
3514 * hence escape it here extra. */
3515 escaped = unit_name_escape(prefix);
3516 if (!escaped)
3517 return -ENOMEM;
3519 if (MANAGER_IS_SYSTEM(u->manager))
3520 slice_name = strjoina("system-", escaped, ".slice");
3521 else
3522 slice_name = strjoina("app-", escaped, ".slice");
3524 } else if (unit_is_extrinsic(u))
3525 /* Keep all extrinsic units (e.g. perpetual units and swap and mount units in user mode) in
3526 * the root slice. They don't really belong in one of the subslices. */
3527 slice_name = SPECIAL_ROOT_SLICE;
3529 else if (MANAGER_IS_SYSTEM(u->manager))
3530 slice_name = SPECIAL_SYSTEM_SLICE;
3531 else
3532 slice_name = SPECIAL_APP_SLICE;
3534 r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice);
3535 if (r < 0)
3536 return r;
3538 return unit_set_slice(u, slice);
3541 const char *unit_slice_name(Unit *u) {
3542 Unit *slice;
3543 assert(u);
3545 slice = UNIT_GET_SLICE(u);
3546 if (!slice)
3547 return NULL;
3549 return slice->id;
3552 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
3553 _cleanup_free_ char *t = NULL;
3554 int r;
3556 assert(u);
3557 assert(type);
3558 assert(_found);
3560 r = unit_name_change_suffix(u->id, type, &t);
3561 if (r < 0)
3562 return r;
3563 if (unit_has_name(u, t))
3564 return -EINVAL;
3566 r = manager_load_unit(u->manager, t, NULL, NULL, _found);
3567 assert(r < 0 || *_found != u);
3568 return r;
3571 static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3572 const char *new_owner;
3573 Unit *u = ASSERT_PTR(userdata);
3574 int r;
3576 assert(message);
3578 r = sd_bus_message_read(message, "sss", NULL, NULL, &new_owner);
3579 if (r < 0) {
3580 bus_log_parse_error(r);
3581 return 0;
3584 if (UNIT_VTABLE(u)->bus_name_owner_change)
3585 UNIT_VTABLE(u)->bus_name_owner_change(u, empty_to_null(new_owner));
3587 return 0;
3590 static int get_name_owner_handler(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3591 const sd_bus_error *e;
3592 const char *new_owner;
3593 Unit *u = ASSERT_PTR(userdata);
3594 int r;
3596 assert(message);
3598 u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
3600 e = sd_bus_message_get_error(message);
3601 if (e) {
3602 if (!sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) {
3603 r = sd_bus_error_get_errno(e);
3604 log_unit_error_errno(u, r,
3605 "Unexpected error response from GetNameOwner(): %s",
3606 bus_error_message(e, r));
3609 new_owner = NULL;
3610 } else {
3611 r = sd_bus_message_read(message, "s", &new_owner);
3612 if (r < 0)
3613 return bus_log_parse_error(r);
3615 assert(!isempty(new_owner));
3618 if (UNIT_VTABLE(u)->bus_name_owner_change)
3619 UNIT_VTABLE(u)->bus_name_owner_change(u, new_owner);
3621 return 0;
3624 int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) {
3625 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
3626 const char *match;
3627 usec_t timeout_usec = 0;
3628 int r;
3630 assert(u);
3631 assert(bus);
3632 assert(name);
3634 if (u->match_bus_slot || u->get_name_owner_slot)
3635 return -EBUSY;
3637 /* NameOwnerChanged and GetNameOwner is used to detect when a service finished starting up. The dbus
3638 * call timeout shouldn't be earlier than that. If we couldn't get the start timeout, use the default
3639 * value defined above. */
3640 if (UNIT_VTABLE(u)->get_timeout_start_usec)
3641 timeout_usec = UNIT_VTABLE(u)->get_timeout_start_usec(u);
3643 match = strjoina("type='signal',"
3644 "sender='org.freedesktop.DBus',"
3645 "path='/org/freedesktop/DBus',"
3646 "interface='org.freedesktop.DBus',"
3647 "member='NameOwnerChanged',"
3648 "arg0='", name, "'");
3650 r = bus_add_match_full(
3651 bus,
3652 &u->match_bus_slot,
3653 true,
3654 match,
3655 signal_name_owner_changed,
3656 NULL,
3658 timeout_usec);
3659 if (r < 0)
3660 return r;
3662 r = sd_bus_message_new_method_call(
3663 bus,
3665 "org.freedesktop.DBus",
3666 "/org/freedesktop/DBus",
3667 "org.freedesktop.DBus",
3668 "GetNameOwner");
3669 if (r < 0)
3670 return r;
3672 r = sd_bus_message_append(m, "s", name);
3673 if (r < 0)
3674 return r;
3676 r = sd_bus_call_async(
3677 bus,
3678 &u->get_name_owner_slot,
3680 get_name_owner_handler,
3682 timeout_usec);
3684 if (r < 0) {
3685 u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
3686 return r;
3689 log_unit_debug(u, "Watching D-Bus name '%s'.", name);
3690 return 0;
3693 int unit_watch_bus_name(Unit *u, const char *name) {
3694 int r;
3696 assert(u);
3697 assert(name);
3699 /* Watch a specific name on the bus. We only support one unit
3700 * watching each name for now. */
3702 if (u->manager->api_bus) {
3703 /* If the bus is already available, install the match directly.
3704 * Otherwise, just put the name in the list. bus_setup_api() will take care later. */
3705 r = unit_install_bus_match(u, u->manager->api_bus, name);
3706 if (r < 0)
3707 return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name);
3710 r = hashmap_put(u->manager->watch_bus, name, u);
3711 if (r < 0) {
3712 u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
3713 u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
3714 return log_warning_errno(r, "Failed to put bus name to hashmap: %m");
3717 return 0;
3720 void unit_unwatch_bus_name(Unit *u, const char *name) {
3721 assert(u);
3722 assert(name);
3724 (void) hashmap_remove_value(u->manager->watch_bus, name, u);
3725 u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
3726 u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
3729 int unit_add_node_dependency(Unit *u, const char *what, UnitDependency dep, UnitDependencyMask mask) {
3730 _cleanup_free_ char *e = NULL;
3731 Unit *device;
3732 int r;
3734 assert(u);
3736 /* Adds in links to the device node that this unit is based on */
3737 if (isempty(what))
3738 return 0;
3740 if (!is_device_path(what))
3741 return 0;
3743 /* When device units aren't supported (such as in a container), don't create dependencies on them. */
3744 if (!unit_type_supported(UNIT_DEVICE))
3745 return 0;
3747 r = unit_name_from_path(what, ".device", &e);
3748 if (r < 0)
3749 return r;
3751 r = manager_load_unit(u->manager, e, NULL, NULL, &device);
3752 if (r < 0)
3753 return r;
3755 if (dep == UNIT_REQUIRES && device_shall_be_bound_by(device, u))
3756 dep = UNIT_BINDS_TO;
3758 return unit_add_two_dependencies(u, UNIT_AFTER,
3759 MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS,
3760 device, true, mask);
3763 int unit_add_blockdev_dependency(Unit *u, const char *what, UnitDependencyMask mask) {
3764 _cleanup_free_ char *escaped = NULL, *target = NULL;
3765 int r;
3767 assert(u);
3769 if (isempty(what))
3770 return 0;
3772 if (!path_startswith(what, "/dev/"))
3773 return 0;
3775 /* If we don't support devices, then also don't bother with blockdev@.target */
3776 if (!unit_type_supported(UNIT_DEVICE))
3777 return 0;
3779 r = unit_name_path_escape(what, &escaped);
3780 if (r < 0)
3781 return r;
3783 r = unit_name_build("blockdev", escaped, ".target", &target);
3784 if (r < 0)
3785 return r;
3787 return unit_add_dependency_by_name(u, UNIT_AFTER, target, true, mask);
3790 int unit_coldplug(Unit *u) {
3791 int r = 0, q;
3793 assert(u);
3795 /* Make sure we don't enter a loop, when coldplugging recursively. */
3796 if (u->coldplugged)
3797 return 0;
3799 u->coldplugged = true;
3801 STRV_FOREACH(i, u->deserialized_refs) {
3802 q = bus_unit_track_add_name(u, *i);
3803 if (q < 0 && r >= 0)
3804 r = q;
3806 u->deserialized_refs = strv_free(u->deserialized_refs);
3808 if (UNIT_VTABLE(u)->coldplug) {
3809 q = UNIT_VTABLE(u)->coldplug(u);
3810 if (q < 0 && r >= 0)
3811 r = q;
3814 if (u->job) {
3815 q = job_coldplug(u->job);
3816 if (q < 0 && r >= 0)
3817 r = q;
3819 if (u->nop_job) {
3820 q = job_coldplug(u->nop_job);
3821 if (q < 0 && r >= 0)
3822 r = q;
3825 return r;
3828 void unit_catchup(Unit *u) {
3829 assert(u);
3831 if (UNIT_VTABLE(u)->catchup)
3832 UNIT_VTABLE(u)->catchup(u);
3834 unit_cgroup_catchup(u);
3837 static bool fragment_mtime_newer(const char *path, usec_t mtime, bool path_masked) {
3838 struct stat st;
3840 if (!path)
3841 return false;
3843 /* If the source is some virtual kernel file system, then we assume we watch it anyway, and hence pretend we
3844 * are never out-of-date. */
3845 if (PATH_STARTSWITH_SET(path, "/proc", "/sys"))
3846 return false;
3848 if (stat(path, &st) < 0)
3849 /* What, cannot access this anymore? */
3850 return true;
3852 if (path_masked)
3853 /* For masked files check if they are still so */
3854 return !null_or_empty(&st);
3855 else
3856 /* For non-empty files check the mtime */
3857 return timespec_load(&st.st_mtim) > mtime;
3859 return false;
3862 bool unit_need_daemon_reload(Unit *u) {
3863 _cleanup_strv_free_ char **t = NULL;
3865 assert(u);
3867 /* For unit files, we allow masking… */
3868 if (fragment_mtime_newer(u->fragment_path, u->fragment_mtime,
3869 u->load_state == UNIT_MASKED))
3870 return true;
3872 /* Source paths should not be masked… */
3873 if (fragment_mtime_newer(u->source_path, u->source_mtime, false))
3874 return true;
3876 if (u->load_state == UNIT_LOADED)
3877 (void) unit_find_dropin_paths(u, &t);
3878 if (!strv_equal(u->dropin_paths, t))
3879 return true;
3881 /* … any drop-ins that are masked are simply omitted from the list. */
3882 STRV_FOREACH(path, u->dropin_paths)
3883 if (fragment_mtime_newer(*path, u->dropin_mtime, false))
3884 return true;
3886 return false;
3889 void unit_reset_failed(Unit *u) {
3890 assert(u);
3892 if (UNIT_VTABLE(u)->reset_failed)
3893 UNIT_VTABLE(u)->reset_failed(u);
3895 ratelimit_reset(&u->start_ratelimit);
3896 u->start_limit_hit = false;
3899 Unit *unit_following(Unit *u) {
3900 assert(u);
3902 if (UNIT_VTABLE(u)->following)
3903 return UNIT_VTABLE(u)->following(u);
3905 return NULL;
3908 bool unit_stop_pending(Unit *u) {
3909 assert(u);
3911 /* This call does check the current state of the unit. It's
3912 * hence useful to be called from state change calls of the
3913 * unit itself, where the state isn't updated yet. This is
3914 * different from unit_inactive_or_pending() which checks both
3915 * the current state and for a queued job. */
3917 return unit_has_job_type(u, JOB_STOP);
3920 bool unit_inactive_or_pending(Unit *u) {
3921 assert(u);
3923 /* Returns true if the unit is inactive or going down */
3925 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)))
3926 return true;
3928 if (unit_stop_pending(u))
3929 return true;
3931 return false;
3934 bool unit_active_or_pending(Unit *u) {
3935 assert(u);
3937 /* Returns true if the unit is active or going up */
3939 if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
3940 return true;
3942 if (u->job &&
3943 IN_SET(u->job->type, JOB_START, JOB_RELOAD_OR_START, JOB_RESTART))
3944 return true;
3946 return false;
3949 bool unit_will_restart_default(Unit *u) {
3950 assert(u);
3952 return unit_has_job_type(u, JOB_START);
3955 bool unit_will_restart(Unit *u) {
3956 assert(u);
3958 if (!UNIT_VTABLE(u)->will_restart)
3959 return false;
3961 return UNIT_VTABLE(u)->will_restart(u);
3964 int unit_kill(Unit *u, KillWho w, int signo, int code, int value, sd_bus_error *error) {
3965 assert(u);
3966 assert(w >= 0 && w < _KILL_WHO_MAX);
3967 assert(SIGNAL_VALID(signo));
3968 assert(IN_SET(code, SI_USER, SI_QUEUE));
3970 if (!UNIT_VTABLE(u)->kill)
3971 return -EOPNOTSUPP;
3973 return UNIT_VTABLE(u)->kill(u, w, signo, code, value, error);
3976 void unit_notify_cgroup_oom(Unit *u, bool managed_oom) {
3977 assert(u);
3979 if (UNIT_VTABLE(u)->notify_cgroup_oom)
3980 UNIT_VTABLE(u)->notify_cgroup_oom(u, managed_oom);
3983 static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) {
3984 _cleanup_set_free_ Set *pid_set = NULL;
3985 int r;
3987 pid_set = set_new(NULL);
3988 if (!pid_set)
3989 return NULL;
3991 /* Exclude the main/control pids from being killed via the cgroup */
3992 if (main_pid > 0) {
3993 r = set_put(pid_set, PID_TO_PTR(main_pid));
3994 if (r < 0)
3995 return NULL;
3998 if (control_pid > 0) {
3999 r = set_put(pid_set, PID_TO_PTR(control_pid));
4000 if (r < 0)
4001 return NULL;
4004 return TAKE_PTR(pid_set);
4007 static int kill_common_log(pid_t pid, int signo, void *userdata) {
4008 _cleanup_free_ char *comm = NULL;
4009 Unit *u = ASSERT_PTR(userdata);
4011 (void) get_process_comm(pid, &comm);
4012 log_unit_info(u, "Sending signal SIG%s to process " PID_FMT " (%s) on client request.",
4013 signal_to_string(signo), pid, strna(comm));
4015 return 1;
4018 static int kill_or_sigqueue(pid_t pid, int signo, int code, int value) {
4019 assert(pid > 0);
4020 assert(SIGNAL_VALID(signo));
4022 switch (code) {
4024 case SI_USER:
4025 log_debug("Killing " PID_FMT " with signal SIG%s.", pid, signal_to_string(signo));
4026 return RET_NERRNO(kill(pid, signo));
4028 case SI_QUEUE:
4029 log_debug("Enqueuing value %i to " PID_FMT " on signal SIG%s.", value, pid, signal_to_string(signo));
4030 return RET_NERRNO(sigqueue(pid, signo, (const union sigval) { .sival_int = value }));
4032 default:
4033 assert_not_reached();
4037 int unit_kill_common(
4038 Unit *u,
4039 KillWho who,
4040 int signo,
4041 int code,
4042 int value,
4043 pid_t main_pid,
4044 pid_t control_pid,
4045 sd_bus_error *error) {
4047 bool killed = false;
4048 int ret = 0, r;
4050 /* This is the common implementation for explicit user-requested killing of unit processes, shared by
4051 * various unit types. Do not confuse with unit_kill_context(), which is what we use when we want to
4052 * stop a service ourselves. */
4054 assert(u);
4055 assert(who >= 0);
4056 assert(who < _KILL_WHO_MAX);
4057 assert(SIGNAL_VALID(signo));
4058 assert(IN_SET(code, SI_USER, SI_QUEUE));
4060 if (IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL)) {
4061 if (main_pid < 0)
4062 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no main processes", unit_type_to_string(u->type));
4063 if (main_pid == 0)
4064 return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
4067 if (IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL)) {
4068 if (control_pid < 0)
4069 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PROCESS, "%s units have no control processes", unit_type_to_string(u->type));
4070 if (control_pid == 0)
4071 return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
4074 if (control_pid > 0 &&
4075 IN_SET(who, KILL_CONTROL, KILL_CONTROL_FAIL, KILL_ALL, KILL_ALL_FAIL)) {
4076 _cleanup_free_ char *comm = NULL;
4077 (void) get_process_comm(control_pid, &comm);
4079 r = kill_or_sigqueue(control_pid, signo, code, value);
4080 if (r < 0) {
4081 ret = r;
4083 /* Report this failure both to the logs and to the client */
4084 sd_bus_error_set_errnof(
4085 error, r,
4086 "Failed to send signal SIG%s to control process " PID_FMT " (%s): %m",
4087 signal_to_string(signo), control_pid, strna(comm));
4088 log_unit_warning_errno(
4089 u, r,
4090 "Failed to send signal SIG%s to control process " PID_FMT " (%s) on client request: %m",
4091 signal_to_string(signo), control_pid, strna(comm));
4092 } else {
4093 log_unit_info(u, "Sent signal SIG%s to control process " PID_FMT " (%s) on client request.",
4094 signal_to_string(signo), control_pid, strna(comm));
4095 killed = true;
4099 if (main_pid > 0 &&
4100 IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) {
4102 _cleanup_free_ char *comm = NULL;
4103 (void) get_process_comm(main_pid, &comm);
4105 r = kill_or_sigqueue(main_pid, signo, code, value);
4106 if (r < 0) {
4107 if (ret == 0) {
4108 ret = r;
4110 sd_bus_error_set_errnof(
4111 error, r,
4112 "Failed to send signal SIG%s to main process " PID_FMT " (%s): %m",
4113 signal_to_string(signo), main_pid, strna(comm));
4116 log_unit_warning_errno(
4117 u, r,
4118 "Failed to send signal SIG%s to main process " PID_FMT " (%s) on client request: %m",
4119 signal_to_string(signo), main_pid, strna(comm));
4121 } else {
4122 log_unit_info(u, "Sent signal SIG%s to main process " PID_FMT " (%s) on client request.",
4123 signal_to_string(signo), main_pid, strna(comm));
4124 killed = true;
4128 /* Note: if we shall enqueue rather than kill we won't do this via the cgroup mechanism, since it
4129 * doesn't really make much sense (and given that enqueued values are a relatively expensive
4130 * resource, and we shouldn't allow us to be subjects for such allocation sprees) */
4131 if (IN_SET(who, KILL_ALL, KILL_ALL_FAIL) && u->cgroup_path && code == SI_USER) {
4132 _cleanup_set_free_ Set *pid_set = NULL;
4134 /* Exclude the main/control pids from being killed via the cgroup */
4135 pid_set = unit_pid_set(main_pid, control_pid);
4136 if (!pid_set)
4137 return log_oom();
4139 r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, 0, pid_set, kill_common_log, u);
4140 if (r < 0) {
4141 if (!IN_SET(r, -ESRCH, -ENOENT)) {
4142 if (ret == 0) {
4143 ret = r;
4145 sd_bus_error_set_errnof(
4146 error, r,
4147 "Failed to send signal SIG%s to auxiliary processes: %m",
4148 signal_to_string(signo));
4151 log_unit_warning_errno(
4152 u, r,
4153 "Failed to send signal SIG%s to auxiliary processes on client request: %m",
4154 signal_to_string(signo));
4156 } else
4157 killed = true;
4160 /* If the "fail" versions of the operation are requested, then complain if the set of processes we killed is empty */
4161 if (ret == 0 && !killed && IN_SET(who, KILL_ALL_FAIL, KILL_CONTROL_FAIL, KILL_MAIN_FAIL))
4162 return sd_bus_error_set_const(error, BUS_ERROR_NO_SUCH_PROCESS, "No matching processes to kill");
4164 return ret;
4167 int unit_following_set(Unit *u, Set **s) {
4168 assert(u);
4169 assert(s);
4171 if (UNIT_VTABLE(u)->following_set)
4172 return UNIT_VTABLE(u)->following_set(u, s);
4174 *s = NULL;
4175 return 0;
4178 UnitFileState unit_get_unit_file_state(Unit *u) {
4179 int r;
4181 assert(u);
4183 if (u->unit_file_state < 0 && u->fragment_path) {
4184 r = unit_file_get_state(
4185 u->manager->runtime_scope,
4186 NULL,
4187 u->id,
4188 &u->unit_file_state);
4189 if (r < 0)
4190 u->unit_file_state = UNIT_FILE_BAD;
4193 return u->unit_file_state;
4196 PresetAction unit_get_unit_file_preset(Unit *u) {
4197 int r;
4199 assert(u);
4201 if (u->unit_file_preset < 0 && u->fragment_path) {
4202 _cleanup_free_ char *bn = NULL;
4204 r = path_extract_filename(u->fragment_path, &bn);
4205 if (r < 0)
4206 return (u->unit_file_preset = r);
4208 if (r == O_DIRECTORY)
4209 return (u->unit_file_preset = -EISDIR);
4211 u->unit_file_preset = unit_file_query_preset(
4212 u->manager->runtime_scope,
4213 NULL,
4215 NULL);
4218 return u->unit_file_preset;
4221 Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target) {
4222 assert(ref);
4223 assert(source);
4224 assert(target);
4226 if (ref->target)
4227 unit_ref_unset(ref);
4229 ref->source = source;
4230 ref->target = target;
4231 LIST_PREPEND(refs_by_target, target->refs_by_target, ref);
4232 return target;
4235 void unit_ref_unset(UnitRef *ref) {
4236 assert(ref);
4238 if (!ref->target)
4239 return;
4241 /* We are about to drop a reference to the unit, make sure the garbage collection has a look at it as it might
4242 * be unreferenced now. */
4243 unit_add_to_gc_queue(ref->target);
4245 LIST_REMOVE(refs_by_target, ref->target->refs_by_target, ref);
4246 ref->source = ref->target = NULL;
4249 static int user_from_unit_name(Unit *u, char **ret) {
4251 static const uint8_t hash_key[] = {
4252 0x58, 0x1a, 0xaf, 0xe6, 0x28, 0x58, 0x4e, 0x96,
4253 0xb4, 0x4e, 0xf5, 0x3b, 0x8c, 0x92, 0x07, 0xec
4256 _cleanup_free_ char *n = NULL;
4257 int r;
4259 r = unit_name_to_prefix(u->id, &n);
4260 if (r < 0)
4261 return r;
4263 if (valid_user_group_name(n, 0)) {
4264 *ret = TAKE_PTR(n);
4265 return 0;
4268 /* If we can't use the unit name as a user name, then let's hash it and use that */
4269 if (asprintf(ret, "_du%016" PRIx64, siphash24(n, strlen(n), hash_key)) < 0)
4270 return -ENOMEM;
4272 return 0;
4275 int unit_patch_contexts(Unit *u) {
4276 CGroupContext *cc;
4277 ExecContext *ec;
4278 int r;
4280 assert(u);
4282 /* Patch in the manager defaults into the exec and cgroup
4283 * contexts, _after_ the rest of the settings have been
4284 * initialized */
4286 ec = unit_get_exec_context(u);
4287 if (ec) {
4288 /* This only copies in the ones that need memory */
4289 for (unsigned i = 0; i < _RLIMIT_MAX; i++)
4290 if (u->manager->rlimit[i] && !ec->rlimit[i]) {
4291 ec->rlimit[i] = newdup(struct rlimit, u->manager->rlimit[i], 1);
4292 if (!ec->rlimit[i])
4293 return -ENOMEM;
4296 if (MANAGER_IS_USER(u->manager) &&
4297 !ec->working_directory) {
4299 r = get_home_dir(&ec->working_directory);
4300 if (r < 0)
4301 return r;
4303 /* Allow user services to run, even if the
4304 * home directory is missing */
4305 ec->working_directory_missing_ok = true;
4308 if (ec->private_devices)
4309 ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_MKNOD) | (UINT64_C(1) << CAP_SYS_RAWIO));
4311 if (ec->protect_kernel_modules)
4312 ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYS_MODULE);
4314 if (ec->protect_kernel_logs)
4315 ec->capability_bounding_set &= ~(UINT64_C(1) << CAP_SYSLOG);
4317 if (ec->protect_clock)
4318 ec->capability_bounding_set &= ~((UINT64_C(1) << CAP_SYS_TIME) | (UINT64_C(1) << CAP_WAKE_ALARM));
4320 if (ec->dynamic_user) {
4321 if (!ec->user) {
4322 r = user_from_unit_name(u, &ec->user);
4323 if (r < 0)
4324 return r;
4327 if (!ec->group) {
4328 ec->group = strdup(ec->user);
4329 if (!ec->group)
4330 return -ENOMEM;
4333 /* If the dynamic user option is on, let's make sure that the unit can't leave its
4334 * UID/GID around in the file system or on IPC objects. Hence enforce a strict
4335 * sandbox. */
4337 ec->private_tmp = true;
4338 ec->remove_ipc = true;
4339 ec->protect_system = PROTECT_SYSTEM_STRICT;
4340 if (ec->protect_home == PROTECT_HOME_NO)
4341 ec->protect_home = PROTECT_HOME_READ_ONLY;
4343 /* Make sure this service can neither benefit from SUID/SGID binaries nor create
4344 * them. */
4345 ec->no_new_privileges = true;
4346 ec->restrict_suid_sgid = true;
4349 for (ExecDirectoryType dt = 0; dt < _EXEC_DIRECTORY_TYPE_MAX; dt++)
4350 exec_directory_sort(ec->directories + dt);
4353 cc = unit_get_cgroup_context(u);
4354 if (cc && ec) {
4356 if (ec->private_devices &&
4357 cc->device_policy == CGROUP_DEVICE_POLICY_AUTO)
4358 cc->device_policy = CGROUP_DEVICE_POLICY_CLOSED;
4360 /* Only add these if needed, as they imply that everything else is blocked. */
4361 if (cc->device_policy != CGROUP_DEVICE_POLICY_AUTO || cc->device_allow) {
4362 if (ec->root_image || ec->mount_images) {
4364 /* When RootImage= or MountImages= is specified, the following devices are touched. */
4365 FOREACH_STRING(p, "/dev/loop-control", "/dev/mapper/control") {
4366 r = cgroup_add_device_allow(cc, p, "rw");
4367 if (r < 0)
4368 return r;
4370 FOREACH_STRING(p, "block-loop", "block-blkext", "block-device-mapper") {
4371 r = cgroup_add_device_allow(cc, p, "rwm");
4372 if (r < 0)
4373 return r;
4376 /* Make sure "block-loop" can be resolved, i.e. make sure "loop" shows up in /proc/devices.
4377 * Same for mapper and verity. */
4378 FOREACH_STRING(p, "modprobe@loop.service", "modprobe@dm_mod.service", "modprobe@dm_verity.service") {
4379 r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, p, true, UNIT_DEPENDENCY_FILE);
4380 if (r < 0)
4381 return r;
4385 if (ec->protect_clock) {
4386 r = cgroup_add_device_allow(cc, "char-rtc", "r");
4387 if (r < 0)
4388 return r;
4391 /* If there are encrypted credentials we might need to access the TPM. */
4392 if (exec_context_has_encrypted_credentials(ec)) {
4393 r = cgroup_add_device_allow(cc, "char-tpm", "rw");
4394 if (r < 0)
4395 return r;
4400 return 0;
4403 ExecContext *unit_get_exec_context(const Unit *u) {
4404 size_t offset;
4405 assert(u);
4407 if (u->type < 0)
4408 return NULL;
4410 offset = UNIT_VTABLE(u)->exec_context_offset;
4411 if (offset <= 0)
4412 return NULL;
4414 return (ExecContext*) ((uint8_t*) u + offset);
4417 KillContext *unit_get_kill_context(Unit *u) {
4418 size_t offset;
4419 assert(u);
4421 if (u->type < 0)
4422 return NULL;
4424 offset = UNIT_VTABLE(u)->kill_context_offset;
4425 if (offset <= 0)
4426 return NULL;
4428 return (KillContext*) ((uint8_t*) u + offset);
4431 CGroupContext *unit_get_cgroup_context(Unit *u) {
4432 size_t offset;
4434 if (u->type < 0)
4435 return NULL;
4437 offset = UNIT_VTABLE(u)->cgroup_context_offset;
4438 if (offset <= 0)
4439 return NULL;
4441 return (CGroupContext*) ((uint8_t*) u + offset);
4444 ExecRuntime *unit_get_exec_runtime(Unit *u) {
4445 size_t offset;
4447 if (u->type < 0)
4448 return NULL;
4450 offset = UNIT_VTABLE(u)->exec_runtime_offset;
4451 if (offset <= 0)
4452 return NULL;
4454 return *(ExecRuntime**) ((uint8_t*) u + offset);
4457 static const char* unit_drop_in_dir(Unit *u, UnitWriteFlags flags) {
4458 assert(u);
4460 if (UNIT_WRITE_FLAGS_NOOP(flags))
4461 return NULL;
4463 if (u->transient) /* Redirect drop-ins for transient units always into the transient directory. */
4464 return u->manager->lookup_paths.transient;
4466 if (flags & UNIT_PERSISTENT)
4467 return u->manager->lookup_paths.persistent_control;
4469 if (flags & UNIT_RUNTIME)
4470 return u->manager->lookup_paths.runtime_control;
4472 return NULL;
4475 const char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf) {
4476 assert(s);
4477 assert(popcount(flags & (UNIT_ESCAPE_EXEC_SYNTAX_ENV | UNIT_ESCAPE_EXEC_SYNTAX | UNIT_ESCAPE_C)) <= 1);
4478 assert(buf);
4480 _cleanup_free_ char *t = NULL;
4482 /* Returns a string with any escaping done. If no escaping was necessary, *buf is set to NULL, and
4483 * the input pointer is returned as-is. If an allocation was needed, the return buffer pointer is
4484 * written to *buf. This means the return value always contains a properly escaped version, but *buf
4485 * only contains a pointer if an allocation was made. Callers can use this to optimize memory
4486 * allocations. */
4488 if (flags & UNIT_ESCAPE_SPECIFIERS) {
4489 t = specifier_escape(s);
4490 if (!t)
4491 return NULL;
4493 s = t;
4496 /* We either do C-escaping or shell-escaping, to additionally escape characters that we parse for
4497 * ExecStart= and friends, i.e. '$' and quotes. */
4499 if (flags & (UNIT_ESCAPE_EXEC_SYNTAX_ENV | UNIT_ESCAPE_EXEC_SYNTAX)) {
4500 char *t2;
4502 if (flags & UNIT_ESCAPE_EXEC_SYNTAX_ENV) {
4503 t2 = strreplace(s, "$", "$$");
4504 if (!t2)
4505 return NULL;
4506 free_and_replace(t, t2);
4509 t2 = shell_escape(t ?: s, "\"");
4510 if (!t2)
4511 return NULL;
4512 free_and_replace(t, t2);
4514 s = t;
4516 } else if (flags & UNIT_ESCAPE_C) {
4517 char *t2;
4519 t2 = cescape(s);
4520 if (!t2)
4521 return NULL;
4522 free_and_replace(t, t2);
4524 s = t;
4527 *buf = TAKE_PTR(t);
4528 return s;
4531 char* unit_concat_strv(char **l, UnitWriteFlags flags) {
4532 _cleanup_free_ char *result = NULL;
4533 size_t n = 0;
4535 /* Takes a list of strings, escapes them, and concatenates them. This may be used to format command
4536 * lines in a way suitable for ExecStart= stanzas. */
4538 STRV_FOREACH(i, l) {
4539 _cleanup_free_ char *buf = NULL;
4540 const char *p;
4541 size_t a;
4542 char *q;
4544 p = unit_escape_setting(*i, flags, &buf);
4545 if (!p)
4546 return NULL;
4548 a = (n > 0) + 1 + strlen(p) + 1; /* separating space + " + entry + " */
4549 if (!GREEDY_REALLOC(result, n + a + 1))
4550 return NULL;
4552 q = result + n;
4553 if (n > 0)
4554 *(q++) = ' ';
4556 *(q++) = '"';
4557 q = stpcpy(q, p);
4558 *(q++) = '"';
4560 n += a;
4563 if (!GREEDY_REALLOC(result, n + 1))
4564 return NULL;
4566 result[n] = 0;
4568 return TAKE_PTR(result);
4571 int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data) {
4572 _cleanup_free_ char *p = NULL, *q = NULL, *escaped = NULL;
4573 const char *dir, *wrapped;
4574 int r;
4576 assert(u);
4577 assert(name);
4578 assert(data);
4580 if (UNIT_WRITE_FLAGS_NOOP(flags))
4581 return 0;
4583 data = unit_escape_setting(data, flags, &escaped);
4584 if (!data)
4585 return -ENOMEM;
4587 /* Prefix the section header. If we are writing this out as transient file, then let's suppress this if the
4588 * previous section header is the same */
4590 if (flags & UNIT_PRIVATE) {
4591 if (!UNIT_VTABLE(u)->private_section)
4592 return -EINVAL;
4594 if (!u->transient_file || u->last_section_private < 0)
4595 data = strjoina("[", UNIT_VTABLE(u)->private_section, "]\n", data);
4596 else if (u->last_section_private == 0)
4597 data = strjoina("\n[", UNIT_VTABLE(u)->private_section, "]\n", data);
4598 } else {
4599 if (!u->transient_file || u->last_section_private < 0)
4600 data = strjoina("[Unit]\n", data);
4601 else if (u->last_section_private > 0)
4602 data = strjoina("\n[Unit]\n", data);
4605 if (u->transient_file) {
4606 /* When this is a transient unit file in creation, then let's not create a new drop-in but instead
4607 * write to the transient unit file. */
4608 fputs(data, u->transient_file);
4610 if (!endswith(data, "\n"))
4611 fputc('\n', u->transient_file);
4613 /* Remember which section we wrote this entry to */
4614 u->last_section_private = !!(flags & UNIT_PRIVATE);
4615 return 0;
4618 dir = unit_drop_in_dir(u, flags);
4619 if (!dir)
4620 return -EINVAL;
4622 wrapped = strjoina("# This is a drop-in unit file extension, created via \"systemctl set-property\"\n"
4623 "# or an equivalent operation. Do not edit.\n",
4624 data,
4625 "\n");
4627 r = drop_in_file(dir, u->id, 50, name, &p, &q);
4628 if (r < 0)
4629 return r;
4631 (void) mkdir_p_label(p, 0755);
4633 /* Make sure the drop-in dir is registered in our path cache. This way we don't need to stupidly
4634 * recreate the cache after every drop-in we write. */
4635 if (u->manager->unit_path_cache) {
4636 r = set_put_strdup(&u->manager->unit_path_cache, p);
4637 if (r < 0)
4638 return r;
4641 r = write_string_file_atomic_label(q, wrapped);
4642 if (r < 0)
4643 return r;
4645 r = strv_push(&u->dropin_paths, q);
4646 if (r < 0)
4647 return r;
4648 q = NULL;
4650 strv_uniq(u->dropin_paths);
4652 u->dropin_mtime = now(CLOCK_REALTIME);
4654 return 0;
4657 int unit_write_settingf(Unit *u, UnitWriteFlags flags, const char *name, const char *format, ...) {
4658 _cleanup_free_ char *p = NULL;
4659 va_list ap;
4660 int r;
4662 assert(u);
4663 assert(name);
4664 assert(format);
4666 if (UNIT_WRITE_FLAGS_NOOP(flags))
4667 return 0;
4669 va_start(ap, format);
4670 r = vasprintf(&p, format, ap);
4671 va_end(ap);
4673 if (r < 0)
4674 return -ENOMEM;
4676 return unit_write_setting(u, flags, name, p);
4679 int unit_make_transient(Unit *u) {
4680 _cleanup_free_ char *path = NULL;
4681 FILE *f;
4683 assert(u);
4685 if (!UNIT_VTABLE(u)->can_transient)
4686 return -EOPNOTSUPP;
4688 (void) mkdir_p_label(u->manager->lookup_paths.transient, 0755);
4690 path = path_join(u->manager->lookup_paths.transient, u->id);
4691 if (!path)
4692 return -ENOMEM;
4694 /* Let's open the file we'll write the transient settings into. This file is kept open as long as we are
4695 * creating the transient, and is closed in unit_load(), as soon as we start loading the file. */
4697 WITH_UMASK(0022) {
4698 f = fopen(path, "we");
4699 if (!f)
4700 return -errno;
4703 safe_fclose(u->transient_file);
4704 u->transient_file = f;
4706 free_and_replace(u->fragment_path, path);
4708 u->source_path = mfree(u->source_path);
4709 u->dropin_paths = strv_free(u->dropin_paths);
4710 u->fragment_mtime = u->source_mtime = u->dropin_mtime = 0;
4712 u->load_state = UNIT_STUB;
4713 u->load_error = 0;
4714 u->transient = true;
4716 unit_add_to_dbus_queue(u);
4717 unit_add_to_gc_queue(u);
4719 fputs("# This is a transient unit file, created programmatically via the systemd API. Do not edit.\n",
4720 u->transient_file);
4722 return 0;
4725 static int log_kill(pid_t pid, int sig, void *userdata) {
4726 _cleanup_free_ char *comm = NULL;
4728 (void) get_process_comm(pid, &comm);
4730 /* Don't log about processes marked with brackets, under the assumption that these are temporary processes
4731 only, like for example systemd's own PAM stub process. */
4732 if (comm && comm[0] == '(')
4733 /* Although we didn't log anything, as this callback is used in unit_kill_context we must return 1
4734 * here to let the manager know that a process was killed. */
4735 return 1;
4737 log_unit_notice(userdata,
4738 "Killing process " PID_FMT " (%s) with signal SIG%s.",
4739 pid,
4740 strna(comm),
4741 signal_to_string(sig));
4743 return 1;
4746 static int operation_to_signal(
4747 const KillContext *c,
4748 KillOperation k,
4749 bool *ret_noteworthy) {
4751 assert(c);
4753 switch (k) {
4755 case KILL_TERMINATE:
4756 case KILL_TERMINATE_AND_LOG:
4757 *ret_noteworthy = false;
4758 return c->kill_signal;
4760 case KILL_RESTART:
4761 *ret_noteworthy = false;
4762 return restart_kill_signal(c);
4764 case KILL_KILL:
4765 *ret_noteworthy = true;
4766 return c->final_kill_signal;
4768 case KILL_WATCHDOG:
4769 *ret_noteworthy = true;
4770 return c->watchdog_signal;
4772 default:
4773 assert_not_reached();
4777 int unit_kill_context(
4778 Unit *u,
4779 KillContext *c,
4780 KillOperation k,
4781 pid_t main_pid,
4782 pid_t control_pid,
4783 bool main_pid_alien) {
4785 bool wait_for_exit = false, send_sighup;
4786 cg_kill_log_func_t log_func = NULL;
4787 int sig, r;
4789 assert(u);
4790 assert(c);
4792 /* Kill the processes belonging to this unit, in preparation for shutting the unit down. Returns > 0
4793 * if we killed something worth waiting for, 0 otherwise. Do not confuse with unit_kill_common()
4794 * which is used for user-requested killing of unit processes. */
4796 if (c->kill_mode == KILL_NONE)
4797 return 0;
4799 bool noteworthy;
4800 sig = operation_to_signal(c, k, &noteworthy);
4801 if (noteworthy)
4802 log_func = log_kill;
4804 send_sighup =
4805 c->send_sighup &&
4806 IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) &&
4807 sig != SIGHUP;
4809 if (main_pid > 0) {
4810 if (log_func)
4811 log_func(main_pid, sig, u);
4813 r = kill_and_sigcont(main_pid, sig);
4814 if (r < 0 && r != -ESRCH) {
4815 _cleanup_free_ char *comm = NULL;
4816 (void) get_process_comm(main_pid, &comm);
4818 log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm));
4819 } else {
4820 if (!main_pid_alien)
4821 wait_for_exit = true;
4823 if (r != -ESRCH && send_sighup)
4824 (void) kill(main_pid, SIGHUP);
4828 if (control_pid > 0) {
4829 if (log_func)
4830 log_func(control_pid, sig, u);
4832 r = kill_and_sigcont(control_pid, sig);
4833 if (r < 0 && r != -ESRCH) {
4834 _cleanup_free_ char *comm = NULL;
4835 (void) get_process_comm(control_pid, &comm);
4837 log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm));
4838 } else {
4839 wait_for_exit = true;
4841 if (r != -ESRCH && send_sighup)
4842 (void) kill(control_pid, SIGHUP);
4846 if (u->cgroup_path &&
4847 (c->kill_mode == KILL_CONTROL_GROUP || (c->kill_mode == KILL_MIXED && k == KILL_KILL))) {
4848 _cleanup_set_free_ Set *pid_set = NULL;
4850 /* Exclude the main/control pids from being killed via the cgroup */
4851 pid_set = unit_pid_set(main_pid, control_pid);
4852 if (!pid_set)
4853 return -ENOMEM;
4855 r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path,
4856 sig,
4857 CGROUP_SIGCONT|CGROUP_IGNORE_SELF,
4858 pid_set,
4859 log_func, u);
4860 if (r < 0) {
4861 if (!IN_SET(r, -EAGAIN, -ESRCH, -ENOENT))
4862 log_unit_warning_errno(u, r, "Failed to kill control group %s, ignoring: %m", empty_to_root(u->cgroup_path));
4864 } else if (r > 0) {
4866 /* FIXME: For now, on the legacy hierarchy, we will not wait for the cgroup members to die if
4867 * we are running in a container or if this is a delegation unit, simply because cgroup
4868 * notification is unreliable in these cases. It doesn't work at all in containers, and outside
4869 * of containers it can be confused easily by left-over directories in the cgroup — which
4870 * however should not exist in non-delegated units. On the unified hierarchy that's different,
4871 * there we get proper events. Hence rely on them. */
4873 if (cg_unified_controller(SYSTEMD_CGROUP_CONTROLLER) > 0 ||
4874 (detect_container() == 0 && !unit_cgroup_delegate(u)))
4875 wait_for_exit = true;
4877 if (send_sighup) {
4878 set_free(pid_set);
4880 pid_set = unit_pid_set(main_pid, control_pid);
4881 if (!pid_set)
4882 return -ENOMEM;
4884 (void) cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path,
4885 SIGHUP,
4886 CGROUP_IGNORE_SELF,
4887 pid_set,
4888 NULL, NULL);
4893 return wait_for_exit;
4896 int unit_require_mounts_for(Unit *u, const char *path, UnitDependencyMask mask) {
4897 int r;
4899 assert(u);
4900 assert(path);
4902 /* Registers a unit for requiring a certain path and all its prefixes. We keep a hashtable of these
4903 * paths in the unit (from the path to the UnitDependencyInfo structure indicating how to the
4904 * dependency came to be). However, we build a prefix table for all possible prefixes so that new
4905 * appearing mount units can easily determine which units to make themselves a dependency of. */
4907 if (!path_is_absolute(path))
4908 return -EINVAL;
4910 if (hashmap_contains(u->requires_mounts_for, path)) /* Exit quickly if the path is already covered. */
4911 return 0;
4913 _cleanup_free_ char *p = strdup(path);
4914 if (!p)
4915 return -ENOMEM;
4917 /* Use the canonical form of the path as the stored key. We call path_is_normalized()
4918 * only after simplification, since path_is_normalized() rejects paths with '.'.
4919 * path_is_normalized() also verifies that the path fits in PATH_MAX. */
4920 path = path_simplify(p);
4922 if (!path_is_normalized(path))
4923 return -EPERM;
4925 UnitDependencyInfo di = {
4926 .origin_mask = mask
4929 r = hashmap_ensure_put(&u->requires_mounts_for, &path_hash_ops, p, di.data);
4930 if (r < 0)
4931 return r;
4932 assert(r > 0);
4933 TAKE_PTR(p); /* path remains a valid pointer to the string stored in the hashmap */
4935 char prefix[strlen(path) + 1];
4936 PATH_FOREACH_PREFIX_MORE(prefix, path) {
4937 Set *x;
4939 x = hashmap_get(u->manager->units_requiring_mounts_for, prefix);
4940 if (!x) {
4941 _cleanup_free_ char *q = NULL;
4943 r = hashmap_ensure_allocated(&u->manager->units_requiring_mounts_for, &path_hash_ops);
4944 if (r < 0)
4945 return r;
4947 q = strdup(prefix);
4948 if (!q)
4949 return -ENOMEM;
4951 x = set_new(NULL);
4952 if (!x)
4953 return -ENOMEM;
4955 r = hashmap_put(u->manager->units_requiring_mounts_for, q, x);
4956 if (r < 0) {
4957 set_free(x);
4958 return r;
4960 q = NULL;
4963 r = set_put(x, u);
4964 if (r < 0)
4965 return r;
4968 return 0;
4971 int unit_setup_exec_runtime(Unit *u) {
4972 _cleanup_(exec_shared_runtime_unrefp) ExecSharedRuntime *esr = NULL;
4973 _cleanup_(dynamic_creds_unrefp) DynamicCreds *dcreds = NULL;
4974 _cleanup_set_free_ Set *units = NULL;
4975 ExecRuntime **rt;
4976 ExecContext *ec;
4977 size_t offset;
4978 Unit *other;
4979 int r;
4981 offset = UNIT_VTABLE(u)->exec_runtime_offset;
4982 assert(offset > 0);
4984 /* Check if there already is an ExecRuntime for this unit? */
4985 rt = (ExecRuntime**) ((uint8_t*) u + offset);
4986 if (*rt)
4987 return 0;
4989 ec = unit_get_exec_context(u);
4990 assert(ec);
4992 r = unit_get_transitive_dependency_set(u, UNIT_ATOM_JOINS_NAMESPACE_OF, &units);
4993 if (r < 0)
4994 return r;
4996 /* Try to get it from somebody else */
4997 SET_FOREACH(other, units) {
4998 r = exec_shared_runtime_acquire(u->manager, NULL, other->id, false, &esr);
4999 if (r < 0)
5000 return r;
5001 if (r > 0)
5002 break;
5005 if (!esr) {
5006 r = exec_shared_runtime_acquire(u->manager, ec, u->id, true, &esr);
5007 if (r < 0)
5008 return r;
5011 if (ec->dynamic_user) {
5012 r = dynamic_creds_make(u->manager, ec->user, ec->group, &dcreds);
5013 if (r < 0)
5014 return r;
5017 r = exec_runtime_make(u, ec, esr, dcreds, rt);
5018 if (r < 0)
5019 return r;
5021 TAKE_PTR(esr);
5022 TAKE_PTR(dcreds);
5024 return r;
5027 bool unit_type_supported(UnitType t) {
5028 static int8_t cache[_UNIT_TYPE_MAX] = {}; /* -1: disabled, 1: enabled: 0: don't know */
5029 int r;
5031 if (_unlikely_(t < 0))
5032 return false;
5033 if (_unlikely_(t >= _UNIT_TYPE_MAX))
5034 return false;
5036 if (cache[t] == 0) {
5037 char *e;
5039 e = strjoina("SYSTEMD_SUPPORT_", unit_type_to_string(t));
5041 r = getenv_bool(ascii_strupper(e));
5042 if (r < 0 && r != -ENXIO)
5043 log_debug_errno(r, "Failed to parse $%s, ignoring: %m", e);
5045 cache[t] = r == 0 ? -1 : 1;
5047 if (cache[t] < 0)
5048 return false;
5050 if (!unit_vtable[t]->supported)
5051 return true;
5053 return unit_vtable[t]->supported();
5056 void unit_warn_if_dir_nonempty(Unit *u, const char* where) {
5057 int r;
5059 assert(u);
5060 assert(where);
5062 if (!unit_log_level_test(u, LOG_NOTICE))
5063 return;
5065 r = dir_is_empty(where, /* ignore_hidden_or_backup= */ false);
5066 if (r > 0 || r == -ENOTDIR)
5067 return;
5068 if (r < 0) {
5069 log_unit_warning_errno(u, r, "Failed to check directory %s: %m", where);
5070 return;
5073 log_unit_struct(u, LOG_NOTICE,
5074 "MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR,
5075 LOG_UNIT_INVOCATION_ID(u),
5076 LOG_UNIT_MESSAGE(u, "Directory %s to mount over is not empty, mounting anyway.", where),
5077 "WHERE=%s", where);
5080 int unit_fail_if_noncanonical(Unit *u, const char* where) {
5081 _cleanup_free_ char *canonical_where = NULL;
5082 int r;
5084 assert(u);
5085 assert(where);
5087 r = chase(where, NULL, CHASE_NONEXISTENT, &canonical_where, NULL);
5088 if (r < 0) {
5089 log_unit_debug_errno(u, r, "Failed to check %s for symlinks, ignoring: %m", where);
5090 return 0;
5093 /* We will happily ignore a trailing slash (or any redundant slashes) */
5094 if (path_equal(where, canonical_where))
5095 return 0;
5097 /* No need to mention "." or "..", they would already have been rejected by unit_name_from_path() */
5098 log_unit_struct(u, LOG_ERR,
5099 "MESSAGE_ID=" SD_MESSAGE_OVERMOUNTING_STR,
5100 LOG_UNIT_INVOCATION_ID(u),
5101 LOG_UNIT_MESSAGE(u, "Mount path %s is not canonical (contains a symlink).", where),
5102 "WHERE=%s", where);
5104 return -ELOOP;
5107 bool unit_is_pristine(Unit *u) {
5108 assert(u);
5110 /* Check if the unit already exists or is already around, in a number of different ways. Note that to
5111 * cater for unit types such as slice, we are generally fine with units that are marked UNIT_LOADED
5112 * even though nothing was actually loaded, as those unit types don't require a file on disk.
5114 * Note that we don't check for drop-ins here, because we allow drop-ins for transient units
5115 * identically to non-transient units, both unit-specific and hierarchical. E.g. for a-b-c.service:
5116 * service.d/….conf, a-.service.d/….conf, a-b-.service.d/….conf, a-b-c.service.d/….conf.
5119 return IN_SET(u->load_state, UNIT_NOT_FOUND, UNIT_LOADED) &&
5120 !u->fragment_path &&
5121 !u->source_path &&
5122 !u->job &&
5123 !u->merged_into;
5126 pid_t unit_control_pid(Unit *u) {
5127 assert(u);
5129 if (UNIT_VTABLE(u)->control_pid)
5130 return UNIT_VTABLE(u)->control_pid(u);
5132 return 0;
5135 pid_t unit_main_pid(Unit *u) {
5136 assert(u);
5138 if (UNIT_VTABLE(u)->main_pid)
5139 return UNIT_VTABLE(u)->main_pid(u);
5141 return 0;
5144 static void unit_unref_uid_internal(
5145 Unit *u,
5146 uid_t *ref_uid,
5147 bool destroy_now,
5148 void (*_manager_unref_uid)(Manager *m, uid_t uid, bool destroy_now)) {
5150 assert(u);
5151 assert(ref_uid);
5152 assert(_manager_unref_uid);
5154 /* Generic implementation of both unit_unref_uid() and unit_unref_gid(), under the assumption that uid_t and
5155 * gid_t are actually the same time, with the same validity rules.
5157 * Drops a reference to UID/GID from a unit. */
5159 assert_cc(sizeof(uid_t) == sizeof(gid_t));
5160 assert_cc(UID_INVALID == (uid_t) GID_INVALID);
5162 if (!uid_is_valid(*ref_uid))
5163 return;
5165 _manager_unref_uid(u->manager, *ref_uid, destroy_now);
5166 *ref_uid = UID_INVALID;
5169 static void unit_unref_uid(Unit *u, bool destroy_now) {
5170 unit_unref_uid_internal(u, &u->ref_uid, destroy_now, manager_unref_uid);
5173 static void unit_unref_gid(Unit *u, bool destroy_now) {
5174 unit_unref_uid_internal(u, (uid_t*) &u->ref_gid, destroy_now, manager_unref_gid);
5177 void unit_unref_uid_gid(Unit *u, bool destroy_now) {
5178 assert(u);
5180 unit_unref_uid(u, destroy_now);
5181 unit_unref_gid(u, destroy_now);
5184 static int unit_ref_uid_internal(
5185 Unit *u,
5186 uid_t *ref_uid,
5187 uid_t uid,
5188 bool clean_ipc,
5189 int (*_manager_ref_uid)(Manager *m, uid_t uid, bool clean_ipc)) {
5191 int r;
5193 assert(u);
5194 assert(ref_uid);
5195 assert(uid_is_valid(uid));
5196 assert(_manager_ref_uid);
5198 /* Generic implementation of both unit_ref_uid() and unit_ref_guid(), under the assumption that uid_t and gid_t
5199 * are actually the same type, and have the same validity rules.
5201 * Adds a reference on a specific UID/GID to this unit. Each unit referencing the same UID/GID maintains a
5202 * reference so that we can destroy the UID/GID's IPC resources as soon as this is requested and the counter
5203 * drops to zero. */
5205 assert_cc(sizeof(uid_t) == sizeof(gid_t));
5206 assert_cc(UID_INVALID == (uid_t) GID_INVALID);
5208 if (*ref_uid == uid)
5209 return 0;
5211 if (uid_is_valid(*ref_uid)) /* Already set? */
5212 return -EBUSY;
5214 r = _manager_ref_uid(u->manager, uid, clean_ipc);
5215 if (r < 0)
5216 return r;
5218 *ref_uid = uid;
5219 return 1;
5222 static int unit_ref_uid(Unit *u, uid_t uid, bool clean_ipc) {
5223 return unit_ref_uid_internal(u, &u->ref_uid, uid, clean_ipc, manager_ref_uid);
5226 static int unit_ref_gid(Unit *u, gid_t gid, bool clean_ipc) {
5227 return unit_ref_uid_internal(u, (uid_t*) &u->ref_gid, (uid_t) gid, clean_ipc, manager_ref_gid);
5230 static int unit_ref_uid_gid_internal(Unit *u, uid_t uid, gid_t gid, bool clean_ipc) {
5231 int r = 0, q = 0;
5233 assert(u);
5235 /* Reference both a UID and a GID in one go. Either references both, or neither. */
5237 if (uid_is_valid(uid)) {
5238 r = unit_ref_uid(u, uid, clean_ipc);
5239 if (r < 0)
5240 return r;
5243 if (gid_is_valid(gid)) {
5244 q = unit_ref_gid(u, gid, clean_ipc);
5245 if (q < 0) {
5246 if (r > 0)
5247 unit_unref_uid(u, false);
5249 return q;
5253 return r > 0 || q > 0;
5256 int unit_ref_uid_gid(Unit *u, uid_t uid, gid_t gid) {
5257 ExecContext *c;
5258 int r;
5260 assert(u);
5262 c = unit_get_exec_context(u);
5264 r = unit_ref_uid_gid_internal(u, uid, gid, c ? c->remove_ipc : false);
5265 if (r < 0)
5266 return log_unit_warning_errno(u, r, "Couldn't add UID/GID reference to unit, proceeding without: %m");
5268 return r;
5271 void unit_notify_user_lookup(Unit *u, uid_t uid, gid_t gid) {
5272 int r;
5274 assert(u);
5276 /* This is invoked whenever one of the forked off processes let's us know the UID/GID its user name/group names
5277 * resolved to. We keep track of which UID/GID is currently assigned in order to be able to destroy its IPC
5278 * objects when no service references the UID/GID anymore. */
5280 r = unit_ref_uid_gid(u, uid, gid);
5281 if (r > 0)
5282 unit_add_to_dbus_queue(u);
5285 int unit_acquire_invocation_id(Unit *u) {
5286 sd_id128_t id;
5287 int r;
5289 assert(u);
5291 r = sd_id128_randomize(&id);
5292 if (r < 0)
5293 return log_unit_error_errno(u, r, "Failed to generate invocation ID for unit: %m");
5295 r = unit_set_invocation_id(u, id);
5296 if (r < 0)
5297 return log_unit_error_errno(u, r, "Failed to set invocation ID for unit: %m");
5299 unit_add_to_dbus_queue(u);
5300 return 0;
5303 int unit_set_exec_params(Unit *u, ExecParameters *p) {
5304 int r;
5306 assert(u);
5307 assert(p);
5309 /* Copy parameters from manager */
5310 r = manager_get_effective_environment(u->manager, &p->environment);
5311 if (r < 0)
5312 return r;
5314 p->runtime_scope = u->manager->runtime_scope;
5316 p->confirm_spawn = manager_get_confirm_spawn(u->manager);
5317 p->cgroup_supported = u->manager->cgroup_supported;
5318 p->prefix = u->manager->prefix;
5319 SET_FLAG(p->flags, EXEC_PASS_LOG_UNIT|EXEC_CHOWN_DIRECTORIES, MANAGER_IS_SYSTEM(u->manager));
5321 /* Copy parameters from unit */
5322 p->cgroup_path = u->cgroup_path;
5323 SET_FLAG(p->flags, EXEC_CGROUP_DELEGATE, unit_cgroup_delegate(u));
5325 p->received_credentials_directory = u->manager->received_credentials_directory;
5326 p->received_encrypted_credentials_directory = u->manager->received_encrypted_credentials_directory;
5328 return 0;
5331 int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) {
5332 int r;
5334 assert(u);
5335 assert(ret);
5337 /* Forks off a helper process and makes sure it is a member of the unit's cgroup. Returns == 0 in the child,
5338 * and > 0 in the parent. The pid parameter is always filled in with the child's PID. */
5340 (void) unit_realize_cgroup(u);
5342 r = safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG, ret);
5343 if (r != 0)
5344 return r;
5346 (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE);
5347 (void) ignore_signals(SIGPIPE);
5349 if (u->cgroup_path) {
5350 r = cg_attach_everywhere(u->manager->cgroup_supported, u->cgroup_path, 0, NULL, NULL);
5351 if (r < 0) {
5352 log_unit_error_errno(u, r, "Failed to join unit cgroup %s: %m", empty_to_root(u->cgroup_path));
5353 _exit(EXIT_CGROUP);
5357 return 0;
5360 int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid) {
5361 pid_t pid;
5362 int r;
5364 assert(u);
5365 assert(ret_pid);
5367 r = unit_fork_helper_process(u, "(sd-rmrf)", &pid);
5368 if (r < 0)
5369 return r;
5370 if (r == 0) {
5371 int ret = EXIT_SUCCESS;
5373 STRV_FOREACH(i, paths) {
5374 r = rm_rf(*i, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_MISSING_OK);
5375 if (r < 0) {
5376 log_error_errno(r, "Failed to remove '%s': %m", *i);
5377 ret = EXIT_FAILURE;
5381 _exit(ret);
5384 r = unit_watch_pid(u, pid, true);
5385 if (r < 0)
5386 return r;
5388 *ret_pid = pid;
5389 return 0;
5392 static void unit_update_dependency_mask(Hashmap *deps, Unit *other, UnitDependencyInfo di) {
5393 assert(deps);
5394 assert(other);
5396 if (di.origin_mask == 0 && di.destination_mask == 0)
5397 /* No bit set anymore, let's drop the whole entry */
5398 assert_se(hashmap_remove(deps, other));
5399 else
5400 /* Mask was reduced, let's update the entry */
5401 assert_se(hashmap_update(deps, other, di.data) == 0);
5404 void unit_remove_dependencies(Unit *u, UnitDependencyMask mask) {
5405 Hashmap *deps;
5406 assert(u);
5408 /* Removes all dependencies u has on other units marked for ownership by 'mask'. */
5410 if (mask == 0)
5411 return;
5413 HASHMAP_FOREACH(deps, u->dependencies) {
5414 bool done;
5416 do {
5417 UnitDependencyInfo di;
5418 Unit *other;
5420 done = true;
5422 HASHMAP_FOREACH_KEY(di.data, other, deps) {
5423 Hashmap *other_deps;
5425 if (FLAGS_SET(~mask, di.origin_mask))
5426 continue;
5428 di.origin_mask &= ~mask;
5429 unit_update_dependency_mask(deps, other, di);
5431 /* We updated the dependency from our unit to the other unit now. But most
5432 * dependencies imply a reverse dependency. Hence, let's delete that one
5433 * too. For that we go through all dependency types on the other unit and
5434 * delete all those which point to us and have the right mask set. */
5436 HASHMAP_FOREACH(other_deps, other->dependencies) {
5437 UnitDependencyInfo dj;
5439 dj.data = hashmap_get(other_deps, u);
5440 if (FLAGS_SET(~mask, dj.destination_mask))
5441 continue;
5443 dj.destination_mask &= ~mask;
5444 unit_update_dependency_mask(other_deps, u, dj);
5447 unit_add_to_gc_queue(other);
5449 /* The unit 'other' may not be wanted by the unit 'u'. */
5450 unit_submit_to_stop_when_unneeded_queue(other);
5452 done = false;
5453 break;
5456 } while (!done);
5460 static int unit_get_invocation_path(Unit *u, char **ret) {
5461 char *p;
5462 int r;
5464 assert(u);
5465 assert(ret);
5467 if (MANAGER_IS_SYSTEM(u->manager))
5468 p = strjoin("/run/systemd/units/invocation:", u->id);
5469 else {
5470 _cleanup_free_ char *user_path = NULL;
5471 r = xdg_user_runtime_dir(&user_path, "/systemd/units/invocation:");
5472 if (r < 0)
5473 return r;
5474 p = strjoin(user_path, u->id);
5477 if (!p)
5478 return -ENOMEM;
5480 *ret = p;
5481 return 0;
5484 static int unit_export_invocation_id(Unit *u) {
5485 _cleanup_free_ char *p = NULL;
5486 int r;
5488 assert(u);
5490 if (u->exported_invocation_id)
5491 return 0;
5493 if (sd_id128_is_null(u->invocation_id))
5494 return 0;
5496 r = unit_get_invocation_path(u, &p);
5497 if (r < 0)
5498 return log_unit_debug_errno(u, r, "Failed to get invocation path: %m");
5500 r = symlink_atomic_label(u->invocation_id_string, p);
5501 if (r < 0)
5502 return log_unit_debug_errno(u, r, "Failed to create invocation ID symlink %s: %m", p);
5504 u->exported_invocation_id = true;
5505 return 0;
5508 static int unit_export_log_level_max(Unit *u, const ExecContext *c) {
5509 const char *p;
5510 char buf[2];
5511 int r;
5513 assert(u);
5514 assert(c);
5516 if (u->exported_log_level_max)
5517 return 0;
5519 if (c->log_level_max < 0)
5520 return 0;
5522 assert(c->log_level_max <= 7);
5524 buf[0] = '0' + c->log_level_max;
5525 buf[1] = 0;
5527 p = strjoina("/run/systemd/units/log-level-max:", u->id);
5528 r = symlink_atomic(buf, p);
5529 if (r < 0)
5530 return log_unit_debug_errno(u, r, "Failed to create maximum log level symlink %s: %m", p);
5532 u->exported_log_level_max = true;
5533 return 0;
5536 static int unit_export_log_extra_fields(Unit *u, const ExecContext *c) {
5537 _cleanup_close_ int fd = -EBADF;
5538 struct iovec *iovec;
5539 const char *p;
5540 char *pattern;
5541 le64_t *sizes;
5542 ssize_t n;
5543 int r;
5545 if (u->exported_log_extra_fields)
5546 return 0;
5548 if (c->n_log_extra_fields <= 0)
5549 return 0;
5551 sizes = newa(le64_t, c->n_log_extra_fields);
5552 iovec = newa(struct iovec, c->n_log_extra_fields * 2);
5554 for (size_t i = 0; i < c->n_log_extra_fields; i++) {
5555 sizes[i] = htole64(c->log_extra_fields[i].iov_len);
5557 iovec[i*2] = IOVEC_MAKE(sizes + i, sizeof(le64_t));
5558 iovec[i*2+1] = c->log_extra_fields[i];
5561 p = strjoina("/run/systemd/units/log-extra-fields:", u->id);
5562 pattern = strjoina(p, ".XXXXXX");
5564 fd = mkostemp_safe(pattern);
5565 if (fd < 0)
5566 return log_unit_debug_errno(u, fd, "Failed to create extra fields file %s: %m", p);
5568 n = writev(fd, iovec, c->n_log_extra_fields*2);
5569 if (n < 0) {
5570 r = log_unit_debug_errno(u, errno, "Failed to write extra fields: %m");
5571 goto fail;
5574 (void) fchmod(fd, 0644);
5576 if (rename(pattern, p) < 0) {
5577 r = log_unit_debug_errno(u, errno, "Failed to rename extra fields file: %m");
5578 goto fail;
5581 u->exported_log_extra_fields = true;
5582 return 0;
5584 fail:
5585 (void) unlink(pattern);
5586 return r;
5589 static int unit_export_log_ratelimit_interval(Unit *u, const ExecContext *c) {
5590 _cleanup_free_ char *buf = NULL;
5591 const char *p;
5592 int r;
5594 assert(u);
5595 assert(c);
5597 if (u->exported_log_ratelimit_interval)
5598 return 0;
5600 if (c->log_ratelimit_interval_usec == 0)
5601 return 0;
5603 p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id);
5605 if (asprintf(&buf, "%" PRIu64, c->log_ratelimit_interval_usec) < 0)
5606 return log_oom();
5608 r = symlink_atomic(buf, p);
5609 if (r < 0)
5610 return log_unit_debug_errno(u, r, "Failed to create log rate limit interval symlink %s: %m", p);
5612 u->exported_log_ratelimit_interval = true;
5613 return 0;
5616 static int unit_export_log_ratelimit_burst(Unit *u, const ExecContext *c) {
5617 _cleanup_free_ char *buf = NULL;
5618 const char *p;
5619 int r;
5621 assert(u);
5622 assert(c);
5624 if (u->exported_log_ratelimit_burst)
5625 return 0;
5627 if (c->log_ratelimit_burst == 0)
5628 return 0;
5630 p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id);
5632 if (asprintf(&buf, "%u", c->log_ratelimit_burst) < 0)
5633 return log_oom();
5635 r = symlink_atomic(buf, p);
5636 if (r < 0)
5637 return log_unit_debug_errno(u, r, "Failed to create log rate limit burst symlink %s: %m", p);
5639 u->exported_log_ratelimit_burst = true;
5640 return 0;
5643 void unit_export_state_files(Unit *u) {
5644 const ExecContext *c;
5646 assert(u);
5648 if (!u->id)
5649 return;
5651 if (MANAGER_IS_TEST_RUN(u->manager))
5652 return;
5654 /* Exports a couple of unit properties to /run/systemd/units/, so that journald can quickly query this data
5655 * from there. Ideally, journald would use IPC to query this, like everybody else, but that's hard, as long as
5656 * the IPC system itself and PID 1 also log to the journal.
5658 * Note that these files really shouldn't be considered API for anyone else, as use a runtime file system as
5659 * IPC replacement is not compatible with today's world of file system namespaces. However, this doesn't really
5660 * apply to communication between the journal and systemd, as we assume that these two daemons live in the same
5661 * namespace at least.
5663 * Note that some of the "files" exported here are actually symlinks and not regular files. Symlinks work
5664 * better for storing small bits of data, in particular as we can write them with two system calls, and read
5665 * them with one. */
5667 (void) unit_export_invocation_id(u);
5669 if (!MANAGER_IS_SYSTEM(u->manager))
5670 return;
5672 c = unit_get_exec_context(u);
5673 if (c) {
5674 (void) unit_export_log_level_max(u, c);
5675 (void) unit_export_log_extra_fields(u, c);
5676 (void) unit_export_log_ratelimit_interval(u, c);
5677 (void) unit_export_log_ratelimit_burst(u, c);
5681 void unit_unlink_state_files(Unit *u) {
5682 const char *p;
5684 assert(u);
5686 if (!u->id)
5687 return;
5689 /* Undoes the effect of unit_export_state() */
5691 if (u->exported_invocation_id) {
5692 _cleanup_free_ char *invocation_path = NULL;
5693 int r = unit_get_invocation_path(u, &invocation_path);
5694 if (r >= 0) {
5695 (void) unlink(invocation_path);
5696 u->exported_invocation_id = false;
5700 if (!MANAGER_IS_SYSTEM(u->manager))
5701 return;
5703 if (u->exported_log_level_max) {
5704 p = strjoina("/run/systemd/units/log-level-max:", u->id);
5705 (void) unlink(p);
5707 u->exported_log_level_max = false;
5710 if (u->exported_log_extra_fields) {
5711 p = strjoina("/run/systemd/units/extra-fields:", u->id);
5712 (void) unlink(p);
5714 u->exported_log_extra_fields = false;
5717 if (u->exported_log_ratelimit_interval) {
5718 p = strjoina("/run/systemd/units/log-rate-limit-interval:", u->id);
5719 (void) unlink(p);
5721 u->exported_log_ratelimit_interval = false;
5724 if (u->exported_log_ratelimit_burst) {
5725 p = strjoina("/run/systemd/units/log-rate-limit-burst:", u->id);
5726 (void) unlink(p);
5728 u->exported_log_ratelimit_burst = false;
5732 int unit_prepare_exec(Unit *u) {
5733 int r;
5735 assert(u);
5737 /* Load any custom firewall BPF programs here once to test if they are existing and actually loadable.
5738 * Fail here early since later errors in the call chain unit_realize_cgroup to cgroup_context_apply are ignored. */
5739 r = bpf_firewall_load_custom(u);
5740 if (r < 0)
5741 return r;
5743 /* Prepares everything so that we can fork of a process for this unit */
5745 (void) unit_realize_cgroup(u);
5747 if (u->reset_accounting) {
5748 (void) unit_reset_accounting(u);
5749 u->reset_accounting = false;
5752 unit_export_state_files(u);
5754 r = unit_setup_exec_runtime(u);
5755 if (r < 0)
5756 return r;
5758 return 0;
5761 static bool ignore_leftover_process(const char *comm) {
5762 return comm && comm[0] == '('; /* Most likely our own helper process (PAM?), ignore */
5765 int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) {
5766 _cleanup_free_ char *comm = NULL;
5768 (void) get_process_comm(pid, &comm);
5770 if (ignore_leftover_process(comm))
5771 return 0;
5773 /* During start we print a warning */
5775 log_unit_warning(userdata,
5776 "Found left-over process " PID_FMT " (%s) in control group while starting unit. Ignoring.\n"
5777 "This usually indicates unclean termination of a previous run, or service implementation deficiencies.",
5778 pid, strna(comm));
5780 return 1;
5783 int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata) {
5784 _cleanup_free_ char *comm = NULL;
5786 (void) get_process_comm(pid, &comm);
5788 if (ignore_leftover_process(comm))
5789 return 0;
5791 /* During stop we only print an informational message */
5793 log_unit_info(userdata,
5794 "Unit process " PID_FMT " (%s) remains running after unit stopped.",
5795 pid, strna(comm));
5797 return 1;
5800 int unit_warn_leftover_processes(Unit *u, cg_kill_log_func_t log_func) {
5801 assert(u);
5803 (void) unit_pick_cgroup_path(u);
5805 if (!u->cgroup_path)
5806 return 0;
5808 return cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, 0, 0, NULL, log_func, u);
5811 bool unit_needs_console(Unit *u) {
5812 ExecContext *ec;
5813 UnitActiveState state;
5815 assert(u);
5817 state = unit_active_state(u);
5819 if (UNIT_IS_INACTIVE_OR_FAILED(state))
5820 return false;
5822 if (UNIT_VTABLE(u)->needs_console)
5823 return UNIT_VTABLE(u)->needs_console(u);
5825 /* If this unit type doesn't implement this call, let's use a generic fallback implementation: */
5826 ec = unit_get_exec_context(u);
5827 if (!ec)
5828 return false;
5830 return exec_context_may_touch_console(ec);
5833 int unit_pid_attachable(Unit *u, pid_t pid, sd_bus_error *error) {
5834 int r;
5836 assert(u);
5838 /* Checks whether the specified PID is generally good for attaching, i.e. a valid PID, not our manager itself,
5839 * and not a kernel thread either */
5841 /* First, a simple range check */
5842 if (!pid_is_valid(pid))
5843 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process identifier " PID_FMT " is not valid.", pid);
5845 /* Some extra safety check */
5846 if (pid == 1 || pid == getpid_cached())
5847 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a manager process, refusing.", pid);
5849 /* Don't even begin to bother with kernel threads */
5850 r = is_kernel_thread(pid);
5851 if (r == -ESRCH)
5852 return sd_bus_error_setf(error, SD_BUS_ERROR_UNIX_PROCESS_ID_UNKNOWN, "Process with ID " PID_FMT " does not exist.", pid);
5853 if (r < 0)
5854 return sd_bus_error_set_errnof(error, r, "Failed to determine whether process " PID_FMT " is a kernel thread: %m", pid);
5855 if (r > 0)
5856 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Process " PID_FMT " is a kernel thread, refusing.", pid);
5858 return 0;
5861 void unit_log_success(Unit *u) {
5862 assert(u);
5864 /* Let's show message "Deactivated successfully" in debug mode (when manager is user) rather than in info mode.
5865 * This message has low information value for regular users and it might be a bit overwhelming on a system with
5866 * a lot of devices. */
5867 log_unit_struct(u,
5868 MANAGER_IS_USER(u->manager) ? LOG_DEBUG : LOG_INFO,
5869 "MESSAGE_ID=" SD_MESSAGE_UNIT_SUCCESS_STR,
5870 LOG_UNIT_INVOCATION_ID(u),
5871 LOG_UNIT_MESSAGE(u, "Deactivated successfully."));
5874 void unit_log_failure(Unit *u, const char *result) {
5875 assert(u);
5876 assert(result);
5878 log_unit_struct(u, LOG_WARNING,
5879 "MESSAGE_ID=" SD_MESSAGE_UNIT_FAILURE_RESULT_STR,
5880 LOG_UNIT_INVOCATION_ID(u),
5881 LOG_UNIT_MESSAGE(u, "Failed with result '%s'.", result),
5882 "UNIT_RESULT=%s", result);
5885 void unit_log_skip(Unit *u, const char *result) {
5886 assert(u);
5887 assert(result);
5889 log_unit_struct(u, LOG_INFO,
5890 "MESSAGE_ID=" SD_MESSAGE_UNIT_SKIPPED_STR,
5891 LOG_UNIT_INVOCATION_ID(u),
5892 LOG_UNIT_MESSAGE(u, "Skipped due to '%s'.", result),
5893 "UNIT_RESULT=%s", result);
5896 void unit_log_process_exit(
5897 Unit *u,
5898 const char *kind,
5899 const char *command,
5900 bool success,
5901 int code,
5902 int status) {
5904 int level;
5906 assert(u);
5907 assert(kind);
5909 /* If this is a successful exit, let's log about the exit code on DEBUG level. If this is a failure
5910 * and the process exited on its own via exit(), then let's make this a NOTICE, under the assumption
5911 * that the service already logged the reason at a higher log level on its own. Otherwise, make it a
5912 * WARNING. */
5913 if (success)
5914 level = LOG_DEBUG;
5915 else if (code == CLD_EXITED)
5916 level = LOG_NOTICE;
5917 else
5918 level = LOG_WARNING;
5920 log_unit_struct(u, level,
5921 "MESSAGE_ID=" SD_MESSAGE_UNIT_PROCESS_EXIT_STR,
5922 LOG_UNIT_MESSAGE(u, "%s exited, code=%s, status=%i/%s%s",
5923 kind,
5924 sigchld_code_to_string(code), status,
5925 strna(code == CLD_EXITED
5926 ? exit_status_to_string(status, EXIT_STATUS_FULL)
5927 : signal_to_string(status)),
5928 success ? " (success)" : ""),
5929 "EXIT_CODE=%s", sigchld_code_to_string(code),
5930 "EXIT_STATUS=%i", status,
5931 "COMMAND=%s", strna(command),
5932 LOG_UNIT_INVOCATION_ID(u));
5935 int unit_exit_status(Unit *u) {
5936 assert(u);
5938 /* Returns the exit status to propagate for the most recent cycle of this unit. Returns a value in the range
5939 * 0…255 if there's something to propagate. EOPNOTSUPP if the concept does not apply to this unit type, ENODATA
5940 * if no data is currently known (for example because the unit hasn't deactivated yet) and EBADE if the main
5941 * service process has exited abnormally (signal/coredump). */
5943 if (!UNIT_VTABLE(u)->exit_status)
5944 return -EOPNOTSUPP;
5946 return UNIT_VTABLE(u)->exit_status(u);
5949 int unit_failure_action_exit_status(Unit *u) {
5950 int r;
5952 assert(u);
5954 /* Returns the exit status to propagate on failure, or an error if there's nothing to propagate */
5956 if (u->failure_action_exit_status >= 0)
5957 return u->failure_action_exit_status;
5959 r = unit_exit_status(u);
5960 if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */
5961 return 255;
5963 return r;
5966 int unit_success_action_exit_status(Unit *u) {
5967 int r;
5969 assert(u);
5971 /* Returns the exit status to propagate on success, or an error if there's nothing to propagate */
5973 if (u->success_action_exit_status >= 0)
5974 return u->success_action_exit_status;
5976 r = unit_exit_status(u);
5977 if (r == -EBADE) /* Exited, but not cleanly (i.e. by signal or such) */
5978 return 255;
5980 return r;
5983 int unit_test_trigger_loaded(Unit *u) {
5984 Unit *trigger;
5986 /* Tests whether the unit to trigger is loaded */
5988 trigger = UNIT_TRIGGER(u);
5989 if (!trigger)
5990 return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT),
5991 "Refusing to start, no unit to trigger.");
5992 if (trigger->load_state != UNIT_LOADED)
5993 return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT),
5994 "Refusing to start, unit %s to trigger not loaded.", trigger->id);
5996 return 0;
5999 void unit_destroy_runtime_data(Unit *u, const ExecContext *context) {
6000 assert(u);
6001 assert(context);
6003 /* EXEC_PRESERVE_RESTART is handled via unit_release_resources()! */
6004 if (context->runtime_directory_preserve_mode == EXEC_PRESERVE_NO)
6005 exec_context_destroy_runtime_directory(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]);
6007 exec_context_destroy_credentials(context, u->manager->prefix[EXEC_DIRECTORY_RUNTIME], u->id);
6008 exec_context_destroy_mount_ns_dir(u);
6011 int unit_clean(Unit *u, ExecCleanMask mask) {
6012 UnitActiveState state;
6014 assert(u);
6016 /* Special return values:
6018 * -EOPNOTSUPP → cleaning not supported for this unit type
6019 * -EUNATCH → cleaning not defined for this resource type
6020 * -EBUSY → unit currently can't be cleaned since it's running or not properly loaded, or has
6021 * a job queued or similar
6024 if (!UNIT_VTABLE(u)->clean)
6025 return -EOPNOTSUPP;
6027 if (mask == 0)
6028 return -EUNATCH;
6030 if (u->load_state != UNIT_LOADED)
6031 return -EBUSY;
6033 if (u->job)
6034 return -EBUSY;
6036 state = unit_active_state(u);
6037 if (state != UNIT_INACTIVE)
6038 return -EBUSY;
6040 return UNIT_VTABLE(u)->clean(u, mask);
6043 int unit_can_clean(Unit *u, ExecCleanMask *ret) {
6044 assert(u);
6046 if (!UNIT_VTABLE(u)->clean ||
6047 u->load_state != UNIT_LOADED) {
6048 *ret = 0;
6049 return 0;
6052 /* When the clean() method is set, can_clean() really should be set too */
6053 assert(UNIT_VTABLE(u)->can_clean);
6055 return UNIT_VTABLE(u)->can_clean(u, ret);
6058 bool unit_can_freeze(Unit *u) {
6059 assert(u);
6061 if (UNIT_VTABLE(u)->can_freeze)
6062 return UNIT_VTABLE(u)->can_freeze(u);
6064 return UNIT_VTABLE(u)->freeze;
6067 void unit_frozen(Unit *u) {
6068 assert(u);
6070 u->freezer_state = FREEZER_FROZEN;
6072 bus_unit_send_pending_freezer_message(u, false);
6075 void unit_thawed(Unit *u) {
6076 assert(u);
6078 u->freezer_state = FREEZER_RUNNING;
6080 bus_unit_send_pending_freezer_message(u, false);
6083 static int unit_freezer_action(Unit *u, FreezerAction action) {
6084 UnitActiveState s;
6085 int (*method)(Unit*);
6086 int r;
6088 assert(u);
6089 assert(IN_SET(action, FREEZER_FREEZE, FREEZER_THAW));
6091 method = action == FREEZER_FREEZE ? UNIT_VTABLE(u)->freeze : UNIT_VTABLE(u)->thaw;
6092 if (!method || !cg_freezer_supported())
6093 return -EOPNOTSUPP;
6095 if (u->job)
6096 return -EBUSY;
6098 if (u->load_state != UNIT_LOADED)
6099 return -EHOSTDOWN;
6101 s = unit_active_state(u);
6102 if (s != UNIT_ACTIVE)
6103 return -EHOSTDOWN;
6105 if ((IN_SET(u->freezer_state, FREEZER_FREEZING, FREEZER_THAWING) && action == FREEZER_FREEZE) ||
6106 (u->freezer_state == FREEZER_THAWING && action == FREEZER_THAW))
6107 return -EALREADY;
6109 r = method(u);
6110 if (r <= 0)
6111 return r;
6113 assert(IN_SET(u->freezer_state, FREEZER_FREEZING, FREEZER_THAWING));
6115 return 1;
6118 int unit_freeze(Unit *u) {
6119 return unit_freezer_action(u, FREEZER_FREEZE);
6122 int unit_thaw(Unit *u) {
6123 return unit_freezer_action(u, FREEZER_THAW);
6126 /* Wrappers around low-level cgroup freezer operations common for service and scope units */
6127 int unit_freeze_vtable_common(Unit *u) {
6128 return unit_cgroup_freezer_action(u, FREEZER_FREEZE);
6131 int unit_thaw_vtable_common(Unit *u) {
6132 return unit_cgroup_freezer_action(u, FREEZER_THAW);
6135 Condition *unit_find_failed_condition(Unit *u) {
6136 Condition *failed_trigger = NULL;
6137 bool has_succeeded_trigger = false;
6139 if (u->condition_result)
6140 return NULL;
6142 LIST_FOREACH(conditions, c, u->conditions)
6143 if (c->trigger) {
6144 if (c->result == CONDITION_SUCCEEDED)
6145 has_succeeded_trigger = true;
6146 else if (!failed_trigger)
6147 failed_trigger = c;
6148 } else if (c->result != CONDITION_SUCCEEDED)
6149 return c;
6151 return failed_trigger && !has_succeeded_trigger ? failed_trigger : NULL;
6154 static const char* const collect_mode_table[_COLLECT_MODE_MAX] = {
6155 [COLLECT_INACTIVE] = "inactive",
6156 [COLLECT_INACTIVE_OR_FAILED] = "inactive-or-failed",
6159 DEFINE_STRING_TABLE_LOOKUP(collect_mode, CollectMode);
6161 Unit* unit_has_dependency(const Unit *u, UnitDependencyAtom atom, Unit *other) {
6162 Unit *i;
6164 assert(u);
6166 /* Checks if the unit has a dependency on 'other' with the specified dependency atom. If 'other' is
6167 * NULL checks if the unit has *any* dependency of that atom. Returns 'other' if found (or if 'other'
6168 * is NULL the first entry found), or NULL if not found. */
6170 UNIT_FOREACH_DEPENDENCY(i, u, atom)
6171 if (!other || other == i)
6172 return i;
6174 return NULL;
6177 int unit_get_dependency_array(const Unit *u, UnitDependencyAtom atom, Unit ***ret_array) {
6178 _cleanup_free_ Unit **array = NULL;
6179 size_t n = 0;
6180 Unit *other;
6182 assert(u);
6183 assert(ret_array);
6185 /* Gets a list of units matching a specific atom as array. This is useful when iterating through
6186 * dependencies while modifying them: the array is an "atomic snapshot" of sorts, that can be read
6187 * while the dependency table is continuously updated. */
6189 UNIT_FOREACH_DEPENDENCY(other, u, atom) {
6190 if (!GREEDY_REALLOC(array, n + 1))
6191 return -ENOMEM;
6193 array[n++] = other;
6196 *ret_array = TAKE_PTR(array);
6198 assert(n <= INT_MAX);
6199 return (int) n;
6202 int unit_get_transitive_dependency_set(Unit *u, UnitDependencyAtom atom, Set **ret) {
6203 _cleanup_set_free_ Set *units = NULL, *queue = NULL;
6204 Unit *other;
6205 int r;
6207 assert(u);
6208 assert(ret);
6210 /* Similar to unit_get_dependency_array(), but also search the same dependency in other units. */
6212 do {
6213 UNIT_FOREACH_DEPENDENCY(other, u, atom) {
6214 r = set_ensure_put(&units, NULL, other);
6215 if (r < 0)
6216 return r;
6217 if (r == 0)
6218 continue;
6219 r = set_ensure_put(&queue, NULL, other);
6220 if (r < 0)
6221 return r;
6223 } while ((u = set_steal_first(queue)));
6225 *ret = TAKE_PTR(units);
6226 return 0;
6229 const ActivationDetailsVTable * const activation_details_vtable[_UNIT_TYPE_MAX] = {
6230 [UNIT_PATH] = &activation_details_path_vtable,
6231 [UNIT_TIMER] = &activation_details_timer_vtable,
6234 ActivationDetails *activation_details_new(Unit *trigger_unit) {
6235 _cleanup_free_ ActivationDetails *details = NULL;
6237 assert(trigger_unit);
6238 assert(trigger_unit->type != _UNIT_TYPE_INVALID);
6239 assert(trigger_unit->id);
6241 details = malloc0(activation_details_vtable[trigger_unit->type]->object_size);
6242 if (!details)
6243 return NULL;
6245 *details = (ActivationDetails) {
6246 .n_ref = 1,
6247 .trigger_unit_type = trigger_unit->type,
6250 details->trigger_unit_name = strdup(trigger_unit->id);
6251 if (!details->trigger_unit_name)
6252 return NULL;
6254 if (ACTIVATION_DETAILS_VTABLE(details)->init)
6255 ACTIVATION_DETAILS_VTABLE(details)->init(details, trigger_unit);
6257 return TAKE_PTR(details);
6260 static ActivationDetails *activation_details_free(ActivationDetails *details) {
6261 if (!details)
6262 return NULL;
6264 if (ACTIVATION_DETAILS_VTABLE(details)->done)
6265 ACTIVATION_DETAILS_VTABLE(details)->done(details);
6267 free(details->trigger_unit_name);
6269 return mfree(details);
6272 void activation_details_serialize(ActivationDetails *details, FILE *f) {
6273 if (!details || details->trigger_unit_type == _UNIT_TYPE_INVALID)
6274 return;
6276 (void) serialize_item(f, "activation-details-unit-type", unit_type_to_string(details->trigger_unit_type));
6277 if (details->trigger_unit_name)
6278 (void) serialize_item(f, "activation-details-unit-name", details->trigger_unit_name);
6279 if (ACTIVATION_DETAILS_VTABLE(details)->serialize)
6280 ACTIVATION_DETAILS_VTABLE(details)->serialize(details, f);
6283 int activation_details_deserialize(const char *key, const char *value, ActivationDetails **details) {
6284 int r;
6286 assert(key);
6287 assert(value);
6288 assert(details);
6290 if (!*details) {
6291 UnitType t;
6293 if (!streq(key, "activation-details-unit-type"))
6294 return -EINVAL;
6296 t = unit_type_from_string(value);
6297 if (t < 0)
6298 return t;
6300 /* The activation details vtable has defined ops only for path and timer units */
6301 if (!activation_details_vtable[t])
6302 return -EINVAL;
6304 *details = malloc0(activation_details_vtable[t]->object_size);
6305 if (!*details)
6306 return -ENOMEM;
6308 **details = (ActivationDetails) {
6309 .n_ref = 1,
6310 .trigger_unit_type = t,
6313 return 0;
6316 if (streq(key, "activation-details-unit-name")) {
6317 r = free_and_strdup(&(*details)->trigger_unit_name, value);
6318 if (r < 0)
6319 return r;
6321 return 0;
6324 if (ACTIVATION_DETAILS_VTABLE(*details)->deserialize)
6325 return ACTIVATION_DETAILS_VTABLE(*details)->deserialize(key, value, details);
6327 return -EINVAL;
6330 int activation_details_append_env(ActivationDetails *details, char ***strv) {
6331 int r = 0;
6333 assert(strv);
6335 if (!details)
6336 return 0;
6338 if (!isempty(details->trigger_unit_name)) {
6339 char *s = strjoin("TRIGGER_UNIT=", details->trigger_unit_name);
6340 if (!s)
6341 return -ENOMEM;
6343 r = strv_consume(strv, TAKE_PTR(s));
6344 if (r < 0)
6345 return r;
6348 if (ACTIVATION_DETAILS_VTABLE(details)->append_env) {
6349 r = ACTIVATION_DETAILS_VTABLE(details)->append_env(details, strv);
6350 if (r < 0)
6351 return r;
6354 return r + !isempty(details->trigger_unit_name); /* Return the number of variables added to the env block */
6357 int activation_details_append_pair(ActivationDetails *details, char ***strv) {
6358 int r = 0;
6360 assert(strv);
6362 if (!details)
6363 return 0;
6365 if (!isempty(details->trigger_unit_name)) {
6366 r = strv_extend(strv, "trigger_unit");
6367 if (r < 0)
6368 return r;
6370 r = strv_extend(strv, details->trigger_unit_name);
6371 if (r < 0)
6372 return r;
6375 if (ACTIVATION_DETAILS_VTABLE(details)->append_pair) {
6376 r = ACTIVATION_DETAILS_VTABLE(details)->append_pair(details, strv);
6377 if (r < 0)
6378 return r;
6381 return r + !isempty(details->trigger_unit_name); /* Return the number of pairs added to the strv */
6384 DEFINE_TRIVIAL_REF_UNREF_FUNC(ActivationDetails, activation_details, activation_details_free);