chiark / gitweb /
dbus: more efficient implementation of properties
[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 BusBoundProperties *bound_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") && bound_properties) {
282                 const char *interface, *property;
283                 const BusBoundProperties *bp;
284                 const BusProperty *p;
285                 void *data;
286                 DBusMessageIter iter, sub;
287
288                 if (!dbus_message_get_args(
289                             message,
290                             &error,
291                             DBUS_TYPE_STRING, &interface,
292                             DBUS_TYPE_STRING, &property,
293                             DBUS_TYPE_INVALID))
294                         return bus_send_error_reply(c, message, &error, -EINVAL);
295
296                 for (bp = bound_properties; bp->interface; bp++) {
297                         if (!streq(bp->interface, interface))
298                                 continue;
299
300                         for (p = bp->properties; p->property; p++)
301                                 if (streq(p->property, property))
302                                         goto get_prop;
303                 }
304
305                 /* no match */
306                 if (!nulstr_contains(interfaces, interface))
307                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
308                 else
309                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_PROPERTY, "Unknown property");
310
311                 return bus_send_error_reply(c, message, &error, -EINVAL);
312
313 get_prop:
314                 reply = dbus_message_new_method_return(message);
315                 if (!reply)
316                         goto oom;
317
318                 dbus_message_iter_init_append(reply, &iter);
319
320                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
321                         goto oom;
322
323                 data = (char*)bp->base + p->offset;
324                 if (p->indirect)
325                         data = *(void**)data;
326                 r = p->append(&sub, property, data);
327                 if (r < 0) {
328                         if (r == -ENOMEM)
329                                 goto oom;
330
331                         dbus_message_unref(reply);
332                         return bus_send_error_reply(c, message, NULL, r);
333                 }
334
335                 if (!dbus_message_iter_close_container(&iter, &sub))
336                         goto oom;
337
338         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && bound_properties) {
339                 const char *interface;
340                 const BusBoundProperties *bp;
341                 const BusProperty *p;
342                 DBusMessageIter iter, sub, sub2, sub3;
343
344                 if (!dbus_message_get_args(
345                             message,
346                             &error,
347                             DBUS_TYPE_STRING, &interface,
348                             DBUS_TYPE_INVALID))
349                         return bus_send_error_reply(c, message, &error, -EINVAL);
350
351                 if (interface[0] && !nulstr_contains(interfaces, interface)) {
352                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
353                         return bus_send_error_reply(c, message, &error, -EINVAL);
354                 }
355
356                 if (!(reply = dbus_message_new_method_return(message)))
357                         goto oom;
358
359                 dbus_message_iter_init_append(reply, &iter);
360
361                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
362                         goto oom;
363
364                 for (bp = bound_properties; bp->interface; bp++) {
365                         if (interface[0] && !streq(bp->interface, interface))
366                                 continue;
367
368                         for (p = bp->properties; p->property; p++) {
369                                 void *data;
370
371                                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
372                                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
373                                     !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
374                                         goto oom;
375
376                                 data = (char*)bp->base + p->offset;
377                                 if (p->indirect)
378                                         data = *(void**)data;
379                                 r = p->append(&sub3, p->property, data);
380                                 if (r < 0) {
381                                         if (r == -ENOMEM)
382                                                 goto oom;
383
384                                         dbus_message_unref(reply);
385                                         return bus_send_error_reply(c, message, NULL, r);
386                                 }
387
388                                 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
389                                     !dbus_message_iter_close_container(&sub, &sub2))
390                                         goto oom;
391                         }
392                 }
393
394                 if (!dbus_message_iter_close_container(&iter, &sub))
395                         goto oom;
396
397         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set") && bound_properties) {
398                 const char *interface, *property;
399                 DBusMessageIter iter;
400                 const BusBoundProperties *bp;
401                 const BusProperty *p;
402                 DBusMessageIter sub;
403                 char *sig;
404
405                 if (!dbus_message_iter_init(message, &iter) ||
406                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
407                         return bus_send_error_reply(c, message, NULL, -EINVAL);
408
409                 dbus_message_iter_get_basic(&iter, &interface);
410
411                 if (!dbus_message_iter_next(&iter) ||
412                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
413                         return bus_send_error_reply(c, message, NULL, -EINVAL);
414
415                 dbus_message_iter_get_basic(&iter, &property);
416
417                 if (!dbus_message_iter_next(&iter) ||
418                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT ||
419                     dbus_message_iter_has_next(&iter))
420                         return bus_send_error_reply(c, message, NULL, -EINVAL);
421
422                 for (bp = bound_properties; bp->interface; bp++) {
423                         if (!streq(bp->interface, interface))
424                                 continue;
425
426                         for (p = bp->properties; p->property; p++)
427                                 if (streq(p->property, property))
428                                         goto set_prop;
429                 }
430
431                 /* no match */
432                 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 set_prop:
440                 if (!p->set) {
441                         dbus_set_error_const(&error, DBUS_ERROR_PROPERTY_READ_ONLY, "Property read-only");
442                         return bus_send_error_reply(c, message, &error, -EINVAL);
443                 }
444
445                 dbus_message_iter_recurse(&iter, &sub);
446
447                 sig = dbus_message_iter_get_signature(&sub);
448                 if (!sig)
449                         goto oom;
450
451                 if (!streq(sig, p->signature)) {
452                         dbus_free(sig);
453                         return bus_send_error_reply(c, message, NULL, -EINVAL);
454                 }
455
456                 dbus_free(sig);
457
458                 r = p->set(&sub, property);
459                 if (r < 0) {
460                         if (r == -ENOMEM)
461                                 goto oom;
462                         return bus_send_error_reply(c, message, NULL, r);
463                 }
464
465                 reply = dbus_message_new_method_return(message);
466                 if (!reply)
467                         goto oom;
468         } else {
469                 const char *interface = dbus_message_get_interface(message);
470
471                 if (!interface || !nulstr_contains(interfaces, interface)) {
472                         dbus_set_error_const(&error, DBUS_ERROR_UNKNOWN_INTERFACE, "Unknown interface");
473                         return bus_send_error_reply(c, message, &error, -EINVAL);
474                 }
475         }
476
477         if (reply) {
478                 if (!dbus_connection_send(c, reply, NULL))
479                         goto oom;
480
481                 dbus_message_unref(reply);
482                 return DBUS_HANDLER_RESULT_HANDLED;
483         }
484
485         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
486
487 oom:
488         if (reply)
489                 dbus_message_unref(reply);
490
491         dbus_error_free(&error);
492
493         return DBUS_HANDLER_RESULT_NEED_MEMORY;
494 }
495
496 int bus_property_append_string(DBusMessageIter *i, const char *property, void *data) {
497         const char *t = data;
498
499         assert(i);
500         assert(property);
501
502         if (!t)
503                 t = "";
504
505         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
506                 return -ENOMEM;
507
508         return 0;
509 }
510
511 int bus_property_append_strv(DBusMessageIter *i, const char *property, void *data) {
512         char **t = data;
513
514         assert(i);
515         assert(property);
516
517         return bus_append_strv_iter(i, t);
518 }
519
520 int bus_property_append_bool(DBusMessageIter *i, const char *property, void *data) {
521         bool *b = data;
522         dbus_bool_t db;
523
524         assert(i);
525         assert(property);
526         assert(b);
527
528         db = *b;
529
530         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
531                 return -ENOMEM;
532
533         return 0;
534 }
535
536 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
537         assert(i);
538         assert(property);
539         assert(data);
540
541         /* Let's ensure that usec_t is actually 64bit, and hence this
542          * function can be used for usec_t */
543         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
544
545         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
546                 return -ENOMEM;
547
548         return 0;
549 }
550
551 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
552         assert(i);
553         assert(property);
554         assert(data);
555
556         /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
557          * 32bit, and hence this function can be used for
558          * pid_t/mode_t/uid_t/gid_t */
559         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
560         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
561         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
562         assert_cc(sizeof(uint32_t) == sizeof(uid_t));
563         assert_cc(sizeof(uint32_t) == sizeof(gid_t));
564
565         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
566                 return -ENOMEM;
567
568         return 0;
569 }
570
571 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
572         assert(i);
573         assert(property);
574         assert(data);
575
576         assert_cc(sizeof(int32_t) == sizeof(int));
577
578         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
579                 return -ENOMEM;
580
581         return 0;
582 }
583
584 int bus_property_append_size(DBusMessageIter *i, const char *property, void *data) {
585         uint64_t u;
586
587         assert(i);
588         assert(property);
589         assert(data);
590
591         u = (uint64_t) *(size_t*) data;
592
593         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
594                 return -ENOMEM;
595
596         return 0;
597 }
598
599 int bus_property_append_ul(DBusMessageIter *i, const char *property, void *data) {
600         uint64_t u;
601
602         assert(i);
603         assert(property);
604         assert(data);
605
606         u = (uint64_t) *(unsigned long*) data;
607
608         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
609                 return -ENOMEM;
610
611         return 0;
612 }
613
614 int bus_property_append_long(DBusMessageIter *i, const char *property, void *data) {
615         int64_t l;
616
617         assert(i);
618         assert(property);
619         assert(data);
620
621         l = (int64_t) *(long*) data;
622
623         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT64, &l))
624                 return -ENOMEM;
625
626         return 0;
627 }
628
629 const char *bus_errno_to_dbus(int error) {
630
631         switch(error) {
632
633         case -EINVAL:
634                 return DBUS_ERROR_INVALID_ARGS;
635
636         case -ENOMEM:
637                 return DBUS_ERROR_NO_MEMORY;
638
639         case -EPERM:
640         case -EACCES:
641                 return DBUS_ERROR_ACCESS_DENIED;
642
643         case -ESRCH:
644                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
645
646         case -ENOENT:
647                 return DBUS_ERROR_FILE_NOT_FOUND;
648
649         case -EEXIST:
650                 return DBUS_ERROR_FILE_EXISTS;
651
652         case -ETIMEDOUT:
653         case -ETIME:
654                 return DBUS_ERROR_TIMEOUT;
655
656         case -EIO:
657                 return DBUS_ERROR_IO_ERROR;
658
659         case -ENETRESET:
660         case -ECONNABORTED:
661         case -ECONNRESET:
662                 return DBUS_ERROR_DISCONNECTED;
663         }
664
665         return DBUS_ERROR_FAILED;
666 }
667
668 DBusHandlerResult bus_send_error_reply(DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
669         DBusMessage *reply = NULL;
670         const char *name, *text;
671
672         if (berror && dbus_error_is_set(berror)) {
673                 name = berror->name;
674                 text = berror->message;
675         } else {
676                 name = bus_errno_to_dbus(error);
677                 text = strerror(-error);
678         }
679
680         if (!(reply = dbus_message_new_error(message, name, text)))
681                 goto oom;
682
683         if (!dbus_connection_send(c, reply, NULL))
684                 goto oom;
685
686         dbus_message_unref(reply);
687
688         if (berror)
689                 dbus_error_free(berror);
690
691         return DBUS_HANDLER_RESULT_HANDLED;
692
693 oom:
694         if (reply)
695                 dbus_message_unref(reply);
696
697         if (berror)
698                 dbus_error_free(berror);
699
700         return DBUS_HANDLER_RESULT_NEED_MEMORY;
701 }
702
703 DBusMessage* bus_properties_changed_new(const char *path, const char *interface, const char *properties) {
704         DBusMessage *m;
705         DBusMessageIter iter, sub;
706         const char *i;
707
708         assert(interface);
709         assert(properties);
710
711         if (!(m = dbus_message_new_signal(path, "org.freedesktop.DBus.Properties", "PropertiesChanged")))
712                 goto oom;
713
714         dbus_message_iter_init_append(m, &iter);
715
716         /* We won't send any property values, since they might be
717          * large and sometimes not cheap to generated */
718
719         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface) ||
720             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub) ||
721             !dbus_message_iter_close_container(&iter, &sub) ||
722             !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub))
723                 goto oom;
724
725         NULSTR_FOREACH(i, properties)
726                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &i))
727                         goto oom;
728
729         if (!dbus_message_iter_close_container(&iter, &sub))
730                 goto oom;
731
732         return m;
733
734 oom:
735         if (m)
736                 dbus_message_unref(m);
737
738         return NULL;
739 }
740
741 uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
742         unsigned flags;
743         uint32_t events = 0;
744
745         assert(bus_watch);
746
747         /* no watch flags for disabled watches */
748         if (!dbus_watch_get_enabled(bus_watch))
749                 return 0;
750
751         flags = dbus_watch_get_flags(bus_watch);
752
753         if (flags & DBUS_WATCH_READABLE)
754                 events |= EPOLLIN;
755         if (flags & DBUS_WATCH_WRITABLE)
756                 events |= EPOLLOUT;
757
758         return events | EPOLLHUP | EPOLLERR;
759 }
760
761 unsigned bus_events_to_flags(uint32_t events) {
762         unsigned flags = 0;
763
764         if (events & EPOLLIN)
765                 flags |= DBUS_WATCH_READABLE;
766         if (events & EPOLLOUT)
767                 flags |= DBUS_WATCH_WRITABLE;
768         if (events & EPOLLHUP)
769                 flags |= DBUS_WATCH_HANGUP;
770         if (events & EPOLLERR)
771                 flags |= DBUS_WATCH_ERROR;
772
773         return flags;
774 }
775
776 int bus_parse_strv(DBusMessage *m, char ***_l) {
777         DBusMessageIter iter;
778
779         assert(m);
780         assert(_l);
781
782         if (!dbus_message_iter_init(m, &iter))
783                 return -EINVAL;
784
785         return bus_parse_strv_iter(&iter, _l);
786 }
787
788 int bus_parse_strv_iter(DBusMessageIter *iter, char ***_l) {
789         DBusMessageIter sub;
790         unsigned n = 0, i = 0;
791         char **l;
792
793         assert(iter);
794         assert(_l);
795
796         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY ||
797             dbus_message_iter_get_element_type(iter) != DBUS_TYPE_STRING)
798             return -EINVAL;
799
800         dbus_message_iter_recurse(iter, &sub);
801
802         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
803                 n++;
804                 dbus_message_iter_next(&sub);
805         }
806
807         if (!(l = new(char*, n+1)))
808                 return -ENOMEM;
809
810         dbus_message_iter_recurse(iter, &sub);
811
812         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
813                 const char *s;
814
815                 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
816                 dbus_message_iter_get_basic(&sub, &s);
817
818                 if (!(l[i++] = strdup(s))) {
819                         strv_free(l);
820                         return -ENOMEM;
821                 }
822
823                 dbus_message_iter_next(&sub);
824         }
825
826         assert(i == n);
827         l[i] = NULL;
828
829         if (_l)
830                 *_l = l;
831
832         return 0;
833 }
834
835 int bus_append_strv_iter(DBusMessageIter *iter, char **l) {
836         DBusMessageIter sub;
837
838         assert(iter);
839
840         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "s", &sub))
841                 return -ENOMEM;
842
843         STRV_FOREACH(l, l)
844                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, l))
845                         return -ENOMEM;
846
847         if (!dbus_message_iter_close_container(iter, &sub))
848                 return -ENOMEM;
849
850         return 0;
851 }
852
853 int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
854
855         assert(iter);
856         assert(data);
857
858         if (dbus_message_iter_get_arg_type(iter) != type)
859                 return -EIO;
860
861         dbus_message_iter_get_basic(iter, data);
862
863         if (!dbus_message_iter_next(iter) != !next)
864                 return -EIO;
865
866         return 0;
867 }
868
869 int generic_print_property(const char *name, DBusMessageIter *iter, bool all) {
870         assert(name);
871         assert(iter);
872
873         switch (dbus_message_iter_get_arg_type(iter)) {
874
875         case DBUS_TYPE_STRING: {
876                 const char *s;
877                 dbus_message_iter_get_basic(iter, &s);
878
879                 if (all || !isempty(s))
880                         printf("%s=%s\n", name, s);
881
882                 return 1;
883         }
884
885         case DBUS_TYPE_BOOLEAN: {
886                 dbus_bool_t b;
887
888                 dbus_message_iter_get_basic(iter, &b);
889                 printf("%s=%s\n", name, yes_no(b));
890
891                 return 1;
892         }
893
894         case DBUS_TYPE_UINT64: {
895                 uint64_t u;
896                 dbus_message_iter_get_basic(iter, &u);
897
898                 /* Yes, heuristics! But we can change this check
899                  * should it turn out to not be sufficient */
900
901                 if (endswith(name, "Timestamp")) {
902                         char timestamp[FORMAT_TIMESTAMP_MAX], *t;
903
904                         t = format_timestamp(timestamp, sizeof(timestamp), u);
905                         if (t || all)
906                                 printf("%s=%s\n", name, strempty(t));
907
908                 } else if (strstr(name, "USec")) {
909                         char timespan[FORMAT_TIMESPAN_MAX];
910
911                         printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
912                 } else
913                         printf("%s=%llu\n", name, (unsigned long long) u);
914
915                 return 1;
916         }
917
918         case DBUS_TYPE_UINT32: {
919                 uint32_t u;
920                 dbus_message_iter_get_basic(iter, &u);
921
922                 if (strstr(name, "UMask") || strstr(name, "Mode"))
923                         printf("%s=%04o\n", name, u);
924                 else
925                         printf("%s=%u\n", name, (unsigned) u);
926
927                 return 1;
928         }
929
930         case DBUS_TYPE_INT32: {
931                 int32_t i;
932                 dbus_message_iter_get_basic(iter, &i);
933
934                 printf("%s=%i\n", name, (int) i);
935                 return 1;
936         }
937
938         case DBUS_TYPE_DOUBLE: {
939                 double d;
940                 dbus_message_iter_get_basic(iter, &d);
941
942                 printf("%s=%g\n", name, d);
943                 return 1;
944         }
945
946         case DBUS_TYPE_ARRAY:
947
948                 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
949                         DBusMessageIter sub;
950                         bool space = false;
951
952                         dbus_message_iter_recurse(iter, &sub);
953                         if (all ||
954                             dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
955                                 printf("%s=", name);
956
957                                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
958                                         const char *s;
959
960                                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
961                                         dbus_message_iter_get_basic(&sub, &s);
962                                         printf("%s%s", space ? " " : "", s);
963
964                                         space = true;
965                                         dbus_message_iter_next(&sub);
966                                 }
967
968                                 puts("");
969                         }
970
971                         return 1;
972
973                 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
974                         DBusMessageIter sub;
975
976                         dbus_message_iter_recurse(iter, &sub);
977                         if (all ||
978                             dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
979                                 printf("%s=", name);
980
981                                 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
982                                         uint8_t u;
983
984                                         assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
985                                         dbus_message_iter_get_basic(&sub, &u);
986                                         printf("%02x", u);
987
988                                         dbus_message_iter_next(&sub);
989                                 }
990
991                                 puts("");
992                         }
993
994                         return 1;
995                 }
996
997                 break;
998         }
999
1000         return 0;
1001 }
1002
1003 static void release_name_pending_cb(DBusPendingCall *pending, void *userdata) {
1004         DBusMessage *reply;
1005         DBusConnection *bus = userdata;
1006
1007         assert_se(reply = dbus_pending_call_steal_reply(pending));
1008         dbus_message_unref(reply);
1009
1010         dbus_connection_close(bus);
1011 }
1012
1013 void bus_async_unregister_and_exit(DBusConnection *bus, const char *name) {
1014         DBusMessage *m = NULL;
1015         DBusPendingCall *pending = NULL;
1016
1017         assert(bus);
1018
1019         /* We unregister the name here, but we continue to process
1020          * requests, until we get the response for it, so that all
1021          * requests are guaranteed to be processed. */
1022
1023         m = dbus_message_new_method_call(
1024                         DBUS_SERVICE_DBUS,
1025                         DBUS_PATH_DBUS,
1026                         DBUS_INTERFACE_DBUS,
1027                         "ReleaseName");
1028         if (!m)
1029                 goto oom;
1030
1031         if (!dbus_message_append_args(
1032                             m,
1033                             DBUS_TYPE_STRING,
1034                             &name,
1035                             DBUS_TYPE_INVALID))
1036                 goto oom;
1037
1038         if (!dbus_connection_send_with_reply(bus, m, &pending, -1))
1039                 goto oom;
1040
1041         if (!dbus_pending_call_set_notify(pending, release_name_pending_cb, bus, NULL))
1042                 goto oom;
1043
1044         dbus_message_unref(m);
1045         dbus_pending_call_unref(pending);
1046
1047         return;
1048
1049 oom:
1050         log_error("Out of memory");
1051
1052         if (pending) {
1053                 dbus_pending_call_cancel(pending);
1054                 dbus_pending_call_unref(pending);
1055         }
1056
1057         if (m)
1058                 dbus_message_unref(m);
1059 }
1060
1061 DBusHandlerResult bus_exit_idle_filter(DBusConnection *bus, DBusMessage *m, void *userdata) {
1062         usec_t *remain_until = userdata;
1063
1064         assert(bus);
1065         assert(m);
1066         assert(remain_until);
1067
1068         /* Everytime we get a new message we reset out timeout */
1069         *remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
1070
1071         if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected"))
1072                 dbus_connection_close(bus);
1073
1074         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1075 }