dbus-unit.c revision 9444b1f20e311f073864d81e913bd4f32fe95cfd
/*-*- 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;
}
char *t;
bool success;
assert(i);
assert(u);
cgb = unit_get_default_cgroup(u);
if (cgb) {
t = cgroup_bonding_to_string(cgb);
if (!t)
return -ENOMEM;
} else
t = (char*) "";
if (cgb)
free(t);
}
return -ENOMEM;
_cleanup_free_ char *t = NULL;
bool success;
t = cgroup_bonding_to_string(cgb);
if (!t)
return -ENOMEM;
if (!success)
return -ENOMEM;
}
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
CGroupAttribute *a;
return -ENOMEM;
_cleanup_free_ char *v = NULL;
bool success;
success =
if (!success)
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;
goto oom;
r = bus_unit_cgroup_set(u, &iter);
if (r < 0)
if (!reply)
goto oom;
goto oom;
r = bus_unit_cgroup_unset(u, &iter);
if (r < 0)
if (!reply)
goto oom;
goto oom;
if (r < 0)
if (!reply)
goto oom;
goto oom;
goto oom;
r = bus_unit_cgroup_attribute_set(u, &iter);
if (r < 0)
if (!reply)
goto oom;
goto oom;
r = bus_unit_cgroup_attribute_unset(u, &iter);
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;
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)
goto oom;
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)
goto oom;
if (bus_broadcast(u->manager, m) < 0)
goto oom;
}
m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit",
if (!m)
goto oom;
} else {
/* Send a new signal */
"org.freedesktop.systemd1.Manager",
"UnitNew");
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;
u->sent_dbus_new_signal = true;
return;
oom:
log_oom();
}
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;
}
}
((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;
}
const char *mode;
int r;
if (r < 0)
return r;
*runtime = true;
*runtime = false;
else
return -EINVAL;
return 0;
}
const char *name;
CGroupBonding *b;
bool runtime;
int r;
assert(u);
if (!unit_get_exec_context(u))
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (!new_path) {
if (!new_path)
return -ENOMEM;
}
return -EINVAL;
if (b) {
return 0;
if (b->essential)
return -EINVAL;
if (!old_path)
return -ENOMEM;
}
r = unit_add_cgroup_from_text(u, name, true, &b);
if (r < 0)
return r;
if (r > 0) {
CGroupAttribute *a;
/* Try to move things to the new place, and clean up the old place */
if (old_path)
/* Apply the attributes to the new group */
cgroup_attribute_apply(a, b);
}
if (!contents)
return -ENOMEM;
}
const char *name;
CGroupAttribute *a, *n;
CGroupBonding *b;
bool runtime;
int r;
assert(u);
if (!unit_get_exec_context(u))
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return -EINVAL;
if (!b)
return -ENOENT;
return -ENOENT;
if (b->essential)
return -EINVAL;
/* Try to migrate the old group away */
cgroup_bonding_free(b, true);
/* Drop all attributes of this controller */
continue;
}
return 0;
}
CGroupAttribute *a;
CGroupBonding *b;
const char *name;
char **l = NULL;
int r;
assert(u);
if (!unit_get_exec_context(u))
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
/* First attempt, read the value from the kernel */
if (b) {
if (r < 0)
return r;
r = read_full_file(p, &v, NULL);
if (r >= 0) {
/* Split on new lines */
l = strv_split_newlines(v);
if (!l)
return -ENOMEM;
*_result = l;
return 0;
}
}
/* If that didn't work, read our cached value */
continue;
r = strv_extend(&l, a->value);
if (r < 0) {
strv_free(l);
return r;
}
}
if (!l)
return -ENOENT;
*_result = l;
return 0;
}
CGroupAttribute *a;
assert(u);
continue;
if (!buf) {
if (!buf)
return -ENOMEM;
} else {
char *b;
if (!b)
return -ENOMEM;
buf = b;
}
}
if (buf)
else
}
_cleanup_strv_free_ char **l = NULL;
int r;
bool runtime = false;
char **value;
const char *name;
assert(u);
if (!unit_get_exec_context(u))
return -EINVAL;
if (r < 0)
return r;
r = bus_parse_strv_iter(iter, &l);
if (r < 0)
return r;
if (!dbus_message_iter_next(iter))
return -EINVAL;
if (r < 0)
return r;
STRV_FOREACH(value, l) {
_cleanup_free_ char *v = NULL;
CGroupAttribute *a;
const CGroupSemantics *s;
if (r < 0)
return r;
if (s && !s->multiple && l[1])
return -EINVAL;
if (r < 0)
return r;
if (r > 0) {
CGroupBonding *b;
if (!b) {
/* Doesn't exist yet? Then let's add it */
r = unit_add_cgroup_from_text(u, a->controller, false, &b);
if (r < 0)
return r;
if (r > 0) {
}
}
/* Make it count */
}
}
if (r < 0)
return r;
return 0;
}
const char *name;
bool runtime;
int r;
assert(u);
if (!unit_get_exec_context(u))
return -EINVAL;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
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) },
{ NULL, }
};
const BusProperty bus_unit_cgroup_properties[] = {
{ NULL, }
};