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