HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/dbus-unit.c

110,864 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: db9688a7c7f1b455a1a7e1b100b5c3c6e2ab5d0e

Lines 2001–2807 of 2,807

                        reterr_error);
        if (r < 0)
                return r;
 
        if (FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY)) {
                affected = set_new(NULL);
                if (!affected)
                        return -ENOMEM;
        }
 
        r = manager_add_job_full(u->manager, type, u, mode, /* extra_flags= */ 0, affected, reterr_error, &j);
        if (r < 0)
                return r;
 
        r = bus_job_track_sender(j, message);
        if (r < 0)
                return r;
 
        /* Before we send the method reply, force out the announcement JobNew for this job */
        bus_job_send_pending_change_signal(j, true);
 
        job_path = job_dbus_path(j);
        if (!job_path)
                return -ENOMEM;
 
        /* The classic response is just a job object path */
        if (!FLAGS_SET(flags, BUS_UNIT_QUEUE_VERBOSE_REPLY))
                return sd_bus_message_append(reply, "o", job_path);
 
        /* In verbose mode respond with the anchor job plus everything that has been affected */
 
        unit_path = unit_dbus_path(j->unit);
        if (!unit_path)
                return -ENOMEM;
 
        r = sd_bus_message_append(reply, "uosos",
                                  j->id, job_path,
                                  j->unit->id, unit_path,
                                  job_type_to_string(j->type));
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(uosos)");
        if (r < 0)
                return r;
 
        SET_FOREACH(a, affected) {
                if (a->id == j->id)
                        continue;
 
                /* Free paths from previous iteration */
                job_path = mfree(job_path);
                unit_path = mfree(unit_path);
 
                job_path = job_dbus_path(a);
                if (!job_path)
                        return -ENOMEM;
 
                unit_path = unit_dbus_path(a->unit);
                if (!unit_path)
                        return -ENOMEM;
 
                r = sd_bus_message_append(reply, "(uosos)",
                                          a->id, job_path,
                                          a->unit->id, unit_path,
                                          job_type_to_string(a->type));
                if (r < 0)
                        return r;
        }
 
        return sd_bus_message_close_container(reply);
}
 
int bus_unit_queue_job(
                sd_bus_message *message,
                Unit *u,
                JobType type,
                JobMode mode,
                BusUnitQueueFlags flags,
                sd_bus_error *reterr_error) {
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        int r;
 
        assert(message);
        assert(u);
        assert(type >= 0 && type < _JOB_TYPE_MAX);
        assert(mode >= 0 && mode < _JOB_MODE_MAX);
 
        r = mac_selinux_unit_access_check(
                        u, message,
                        job_type_to_access_method(type),
                        reterr_error);
        if (r < 0)
                return r;
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = bus_unit_queue_job_one(message, u, type, mode, flags, reply, reterr_error);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int bus_unit_set_live_property(
                Unit *u,
                const char *name,
                sd_bus_message *message,
                UnitWriteFlags flags,
                sd_bus_error *reterr_error) {
 
        int r;
 
        assert(u);
        assert(name);
        assert(message);
 
        /* Handles setting properties both "live" (i.e. at any time during runtime), and during creation (for
         * transient units that are being created). */
 
        if (streq(name, "Description")) {
                const char *d;
 
                r = sd_bus_message_read(message, "s", &d);
                if (r < 0)
                        return r;
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        r = unit_set_description(u, d);
                        if (r < 0)
                                return r;
 
                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "Description=%s", d);
                }
 
                return 1;
        }
 
        /* A setting that only applies to active units. We don't actually write this to /run, this state is
         * managed internally. "+foo" sets flag foo, "-foo" unsets flag foo, just "foo" resets flags to
         * foo. The last type cannot be mixed with "+" or "-". */
 
        if (streq(name, "Markers")) {
                unsigned settings = 0, mask = 0;
                bool some_plus_minus = false, some_absolute = false;
 
                r = sd_bus_message_enter_container(message, 'a', "s");
                if (r < 0)
                        return r;
 
                for (;;) {
                        const char *word;
 
                        r = sd_bus_message_read(message, "s", &word);
                        if (r < 0)
                                return r;
                        if (r == 0)
                                break;
 
                        r = parse_unit_marker(word, &settings, &mask);
                        if (r < 0)
                                return sd_bus_error_setf(reterr_error, BUS_ERROR_BAD_UNIT_SETTING,
                                                         "Unknown marker \"%s\".", word);
                        if (r > 0)
                                some_plus_minus = true;
                        else
                                some_absolute = true;
                }
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        return r;
 
                if (some_plus_minus && some_absolute)
                        return sd_bus_error_set(reterr_error, BUS_ERROR_BAD_UNIT_SETTING, "Bad marker syntax.");
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        if (some_absolute)
                                mask = UINT_MAX;
                        u->markers = unit_normalize_markers((u->markers & ~mask), settings);
                }
 
                return 1;
        }
 
        return 0;
}
 
static int bus_set_transient_emergency_action(
                Unit *u,
                const char *name,
                EmergencyAction *p,
                sd_bus_message *message,
                UnitWriteFlags flags,
                sd_bus_error *reterr_error) {
 
        const char *s;
        EmergencyAction v;
        int r;
 
        assert(p);
 
        r = sd_bus_message_read(message, "s", &s);
        if (r < 0)
                return r;
 
        r = parse_emergency_action(s, u->manager->runtime_scope, &v);
        if (r < 0)
                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
                                         r == -EOPNOTSUPP ? "%s setting invalid for manager type: %s"
                                                          : "Invalid %s setting: %s",
                                         name, s);
 
        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                *p = v;
                unit_write_settingf(u, flags, name,
                                    "%s=%s", name, s);
        }
 
        return 1;
}
 
static int bus_set_transient_exit_status(
                Unit *u,
                const char *name,
                int *p,
                sd_bus_message *message,
                UnitWriteFlags flags,
                sd_bus_error *reterr_error) {
 
        int32_t k;
        int r;
 
        assert(p);
 
        r = sd_bus_message_read(message, "i", &k);
        if (r < 0)
                return r;
 
        if (k > 255)
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Exit status must be in range 0…255 or negative.");
 
        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                *p = k < 0 ? -1 : k;
 
                if (k < 0)
                        unit_write_settingf(u, flags, name, "%s=", name);
                else
                        unit_write_settingf(u, flags, name, "%s=%i", name, k);
        }
 
        return 1;
}
 
static BUS_DEFINE_SET_TRANSIENT_PARSE(collect_mode, CollectMode, collect_mode_from_string);
static BUS_DEFINE_SET_TRANSIENT_PARSE(job_mode, JobMode, job_mode_from_string);
 
static int bus_set_transient_conditions(
                Unit *u,
                const char *name,
                Condition **list,
                bool is_condition,
                sd_bus_message *message,
                UnitWriteFlags flags,
                sd_bus_error *reterr_error) {
 
        const char *type_name, *param;
        int trigger, negate, r;
        bool empty = true;
 
        assert(list);
 
        r = sd_bus_message_enter_container(message, 'a', "(sbbs)");
        if (r < 0)
                return r;
 
        while ((r = sd_bus_message_read(message, "(sbbs)", &type_name, &trigger, &negate, &param)) > 0) {
                ConditionType t;
 
                t = is_condition ? condition_type_from_string(type_name) : assert_type_from_string(type_name);
                if (t < 0)
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid condition type: %s", type_name);
 
                if (isempty(param))
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Condition parameter in %s is empty", type_name);
 
                if (condition_takes_path(t) && !path_is_absolute(param))
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path in condition %s is not absolute: %s", type_name, param);
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        Condition *c;
 
                        c = condition_new(t, param, trigger, negate);
                        if (!c)
                                return -ENOMEM;
 
                        LIST_PREPEND(conditions, *list, c);
 
                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
                                            "%s=%s%s%s", type_name,
                                            trigger ? "|" : "", negate ? "!" : "", param);
                }
 
                empty = false;
        }
        if (r < 0)
                return r;
 
        r = sd_bus_message_exit_container(message);
        if (r < 0)
                return r;
 
        if (!UNIT_WRITE_FLAGS_NOOP(flags) && empty) {
                *list = condition_free_list(*list);
                unit_write_settingf(u, flags, name, "%sNull=", is_condition ? "Condition" : "Assert");
        }
 
        return 1;
}
 
static int bus_unit_set_transient_property(
                Unit *u,
                const char *name,
                sd_bus_message *message,
                UnitWriteFlags flags,
                sd_bus_error *reterr_error) {
 
        UnitDependency d;
        int r;
 
        assert(u);
        assert(name);
        assert(message);
 
        /* Handles settings when transient units are created. This settings cannot be altered anymore after
         * the unit has been created. */
 
        if (streq(name, "SourcePath"))
                return bus_set_transient_path(u, name, &u->source_path, message, flags, reterr_error);
 
        if (streq(name, "StopWhenUnneeded"))
                return bus_set_transient_bool(u, name, &u->stop_when_unneeded, message, flags, reterr_error);
 
        if (streq(name, "RefuseManualStart"))
                return bus_set_transient_bool(u, name, &u->refuse_manual_start, message, flags, reterr_error);
 
        if (streq(name, "RefuseManualStop"))
                return bus_set_transient_bool(u, name, &u->refuse_manual_stop, message, flags, reterr_error);
 
        if (streq(name, "AllowIsolate"))
                return bus_set_transient_bool(u, name, &u->allow_isolate, message, flags, reterr_error);
 
        if (streq(name, "DefaultDependencies"))
                return bus_set_transient_bool(u, name, &u->default_dependencies, message, flags, reterr_error);
 
        if (streq(name, "SurviveFinalKillSignal"))
                return bus_set_transient_bool(u, name, &u->survive_final_kill_signal, message, flags, reterr_error);
 
        if (streq(name, "OnSuccessJobMode"))
                return bus_set_transient_job_mode(u, name, &u->on_success_job_mode, message, flags, reterr_error);
 
        if (streq(name, "OnFailureJobMode"))
                return bus_set_transient_job_mode(u, name, &u->on_failure_job_mode, message, flags, reterr_error);
 
        if (streq(name, "IgnoreOnIsolate"))
                return bus_set_transient_bool(u, name, &u->ignore_on_isolate, message, flags, reterr_error);
 
        if (streq(name, "JobTimeoutUSec")) {
                r = bus_set_transient_usec_fix_0(u, name, &u->job_timeout, message, flags, reterr_error);
                if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags) && !u->job_running_timeout_set)
                        u->job_running_timeout = u->job_timeout;
 
                return r;
        }
 
        if (streq(name, "JobRunningTimeoutUSec")) {
                r = bus_set_transient_usec_fix_0(u, name, &u->job_running_timeout, message, flags, reterr_error);
                if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags))
                        u->job_running_timeout_set = true;
 
                return r;
        }
 
        if (streq(name, "JobTimeoutAction"))
                return bus_set_transient_emergency_action(u, name, &u->job_timeout_action, message, flags, reterr_error);
 
        if (streq(name, "JobTimeoutRebootArgument"))
                return bus_set_transient_reboot_parameter(u, name, &u->job_timeout_reboot_arg, message, flags, reterr_error);
 
        if (streq(name, "StartLimitIntervalUSec"))
                return bus_set_transient_usec(u, name, &u->start_ratelimit.interval, message, flags, reterr_error);
 
        if (streq(name, "StartLimitBurst"))
                return bus_set_transient_unsigned(u, name, &u->start_ratelimit.burst, message, flags, reterr_error);
 
        if (streq(name, "StartLimitAction"))
                return bus_set_transient_emergency_action(u, name, &u->start_limit_action, message, flags, reterr_error);
 
        if (streq(name, "FailureAction"))
                return bus_set_transient_emergency_action(u, name, &u->failure_action, message, flags, reterr_error);
 
        if (streq(name, "SuccessAction"))
                return bus_set_transient_emergency_action(u, name, &u->success_action, message, flags, reterr_error);
 
        if (streq(name, "FailureActionExitStatus"))
                return bus_set_transient_exit_status(u, name, &u->failure_action_exit_status, message, flags, reterr_error);
 
        if (streq(name, "SuccessActionExitStatus"))
                return bus_set_transient_exit_status(u, name, &u->success_action_exit_status, message, flags, reterr_error);
 
        if (streq(name, "RebootArgument"))
                return bus_set_transient_reboot_parameter(u, name, &u->reboot_arg, message, flags, reterr_error);
 
        if (streq(name, "CollectMode"))
                return bus_set_transient_collect_mode(u, name, &u->collect_mode, message, flags, reterr_error);
 
        if (streq(name, "Conditions"))
                return bus_set_transient_conditions(u, name, &u->conditions, true, message, flags, reterr_error);
 
        if (streq(name, "Asserts"))
                return bus_set_transient_conditions(u, name, &u->asserts, false, message, flags, reterr_error);
 
        if (streq(name, "Documentation")) {
                _cleanup_strv_free_ char **l = NULL;
 
                r = sd_bus_message_read_strv(message, &l);
                if (r < 0)
                        return r;
 
                STRV_FOREACH(p, l)
                        if (!documentation_url_is_valid(*p))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid URL in %s: %s", name, *p);
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        if (strv_isempty(l)) {
                                u->documentation = strv_free(u->documentation);
                                unit_write_settingf(u, flags, name, "%s=", name);
                        } else {
                                r = strv_extend_strv(&u->documentation, l, /* filter_duplicates= */ false);
                                if (r < 0)
                                        return r;
 
                                STRV_FOREACH(p, l)
                                        unit_write_settingf(u, flags, name, "%s=%s", name, *p);
                        }
                }
 
                return 1;
        }
 
        if (streq(name, "Slice")) {
                Unit *slice;
                const char *s;
 
                if (!UNIT_HAS_CGROUP_CONTEXT(u))
                        return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
                if (u->type == UNIT_SLICE)
                        return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
                if (unit_has_name(u, SPECIAL_INIT_SCOPE))
                        return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Cannot set slice for init.scope");
 
                r = sd_bus_message_read(message, "s", &s);
                if (r < 0)
                        return r;
 
                if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name '%s'", s);
 
                /* Note that we do not dispatch the load queue here yet, as we don't want our own transient unit to be
                 * loaded while we are still setting it up. Or in other words, we use manager_load_unit_prepare()
                 * instead of manager_load_unit() on purpose, here. */
                r = manager_load_unit_prepare(u->manager, s, NULL, reterr_error, &slice);
                if (r < 0)
                        return r;
 
                if (slice->type != UNIT_SLICE)
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Unit name '%s' is not a slice", s);
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                        r = unit_set_slice(u, slice);
                        if (r < 0)
                                return r;
 
                        unit_write_settingf(u, flags|UNIT_PRIVATE, name, "Slice=%s", s);
                }
 
                return 1;
        }
 
        if (STR_IN_SET(name, "RequiresMountsFor", "WantsMountsFor")) {
                _cleanup_strv_free_ char **l = NULL;
 
                r = sd_bus_message_read_strv(message, &l);
                if (r < 0)
                        return r;
 
                STRV_FOREACH(p, l) {
                        path_simplify(*p);
 
                        if (!path_is_absolute(*p))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not absolute: %s", name, *p);
 
                        if (!path_is_valid(*p))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s has invalid length: %s", name, *p);
 
                        if (!path_is_normalized(*p))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Path specified in %s is not normalized: %s", name, *p);
 
                        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                                r = unit_add_mounts_for(u, *p, UNIT_DEPENDENCY_FILE, unit_mount_dependency_type_from_string(name));
                                if (r < 0)
                                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Failed to add requested mount \"%s\": %m", *p);
 
                                unit_write_settingf(u, flags, name, "%s=%s", name, *p);
                        }
                }
 
                return 1;
        }
 
        if (streq(name, "AddRef")) {
                int b;
 
                /* Why is this called "AddRef" rather than just "Ref", or "Reference"? There's already a "Ref()" method
                 * on the Unit interface, and it's probably not a good idea to expose a property and a method on the
                 * same interface (well, strictly speaking AddRef isn't exposed as full property, we just read it for
                 * transient units, but still). And "References" and "ReferencedBy" is already used as unit reference
                 * dependency type, hence let's not confuse things with that.
                 *
                 * Note that we don't actually add the reference to the bus track. We do that only after the setup of
                 * the transient unit is complete, so that setting this property multiple times in the same transient
                 * unit creation call doesn't count as individual references. */
 
                r = sd_bus_message_read(message, "b", &b);
                if (r < 0)
                        return r;
 
                if (!UNIT_WRITE_FLAGS_NOOP(flags))
                        u->bus_track_add = b;
 
                return 1;
        }
 
        if (streq(name, "RequiresOverridable"))
                d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
        else if (streq(name, "RequisiteOverridable"))
                d = UNIT_REQUISITE; /* same here */
        else
                d = unit_dependency_from_string(name);
        if (d >= 0) {
                const char *other;
 
                if (!IN_SET(d,
                            UNIT_REQUIRES,
                            UNIT_REQUISITE,
                            UNIT_WANTS,
                            UNIT_BINDS_TO,
                            UNIT_PART_OF,
                            UNIT_UPHOLDS,
                            UNIT_CONFLICTS,
                            UNIT_BEFORE,
                            UNIT_AFTER,
                            UNIT_ON_SUCCESS,
                            UNIT_ON_FAILURE,
                            UNIT_PROPAGATES_RELOAD_TO,
                            UNIT_RELOAD_PROPAGATED_FROM,
                            UNIT_PROPAGATES_STOP_TO,
                            UNIT_STOP_PROPAGATED_FROM,
                            UNIT_JOINS_NAMESPACE_OF))
                    return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Dependency type %s may not be created transiently.", unit_dependency_to_string(d));
 
                r = sd_bus_message_enter_container(message, 'a', "s");
                if (r < 0)
                        return r;
 
                while ((r = sd_bus_message_read(message, "s", &other)) > 0) {
                        if (!unit_name_is_valid(other, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
                                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit name %s", other);
 
                        if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
                                _cleanup_free_ char *label = NULL;
 
                                r = unit_add_dependency_by_name(u, d, other, true, UNIT_DEPENDENCY_FILE);
                                if (r < 0)
                                        return r;
 
                                label = strjoin(name, "-", other);
                                if (!label)
                                        return -ENOMEM;
 
                                unit_write_settingf(u, flags, label, "%s=%s", unit_dependency_to_string(d), other);
                        }
 
                }
                if (r < 0)
                        return r;
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        return r;
 
                return 1;
        }
 
        return 0;
}
 
int bus_unit_set_properties(
                Unit *u,
                sd_bus_message *message,
                UnitWriteFlags flags,
                bool commit,
                sd_bus_error *reterr_error) {
 
        bool for_real = false;
        unsigned n = 0;
        int r;
 
        assert(u);
        assert(message);
 
        /* We iterate through the array twice. First run just checks if all passed data is valid, second run
         * actually applies it. This implements transaction-like behaviour without actually providing full
         * transactions. */
 
        r = sd_bus_message_enter_container(message, 'a', "(sv)");
        if (r < 0)
                goto error;
 
        for (;;) {
                const char *name;
                UnitWriteFlags f;
 
                r = sd_bus_message_enter_container(message, 'r', "sv");
                if (r < 0)
                        goto error;
                if (r == 0) {
                        if (for_real || UNIT_WRITE_FLAGS_NOOP(flags))
                                break;
 
                        /* Reached EOF. Let's try again, and this time for realz... */
                        r = sd_bus_message_rewind(message, false);
                        if (r < 0)
                                goto error;
 
                        for_real = true;
                        continue;
                }
 
                r = sd_bus_message_read(message, "s", &name);
                if (r < 0)
                        goto error;
 
                r = sd_bus_message_enter_container(message, 'v', NULL);
                if (r < 0)
                        goto error;
 
                /* If not for real, then mask out the two target flags */
                f = for_real ? flags : (flags & ~(UNIT_RUNTIME|UNIT_PERSISTENT));
 
                if (UNIT_VTABLE(u)->bus_set_property)
                        r = UNIT_VTABLE(u)->bus_set_property(u, name, message, f, reterr_error);
                else
                        r = 0;
                if (r == 0 && u->transient && u->load_state == UNIT_STUB)
                        r = bus_unit_set_transient_property(u, name, message, f, reterr_error);
                if (r == 0)
                        r = bus_unit_set_live_property(u, name, message, f, reterr_error);
                if (r < 0)
                        goto error;
 
                if (r == 0)
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_PROPERTY_READ_ONLY,
                                                 "Cannot set property %s, or unknown property.", name);
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        goto error;
 
                r = sd_bus_message_exit_container(message);
                if (r < 0)
                        goto error;
 
                n += for_real;
        }
 
        r = sd_bus_message_exit_container(message);
        if (r < 0)
                goto error;
 
        if (commit && n > 0 && UNIT_VTABLE(u)->bus_commit_properties)
                UNIT_VTABLE(u)->bus_commit_properties(u);
 
        return n;
 
 error:
        /* Pretty much any of the calls above can fail if the message is not formed properly
         * or if it has unexpected contents. Fill in a more informative error message here. */
        if (sd_bus_error_is_set(reterr_error))
                return r;
        return sd_bus_error_set_errnof(reterr_error, r,
                                       r == -ENXIO ? "Failed to set unit properties: Unexpected message contents"
                                                   : "Failed to set unit properties: %m");
}
 
int bus_unit_validate_load_state(Unit *u, sd_bus_error *reterr_error) {
        assert(u);
 
        /* Generates a pretty error if a unit isn't properly loaded. */
 
        switch (u->load_state) {
 
        case UNIT_LOADED:
                return 0;
 
        case UNIT_NOT_FOUND:
                return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not found.", u->id);
 
        case UNIT_BAD_SETTING:
                return sd_bus_error_setf(reterr_error, BUS_ERROR_BAD_UNIT_SETTING, "Unit %s has a bad unit file setting.", u->id);
 
        case UNIT_ERROR: /* Only show .load_error in UNIT_ERROR state */
                return sd_bus_error_set_errnof(reterr_error, u->load_error,
                                               "Unit %s failed to load properly, please adjust/correct and reload service manager: %m", u->id);
 
        case UNIT_MASKED:
                return sd_bus_error_setf(reterr_error, BUS_ERROR_UNIT_MASKED, "Unit %s is masked.", u->id);
 
        case UNIT_STUB:
        case UNIT_MERGED:
        default:
                return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT, "Unexpected load state of unit %s", u->id);
        }
}
 
static int bus_unit_track_handler(sd_bus_track *t, void *userdata) {
        Unit *u = ASSERT_PTR(userdata);
 
        assert(t);
 
        u->bus_track = sd_bus_track_unref(u->bus_track); /* make sure we aren't called again */
 
        /* Add the unit to the GC queue, after all if the client left it might be time to GC this unit */
        unit_add_to_gc_queue(u);
 
        return 0;
}
 
static int bus_unit_allocate_bus_track(Unit *u) {
        int r;
 
        assert(u);
 
        if (u->bus_track)
                return 0;
 
        r = sd_bus_track_new(u->manager->api_bus, &u->bus_track, bus_unit_track_handler, u);
        if (r < 0)
                return r;
 
        r = sd_bus_track_set_recursive(u->bus_track, true);
        if (r < 0) {
                u->bus_track = sd_bus_track_unref(u->bus_track);
                return r;
        }
 
        return 0;
}
 
int bus_unit_track_add_name(Unit *u, const char *name) {
        int r;
 
        assert(u);
 
        r = bus_unit_allocate_bus_track(u);
        if (r < 0)
                return r;
 
        return sd_bus_track_add_name(u->bus_track, name);
}
 
int bus_unit_track_add_sender(Unit *u, sd_bus_message *m) {
        int r;
 
        assert(u);
 
        r = bus_unit_allocate_bus_track(u);
        if (r < 0)
                return r;
 
        return sd_bus_track_add_sender(u->bus_track, m);
}
 
int bus_unit_track_remove_sender(Unit *u, sd_bus_message *m) {
        assert(u);
 
        /* If we haven't allocated the bus track object yet, then there's definitely no reference taken yet,
         * return an error */
        if (!u->bus_track)
                return -EUNATCH;
 
        return sd_bus_track_remove_sender(u->bus_track, m);
}