dbus-manager.c revision 9a60da2834074d970ca063c210fe9d2f05c70532
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include "dbus.h"
#include "log.h"
#include "dbus-manager.h"
#include "strv.h"
#include "bus-errors.h"
#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=\"mode\" 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=\"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" \
" <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=\"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=\"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"
#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" \
" </signal>\n" \
" <signal name=\"JobRemoved\">\n" \
" <arg name=\"id\" type=\"u\"/>\n" \
" <arg name=\"job\" type=\"o\"/>\n" \
" <arg name=\"success\" type=\"b\"/>\n" \
" </signal>"
#define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
" <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
" <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
" <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
" <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\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=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
" <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
" <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
" <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n"
\
#ifdef HAVE_SYSV_COMPAT
#define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
" <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
" <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
" <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
#else
#endif
#define BUS_MANAGER_INTERFACE_END \
" </interface>\n"
#define BUS_MANAGER_INTERFACE \
#define INTROSPECTION_BEGIN \
"<node>\n" \
#define INTROSPECTION_END \
"</node>\n"
static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
const char *t;
assert(m);
assert(i);
t = log_target_to_string(log_get_target());
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
return -ENOMEM;
return 0;
}
static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
const char *t;
assert(m);
assert(i);
t = log_level_to_string(log_get_max_level());
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
return -ENOMEM;
return 0;
}
static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
uint32_t u;
assert(m);
assert(i);
u = hashmap_size(m->units);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
return -ENOMEM;
return 0;
}
static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
uint32_t u;
assert(m);
assert(i);
u = hashmap_size(m->jobs);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
return -ENOMEM;
return 0;
}
static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
double d;
assert(m);
assert(i);
if (dual_timestamp_is_set(&m->finish_timestamp))
d = 1.0;
else
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
return -ENOMEM;
return 0;
}
static const char *message_get_sender_with_fallback(DBusMessage *m) {
const char *s;
assert(m);
if ((s = dbus_message_get_sender(m)))
return s;
/* When the message came in from a direct connection the
* message will have no sender. We fix that here. */
return ":no-sender";
}
static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
const BusProperty properties[] = {
{ "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
{ "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
{ "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
{ "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
{ "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
{ "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
{ "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
{ "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
{ "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
{ "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
{ "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
{ "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
{ "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
#ifdef HAVE_SYSV_COMPAT
{ "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
{ "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
{ "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
#endif
};
int r;
bool reload_if_possible = false;
assert(m);
const char *name;
Unit *u;
if (!dbus_message_get_args(
&error,
if (!(u = manager_get_unit(m, name))) {
}
goto oom;
if (!(path = unit_dbus_path(u)))
goto oom;
if (!dbus_message_append_args(
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
Unit *u;
if (!dbus_message_get_args(
&error,
dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
}
goto oom;
if (!(path = unit_dbus_path(u)))
goto oom;
if (!dbus_message_append_args(
goto oom;
const char *name;
Unit *u;
if (!dbus_message_get_args(
&error,
goto oom;
if (!(path = unit_dbus_path(u)))
goto oom;
if (!dbus_message_append_args(
goto oom;
else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
reload_if_possible = true;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
reload_if_possible = true;
Unit *u;
if (!dbus_message_get_args(
&error,
signo <= 0 ||
if (!(u = manager_get_unit(m, name))) {
}
goto oom;
Job *j;
if (!dbus_message_get_args(
&error,
if (!(j = manager_get_job(m, id))) {
}
goto oom;
if (!(path = job_dbus_path(j)))
goto oom;
if (!dbus_message_append_args(
goto oom;
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
const char *name;
Unit *u;
if (!dbus_message_get_args(
&error,
if (!(u = manager_get_unit(m, name))) {
}
goto oom;
Iterator i;
Unit *u;
const char *k;
goto oom;
goto oom;
HASHMAP_FOREACH_KEY(u, k, m->units, i) {
Unit *f;
continue;
goto oom;
description = unit_description(u);
f = unit_following(u);
if (!(u_path = unit_dbus_path(u)))
goto oom;
goto oom;
}
} else {
job_id = 0;
sjob_type = "";
}
goto oom;
}
goto oom;
}
goto oom;
Iterator i;
Job *j;
goto oom;
goto oom;
HASHMAP_FOREACH(j, m->jobs, i) {
goto oom;
if (!(j_path = job_dbus_path(j)))
goto oom;
goto oom;
}
goto oom;
}
goto oom;
}
goto oom;
char *client;
Set *s;
if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
goto oom;
set_free(s);
goto oom;
}
}
goto oom;
}
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
char *client;
if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
}
goto oom;
FILE *f;
goto oom;
goto oom;
manager_dump_units(m, f, NULL);
manager_dump_jobs(m, f, NULL);
if (ferror(f)) {
fclose(f);
goto oom;
}
fclose(f);
goto oom;
}
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
const char *name;
Snapshot *s;
if (!dbus_message_get_args(
&error,
goto oom;
goto oom;
if (!dbus_message_append_args(
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
char *introspection = NULL;
FILE *f;
Iterator i;
Unit *u;
Job *j;
const char *k;
goto oom;
/* We roll our own introspection code here, instead of
* relying on bus_default_message_handler() because we
* need to generate our introspection string
* dynamically. */
goto oom;
fputs(INTROSPECTION_BEGIN, f);
HASHMAP_FOREACH_KEY(u, k, m->units, i) {
char *p;
continue;
if (!(p = bus_path_escape(k))) {
fclose(f);
goto oom;
}
fprintf(f, "<node name=\"unit/%s\"/>", p);
free(p);
}
HASHMAP_FOREACH(j, m->jobs, i)
fputs(INTROSPECTION_END, f);
if (ferror(f)) {
fclose(f);
goto oom;
}
fclose(f);
if (!introspection)
goto oom;
goto oom;
}
assert(!m->queued_message);
/* 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. */
goto oom;
m->exit_code = MANAGER_RELOAD;
goto oom;
m->exit_code = MANAGER_REEXECUTE;
if (m->running_as == MANAGER_SYSTEM) {
dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
}
goto oom;
m->exit_code = MANAGER_EXIT;
if (m->running_as != MANAGER_SYSTEM) {
}
goto oom;
m->exit_code = MANAGER_REBOOT;
if (m->running_as != MANAGER_SYSTEM) {
dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
}
goto oom;
m->exit_code = MANAGER_POWEROFF;
if (m->running_as != MANAGER_SYSTEM) {
}
goto oom;
m->exit_code = MANAGER_HALT;
if (m->running_as != MANAGER_SYSTEM) {
}
goto oom;
m->exit_code = MANAGER_KEXEC;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
if ((r = bus_parse_strv(message, &l)) < 0) {
if (r == -ENOMEM)
goto oom;
}
strv_free(l);
if (!e)
goto oom;
strv_free(e);
goto oom;
}
strv_free(m->environment);
m->environment = e;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
if ((r = bus_parse_strv(message, &l)) < 0) {
if (r == -ENOMEM)
goto oom;
}
strv_free(l);
if (!e)
goto oom;
goto oom;
strv_free(m->environment);
m->environment = e;
} else
if (job_type != _JOB_TYPE_INVALID) {
Job *j;
Unit *u;
bool b;
&error,
else
&error,
if (!b)
if (old_name)
if (!(u = manager_get_unit(m, old_name)) ||
}
}
if (reload_if_possible && unit_can_reload(u)) {
if (job_type == JOB_RESTART)
else if (job_type == JOB_TRY_RESTART)
}
dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
}
goto oom;
j->bus = connection;
goto oom;
if (!(path = job_dbus_path(j)))
goto oom;
if (!dbus_message_append_args(
goto oom;
}
if (reply) {
goto oom;
}
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
const DBusObjectPathVTable bus_manager_vtable = {
};