chiark / gitweb /
ce34130993872bf018316a1d7003e45724e4a03e
[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                 !u->meta.only_by_dependency;
133
134         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
135                 return -ENOMEM;
136
137         return 0;
138 }
139
140 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
141         Unit *u = data;
142         dbus_bool_t b;
143
144         assert(m);
145         assert(i);
146         assert(property);
147         assert(u);
148
149         b = unit_can_reload(u);
150
151         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
152                 return -ENOMEM;
153
154         return 0;
155 }
156
157 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
158         Unit *u = data;
159         DBusMessageIter sub;
160         char *p;
161
162         assert(m);
163         assert(i);
164         assert(property);
165         assert(u);
166
167         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
168                 return -ENOMEM;
169
170         if (u->meta.job) {
171
172                 if (!(p = job_dbus_path(u->meta.job)))
173                         return -ENOMEM;
174
175                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
176                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
177                         free(p);
178                         return -ENOMEM;
179                 }
180         } else {
181                 uint32_t id = 0;
182
183                 /* No job, so let's fill in some placeholder
184                  * data. Since we need to fill in a valid path we
185                  * simple point to ourselves. */
186
187                 if (!(p = unit_dbus_path(u)))
188                         return -ENOMEM;
189
190                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
191                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
192                         free(p);
193                         return -ENOMEM;
194                 }
195         }
196
197         free(p);
198
199         if (!dbus_message_iter_close_container(i, &sub))
200                 return -ENOMEM;
201
202         return 0;
203 }
204
205 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
206         Unit *u = data;
207         char *t;
208         CGroupBonding *cgb;
209         bool success;
210
211         assert(m);
212         assert(i);
213         assert(property);
214         assert(u);
215
216         if ((cgb = unit_get_default_cgroup(u))) {
217                 if (!(t = cgroup_bonding_to_string(cgb)))
218                         return -ENOMEM;
219         } else
220                 t = (char*) "";
221
222         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
223
224         if (cgb)
225                 free(t);
226
227         return success ? 0 : -ENOMEM;
228 }
229
230 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
231         Unit *u = data;
232         CGroupBonding *cgb;
233         DBusMessageIter sub;
234
235         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
236                 return -ENOMEM;
237
238         LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
239                 char *t;
240                 bool success;
241
242                 if (!(t = cgroup_bonding_to_string(cgb)))
243                         return -ENOMEM;
244
245                 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
246                 free(t);
247
248                 if (!success)
249                         return -ENOMEM;
250         }
251
252         if (!dbus_message_iter_close_container(i, &sub))
253                 return -ENOMEM;
254
255         return 0;
256 }
257
258 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
259         Unit *u = data;
260         dbus_bool_t b;
261
262         assert(m);
263         assert(i);
264         assert(property);
265         assert(u);
266
267         b = unit_need_daemon_reload(u);
268
269         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
270                 return -ENOMEM;
271
272         return 0;
273 }
274
275 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
276         DBusMessage *reply = NULL;
277         Manager *m = u->meta.manager;
278         DBusError error;
279         JobType job_type = _JOB_TYPE_INVALID;
280         char *path = NULL;
281         bool reload_if_possible = false;
282
283         dbus_error_init(&error);
284
285         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
286                 job_type = JOB_START;
287         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
288                 job_type = JOB_STOP;
289         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
290                 job_type = JOB_RELOAD;
291         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
292                 job_type = JOB_RESTART;
293         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
294                 job_type = JOB_TRY_RESTART;
295         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
296                 reload_if_possible = true;
297                 job_type = JOB_RESTART;
298         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
299                 reload_if_possible = true;
300                 job_type = JOB_TRY_RESTART;
301         } else if (UNIT_VTABLE(u)->bus_message_handler)
302                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
303         else
304                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
305
306         if (job_type != _JOB_TYPE_INVALID) {
307                 const char *smode;
308                 JobMode mode;
309                 Job *j;
310                 int r;
311
312                 if (job_type == JOB_START && u->meta.only_by_dependency) {
313                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only.");
314                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
315                 }
316
317                 if (!dbus_message_get_args(
318                                     message,
319                                     &error,
320                                     DBUS_TYPE_STRING, &smode,
321                                     DBUS_TYPE_INVALID))
322                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
323
324                 if (reload_if_possible && unit_can_reload(u)) {
325                         if (job_type == JOB_RESTART)
326                                 job_type = JOB_RELOAD_OR_START;
327                         else if (job_type == JOB_TRY_RESTART)
328                                 job_type = JOB_RELOAD;
329                 }
330
331                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
332                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
333                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
334                 }
335
336                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
337                         return bus_send_error_reply(m, connection, message, &error, r);
338
339                 if (!(reply = dbus_message_new_method_return(message)))
340                         goto oom;
341
342                 if (!(path = job_dbus_path(j)))
343                         goto oom;
344
345                 if (!dbus_message_append_args(
346                                     reply,
347                                     DBUS_TYPE_OBJECT_PATH, &path,
348                                     DBUS_TYPE_INVALID))
349                         goto oom;
350         }
351
352         free(path);
353
354         if (reply) {
355                 if (!dbus_connection_send(connection, reply, NULL))
356                         goto oom;
357
358                 dbus_message_unref(reply);
359         }
360
361         return DBUS_HANDLER_RESULT_HANDLED;
362
363 oom:
364         free(path);
365
366         if (reply)
367                 dbus_message_unref(reply);
368
369         dbus_error_free(&error);
370
371         return DBUS_HANDLER_RESULT_NEED_MEMORY;
372 }
373
374 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
375         Manager *m = data;
376         Unit *u;
377         int r;
378
379         assert(connection);
380         assert(message);
381         assert(m);
382
383         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
384
385                 if (r == -ENOMEM)
386                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
387
388                 if (r == -ENOENT)
389                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
390
391                 return bus_send_error_reply(m, connection, message, NULL, r);
392         }
393
394         return bus_unit_message_dispatch(u, connection, message);
395 }
396
397 const DBusObjectPathVTable bus_unit_vtable = {
398         .message_function = bus_unit_message_handler
399 };
400
401 void bus_unit_send_change_signal(Unit *u) {
402         char *p = NULL;
403         DBusMessage *m = NULL;
404
405         assert(u);
406
407         if (u->meta.in_dbus_queue) {
408                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
409                 u->meta.in_dbus_queue = false;
410         }
411
412         if (!bus_has_subscriber(u->meta.manager)) {
413                 u->meta.sent_dbus_new_signal = true;
414                 return;
415         }
416
417         if (!(p = unit_dbus_path(u)))
418                 goto oom;
419
420         if (u->meta.sent_dbus_new_signal) {
421                 /* Send a change signal */
422
423                 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
424                         goto oom;
425         } else {
426                 /* Send a new signal */
427
428                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
429                         goto oom;
430
431                 if (!dbus_message_append_args(m,
432                                               DBUS_TYPE_STRING, &u->meta.id,
433                                               DBUS_TYPE_OBJECT_PATH, &p,
434                                               DBUS_TYPE_INVALID))
435                         goto oom;
436         }
437
438         if (bus_broadcast(u->meta.manager, m) < 0)
439                 goto oom;
440
441         free(p);
442         dbus_message_unref(m);
443
444         u->meta.sent_dbus_new_signal = true;
445
446         return;
447
448 oom:
449         free(p);
450
451         if (m)
452                 dbus_message_unref(m);
453
454         log_error("Failed to allocate unit change/new signal.");
455 }
456
457 void bus_unit_send_removed_signal(Unit *u) {
458         char *p = NULL;
459         DBusMessage *m = NULL;
460
461         assert(u);
462
463         if (!bus_has_subscriber(u->meta.manager))
464                 return;
465
466         if (!u->meta.sent_dbus_new_signal)
467                 bus_unit_send_change_signal(u);
468
469         if (!(p = unit_dbus_path(u)))
470                 goto oom;
471
472         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
473                 goto oom;
474
475         if (!dbus_message_append_args(m,
476                                       DBUS_TYPE_STRING, &u->meta.id,
477                                       DBUS_TYPE_OBJECT_PATH, &p,
478                                       DBUS_TYPE_INVALID))
479                 goto oom;
480
481         if (bus_broadcast(u->meta.manager, m) < 0)
482                 goto oom;
483
484         free(p);
485         dbus_message_unref(m);
486
487         return;
488
489 oom:
490         free(p);
491
492         if (m)
493                 dbus_message_unref(m);
494
495         log_error("Failed to allocate unit remove signal.");
496 }