]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus-manager.c
dbus: greatly extend dbus coverage
[people/ms/systemd.git] / dbus-manager.c
CommitLineData
ea430986
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
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
ea430986
LP
22#include <errno.h>
23
24#include "dbus.h"
25#include "log.h"
4139c1b2 26#include "dbus-manager.h"
ea430986
LP
27
28#define INTROSPECTION_BEGIN \
29 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
30 "<node>" \
4139c1b2 31 " <interface name=\"org.freedesktop.systemd1.Manager\">" \
ea430986
LP
32 " <method name=\"GetUnit\">" \
33 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
34 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
35 " </method>" \
36 " <method name=\"LoadUnit\">" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
39 " </method>" \
40 " <method name=\"GetJob\">" \
41 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
4ca5640b 42 " <arg name=\"job\" type=\"o\" direction=\"out\"/>" \
ea430986
LP
43 " </method>" \
44 " <method name=\"ClearJobs\"/>" \
45 " <method name=\"ListUnits\">" \
10a94420 46 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>" \
ea430986
LP
47 " </method>" \
48 " <method name=\"ListJobs\">" \
49 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
50 " </method>" \
c1e1601e
LP
51 " <method name=\"Subscribe\"/>" \
52 " <method name=\"Unsubscribe\"/>" \
b152adec 53 " <method name=\"Dump\"/>" \
4139c1b2
LP
54 " <method name=\"CreateSnapshot\">" \
55 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
56 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>" \
57 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
58 " </method>" \
c1e1601e
LP
59 " <signal name=\"UnitNew\">" \
60 " <arg name=\"id\" type=\"s\"/>" \
61 " <arg name=\"unit\" type=\"o\"/>" \
62 " </signal>" \
63 " <signal name=\"UnitRemoved\">" \
64 " <arg name=\"id\" type=\"s\"/>" \
65 " <arg name=\"unit\" type=\"o\"/>" \
66 " </signal>" \
67 " <signal name=\"JobNew\">" \
68 " <arg name=\"id\" type=\"u\"/>" \
69 " <arg name=\"job\" type=\"o\"/>" \
70 " </signal>" \
71 " <signal name=\"JobRemoved\">" \
72 " <arg name=\"id\" type=\"u\"/>" \
73 " <arg name=\"job\" type=\"o\"/>" \
74 " </signal>" \
1adf1049
LP
75 " <property name=\"Version\" type=\"s\" access=\"read\"/>" \
76 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>" \
77 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
78 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>" \
79 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>" \
ea430986
LP
80 " </interface>" \
81 BUS_PROPERTIES_INTERFACE \
82 BUS_INTROSPECTABLE_INTERFACE
83
84#define INTROSPECTION_END \
85 "</node>"
86
4139c1b2 87static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
1adf1049
LP
88
89static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
90 const char *t;
91
92 assert(m);
93 assert(i);
94 assert(property);
95
96 t = log_target_to_string(log_get_target());
97
98 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
99 return -ENOMEM;
100
101 return 0;
102}
103
104static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
105 const char *t;
106
107 assert(m);
108 assert(i);
109 assert(property);
110
111 t = log_level_to_string(log_get_max_level());
112
113 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
114 return -ENOMEM;
115
116 return 0;
117}
118
47be870b 119static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
ea430986 120 Manager *m = data;
1adf1049
LP
121
122 const BusProperty properties[] = {
4139c1b2
LP
123 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
124 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
125 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64, "t", &m->boot_timestamp },
126 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
127 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
1adf1049
LP
128 { NULL, NULL, NULL, NULL, NULL }
129 };
130
131 int r;
ea430986
LP
132 DBusError error;
133 DBusMessage *reply = NULL;
134 char * path = NULL;
135
136 assert(connection);
137 assert(message);
138 assert(m);
139
140 dbus_error_init(&error);
141
142 log_debug("Got D-Bus request: %s.%s() on %s",
143 dbus_message_get_interface(message),
144 dbus_message_get_member(message),
145 dbus_message_get_path(message));
146
4139c1b2 147 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
ea430986
LP
148 const char *name;
149 Unit *u;
150
151 if (!dbus_message_get_args(
152 message,
153 &error,
154 DBUS_TYPE_STRING, &name,
155 DBUS_TYPE_INVALID))
156 return bus_send_error_reply(m, message, &error, -EINVAL);
157
158 if (!(u = manager_get_unit(m, name)))
159 return bus_send_error_reply(m, message, NULL, -ENOENT);
160
161 if (!(reply = dbus_message_new_method_return(message)))
162 goto oom;
163
164 if (!(path = unit_dbus_path(u)))
165 goto oom;
166
167 if (!dbus_message_append_args(
168 reply,
169 DBUS_TYPE_OBJECT_PATH, &path,
170 DBUS_TYPE_INVALID))
171 goto oom;
172
4139c1b2 173 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
ea430986
LP
174 const char *name;
175 Unit *u;
176
177 if (!dbus_message_get_args(
178 message,
179 &error,
180 DBUS_TYPE_STRING, &name,
181 DBUS_TYPE_INVALID))
182 return bus_send_error_reply(m, message, &error, -EINVAL);
183
9e2f7c11 184 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
ea430986
LP
185 return bus_send_error_reply(m, message, NULL, r);
186
187 if (!(reply = dbus_message_new_method_return(message)))
188 goto oom;
189
190 if (!(path = unit_dbus_path(u)))
191 goto oom;
192
193 if (!dbus_message_append_args(
194 reply,
195 DBUS_TYPE_OBJECT_PATH, &path,
196 DBUS_TYPE_INVALID))
197 goto oom;
198
4ca5640b 199 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetJob")) {
ea430986
LP
200 uint32_t id;
201 Job *j;
202
203 if (!dbus_message_get_args(
204 message,
205 &error,
206 DBUS_TYPE_UINT32, &id,
207 DBUS_TYPE_INVALID))
208 return bus_send_error_reply(m, message, &error, -EINVAL);
209
210 if (!(j = manager_get_job(m, id)))
211 return bus_send_error_reply(m, message, NULL, -ENOENT);
212
213 if (!(reply = dbus_message_new_method_return(message)))
214 goto oom;
215
216 if (!(path = job_dbus_path(j)))
217 goto oom;
218
219 if (!dbus_message_append_args(
220 reply,
221 DBUS_TYPE_OBJECT_PATH, &path,
222 DBUS_TYPE_INVALID))
223 goto oom;
224
4139c1b2 225 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
ea430986
LP
226
227 manager_clear_jobs(m);
228
229 if (!(reply = dbus_message_new_method_return(message)))
230 goto oom;
231
4139c1b2 232 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
ea430986
LP
233 DBusMessageIter iter, sub;
234 Iterator i;
235 Unit *u;
236 const char *k;
237
238 if (!(reply = dbus_message_new_method_return(message)))
239 goto oom;
240
241 dbus_message_iter_init_append(reply, &iter);
242
10a94420 243 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
ea430986
LP
244 goto oom;
245
246 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
47be870b 247 char *u_path, *j_path;
9e2f7c11 248 const char *description, *load_state, *active_state, *sub_state, *job_type;
ea430986
LP
249 DBusMessageIter sub2;
250 uint32_t job_id;
251
9e2f7c11 252 if (k != u->meta.id)
ea430986
LP
253 continue;
254
255 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
256 goto oom;
257
258 description = unit_description(u);
259 load_state = unit_load_state_to_string(u->meta.load_state);
260 active_state = unit_active_state_to_string(unit_active_state(u));
10a94420 261 sub_state = unit_sub_state_to_string(u);
ea430986 262
47be870b 263 if (!(u_path = unit_dbus_path(u)))
ea430986
LP
264 goto oom;
265
266 if (u->meta.job) {
267 job_id = (uint32_t) u->meta.job->id;
268
47be870b
LP
269 if (!(j_path = job_dbus_path(u->meta.job))) {
270 free(u_path);
ea430986
LP
271 goto oom;
272 }
273
2b53c70b 274 job_type = job_type_to_string(u->meta.job->type);
ea430986
LP
275 } else {
276 job_id = 0;
47be870b 277 j_path = u_path;
ea430986
LP
278 job_type = "";
279 }
280
9e2f7c11 281 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
ea430986
LP
282 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
283 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
284 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
10a94420 285 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
47be870b 286 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
ea430986
LP
287 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
288 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
47be870b
LP
289 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
290 free(u_path);
ea430986 291 if (u->meta.job)
47be870b 292 free(j_path);
ea430986
LP
293 goto oom;
294 }
295
47be870b 296 free(u_path);
ea430986 297 if (u->meta.job)
47be870b 298 free(j_path);
ea430986
LP
299
300 if (!dbus_message_iter_close_container(&sub, &sub2))
301 goto oom;
302 }
303
304 if (!dbus_message_iter_close_container(&iter, &sub))
305 goto oom;
306
4139c1b2 307 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
ea430986
LP
308 DBusMessageIter iter, sub;
309 Iterator i;
310 Job *j;
311
312 if (!(reply = dbus_message_new_method_return(message)))
313 goto oom;
314
315 dbus_message_iter_init_append(reply, &iter);
316
317 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
318 goto oom;
319
320 HASHMAP_FOREACH(j, m->jobs, i) {
47be870b 321 char *u_path, *j_path;
9e2f7c11 322 const char *state, *type;
ea430986
LP
323 uint32_t id;
324 DBusMessageIter sub2;
325
326 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
327 goto oom;
328
329 id = (uint32_t) j->id;
ea430986 330 state = job_state_to_string(j->state);
2b53c70b 331 type = job_type_to_string(j->type);
ea430986 332
47be870b 333 if (!(j_path = job_dbus_path(j)))
ea430986
LP
334 goto oom;
335
47be870b
LP
336 if (!(u_path = unit_dbus_path(j->unit))) {
337 free(j_path);
ea430986
LP
338 goto oom;
339 }
340
341 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
9e2f7c11 342 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
ea430986
LP
343 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
344 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
47be870b
LP
345 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
346 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
347 free(j_path);
348 free(u_path);
ea430986
LP
349 goto oom;
350 }
351
47be870b
LP
352 free(j_path);
353 free(u_path);
ea430986
LP
354
355 if (!dbus_message_iter_close_container(&sub, &sub2))
356 goto oom;
357 }
358
359 if (!dbus_message_iter_close_container(&iter, &sub))
360 goto oom;
361
4139c1b2 362 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
c1e1601e
LP
363 char *client;
364
365 if (!(client = strdup(dbus_message_get_sender(message))))
366 goto oom;
367
368 r = set_put(m->subscribed, client);
369
370 if (r < 0)
371 return bus_send_error_reply(m, message, NULL, r);
372
373 if (!(reply = dbus_message_new_method_return(message)))
374 goto oom;
375
4139c1b2 376 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
c1e1601e
LP
377 char *client;
378
379 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
380 return bus_send_error_reply(m, message, NULL, -ENOENT);
381
382 free(client);
383
384 if (!(reply = dbus_message_new_method_return(message)))
385 goto oom;
386
4139c1b2 387 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
b152adec
LP
388 FILE *f;
389 char *dump = NULL;
390 size_t size;
391
392 if (!(reply = dbus_message_new_method_return(message)))
393 goto oom;
394
395 if (!(f = open_memstream(&dump, &size)))
396 goto oom;
397
398 manager_dump_units(m, f, NULL);
399 manager_dump_jobs(m, f, NULL);
400
401 if (ferror(f)) {
402 fclose(f);
403 free(dump);
404 goto oom;
405 }
406
407 fclose(f);
408
409 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
410 free(dump);
411 goto oom;
412 }
413
414 free(dump);
4139c1b2
LP
415 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
416 const char *name;
417 dbus_bool_t cleanup;
418 Snapshot *s;
419
420 if (!dbus_message_get_args(
421 message,
422 &error,
423 DBUS_TYPE_STRING, &name,
424 DBUS_TYPE_BOOLEAN, &cleanup,
425 DBUS_TYPE_INVALID))
426 return bus_send_error_reply(m, message, &error, -EINVAL);
427
428 if (name && name[0] == 0)
429 name = NULL;
430
431 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
432 return bus_send_error_reply(m, message, NULL, r);
433
434 if (!(reply = dbus_message_new_method_return(message)))
435 goto oom;
436
437 if (!(path = unit_dbus_path(UNIT(s))))
438 goto oom;
439
440 if (!dbus_message_append_args(
441 reply,
442 DBUS_TYPE_OBJECT_PATH, &path,
443 DBUS_TYPE_INVALID))
444 goto oom;
b152adec 445
ea430986
LP
446 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
447 char *introspection = NULL;
448 FILE *f;
449 Iterator i;
450 Unit *u;
451 Job *j;
452 const char *k;
453 size_t size;
454
455 if (!(reply = dbus_message_new_method_return(message)))
456 goto oom;
457
458 /* We roll our own introspection code here, instead of
459 * relying on bus_default_message_handler() because we
460 * need to generate our introspection string
461 * dynamically. */
462
463 if (!(f = open_memstream(&introspection, &size)))
464 goto oom;
465
466 fputs(INTROSPECTION_BEGIN, f);
467
468 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
469 char *p;
470
9e2f7c11 471 if (k != u->meta.id)
ea430986
LP
472 continue;
473
474 if (!(p = bus_path_escape(k))) {
475 fclose(f);
476 free(introspection);
477 goto oom;
478 }
479
480 fprintf(f, "<node name=\"unit/%s\"/>", p);
481 free(p);
482 }
483
484 HASHMAP_FOREACH(j, m->jobs, i)
485 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
486
487 fputs(INTROSPECTION_END, f);
488
489 if (ferror(f)) {
490 fclose(f);
491 free(introspection);
492 goto oom;
493 }
494
495 fclose(f);
496
497 if (!introspection)
498 goto oom;
499
500 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
501 free(introspection);
502 goto oom;
503 }
504
505 free(introspection);
506
507 } else
1adf1049 508 return bus_default_message_handler(m, message, NULL, properties);
ea430986
LP
509
510 free(path);
511
512 if (reply) {
513 if (!dbus_connection_send(connection, reply, NULL))
514 goto oom;
515
516 dbus_message_unref(reply);
517 }
518
519 return DBUS_HANDLER_RESULT_HANDLED;
520
521oom:
522 free(path);
523
524 if (reply)
525 dbus_message_unref(reply);
526
527 dbus_error_free(&error);
528
529 return DBUS_HANDLER_RESULT_NEED_MEMORY;
530}
531
532const DBusObjectPathVTable bus_manager_vtable = {
533 .message_function = bus_manager_message_handler
534};