chiark / gitweb /
b65481d19320645e085eab5e1c109fd6de157ae2
[elogind.git] / src / dbus-unit.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #define INVALIDATING_PROPERTIES                 \
32         "LoadState\0"                           \
33         "ActiveState\0"                         \
34         "SubState\0"                            \
35         "InactiveExitTimestamp\0"               \
36         "ActiveEnterTimestamp\0"                \
37         "ActiveExitTimestamp\0"                 \
38         "InactiveEnterTimestamp\0"              \
39         "Job\0"                                 \
40         "NeedDaemonReload\0"                    \
41         "\0"
42
43 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
44         char *t;
45         Iterator j;
46         DBusMessageIter sub;
47         Unit *u = data;
48
49         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
50                 return -ENOMEM;
51
52         SET_FOREACH(t, u->meta.names, j)
53                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
54                         return -ENOMEM;
55
56         if (!dbus_message_iter_close_container(i, &sub))
57                 return -ENOMEM;
58
59         return 0;
60 }
61
62 int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) {
63         Unit *u = data, *f;
64         const char *d;
65
66         assert(m);
67         assert(i);
68         assert(property);
69         assert(u);
70
71         f = unit_following(u);
72         d = f ? f->meta.id : "";
73
74         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
75                 return -ENOMEM;
76
77         return 0;
78 }
79
80 int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
81         Unit *u;
82         Iterator j;
83         DBusMessageIter sub;
84         Set *s = data;
85
86         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
87                 return -ENOMEM;
88
89         SET_FOREACH(u, s, j)
90                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
91                         return -ENOMEM;
92
93         if (!dbus_message_iter_close_container(i, &sub))
94                 return -ENOMEM;
95
96         return 0;
97 }
98
99 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
100         Unit *u = data;
101         const char *d;
102
103         assert(m);
104         assert(i);
105         assert(property);
106         assert(u);
107
108         d = unit_description(u);
109
110         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
111                 return -ENOMEM;
112
113         return 0;
114 }
115
116 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
117
118 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
119         Unit *u = data;
120         const char *state;
121
122         assert(m);
123         assert(i);
124         assert(property);
125         assert(u);
126
127         state = unit_active_state_to_string(unit_active_state(u));
128
129         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
130                 return -ENOMEM;
131
132         return 0;
133 }
134
135 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
136         Unit *u = data;
137         const char *state;
138
139         assert(m);
140         assert(i);
141         assert(property);
142         assert(u);
143
144         state = unit_sub_state_to_string(u);
145
146         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
147                 return -ENOMEM;
148
149         return 0;
150 }
151
152 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
153         Unit *u = data;
154         dbus_bool_t b;
155
156         assert(m);
157         assert(i);
158         assert(property);
159         assert(u);
160
161         b = unit_can_start(u) &&
162                 !u->meta.refuse_manual_start;
163
164         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
165                 return -ENOMEM;
166
167         return 0;
168 }
169
170 int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) {
171         Unit *u = data;
172         dbus_bool_t b;
173
174         assert(m);
175         assert(i);
176         assert(property);
177         assert(u);
178
179         /* On the lower levels we assume that every unit we can start
180          * we can also stop */
181
182         b = unit_can_start(u) &&
183                 !u->meta.refuse_manual_stop;
184
185         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
186                 return -ENOMEM;
187
188         return 0;
189 }
190
191 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
192         Unit *u = data;
193         dbus_bool_t b;
194
195         assert(m);
196         assert(i);
197         assert(property);
198         assert(u);
199
200         b = unit_can_reload(u);
201
202         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
203                 return -ENOMEM;
204
205         return 0;
206 }
207
208 int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data) {
209         Unit *u = data;
210         dbus_bool_t b;
211
212         assert(m);
213         assert(i);
214         assert(property);
215         assert(u);
216
217         b = unit_can_isolate(u) &&
218                 !u->meta.refuse_manual_start;
219
220         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
221                 return -ENOMEM;
222
223         return 0;
224 }
225
226 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
227         Unit *u = data;
228         DBusMessageIter sub;
229         char *p;
230
231         assert(m);
232         assert(i);
233         assert(property);
234         assert(u);
235
236         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
237                 return -ENOMEM;
238
239         if (u->meta.job) {
240
241                 if (!(p = job_dbus_path(u->meta.job)))
242                         return -ENOMEM;
243
244                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
245                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
246                         free(p);
247                         return -ENOMEM;
248                 }
249         } else {
250                 uint32_t id = 0;
251
252                 /* No job, so let's fill in some placeholder
253                  * data. Since we need to fill in a valid path we
254                  * simple point to ourselves. */
255
256                 if (!(p = unit_dbus_path(u)))
257                         return -ENOMEM;
258
259                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
260                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
261                         free(p);
262                         return -ENOMEM;
263                 }
264         }
265
266         free(p);
267
268         if (!dbus_message_iter_close_container(i, &sub))
269                 return -ENOMEM;
270
271         return 0;
272 }
273
274 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
275         Unit *u = data;
276         char *t;
277         CGroupBonding *cgb;
278         bool success;
279
280         assert(m);
281         assert(i);
282         assert(property);
283         assert(u);
284
285         if ((cgb = unit_get_default_cgroup(u))) {
286                 if (!(t = cgroup_bonding_to_string(cgb)))
287                         return -ENOMEM;
288         } else
289                 t = (char*) "";
290
291         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
292
293         if (cgb)
294                 free(t);
295
296         return success ? 0 : -ENOMEM;
297 }
298
299 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
300         Unit *u = data;
301         CGroupBonding *cgb;
302         DBusMessageIter sub;
303
304         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
305                 return -ENOMEM;
306
307         LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
308                 char *t;
309                 bool success;
310
311                 if (!(t = cgroup_bonding_to_string(cgb)))
312                         return -ENOMEM;
313
314                 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
315                 free(t);
316
317                 if (!success)
318                         return -ENOMEM;
319         }
320
321         if (!dbus_message_iter_close_container(i, &sub))
322                 return -ENOMEM;
323
324         return 0;
325 }
326
327 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
328         Unit *u = data;
329         dbus_bool_t b;
330
331         assert(m);
332         assert(i);
333         assert(property);
334         assert(u);
335
336         b = unit_need_daemon_reload(u);
337
338         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
339                 return -ENOMEM;
340
341         return 0;
342 }
343
344 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
345         DBusMessage *reply = NULL;
346         Manager *m = u->meta.manager;
347         DBusError error;
348         JobType job_type = _JOB_TYPE_INVALID;
349         char *path = NULL;
350         bool reload_if_possible = false;
351
352         dbus_error_init(&error);
353
354         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
355                 job_type = JOB_START;
356         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
357                 job_type = JOB_STOP;
358         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
359                 job_type = JOB_RELOAD;
360         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
361                 job_type = JOB_RESTART;
362         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
363                 job_type = JOB_TRY_RESTART;
364         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
365                 reload_if_possible = true;
366                 job_type = JOB_RESTART;
367         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
368                 reload_if_possible = true;
369                 job_type = JOB_TRY_RESTART;
370         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
371
372                 unit_reset_failed(u);
373
374                 if (!(reply = dbus_message_new_method_return(message)))
375                         goto oom;
376
377         } else if (UNIT_VTABLE(u)->bus_message_handler)
378                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
379         else
380                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
381
382         if (job_type != _JOB_TYPE_INVALID) {
383                 const char *smode;
384                 JobMode mode;
385                 Job *j;
386                 int r;
387
388                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
389                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
390                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
391                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
392                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
393                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
394                 }
395
396                 if (!dbus_message_get_args(
397                                     message,
398                                     &error,
399                                     DBUS_TYPE_STRING, &smode,
400                                     DBUS_TYPE_INVALID))
401                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
402
403                 if (reload_if_possible && unit_can_reload(u)) {
404                         if (job_type == JOB_RESTART)
405                                 job_type = JOB_RELOAD_OR_START;
406                         else if (job_type == JOB_TRY_RESTART)
407                                 job_type = JOB_RELOAD;
408                 }
409
410                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
411                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
412                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
413                 }
414
415                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
416                         return bus_send_error_reply(m, connection, message, &error, r);
417
418                 if (!(reply = dbus_message_new_method_return(message)))
419                         goto oom;
420
421                 if (!(path = job_dbus_path(j)))
422                         goto oom;
423
424                 if (!dbus_message_append_args(
425                                     reply,
426                                     DBUS_TYPE_OBJECT_PATH, &path,
427                                     DBUS_TYPE_INVALID))
428                         goto oom;
429         }
430
431         free(path);
432
433         if (reply) {
434                 if (!dbus_connection_send(connection, reply, NULL))
435                         goto oom;
436
437                 dbus_message_unref(reply);
438         }
439
440         return DBUS_HANDLER_RESULT_HANDLED;
441
442 oom:
443         free(path);
444
445         if (reply)
446                 dbus_message_unref(reply);
447
448         dbus_error_free(&error);
449
450         return DBUS_HANDLER_RESULT_NEED_MEMORY;
451 }
452
453 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
454         Manager *m = data;
455         Unit *u;
456         int r;
457
458         assert(connection);
459         assert(message);
460         assert(m);
461
462         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
463
464                 if (r == -ENOMEM)
465                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
466
467                 if (r == -ENOENT)
468                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
469
470                 return bus_send_error_reply(m, connection, message, NULL, r);
471         }
472
473         return bus_unit_message_dispatch(u, connection, message);
474 }
475
476 const DBusObjectPathVTable bus_unit_vtable = {
477         .message_function = bus_unit_message_handler
478 };
479
480 void bus_unit_send_change_signal(Unit *u) {
481         char *p = NULL;
482         DBusMessage *m = NULL;
483
484         assert(u);
485
486         if (u->meta.in_dbus_queue) {
487                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
488                 u->meta.in_dbus_queue = false;
489         }
490
491         if (!u->meta.id)
492                 return;
493
494         if (!bus_has_subscriber(u->meta.manager)) {
495                 u->meta.sent_dbus_new_signal = true;
496                 return;
497         }
498
499         if (!(p = unit_dbus_path(u)))
500                 goto oom;
501
502         if (u->meta.sent_dbus_new_signal) {
503                 /* Send a properties changed signal. First for the
504                  * specific type, then for the generic unit. The
505                  * clients may rely on this order to get atomic
506                  * behaviour if needed. */
507
508                 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
509
510                         if (!(m = bus_properties_changed_new(p,
511                                                              UNIT_VTABLE(u)->bus_interface,
512                                                              UNIT_VTABLE(u)->bus_invalidating_properties)))
513                                 goto oom;
514
515                         if (bus_broadcast(u->meta.manager, m) < 0)
516                                 goto oom;
517
518                         dbus_message_unref(m);
519                 }
520
521                 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
522                         goto oom;
523
524         } else {
525                 /* Send a new signal */
526
527                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
528                         goto oom;
529
530                 if (!dbus_message_append_args(m,
531                                               DBUS_TYPE_STRING, &u->meta.id,
532                                               DBUS_TYPE_OBJECT_PATH, &p,
533                                               DBUS_TYPE_INVALID))
534                         goto oom;
535         }
536
537         if (bus_broadcast(u->meta.manager, m) < 0)
538                 goto oom;
539
540         free(p);
541         dbus_message_unref(m);
542
543         u->meta.sent_dbus_new_signal = true;
544
545         return;
546
547 oom:
548         free(p);
549
550         if (m)
551                 dbus_message_unref(m);
552
553         log_error("Failed to allocate unit change/new signal.");
554 }
555
556 void bus_unit_send_removed_signal(Unit *u) {
557         char *p = NULL;
558         DBusMessage *m = NULL;
559
560         assert(u);
561
562         if (!bus_has_subscriber(u->meta.manager))
563                 return;
564
565         if (!u->meta.sent_dbus_new_signal)
566                 bus_unit_send_change_signal(u);
567
568         if (!u->meta.id)
569                 return;
570
571         if (!(p = unit_dbus_path(u)))
572                 goto oom;
573
574         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
575                 goto oom;
576
577         if (!dbus_message_append_args(m,
578                                       DBUS_TYPE_STRING, &u->meta.id,
579                                       DBUS_TYPE_OBJECT_PATH, &p,
580                                       DBUS_TYPE_INVALID))
581                 goto oom;
582
583         if (bus_broadcast(u->meta.manager, m) < 0)
584                 goto oom;
585
586         free(p);
587         dbus_message_unref(m);
588
589         return;
590
591 oom:
592         free(p);
593
594         if (m)
595                 dbus_message_unref(m);
596
597         log_error("Failed to allocate unit remove signal.");
598 }