chiark / gitweb /
mechanisms: add mechanisms to change system locale and clock
[elogind.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         DBusMessageIter sub;
467         char **t = data;
468
469         assert(i);
470         assert(property);
471
472         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
473                 return -ENOMEM;
474
475         STRV_FOREACH(t, t)
476                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
477                         return -ENOMEM;
478
479         if (!dbus_message_iter_close_container(i, &sub))
480                 return -ENOMEM;
481
482         return 0;
483 }
484
485 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
486         bool *b = data;
487         dbus_bool_t db;
488
489         assert(i);
490         assert(property);
491         assert(b);
492
493         db = *b;
494
495         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
496                 return -ENOMEM;
497
498         return 0;
499 }
500
501 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
502         assert(i);
503         assert(property);
504         assert(data);
505
506         /* Let's ensure that usec_t is actually 64bit, and hence this
507          * function can be used for usec_t */
508         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
509
510         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
511                 return -ENOMEM;
512
513         return 0;
514 }
515
516 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
517         assert(i);
518         assert(property);
519         assert(data);
520
521         /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
522          * 32bit, and hence this function can be used for
523          * pid_t/mode_t/uid_t/gid_t */
524         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
525         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
526         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
527         assert_cc(sizeof(uint32_t) == sizeof(uid_t));
528         assert_cc(sizeof(uint32_t) == sizeof(gid_t));
529
530         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
531                 return -ENOMEM;
532
533         return 0;
534 }
535
536 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
537         assert(i);
538         assert(property);
539         assert(data);
540
541         assert_cc(sizeof(int32_t) == sizeof(int));
542
543         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
544                 return -ENOMEM;
545
546         return 0;
547 }
548
549 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
550         uint64_t u;
551
552         assert(i);
553         assert(property);
554         assert(data);
555
556         u = (uint64_t) *(size_t*) data;
557
558         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
559                 return -ENOMEM;
560
561         return 0;
562 }
563
564 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
565         uint64_t u;
566
567         assert(i);
568         assert(property);
569         assert(data);
570
571         u = (uint64_t) *(unsigned long*) data;
572
573         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
574                 return -ENOMEM;
575
576         return 0;
577 }
578
579 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
580         int64_t l;
581
582         assert(i);
583         assert(property);
584         assert(data);
585
586         l = (int64_t) *(long*) data;
587
588         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
589                 return -ENOMEM;
590
591         return 0;
592 }
593
594 const char *bus_errno_to_dbus(int error) {
595
596         switch(error) {
597
598         case -EINVAL:
599                 return DBUS_ERROR_INVALID_ARGS;
600
601         case -ENOMEM:
602                 return DBUS_ERROR_NO_MEMORY;
603
604         case -EPERM:
605         case -EACCES:
606                 return DBUS_ERROR_ACCESS_DENIED;
607
608         case -ESRCH:
609                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
610
611         case -ENOENT:
612                 return DBUS_ERROR_FILE_NOT_FOUND;
613
614         case -EEXIST:
615                 return DBUS_ERROR_FILE_EXISTS;
616
617         case -ETIMEDOUT:
618         case -ETIME:
619                 return DBUS_ERROR_TIMEOUT;
620
621         case -EIO:
622                 return DBUS_ERROR_IO_ERROR;
623
624         case -ENETRESET:
625         case -ECONNABORTED:
626         case -ECONNRESET:
627                 return DBUS_ERROR_DISCONNECTED;
628         }
629
630         return DBUS_ERROR_FAILED;
631 }
632
633 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
634         DBusMessage *reply = NULL;
635         const char *name, *text;
636
637         if (berror && dbus_error_is_set(berror)) {
638                 name = berror->name;
639                 text = berror->message;
640         } else {
641                 name = bus_errno_to_dbus(error);
642                 text = strerror(-error);
643         }
644
645         if (!(reply = dbus_message_new_error(message, name, text)))
646                 goto oom;
647
648         if (!dbus_connection_send(c, reply, NULL))
649                 goto oom;
650
651         dbus_message_unref(reply);
652
653         if (berror)
654                 dbus_error_free(berror);
655
656         return DBUS_HANDLER_RESULT_HANDLED;
657
658 oom:
659         if (reply)
660                 dbus_message_unref(reply);
661
662         if (berror)
663                 dbus_error_free(berror);
664
665         return DBUS_HANDLER_RESULT_NEED_MEMORY;
666 }
667
668 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
669         DBusMessage *m;
670         DBusMessageIter iter, sub;
671         const char *i;
672
673         assert(interface);
674         assert(properties);
675
676         if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
677                 goto oom;
678
679         dbus_message_iter_init_append(m, &iter);
680
681         /* We won't send any property values, since they might be
682          * large and sometimes not cheap to generated */
683
684         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
685             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
686             !dbus_message_iter_close_container(&iter, &sub) ||
687             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
688                 goto oom;
689
690         NULSTR_FOREACH(i, properties)
691                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
692                         goto oom;
693
694         if (!dbus_message_iter_close_container(&iter, &sub))
695                 goto oom;
696
697         return m;
698
699 oom:
700         if (m)
701                 dbus_message_unref(m);
702
703         return NULL;
704 }
705
706 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
707         unsigned flags;
708         uint32_t events = 0;
709
710         assert(bus_watch);
711
712         /* no watch flags for disabled watches */
713         if (!dbus_watch_get_enabled(bus_watch))
714                 return 0;
715
716         flags = dbus_watch_get_flags(bus_watch);
717
718         if (flags & DBUS_WATCH_READABLE)
719                 events |= EPOLLIN;
720         if (flags & DBUS_WATCH_WRITABLE)
721                 events |= EPOLLOUT;
722
723         return events | EPOLLHUP | EPOLLERR;
724 }
725
726 unsigned bus_events_to_flags(uint32_t events) {
727         unsigned flags = 0;
728
729         if (events & EPOLLIN)
730                 flags |= DBUS_WATCH_READABLE;
731         if (events & EPOLLOUT)
732                 flags |= DBUS_WATCH_WRITABLE;
733         if (events & EPOLLHUP)
734                 flags |= DBUS_WATCH_HANGUP;
735         if (events & EPOLLERR)
736                 flags |= DBUS_WATCH_ERROR;
737
738         return flags;
739 }
740
741 int bus_parse_strv(DBusMessage *m, char ***_l) {
742         DBusMessageIter iter;
743
744         assert(m);
745         assert(_l);
746
747         if (!dbus_message_iter_init(m, &iter))
748                 return -EINVAL;
749
750         return bus_parse_strv_iter(&iter, _l);
751 }
752
753 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
754         DBusMessageIter sub;
755         unsigned n = 0, i = 0;
756         char **l;
757
758         assert(iter);
759         assert(_l);
760
761         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
762             dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
763             return -EINVAL;
764
765         dbus_message_iter_recurse(iter, &sub);
766
767         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
768                 n++;
769                 dbus_message_iter_next(&sub);
770         }
771
772         if (!(l = new(char*, n+1)))
773                 return -ENOMEM;
774
775         dbus_message_iter_recurse(iter, &sub);
776
777         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
778                 const char *s;
779
780                 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
781                 dbus_message_iter_get_basic(&sub, &s);
782
783                 if (!(l[i++] = strdup(s))) {
784                         strv_free(l);
785                         return -ENOMEM;
786                 }
787
788                 dbus_message_iter_next(&sub);
789         }
790
791         assert(i == n);
792         l[i] = NULL;
793
794         if (_l)
795                 *_l = l;
796
797         return 0;
798 }