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