dbus-manager.c revision 5c22925a5713ad6f62d89ef478b82e571e9ba688
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen This file is part of systemd.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen Copyright 2010 Lennart Poettering
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen systemd is free software; you can redistribute it and/or modify it
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen under the terms of the GNU Lesser General Public License as published by
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen the Free Software Foundation; either version 2.1 of the License, or
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen (at your option) any later version.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen systemd is distributed in the hope that it will be useful, but
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen WITHOUT ANY WARRANTY; without even the implied warranty of
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen Lesser General Public License for more details.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen You should have received a copy of the GNU Lesser General Public License
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen along with systemd; If not, see <http://www.gnu.org/licenses/>.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return sd_bus_message_append(reply, "s", PACKAGE_VERSION);
static int property_get_virtualization(
const char *path,
const char *interface,
const char *property,
void *userdata,
v = detect_virtualization();
return sd_bus_message_append(
static int property_get_architecture(
const char *path,
const char *interface,
const char *property,
void *userdata,
static int property_get_tainted(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
if (m->taint_usr)
if (clock_is_localtime() > 0)
if (e != buf)
static int property_get_log_target(
const char *path,
const char *interface,
const char *property,
void *userdata,
static int property_set_log_target(
const char *path,
const char *interface,
const char *property,
void *userdata,
return log_set_target_from_string(t);
static int property_get_log_level(
const char *path,
const char *interface,
const char *property,
void *userdata,
static int property_set_log_level(
const char *path,
const char *interface,
const char *property,
void *userdata,
return log_set_max_level_from_string(t);
static int property_get_n_names(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int property_get_n_failed_units(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int property_get_n_jobs(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int property_get_progress(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int property_get_system_state(
const char *path,
const char *interface,
const char *property,
void *userdata,
assert(m);
static int property_set_runtime_watchdog(
const char *path,
const char *interface,
const char *property,
void *userdata,
return watchdog_set_timeout(t);
const char *name;
Unit *u;
assert(m);
if (!path)
return -ENOMEM;
Unit *u;
assert(m);
if (pid < 0)
if (pid == 0) {
return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pid);
if (!path)
return -ENOMEM;
const char *name;
Unit *u;
assert(m);
if (!path)
return -ENOMEM;
static int method_start_unit_generic(sd_bus_message *message, Manager *m, JobType job_type, bool reload_if_possible, sd_bus_error *error) {
const char *name;
Unit *u;
assert(m);
static int method_reload_or_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_reload_or_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_start_unit_replace(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *old_name;
Unit *u;
assert(m);
const char *name;
Unit *u;
assert(m);
const char *name;
Unit *u;
assert(m);
static int method_set_unit_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
Unit *u;
assert(m);
static int transient_unit_from_message(
Manager *m,
const char *name,
Unit *u;
assert(m);
r = unit_make_transient(u);
*unit = u;
static int transient_aux_units_from_message(
Manager *m,
Unit *u;
assert(m);
if (r < 0 && r != -EEXIST)
if (r != -EEXIST) {
r = unit_load(u);
static int method_start_transient_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
UnitType t;
Unit *u;
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit type %s does not support transient units.", unit_type_to_string(t));
if (mode < 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = unit_load(u);
Job *j;
assert(m);
if (!path)
return -ENOMEM;
Job *j;
assert(m);
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_error *error, char **states) {
Iterator i;
Unit *u;
assert(m);
if (k != u->id)
if (!unit_path)
return -ENOMEM;
if (u->job) {
if (!job_path)
return -ENOMEM;
u->id,
unit_description(u),
static int method_list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Iterator i;
Job *j;
assert(m);
if (!job_path)
return -ENOMEM;
if (!unit_path)
return -ENOMEM;
j->id,
assert(m);
if (!m->subscribed) {
assert(m);
assert(m);
return -ENOMEM;
r = fflush_and_check(f);
const char *name;
int cleanup;
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (!path)
return -ENOMEM;
const char *name;
Unit *u;
assert(m);
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
assert(m);
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Halt is only supported for system managers.");
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "KExec is only supported for system managers.");
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Root switching is only supported by system manager.");
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified switch root path %s does not seem to be an OS tree. os-release file is missing.", root);
return -ENOMEM;
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified init binary %s does not exist.", p);
if (!rt)
return -ENOMEM;
if (!ri) {
return -ENOMEM;
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments");
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_unset_and_set_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) {
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments");
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
assert(m);
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "ExitCode can only be set for user service managers or in containers.");
Hashmap *h;
Iterator i;
assert(m);
return -ENOMEM;
r = unit_file_get_list(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
goto fail;
goto fail;
goto fail;
fail:
static int method_get_unit_file_state(sd_bus_message *message, void *userdata, sd_bus_error *error) {
const char *name;
assert(m);
if (state < 0)
return state;
static int method_get_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) {
assert(m);
r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitFilesChanged");
static int reply_unit_file_changes_and_free(
Manager *m,
int carries_install_info,
unsigned n_changes) {
if (n_changes > 0) {
goto fail;
if (carries_install_info >= 0) {
goto fail;
goto fail;
for (i = 0; i < n_changes; i++) {
goto fail;
goto fail;
fail:
static int method_enable_unit_files_generic(
Manager *m,
const char *verb,
int (*call)(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes),
bool carries_install_info,
unsigned n_changes = 0;
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
return reply_unit_file_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes);
return method_enable_unit_files_generic(message, userdata, "enable", unit_file_enable, true, error);
static int method_reenable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
return method_enable_unit_files_generic(message, userdata, "enable", unit_file_reenable, true, error);
static int unit_file_preset_without_mode(UnitFileScope scope, bool runtime, const char *root_dir, char **files, bool force, UnitFileChange **changes, unsigned *n_changes) {
return unit_file_preset(scope, runtime, root_dir, files, UNIT_FILE_PRESET_FULL, force, changes, n_changes);
return method_enable_unit_files_generic(message, userdata, "enable", unit_file_preset_without_mode, true, error);
return method_enable_unit_files_generic(message, userdata, "disable", unit_file_mask, false, error);
static int method_preset_unit_files_with_mode(sd_bus_message *message, void *userdata, sd_bus_error *error) {
unsigned n_changes = 0;
const char *mode;
assert(m);
if (mm < 0)
return -EINVAL;
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_disable_unit_files_generic(
Manager *m, const
char *verb,
int (*call)(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], UnitFileChange **changes, unsigned *n_changes),
unsigned n_changes = 0;
int r, runtime;
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_disable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
static int method_set_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) {
unsigned n_changes = 0;
const char *name;
int force, r;
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_preset_all_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
unsigned n_changes = 0;
const char *mode;
assert(m);
if (mm < 0)
return -EINVAL;
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
static int method_add_dependency_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
unsigned n_changes = 0;
char *target;
char *type;
assert(m);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
if (dep < 0)
return -EINVAL;
SD_BUS_PROPERTY("Virtualization", "s", property_get_virtualization, 0, SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("FirmwareTimestamp", offsetof(Manager, firmware_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("LoaderTimestamp", offsetof(Manager, loader_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("KernelTimestamp", offsetof(Manager, kernel_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("InitRDTimestamp", offsetof(Manager, initrd_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("UserspaceTimestamp", offsetof(Manager, userspace_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("FinishTimestamp", offsetof(Manager, finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("SecurityStartTimestamp", offsetof(Manager, security_start_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("SecurityFinishTimestamp", offsetof(Manager, security_finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsStartTimestamp", offsetof(Manager, generators_start_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsFinishTimestamp", offsetof(Manager, generators_finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadStartTimestamp", offsetof(Manager, units_load_start_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadFinishTimestamp", offsetof(Manager, units_load_finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("NFailedUnits", "u", property_get_n_failed_units, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("NInstalledJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_installed_jobs), 0),
SD_BUS_PROPERTY("NFailedJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_failed_jobs), 0),
SD_BUS_PROPERTY("ConfirmSpawn", "b", bus_property_get_bool, offsetof(Manager, confirm_spawn), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ShowStatus", "b", bus_property_get_bool, offsetof(Manager, show_status), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("UnitPath", "as", NULL, offsetof(Manager, lookup_paths.unit_path), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("DefaultStandardOutput", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("DefaultStandardError", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogUSec", "t", bus_property_get_usec, property_set_runtime_watchdog, offsetof(Manager, runtime_watchdog), 0),
SD_BUS_WRITABLE_PROPERTY("ShutdownWatchdogUSec", "t", bus_property_get_usec, bus_property_set_usec, offsetof(Manager, shutdown_watchdog), 0),
SD_BUS_METHOD("StartUnitReplace", "sss", "o", method_start_unit_replace, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ReloadOrRestartUnit", "ss", "o", method_reload_or_restart_unit, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ReloadOrTryRestartUnit", "ss", "o", method_reload_or_try_restart_unit, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetUnitProperties", "sba(sv)", NULL, method_set_unit_properties, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("StartTransientUnit", "ssa(sv)a(sa(sv))", "o", method_start_transient_unit, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ListUnitsFiltered", "as", "a(ssssssouso)", method_list_units_filtered, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SwitchRoot", "ss", NULL, method_switch_root, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
SD_BUS_METHOD("UnsetEnvironment", "as", NULL, method_unset_environment, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnsetAndSetEnvironment", "asas", NULL, method_unset_and_set_environment, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("GetUnitFileState", "s", "s", method_get_unit_file_state, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("EnableUnitFiles", "asbb", "ba(sss)", method_enable_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("DisableUnitFiles", "asb", "a(sss)", method_disable_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("ReenableUnitFiles", "asbb", "ba(sss)", method_reenable_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("LinkUnitFiles", "asbb", "a(sss)", method_link_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("PresetUnitFiles", "asbb", "ba(sss)", method_preset_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("PresetUnitFilesWithMode", "assbb", "ba(sss)", method_preset_unit_files_with_mode, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("MaskUnitFiles", "asbb", "a(sss)", method_mask_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("UnmaskUnitFiles", "asb", "a(sss)", method_unmask_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetDefaultTarget", "sb", "a(sss)", method_set_default_target, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("GetDefaultTarget", NULL, "s", method_get_default_target, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("PresetAllUnitFiles", "sbb", "a(sss)", method_preset_all_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("AddDependencyUnitFiles", "asssbb", "a(sss)", method_add_dependency_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartupFinished");
r = sd_bus_message_append(message, "tttttt", times[0], times[1], times[2], times[3], times[4], times[5]);
Manager *m,
assert(m);
r = bus_foreach_bus(
NULL,
r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reloading");
assert(m);
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
NULL);
assert(m);