]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/dbus.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / core / dbus.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <sys/epoll.h>
22 #include <unistd.h>
23
24 #include "sd-bus.h"
25
26 #include "alloc-util.h"
27 #include "bus-common-errors.h"
28 #include "bus-error.h"
29 #include "bus-internal.h"
30 #include "bus-util.h"
31 #include "dbus-cgroup.h"
32 #include "dbus-execute.h"
33 #include "dbus-job.h"
34 #include "dbus-kill.h"
35 #include "dbus-manager.h"
36 #include "dbus-unit.h"
37 #include "dbus.h"
38 #include "fd-util.h"
39 #include "log.h"
40 #include "missing.h"
41 #include "mkdir.h"
42 #include "selinux-access.h"
43 #include "special.h"
44 #include "string-util.h"
45 #include "strv.h"
46 #include "strxcpyx.h"
47 #include "user-util.h"
48
49 #define CONNECTIONS_MAX 4096
50
51 static void destroy_bus(Manager *m, sd_bus **bus);
52
53 int bus_send_queued_message(Manager *m) {
54 int r;
55
56 assert(m);
57
58 if (!m->queued_message)
59 return 0;
60
61 /* If we cannot get rid of this message we won't dispatch any
62 * D-Bus messages, so that we won't end up wanting to queue
63 * another message. */
64
65 r = sd_bus_send(NULL, m->queued_message, NULL);
66 if (r < 0)
67 log_warning_errno(r, "Failed to send queued message: %m");
68
69 m->queued_message = sd_bus_message_unref(m->queued_message);
70
71 return 0;
72 }
73
74 int bus_forward_agent_released(Manager *m, const char *path) {
75 int r;
76
77 assert(m);
78 assert(path);
79
80 if (!MANAGER_IS_SYSTEM(m))
81 return 0;
82
83 if (!m->system_bus)
84 return 0;
85
86 /* If we are running a system instance we forward the agent message on the system bus, so that the user
87 * instances get notified about this, too */
88
89 r = sd_bus_emit_signal(m->system_bus,
90 "/org/freedesktop/systemd1/agent",
91 "org.freedesktop.systemd1.Agent",
92 "Released",
93 "s", path);
94 if (r < 0)
95 return log_debug_errno(r, "Failed to propagate agent release message: %m");
96
97 return 1;
98 }
99
100 static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) {
101 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
102 Manager *m = userdata;
103 const char *cgroup;
104 uid_t sender_uid;
105 int r;
106
107 assert(message);
108 assert(m);
109
110 /* only accept org.freedesktop.systemd1.Agent from UID=0 */
111 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
112 if (r < 0)
113 return r;
114
115 r = sd_bus_creds_get_euid(creds, &sender_uid);
116 if (r < 0 || sender_uid != 0)
117 return 0;
118
119 /* parse 'cgroup-empty' notification */
120 r = sd_bus_message_read(message, "s", &cgroup);
121 if (r < 0) {
122 bus_log_parse_error(r);
123 return 0;
124 }
125
126 manager_notify_cgroup_empty(m, cgroup);
127 return 0;
128 }
129
130 static int signal_disconnected(sd_bus_message *message, void *userdata, sd_bus_error *error) {
131 Manager *m = userdata;
132 sd_bus *bus;
133
134 assert(message);
135 assert(m);
136 assert_se(bus = sd_bus_message_get_bus(message));
137
138 if (bus == m->api_bus)
139 destroy_bus(m, &m->api_bus);
140 if (bus == m->system_bus)
141 destroy_bus(m, &m->system_bus);
142 if (set_remove(m->private_buses, bus)) {
143 log_debug("Got disconnect on private connection.");
144 destroy_bus(m, &bus);
145 }
146
147 return 0;
148 }
149
150 static int signal_activation_request(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
151 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
152 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
153 Manager *m = userdata;
154 const char *name;
155 Unit *u;
156 int r;
157
158 assert(message);
159 assert(m);
160
161 r = sd_bus_message_read(message, "s", &name);
162 if (r < 0) {
163 bus_log_parse_error(r);
164 return 0;
165 }
166
167 if (manager_unit_inactive_or_pending(m, SPECIAL_DBUS_SERVICE) ||
168 manager_unit_inactive_or_pending(m, SPECIAL_DBUS_SOCKET)) {
169 r = sd_bus_error_setf(&error, BUS_ERROR_SHUTTING_DOWN, "Refusing activation, D-Bus is shutting down.");
170 goto failed;
171 }
172
173 r = manager_load_unit(m, name, NULL, &error, &u);
174 if (r < 0)
175 goto failed;
176
177 if (u->refuse_manual_start) {
178 r = sd_bus_error_setf(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, %s may be requested by dependency only (it is configured to refuse manual start/stop).", u->id);
179 goto failed;
180 }
181
182 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, &error, NULL);
183 if (r < 0)
184 goto failed;
185
186 /* Successfully queued, that's it for us */
187 return 0;
188
189 failed:
190 if (!sd_bus_error_is_set(&error))
191 sd_bus_error_set_errno(&error, r);
192
193 log_debug("D-Bus activation failed for %s: %s", name, bus_error_message(&error, r));
194
195 r = sd_bus_message_new_signal(sd_bus_message_get_bus(message), &reply, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure");
196 if (r < 0) {
197 bus_log_create_error(r);
198 return 0;
199 }
200
201 r = sd_bus_message_append(reply, "sss", name, error.name, error.message);
202 if (r < 0) {
203 bus_log_create_error(r);
204 return 0;
205 }
206
207 r = sd_bus_send_to(NULL, reply, "org.freedesktop.DBus", NULL);
208 if (r < 0)
209 return log_error_errno(r, "Failed to respond with to bus activation request: %m");
210
211 return 0;
212 }
213
214 #if HAVE_SELINUX
215 static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_error *error) {
216 Manager *m = userdata;
217 const char *verb, *path;
218 Unit *u = NULL;
219 Job *j;
220 int r;
221
222 assert(message);
223
224 /* Our own method calls are all protected individually with
225 * selinux checks, but the built-in interfaces need to be
226 * protected too. */
227
228 if (sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set"))
229 verb = "reload";
230 else if (sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", NULL) ||
231 sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Properties", NULL) ||
232 sd_bus_message_is_method_call(message, "org.freedesktop.DBus.ObjectManager", NULL) ||
233 sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Peer", NULL))
234 verb = "status";
235 else
236 return 0;
237
238 path = sd_bus_message_get_path(message);
239
240 if (object_path_startswith("/org/freedesktop/systemd1", path)) {
241
242 r = mac_selinux_access_check(message, verb, error);
243 if (r < 0)
244 return r;
245
246 return 0;
247 }
248
249 if (streq_ptr(path, "/org/freedesktop/systemd1/unit/self")) {
250 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
251 pid_t pid;
252
253 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
254 if (r < 0)
255 return 0;
256
257 r = sd_bus_creds_get_pid(creds, &pid);
258 if (r < 0)
259 return 0;
260
261 u = manager_get_unit_by_pid(m, pid);
262 } else {
263 r = manager_get_job_from_dbus_path(m, path, &j);
264 if (r >= 0)
265 u = j->unit;
266 else
267 manager_load_unit_from_dbus_path(m, path, NULL, &u);
268 }
269
270 if (!u)
271 return 0;
272
273 r = mac_selinux_unit_access_check(u, message, verb, error);
274 if (r < 0)
275 return r;
276
277 return 0;
278 }
279 #endif
280
281 static int bus_job_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
282 Manager *m = userdata;
283 Job *j;
284 int r;
285
286 assert(bus);
287 assert(path);
288 assert(interface);
289 assert(found);
290 assert(m);
291
292 r = manager_get_job_from_dbus_path(m, path, &j);
293 if (r < 0)
294 return 0;
295
296 *found = j;
297 return 1;
298 }
299
300 static int find_unit(Manager *m, sd_bus *bus, const char *path, Unit **unit, sd_bus_error *error) {
301 Unit *u = NULL; /* just to appease gcc, initialization is not really necessary */
302 int r;
303
304 assert(m);
305 assert(bus);
306 assert(path);
307
308 if (streq_ptr(path, "/org/freedesktop/systemd1/unit/self")) {
309 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
310 sd_bus_message *message;
311 pid_t pid;
312
313 message = sd_bus_get_current_message(bus);
314 if (!message)
315 return 0;
316
317 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
318 if (r < 0)
319 return r;
320
321 r = sd_bus_creds_get_pid(creds, &pid);
322 if (r < 0)
323 return r;
324
325 u = manager_get_unit_by_pid(m, pid);
326 if (!u)
327 return 0;
328 } else {
329 r = manager_load_unit_from_dbus_path(m, path, error, &u);
330 if (r < 0)
331 return 0;
332 assert(u);
333 }
334
335 *unit = u;
336 return 1;
337 }
338
339 static int bus_unit_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
340 Manager *m = userdata;
341
342 assert(bus);
343 assert(path);
344 assert(interface);
345 assert(found);
346 assert(m);
347
348 return find_unit(m, bus, path, (Unit**) found, error);
349 }
350
351 static int bus_unit_interface_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
352 Manager *m = userdata;
353 Unit *u;
354 int r;
355
356 assert(bus);
357 assert(path);
358 assert(interface);
359 assert(found);
360 assert(m);
361
362 r = find_unit(m, bus, path, &u, error);
363 if (r <= 0)
364 return r;
365
366 if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type)))
367 return 0;
368
369 *found = u;
370 return 1;
371 }
372
373 static int bus_unit_cgroup_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
374 Manager *m = userdata;
375 Unit *u;
376 int r;
377
378 assert(bus);
379 assert(path);
380 assert(interface);
381 assert(found);
382 assert(m);
383
384 r = find_unit(m, bus, path, &u, error);
385 if (r <= 0)
386 return r;
387
388 if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type)))
389 return 0;
390
391 if (!UNIT_HAS_CGROUP_CONTEXT(u))
392 return 0;
393
394 *found = u;
395 return 1;
396 }
397
398 static int bus_cgroup_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
399 Manager *m = userdata;
400 CGroupContext *c;
401 Unit *u;
402 int r;
403
404 assert(bus);
405 assert(path);
406 assert(interface);
407 assert(found);
408 assert(m);
409
410 r = find_unit(m, bus, path, &u, error);
411 if (r <= 0)
412 return r;
413
414 if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type)))
415 return 0;
416
417 c = unit_get_cgroup_context(u);
418 if (!c)
419 return 0;
420
421 *found = c;
422 return 1;
423 }
424
425 static int bus_exec_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
426 Manager *m = userdata;
427 ExecContext *c;
428 Unit *u;
429 int r;
430
431 assert(bus);
432 assert(path);
433 assert(interface);
434 assert(found);
435 assert(m);
436
437 r = find_unit(m, bus, path, &u, error);
438 if (r <= 0)
439 return r;
440
441 if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type)))
442 return 0;
443
444 c = unit_get_exec_context(u);
445 if (!c)
446 return 0;
447
448 *found = c;
449 return 1;
450 }
451
452 static int bus_kill_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
453 Manager *m = userdata;
454 KillContext *c;
455 Unit *u;
456 int r;
457
458 assert(bus);
459 assert(path);
460 assert(interface);
461 assert(found);
462 assert(m);
463
464 r = find_unit(m, bus, path, &u, error);
465 if (r <= 0)
466 return r;
467
468 if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type)))
469 return 0;
470
471 c = unit_get_kill_context(u);
472 if (!c)
473 return 0;
474
475 *found = c;
476 return 1;
477 }
478
479 static int bus_job_enumerate(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
480 _cleanup_strv_free_ char **l = NULL;
481 Manager *m = userdata;
482 unsigned k = 0;
483 Iterator i;
484 Job *j;
485
486 l = new0(char*, hashmap_size(m->jobs)+1);
487 if (!l)
488 return -ENOMEM;
489
490 HASHMAP_FOREACH(j, m->jobs, i) {
491 l[k] = job_dbus_path(j);
492 if (!l[k])
493 return -ENOMEM;
494
495 k++;
496 }
497
498 assert(hashmap_size(m->jobs) == k);
499
500 *nodes = l;
501 l = NULL;
502
503 return k;
504 }
505
506 static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
507 _cleanup_strv_free_ char **l = NULL;
508 Manager *m = userdata;
509 unsigned k = 0;
510 Iterator i;
511 Unit *u;
512
513 l = new0(char*, hashmap_size(m->units)+1);
514 if (!l)
515 return -ENOMEM;
516
517 HASHMAP_FOREACH(u, m->units, i) {
518 l[k] = unit_dbus_path(u);
519 if (!l[k])
520 return -ENOMEM;
521
522 k++;
523 }
524
525 *nodes = l;
526 l = NULL;
527
528 return k;
529 }
530
531 static int bus_setup_api_vtables(Manager *m, sd_bus *bus) {
532 UnitType t;
533 int r;
534
535 assert(m);
536 assert(bus);
537
538 #if HAVE_SELINUX
539 r = sd_bus_add_filter(bus, NULL, mac_selinux_filter, m);
540 if (r < 0)
541 return log_error_errno(r, "Failed to add SELinux access filter: %m");
542 #endif
543
544 r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", bus_manager_vtable, m);
545 if (r < 0)
546 return log_error_errno(r, "Failed to register Manager vtable: %m");
547
548 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/job", "org.freedesktop.systemd1.Job", bus_job_vtable, bus_job_find, m);
549 if (r < 0)
550 return log_error_errno(r, "Failed to register Job vtable: %m");
551
552 r = sd_bus_add_node_enumerator(bus, NULL, "/org/freedesktop/systemd1/job", bus_job_enumerate, m);
553 if (r < 0)
554 return log_error_errno(r, "Failed to add job enumerator: %m");
555
556 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", "org.freedesktop.systemd1.Unit", bus_unit_vtable, bus_unit_find, m);
557 if (r < 0)
558 return log_error_errno(r, "Failed to register Unit vtable: %m");
559
560 r = sd_bus_add_node_enumerator(bus, NULL, "/org/freedesktop/systemd1/unit", bus_unit_enumerate, m);
561 if (r < 0)
562 return log_error_errno(r, "Failed to add job enumerator: %m");
563
564 for (t = 0; t < _UNIT_TYPE_MAX; t++) {
565 const char *interface;
566
567 assert_se(interface = unit_dbus_interface_from_type(t));
568
569 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, unit_vtable[t]->bus_vtable, bus_unit_interface_find, m);
570 if (r < 0)
571 return log_error_errno(r, "Failed to register type specific vtable for %s: %m", interface);
572
573 if (unit_vtable[t]->cgroup_context_offset > 0) {
574 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_unit_cgroup_vtable, bus_unit_cgroup_find, m);
575 if (r < 0)
576 return log_error_errno(r, "Failed to register control group unit vtable for %s: %m", interface);
577
578 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_cgroup_vtable, bus_cgroup_context_find, m);
579 if (r < 0)
580 return log_error_errno(r, "Failed to register control group vtable for %s: %m", interface);
581 }
582
583 if (unit_vtable[t]->exec_context_offset > 0) {
584 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_exec_vtable, bus_exec_context_find, m);
585 if (r < 0)
586 return log_error_errno(r, "Failed to register execute vtable for %s: %m", interface);
587 }
588
589 if (unit_vtable[t]->kill_context_offset > 0) {
590 r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_kill_vtable, bus_kill_context_find, m);
591 if (r < 0)
592 return log_error_errno(r, "Failed to register kill vtable for %s: %m", interface);
593 }
594 }
595
596 return 0;
597 }
598
599 static int bus_setup_disconnected_match(Manager *m, sd_bus *bus) {
600 int r;
601
602 assert(m);
603 assert(bus);
604
605 r = sd_bus_add_match(
606 bus,
607 NULL,
608 "sender='org.freedesktop.DBus.Local',"
609 "type='signal',"
610 "path='/org/freedesktop/DBus/Local',"
611 "interface='org.freedesktop.DBus.Local',"
612 "member='Disconnected'",
613 signal_disconnected, m);
614
615 if (r < 0)
616 return log_error_errno(r, "Failed to register match for Disconnected message: %m");
617
618 return 0;
619 }
620
621 static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
622 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
623 _cleanup_close_ int nfd = -1;
624 Manager *m = userdata;
625 sd_id128_t id;
626 int r;
627
628 assert(s);
629 assert(m);
630
631 nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
632 if (nfd < 0) {
633 log_warning_errno(errno, "Failed to accept private connection, ignoring: %m");
634 return 0;
635 }
636
637 if (set_size(m->private_buses) >= CONNECTIONS_MAX) {
638 log_warning("Too many concurrent connections, refusing");
639 return 0;
640 }
641
642 r = set_ensure_allocated(&m->private_buses, NULL);
643 if (r < 0) {
644 log_oom();
645 return 0;
646 }
647
648 r = sd_bus_new(&bus);
649 if (r < 0) {
650 log_warning_errno(r, "Failed to allocate new private connection bus: %m");
651 return 0;
652 }
653
654 r = sd_bus_set_fd(bus, nfd, nfd);
655 if (r < 0) {
656 log_warning_errno(r, "Failed to set fd on new connection bus: %m");
657 return 0;
658 }
659
660 nfd = -1;
661
662 r = bus_check_peercred(bus);
663 if (r < 0) {
664 log_warning_errno(r, "Incoming private connection from unprivileged client, refusing: %m");
665 return 0;
666 }
667
668 assert_se(sd_id128_randomize(&id) >= 0);
669
670 r = sd_bus_set_server(bus, 1, id);
671 if (r < 0) {
672 log_warning_errno(r, "Failed to enable server support for new connection bus: %m");
673 return 0;
674 }
675
676 r = sd_bus_negotiate_creds(bus, 1,
677 SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|
678 SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS|
679 SD_BUS_CREDS_SELINUX_CONTEXT);
680 if (r < 0) {
681 log_warning_errno(r, "Failed to enable credentials for new connection: %m");
682 return 0;
683 }
684
685 r = sd_bus_start(bus);
686 if (r < 0) {
687 log_warning_errno(r, "Failed to start new connection bus: %m");
688 return 0;
689 }
690
691 r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
692 if (r < 0) {
693 log_warning_errno(r, "Failed to attach new connection bus to event loop: %m");
694 return 0;
695 }
696
697 r = bus_setup_disconnected_match(m, bus);
698 if (r < 0)
699 return 0;
700
701 r = bus_setup_api_vtables(m, bus);
702 if (r < 0) {
703 log_warning_errno(r, "Failed to set up API vtables on new connection bus: %m");
704 return 0;
705 }
706
707 r = set_put(m->private_buses, bus);
708 if (r < 0) {
709 log_warning_errno(r, "Failed to add new connection bus to set: %m");
710 return 0;
711 }
712
713 bus = NULL;
714
715 log_debug("Accepted new private connection.");
716
717 return 0;
718 }
719
720 int manager_sync_bus_names(Manager *m, sd_bus *bus) {
721 _cleanup_strv_free_ char **names = NULL;
722 const char *name;
723 Iterator i;
724 Unit *u;
725 int r;
726
727 assert(m);
728 assert(bus);
729
730 r = sd_bus_list_names(bus, &names, NULL);
731 if (r < 0)
732 return log_error_errno(r, "Failed to get initial list of names: %m");
733
734 /* We have to synchronize the current bus names with the
735 * list of active services. To do this, walk the list of
736 * all units with bus names. */
737 HASHMAP_FOREACH_KEY(u, name, m->watch_bus, i) {
738 Service *s = SERVICE(u);
739
740 assert(s);
741
742 if (!streq_ptr(s->bus_name, name)) {
743 log_unit_warning(u, "Bus name has changed from %s → %s, ignoring.", s->bus_name, name);
744 continue;
745 }
746
747 /* Check if a service's bus name is in the list of currently
748 * active names */
749 if (strv_contains(names, name)) {
750 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
751 const char *unique;
752
753 /* If it is, determine its current owner */
754 r = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_UNIQUE_NAME, &creds);
755 if (r < 0) {
756 log_full_errno(r == -ENXIO ? LOG_DEBUG : LOG_ERR, r, "Failed to get bus name owner %s: %m", name);
757 continue;
758 }
759
760 r = sd_bus_creds_get_unique_name(creds, &unique);
761 if (r < 0) {
762 log_full_errno(r == -ENXIO ? LOG_DEBUG : LOG_ERR, r, "Failed to get unique name for %s: %m", name);
763 continue;
764 }
765
766 /* Now, let's compare that to the previous bus owner, and
767 * if it's still the same, all is fine, so just don't
768 * bother the service. Otherwise, the name has apparently
769 * changed, so synthesize a name owner changed signal. */
770
771 if (!streq_ptr(unique, s->bus_name_owner))
772 UNIT_VTABLE(u)->bus_name_owner_change(u, name, s->bus_name_owner, unique);
773 } else {
774 /* So, the name we're watching is not on the bus.
775 * This either means it simply hasn't appeared yet,
776 * or it was lost during the daemon reload.
777 * Check if the service has a stored name owner,
778 * and synthesize a name loss signal in this case. */
779
780 if (s->bus_name_owner)
781 UNIT_VTABLE(u)->bus_name_owner_change(u, name, s->bus_name_owner, NULL);
782 }
783 }
784
785 return 0;
786 }
787
788 static int bus_setup_api(Manager *m, sd_bus *bus) {
789 Iterator i;
790 char *name;
791 Unit *u;
792 int r;
793
794 assert(m);
795 assert(bus);
796
797 /* Let's make sure we have enough credential bits so that we can make security and selinux decisions */
798 r = sd_bus_negotiate_creds(bus, 1,
799 SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|
800 SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS|
801 SD_BUS_CREDS_SELINUX_CONTEXT);
802 if (r < 0)
803 log_warning_errno(r, "Failed to enable credential passing, ignoring: %m");
804
805 r = bus_setup_api_vtables(m, bus);
806 if (r < 0)
807 return r;
808
809 HASHMAP_FOREACH_KEY(u, name, m->watch_bus, i) {
810 r = unit_install_bus_match(u, bus, name);
811 if (r < 0)
812 log_error_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name);
813 }
814
815 r = sd_bus_add_match(
816 bus,
817 NULL,
818 "type='signal',"
819 "sender='org.freedesktop.DBus',"
820 "path='/org/freedesktop/DBus',"
821 "interface='org.freedesktop.systemd1.Activator',"
822 "member='ActivationRequest'",
823 signal_activation_request, m);
824 if (r < 0)
825 log_warning_errno(r, "Failed to subscribe to activation signal: %m");
826
827 /* Allow replacing of our name, to ease implementation of
828 * reexecution, where we keep the old connection open until
829 * after the new connection is set up and the name installed
830 * to allow clients to synchronously wait for reexecution to
831 * finish */
832 r = sd_bus_request_name(bus,"org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_ALLOW_REPLACEMENT);
833 if (r < 0)
834 return log_error_errno(r, "Failed to register name: %m");
835
836 r = manager_sync_bus_names(m, bus);
837 if (r < 0)
838 return r;
839
840 log_debug("Successfully connected to API bus.");
841 return 0;
842 }
843
844 static int bus_init_api(Manager *m) {
845 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
846 int r;
847
848 if (m->api_bus)
849 return 0;
850
851 /* The API and system bus is the same if we are running in system mode */
852 if (MANAGER_IS_SYSTEM(m) && m->system_bus)
853 bus = sd_bus_ref(m->system_bus);
854 else {
855 if (MANAGER_IS_SYSTEM(m))
856 r = sd_bus_open_system(&bus);
857 else
858 r = sd_bus_open_user(&bus);
859
860 if (r < 0) {
861 log_debug("Failed to connect to API bus, retrying later...");
862 return 0;
863 }
864
865 r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
866 if (r < 0) {
867 log_error_errno(r, "Failed to attach API bus to event loop: %m");
868 return 0;
869 }
870
871 r = bus_setup_disconnected_match(m, bus);
872 if (r < 0)
873 return 0;
874 }
875
876 r = bus_setup_api(m, bus);
877 if (r < 0) {
878 log_error_errno(r, "Failed to set up API bus: %m");
879 return 0;
880 }
881
882 m->api_bus = bus;
883 bus = NULL;
884
885 return 0;
886 }
887
888 static int bus_setup_system(Manager *m, sd_bus *bus) {
889 int r;
890
891 assert(m);
892 assert(bus);
893
894 /* if we are a user instance we get the Released message via the system bus */
895 if (MANAGER_IS_USER(m)) {
896 r = sd_bus_add_match(
897 bus,
898 NULL,
899 "type='signal',"
900 "interface='org.freedesktop.systemd1.Agent',"
901 "member='Released',"
902 "path='/org/freedesktop/systemd1/agent'",
903 signal_agent_released, m);
904 if (r < 0)
905 log_warning_errno(r, "Failed to register Released match on system bus: %m");
906 }
907
908 log_debug("Successfully connected to system bus.");
909 return 0;
910 }
911
912 static int bus_init_system(Manager *m) {
913 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
914 int r;
915
916 if (m->system_bus)
917 return 0;
918
919 /* The API and system bus is the same if we are running in system mode */
920 if (MANAGER_IS_SYSTEM(m) && m->api_bus) {
921 m->system_bus = sd_bus_ref(m->api_bus);
922 return 0;
923 }
924
925 r = sd_bus_open_system(&bus);
926 if (r < 0) {
927 log_debug("Failed to connect to system bus, retrying later...");
928 return 0;
929 }
930
931 r = bus_setup_disconnected_match(m, bus);
932 if (r < 0)
933 return 0;
934
935 r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL);
936 if (r < 0) {
937 log_error_errno(r, "Failed to attach system bus to event loop: %m");
938 return 0;
939 }
940
941 r = bus_setup_system(m, bus);
942 if (r < 0) {
943 log_error_errno(r, "Failed to set up system bus: %m");
944 return 0;
945 }
946
947 m->system_bus = bus;
948 bus = NULL;
949
950 return 0;
951 }
952
953 static int bus_init_private(Manager *m) {
954 _cleanup_close_ int fd = -1;
955 union sockaddr_union sa = {
956 .un.sun_family = AF_UNIX
957 };
958 sd_event_source *s;
959 socklen_t salen;
960 int r;
961
962 assert(m);
963
964 if (m->private_listen_fd >= 0)
965 return 0;
966
967 if (MANAGER_IS_SYSTEM(m)) {
968
969 /* We want the private bus only when running as init */
970 if (getpid_cached() != 1)
971 return 0;
972
973 strcpy(sa.un.sun_path, "/run/systemd/private");
974 salen = SOCKADDR_UN_LEN(sa.un);
975 } else {
976 size_t left = sizeof(sa.un.sun_path);
977 char *p = sa.un.sun_path;
978 const char *e;
979
980 e = secure_getenv("XDG_RUNTIME_DIR");
981 if (!e) {
982 log_error("Failed to determine XDG_RUNTIME_DIR");
983 return -EHOSTDOWN;
984 }
985
986 left = strpcpy(&p, left, e);
987 left = strpcpy(&p, left, "/systemd/private");
988
989 salen = sizeof(sa.un) - left;
990 }
991
992 (void) mkdir_parents_label(sa.un.sun_path, 0755);
993 (void) unlink(sa.un.sun_path);
994
995 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
996 if (fd < 0)
997 return log_error_errno(errno, "Failed to allocate private socket: %m");
998
999 r = bind(fd, &sa.sa, salen);
1000 if (r < 0)
1001 return log_error_errno(errno, "Failed to bind private socket: %m");
1002
1003 r = listen(fd, SOMAXCONN);
1004 if (r < 0)
1005 return log_error_errno(errno, "Failed to make private socket listening: %m");
1006
1007 r = sd_event_add_io(m->event, &s, fd, EPOLLIN, bus_on_connection, m);
1008 if (r < 0)
1009 return log_error_errno(r, "Failed to allocate event source: %m");
1010
1011 (void) sd_event_source_set_description(s, "bus-connection");
1012
1013 m->private_listen_fd = fd;
1014 m->private_listen_event_source = s;
1015 fd = -1;
1016
1017 log_debug("Successfully created private D-Bus server.");
1018
1019 return 0;
1020 }
1021
1022 int bus_init(Manager *m, bool try_bus_connect) {
1023 int r;
1024
1025 if (try_bus_connect) {
1026 r = bus_init_system(m);
1027 if (r < 0)
1028 return r;
1029
1030 r = bus_init_api(m);
1031 if (r < 0)
1032 return r;
1033 }
1034
1035 r = bus_init_private(m);
1036 if (r < 0)
1037 return r;
1038
1039 return 0;
1040 }
1041
1042 static void destroy_bus(Manager *m, sd_bus **bus) {
1043 Iterator i;
1044 Unit *u;
1045 Job *j;
1046
1047 assert(m);
1048 assert(bus);
1049
1050 if (!*bus)
1051 return;
1052
1053 /* Make sure all bus slots watching names are released. */
1054 HASHMAP_FOREACH(u, m->watch_bus, i) {
1055 if (!u->match_bus_slot)
1056 continue;
1057
1058 if (sd_bus_slot_get_bus(u->match_bus_slot) != *bus)
1059 continue;
1060
1061 u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
1062 }
1063
1064 /* Get rid of tracked clients on this bus */
1065 if (m->subscribed && sd_bus_track_get_bus(m->subscribed) == *bus)
1066 m->subscribed = sd_bus_track_unref(m->subscribed);
1067
1068 HASHMAP_FOREACH(j, m->jobs, i)
1069 if (j->bus_track && sd_bus_track_get_bus(j->bus_track) == *bus)
1070 j->bus_track = sd_bus_track_unref(j->bus_track);
1071
1072 /* Get rid of queued message on this bus */
1073 if (m->queued_message && sd_bus_message_get_bus(m->queued_message) == *bus)
1074 m->queued_message = sd_bus_message_unref(m->queued_message);
1075
1076 /* Possibly flush unwritten data, but only if we are
1077 * unprivileged, since we don't want to sync here */
1078 if (!MANAGER_IS_SYSTEM(m))
1079 sd_bus_flush(*bus);
1080
1081 /* And destroy the object */
1082 sd_bus_close(*bus);
1083 *bus = sd_bus_unref(*bus);
1084 }
1085
1086 void bus_done(Manager *m) {
1087 sd_bus *b;
1088
1089 assert(m);
1090
1091 if (m->api_bus)
1092 destroy_bus(m, &m->api_bus);
1093 if (m->system_bus)
1094 destroy_bus(m, &m->system_bus);
1095 while ((b = set_steal_first(m->private_buses)))
1096 destroy_bus(m, &b);
1097
1098 m->private_buses = set_free(m->private_buses);
1099
1100 m->subscribed = sd_bus_track_unref(m->subscribed);
1101 m->deserialized_subscribed = strv_free(m->deserialized_subscribed);
1102
1103 if (m->private_listen_event_source)
1104 m->private_listen_event_source = sd_event_source_unref(m->private_listen_event_source);
1105
1106 m->private_listen_fd = safe_close(m->private_listen_fd);
1107
1108 bus_verify_polkit_async_registry_free(m->polkit_registry);
1109 }
1110
1111 int bus_fdset_add_all(Manager *m, FDSet *fds) {
1112 Iterator i;
1113 sd_bus *b;
1114 int fd;
1115
1116 assert(m);
1117 assert(fds);
1118
1119 /* When we are about to reexecute we add all D-Bus fds to the
1120 * set to pass over to the newly executed systemd. They won't
1121 * be used there however, except thatt they are closed at the
1122 * very end of deserialization, those making it possible for
1123 * clients to synchronously wait for systemd to reexec by
1124 * simply waiting for disconnection */
1125
1126 if (m->api_bus) {
1127 fd = sd_bus_get_fd(m->api_bus);
1128 if (fd >= 0) {
1129 fd = fdset_put_dup(fds, fd);
1130 if (fd < 0)
1131 return fd;
1132 }
1133 }
1134
1135 SET_FOREACH(b, m->private_buses, i) {
1136 fd = sd_bus_get_fd(b);
1137 if (fd >= 0) {
1138 fd = fdset_put_dup(fds, fd);
1139 if (fd < 0)
1140 return fd;
1141 }
1142 }
1143
1144 /* We don't offer any APIs on the system bus (well, unless it
1145 * is the same as the API bus) hence we don't bother with it
1146 * here */
1147
1148 return 0;
1149 }
1150
1151 int bus_foreach_bus(
1152 Manager *m,
1153 sd_bus_track *subscribed2,
1154 int (*send_message)(sd_bus *bus, void *userdata),
1155 void *userdata) {
1156
1157 Iterator i;
1158 sd_bus *b;
1159 int r, ret = 0;
1160
1161 /* Send to all direct buses, unconditionally */
1162 SET_FOREACH(b, m->private_buses, i) {
1163 r = send_message(b, userdata);
1164 if (r < 0)
1165 ret = r;
1166 }
1167
1168 /* Send to API bus, but only if somebody is subscribed */
1169 if (sd_bus_track_count(m->subscribed) > 0 ||
1170 sd_bus_track_count(subscribed2) > 0) {
1171 r = send_message(m->api_bus, userdata);
1172 if (r < 0)
1173 ret = r;
1174 }
1175
1176 return ret;
1177 }
1178
1179 void bus_track_serialize(sd_bus_track *t, FILE *f, const char *prefix) {
1180 const char *n;
1181
1182 assert(f);
1183 assert(prefix);
1184
1185 for (n = sd_bus_track_first(t); n; n = sd_bus_track_next(t)) {
1186 int c, j;
1187
1188 c = sd_bus_track_count_name(t, n);
1189
1190 for (j = 0; j < c; j++) {
1191 fputs(prefix, f);
1192 fputc('=', f);
1193 fputs(n, f);
1194 fputc('\n', f);
1195 }
1196 }
1197 }
1198
1199 int bus_track_coldplug(Manager *m, sd_bus_track **t, bool recursive, char **l) {
1200 int r = 0;
1201
1202 assert(m);
1203 assert(t);
1204
1205 if (strv_isempty(l))
1206 return 0;
1207
1208 if (!m->api_bus)
1209 return 0;
1210
1211 if (!*t) {
1212 r = sd_bus_track_new(m->api_bus, t, NULL, NULL);
1213 if (r < 0)
1214 return r;
1215 }
1216
1217 r = sd_bus_track_set_recursive(*t, recursive);
1218 if (r < 0)
1219 return r;
1220
1221 return bus_track_add_name_many(*t, l);
1222 }
1223
1224 int bus_verify_manage_units_async(Manager *m, sd_bus_message *call, sd_bus_error *error) {
1225 return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.manage-units", NULL, false, UID_INVALID, &m->polkit_registry, error);
1226 }
1227
1228 int bus_verify_manage_unit_files_async(Manager *m, sd_bus_message *call, sd_bus_error *error) {
1229 return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.manage-unit-files", NULL, false, UID_INVALID, &m->polkit_registry, error);
1230 }
1231
1232 int bus_verify_reload_daemon_async(Manager *m, sd_bus_message *call, sd_bus_error *error) {
1233 return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.reload-daemon", NULL, false, UID_INVALID, &m->polkit_registry, error);
1234 }
1235
1236 int bus_verify_set_environment_async(Manager *m, sd_bus_message *call, sd_bus_error *error) {
1237 return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.set-environment", NULL, false, UID_INVALID, &m->polkit_registry, error);
1238 }