chiark / gitweb /
bash: fix typo
[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[] _introspect_("Unit") = 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
42 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
43         char *t;
44         Iterator j;
45         DBusMessageIter sub;
46         Unit *u = data;
47
48         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
49                 return -ENOMEM;
50
51         SET_FOREACH(t, u->meta.names, j)
52                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
53                         return -ENOMEM;
54
55         if (!dbus_message_iter_close_container(i, &sub))
56                 return -ENOMEM;
57
58         return 0;
59 }
60
61 int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) {
62         Unit *u = data, *f;
63         const char *d;
64
65         assert(m);
66         assert(i);
67         assert(property);
68         assert(u);
69
70         f = unit_following(u);
71         d = f ? f->meta.id : "";
72
73         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
74                 return -ENOMEM;
75
76         return 0;
77 }
78
79 int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
80         Unit *u;
81         Iterator j;
82         DBusMessageIter sub;
83         Set *s = data;
84
85         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
86                 return -ENOMEM;
87
88         SET_FOREACH(u, s, j)
89                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
90                         return -ENOMEM;
91
92         if (!dbus_message_iter_close_container(i, &sub))
93                 return -ENOMEM;
94
95         return 0;
96 }
97
98 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
99         Unit *u = data;
100         const char *d;
101
102         assert(m);
103         assert(i);
104         assert(property);
105         assert(u);
106
107         d = unit_description(u);
108
109         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
110                 return -ENOMEM;
111
112         return 0;
113 }
114
115 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
116
117 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
118         Unit *u = data;
119         const char *state;
120
121         assert(m);
122         assert(i);
123         assert(property);
124         assert(u);
125
126         state = unit_active_state_to_string(unit_active_state(u));
127
128         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
129                 return -ENOMEM;
130
131         return 0;
132 }
133
134 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
135         Unit *u = data;
136         const char *state;
137
138         assert(m);
139         assert(i);
140         assert(property);
141         assert(u);
142
143         state = unit_sub_state_to_string(u);
144
145         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
146                 return -ENOMEM;
147
148         return 0;
149 }
150
151 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
152         Unit *u = data;
153         dbus_bool_t b;
154
155         assert(m);
156         assert(i);
157         assert(property);
158         assert(u);
159
160         b = unit_can_start(u) &&
161                 !u->meta.refuse_manual_start;
162
163         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
164                 return -ENOMEM;
165
166         return 0;
167 }
168
169 int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) {
170         Unit *u = data;
171         dbus_bool_t b;
172
173         assert(m);
174         assert(i);
175         assert(property);
176         assert(u);
177
178         /* On the lower levels we assume that every unit we can start
179          * we can also stop */
180
181         b = unit_can_start(u) &&
182                 !u->meta.refuse_manual_stop;
183
184         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
185                 return -ENOMEM;
186
187         return 0;
188 }
189
190 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
191         Unit *u = data;
192         dbus_bool_t b;
193
194         assert(m);
195         assert(i);
196         assert(property);
197         assert(u);
198
199         b = unit_can_reload(u);
200
201         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
202                 return -ENOMEM;
203
204         return 0;
205 }
206
207 int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data) {
208         Unit *u = data;
209         dbus_bool_t b;
210
211         assert(m);
212         assert(i);
213         assert(property);
214         assert(u);
215
216         b = unit_can_isolate(u) &&
217                 !u->meta.refuse_manual_start;
218
219         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
220                 return -ENOMEM;
221
222         return 0;
223 }
224
225 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
226         Unit *u = data;
227         DBusMessageIter sub;
228         char *p;
229
230         assert(m);
231         assert(i);
232         assert(property);
233         assert(u);
234
235         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
236                 return -ENOMEM;
237
238         if (u->meta.job) {
239
240                 if (!(p = job_dbus_path(u->meta.job)))
241                         return -ENOMEM;
242
243                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
244                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
245                         free(p);
246                         return -ENOMEM;
247                 }
248         } else {
249                 uint32_t id = 0;
250
251                 /* No job, so let's fill in some placeholder
252                  * data. Since we need to fill in a valid path we
253                  * simple point to ourselves. */
254
255                 if (!(p = unit_dbus_path(u)))
256                         return -ENOMEM;
257
258                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
259                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
260                         free(p);
261                         return -ENOMEM;
262                 }
263         }
264
265         free(p);
266
267         if (!dbus_message_iter_close_container(i, &sub))
268                 return -ENOMEM;
269
270         return 0;
271 }
272
273 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
274         Unit *u = data;
275         char *t;
276         CGroupBonding *cgb;
277         bool success;
278
279         assert(m);
280         assert(i);
281         assert(property);
282         assert(u);
283
284         if ((cgb = unit_get_default_cgroup(u))) {
285                 if (!(t = cgroup_bonding_to_string(cgb)))
286                         return -ENOMEM;
287         } else
288                 t = (char*) "";
289
290         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
291
292         if (cgb)
293                 free(t);
294
295         return success ? 0 : -ENOMEM;
296 }
297
298 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
299         Unit *u = data;
300         CGroupBonding *cgb;
301         DBusMessageIter sub;
302
303         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
304                 return -ENOMEM;
305
306         LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
307                 char *t;
308                 bool success;
309
310                 if (!(t = cgroup_bonding_to_string(cgb)))
311                         return -ENOMEM;
312
313                 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
314                 free(t);
315
316                 if (!success)
317                         return -ENOMEM;
318         }
319
320         if (!dbus_message_iter_close_container(i, &sub))
321                 return -ENOMEM;
322
323         return 0;
324 }
325
326 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
327         Unit *u = data;
328         dbus_bool_t b;
329
330         assert(m);
331         assert(i);
332         assert(property);
333         assert(u);
334
335         b = unit_need_daemon_reload(u);
336
337         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
338                 return -ENOMEM;
339
340         return 0;
341 }
342
343 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
344         DBusMessage *reply = NULL;
345         Manager *m = u->meta.manager;
346         DBusError error;
347         JobType job_type = _JOB_TYPE_INVALID;
348         char *path = NULL;
349         bool reload_if_possible = false;
350
351         dbus_error_init(&error);
352
353         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
354                 job_type = JOB_START;
355         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
356                 job_type = JOB_STOP;
357         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
358                 job_type = JOB_RELOAD;
359         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
360                 job_type = JOB_RESTART;
361         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
362                 job_type = JOB_TRY_RESTART;
363         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
364                 reload_if_possible = true;
365                 job_type = JOB_RESTART;
366         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
367                 reload_if_possible = true;
368                 job_type = JOB_TRY_RESTART;
369         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Kill")) {
370                 const char *swho, *smode;
371                 int32_t signo;
372                 KillMode mode;
373                 KillWho who;
374                 int r;
375
376                 if (!dbus_message_get_args(
377                                     message,
378                                     &error,
379                                     DBUS_TYPE_STRING, &swho,
380                                     DBUS_TYPE_STRING, &smode,
381                                     DBUS_TYPE_INT32, &signo,
382                                     DBUS_TYPE_INVALID))
383                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
384
385                 if ((mode = kill_mode_from_string(smode)) < 0 ||
386                     (who = kill_who_from_string(swho)) < 0 ||
387                     signo <= 0 ||
388                     signo >= _NSIG)
389                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
390
391                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
392                         return bus_send_error_reply(m, connection, message, &error, r);
393
394                 if (!(reply = dbus_message_new_method_return(message)))
395                         goto oom;
396
397         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
398
399                 unit_reset_failed(u);
400
401                 if (!(reply = dbus_message_new_method_return(message)))
402                         goto oom;
403
404         } else if (UNIT_VTABLE(u)->bus_message_handler)
405                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
406         else
407                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
408
409         if (job_type != _JOB_TYPE_INVALID) {
410                 const char *smode;
411                 JobMode mode;
412                 Job *j;
413                 int r;
414
415                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
416                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
417                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
418                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
419                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
420                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
421                 }
422
423                 if (!dbus_message_get_args(
424                                     message,
425                                     &error,
426                                     DBUS_TYPE_STRING, &smode,
427                                     DBUS_TYPE_INVALID))
428                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
429
430                 if (reload_if_possible && unit_can_reload(u)) {
431                         if (job_type == JOB_RESTART)
432                                 job_type = JOB_RELOAD_OR_START;
433                         else if (job_type == JOB_TRY_RESTART)
434                                 job_type = JOB_RELOAD;
435                 }
436
437                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
438                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
439                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
440                 }
441
442                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
443                         return bus_send_error_reply(m, connection, message, &error, r);
444
445                 if (!(reply = dbus_message_new_method_return(message)))
446                         goto oom;
447
448                 if (!(path = job_dbus_path(j)))
449                         goto oom;
450
451                 if (!dbus_message_append_args(
452                                     reply,
453                                     DBUS_TYPE_OBJECT_PATH, &path,
454                                     DBUS_TYPE_INVALID))
455                         goto oom;
456         }
457
458         if (reply) {
459                 if (!dbus_connection_send(connection, reply, NULL))
460                         goto oom;
461
462                 dbus_message_unref(reply);
463         }
464
465         free(path);
466
467         return DBUS_HANDLER_RESULT_HANDLED;
468
469 oom:
470         free(path);
471
472         if (reply)
473                 dbus_message_unref(reply);
474
475         dbus_error_free(&error);
476
477         return DBUS_HANDLER_RESULT_NEED_MEMORY;
478 }
479
480 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
481         Manager *m = data;
482         Unit *u;
483         int r;
484         DBusMessage *reply;
485
486         assert(connection);
487         assert(message);
488         assert(m);
489
490         if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
491                 /* Be nice to gdbus and return introspection data for our mid-level paths */
492
493                 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
494                         char *introspection = NULL;
495                         FILE *f;
496                         Iterator i;
497                         const char *k;
498                         size_t size;
499
500                         if (!(reply = dbus_message_new_method_return(message)))
501                                 goto oom;
502
503                         /* We roll our own introspection code here, instead of
504                          * relying on bus_default_message_handler() because we
505                          * need to generate our introspection string
506                          * dynamically. */
507
508                         if (!(f = open_memstream(&introspection, &size)))
509                                 goto oom;
510
511                         fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
512                               "<node>\n", f);
513
514                         fputs(BUS_INTROSPECTABLE_INTERFACE, f);
515                         fputs(BUS_PEER_INTERFACE, f);
516
517                         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
518                                 char *p;
519
520                                 if (k != u->meta.id)
521                                         continue;
522
523                                 if (!(p = bus_path_escape(k))) {
524                                         fclose(f);
525                                         free(introspection);
526                                         goto oom;
527                                 }
528
529                                 fprintf(f, "<node name=\"%s\"/>", p);
530                                 free(p);
531                         }
532
533                         fputs("</node>\n", f);
534
535                         if (ferror(f)) {
536                                 fclose(f);
537                                 free(introspection);
538                                 goto oom;
539                         }
540
541                         fclose(f);
542
543                         if (!introspection)
544                                 goto oom;
545
546                         if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
547                                 free(introspection);
548                                 goto oom;
549                         }
550
551                         free(introspection);
552
553                         if (!dbus_connection_send(connection, reply, NULL))
554                                 goto oom;
555
556                         dbus_message_unref(reply);
557
558                         return DBUS_HANDLER_RESULT_HANDLED;
559                 }
560
561                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
562         }
563
564         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
565
566                 if (r == -ENOMEM)
567                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
568
569                 if (r == -ENOENT) {
570                         DBusError e;
571
572                         dbus_error_init(&e);
573                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown unit");
574                         return bus_send_error_reply(m, connection, message, &e, r);
575                 }
576
577                 return bus_send_error_reply(m, connection, message, NULL, r);
578         }
579
580         return bus_unit_message_dispatch(u, connection, message);
581
582 oom:
583         if (reply)
584                 dbus_message_unref(reply);
585
586         return DBUS_HANDLER_RESULT_NEED_MEMORY;
587 }
588
589 const DBusObjectPathVTable bus_unit_vtable = {
590         .message_function = bus_unit_message_handler
591 };
592
593 void bus_unit_send_change_signal(Unit *u) {
594         char *p = NULL;
595         DBusMessage *m = NULL;
596
597         assert(u);
598
599         if (u->meta.in_dbus_queue) {
600                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
601                 u->meta.in_dbus_queue = false;
602         }
603
604         if (!u->meta.id)
605                 return;
606
607         if (!bus_has_subscriber(u->meta.manager)) {
608                 u->meta.sent_dbus_new_signal = true;
609                 return;
610         }
611
612         if (!(p = unit_dbus_path(u)))
613                 goto oom;
614
615         if (u->meta.sent_dbus_new_signal) {
616                 /* Send a properties changed signal. First for the
617                  * specific type, then for the generic unit. The
618                  * clients may rely on this order to get atomic
619                  * behaviour if needed. */
620
621                 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
622
623                         if (!(m = bus_properties_changed_new(p,
624                                                              UNIT_VTABLE(u)->bus_interface,
625                                                              UNIT_VTABLE(u)->bus_invalidating_properties)))
626                                 goto oom;
627
628                         if (bus_broadcast(u->meta.manager, m) < 0)
629                                 goto oom;
630
631                         dbus_message_unref(m);
632                 }
633
634                 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
635                         goto oom;
636
637         } else {
638                 /* Send a new signal */
639
640                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
641                         goto oom;
642
643                 if (!dbus_message_append_args(m,
644                                               DBUS_TYPE_STRING, &u->meta.id,
645                                               DBUS_TYPE_OBJECT_PATH, &p,
646                                               DBUS_TYPE_INVALID))
647                         goto oom;
648         }
649
650         if (bus_broadcast(u->meta.manager, m) < 0)
651                 goto oom;
652
653         free(p);
654         dbus_message_unref(m);
655
656         u->meta.sent_dbus_new_signal = true;
657
658         return;
659
660 oom:
661         free(p);
662
663         if (m)
664                 dbus_message_unref(m);
665
666         log_error("Failed to allocate unit change/new signal.");
667 }
668
669 void bus_unit_send_removed_signal(Unit *u) {
670         char *p = NULL;
671         DBusMessage *m = NULL;
672
673         assert(u);
674
675         if (!bus_has_subscriber(u->meta.manager))
676                 return;
677
678         if (!u->meta.sent_dbus_new_signal)
679                 bus_unit_send_change_signal(u);
680
681         if (!u->meta.id)
682                 return;
683
684         if (!(p = unit_dbus_path(u)))
685                 goto oom;
686
687         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
688                 goto oom;
689
690         if (!dbus_message_append_args(m,
691                                       DBUS_TYPE_STRING, &u->meta.id,
692                                       DBUS_TYPE_OBJECT_PATH, &p,
693                                       DBUS_TYPE_INVALID))
694                 goto oom;
695
696         if (bus_broadcast(u->meta.manager, m) < 0)
697                 goto oom;
698
699         free(p);
700         dbus_message_unref(m);
701
702         return;
703
704 oom:
705         free(p);
706
707         if (m)
708                 dbus_message_unref(m);
709
710         log_error("Failed to allocate unit remove signal.");
711 }