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