1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 static const char introspection[] =
28 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
30 " <interface name=\"org.freedesktop.systemd1.Unit\">"
31 " <method name=\"Start\">"
32 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
33 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
35 " <method name=\"Stop\">"
36 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
37 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
39 " <method name=\"Restart\">"
40 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
41 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
43 " <method name=\"Reload\">"
44 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>"
45 " <arg name=\"job\" type=\"o\" direction=\"out\"/>"
47 " <signal name=\"Changed\"/>"
48 " <property name=\"Id\" type=\"s\" access=\"read\"/>"
49 " <property name=\"Description\" type=\"s\" access=\"read\"/>"
50 " <property name=\"LoadState\" type=\"s\" access=\"read\"/>"
51 " <property name=\"ActiveState\" type=\"s\" access=\"read\"/>"
52 " <property name=\"SubState\" type=\"s\" access=\"read\"/>"
53 " <property name=\"FragmentPath\" type=\"s\" access=\"read\"/>"
54 " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>"
55 " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>"
56 " <property name=\"CanReload\" type=\"b\" access=\"read\"/>"
57 " <property name=\"CanStart\" type=\"b\" access=\"read\"/>"
58 " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>"
60 BUS_PROPERTIES_INTERFACE
61 BUS_INTROSPECTABLE_INTERFACE
64 static int bus_unit_append_id(Manager *m, DBusMessageIter *i, const char *property, void *data) {
75 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &id))
81 static int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
90 d = unit_description(u);
92 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
98 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
100 static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
109 state = unit_active_state_to_string(unit_active_state(u));
111 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
117 static int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
126 state = unit_sub_state_to_string(u);
128 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
134 static int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
143 b = unit_can_reload(u);
145 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
151 static int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
160 b = unit_can_start(u);
162 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
168 static int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
178 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
183 if (!(p = job_dbus_path(u->meta.job)))
186 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
187 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
194 /* No job, so let's fill in some placeholder
195 * data. Since we need to fill in a valid path we
196 * simple point to ourselves. */
198 if (!(p = unit_dbus_path(u)))
201 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
202 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
210 if (!dbus_message_iter_close_container(i, &sub))
216 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) {
218 const BusProperty properties[] = {
219 { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id, "s", u },
220 { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description, "s", u },
221 { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state, "s", &u->meta.load_state },
222 { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u },
223 { "org.freedesktop.systemd1.Unit", "SubState", bus_unit_append_sub_state, "s", u },
224 { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string, "s", u->meta.fragment_path },
225 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64, "t", &u->meta.active_enter_timestamp },
226 { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64, "t", &u->meta.active_exit_timestamp },
227 { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload, "b", u },
228 { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start, "b", u },
229 { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job, "(uo)", u },
230 { NULL, NULL, NULL, NULL, NULL }
233 DBusMessage *reply = NULL;
234 Manager *m = u->meta.manager;
236 JobType job_type = _JOB_TYPE_INVALID;
238 dbus_error_init(&error);
240 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
241 job_type = JOB_START;
242 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
244 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
245 job_type = JOB_RELOAD;
246 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
247 job_type = JOB_RESTART;
249 return bus_default_message_handler(u->meta.manager, message, introspection, properties);
251 if (job_type != _JOB_TYPE_INVALID) {
258 if (!dbus_message_get_args(
261 DBUS_TYPE_STRING, &smode,
263 return bus_send_error_reply(m, message, &error, -EINVAL);
265 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
266 return bus_send_error_reply(m, message, NULL, -EINVAL);
268 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
269 return bus_send_error_reply(m, message, NULL, r);
271 if (!(reply = dbus_message_new_method_return(message)))
274 if (!(path = job_dbus_path(j)))
277 if (!dbus_message_append_args(
279 DBUS_TYPE_OBJECT_PATH, &path,
285 if (!dbus_connection_send(m->api_bus, reply, NULL))
288 dbus_message_unref(reply);
291 return DBUS_HANDLER_RESULT_HANDLED;
295 dbus_message_unref(reply);
297 dbus_error_free(&error);
299 return DBUS_HANDLER_RESULT_NEED_MEMORY;
302 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
311 log_debug("Got D-Bus request: %s.%s() on %s",
312 dbus_message_get_interface(message),
313 dbus_message_get_member(message),
314 dbus_message_get_path(message));
316 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
319 return DBUS_HANDLER_RESULT_NEED_MEMORY;
322 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
324 return bus_send_error_reply(m, message, NULL, r);
327 return bus_unit_message_dispatch(u, message);
330 const DBusObjectPathVTable bus_unit_vtable = {
331 .message_function = bus_unit_message_handler
334 void bus_unit_send_change_signal(Unit *u) {
336 DBusMessage *m = NULL;
339 assert(u->meta.in_dbus_queue);
341 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
342 u->meta.in_dbus_queue = false;
344 if (set_isempty(u->meta.manager->subscribed))
347 if (!(p = unit_dbus_path(u)))
350 if (u->meta.sent_dbus_new_signal) {
351 /* Send a change signal */
353 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
357 /* Send a new signal */
359 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew")))
363 if (!dbus_message_append_args(m,
364 DBUS_TYPE_STRING, &id,
365 DBUS_TYPE_OBJECT_PATH, &p,
370 if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
374 dbus_message_unref(m);
376 u->meta.sent_dbus_new_signal = true;
384 dbus_message_unref(m);
386 log_error("Failed to allocate unit change/new signal.");
389 void bus_unit_send_removed_signal(Unit *u) {
391 DBusMessage *m = NULL;
396 if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal)
399 if (!(p = unit_dbus_path(u)))
402 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved")))
406 if (!dbus_message_append_args(m,
407 DBUS_TYPE_STRING, &id,
408 DBUS_TYPE_OBJECT_PATH, &p,
412 if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
416 dbus_message_unref(m);
424 dbus_message_unref(m);
426 log_error("Failed to allocate unit remove signal.");