HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/machine/machined-dbus.c

66,475 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 0653101b1f138fa23519463c8ec1e4dc8825ec04

Lines 1001–1704 of 1,704

                mode = IMAGE_CLEAN_POOL_REMOVE_HIDDEN;
        else
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm);
 
        if (m->runtime_scope != RUNTIME_SCOPE_USER) {
                const char *details[] = {
                        "verb", "clean_pool",
                        "mode", mm,
                        NULL
                };
 
                r = bus_verify_polkit_async(
                                message,
                                "org.freedesktop.machine1.manage-machines",
                                details,
                                &m->polkit_registry,
                                error);
                if (r < 0)
                        return r;
                if (r == 0)
                        return 1; /* Will call us back */
        }
 
        r = image_clean_pool_operation(m, mode, &operation);
        if (r < 0)
                return log_debug_errno(r, "Failed to clean pool of images: %m");
 
        operation_attach_bus_reply(operation, message);
        operation->done = clean_pool_done;
        return 1;
}
 
static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = userdata;
        uint64_t limit;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "t", &limit);
        if (r < 0)
                return r;
        if (!FILE_SIZE_VALID_OR_INFINITY(limit))
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range");
 
        if (m->runtime_scope != RUNTIME_SCOPE_USER) {
                const char *details[] = {
                        "verb", "set_pool_limit",
                        NULL
                };
 
                r = bus_verify_polkit_async(
                                message,
                                "org.freedesktop.machine1.manage-machines",
                                details,
                                &m->polkit_registry,
                                error);
                if (r < 0)
                        return r;
                if (r == 0)
                        return 1; /* Will call us back */
        }
 
        /* Set up the machine directory if necessary */
        r = image_setup_pool(
                        m->runtime_scope,
                        IMAGE_MACHINE,
                        /* use_btrfs_subvol= */ true,
                        /* use_btrfs_quota= */ true);
        if (r < 0)
                return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m");
 
        r = image_set_pool_limit(m->runtime_scope, IMAGE_MACHINE, limit);
        if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
                return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs.");
        if (r < 0)
                return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m");
 
        return sd_bus_reply_method_return(message, NULL);
}
 
static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit);
}
 
static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = userdata;
        const char *name;
        Machine *machine;
        uint32_t uid;
        uid_t converted;
        int r;
 
        r = sd_bus_message_read(message, "su", &name, &uid);
        if (r < 0)
                return r;
 
        if (!uid_is_valid(uid))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
 
        machine = hashmap_get(m->machines, name);
        if (!machine)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
 
        if (machine->class != MACHINE_CONTAINER)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
 
        r = machine_translate_uid(machine, uid, &converted);
        if (r == -ESRCH)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name);
        if (r < 0)
                return r;
 
        return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
}
 
static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *o = NULL;
        Manager *m = userdata;
        Machine *machine;
        uid_t uid, converted;
        int r;
 
        r = sd_bus_message_read(message, "u", &uid);
        if (r < 0)
                return r;
        if (!uid_is_valid(uid))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid);
        if (uid < 0x10000)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid);
 
        r = manager_find_machine_for_uid(m, uid, &machine, &converted);
        if (r < 0)
                return r;
        if (!r)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid);
 
        o = machine_bus_path(machine);
        if (!o)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
}
 
static int method_map_from_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = userdata;
        const char *name;
        Machine *machine;
        gid_t converted;
        uint32_t gid;
        int r;
 
        r = sd_bus_message_read(message, "su", &name, &gid);
        if (r < 0)
                return r;
 
        if (!gid_is_valid(gid))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
 
        machine = hashmap_get(m->machines, name);
        if (!machine)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name);
 
        if (machine->class != MACHINE_CONTAINER)
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines.");
 
        r = machine_translate_gid(machine, gid, &converted);
        if (r == -ESRCH)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching group mappings.", name);
        if (r < 0)
                return r;
 
        return sd_bus_reply_method_return(message, "u", (uint32_t) converted);
}
 
static int method_map_to_machine_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *o = NULL;
        Manager *m = userdata;
        Machine *machine;
        gid_t gid, converted;
        int r;
 
        r = sd_bus_message_read(message, "u", &gid);
        if (r < 0)
                return r;
        if (!gid_is_valid(gid))
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid);
        if (gid < 0x10000)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid);
 
        r = manager_find_machine_for_gid(m, gid, &machine, &converted);
        if (r < 0)
                return r;
        if (!r)
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid);
 
        o = machine_bus_path(machine);
        if (!o)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted);
}
 
static const sd_bus_vtable manager_vtable[] = {
        SD_BUS_VTABLE_START(0),
 
        SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0),
        SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0),
        SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0),
 
        SD_BUS_METHOD_WITH_ARGS("GetMachine",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("o", machine),
                                method_get_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetImage",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("o", image),
                                method_get_image,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetMachineByPID",
                                SD_BUS_ARGS("u", pid),
                                SD_BUS_RESULT("o", machine),
                                method_get_machine_by_pid,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("ListMachines",
                                SD_BUS_NO_ARGS,
                                SD_BUS_RESULT("a(ssso)", machines),
                                method_list_machines,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("ListImages",
                                SD_BUS_NO_ARGS,
                                SD_BUS_RESULT("a(ssbttto)", images),
                                method_list_images,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CreateMachine",
                                SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "a(sv)", scope_properties),
                                SD_BUS_RESULT("o", path),
                                method_create_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CreateMachineWithNetwork",
                                SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices, "a(sv)", scope_properties),
                                SD_BUS_RESULT("o", path),
                                method_create_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CreateMachineEx",
                                SD_BUS_ARGS("s", name, "a(sv)", properties, "a(sv)", scope_properties),
                                SD_BUS_RESULT("o", path),
                                method_create_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("RegisterMachine",
                                SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory),
                                SD_BUS_RESULT("o", path),
                                method_register_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("RegisterMachineWithNetwork",
                                SD_BUS_ARGS("s", name, "ay", id, "s", service, "s", class, "u", leader, "s", root_directory, "ai", ifindices),
                                SD_BUS_RESULT("o", path),
                                method_register_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("RegisterMachineEx",
                                SD_BUS_ARGS("s", name, "a(sv)", properties),
                                SD_BUS_RESULT("o", path),
                                method_register_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("UnregisterMachine",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_NO_RESULT,
                                method_unregister_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("TerminateMachine",
                                SD_BUS_ARGS("s", id),
                                SD_BUS_NO_RESULT,
                                method_terminate_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("KillMachine",
                                SD_BUS_ARGS("s", name, "s", whom, "i", signal),
                                SD_BUS_NO_RESULT,
                                method_kill_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetMachineAddresses",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("a(iay)", addresses),
                                method_get_machine_addresses,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetMachineSSHInfo",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("s", ssh_address, "s", ssh_private_key_path),
                                method_get_machine_ssh_info,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetMachineOSRelease",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("a{ss}", fields),
                                method_get_machine_os_release,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("OpenMachinePTY",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("h", pty, "s", pty_path),
                                method_open_machine_pty,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("OpenMachineLogin",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("h", pty, "s", pty_path),
                                method_open_machine_login,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("OpenMachineShell",
                                SD_BUS_ARGS("s", name, "s", user, "s", path, "as", args, "as", environment),
                                SD_BUS_RESULT("h", pty, "s", pty_path),
                                method_open_machine_shell,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("BindMountMachine",
                                SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir),
                                SD_BUS_NO_RESULT,
                                method_bind_mount_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CopyFromMachine",
                                SD_BUS_ARGS("s", name, "s", source, "s", destination),
                                SD_BUS_NO_RESULT,
                                method_copy_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CopyToMachine",
                                SD_BUS_ARGS("s", name, "s", source, "s", destination),
                                SD_BUS_NO_RESULT,
                                method_copy_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags",
                                SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
                                SD_BUS_NO_RESULT,
                                method_copy_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags",
                                SD_BUS_ARGS("s", name, "s", source, "s", destination, "t", flags),
                                SD_BUS_NO_RESULT,
                                method_copy_machine,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("h", fd),
                                method_open_machine_root_directory,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetMachineUIDShift",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("u", shift),
                                method_get_machine_uid_shift,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("RemoveImage",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_NO_RESULT,
                                method_remove_image,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("RenameImage",
                                SD_BUS_ARGS("s", name, "s", new_name),
                                SD_BUS_NO_RESULT,
                                method_rename_image,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CloneImage",
                                SD_BUS_ARGS("s", name, "s", new_name, "b", read_only),
                                SD_BUS_NO_RESULT,
                                method_clone_image,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly",
                                SD_BUS_ARGS("s", name, "b", read_only),
                                SD_BUS_NO_RESULT,
                                method_mark_image_read_only,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetImageHostname",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("s", hostname),
                                method_get_image_hostname,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetImageMachineID",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("ay", id),
                                method_get_image_machine_id,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetImageMachineInfo",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("a{ss}", machine_info),
                                method_get_image_machine_info,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease",
                                SD_BUS_ARGS("s", name),
                                SD_BUS_RESULT("a{ss}", os_release),
                                method_get_image_os_release,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("SetPoolLimit",
                                SD_BUS_ARGS("t", size),
                                SD_BUS_NO_RESULT,
                                method_set_pool_limit,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("SetImageLimit",
                                SD_BUS_ARGS("s", name, "t", size),
                                SD_BUS_NO_RESULT,
                                method_set_image_limit,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("CleanPool",
                                SD_BUS_ARGS("s", mode),
                                SD_BUS_RESULT("a(st)",images),
                                method_clean_pool,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("MapFromMachineUser",
                                SD_BUS_ARGS("s", name, "u", uid_inner),
                                SD_BUS_RESULT("u", uid_outer),
                                method_map_from_machine_user,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("MapToMachineUser",
                                SD_BUS_ARGS("u", uid_outer),
                                SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", uid_inner),
                                method_map_to_machine_user,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("MapFromMachineGroup",
                                SD_BUS_ARGS("s", name, "u", gid_inner),
                                SD_BUS_RESULT("u", gid_outer),
                                method_map_from_machine_group,
                                SD_BUS_VTABLE_UNPRIVILEGED),
        SD_BUS_METHOD_WITH_ARGS("MapToMachineGroup",
                                SD_BUS_ARGS("u", gid_outer),
                                SD_BUS_RESULT("s", machine_name, "o", machine_path, "u", gid_inner),
                                method_map_to_machine_group,
                                SD_BUS_VTABLE_UNPRIVILEGED),
 
        SD_BUS_SIGNAL_WITH_ARGS("MachineNew",
                                SD_BUS_ARGS("s", machine, "o", path),
                                0),
        SD_BUS_SIGNAL_WITH_ARGS("MachineRemoved",
                                SD_BUS_ARGS("s", machine, "o", path),
                                0),
 
        SD_BUS_VTABLE_END
};
 
const BusObjectImplementation manager_object = {
        "/org/freedesktop/machine1",
        "org.freedesktop.machine1.Manager",
        .vtables = BUS_VTABLES(manager_vtable),
        .children = BUS_IMPLEMENTATIONS(&machine_object,
                                        &image_object),
};
 
int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        const char *path, *result, *unit;
        Manager *m = ASSERT_PTR(userdata);
        Machine *machine;
        uint32_t id;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
        if (r < 0) {
                bus_log_parse_error(r);
                return 0;
        }
 
        machine = hashmap_get(m->machines_by_unit, unit);
        if (!machine)
                return 0;
 
        if (streq_ptr(path, machine->scope_job)) {
                machine->scope_job = mfree(machine->scope_job);
 
                if (machine->started) {
                        if (streq(result, "done"))
                                machine_send_create_reply(machine, NULL);
                        else {
                                _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
 
                                sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
 
                                machine_send_create_reply(machine, &e);
                        }
                }
 
                machine_save(machine);
        }
 
        machine_add_to_gc_queue(machine);
        return 0;
}
 
int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        _cleanup_free_ char *unit = NULL;
        const char *path;
        Manager *m = ASSERT_PTR(userdata);
        Machine *machine;
        int r;
 
        assert(message);
 
        path = sd_bus_message_get_path(message);
        if (!path)
                return 0;
 
        r = unit_name_from_dbus_path(path, &unit);
        if (r == -EINVAL) /* not for a unit */
                return 0;
        if (r < 0) {
                log_oom();
                return 0;
        }
 
        machine = hashmap_get(m->machines_by_unit, unit);
        if (!machine)
                return 0;
 
        machine_add_to_gc_queue(machine);
        return 0;
}
 
int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        const char *path, *unit;
        Manager *m = ASSERT_PTR(userdata);
        Machine *machine;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "so", &unit, &path);
        if (r < 0) {
                bus_log_parse_error(r);
                return 0;
        }
 
        machine = hashmap_get(m->machines_by_unit, unit);
        if (!machine)
                return 0;
 
        machine_add_to_gc_queue(machine);
        return 0;
}
 
int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
        Manager *m = ASSERT_PTR(userdata);
        Machine *machine;
        int b, r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "b", &b);
        if (r < 0) {
                bus_log_parse_error(r);
                return 0;
        }
        if (b)
                return 0;
 
        /* systemd finished reloading, let's recheck all our machines */
        log_debug("System manager has been reloaded, rechecking machines...");
 
        HASHMAP_FOREACH(machine, m->machines)
                machine_add_to_gc_queue(machine);
 
        return 0;
}
 
int manager_unref_unit(
                Manager *m,
                const char *unit,
                sd_bus_error *error) {
 
        assert(m);
        assert(unit);
 
        return bus_call_method(m->api_bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit);
}
 
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        int r;
 
        assert(manager);
        assert(unit);
 
        r = bus_call_method(manager->api_bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail");
        if (r < 0) {
                if (sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
                                                  BUS_ERROR_LOAD_FAILED)) {
 
                        if (job)
                                *job = NULL;
 
                        sd_bus_error_free(error);
                        return 0;
                }
 
                return r;
        }
 
        if (job) {
                const char *j;
                char *copy;
 
                r = sd_bus_message_read(reply, "o", &j);
                if (r < 0)
                        return r;
 
                copy = strdup(j);
                if (!copy)
                        return -ENOMEM;
 
                *job = copy;
        }
 
        return 1;
}
 
int manager_kill_unit(Manager *manager, const char *unit, const char *subgroup, int signo, sd_bus_error *reterr_error) {
        assert(manager);
        assert(unit);
 
        if (empty_or_root(subgroup))
                return bus_call_method(manager->api_bus, bus_systemd_mgr, "KillUnit", reterr_error, NULL, "ssi", unit, "all", signo);
 
        return bus_call_method(manager->api_bus, bus_systemd_mgr, "KillUnitSubgroup", reterr_error, NULL, "sssi", unit, "cgroup", subgroup, signo);
}
 
int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *reterr_error) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_free_ char *path = NULL;
        const char *state;
        int r;
 
        assert(manager);
        assert(unit);
 
        r = sd_bus_is_ready(manager->api_bus);
        if (r < 0)
                return r;
        if (r == 0)
                return -ENOTCONN;
 
        path = unit_dbus_path_from_name(unit);
        if (!path)
                return -ENOMEM;
 
        r = sd_bus_get_property(
                        manager->api_bus,
                        "org.freedesktop.systemd1",
                        path,
                        "org.freedesktop.systemd1.Unit",
                        "ActiveState",
                        &error,
                        &reply,
                        "s");
        if (r < 0) {
                if (bus_error_is_connection(&error))
                        return true;
 
                if (sd_bus_error_has_names(&error, BUS_ERROR_NO_SUCH_UNIT,
                                                   BUS_ERROR_LOAD_FAILED))
                        return false;
 
                sd_bus_error_move(reterr_error, &error);
                return r;
        }
 
        r = sd_bus_message_read(reply, "s", &state);
        if (r < 0)
                return -EINVAL;
 
        return !STR_IN_SET(state, "inactive", "failed");
}
 
int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *reterr_error) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        int r;
 
        assert(manager);
        assert(path);
 
        r = sd_bus_is_ready(manager->api_bus);
        if (r < 0)
                return r;
        if (r == 0)
                return -ENOTCONN;
 
        r = sd_bus_get_property(
                        manager->api_bus,
                        "org.freedesktop.systemd1",
                        path,
                        "org.freedesktop.systemd1.Job",
                        "State",
                        &error,
                        &reply,
                        "s");
        if (r < 0) {
                if (bus_error_is_connection(&error))
                        return true;
 
                if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
                        return false;
 
                sd_bus_error_move(reterr_error, &error);
                return r;
        }
 
        /* We don't actually care about the state really. The fact
         * that we could read the job state is enough for us */
 
        return true;
}