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