HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/dbus-manager.c
Git blob: 4a6adcb82cb77d2d424a21f5e30e9014607685c6
Lines 2001–3000 of 3,732
if (strv_length(plus) > ENVIRONMENT_ASSIGNMENTS_MAX)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED,
"Too many environment assignments in a single query.");
if (!strv_env_is_valid(plus))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments");
r = bus_verify_set_environment_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = manager_client_environment_modify(m, NULL, plus);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int method_unset_environment(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **minus = NULL;
Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_access_check(message, "reload", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read_strv(message, &minus);
if (r < 0)
return r;
if (strv_length(minus) > ENVIRONMENT_ASSIGNMENTS_MAX)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED,
"Too many environment variable names in a single query.");
if (!strv_env_name_or_assignment_is_valid(minus))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid environment variable names or assignments");
r = bus_verify_set_environment_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = manager_client_environment_modify(m, minus, NULL);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int method_unset_and_set_environment(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **minus = NULL, **plus = NULL;
Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_access_check(message, "reload", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read_strv(message, &minus);
if (r < 0)
return r;
r = sd_bus_message_read_strv(message, &plus);
if (r < 0)
return r;
if (strv_length(plus) > ENVIRONMENT_ASSIGNMENTS_MAX || strv_length(minus) > ENVIRONMENT_ASSIGNMENTS_MAX)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED,
"Too many environment variable names or assignments in a single query.");
if (!strv_env_name_or_assignment_is_valid(minus))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid environment variable names or assignments");
if (!strv_env_is_valid(plus))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid environment assignments");
r = bus_verify_set_environment_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = manager_client_environment_modify(m, minus, plus);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int method_set_exit_code(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
uint8_t code;
int r;
assert(message);
r = mac_selinux_access_check(message, "exit", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read_basic(message, 'y', &code);
if (r < 0)
return r;
m->return_value = code;
return sd_bus_reply_method_return(message, NULL);
}
static int method_lookup_dynamic_user_by_name(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
const char *name;
uid_t uid;
int r;
assert(message);
r = sd_bus_message_read_basic(message, 's', &name);
if (r < 0)
return r;
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED,
"Dynamic users are only supported in the system instance.");
if (!valid_user_group_name(name, VALID_USER_RELAX))
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"User name invalid: %s", name);
r = dynamic_user_lookup_name(m, name, &uid);
if (r == -ESRCH)
return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_DYNAMIC_USER,
"Dynamic user %s does not exist.", name);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, "u", (uint32_t) uid);
}
static int method_lookup_dynamic_user_by_uid(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_free_ char *name = NULL;
Manager *m = ASSERT_PTR(userdata);
uid_t uid;
int r;
assert(message);
assert_cc(sizeof(uid_t) == sizeof(uint32_t));
r = sd_bus_message_read_basic(message, 'u', &uid);
if (r < 0)
return r;
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED,
"Dynamic users are only supported in the system instance.");
if (!uid_is_valid(uid))
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"User ID invalid: " UID_FMT, uid);
r = dynamic_user_lookup_uid(m, uid, &name);
if (r == -ESRCH)
return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_DYNAMIC_USER,
"Dynamic user ID " UID_FMT " does not exist.", uid);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, "s", name);
}
static int method_get_dynamic_users(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
Manager *m = ASSERT_PTR(userdata);
DynamicUser *d;
int r;
assert(message);
assert_cc(sizeof(uid_t) == sizeof(uint32_t));
if (!MANAGER_IS_SYSTEM(m))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED,
"Dynamic users are only supported in the system instance.");
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'a', "(us)");
if (r < 0)
return r;
HASHMAP_FOREACH(d, m->dynamic_users) {
uid_t uid;
r = dynamic_user_current(d, &uid);
if (r == -EAGAIN) /* not realized yet? */
continue;
if (r < 0)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_FAILED,
"Failed to look up a dynamic user.");
r = sd_bus_message_append(reply, "(us)", uid, d->name);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int method_enqueue_marked_jobs(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_access_check(message, "start", reterr_error);
if (r < 0)
return r;
r = bus_verify_manage_units_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
log_info("Queuing reload/restart jobs for marked units%s", glyph(GLYPH_ELLIPSIS));
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'a', "o");
if (r < 0)
return r;
Unit *u;
char *k;
int ret = 0;
HASHMAP_FOREACH_KEY(u, k, m->units) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
/* ignore aliases */
if (u->id != k)
continue;
BusUnitQueueFlags flags;
JobType job;
if (BIT_SET(u->markers, UNIT_MARKER_NEEDS_RESTART)) {
flags = 0;
job = JOB_TRY_RESTART;
} else if (BIT_SET(u->markers, UNIT_MARKER_NEEDS_RELOAD)) {
flags = BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
job = JOB_TRY_RESTART;
} else if (BIT_SET(u->markers, UNIT_MARKER_NEEDS_STOP)) {
flags = 0;
job = JOB_STOP;
} else if (BIT_SET(u->markers, UNIT_MARKER_NEEDS_START)) {
flags = 0;
job = JOB_START;
} else
continue;
r = mac_selinux_unit_access_check(u, message, job_type_to_access_method(job), &error);
if (r >= 0)
r = bus_unit_queue_job_one(message, u,
job, JOB_FAIL, flags,
reply, &error);
if (ERRNO_IS_NEG_RESOURCE(r))
return r;
if (r < 0)
RET_GATHER(ret, log_unit_warning_errno(u, r, "Failed to enqueue marked job: %s",
bus_error_message(&error, r)));
}
if (ret < 0)
return sd_bus_error_set_errnof(reterr_error, ret,
"Failed to enqueue some jobs, see logs for details: %m");
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int list_unit_files_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error, char **states, char **patterns) {
Manager *m = ASSERT_PTR(userdata);
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_hashmap_free_ Hashmap *h = NULL;
int r;
assert(message);
/* Anyone can call this method */
if (strv_length(states) > MANAGER_MAX_STATES_PER_CALL)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED,
"Too many states in a single query.");
if (strv_length(patterns) > MANAGER_MAX_PATTERNS_PER_CALL)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED,
"Too many patterns in a single query.");
r = mac_selinux_access_check(message, "status", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
r = unit_file_get_list(m->runtime_scope, /* root_dir= */ NULL, states, patterns, &h);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'a', "(ss)");
if (r < 0)
return r;
UnitFileList *item;
HASHMAP_FOREACH(item, h) {
r = sd_bus_message_append(reply, "(ss)", item->path, unit_file_state_to_string(item->state));
if (r < 0)
return r;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int method_list_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return list_unit_files_by_patterns(message, userdata, reterr_error, NULL, NULL);
}
static int method_list_unit_files_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **states = NULL;
_cleanup_strv_free_ char **patterns = NULL;
int r;
r = sd_bus_message_read_strv(message, &states);
if (r < 0)
return r;
r = sd_bus_message_read_strv(message, &patterns);
if (r < 0)
return r;
return list_unit_files_by_patterns(message, userdata, reterr_error, states, patterns);
}
static int method_get_unit_file_state(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
const char *name;
UnitFileState state;
int r;
assert(message);
/* Anyone can call this method */
r = mac_selinux_access_check(message, "status", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read(message, "s", &name);
if (r < 0)
return r;
r = unit_file_get_state(m->runtime_scope, NULL, name, &state);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, "s", unit_file_state_to_string(state));
}
static int method_get_default_target(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_free_ char *default_target = NULL;
Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
/* Anyone can call this method */
r = mac_selinux_access_check(message, "status", reterr_error);
if (r < 0)
return r;
r = unit_file_get_default(m->runtime_scope, NULL, &default_target);
if (r == -ERFKILL)
return sd_bus_error_set(reterr_error, BUS_ERROR_UNIT_MASKED, "Default target unit file is masked.");
if (r < 0)
return r;
return sd_bus_reply_method_return(message, "s", default_target);
}
static int send_unit_files_changed(sd_bus *bus, void *userdata) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL;
int r;
assert(bus);
r = sd_bus_message_new_signal(bus, &message,
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"UnitFilesChanged");
if (r < 0)
return r;
return sd_bus_send(bus, message, NULL);
}
static void manager_unit_files_changed(Manager *m, const InstallChange *changes, size_t n_changes) {
int r;
assert(m);
assert(changes || n_changes == 0);
if (!install_changes_have_modification(changes, n_changes))
return;
/* See comments for this variable in manager.h */
m->unit_file_state_outdated = true;
r = bus_foreach_bus(m, NULL, send_unit_files_changed, NULL);
if (r < 0)
log_debug_errno(r, "Failed to send UnitFilesChanged signal, ignoring: %m");
}
static int install_error(
sd_bus_error *reterr_error,
int c,
InstallChange *changes,
size_t n_changes) {
int r;
/* Create an error reply, using the error information from changes[] if possible, and fall back to
* generating an error from error code c. The error message only describes the first error. */
assert(changes || n_changes == 0);
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
FOREACH_ARRAY(i, changes, n_changes) {
_cleanup_free_ char *err_message = NULL;
const char *bus_error;
if (i->type >= 0)
continue;
r = install_change_dump_error(i, &err_message, &bus_error);
if (r == -ENOMEM)
return r;
if (r < 0)
return sd_bus_error_set_errnof(reterr_error, r, "File %s: %m", i->path);
return sd_bus_error_set(reterr_error, bus_error, err_message);
}
return c < 0 ? c : -EINVAL;
}
static int reply_install_changes_and_free(
Manager *m,
sd_bus_message *message,
int carries_install_info,
InstallChange *changes,
size_t n_changes,
sd_bus_error *reterr_error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
bool bad = false, good = false;
int r;
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
if (carries_install_info >= 0) {
r = sd_bus_message_append(reply, "b", carries_install_info);
if (r < 0)
return r;
}
r = sd_bus_message_open_container(reply, 'a', "(sss)");
if (r < 0)
return r;
FOREACH_ARRAY(i, changes, n_changes) {
if (i->type < 0) {
bad = true;
continue;
}
r = sd_bus_message_append(
reply, "(sss)",
install_change_type_to_string(i->type),
i->path,
i->source);
if (r < 0)
return r;
good = true;
}
/* If there was a failed change, and no successful change, then return the first failure as proper
* method call error. */
if (bad && !good)
return install_error(reterr_error, 0, TAKE_PTR(changes), n_changes);
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int method_enable_unit_files_generic(
sd_bus_message *message,
Manager *m,
int (*call)(RuntimeScope scope, UnitFileFlags flags, const char *root_dir, char * const *files, InstallChange **changes, size_t *n_changes),
bool carries_install_info,
sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **l = NULL;
InstallChange *changes = NULL;
size_t n_changes = 0;
UnitFileFlags flags;
int r;
assert(message);
assert(m);
r = sd_bus_message_read_strv(message, &l);
if (r < 0)
return r;
if (sd_bus_message_is_method_call(message, NULL, "EnableUnitFilesWithFlags")) {
uint64_t raw_flags;
r = sd_bus_message_read(message, "t", &raw_flags);
if (r < 0)
return r;
if ((raw_flags & ~_UNIT_FILE_FLAGS_MASK_PUBLIC) != 0)
return -EINVAL;
flags = raw_flags;
} else {
int runtime, force;
r = sd_bus_message_read(message, "bb", &runtime, &force);
if (r < 0)
return r;
flags = unit_file_bools_to_flags(runtime, force);
}
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = call(m->runtime_scope, flags, NULL, l, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes, reterr_error);
}
static int method_enable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_enable_unit_files_generic(message, userdata, unit_file_enable, /* carries_install_info= */ true, reterr_error);
}
static int method_enable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_enable_unit_files_generic(message, userdata, unit_file_enable, /* carries_install_info= */ true, reterr_error);
}
static int method_reenable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_enable_unit_files_generic(message, userdata, unit_file_reenable, /* carries_install_info= */ true, reterr_error);
}
static int method_link_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_enable_unit_files_generic(message, userdata, unit_file_link, /* carries_install_info= */ false, reterr_error);
}
static int unit_file_preset_without_mode(RuntimeScope scope, UnitFileFlags flags, const char *root_dir, char * const *files, InstallChange **changes, size_t *n_changes) {
return unit_file_preset(scope, flags, root_dir, files, UNIT_FILE_PRESET_FULL, changes, n_changes);
}
static int method_preset_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_enable_unit_files_generic(message, userdata, unit_file_preset_without_mode, /* carries_install_info= */ true, reterr_error);
}
static int method_mask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_enable_unit_files_generic(message, userdata, unit_file_mask, /* carries_install_info= */ false, reterr_error);
}
static int method_preset_unit_files_with_mode(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **l = NULL;
InstallChange *changes = NULL;
size_t n_changes = 0;
Manager *m = ASSERT_PTR(userdata);
UnitFilePresetMode preset_mode;
int runtime, force, r;
UnitFileFlags flags;
const char *mode;
assert(message);
r = sd_bus_message_read_strv(message, &l);
if (r < 0)
return r;
r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force);
if (r < 0)
return r;
flags = unit_file_bools_to_flags(runtime, force);
if (isempty(mode))
preset_mode = UNIT_FILE_PRESET_FULL;
else {
preset_mode = unit_file_preset_mode_from_string(mode);
if (preset_mode < 0)
return -EINVAL;
}
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = unit_file_preset(m->runtime_scope, flags, NULL, l, preset_mode, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, r, changes, n_changes, reterr_error);
}
static int method_disable_unit_files_generic(
sd_bus_message *message,
Manager *m,
int (*call)(RuntimeScope scope, UnitFileFlags flags, const char *root_dir, char * const *files, InstallChange **changes, size_t *n_changes),
bool carries_install_info,
sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **l = NULL;
InstallChange *changes = NULL;
UnitFileFlags flags;
size_t n_changes = 0;
int r;
assert(message);
assert(m);
r = sd_bus_message_read_strv(message, &l);
if (r < 0)
return r;
if (sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlags") ||
sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlagsAndInstallInfo")) {
uint64_t raw_flags;
r = sd_bus_message_read(message, "t", &raw_flags);
if (r < 0)
return r;
if ((raw_flags & ~_UNIT_FILE_FLAGS_MASK_PUBLIC) != 0 ||
FLAGS_SET(raw_flags, UNIT_FILE_FORCE))
return -EINVAL;
flags = raw_flags;
} else {
int runtime;
r = sd_bus_message_read(message, "b", &runtime);
if (r < 0)
return r;
flags = unit_file_bools_to_flags(runtime, false);
}
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = call(m->runtime_scope, flags, NULL, l, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes, reterr_error);
}
static int method_disable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info= */ false, reterr_error);
}
static int method_disable_unit_files_with_flags_and_install_info(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info= */ true, reterr_error);
}
static int method_disable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_disable_unit_files_generic(message, userdata, unit_file_disable, /* carries_install_info= */ false, reterr_error);
}
static int method_unmask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_disable_unit_files_generic(message, userdata, unit_file_unmask, /* carries_install_info= */ false, reterr_error);
}
static int method_revert_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **l = NULL;
InstallChange *changes = NULL;
size_t n_changes = 0;
Manager *m = ASSERT_PTR(userdata);
int r;
assert(message);
r = sd_bus_message_read_strv(message, &l);
if (r < 0)
return r;
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = unit_file_revert(m->runtime_scope, NULL, l, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, -1, changes, n_changes, reterr_error);
}
static int method_set_default_target(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
InstallChange *changes = NULL;
size_t n_changes = 0;
Manager *m = ASSERT_PTR(userdata);
const char *name;
int force, r;
assert(message);
r = mac_selinux_access_check(message, "enable", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read(message, "sb", &name, &force);
if (r < 0)
return r;
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = unit_file_set_default(m->runtime_scope, force ? UNIT_FILE_FORCE : 0, NULL, name, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, -1, changes, n_changes, reterr_error);
}
static int method_preset_all_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
InstallChange *changes = NULL;
size_t n_changes = 0;
Manager *m = ASSERT_PTR(userdata);
UnitFilePresetMode preset_mode;
const char *mode;
UnitFileFlags flags;
int force, runtime, r;
assert(message);
r = mac_selinux_access_check(message, "enable", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force);
if (r < 0)
return r;
flags = unit_file_bools_to_flags(runtime, force);
if (isempty(mode))
preset_mode = UNIT_FILE_PRESET_FULL;
else {
preset_mode = unit_file_preset_mode_from_string(mode);
if (preset_mode < 0)
return -EINVAL;
}
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = unit_file_preset_all(m->runtime_scope, flags, NULL, preset_mode, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, -1, changes, n_changes, reterr_error);
}
static int method_add_dependency_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_strv_free_ char **l = NULL;
Manager *m = ASSERT_PTR(userdata);
InstallChange *changes = NULL;
size_t n_changes = 0;
int runtime, force, r;
char *target, *type;
UnitDependency dep;
UnitFileFlags flags;
assert(message);
r = bus_verify_manage_unit_files_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = sd_bus_message_read_strv(message, &l);
if (r < 0)
return r;
r = sd_bus_message_read(message, "ssbb", &target, &type, &runtime, &force);
if (r < 0)
return r;
flags = unit_file_bools_to_flags(runtime, force);
dep = unit_dependency_from_string(type);
if (dep < 0 || !IN_SET(dep, UNIT_WANTS, UNIT_REQUIRES))
return -EINVAL;
r = unit_file_add_dependency(m->runtime_scope, flags, NULL, l, target, dep, &changes, &n_changes);
manager_unit_files_changed(m, changes, n_changes);
if (r < 0)
return install_error(reterr_error, r, changes, n_changes);
return reply_install_changes_and_free(m, message, -1, changes, n_changes, reterr_error);
}
static int method_get_unit_file_links(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
Manager *m = ASSERT_PTR(userdata);
InstallChange *changes = NULL;
size_t n_changes = 0, i;
const char *name;
int runtime, r;
CLEANUP_ARRAY(changes, n_changes, install_changes_free);
r = sd_bus_message_read(message, "sb", &name, &runtime);
if (r < 0)
return r;
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "s");
if (r < 0)
return r;
r = unit_file_disable(m->runtime_scope,
UNIT_FILE_DRY_RUN | (runtime ? UNIT_FILE_RUNTIME : 0),
NULL, STRV_MAKE(name), &changes, &n_changes);
if (r < 0)
return log_error_errno(r, "Failed to get file links for %s: %m", name);
for (i = 0; i < n_changes; i++)
if (changes[i].type == INSTALL_CHANGE_UNLINK) {
r = sd_bus_message_append(reply, "s", changes[i].path);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int method_get_job_waiting(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
uint32_t id;
Job *j;
int r;
assert(message);
r = sd_bus_message_read(message, "u", &id);
if (r < 0)
return r;
j = manager_get_job(m, id);
if (!j)
return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
return bus_job_method_get_waiting_jobs(message, j, reterr_error);
}
static int method_abandon_scope(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
const char *name;
Unit *u;
int r;
assert(message);
r = sd_bus_message_read(message, "s", &name);
if (r < 0)
return r;
r = bus_get_unit_by_name(m, message, name, &u, reterr_error);
if (r < 0)
return r;
if (u->type != UNIT_SCOPE)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"Unit '%s' is not a scope unit, refusing.", name);
return bus_scope_method_abandon(message, u, reterr_error);
}
static int method_set_show_status(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Manager *m = ASSERT_PTR(userdata);
ShowStatus mode = _SHOW_STATUS_INVALID;
const char *t;
int r;
assert(message);
r = mac_selinux_access_check(message, "reload", reterr_error);
if (r < 0)
return r;
r = bus_verify_set_environment_async(m, message, reterr_error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = sd_bus_message_read(message, "s", &t);
if (r < 0)
return r;
if (!isempty(t)) {
mode = show_status_from_string(t);
if (mode < 0)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"Invalid show status '%s'", t);
}
manager_override_show_status(m, mode, "bus");
return sd_bus_reply_method_return(message, NULL);
}
static int method_dump_unit_descriptor_store(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return method_generic_unit_operation(message, userdata, reterr_error, UNIT_SERVICE, bus_service_method_dump_file_descriptor_store, GENERIC_UNIT_VALIDATE_LOADED);
}
static int method_start_aux_scope(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED, "StartAuxiliaryScope() method has been removed.");
}
const sd_bus_vtable bus_manager_vtable[] = {
SD_BUS_VTABLE_START(0),