]>
Commit | Line | Data |
---|---|---|
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" |
bfebab7f | 27 | #include "dbus-common.h" |
ea430986 | 28 | |
4288f619 LP |
29 | #define BUS_JOB_INTERFACE \ |
30 | " <interface name=\"org.freedesktop.systemd1.Job\">\n" \ | |
31 | " <method name=\"Cancel\"/>\n" \ | |
4288f619 LP |
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" \ | |
36 | " </interface>\n" | |
37 | ||
38 | #define INTROSPECTION \ | |
39 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ | |
40 | "<node>\n" \ | |
41 | BUS_JOB_INTERFACE \ | |
42 | BUS_PROPERTIES_INTERFACE \ | |
c4e2ceae | 43 | BUS_PEER_INTERFACE \ |
4288f619 LP |
44 | BUS_INTROSPECTABLE_INTERFACE \ |
45 | "</node>\n" | |
46 | ||
9a60da28 | 47 | const char bus_job_interface[] _introspect_("Job") = BUS_JOB_INTERFACE; |
ea430986 | 48 | |
05feefe0 LP |
49 | #define INTERFACES_LIST \ |
50 | BUS_GENERIC_INTERFACES_LIST \ | |
51 | "org.freedesktop.systemd1.Job\0" | |
52 | ||
c4e2ceae | 53 | #define INVALIDATING_PROPERTIES \ |
34df5a34 | 54 | "State\0" |
c4e2ceae | 55 | |
4139c1b2 LP |
56 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState); |
57 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType); | |
86fbf370 | 58 | |
bfebab7f | 59 | static int bus_job_append_unit(DBusMessageIter *i, const char *property, void *data) { |
86fbf370 LP |
60 | Job *j = data; |
61 | DBusMessageIter sub; | |
62 | char *p; | |
86fbf370 | 63 | |
86fbf370 LP |
64 | assert(i); |
65 | assert(property); | |
66 | assert(j); | |
67 | ||
68 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
69 | return -ENOMEM; | |
70 | ||
71 | if (!(p = unit_dbus_path(j->unit))) | |
72 | return -ENOMEM; | |
73 | ||
9e2f7c11 | 74 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) || |
86fbf370 LP |
75 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
76 | free(p); | |
77 | return -ENOMEM; | |
78 | } | |
79 | ||
80 | free(p); | |
81 | ||
82 | if (!dbus_message_iter_close_container(i, &sub)) | |
83 | return -ENOMEM; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
5e8d1c9a | 88 | static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connection, DBusMessage *message) { |
86fbf370 | 89 | const BusProperty properties[] = { |
6f4706b7 LP |
90 | { "org.freedesktop.systemd1.Job", "Id", bus_property_append_uint32, "u", &j->id }, |
91 | { "org.freedesktop.systemd1.Job", "State", bus_job_append_state, "s", &j->state }, | |
92 | { "org.freedesktop.systemd1.Job", "JobType", bus_job_append_type, "s", &j->type }, | |
93 | { "org.freedesktop.systemd1.Job", "Unit", bus_job_append_unit, "(so)", j }, | |
86fbf370 LP |
94 | { NULL, NULL, NULL, NULL, NULL } |
95 | }; | |
96 | ||
b548631a | 97 | DBusMessage *reply = NULL; |
b548631a LP |
98 | |
99 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Job", "Cancel")) { | |
100 | if (!(reply = dbus_message_new_method_return(message))) | |
101 | goto oom; | |
102 | ||
c77bc38d | 103 | job_finish_and_invalidate(j, JOB_CANCELED); |
b548631a LP |
104 | |
105 | } else | |
bfebab7f | 106 | return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); |
b548631a LP |
107 | |
108 | if (reply) { | |
5e8d1c9a | 109 | if (!dbus_connection_send(connection, reply, NULL)) |
b548631a LP |
110 | goto oom; |
111 | ||
112 | dbus_message_unref(reply); | |
113 | } | |
114 | ||
115 | return DBUS_HANDLER_RESULT_HANDLED; | |
116 | ||
117 | oom: | |
118 | if (reply) | |
119 | dbus_message_unref(reply); | |
120 | ||
121 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
86fbf370 LP |
122 | } |
123 | ||
5e8d1c9a | 124 | static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { |
ea430986 | 125 | Manager *m = data; |
86fbf370 LP |
126 | Job *j; |
127 | int r; | |
2cccbca4 | 128 | DBusMessage *reply; |
ea430986 LP |
129 | |
130 | assert(connection); | |
131 | assert(message); | |
132 | assert(m); | |
133 | ||
2cccbca4 LP |
134 | if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/job")) { |
135 | /* Be nice to gdbus and return introspection data for our mid-level paths */ | |
136 | ||
137 | if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) { | |
138 | char *introspection = NULL; | |
139 | FILE *f; | |
140 | Iterator i; | |
141 | size_t size; | |
142 | ||
143 | if (!(reply = dbus_message_new_method_return(message))) | |
144 | goto oom; | |
145 | ||
146 | /* We roll our own introspection code here, instead of | |
147 | * relying on bus_default_message_handler() because we | |
148 | * need to generate our introspection string | |
149 | * dynamically. */ | |
150 | ||
151 | if (!(f = open_memstream(&introspection, &size))) | |
152 | goto oom; | |
153 | ||
154 | fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE | |
155 | "<node>\n", f); | |
156 | ||
157 | fputs(BUS_INTROSPECTABLE_INTERFACE, f); | |
158 | fputs(BUS_PEER_INTERFACE, f); | |
159 | ||
160 | HASHMAP_FOREACH(j, m->jobs, i) | |
161 | fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id); | |
162 | ||
163 | fputs("</node>\n", f); | |
164 | ||
165 | if (ferror(f)) { | |
166 | fclose(f); | |
167 | free(introspection); | |
168 | goto oom; | |
169 | } | |
170 | ||
171 | fclose(f); | |
172 | ||
173 | if (!introspection) | |
174 | goto oom; | |
175 | ||
176 | if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) { | |
177 | free(introspection); | |
178 | goto oom; | |
179 | } | |
180 | ||
181 | free(introspection); | |
182 | ||
183 | if (!dbus_connection_send(connection, reply, NULL)) | |
184 | goto oom; | |
185 | ||
186 | dbus_message_unref(reply); | |
187 | ||
188 | return DBUS_HANDLER_RESULT_HANDLED; | |
189 | } | |
190 | ||
191 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
192 | } | |
193 | ||
86fbf370 LP |
194 | if ((r = manager_get_job_from_dbus_path(m, dbus_message_get_path(message), &j)) < 0) { |
195 | ||
196 | if (r == -ENOMEM) | |
197 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
198 | ||
08672cb5 LP |
199 | if (r == -ENOENT) { |
200 | DBusError e; | |
b8a021c9 AB |
201 | |
202 | dbus_error_init(&e); | |
08672cb5 | 203 | dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown job"); |
bfebab7f | 204 | return bus_send_error_reply(connection, message, &e, r); |
08672cb5 | 205 | } |
86fbf370 | 206 | |
bfebab7f | 207 | return bus_send_error_reply(connection, message, NULL, r); |
86fbf370 LP |
208 | } |
209 | ||
5e8d1c9a | 210 | return bus_job_message_dispatch(j, connection, message); |
2cccbca4 LP |
211 | |
212 | oom: | |
213 | if (reply) | |
214 | dbus_message_unref(reply); | |
215 | ||
216 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
ea430986 LP |
217 | } |
218 | ||
219 | const DBusObjectPathVTable bus_job_vtable = { | |
220 | .message_function = bus_job_message_handler | |
221 | }; | |
c1e1601e | 222 | |
a567261a LP |
223 | static int job_send_message(Job *j, DBusMessage *m) { |
224 | int r; | |
225 | ||
226 | assert(j); | |
227 | assert(m); | |
228 | ||
229 | if (bus_has_subscriber(j->manager)) { | |
230 | if ((r = bus_broadcast(j->manager, m)) < 0) | |
231 | return r; | |
232 | ||
233 | } else if (j->bus_client) { | |
234 | /* If nobody is subscribed, we just send the message | |
235 | * to the client which created the job */ | |
236 | ||
237 | assert(j->bus); | |
238 | ||
239 | if (!dbus_message_set_destination(m, j->bus_client)) | |
240 | return -ENOMEM; | |
241 | ||
242 | if (!dbus_connection_send(j->bus, m, NULL)) | |
243 | return -ENOMEM; | |
244 | } | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
c1e1601e LP |
249 | void bus_job_send_change_signal(Job *j) { |
250 | char *p = NULL; | |
251 | DBusMessage *m = NULL; | |
252 | ||
253 | assert(j); | |
c1e1601e | 254 | |
c0bd0cf7 LP |
255 | if (j->in_dbus_queue) { |
256 | LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j); | |
257 | j->in_dbus_queue = false; | |
258 | } | |
c1e1601e | 259 | |
a567261a | 260 | if (!bus_has_subscriber(j->manager) && !j->bus_client) { |
94b6dfa2 | 261 | j->sent_dbus_new_signal = true; |
c1e1601e | 262 | return; |
94b6dfa2 | 263 | } |
c1e1601e LP |
264 | |
265 | if (!(p = job_dbus_path(j))) | |
266 | goto oom; | |
267 | ||
268 | if (j->sent_dbus_new_signal) { | |
c4e2ceae | 269 | /* Send a properties changed signal */ |
c1e1601e | 270 | |
c4e2ceae | 271 | if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Job", INVALIDATING_PROPERTIES))) |
c1e1601e | 272 | goto oom; |
c4e2ceae | 273 | |
c1e1601e LP |
274 | } else { |
275 | /* Send a new signal */ | |
276 | ||
701cc384 | 277 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew"))) |
c1e1601e LP |
278 | goto oom; |
279 | ||
280 | if (!dbus_message_append_args(m, | |
281 | DBUS_TYPE_UINT32, &j->id, | |
282 | DBUS_TYPE_OBJECT_PATH, &p, | |
283 | DBUS_TYPE_INVALID)) | |
284 | goto oom; | |
285 | } | |
286 | ||
a567261a | 287 | if (job_send_message(j, m) < 0) |
c1e1601e LP |
288 | goto oom; |
289 | ||
290 | free(p); | |
291 | dbus_message_unref(m); | |
292 | ||
293 | j->sent_dbus_new_signal = true; | |
294 | ||
295 | return; | |
296 | ||
297 | oom: | |
298 | free(p); | |
299 | ||
300 | if (m) | |
301 | dbus_message_unref(m); | |
302 | ||
303 | log_error("Failed to allocate job change signal."); | |
304 | } | |
305 | ||
5d44db4a | 306 | void bus_job_send_removed_signal(Job *j) { |
c1e1601e LP |
307 | char *p = NULL; |
308 | DBusMessage *m = NULL; | |
5d44db4a | 309 | const char *r; |
c1e1601e LP |
310 | |
311 | assert(j); | |
312 | ||
a567261a | 313 | if (!bus_has_subscriber(j->manager) && !j->bus_client) |
c1e1601e LP |
314 | return; |
315 | ||
7535cc78 LP |
316 | if (!j->sent_dbus_new_signal) |
317 | bus_job_send_change_signal(j); | |
318 | ||
c1e1601e LP |
319 | if (!(p = job_dbus_path(j))) |
320 | goto oom; | |
321 | ||
701cc384 | 322 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved"))) |
c1e1601e LP |
323 | goto oom; |
324 | ||
5d44db4a LP |
325 | r = job_result_to_string(j->result); |
326 | ||
c1e1601e LP |
327 | if (!dbus_message_append_args(m, |
328 | DBUS_TYPE_UINT32, &j->id, | |
329 | DBUS_TYPE_OBJECT_PATH, &p, | |
5d44db4a | 330 | DBUS_TYPE_STRING, &r, |
c1e1601e LP |
331 | DBUS_TYPE_INVALID)) |
332 | goto oom; | |
333 | ||
a567261a | 334 | if (job_send_message(j, m) < 0) |
c1e1601e LP |
335 | goto oom; |
336 | ||
337 | free(p); | |
338 | dbus_message_unref(m); | |
339 | ||
340 | return; | |
341 | ||
342 | oom: | |
343 | free(p); | |
344 | ||
345 | if (m) | |
346 | dbus_message_unref(m); | |
347 | ||
348 | log_error("Failed to allocate job remove signal."); | |
349 | } |