]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/dbus.c
service: make chain of main commands and control commands independent of each other...
[thirdparty/systemd.git] / src / dbus.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ea430986 2
a7334b09
LP
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
ea430986
LP
22#include <sys/epoll.h>
23#include <sys/timerfd.h>
24#include <errno.h>
25#include <unistd.h>
4139c1b2 26#include <dbus/dbus.h>
ea430986
LP
27
28#include "dbus.h"
29#include "log.h"
30#include "strv.h"
8e274523 31#include "cgroup.h"
4139c1b2
LP
32#include "dbus-unit.h"
33#include "dbus-job.h"
34#include "dbus-manager.h"
4288f619
LP
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"
871d7de4 43#include "dbus-timer.h"
01f78473 44#include "dbus-path.h"
398ef8ba 45#include "bus-errors.h"
8f6df3fa 46#include "special.h"
4288f619 47
5e8d1c9a
LP
48#define CONNECTIONS_MAX 52
49
4288f619
LP
50static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
51static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
52
53const 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,
871d7de4 67 "org.freedesktop.systemd1.Timer", bus_timer_interface,
01f78473 68 "org.freedesktop.systemd1.Path", bus_path_interface,
4288f619
LP
69 NULL
70};
ea430986 71
0034c15c 72static const char *error_to_dbus(int error);
5e8d1c9a
LP
73static void bus_done_api(Manager *m);
74static void bus_done_system(Manager *m);
75static void bus_done_private(Manager *m);
923f8d76 76static void shutdown_connection(Manager *m, DBusConnection *c);
0034c15c 77
5e8d1c9a 78static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
8e274523
LP
79 Manager *m = data;
80
81 assert(bus);
82 assert(m);
2e317f52 83
5e8d1c9a
LP
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. */
8e274523 87
5e8d1c9a
LP
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);
8e274523
LP
92}
93
ea430986
LP
94static 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
114static 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
129void 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
8e274523 136 if (!dbus_watch_get_enabled(w->data.bus_watch))
ea430986
LP
137 return;
138
139 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
140}
141
142static 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
192static 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
211static 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
229static 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
248void 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
261static 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
292fail:
293 if (w->fd >= 0)
294 close_nointr_nofail(w->fd);
295
296 free(w);
297 return FALSE;
298}
299
300static 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
316static 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
cf3e2471 331static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
c1e1601e
LP
332 Manager *m = data;
333 DBusError error;
0034c15c 334 DBusMessage *reply = NULL;
c1e1601e
LP
335
336 assert(connection);
337 assert(message);
338 assert(m);
339
340 dbus_error_init(&error);
341
1e001f52
LP
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));
c1e1601e
LP
348
349 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
9028d0ec 350 log_debug("API D-Bus connection terminated.");
f278026d 351 bus_done_api(m);
c1e1601e
LP
352
353 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
05e343b7 354 const char *name, *old_owner, *new_owner;
c1e1601e
LP
355
356 if (!dbus_message_get_args(message, &error,
357 DBUS_TYPE_STRING, &name,
05e343b7
LP
358 DBUS_TYPE_STRING, &old_owner,
359 DBUS_TYPE_STRING, &new_owner,
c1e1601e
LP
360 DBUS_TYPE_INVALID))
361 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
362 else {
a567261a
LP
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)));
05e343b7
LP
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);
c1e1601e 373 }
0034c15c
LP
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
46018844
LP
385 log_debug("Got D-Bus activation request for %s", name);
386
8f6df3fa
LP
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);
0034c15c 393
8f6df3fa
LP
394 if (r >= 0 && u->meta.refuse_manual_start)
395 r = -EPERM;
0034c15c 396
8f6df3fa
LP
397 if (r >= 0)
398 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
399 }
0034c15c
LP
400
401 if (r < 0) {
402 const char *id, *text;
403
2a8cd298 404 log_debug("D-Bus activation failed for %s: %s", name, strerror(-r));
af25ec12 405
0034c15c
LP
406 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
407 goto oom;
408
398ef8ba
LP
409 id = error.name ? error.name : error_to_dbus(r);
410 text = bus_error(&error, r);
0034c15c
LP
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 spwaned now */
422 }
c1e1601e
LP
423 }
424
425 dbus_error_free(&error);
0034c15c
LP
426
427 if (reply) {
428 if (!dbus_connection_send(connection, reply, NULL))
429 goto oom;
430
431 dbus_message_unref(reply);
432 }
433
c1e1601e 434 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
0034c15c
LP
435
436oom:
437 if (reply)
438 dbus_message_unref(reply);
439
440 dbus_error_free(&error);
441
442 return DBUS_HANDLER_RESULT_NEED_MEMORY;
c1e1601e
LP
443}
444
cf3e2471 445static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
8e274523
LP
446 Manager *m = data;
447 DBusError error;
448
449 assert(connection);
450 assert(message);
451 assert(m);
452
453 dbus_error_init(&error);
454
1e001f52
LP
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));
8e274523
LP
462
463 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
91805b3b 464 log_debug("System D-Bus connection terminated.");
f278026d 465 bus_done_system(m);
8e274523 466
53c6a358
LP
467 } else if (m->running_as != MANAGER_SYSTEM &&
468 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
469
8e274523
LP
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
5e8d1c9a
LP
484static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
485 Manager *m = data;
3c661fad 486 DBusError error;
5e8d1c9a
LP
487
488 assert(connection);
489 assert(message);
490 assert(m);
491
3c661fad
LP
492 dbus_error_init(&error);
493
1e001f52
LP
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));
5e8d1c9a 500
923f8d76
LP
501 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
502 shutdown_connection(m, connection);
53c6a358
LP
503 else if (m->running_as == MANAGER_SYSTEM &&
504 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
505
3c661fad
LP
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);
53c6a358
LP
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);
3c661fad
LP
520 }
521
522 dbus_error_free(&error);
5e8d1c9a
LP
523
524 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
525}
526
c1e1601e 527unsigned bus_dispatch(Manager *m) {
5e8d1c9a
LP
528 DBusConnection *c;
529
ea430986
LP
530 assert(m);
531
a16e1123
LP
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
7b97f477
LP
537 if (m->queued_message_connection)
538 if (!dbus_connection_send(m->queued_message_connection, m->queued_message, NULL))
5e8d1c9a 539 return 0;
a16e1123
LP
540
541 dbus_message_unref(m->queued_message);
542 m->queued_message = NULL;
7b97f477 543 m->queued_message_connection = NULL;
a16e1123
LP
544 }
545
5e8d1c9a
LP
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);
fd18e1f4
LP
549
550 return 1;
551 }
c1e1601e 552
8e274523 553 return 0;
ea430986
LP
554}
555
05e343b7 556static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
61902ea3
LP
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
ea430986 599static int request_name(Manager *m) {
ea430986
LP
600 const char *name = "org.freedesktop.systemd1";
601 uint32_t flags = 0;
05e343b7
LP
602 DBusMessage *message = NULL;
603 DBusPendingCall *pending = NULL;
ea430986
LP
604
605 if (!(message = dbus_message_new_method_call(
606 DBUS_SERVICE_DBUS,
607 DBUS_PATH_DBUS,
608 DBUS_INTERFACE_DBUS,
609 "RequestName")))
05e343b7 610 goto oom;
ea430986
LP
611
612 if (!dbus_message_append_args(
613 message,
614 DBUS_TYPE_STRING, &name,
615 DBUS_TYPE_UINT32, &flags,
05e343b7
LP
616 DBUS_TYPE_INVALID))
617 goto oom;
ea430986 618
05e343b7
LP
619 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
620 goto oom;
ea430986 621
05e343b7
LP
622 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
623 goto oom;
ea430986
LP
624
625 dbus_message_unref(message);
61902ea3
LP
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
ea430986 631 return 0;
05e343b7
LP
632
633oom:
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;
ea430986
LP
643}
644
6dded4c7
LP
645static 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
695static 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
722oom:
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
8e274523
LP
734static int bus_setup_loop(Manager *m, DBusConnection *bus) {
735 assert(m);
736 assert(bus);
737
738 dbus_connection_set_exit_on_disconnect(bus, FALSE);
fd18e1f4 739
8e274523 740 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
5e8d1c9a
LP
741 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
742 log_error("Not enough memory");
8e274523 743 return -ENOMEM;
5e8d1c9a 744 }
8e274523 745
5e8d1c9a
LP
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);
8e274523
LP
752 return 0;
753}
754
5e8d1c9a
LP
755static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
756 return uid == 0;
757}
758
759static 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
791static int bus_init_system(Manager *m) {
ea430986 792 DBusError error;
ea430986
LP
793 int r;
794
795 assert(m);
796
ea430986 797 dbus_error_init(&error);
ea430986 798
f278026d
LP
799 if (m->system_bus)
800 return 0;
8e274523 801
a3d4e06d 802 if (m->running_as == MANAGER_SYSTEM && m->api_bus)
f278026d
LP
803 m->system_bus = m->api_bus;
804 else {
8e274523 805 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
266e525c 806 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
5e8d1c9a
LP
807 r = 0;
808 goto fail;
8e274523
LP
809 }
810
5e8d1c9a
LP
811 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
812 goto fail;
f278026d
LP
813 }
814
815 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
5e8d1c9a
LP
816 log_error("Not enough memory");
817 r = -EIO;
818 goto fail;
c1e1601e
LP
819 }
820
53c6a358
LP
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 }
ea430986
LP
834 }
835
9014a8bd
LP
836 if (m->api_bus != m->system_bus) {
837 char *id;
91805b3b 838 log_debug("Successfully connected to system D-Bus bus %s as %s",
9014a8bd
LP
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 }
f278026d 843
f278026d 844 return 0;
5e8d1c9a
LP
845
846fail:
847 bus_done_system(m);
848 dbus_error_free(&error);
849
850 return r;
f278026d
LP
851}
852
5e8d1c9a 853static int bus_init_api(Manager *m) {
f278026d 854 DBusError error;
f278026d
LP
855 int r;
856
857 assert(m);
858
859 dbus_error_init(&error);
860
861 if (m->api_bus)
862 return 0;
863
a3d4e06d 864 if (m->running_as == MANAGER_SYSTEM && m->system_bus)
f278026d
LP
865 m->api_bus = m->system_bus;
866 else {
af2d49f7 867 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_USER ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
266e525c 868 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
5e8d1c9a
LP
869 r = 0;
870 goto fail;
f278026d
LP
871 }
872
5e8d1c9a
LP
873 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
874 goto fail;
ea430986
LP
875 }
876
f278026d
LP
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)) {
5e8d1c9a
LP
881 log_error("Not enough memory");
882 r = -ENOMEM;
883 goto fail;
f278026d
LP
884 }
885
0034c15c 886 /* Get NameOwnerChange messages */
f278026d 887 dbus_bus_add_match(m->api_bus,
8e274523 888 "type='signal',"
f278026d
LP
889 "sender='"DBUS_SERVICE_DBUS"',"
890 "interface='"DBUS_INTERFACE_DBUS"',"
fd0d7f7a 891 "member='NameOwnerChanged',"
f278026d 892 "path='"DBUS_PATH_DBUS"'",
8e274523
LP
893 &error);
894
895 if (dbus_error_is_set(&error)) {
896 log_error("Failed to register match: %s", error.message);
5e8d1c9a
LP
897 r = -EIO;
898 goto fail;
8e274523
LP
899 }
900
0034c15c
LP
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',"
cf3e2471 906 "member='ActivationRequest',"
0034c15c
LP
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);
5e8d1c9a
LP
912 r = -EIO;
913 goto fail;
0034c15c
LP
914 }
915
5e8d1c9a
LP
916 if ((r = request_name(m)) < 0)
917 goto fail;
ea430986 918
5e8d1c9a
LP
919 if ((r = query_name_list(m)) < 0)
920 goto fail;
6dded4c7 921
9014a8bd
LP
922 if (m->api_bus != m->system_bus) {
923 char *id;
91805b3b 924 log_debug("Successfully connected to API D-Bus bus %s as %s",
9014a8bd
LP
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 }
8e274523 929
5e8d1c9a
LP
930 return 0;
931
932fail:
933 bus_done_api(m);
934 dbus_error_free(&error);
935
936 return r;
937}
938
939static 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 */
a821caaa 955 if (getpid() != 1)
5e8d1c9a
LP
956 return 0;
957
958 if (!(m->private_bus = dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
959 log_error("Failed to create private D-Bus server: %s", error.message);
960 r = -EIO;
961 goto fail;
962 }
963
964 if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
965 !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
966 !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
967 log_error("Not enough memory");
968 r = -ENOMEM;
969 goto fail;
970 }
971
972 dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
973
54165a39 974 log_debug("Successfully created private D-Bus server.");
5e8d1c9a
LP
975
976 return 0;
977
978fail:
979 bus_done_private(m);
980 dbus_error_free(&error);
981
982 return r;
983}
984
985int bus_init(Manager *m) {
986 int r;
987
988 if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
989 set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
990 log_error("Not enough memory");
991 return -ENOMEM;
992 }
993
994 if (m->name_data_slot < 0)
995 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
996 log_error("Not enough memory");
f278026d 997 return -ENOMEM;
5e8d1c9a
LP
998 }
999
a567261a 1000 if (m->subscribed_data_slot < 0)
d83685ac 1001 if (!dbus_connection_allocate_data_slot(&m->subscribed_data_slot)) {
a567261a
LP
1002 log_error("Not enough memory");
1003 return -ENOMEM;
1004 }
1005
5e8d1c9a
LP
1006 if ((r = bus_init_system(m)) < 0 ||
1007 (r = bus_init_api(m)) < 0 ||
1008 (r = bus_init_private(m)) < 0)
1009 return r;
f278026d 1010
ea430986
LP
1011 return 0;
1012}
1013
fae20b11 1014static void shutdown_connection(Manager *m, DBusConnection *c) {
a567261a
LP
1015 Set *s;
1016 Job *j;
1017 Iterator i;
1018
1019 HASHMAP_FOREACH(j, m->jobs, i)
1020 if (j->bus == c) {
1021 free(j->bus_client);
1022 j->bus_client = NULL;
1023
1024 j->bus = NULL;
1025 }
1026
fae20b11
LP
1027 set_remove(m->bus_connections, c);
1028 set_remove(m->bus_connections_for_dispatch, c);
1029
a567261a
LP
1030 if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
1031 char *t;
1032
1033 while ((t = set_steal_first(s)))
1034 free(t);
1035
1036 set_free(s);
1037 }
1038
7b97f477
LP
1039 if (m->queued_message_connection == c) {
1040 m->queued_message_connection = NULL;
1041
1042 if (m->queued_message) {
1043 dbus_message_unref(m->queued_message);
1044 m->queued_message = NULL;
1045 }
1046 }
1047
5e8d1c9a
LP
1048 dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1049 dbus_connection_flush(c);
1050 dbus_connection_close(c);
1051 dbus_connection_unref(c);
1052}
1053
1054static void bus_done_api(Manager *m) {
ea430986
LP
1055 assert(m);
1056
f278026d
LP
1057 if (m->api_bus) {
1058 if (m->system_bus == m->api_bus)
1059 m->system_bus = NULL;
1060
fae20b11 1061 shutdown_connection(m, m->api_bus);
f278026d 1062 m->api_bus = NULL;
ea430986 1063 }
c1e1601e 1064
05e343b7 1065
a16e1123
LP
1066 if (m->queued_message) {
1067 dbus_message_unref(m->queued_message);
1068 m->queued_message = NULL;
1069 }
ea430986
LP
1070}
1071
5e8d1c9a 1072static void bus_done_system(Manager *m) {
f278026d
LP
1073 assert(m);
1074
1075 if (m->system_bus == m->api_bus)
1076 bus_done_api(m);
1077
1078 if (m->system_bus) {
fae20b11 1079 shutdown_connection(m, m->system_bus);
f278026d
LP
1080 m->system_bus = NULL;
1081 }
1082}
1083
5e8d1c9a
LP
1084static void bus_done_private(Manager *m) {
1085
1086 if (m->private_bus) {
1087 dbus_server_disconnect(m->private_bus);
1088 dbus_server_unref(m->private_bus);
1089 m->private_bus = NULL;
1090 }
1091}
1092
1093void bus_done(Manager *m) {
1094 DBusConnection *c;
1095
1096 bus_done_api(m);
1097 bus_done_system(m);
1098 bus_done_private(m);
1099
1100 while ((c = set_steal_first(m->bus_connections)))
fae20b11 1101 shutdown_connection(m, c);
5e8d1c9a
LP
1102
1103 while ((c = set_steal_first(m->bus_connections_for_dispatch)))
fae20b11 1104 shutdown_connection(m, c);
5e8d1c9a
LP
1105
1106 set_free(m->bus_connections);
1107 set_free(m->bus_connections_for_dispatch);
1108
1109 if (m->name_data_slot >= 0)
1110 dbus_pending_call_free_data_slot(&m->name_data_slot);
a567261a
LP
1111
1112 if (m->subscribed_data_slot >= 0)
d83685ac 1113 dbus_connection_free_data_slot(&m->subscribed_data_slot);
5e8d1c9a
LP
1114}
1115
05e343b7
LP
1116static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1117 Manager *m = userdata;
1118 DBusMessage *reply;
1119 DBusError error;
1120 const char *name;
1121
1122 dbus_error_init(&error);
1123
a567261a 1124 assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
05e343b7
LP
1125 assert_se(reply = dbus_pending_call_steal_reply(pending));
1126
1127 switch (dbus_message_get_type(reply)) {
1128
1129 case DBUS_MESSAGE_TYPE_ERROR:
1130
1131 assert_se(dbus_set_error_from_message(&error, reply));
1132 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1133 break;
1134
1135 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1136 uint32_t r;
1137
1138 if (!dbus_message_get_args(reply,
1139 &error,
1140 DBUS_TYPE_UINT32, &r,
1141 DBUS_TYPE_INVALID)) {
1142 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1143 break;
1144 }
1145
1146 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1147 break;
1148 }
1149
1150 default:
1151 assert_not_reached("Invalid reply message");
1152 }
1153
1154 dbus_message_unref(reply);
1155 dbus_error_free(&error);
1156}
1157
1158int bus_query_pid(Manager *m, const char *name) {
1159 DBusMessage *message = NULL;
1160 DBusPendingCall *pending = NULL;
1161 char *n = NULL;
1162
1163 assert(m);
1164 assert(name);
1165
1166 if (!(message = dbus_message_new_method_call(
1167 DBUS_SERVICE_DBUS,
1168 DBUS_PATH_DBUS,
1169 DBUS_INTERFACE_DBUS,
1170 "GetConnectionUnixProcessID")))
1171 goto oom;
1172
1173 if (!(dbus_message_append_args(
1174 message,
1175 DBUS_TYPE_STRING, &name,
1176 DBUS_TYPE_INVALID)))
1177 goto oom;
1178
1179 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1180 goto oom;
1181
1182 if (!(n = strdup(name)))
1183 goto oom;
1184
1185 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1186 goto oom;
1187
1188 n = NULL;
1189
1190 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1191 goto oom;
1192
1193 dbus_message_unref(message);
1194 dbus_pending_call_unref(pending);
1195
1196 return 0;
1197
1198oom:
1199 free(n);
1200
1201 if (pending) {
1202 dbus_pending_call_cancel(pending);
1203 dbus_pending_call_unref(pending);
1204 }
1205
1206 if (message)
1207 dbus_message_unref(message);
1208
1209 return -ENOMEM;
1210}
1211
5e8d1c9a 1212DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
ea430986
LP
1213 DBusError error;
1214 DBusMessage *reply = NULL;
1215 int r;
1216
1217 assert(m);
1218 assert(message);
1219
1220 dbus_error_init(&error);
1221
1222 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
1223
1224 if (!(reply = dbus_message_new_method_return(message)))
1225 goto oom;
1226
1227 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
1228 goto oom;
1229
1230 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
1231 const char *interface, *property;
1232 const BusProperty *p;
1233
1234 if (!dbus_message_get_args(
1235 message,
1236 &error,
1237 DBUS_TYPE_STRING, &interface,
1238 DBUS_TYPE_STRING, &property,
1239 DBUS_TYPE_INVALID))
5e8d1c9a 1240 return bus_send_error_reply(m, c, message, &error, -EINVAL);
ea430986
LP
1241
1242 for (p = properties; p->property; p++)
1243 if (streq(p->interface, interface) && streq(p->property, property))
1244 break;
1245
1246 if (p->property) {
1247 DBusMessageIter iter, sub;
1248
1249 if (!(reply = dbus_message_new_method_return(message)))
1250 goto oom;
1251
1252 dbus_message_iter_init_append(reply, &iter);
1253
1254 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
1255 goto oom;
1256
b9f49ee4 1257 if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
ea430986
LP
1258
1259 if (r == -ENOMEM)
1260 goto oom;
1261
1262 dbus_message_unref(reply);
5e8d1c9a 1263 return bus_send_error_reply(m, c, message, NULL, r);
ea430986
LP
1264 }
1265
1266 if (!dbus_message_iter_close_container(&iter, &sub))
1267 goto oom;
1268 }
c4e2ceae 1269
ea430986
LP
1270 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
1271 const char *interface;
1272 const BusProperty *p;
1273 DBusMessageIter iter, sub, sub2, sub3;
ea430986
LP
1274
1275 if (!dbus_message_get_args(
1276 message,
1277 &error,
1278 DBUS_TYPE_STRING, &interface,
1279 DBUS_TYPE_INVALID))
5e8d1c9a 1280 return bus_send_error_reply(m, c, message, &error, -EINVAL);
ea430986
LP
1281
1282 if (!(reply = dbus_message_new_method_return(message)))
1283 goto oom;
1284
1285 dbus_message_iter_init_append(reply, &iter);
1286
1287 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
1288 goto oom;
1289
1290 for (p = properties; p->property; p++) {
09c66196 1291 if (interface[0] && !streq(p->interface, interface))
ea430986
LP
1292 continue;
1293
c401a1e0 1294 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
ea430986
LP
1295 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
1296 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
1297 goto oom;
1298
b9f49ee4 1299 if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
ea430986
LP
1300
1301 if (r == -ENOMEM)
1302 goto oom;
1303
1304 dbus_message_unref(reply);
5e8d1c9a 1305 return bus_send_error_reply(m, c, message, NULL, r);
ea430986
LP
1306 }
1307
1308 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
1309 !dbus_message_iter_close_container(&sub, &sub2))
1310 goto oom;
ea430986
LP
1311 }
1312
1313 if (!dbus_message_iter_close_container(&iter, &sub))
1314 goto oom;
1315 }
1316
1317 if (reply) {
5e8d1c9a 1318 if (!dbus_connection_send(c, reply, NULL))
ea430986
LP
1319 goto oom;
1320
1321 dbus_message_unref(reply);
1322 return DBUS_HANDLER_RESULT_HANDLED;
1323 }
1324
1325 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1326
1327oom:
1328 if (reply)
1329 dbus_message_unref(reply);
1330
1331 dbus_error_free(&error);
1332
1333 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1334}
1335
ea430986
LP
1336static const char *error_to_dbus(int error) {
1337
1338 switch(error) {
1339
1340 case -EINVAL:
1341 return DBUS_ERROR_INVALID_ARGS;
1342
1343 case -ENOMEM:
1344 return DBUS_ERROR_NO_MEMORY;
1345
1346 case -EPERM:
1347 case -EACCES:
1348 return DBUS_ERROR_ACCESS_DENIED;
1349
1350 case -ESRCH:
1351 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
1352
1353 case -ENOENT:
1354 return DBUS_ERROR_FILE_NOT_FOUND;
1355
1356 case -EEXIST:
1357 return DBUS_ERROR_FILE_EXISTS;
1358
1359 case -ETIMEDOUT:
1360 return DBUS_ERROR_TIMEOUT;
1361
1362 case -EIO:
1363 return DBUS_ERROR_IO_ERROR;
1364
1365 case -ENETRESET:
1366 case -ECONNABORTED:
1367 case -ECONNRESET:
1368 return DBUS_ERROR_DISCONNECTED;
1369 }
1370
1371 return DBUS_ERROR_FAILED;
1372}
1373
398ef8ba 1374DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
ea430986
LP
1375 DBusMessage *reply = NULL;
1376 const char *name, *text;
1377
398ef8ba
LP
1378 if (berror && dbus_error_is_set(berror)) {
1379 name = berror->name;
1380 text = berror->message;
ea430986
LP
1381 } else {
1382 name = error_to_dbus(error);
1383 text = strerror(-error);
1384 }
1385
1386 if (!(reply = dbus_message_new_error(message, name, text)))
1387 goto oom;
1388
5e8d1c9a 1389 if (!dbus_connection_send(c, reply, NULL))
ea430986
LP
1390 goto oom;
1391
1392 dbus_message_unref(reply);
1393
398ef8ba
LP
1394 if (berror)
1395 dbus_error_free(berror);
ea430986
LP
1396
1397 return DBUS_HANDLER_RESULT_HANDLED;
1398
1399oom:
1400 if (reply)
1401 dbus_message_unref(reply);
1402
398ef8ba
LP
1403 if (berror)
1404 dbus_error_free(berror);
ea430986
LP
1405
1406 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1407}
1408
5e8d1c9a
LP
1409int bus_broadcast(Manager *m, DBusMessage *message) {
1410 bool oom = false;
1411 Iterator i;
1412 DBusConnection *c;
1413
1414 assert(m);
1415 assert(message);
1416
1417 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
a3d4e06d 1418 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
5e8d1c9a
LP
1419 oom = !dbus_connection_send(c, message, NULL);
1420
1421 SET_FOREACH(c, m->bus_connections, i)
a3d4e06d 1422 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
5e8d1c9a
LP
1423 oom = !dbus_connection_send(c, message, NULL);
1424
1425 return oom ? -ENOMEM : 0;
1426}
1427
ea430986
LP
1428int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1429 const char *t = data;
1430
1431 assert(m);
1432 assert(i);
1433 assert(property);
38131695
LP
1434
1435 if (!t)
1436 t = "";
ea430986
LP
1437
1438 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
1439 return -ENOMEM;
1440
1441 return 0;
1442}
1443
1444int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1445 DBusMessageIter sub;
1446 char **t = data;
1447
1448 assert(m);
1449 assert(i);
1450 assert(property);
1451
1452 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
1453 return -ENOMEM;
1454
1455 STRV_FOREACH(t, t)
1456 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1457 return -ENOMEM;
1458
1459 if (!dbus_message_iter_close_container(i, &sub))
1460 return -ENOMEM;
1461
1462 return 0;
1463}
1464
1465int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1466 bool *b = data;
1467 dbus_bool_t db;
1468
1469 assert(m);
1470 assert(i);
1471 assert(property);
1472 assert(b);
1473
1474 db = *b;
1475
1476 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1477 return -ENOMEM;
1478
1479 return 0;
1480}
38131695
LP
1481
1482int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1483 assert(m);
1484 assert(i);
1485 assert(property);
1486 assert(data);
1487
4139c1b2
LP
1488 /* Let's ensure that pid_t is actually 64bit, and hence this
1489 * function can be used for usec_t */
1490 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1491
38131695
LP
1492 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1493 return -ENOMEM;
1494
1495 return 0;
1496}
1497
1498int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1499 assert(m);
1500 assert(i);
1501 assert(property);
1502 assert(data);
1503
4139c1b2
LP
1504 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1505 * hence this function can be used for pid_t/mode_t */
1506 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1507 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1508 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1509
38131695
LP
1510 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1511 return -ENOMEM;
1512
1513 return 0;
1514}
4139c1b2
LP
1515
1516int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1517 assert(m);
1518 assert(i);
1519 assert(property);
1520 assert(data);
1521
1522 assert_cc(sizeof(int32_t) == sizeof(int));
1523
1524 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
1525 return -ENOMEM;
1526
1527 return 0;
1528}
1137a57c 1529
4fd5948e
LP
1530int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1531 uint64_t u;
1532
1533 assert(m);
1534 assert(i);
1535 assert(property);
1536 assert(data);
1537
1538 u = (uint64_t) *(size_t*) data;
1539
1540 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1541 return -ENOMEM;
1542
1543 return 0;
1544}
1545
82c121a4
LP
1546int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1547 uint64_t u;
1548
1549 assert(m);
1550 assert(i);
1551 assert(property);
1552 assert(data);
1553
1554 u = (uint64_t) *(unsigned long*) data;
1555
1556 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1557 return -ENOMEM;
1558
1559 return 0;
1560}
1561
1137a57c
LP
1562int bus_parse_strv(DBusMessage *m, char ***_l) {
1563 DBusMessageIter iter, sub;
1564 unsigned n = 0, i = 0;
1565 char **l;
1566
1567 assert(m);
1568 assert(_l);
1569
1570 if (!dbus_message_iter_init(m, &iter) ||
1571 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1572 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1573 return -EINVAL;
1574
1575 dbus_message_iter_recurse(&iter, &sub);
1576
1577 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1578 n++;
1579 dbus_message_iter_next(&sub);
1580 }
1581
1582 if (!(l = new(char*, n+1)))
1583 return -ENOMEM;
1584
1585 assert_se(dbus_message_iter_init(m, &iter));
1586 dbus_message_iter_recurse(&iter, &sub);
1587
1588 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1589 const char *s;
1590
1591 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1592 dbus_message_iter_get_basic(&sub, &s);
1593
1594 if (!(l[i++] = strdup(s))) {
1595 strv_free(l);
1596 return -ENOMEM;
1597 }
1598
1599 dbus_message_iter_next(&sub);
1600 }
1601
1602 assert(i == n);
1603 l[i] = NULL;
1604
1605 if (_l)
1606 *_l = l;
1607
1608 return 0;
1609}
a567261a
LP
1610
1611bool bus_has_subscriber(Manager *m) {
1612 Iterator i;
1613 DBusConnection *c;
1614
1615 assert(m);
1616
1617 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1618 if (bus_connection_has_subscriber(m, c))
1619 return true;
1620
1621 SET_FOREACH(c, m->bus_connections, i)
1622 if (bus_connection_has_subscriber(m, c))
1623 return true;
1624
1625 return false;
1626}
1627
1628bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1629 assert(m);
1630 assert(c);
1631
1632 return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1633}
c4e2ceae
LP
1634
1635DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
1636 DBusMessage *m;
1637 DBusMessageIter iter, sub;
1638 const char *i;
1639
1640 assert(interface);
1641 assert(properties);
1642
1643 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
1644 goto oom;
1645
1646 dbus_message_iter_init_append(m, &iter);
1647
1648 /* We won't send any property values, since they might be
1649 * large and sometimes not cheap to generated */
1650
1651 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
1652 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
1653 !dbus_message_iter_close_container(&iter, &sub) ||
1654 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
1655 goto oom;
1656
1657 NULSTR_FOREACH(i, properties)
1658 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
1659 goto oom;
1660
1661 if (!dbus_message_iter_close_container(&iter, &sub))
1662 goto oom;
1663
1664 return m;
1665
1666oom:
1667 if (m)
1668 dbus_message_unref(m);
1669
1670 return NULL;
1671}