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/>.
28 #define BUS_JOB_INTERFACE \
29 " <interface name=\"org.freedesktop.systemd1.Job\">\n" \
30 " <method name=\"Cancel\"/>\n" \
31 " <signal name=\"Changed\"/>\n" \
32 " <property name=\"Id\" type=\"u\" access=\"read\"/>\n" \
33 " <property name=\"Unit\" type=\"(so)\" access=\"read\"/>\n" \
34 " <property name=\"JobType\" type=\"s\" access=\"read\"/>\n" \
35 " <property name=\"State\" type=\"s\" access=\"read\"/>\n" \
38 #define INTROSPECTION \
39 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
42 BUS_PROPERTIES_INTERFACE \
43 BUS_INTROSPECTABLE_INTERFACE \
46 const char bus_job_interface[] = BUS_JOB_INTERFACE;
48 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
49 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
51 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
61 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
64 if (!(p = unit_dbus_path(j->unit)))
67 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
68 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
75 if (!dbus_message_iter_close_container(i, &sub))
81 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) {
82 const BusProperty properties[] = {
83 { "org.freedesktop.systemd1.Job", "Id", bus_property_append_uint32, "u", &j->id },
84 { "org.freedesktop.systemd1.Job", "State", bus_job_append_state, "s", &j->state },
85 { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type, "s", &j->type },
86 { "org.freedesktop.systemd1.Job", "Unit", bus_job_append_unit, "(so)", j },
87 { NULL, NULL, NULL, NULL, NULL }
90 DBusMessage *reply = NULL;
91 Manager *m = j->manager;
93 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
94 if (!(reply = dbus_message_new_method_return(message)))
100 return bus_default_message_handler(j->manager, message, INTROSPECTION, properties);
103 if (!dbus_connection_send(m->api_bus, reply, NULL))
106 dbus_message_unref(reply);
109 return DBUS_HANDLER_RESULT_HANDLED;
113 dbus_message_unref(reply);
115 return DBUS_HANDLER_RESULT_NEED_MEMORY;
118 static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
127 log_debug("Got D-Bus request: %s.%s() on %s",
128 dbus_message_get_interface(message),
129 dbus_message_get_member(message),
130 dbus_message_get_path(message));
132 if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
135 return DBUS_HANDLER_RESULT_NEED_MEMORY;
138 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
140 return bus_send_error_reply(m, message, NULL, r);
143 return bus_job_message_dispatch(j, message);
146 const DBusObjectPathVTable bus_job_vtable = {
147 .message_function = bus_job_message_handler
150 void bus_job_send_change_signal(Job *j) {
152 DBusMessage *m = NULL;
155 assert(j->in_dbus_queue);
157 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
158 j->in_dbus_queue = false;
160 if (set_isempty(j->manager->subscribed)) {
161 j->sent_dbus_new_signal = true;
165 if (!(p = job_dbus_path(j)))
168 if (j->sent_dbus_new_signal) {
169 /* Send a change signal */
171 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Job", "Changed")))
174 /* Send a new signal */
176 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
179 if (!dbus_message_append_args(m,
180 DBUS_TYPE_UINT32, &j->id,
181 DBUS_TYPE_OBJECT_PATH, &p,
186 if (!dbus_connection_send(j->manager->api_bus, m, NULL))
190 dbus_message_unref(m);
192 j->sent_dbus_new_signal = true;
200 dbus_message_unref(m);
202 log_error("Failed to allocate job change signal.");
205 void bus_job_send_removed_signal(Job *j, bool success) {
207 DBusMessage *m = NULL;
208 dbus_bool_t b = success;
212 if (set_isempty(j->manager->subscribed))
215 if (!j->sent_dbus_new_signal)
216 bus_job_send_change_signal(j);
218 if (!(p = job_dbus_path(j)))
221 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
224 if (!dbus_message_append_args(m,
225 DBUS_TYPE_UINT32, &j->id,
226 DBUS_TYPE_OBJECT_PATH, &p,
227 DBUS_TYPE_BOOLEAN, &b,
231 if (!dbus_connection_send(j->manager->api_bus, m, NULL))
235 dbus_message_unref(m);
243 dbus_message_unref(m);
245 log_error("Failed to allocate job remove signal.");