chiark / gitweb /
exec: add high-level controls for blkio cgroup attributes
[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 && ucred.uid != geteuid())
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 = NULL;
103         int r;
104         bool private = true;
105
106         assert(_bus);
107
108         if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
109                 /* If we are root, then let's talk directly to the
110                  * system instance, instead of going via the bus */
111
112                 bus = dbus_connection_open_private("unix:path=/run/systemd/private", error);
113                 if (!bus)
114                         return -EIO;
115
116         } else {
117                 if (t == DBUS_BUS_SESSION) {
118                         const char *e;
119
120                         /* If we are supposed to talk to the instance,
121                          * try via XDG_RUNTIME_DIR first, then
122                          * fallback to normal bus access */
123
124                         e = getenv("XDG_RUNTIME_DIR");
125                         if (e) {
126                                 char *p;
127
128                                 if (asprintf(&p, "unix:path=%s/systemd/private", e) < 0)
129                                         return -ENOMEM;
130
131                                 bus = dbus_connection_open_private(p, NULL);
132                                 free(p);
133                         }
134                 }
135
136                 if (!bus) {
137                         bus = dbus_bus_get_private(t, error);
138                         if (!bus)
139                                 return -EIO;
140
141                         private = false;
142                 }
143         }
144
145         dbus_connection_set_exit_on_disconnect(bus, FALSE);
146
147         if (private) {
148                 if (bus_check_peercred(bus) < 0) {
149                         dbus_connection_close(bus);
150                         dbus_connection_unref(bus);
151
152                         dbus_set_error_const(error, DBUS_ERROR_ACCESS_DENIED, "Failed to verify owner of bus.");
153                         return -EACCES;
154                 }
155         }
156
157         r = sync_auth(bus, error);
158         if (r < 0) {
159                 dbus_connection_close(bus);
160                 dbus_connection_unref(bus);
161                 return r;
162         }
163
164         if (_private)
165                 *_private = private;
166
167         *_bus = bus;
168         return 0;
169 }
170
171 int bus_connect_system_ssh(const char *user, const char *host, DBusConnection **_bus, DBusError *error) {
172         DBusConnection *bus;
173         char *p = NULL;
174         int r;
175
176         assert(_bus);
177         assert(user || host);
178
179         if (user && host)
180                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@%s,argv3=systemd-stdio-bridge", user, host);
181         else if (user)
182                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s@localhost,argv3=systemd-stdio-bridge", user);
183         else if (host)
184                 asprintf(&p, "exec:path=ssh,argv1=-xT,argv2=%s,argv3=systemd-stdio-bridge", host);
185
186         if (!p) {
187                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
188                 return -ENOMEM;
189         }
190
191         bus = dbus_connection_open_private(p, error);
192         free(p);
193
194         if (!bus)
195                 return -EIO;
196
197         dbus_connection_set_exit_on_disconnect(bus, FALSE);
198
199         if ((r = sync_auth(bus, error)) < 0) {
200                 dbus_connection_close(bus);
201                 dbus_connection_unref(bus);
202                 return r;
203         }
204
205         if (!dbus_bus_register(bus, error)) {
206                 dbus_connection_close(bus);
207                 dbus_connection_unref(bus);
208                 return r;
209         }
210
211         *_bus = bus;
212         return 0;
213 }
214
215 int bus_connect_system_polkit(DBusConnection **_bus, DBusError *error) {
216         DBusConnection *bus;
217         int r;
218
219         assert(_bus);
220
221         /* Don't bother with PolicyKit if we are root */
222         if (geteuid() == 0)
223                 return bus_connect(DBUS_BUS_SYSTEM, _bus, NULL, error);
224
225         if (!(bus = dbus_connection_open_private("exec:path=pkexec,argv1=" SYSTEMD_STDIO_BRIDGE_BINARY_PATH, error)))
226                 return -EIO;
227
228         dbus_connection_set_exit_on_disconnect(bus, FALSE);
229
230         if ((r = sync_auth(bus, error)) < 0) {
231                 dbus_connection_close(bus);
232                 dbus_connection_unref(bus);
233                 return r;
234         }
235
236         if (!dbus_bus_register(bus, error)) {
237                 dbus_connection_close(bus);
238                 dbus_connection_unref(bus);
239                 return r;
240         }
241
242         *_bus = bus;
243         return 0;
244 }
245
246 const char *bus_error_message(const DBusError *error) {
247         assert(error);
248
249         /* Sometimes the D-Bus server is a little bit too verbose with
250          * its error messages, so let's override them here */
251         if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED))
252                 return "Access denied";
253
254         return error->message;
255 }
256
257 DBusHandlerResult bus_default_message_handler(
258                 DBusConnection *c,
259                 DBusMessage *message,
260                 const char *introspection,
261                 const char *interfaces,
262                 const BusProperty *properties) {
263
264         DBusError error;
265         DBusMessage *reply = NULL;
266         int r;
267
268         assert(c);
269         assert(message);
270
271         dbus_error_init(&error);
272
273         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
274
275                 if (!(reply = dbus_message_new_method_return(message)))
276                         goto oom;
277
278                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
279                         goto oom;
280
281         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
282                 const char *interface, *property;
283                 const BusProperty *p;
284
285                 if (!dbus_message_get_args(
286                             message,
287                             &error,
288                             DBUS_TYPE_STRING, &interface,
289                             DBUS_TYPE_STRING, &property,
290                             DBUS_TYPE_INVALID))
291                         return bus_send_error_reply(c, message, &error, -EINVAL);
292
293                 for (p = properties; p->property; p++)
294                         if (streq(p->interface, interface) && streq(p->property, property))
295                                 break;
296
297                 if (p->property) {
298                         DBusMessageIter iter, sub;
299
300                         if (!(reply = dbus_message_new_method_return(message)))
301                                 goto oom;
302
303                         dbus_message_iter_init_append(reply, &iter);
304
305                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
306                                 goto oom;
307
308                         if ((r = p->append(&sub, property, (void*) p->data)) < 0) {
309
310                                 if (r == -ENOMEM)
311                                         goto oom;
312
313                                 dbus_message_unref(reply);
314                                 return bus_send_error_reply(c, message, NULL, r);
315                         }
316
317                         if (!dbus_message_iter_close_container(&iter, &sub))
318                                 goto oom;
319                 } else {
320                         if (!nulstr_contains(interfaces, interface))
321                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
322                         else
323                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
324
325                         return bus_send_error_reply(c, message, &error, -EINVAL);
326                 }
327
328         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
329                 const char *interface;
330                 const BusProperty *p;
331                 DBusMessageIter iter, sub, sub2, sub3;
332
333                 if (!dbus_message_get_args(
334                             message,
335                             &error,
336                             DBUS_TYPE_STRING, &interface,
337                             DBUS_TYPE_INVALID))
338                         return bus_send_error_reply(c, message, &error, -EINVAL);
339
340                 if (interface[0] && !nulstr_contains(interfaces, interface)) {
341                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
342                         return bus_send_error_reply(c, message, &error, -EINVAL);
343                 }
344
345                 if (!(reply = dbus_message_new_method_return(message)))
346                         goto oom;
347
348                 dbus_message_iter_init_append(reply, &iter);
349
350                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
351                         goto oom;
352
353                 for (p = properties; p->property; p++) {
354                         if (interface[0] && !streq(p->interface, interface))
355                                 continue;
356
357                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
358                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
359                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
360                                 goto oom;
361
362                         if ((r = p->append(&sub3, p->property, (void*) p->data)) < 0) {
363
364                                 if (r == -ENOMEM)
365                                         goto oom;
366
367                                 dbus_message_unref(reply);
368                                 return bus_send_error_reply(c, message, NULL, r);
369                         }
370
371                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
372                             !dbus_message_iter_close_container(&sub, &sub2))
373                                 goto oom;
374                 }
375
376                 if (!dbus_message_iter_close_container(&iter, &sub))
377                         goto oom;
378
379         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && properties) {
380                 const char *interface, *property;
381                 DBusMessageIter iter;
382                 const BusProperty *p;
383
384                 if (!dbus_message_iter_init(message, &iter) ||
385                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
386                         return bus_send_error_reply(c, message, NULL, -EINVAL);
387
388                 dbus_message_iter_get_basic(&iter, &interface);
389
390                 if (!dbus_message_iter_next(&iter) ||
391                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
392                         return bus_send_error_reply(c, message, NULL, -EINVAL);
393
394                 dbus_message_iter_get_basic(&iter, &property);
395
396                 if (!dbus_message_iter_next(&iter) ||
397                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
398                     dbus_message_iter_has_next(&iter))
399                         return bus_send_error_reply(c, message, NULL, -EINVAL);
400
401                 for (p = properties; p->property; p++)
402                         if (streq(p->interface, interface) && streq(p->property, property))
403                                 break;
404
405                 if (p->set) {
406                         DBusMessageIter sub;
407                         char *sig;
408
409                         dbus_message_iter_recurse(&iter, &sub);
410
411                         if (!(sig = dbus_message_iter_get_signature(&sub)))
412                                 goto oom;
413
414                         if (!streq(sig, p->signature)) {
415                                 dbus_free(sig);
416                                 return bus_send_error_reply(c, message, NULL, -EINVAL);
417                         }
418
419                         dbus_free(sig);
420
421                         if ((r = p->set(&sub, property)) < 0) {
422                                 if (r == -ENOMEM)
423                                         goto oom;
424                                 return bus_send_error_reply(c, message, NULL, r);
425                         }
426
427                         if (!(reply = dbus_message_new_method_return(message)))
428                                 goto oom;
429                 } else {
430                         if (p->property)
431                                 dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
432                         else if (!nulstr_contains(interfaces, interface))
433                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
434                         else
435                                 dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
436
437                         return bus_send_error_reply(c, message, &error, -EINVAL);
438                 }
439
440         } else {
441                 const char *interface = dbus_message_get_interface(message);
442
443                 if (!interface || !nulstr_contains(interfaces, interface)) {
444                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
445                         return bus_send_error_reply(c, message, &error, -EINVAL);
446                 }
447         }
448
449         if (reply) {
450                 if (!dbus_connection_send(c, reply, NULL))
451                         goto oom;
452
453                 dbus_message_unref(reply);
454                 return DBUS_HANDLER_RESULT_HANDLED;
455         }
456
457         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
458
459 oom:
460         if (reply)
461                 dbus_message_unref(reply);
462
463         dbus_error_free(&error);
464
465         return DBUS_HANDLER_RESULT_NEED_MEMORY;
466 }
467
468 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
469         const char *t = data;
470
471         assert(i);
472         assert(property);
473
474         if (!t)
475                 t = "";
476
477         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
478                 return -ENOMEM;
479
480         return 0;
481 }
482
483 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
484         char **t = data;
485
486         assert(i);
487         assert(property);
488
489         return bus_append_strv_iter(i, t);
490 }
491
492 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
493         bool *b = data;
494         dbus_bool_t db;
495
496         assert(i);
497         assert(property);
498         assert(b);
499
500         db = *b;
501
502         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
503                 return -ENOMEM;
504
505         return 0;
506 }
507
508 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
509         assert(i);
510         assert(property);
511         assert(data);
512
513         /* Let's ensure that usec_t is actually 64bit, and hence this
514          * function can be used for usec_t */
515         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
516
517         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
518                 return -ENOMEM;
519
520         return 0;
521 }
522
523 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
524         assert(i);
525         assert(property);
526         assert(data);
527
528         /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
529          * 32bit, and hence this function can be used for
530          * pid_t/mode_t/uid_t/gid_t */
531         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
532         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
533         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
534         assert_cc(sizeof(uint32_t) == sizeof(uid_t));
535         assert_cc(sizeof(uint32_t) == sizeof(gid_t));
536
537         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
538                 return -ENOMEM;
539
540         return 0;
541 }
542
543 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
544         assert(i);
545         assert(property);
546         assert(data);
547
548         assert_cc(sizeof(int32_t) == sizeof(int));
549
550         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
551                 return -ENOMEM;
552
553         return 0;
554 }
555
556 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
557         uint64_t u;
558
559         assert(i);
560         assert(property);
561         assert(data);
562
563         u = (uint64_t) *(size_t*) data;
564
565         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
566                 return -ENOMEM;
567
568         return 0;
569 }
570
571 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
572         uint64_t u;
573
574         assert(i);
575         assert(property);
576         assert(data);
577
578         u = (uint64_t) *(unsigned long*) data;
579
580         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
581                 return -ENOMEM;
582
583         return 0;
584 }
585
586 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
587         int64_t l;
588
589         assert(i);
590         assert(property);
591         assert(data);
592
593         l = (int64_t) *(long*) data;
594
595         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
596                 return -ENOMEM;
597
598         return 0;
599 }
600
601 const char *bus_errno_to_dbus(int error) {
602
603         switch(error) {
604
605         case -EINVAL:
606                 return DBUS_ERROR_INVALID_ARGS;
607
608         case -ENOMEM:
609                 return DBUS_ERROR_NO_MEMORY;
610
611         case -EPERM:
612         case -EACCES:
613                 return DBUS_ERROR_ACCESS_DENIED;
614
615         case -ESRCH:
616                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
617
618         case -ENOENT:
619                 return DBUS_ERROR_FILE_NOT_FOUND;
620
621         case -EEXIST:
622                 return DBUS_ERROR_FILE_EXISTS;
623
624         case -ETIMEDOUT:
625         case -ETIME:
626                 return DBUS_ERROR_TIMEOUT;
627
628         case -EIO:
629                 return DBUS_ERROR_IO_ERROR;
630
631         case -ENETRESET:
632         case -ECONNABORTED:
633         case -ECONNRESET:
634                 return DBUS_ERROR_DISCONNECTED;
635         }
636
637         return DBUS_ERROR_FAILED;
638 }
639
640 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
641         DBusMessage *reply = NULL;
642         const char *name, *text;
643
644         if (berror && dbus_error_is_set(berror)) {
645                 name = berror->name;
646                 text = berror->message;
647         } else {
648                 name = bus_errno_to_dbus(error);
649                 text = strerror(-error);
650         }
651
652         if (!(reply = dbus_message_new_error(message, name, text)))
653                 goto oom;
654
655         if (!dbus_connection_send(c, reply, NULL))
656                 goto oom;
657
658         dbus_message_unref(reply);
659
660         if (berror)
661                 dbus_error_free(berror);
662
663         return DBUS_HANDLER_RESULT_HANDLED;
664
665 oom:
666         if (reply)
667                 dbus_message_unref(reply);
668
669         if (berror)
670                 dbus_error_free(berror);
671
672         return DBUS_HANDLER_RESULT_NEED_MEMORY;
673 }
674
675 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
676         DBusMessage *m;
677         DBusMessageIter iter, sub;
678         const char *i;
679
680         assert(interface);
681         assert(properties);
682
683         if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
684                 goto oom;
685
686         dbus_message_iter_init_append(m, &iter);
687
688         /* We won't send any property values, since they might be
689          * large and sometimes not cheap to generated */
690
691         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
692             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
693             !dbus_message_iter_close_container(&iter, &sub) ||
694             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
695                 goto oom;
696
697         NULSTR_FOREACH(i, properties)
698                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
699                         goto oom;
700
701         if (!dbus_message_iter_close_container(&iter, &sub))
702                 goto oom;
703
704         return m;
705
706 oom:
707         if (m)
708                 dbus_message_unref(m);
709
710         return NULL;
711 }
712
713 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
714         unsigned flags;
715         uint32_t events = 0;
716
717         assert(bus_watch);
718
719         /* no watch flags for disabled watches */
720         if (!dbus_watch_get_enabled(bus_watch))
721                 return 0;
722
723         flags = dbus_watch_get_flags(bus_watch);
724
725         if (flags & DBUS_WATCH_READABLE)
726                 events |= EPOLLIN;
727         if (flags & DBUS_WATCH_WRITABLE)
728                 events |= EPOLLOUT;
729
730         return events | EPOLLHUP | EPOLLERR;
731 }
732
733 unsigned bus_events_to_flags(uint32_t events) {
734         unsigned flags = 0;
735
736         if (events & EPOLLIN)
737                 flags |= DBUS_WATCH_READABLE;
738         if (events & EPOLLOUT)
739                 flags |= DBUS_WATCH_WRITABLE;
740         if (events & EPOLLHUP)
741                 flags |= DBUS_WATCH_HANGUP;
742         if (events & EPOLLERR)
743                 flags |= DBUS_WATCH_ERROR;
744
745         return flags;
746 }
747
748 int bus_parse_strv(DBusMessage *m, char ***_l) {
749         DBusMessageIter iter;
750
751         assert(m);
752         assert(_l);
753
754         if (!dbus_message_iter_init(m, &iter))
755                 return -EINVAL;
756
757         return bus_parse_strv_iter(&iter, _l);
758 }
759
760 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
761         DBusMessageIter sub;
762         unsigned n = 0, i = 0;
763         char **l;
764
765         assert(iter);
766         assert(_l);
767
768         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
769             dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
770             return -EINVAL;
771
772         dbus_message_iter_recurse(iter, &sub);
773
774         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
775                 n++;
776                 dbus_message_iter_next(&sub);
777         }
778
779         if (!(l = new(char*, n+1)))
780                 return -ENOMEM;
781
782         dbus_message_iter_recurse(iter, &sub);
783
784         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
785                 const char *s;
786
787                 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
788                 dbus_message_iter_get_basic(&sub, &s);
789
790                 if (!(l[i++] = strdup(s))) {
791                         strv_free(l);
792                         return -ENOMEM;
793                 }
794
795                 dbus_message_iter_next(&sub);
796         }
797
798         assert(i == n);
799         l[i] = NULL;
800
801         if (_l)
802                 *_l = l;
803
804         return 0;
805 }
806
807 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
808         DBusMessageIter sub;
809
810         assert(iter);
811
812         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
813                 return -ENOMEM;
814
815         STRV_FOREACH(l, l)
816                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
817                         return -ENOMEM;
818
819         if (!dbus_message_iter_close_container(iter, &sub))
820                 return -ENOMEM;
821
822         return 0;
823 }
824
825 int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
826
827         assert(iter);
828         assert(data);
829
830         if (dbus_message_iter_get_arg_type(iter) != type)
831                 return -EIO;
832
833         dbus_message_iter_get_basic(iter, data);
834
835         if (!dbus_message_iter_next(iter) != !next)
836                 return -EIO;
837
838         return 0;
839 }
840
841 int generic_print_property(const char *name, DBusMessageIter *iter, bool all) {
842         assert(name);
843         assert(iter);
844
845         switch (dbus_message_iter_get_arg_type(iter)) {
846
847         case DBUS_TYPE_STRING: {
848                 const char *s;
849                 dbus_message_iter_get_basic(iter, &s);
850
851                 if (all || !isempty(s))
852                         printf("%s=%s\n", name, s);
853
854                 return 1;
855         }
856
857         case DBUS_TYPE_BOOLEAN: {
858                 dbus_bool_t b;
859
860                 dbus_message_iter_get_basic(iter, &b);
861                 printf("%s=%s\n", name, yes_no(b));
862
863                 return 1;
864         }
865
866         case DBUS_TYPE_UINT64: {
867                 uint64_t u;
868                 dbus_message_iter_get_basic(iter, &u);
869
870                 /* Yes, heuristics! But we can change this check
871                  * should it turn out to not be sufficient */
872
873                 if (endswith(name, "Timestamp")) {
874                         char timestamp[FORMAT_TIMESTAMP_MAX], *t;
875
876                         t = format_timestamp(timestamp, sizeof(timestamp), u);
877                         if (t || all)
878                                 printf("%s=%s\n", name, strempty(t));
879
880                 } else if (strstr(name, "USec")) {
881                         char timespan[FORMAT_TIMESPAN_MAX];
882
883                         printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
884                 } else
885                         printf("%s=%llu\n", name, (unsigned long long) u);
886
887                 return 1;
888         }
889
890         case DBUS_TYPE_UINT32: {
891                 uint32_t u;
892                 dbus_message_iter_get_basic(iter, &u);
893
894                 if (strstr(name, "UMask") || strstr(name, "Mode"))
895                         printf("%s=%04o\n", name, u);
896                 else
897                         printf("%s=%u\n", name, (unsigned) u);
898
899                 return 1;
900         }
901
902         case DBUS_TYPE_INT32: {
903                 int32_t i;
904                 dbus_message_iter_get_basic(iter, &i);
905
906                 printf("%s=%i\n", name, (int) i);
907                 return 1;
908         }
909
910         case DBUS_TYPE_DOUBLE: {
911                 double d;
912                 dbus_message_iter_get_basic(iter, &d);
913
914                 printf("%s=%g\n", name, d);
915                 return 1;
916         }
917
918         case DBUS_TYPE_ARRAY:
919
920                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
921                         DBusMessageIter sub;
922                         bool space = false;
923
924                         dbus_message_iter_recurse(iter, &sub);
925                         if (all ||
926                             dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
927                                 printf("%s=", name);
928
929                                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
930                                         const char *s;
931
932                                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
933                                         dbus_message_iter_get_basic(&sub, &s);
934                                         printf("%s%s", space ? " " : "", s);
935
936                                         space = true;
937                                         dbus_message_iter_next(&sub);
938                                 }
939
940                                 puts("");
941                         }
942
943                         return 1;
944
945                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
946                         DBusMessageIter sub;
947
948                         dbus_message_iter_recurse(iter, &sub);
949                         if (all ||
950                             dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
951                                 printf("%s=", name);
952
953                                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
954                                         uint8_t u;
955
956                                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
957                                         dbus_message_iter_get_basic(&sub, &u);
958                                         printf("%02x", u);
959
960                                         dbus_message_iter_next(&sub);
961                                 }
962
963                                 puts("");
964                         }
965
966                         return 1;
967                 }
968
969                 break;
970         }
971
972         return 0;
973 }