]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus.c
dbus: make daemon reexecution synchronous
[thirdparty/systemd.git] / src / dbus.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 <sys/epoll.h>
23 #include <sys/timerfd.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <dbus/dbus.h>
27
28 #include "dbus.h"
29 #include "log.h"
30 #include "strv.h"
31 #include "cgroup.h"
32 #include "dbus-unit.h"
33 #include "dbus-job.h"
34 #include "dbus-manager.h"
35 #include "dbus-service.h"
36 #include "dbus-socket.h"
37 #include "dbus-target.h"
38 #include "dbus-device.h"
39 #include "dbus-mount.h"
40 #include "dbus-automount.h"
41 #include "dbus-snapshot.h"
42 #include "dbus-swap.h"
43 #include "dbus-timer.h"
44 #include "dbus-path.h"
45 #include "bus-errors.h"
46 #include "special.h"
47 #include "dbus-common.h"
48
49 #define CONNECTIONS_MAX 52
50
51 static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
52 static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
53
54 const char *const bus_interface_table[] = {
55 "org.freedesktop.DBus.Properties", bus_properties_interface,
56 "org.freedesktop.DBus.Introspectable", bus_introspectable_interface,
57 "org.freedesktop.systemd1.Manager", bus_manager_interface,
58 "org.freedesktop.systemd1.Job", bus_job_interface,
59 "org.freedesktop.systemd1.Unit", bus_unit_interface,
60 "org.freedesktop.systemd1.Service", bus_service_interface,
61 "org.freedesktop.systemd1.Socket", bus_socket_interface,
62 "org.freedesktop.systemd1.Target", bus_target_interface,
63 "org.freedesktop.systemd1.Device", bus_device_interface,
64 "org.freedesktop.systemd1.Mount", bus_mount_interface,
65 "org.freedesktop.systemd1.Automount", bus_automount_interface,
66 "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface,
67 "org.freedesktop.systemd1.Swap", bus_swap_interface,
68 "org.freedesktop.systemd1.Timer", bus_timer_interface,
69 "org.freedesktop.systemd1.Path", bus_path_interface,
70 NULL
71 };
72
73 static void bus_done_api(Manager *m);
74 static void bus_done_system(Manager *m);
75 static void bus_done_private(Manager *m);
76 static void shutdown_connection(Manager *m, DBusConnection *c);
77
78 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
79 Manager *m = data;
80
81 assert(bus);
82 assert(m);
83
84 /* We maintain two sets, one for those connections where we
85 * requested a dispatch, and another where we didn't. And then,
86 * we move the connections between the two sets. */
87
88 if (status == DBUS_DISPATCH_COMPLETE)
89 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, bus);
90 else
91 set_move_one(m->bus_connections_for_dispatch, m->bus_connections, bus);
92 }
93
94 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
95 unsigned flags;
96 uint32_t events = 0;
97
98 assert(bus_watch);
99
100 /* no watch flags for disabled watches */
101 if (!dbus_watch_get_enabled(bus_watch))
102 return 0;
103
104 flags = dbus_watch_get_flags(bus_watch);
105
106 if (flags & DBUS_WATCH_READABLE)
107 events |= EPOLLIN;
108 if (flags & DBUS_WATCH_WRITABLE)
109 events |= EPOLLOUT;
110
111 return events | EPOLLHUP | EPOLLERR;
112 }
113
114 static unsigned events_to_bus_flags(uint32_t events) {
115 unsigned flags = 0;
116
117 if (events & EPOLLIN)
118 flags |= DBUS_WATCH_READABLE;
119 if (events & EPOLLOUT)
120 flags |= DBUS_WATCH_WRITABLE;
121 if (events & EPOLLHUP)
122 flags |= DBUS_WATCH_HANGUP;
123 if (events & EPOLLERR)
124 flags |= DBUS_WATCH_ERROR;
125
126 return flags;
127 }
128
129 void bus_watch_event(Manager *m, Watch *w, int events) {
130 assert(m);
131 assert(w);
132
133 /* This is called by the event loop whenever there is
134 * something happening on D-Bus' file handles. */
135
136 if (!dbus_watch_get_enabled(w->data.bus_watch))
137 return;
138
139 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
140 }
141
142 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
143 Manager *m = data;
144 Watch *w;
145 struct epoll_event ev;
146
147 assert(bus_watch);
148 assert(m);
149
150 if (!(w = new0(Watch, 1)))
151 return FALSE;
152
153 w->fd = dbus_watch_get_unix_fd(bus_watch);
154 w->type = WATCH_DBUS_WATCH;
155 w->data.bus_watch = bus_watch;
156
157 zero(ev);
158 ev.events = bus_flags_to_events(bus_watch);
159 ev.data.ptr = w;
160
161 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
162
163 if (errno != EEXIST) {
164 free(w);
165 return FALSE;
166 }
167
168 /* Hmm, bloody D-Bus creates multiple watches on the
169 * same fd. epoll() does not like that. As a dirty
170 * hack we simply dup() the fd and hence get a second
171 * one we can safely add to the epoll(). */
172
173 if ((w->fd = dup(w->fd)) < 0) {
174 free(w);
175 return FALSE;
176 }
177
178 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
179 close_nointr_nofail(w->fd);
180 free(w);
181 return FALSE;
182 }
183
184 w->fd_is_dupped = true;
185 }
186
187 dbus_watch_set_data(bus_watch, w, NULL);
188
189 return TRUE;
190 }
191
192 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
193 Manager *m = data;
194 Watch *w;
195
196 assert(bus_watch);
197 assert(m);
198
199 if (!(w = dbus_watch_get_data(bus_watch)))
200 return;
201
202 assert(w->type == WATCH_DBUS_WATCH);
203 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
204
205 if (w->fd_is_dupped)
206 close_nointr_nofail(w->fd);
207
208 free(w);
209 }
210
211 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
212 Manager *m = data;
213 Watch *w;
214 struct epoll_event ev;
215
216 assert(bus_watch);
217 assert(m);
218
219 assert_se(w = dbus_watch_get_data(bus_watch));
220 assert(w->type == WATCH_DBUS_WATCH);
221
222 zero(ev);
223 ev.events = bus_flags_to_events(bus_watch);
224 ev.data.ptr = w;
225
226 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
227 }
228
229 static int bus_timeout_arm(Manager *m, Watch *w) {
230 struct itimerspec its;
231
232 assert(m);
233 assert(w);
234
235 zero(its);
236
237 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
238 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
239 its.it_interval = its.it_value;
240 }
241
242 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
243 return -errno;
244
245 return 0;
246 }
247
248 void bus_timeout_event(Manager *m, Watch *w, int events) {
249 assert(m);
250 assert(w);
251
252 /* This is called by the event loop whenever there is
253 * something happening on D-Bus' file handles. */
254
255 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
256 return;
257
258 dbus_timeout_handle(w->data.bus_timeout);
259 }
260
261 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
262 Manager *m = data;
263 Watch *w;
264 struct epoll_event ev;
265
266 assert(timeout);
267 assert(m);
268
269 if (!(w = new0(Watch, 1)))
270 return FALSE;
271
272 if ((w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
273 goto fail;
274
275 w->type = WATCH_DBUS_TIMEOUT;
276 w->data.bus_timeout = timeout;
277
278 if (bus_timeout_arm(m, w) < 0)
279 goto fail;
280
281 zero(ev);
282 ev.events = EPOLLIN;
283 ev.data.ptr = w;
284
285 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
286 goto fail;
287
288 dbus_timeout_set_data(timeout, w, NULL);
289
290 return TRUE;
291
292 fail:
293 if (w->fd >= 0)
294 close_nointr_nofail(w->fd);
295
296 free(w);
297 return FALSE;
298 }
299
300 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
301 Manager *m = data;
302 Watch *w;
303
304 assert(timeout);
305 assert(m);
306
307 if (!(w = dbus_timeout_get_data(timeout)))
308 return;
309
310 assert(w->type == WATCH_DBUS_TIMEOUT);
311 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
312 close_nointr_nofail(w->fd);
313 free(w);
314 }
315
316 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
317 Manager *m = data;
318 Watch *w;
319 int r;
320
321 assert(timeout);
322 assert(m);
323
324 assert_se(w = dbus_timeout_get_data(timeout));
325 assert(w->type == WATCH_DBUS_TIMEOUT);
326
327 if ((r = bus_timeout_arm(m, w)) < 0)
328 log_error("Failed to rearm timer: %s", strerror(-r));
329 }
330
331 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
332 Manager *m = data;
333 DBusError error;
334 DBusMessage *reply = NULL;
335
336 assert(connection);
337 assert(message);
338 assert(m);
339
340 dbus_error_init(&error);
341
342 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
343 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
344 log_debug("Got D-Bus request: %s.%s() on %s",
345 dbus_message_get_interface(message),
346 dbus_message_get_member(message),
347 dbus_message_get_path(message));
348
349 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
350 log_debug("API D-Bus connection terminated.");
351 bus_done_api(m);
352
353 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
354 const char *name, *old_owner, *new_owner;
355
356 if (!dbus_message_get_args(message, &error,
357 DBUS_TYPE_STRING, &name,
358 DBUS_TYPE_STRING, &old_owner,
359 DBUS_TYPE_STRING, &new_owner,
360 DBUS_TYPE_INVALID))
361 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
362 else {
363 if (set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) name))
364 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(BUS_CONNECTION_SUBSCRIBED(m, connection)));
365
366 if (old_owner[0] == 0)
367 old_owner = NULL;
368
369 if (new_owner[0] == 0)
370 new_owner = NULL;
371
372 manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
373 }
374 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
375 const char *name;
376
377 if (!dbus_message_get_args(message, &error,
378 DBUS_TYPE_STRING, &name,
379 DBUS_TYPE_INVALID))
380 log_error("Failed to parse ActivationRequest message: %s", error.message);
381 else {
382 int r;
383 Unit *u;
384
385 log_debug("Got D-Bus activation request for %s", name);
386
387 if (manager_unit_pending_inactive(m, SPECIAL_DBUS_SERVICE) ||
388 manager_unit_pending_inactive(m, SPECIAL_DBUS_SOCKET)) {
389 r = -EADDRNOTAVAIL;
390 dbus_set_error(&error, BUS_ERROR_SHUTTING_DOWN, "Refusing activation, D-Bus is shutting down.");
391 } else {
392 r = manager_load_unit(m, name, NULL, &error, &u);
393
394 if (r >= 0 && u->meta.refuse_manual_start)
395 r = -EPERM;
396
397 if (r >= 0)
398 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
399 }
400
401 if (r < 0) {
402 const char *id, *text;
403
404 log_debug("D-Bus activation failed for %s: %s", name, strerror(-r));
405
406 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
407 goto oom;
408
409 id = error.name ? error.name : bus_errno_to_dbus(r);
410 text = bus_error(&error, r);
411
412 if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) ||
413 !dbus_message_append_args(reply,
414 DBUS_TYPE_STRING, &name,
415 DBUS_TYPE_STRING, &id,
416 DBUS_TYPE_STRING, &text,
417 DBUS_TYPE_INVALID))
418 goto oom;
419 }
420
421 /* On success we don't do anything, the service will be spawned now */
422 }
423 }
424
425 dbus_error_free(&error);
426
427 if (reply) {
428 if (!dbus_connection_send(connection, reply, NULL))
429 goto oom;
430
431 dbus_message_unref(reply);
432 }
433
434 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
435
436 oom:
437 if (reply)
438 dbus_message_unref(reply);
439
440 dbus_error_free(&error);
441
442 return DBUS_HANDLER_RESULT_NEED_MEMORY;
443 }
444
445 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
446 Manager *m = data;
447 DBusError error;
448
449 assert(connection);
450 assert(message);
451 assert(m);
452
453 dbus_error_init(&error);
454
455 if (m->api_bus != m->system_bus &&
456 (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
457 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL))
458 log_debug("Got D-Bus request on system bus: %s.%s() on %s",
459 dbus_message_get_interface(message),
460 dbus_message_get_member(message),
461 dbus_message_get_path(message));
462
463 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
464 log_debug("System D-Bus connection terminated.");
465 bus_done_system(m);
466
467 } else if (m->running_as != MANAGER_SYSTEM &&
468 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
469
470 const char *cgroup;
471
472 if (!dbus_message_get_args(message, &error,
473 DBUS_TYPE_STRING, &cgroup,
474 DBUS_TYPE_INVALID))
475 log_error("Failed to parse Released message: %s", error.message);
476 else
477 cgroup_notify_empty(m, cgroup);
478 }
479
480 dbus_error_free(&error);
481 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
482 }
483
484 static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
485 Manager *m = data;
486 DBusError error;
487
488 assert(connection);
489 assert(message);
490 assert(m);
491
492 dbus_error_init(&error);
493
494 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
495 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
496 log_debug("Got D-Bus request: %s.%s() on %s",
497 dbus_message_get_interface(message),
498 dbus_message_get_member(message),
499 dbus_message_get_path(message));
500
501 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
502 shutdown_connection(m, connection);
503 else if (m->running_as == MANAGER_SYSTEM &&
504 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
505
506 const char *cgroup;
507
508 if (!dbus_message_get_args(message, &error,
509 DBUS_TYPE_STRING, &cgroup,
510 DBUS_TYPE_INVALID))
511 log_error("Failed to parse Released message: %s", error.message);
512 else
513 cgroup_notify_empty(m, cgroup);
514
515 /* Forward the message to the system bus, so that user
516 * instances are notified as well */
517
518 if (m->system_bus)
519 dbus_connection_send(m->system_bus, message, NULL);
520 }
521
522 dbus_error_free(&error);
523
524 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
525 }
526
527 unsigned bus_dispatch(Manager *m) {
528 DBusConnection *c;
529
530 assert(m);
531
532 if (m->queued_message) {
533 /* If we cannot get rid of this message we won't
534 * dispatch any D-Bus messages, so that we won't end
535 * up wanting to queue another message. */
536
537 if (m->queued_message_connection)
538 if (!dbus_connection_send(m->queued_message_connection, m->queued_message, NULL))
539 return 0;
540
541 dbus_message_unref(m->queued_message);
542 m->queued_message = NULL;
543 m->queued_message_connection = NULL;
544 }
545
546 if ((c = set_first(m->bus_connections_for_dispatch))) {
547 if (dbus_connection_dispatch(c) == DBUS_DISPATCH_COMPLETE)
548 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, c);
549
550 return 1;
551 }
552
553 return 0;
554 }
555
556 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
557 DBusMessage *reply;
558 DBusError error;
559
560 dbus_error_init(&error);
561
562 assert_se(reply = dbus_pending_call_steal_reply(pending));
563
564 switch (dbus_message_get_type(reply)) {
565
566 case DBUS_MESSAGE_TYPE_ERROR:
567
568 assert_se(dbus_set_error_from_message(&error, reply));
569 log_warning("RequestName() failed: %s", error.message);
570 break;
571
572 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
573 uint32_t r;
574
575 if (!dbus_message_get_args(reply,
576 &error,
577 DBUS_TYPE_UINT32, &r,
578 DBUS_TYPE_INVALID)) {
579 log_error("Failed to parse RequestName() reply: %s", error.message);
580 break;
581 }
582
583 if (r == 1)
584 log_debug("Successfully acquired name.");
585 else
586 log_error("Name already owned.");
587
588 break;
589 }
590
591 default:
592 assert_not_reached("Invalid reply message");
593 }
594
595 dbus_message_unref(reply);
596 dbus_error_free(&error);
597 }
598
599 static int request_name(Manager *m) {
600 const char *name = "org.freedesktop.systemd1";
601 /* Allow replacing of our name, to ease implementation of
602 * reexecution, where we keep the old connection open until
603 * after the new connection is set up and the name installed
604 * to allow clients to synchronously wait for reexecution to
605 * finish */
606 uint32_t flags = DBUS_NAME_FLAG_ALLOW_REPLACEMENT|DBUS_NAME_FLAG_REPLACE_EXISTING;
607 DBusMessage *message = NULL;
608 DBusPendingCall *pending = NULL;
609
610 if (!(message = dbus_message_new_method_call(
611 DBUS_SERVICE_DBUS,
612 DBUS_PATH_DBUS,
613 DBUS_INTERFACE_DBUS,
614 "RequestName")))
615 goto oom;
616
617 if (!dbus_message_append_args(
618 message,
619 DBUS_TYPE_STRING, &name,
620 DBUS_TYPE_UINT32, &flags,
621 DBUS_TYPE_INVALID))
622 goto oom;
623
624 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
625 goto oom;
626
627 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
628 goto oom;
629
630 dbus_message_unref(message);
631 dbus_pending_call_unref(pending);
632
633 /* We simple ask for the name and don't wait for it. Sooner or
634 * later we'll have it. */
635
636 return 0;
637
638 oom:
639 if (pending) {
640 dbus_pending_call_cancel(pending);
641 dbus_pending_call_unref(pending);
642 }
643
644 if (message)
645 dbus_message_unref(message);
646
647 return -ENOMEM;
648 }
649
650 static void query_name_list_pending_cb(DBusPendingCall *pending, void *userdata) {
651 DBusMessage *reply;
652 DBusError error;
653 Manager *m = userdata;
654
655 assert(m);
656
657 dbus_error_init(&error);
658
659 assert_se(reply = dbus_pending_call_steal_reply(pending));
660
661 switch (dbus_message_get_type(reply)) {
662
663 case DBUS_MESSAGE_TYPE_ERROR:
664
665 assert_se(dbus_set_error_from_message(&error, reply));
666 log_warning("ListNames() failed: %s", error.message);
667 break;
668
669 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
670 int r;
671 char **l;
672
673 if ((r = bus_parse_strv(reply, &l)) < 0)
674 log_warning("Failed to parse ListNames() reply: %s", strerror(-r));
675 else {
676 char **t;
677
678 STRV_FOREACH(t, l)
679 /* This is a bit hacky, we say the
680 * owner of the name is the name
681 * itself, because we don't want the
682 * extra traffic to figure out the
683 * real owner. */
684 manager_dispatch_bus_name_owner_changed(m, *t, NULL, *t);
685
686 strv_free(l);
687 }
688
689 break;
690 }
691
692 default:
693 assert_not_reached("Invalid reply message");
694 }
695
696 dbus_message_unref(reply);
697 dbus_error_free(&error);
698 }
699
700 static int query_name_list(Manager *m) {
701 DBusMessage *message = NULL;
702 DBusPendingCall *pending = NULL;
703
704 /* Asks for the currently installed bus names */
705
706 if (!(message = dbus_message_new_method_call(
707 DBUS_SERVICE_DBUS,
708 DBUS_PATH_DBUS,
709 DBUS_INTERFACE_DBUS,
710 "ListNames")))
711 goto oom;
712
713 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
714 goto oom;
715
716 if (!dbus_pending_call_set_notify(pending, query_name_list_pending_cb, m, NULL))
717 goto oom;
718
719 dbus_message_unref(message);
720 dbus_pending_call_unref(pending);
721
722 /* We simple ask for the list and don't wait for it. Sooner or
723 * later we'll get it. */
724
725 return 0;
726
727 oom:
728 if (pending) {
729 dbus_pending_call_cancel(pending);
730 dbus_pending_call_unref(pending);
731 }
732
733 if (message)
734 dbus_message_unref(message);
735
736 return -ENOMEM;
737 }
738
739 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
740 assert(m);
741 assert(bus);
742
743 dbus_connection_set_exit_on_disconnect(bus, FALSE);
744
745 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
746 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
747 log_error("Not enough memory");
748 return -ENOMEM;
749 }
750
751 if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
752 log_error("Not enough memory");
753 return -ENOMEM;
754 }
755
756 dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
757 return 0;
758 }
759
760 static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
761 return uid == 0;
762 }
763
764 static void bus_new_connection(
765 DBusServer *server,
766 DBusConnection *new_connection,
767 void *data) {
768
769 Manager *m = data;
770
771 assert(m);
772
773 if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
774 log_error("Too many concurrent connections.");
775 return;
776 }
777
778 dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
779
780 if (bus_setup_loop(m, new_connection) < 0)
781 return;
782
783 if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
784 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
785 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
786 !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
787 log_error("Not enough memory.");
788 return;
789 }
790
791 log_debug("Accepted connection on private bus.");
792
793 dbus_connection_ref(new_connection);
794 }
795
796 static int bus_init_system(Manager *m) {
797 DBusError error;
798 int r;
799
800 assert(m);
801
802 dbus_error_init(&error);
803
804 if (m->system_bus)
805 return 0;
806
807 if (m->running_as == MANAGER_SYSTEM && m->api_bus)
808 m->system_bus = m->api_bus;
809 else {
810 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
811 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
812 r = 0;
813 goto fail;
814 }
815
816 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
817 goto fail;
818 }
819
820 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
821 log_error("Not enough memory");
822 r = -EIO;
823 goto fail;
824 }
825
826 if (m->running_as != MANAGER_SYSTEM) {
827 dbus_bus_add_match(m->system_bus,
828 "type='signal',"
829 "interface='org.freedesktop.systemd1.Agent',"
830 "member='Released',"
831 "path='/org/freedesktop/systemd1/agent'",
832 &error);
833
834 if (dbus_error_is_set(&error)) {
835 log_error("Failed to register match: %s", error.message);
836 r = -EIO;
837 goto fail;
838 }
839 }
840
841 if (m->api_bus != m->system_bus) {
842 char *id;
843 log_debug("Successfully connected to system D-Bus bus %s as %s",
844 strnull((id = dbus_connection_get_server_id(m->system_bus))),
845 strnull(dbus_bus_get_unique_name(m->system_bus)));
846 dbus_free(id);
847 }
848
849 return 0;
850
851 fail:
852 bus_done_system(m);
853 dbus_error_free(&error);
854
855 return r;
856 }
857
858 static int bus_init_api(Manager *m) {
859 DBusError error;
860 int r;
861
862 assert(m);
863
864 dbus_error_init(&error);
865
866 if (m->api_bus)
867 return 0;
868
869 if (m->running_as == MANAGER_SYSTEM && m->system_bus)
870 m->api_bus = m->system_bus;
871 else {
872 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_USER ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
873 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
874 r = 0;
875 goto fail;
876 }
877
878 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
879 goto fail;
880 }
881
882 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
883 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
884 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
885 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
886 log_error("Not enough memory");
887 r = -ENOMEM;
888 goto fail;
889 }
890
891 /* Get NameOwnerChange messages */
892 dbus_bus_add_match(m->api_bus,
893 "type='signal',"
894 "sender='"DBUS_SERVICE_DBUS"',"
895 "interface='"DBUS_INTERFACE_DBUS"',"
896 "member='NameOwnerChanged',"
897 "path='"DBUS_PATH_DBUS"'",
898 &error);
899
900 if (dbus_error_is_set(&error)) {
901 log_error("Failed to register match: %s", error.message);
902 r = -EIO;
903 goto fail;
904 }
905
906 /* Get activation requests */
907 dbus_bus_add_match(m->api_bus,
908 "type='signal',"
909 "sender='"DBUS_SERVICE_DBUS"',"
910 "interface='org.freedesktop.systemd1.Activator',"
911 "member='ActivationRequest',"
912 "path='"DBUS_PATH_DBUS"'",
913 &error);
914
915 if (dbus_error_is_set(&error)) {
916 log_error("Failed to register match: %s", error.message);
917 r = -EIO;
918 goto fail;
919 }
920
921 if ((r = request_name(m)) < 0)
922 goto fail;
923
924 if ((r = query_name_list(m)) < 0)
925 goto fail;
926
927 if (m->api_bus != m->system_bus) {
928 char *id;
929 log_debug("Successfully connected to API D-Bus bus %s as %s",
930 strnull((id = dbus_connection_get_server_id(m->api_bus))),
931 strnull(dbus_bus_get_unique_name(m->api_bus)));
932 dbus_free(id);
933 }
934
935 return 0;
936
937 fail:
938 bus_done_api(m);
939 dbus_error_free(&error);
940
941 return r;
942 }
943
944 static int bus_init_private(Manager *m) {
945 DBusError error;
946 int r;
947 const char *const external_only[] = {
948 "EXTERNAL",
949 NULL
950 };
951
952 assert(m);
953
954 dbus_error_init(&error);
955
956 if (m->private_bus)
957 return 0;
958
959 /* We want the private bus only when running as init */
960 if (getpid() != 1)
961 return 0;
962
963 unlink("/run/systemd/private");
964 if (!(m->private_bus = dbus_server_listen("unix:path=/run/systemd/private", &error))) {
965 log_error("Failed to create private D-Bus server: %s", error.message);
966 r = -EIO;
967 goto fail;
968 }
969
970 if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
971 !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
972 !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
973 log_error("Not enough memory");
974 r = -ENOMEM;
975 goto fail;
976 }
977
978 dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
979
980 log_debug("Successfully created private D-Bus server.");
981
982 return 0;
983
984 fail:
985 bus_done_private(m);
986 dbus_error_free(&error);
987
988 return r;
989 }
990
991 int bus_init(Manager *m, bool try_bus_connect) {
992 int r;
993
994 if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
995 set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
996 log_error("Not enough memory");
997 return -ENOMEM;
998 }
999
1000 if (m->name_data_slot < 0)
1001 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
1002 log_error("Not enough memory");
1003 return -ENOMEM;
1004 }
1005
1006 if (m->subscribed_data_slot < 0)
1007 if (!dbus_connection_allocate_data_slot(&m->subscribed_data_slot)) {
1008 log_error("Not enough memory");
1009 return -ENOMEM;
1010 }
1011
1012 if (try_bus_connect) {
1013 if ((r = bus_init_system(m)) < 0 ||
1014 (r = bus_init_api(m)) < 0)
1015 return r;
1016 }
1017
1018 if ((r = bus_init_private(m)) < 0)
1019 return r;
1020
1021 return 0;
1022 }
1023
1024 static void shutdown_connection(Manager *m, DBusConnection *c) {
1025 Set *s;
1026 Job *j;
1027 Iterator i;
1028
1029 HASHMAP_FOREACH(j, m->jobs, i)
1030 if (j->bus == c) {
1031 free(j->bus_client);
1032 j->bus_client = NULL;
1033
1034 j->bus = NULL;
1035 }
1036
1037 set_remove(m->bus_connections, c);
1038 set_remove(m->bus_connections_for_dispatch, c);
1039
1040 if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
1041 char *t;
1042
1043 while ((t = set_steal_first(s)))
1044 free(t);
1045
1046 set_free(s);
1047 }
1048
1049 if (m->queued_message_connection == c) {
1050 m->queued_message_connection = NULL;
1051
1052 if (m->queued_message) {
1053 dbus_message_unref(m->queued_message);
1054 m->queued_message = NULL;
1055 }
1056 }
1057
1058 dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1059 dbus_connection_flush(c);
1060 dbus_connection_close(c);
1061 dbus_connection_unref(c);
1062 }
1063
1064 static void bus_done_api(Manager *m) {
1065 assert(m);
1066
1067 if (m->api_bus) {
1068 if (m->system_bus == m->api_bus)
1069 m->system_bus = NULL;
1070
1071 shutdown_connection(m, m->api_bus);
1072 m->api_bus = NULL;
1073 }
1074
1075
1076 if (m->queued_message) {
1077 dbus_message_unref(m->queued_message);
1078 m->queued_message = NULL;
1079 }
1080 }
1081
1082 static void bus_done_system(Manager *m) {
1083 assert(m);
1084
1085 if (m->system_bus == m->api_bus)
1086 bus_done_api(m);
1087
1088 if (m->system_bus) {
1089 shutdown_connection(m, m->system_bus);
1090 m->system_bus = NULL;
1091 }
1092 }
1093
1094 static void bus_done_private(Manager *m) {
1095
1096 if (m->private_bus) {
1097 dbus_server_disconnect(m->private_bus);
1098 dbus_server_unref(m->private_bus);
1099 m->private_bus = NULL;
1100 }
1101 }
1102
1103 void bus_done(Manager *m) {
1104 DBusConnection *c;
1105
1106 bus_done_api(m);
1107 bus_done_system(m);
1108 bus_done_private(m);
1109
1110 while ((c = set_steal_first(m->bus_connections)))
1111 shutdown_connection(m, c);
1112
1113 while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1114 shutdown_connection(m, c);
1115
1116 set_free(m->bus_connections);
1117 set_free(m->bus_connections_for_dispatch);
1118
1119 if (m->name_data_slot >= 0)
1120 dbus_pending_call_free_data_slot(&m->name_data_slot);
1121
1122 if (m->subscribed_data_slot >= 0)
1123 dbus_connection_free_data_slot(&m->subscribed_data_slot);
1124 }
1125
1126 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1127 Manager *m = userdata;
1128 DBusMessage *reply;
1129 DBusError error;
1130 const char *name;
1131
1132 dbus_error_init(&error);
1133
1134 assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1135 assert_se(reply = dbus_pending_call_steal_reply(pending));
1136
1137 switch (dbus_message_get_type(reply)) {
1138
1139 case DBUS_MESSAGE_TYPE_ERROR:
1140
1141 assert_se(dbus_set_error_from_message(&error, reply));
1142 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1143 break;
1144
1145 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1146 uint32_t r;
1147
1148 if (!dbus_message_get_args(reply,
1149 &error,
1150 DBUS_TYPE_UINT32, &r,
1151 DBUS_TYPE_INVALID)) {
1152 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1153 break;
1154 }
1155
1156 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1157 break;
1158 }
1159
1160 default:
1161 assert_not_reached("Invalid reply message");
1162 }
1163
1164 dbus_message_unref(reply);
1165 dbus_error_free(&error);
1166 }
1167
1168 int bus_query_pid(Manager *m, const char *name) {
1169 DBusMessage *message = NULL;
1170 DBusPendingCall *pending = NULL;
1171 char *n = NULL;
1172
1173 assert(m);
1174 assert(name);
1175
1176 if (!(message = dbus_message_new_method_call(
1177 DBUS_SERVICE_DBUS,
1178 DBUS_PATH_DBUS,
1179 DBUS_INTERFACE_DBUS,
1180 "GetConnectionUnixProcessID")))
1181 goto oom;
1182
1183 if (!(dbus_message_append_args(
1184 message,
1185 DBUS_TYPE_STRING, &name,
1186 DBUS_TYPE_INVALID)))
1187 goto oom;
1188
1189 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1190 goto oom;
1191
1192 if (!(n = strdup(name)))
1193 goto oom;
1194
1195 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1196 goto oom;
1197
1198 n = NULL;
1199
1200 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1201 goto oom;
1202
1203 dbus_message_unref(message);
1204 dbus_pending_call_unref(pending);
1205
1206 return 0;
1207
1208 oom:
1209 free(n);
1210
1211 if (pending) {
1212 dbus_pending_call_cancel(pending);
1213 dbus_pending_call_unref(pending);
1214 }
1215
1216 if (message)
1217 dbus_message_unref(message);
1218
1219 return -ENOMEM;
1220 }
1221
1222 int bus_broadcast(Manager *m, DBusMessage *message) {
1223 bool oom = false;
1224 Iterator i;
1225 DBusConnection *c;
1226
1227 assert(m);
1228 assert(message);
1229
1230 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1231 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1232 oom = !dbus_connection_send(c, message, NULL);
1233
1234 SET_FOREACH(c, m->bus_connections, i)
1235 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1236 oom = !dbus_connection_send(c, message, NULL);
1237
1238 return oom ? -ENOMEM : 0;
1239 }
1240
1241 int bus_parse_strv(DBusMessage *m, char ***_l) {
1242 DBusMessageIter iter, sub;
1243 unsigned n = 0, i = 0;
1244 char **l;
1245
1246 assert(m);
1247 assert(_l);
1248
1249 if (!dbus_message_iter_init(m, &iter) ||
1250 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1251 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1252 return -EINVAL;
1253
1254 dbus_message_iter_recurse(&iter, &sub);
1255
1256 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1257 n++;
1258 dbus_message_iter_next(&sub);
1259 }
1260
1261 if (!(l = new(char*, n+1)))
1262 return -ENOMEM;
1263
1264 assert_se(dbus_message_iter_init(m, &iter));
1265 dbus_message_iter_recurse(&iter, &sub);
1266
1267 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1268 const char *s;
1269
1270 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1271 dbus_message_iter_get_basic(&sub, &s);
1272
1273 if (!(l[i++] = strdup(s))) {
1274 strv_free(l);
1275 return -ENOMEM;
1276 }
1277
1278 dbus_message_iter_next(&sub);
1279 }
1280
1281 assert(i == n);
1282 l[i] = NULL;
1283
1284 if (_l)
1285 *_l = l;
1286
1287 return 0;
1288 }
1289
1290 bool bus_has_subscriber(Manager *m) {
1291 Iterator i;
1292 DBusConnection *c;
1293
1294 assert(m);
1295
1296 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1297 if (bus_connection_has_subscriber(m, c))
1298 return true;
1299
1300 SET_FOREACH(c, m->bus_connections, i)
1301 if (bus_connection_has_subscriber(m, c))
1302 return true;
1303
1304 return false;
1305 }
1306
1307 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1308 assert(m);
1309 assert(c);
1310
1311 return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1312 }
1313
1314 int bus_fdset_add_all(Manager *m, FDSet *fds) {
1315 Iterator i;
1316 DBusConnection *c;
1317
1318 assert(m);
1319 assert(fds);
1320
1321 /* When we are about to reexecute we add all D-Bus fds to the
1322 * set to pass over to the newly executed systemd. They won't
1323 * be used there however, except that they are closed at the
1324 * very end of deserialization, those making it possible for
1325 * clients to synchronously wait for systemd to reexec buy
1326 * simply waiting for disconnection */
1327
1328 SET_FOREACH(c, m->bus_connections_for_dispatch, i) {
1329 int fd;
1330
1331 if (dbus_connection_get_unix_fd(c, &fd)) {
1332 fd = fdset_put_dup(fds, fd);
1333
1334 if (fd < 0)
1335 return fd;
1336 }
1337 }
1338
1339 SET_FOREACH(c, m->bus_connections, i) {
1340 int fd;
1341
1342 if (dbus_connection_get_unix_fd(c, &fd)) {
1343 fd = fdset_put_dup(fds, fd);
1344
1345 if (fd < 0)
1346 return fd;
1347 }
1348 }
1349
1350 return 0;
1351 }