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