dbus-unit.c revision b9ec9359369f224bfb13db616f97401a6a177bd8
/*-*- 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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-unit.h"
#include "bus-errors.h"
#include "dbus-common.h"
#include "selinux-access.h"
#include "cgroup-util.h"
#include "strv.h"
#include "path-util.h"
#include "fileio.h"
#define INVALIDATING_PROPERTIES \
"LoadState\0" \
"ActiveState\0" \
"SubState\0" \
"InactiveExitTimestamp\0" \
"ActiveEnterTimestamp\0" \
"ActiveExitTimestamp\0" \
"InactiveEnterTimestamp\0" \
"Job\0" \
"NeedDaemonReload\0"
char *t;
Iterator j;
return -ENOMEM;
SET_FOREACH(t, u->names, j)
return -ENOMEM;
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
const char *d;
assert(i);
assert(u);
f = unit_following(u);
d = f ? f->id : "";
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
return -ENOMEM;
return 0;
}
const char *d;
assert(i);
assert(u);
d = strempty(unit_slice_name(u));
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
return -ENOMEM;
return 0;
}
Unit *u;
Iterator j;
return -ENOMEM;
SET_FOREACH(u, s, j)
return -ENOMEM;
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
const char *d;
assert(i);
assert(u);
d = unit_description(u);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
return -ENOMEM;
return 0;
}
const char *state;
assert(i);
assert(u);
return -ENOMEM;
return 0;
}
const char *state;
assert(i);
assert(u);
return -ENOMEM;
return 0;
}
const char *state;
assert(i);
assert(u);
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(i);
assert(u);
b = unit_can_start(u) &&
!u->refuse_manual_start;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(i);
assert(u);
/* On the lower levels we assume that every unit we can start
* we can also stop */
b = unit_can_start(u) &&
!u->refuse_manual_stop;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(i);
assert(u);
b = unit_can_reload(u);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(i);
assert(u);
b = unit_can_isolate(u) &&
!u->refuse_manual_start;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
_cleanup_free_ char *p = NULL;
assert(i);
assert(u);
return -ENOMEM;
if (u->job) {
p = job_dbus_path(u->job);
if (!p)
return -ENOMEM;
return -ENOMEM;
} else {
/* No job, so let's fill in some placeholder
* data. Since we need to fill in a valid path we
* simple point to ourselves. */
p = unit_dbus_path(u);
if (!p)
return -ENOMEM;
return -ENOMEM;
}
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
static int bus_unit_append_need_daemon_reload(DBusMessageIter *i, const char *property, void *data) {
dbus_bool_t b;
assert(i);
assert(u);
b = unit_need_daemon_reload(u);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
assert(i);
assert(u);
if (u->load_error != 0) {
} else
return -ENOMEM;
return 0;
}
static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
bool reload_if_possible = false;
int r;
else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
reload_if_possible = true;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
reload_if_possible = true;
const char *swho;
if (!dbus_message_get_args(
&error,
else {
if (who < 0)
}
if (r < 0)
if (!reply)
goto oom;
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "SetProperties")) {
goto oom;
if (r < 0)
if (!reply)
goto oom;
} else if (UNIT_VTABLE(u)->bus_message_handler)
else
if (job_type != _JOB_TYPE_INVALID) {
const char *smode;
if (!dbus_message_get_args(
&error,
if (mode < 0) {
}
}
if (reply)
goto oom;
return DBUS_HANDLER_RESULT_HANDLED;
oom:
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
Unit *u;
int r;
assert(m);
/* Be nice to gdbus and return introspection data for our mid-level paths */
char *introspection = NULL;
FILE *f;
Iterator i;
const char *k;
if (!reply)
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. */
if (!f)
goto oom;
"<node>\n", f);
fputs(BUS_PEER_INTERFACE, f);
HASHMAP_FOREACH_KEY(u, k, m->units, i) {
char *p;
if (k != u->id)
continue;
p = bus_path_escape(k);
if (!p) {
fclose(f);
goto oom;
}
fprintf(f, "<node name=\"%s\"/>", p);
free(p);
}
fputs("</node>\n", f);
if (ferror(f)) {
fclose(f);
goto oom;
}
fclose(f);
if (!introspection)
goto oom;
goto oom;
}
goto oom;
return DBUS_HANDLER_RESULT_HANDLED;
}
}
if (r == -ENOMEM)
goto oom;
if (r < 0)
oom:
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
const DBusObjectPathVTable bus_unit_vtable = {
};
void bus_unit_send_change_signal(Unit *u) {
_cleanup_free_ char *p = NULL;
int r;
assert(u);
if (u->in_dbus_queue) {
u->in_dbus_queue = false;
}
if (!u->id)
return;
if (!bus_has_subscriber(u->manager)) {
u->sent_dbus_new_signal = true;
return;
}
p = unit_dbus_path(u);
if (!p) {
log_oom();
return;
}
if (u->sent_dbus_new_signal) {
/* Send a properties changed signal. First for the
* specific type, then for the generic unit. The
* clients may rely on this order to get atomic
* behavior if needed. */
if (UNIT_VTABLE(u)->bus_invalidating_properties) {
m = bus_properties_changed_new(p,
UNIT_VTABLE(u)->bus_interface,
if (!m) {
log_oom();
return;
}
r = bus_broadcast(u->manager, m);
if (r < 0) {
return;
}
}
m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit",
if (!m) {
log_oom();
return;
}
} else {
/* Send a new signal */
"org.freedesktop.systemd1.Manager",
"UnitNew");
if (!m) {
log_oom();
return;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &u->id,
log_oom();
return;
}
}
r = bus_broadcast(u->manager, m);
if (r < 0) {
log_error("Failed to broadcast UnitNew/PropertiesChanged message.");
return;
}
u->sent_dbus_new_signal = true;
}
void bus_unit_send_removed_signal(Unit *u) {
_cleanup_free_ char *p = NULL;
assert(u);
if (!bus_has_subscriber(u->manager))
return;
if (!u->sent_dbus_new_signal)
if (!u->id)
return;
p = unit_dbus_path(u);
if (!p)
goto oom;
"org.freedesktop.systemd1.Manager",
"UnitRemoved");
if (!m)
goto oom;
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &u->id,
goto oom;
if (bus_broadcast(u->manager, m) < 0)
goto oom;
return;
oom:
log_oom();
}
Unit *u,
bool reload_if_possible) {
Job *j;
int r;
assert(u);
if (reload_if_possible && unit_can_reload(u)) {
if (type == JOB_RESTART)
else if (type == JOB_TRY_RESTART)
type = JOB_RELOAD;
}
if (type == JOB_STOP && (u->load_state == UNIT_NOT_FOUND || u->load_state == UNIT_ERROR) && unit_active_state(u) == UNIT_INACTIVE) {
}
((type == JOB_RESTART || type == JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop))) {
"Operation refused, unit %s may be requested by dependency only.", u->id);
}
if (r < 0)
if (!cl)
goto oom;
if (!reply)
goto oom;
path = job_dbus_path(j);
if (!path)
goto oom;
if (!dbus_message_append_args(
goto oom;
goto oom;
return DBUS_HANDLER_RESULT_HANDLED;
oom:
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static int bus_unit_set_transient_property(
Unit *u,
const char *name,
DBusMessageIter *i,
int r;
assert(u);
assert(i);
if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING)
return -EINVAL;
if (mode != UNIT_CHECK) {
const char *description;
r = unit_set_description(u, description);
if (r < 0)
return r;
}
return 1;
const char *s;
if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING)
return -EINVAL;
dbus_message_iter_get_basic(i, &s);
if (isempty(s)) {
if (mode != UNIT_CHECK) {
unit_ref_unset(&u->slice);
}
} else {
if (r < 0)
return r;
return -EINVAL;
if (mode != UNIT_CHECK) {
}
}
return 1;
if (d < 0)
return -EINVAL;
if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_ARRAY ||
return -EINVAL;
dbus_message_iter_recurse(i, &sub);
const char *other;
if (!unit_name_is_valid(other, false))
return -EINVAL;
if (mode != UNIT_CHECK) {
if (r < 0)
return r;
if (!label)
return -ENOMEM;
}
}
return 1;
}
return 0;
}
Unit *u,
bool commit,
bool for_real = false;
unsigned n = 0;
int r;
assert(u);
if (u->transient)
mode &= UNIT_RUNTIME;
/* We iterate through the array twice. First run we just check
* if all passed data is valid, second run actually applies
* it. This is to implement transaction-like behaviour without
* actually providing full transactions. */
return -EINVAL;
for (;;) {
const char *name;
if (for_real)
break;
/* Reached EOF. Let's try again, and this time for realz... */
for_real = true;
continue;
}
return -EINVAL;
return -EINVAL;
if (!UNIT_VTABLE(u)->bus_set_property) {
dbus_set_error(error, DBUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties.");
return -ENOENT;
}
if (r < 0)
return r;
if (r == 0) {
dbus_set_error(error, DBUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name);
return -ENOENT;
}
n += for_real;
}
UNIT_VTABLE(u)->bus_commit_properties(u);
return n;
}
const BusProperty bus_unit_properties[] = {
{ "Requires", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_REQUIRES]), true },
{ "RequiresOverridable", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_REQUIRES_OVERRIDABLE]), true },
{ "Requisite", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_REQUISITE]), true },
{ "RequisiteOverridable", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_REQUISITE_OVERRIDABLE]), true },
{ "BindsTo", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_BINDS_TO]), true },
{ "RequiredBy", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), true },
{ "RequiredByOverridable",bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_REQUIRED_BY_OVERRIDABLE]), true },
{ "WantedBy", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_WANTED_BY]), true },
{ "BoundBy", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_BOUND_BY]), true },
{ "ConsistsOf", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), true },
{ "Conflicts", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_CONFLICTS]), true },
{ "ConflictedBy", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), true },
{ "OnFailure", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_ON_FAILURE]), true },
{ "Triggers", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_TRIGGERS]), true },
{ "TriggeredBy", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), true },
{ "PropagatesReloadTo", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), true },
{ "ReloadPropagatedFrom", bus_unit_append_dependencies, "as", offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), true },
{ "InactiveExitTimestamp",bus_property_append_usec, "t", offsetof(Unit, inactive_exit_timestamp.realtime) },
{ "InactiveExitTimestampMonotonic", bus_property_append_usec, "t", offsetof(Unit, inactive_exit_timestamp.monotonic) },
{ "ActiveEnterTimestamp", bus_property_append_usec, "t", offsetof(Unit, active_enter_timestamp.realtime) },
{ "ActiveEnterTimestampMonotonic", bus_property_append_usec, "t", offsetof(Unit, active_enter_timestamp.monotonic) },
{ "ActiveExitTimestamp", bus_property_append_usec, "t", offsetof(Unit, active_exit_timestamp.realtime) },
{ "ActiveExitTimestampMonotonic", bus_property_append_usec, "t", offsetof(Unit, active_exit_timestamp.monotonic) },
{ "InactiveEnterTimestamp", bus_property_append_usec, "t", offsetof(Unit, inactive_enter_timestamp.realtime) },
{ "InactiveEnterTimestampMonotonic",bus_property_append_usec, "t", offsetof(Unit, inactive_enter_timestamp.monotonic) },
{ "ConditionTimestamp", bus_property_append_usec, "t", offsetof(Unit, condition_timestamp.realtime) },
{ "ConditionTimestampMonotonic", bus_property_append_usec, "t", offsetof(Unit, condition_timestamp.monotonic) },
{}
};
const BusProperty bus_unit_cgroup_properties[] = {
{}
};