dbus-unit.c revision 8a0867d6c5b47c8697d1ddd815116a548433c718
/*-*- 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-unit.h"
#include "bus-errors.h"
const char bus_unit_interface[] = BUS_UNIT_INTERFACE;
#define INVALIDATING_PROPERTIES \
"LoadState\0" \
"ActiveState\0" \
"SubState\0" \
"InactiveExitTimestamp\0" \
"ActiveEnterTimestamp\0" \
"ActiveExitTimestamp\0" \
"InactiveEnterTimestamp\0" \
"Job\0" \
"NeedDaemonReload\0" \
"\0"
char *t;
Iterator j;
return -ENOMEM;
return -ENOMEM;
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
const char *d;
assert(m);
assert(i);
assert(u);
f = unit_following(u);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
return -ENOMEM;
return 0;
}
int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
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(m);
assert(i);
assert(u);
d = unit_description(u);
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
return -ENOMEM;
return 0;
}
int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
const char *state;
assert(m);
assert(i);
assert(u);
return -ENOMEM;
return 0;
}
const char *state;
assert(m);
assert(i);
assert(u);
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(m);
assert(i);
assert(u);
b = unit_can_start(u) &&
!u->meta.refuse_manual_start;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(m);
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->meta.refuse_manual_stop;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
dbus_bool_t b;
assert(m);
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(m);
assert(i);
assert(u);
b = unit_can_isolate(u) &&
!u->meta.refuse_manual_start;
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
return -ENOMEM;
return 0;
}
char *p;
assert(m);
assert(i);
assert(u);
return -ENOMEM;
return -ENOMEM;
free(p);
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. */
if (!(p = unit_dbus_path(u)))
return -ENOMEM;
free(p);
return -ENOMEM;
}
}
free(p);
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
char *t;
bool success;
assert(m);
assert(i);
assert(u);
if ((cgb = unit_get_default_cgroup(u))) {
if (!(t = cgroup_bonding_to_string(cgb)))
return -ENOMEM;
} else
t = (char*) "";
if (cgb)
free(t);
}
return -ENOMEM;
char *t;
bool success;
if (!(t = cgroup_bonding_to_string(cgb)))
return -ENOMEM;
free(t);
if (!success)
return -ENOMEM;
}
if (!dbus_message_iter_close_container(i, &sub))
return -ENOMEM;
return 0;
}
int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
dbus_bool_t b;
assert(m);
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;
}
static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
bool reload_if_possible = false;
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;
int r;
if (!dbus_message_get_args(
&error,
signo <= 0 ||
goto oom;
goto oom;
} else if (UNIT_VTABLE(u)->bus_message_handler)
else
if (job_type != _JOB_TYPE_INVALID) {
const char *smode;
Job *j;
int r;
dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
}
if (!dbus_message_get_args(
&error,
if (reload_if_possible && unit_can_reload(u)) {
if (job_type == JOB_RESTART)
else if (job_type == JOB_TRY_RESTART)
}
}
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;
}
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;
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;
"<node>\n", f);
fputs(BUS_PEER_INTERFACE, 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=\"%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)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
if (r == -ENOENT)
}
oom:
if (reply)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
const DBusObjectPathVTable bus_unit_vtable = {
};
void bus_unit_send_change_signal(Unit *u) {
char *p = NULL;
DBusMessage *m = NULL;
assert(u);
if (u->meta.in_dbus_queue) {
u->meta.in_dbus_queue = false;
}
return;
u->meta.sent_dbus_new_signal = true;
return;
}
if (!(p = unit_dbus_path(u)))
goto oom;
if (u->meta.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
* behaviour if needed. */
if (UNIT_VTABLE(u)->bus_invalidating_properties) {
if (!(m = bus_properties_changed_new(p,
UNIT_VTABLE(u)->bus_interface,
goto oom;
goto oom;
}
goto oom;
} else {
/* Send a new signal */
if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
goto oom;
if (!dbus_message_append_args(m,
goto oom;
}
goto oom;
free(p);
u->meta.sent_dbus_new_signal = true;
return;
oom:
free(p);
if (m)
}
void bus_unit_send_removed_signal(Unit *u) {
char *p = NULL;
DBusMessage *m = NULL;
assert(u);
return;
if (!u->meta.sent_dbus_new_signal)
return;
if (!(p = unit_dbus_path(u)))
goto oom;
if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
goto oom;
if (!dbus_message_append_args(m,
goto oom;
goto oom;
free(p);
return;
oom:
free(p);
if (m)
log_error("Failed to allocate unit remove signal.");
}