chiark / gitweb /
dbus: properly generate UnknownInterface, UnknownProperty and PropertyReadOnly errors
[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 INTERFACES_LIST                              \
49         BUS_GENERIC_INTERFACES_LIST                  \
50         "org.freedesktop.systemd1.Job\0"
51
52 #define INVALIDATING_PROPERTIES                 \
53         "State\0"
54
55 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_state, job_state, JobState);
56 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_job_append_type, job_type, JobType);
57
58 static int bus_job_append_unit(Manager *m, DBusMessageIter *i, const char *property, void *data) {
59         Job *j = data;
60         DBusMessageIter sub;
61         char *p;
62
63         assert(m);
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
74         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &j->unit->meta.id) ||
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
88 static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusConnection *connection, DBusMessage *message) {
89         const BusProperty properties[] = {
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         },
94                 { NULL, NULL, NULL, NULL, NULL }
95         };
96
97         DBusMessage *reply = NULL;
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
103                 job_finish_and_invalidate(j, JOB_CANCELED);
104
105         } else
106                 return bus_default_message_handler(j->manager, connection, message, INTROSPECTION, INTERFACES_LIST, properties);
107
108         if (reply) {
109                 if (!dbus_connection_send(connection, reply, NULL))
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;
122 }
123
124 static DBusHandlerResult bus_job_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
125         Manager *m = data;
126         Job *j;
127         int r;
128         DBusMessage *reply;
129
130         assert(connection);
131         assert(message);
132         assert(m);
133
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
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
199                 if (r == -ENOENT) {
200                         DBusError e;
201                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown job");
202                         return bus_send_error_reply(m, connection, message, &e, r);
203                 }
204
205                 return bus_send_error_reply(m, connection, message, NULL, r);
206         }
207
208         return bus_job_message_dispatch(j, connection, message);
209
210 oom:
211         if (reply)
212                 dbus_message_unref(reply);
213
214         return DBUS_HANDLER_RESULT_NEED_MEMORY;
215 }
216
217 const DBusObjectPathVTable bus_job_vtable = {
218         .message_function = bus_job_message_handler
219 };
220
221 static int job_send_message(Job *j, DBusMessage *m) {
222         int r;
223
224         assert(j);
225         assert(m);
226
227         if (bus_has_subscriber(j->manager)) {
228                 if ((r = bus_broadcast(j->manager, m)) < 0)
229                         return r;
230
231         } else  if (j->bus_client) {
232                 /* If nobody is subscribed, we just send the message
233                  * to the client which created the job */
234
235                 assert(j->bus);
236
237                 if (!dbus_message_set_destination(m, j->bus_client))
238                         return -ENOMEM;
239
240                 if (!dbus_connection_send(j->bus, m, NULL))
241                         return -ENOMEM;
242         }
243
244         return 0;
245 }
246
247 void bus_job_send_change_signal(Job *j) {
248         char *p = NULL;
249         DBusMessage *m = NULL;
250
251         assert(j);
252
253         if (j->in_dbus_queue) {
254                 LIST_REMOVE(Job, dbus_queue, j->manager->dbus_job_queue, j);
255                 j->in_dbus_queue = false;
256         }
257
258         if (!bus_has_subscriber(j->manager) && !j->bus_client) {
259                 j->sent_dbus_new_signal = true;
260                 return;
261         }
262
263         if (!(p = job_dbus_path(j)))
264                 goto oom;
265
266         if (j->sent_dbus_new_signal) {
267                 /* Send a properties changed signal */
268
269                 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Job", INVALIDATING_PROPERTIES)))
270                         goto oom;
271
272         } else {
273                 /* Send a new signal */
274
275                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
276                         goto oom;
277
278                 if (!dbus_message_append_args(m,
279                                               DBUS_TYPE_UINT32, &j->id,
280                                               DBUS_TYPE_OBJECT_PATH, &p,
281                                               DBUS_TYPE_INVALID))
282                         goto oom;
283         }
284
285         if (job_send_message(j, m) < 0)
286                 goto oom;
287
288         free(p);
289         dbus_message_unref(m);
290
291         j->sent_dbus_new_signal = true;
292
293         return;
294
295 oom:
296         free(p);
297
298         if (m)
299                 dbus_message_unref(m);
300
301         log_error("Failed to allocate job change signal.");
302 }
303
304 void bus_job_send_removed_signal(Job *j) {
305         char *p = NULL;
306         DBusMessage *m = NULL;
307         const char *r;
308
309         assert(j);
310
311         if (!bus_has_subscriber(j->manager) && !j->bus_client)
312                 return;
313
314         if (!j->sent_dbus_new_signal)
315                 bus_job_send_change_signal(j);
316
317         if (!(p = job_dbus_path(j)))
318                 goto oom;
319
320         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
321                 goto oom;
322
323         r = job_result_to_string(j->result);
324
325         if (!dbus_message_append_args(m,
326                                       DBUS_TYPE_UINT32, &j->id,
327                                       DBUS_TYPE_OBJECT_PATH, &p,
328                                       DBUS_TYPE_STRING, &r,
329                                       DBUS_TYPE_INVALID))
330                 goto oom;
331
332         if (job_send_message(j, m) < 0)
333                 goto oom;
334
335         free(p);
336         dbus_message_unref(m);
337
338         return;
339
340 oom:
341         free(p);
342
343         if (m)
344                 dbus_message_unref(m);
345
346         log_error("Failed to allocate job remove signal.");
347 }