chiark / gitweb /
log: add automatic log target
[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", "Kill")) {
371                 const char *swho, *smode;
372                 int32_t signo;
373                 KillMode mode;
374                 KillWho who;
375                 int r;
376
377                 if (!dbus_message_get_args(
378                                     message,
379                                     &error,
380                                     DBUS_TYPE_STRING, &swho,
381                                     DBUS_TYPE_STRING, &smode,
382                                     DBUS_TYPE_INT32, &signo,
383                                     DBUS_TYPE_INVALID))
384                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
385
386                 if ((mode = kill_mode_from_string(smode)) < 0 ||
387                     (who = kill_who_from_string(swho)) < 0 ||
388                     signo <= 0 ||
389                     signo >= _NSIG)
390                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
391
392                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
393                         return bus_send_error_reply(m, connection, message, &error, r);
394
395                 if (!(reply = dbus_message_new_method_return(message)))
396                         goto oom;
397
398         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
399
400                 unit_reset_failed(u);
401
402                 if (!(reply = dbus_message_new_method_return(message)))
403                         goto oom;
404
405         } else if (UNIT_VTABLE(u)->bus_message_handler)
406                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
407         else
408                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
409
410         if (job_type != _JOB_TYPE_INVALID) {
411                 const char *smode;
412                 JobMode mode;
413                 Job *j;
414                 int r;
415
416                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
417                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
418                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
419                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
420                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
421                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
422                 }
423
424                 if (!dbus_message_get_args(
425                                     message,
426                                     &error,
427                                     DBUS_TYPE_STRING, &smode,
428                                     DBUS_TYPE_INVALID))
429                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
430
431                 if (reload_if_possible && unit_can_reload(u)) {
432                         if (job_type == JOB_RESTART)
433                                 job_type = JOB_RELOAD_OR_START;
434                         else if (job_type == JOB_TRY_RESTART)
435                                 job_type = JOB_RELOAD;
436                 }
437
438                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
439                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
440                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
441                 }
442
443                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
444                         return bus_send_error_reply(m, connection, message, &error, r);
445
446                 if (!(reply = dbus_message_new_method_return(message)))
447                         goto oom;
448
449                 if (!(path = job_dbus_path(j)))
450                         goto oom;
451
452                 if (!dbus_message_append_args(
453                                     reply,
454                                     DBUS_TYPE_OBJECT_PATH, &path,
455                                     DBUS_TYPE_INVALID))
456                         goto oom;
457         }
458
459         free(path);
460
461         if (reply) {
462                 if (!dbus_connection_send(connection, reply, NULL))
463                         goto oom;
464
465                 dbus_message_unref(reply);
466         }
467
468         return DBUS_HANDLER_RESULT_HANDLED;
469
470 oom:
471         free(path);
472
473         if (reply)
474                 dbus_message_unref(reply);
475
476         dbus_error_free(&error);
477
478         return DBUS_HANDLER_RESULT_NEED_MEMORY;
479 }
480
481 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
482         Manager *m = data;
483         Unit *u;
484         int r;
485         DBusMessage *reply;
486
487         assert(connection);
488         assert(message);
489         assert(m);
490
491         if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
492                 /* Be nice to gdbus and return introspection data for our mid-level paths */
493
494                 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
495                         char *introspection = NULL;
496                         FILE *f;
497                         Iterator i;
498                         const char *k;
499                         size_t size;
500
501                         if (!(reply = dbus_message_new_method_return(message)))
502                                 goto oom;
503
504                         /* We roll our own introspection code here, instead of
505                          * relying on bus_default_message_handler() because we
506                          * need to generate our introspection string
507                          * dynamically. */
508
509                         if (!(f = open_memstream(&introspection, &size)))
510                                 goto oom;
511
512                         fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
513                               "<node>\n", f);
514
515                         fputs(BUS_INTROSPECTABLE_INTERFACE, f);
516                         fputs(BUS_PEER_INTERFACE, f);
517
518                         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
519                                 char *p;
520
521                                 if (k != u->meta.id)
522                                         continue;
523
524                                 if (!(p = bus_path_escape(k))) {
525                                         fclose(f);
526                                         free(introspection);
527                                         goto oom;
528                                 }
529
530                                 fprintf(f, "<node name=\"%s\"/>", p);
531                                 free(p);
532                         }
533
534                         fputs("</node>\n", f);
535
536                         if (ferror(f)) {
537                                 fclose(f);
538                                 free(introspection);
539                                 goto oom;
540                         }
541
542                         fclose(f);
543
544                         if (!introspection)
545                                 goto oom;
546
547                         if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
548                                 free(introspection);
549                                 goto oom;
550                         }
551
552                         free(introspection);
553
554                         if (!dbus_connection_send(connection, reply, NULL))
555                                 goto oom;
556
557                         dbus_message_unref(reply);
558
559                         return DBUS_HANDLER_RESULT_HANDLED;
560                 }
561
562                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
563         }
564
565         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
566
567                 if (r == -ENOMEM)
568                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
569
570                 if (r == -ENOENT)
571                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
572
573                 return bus_send_error_reply(m, connection, message, NULL, r);
574         }
575
576         return bus_unit_message_dispatch(u, connection, message);
577
578 oom:
579         if (reply)
580                 dbus_message_unref(reply);
581
582         return DBUS_HANDLER_RESULT_NEED_MEMORY;
583 }
584
585 const DBusObjectPathVTable bus_unit_vtable = {
586         .message_function = bus_unit_message_handler
587 };
588
589 void bus_unit_send_change_signal(Unit *u) {
590         char *p = NULL;
591         DBusMessage *m = NULL;
592
593         assert(u);
594
595         if (u->meta.in_dbus_queue) {
596                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
597                 u->meta.in_dbus_queue = false;
598         }
599
600         if (!u->meta.id)
601                 return;
602
603         if (!bus_has_subscriber(u->meta.manager)) {
604                 u->meta.sent_dbus_new_signal = true;
605                 return;
606         }
607
608         if (!(p = unit_dbus_path(u)))
609                 goto oom;
610
611         if (u->meta.sent_dbus_new_signal) {
612                 /* Send a properties changed signal. First for the
613                  * specific type, then for the generic unit. The
614                  * clients may rely on this order to get atomic
615                  * behaviour if needed. */
616
617                 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
618
619                         if (!(m = bus_properties_changed_new(p,
620                                                              UNIT_VTABLE(u)->bus_interface,
621                                                              UNIT_VTABLE(u)->bus_invalidating_properties)))
622                                 goto oom;
623
624                         if (bus_broadcast(u->meta.manager, m) < 0)
625                                 goto oom;
626
627                         dbus_message_unref(m);
628                 }
629
630                 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
631                         goto oom;
632
633         } else {
634                 /* Send a new signal */
635
636                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
637                         goto oom;
638
639                 if (!dbus_message_append_args(m,
640                                               DBUS_TYPE_STRING, &u->meta.id,
641                                               DBUS_TYPE_OBJECT_PATH, &p,
642                                               DBUS_TYPE_INVALID))
643                         goto oom;
644         }
645
646         if (bus_broadcast(u->meta.manager, m) < 0)
647                 goto oom;
648
649         free(p);
650         dbus_message_unref(m);
651
652         u->meta.sent_dbus_new_signal = true;
653
654         return;
655
656 oom:
657         free(p);
658
659         if (m)
660                 dbus_message_unref(m);
661
662         log_error("Failed to allocate unit change/new signal.");
663 }
664
665 void bus_unit_send_removed_signal(Unit *u) {
666         char *p = NULL;
667         DBusMessage *m = NULL;
668
669         assert(u);
670
671         if (!bus_has_subscriber(u->meta.manager))
672                 return;
673
674         if (!u->meta.sent_dbus_new_signal)
675                 bus_unit_send_change_signal(u);
676
677         if (!u->meta.id)
678                 return;
679
680         if (!(p = unit_dbus_path(u)))
681                 goto oom;
682
683         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
684                 goto oom;
685
686         if (!dbus_message_append_args(m,
687                                       DBUS_TYPE_STRING, &u->meta.id,
688                                       DBUS_TYPE_OBJECT_PATH, &p,
689                                       DBUS_TYPE_INVALID))
690                 goto oom;
691
692         if (bus_broadcast(u->meta.manager, m) < 0)
693                 goto oom;
694
695         free(p);
696         dbus_message_unref(m);
697
698         return;
699
700 oom:
701         free(p);
702
703         if (m)
704                 dbus_message_unref(m);
705
706         log_error("Failed to allocate unit remove signal.");
707 }