HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/unit.c

263,853 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 1de634c975a3311494dacb28ce1bb5e67261717a

Lines 3001–4000 of 7,295

                array[m] = NULL; /* set trailing NULL marker on the new end */
 
                if (m == 0) {
                        /* The array is now empty, remove the entire entry */
                        assert_se(hashmap_remove_value(u->manager->watch_pids_more, pid3, array));
                        free(array);
                } else {
                        /* The array is not empty, but let's make sure the entry is not keyed by the PidRef
                         * we will delete, but by the PidRef object of the Unit that is now first in the
                         * array. */
 
                        PidRef *new_pid3 = ASSERT_PTR(set_get(array[0]->pids, pid));
                        assert_se(hashmap_replace(u->manager->watch_pids_more, new_pid3, array) >= 0);
                }
        }
}
 
void unit_unwatch_all_pids(Unit *u) {
        assert(u);
 
        while (!set_isempty(u->pids))
                unit_unwatch_pidref(u, set_first(u->pids));
 
        u->pids = set_free(u->pids);
}
 
void unit_unwatch_pidref_done(Unit *u, PidRef *pidref) {
        assert(u);
 
        if (!pidref_is_set(pidref))
                return;
 
        unit_unwatch_pidref(u, pidref);
        pidref_done(pidref);
}
 
bool unit_job_is_applicable(Unit *u, JobType j) {
        assert(u);
        assert(j >= 0 && j < _JOB_TYPE_MAX);
 
        switch (j) {
 
        case JOB_VERIFY_ACTIVE:
        case JOB_START:
        case JOB_NOP:
                /* Note that we don't check unit_can_start() here. That's because .device units and suchlike are not
                 * startable by us but may appear due to external events, and it thus makes sense to permit enqueuing
                 * jobs for it. */
                return true;
 
        case JOB_STOP:
                /* Similar as above. However, perpetual units can never be stopped (neither explicitly nor due to
                 * external events), hence it makes no sense to permit enqueuing such a request either. */
                return !u->perpetual;
 
        case JOB_RESTART:
        case JOB_TRY_RESTART:
                return unit_can_stop(u) && unit_can_start(u);
 
        case JOB_RELOAD:
        case JOB_TRY_RELOAD:
                return unit_can_reload(u);
 
        case JOB_RELOAD_OR_START:
                return unit_can_reload(u) && unit_can_start(u);
 
        default:
                assert_not_reached();
        }
}
 
static Hashmap *unit_get_dependency_hashmap_per_type(Unit *u, UnitDependency d) {
        Hashmap *deps;
 
        assert(u);
        assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
 
        deps = hashmap_get(u->dependencies, UNIT_DEPENDENCY_TO_PTR(d));
        if (!deps) {
                _cleanup_hashmap_free_ Hashmap *h = NULL;
 
                h = hashmap_new(NULL);
                if (!h)
                        return NULL;
 
                if (hashmap_ensure_put(&u->dependencies, NULL, UNIT_DEPENDENCY_TO_PTR(d), h) < 0)
                        return NULL;
 
                deps = TAKE_PTR(h);
        }
 
        return deps;
}
 
typedef enum NotifyDependencyFlags {
        NOTIFY_DEPENDENCY_UPDATE_FROM = 1 << 0,
        NOTIFY_DEPENDENCY_UPDATE_TO   = 1 << 1,
} NotifyDependencyFlags;
 
static int unit_add_dependency_impl(
                Unit *u,
                UnitDependency d,
                Unit *other,
                UnitDependencyMask mask) {
 
        static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
                [UNIT_REQUIRES]               = UNIT_REQUIRED_BY,
                [UNIT_REQUISITE]              = UNIT_REQUISITE_OF,
                [UNIT_WANTS]                  = UNIT_WANTED_BY,
                [UNIT_BINDS_TO]               = UNIT_BOUND_BY,
                [UNIT_PART_OF]                = UNIT_CONSISTS_OF,
                [UNIT_UPHOLDS]                = UNIT_UPHELD_BY,
                [UNIT_REQUIRED_BY]            = UNIT_REQUIRES,
                [UNIT_REQUISITE_OF]           = UNIT_REQUISITE,
                [UNIT_WANTED_BY]              = UNIT_WANTS,
                [UNIT_BOUND_BY]               = UNIT_BINDS_TO,
                [UNIT_CONSISTS_OF]            = UNIT_PART_OF,
                [UNIT_UPHELD_BY]              = UNIT_UPHOLDS,
                [UNIT_CONFLICTS]              = UNIT_CONFLICTED_BY,
                [UNIT_CONFLICTED_BY]          = UNIT_CONFLICTS,
                [UNIT_BEFORE]                 = UNIT_AFTER,
                [UNIT_AFTER]                  = UNIT_BEFORE,
                [UNIT_ON_SUCCESS]             = UNIT_ON_SUCCESS_OF,
                [UNIT_ON_SUCCESS_OF]          = UNIT_ON_SUCCESS,
                [UNIT_ON_FAILURE]             = UNIT_ON_FAILURE_OF,
                [UNIT_ON_FAILURE_OF]          = UNIT_ON_FAILURE,
                [UNIT_TRIGGERS]               = UNIT_TRIGGERED_BY,
                [UNIT_TRIGGERED_BY]           = UNIT_TRIGGERS,
                [UNIT_PROPAGATES_RELOAD_TO]   = UNIT_RELOAD_PROPAGATED_FROM,
                [UNIT_RELOAD_PROPAGATED_FROM] = UNIT_PROPAGATES_RELOAD_TO,
                [UNIT_PROPAGATES_STOP_TO]     = UNIT_STOP_PROPAGATED_FROM,
                [UNIT_STOP_PROPAGATED_FROM]   = UNIT_PROPAGATES_STOP_TO,
                [UNIT_JOINS_NAMESPACE_OF]     = UNIT_JOINS_NAMESPACE_OF, /* symmetric! 👓 */
                [UNIT_REFERENCES]             = UNIT_REFERENCED_BY,
                [UNIT_REFERENCED_BY]          = UNIT_REFERENCES,
                [UNIT_IN_SLICE]               = UNIT_SLICE_OF,
                [UNIT_SLICE_OF]               = UNIT_IN_SLICE,
        };
 
        Hashmap *u_deps, *other_deps;
        UnitDependencyInfo u_info, u_info_old, other_info, other_info_old;
        NotifyDependencyFlags flags = 0;
        int r;
 
        assert(u);
        assert(other);
        assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
        assert(inverse_table[d] >= 0 && inverse_table[d] < _UNIT_DEPENDENCY_MAX);
        assert(mask > 0 && mask < _UNIT_DEPENDENCY_MASK_FULL);
 
        /* Ensure the following two hashmaps for each unit exist:
         * - the top-level dependency hashmap that maps UnitDependency → Hashmap(Unit* → UnitDependencyInfo),
         * - the inner hashmap, that maps Unit* → UnitDependencyInfo, for the specified dependency type. */
        u_deps = unit_get_dependency_hashmap_per_type(u, d);
        if (!u_deps)
                return -ENOMEM;
 
        other_deps = unit_get_dependency_hashmap_per_type(other, inverse_table[d]);
        if (!other_deps)
                return -ENOMEM;
 
        /* Save the original dependency info. */
        u_info.data = u_info_old.data = hashmap_get(u_deps, other);
        other_info.data = other_info_old.data = hashmap_get(other_deps, u);
 
        /* Update dependency info. */
        u_info.origin_mask |= mask;
        other_info.destination_mask |= mask;
 
        /* Save updated dependency info. */
        if (u_info.data != u_info_old.data) {
                r = hashmap_replace(u_deps, other, u_info.data);
                if (r < 0)
                        return r;
 
                flags = NOTIFY_DEPENDENCY_UPDATE_FROM;
                u->dependency_generation++;
        }
 
        if (other_info.data != other_info_old.data) {
                r = hashmap_replace(other_deps, u, other_info.data);
                if (r < 0) {
                        if (u_info.data != u_info_old.data) {
                                /* Restore the old dependency. */
                                if (u_info_old.data)
                                        (void) hashmap_update(u_deps, other, u_info_old.data);
                                else
                                        hashmap_remove(u_deps, other);
                        }
                        return r;
                }
 
                flags |= NOTIFY_DEPENDENCY_UPDATE_TO;
                other->dependency_generation++;
        }
 
        return flags;
}
 
int unit_add_dependency(
                Unit *u,
                UnitDependency d,
                Unit *other,
                bool add_reference,
                UnitDependencyMask mask) {
 
        UnitDependencyAtom a;
        int r;
 
        /* Helper to know whether sending a notification is necessary or not: if the dependency is already
         * there, no need to notify! */
        NotifyDependencyFlags notify_flags;
 
        assert(u);
        assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
        assert(other);
 
        u = unit_follow_merge(u);
        other = unit_follow_merge(other);
        a = unit_dependency_to_atom(d);
        assert(a >= 0);
 
        /* We won't allow dependencies on ourselves. We will not consider them an error however. */
        if (u == other) {
                if (unit_should_warn_about_dependency(d))
                        log_unit_warning(u, "Dependency %s=%s is dropped.",
                                         unit_dependency_to_string(d), u->id);
                return 0;
        }
 
        if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
                return 0;
 
        /* Note that ordering a device unit after a unit is permitted since it allows its job running
         * timeout to be started at a specific time. */
        if (FLAGS_SET(a, UNIT_ATOM_BEFORE) && other->type == UNIT_DEVICE) {
                log_unit_warning(u, "Dependency Before=%s ignored (.device units cannot be delayed)", other->id);
                return 0;
        }
 
        if (FLAGS_SET(a, UNIT_ATOM_ON_FAILURE) && !UNIT_VTABLE(u)->can_fail) {
                log_unit_warning(u, "Requested dependency OnFailure=%s ignored (%s units cannot fail).", other->id, unit_type_to_string(u->type));
                return 0;
        }
 
        if (FLAGS_SET(a, UNIT_ATOM_TRIGGERS) && !UNIT_VTABLE(u)->can_trigger)
                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                            "Requested dependency Triggers=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(u->type));
        if (FLAGS_SET(a, UNIT_ATOM_TRIGGERED_BY) && !UNIT_VTABLE(other)->can_trigger)
                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                            "Requested dependency TriggeredBy=%s refused (%s units cannot trigger other units).", other->id, unit_type_to_string(other->type));
 
        if (FLAGS_SET(a, UNIT_ATOM_IN_SLICE) && other->type != UNIT_SLICE)
                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                            "Requested dependency Slice=%s refused (%s is not a slice unit).", other->id, other->id);
        if (FLAGS_SET(a, UNIT_ATOM_SLICE_OF) && u->type != UNIT_SLICE)
                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                            "Requested dependency SliceOf=%s refused (%s is not a slice unit).", other->id, u->id);
 
        if (FLAGS_SET(a, UNIT_ATOM_IN_SLICE) && !UNIT_HAS_CGROUP_CONTEXT(u))
                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                            "Requested dependency Slice=%s refused (%s is not a cgroup unit).", other->id, u->id);
 
        if (FLAGS_SET(a, UNIT_ATOM_SLICE_OF) && !UNIT_HAS_CGROUP_CONTEXT(other))
                return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
                                            "Requested dependency SliceOf=%s refused (%s is not a cgroup unit).", other->id, other->id);
 
        r = unit_add_dependency_impl(u, d, other, mask);
        if (r < 0)
                return r;
        notify_flags = r;
 
        if (add_reference) {
                r = unit_add_dependency_impl(u, UNIT_REFERENCES, other, mask);
                if (r < 0)
                        return r;
                notify_flags |= r;
        }
 
        if (FLAGS_SET(notify_flags, NOTIFY_DEPENDENCY_UPDATE_FROM))
                unit_add_to_dbus_queue(u);
        if (FLAGS_SET(notify_flags, NOTIFY_DEPENDENCY_UPDATE_TO))
                unit_add_to_dbus_queue(other);
 
        return notify_flags != 0;
}
 
int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask) {
        int r = 0, s = 0;
 
        assert(u);
        assert(d >= 0 || e >= 0);
 
        if (d >= 0) {
                r = unit_add_dependency(u, d, other, add_reference, mask);
                if (r < 0)
                        return r;
        }
 
        if (e >= 0) {
                s = unit_add_dependency(u, e, other, add_reference, mask);
                if (s < 0)
                        return s;
        }
 
        return r > 0 || s > 0;
}
 
static int resolve_template(Unit *u, const char *name, char **buf, const char **ret) {
        int r;
 
        assert(u);
        assert(name);
        assert(buf);
        assert(ret);
 
        if (!unit_name_is_valid(name, UNIT_NAME_TEMPLATE)) {
                *buf = NULL;
                *ret = name;
                return 0;
        }
 
        if (u->instance)
                r = unit_name_replace_instance(name, u->instance, buf);
        else {
                _cleanup_free_ char *i = NULL;
 
                r = unit_name_to_prefix(u->id, &i);
                if (r < 0)
                        return r;
 
                r = unit_name_replace_instance(name, i, buf);
        }
        if (r < 0)
                return r;
 
        *ret = *buf;
        return 0;
}
 
int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, bool add_reference, UnitDependencyMask mask) {
        _cleanup_free_ char *buf = NULL;
        Unit *other;
        int r;
 
        assert(u);
        assert(name);
 
        r = resolve_template(u, name, &buf, &name);
        if (r < 0)
                return r;
 
        if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
                return 0;
 
        r = manager_load_unit(u->manager, name, NULL, NULL, &other);
        if (r < 0)
                return r;
 
        return unit_add_dependency(u, d, other, add_reference, mask);
}
 
int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, bool add_reference, UnitDependencyMask mask) {
        _cleanup_free_ char *buf = NULL;
        Unit *other;
        int r;
 
        assert(u);
        assert(name);
 
        r = resolve_template(u, name, &buf, &name);
        if (r < 0)
                return r;
 
        if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
                return 0;
 
        r = manager_load_unit(u->manager, name, NULL, NULL, &other);
        if (r < 0)
                return r;
 
        return unit_add_two_dependencies(u, d, e, other, add_reference, mask);
}
 
int setenv_unit_path(const char *p) {
        assert(p);
 
        /* This is mostly for debug purposes */
        return RET_NERRNO(setenv("SYSTEMD_UNIT_PATH", p, /* overwrite= */ true));
}
 
char* unit_dbus_path(Unit *u) {
        assert(u);
 
        if (!u->id)
                return NULL;
 
        return unit_dbus_path_from_name(u->id);
}
 
char* unit_dbus_path_invocation_id(Unit *u) {
        assert(u);
 
        if (sd_id128_is_null(u->invocation_id))
                return NULL;
 
        return unit_dbus_path_from_name(u->invocation_id_string);
}
 
int unit_set_invocation_id(Unit *u, sd_id128_t id) {
        int r;
 
        assert(u);
 
        /* Set the invocation ID for this unit. If we cannot, this will not roll back, but reset the whole thing. */
 
        if (sd_id128_equal(u->invocation_id, id))
                return 0;
 
        if (!sd_id128_is_null(u->invocation_id))
                (void) hashmap_remove_value(u->manager->units_by_invocation_id, &u->invocation_id, u);
 
        if (sd_id128_is_null(id)) {
                r = 0;
                goto reset;
        }
 
        r = hashmap_ensure_allocated(&u->manager->units_by_invocation_id, &id128_hash_ops);
        if (r < 0)
                goto reset;
 
        u->invocation_id = id;
        sd_id128_to_string(id, u->invocation_id_string);
 
        r = hashmap_put(u->manager->units_by_invocation_id, &u->invocation_id, u);
        if (r < 0)
                goto reset;
 
        return 0;
 
reset:
        u->invocation_id = SD_ID128_NULL;
        u->invocation_id_string[0] = 0;
        return r;
}
 
int unit_set_slice(Unit *u, Unit *slice) {
        int r;
 
        assert(u);
        assert(slice);
 
        /* Sets the unit slice if it has not been set before. Is extra careful, to only allow this for units
         * that actually have a cgroup context. Also, we don't allow to set this for slices (since the parent
         * slice is derived from the name). Make sure the unit we set is actually a slice. */
 
        if (!UNIT_HAS_CGROUP_CONTEXT(u))
                return -EOPNOTSUPP;
 
        if (u->type == UNIT_SLICE)
                return -EINVAL;
 
        if (unit_active_state(u) != UNIT_INACTIVE)
                return -EBUSY;
 
        if (slice->type != UNIT_SLICE)
                return -EINVAL;
 
        if (unit_has_name(u, SPECIAL_INIT_SCOPE) &&
            !unit_has_name(slice, SPECIAL_ROOT_SLICE))
                return -EPERM;
 
        if (UNIT_GET_SLICE(u) == slice)
                return 0;
 
        /* Disallow slice changes if @u is already bound to cgroups */
        if (UNIT_GET_SLICE(u)) {
                CGroupRuntime *crt = unit_get_cgroup_runtime(u);
                if (crt && crt->cgroup_path)
                        return -EBUSY;
        }
 
        /* Remove any slices assigned prior; we should only have one UNIT_IN_SLICE dependency */
        if (UNIT_GET_SLICE(u))
                unit_remove_dependencies(u, UNIT_DEPENDENCY_SLICE_PROPERTY);
 
        r = unit_add_dependency(u, UNIT_IN_SLICE, slice, true, UNIT_DEPENDENCY_SLICE_PROPERTY);
        if (r < 0)
                return r;
 
        return 1;
}
 
int unit_set_default_slice(Unit *u) {
        const char *slice_name;
        Unit *slice;
        int r;
 
        assert(u);
 
        if (u->manager && FLAGS_SET(u->manager->test_run_flags, MANAGER_TEST_RUN_IGNORE_DEPENDENCIES))
                return 0;
 
        if (UNIT_GET_SLICE(u))
                return 0;
 
        if (u->instance) {
                _cleanup_free_ char *prefix = NULL, *escaped = NULL;
 
                /* Implicitly place all instantiated units in their
                 * own per-template slice */
 
                r = unit_name_to_prefix(u->id, &prefix);
                if (r < 0)
                        return r;
 
                /* The prefix is already escaped, but it might include
                 * "-" which has a special meaning for slice units,
                 * hence escape it here extra. */
                escaped = unit_name_escape(prefix);
                if (!escaped)
                        return -ENOMEM;
 
                if (MANAGER_IS_SYSTEM(u->manager))
                        slice_name = strjoina("system-", escaped, ".slice");
                else
                        slice_name = strjoina("app-", escaped, ".slice");
 
        } else if (unit_is_extrinsic(u))
                /* Keep all extrinsic units (e.g. perpetual units and swap and mount units in user mode) in
                 * the root slice. They don't really belong in one of the subslices. */
                slice_name = SPECIAL_ROOT_SLICE;
 
        else if (MANAGER_IS_SYSTEM(u->manager))
                slice_name = SPECIAL_SYSTEM_SLICE;
        else
                slice_name = SPECIAL_APP_SLICE;
 
        r = manager_load_unit(u->manager, slice_name, NULL, NULL, &slice);
        if (r < 0)
                return r;
 
        return unit_set_slice(u, slice);
}
 
const char* unit_slice_name(Unit *u) {
        Unit *slice;
        assert(u);
 
        slice = UNIT_GET_SLICE(u);
        if (!slice)
                return NULL;
 
        return slice->id;
}
 
int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
        _cleanup_free_ char *t = NULL;
        int r;
 
        assert(u);
        assert(type);
        assert(_found);
 
        r = unit_name_change_suffix(u->id, type, &t);
        if (r < 0)
                return r;
        if (unit_has_name(u, t))
                return -EINVAL;
 
        r = manager_load_unit(u->manager, t, NULL, NULL, _found);
        assert(r < 0 || *_found != u);
        return r;
}
 
static int signal_name_owner_changed_install_handler(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Unit *u = ASSERT_PTR(userdata);
        const sd_bus_error *e;
        int r;
 
        e = sd_bus_message_get_error(message);
        if (!e) {
                log_unit_trace(u, "Successfully installed NameOwnerChanged signal match.");
                return 0;
        }
 
        r = sd_bus_error_get_errno(e);
        log_unit_error_errno(u, r,
                             "Unexpected error response on installing NameOwnerChanged signal match: %s",
                             bus_error_message(e, r));
 
        /* If we failed to install NameOwnerChanged signal, also unref the bus slot of GetNameOwner(). */
        u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
        u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
 
        if (UNIT_VTABLE(u)->bus_name_owner_change)
                UNIT_VTABLE(u)->bus_name_owner_change(u, NULL, /* from_signal= */ true);
 
        return 0;
}
 
static int signal_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        const char *new_owner;
        Unit *u = ASSERT_PTR(userdata);
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "sss", NULL, NULL, &new_owner);
        if (r < 0) {
                bus_log_parse_error(r);
                return 0;
        }
 
        if (UNIT_VTABLE(u)->bus_name_owner_change)
                UNIT_VTABLE(u)->bus_name_owner_change(u, empty_to_null(new_owner), /* from_signal= */ true);
 
        return 0;
}
 
static int get_name_owner_handler(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        const sd_bus_error *e;
        const char *new_owner;
        Unit *u = ASSERT_PTR(userdata);
        int r;
 
        assert(message);
 
        u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
 
        e = sd_bus_message_get_error(message);
        if (e) {
                if (!sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) {
                        r = sd_bus_error_get_errno(e);
                        log_unit_error_errno(u, r,
                                             "Unexpected error response from GetNameOwner(): %s",
                                             bus_error_message(e, r));
                }
 
                new_owner = NULL;
        } else {
                r = sd_bus_message_read(message, "s", &new_owner);
                if (r < 0)
                        return bus_log_parse_error(r);
 
                assert(!isempty(new_owner));
        }
 
        /* Given we're in a GetNameOwner() refresh, which we issue right after (re)connecting to the broker,
         * a NULL owner here may simply mean the owning client is still getting its bearings, so treat it as
         * non-definitive and worth waiting a bit for. */
        if (UNIT_VTABLE(u)->bus_name_owner_change)
                UNIT_VTABLE(u)->bus_name_owner_change(u, new_owner, /* from_signal= */ false);
 
        return 0;
}
 
int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        const char *match;
        usec_t timeout_usec = 0;
        int r;
 
        assert(u);
        assert(bus);
        assert(name);
 
        if (u->match_bus_slot || u->get_name_owner_slot)
                return -EBUSY;
 
        /* NameOwnerChanged and GetNameOwner is used to detect when a service finished starting up. The dbus
         * call timeout shouldn't be earlier than that. If we couldn't get the start timeout, use the default
         * value defined above. */
        if (UNIT_VTABLE(u)->get_timeout_start_usec)
                timeout_usec = UNIT_VTABLE(u)->get_timeout_start_usec(u);
 
        match = strjoina("type='signal',"
                         "sender='org.freedesktop.DBus',"
                         "path='/org/freedesktop/DBus',"
                         "interface='org.freedesktop.DBus',"
                         "member='NameOwnerChanged',"
                         "arg0='", name, "'");
 
        r = bus_add_match_full(
                        bus,
                        &u->match_bus_slot,
                        /* asynchronous= */ true,
                        match,
                        signal_name_owner_changed,
                        signal_name_owner_changed_install_handler,
                        u,
                        timeout_usec);
        if (r < 0)
                return r;
 
        r = sd_bus_message_new_method_call(
                        bus,
                        &m,
                        "org.freedesktop.DBus",
                        "/org/freedesktop/DBus",
                        "org.freedesktop.DBus",
                        "GetNameOwner");
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(m, "s", name);
        if (r < 0)
                return r;
 
        r = sd_bus_call_async(
                        bus,
                        &u->get_name_owner_slot,
                        m,
                        get_name_owner_handler,
                        u,
                        timeout_usec);
        if (r < 0) {
                u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
                return r;
        }
 
        log_unit_debug(u, "Watching D-Bus name '%s'.", name);
        return 0;
}
 
int unit_watch_bus_name(Unit *u, const char *name) {
        int r;
 
        assert(u);
        assert(name);
 
        /* Watch a specific name on the bus. We only support one unit
         * watching each name for now. */
 
        if (u->manager->api_bus) {
                /* If the bus is already available, install the match directly.
                 * Otherwise, just put the name in the list. bus_setup_api() will take care later. */
                r = unit_install_bus_match(u, u->manager->api_bus, name);
                if (r < 0)
                        return log_warning_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name);
        }
 
        r = hashmap_put(u->manager->watch_bus, name, u);
        if (r < 0) {
                u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
                u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
                return log_warning_errno(r, "Failed to put bus name to hashmap: %m");
        }
 
        return 0;
}
 
void unit_unwatch_bus_name(Unit *u, const char *name) {
        assert(u);
        assert(name);
 
        (void) hashmap_remove_value(u->manager->watch_bus, name, u);
        u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
        u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
}
 
int unit_add_node_dependency(Unit *u, const char *what, UnitDependency dep, UnitDependencyMask mask) {
        _cleanup_free_ char *e = NULL;
        Unit *device;
        int r;
 
        assert(u);
 
        /* Adds in links to the device node that this unit is based on */
        if (isempty(what))
                return 0;
 
        if (!is_device_path(what))
                return 0;
 
        /* When device units aren't supported (such as in a container), don't create dependencies on them. */
        if (!unit_type_supported(UNIT_DEVICE))
                return 0;
 
        r = unit_name_from_path(what, ".device", &e);
        if (r < 0)
                return r;
 
        r = manager_load_unit(u->manager, e, NULL, NULL, &device);
        if (r < 0)
                return r;
 
        if (dep == UNIT_REQUIRES && device_shall_be_bound_by(device, u))
                dep = UNIT_BINDS_TO;
 
        return unit_add_two_dependencies(u, UNIT_AFTER,
                                         MANAGER_IS_SYSTEM(u->manager) ? dep : UNIT_WANTS,
                                         device, true, mask);
}
 
int unit_add_blockdev_dependency(Unit *u, const char *what, UnitDependencyMask mask) {
        _cleanup_free_ char *escaped = NULL, *target = NULL;
        int r;
 
        assert(u);
 
        if (isempty(what))
                return 0;
 
        if (!path_startswith(what, "/dev/"))
                return 0;
 
        /* If we don't support devices, then also don't bother with blockdev@.target */
        if (!unit_type_supported(UNIT_DEVICE))
                return 0;
 
        r = unit_name_path_escape(what, &escaped);
        if (r < 0)
                return r;
 
        r = unit_name_build("blockdev", escaped, ".target", &target);
        if (r < 0)
                return r;
 
        return unit_add_dependency_by_name(u, UNIT_AFTER, target, true, mask);
}
 
int unit_coldplug(Unit *u) {
        int r = 0;
 
        assert(u);
 
        /* Make sure we don't enter a loop, when coldplugging recursively. */
        if (u->coldplugged)
                return 0;
 
        u->coldplugged = true;
 
        STRV_FOREACH(i, u->deserialized_refs)
                RET_GATHER(r, bus_unit_track_add_name(u, *i));
 
        u->deserialized_refs = strv_free(u->deserialized_refs);
 
        if (UNIT_VTABLE(u)->coldplug)
                RET_GATHER(r, UNIT_VTABLE(u)->coldplug(u));
 
        if (u->job)
                RET_GATHER(r, job_coldplug(u->job));
        if (u->nop_job)
                RET_GATHER(r, job_coldplug(u->nop_job));
 
        return r;
}
 
void unit_catchup(Unit *u) {
        assert(u);
 
        if (UNIT_VTABLE(u)->catchup)
                UNIT_VTABLE(u)->catchup(u);
 
        unit_cgroup_catchup(u);
}
 
static bool fragment_mtime_newer(const char *path, usec_t mtime, bool path_masked) {
        struct stat st;
 
        if (!path)
                return false;
 
        /* If the source is some virtual kernel file system, then we assume we watch it anyway, and hence pretend we
         * are never out-of-date. */
        if (PATH_STARTSWITH_SET(path, "/proc", "/sys"))
                return false;
 
        if (stat(path, &st) < 0)
                /* What, cannot access this anymore? */
                return true;
 
        if (path_masked)
                /* For masked files check if they are still so */
                return !null_or_empty(&st);
        else
                /* For non-empty files check the mtime */
                return timespec_load(&st.st_mtim) > mtime;
 
        return false;
}
 
bool unit_need_daemon_reload(Unit *u) {
        assert(u);
        assert(u->manager);
 
        if (u->manager->unit_file_state_outdated)
                return true;
 
        /* For unit files, we allow masking… */
        if (fragment_mtime_newer(u->fragment_path, u->fragment_mtime,
                                 u->load_state == UNIT_MASKED))
                return true;
 
        /* Source paths should not be masked… */
        if (fragment_mtime_newer(u->source_path, u->source_mtime, false))
                return true;
 
        if (u->load_state == UNIT_LOADED) {
                _cleanup_strv_free_ char **dropins = NULL;
 
                (void) unit_find_dropin_paths(u, /* use_unit_path_cache= */ false, &dropins);
 
                if (!strv_equal(u->dropin_paths, dropins))
                        return true;
 
                /* … any drop-ins that are masked are simply omitted from the list. */
                STRV_FOREACH(path, u->dropin_paths)
                        if (fragment_mtime_newer(*path, u->dropin_mtime, false))
                                return true;
        }
 
        return false;
}
 
void unit_reset_failed(Unit *u) {
        assert(u);
 
        if (UNIT_VTABLE(u)->reset_failed)
                UNIT_VTABLE(u)->reset_failed(u);
 
        ratelimit_reset(&u->start_ratelimit);
        u->start_limit_hit = false;
 
        (void) unit_set_debug_invocation(u, /* enable= */ false);
}
 
Unit *unit_following(Unit *u) {
        assert(u);
 
        if (UNIT_VTABLE(u)->following)
                return UNIT_VTABLE(u)->following(u);
 
        return NULL;
}
 
bool unit_stop_pending(Unit *u) {
        assert(u);
 
        /* This call does check the current state of the unit. It's
         * hence useful to be called from state change calls of the
         * unit itself, where the state isn't updated yet. This is
         * different from unit_inactive_or_pending() which checks both
         * the current state and for a queued job. */
 
        return unit_has_job_type(u, JOB_STOP);
}
 
bool unit_inactive_or_pending(Unit *u) {
        assert(u);
 
        /* Returns true if the unit is inactive or going down */
 
        if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)))
                return true;
 
        if (unit_stop_pending(u))
                return true;
 
        return false;
}
 
bool unit_active_or_pending(Unit *u) {
        assert(u);
 
        /* Returns true if the unit is active or going up */
 
        if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
                return true;
 
        if (u->job && IN_SET(u->job->type, JOB_START, JOB_RESTART))
                return true;
 
        return false;
}
 
bool unit_will_restart_default(Unit *u) {
        assert(u);
 
        return unit_has_job_type(u, JOB_START);
}
 
bool unit_will_restart(Unit *u) {
        assert(u);
 
        if (!UNIT_VTABLE(u)->will_restart)
                return false;
 
        return UNIT_VTABLE(u)->will_restart(u);
}
 
void unit_notify_cgroup_oom(Unit *u, bool managed_oom) {
        assert(u);
 
        if (UNIT_VTABLE(u)->notify_cgroup_oom)
                UNIT_VTABLE(u)->notify_cgroup_oom(u, managed_oom);
}
 
static int unit_pid_set(Unit *u, Set **pid_set) {