]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus-common.c
logind: hook up PAM module with logind
[thirdparty/systemd.git] / src / dbus-common.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <sys/socket.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <dbus/dbus.h>
29 #include <string.h>
30 #include <sys/epoll.h>
31
32 #include "log.h"
33 #include "dbus-common.h"
34 #include "util.h"
35 #include "def.h"
36 #include "strv.h"
37
38 int bus_check_peercred(DBusConnection *c) {
39 int fd;
40 struct ucred ucred;
41 socklen_t l;
42
43 assert(c);
44
45 assert_se(dbus_connection_get_unix_fd(c, &fd));
46
47 l = sizeof(struct ucred);
48 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0) {
49 log_error("SO_PEERCRED failed: %m");
50 return -errno;
51 }
52
53 if (l != sizeof(struct ucred)) {
54 log_error("SO_PEERCRED returned wrong size.");
55 return -E2BIG;
56 }
57
58 if (ucred.uid != 0)
59 return -EPERM;
60
61 return 1;
62 }
63
64 static int sync_auth(DBusConnection *bus, DBusError *error) {
65 usec_t begin, tstamp;
66
67 assert(bus);
68
69 /* This complexity should probably move into D-Bus itself:
70 *
71 * https://bugs.freedesktop.org/show_bug.cgi?id=35189 */
72
73 begin = tstamp = now(CLOCK_MONOTONIC);
74 for (;;) {
75
76 if (tstamp > begin + DEFAULT_TIMEOUT_USEC)
77 break;
78
79 if (dbus_connection_get_is_authenticated(bus))
80 break;
81
82 if (!dbus_connection_read_write_dispatch(bus, ((begin + DEFAULT_TIMEOUT_USEC - tstamp) + USEC_PER_MSEC - 1) / USEC_PER_MSEC))
83 break;
84
85 tstamp = now(CLOCK_MONOTONIC);
86 }
87
88 if (!dbus_connection_get_is_connected(bus)) {
89 dbus_set_error_const(error, DBUS_ERROR_NO_SERVER, "Connection terminated during authentication.");
90 return -ECONNREFUSED;
91 }
92
93 if (!dbus_connection_get_is_authenticated(bus)) {
94 dbus_set_error_const(error, DBUS_ERROR_TIMEOUT, "Failed to authenticate in time.");
95 return -EACCES;
96 }
97
98 return 0;
99 }
100
101 int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *error) {
102 DBusConnection *bus;
103 int r;
104
105 assert(_bus);
106
107 /* If we are root, then let's not go via the bus */
108 if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
109
110 if (!(bus = dbus_connection_open_private("unix:path=/run/systemd/private", error))) {
111 #ifndef LEGACY
112 dbus_error_free(error);
113
114 /* Retry with the pre v21 socket name, to ease upgrades */
115 if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
116 #endif
117 return -EIO;
118 }
119
120 dbus_connection_set_exit_on_disconnect(bus, FALSE);
121
122 if (bus_check_peercred(bus) < 0) {
123 dbus_connection_close(bus);
124 dbus_connection_unref(bus);
125
126 dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
127 return -EACCES;
128 }
129
130 if (private)
131 *private = true;
132
133 } else {
134 if (!(bus = dbus_bus_get_private(t, error)))
135 return -EIO;
136
137 dbus_connection_set_exit_on_disconnect(bus, FALSE);
138
139 if (private)
140 *private = false;
141 }
142
143 if ((r = sync_auth(bus, error)) < 0) {
144 dbus_connection_close(bus);
145 dbus_connection_unref(bus);
146 return r;
147 }
148
149 *_bus = bus;
150 return 0;
151 }
152
153 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
154 DBusConnection *bus;
155 char *p = NULL;
156 int r;
157
158 assert(_bus);
159 assert(user || host);
160
161 if (user && host)
162 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
163 else if (user)
164 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
165 else if (host)
166 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
167
168 if (!p) {
169 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
170 return -ENOMEM;
171 }
172
173 bus = dbus_connection_open_private(p, error);
174 free(p);
175
176 if (!bus)
177 return -EIO;
178
179 dbus_connection_set_exit_on_disconnect(bus, FALSE);
180
181 if ((r = sync_auth(bus, error)) < 0) {
182 dbus_connection_close(bus);
183 dbus_connection_unref(bus);
184 return r;
185 }
186
187 if (!dbus_bus_register(bus, error)) {
188 dbus_connection_close(bus);
189 dbus_connection_unref(bus);
190 return r;
191 }
192
193 *_bus = bus;
194 return 0;
195 }
196
197 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
198 DBusConnection *bus;
199 int r;
200
201 assert(_bus);
202
203 /* Don't bother with PolicyKit if we are root */
204 if (geteuid() == 0)
205 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
206
207 if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
208 return -EIO;
209
210 dbus_connection_set_exit_on_disconnect(bus, FALSE);
211
212 if ((r = sync_auth(bus, error)) < 0) {
213 dbus_connection_close(bus);
214 dbus_connection_unref(bus);
215 return r;
216 }
217
218 if (!dbus_bus_register(bus, error)) {
219 dbus_connection_close(bus);
220 dbus_connection_unref(bus);
221 return r;
222 }
223
224 *_bus = bus;
225 return 0;
226 }
227
228 const char *bus_error_message(const DBusError *error) {
229 assert(error);
230
231 /* Sometimes the D-Bus server is a little bit too verbose with
232 * its error messages, so let's override them here */
233 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
234 return "Access denied";
235
236 return error->message;
237 }
238
239 DBusHandlerResult bus_default_message_handler(
240 DBusConnection *c,
241 DBusMessage *message,
242 const char *introspection,
243 const char *interfaces,
244 const BusProperty *properties) {
245
246 DBusError error;
247 DBusMessage *reply = NULL;
248 int r;
249
250 assert(c);
251 assert(message);
252
253 dbus_error_init(&error);
254
255 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
256
257 if (!(reply = dbus_message_new_method_return(message)))
258 goto oom;
259
260 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
261 goto oom;
262
263 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
264 const char *interface, *property;
265 const BusProperty *p;
266
267 if (!dbus_message_get_args(
268 message,
269 &error,
270 DBUS_TYPE_STRING, &interface,
271 DBUS_TYPE_STRING, &property,
272 DBUS_TYPE_INVALID))
273 return bus_send_error_reply(c, message, &error, -EINVAL);
274
275 for (p = properties; p->property; p++)
276 if (streq(p->interface, interface) && streq(p->property, property))
277 break;
278
279 if (p->property) {
280 DBusMessageIter iter, sub;
281
282 if (!(reply = dbus_message_new_method_return(message)))
283 goto oom;
284
285 dbus_message_iter_init_append(reply, &iter);
286
287 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
288 goto oom;
289
290 if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
291
292 if (r == -ENOMEM)
293 goto oom;
294
295 dbus_message_unref(reply);
296 return bus_send_error_reply(c, message, NULL, r);
297 }
298
299 if (!dbus_message_iter_close_container(&iter, &sub))
300 goto oom;
301 } else {
302 if (!nulstr_contains(interfaces, interface))
303 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
304 else
305 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
306
307 return bus_send_error_reply(c, message, &error, -EINVAL);
308 }
309
310 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
311 const char *interface;
312 const BusProperty *p;
313 DBusMessageIter iter, sub, sub2, sub3;
314
315 if (!dbus_message_get_args(
316 message,
317 &error,
318 DBUS_TYPE_STRING, &interface,
319 DBUS_TYPE_INVALID))
320 return bus_send_error_reply(c, message, &error, -EINVAL);
321
322 if (interface[0] && !nulstr_contains(interfaces, interface)) {
323 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
324 return bus_send_error_reply(c, message, &error, -EINVAL);
325 }
326
327 if (!(reply = dbus_message_new_method_return(message)))
328 goto oom;
329
330 dbus_message_iter_init_append(reply, &iter);
331
332 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
333 goto oom;
334
335 for (p = properties; p->property; p++) {
336 if (interface[0] && !streq(p->interface, interface))
337 continue;
338
339 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
340 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
341 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
342 goto oom;
343
344 if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
345
346 if (r == -ENOMEM)
347 goto oom;
348
349 dbus_message_unref(reply);
350 return bus_send_error_reply(c, message, NULL, r);
351 }
352
353 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
354 !dbus_message_iter_close_container(&sub, &sub2))
355 goto oom;
356 }
357
358 if (!dbus_message_iter_close_container(&iter, &sub))
359 goto oom;
360
361 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) {
362 const char *interface, *property;
363 DBusMessageIter iter;
364 const BusProperty *p;
365
366 if (!dbus_message_iter_init(message, &iter) ||
367 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
368 return bus_send_error_reply(c, message, NULL, -EINVAL);
369
370 dbus_message_iter_get_basic(&iter, &interface);
371
372 if (!dbus_message_iter_next(&iter) ||
373 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
374 return bus_send_error_reply(c, message, NULL, -EINVAL);
375
376 dbus_message_iter_get_basic(&iter, &property);
377
378 if (!dbus_message_iter_next(&iter) ||
379 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
380 dbus_message_iter_has_next(&iter))
381 return bus_send_error_reply(c, message, NULL, -EINVAL);
382
383 for (p = properties; p->property; p++)
384 if (streq(p->interface, interface) && streq(p->property, property))
385 break;
386
387 if (p->set) {
388 DBusMessageIter sub;
389 char *sig;
390
391 dbus_message_iter_recurse(&iter, &sub);
392
393 if (!(sig = dbus_message_iter_get_signature(&sub)))
394 goto oom;
395
396 if (!streq(sig, p->signature)) {
397 dbus_free(sig);
398 return bus_send_error_reply(c, message, NULL, -EINVAL);
399 }
400
401 dbus_free(sig);
402
403 if ((r = p->set(&sub, property)) < 0) {
404 if (r == -ENOMEM)
405 goto oom;
406 return bus_send_error_reply(c, message, NULL, r);
407 }
408
409 if (!(reply = dbus_message_new_method_return(message)))
410 goto oom;
411 } else {
412 if (p->property)
413 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
414 else if (!nulstr_contains(interfaces, interface))
415 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
416 else
417 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
418
419 return bus_send_error_reply(c, message, &error, -EINVAL);
420 }
421
422 } else {
423 const char *interface = dbus_message_get_interface(message);
424
425 if (!interface || !nulstr_contains(interfaces, interface)) {
426 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
427 return bus_send_error_reply(c, message, &error, -EINVAL);
428 }
429 }
430
431 if (reply) {
432 if (!dbus_connection_send(c, reply, NULL))
433 goto oom;
434
435 dbus_message_unref(reply);
436 return DBUS_HANDLER_RESULT_HANDLED;
437 }
438
439 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
440
441 oom:
442 if (reply)
443 dbus_message_unref(reply);
444
445 dbus_error_free(&error);
446
447 return DBUS_HANDLER_RESULT_NEED_MEMORY;
448 }
449
450 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
451 const char *t = data;
452
453 assert(i);
454 assert(property);
455
456 if (!t)
457 t = "";
458
459 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
460 return -ENOMEM;
461
462 return 0;
463 }
464
465 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
466 char **t = data;
467
468 assert(i);
469 assert(property);
470
471 return bus_append_strv_iter(i, t);
472 }
473
474 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
475 bool *b = data;
476 dbus_bool_t db;
477
478 assert(i);
479 assert(property);
480 assert(b);
481
482 db = *b;
483
484 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
485 return -ENOMEM;
486
487 return 0;
488 }
489
490 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
491 assert(i);
492 assert(property);
493 assert(data);
494
495 /* Let's ensure that usec_t is actually 64bit, and hence this
496 * function can be used for usec_t */
497 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
498
499 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
500 return -ENOMEM;
501
502 return 0;
503 }
504
505 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
506 assert(i);
507 assert(property);
508 assert(data);
509
510 /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
511 * 32bit, and hence this function can be used for
512 * pid_t/mode_t/uid_t/gid_t */
513 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
514 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
515 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
516 assert_cc(sizeof(uint32_t) == sizeof(uid_t));
517 assert_cc(sizeof(uint32_t) == sizeof(gid_t));
518
519 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
520 return -ENOMEM;
521
522 return 0;
523 }
524
525 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
526 assert(i);
527 assert(property);
528 assert(data);
529
530 assert_cc(sizeof(int32_t) == sizeof(int));
531
532 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
533 return -ENOMEM;
534
535 return 0;
536 }
537
538 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
539 uint64_t u;
540
541 assert(i);
542 assert(property);
543 assert(data);
544
545 u = (uint64_t) *(size_t*) data;
546
547 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
548 return -ENOMEM;
549
550 return 0;
551 }
552
553 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
554 uint64_t u;
555
556 assert(i);
557 assert(property);
558 assert(data);
559
560 u = (uint64_t) *(unsigned long*) data;
561
562 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
563 return -ENOMEM;
564
565 return 0;
566 }
567
568 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
569 int64_t l;
570
571 assert(i);
572 assert(property);
573 assert(data);
574
575 l = (int64_t) *(long*) data;
576
577 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
578 return -ENOMEM;
579
580 return 0;
581 }
582
583 const char *bus_errno_to_dbus(int error) {
584
585 switch(error) {
586
587 case -EINVAL:
588 return DBUS_ERROR_INVALID_ARGS;
589
590 case -ENOMEM:
591 return DBUS_ERROR_NO_MEMORY;
592
593 case -EPERM:
594 case -EACCES:
595 return DBUS_ERROR_ACCESS_DENIED;
596
597 case -ESRCH:
598 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
599
600 case -ENOENT:
601 return DBUS_ERROR_FILE_NOT_FOUND;
602
603 case -EEXIST:
604 return DBUS_ERROR_FILE_EXISTS;
605
606 case -ETIMEDOUT:
607 case -ETIME:
608 return DBUS_ERROR_TIMEOUT;
609
610 case -EIO:
611 return DBUS_ERROR_IO_ERROR;
612
613 case -ENETRESET:
614 case -ECONNABORTED:
615 case -ECONNRESET:
616 return DBUS_ERROR_DISCONNECTED;
617 }
618
619 return DBUS_ERROR_FAILED;
620 }
621
622 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
623 DBusMessage *reply = NULL;
624 const char *name, *text;
625
626 if (berror && dbus_error_is_set(berror)) {
627 name = berror->name;
628 text = berror->message;
629 } else {
630 name = bus_errno_to_dbus(error);
631 text = strerror(-error);
632 }
633
634 if (!(reply = dbus_message_new_error(message, name, text)))
635 goto oom;
636
637 if (!dbus_connection_send(c, reply, NULL))
638 goto oom;
639
640 dbus_message_unref(reply);
641
642 if (berror)
643 dbus_error_free(berror);
644
645 return DBUS_HANDLER_RESULT_HANDLED;
646
647 oom:
648 if (reply)
649 dbus_message_unref(reply);
650
651 if (berror)
652 dbus_error_free(berror);
653
654 return DBUS_HANDLER_RESULT_NEED_MEMORY;
655 }
656
657 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
658 DBusMessage *m;
659 DBusMessageIter iter, sub;
660 const char *i;
661
662 assert(interface);
663 assert(properties);
664
665 if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
666 goto oom;
667
668 dbus_message_iter_init_append(m, &iter);
669
670 /* We won't send any property values, since they might be
671 * large and sometimes not cheap to generated */
672
673 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
674 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
675 !dbus_message_iter_close_container(&iter, &sub) ||
676 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
677 goto oom;
678
679 NULSTR_FOREACH(i, properties)
680 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
681 goto oom;
682
683 if (!dbus_message_iter_close_container(&iter, &sub))
684 goto oom;
685
686 return m;
687
688 oom:
689 if (m)
690 dbus_message_unref(m);
691
692 return NULL;
693 }
694
695 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
696 unsigned flags;
697 uint32_t events = 0;
698
699 assert(bus_watch);
700
701 /* no watch flags for disabled watches */
702 if (!dbus_watch_get_enabled(bus_watch))
703 return 0;
704
705 flags = dbus_watch_get_flags(bus_watch);
706
707 if (flags & DBUS_WATCH_READABLE)
708 events |= EPOLLIN;
709 if (flags & DBUS_WATCH_WRITABLE)
710 events |= EPOLLOUT;
711
712 return events | EPOLLHUP | EPOLLERR;
713 }
714
715 unsigned bus_events_to_flags(uint32_t events) {
716 unsigned flags = 0;
717
718 if (events & EPOLLIN)
719 flags |= DBUS_WATCH_READABLE;
720 if (events & EPOLLOUT)
721 flags |= DBUS_WATCH_WRITABLE;
722 if (events & EPOLLHUP)
723 flags |= DBUS_WATCH_HANGUP;
724 if (events & EPOLLERR)
725 flags |= DBUS_WATCH_ERROR;
726
727 return flags;
728 }
729
730 int bus_parse_strv(DBusMessage *m, char ***_l) {
731 DBusMessageIter iter;
732
733 assert(m);
734 assert(_l);
735
736 if (!dbus_message_iter_init(m, &iter))
737 return -EINVAL;
738
739 return bus_parse_strv_iter(&iter, _l);
740 }
741
742 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
743 DBusMessageIter sub;
744 unsigned n = 0, i = 0;
745 char **l;
746
747 assert(iter);
748 assert(_l);
749
750 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
751 dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
752 return -EINVAL;
753
754 dbus_message_iter_recurse(iter, &sub);
755
756 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
757 n++;
758 dbus_message_iter_next(&sub);
759 }
760
761 if (!(l = new(char*, n+1)))
762 return -ENOMEM;
763
764 dbus_message_iter_recurse(iter, &sub);
765
766 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
767 const char *s;
768
769 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
770 dbus_message_iter_get_basic(&sub, &s);
771
772 if (!(l[i++] = strdup(s))) {
773 strv_free(l);
774 return -ENOMEM;
775 }
776
777 dbus_message_iter_next(&sub);
778 }
779
780 assert(i == n);
781 l[i] = NULL;
782
783 if (_l)
784 *_l = l;
785
786 return 0;
787 }
788
789 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
790 DBusMessageIter sub;
791
792 assert(iter);
793
794 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
795 return -ENOMEM;
796
797 STRV_FOREACH(l, l)
798 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
799 return -ENOMEM;
800
801 if (!dbus_message_iter_close_container(iter, &sub))
802 return -ENOMEM;
803
804 return 0;
805 }