#include <errno.h>
#include <unistd.h>
-#include "dbus.h"
#include "log.h"
-#include "dbus-manager.h"
#include "strv.h"
-#include "bus-errors.h"
#include "build.h"
-#include "dbus-common.h"
#include "install.h"
#include "selinux-access.h"
#include "watchdog.h"
-#include "hwclock.h"
+#include "clock-util.h"
#include "path-util.h"
-#include "dbus-unit.h"
#include "virt.h"
+#include "architecture.h"
#include "env-util.h"
+#include "dbus.h"
+#include "dbus-job.h"
+#include "dbus-manager.h"
+#include "dbus-unit.h"
+#include "dbus-snapshot.h"
+#include "dbus-execute.h"
+#include "bus-common-errors.h"
+
+static int property_get_version(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ assert(bus);
+ assert(reply);
+
+ return sd_bus_message_append(reply, "s", PACKAGE_VERSION);
+}
-#define BUS_MANAGER_INTERFACE_BEGIN \
- " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
-
-#define BUS_MANAGER_INTERFACE_METHODS \
- " <method name=\"GetUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"GetUnitByPID\">\n" \
- " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
- " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"LoadUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"StartUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"StartUnitReplace\">\n" \
- " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"StopUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"ReloadUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"RestartUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"TryRestartUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"ReloadOrRestartUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"ReloadOrTryRestartUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"KillUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"ResetFailedUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"GetJob\">\n" \
- " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"CancelJob\">\n" \
- " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"ClearJobs\"/>\n" \
- " <method name=\"ResetFailed\"/>\n" \
- " <method name=\"ListUnits\">\n" \
- " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"ListJobs\">\n" \
- " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"Subscribe\"/>\n" \
- " <method name=\"Unsubscribe\"/>\n" \
- " <method name=\"Dump\">\n" \
- " <arg name=\"dump\" type=\"s\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"CreateSnapshot\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"RemoveSnapshot\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"Reload\"/>\n" \
- " <method name=\"Reexecute\"/>\n" \
- " <method name=\"Exit\"/>\n" \
- " <method name=\"Reboot\"/>\n" \
- " <method name=\"PowerOff\"/>\n" \
- " <method name=\"Halt\"/>\n" \
- " <method name=\"KExec\"/>\n" \
- " <method name=\"SwitchRoot\">\n" \
- " <arg name=\"new_root\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"init\" type=\"s\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"SetEnvironment\">\n" \
- " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"UnsetEnvironment\">\n" \
- " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"UnsetAndSetEnvironment\">\n" \
- " <arg name=\"unset\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"set\" type=\"as\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"ListUnitFiles\">\n" \
- " <arg name=\"files\" type=\"a(ss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"GetUnitFileState\">\n" \
- " <arg name=\"file\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"state\" type=\"s\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"EnableUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"carries_install_info\" type=\"b\" direction=\"out\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"DisableUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"ReenableUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"carries_install_info\" type=\"b\" direction=\"out\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"LinkUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"PresetUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"carries_install_info\" type=\"b\" direction=\"out\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"MaskUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"UnmaskUnitFiles\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"SetDefaultTarget\">\n" \
- " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
- " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"GetDefaultTarget\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"out\"/>\n" \
- " </method>\n" \
- " <method name=\"SetUnitProperties\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
- " <arg name=\"properties\" type=\"a(sv)\" direction=\"in\"/>\n" \
- " </method>\n" \
- " <method name=\"StartTransientUnit\">\n" \
- " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
- " <arg name=\"properties\" type=\"a(sv)\" direction=\"in\"/>\n" \
- " <arg name=\"aux\" type=\"a(sa(sv))\" direction=\"in\"/>\n" \
- " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
- " </method>\n"
-
-#define BUS_MANAGER_INTERFACE_SIGNALS \
- " <signal name=\"UnitNew\">\n" \
- " <arg name=\"id\" type=\"s\"/>\n" \
- " <arg name=\"unit\" type=\"o\"/>\n" \
- " </signal>\n" \
- " <signal name=\"UnitRemoved\">\n" \
- " <arg name=\"id\" type=\"s\"/>\n" \
- " <arg name=\"unit\" type=\"o\"/>\n" \
- " </signal>\n" \
- " <signal name=\"JobNew\">\n" \
- " <arg name=\"id\" type=\"u\"/>\n" \
- " <arg name=\"job\" type=\"o\"/>\n" \
- " <arg name=\"unit\" type=\"s\"/>\n" \
- " </signal>\n" \
- " <signal name=\"JobRemoved\">\n" \
- " <arg name=\"id\" type=\"u\"/>\n" \
- " <arg name=\"job\" type=\"o\"/>\n" \
- " <arg name=\"unit\" type=\"s\"/>\n" \
- " <arg name=\"result\" type=\"s\"/>\n" \
- " </signal>" \
- " <signal name=\"StartupFinished\">\n" \
- " <arg name=\"firmware\" type=\"t\"/>\n" \
- " <arg name=\"loader\" type=\"t\"/>\n" \
- " <arg name=\"kernel\" type=\"t\"/>\n" \
- " <arg name=\"initrd\" type=\"t\"/>\n" \
- " <arg name=\"userspace\" type=\"t\"/>\n" \
- " <arg name=\"total\" type=\"t\"/>\n" \
- " </signal>" \
- " <signal name=\"UnitFilesChanged\"/>\n" \
- " <signal name=\"Reloading\">\n" \
- " <arg name=\"active\" type=\"b\"/>\n" \
- " </signal>"
-
-#define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
- " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
- " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
- " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
- " <property name=\"FirmwareTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"FirmwareTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"LoaderTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"LoaderTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"KernelTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"KernelTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"UserspaceTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"UserspaceTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"GeneratorsStartTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"GeneratorsStartTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"GeneratorsFinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"GeneratorsFinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"UnitsLoadStartTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"UnitsLoadStartTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"UnitsLoadFinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"UnitsLoadFinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
- " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
- " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
- " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
- " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
- " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
- " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
- " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
- " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
- " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
- " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
- " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
- " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
- " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n" \
- " <property name=\"RuntimeWatchdogUSec\" type=\"t\" access=\"readwrite\"/>\n" \
- " <property name=\"ShutdownWatchdogUSec\" type=\"t\" access=\"readwrite\"/>\n" \
- " <property name=\"Virtualization\" type=\"s\" access=\"read\"/>\n"
-
-#define BUS_MANAGER_INTERFACE_END \
- " </interface>\n"
-
-#define BUS_MANAGER_INTERFACE \
- BUS_MANAGER_INTERFACE_BEGIN \
- BUS_MANAGER_INTERFACE_METHODS \
- BUS_MANAGER_INTERFACE_SIGNALS \
- BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
- BUS_MANAGER_INTERFACE_END
-
-#define INTROSPECTION_BEGIN \
- DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
- "<node>\n" \
- BUS_MANAGER_INTERFACE \
- BUS_PROPERTIES_INTERFACE \
- BUS_PEER_INTERFACE \
- BUS_INTROSPECTABLE_INTERFACE
-
-#define INTROSPECTION_END \
- "</node>\n"
-
-#define INTERFACES_LIST \
- BUS_GENERIC_INTERFACES_LIST \
- "org.freedesktop.systemd1.Manager\0"
-
-const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
-
-static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
-
-static int bus_manager_append_tainted(DBusMessageIter *i, const char *property, void *data) {
- const char *t;
- Manager *m = data;
- char buf[LINE_MAX] = "", *e = buf, *p = NULL;
+static int property_get_features(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ assert(bus);
+ assert(reply);
+
+ return sd_bus_message_append(reply, "s", SYSTEMD_FEATURES);
+}
+
+static int property_get_virtualization(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ const char *id = NULL;
+
+ assert(bus);
+ assert(reply);
+
+ detect_virtualization(&id);
- assert(i);
- assert(property);
+ return sd_bus_message_append(reply, "s", id);
+}
+
+static int property_get_architecture(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ assert(bus);
+ assert(reply);
+
+ return sd_bus_message_append(reply, "s", architecture_to_string(uname_architecture()));
+}
+
+static int property_get_tainted(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ char buf[sizeof("split-usr:mtab-not-symlink:cgroups-missing:local-hwclock:")] = "", *e = buf;
+ _cleanup_free_ char *p = NULL;
+ Manager *m = userdata;
+
+ assert(bus);
+ assert(reply);
assert(m);
if (m->taint_usr)
if (readlink_malloc("/etc/mtab", &p) < 0)
e = stpcpy(e, "mtab-not-symlink:");
- else
- free(p);
if (access("/proc/cgroups", F_OK) < 0)
e = stpcpy(e, "cgroups-missing:");
- if (hwclock_is_localtime() > 0)
+ if (clock_is_localtime() > 0)
e = stpcpy(e, "local-hwclock:");
/* remove the last ':' */
if (e != buf)
e[-1] = 0;
- t = buf;
-
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
- return -ENOMEM;
-
- return 0;
+ return sd_bus_message_append(reply, "s", buf);
}
-static int bus_manager_append_log_target(DBusMessageIter *i, const char *property, void *data) {
- const char *t;
-
- assert(i);
- assert(property);
-
- t = log_target_to_string(log_get_target());
+static int property_get_log_target(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
- return -ENOMEM;
+ assert(bus);
+ assert(reply);
- return 0;
+ return sd_bus_message_append(reply, "s", log_target_to_string(log_get_target()));
}
-static int bus_manager_set_log_target(DBusMessageIter *i, const char *property, void *data) {
+static int property_set_log_target(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *value,
+ void *userdata,
+ sd_bus_error *error) {
+
const char *t;
+ int r;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(value);
- dbus_message_iter_get_basic(i, &t);
+ r = sd_bus_message_read(value, "s", &t);
+ if (r < 0)
+ return r;
return log_set_target_from_string(t);
}
-static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) {
+static int property_get_log_level(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
_cleanup_free_ char *t = NULL;
int r;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(reply);
r = log_level_to_string_alloc(log_get_max_level(), &t);
if (r < 0)
return r;
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
- r = -ENOMEM;
-
- return r;
+ return sd_bus_message_append(reply, "s", t);
}
-static int bus_manager_set_log_level(DBusMessageIter *i, const char *property, void *data) {
+static int property_set_log_level(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *value,
+ void *userdata,
+ sd_bus_error *error) {
+
const char *t;
+ int r;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(value);
- dbus_message_iter_get_basic(i, &t);
+ r = sd_bus_message_read(value, "s", &t);
+ if (r < 0)
+ return r;
return log_set_max_level_from_string(t);
}
-static int bus_manager_append_n_names(DBusMessageIter *i, const char *property, void *data) {
- Manager *m = data;
- uint32_t u;
-
- assert(i);
- assert(property);
- assert(m);
+static int property_get_n_names(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
- u = hashmap_size(m->units);
+ Manager *m = userdata;
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
- return -ENOMEM;
+ assert(bus);
+ assert(reply);
+ assert(m);
- return 0;
+ return sd_bus_message_append(reply, "u", (uint32_t) hashmap_size(m->units));
}
-static int bus_manager_append_n_jobs(DBusMessageIter *i, const char *property, void *data) {
- Manager *m = data;
- uint32_t u;
+static int property_get_n_failed_units(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
- assert(i);
- assert(property);
+ Manager *m = userdata;
+
+ assert(bus);
+ assert(reply);
assert(m);
- u = hashmap_size(m->jobs);
+ return sd_bus_message_append(reply, "u", (uint32_t) set_size(m->failed_units));
+}
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
- return -ENOMEM;
+static int property_get_n_jobs(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
- return 0;
+ Manager *m = userdata;
+
+ assert(bus);
+ assert(reply);
+ assert(m);
+
+ return sd_bus_message_append(reply, "u", (uint32_t) hashmap_size(m->jobs));
}
-static int bus_manager_append_progress(DBusMessageIter *i, const char *property, void *data) {
+static int property_get_progress(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ Manager *m = userdata;
double d;
- Manager *m = data;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(reply);
assert(m);
if (dual_timestamp_is_set(&m->finish_timestamp))
else
d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
- return -ENOMEM;
-
- return 0;
+ return sd_bus_message_append(reply, "d", d);
}
-static int bus_manager_append_virt(DBusMessageIter *i, const char *property, void *data) {
- Manager *m = data;
- const char *id = "";
+static int property_get_system_state(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ Manager *m = userdata;
- assert(i);
- assert(property);
+ assert(bus);
+ assert(reply);
assert(m);
- detect_virtualization(&id);
+ return sd_bus_message_append(reply, "s", manager_state_to_string(manager_state(m)));
+}
- if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &id))
- return -ENOMEM;
+static int property_set_runtime_watchdog(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *value,
+ void *userdata,
+ sd_bus_error *error) {
- return 0;
+ usec_t *t = userdata;
+ int r;
+
+ assert(bus);
+ assert(value);
+
+ assert_cc(sizeof(usec_t) == sizeof(uint64_t));
+
+ r = sd_bus_message_read(value, "t", t);
+ if (r < 0)
+ return r;
+
+ return watchdog_set_timeout(t);
}
-static DBusMessage *message_from_file_changes(
- DBusMessage *m,
- UnitFileChange *changes,
- unsigned n_changes,
- int carries_install_info) {
+static int method_get_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *path = NULL;
+ Manager *m = userdata;
+ const char *name;
+ Unit *u;
+ int r;
- DBusMessageIter iter, sub, sub2;
- DBusMessage *reply;
- unsigned i;
+ assert(bus);
+ assert(message);
+ assert(m);
- reply = dbus_message_new_method_return(m);
- if (!reply)
- return NULL;
+ /* Anyone can call this method */
- dbus_message_iter_init_append(reply, &iter);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- if (carries_install_info >= 0) {
- dbus_bool_t b;
+ if (isempty(name)) {
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+ pid_t pid;
- b = !!carries_install_info;
- if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b))
- goto oom;
- }
+ r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sss)", &sub))
- goto oom;
+ r = sd_bus_creds_get_pid(creds, &pid);
+ if (r < 0)
+ return r;
- for (i = 0; i < n_changes; i++) {
- const char *type, *path, *source;
-
- type = unit_file_change_type_to_string(changes[i].type);
- path = strempty(changes[i].path);
- source = strempty(changes[i].source);
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &path) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &source) ||
- !dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
+ u = manager_get_unit_by_pid(m, pid);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit.");
+ } else {
+ u = manager_get_unit(m, name);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name);
}
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
+ r = mac_selinux_unit_access_check(u, message, "status", error);
+ if (r < 0)
+ return r;
- return reply;
+ path = unit_dbus_path(u);
+ if (!path)
+ return -ENOMEM;
-oom:
- dbus_message_unref(reply);
- return NULL;
+ return sd_bus_reply_method_return(message, "o", path);
}
-static int bus_manager_send_unit_files_changed(Manager *m) {
- DBusMessage *s;
+static int method_get_unit_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *path = NULL;
+ Manager *m = userdata;
+ pid_t pid;
+ Unit *u;
int r;
- s = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitFilesChanged");
- if (!s)
- return -ENOMEM;
+ assert(bus);
+ assert(message);
+ assert(m);
- r = bus_broadcast(m, s);
- dbus_message_unref(s);
+ assert_cc(sizeof(pid_t) == sizeof(uint32_t));
- return r;
-}
+ /* Anyone can call this method */
-static int bus_manager_set_runtime_watchdog_usec(DBusMessageIter *i, const char *property, void *data) {
- uint64_t *t = data;
+ r = sd_bus_message_read(message, "u", &pid);
+ if (r < 0)
+ return r;
+ if (pid < 0)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PID " PID_FMT, pid);
- assert(i);
- assert(property);
+ if (pid == 0) {
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
- dbus_message_iter_get_basic(i, t);
+ r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
+ if (r < 0)
+ return r;
- return watchdog_set_timeout(t);
-}
+ r = sd_bus_creds_get_pid(creds, &pid);
+ if (r < 0)
+ return r;
+ }
-static const char systemd_property_string[] =
- PACKAGE_STRING "\0"
- SYSTEMD_FEATURES;
+ u = manager_get_unit_by_pid(m, pid);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pid);
-static const BusProperty bus_systemd_properties[] = {
- { "Version", bus_property_append_string, "s", 0 },
- { "Features", bus_property_append_string, "s", sizeof(PACKAGE_STRING) },
- { NULL, }
-};
+ r = mac_selinux_unit_access_check(u, message, "status", error);
+ if (r < 0)
+ return r;
-static const BusProperty bus_manager_properties[] = {
- { "Tainted", bus_manager_append_tainted, "s", 0 },
- { "FirmwareTimestamp", bus_property_append_uint64, "t", offsetof(Manager, firmware_timestamp.realtime) },
- { "FirmwareTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, firmware_timestamp.monotonic) },
- { "LoaderTimestamp", bus_property_append_uint64, "t", offsetof(Manager, loader_timestamp.realtime) },
- { "LoaderTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, loader_timestamp.monotonic) },
- { "KernelTimestamp", bus_property_append_uint64, "t", offsetof(Manager, kernel_timestamp.realtime) },
- { "KernelTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, kernel_timestamp.monotonic) },
- { "InitRDTimestamp", bus_property_append_uint64, "t", offsetof(Manager, initrd_timestamp.realtime) },
- { "InitRDTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, initrd_timestamp.monotonic) },
- { "UserspaceTimestamp", bus_property_append_uint64, "t", offsetof(Manager, userspace_timestamp.realtime) },
- { "UserspaceTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, userspace_timestamp.monotonic) },
- { "FinishTimestamp", bus_property_append_uint64, "t", offsetof(Manager, finish_timestamp.realtime) },
- { "FinishTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, finish_timestamp.monotonic) },
- { "GeneratorsStartTimestamp", bus_property_append_uint64, "t", offsetof(Manager, generators_start_timestamp.realtime) },
- { "GeneratorsStartTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, generators_start_timestamp.monotonic) },
- { "GeneratorsFinishTimestamp", bus_property_append_uint64, "t", offsetof(Manager, generators_finish_timestamp.realtime) },
- { "GeneratorsFinishTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, generators_finish_timestamp.monotonic) },
- { "UnitsLoadStartTimestamp", bus_property_append_uint64, "t", offsetof(Manager, unitsload_start_timestamp.realtime) },
- { "UnitsLoadStartTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, unitsload_start_timestamp.monotonic) },
- { "UnitsLoadFinishTimestamp", bus_property_append_uint64, "t", offsetof(Manager, unitsload_finish_timestamp.realtime) },
- { "UnitsLoadFinishTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, unitsload_finish_timestamp.monotonic) },
- { "LogLevel", bus_manager_append_log_level, "s", 0, false, bus_manager_set_log_level },
- { "LogTarget", bus_manager_append_log_target, "s", 0, false, bus_manager_set_log_target },
- { "NNames", bus_manager_append_n_names, "u", 0 },
- { "NJobs", bus_manager_append_n_jobs, "u", 0 },
- { "NInstalledJobs", bus_property_append_uint32, "u", offsetof(Manager, n_installed_jobs) },
- { "NFailedJobs", bus_property_append_uint32, "u", offsetof(Manager, n_failed_jobs) },
- { "Progress", bus_manager_append_progress, "d", 0 },
- { "Environment", bus_property_append_strv, "as", offsetof(Manager, environment), true },
- { "ConfirmSpawn", bus_property_append_bool, "b", offsetof(Manager, confirm_spawn) },
- { "ShowStatus", bus_property_append_bool, "b", offsetof(Manager, show_status) },
- { "UnitPath", bus_property_append_strv, "as", offsetof(Manager, lookup_paths.unit_path), true },
- { "DefaultStandardOutput", bus_manager_append_exec_output, "s", offsetof(Manager, default_std_output) },
- { "DefaultStandardError", bus_manager_append_exec_output, "s", offsetof(Manager, default_std_error) },
- { "RuntimeWatchdogUSec", bus_property_append_usec, "t", offsetof(Manager, runtime_watchdog), false, bus_manager_set_runtime_watchdog_usec },
- { "ShutdownWatchdogUSec", bus_property_append_usec, "t", offsetof(Manager, shutdown_watchdog), false, bus_property_set_usec },
- { "Virtualization", bus_manager_append_virt, "s", 0, },
- { NULL, }
-};
+ path = unit_dbus_path(u);
+ if (!path)
+ return -ENOMEM;
-static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
- _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
- _cleanup_free_ char * path = NULL;
- Manager *m = data;
+ return sd_bus_reply_method_return(message, "o", path);
+}
+
+static int method_load_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *path = NULL;
+ Manager *m = userdata;
+ const char *name;
+ Unit *u;
int r;
- DBusError error;
- JobType job_type = _JOB_TYPE_INVALID;
- bool reload_if_possible = false;
- const char *member;
- assert(connection);
+ assert(bus);
assert(message);
assert(m);
- dbus_error_init(&error);
+ /* Anyone can call this method */
- member = dbus_message_get_member(message);
-
- if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
- const char *name;
- Unit *u;
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ if (isempty(name)) {
+ _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
+ pid_t pid;
- u = manager_get_unit(m, name);
- if (!u) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
+ if (r < 0)
+ return r;
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "status");
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- path = unit_dbus_path(u);
- if (!path)
- goto oom;
-
- if (!dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &path,
- DBUS_TYPE_INVALID))
- goto oom;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
- Unit *u;
- uint32_t pid;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &pid,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- u = manager_get_unit_by_pid(m, (pid_t) pid);
- if (!u) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ r = sd_bus_creds_get_pid(creds, &pid);
+ if (r < 0)
+ return r;
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "status");
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- path = unit_dbus_path(u);
- if (!path)
- goto oom;
-
- if (!dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &path,
- DBUS_TYPE_INVALID))
- goto oom;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
- const char *name;
- Unit *u;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- r = manager_load_unit(m, name, NULL, &error, &u);
+ u = manager_get_unit_by_pid(m, pid);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit.");
+ } else {
+ r = manager_load_unit(m, name, NULL, error, &u);
if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
-
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "status");
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- path = unit_dbus_path(u);
- if (!path)
- goto oom;
-
- if (!dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &path,
- DBUS_TYPE_INVALID))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
- job_type = JOB_START;
- else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
- job_type = JOB_START;
- else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
- job_type = JOB_STOP;
- else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
- job_type = JOB_RELOAD;
- else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
- job_type = JOB_RESTART;
- else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
- job_type = JOB_TRY_RESTART;
- else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
- reload_if_possible = true;
- job_type = JOB_RESTART;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
- reload_if_possible = true;
- job_type = JOB_TRY_RESTART;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
- const char *name, *swho;
- int32_t signo;
- Unit *u;
- KillWho who;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &swho,
- DBUS_TYPE_INT32, &signo,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- if (isempty(swho))
- who = KILL_ALL;
- else {
- who = kill_who_from_string(swho);
- if (who < 0)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
- }
+ return r;
+ }
- if (signo <= 0 || signo >= _NSIG)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ r = mac_selinux_unit_access_check(u, message, "status", error);
+ if (r < 0)
+ return r;
- u = manager_get_unit(m, name);
- if (!u) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ path = unit_dbus_path(u);
+ if (!path)
+ return -ENOMEM;
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "stop");
+ return sd_bus_reply_method_return(message, "o", path);
+}
- r = unit_kill(u, who, signo, &error);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
- uint32_t id;
- Job *j;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &id,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- j = manager_get_job(m, id);
- if (!j) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+static int method_start_unit_generic(sd_bus *bus, sd_bus_message *message, Manager *m, JobType job_type, bool reload_if_possible, sd_bus_error *error) {
+ const char *name;
+ Unit *u;
+ int r;
- SELINUX_UNIT_ACCESS_CHECK(j->unit, connection, message, "status");
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- path = job_dbus_path(j);
- if (!path)
- goto oom;
-
- if (!dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &path,
- DBUS_TYPE_INVALID))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CancelJob")) {
- uint32_t id;
- Job *j;
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_UINT32, &id,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- j = manager_get_job(m, id);
- if (!j) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ assert(bus);
+ assert(message);
+ assert(m);
- SELINUX_UNIT_ACCESS_CHECK(j->unit, connection, message, "stop");
- job_finish_and_invalidate(j, JOB_CANCELED, true);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = manager_load_unit(m, name, NULL, error, &u);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
+ return bus_unit_method_start_generic(bus, message, u, job_type, reload_if_possible, error);
+}
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
- manager_clear_jobs(m);
+static int method_start_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_START, false, error);
+}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+static int method_stop_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_STOP, false, error);
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
+static int method_reload_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_RELOAD, false, error);
+}
- SELINUX_ACCESS_CHECK(connection, message, "reload");
+static int method_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_RESTART, false, error);
+}
- manager_reset_failed(m);
+static int method_try_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_TRY_RESTART, false, error);
+}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+static int method_reload_or_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_RESTART, true, error);
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
- const char *name;
- Unit *u;
+static int method_reload_or_try_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_start_unit_generic(bus, message, userdata, JOB_TRY_RESTART, true, error);
+}
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+static int method_start_unit_replace(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ const char *old_name;
+ Unit *u;
+ int r;
- u = manager_get_unit(m, name);
- if (!u) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ assert(bus);
+ assert(message);
+ assert(m);
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "reload");
-
- unit_reset_failed(u);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
- DBusMessageIter iter, sub;
- Iterator i;
- Unit *u;
- const char *k;
-
- SELINUX_ACCESS_CHECK(connection, message, "status");
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- dbus_message_iter_init_append(reply, &iter);
-
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
- goto oom;
-
- HASHMAP_FOREACH_KEY(u, k, m->units, i) {
- char *u_path, *j_path;
- const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
- DBusMessageIter sub2;
- uint32_t job_id;
- Unit *f;
-
- if (k != u->id)
- continue;
-
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
-
- description = unit_description(u);
- load_state = unit_load_state_to_string(u->load_state);
- active_state = unit_active_state_to_string(unit_active_state(u));
- sub_state = unit_sub_state_to_string(u);
-
- f = unit_following(u);
- following = f ? f->id : "";
-
- u_path = unit_dbus_path(u);
- if (!u_path)
- goto oom;
-
- if (u->job) {
- job_id = (uint32_t) u->job->id;
-
- if (!(j_path = job_dbus_path(u->job))) {
- free(u_path);
- goto oom;
- }
-
- sjob_type = job_type_to_string(u->job->type);
- } else {
- job_id = 0;
- j_path = u_path;
- sjob_type = "";
- }
-
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
- free(u_path);
- if (u->job)
- free(j_path);
- goto oom;
- }
-
- free(u_path);
- if (u->job)
- free(j_path);
-
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
+ r = sd_bus_message_read(message, "s", &old_name);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
+ u = manager_get_unit(m, old_name);
+ if (!u || !u->job || u->job->type != JOB_START)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
- DBusMessageIter iter, sub;
- Iterator i;
- Job *j;
+ return method_start_unit_generic(bus, message, m, JOB_START, false, error);
+}
- SELINUX_ACCESS_CHECK(connection, message, "status");
+static int method_kill_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ const char *name;
+ Unit *u;
+ int r;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ assert(bus);
+ assert(message);
+ assert(m);
- dbus_message_iter_init_append(reply, &iter);
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
- goto oom;
+ u = manager_get_unit(m, name);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- HASHMAP_FOREACH(j, m->jobs, i) {
- char *u_path, *j_path;
- const char *state, *type;
- uint32_t id;
- DBusMessageIter sub2;
+ return bus_unit_method_kill(bus, message, u, error);
+}
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
- goto oom;
+static int method_reset_failed_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ const char *name;
+ Unit *u;
+ int r;
- id = (uint32_t) j->id;
- state = job_state_to_string(j->state);
- type = job_type_to_string(j->type);
+ assert(bus);
+ assert(message);
+ assert(m);
- j_path = job_dbus_path(j);
- if (!j_path)
- goto oom;
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- u_path = unit_dbus_path(j->unit);
- if (!u_path) {
- free(j_path);
- goto oom;
- }
+ u = manager_get_unit(m, name);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->id) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
- free(j_path);
- free(u_path);
- goto oom;
- }
+ return bus_unit_method_reset_failed(bus, message, u, error);
+}
- free(j_path);
- free(u_path);
+static int method_set_unit_properties(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ const char *name;
+ Unit *u;
+ int r;
- if (!dbus_message_iter_close_container(&sub, &sub2))
- goto oom;
- }
+ assert(bus);
+ assert(message);
+ assert(m);
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
- char *client;
- Set *s;
+ u = manager_get_unit(m, name);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- SELINUX_ACCESS_CHECK(connection, message, "status");
+ return bus_unit_method_set_properties(bus, message, u, error);
+}
- s = bus_acquire_subscribed(m, connection);
- if (!s)
- goto oom;
+static int transient_unit_from_message(
+ Manager *m,
+ sd_bus_message *message,
+ const char *name,
+ Unit **unit,
+ sd_bus_error *error) {
- client = strdup(bus_message_get_sender_with_fallback(message));
- if (!client)
- goto oom;
+ Unit *u;
+ int r;
- r = set_consume(s, client);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
+ assert(m);
+ assert(message);
+ assert(name);
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = manager_load_unit(m, name, NULL, error, &u);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
- char *client;
+ if (u->load_state != UNIT_NOT_FOUND ||
+ set_size(u->dependencies[UNIT_REFERENCED_BY]) > 0)
+ return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit %s already exists.", name);
- SELINUX_ACCESS_CHECK(connection, message, "status");
+ /* OK, the unit failed to load and is unreferenced, now let's
+ * fill in the transient data instead */
+ r = unit_make_transient(u);
+ if (r < 0)
+ return r;
- client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) bus_message_get_sender_with_fallback(message));
- if (!client) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ /* Set our properties */
+ r = bus_unit_set_properties(u, message, UNIT_RUNTIME, false, error);
+ if (r < 0)
+ return r;
- free(client);
+ *unit = u;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ return 0;
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
- FILE *f;
- char *dump = NULL;
- size_t size;
+static int transient_aux_units_from_message(
+ Manager *m,
+ sd_bus_message *message,
+ sd_bus_error *error) {
- SELINUX_ACCESS_CHECK(connection, message, "status");
+ Unit *u;
+ char *name = NULL;
+ int r;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ assert(m);
+ assert(message);
- f = open_memstream(&dump, &size);
- if (!f)
- goto oom;
+ r = sd_bus_message_enter_container(message, 'a', "(sa(sv))");
+ if (r < 0)
+ return r;
- manager_dump_units(m, f, NULL);
- manager_dump_jobs(m, f, NULL);
+ while ((r = sd_bus_message_enter_container(message, 'r', "sa(sv)")) > 0) {
+ if (r <= 0)
+ return r;
- if (ferror(f)) {
- fclose(f);
- free(dump);
- goto oom;
- }
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- fclose(f);
+ r = transient_unit_from_message(m, message, name, &u, error);
+ if (r < 0 && r != -EEXIST)
+ return r;
- if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
- free(dump);
- goto oom;
+ if (r != -EEXIST) {
+ r = unit_load(u);
+ if (r < 0)
+ return r;
}
- free(dump);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
- const char *name;
- dbus_bool_t cleanup;
- Snapshot *s;
+ r = sd_bus_message_exit_container(message);
+ if (r < 0)
+ return r;
+ }
+ if (r < 0)
+ return r;
- SELINUX_ACCESS_CHECK(connection, message, "start");
+ r = sd_bus_message_exit_container(message);
+ if (r < 0)
+ return r;
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_BOOLEAN, &cleanup,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ return 0;
+}
- if (isempty(name))
- name = NULL;
+static int method_start_transient_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ const char *name, *smode;
+ Manager *m = userdata;
+ JobMode mode;
+ UnitType t;
+ Unit *u;
+ int r;
- r = snapshot_create(m, name, cleanup, &error, &s);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
+ assert(bus);
+ assert(message);
+ assert(m);
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = mac_selinux_access_check(message, "start", error);
+ if (r < 0)
+ return r;
- path = unit_dbus_path(UNIT(s));
- if (!path)
- goto oom;
+ r = sd_bus_message_read(message, "ss", &name, &smode);
+ if (r < 0)
+ return r;
- if (!dbus_message_append_args(
- reply,
- DBUS_TYPE_OBJECT_PATH, &path,
- DBUS_TYPE_INVALID))
- goto oom;
+ t = unit_name_to_type(name);
+ if (t < 0)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit type.");
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RemoveSnapshot")) {
- const char *name;
- Unit *u;
+ if (!unit_vtable[t]->can_transient)
+ 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 (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ mode = job_mode_from_string(smode);
+ if (mode < 0)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s is invalid.", smode);
- u = manager_get_unit(m, name);
- if (!u) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s does not exist.", name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ r = bus_verify_manage_units_async(m, message, 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 */
- if (u->type != UNIT_SNAPSHOT) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not a snapshot.", name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+ r = transient_unit_from_message(m, message, name, &u, error);
+ if (r < 0)
+ return r;
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "stop");
- snapshot_remove(SNAPSHOT(u));
+ r = transient_aux_units_from_message(m, message, error);
+ if (r < 0)
+ return r;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ /* And load this stub fully */
+ r = unit_load(u);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
- _cleanup_free_ char *introspection = NULL;
- FILE *f;
- Iterator i;
- Unit *u;
- Job *j;
- const char *k;
- size_t size;
+ manager_dispatch_load_queue(m);
- SELINUX_ACCESS_CHECK(connection, message, "status");
+ /* Finally, start it */
+ return bus_unit_queue_job(bus, message, u, JOB_START, mode, false, error);
+}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+static int method_get_job(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *path = NULL;
+ Manager *m = userdata;
+ uint32_t id;
+ Job *j;
+ int r;
- /* We roll our own introspection code here, instead of
- * relying on bus_default_message_handler() because we
- * need to generate our introspection string
- * dynamically. */
+ assert(bus);
+ assert(message);
+ assert(m);
- f = open_memstream(&introspection, &size);
- if (!f)
- goto oom;
+ /* Anyone can call this method */
- fputs(INTROSPECTION_BEGIN, f);
+ r = sd_bus_message_read(message, "u", &id);
+ if (r < 0)
+ return r;
- HASHMAP_FOREACH_KEY(u, k, m->units, i) {
- _cleanup_free_ char *p = NULL;
+ j = manager_get_job(m, id);
+ if (!j)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
- if (k != u->id)
- continue;
+ r = mac_selinux_unit_access_check(j->unit, message, "status", error);
+ if (r < 0)
+ return r;
- p = bus_path_escape(k);
- if (!p) {
- fclose(f);
- goto oom;
- }
+ path = job_dbus_path(j);
+ if (!path)
+ return -ENOMEM;
- fprintf(f, "<node name=\"unit/%s\"/>", p);
- }
+ return sd_bus_reply_method_return(message, "o", path);
+}
- HASHMAP_FOREACH(j, m->jobs, i)
- fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
+static int method_cancel_job(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ uint32_t id;
+ Job *j;
+ int r;
- fputs(INTROSPECTION_END, f);
+ assert(bus);
+ assert(message);
+ assert(m);
- if (ferror(f)) {
- fclose(f);
- goto oom;
- }
+ r = sd_bus_message_read(message, "u", &id);
+ if (r < 0)
+ return r;
- fclose(f);
+ j = manager_get_job(m, id);
+ if (!j)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
- if (!introspection)
- goto oom;
+ return bus_job_method_cancel(bus, message, j, error);
+}
- if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
- goto oom;
- }
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
+static int method_clear_jobs(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- SELINUX_ACCESS_CHECK(connection, message, "reload");
+ assert(bus);
+ assert(message);
+ assert(m);
- assert(!m->queued_message);
+ r = mac_selinux_access_check(message, "reload", error);
+ if (r < 0)
+ return r;
- /* Instead of sending the reply back right away, we
- * just remember that we need to and then send it
- * after the reload is finished. That way the caller
- * knows when the reload finished. */
+ r = bus_verify_manage_units_async(m, message, 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 */
- m->queued_message = dbus_message_new_method_return(message);
- if (!m->queued_message)
- goto oom;
+ manager_clear_jobs(m);
- m->queued_message_connection = connection;
- m->exit_code = MANAGER_RELOAD;
+ return sd_bus_reply_method_return(message, NULL);
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
+static int method_reset_failed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- SELINUX_ACCESS_CHECK(connection, message, "reload");
+ assert(bus);
+ assert(message);
+ assert(m);
- /* We don't send a reply back here, the client should
- * just wait for us disconnecting. */
+ r = mac_selinux_access_check(message, "reload", error);
+ if (r < 0)
+ return r;
- m->exit_code = MANAGER_REEXECUTE;
+ r = bus_verify_manage_units_async(m, message, 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 */
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
+ manager_reset_failed(m);
- SELINUX_ACCESS_CHECK(connection, message, "halt");
+ return sd_bus_reply_method_return(message, NULL);
+}
- if (m->running_as == SYSTEMD_SYSTEM) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
- return bus_send_error_reply(connection, message, &error, -ENOTSUP);
- }
+static int list_units_filtered(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error, char **states) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ Manager *m = userdata;
+ const char *k;
+ Iterator i;
+ Unit *u;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ /* Anyone can call this method */
- m->exit_code = MANAGER_EXIT;
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
+ r = sd_bus_message_new_method_return(message, &reply);
+ if (r < 0)
+ return r;
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
+ r = sd_bus_message_open_container(reply, 'a', "(ssssssouso)");
+ if (r < 0)
+ return r;
- if (m->running_as != SYSTEMD_SYSTEM) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
- return bus_send_error_reply(connection, message, &error, -ENOTSUP);
- }
+ HASHMAP_FOREACH_KEY(u, k, m->units, i) {
+ _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
+ Unit *following;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ if (k != u->id)
+ continue;
- m->exit_code = MANAGER_REBOOT;
+ following = unit_following(u);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
+ if (!strv_isempty(states) &&
+ !strv_contains(states, unit_load_state_to_string(u->load_state)) &&
+ !strv_contains(states, unit_active_state_to_string(unit_active_state(u))) &&
+ !strv_contains(states, unit_sub_state_to_string(u)))
+ continue;
- SELINUX_ACCESS_CHECK(connection, message, "halt");
+ unit_path = unit_dbus_path(u);
+ if (!unit_path)
+ return -ENOMEM;
- if (m->running_as != SYSTEMD_SYSTEM) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
- return bus_send_error_reply(connection, message, &error, -ENOTSUP);
+ if (u->job) {
+ job_path = job_dbus_path(u->job);
+ if (!job_path)
+ return -ENOMEM;
}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = sd_bus_message_append(
+ reply, "(ssssssouso)",
+ u->id,
+ unit_description(u),
+ unit_load_state_to_string(u->load_state),
+ unit_active_state_to_string(unit_active_state(u)),
+ unit_sub_state_to_string(u),
+ following ? following->id : "",
+ unit_path,
+ u->job ? u->job->id : 0,
+ u->job ? job_type_to_string(u->job->type) : "",
+ job_path ? job_path : "/");
+ if (r < 0)
+ return r;
+ }
+
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, reply, NULL);
+}
- m->exit_code = MANAGER_POWEROFF;
+static int method_list_units(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return list_units_filtered(bus, message, userdata, error, NULL);
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
+static int method_list_units_filtered(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_strv_free_ char **states = NULL;
+ int r;
- SELINUX_ACCESS_CHECK(connection, message, "halt");
+ r = sd_bus_message_read_strv(message, &states);
+ if (r < 0)
+ return r;
- if (m->running_as != SYSTEMD_SYSTEM) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
- return bus_send_error_reply(connection, message, &error, -ENOTSUP);
- }
+ return list_units_filtered(bus, message, userdata, error, states);
+}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+static int method_list_jobs(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ Manager *m = userdata;
+ Iterator i;
+ Job *j;
+ int r;
- m->exit_code = MANAGER_HALT;
+ assert(bus);
+ assert(message);
+ assert(m);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
+ /* Anyone can call this method */
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
- if (m->running_as != SYSTEMD_SYSTEM) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
- return bus_send_error_reply(connection, message, &error, -ENOTSUP);
- }
+ r = sd_bus_message_new_method_return(message, &reply);
+ if (r < 0)
+ return r;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = sd_bus_message_open_container(reply, 'a', "(usssoo)");
+ if (r < 0)
+ return r;
- m->exit_code = MANAGER_KEXEC;
+ HASHMAP_FOREACH(j, m->jobs, i) {
+ _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
+
+ job_path = job_dbus_path(j);
+ if (!job_path)
+ return -ENOMEM;
+
+ unit_path = unit_dbus_path(j->unit);
+ if (!unit_path)
+ return -ENOMEM;
+
+ r = sd_bus_message_append(
+ reply, "(usssoo)",
+ j->id,
+ j->unit->id,
+ job_type_to_string(j->type),
+ job_state_to_string(j->state),
+ job_path,
+ unit_path);
+ if (r < 0)
+ return r;
+ }
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SwitchRoot")) {
- const char *switch_root, *switch_root_init;
- char *u, *v;
- bool good;
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ return r;
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
+ return sd_bus_send(bus, reply, NULL);
+}
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &switch_root,
- DBUS_TYPE_STRING, &switch_root_init,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+static int method_subscribe(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- if (path_equal(switch_root, "/") || !path_is_absolute(switch_root))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
+ assert(bus);
+ assert(message);
+ assert(m);
- if (!isempty(switch_root_init) && !path_is_absolute(switch_root_init))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
+ /* Anyone can call this method */
- if (m->running_as != SYSTEMD_SYSTEM) {
- dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Switching root is only supported for system managers.");
- return bus_send_error_reply(connection, message, &error, -ENOTSUP);
- }
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
- /* Safety check */
- if (isempty(switch_root_init)) {
- good = path_is_os_tree(switch_root);
- if (!good)
- log_error("Not switching root: %s does not seem to be an OS tree. /etc/os-release is missing.", switch_root);
- }
- else {
- _cleanup_free_ char *p = NULL;
+ if (bus == m->api_bus) {
- p = strjoin(switch_root, "/", switch_root_init, NULL);
- if (!p)
- goto oom;
+ /* Note that direct bus connection subscribe by
+ * default, we only track peers on the API bus here */
- good = access(p, X_OK) >= 0;
- if (!good)
- log_error("Not switching root: cannot execute new init %s", p);
+ if (!m->subscribed) {
+ r = sd_bus_track_new(bus, &m->subscribed, NULL, NULL);
+ if (r < 0)
+ return r;
}
- if (!good)
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
-
- u = strdup(switch_root);
- if (!u)
- goto oom;
- if (!isempty(switch_root_init)) {
- v = strdup(switch_root_init);
- if (!v) {
- free(u);
- goto oom;
- }
- } else
- v = NULL;
+ r = sd_bus_track_add_sender(m->subscribed, message);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_ALREADY_SUBSCRIBED, "Client is already subscribed.");
+ }
- free(m->switch_root);
- free(m->switch_root_init);
- m->switch_root = u;
- m->switch_root_init = v;
+ return sd_bus_reply_method_return(message, NULL);
+}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+static int method_unsubscribe(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- m->exit_code = MANAGER_SWITCH_ROOT;
+ assert(bus);
+ assert(message);
+ assert(m);
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
- _cleanup_strv_free_ char **l = NULL;
- char **e = NULL;
+ /* Anyone can call this method */
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
- r = bus_parse_strv(message, &l);
- if (r == -ENOMEM)
- goto oom;
+ if (bus == m->api_bus) {
+ r = sd_bus_track_remove_sender(m->subscribed, message);
if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
- if (!strv_env_is_valid(l))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
-
- e = strv_env_merge(2, m->environment, l);
- if (!e)
- goto oom;
-
- reply = dbus_message_new_method_return(message);
- if (!reply) {
- strv_free(e);
- goto oom;
- }
+ return r;
+ if (r == 0)
+ return sd_bus_error_setf(error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
+ }
- strv_free(m->environment);
- m->environment = e;
+ return sd_bus_reply_method_return(message, NULL);
+}
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
- _cleanup_strv_free_ char **l = NULL;
- char **e = NULL;
+static int method_dump(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *dump = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ Manager *m = userdata;
+ size_t size;
+ int r;
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
+ assert(bus);
+ assert(message);
+ assert(m);
- r = bus_parse_strv(message, &l);
- if (r == -ENOMEM)
- goto oom;
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
- if (!strv_env_name_or_assignment_is_valid(l))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
-
- e = strv_env_delete(m->environment, 1, l);
- if (!e)
- goto oom;
-
- reply = dbus_message_new_method_return(message);
- if (!reply) {
- strv_free(e);
- goto oom;
- }
+ /* Anyone can call this method */
- strv_free(m->environment);
- m->environment = e;
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
- _cleanup_strv_free_ char **l_set = NULL, **l_unset = NULL, **e = NULL;
- char **f = NULL;
- DBusMessageIter iter;
+ f = open_memstream(&dump, &size);
+ if (!f)
+ return -ENOMEM;
- SELINUX_ACCESS_CHECK(connection, message, "reboot");
+ manager_dump_units(m, f, NULL);
+ manager_dump_jobs(m, f, NULL);
- if (!dbus_message_iter_init(message, &iter))
- goto oom;
+ fflush(f);
- r = bus_parse_strv_iter(&iter, &l_unset);
- if (r == -ENOMEM)
- goto oom;
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
- if (!strv_env_name_or_assignment_is_valid(l_unset))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
+ if (ferror(f))
+ return -ENOMEM;
- if (!dbus_message_iter_next(&iter))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
+ return sd_bus_reply_method_return(message, "s", dump);
+}
- r = bus_parse_strv_iter(&iter, &l_set);
- if (r == -ENOMEM)
- goto oom;
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
- if (!strv_env_is_valid(l_set))
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
-
- e = strv_env_delete(m->environment, 1, l_unset);
- if (!e)
- goto oom;
-
- f = strv_env_merge(2, e, l_set);
- if (!f)
- goto oom;
-
- reply = dbus_message_new_method_return(message);
- if (!reply) {
- strv_free(f);
- goto oom;
- }
+static int method_create_snapshot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *path = NULL;
+ Manager *m = userdata;
+ const char *name;
+ int cleanup;
+ Snapshot *s = NULL;
+ int r;
- strv_free(m->environment);
- m->environment = f;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnitFiles")) {
- DBusMessageIter iter, sub, sub2;
- Hashmap *h;
- Iterator i;
- UnitFileList *item;
+ assert(bus);
+ assert(message);
+ assert(m);
- SELINUX_ACCESS_CHECK(connection, message, "status");
+ r = mac_selinux_access_check(message, "start", error);
+ if (r < 0)
+ return r;
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+ r = sd_bus_message_read(message, "sb", &name, &cleanup);
+ if (r < 0)
+ return r;
- h = hashmap_new(string_hash_func, string_compare_func);
- if (!h)
- goto oom;
+ if (isempty(name))
+ name = NULL;
- r = unit_file_get_list(m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
- if (r < 0) {
- unit_file_list_free(h);
- return bus_send_error_reply(connection, message, NULL, r);
- }
+ r = bus_verify_manage_units_async(m, message, 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 */
- dbus_message_iter_init_append(reply, &iter);
+ r = snapshot_create(m, name, cleanup, error, &s);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ss)", &sub)) {
- unit_file_list_free(h);
- goto oom;
- }
+ path = unit_dbus_path(UNIT(s));
+ if (!path)
+ return -ENOMEM;
- HASHMAP_FOREACH(item, h, i) {
- const char *state;
+ return sd_bus_reply_method_return(message, "o", path);
+}
- state = unit_file_state_to_string(item->state);
- assert(state);
+static int method_remove_snapshot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ const char *name;
+ Unit *u;
+ int r;
- if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &item->path) ||
- !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
- !dbus_message_iter_close_container(&sub, &sub2)) {
- unit_file_list_free(h);
- goto oom;
- }
- }
+ assert(bus);
+ assert(message);
+ assert(m);
- unit_file_list_free(h);
-
- if (!dbus_message_iter_close_container(&iter, &sub))
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitFileState")) {
- const char *name;
- UnitFileState state;
- const char *s;
-
- SELINUX_ACCESS_CHECK(connection, message, "status");
-
- if (!dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_INVALID))
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- state = unit_file_get_state(m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, name);
- if (state < 0)
- return bus_send_error_reply(connection, message, NULL, state);
-
- s = unit_file_state_to_string(state);
- assert(s);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- if (!dbus_message_append_args(
- reply,
- DBUS_TYPE_STRING, &s,
- DBUS_TYPE_INVALID))
- goto oom;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "EnableUnitFiles") ||
- dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReenableUnitFiles") ||
- dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LinkUnitFiles") ||
- dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PresetUnitFiles") ||
- dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "MaskUnitFiles") ||
- dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetDefaultTarget")) {
-
- char **l = NULL;
- DBusMessageIter iter;
- UnitFileScope scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
- UnitFileChange *changes = NULL;
- unsigned n_changes = 0;
- dbus_bool_t runtime, force;
- int carries_install_info = -1;
-
- SELINUX_ACCESS_CHECK(connection, message, streq(member, "MaskUnitFiles") ? "disable" : "enable");
-
- if (!dbus_message_iter_init(message, &iter))
- goto oom;
-
- r = bus_parse_strv_iter(&iter, &l);
- if (r < 0) {
- if (r == -ENOMEM)
- goto oom;
-
- return bus_send_error_reply(connection, message, NULL, r);
- }
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
- if (!dbus_message_iter_next(&iter) ||
- bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, true) < 0 ||
- bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &force, false) < 0) {
- strv_free(l);
- return bus_send_error_reply(connection, message, NULL, -EIO);
- }
+ u = manager_get_unit(m, name);
+ if (!u)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s does not exist.", name);
- if (streq(member, "EnableUnitFiles")) {
- r = unit_file_enable(scope, runtime, NULL, l, force, &changes, &n_changes);
- carries_install_info = r;
- } else if (streq(member, "ReenableUnitFiles")) {
- r = unit_file_reenable(scope, runtime, NULL, l, force, &changes, &n_changes);
- carries_install_info = r;
- } else if (streq(member, "LinkUnitFiles"))
- r = unit_file_link(scope, runtime, NULL, l, force, &changes, &n_changes);
- else if (streq(member, "PresetUnitFiles")) {
- r = unit_file_preset(scope, runtime, NULL, l, force, &changes, &n_changes);
- carries_install_info = r;
- } else if (streq(member, "MaskUnitFiles"))
- r = unit_file_mask(scope, runtime, NULL, l, force, &changes, &n_changes);
- else if (streq(member, "SetDefaultTarget"))
- r = unit_file_set_default(scope, NULL, l[0], &changes, &n_changes);
- else
- assert_not_reached("Uh? Wrong method");
-
- strv_free(l);
- bus_manager_send_unit_files_changed(m);
-
- if (r < 0) {
- unit_file_changes_free(changes, n_changes);
- return bus_send_error_reply(connection, message, NULL, r);
- }
+ if (u->type != UNIT_SNAPSHOT)
+ return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not a snapshot", name);
- reply = message_from_file_changes(message, changes, n_changes, carries_install_info);
- unit_file_changes_free(changes, n_changes);
+ return bus_snapshot_method_remove(bus, message, u, error);
+}
- if (!reply)
- goto oom;
+static int method_reload(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "DisableUnitFiles") ||
- dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnmaskUnitFiles")) {
+ assert(bus);
+ assert(message);
+ assert(m);
- char **l = NULL;
- DBusMessageIter iter;
- UnitFileScope scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
- UnitFileChange *changes = NULL;
- unsigned n_changes = 0;
- dbus_bool_t runtime;
+ r = mac_selinux_access_check(message, "reload", error);
+ if (r < 0)
+ return r;
- SELINUX_ACCESS_CHECK(connection, message, streq(member, "UnmaskUnitFiles") ? "enable" : "disable");
+ r = bus_verify_reload_daemon_async(m, message, 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 */
- if (!dbus_message_iter_init(message, &iter))
- goto oom;
+ /* Instead of sending the reply back right away, we just
+ * remember that we need to and then send it after the reload
+ * is finished. That way the caller knows when the reload
+ * finished. */
- r = bus_parse_strv_iter(&iter, &l);
- if (r < 0) {
- if (r == -ENOMEM)
- goto oom;
+ assert(!m->queued_message);
+ r = sd_bus_message_new_method_return(message, &m->queued_message);
+ if (r < 0)
+ return r;
- return bus_send_error_reply(connection, message, NULL, r);
- }
+ m->queued_message_bus = sd_bus_ref(bus);
+ m->exit_code = MANAGER_RELOAD;
- if (!dbus_message_iter_next(&iter) ||
- bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, false) < 0) {
- strv_free(l);
- return bus_send_error_reply(connection, message, NULL, -EIO);
- }
+ return 1;
+}
- if (streq(member, "DisableUnitFiles"))
- r = unit_file_disable(scope, runtime, NULL, l, &changes, &n_changes);
- else if (streq(member, "UnmaskUnitFiles"))
- r = unit_file_unmask(scope, runtime, NULL, l, &changes, &n_changes);
- else
- assert_not_reached("Uh? Wrong method");
+static int method_reexecute(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- strv_free(l);
- bus_manager_send_unit_files_changed(m);
+ assert(bus);
+ assert(message);
+ assert(m);
- if (r < 0) {
- unit_file_changes_free(changes, n_changes);
- return bus_send_error_reply(connection, message, NULL, r);
- }
+ r = mac_selinux_access_check(message, "reload", error);
+ if (r < 0)
+ return r;
- reply = message_from_file_changes(message, changes, n_changes, -1);
- unit_file_changes_free(changes, n_changes);
+ r = bus_verify_reload_daemon_async(m, message, 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 */
- if (!reply)
- goto oom;
+ /* We don't send a reply back here, the client should
+ * just wait for us disconnecting. */
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetDefaultTarget")) {
- UnitFileScope scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
- _cleanup_free_ char *default_target = NULL;
+ m->exit_code = MANAGER_REEXECUTE;
+ return 1;
+}
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
+static int method_exit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- r = unit_file_get_default(scope, NULL, &default_target);
- if (r < 0)
- return bus_send_error_reply(connection, message, NULL, r);
+ assert(bus);
+ assert(message);
+ assert(m);
- if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &default_target, DBUS_TYPE_INVALID)) {
- goto oom;
- }
+ r = mac_selinux_access_check(message, "halt", error);
+ if (r < 0)
+ return r;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetUnitProperties")) {
- DBusMessageIter iter;
- dbus_bool_t runtime;
- const char *name;
- Unit *u;
+ if (m->running_as == SYSTEMD_SYSTEM)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
- if (!dbus_message_iter_init(message, &iter))
- goto oom;
+ m->exit_code = MANAGER_EXIT;
- if (bus_iter_get_basic_and_next(&iter, DBUS_TYPE_STRING, &name, true) < 0 ||
- bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, true) < 0)
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
+ return sd_bus_reply_method_return(message, NULL);
+}
- u = manager_get_unit(m, name);
- if (!u) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
+static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "start");
+ assert(bus);
+ assert(message);
+ assert(m);
- r = bus_unit_set_properties(u, &iter, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, &error);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
-
- reply = dbus_message_new_method_return(message);
- if (!reply)
- goto oom;
-
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartTransientUnit")) {
- const char *name, *smode;
- DBusMessageIter iter;
- JobMode mode;
- UnitType t;
- Unit *u;
-
- if (!dbus_message_iter_init(message, &iter))
- goto oom;
-
- if (bus_iter_get_basic_and_next(&iter, DBUS_TYPE_STRING, &name, true) < 0 ||
- bus_iter_get_basic_and_next(&iter, DBUS_TYPE_STRING, &smode, true) < 0)
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
-
- t = unit_name_to_type(name);
- if (t < 0)
- return bus_send_error_reply(connection, message, NULL, -EINVAL);
- if (!unit_vtable[t]->can_transient) {
- dbus_set_error(&error, DBUS_ERROR_INVALID_ARGS, "Unit type %s does not support transient units.", unit_type_to_string(t));
- return bus_send_error_reply(connection, message, &error, -EINVAL);
- }
+ r = mac_selinux_access_check(message, "reboot", error);
+ if (r < 0)
+ return r;
- mode = job_mode_from_string(smode);
- if (mode < 0) {
- dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
- return bus_send_error_reply(connection, message, &error, -EINVAL);
- }
+ if (m->running_as != SYSTEMD_SYSTEM)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
- r = manager_load_unit(m, name, NULL, NULL, &u);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
+ m->exit_code = MANAGER_REBOOT;
- SELINUX_UNIT_ACCESS_CHECK(u, connection, message, "start");
+ return sd_bus_reply_method_return(message, NULL);
+}
- if (u->load_state != UNIT_NOT_FOUND || set_size(u->dependencies[UNIT_REFERENCED_BY]) > 0) {
- dbus_set_error(&error, BUS_ERROR_UNIT_EXISTS, "Unit %s already exists.", name);
- return bus_send_error_reply(connection, message, &error, -EEXIST);
- }
+static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
- /* OK, the unit failed to load and is unreferenced,
- * now let's fill in the transient data instead */
- r = unit_make_transient(u);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
+ assert(bus);
+ assert(message);
+ assert(m);
- /* Set our properties */
- r = bus_unit_set_properties(u, &iter, UNIT_RUNTIME, false, &error);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
+ r = mac_selinux_access_check(message, "halt", error);
+ if (r < 0)
+ return r;
- /* And load this stub fully */
- r = unit_load(u);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
+ if (m->running_as != SYSTEMD_SYSTEM)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
+
+ m->exit_code = MANAGER_POWEROFF;
+
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_halt(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "halt", error);
+ if (r < 0)
+ return r;
+
+ if (m->running_as != SYSTEMD_SYSTEM)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Halt is only supported for system managers.");
- manager_dispatch_load_queue(m);
+ m->exit_code = MANAGER_HALT;
- /* Finally, start it */
- return bus_unit_queue_job(connection, message, u, JOB_START, mode, false);
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_kexec(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "reboot", error);
+ if (r < 0)
+ return r;
+
+ if (m->running_as != SYSTEMD_SYSTEM)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "KExec is only supported for system managers.");
+
+ m->exit_code = MANAGER_KEXEC;
+
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_switch_root(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ char *ri = NULL, *rt = NULL;
+ const char *root, *init;
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "reboot", error);
+ if (r < 0)
+ return r;
+
+ if (m->running_as != SYSTEMD_SYSTEM)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Root switching is only supported by system manager.");
+
+ r = sd_bus_message_read(message, "ss", &root, &init);
+ if (r < 0)
+ return r;
+
+ if (path_equal(root, "/") || !path_is_absolute(root))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid switch root path %s", root);
+ /* Safety check */
+ if (isempty(init)) {
+ if (!path_is_os_tree(root))
+ 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);
} else {
- const BusBoundProperties bps[] = {
- { "org.freedesktop.systemd1.Manager", bus_systemd_properties, systemd_property_string },
- { "org.freedesktop.systemd1.Manager", bus_manager_properties, m },
- { NULL, }
- };
+ _cleanup_free_ char *p = NULL;
- SELINUX_ACCESS_CHECK(connection, message, "status");
+ if (!path_is_absolute(init))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid init path %s", init);
- return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, bps);
+ p = strappend(root, init);
+ if (!p)
+ return -ENOMEM;
+
+ if (access(p, X_OK) < 0)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified init binary %s does not exist.", p);
}
- if (job_type != _JOB_TYPE_INVALID) {
- const char *name, *smode, *old_name = NULL;
- JobMode mode;
- Unit *u;
- dbus_bool_t b;
-
- if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
- b = dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &old_name,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &smode,
- DBUS_TYPE_INVALID);
- else
- b = dbus_message_get_args(
- message,
- &error,
- DBUS_TYPE_STRING, &name,
- DBUS_TYPE_STRING, &smode,
- DBUS_TYPE_INVALID);
- if (!b)
- return bus_send_error_reply(connection, message, &error, -EINVAL);
-
- if (old_name) {
- u = manager_get_unit(m, old_name);
- if (!u || !u->job || u->job->type != JOB_START) {
- dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
- return bus_send_error_reply(connection, message, &error, -ENOENT);
- }
- }
+ rt = strdup(root);
+ if (!rt)
+ return -ENOMEM;
- mode = job_mode_from_string(smode);
- if (mode < 0) {
- dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
- return bus_send_error_reply(connection, message, &error, -EINVAL);
+ if (!isempty(init)) {
+ ri = strdup(init);
+ if (!ri) {
+ free(rt);
+ return -ENOMEM;
}
+ }
- r = manager_load_unit(m, name, NULL, &error, &u);
- if (r < 0)
- return bus_send_error_reply(connection, message, &error, r);
+ free(m->switch_root);
+ m->switch_root = rt;
- return bus_unit_queue_job(connection, message, u, job_type, mode, reload_if_possible);
- }
+ free(m->switch_root_init);
+ m->switch_root_init = ri;
- if (reply)
- if (!bus_maybe_send_reply(connection, message, reply))
- goto oom;
+ m->exit_code = MANAGER_SWITCH_ROOT;
- return DBUS_HANDLER_RESULT_HANDLED;
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_set_environment(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_strv_free_ char **plus = NULL;
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "reload", error);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read_strv(message, &plus);
+ if (r < 0)
+ return r;
+ if (!strv_env_is_valid(plus))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments");
-oom:
- dbus_error_free(&error);
+ r = bus_verify_set_environment_async(m, message, 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_environment_add(m, NULL, plus);
+ if (r < 0)
+ return r;
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
+ return sd_bus_reply_method_return(message, NULL);
}
-const DBusObjectPathVTable bus_manager_vtable = {
- .message_function = bus_manager_message_handler
-};
+static int method_unset_environment(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_strv_free_ char **minus = NULL;
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "reload", error);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read_strv(message, &minus);
+ if (r < 0)
+ return r;
+
+ if (!strv_env_name_or_assignment_is_valid(minus))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments");
+
+ r = bus_verify_set_environment_async(m, message, 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_environment_add(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 *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_strv_free_ char **minus = NULL, **plus = NULL;
+ Manager *m = userdata;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "reload", 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_env_name_or_assignment_is_valid(minus))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments");
+ if (!strv_env_is_valid(plus))
+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments");
+
+ r = bus_verify_set_environment_async(m, message, 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_environment_add(m, minus, plus);
+ if (r < 0)
+ return r;
+
+ return sd_bus_reply_method_return(message, NULL);
+}
+
+static int method_list_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ Manager *m = userdata;
+ UnitFileList *item;
+ Hashmap *h;
+ Iterator i;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ /* Anyone can call this method */
+
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_new_method_return(message, &reply);
+ if (r < 0)
+ return r;
+
+ h = hashmap_new(&string_hash_ops);
+ if (!h)
+ return -ENOMEM;
+
+ r = unit_file_get_list(m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
+ if (r < 0)
+ goto fail;
+
+ r = sd_bus_message_open_container(reply, 'a', "(ss)");
+ if (r < 0)
+ goto fail;
+
+ HASHMAP_FOREACH(item, h, i) {
+
+ r = sd_bus_message_append(reply, "(ss)", item->path, unit_file_state_to_string(item->state));
+ if (r < 0)
+ goto fail;
+ }
+
+ unit_file_list_free(h);
+
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, reply, NULL);
+
+fail:
+ unit_file_list_free(h);
+ return r;
+}
+
+static int method_get_unit_file_state(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ Manager *m = userdata;
+ const char *name;
+ UnitFileState state;
+ UnitFileScope scope;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ /* Anyone can call this method */
+
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read(message, "s", &name);
+ if (r < 0)
+ return r;
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ state = unit_file_get_state(scope, NULL, name);
+ if (state < 0)
+ return state;
+
+ return sd_bus_reply_method_return(message, "s", unit_file_state_to_string(state));
+}
+
+static int method_get_default_target(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_free_ char *default_target = NULL;
+ Manager *m = userdata;
+ UnitFileScope scope;
+ int r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ /* Anyone can call this method */
+
+ r = mac_selinux_access_check(message, "status", error);
+ if (r < 0)
+ return r;
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = unit_file_get_default(scope, NULL, &default_target);
+ 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_bus_message_unref_ 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 int reply_unit_file_changes_and_free(
+ Manager *m,
+ sd_bus *bus,
+ sd_bus_message *message,
+ int carries_install_info,
+ UnitFileChange *changes,
+ unsigned n_changes) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ unsigned i;
+ int r;
+
+ if (n_changes > 0) {
+ r = bus_foreach_bus(m, NULL, send_unit_files_changed, NULL);
+ if (r < 0)
+ log_debug_errno(r, "Failed to send UnitFilesChanged signal: %m");
+ }
+
+ r = sd_bus_message_new_method_return(message, &reply);
+ if (r < 0)
+ goto fail;
+
+ if (carries_install_info >= 0) {
+ r = sd_bus_message_append(reply, "b", carries_install_info);
+ if (r < 0)
+ goto fail;
+ }
+
+ r = sd_bus_message_open_container(reply, 'a', "(sss)");
+ if (r < 0)
+ goto fail;
+
+ for (i = 0; i < n_changes; i++) {
+ r = sd_bus_message_append(
+ reply, "(sss)",
+ unit_file_change_type_to_string(changes[i].type),
+ changes[i].path,
+ changes[i].source);
+ if (r < 0)
+ goto fail;
+ }
+
+ r = sd_bus_message_close_container(reply);
+ if (r < 0)
+ goto fail;
+
+ return sd_bus_send(bus, reply, NULL);
+
+fail:
+ unit_file_changes_free(changes, n_changes);
+ return r;
+}
+
+static int method_enable_unit_files_generic(
+ sd_bus *bus,
+ sd_bus_message *message,
+ 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,
+ sd_bus_error *error) {
+
+ _cleanup_strv_free_ char **l = NULL;
+ UnitFileChange *changes = NULL;
+ unsigned n_changes = 0;
+ UnitFileScope scope;
+ int runtime, force, r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = sd_bus_message_read_strv(message, &l);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read(message, "bb", &runtime, &force);
+ if (r < 0)
+ return r;
+
+ r = mac_selinux_unit_access_check_strv(l, message, m, verb, error);
+ if (r < 0)
+ return r;
+
+ r = bus_verify_manage_unit_files_async(m, message, 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 */
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = call(scope, runtime, NULL, l, force, &changes, &n_changes);
+ if (r < 0)
+ return r;
+
+ return reply_unit_file_changes_and_free(m, bus, message, carries_install_info ? r : -1, changes, n_changes);
+}
+
+static int method_enable_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_enable, true, error);
+}
+
+static int method_reenable_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_reenable, true, error);
+}
+
+static int method_link_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_link, false, 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);
+}
+
+static int method_preset_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_preset_without_mode, true, error);
+}
+
+static int method_mask_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_enable_unit_files_generic(bus, message, userdata, "disable", unit_file_mask, false, error);
+}
+
+static int method_preset_unit_files_with_mode(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+
+ _cleanup_strv_free_ char **l = NULL;
+ UnitFileChange *changes = NULL;
+ unsigned n_changes = 0;
+ Manager *m = userdata;
+ UnitFilePresetMode mm;
+ UnitFileScope scope;
+ int runtime, force, r;
+ const char *mode;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ 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;
+
+ if (isempty(mode))
+ mm = UNIT_FILE_PRESET_FULL;
+ else {
+ mm = unit_file_preset_mode_from_string(mode);
+ if (mm < 0)
+ return -EINVAL;
+ }
+
+ r = mac_selinux_unit_access_check_strv(l, message, m, "enable", error);
+ if (r < 0)
+ return r;
+
+ r = bus_verify_manage_unit_files_async(m, message, 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 */
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = unit_file_preset(scope, runtime, NULL, l, mm, force, &changes, &n_changes);
+ if (r < 0)
+ return r;
+
+ return reply_unit_file_changes_and_free(m, bus, message, r, changes, n_changes);
+}
+
+static int method_disable_unit_files_generic(
+ sd_bus *bus,
+ sd_bus_message *message,
+ Manager *m, const
+ char *verb,
+ int (*call)(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], UnitFileChange **changes, unsigned *n_changes),
+ sd_bus_error *error) {
+
+ _cleanup_strv_free_ char **l = NULL;
+ UnitFileChange *changes = NULL;
+ unsigned n_changes = 0;
+ UnitFileScope scope;
+ int r, runtime;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, verb, error);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read_strv(message, &l);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read(message, "b", &runtime);
+ if (r < 0)
+ return r;
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = bus_verify_manage_unit_files_async(m, message, 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(scope, runtime, NULL, l, &changes, &n_changes);
+ if (r < 0)
+ return r;
+
+ return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
+}
+
+static int method_disable_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_disable_unit_files_generic(bus, message, userdata, "disable", unit_file_disable, error);
+}
+
+static int method_unmask_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ return method_disable_unit_files_generic(bus, message, userdata, "enable", unit_file_unmask, error);
+}
+
+static int method_set_default_target(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ UnitFileChange *changes = NULL;
+ unsigned n_changes = 0;
+ Manager *m = userdata;
+ UnitFileScope scope;
+ const char *name;
+ int force, r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "enable", 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, 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 */
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = unit_file_set_default(scope, NULL, name, force, &changes, &n_changes);
+ if (r < 0)
+ return r;
+
+ return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
+}
+
+static int method_preset_all_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ UnitFileChange *changes = NULL;
+ unsigned n_changes = 0;
+ Manager *m = userdata;
+ UnitFilePresetMode mm;
+ UnitFileScope scope;
+ const char *mode;
+ int force, runtime, r;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = mac_selinux_access_check(message, "enable", error);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force);
+ if (r < 0)
+ return r;
+
+ if (isempty(mode))
+ mm = UNIT_FILE_PRESET_FULL;
+ else {
+ mm = unit_file_preset_mode_from_string(mode);
+ if (mm < 0)
+ return -EINVAL;
+ }
+
+ r = bus_verify_manage_unit_files_async(m, message, 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 */
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = unit_file_preset_all(scope, runtime, NULL, mm, force, &changes, &n_changes);
+ if (r < 0)
+ return r;
+
+ return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
+}
+
+static int method_add_dependency_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+ _cleanup_strv_free_ char **l = NULL;
+ Manager *m = userdata;
+ UnitFileChange *changes = NULL;
+ unsigned n_changes = 0;
+ UnitFileScope scope;
+ int runtime, force, r;
+ char *target;
+ char *type;
+ UnitDependency dep;
+
+ assert(bus);
+ assert(message);
+ assert(m);
+
+ r = bus_verify_manage_unit_files_async(m, message, 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;
+
+ dep = unit_dependency_from_string(type);
+ if (dep < 0)
+ return -EINVAL;
+
+ r = mac_selinux_unit_access_check_strv(l, message, m, "enable", error);
+ if (r < 0)
+ return r;
+
+ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+ r = unit_file_add_dependency(scope, runtime, NULL, l, target, dep, force, &changes, &n_changes);
+ if (r < 0)
+ return r;
+
+ return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
+}
+
+const sd_bus_vtable bus_manager_vtable[] = {
+ SD_BUS_VTABLE_START(0),
+
+ SD_BUS_PROPERTY("Version", "s", property_get_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("Features", "s", property_get_features, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("Virtualization", "s", property_get_virtualization, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("Architecture", "s", property_get_architecture, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("Tainted", "s", property_get_tainted, 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_WRITABLE_PROPERTY("LogLevel", "s", property_get_log_level, property_set_log_level, 0, 0),
+ SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", property_get_log_target, property_set_log_target, 0, 0),
+ SD_BUS_PROPERTY("NNames", "u", property_get_n_names, 0, 0),
+ SD_BUS_PROPERTY("NFailedUnits", "u", property_get_n_failed_units, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
+ SD_BUS_PROPERTY("NJobs", "u", property_get_n_jobs, 0, 0),
+ 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("Progress", "d", property_get_progress, 0, 0),
+ SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(Manager, environment), 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_PROPERTY("ControlGroup", "s", NULL, offsetof(Manager, cgroup_root), 0),
+ SD_BUS_PROPERTY("SystemState", "s", property_get_system_state, 0, 0),
+
+ SD_BUS_METHOD("GetUnit", "s", "o", method_get_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("GetUnitByPID", "u", "o", method_get_unit_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("LoadUnit", "s", "o", method_load_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("StartUnit", "ss", "o", method_start_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("StartUnitReplace", "sss", "o", method_start_unit_replace, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("StopUnit", "ss", "o", method_stop_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ReloadUnit", "ss", "o", method_reload_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("RestartUnit", "ss", "o", method_restart_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("TryRestartUnit", "ss", "o", method_try_restart_unit, 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("KillUnit", "ssi", NULL, method_kill_unit, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ResetFailedUnit", "s", NULL, method_reset_failed_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("GetJob", "u", "o", method_get_job, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("CancelJob", "u", NULL, method_cancel_job, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ClearJobs", NULL, NULL, method_clear_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ResetFailed", NULL, NULL, method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ListUnits", NULL, "a(ssssssouso)", method_list_units, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ListUnitsFiltered", "as", "a(ssssssouso)", method_list_units_filtered, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("ListJobs", NULL, "a(usssoo)", method_list_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("Subscribe", NULL, NULL, method_subscribe, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("Unsubscribe", NULL, NULL, method_unsubscribe, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("Dump", NULL, "s", method_dump, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("CreateSnapshot", "sb", "o", method_create_snapshot, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("RemoveSnapshot", "s", NULL, method_remove_snapshot, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("Reload", NULL, NULL, method_reload, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("Reexecute", NULL, NULL, method_reexecute, SD_BUS_VTABLE_UNPRIVILEGED),
+ SD_BUS_METHOD("Exit", NULL, NULL, method_exit, 0),
+ SD_BUS_METHOD("Reboot", NULL, NULL, method_reboot, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
+ SD_BUS_METHOD("PowerOff", NULL, NULL, method_poweroff, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
+ SD_BUS_METHOD("Halt", NULL, NULL, method_halt, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
+ SD_BUS_METHOD("KExec", NULL, NULL, method_kexec, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
+ SD_BUS_METHOD("SwitchRoot", "ss", NULL, method_switch_root, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
+ SD_BUS_METHOD("SetEnvironment", "as", NULL, method_set_environment, SD_BUS_VTABLE_UNPRIVILEGED),
+ 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("ListUnitFiles", NULL, "a(ss)", method_list_unit_files, 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),
+
+ SD_BUS_SIGNAL("UnitNew", "so", 0),
+ SD_BUS_SIGNAL("UnitRemoved", "so", 0),
+ SD_BUS_SIGNAL("JobNew", "uos", 0),
+ SD_BUS_SIGNAL("JobRemoved", "uoss", 0),
+ SD_BUS_SIGNAL("StartupFinished", "tttttt", 0),
+ SD_BUS_SIGNAL("UnitFilesChanged", NULL, 0),
+ SD_BUS_SIGNAL("Reloading", "b", 0),
+
+ SD_BUS_VTABLE_END
+};
+
+static int send_finished(sd_bus *bus, void *userdata) {
+ _cleanup_bus_message_unref_ sd_bus_message *message = NULL;
+ usec_t *times = userdata;
+ int r;
+
+ assert(bus);
+ assert(times);
+
+ r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartupFinished");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(message, "tttttt", times[0], times[1], times[2], times[3], times[4], times[5]);
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, message, NULL);
+}
+
+void bus_manager_send_finished(
+ Manager *m,
+ usec_t firmware_usec,
+ usec_t loader_usec,
+ usec_t kernel_usec,
+ usec_t initrd_usec,
+ usec_t userspace_usec,
+ usec_t total_usec) {
+
+ int r;
+
+ assert(m);
+
+ r = bus_foreach_bus(
+ m,
+ NULL,
+ send_finished,
+ (usec_t[6]) {
+ firmware_usec,
+ loader_usec,
+ kernel_usec,
+ initrd_usec,
+ userspace_usec,
+ total_usec
+ });
+ if (r < 0)
+ log_debug_errno(r, "Failed to send finished signal: %m");
+}
+
+static int send_reloading(sd_bus *bus, void *userdata) {
+ _cleanup_bus_message_unref_ sd_bus_message *message = NULL;
+ int r;
+
+ assert(bus);
+
+ r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reloading");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(message, "b", PTR_TO_INT(userdata));
+ if (r < 0)
+ return r;
+
+ return sd_bus_send(bus, message, NULL);
+}
+
+void bus_manager_send_reloading(Manager *m, bool active) {
+ int r;
+
+ assert(m);
+
+ r = bus_foreach_bus(m, NULL, send_reloading, INT_TO_PTR(active));
+ if (r < 0)
+ log_debug_errno(r, "Failed to send reloading signal: %m");
+}
+
+static int send_changed_signal(sd_bus *bus, void *userdata) {
+ assert(bus);
+
+ return sd_bus_emit_properties_changed_strv(bus,
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ NULL);
+}
+
+void bus_manager_send_change_signal(Manager *m) {
+ int r;
+
+ assert(m);
+
+ r = bus_foreach_bus(m, NULL, send_changed_signal, NULL);
+ if (r < 0)
+ log_debug_errno(r, "Failed to send manager change signal: %m");
+}