]> git.ipfire.org Git - people/ms/systemd.git/blob - dbus-manager.c
dbus: send out signals when units/jobs come, go and change
[people/ms/systemd.git] / dbus-manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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
27 #define INTROSPECTION_BEGIN \
28 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
29 "<node>" \
30 " <interface name=\"org.freedesktop.systemd1\">" \
31 " <method name=\"GetUnit\">" \
32 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
33 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
34 " </method>" \
35 " <method name=\"LoadUnit\">" \
36 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
37 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
38 " </method>" \
39 " <method name=\"GetJob\">" \
40 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
41 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
42 " </method>" \
43 " <method name=\"ClearJobs\"/>" \
44 " <method name=\"ListUnits\">" \
45 " <arg name=\"units\" type=\"a(ssssouso)\" direction=\"out\"/>" \
46 " </method>" \
47 " <method name=\"ListJobs\">" \
48 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
49 " </method>" \
50 " <method name=\"Subscribe\"/>" \
51 " <method name=\"Unsubscribe\"/>" \
52 " <signal name=\"UnitNew\">" \
53 " <arg name=\"id\" type=\"s\"/>" \
54 " <arg name=\"unit\" type=\"o\"/>" \
55 " </signal>" \
56 " <signal name=\"UnitRemoved\">" \
57 " <arg name=\"id\" type=\"s\"/>" \
58 " <arg name=\"unit\" type=\"o\"/>" \
59 " </signal>" \
60 " <signal name=\"JobNew\">" \
61 " <arg name=\"id\" type=\"u\"/>" \
62 " <arg name=\"job\" type=\"o\"/>" \
63 " </signal>" \
64 " <signal name=\"JobRemoved\">" \
65 " <arg name=\"id\" type=\"u\"/>" \
66 " <arg name=\"job\" type=\"o\"/>" \
67 " </signal>" \
68 " </interface>" \
69 BUS_PROPERTIES_INTERFACE \
70 BUS_INTROSPECTABLE_INTERFACE
71
72 #define INTROSPECTION_END \
73 "</node>"
74
75 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
76 int r;
77 Manager *m = data;
78 DBusError error;
79 DBusMessage *reply = NULL;
80 char * path = NULL;
81
82 assert(connection);
83 assert(message);
84 assert(m);
85
86 dbus_error_init(&error);
87
88 log_debug("Got D-Bus request: %s.%s() on %s",
89 dbus_message_get_interface(message),
90 dbus_message_get_member(message),
91 dbus_message_get_path(message));
92
93 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetUnit")) {
94 const char *name;
95 Unit *u;
96
97 if (!dbus_message_get_args(
98 message,
99 &error,
100 DBUS_TYPE_STRING, &name,
101 DBUS_TYPE_INVALID))
102 return bus_send_error_reply(m, message, &error, -EINVAL);
103
104 if (!(u = manager_get_unit(m, name)))
105 return bus_send_error_reply(m, message, NULL, -ENOENT);
106
107 if (!(reply = dbus_message_new_method_return(message)))
108 goto oom;
109
110 if (!(path = unit_dbus_path(u)))
111 goto oom;
112
113 if (!dbus_message_append_args(
114 reply,
115 DBUS_TYPE_OBJECT_PATH, &path,
116 DBUS_TYPE_INVALID))
117 goto oom;
118
119 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "LoadUnit")) {
120 const char *name;
121 Unit *u;
122
123 if (!dbus_message_get_args(
124 message,
125 &error,
126 DBUS_TYPE_STRING, &name,
127 DBUS_TYPE_INVALID))
128 return bus_send_error_reply(m, message, &error, -EINVAL);
129
130 if ((r = manager_load_unit(m, name, &u)) < 0)
131 return bus_send_error_reply(m, message, NULL, r);
132
133 if (!(reply = dbus_message_new_method_return(message)))
134 goto oom;
135
136 if (!(path = unit_dbus_path(u)))
137 goto oom;
138
139 if (!dbus_message_append_args(
140 reply,
141 DBUS_TYPE_OBJECT_PATH, &path,
142 DBUS_TYPE_INVALID))
143 goto oom;
144
145 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetJob")) {
146 uint32_t id;
147 Job *j;
148
149 if (!dbus_message_get_args(
150 message,
151 &error,
152 DBUS_TYPE_UINT32, &id,
153 DBUS_TYPE_INVALID))
154 return bus_send_error_reply(m, message, &error, -EINVAL);
155
156 if (!(j = manager_get_job(m, id)))
157 return bus_send_error_reply(m, message, NULL, -ENOENT);
158
159 if (!(reply = dbus_message_new_method_return(message)))
160 goto oom;
161
162 if (!(path = job_dbus_path(j)))
163 goto oom;
164
165 if (!dbus_message_append_args(
166 reply,
167 DBUS_TYPE_OBJECT_PATH, &path,
168 DBUS_TYPE_INVALID))
169 goto oom;
170
171 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "ClearJobs")) {
172
173 manager_clear_jobs(m);
174
175 if (!(reply = dbus_message_new_method_return(message)))
176 goto oom;
177
178 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "ListUnits")) {
179 DBusMessageIter iter, sub;
180 Iterator i;
181 Unit *u;
182 const char *k;
183
184 if (!(reply = dbus_message_new_method_return(message)))
185 goto oom;
186
187 dbus_message_iter_init_append(reply, &iter);
188
189 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssouso)", &sub))
190 goto oom;
191
192 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
193 char *u_path, *j_path;
194 const char *id, *description, *load_state, *active_state, *job_type;
195 DBusMessageIter sub2;
196 uint32_t job_id;
197
198 id = unit_id(u);
199 if (k != id)
200 continue;
201
202 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
203 goto oom;
204
205 description = unit_description(u);
206 load_state = unit_load_state_to_string(u->meta.load_state);
207 active_state = unit_active_state_to_string(unit_active_state(u));
208
209 if (!(u_path = unit_dbus_path(u)))
210 goto oom;
211
212 if (u->meta.job) {
213 job_id = (uint32_t) u->meta.job->id;
214
215 if (!(j_path = job_dbus_path(u->meta.job))) {
216 free(u_path);
217 goto oom;
218 }
219
220 job_type = job_type_to_string(u->meta.job->type);
221 } else {
222 job_id = 0;
223 j_path = u_path;
224 job_type = "";
225 }
226
227 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &id) ||
228 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
229 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
230 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
231 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
232 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
233 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
234 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
235 free(u_path);
236 if (u->meta.job)
237 free(j_path);
238 goto oom;
239 }
240
241 free(u_path);
242 if (u->meta.job)
243 free(j_path);
244
245 if (!dbus_message_iter_close_container(&sub, &sub2))
246 goto oom;
247 }
248
249 if (!dbus_message_iter_close_container(&iter, &sub))
250 goto oom;
251
252 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "ListJobs")) {
253 DBusMessageIter iter, sub;
254 Iterator i;
255 Job *j;
256
257 if (!(reply = dbus_message_new_method_return(message)))
258 goto oom;
259
260 dbus_message_iter_init_append(reply, &iter);
261
262 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
263 goto oom;
264
265 HASHMAP_FOREACH(j, m->jobs, i) {
266 char *u_path, *j_path;
267 const char *unit, *state, *type;
268 uint32_t id;
269 DBusMessageIter sub2;
270
271 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
272 goto oom;
273
274 id = (uint32_t) j->id;
275 unit = unit_id(j->unit);
276 state = job_state_to_string(j->state);
277 type = job_type_to_string(j->type);
278
279 if (!(j_path = job_dbus_path(j)))
280 goto oom;
281
282 if (!(u_path = unit_dbus_path(j->unit))) {
283 free(j_path);
284 goto oom;
285 }
286
287 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
288 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &unit) ||
289 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
290 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
291 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
292 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
293 free(j_path);
294 free(u_path);
295 goto oom;
296 }
297
298 free(j_path);
299 free(u_path);
300
301 if (!dbus_message_iter_close_container(&sub, &sub2))
302 goto oom;
303 }
304
305 if (!dbus_message_iter_close_container(&iter, &sub))
306 goto oom;
307
308 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "Subscribe")) {
309 char *client;
310
311 if (!(client = strdup(dbus_message_get_sender(message))))
312 goto oom;
313
314 r = set_put(m->subscribed, client);
315
316 if (r < 0)
317 return bus_send_error_reply(m, message, NULL, r);
318
319 if (!(reply = dbus_message_new_method_return(message)))
320 goto oom;
321
322 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "Unsubscribe")) {
323 char *client;
324
325 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
326 return bus_send_error_reply(m, message, NULL, -ENOENT);
327
328 free(client);
329
330 if (!(reply = dbus_message_new_method_return(message)))
331 goto oom;
332
333 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
334 char *introspection = NULL;
335 FILE *f;
336 Iterator i;
337 Unit *u;
338 Job *j;
339 const char *k;
340 size_t size;
341
342 if (!(reply = dbus_message_new_method_return(message)))
343 goto oom;
344
345 /* We roll our own introspection code here, instead of
346 * relying on bus_default_message_handler() because we
347 * need to generate our introspection string
348 * dynamically. */
349
350 if (!(f = open_memstream(&introspection, &size)))
351 goto oom;
352
353 fputs(INTROSPECTION_BEGIN, f);
354
355 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
356 char *p;
357
358 if (k != unit_id(u))
359 continue;
360
361 if (!(p = bus_path_escape(k))) {
362 fclose(f);
363 free(introspection);
364 goto oom;
365 }
366
367 fprintf(f, "<node name=\"unit/%s\"/>", p);
368 free(p);
369 }
370
371 HASHMAP_FOREACH(j, m->jobs, i)
372 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
373
374 fputs(INTROSPECTION_END, f);
375
376 if (ferror(f)) {
377 fclose(f);
378 free(introspection);
379 goto oom;
380 }
381
382 fclose(f);
383
384 if (!introspection)
385 goto oom;
386
387 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
388 free(introspection);
389 goto oom;
390 }
391
392 free(introspection);
393
394 } else
395 return bus_default_message_handler(m, message, NULL, NULL);
396
397 free(path);
398
399 if (reply) {
400 if (!dbus_connection_send(connection, reply, NULL))
401 goto oom;
402
403 dbus_message_unref(reply);
404 }
405
406 return DBUS_HANDLER_RESULT_HANDLED;
407
408 oom:
409 free(path);
410
411 if (reply)
412 dbus_message_unref(reply);
413
414 dbus_error_free(&error);
415
416 return DBUS_HANDLER_RESULT_NEED_MEMORY;
417 }
418
419 const DBusObjectPathVTable bus_manager_vtable = {
420 .message_function = bus_manager_message_handler
421 };