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