1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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/>.
28 #define BUS_JOB_INTERFACE \
29 " <interface name=\"org.freedesktop.systemd1.Job\">\n" \
30 " <method name=\"Cancel\"/>\n" \
31 " <property name=\"Id\" type=\"u\" access=\"read\"/>\n" \
32 " <property name=\"Unit\" type=\"(so)\" access=\"read\"/>\n" \
33 " <property name=\"JobType\" type=\"s\" access=\"read\"/>\n" \
34 " <property name=\"State\" type=\"s\" access=\"read\"/>\n" \
37 #define INTROSPECTION \
38 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
41 BUS_PROPERTIES_INTERFACE \
43 BUS_INTROSPECTABLE_INTERFACE \
46 const char bus_job_interface[] = BUS_JOB_INTERFACE;
48 #define INVALIDATING_PROPERTIES \
52 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
53 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
55 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
65 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
68 if (!(p = unit_dbus_path(j->unit)))
71 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
72 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
79 if (!dbus_message_iter_close_container(i, &sub))
85 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connection, DBusMessage *message) {
86 const BusProperty properties[] = {
87 { "org.freedesktop.systemd1.Job", "Id", bus_property_append_uint32, "u", &j->id },
88 { "org.freedesktop.systemd1.Job", "State", bus_job_append_state, "s", &j->state },
89 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type, "s", &j->type },
90 { "org.freedesktop.systemd1.Job", "Unit", bus_job_append_unit, "(so)", j },
91 { NULL, NULL, NULL, NULL, NULL }
94 DBusMessage *reply = NULL;
96 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
97 if (!(reply = dbus_message_new_method_return(message)))
103 return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, properties);
106 if (!dbus_connection_send(connection, reply, NULL))
109 dbus_message_unref(reply);
112 return DBUS_HANDLER_RESULT_HANDLED;
116 dbus_message_unref(reply);
118 return DBUS_HANDLER_RESULT_NEED_MEMORY;
121 static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
131 if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/job")) {
132 /* Be nice to gdbus and return introspection data for our mid-level paths */
134 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
135 char *introspection = NULL;
140 if (!(reply = dbus_message_new_method_return(message)))
143 /* We roll our own introspection code here, instead of
144 * relying on bus_default_message_handler() because we
145 * need to generate our introspection string
148 if (!(f = open_memstream(&introspection, &size)))
151 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
154 fputs(BUS_INTROSPECTABLE_INTERFACE, f);
155 fputs(BUS_PEER_INTERFACE, f);
157 HASHMAP_FOREACH(j, m->jobs, i)
158 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
160 fputs("</node>\n", f);
173 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
180 if (!dbus_connection_send(connection, reply, NULL))
183 dbus_message_unref(reply);
185 return DBUS_HANDLER_RESULT_HANDLED;
188 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
191 if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
194 return DBUS_HANDLER_RESULT_NEED_MEMORY;
197 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
199 return bus_send_error_reply(m, connection, message, NULL, r);
202 return bus_job_message_dispatch(j, connection, message);
206 dbus_message_unref(reply);
208 return DBUS_HANDLER_RESULT_NEED_MEMORY;
211 const DBusObjectPathVTable bus_job_vtable = {
212 .message_function = bus_job_message_handler
215 static int job_send_message(Job *j, DBusMessage *m) {
221 if (bus_has_subscriber(j->manager)) {
222 if ((r = bus_broadcast(j->manager, m)) < 0)
225 } else if (j->bus_client) {
226 /* If nobody is subscribed, we just send the message
227 * to the client which created the job */
231 if (!dbus_message_set_destination(m, j->bus_client))
234 if (!dbus_connection_send(j->bus, m, NULL))
241 void bus_job_send_change_signal(Job *j) {
243 DBusMessage *m = NULL;
247 if (j->in_dbus_queue) {
248 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
249 j->in_dbus_queue = false;
252 if (!bus_has_subscriber(j->manager) && !j->bus_client) {
253 j->sent_dbus_new_signal = true;
257 if (!(p = job_dbus_path(j)))
260 if (j->sent_dbus_new_signal) {
261 /* Send a properties changed signal */
263 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Job", INVALIDATING_PROPERTIES)))
267 /* Send a new signal */
269 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
272 if (!dbus_message_append_args(m,
273 DBUS_TYPE_UINT32, &j->id,
274 DBUS_TYPE_OBJECT_PATH, &p,
279 if (job_send_message(j, m) < 0)
283 dbus_message_unref(m);
285 j->sent_dbus_new_signal = true;
293 dbus_message_unref(m);
295 log_error("Failed to allocate job change signal.");
298 void bus_job_send_removed_signal(Job *j, bool success) {
300 DBusMessage *m = NULL;
301 dbus_bool_t b = success;
305 if (!bus_has_subscriber(j->manager) && !j->bus_client)
308 if (!j->sent_dbus_new_signal)
309 bus_job_send_change_signal(j);
311 if (!(p = job_dbus_path(j)))
314 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
317 if (!dbus_message_append_args(m,
318 DBUS_TYPE_UINT32, &j->id,
319 DBUS_TYPE_OBJECT_PATH, &p,
320 DBUS_TYPE_BOOLEAN, &b,
324 if (job_send_message(j, m) < 0)
328 dbus_message_unref(m);
336 dbus_message_unref(m);
338 log_error("Failed to allocate job remove signal.");