chiark / gitweb /
systemctl: add to command for virtualizing the dependency tree with graphviz
[elogind.git] / src / dbus-unit.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 #include "dbus-unit.h"
27 #include "bus-errors.h"
28
29 const char bus_unit_interface[] = BUS_UNIT_INTERFACE;
30
31 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
32         char *t;
33         Iterator j;
34         DBusMessageIter sub;
35         Unit *u = data;
36
37         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
38                 return -ENOMEM;
39
40         SET_FOREACH(t, u->meta.names, j)
41                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
42                         return -ENOMEM;
43
44         if (!dbus_message_iter_close_container(i, &sub))
45                 return -ENOMEM;
46
47         return 0;
48 }
49
50 int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
51         Unit *u;
52         Iterator j;
53         DBusMessageIter sub;
54         Set *s = data;
55
56         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
57                 return -ENOMEM;
58
59         SET_FOREACH(u, s, j)
60                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
61                         return -ENOMEM;
62
63         if (!dbus_message_iter_close_container(i, &sub))
64                 return -ENOMEM;
65
66         return 0;
67 }
68
69 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
70         Unit *u = data;
71         const char *d;
72
73         assert(m);
74         assert(i);
75         assert(property);
76         assert(u);
77
78         d = unit_description(u);
79
80         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
81                 return -ENOMEM;
82
83         return 0;
84 }
85
86 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
87
88 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
89         Unit *u = data;
90         const char *state;
91
92         assert(m);
93         assert(i);
94         assert(property);
95         assert(u);
96
97         state = unit_active_state_to_string(unit_active_state(u));
98
99         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
100                 return -ENOMEM;
101
102         return 0;
103 }
104
105 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
106         Unit *u = data;
107         const char *state;
108
109         assert(m);
110         assert(i);
111         assert(property);
112         assert(u);
113
114         state = unit_sub_state_to_string(u);
115
116         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
117                 return -ENOMEM;
118
119         return 0;
120 }
121
122 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
123         Unit *u = data;
124         dbus_bool_t b;
125
126         assert(m);
127         assert(i);
128         assert(property);
129         assert(u);
130
131         b = unit_can_start(u);
132
133         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
134                 return -ENOMEM;
135
136         return 0;
137 }
138
139 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
140         Unit *u = data;
141         dbus_bool_t b;
142
143         assert(m);
144         assert(i);
145         assert(property);
146         assert(u);
147
148         b = unit_can_reload(u);
149
150         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
151                 return -ENOMEM;
152
153         return 0;
154 }
155
156 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
157         Unit *u = data;
158         DBusMessageIter sub;
159         char *p;
160
161         assert(m);
162         assert(i);
163         assert(property);
164         assert(u);
165
166         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
167                 return -ENOMEM;
168
169         if (u->meta.job) {
170
171                 if (!(p = job_dbus_path(u->meta.job)))
172                         return -ENOMEM;
173
174                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
175                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
176                         free(p);
177                         return -ENOMEM;
178                 }
179         } else {
180                 uint32_t id = 0;
181
182                 /* No job, so let's fill in some placeholder
183                  * data. Since we need to fill in a valid path we
184                  * simple point to ourselves. */
185
186                 if (!(p = unit_dbus_path(u)))
187                         return -ENOMEM;
188
189                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
190                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
191                         free(p);
192                         return -ENOMEM;
193                 }
194         }
195
196         free(p);
197
198         if (!dbus_message_iter_close_container(i, &sub))
199                 return -ENOMEM;
200
201         return 0;
202 }
203
204 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
205         Unit *u = data;
206         char *t;
207         CGroupBonding *cgb;
208         bool success;
209
210         assert(m);
211         assert(i);
212         assert(property);
213         assert(u);
214
215         if ((cgb = unit_get_default_cgroup(u))) {
216                 if (!(t = cgroup_bonding_to_string(cgb)))
217                         return -ENOMEM;
218         } else
219                 t = (char*) "";
220
221         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
222
223         if (cgb)
224                 free(t);
225
226         return success ? 0 : -ENOMEM;
227 }
228
229 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
230         Unit *u = data;
231         CGroupBonding *cgb;
232         DBusMessageIter sub;
233
234         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
235                 return -ENOMEM;
236
237         LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
238                 char *t;
239                 bool success;
240
241                 if (!(t = cgroup_bonding_to_string(cgb)))
242                         return -ENOMEM;
243
244                 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
245                 free(t);
246
247                 if (!success)
248                         return -ENOMEM;
249         }
250
251         if (!dbus_message_iter_close_container(i, &sub))
252                 return -ENOMEM;
253
254         return 0;
255 }
256
257 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
258         DBusMessage *reply = NULL;
259         Manager *m = u->meta.manager;
260         DBusError error;
261         JobType job_type = _JOB_TYPE_INVALID;
262         char *path = NULL;
263         bool reload_if_possible = false;
264
265         dbus_error_init(&error);
266
267         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
268                 job_type = JOB_START;
269         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
270                 job_type = JOB_STOP;
271         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
272                 job_type = JOB_RELOAD;
273         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
274                 job_type = JOB_RESTART;
275         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
276                 job_type = JOB_TRY_RESTART;
277         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
278                 reload_if_possible = true;
279                 job_type = JOB_RESTART;
280         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
281                 reload_if_possible = true;
282                 job_type = JOB_TRY_RESTART;
283         } else if (UNIT_VTABLE(u)->bus_message_handler)
284                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
285         else
286                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
287
288         if (job_type != _JOB_TYPE_INVALID) {
289                 const char *smode;
290                 JobMode mode;
291                 Job *j;
292                 int r;
293
294                 if (job_type == JOB_START && u->meta.only_by_dependency) {
295                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only.");
296                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
297                 }
298
299                 if (!dbus_message_get_args(
300                                     message,
301                                     &error,
302                                     DBUS_TYPE_STRING, &smode,
303                                     DBUS_TYPE_INVALID))
304                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
305
306                 if (reload_if_possible && unit_can_reload(u)) {
307                         if (job_type == JOB_RESTART)
308                                 job_type = JOB_RELOAD_OR_START;
309                         else if (job_type == JOB_TRY_RESTART)
310                                 job_type = JOB_RELOAD;
311                 }
312
313                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
314                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
315                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
316                 }
317
318                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
319                         return bus_send_error_reply(m, connection, message, &error, r);
320
321                 if (!(reply = dbus_message_new_method_return(message)))
322                         goto oom;
323
324                 if (!(path = job_dbus_path(j)))
325                         goto oom;
326
327                 if (!dbus_message_append_args(
328                                     reply,
329                                     DBUS_TYPE_OBJECT_PATH, &path,
330                                     DBUS_TYPE_INVALID))
331                         goto oom;
332         }
333
334         free(path);
335
336         if (reply) {
337                 if (!dbus_connection_send(connection, reply, NULL))
338                         goto oom;
339
340                 dbus_message_unref(reply);
341         }
342
343         return DBUS_HANDLER_RESULT_HANDLED;
344
345 oom:
346         free(path);
347
348         if (reply)
349                 dbus_message_unref(reply);
350
351         dbus_error_free(&error);
352
353         return DBUS_HANDLER_RESULT_NEED_MEMORY;
354 }
355
356 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
357         Manager *m = data;
358         Unit *u;
359         int r;
360
361         assert(connection);
362         assert(message);
363         assert(m);
364
365         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
366
367                 if (r == -ENOMEM)
368                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
369
370                 if (r == -ENOENT)
371                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
372
373                 return bus_send_error_reply(m, connection, message, NULL, r);
374         }
375
376         return bus_unit_message_dispatch(u, connection, message);
377 }
378
379 const DBusObjectPathVTable bus_unit_vtable = {
380         .message_function = bus_unit_message_handler
381 };
382
383 void bus_unit_send_change_signal(Unit *u) {
384         char *p = NULL;
385         DBusMessage *m = NULL;
386
387         assert(u);
388
389         if (u->meta.in_dbus_queue) {
390                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
391                 u->meta.in_dbus_queue = false;
392         }
393
394         if (!bus_has_subscriber(u->meta.manager)) {
395                 u->meta.sent_dbus_new_signal = true;
396                 return;
397         }
398
399         if (!(p = unit_dbus_path(u)))
400                 goto oom;
401
402         if (u->meta.sent_dbus_new_signal) {
403                 /* Send a change signal */
404
405                 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
406                         goto oom;
407         } else {
408                 /* Send a new signal */
409
410                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
411                         goto oom;
412
413                 if (!dbus_message_append_args(m,
414                                               DBUS_TYPE_STRING, &u->meta.id,
415                                               DBUS_TYPE_OBJECT_PATH, &p,
416                                               DBUS_TYPE_INVALID))
417                         goto oom;
418         }
419
420         if (bus_broadcast(u->meta.manager, m) < 0)
421                 goto oom;
422
423         free(p);
424         dbus_message_unref(m);
425
426         u->meta.sent_dbus_new_signal = true;
427
428         return;
429
430 oom:
431         free(p);
432
433         if (m)
434                 dbus_message_unref(m);
435
436         log_error("Failed to allocate unit change/new signal.");
437 }
438
439 void bus_unit_send_removed_signal(Unit *u) {
440         char *p = NULL;
441         DBusMessage *m = NULL;
442
443         assert(u);
444
445         if (!bus_has_subscriber(u->meta.manager))
446                 return;
447
448         if (!u->meta.sent_dbus_new_signal)
449                 bus_unit_send_change_signal(u);
450
451         if (!(p = unit_dbus_path(u)))
452                 goto oom;
453
454         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
455                 goto oom;
456
457         if (!dbus_message_append_args(m,
458                                       DBUS_TYPE_STRING, &u->meta.id,
459                                       DBUS_TYPE_OBJECT_PATH, &p,
460                                       DBUS_TYPE_INVALID))
461                 goto oom;
462
463         if (bus_broadcast(u->meta.manager, m) < 0)
464                 goto oom;
465
466         free(p);
467         dbus_message_unref(m);
468
469         return;
470
471 oom:
472         free(p);
473
474         if (m)
475                 dbus_message_unref(m);
476
477         log_error("Failed to allocate unit remove signal.");
478 }