]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus.c
main: remove AF_UNIX sockets before binding
[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
48 #define CONNECTIONS_MAX 52
49
50 static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
51 static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
52
53 const char *const bus_interface_table[] = {
54 "org.freedesktop.DBus.Properties", bus_properties_interface,
55 "org.freedesktop.DBus.Introspectable", bus_introspectable_interface,
56 "org.freedesktop.systemd1.Manager", bus_manager_interface,
57 "org.freedesktop.systemd1.Job", bus_job_interface,
58 "org.freedesktop.systemd1.Unit", bus_unit_interface,
59 "org.freedesktop.systemd1.Service", bus_service_interface,
60 "org.freedesktop.systemd1.Socket", bus_socket_interface,
61 "org.freedesktop.systemd1.Target", bus_target_interface,
62 "org.freedesktop.systemd1.Device", bus_device_interface,
63 "org.freedesktop.systemd1.Mount", bus_mount_interface,
64 "org.freedesktop.systemd1.Automount", bus_automount_interface,
65 "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface,
66 "org.freedesktop.systemd1.Swap", bus_swap_interface,
67 "org.freedesktop.systemd1.Timer", bus_timer_interface,
68 "org.freedesktop.systemd1.Path", bus_path_interface,
69 NULL
70 };
71
72 static const char *error_to_dbus(int error);
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 free(w);
180 close_nointr_nofail(w->fd);
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_interval;
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 : error_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 uint32_t flags = 0;
602 DBusMessage *message = NULL;
603 DBusPendingCall *pending = NULL;
604
605 if (!(message = dbus_message_new_method_call(
606 DBUS_SERVICE_DBUS,
607 DBUS_PATH_DBUS,
608 DBUS_INTERFACE_DBUS,
609 "RequestName")))
610 goto oom;
611
612 if (!dbus_message_append_args(
613 message,
614 DBUS_TYPE_STRING, &name,
615 DBUS_TYPE_UINT32, &flags,
616 DBUS_TYPE_INVALID))
617 goto oom;
618
619 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
620 goto oom;
621
622 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
623 goto oom;
624
625 dbus_message_unref(message);
626 dbus_pending_call_unref(pending);
627
628 /* We simple ask for the name and don't wait for it. Sooner or
629 * later we'll have it. */
630
631 return 0;
632
633 oom:
634 if (pending) {
635 dbus_pending_call_cancel(pending);
636 dbus_pending_call_unref(pending);
637 }
638
639 if (message)
640 dbus_message_unref(message);
641
642 return -ENOMEM;
643 }
644
645 static void query_name_list_pending_cb(DBusPendingCall *pending, void *userdata) {
646 DBusMessage *reply;
647 DBusError error;
648 Manager *m = userdata;
649
650 assert(m);
651
652 dbus_error_init(&error);
653
654 assert_se(reply = dbus_pending_call_steal_reply(pending));
655
656 switch (dbus_message_get_type(reply)) {
657
658 case DBUS_MESSAGE_TYPE_ERROR:
659
660 assert_se(dbus_set_error_from_message(&error, reply));
661 log_warning("ListNames() failed: %s", error.message);
662 break;
663
664 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
665 int r;
666 char **l;
667
668 if ((r = bus_parse_strv(reply, &l)) < 0)
669 log_warning("Failed to parse ListNames() reply: %s", strerror(-r));
670 else {
671 char **t;
672
673 STRV_FOREACH(t, l)
674 /* This is a bit hacky, we say the
675 * owner of the name is the name
676 * itself, because we don't want the
677 * extra traffic to figure out the
678 * real owner. */
679 manager_dispatch_bus_name_owner_changed(m, *t, NULL, *t);
680
681 strv_free(l);
682 }
683
684 break;
685 }
686
687 default:
688 assert_not_reached("Invalid reply message");
689 }
690
691 dbus_message_unref(reply);
692 dbus_error_free(&error);
693 }
694
695 static int query_name_list(Manager *m) {
696 DBusMessage *message = NULL;
697 DBusPendingCall *pending = NULL;
698
699 /* Asks for the currently installed bus names */
700
701 if (!(message = dbus_message_new_method_call(
702 DBUS_SERVICE_DBUS,
703 DBUS_PATH_DBUS,
704 DBUS_INTERFACE_DBUS,
705 "ListNames")))
706 goto oom;
707
708 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
709 goto oom;
710
711 if (!dbus_pending_call_set_notify(pending, query_name_list_pending_cb, m, NULL))
712 goto oom;
713
714 dbus_message_unref(message);
715 dbus_pending_call_unref(pending);
716
717 /* We simple ask for the list and don't wait for it. Sooner or
718 * later we'll get it. */
719
720 return 0;
721
722 oom:
723 if (pending) {
724 dbus_pending_call_cancel(pending);
725 dbus_pending_call_unref(pending);
726 }
727
728 if (message)
729 dbus_message_unref(message);
730
731 return -ENOMEM;
732 }
733
734 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
735 assert(m);
736 assert(bus);
737
738 dbus_connection_set_exit_on_disconnect(bus, FALSE);
739
740 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
741 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
742 log_error("Not enough memory");
743 return -ENOMEM;
744 }
745
746 if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
747 log_error("Not enough memory");
748 return -ENOMEM;
749 }
750
751 dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
752 return 0;
753 }
754
755 static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
756 return uid == 0;
757 }
758
759 static void bus_new_connection(
760 DBusServer *server,
761 DBusConnection *new_connection,
762 void *data) {
763
764 Manager *m = data;
765
766 assert(m);
767
768 if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
769 log_error("Too many concurrent connections.");
770 return;
771 }
772
773 dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
774
775 if (bus_setup_loop(m, new_connection) < 0)
776 return;
777
778 if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
779 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
780 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
781 !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
782 log_error("Not enough memory.");
783 return;
784 }
785
786 log_debug("Accepted connection on private bus.");
787
788 dbus_connection_ref(new_connection);
789 }
790
791 static int bus_init_system(Manager *m) {
792 DBusError error;
793 int r;
794
795 assert(m);
796
797 dbus_error_init(&error);
798
799 if (m->system_bus)
800 return 0;
801
802 if (m->running_as == MANAGER_SYSTEM && m->api_bus)
803 m->system_bus = m->api_bus;
804 else {
805 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
806 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
807 r = 0;
808 goto fail;
809 }
810
811 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
812 goto fail;
813 }
814
815 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
816 log_error("Not enough memory");
817 r = -EIO;
818 goto fail;
819 }
820
821 if (m->running_as != MANAGER_SYSTEM) {
822 dbus_bus_add_match(m->system_bus,
823 "type='signal',"
824 "interface='org.freedesktop.systemd1.Agent',"
825 "member='Released',"
826 "path='/org/freedesktop/systemd1/agent'",
827 &error);
828
829 if (dbus_error_is_set(&error)) {
830 log_error("Failed to register match: %s", error.message);
831 r = -EIO;
832 goto fail;
833 }
834 }
835
836 if (m->api_bus != m->system_bus) {
837 char *id;
838 log_debug("Successfully connected to system D-Bus bus %s as %s",
839 strnull((id = dbus_connection_get_server_id(m->system_bus))),
840 strnull(dbus_bus_get_unique_name(m->system_bus)));
841 dbus_free(id);
842 }
843
844 return 0;
845
846 fail:
847 bus_done_system(m);
848 dbus_error_free(&error);
849
850 return r;
851 }
852
853 static int bus_init_api(Manager *m) {
854 DBusError error;
855 int r;
856
857 assert(m);
858
859 dbus_error_init(&error);
860
861 if (m->api_bus)
862 return 0;
863
864 if (m->running_as == MANAGER_SYSTEM && m->system_bus)
865 m->api_bus = m->system_bus;
866 else {
867 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_USER ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
868 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
869 r = 0;
870 goto fail;
871 }
872
873 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
874 goto fail;
875 }
876
877 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
878 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
879 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
880 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
881 log_error("Not enough memory");
882 r = -ENOMEM;
883 goto fail;
884 }
885
886 /* Get NameOwnerChange messages */
887 dbus_bus_add_match(m->api_bus,
888 "type='signal',"
889 "sender='"DBUS_SERVICE_DBUS"',"
890 "interface='"DBUS_INTERFACE_DBUS"',"
891 "member='NameOwnerChanged',"
892 "path='"DBUS_PATH_DBUS"'",
893 &error);
894
895 if (dbus_error_is_set(&error)) {
896 log_error("Failed to register match: %s", error.message);
897 r = -EIO;
898 goto fail;
899 }
900
901 /* Get activation requests */
902 dbus_bus_add_match(m->api_bus,
903 "type='signal',"
904 "sender='"DBUS_SERVICE_DBUS"',"
905 "interface='org.freedesktop.systemd1.Activator',"
906 "member='ActivationRequest',"
907 "path='"DBUS_PATH_DBUS"'",
908 &error);
909
910 if (dbus_error_is_set(&error)) {
911 log_error("Failed to register match: %s", error.message);
912 r = -EIO;
913 goto fail;
914 }
915
916 if ((r = request_name(m)) < 0)
917 goto fail;
918
919 if ((r = query_name_list(m)) < 0)
920 goto fail;
921
922 if (m->api_bus != m->system_bus) {
923 char *id;
924 log_debug("Successfully connected to API D-Bus bus %s as %s",
925 strnull((id = dbus_connection_get_server_id(m->api_bus))),
926 strnull(dbus_bus_get_unique_name(m->api_bus)));
927 dbus_free(id);
928 }
929
930 return 0;
931
932 fail:
933 bus_done_api(m);
934 dbus_error_free(&error);
935
936 return r;
937 }
938
939 static int bus_init_private(Manager *m) {
940 DBusError error;
941 int r;
942 const char *const external_only[] = {
943 "EXTERNAL",
944 NULL
945 };
946
947 assert(m);
948
949 dbus_error_init(&error);
950
951 if (m->private_bus)
952 return 0;
953
954 /* We want the private bus only when running as init */
955 if (getpid() != 1)
956 return 0;
957
958 unlink("/dev/.run/systemd/private");
959 if (!(m->private_bus = dbus_server_listen("unix:path=/dev/.run/systemd/private", &error))) {
960 log_error("Failed to create private D-Bus server: %s", error.message);
961 r = -EIO;
962 goto fail;
963 }
964
965 if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
966 !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
967 !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
968 log_error("Not enough memory");
969 r = -ENOMEM;
970 goto fail;
971 }
972
973 dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
974
975 log_debug("Successfully created private D-Bus server.");
976
977 return 0;
978
979 fail:
980 bus_done_private(m);
981 dbus_error_free(&error);
982
983 return r;
984 }
985
986 int bus_init(Manager *m, bool try_bus_connect) {
987 int r;
988
989 if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
990 set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
991 log_error("Not enough memory");
992 return -ENOMEM;
993 }
994
995 if (m->name_data_slot < 0)
996 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
997 log_error("Not enough memory");
998 return -ENOMEM;
999 }
1000
1001 if (m->subscribed_data_slot < 0)
1002 if (!dbus_connection_allocate_data_slot(&m->subscribed_data_slot)) {
1003 log_error("Not enough memory");
1004 return -ENOMEM;
1005 }
1006
1007 if (try_bus_connect) {
1008 if ((r = bus_init_system(m)) < 0 ||
1009 (r = bus_init_api(m)) < 0)
1010 return r;
1011 }
1012
1013 if ((r = bus_init_private(m)) < 0)
1014 return r;
1015
1016 return 0;
1017 }
1018
1019 static void shutdown_connection(Manager *m, DBusConnection *c) {
1020 Set *s;
1021 Job *j;
1022 Iterator i;
1023
1024 HASHMAP_FOREACH(j, m->jobs, i)
1025 if (j->bus == c) {
1026 free(j->bus_client);
1027 j->bus_client = NULL;
1028
1029 j->bus = NULL;
1030 }
1031
1032 set_remove(m->bus_connections, c);
1033 set_remove(m->bus_connections_for_dispatch, c);
1034
1035 if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
1036 char *t;
1037
1038 while ((t = set_steal_first(s)))
1039 free(t);
1040
1041 set_free(s);
1042 }
1043
1044 if (m->queued_message_connection == c) {
1045 m->queued_message_connection = NULL;
1046
1047 if (m->queued_message) {
1048 dbus_message_unref(m->queued_message);
1049 m->queued_message = NULL;
1050 }
1051 }
1052
1053 dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1054 dbus_connection_flush(c);
1055 dbus_connection_close(c);
1056 dbus_connection_unref(c);
1057 }
1058
1059 static void bus_done_api(Manager *m) {
1060 assert(m);
1061
1062 if (m->api_bus) {
1063 if (m->system_bus == m->api_bus)
1064 m->system_bus = NULL;
1065
1066 shutdown_connection(m, m->api_bus);
1067 m->api_bus = NULL;
1068 }
1069
1070
1071 if (m->queued_message) {
1072 dbus_message_unref(m->queued_message);
1073 m->queued_message = NULL;
1074 }
1075 }
1076
1077 static void bus_done_system(Manager *m) {
1078 assert(m);
1079
1080 if (m->system_bus == m->api_bus)
1081 bus_done_api(m);
1082
1083 if (m->system_bus) {
1084 shutdown_connection(m, m->system_bus);
1085 m->system_bus = NULL;
1086 }
1087 }
1088
1089 static void bus_done_private(Manager *m) {
1090
1091 if (m->private_bus) {
1092 dbus_server_disconnect(m->private_bus);
1093 dbus_server_unref(m->private_bus);
1094 m->private_bus = NULL;
1095 }
1096 }
1097
1098 void bus_done(Manager *m) {
1099 DBusConnection *c;
1100
1101 bus_done_api(m);
1102 bus_done_system(m);
1103 bus_done_private(m);
1104
1105 while ((c = set_steal_first(m->bus_connections)))
1106 shutdown_connection(m, c);
1107
1108 while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1109 shutdown_connection(m, c);
1110
1111 set_free(m->bus_connections);
1112 set_free(m->bus_connections_for_dispatch);
1113
1114 if (m->name_data_slot >= 0)
1115 dbus_pending_call_free_data_slot(&m->name_data_slot);
1116
1117 if (m->subscribed_data_slot >= 0)
1118 dbus_connection_free_data_slot(&m->subscribed_data_slot);
1119 }
1120
1121 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1122 Manager *m = userdata;
1123 DBusMessage *reply;
1124 DBusError error;
1125 const char *name;
1126
1127 dbus_error_init(&error);
1128
1129 assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1130 assert_se(reply = dbus_pending_call_steal_reply(pending));
1131
1132 switch (dbus_message_get_type(reply)) {
1133
1134 case DBUS_MESSAGE_TYPE_ERROR:
1135
1136 assert_se(dbus_set_error_from_message(&error, reply));
1137 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1138 break;
1139
1140 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1141 uint32_t r;
1142
1143 if (!dbus_message_get_args(reply,
1144 &error,
1145 DBUS_TYPE_UINT32, &r,
1146 DBUS_TYPE_INVALID)) {
1147 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1148 break;
1149 }
1150
1151 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1152 break;
1153 }
1154
1155 default:
1156 assert_not_reached("Invalid reply message");
1157 }
1158
1159 dbus_message_unref(reply);
1160 dbus_error_free(&error);
1161 }
1162
1163 int bus_query_pid(Manager *m, const char *name) {
1164 DBusMessage *message = NULL;
1165 DBusPendingCall *pending = NULL;
1166 char *n = NULL;
1167
1168 assert(m);
1169 assert(name);
1170
1171 if (!(message = dbus_message_new_method_call(
1172 DBUS_SERVICE_DBUS,
1173 DBUS_PATH_DBUS,
1174 DBUS_INTERFACE_DBUS,
1175 "GetConnectionUnixProcessID")))
1176 goto oom;
1177
1178 if (!(dbus_message_append_args(
1179 message,
1180 DBUS_TYPE_STRING, &name,
1181 DBUS_TYPE_INVALID)))
1182 goto oom;
1183
1184 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1185 goto oom;
1186
1187 if (!(n = strdup(name)))
1188 goto oom;
1189
1190 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1191 goto oom;
1192
1193 n = NULL;
1194
1195 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1196 goto oom;
1197
1198 dbus_message_unref(message);
1199 dbus_pending_call_unref(pending);
1200
1201 return 0;
1202
1203 oom:
1204 free(n);
1205
1206 if (pending) {
1207 dbus_pending_call_cancel(pending);
1208 dbus_pending_call_unref(pending);
1209 }
1210
1211 if (message)
1212 dbus_message_unref(message);
1213
1214 return -ENOMEM;
1215 }
1216
1217 DBusHandlerResult bus_default_message_handler(
1218 Manager *m,
1219 DBusConnection *c,
1220 DBusMessage *message,
1221 const char *introspection,
1222 const char *interfaces,
1223 const BusProperty *properties) {
1224
1225 DBusError error;
1226 DBusMessage *reply = NULL;
1227 int r;
1228
1229 assert(m);
1230 assert(c);
1231 assert(message);
1232
1233 dbus_error_init(&error);
1234
1235 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
1236
1237 if (!(reply = dbus_message_new_method_return(message)))
1238 goto oom;
1239
1240 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
1241 goto oom;
1242
1243 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
1244 const char *interface, *property;
1245 const BusProperty *p;
1246
1247 if (!dbus_message_get_args(
1248 message,
1249 &error,
1250 DBUS_TYPE_STRING, &interface,
1251 DBUS_TYPE_STRING, &property,
1252 DBUS_TYPE_INVALID))
1253 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1254
1255 for (p = properties; p->property; p++)
1256 if (streq(p->interface, interface) && streq(p->property, property))
1257 break;
1258
1259 if (p->property) {
1260 DBusMessageIter iter, sub;
1261
1262 if (!(reply = dbus_message_new_method_return(message)))
1263 goto oom;
1264
1265 dbus_message_iter_init_append(reply, &iter);
1266
1267 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
1268 goto oom;
1269
1270 if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
1271
1272 if (r == -ENOMEM)
1273 goto oom;
1274
1275 dbus_message_unref(reply);
1276 return bus_send_error_reply(m, c, message, NULL, r);
1277 }
1278
1279 if (!dbus_message_iter_close_container(&iter, &sub))
1280 goto oom;
1281 } else {
1282 if (!nulstr_contains(interfaces, interface))
1283 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
1284 else
1285 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
1286
1287 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1288 }
1289
1290 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
1291 const char *interface;
1292 const BusProperty *p;
1293 DBusMessageIter iter, sub, sub2, sub3;
1294
1295 if (!dbus_message_get_args(
1296 message,
1297 &error,
1298 DBUS_TYPE_STRING, &interface,
1299 DBUS_TYPE_INVALID))
1300 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1301
1302 if (interface[0] && !nulstr_contains(interfaces, interface)) {
1303 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
1304 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1305 }
1306
1307 if (!(reply = dbus_message_new_method_return(message)))
1308 goto oom;
1309
1310 dbus_message_iter_init_append(reply, &iter);
1311
1312 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
1313 goto oom;
1314
1315 for (p = properties; p->property; p++) {
1316 if (interface[0] && !streq(p->interface, interface))
1317 continue;
1318
1319 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
1320 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
1321 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
1322 goto oom;
1323
1324 if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
1325
1326 if (r == -ENOMEM)
1327 goto oom;
1328
1329 dbus_message_unref(reply);
1330 return bus_send_error_reply(m, c, message, NULL, r);
1331 }
1332
1333 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
1334 !dbus_message_iter_close_container(&sub, &sub2))
1335 goto oom;
1336 }
1337
1338 if (!dbus_message_iter_close_container(&iter, &sub))
1339 goto oom;
1340
1341 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) {
1342 const char *interface, *property;
1343 DBusMessageIter iter;
1344 const BusProperty *p;
1345
1346 if (!dbus_message_iter_init(message, &iter) ||
1347 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1348 return bus_send_error_reply(m, c, message, NULL, -EINVAL);
1349
1350 dbus_message_iter_get_basic(&iter, &interface);
1351
1352 if (!dbus_message_iter_next(&iter) ||
1353 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
1354 return bus_send_error_reply(m, c, message, NULL, -EINVAL);
1355
1356 dbus_message_iter_get_basic(&iter, &property);
1357
1358 if (!dbus_message_iter_next(&iter) ||
1359 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
1360 dbus_message_iter_has_next(&iter))
1361 return bus_send_error_reply(m, c, message, NULL, -EINVAL);
1362
1363 for (p = properties; p->property; p++)
1364 if (streq(p->interface, interface) && streq(p->property, property))
1365 break;
1366
1367 if (p->set) {
1368 DBusMessageIter sub;
1369 char *sig;
1370
1371 dbus_message_iter_recurse(&iter, &sub);
1372
1373 if (!(sig = dbus_message_iter_get_signature(&sub)))
1374 goto oom;
1375
1376 if (!streq(sig, p->signature)) {
1377 dbus_free(sig);
1378 return bus_send_error_reply(m, c, message, NULL, -EINVAL);
1379 }
1380
1381 dbus_free(sig);
1382
1383 if ((r = p->set(m, &sub, property)) < 0) {
1384 if (r == -ENOMEM)
1385 goto oom;
1386 return bus_send_error_reply(m, c, message, NULL, r);
1387 }
1388
1389 if (!(reply = dbus_message_new_method_return(message)))
1390 goto oom;
1391 } else {
1392 if (p->property)
1393 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
1394 else if (!nulstr_contains(interfaces, interface))
1395 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
1396 else
1397 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
1398
1399 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1400 }
1401
1402 } else if (!nulstr_contains(interfaces, dbus_message_get_interface(message))) {
1403 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
1404 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1405 }
1406
1407 if (reply) {
1408 if (!dbus_connection_send(c, reply, NULL))
1409 goto oom;
1410
1411 dbus_message_unref(reply);
1412 return DBUS_HANDLER_RESULT_HANDLED;
1413 }
1414
1415 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1416
1417 oom:
1418 if (reply)
1419 dbus_message_unref(reply);
1420
1421 dbus_error_free(&error);
1422
1423 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1424 }
1425
1426 static const char *error_to_dbus(int error) {
1427
1428 switch(error) {
1429
1430 case -EINVAL:
1431 return DBUS_ERROR_INVALID_ARGS;
1432
1433 case -ENOMEM:
1434 return DBUS_ERROR_NO_MEMORY;
1435
1436 case -EPERM:
1437 case -EACCES:
1438 return DBUS_ERROR_ACCESS_DENIED;
1439
1440 case -ESRCH:
1441 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
1442
1443 case -ENOENT:
1444 return DBUS_ERROR_FILE_NOT_FOUND;
1445
1446 case -EEXIST:
1447 return DBUS_ERROR_FILE_EXISTS;
1448
1449 case -ETIMEDOUT:
1450 case -ETIME:
1451 return DBUS_ERROR_TIMEOUT;
1452
1453 case -EIO:
1454 return DBUS_ERROR_IO_ERROR;
1455
1456 case -ENETRESET:
1457 case -ECONNABORTED:
1458 case -ECONNRESET:
1459 return DBUS_ERROR_DISCONNECTED;
1460 }
1461
1462 return DBUS_ERROR_FAILED;
1463 }
1464
1465 DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
1466 DBusMessage *reply = NULL;
1467 const char *name, *text;
1468
1469 if (berror && dbus_error_is_set(berror)) {
1470 name = berror->name;
1471 text = berror->message;
1472 } else {
1473 name = error_to_dbus(error);
1474 text = strerror(-error);
1475 }
1476
1477 if (!(reply = dbus_message_new_error(message, name, text)))
1478 goto oom;
1479
1480 if (!dbus_connection_send(c, reply, NULL))
1481 goto oom;
1482
1483 dbus_message_unref(reply);
1484
1485 if (berror)
1486 dbus_error_free(berror);
1487
1488 return DBUS_HANDLER_RESULT_HANDLED;
1489
1490 oom:
1491 if (reply)
1492 dbus_message_unref(reply);
1493
1494 if (berror)
1495 dbus_error_free(berror);
1496
1497 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1498 }
1499
1500 int bus_broadcast(Manager *m, DBusMessage *message) {
1501 bool oom = false;
1502 Iterator i;
1503 DBusConnection *c;
1504
1505 assert(m);
1506 assert(message);
1507
1508 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1509 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1510 oom = !dbus_connection_send(c, message, NULL);
1511
1512 SET_FOREACH(c, m->bus_connections, i)
1513 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1514 oom = !dbus_connection_send(c, message, NULL);
1515
1516 return oom ? -ENOMEM : 0;
1517 }
1518
1519 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1520 const char *t = data;
1521
1522 assert(m);
1523 assert(i);
1524 assert(property);
1525
1526 if (!t)
1527 t = "";
1528
1529 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
1530 return -ENOMEM;
1531
1532 return 0;
1533 }
1534
1535 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1536 DBusMessageIter sub;
1537 char **t = data;
1538
1539 assert(m);
1540 assert(i);
1541 assert(property);
1542
1543 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
1544 return -ENOMEM;
1545
1546 STRV_FOREACH(t, t)
1547 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1548 return -ENOMEM;
1549
1550 if (!dbus_message_iter_close_container(i, &sub))
1551 return -ENOMEM;
1552
1553 return 0;
1554 }
1555
1556 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1557 bool *b = data;
1558 dbus_bool_t db;
1559
1560 assert(m);
1561 assert(i);
1562 assert(property);
1563 assert(b);
1564
1565 db = *b;
1566
1567 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1568 return -ENOMEM;
1569
1570 return 0;
1571 }
1572
1573 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1574 assert(m);
1575 assert(i);
1576 assert(property);
1577 assert(data);
1578
1579 /* Let's ensure that pid_t is actually 64bit, and hence this
1580 * function can be used for usec_t */
1581 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1582
1583 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1584 return -ENOMEM;
1585
1586 return 0;
1587 }
1588
1589 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1590 assert(m);
1591 assert(i);
1592 assert(property);
1593 assert(data);
1594
1595 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1596 * hence this function can be used for pid_t/mode_t */
1597 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1598 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1599 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1600
1601 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1602 return -ENOMEM;
1603
1604 return 0;
1605 }
1606
1607 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1608 assert(m);
1609 assert(i);
1610 assert(property);
1611 assert(data);
1612
1613 assert_cc(sizeof(int32_t) == sizeof(int));
1614
1615 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
1616 return -ENOMEM;
1617
1618 return 0;
1619 }
1620
1621 int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1622 uint64_t u;
1623
1624 assert(m);
1625 assert(i);
1626 assert(property);
1627 assert(data);
1628
1629 u = (uint64_t) *(size_t*) data;
1630
1631 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1632 return -ENOMEM;
1633
1634 return 0;
1635 }
1636
1637 int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1638 uint64_t u;
1639
1640 assert(m);
1641 assert(i);
1642 assert(property);
1643 assert(data);
1644
1645 u = (uint64_t) *(unsigned long*) data;
1646
1647 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1648 return -ENOMEM;
1649
1650 return 0;
1651 }
1652
1653 int bus_parse_strv(DBusMessage *m, char ***_l) {
1654 DBusMessageIter iter, sub;
1655 unsigned n = 0, i = 0;
1656 char **l;
1657
1658 assert(m);
1659 assert(_l);
1660
1661 if (!dbus_message_iter_init(m, &iter) ||
1662 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1663 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1664 return -EINVAL;
1665
1666 dbus_message_iter_recurse(&iter, &sub);
1667
1668 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1669 n++;
1670 dbus_message_iter_next(&sub);
1671 }
1672
1673 if (!(l = new(char*, n+1)))
1674 return -ENOMEM;
1675
1676 assert_se(dbus_message_iter_init(m, &iter));
1677 dbus_message_iter_recurse(&iter, &sub);
1678
1679 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1680 const char *s;
1681
1682 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1683 dbus_message_iter_get_basic(&sub, &s);
1684
1685 if (!(l[i++] = strdup(s))) {
1686 strv_free(l);
1687 return -ENOMEM;
1688 }
1689
1690 dbus_message_iter_next(&sub);
1691 }
1692
1693 assert(i == n);
1694 l[i] = NULL;
1695
1696 if (_l)
1697 *_l = l;
1698
1699 return 0;
1700 }
1701
1702 bool bus_has_subscriber(Manager *m) {
1703 Iterator i;
1704 DBusConnection *c;
1705
1706 assert(m);
1707
1708 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1709 if (bus_connection_has_subscriber(m, c))
1710 return true;
1711
1712 SET_FOREACH(c, m->bus_connections, i)
1713 if (bus_connection_has_subscriber(m, c))
1714 return true;
1715
1716 return false;
1717 }
1718
1719 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1720 assert(m);
1721 assert(c);
1722
1723 return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1724 }
1725
1726 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
1727 DBusMessage *m;
1728 DBusMessageIter iter, sub;
1729 const char *i;
1730
1731 assert(interface);
1732 assert(properties);
1733
1734 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
1735 goto oom;
1736
1737 dbus_message_iter_init_append(m, &iter);
1738
1739 /* We won't send any property values, since they might be
1740 * large and sometimes not cheap to generated */
1741
1742 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
1743 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
1744 !dbus_message_iter_close_container(&iter, &sub) ||
1745 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
1746 goto oom;
1747
1748 NULSTR_FOREACH(i, properties)
1749 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
1750 goto oom;
1751
1752 if (!dbus_message_iter_close_container(&iter, &sub))
1753 goto oom;
1754
1755 return m;
1756
1757 oom:
1758 if (m)
1759 dbus_message_unref(m);
1760
1761 return NULL;
1762 }