chiark / gitweb /
systemctl: very very trivial typo patch :)
[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_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
209         Unit *u = data;
210         DBusMessageIter sub;
211         char *p;
212
213         assert(m);
214         assert(i);
215         assert(property);
216         assert(u);
217
218         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
219                 return -ENOMEM;
220
221         if (u->meta.job) {
222
223                 if (!(p = job_dbus_path(u->meta.job)))
224                         return -ENOMEM;
225
226                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
227                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
228                         free(p);
229                         return -ENOMEM;
230                 }
231         } else {
232                 uint32_t id = 0;
233
234                 /* No job, so let's fill in some placeholder
235                  * data. Since we need to fill in a valid path we
236                  * simple point to ourselves. */
237
238                 if (!(p = unit_dbus_path(u)))
239                         return -ENOMEM;
240
241                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
242                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
243                         free(p);
244                         return -ENOMEM;
245                 }
246         }
247
248         free(p);
249
250         if (!dbus_message_iter_close_container(i, &sub))
251                 return -ENOMEM;
252
253         return 0;
254 }
255
256 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
257         Unit *u = data;
258         char *t;
259         CGroupBonding *cgb;
260         bool success;
261
262         assert(m);
263         assert(i);
264         assert(property);
265         assert(u);
266
267         if ((cgb = unit_get_default_cgroup(u))) {
268                 if (!(t = cgroup_bonding_to_string(cgb)))
269                         return -ENOMEM;
270         } else
271                 t = (char*) "";
272
273         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
274
275         if (cgb)
276                 free(t);
277
278         return success ? 0 : -ENOMEM;
279 }
280
281 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
282         Unit *u = data;
283         CGroupBonding *cgb;
284         DBusMessageIter sub;
285
286         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
287                 return -ENOMEM;
288
289         LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
290                 char *t;
291                 bool success;
292
293                 if (!(t = cgroup_bonding_to_string(cgb)))
294                         return -ENOMEM;
295
296                 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
297                 free(t);
298
299                 if (!success)
300                         return -ENOMEM;
301         }
302
303         if (!dbus_message_iter_close_container(i, &sub))
304                 return -ENOMEM;
305
306         return 0;
307 }
308
309 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
310         Unit *u = data;
311         dbus_bool_t b;
312
313         assert(m);
314         assert(i);
315         assert(property);
316         assert(u);
317
318         b = unit_need_daemon_reload(u);
319
320         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
321                 return -ENOMEM;
322
323         return 0;
324 }
325
326 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
327         DBusMessage *reply = NULL;
328         Manager *m = u->meta.manager;
329         DBusError error;
330         JobType job_type = _JOB_TYPE_INVALID;
331         char *path = NULL;
332         bool reload_if_possible = false;
333
334         dbus_error_init(&error);
335
336         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
337                 job_type = JOB_START;
338         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
339                 job_type = JOB_STOP;
340         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
341                 job_type = JOB_RELOAD;
342         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
343                 job_type = JOB_RESTART;
344         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
345                 job_type = JOB_TRY_RESTART;
346         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
347                 reload_if_possible = true;
348                 job_type = JOB_RESTART;
349         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
350                 reload_if_possible = true;
351                 job_type = JOB_TRY_RESTART;
352         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetMaintenance")) {
353
354                 unit_reset_maintenance(u);
355
356                 if (!(reply = dbus_message_new_method_return(message)))
357                         goto oom;
358
359         } else if (UNIT_VTABLE(u)->bus_message_handler)
360                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
361         else
362                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
363
364         if (job_type != _JOB_TYPE_INVALID) {
365                 const char *smode;
366                 JobMode mode;
367                 Job *j;
368                 int r;
369
370                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
371                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
372                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
373                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
374                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
375                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
376                 }
377
378                 if (!dbus_message_get_args(
379                                     message,
380                                     &error,
381                                     DBUS_TYPE_STRING, &smode,
382                                     DBUS_TYPE_INVALID))
383                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
384
385                 if (reload_if_possible && unit_can_reload(u)) {
386                         if (job_type == JOB_RESTART)
387                                 job_type = JOB_RELOAD_OR_START;
388                         else if (job_type == JOB_TRY_RESTART)
389                                 job_type = JOB_RELOAD;
390                 }
391
392                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
393                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
394                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
395                 }
396
397                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
398                         return bus_send_error_reply(m, connection, message, &error, r);
399
400                 if (!(reply = dbus_message_new_method_return(message)))
401                         goto oom;
402
403                 if (!(path = job_dbus_path(j)))
404                         goto oom;
405
406                 if (!dbus_message_append_args(
407                                     reply,
408                                     DBUS_TYPE_OBJECT_PATH, &path,
409                                     DBUS_TYPE_INVALID))
410                         goto oom;
411         }
412
413         free(path);
414
415         if (reply) {
416                 if (!dbus_connection_send(connection, reply, NULL))
417                         goto oom;
418
419                 dbus_message_unref(reply);
420         }
421
422         return DBUS_HANDLER_RESULT_HANDLED;
423
424 oom:
425         free(path);
426
427         if (reply)
428                 dbus_message_unref(reply);
429
430         dbus_error_free(&error);
431
432         return DBUS_HANDLER_RESULT_NEED_MEMORY;
433 }
434
435 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
436         Manager *m = data;
437         Unit *u;
438         int r;
439
440         assert(connection);
441         assert(message);
442         assert(m);
443
444         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
445
446                 if (r == -ENOMEM)
447                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
448
449                 if (r == -ENOENT)
450                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
451
452                 return bus_send_error_reply(m, connection, message, NULL, r);
453         }
454
455         return bus_unit_message_dispatch(u, connection, message);
456 }
457
458 const DBusObjectPathVTable bus_unit_vtable = {
459         .message_function = bus_unit_message_handler
460 };
461
462 void bus_unit_send_change_signal(Unit *u) {
463         char *p = NULL;
464         DBusMessage *m = NULL;
465
466         assert(u);
467
468         if (u->meta.in_dbus_queue) {
469                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
470                 u->meta.in_dbus_queue = false;
471         }
472
473         if (!u->meta.id)
474                 return;
475
476         if (!bus_has_subscriber(u->meta.manager)) {
477                 u->meta.sent_dbus_new_signal = true;
478                 return;
479         }
480
481         if (!(p = unit_dbus_path(u)))
482                 goto oom;
483
484         if (u->meta.sent_dbus_new_signal) {
485                 /* Send a properties changed signal. First for the
486                  * specific type, then for the generic unit. The
487                  * clients may rely on this order to get atomic
488                  * behaviour if needed. */
489
490                 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
491
492                         if (!(m = bus_properties_changed_new(p,
493                                                              UNIT_VTABLE(u)->bus_interface,
494                                                              UNIT_VTABLE(u)->bus_invalidating_properties)))
495                                 goto oom;
496
497                         if (bus_broadcast(u->meta.manager, m) < 0)
498                                 goto oom;
499
500                         dbus_message_unref(m);
501                 }
502
503                 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
504                         goto oom;
505
506         } else {
507                 /* Send a new signal */
508
509                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
510                         goto oom;
511
512                 if (!dbus_message_append_args(m,
513                                               DBUS_TYPE_STRING, &u->meta.id,
514                                               DBUS_TYPE_OBJECT_PATH, &p,
515                                               DBUS_TYPE_INVALID))
516                         goto oom;
517         }
518
519         if (bus_broadcast(u->meta.manager, m) < 0)
520                 goto oom;
521
522         free(p);
523         dbus_message_unref(m);
524
525         u->meta.sent_dbus_new_signal = true;
526
527         return;
528
529 oom:
530         free(p);
531
532         if (m)
533                 dbus_message_unref(m);
534
535         log_error("Failed to allocate unit change/new signal.");
536 }
537
538 void bus_unit_send_removed_signal(Unit *u) {
539         char *p = NULL;
540         DBusMessage *m = NULL;
541
542         assert(u);
543
544         if (!bus_has_subscriber(u->meta.manager))
545                 return;
546
547         if (!u->meta.sent_dbus_new_signal)
548                 bus_unit_send_change_signal(u);
549
550         if (!u->meta.id)
551                 return;
552
553         if (!(p = unit_dbus_path(u)))
554                 goto oom;
555
556         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
557                 goto oom;
558
559         if (!dbus_message_append_args(m,
560                                       DBUS_TYPE_STRING, &u->meta.id,
561                                       DBUS_TYPE_OBJECT_PATH, &p,
562                                       DBUS_TYPE_INVALID))
563                 goto oom;
564
565         if (bus_broadcast(u->meta.manager, m) < 0)
566                 goto oom;
567
568         free(p);
569         dbus_message_unref(m);
570
571         return;
572
573 oom:
574         free(p);
575
576         if (m)
577                 dbus_message_unref(m);
578
579         log_error("Failed to allocate unit remove signal.");
580 }