]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/dbus-job.c
dbus: follow standardized fdo PropertiesChanged signal spec
[thirdparty/systemd.git] / src / dbus-job.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ea430986 2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
86fbf370
LP
22#include <errno.h>
23
ea430986
LP
24#include "dbus.h"
25#include "log.h"
4139c1b2 26#include "dbus-job.h"
ea430986 27
4288f619
LP
28#define BUS_JOB_INTERFACE \
29 " <interface name=\"org.freedesktop.systemd1.Job\">\n" \
30 " <method name=\"Cancel\"/>\n" \
4288f619
LP
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" \
35 " </interface>\n"
36
37#define INTROSPECTION \
38 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
39 "<node>\n" \
40 BUS_JOB_INTERFACE \
41 BUS_PROPERTIES_INTERFACE \
c4e2ceae 42 BUS_PEER_INTERFACE \
4288f619
LP
43 BUS_INTROSPECTABLE_INTERFACE \
44 "</node>\n"
45
46const char bus_job_interface[] = BUS_JOB_INTERFACE;
ea430986 47
c4e2ceae
LP
48#define INVALIDATING_PROPERTIES \
49 "State\0" \
50 "\0" \
51
4139c1b2
LP
52static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
53static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
86fbf370
LP
54
55static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
56 Job *j = data;
57 DBusMessageIter sub;
58 char *p;
86fbf370
LP
59
60 assert(m);
61 assert(i);
62 assert(property);
63 assert(j);
64
65 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
66 return -ENOMEM;
67
68 if (!(p = unit_dbus_path(j->unit)))
69 return -ENOMEM;
70
9e2f7c11 71 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
86fbf370
LP
72 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
73 free(p);
74 return -ENOMEM;
75 }
76
77 free(p);
78
79 if (!dbus_message_iter_close_container(i, &sub))
80 return -ENOMEM;
81
82 return 0;
83}
84
5e8d1c9a 85static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connection, DBusMessage *message) {
86fbf370 86 const BusProperty properties[] = {
6f4706b7
LP
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 },
86fbf370
LP
91 { NULL, NULL, NULL, NULL, NULL }
92 };
93
b548631a 94 DBusMessage *reply = NULL;
b548631a
LP
95
96 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) {
97 if (!(reply = dbus_message_new_method_return(message)))
98 goto oom;
99
100 job_free(j);
101
102 } else
5e8d1c9a 103 return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, properties);
b548631a
LP
104
105 if (reply) {
5e8d1c9a 106 if (!dbus_connection_send(connection, reply, NULL))
b548631a
LP
107 goto oom;
108
109 dbus_message_unref(reply);
110 }
111
112 return DBUS_HANDLER_RESULT_HANDLED;
113
114oom:
115 if (reply)
116 dbus_message_unref(reply);
117
118 return DBUS_HANDLER_RESULT_NEED_MEMORY;
86fbf370
LP
119}
120
5e8d1c9a 121static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
ea430986 122 Manager *m = data;
86fbf370
LP
123 Job *j;
124 int r;
ea430986
LP
125
126 assert(connection);
127 assert(message);
128 assert(m);
129
86fbf370
LP
130 if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) {
131
132 if (r == -ENOMEM)
133 return DBUS_HANDLER_RESULT_NEED_MEMORY;
134
135 if (r == -ENOENT)
136 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
137
5e8d1c9a 138 return bus_send_error_reply(m, connection, message, NULL, r);
86fbf370
LP
139 }
140
5e8d1c9a 141 return bus_job_message_dispatch(j, connection, message);
ea430986
LP
142}
143
144const DBusObjectPathVTable bus_job_vtable = {
145 .message_function = bus_job_message_handler
146};
c1e1601e 147
a567261a
LP
148static int job_send_message(Job *j, DBusMessage *m) {
149 int r;
150
151 assert(j);
152 assert(m);
153
154 if (bus_has_subscriber(j->manager)) {
155 if ((r = bus_broadcast(j->manager, m)) < 0)
156 return r;
157
158 } else if (j->bus_client) {
159 /* If nobody is subscribed, we just send the message
160 * to the client which created the job */
161
162 assert(j->bus);
163
164 if (!dbus_message_set_destination(m, j->bus_client))
165 return -ENOMEM;
166
167 if (!dbus_connection_send(j->bus, m, NULL))
168 return -ENOMEM;
169 }
170
171 return 0;
172}
173
c1e1601e
LP
174void bus_job_send_change_signal(Job *j) {
175 char *p = NULL;
176 DBusMessage *m = NULL;
177
178 assert(j);
c1e1601e 179
c0bd0cf7
LP
180 if (j->in_dbus_queue) {
181 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
182 j->in_dbus_queue = false;
183 }
c1e1601e 184
a567261a 185 if (!bus_has_subscriber(j->manager) && !j->bus_client) {
94b6dfa2 186 j->sent_dbus_new_signal = true;
c1e1601e 187 return;
94b6dfa2 188 }
c1e1601e
LP
189
190 if (!(p = job_dbus_path(j)))
191 goto oom;
192
193 if (j->sent_dbus_new_signal) {
c4e2ceae 194 /* Send a properties changed signal */
c1e1601e 195
c4e2ceae 196 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Job", INVALIDATING_PROPERTIES)))
c1e1601e 197 goto oom;
c4e2ceae 198
c1e1601e
LP
199 } else {
200 /* Send a new signal */
201
701cc384 202 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
c1e1601e
LP
203 goto oom;
204
205 if (!dbus_message_append_args(m,
206 DBUS_TYPE_UINT32, &j->id,
207 DBUS_TYPE_OBJECT_PATH, &p,
208 DBUS_TYPE_INVALID))
209 goto oom;
210 }
211
a567261a 212 if (job_send_message(j, m) < 0)
c1e1601e
LP
213 goto oom;
214
215 free(p);
216 dbus_message_unref(m);
217
218 j->sent_dbus_new_signal = true;
219
220 return;
221
222oom:
223 free(p);
224
225 if (m)
226 dbus_message_unref(m);
227
228 log_error("Failed to allocate job change signal.");
229}
230
7535cc78 231void bus_job_send_removed_signal(Job *j, bool success) {
c1e1601e
LP
232 char *p = NULL;
233 DBusMessage *m = NULL;
7535cc78 234 dbus_bool_t b = success;
c1e1601e
LP
235
236 assert(j);
237
a567261a 238 if (!bus_has_subscriber(j->manager) && !j->bus_client)
c1e1601e
LP
239 return;
240
7535cc78
LP
241 if (!j->sent_dbus_new_signal)
242 bus_job_send_change_signal(j);
243
c1e1601e
LP
244 if (!(p = job_dbus_path(j)))
245 goto oom;
246
701cc384 247 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
c1e1601e
LP
248 goto oom;
249
250 if (!dbus_message_append_args(m,
251 DBUS_TYPE_UINT32, &j->id,
252 DBUS_TYPE_OBJECT_PATH, &p,
7535cc78 253 DBUS_TYPE_BOOLEAN, &b,
c1e1601e
LP
254 DBUS_TYPE_INVALID))
255 goto oom;
256
a567261a 257 if (job_send_message(j, m) < 0)
c1e1601e
LP
258 goto oom;
259
260 free(p);
261 dbus_message_unref(m);
262
263 return;
264
265oom:
266 free(p);
267
268 if (m)
269 dbus_message_unref(m);
270
271 log_error("Failed to allocate job remove signal.");
272}