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=\"FragmentPath\" type=\"s\" access=\"read\"/>"
53 " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>"
54 " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>"
55 " <property name=\"CanReload\" type=\"b\" access=\"read\"/>"
56 " <property name=\"CanStart\" type=\"b\" access=\"read\"/>"
57 " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>"
59 BUS_PROPERTIES_INTERFACE
60 BUS_INTROSPECTABLE_INTERFACE
63 static int bus_unit_append_id(Manager *m, DBusMessageIter *i, const char *property, void *data) {
74 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &id))
80 static int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
89 d = unit_description(u);
91 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
97 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
99 static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
108 state = unit_active_state_to_string(unit_active_state(u));
110 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
116 static int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
125 b = unit_can_reload(u);
127 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
133 static int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
142 b = unit_can_start(u);
144 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
150 static int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
160 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
165 if (!(p = job_dbus_path(u->meta.job)))
168 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
169 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
176 /* No job, so let's fill in some placeholder
177 * data. Since we need to fill in a valid path we
178 * simple point to ourselves. */
180 if (!(p = unit_dbus_path(u)))
183 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
184 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
192 if (!dbus_message_iter_close_container(i, &sub))
198 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) {
200 const BusProperty properties[] = {
201 { "org.freedesktop.systemd1.Unit", "Id", bus_unit_append_id, "s", u },
202 { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description, "s", u },
203 { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state, "s", &u->meta.load_state },
204 { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u },
205 { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string, "s", u->meta.fragment_path },
206 { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64, "t", &u->meta.active_enter_timestamp },
207 { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64, "t", &u->meta.active_exit_timestamp },
208 { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload, "b", u },
209 { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start, "b", u },
210 { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job, "(uo)", u },
211 { NULL, NULL, NULL, NULL, NULL }
214 DBusMessage *reply = NULL;
215 Manager *m = u->meta.manager;
217 JobType job_type = _JOB_TYPE_INVALID;
219 dbus_error_init(&error);
221 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
222 job_type = JOB_START;
223 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
225 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
226 job_type = JOB_RELOAD;
227 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
228 job_type = JOB_RESTART;
230 return bus_default_message_handler(u->meta.manager, message, introspection, properties);
232 if (job_type != _JOB_TYPE_INVALID) {
239 if (!dbus_message_get_args(
242 DBUS_TYPE_STRING, &smode,
244 return bus_send_error_reply(m, message, &error, -EINVAL);
246 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
247 return bus_send_error_reply(m, message, NULL, -EINVAL);
249 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
250 return bus_send_error_reply(m, message, NULL, r);
252 if (!(reply = dbus_message_new_method_return(message)))
255 if (!(path = job_dbus_path(j)))
258 if (!dbus_message_append_args(
260 DBUS_TYPE_OBJECT_PATH, &path,
266 if (!dbus_connection_send(m->api_bus, reply, NULL))
269 dbus_message_unref(reply);
272 return DBUS_HANDLER_RESULT_HANDLED;
276 dbus_message_unref(reply);
278 dbus_error_free(&error);
280 return DBUS_HANDLER_RESULT_NEED_MEMORY;
283 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
292 log_debug("Got D-Bus request: %s.%s() on %s",
293 dbus_message_get_interface(message),
294 dbus_message_get_member(message),
295 dbus_message_get_path(message));
297 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
300 return DBUS_HANDLER_RESULT_NEED_MEMORY;
303 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
305 return bus_send_error_reply(m, message, NULL, r);
308 return bus_unit_message_dispatch(u, message);
311 const DBusObjectPathVTable bus_unit_vtable = {
312 .message_function = bus_unit_message_handler
315 void bus_unit_send_change_signal(Unit *u) {
317 DBusMessage *m = NULL;
320 assert(u->meta.in_dbus_queue);
322 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
323 u->meta.in_dbus_queue = false;
325 if (set_isempty(u->meta.manager->subscribed))
328 if (!(p = unit_dbus_path(u)))
331 if (u->meta.sent_dbus_new_signal) {
332 /* Send a change signal */
334 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
338 /* Send a new signal */
340 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew")))
344 if (!dbus_message_append_args(m,
345 DBUS_TYPE_STRING, &id,
346 DBUS_TYPE_OBJECT_PATH, &p,
351 if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
355 dbus_message_unref(m);
357 u->meta.sent_dbus_new_signal = true;
365 dbus_message_unref(m);
367 log_error("Failed to allocate unit change/new signal.");
370 void bus_unit_send_removed_signal(Unit *u) {
372 DBusMessage *m = NULL;
377 if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal)
380 if (!(p = unit_dbus_path(u)))
383 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved")))
387 if (!dbus_message_append_args(m,
388 DBUS_TYPE_STRING, &id,
389 DBUS_TYPE_OBJECT_PATH, &p,
393 if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL))
397 dbus_message_unref(m);
405 dbus_message_unref(m);
407 log_error("Failed to allocate unit remove signal.");