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