chiark / gitweb /
bus: when NULL is specified as property callback, automatically handle serialization...
[elogind.git] / src / libsystemd-bus / sd-bus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <endian.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <netdb.h>
27 #include <sys/poll.h>
28 #include <byteswap.h>
29 #include <sys/mman.h>
30 #include <pthread.h>
31
32 #include "util.h"
33 #include "macro.h"
34 #include "strv.h"
35 #include "set.h"
36 #include "missing.h"
37
38 #include "sd-bus.h"
39 #include "bus-internal.h"
40 #include "bus-message.h"
41 #include "bus-type.h"
42 #include "bus-socket.h"
43 #include "bus-kernel.h"
44 #include "bus-control.h"
45 #include "bus-introspect.h"
46 #include "bus-signature.h"
47
48 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
49
50 static void bus_close_fds(sd_bus *b) {
51         assert(b);
52
53         if (b->input_fd >= 0)
54                 close_nointr_nofail(b->input_fd);
55
56         if (b->output_fd >= 0 && b->output_fd != b->input_fd)
57                 close_nointr_nofail(b->output_fd);
58
59         b->input_fd = b->output_fd = -1;
60 }
61
62 static void bus_node_destroy(sd_bus *b, struct node *n) {
63         struct node_callback *c;
64         struct node_vtable *v;
65         struct node_enumerator *e;
66
67         assert(b);
68
69         if (!n)
70                 return;
71
72         while (n->child)
73                 bus_node_destroy(b, n->child);
74
75         while ((c = n->callbacks)) {
76                 LIST_REMOVE(struct node_callback, callbacks, n->callbacks, c);
77                 free(c);
78         }
79
80         while ((v = n->vtables)) {
81                 LIST_REMOVE(struct node_vtable, vtables, n->vtables, v);
82                 free(v->interface);
83                 free(v);
84         }
85
86         while ((e = n->enumerators)) {
87                 LIST_REMOVE(struct node_enumerator, enumerators, n->enumerators, e);
88                 free(e);
89         }
90
91         if (n->parent)
92                 LIST_REMOVE(struct node, siblings, n->parent->child, n);
93
94         assert_se(hashmap_remove(b->nodes, n->path) == n);
95         free(n->path);
96         free(n);
97 }
98
99 static void bus_free(sd_bus *b) {
100         struct filter_callback *f;
101         struct node *n;
102         unsigned i;
103
104         assert(b);
105
106         bus_close_fds(b);
107
108         if (b->kdbus_buffer)
109                 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
110
111         free(b->rbuffer);
112         free(b->unique_name);
113         free(b->auth_buffer);
114         free(b->address);
115         free(b->kernel);
116
117         free(b->exec_path);
118         strv_free(b->exec_argv);
119
120         close_many(b->fds, b->n_fds);
121         free(b->fds);
122
123         for (i = 0; i < b->rqueue_size; i++)
124                 sd_bus_message_unref(b->rqueue[i]);
125         free(b->rqueue);
126
127         for (i = 0; i < b->wqueue_size; i++)
128                 sd_bus_message_unref(b->wqueue[i]);
129         free(b->wqueue);
130
131         hashmap_free_free(b->reply_callbacks);
132         prioq_free(b->reply_callbacks_prioq);
133
134         while ((f = b->filter_callbacks)) {
135                 LIST_REMOVE(struct filter_callback, callbacks, b->filter_callbacks, f);
136                 free(f);
137         }
138
139         bus_match_free(&b->match_callbacks);
140
141         hashmap_free_free(b->vtable_methods);
142         hashmap_free_free(b->vtable_properties);
143
144         while ((n = hashmap_first(b->nodes)))
145                 bus_node_destroy(b, n);
146
147         hashmap_free(b->nodes);
148
149         bus_kernel_flush_memfd(b);
150
151         assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
152
153         free(b);
154 }
155
156 int sd_bus_new(sd_bus **ret) {
157         sd_bus *r;
158
159         if (!ret)
160                 return -EINVAL;
161
162         r = new0(sd_bus, 1);
163         if (!r)
164                 return -ENOMEM;
165
166         r->n_ref = REFCNT_INIT;
167         r->input_fd = r->output_fd = -1;
168         r->message_version = 1;
169         r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
170         r->original_pid = getpid();
171
172         assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
173
174         /* We guarantee that wqueue always has space for at least one
175          * entry */
176         r->wqueue = new(sd_bus_message*, 1);
177         if (!r->wqueue) {
178                 free(r);
179                 return -ENOMEM;
180         }
181
182         *ret = r;
183         return 0;
184 }
185
186 int sd_bus_set_address(sd_bus *bus, const char *address) {
187         char *a;
188
189         if (!bus)
190                 return -EINVAL;
191         if (bus->state != BUS_UNSET)
192                 return -EPERM;
193         if (!address)
194                 return -EINVAL;
195         if (bus_pid_changed(bus))
196                 return -ECHILD;
197
198         a = strdup(address);
199         if (!a)
200                 return -ENOMEM;
201
202         free(bus->address);
203         bus->address = a;
204
205         return 0;
206 }
207
208 int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
209         if (!bus)
210                 return -EINVAL;
211         if (bus->state != BUS_UNSET)
212                 return -EPERM;
213         if (input_fd < 0)
214                 return -EINVAL;
215         if (output_fd < 0)
216                 return -EINVAL;
217         if (bus_pid_changed(bus))
218                 return -ECHILD;
219
220         bus->input_fd = input_fd;
221         bus->output_fd = output_fd;
222         return 0;
223 }
224
225 int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
226         char *p, **a;
227
228         if (!bus)
229                 return -EINVAL;
230         if (bus->state != BUS_UNSET)
231                 return -EPERM;
232         if (!path)
233                 return -EINVAL;
234         if (strv_isempty(argv))
235                 return -EINVAL;
236         if (bus_pid_changed(bus))
237                 return -ECHILD;
238
239         p = strdup(path);
240         if (!p)
241                 return -ENOMEM;
242
243         a = strv_copy(argv);
244         if (!a) {
245                 free(p);
246                 return -ENOMEM;
247         }
248
249         free(bus->exec_path);
250         strv_free(bus->exec_argv);
251
252         bus->exec_path = p;
253         bus->exec_argv = a;
254
255         return 0;
256 }
257
258 int sd_bus_set_bus_client(sd_bus *bus, int b) {
259         if (!bus)
260                 return -EINVAL;
261         if (bus->state != BUS_UNSET)
262                 return -EPERM;
263         if (bus_pid_changed(bus))
264                 return -ECHILD;
265
266         bus->bus_client = !!b;
267         return 0;
268 }
269
270 int sd_bus_negotiate_fds(sd_bus *bus, int b) {
271         if (!bus)
272                 return -EINVAL;
273         if (bus->state != BUS_UNSET)
274                 return -EPERM;
275         if (bus_pid_changed(bus))
276                 return -ECHILD;
277
278         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
279         return 0;
280 }
281
282 int sd_bus_negotiate_attach_comm(sd_bus *bus, int b) {
283         if (!bus)
284                 return -EINVAL;
285         if (bus->state != BUS_UNSET)
286                 return -EPERM;
287         if (bus_pid_changed(bus))
288                 return -ECHILD;
289
290         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_COMM, b);
291         return 0;
292 }
293
294 int sd_bus_negotiate_attach_exe(sd_bus *bus, int b) {
295         if (!bus)
296                 return -EINVAL;
297         if (bus->state != BUS_UNSET)
298                 return -EPERM;
299         if (bus_pid_changed(bus))
300                 return -ECHILD;
301
302         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_EXE, b);
303         return 0;
304 }
305
306 int sd_bus_negotiate_attach_cmdline(sd_bus *bus, int b) {
307         if (!bus)
308                 return -EINVAL;
309         if (bus->state != BUS_UNSET)
310                 return -EPERM;
311         if (bus_pid_changed(bus))
312                 return -ECHILD;
313
314         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CMDLINE, b);
315         return 0;
316 }
317
318 int sd_bus_negotiate_attach_cgroup(sd_bus *bus, int b) {
319         if (!bus)
320                 return -EINVAL;
321         if (bus->state != BUS_UNSET)
322                 return -EPERM;
323         if (bus_pid_changed(bus))
324                 return -ECHILD;
325
326         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CGROUP, b);
327         return 0;
328 }
329
330 int sd_bus_negotiate_attach_caps(sd_bus *bus, int b) {
331         if (!bus)
332                 return -EINVAL;
333         if (bus->state != BUS_UNSET)
334                 return -EPERM;
335         if (bus_pid_changed(bus))
336                 return -ECHILD;
337
338         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_CAPS, b);
339         return 0;
340 }
341
342 int sd_bus_negotiate_attach_selinux_context(sd_bus *bus, int b) {
343         if (!bus)
344                 return -EINVAL;
345         if (bus->state != BUS_UNSET)
346                 return -EPERM;
347         if (bus_pid_changed(bus))
348                 return -ECHILD;
349
350         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_SECLABEL, b);
351         return 0;
352 }
353
354 int sd_bus_negotiate_attach_audit(sd_bus *bus, int b) {
355         if (!bus)
356                 return -EINVAL;
357         if (bus->state != BUS_UNSET)
358                 return -EPERM;
359         if (bus_pid_changed(bus))
360                 return -ECHILD;
361
362         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ATTACH_AUDIT, b);
363         return 0;
364 }
365
366 int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
367         if (!bus)
368                 return -EINVAL;
369         if (!b && !sd_id128_equal(server_id, SD_ID128_NULL))
370                 return -EINVAL;
371         if (bus->state != BUS_UNSET)
372                 return -EPERM;
373         if (bus_pid_changed(bus))
374                 return -ECHILD;
375
376         bus->is_server = !!b;
377         bus->server_id = server_id;
378         return 0;
379 }
380
381 int sd_bus_set_anonymous(sd_bus *bus, int b) {
382         if (!bus)
383                 return -EINVAL;
384         if (bus->state != BUS_UNSET)
385                 return -EPERM;
386         if (bus_pid_changed(bus))
387                 return -ECHILD;
388
389         bus->anonymous_auth = !!b;
390         return 0;
391 }
392
393 static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata) {
394         const char *s;
395         int r;
396
397         assert(bus);
398         assert(bus->state == BUS_HELLO);
399         assert(reply);
400
401         r = bus_message_to_errno(reply);
402         if (r < 0)
403                 return r;
404
405         r = sd_bus_message_read(reply, "s", &s);
406         if (r < 0)
407                 return r;
408
409         if (!service_name_is_valid(s) || s[0] != ':')
410                 return -EBADMSG;
411
412         bus->unique_name = strdup(s);
413         if (!bus->unique_name)
414                 return -ENOMEM;
415
416         bus->state = BUS_RUNNING;
417
418         return 1;
419 }
420
421 static int bus_send_hello(sd_bus *bus) {
422         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
423         int r;
424
425         assert(bus);
426
427         if (!bus->bus_client || bus->is_kernel)
428                 return 0;
429
430         r = sd_bus_message_new_method_call(
431                         bus,
432                         "org.freedesktop.DBus",
433                         "/",
434                         "org.freedesktop.DBus",
435                         "Hello",
436                         &m);
437         if (r < 0)
438                 return r;
439
440         return sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
441 }
442
443 int bus_start_running(sd_bus *bus) {
444         assert(bus);
445
446         if (bus->bus_client && !bus->is_kernel) {
447                 bus->state = BUS_HELLO;
448                 return 1;
449         }
450
451         bus->state = BUS_RUNNING;
452         return 1;
453 }
454
455 static int parse_address_key(const char **p, const char *key, char **value) {
456         size_t l, n = 0;
457         const char *a;
458         char *r = NULL;
459
460         assert(p);
461         assert(*p);
462         assert(value);
463
464         if (key) {
465                 l = strlen(key);
466                 if (strncmp(*p, key, l) != 0)
467                         return 0;
468
469                 if ((*p)[l] != '=')
470                         return 0;
471
472                 if (*value)
473                         return -EINVAL;
474
475                 a = *p + l + 1;
476         } else
477                 a = *p;
478
479         while (*a != ';' && *a != ',' && *a != 0) {
480                 char c, *t;
481
482                 if (*a == '%') {
483                         int x, y;
484
485                         x = unhexchar(a[1]);
486                         if (x < 0) {
487                                 free(r);
488                                 return x;
489                         }
490
491                         y = unhexchar(a[2]);
492                         if (y < 0) {
493                                 free(r);
494                                 return y;
495                         }
496
497                         c = (char) ((x << 4) | y);
498                         a += 3;
499                 } else {
500                         c = *a;
501                         a++;
502                 }
503
504                 t = realloc(r, n + 2);
505                 if (!t) {
506                         free(r);
507                         return -ENOMEM;
508                 }
509
510                 r = t;
511                 r[n++] = c;
512         }
513
514         if (!r) {
515                 r = strdup("");
516                 if (!r)
517                         return -ENOMEM;
518         } else
519                 r[n] = 0;
520
521         if (*a == ',')
522                 a++;
523
524         *p = a;
525
526         free(*value);
527         *value = r;
528
529         return 1;
530 }
531
532 static void skip_address_key(const char **p) {
533         assert(p);
534         assert(*p);
535
536         *p += strcspn(*p, ",");
537
538         if (**p == ',')
539                 (*p) ++;
540 }
541
542 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
543         _cleanup_free_ char *path = NULL, *abstract = NULL;
544         size_t l;
545         int r;
546
547         assert(b);
548         assert(p);
549         assert(*p);
550         assert(guid);
551
552         while (**p != 0 && **p != ';') {
553                 r = parse_address_key(p, "guid", guid);
554                 if (r < 0)
555                         return r;
556                 else if (r > 0)
557                         continue;
558
559                 r = parse_address_key(p, "path", &path);
560                 if (r < 0)
561                         return r;
562                 else if (r > 0)
563                         continue;
564
565                 r = parse_address_key(p, "abstract", &abstract);
566                 if (r < 0)
567                         return r;
568                 else if (r > 0)
569                         continue;
570
571                 skip_address_key(p);
572         }
573
574         if (!path && !abstract)
575                 return -EINVAL;
576
577         if (path && abstract)
578                 return -EINVAL;
579
580         if (path) {
581                 l = strlen(path);
582                 if (l > sizeof(b->sockaddr.un.sun_path))
583                         return -E2BIG;
584
585                 b->sockaddr.un.sun_family = AF_UNIX;
586                 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
587                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
588         } else if (abstract) {
589                 l = strlen(abstract);
590                 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
591                         return -E2BIG;
592
593                 b->sockaddr.un.sun_family = AF_UNIX;
594                 b->sockaddr.un.sun_path[0] = 0;
595                 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
596                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
597         }
598
599         return 0;
600 }
601
602 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
603         _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
604         int r;
605         struct addrinfo *result, hints = {
606                 .ai_socktype = SOCK_STREAM,
607                 .ai_flags = AI_ADDRCONFIG,
608         };
609
610         assert(b);
611         assert(p);
612         assert(*p);
613         assert(guid);
614
615         while (**p != 0 && **p != ';') {
616                 r = parse_address_key(p, "guid", guid);
617                 if (r < 0)
618                         return r;
619                 else if (r > 0)
620                         continue;
621
622                 r = parse_address_key(p, "host", &host);
623                 if (r < 0)
624                         return r;
625                 else if (r > 0)
626                         continue;
627
628                 r = parse_address_key(p, "port", &port);
629                 if (r < 0)
630                         return r;
631                 else if (r > 0)
632                         continue;
633
634                 r = parse_address_key(p, "family", &family);
635                 if (r < 0)
636                         return r;
637                 else if (r > 0)
638                         continue;
639
640                 skip_address_key(p);
641         }
642
643         if (!host || !port)
644                 return -EINVAL;
645
646         if (family) {
647                 if (streq(family, "ipv4"))
648                         hints.ai_family = AF_INET;
649                 else if (streq(family, "ipv6"))
650                         hints.ai_family = AF_INET6;
651                 else
652                         return -EINVAL;
653         }
654
655         r = getaddrinfo(host, port, &hints, &result);
656         if (r == EAI_SYSTEM)
657                 return -errno;
658         else if (r != 0)
659                 return -EADDRNOTAVAIL;
660
661         memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
662         b->sockaddr_size = result->ai_addrlen;
663
664         freeaddrinfo(result);
665
666         return 0;
667 }
668
669 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
670         char *path = NULL;
671         unsigned n_argv = 0, j;
672         char **argv = NULL;
673         int r;
674
675         assert(b);
676         assert(p);
677         assert(*p);
678         assert(guid);
679
680         while (**p != 0 && **p != ';') {
681                 r = parse_address_key(p, "guid", guid);
682                 if (r < 0)
683                         goto fail;
684                 else if (r > 0)
685                         continue;
686
687                 r = parse_address_key(p, "path", &path);
688                 if (r < 0)
689                         goto fail;
690                 else if (r > 0)
691                         continue;
692
693                 if (startswith(*p, "argv")) {
694                         unsigned ul;
695
696                         errno = 0;
697                         ul = strtoul(*p + 4, (char**) p, 10);
698                         if (errno > 0 || **p != '=' || ul > 256) {
699                                 r = -EINVAL;
700                                 goto fail;
701                         }
702
703                         (*p) ++;
704
705                         if (ul >= n_argv) {
706                                 char **x;
707
708                                 x = realloc(argv, sizeof(char*) * (ul + 2));
709                                 if (!x) {
710                                         r = -ENOMEM;
711                                         goto fail;
712                                 }
713
714                                 memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
715
716                                 argv = x;
717                                 n_argv = ul + 1;
718                         }
719
720                         r = parse_address_key(p, NULL, argv + ul);
721                         if (r < 0)
722                                 goto fail;
723
724                         continue;
725                 }
726
727                 skip_address_key(p);
728         }
729
730         if (!path) {
731                 r = -EINVAL;
732                 goto fail;
733         }
734
735         /* Make sure there are no holes in the array, with the
736          * exception of argv[0] */
737         for (j = 1; j < n_argv; j++)
738                 if (!argv[j]) {
739                         r = -EINVAL;
740                         goto fail;
741                 }
742
743         if (argv && argv[0] == NULL) {
744                 argv[0] = strdup(path);
745                 if (!argv[0]) {
746                         r = -ENOMEM;
747                         goto fail;
748                 }
749         }
750
751         b->exec_path = path;
752         b->exec_argv = argv;
753         return 0;
754
755 fail:
756         for (j = 0; j < n_argv; j++)
757                 free(argv[j]);
758
759         free(argv);
760         free(path);
761         return r;
762 }
763
764 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
765         _cleanup_free_ char *path = NULL;
766         int r;
767
768         assert(b);
769         assert(p);
770         assert(*p);
771         assert(guid);
772
773         while (**p != 0 && **p != ';') {
774                 r = parse_address_key(p, "guid", guid);
775                 if (r < 0)
776                         return r;
777                 else if (r > 0)
778                         continue;
779
780                 r = parse_address_key(p, "path", &path);
781                 if (r < 0)
782                         return r;
783                 else if (r > 0)
784                         continue;
785
786                 skip_address_key(p);
787         }
788
789         if (!path)
790                 return -EINVAL;
791
792         free(b->kernel);
793         b->kernel = path;
794         path = NULL;
795
796         return 0;
797 }
798
799 static void bus_reset_parsed_address(sd_bus *b) {
800         assert(b);
801
802         zero(b->sockaddr);
803         b->sockaddr_size = 0;
804         strv_free(b->exec_argv);
805         free(b->exec_path);
806         b->exec_path = NULL;
807         b->exec_argv = NULL;
808         b->server_id = SD_ID128_NULL;
809         free(b->kernel);
810         b->kernel = NULL;
811 }
812
813 static int bus_parse_next_address(sd_bus *b) {
814         _cleanup_free_ char *guid = NULL;
815         const char *a;
816         int r;
817
818         assert(b);
819
820         if (!b->address)
821                 return 0;
822         if (b->address[b->address_index] == 0)
823                 return 0;
824
825         bus_reset_parsed_address(b);
826
827         a = b->address + b->address_index;
828
829         while (*a != 0) {
830
831                 if (*a == ';') {
832                         a++;
833                         continue;
834                 }
835
836                 if (startswith(a, "unix:")) {
837                         a += 5;
838
839                         r = parse_unix_address(b, &a, &guid);
840                         if (r < 0)
841                                 return r;
842                         break;
843
844                 } else if (startswith(a, "tcp:")) {
845
846                         a += 4;
847                         r = parse_tcp_address(b, &a, &guid);
848                         if (r < 0)
849                                 return r;
850
851                         break;
852
853                 } else if (startswith(a, "unixexec:")) {
854
855                         a += 9;
856                         r = parse_exec_address(b, &a, &guid);
857                         if (r < 0)
858                                 return r;
859
860                         break;
861
862                 } else if (startswith(a, "kernel:")) {
863
864                         a += 7;
865                         r = parse_kernel_address(b, &a, &guid);
866                         if (r < 0)
867                                 return r;
868
869                         break;
870                 }
871
872                 a = strchr(a, ';');
873                 if (!a)
874                         return 0;
875         }
876
877         if (guid) {
878                 r = sd_id128_from_string(guid, &b->server_id);
879                 if (r < 0)
880                         return r;
881         }
882
883         b->address_index = a - b->address;
884         return 1;
885 }
886
887 static int bus_start_address(sd_bus *b) {
888         int r;
889
890         assert(b);
891
892         for (;;) {
893                 sd_bus_close(b);
894
895                 if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
896
897                         r = bus_socket_connect(b);
898                         if (r >= 0)
899                                 return r;
900
901                         b->last_connect_error = -r;
902
903                 } else if (b->exec_path) {
904
905                         r = bus_socket_exec(b);
906                         if (r >= 0)
907                                 return r;
908
909                         b->last_connect_error = -r;
910                 } else if (b->kernel) {
911
912                         r = bus_kernel_connect(b);
913                         if (r >= 0)
914                                 return r;
915
916                         b->last_connect_error = -r;
917                 }
918
919                 r = bus_parse_next_address(b);
920                 if (r < 0)
921                         return r;
922                 if (r == 0)
923                         return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
924         }
925 }
926
927 int bus_next_address(sd_bus *b) {
928         assert(b);
929
930         bus_reset_parsed_address(b);
931         return bus_start_address(b);
932 }
933
934 static int bus_start_fd(sd_bus *b) {
935         struct stat st;
936         int r;
937
938         assert(b);
939         assert(b->input_fd >= 0);
940         assert(b->output_fd >= 0);
941
942         r = fd_nonblock(b->input_fd, true);
943         if (r < 0)
944                 return r;
945
946         r = fd_cloexec(b->input_fd, true);
947         if (r < 0)
948                 return r;
949
950         if (b->input_fd != b->output_fd) {
951                 r = fd_nonblock(b->output_fd, true);
952                 if (r < 0)
953                         return r;
954
955                 r = fd_cloexec(b->output_fd, true);
956                 if (r < 0)
957                         return r;
958         }
959
960         if (fstat(b->input_fd, &st) < 0)
961                 return -errno;
962
963         if (S_ISCHR(b->input_fd))
964                 return bus_kernel_take_fd(b);
965         else
966                 return bus_socket_take_fd(b);
967 }
968
969 int sd_bus_start(sd_bus *bus) {
970         int r;
971
972         if (!bus)
973                 return -EINVAL;
974         if (bus->state != BUS_UNSET)
975                 return -EPERM;
976         if (bus_pid_changed(bus))
977                 return -ECHILD;
978
979         bus->state = BUS_OPENING;
980
981         if (bus->is_server && bus->bus_client)
982                 return -EINVAL;
983
984         if (bus->input_fd >= 0)
985                 r = bus_start_fd(bus);
986         else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel)
987                 r = bus_start_address(bus);
988         else
989                 return -EINVAL;
990
991         if (r < 0)
992                 return r;
993
994         return bus_send_hello(bus);
995 }
996
997 int sd_bus_open_system(sd_bus **ret) {
998         const char *e;
999         sd_bus *b;
1000         int r;
1001
1002         if (!ret)
1003                 return -EINVAL;
1004
1005         r = sd_bus_new(&b);
1006         if (r < 0)
1007                 return r;
1008
1009         e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1010         if (e) {
1011                 r = sd_bus_set_address(b, e);
1012                 if (r < 0)
1013                         goto fail;
1014         } else {
1015                 b->sockaddr.un.sun_family = AF_UNIX;
1016                 strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
1017                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
1018         }
1019
1020         b->bus_client = true;
1021
1022         r = sd_bus_start(b);
1023         if (r < 0)
1024                 goto fail;
1025
1026         *ret = b;
1027         return 0;
1028
1029 fail:
1030         bus_free(b);
1031         return r;
1032 }
1033
1034 int sd_bus_open_user(sd_bus **ret) {
1035         const char *e;
1036         sd_bus *b;
1037         size_t l;
1038         int r;
1039
1040         if (!ret)
1041                 return -EINVAL;
1042
1043         r = sd_bus_new(&b);
1044         if (r < 0)
1045                 return r;
1046
1047         e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1048         if (e) {
1049                 r = sd_bus_set_address(b, e);
1050                 if (r < 0)
1051                         goto fail;
1052         } else {
1053                 e = secure_getenv("XDG_RUNTIME_DIR");
1054                 if (!e) {
1055                         r = -ENOENT;
1056                         goto fail;
1057                 }
1058
1059                 l = strlen(e);
1060                 if (l + 4 > sizeof(b->sockaddr.un.sun_path)) {
1061                         r = -E2BIG;
1062                         goto fail;
1063                 }
1064
1065                 b->sockaddr.un.sun_family = AF_UNIX;
1066                 memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
1067                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
1068         }
1069
1070         b->bus_client = true;
1071
1072         r = sd_bus_start(b);
1073         if (r < 0)
1074                 goto fail;
1075
1076         *ret = b;
1077         return 0;
1078
1079 fail:
1080         bus_free(b);
1081         return r;
1082 }
1083
1084 void sd_bus_close(sd_bus *bus) {
1085         if (!bus)
1086                 return;
1087         if (bus->state == BUS_CLOSED)
1088                 return;
1089         if (bus_pid_changed(bus))
1090                 return;
1091
1092         bus->state = BUS_CLOSED;
1093
1094         if (!bus->is_kernel)
1095                 bus_close_fds(bus);
1096
1097         /* We'll leave the fd open in case this is a kernel bus, since
1098          * there might still be memblocks around that reference this
1099          * bus, and they might need to invoke the
1100          * KDBUS_CMD_MSG_RELEASE ioctl on the fd when they are
1101          * freed. */
1102 }
1103
1104 sd_bus *sd_bus_ref(sd_bus *bus) {
1105         if (!bus)
1106                 return NULL;
1107
1108         assert_se(REFCNT_INC(bus->n_ref) >= 2);
1109
1110         return bus;
1111 }
1112
1113 sd_bus *sd_bus_unref(sd_bus *bus) {
1114         if (!bus)
1115                 return NULL;
1116
1117         if (REFCNT_DEC(bus->n_ref) <= 0)
1118                 bus_free(bus);
1119
1120         return NULL;
1121 }
1122
1123 int sd_bus_is_open(sd_bus *bus) {
1124         if (!bus)
1125                 return -EINVAL;
1126         if (bus_pid_changed(bus))
1127                 return -ECHILD;
1128
1129         return BUS_IS_OPEN(bus->state);
1130 }
1131
1132 int sd_bus_can_send(sd_bus *bus, char type) {
1133         int r;
1134
1135         if (!bus)
1136                 return -EINVAL;
1137         if (bus->state == BUS_UNSET)
1138                 return -ENOTCONN;
1139         if (bus_pid_changed(bus))
1140                 return -ECHILD;
1141
1142         if (type == SD_BUS_TYPE_UNIX_FD) {
1143                 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1144                         return 0;
1145
1146                 r = bus_ensure_running(bus);
1147                 if (r < 0)
1148                         return r;
1149
1150                 return bus->can_fds;
1151         }
1152
1153         return bus_type_is_valid(type);
1154 }
1155
1156 int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1157         int r;
1158
1159         if (!bus)
1160                 return -EINVAL;
1161         if (!server_id)
1162                 return -EINVAL;
1163         if (bus_pid_changed(bus))
1164                 return -ECHILD;
1165
1166         r = bus_ensure_running(bus);
1167         if (r < 0)
1168                 return r;
1169
1170         *server_id = bus->server_id;
1171         return 0;
1172 }
1173
1174 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
1175         assert(m);
1176
1177         if (m->header->version > b->message_version)
1178                 return -EPERM;
1179
1180         if (m->sealed)
1181                 return 0;
1182
1183         return bus_message_seal(m, ++b->serial);
1184 }
1185
1186 static int dispatch_wqueue(sd_bus *bus) {
1187         int r, ret = 0;
1188
1189         assert(bus);
1190         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1191
1192         while (bus->wqueue_size > 0) {
1193
1194                 if (bus->is_kernel)
1195                         r = bus_kernel_write_message(bus, bus->wqueue[0]);
1196                 else
1197                         r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
1198
1199                 if (r < 0) {
1200                         sd_bus_close(bus);
1201                         return r;
1202                 } else if (r == 0)
1203                         /* Didn't do anything this time */
1204                         return ret;
1205                 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1206                         /* Fully written. Let's drop the entry from
1207                          * the queue.
1208                          *
1209                          * This isn't particularly optimized, but
1210                          * well, this is supposed to be our worst-case
1211                          * buffer only, and the socket buffer is
1212                          * supposed to be our primary buffer, and if
1213                          * it got full, then all bets are off
1214                          * anyway. */
1215
1216                         sd_bus_message_unref(bus->wqueue[0]);
1217                         bus->wqueue_size --;
1218                         memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1219                         bus->windex = 0;
1220
1221                         ret = 1;
1222                 }
1223         }
1224
1225         return ret;
1226 }
1227
1228 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1229         sd_bus_message *z = NULL;
1230         int r, ret = 0;
1231
1232         assert(bus);
1233         assert(m);
1234         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1235
1236         if (bus->rqueue_size > 0) {
1237                 /* Dispatch a queued message */
1238
1239                 *m = bus->rqueue[0];
1240                 bus->rqueue_size --;
1241                 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1242                 return 1;
1243         }
1244
1245         /* Try to read a new message */
1246         do {
1247                 if (bus->is_kernel)
1248                         r = bus_kernel_read_message(bus, &z);
1249                 else
1250                         r = bus_socket_read_message(bus, &z);
1251
1252                 if (r < 0) {
1253                         sd_bus_close(bus);
1254                         return r;
1255                 }
1256                 if (r == 0)
1257                         return ret;
1258
1259                 ret = 1;
1260         } while (!z);
1261
1262         *m = z;
1263         return ret;
1264 }
1265
1266 int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1267         int r;
1268
1269         if (!bus)
1270                 return -EINVAL;
1271         if (!BUS_IS_OPEN(bus->state))
1272                 return -ENOTCONN;
1273         if (!m)
1274                 return -EINVAL;
1275         if (bus_pid_changed(bus))
1276                 return -ECHILD;
1277
1278         if (m->n_fds > 0) {
1279                 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1280                 if (r < 0)
1281                         return r;
1282                 if (r == 0)
1283                         return -ENOTSUP;
1284         }
1285
1286         /* If the serial number isn't kept, then we know that no reply
1287          * is expected */
1288         if (!serial && !m->sealed)
1289                 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1290
1291         r = bus_seal_message(bus, m);
1292         if (r < 0)
1293                 return r;
1294
1295         /* If this is a reply and no reply was requested, then let's
1296          * suppress this, if we can */
1297         if (m->dont_send && !serial)
1298                 return 0;
1299
1300         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1301                 size_t idx = 0;
1302
1303                 if (bus->is_kernel)
1304                         r = bus_kernel_write_message(bus, m);
1305                 else
1306                         r = bus_socket_write_message(bus, m, &idx);
1307
1308                 if (r < 0) {
1309                         sd_bus_close(bus);
1310                         return r;
1311                 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m))  {
1312                         /* Wasn't fully written. So let's remember how
1313                          * much was written. Note that the first entry
1314                          * of the wqueue array is always allocated so
1315                          * that we always can remember how much was
1316                          * written. */
1317                         bus->wqueue[0] = sd_bus_message_ref(m);
1318                         bus->wqueue_size = 1;
1319                         bus->windex = idx;
1320                 }
1321         } else {
1322                 sd_bus_message **q;
1323
1324                 /* Just append it to the queue. */
1325
1326                 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1327                         return -ENOBUFS;
1328
1329                 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1330                 if (!q)
1331                         return -ENOMEM;
1332
1333                 bus->wqueue = q;
1334                 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1335         }
1336
1337         if (serial)
1338                 *serial = BUS_MESSAGE_SERIAL(m);
1339
1340         return 0;
1341 }
1342
1343 static usec_t calc_elapse(uint64_t usec) {
1344         if (usec == (uint64_t) -1)
1345                 return 0;
1346
1347         if (usec == 0)
1348                 usec = BUS_DEFAULT_TIMEOUT;
1349
1350         return now(CLOCK_MONOTONIC) + usec;
1351 }
1352
1353 static int timeout_compare(const void *a, const void *b) {
1354         const struct reply_callback *x = a, *y = b;
1355
1356         if (x->timeout != 0 && y->timeout == 0)
1357                 return -1;
1358
1359         if (x->timeout == 0 && y->timeout != 0)
1360                 return 1;
1361
1362         if (x->timeout < y->timeout)
1363                 return -1;
1364
1365         if (x->timeout > y->timeout)
1366                 return 1;
1367
1368         return 0;
1369 }
1370
1371 int sd_bus_send_with_reply(
1372                 sd_bus *bus,
1373                 sd_bus_message *m,
1374                 sd_bus_message_handler_t callback,
1375                 void *userdata,
1376                 uint64_t usec,
1377                 uint64_t *serial) {
1378
1379         struct reply_callback *c;
1380         int r;
1381
1382         if (!bus)
1383                 return -EINVAL;
1384         if (!BUS_IS_OPEN(bus->state))
1385                 return -ENOTCONN;
1386         if (!m)
1387                 return -EINVAL;
1388         if (!callback)
1389                 return -EINVAL;
1390         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1391                 return -EINVAL;
1392         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1393                 return -EINVAL;
1394         if (bus_pid_changed(bus))
1395                 return -ECHILD;
1396
1397         r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1398         if (r < 0)
1399                 return r;
1400
1401         if (usec != (uint64_t) -1) {
1402                 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1403                 if (r < 0)
1404                         return r;
1405         }
1406
1407         r = bus_seal_message(bus, m);
1408         if (r < 0)
1409                 return r;
1410
1411         c = new0(struct reply_callback, 1);
1412         if (!c)
1413                 return -ENOMEM;
1414
1415         c->callback = callback;
1416         c->userdata = userdata;
1417         c->serial = BUS_MESSAGE_SERIAL(m);
1418         c->timeout = calc_elapse(usec);
1419
1420         r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1421         if (r < 0) {
1422                 free(c);
1423                 return r;
1424         }
1425
1426         if (c->timeout != 0) {
1427                 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1428                 if (r < 0) {
1429                         c->timeout = 0;
1430                         sd_bus_send_with_reply_cancel(bus, c->serial);
1431                         return r;
1432                 }
1433         }
1434
1435         r = sd_bus_send(bus, m, serial);
1436         if (r < 0) {
1437                 sd_bus_send_with_reply_cancel(bus, c->serial);
1438                 return r;
1439         }
1440
1441         return r;
1442 }
1443
1444 int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
1445         struct reply_callback *c;
1446
1447         if (!bus)
1448                 return -EINVAL;
1449         if (serial == 0)
1450                 return -EINVAL;
1451         if (bus_pid_changed(bus))
1452                 return -ECHILD;
1453
1454         c = hashmap_remove(bus->reply_callbacks, &serial);
1455         if (!c)
1456                 return 0;
1457
1458         if (c->timeout != 0)
1459                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1460
1461         free(c);
1462         return 1;
1463 }
1464
1465 int bus_ensure_running(sd_bus *bus) {
1466         int r;
1467
1468         assert(bus);
1469
1470         if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED)
1471                 return -ENOTCONN;
1472         if (bus->state == BUS_RUNNING)
1473                 return 1;
1474
1475         for (;;) {
1476                 r = sd_bus_process(bus, NULL);
1477                 if (r < 0)
1478                         return r;
1479                 if (bus->state == BUS_RUNNING)
1480                         return 1;
1481                 if (r > 0)
1482                         continue;
1483
1484                 r = sd_bus_wait(bus, (uint64_t) -1);
1485                 if (r < 0)
1486                         return r;
1487         }
1488 }
1489
1490 int sd_bus_send_with_reply_and_block(
1491                 sd_bus *bus,
1492                 sd_bus_message *m,
1493                 uint64_t usec,
1494                 sd_bus_error *error,
1495                 sd_bus_message **reply) {
1496
1497         int r;
1498         usec_t timeout;
1499         uint64_t serial;
1500         bool room = false;
1501
1502         if (!bus)
1503                 return -EINVAL;
1504         if (!BUS_IS_OPEN(bus->state))
1505                 return -ENOTCONN;
1506         if (!m)
1507                 return -EINVAL;
1508         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1509                 return -EINVAL;
1510         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1511                 return -EINVAL;
1512         if (bus_error_is_dirty(error))
1513                 return -EINVAL;
1514         if (bus_pid_changed(bus))
1515                 return -ECHILD;
1516
1517         r = bus_ensure_running(bus);
1518         if (r < 0)
1519                 return r;
1520
1521         r = sd_bus_send(bus, m, &serial);
1522         if (r < 0)
1523                 return r;
1524
1525         timeout = calc_elapse(usec);
1526
1527         for (;;) {
1528                 usec_t left;
1529                 sd_bus_message *incoming = NULL;
1530
1531                 if (!room) {
1532                         sd_bus_message **q;
1533
1534                         if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1535                                 return -ENOBUFS;
1536
1537                         /* Make sure there's room for queuing this
1538                          * locally, before we read the message */
1539
1540                         q = realloc(bus->rqueue, (bus->rqueue_size + 1) * sizeof(sd_bus_message*));
1541                         if (!q)
1542                                 return -ENOMEM;
1543
1544                         bus->rqueue = q;
1545                         room = true;
1546                 }
1547
1548                 if (bus->is_kernel)
1549                         r = bus_kernel_read_message(bus, &incoming);
1550                 else
1551                         r = bus_socket_read_message(bus, &incoming);
1552                 if (r < 0)
1553                         return r;
1554                 if (incoming) {
1555
1556                         if (incoming->reply_serial == serial) {
1557                                 /* Found a match! */
1558
1559                                 if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
1560
1561                                         if (reply)
1562                                                 *reply = incoming;
1563                                         else
1564                                                 sd_bus_message_unref(incoming);
1565
1566                                         return 0;
1567                                 }
1568
1569                                 if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
1570                                         int k;
1571
1572                                         r = sd_bus_error_copy(error, &incoming->error);
1573                                         if (r < 0) {
1574                                                 sd_bus_message_unref(incoming);
1575                                                 return r;
1576                                         }
1577
1578                                         k = bus_error_to_errno(&incoming->error);
1579                                         sd_bus_message_unref(incoming);
1580                                         return k;
1581                                 }
1582
1583                                 sd_bus_message_unref(incoming);
1584                                 return -EIO;
1585                         }
1586
1587                         /* There's already guaranteed to be room for
1588                          * this, so need to resize things here */
1589                         bus->rqueue[bus->rqueue_size ++] = incoming;
1590                         room = false;
1591
1592                         /* Try to read more, right-away */
1593                         continue;
1594                 }
1595                 if (r != 0)
1596                         continue;
1597
1598                 if (timeout > 0) {
1599                         usec_t n;
1600
1601                         n = now(CLOCK_MONOTONIC);
1602                         if (n >= timeout)
1603                                 return -ETIMEDOUT;
1604
1605                         left = timeout - n;
1606                 } else
1607                         left = (uint64_t) -1;
1608
1609                 r = bus_poll(bus, true, left);
1610                 if (r < 0)
1611                         return r;
1612
1613                 r = dispatch_wqueue(bus);
1614                 if (r < 0)
1615                         return r;
1616         }
1617 }
1618
1619 int sd_bus_get_fd(sd_bus *bus) {
1620         if (!bus)
1621                 return -EINVAL;
1622         if (!BUS_IS_OPEN(bus->state))
1623                 return -ENOTCONN;
1624         if (bus->input_fd != bus->output_fd)
1625                 return -EPERM;
1626         if (bus_pid_changed(bus))
1627                 return -ECHILD;
1628
1629         return bus->input_fd;
1630 }
1631
1632 int sd_bus_get_events(sd_bus *bus) {
1633         int flags = 0;
1634
1635         if (!bus)
1636                 return -EINVAL;
1637         if (!BUS_IS_OPEN(bus->state))
1638                 return -ENOTCONN;
1639         if (bus_pid_changed(bus))
1640                 return -ECHILD;
1641
1642         if (bus->state == BUS_OPENING)
1643                 flags |= POLLOUT;
1644         else if (bus->state == BUS_AUTHENTICATING) {
1645
1646                 if (bus_socket_auth_needs_write(bus))
1647                         flags |= POLLOUT;
1648
1649                 flags |= POLLIN;
1650
1651         } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1652                 if (bus->rqueue_size <= 0)
1653                         flags |= POLLIN;
1654                 if (bus->wqueue_size > 0)
1655                         flags |= POLLOUT;
1656         }
1657
1658         return flags;
1659 }
1660
1661 int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1662         struct reply_callback *c;
1663
1664         if (!bus)
1665                 return -EINVAL;
1666         if (!timeout_usec)
1667                 return -EINVAL;
1668         if (!BUS_IS_OPEN(bus->state))
1669                 return -ENOTCONN;
1670         if (bus_pid_changed(bus))
1671                 return -ECHILD;
1672
1673         if (bus->state == BUS_AUTHENTICATING) {
1674                 *timeout_usec = bus->auth_timeout;
1675                 return 1;
1676         }
1677
1678         if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1679                 *timeout_usec = (uint64_t) -1;
1680                 return 0;
1681         }
1682
1683         c = prioq_peek(bus->reply_callbacks_prioq);
1684         if (!c) {
1685                 *timeout_usec = (uint64_t) -1;
1686                 return 0;
1687         }
1688
1689         *timeout_usec = c->timeout;
1690         return 1;
1691 }
1692
1693 static int process_timeout(sd_bus *bus) {
1694         _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1695         struct reply_callback *c;
1696         usec_t n;
1697         int r;
1698
1699         assert(bus);
1700
1701         c = prioq_peek(bus->reply_callbacks_prioq);
1702         if (!c)
1703                 return 0;
1704
1705         n = now(CLOCK_MONOTONIC);
1706         if (c->timeout > n)
1707                 return 0;
1708
1709         r = bus_message_new_synthetic_error(
1710                         bus,
1711                         c->serial,
1712                         &SD_BUS_ERROR_MAKE("org.freedesktop.DBus.Error.Timeout", "Timed out"),
1713                         &m);
1714         if (r < 0)
1715                 return r;
1716
1717         assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1718         hashmap_remove(bus->reply_callbacks, &c->serial);
1719
1720         r = c->callback(bus, m, c->userdata);
1721         free(c);
1722
1723         return r < 0 ? r : 1;
1724 }
1725
1726 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1727         assert(bus);
1728         assert(m);
1729
1730         if (bus->state != BUS_HELLO)
1731                 return 0;
1732
1733         /* Let's make sure the first message on the bus is the HELLO
1734          * reply. But note that we don't actually parse the message
1735          * here (we leave that to the usual handling), we just verify
1736          * we don't let any earlier msg through. */
1737
1738         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
1739             m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
1740                 return -EIO;
1741
1742         if (m->reply_serial != bus->hello_serial)
1743                 return -EIO;
1744
1745         return 0;
1746 }
1747
1748 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1749         struct reply_callback *c;
1750         int r;
1751
1752         assert(bus);
1753         assert(m);
1754
1755         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
1756             m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
1757                 return 0;
1758
1759         c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1760         if (!c)
1761                 return 0;
1762
1763         if (c->timeout != 0)
1764                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1765
1766         r = sd_bus_message_rewind(m, true);
1767         if (r < 0)
1768                 return r;
1769
1770         r = c->callback(bus, m, c->userdata);
1771         free(c);
1772
1773         return r;
1774 }
1775
1776 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1777         struct filter_callback *l;
1778         int r;
1779
1780         assert(bus);
1781         assert(m);
1782
1783         do {
1784                 bus->filter_callbacks_modified = false;
1785
1786                 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1787
1788                         if (bus->filter_callbacks_modified)
1789                                 break;
1790
1791                         /* Don't run this more than once per iteration */
1792                         if (l->last_iteration == bus->iteration_counter)
1793                                 continue;
1794
1795                         l->last_iteration = bus->iteration_counter;
1796
1797                         r = sd_bus_message_rewind(m, true);
1798                         if (r < 0)
1799                                 return r;
1800
1801                         r = l->callback(bus, m, l->userdata);
1802                         if (r != 0)
1803                                 return r;
1804
1805                 }
1806
1807         } while (bus->filter_callbacks_modified);
1808
1809         return 0;
1810 }
1811
1812 static int process_match(sd_bus *bus, sd_bus_message *m) {
1813         int r;
1814
1815         assert(bus);
1816         assert(m);
1817
1818         do {
1819                 bus->match_callbacks_modified = false;
1820
1821                 r = bus_match_run(bus, &bus->match_callbacks, m);
1822                 if (r != 0)
1823                         return r;
1824
1825         } while (bus->match_callbacks_modified);
1826
1827         return 0;
1828 }
1829
1830 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1831         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1832         int r;
1833
1834         assert(bus);
1835         assert(m);
1836
1837         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1838                 return 0;
1839
1840         if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1841                 return 0;
1842
1843         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1844                 return 1;
1845
1846         if (streq_ptr(m->member, "Ping"))
1847                 r = sd_bus_message_new_method_return(bus, m, &reply);
1848         else if (streq_ptr(m->member, "GetMachineId")) {
1849                 sd_id128_t id;
1850                 char sid[33];
1851
1852                 r = sd_id128_get_machine(&id);
1853                 if (r < 0)
1854                         return r;
1855
1856                 r = sd_bus_message_new_method_return(bus, m, &reply);
1857                 if (r < 0)
1858                         return r;
1859
1860                 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
1861         } else {
1862                 r = sd_bus_message_new_method_errorf(
1863                                 bus, m, &reply,
1864                                 "org.freedesktop.DBus.Error.UnknownMethod",
1865                                  "Unknown method '%s' on interface '%s'.", m->member, m->interface);
1866         }
1867
1868         if (r < 0)
1869                 return r;
1870
1871         r = sd_bus_send(bus, reply, NULL);
1872         if (r < 0)
1873                 return r;
1874
1875         return 1;
1876 }
1877
1878 static int node_vtable_get_userdata(
1879                 sd_bus *bus,
1880                 const char *path,
1881                 struct node_vtable *c,
1882                 void **userdata) {
1883
1884         void *u;
1885         int r;
1886
1887         assert(bus);
1888         assert(path);
1889         assert(c);
1890
1891         u = c->userdata;
1892         if (c->find) {
1893                 r = c->find(bus, path, c->interface, &u, u);
1894                 if (r <= 0)
1895                         return r;
1896         }
1897
1898         if (userdata)
1899                 *userdata = u;
1900
1901         return 1;
1902 }
1903
1904 static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
1905         assert(p);
1906
1907         return (uint8_t*) u + p->property.offset;
1908 }
1909
1910 static int vtable_property_get_userdata(
1911                 sd_bus *bus,
1912                 const char *path,
1913                 struct vtable_member *p,
1914                 void **userdata) {
1915
1916         void *u;
1917         int r;
1918
1919         assert(bus);
1920         assert(path);
1921         assert(p);
1922         assert(userdata);
1923
1924         r = node_vtable_get_userdata(bus, path, p->parent, &u);
1925         if (r <= 0)
1926                 return r;
1927
1928         *userdata = vtable_property_convert_userdata(p->vtable, u);
1929         return 1;
1930 }
1931
1932 static int add_enumerated_to_set(sd_bus *bus, const char *prefix, struct node_enumerator *first, Set *s) {
1933         struct node_enumerator *c;
1934         int r;
1935
1936         assert(bus);
1937         assert(prefix);
1938         assert(s);
1939
1940         LIST_FOREACH(enumerators, c, first) {
1941                 char **children = NULL, **k;
1942
1943                 r = c->callback(bus, prefix, &children, c->userdata);
1944                 if (r < 0)
1945                         return r;
1946
1947                 STRV_FOREACH(k, children) {
1948                         if (r < 0) {
1949                                 free(*k);
1950                                 continue;
1951                         }
1952
1953                         if (!object_path_is_valid(*k) && object_path_startswith(*k, prefix)) {
1954                                 free(*k);
1955                                 r = -EINVAL;
1956                                 continue;
1957                         }
1958
1959                         r = set_consume(s, *k);
1960                 }
1961
1962                 free(children);
1963                 if (r < 0)
1964                         return r;
1965         }
1966
1967         return 0;
1968 }
1969
1970 static int add_subtree_to_set(sd_bus *bus, const char *prefix, struct node *n, Set *s) {
1971         struct node *i;
1972         int r;
1973
1974         assert(bus);
1975         assert(prefix);
1976         assert(n);
1977         assert(s);
1978
1979         r = add_enumerated_to_set(bus, prefix, n->enumerators, s);
1980         if (r < 0)
1981                 return r;
1982
1983         LIST_FOREACH(siblings, i, n->child) {
1984                 char *t;
1985
1986                 t = strdup(i->path);
1987                 if (!t)
1988                         return -ENOMEM;
1989
1990                 r = set_consume(s, t);
1991                 if (r < 0 && r != -EEXIST)
1992                         return r;
1993
1994                 r = add_subtree_to_set(bus, prefix, i, s);
1995                 if (r < 0)
1996                         return r;
1997         }
1998
1999         return 0;
2000 }
2001
2002 static int get_child_nodes(sd_bus *bus, const char *prefix, struct node *n, Set **_s) {
2003         Set *s = NULL;
2004         int r;
2005
2006         assert(bus);
2007         assert(n);
2008         assert(_s);
2009
2010         s = set_new(string_hash_func, string_compare_func);
2011         if (!s)
2012                 return -ENOMEM;
2013
2014         r = add_subtree_to_set(bus, prefix, n, s);
2015         if (r < 0) {
2016                 set_free_free(s);
2017                 return r;
2018         }
2019
2020         *_s = s;
2021         return 0;
2022 }
2023
2024 static int node_callbacks_run(
2025                 sd_bus *bus,
2026                 sd_bus_message *m,
2027                 struct node_callback *first,
2028                 bool require_fallback,
2029                 bool *found_object) {
2030
2031         struct node_callback *c;
2032         int r;
2033
2034         assert(bus);
2035         assert(m);
2036         assert(found_object);
2037
2038         LIST_FOREACH(callbacks, c, first) {
2039                 if (require_fallback && !c->is_fallback)
2040                         continue;
2041
2042                 *found_object = true;
2043
2044                 if (c->last_iteration == bus->iteration_counter)
2045                         continue;
2046
2047                 r = sd_bus_message_rewind(m, true);
2048                 if (r < 0)
2049                         return r;
2050
2051                 r = c->callback(bus, m, c->userdata);
2052                 if (r != 0)
2053                         return r;
2054         }
2055
2056         return 0;
2057 }
2058
2059 static int method_callbacks_run(
2060                 sd_bus *bus,
2061                 sd_bus_message *m,
2062                 struct vtable_member *c,
2063                 bool require_fallback,
2064                 bool *found_object) {
2065
2066         const char *signature;
2067         void *u;
2068         int r;
2069
2070         assert(bus);
2071         assert(m);
2072         assert(c);
2073         assert(found_object);
2074
2075         if (require_fallback && !c->parent->is_fallback)
2076                 return 0;
2077
2078         r = node_vtable_get_userdata(bus, m->path, c->parent, &u);
2079         if (r <= 0)
2080                 return r;
2081
2082         *found_object = true;
2083
2084         r = sd_bus_message_rewind(m, true);
2085         if (r < 0)
2086                 return r;
2087
2088         r = sd_bus_message_get_signature(m, true, &signature);
2089         if (r < 0)
2090                 return r;
2091
2092         if (!streq(c->vtable->method.signature, signature)) {
2093                 r = sd_bus_reply_method_errorf(bus, m,
2094                                                "org.freedesktop.DBus.Error.InvalidArgs",
2095                                                "Invalid arguments '%s' to call %s:%s, expecting '%s'.",
2096                                                signature, c->interface, c->member, c->vtable->method.signature);
2097                 if (r < 0)
2098                         return r;
2099
2100                 return 1;
2101         }
2102
2103         return c->vtable->method.handler(bus, m, u);
2104 }
2105
2106 static int invoke_property_get(
2107                 sd_bus *bus,
2108                 const sd_bus_vtable *v,
2109                 const char *path,
2110                 const char *interface,
2111                 const char *property,
2112                 sd_bus_message *m,
2113                 sd_bus_error *error,
2114                 void *userdata) {
2115
2116         int r;
2117         void *p;
2118
2119         assert(bus);
2120         assert(v);
2121
2122         if (v->property.get)
2123                 return v->property.get(bus, path, interface, property, m, error, userdata);
2124
2125         /* Automatic handling if no callback is defined. */
2126
2127         switch (v->property.signature[0]) {
2128
2129         case SD_BUS_TYPE_STRING:
2130         case SD_BUS_TYPE_OBJECT_PATH:
2131         case SD_BUS_TYPE_SIGNATURE:
2132                 p = *(char**) userdata;
2133                 break;
2134
2135         default:
2136                 p = userdata;
2137         }
2138
2139         r = sd_bus_message_append_basic(m, v->property.signature[0], p);
2140         if (r < 0)
2141                 return r;
2142
2143         return 1;
2144 }
2145
2146 static int property_get_set_callbacks_run(
2147                 sd_bus *bus,
2148                 sd_bus_message *m,
2149                 struct vtable_member *c,
2150                 bool require_fallback,
2151                 bool is_get,
2152                 bool *found_object) {
2153
2154         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2155         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2156         void *u;
2157         int r;
2158
2159         assert(bus);
2160         assert(m);
2161         assert(found_object);
2162
2163         if (require_fallback && !c->parent->is_fallback)
2164                 return 0;
2165
2166         r = vtable_property_get_userdata(bus, m->path, c, &u);
2167         if (r <= 0)
2168                 return r;
2169
2170         *found_object = true;
2171
2172         r = sd_bus_message_new_method_return(bus, m, &reply);
2173         if (r < 0)
2174                 return r;
2175
2176         c->last_iteration = bus->iteration_counter;
2177
2178         if (is_get) {
2179                 r = sd_bus_message_open_container(reply, 'v', c->vtable->property.signature);
2180                 if (r < 0)
2181                         return r;
2182
2183                 r = invoke_property_get(bus, c->vtable, m->path, c->interface, c->member, reply, &error, u);
2184                 if (r < 0)
2185                         return r;
2186
2187                 if (sd_bus_error_is_set(&error)) {
2188                         r = sd_bus_reply_method_error(bus, m, &error);
2189                         if (r < 0)
2190                                 return r;
2191
2192                         return 1;
2193                 }
2194
2195                 r = sd_bus_message_close_container(reply);
2196                 if (r < 0)
2197                         return r;
2198
2199         } else {
2200                 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
2201                         sd_bus_error_setf(&error, "org.freedesktop.DBus.Error.PropertyReadOnly", "Property '%s' is not writable.", c->member);
2202                 else  {
2203                         r = sd_bus_message_enter_container(m, 'v', c->vtable->property.signature);
2204                         if (r < 0)
2205                                 return r;
2206
2207                         if (c->vtable->property.set) {
2208                                 r = c->vtable->property.set(bus, m->path, c->interface, c->member, m, &error, u);
2209                                 if (r < 0)
2210                                         return r;
2211                         } else
2212                                 assert_not_reached("automatic properties not supported yet");
2213                 }
2214
2215                 if (sd_bus_error_is_set(&error)) {
2216                         r = sd_bus_reply_method_error(bus, m, &error);
2217                         if (r < 0)
2218                                 return r;
2219
2220                         return 1;
2221                 }
2222
2223                 r = sd_bus_message_exit_container(m);
2224                 if (r < 0)
2225                         return r;
2226         }
2227
2228         r = sd_bus_send(bus, reply, NULL);
2229         if (r < 0)
2230                 return r;
2231
2232         return 1;
2233 }
2234
2235 static int vtable_append_all_properties(
2236                 sd_bus *bus,
2237                 sd_bus_message *reply,
2238                 const char *path,
2239                 struct node_vtable *c,
2240                 void *userdata,
2241                 sd_bus_error *error) {
2242
2243         const sd_bus_vtable *v;
2244         int r;
2245
2246         assert(bus);
2247         assert(reply);
2248         assert(c);
2249
2250         for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2251                 if (v->type != _SD_BUS_VTABLE_PROPERTY && v->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
2252                         continue;
2253
2254                 r = sd_bus_message_open_container(reply, 'e', "sv");
2255                 if (r < 0)
2256                         return r;
2257
2258                 r = sd_bus_message_append(reply, "s", c->interface);
2259                 if (r < 0)
2260                         return r;
2261
2262                 r = sd_bus_message_open_container(reply, 'v', v->property.signature);
2263                 if (r < 0)
2264                         return r;
2265
2266                 r = invoke_property_get(bus, v, path, c->interface, v->property.member, reply, error, vtable_property_convert_userdata(v, userdata));
2267                 if (r < 0)
2268                         return r;
2269
2270                 if (sd_bus_error_is_set(error))
2271                         return 0;
2272
2273                 r = sd_bus_message_close_container(reply);
2274                 if (r < 0)
2275                         return r;
2276
2277                 r = sd_bus_message_close_container(reply);
2278                 if (r < 0)
2279                         return r;
2280         }
2281
2282         return 1;
2283 }
2284
2285 static int property_get_all_callbacks_run(
2286                 sd_bus *bus,
2287                 sd_bus_message *m,
2288                 struct node_vtable *first,
2289                 bool require_fallback,
2290                 const char *iface,
2291                 bool *found_object) {
2292
2293         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2294         struct node_vtable *c;
2295         bool found_interface = false;
2296         int r;
2297
2298         assert(bus);
2299         assert(m);
2300         assert(found_object);
2301
2302         r = sd_bus_message_new_method_return(bus, m, &reply);
2303         if (r < 0)
2304                 return r;
2305
2306         r = sd_bus_message_open_container(reply, 'a', "{sv}");
2307         if (r < 0)
2308                 return r;
2309
2310         LIST_FOREACH(vtables, c, first) {
2311                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2312                 void *u;
2313
2314                 if (require_fallback && !c->is_fallback)
2315                         continue;
2316
2317                 r = node_vtable_get_userdata(bus, m->path, c, &u);
2318                 if (r < 0)
2319                         return r;
2320                 if (r == 0)
2321                         continue;
2322
2323                 *found_object = true;
2324
2325                 if (iface && !streq(c->interface, iface))
2326                         continue;
2327                 found_interface = true;
2328
2329                 c->last_iteration = bus->iteration_counter;
2330
2331                 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
2332                 if (r < 0)
2333                         return r;
2334
2335                 if (sd_bus_error_is_set(&error)) {
2336                         r = sd_bus_reply_method_error(bus, m, &error);
2337                         if (r < 0)
2338                                 return r;
2339
2340                         return 1;
2341                 }
2342         }
2343
2344         if (!found_interface) {
2345                 r = sd_bus_reply_method_errorf(
2346                                 bus, m,
2347                                 "org.freedesktop.DBus.Error.UnknownInterface",
2348                                 "Unknown interface '%s'.", iface);
2349                 if (r < 0)
2350                         return r;
2351
2352                 return 1;
2353         }
2354
2355         r = sd_bus_message_close_container(reply);
2356         if (r < 0)
2357                 return r;
2358
2359         r = sd_bus_send(bus, reply, NULL);
2360         if (r < 0)
2361                 return r;
2362
2363         return 1;
2364 }
2365
2366 static bool bus_node_with_object_manager(sd_bus *bus, struct node *n) {
2367         assert(bus);
2368
2369         if (n->object_manager)
2370                 return true;
2371
2372         if (n->parent)
2373                 return bus_node_with_object_manager(bus, n->parent);
2374
2375         return false;
2376 }
2377
2378 static bool bus_node_exists(sd_bus *bus, struct node *n, const char *path, bool require_fallback) {
2379         struct node_vtable *c;
2380         struct node_callback *k;
2381
2382         assert(bus);
2383         assert(n);
2384
2385         /* Tests if there's anything attached directly to this node
2386          * for the specified path */
2387
2388         LIST_FOREACH(callbacks, k, n->callbacks) {
2389                 if (require_fallback && !k->is_fallback)
2390                         continue;
2391
2392                 return true;
2393         }
2394
2395         LIST_FOREACH(vtables, c, n->vtables) {
2396
2397                 if (require_fallback && !c->is_fallback)
2398                         continue;
2399
2400                 if (node_vtable_get_userdata(bus, path, c, NULL) > 0)
2401                         return true;
2402         }
2403
2404         return !require_fallback && (n->enumerators || n->object_manager);
2405 }
2406
2407 static int process_introspect(
2408                 sd_bus *bus,
2409                 sd_bus_message *m,
2410                 struct node *n,
2411                 bool require_fallback,
2412                 bool *found_object) {
2413
2414         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2415         _cleanup_set_free_free_ Set *s = NULL;
2416         struct introspect intro;
2417         struct node_vtable *c;
2418         bool empty;
2419         int r;
2420
2421         assert(bus);
2422         assert(m);
2423         assert(n);
2424         assert(found_object);
2425
2426         r = get_child_nodes(bus, m->path, n, &s);
2427         if (r < 0)
2428                 return r;
2429
2430         r = introspect_begin(&intro);
2431         if (r < 0)
2432                 return r;
2433
2434         r = introspect_write_default_interfaces(&intro, bus_node_with_object_manager(bus, n));
2435         if (r < 0)
2436                 return r;
2437
2438         empty = set_isempty(s);
2439
2440         LIST_FOREACH(vtables, c, n->vtables) {
2441                 if (require_fallback && !c->is_fallback)
2442                         continue;
2443
2444                 r = node_vtable_get_userdata(bus, m->path, c, NULL);
2445                 if (r < 0)
2446                         return r;
2447                 if (r == 0)
2448                         continue;
2449
2450                 empty = false;
2451
2452                 r = introspect_write_interface(&intro, c->interface, c->vtable);
2453                 if (r < 0)
2454                         goto finish;
2455         }
2456
2457         if (empty) {
2458                 /* Nothing?, let's see if we exist at all, and if not
2459                  * refuse to do anything */
2460                 r = bus_node_exists(bus, n, m->path, require_fallback);
2461                 if (r < 0)
2462                         return r;
2463
2464                 if (r == 0)
2465                         goto finish;
2466         }
2467
2468         *found_object = true;
2469
2470         r = introspect_write_child_nodes(&intro, s, m->path);
2471         if (r < 0)
2472                 goto finish;
2473
2474         r = introspect_finish(&intro, bus, m, &reply);
2475         if (r < 0)
2476                 goto finish;
2477
2478         r = sd_bus_send(bus, reply, NULL);
2479         if (r < 0)
2480                 goto finish;
2481
2482         r = 1;
2483
2484 finish:
2485         introspect_free(&intro);
2486         return r;
2487 }
2488
2489 static int object_manager_serialize_vtable(
2490                 sd_bus *bus,
2491                 sd_bus_message *reply,
2492                 const char *path,
2493                 struct node_vtable *c,
2494                 sd_bus_error *error) {
2495
2496         void *u;
2497         int r;
2498
2499         assert(bus);
2500         assert(reply);
2501         assert(path);
2502         assert(c);
2503         assert(error);
2504
2505         r = node_vtable_get_userdata(bus, path, c, &u);
2506         if (r <= 0)
2507                 return r;
2508
2509         r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
2510         if (r < 0)
2511                 return r;
2512
2513         r = sd_bus_message_append(reply, "s", c->interface);
2514         if (r < 0)
2515                 return r;
2516
2517         r = sd_bus_message_open_container(reply, 'a', "{sv}");
2518         if (r < 0)
2519                 return r;
2520
2521         r = vtable_append_all_properties(bus, reply, path, c, u, error);
2522         if (r < 0)
2523                 return r;
2524
2525         r = sd_bus_message_close_container(reply);
2526         if (r < 0)
2527                 return r;
2528
2529         r = sd_bus_message_close_container(reply);
2530         if (r < 0)
2531                 return r;
2532
2533         return 0;
2534 }
2535
2536 static int object_manager_serialize_path(
2537                 sd_bus *bus,
2538                 sd_bus_message *reply,
2539                 const char *prefix,
2540                 const char *path,
2541                 bool require_fallback,
2542                 sd_bus_error *error) {
2543
2544         struct node_vtable *i;
2545         struct node *n;
2546         int r;
2547
2548         assert(bus);
2549         assert(reply);
2550         assert(prefix);
2551         assert(path);
2552         assert(error);
2553
2554         n = hashmap_get(bus->nodes, prefix);
2555         if (!n)
2556                 return 0;
2557
2558         r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
2559         if (r < 0)
2560                 return r;
2561
2562         r = sd_bus_message_append(reply, "o", path);
2563         if (r < 0)
2564                 return r;
2565
2566         r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
2567         if (r < 0)
2568                 return r;
2569
2570         LIST_FOREACH(vtables, i, n->vtables) {
2571
2572                 if (require_fallback && !i->is_fallback)
2573                         continue;
2574
2575                 r = object_manager_serialize_vtable(bus, reply, path, i, error);
2576                 if (r < 0)
2577                         return r;
2578                 if (sd_bus_error_is_set(error))
2579                         return 0;
2580         }
2581
2582         r = sd_bus_message_close_container(reply);
2583         if (r < 0)
2584                 return r;
2585
2586         r = sd_bus_message_close_container(reply);
2587         if (r < 0)
2588                 return r;
2589
2590         return 1;
2591 }
2592
2593 static int object_manager_serialize_path_and_fallbacks(
2594                 sd_bus *bus,
2595                 sd_bus_message *reply,
2596                 const char *path,
2597                 sd_bus_error *error) {
2598
2599         size_t pl;
2600         int r;
2601
2602         assert(bus);
2603         assert(reply);
2604         assert(path);
2605         assert(error);
2606
2607         /* First, add all vtables registered for this path */
2608         r = object_manager_serialize_path(bus, reply, path, path, false, error);
2609         if (r < 0)
2610                 return r;
2611         if (sd_bus_error_is_set(error))
2612                 return 0;
2613
2614         /* Second, add fallback vtables registered for any of the prefixes */
2615         pl = strlen(path);
2616         if (pl > 1) {
2617                 char p[pl + 1];
2618                 strcpy(p, path);
2619
2620                 for (;;) {
2621                         char *e;
2622
2623                         e = strrchr(p, '/');
2624                         if (e == p || !e)
2625                                 break;
2626
2627                         *e = 0;
2628
2629                         r = object_manager_serialize_path(bus, reply, p, path, true, error);
2630                         if (r < 0)
2631                                 return r;
2632
2633                         if (sd_bus_error_is_set(error))
2634                                 return 0;
2635                 }
2636         }
2637
2638         return 0;
2639 }
2640
2641 static int process_get_managed_objects(
2642                 sd_bus *bus,
2643                 sd_bus_message *m,
2644                 struct node *n,
2645                 bool require_fallback,
2646                 bool *found_object) {
2647
2648         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2649         _cleanup_set_free_free_ Set *s = NULL;
2650         bool empty;
2651         int r;
2652
2653         assert(bus);
2654         assert(m);
2655         assert(n);
2656         assert(found_object);
2657
2658         if (!bus_node_with_object_manager(bus, n))
2659                 return 0;
2660
2661         r = get_child_nodes(bus, m->path, n, &s);
2662         if (r < 0)
2663                 return r;
2664
2665         r = sd_bus_message_new_method_return(bus, m, &reply);
2666         if (r < 0)
2667                 return r;
2668
2669         r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
2670         if (r < 0)
2671                 return r;
2672
2673         empty = set_isempty(s);
2674         if (empty) {
2675                 struct node_vtable *c;
2676
2677                 /* Hmm, so we have no children? Then let's check
2678                  * whether we exist at all, i.e. whether at least one
2679                  * vtable exists. */
2680
2681                 LIST_FOREACH(vtables, c, n->vtables) {
2682
2683                         if (require_fallback && !c->is_fallback)
2684                                 continue;
2685
2686                         if (r < 0)
2687                                 return r;
2688                         if (r == 0)
2689                                 continue;
2690
2691                         empty = false;
2692                         break;
2693                 }
2694
2695                 if (empty)
2696                         return 0;
2697         } else {
2698                 Iterator i;
2699                 char *path;
2700
2701                 SET_FOREACH(path, s, i) {
2702                         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2703
2704                         r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
2705                         if (r < 0)
2706                                 return -ENOMEM;
2707
2708                         if (sd_bus_error_is_set(&error)) {
2709                                 r = sd_bus_reply_method_error(bus, m, &error);
2710                                 if (r < 0)
2711                                         return r;
2712
2713                                 return 1;
2714                         }
2715                 }
2716         }
2717
2718         r = sd_bus_message_close_container(reply);
2719         if (r < 0)
2720                 return r;
2721
2722         r = sd_bus_send(bus, reply, NULL);
2723         if (r < 0)
2724                 return r;
2725
2726         return 1;
2727 }
2728
2729 static int object_find_and_run(
2730                 sd_bus *bus,
2731                 sd_bus_message *m,
2732                 const char *p,
2733                 bool require_fallback,
2734                 bool *found_object) {
2735
2736         struct node *n;
2737         struct vtable_member vtable_key, *v;
2738         int r;
2739
2740         assert(bus);
2741         assert(m);
2742         assert(p);
2743         assert(found_object);
2744
2745         n = hashmap_get(bus->nodes, p);
2746         if (!n)
2747                 return 0;
2748
2749         /* First, try object callbacks */
2750         r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
2751         if (r != 0)
2752                 return r;
2753
2754         if (!m->interface || !m->member)
2755                 return 0;
2756
2757         /* Then, look for a known method */
2758         vtable_key.path = (char*) p;
2759         vtable_key.interface = m->interface;
2760         vtable_key.member = m->member;
2761
2762         v = hashmap_get(bus->vtable_methods, &vtable_key);
2763         if (v) {
2764                 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
2765                 if (r != 0)
2766                         return r;
2767         }
2768
2769         /* Then, look for a known property */
2770         if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
2771                 bool get = false;
2772
2773                 get = streq(m->member, "Get");
2774
2775                 if (get || streq(m->member, "Set")) {
2776
2777                         r = sd_bus_message_rewind(m, true);
2778                         if (r < 0)
2779                                 return r;
2780
2781                         vtable_key.path = (char*) p;
2782
2783                         r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
2784                         if (r < 0)
2785                                 return r;
2786
2787                         v = hashmap_get(bus->vtable_properties, &vtable_key);
2788                         if (v) {
2789                                 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
2790                                 if (r != 0)
2791                                         return r;
2792                         }
2793
2794                 } else if (streq(m->member, "GetAll")) {
2795                         const char *iface;
2796
2797                         r = sd_bus_message_rewind(m, true);
2798                         if (r < 0)
2799                                 return r;
2800
2801                         r = sd_bus_message_read(m, "s", &iface);
2802                         if (r < 0)
2803                                 return r;
2804
2805                         if (iface[0] == 0)
2806                                 iface = NULL;
2807
2808                         r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
2809                         if (r != 0)
2810                                 return r;
2811                 }
2812
2813         } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
2814
2815                 r = process_introspect(bus, m, n, require_fallback, found_object);
2816                 if (r != 0)
2817                         return r;
2818
2819         } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
2820
2821                 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
2822                 if (r != 0)
2823                         return r;
2824         }
2825
2826         if (!*found_object) {
2827                 r = bus_node_exists(bus, n, m->path, require_fallback);
2828                 if (r < 0)
2829                         return r;
2830
2831                 if (r > 0)
2832                         *found_object = true;
2833         }
2834
2835         return 0;
2836 }
2837
2838 static int process_object(sd_bus *bus, sd_bus_message *m) {
2839         int r;
2840         size_t pl;
2841         bool found_object = false;
2842
2843         assert(bus);
2844         assert(m);
2845
2846         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
2847                 return 0;
2848
2849         if (!m->path)
2850                 return 0;
2851
2852         if (hashmap_isempty(bus->nodes))
2853                 return 0;
2854
2855         pl = strlen(m->path);
2856         do {
2857                 char p[pl+1];
2858
2859                 bus->nodes_modified = false;
2860
2861                 r = object_find_and_run(bus, m, m->path, false, &found_object);
2862                 if (r != 0)
2863                         return r;
2864
2865                 /* Look for fallback prefixes */
2866                 strcpy(p, m->path);
2867                 for (;;) {
2868                         char *e;
2869
2870                         if (streq(p, "/"))
2871                                 break;
2872
2873                         if (bus->nodes_modified)
2874                                 break;
2875
2876                         e = strrchr(p, '/');
2877                         assert(e);
2878                         if (e == p)
2879                                 *(e+1) = 0;
2880                         else
2881                                 *e = 0;
2882
2883                         r = object_find_and_run(bus, m, p, true, &found_object);
2884                         if (r != 0)
2885                                 return r;
2886                 }
2887
2888         } while (bus->nodes_modified);
2889
2890         if (!found_object)
2891                 return 0;
2892
2893         if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
2894             sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set"))
2895                 r = sd_bus_reply_method_errorf(
2896                                 bus, m,
2897                                 "org.freedesktop.DBus.Error.UnknownProperty",
2898                                 "Unknown property or interface.");
2899         else
2900                 r = sd_bus_reply_method_errorf(
2901                                 bus, m,
2902                                 "org.freedesktop.DBus.Error.UnknownMethod",
2903                                 "Unknown method '%s' or interface '%s'.", m->member, m->interface);
2904
2905         if (r < 0)
2906                 return r;
2907
2908         return 1;
2909 }
2910
2911 static int process_message(sd_bus *bus, sd_bus_message *m) {
2912         int r;
2913
2914         assert(bus);
2915         assert(m);
2916
2917         bus->iteration_counter++;
2918
2919         r = process_hello(bus, m);
2920         if (r != 0)
2921                 return r;
2922
2923         r = process_reply(bus, m);
2924         if (r != 0)
2925                 return r;
2926
2927         r = process_filter(bus, m);
2928         if (r != 0)
2929                 return r;
2930
2931         r = process_match(bus, m);
2932         if (r != 0)
2933                 return r;
2934
2935         r = process_builtin(bus, m);
2936         if (r != 0)
2937                 return r;
2938
2939         return process_object(bus, m);
2940 }
2941
2942 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2943         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2944         int r;
2945
2946         assert(bus);
2947         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2948
2949         r = process_timeout(bus);
2950         if (r != 0)
2951                 goto null_message;
2952
2953         r = dispatch_wqueue(bus);
2954         if (r != 0)
2955                 goto null_message;
2956
2957         r = dispatch_rqueue(bus, &m);
2958         if (r < 0)
2959                 return r;
2960         if (!m)
2961                 goto null_message;
2962
2963         r = process_message(bus, m);
2964         if (r != 0)
2965                 goto null_message;
2966
2967         if (ret) {
2968                 r = sd_bus_message_rewind(m, true);
2969                 if (r < 0)
2970                         return r;
2971
2972                 *ret = m;
2973                 m = NULL;
2974                 return 1;
2975         }
2976
2977         if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) {
2978
2979                 r = sd_bus_reply_method_errorf(
2980                                 bus, m,
2981                                 "org.freedesktop.DBus.Error.UnknownObject",
2982                                 "Unknown object '%s'.", m->path);
2983                 if (r < 0)
2984                         return r;
2985         }
2986
2987         return 1;
2988
2989 null_message:
2990         if (r >= 0 && ret)
2991                 *ret = NULL;
2992
2993         return r;
2994 }
2995
2996 int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2997         int r;
2998
2999         /* Returns 0 when we didn't do anything. This should cause the
3000          * caller to invoke sd_bus_wait() before returning the next
3001          * time. Returns > 0 when we did something, which possibly
3002          * means *ret is filled in with an unprocessed message. */
3003
3004         if (!bus)
3005                 return -EINVAL;
3006         if (bus_pid_changed(bus))
3007                 return -ECHILD;
3008
3009         /* We don't allow recursively invoking sd_bus_process(). */
3010         if (bus->processing)
3011                 return -EBUSY;
3012
3013         switch (bus->state) {
3014
3015         case BUS_UNSET:
3016         case BUS_CLOSED:
3017                 return -ENOTCONN;
3018
3019         case BUS_OPENING:
3020                 r = bus_socket_process_opening(bus);
3021                 if (r < 0)
3022                         return r;
3023                 if (ret)
3024                         *ret = NULL;
3025                 return r;
3026
3027         case BUS_AUTHENTICATING:
3028
3029                 r = bus_socket_process_authenticating(bus);
3030                 if (r < 0)
3031                         return r;
3032                 if (ret)
3033                         *ret = NULL;
3034                 return r;
3035
3036         case BUS_RUNNING:
3037         case BUS_HELLO:
3038
3039                 bus->processing = true;
3040                 r = process_running(bus, ret);
3041                 bus->processing = false;
3042
3043                 return r;
3044         }
3045
3046         assert_not_reached("Unknown state");
3047 }
3048
3049 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
3050         struct pollfd p[2] = {};
3051         int r, e, n;
3052         struct timespec ts;
3053         usec_t until, m;
3054
3055         assert(bus);
3056
3057         if (!BUS_IS_OPEN(bus->state))
3058                 return -ENOTCONN;
3059
3060         e = sd_bus_get_events(bus);
3061         if (e < 0)
3062                 return e;
3063
3064         if (need_more)
3065                 e |= POLLIN;
3066
3067         r = sd_bus_get_timeout(bus, &until);
3068         if (r < 0)
3069                 return r;
3070         if (r == 0)
3071                 m = (uint64_t) -1;
3072         else {
3073                 usec_t nw;
3074                 nw = now(CLOCK_MONOTONIC);
3075                 m = until > nw ? until - nw : 0;
3076         }
3077
3078         if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
3079                 m = timeout_usec;
3080
3081         p[0].fd = bus->input_fd;
3082         if (bus->output_fd == bus->input_fd) {
3083                 p[0].events = e;
3084                 n = 1;
3085         } else {
3086                 p[0].events = e & POLLIN;
3087                 p[1].fd = bus->output_fd;
3088                 p[1].events = e & POLLOUT;
3089                 n = 2;
3090         }
3091
3092         r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
3093         if (r < 0)
3094                 return -errno;
3095
3096         return r > 0 ? 1 : 0;
3097 }
3098
3099 int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
3100
3101         if (!bus)
3102                 return -EINVAL;
3103         if (!BUS_IS_OPEN(bus->state))
3104                 return -ENOTCONN;
3105         if (bus_pid_changed(bus))
3106                 return -ECHILD;
3107
3108         if (bus->rqueue_size > 0)
3109                 return 0;
3110
3111         return bus_poll(bus, false, timeout_usec);
3112 }
3113
3114 int sd_bus_flush(sd_bus *bus) {
3115         int r;
3116
3117         if (!bus)
3118                 return -EINVAL;
3119         if (!BUS_IS_OPEN(bus->state))
3120                 return -ENOTCONN;
3121         if (bus_pid_changed(bus))
3122                 return -ECHILD;
3123
3124         r = bus_ensure_running(bus);
3125         if (r < 0)
3126                 return r;
3127
3128         if (bus->wqueue_size <= 0)
3129                 return 0;
3130
3131         for (;;) {
3132                 r = dispatch_wqueue(bus);
3133                 if (r < 0)
3134                         return r;
3135
3136                 if (bus->wqueue_size <= 0)
3137                         return 0;
3138
3139                 r = bus_poll(bus, false, (uint64_t) -1);
3140                 if (r < 0)
3141                         return r;
3142         }
3143 }
3144
3145 int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) {
3146         struct filter_callback *f;
3147
3148         if (!bus)
3149                 return -EINVAL;
3150         if (!callback)
3151                 return -EINVAL;
3152         if (bus_pid_changed(bus))
3153                 return -ECHILD;
3154
3155         f = new0(struct filter_callback, 1);
3156         if (!f)
3157                 return -ENOMEM;
3158         f->callback = callback;
3159         f->userdata = userdata;
3160
3161         bus->filter_callbacks_modified = true;
3162         LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f);
3163         return 0;
3164 }
3165
3166 int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) {
3167         struct filter_callback *f;
3168
3169         if (!bus)
3170                 return -EINVAL;
3171         if (!callback)
3172                 return -EINVAL;
3173         if (bus_pid_changed(bus))
3174                 return -ECHILD;
3175
3176         LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
3177                 if (f->callback == callback && f->userdata == userdata) {
3178                         bus->filter_callbacks_modified = true;
3179                         LIST_REMOVE(struct filter_callback, callbacks, bus->filter_callbacks, f);
3180                         free(f);
3181                         return 1;
3182                 }
3183         }
3184
3185         return 0;
3186 }
3187
3188 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
3189         struct node *n, *parent;
3190         const char *e;
3191         char *s, *p;
3192         int r;
3193
3194         assert(bus);
3195         assert(path);
3196         assert(path[0] == '/');
3197
3198         n = hashmap_get(bus->nodes, path);
3199         if (n)
3200                 return n;
3201
3202         r = hashmap_ensure_allocated(&bus->nodes, string_hash_func, string_compare_func);
3203         if (r < 0)
3204                 return NULL;
3205
3206         s = strdup(path);
3207         if (!s)
3208                 return NULL;
3209
3210         if (streq(path, "/"))
3211                 parent = NULL;
3212         else {
3213                 e = strrchr(path, '/');
3214                 assert(e);
3215
3216                 p = strndupa(path, MAX(1, path - e));
3217
3218                 parent = bus_node_allocate(bus, p);
3219                 if (!parent) {
3220                         free(s);
3221                         return NULL;
3222                 }
3223         }
3224
3225         n = new0(struct node, 1);
3226         if (!n)
3227                 return NULL;
3228
3229         n->parent = parent;
3230         n->path = s;
3231
3232         r = hashmap_put(bus->nodes, s, n);
3233         if (r < 0) {
3234                 free(s);
3235                 free(n);
3236                 return NULL;
3237         }
3238
3239         if (parent)
3240                 LIST_PREPEND(struct node, siblings, parent->child, n);
3241
3242         return n;
3243 }
3244
3245 static void bus_node_gc(sd_bus *b, struct node *n) {
3246         assert(b);
3247
3248         if (!n)
3249                 return;
3250
3251         if (n->child ||
3252             n->callbacks ||
3253             n->vtables ||
3254             n->enumerators ||
3255             n->object_manager)
3256                 return;
3257
3258         assert(hashmap_remove(b->nodes, n->path) == n);
3259
3260         if (n->parent)
3261                 LIST_REMOVE(struct node, siblings, n->parent->child, n);
3262
3263         free(n->path);
3264         bus_node_gc(b, n->parent);
3265         free(n);
3266 }
3267
3268 static int bus_add_object(
3269                 sd_bus *b,
3270                 bool fallback,
3271                 const char *path,
3272                 sd_bus_message_handler_t callback,
3273                 void *userdata) {
3274
3275         struct node_callback *c;
3276         struct node *n;
3277         int r;
3278
3279         if (!b)
3280                 return -EINVAL;
3281         if (!object_path_is_valid(path))
3282                 return -EINVAL;
3283         if (!callback)
3284                 return -EINVAL;
3285         if (bus_pid_changed(b))
3286                 return -ECHILD;
3287
3288         n = bus_node_allocate(b, path);
3289         if (!n)
3290                 return -ENOMEM;
3291
3292         c = new0(struct node_callback, 1);
3293         if (!c) {
3294                 r = -ENOMEM;
3295                 goto fail;
3296         }
3297
3298         c->node = n;
3299         c->callback = callback;
3300         c->userdata = userdata;
3301         c->is_fallback = fallback;
3302
3303         LIST_PREPEND(struct node_callback, callbacks, n->callbacks, c);
3304         return 0;
3305
3306 fail:
3307         free(c);
3308         bus_node_gc(b, n);
3309         return r;
3310 }
3311
3312 static int bus_remove_object(
3313                 sd_bus *bus,
3314                 bool fallback,
3315                 const char *path,
3316                 sd_bus_message_handler_t callback,
3317                 void *userdata) {
3318
3319         struct node_callback *c;
3320         struct node *n;
3321
3322         if (!bus)
3323                 return -EINVAL;
3324         if (!object_path_is_valid(path))
3325                 return -EINVAL;
3326         if (!callback)
3327                 return -EINVAL;
3328         if (bus_pid_changed(bus))
3329                 return -ECHILD;
3330
3331         n = hashmap_get(bus->nodes, path);
3332         if (!n)
3333                 return 0;
3334
3335         LIST_FOREACH(callbacks, c, n->callbacks)
3336                 if (c->callback == callback && c->userdata == userdata && c->is_fallback == fallback)
3337                         break;
3338         if (!c)
3339                 return 0;
3340
3341         LIST_REMOVE(struct node_callback, callbacks, n->callbacks, c);
3342         free(c);
3343
3344         bus_node_gc(bus, n);
3345
3346         return 1;
3347 }
3348
3349 int sd_bus_add_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
3350         return bus_add_object(bus, false, path, callback, userdata);
3351 }
3352
3353 int sd_bus_remove_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
3354         return bus_remove_object(bus, false, path, callback, userdata);
3355 }
3356
3357 int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
3358         return bus_add_object(bus, true, prefix, callback, userdata);
3359 }
3360
3361 int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
3362         return bus_remove_object(bus, true, prefix, callback, userdata);
3363 }
3364
3365 int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
3366         struct bus_match_component *components = NULL;
3367         unsigned n_components = 0;
3368         uint64_t cookie = 0;
3369         int r = 0;
3370
3371         if (!bus)
3372                 return -EINVAL;
3373         if (!match)
3374                 return -EINVAL;
3375         if (bus_pid_changed(bus))
3376                 return -ECHILD;
3377
3378         r = bus_match_parse(match, &components, &n_components);
3379         if (r < 0)
3380                 goto finish;
3381
3382         if (bus->bus_client) {
3383                 cookie = ++bus->match_cookie;
3384
3385                 r = bus_add_match_internal(bus, match, components, n_components, cookie);
3386                 if (r < 0)
3387                         goto finish;
3388         }
3389
3390         bus->match_callbacks_modified = true;
3391         r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
3392         if (r < 0) {
3393                 if (bus->bus_client)
3394                         bus_remove_match_internal(bus, match, cookie);
3395         }
3396
3397 finish:
3398         bus_match_parse_free(components, n_components);
3399         return r;
3400 }
3401
3402 int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
3403         struct bus_match_component *components = NULL;
3404         unsigned n_components = 0;
3405         int r = 0, q = 0;
3406         uint64_t cookie = 0;
3407
3408         if (!bus)
3409                 return -EINVAL;
3410         if (!match)
3411                 return -EINVAL;
3412         if (bus_pid_changed(bus))
3413                 return -ECHILD;
3414
3415         r = bus_match_parse(match, &components, &n_components);
3416         if (r < 0)
3417                 return r;
3418
3419         bus->match_callbacks_modified = true;
3420         r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
3421
3422         if (bus->bus_client)
3423                 q = bus_remove_match_internal(bus, match, cookie);
3424
3425         bus_match_parse_free(components, n_components);
3426
3427         return r < 0 ? r : q;
3428 }
3429
3430 int sd_bus_emit_signal(
3431                 sd_bus *bus,
3432                 const char *path,
3433                 const char *interface,
3434                 const char *member,
3435                 const char *types, ...) {
3436
3437         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3438         int r;
3439
3440         if (!bus)
3441                 return -EINVAL;
3442         if (!BUS_IS_OPEN(bus->state))
3443                 return -ENOTCONN;
3444         if (bus_pid_changed(bus))
3445                 return -ECHILD;
3446
3447         r = sd_bus_message_new_signal(bus, path, interface, member, &m);
3448         if (r < 0)
3449                 return r;
3450
3451         if (!isempty(types)) {
3452                 va_list ap;
3453
3454                 va_start(ap, types);
3455                 r = bus_message_append_ap(m, types, ap);
3456                 va_end(ap);
3457                 if (r < 0)
3458                         return r;
3459         }
3460
3461         return sd_bus_send(bus, m, NULL);
3462 }
3463
3464 int sd_bus_call_method(
3465                 sd_bus *bus,
3466                 const char *destination,
3467                 const char *path,
3468                 const char *interface,
3469                 const char *member,
3470                 sd_bus_error *error,
3471                 sd_bus_message **reply,
3472                 const char *types, ...) {
3473
3474         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3475         int r;
3476
3477         if (!bus)
3478
3479                 return -EINVAL;
3480         if (!BUS_IS_OPEN(bus->state))
3481                 return -ENOTCONN;
3482         if (bus_pid_changed(bus))
3483                 return -ECHILD;
3484
3485         r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
3486         if (r < 0)
3487                 return r;
3488
3489         if (!isempty(types)) {
3490                 va_list ap;
3491
3492                 va_start(ap, types);
3493                 r = bus_message_append_ap(m, types, ap);
3494                 va_end(ap);
3495                 if (r < 0)
3496                         return r;
3497         }
3498
3499         return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
3500 }
3501
3502 int sd_bus_reply_method_return(
3503                 sd_bus *bus,
3504                 sd_bus_message *call,
3505                 const char *types, ...) {
3506
3507         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3508         int r;
3509
3510         if (!bus)
3511                 return -EINVAL;
3512         if (!call)
3513                 return -EINVAL;
3514         if (!call->sealed)
3515                 return -EPERM;
3516         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
3517                 return -EINVAL;
3518         if (!BUS_IS_OPEN(bus->state))
3519                 return -ENOTCONN;
3520         if (bus_pid_changed(bus))
3521                 return -ECHILD;
3522
3523         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
3524                 return 0;
3525
3526         r = sd_bus_message_new_method_return(bus, call, &m);
3527         if (r < 0)
3528                 return r;
3529
3530         if (!isempty(types)) {
3531                 va_list ap;
3532
3533                 va_start(ap, types);
3534                 r = bus_message_append_ap(m, types, ap);
3535                 va_end(ap);
3536                 if (r < 0)
3537                         return r;
3538         }
3539
3540         return sd_bus_send(bus, m, NULL);
3541 }
3542
3543 int sd_bus_reply_method_error(
3544                 sd_bus *bus,
3545                 sd_bus_message *call,
3546                 const sd_bus_error *e) {
3547
3548         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
3549         int r;
3550
3551         if (!bus)
3552                 return -EINVAL;
3553         if (!call)
3554                 return -EINVAL;
3555         if (!call->sealed)
3556                 return -EPERM;
3557         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
3558                 return -EINVAL;
3559         if (!sd_bus_error_is_set(e))
3560                 return -EINVAL;
3561         if (!BUS_IS_OPEN(bus->state))
3562                 return -ENOTCONN;
3563         if (bus_pid_changed(bus))
3564                 return -ECHILD;
3565
3566         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
3567                 return 0;
3568
3569         r = sd_bus_message_new_method_error(bus, call, e, &m);
3570         if (r < 0)
3571                 return r;
3572
3573         return sd_bus_send(bus, m, NULL);
3574 }
3575
3576 int sd_bus_reply_method_errorf(
3577                 sd_bus *bus,
3578                 sd_bus_message *call,
3579                 const char *name,
3580                 const char *format,
3581                 ...) {
3582
3583         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
3584         char *n, *m;
3585         va_list ap;
3586         int r;
3587
3588         n = strdup(name);
3589         if (!n)
3590                 return -ENOMEM;
3591
3592         if (format) {
3593                 va_start(ap, format);
3594                 r = vasprintf(&m, format, ap);
3595                 va_end(ap);
3596
3597                 if (r < 0) {
3598                         free(n);
3599                         return -ENOMEM;
3600                 }
3601         }
3602
3603         error.name = n;
3604         error.message = m;
3605         error.need_free = true;
3606
3607         return sd_bus_reply_method_error(bus, call, &error);
3608 }
3609
3610 bool bus_pid_changed(sd_bus *bus) {
3611         assert(bus);
3612
3613         /* We don't support people creating a bus connection and
3614          * keeping it around over a fork(). Let's complain. */
3615
3616         return bus->original_pid != getpid();
3617 }
3618
3619 static void free_node_vtable(sd_bus *bus, struct node_vtable *w) {
3620         assert(bus);
3621
3622         if (!w)
3623                 return;
3624
3625         if (w->interface && w->node && w->vtable) {
3626                 const sd_bus_vtable *v;
3627
3628                 for (v = w->vtable; v->type != _SD_BUS_VTABLE_END; v++) {
3629                         struct vtable_member *x = NULL;
3630
3631                         switch (v->type) {
3632
3633                         case _SD_BUS_VTABLE_METHOD: {
3634                                 struct vtable_member key;
3635
3636                                 key.path = w->node->path;
3637                                 key.interface = w->interface;
3638                                 key.member = v->method.member;
3639
3640                                 x = hashmap_remove(bus->vtable_methods, &key);
3641                                 break;
3642                         }
3643
3644                         case _SD_BUS_VTABLE_PROPERTY:
3645                         case _SD_BUS_VTABLE_WRITABLE_PROPERTY: {
3646                                 struct vtable_member key;
3647
3648                                 key.path = w->node->path;
3649                                 key.interface = w->interface;
3650                                 key.member = v->property.member;
3651                                 x = hashmap_remove(bus->vtable_properties, &key);
3652                                 break;
3653                         }}
3654
3655                         free(x);
3656                 }
3657         }
3658
3659         free(w->interface);
3660         free(w);
3661 }
3662
3663 static unsigned vtable_member_hash_func(const void *a) {
3664         const struct vtable_member *m = a;
3665
3666         return
3667                 string_hash_func(m->path) ^
3668                 string_hash_func(m->interface) ^
3669                 string_hash_func(m->member);
3670 }
3671
3672 static int vtable_member_compare_func(const void *a, const void *b) {
3673         const struct vtable_member *x = a, *y = b;
3674         int r;
3675
3676         r = strcmp(x->path, y->path);
3677         if (r != 0)
3678                 return r;
3679
3680         r = strcmp(x->interface, y->interface);
3681         if (r != 0)
3682                 return r;
3683
3684         return strcmp(x->member, y->member);
3685 }
3686
3687 static int add_object_vtable_internal(
3688                 sd_bus *bus,
3689                 const char *path,
3690                 const char *interface,
3691                 const sd_bus_vtable *vtable,
3692                 bool fallback,
3693                 sd_bus_object_find_t find,
3694                 void *userdata) {
3695
3696         struct node_vtable *c = NULL, *i;
3697         const sd_bus_vtable *v;
3698         struct node *n;
3699         int r;
3700
3701         if (!bus)
3702                 return -EINVAL;
3703         if (!object_path_is_valid(path))
3704                 return -EINVAL;
3705         if (!interface_name_is_valid(interface))
3706                 return -EINVAL;
3707         if (!vtable || vtable[0].type != _SD_BUS_VTABLE_START || vtable[0].start.element_size != sizeof(struct sd_bus_vtable))
3708                 return -EINVAL;
3709         if (bus_pid_changed(bus))
3710                 return -ECHILD;
3711
3712         r = hashmap_ensure_allocated(&bus->vtable_methods, vtable_member_hash_func, vtable_member_compare_func);
3713         if (r < 0)
3714                 return r;
3715
3716         r = hashmap_ensure_allocated(&bus->vtable_properties, vtable_member_hash_func, vtable_member_compare_func);
3717         if (r < 0)
3718                 return r;
3719
3720         n = bus_node_allocate(bus, path);
3721         if (!n)
3722                 return -ENOMEM;
3723
3724         LIST_FOREACH(vtables, i, n->vtables) {
3725                 if (streq(i->interface, interface)) {
3726                         r = -EEXIST;
3727                         goto fail;
3728                 }
3729
3730                 if (i->is_fallback != fallback) {
3731                         r = -EPROTOTYPE;
3732                         goto fail;
3733                 }
3734         }
3735
3736         c = new0(struct node_vtable, 1);
3737         if (!c) {
3738                 r = -ENOMEM;
3739                 goto fail;
3740         }
3741
3742         c->node = n;
3743         c->is_fallback = fallback;
3744         c->vtable = vtable;
3745         c->userdata = userdata;
3746         c->find = find;
3747
3748         c->interface = strdup(interface);
3749         if (!c->interface) {
3750                 r = -ENOMEM;
3751                 goto fail;
3752         }
3753
3754         for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
3755
3756                 switch (v->type) {
3757
3758                 case _SD_BUS_VTABLE_METHOD: {
3759                         struct vtable_member *m;
3760
3761                         if (!member_name_is_valid(v->method.member) ||
3762                             !signature_is_valid(v->method.signature, false) ||
3763                             !signature_is_valid(v->method.result, false) ||
3764                             !v->method.handler ||
3765                             v->flags & (SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY)) {
3766                                 r = -EINVAL;
3767                                 goto fail;
3768                         }
3769
3770                         m = new0(struct vtable_member, 1);
3771                         if (!m) {
3772                                 r = -ENOMEM;
3773                                 goto fail;
3774                         }
3775
3776                         m->parent = c;
3777                         m->path = n->path;
3778                         m->interface = c->interface;
3779                         m->member = v->method.member;
3780                         m->vtable = v;
3781
3782                         r = hashmap_put(bus->vtable_methods, m, m);
3783                         if (r < 0) {
3784                                 free(m);
3785                                 goto fail;
3786                         }
3787
3788                         break;
3789                 }
3790
3791                 case _SD_BUS_VTABLE_PROPERTY:
3792                 case _SD_BUS_VTABLE_WRITABLE_PROPERTY: {
3793                         struct vtable_member *m;
3794
3795                         if (!member_name_is_valid(v->property.member) ||
3796                             !signature_is_single(v->property.signature, false) ||
3797                             !(v->property.get || bus_type_is_basic(v->property.signature[0])) ||
3798                             v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY ||
3799                             (v->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY && !(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))) {
3800                                 r = -EINVAL;
3801                                 goto fail;
3802                         }
3803
3804
3805                         m = new0(struct vtable_member, 1);
3806                         if (!m) {
3807                                 r = -ENOMEM;
3808                                 goto fail;
3809                         }
3810
3811                         m->parent = c;
3812                         m->path = n->path;
3813                         m->interface = c->interface;
3814                         m->member = v->property.member;
3815                         m->vtable = v;
3816
3817                         r = hashmap_put(bus->vtable_properties, m, m);
3818                         if (r < 0) {
3819                                 free(m);
3820                                 goto fail;
3821                         }
3822
3823                         break;
3824                 }
3825
3826                 case _SD_BUS_VTABLE_SIGNAL:
3827
3828                         if (!member_name_is_valid(v->signal.member) ||
3829                             !signature_is_single(v->signal.signature, false)) {
3830                                 r = -EINVAL;
3831                                 goto fail;
3832                         }
3833
3834                         break;
3835
3836                 default:
3837                         r = -EINVAL;
3838                         goto fail;
3839                 }
3840         }
3841
3842         LIST_PREPEND(struct node_vtable, vtables, n->vtables, c);
3843         return 0;
3844
3845 fail:
3846         if (c)
3847                 free_node_vtable(bus, c);
3848
3849         bus_node_gc(bus, n);
3850         return r;
3851 }
3852
3853 static int remove_object_vtable_internal(
3854                 sd_bus *bus,
3855                 const char *path,
3856                 const char *interface,
3857                 bool fallback) {
3858
3859         struct node_vtable *c;
3860         struct node *n;
3861
3862         if (!bus)
3863                 return -EINVAL;
3864         if (!object_path_is_valid(path))
3865                 return -EINVAL;
3866         if (!interface_name_is_valid(interface))
3867                 return -EINVAL;
3868         if (bus_pid_changed(bus))
3869                 return -ECHILD;
3870
3871         n = hashmap_get(bus->nodes, path);
3872         if (!n)
3873                 return 0;
3874
3875         LIST_FOREACH(vtables, c, n->vtables)
3876                 if (streq(c->interface, interface) && c->is_fallback == fallback)
3877                         break;
3878
3879         if (!c)
3880                 return 0;
3881
3882         LIST_REMOVE(struct node_vtable, vtables, n->vtables, c);
3883
3884         free_node_vtable(bus, c);
3885         return 1;
3886 }
3887
3888 int sd_bus_add_object_vtable(
3889                 sd_bus *bus,
3890                 const char *path,
3891                 const char *interface,
3892                 const sd_bus_vtable *vtable,
3893                 void *userdata) {
3894
3895         return add_object_vtable_internal(bus, path, interface, vtable, false, NULL, userdata);
3896 }
3897
3898 int sd_bus_remove_object_vtable(
3899                 sd_bus *bus,
3900                 const char *path,
3901                 const char *interface) {
3902
3903         return remove_object_vtable_internal(bus, path, interface, false);
3904 }
3905
3906 int sd_bus_add_fallback_vtable(
3907                 sd_bus *bus,
3908                 const char *path,
3909                 const char *interface,
3910                 const sd_bus_vtable *vtable,
3911                 sd_bus_object_find_t find,
3912                 void *userdata) {
3913
3914         return add_object_vtable_internal(bus, path, interface, vtable, true, find, userdata);
3915 }
3916
3917 int sd_bus_remove_fallback_vtable(
3918                 sd_bus *bus,
3919                 const char *path,
3920                 const char *interface) {
3921
3922         return remove_object_vtable_internal(bus, path, interface, true);
3923 }
3924
3925 int sd_bus_add_node_enumerator(
3926                 sd_bus *bus,
3927                 const char *path,
3928                 sd_bus_node_enumerator_t callback,
3929                 void *userdata) {
3930
3931         struct node_enumerator *c;
3932         struct node *n;
3933         int r;
3934
3935         if (!bus)
3936                 return -EINVAL;
3937         if (!object_path_is_valid(path))
3938                 return -EINVAL;
3939         if (!callback)
3940                 return -EINVAL;
3941         if (bus_pid_changed(bus))
3942                 return -ECHILD;
3943
3944         n = bus_node_allocate(bus, path);
3945         if (!n)
3946                 return -ENOMEM;
3947
3948         c = new0(struct node_enumerator, 1);
3949         if (!c) {
3950                 r = -ENOMEM;
3951                 goto fail;
3952         }
3953
3954         c->node = n;
3955         c->callback = callback;
3956         c->userdata = userdata;
3957
3958         LIST_PREPEND(struct node_enumerator, enumerators, n->enumerators, c);
3959         return 0;
3960
3961 fail:
3962         free(c);
3963         bus_node_gc(bus, n);
3964         return r;
3965 }
3966
3967 int sd_bus_remove_node_enumerator(
3968                 sd_bus *bus,
3969                 const char *path,
3970                 sd_bus_node_enumerator_t callback,
3971                 void *userdata) {
3972
3973         struct node_enumerator *c;
3974         struct node *n;
3975
3976         if (!bus)
3977                 return -EINVAL;
3978         if (!object_path_is_valid(path))
3979                 return -EINVAL;
3980         if (!callback)
3981                 return -EINVAL;
3982         if (bus_pid_changed(bus))
3983                 return -ECHILD;
3984
3985         n = hashmap_get(bus->nodes, path);
3986         if (!n)
3987                 return 0;
3988
3989         LIST_FOREACH(enumerators, c, n->enumerators)
3990                 if (c->callback == callback && c->userdata == userdata)
3991                         break;
3992
3993         if (!c)
3994                 return 0;
3995
3996         LIST_REMOVE(struct node_enumerator, enumerators, n->enumerators, c);
3997         free(c);
3998
3999         bus_node_gc(bus, n);
4000
4001         return 1;
4002 }
4003
4004 static int emit_properties_changed_on_interface(
4005                 sd_bus *bus,
4006                 const char *prefix,
4007                 const char *path,
4008                 const char *interface,
4009                 bool require_fallback,
4010                 char **names) {
4011
4012         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
4013         bool has_invalidating = false;
4014         struct vtable_member key;
4015         struct node_vtable *c;
4016         struct node *n;
4017         char **property;
4018         void *u = NULL;
4019         int r;
4020
4021         assert(bus);
4022         assert(path);
4023         assert(interface);
4024
4025         n = hashmap_get(bus->nodes, prefix);
4026         if (!n)
4027                 return 0;
4028
4029         LIST_FOREACH(vtables, c, n->vtables) {
4030                 if (require_fallback && !c->is_fallback)
4031                         continue;
4032
4033                 if (streq(c->interface, interface))
4034                         break;
4035         }
4036
4037         if (!c)
4038                 return 0;
4039
4040         r = node_vtable_get_userdata(bus, path, c, &u);
4041         if (r <= 0)
4042                 return r;
4043
4044         r = sd_bus_message_new_signal(bus, path, "org.freedesktop.DBus.Properties", "PropertiesChanged", &m);
4045         if (r < 0)
4046                 return r;
4047
4048         r = sd_bus_message_append(m, "s", interface);
4049         if (r < 0)
4050                 return r;
4051
4052         r = sd_bus_message_open_container(m, 'a', "{sv}");
4053         if (r < 0)
4054                 return r;
4055
4056         key.path = prefix;
4057         key.interface = interface;
4058
4059         STRV_FOREACH(property, names) {
4060                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
4061                 struct vtable_member *v;
4062
4063                 key.member = *property;
4064                 v = hashmap_get(bus->vtable_properties, &key);
4065                 if (!v)
4066                         return -ENOENT;
4067
4068                 assert(c == v->parent);
4069
4070                 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
4071                         return -EDOM;
4072                 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY) {
4073                         has_invalidating = true;
4074                         continue;
4075                 }
4076
4077                 r = sd_bus_message_open_container(m, 'e', "sv");
4078                 if (r < 0)
4079                         return r;
4080
4081                 r = sd_bus_message_append(m, "s", *property);
4082                 if (r < 0)
4083                         return r;
4084
4085                 r = sd_bus_message_open_container(m, 'v', v->vtable->property.signature);
4086                 if (r < 0)
4087                         return r;
4088
4089                 r = invoke_property_get(bus, v->vtable, m->path, interface, *property, m, &error, vtable_property_convert_userdata(v->vtable, u));
4090                 if (r < 0)
4091                         return r;
4092
4093                 if (sd_bus_error_is_set(&error))
4094                         return bus_error_to_errno(&error);
4095
4096                 r = sd_bus_message_close_container(m);
4097                 if (r < 0)
4098                         return r;
4099
4100                 r = sd_bus_message_close_container(m);
4101                 if (r < 0)
4102                         return r;
4103         }
4104
4105         r = sd_bus_message_close_container(m);
4106         if (r < 0)
4107                 return r;
4108
4109         r = sd_bus_message_open_container(m, 'a', "s");
4110         if (r < 0)
4111                 return r;
4112
4113         if (has_invalidating) {
4114                 STRV_FOREACH(property, names) {
4115                         struct vtable_member *v;
4116
4117                         key.member = *property;
4118                         assert_se(v = hashmap_get(bus->vtable_properties, &key));
4119                         assert(c == v->parent);
4120
4121                         if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY))
4122                                 continue;
4123
4124                         r = sd_bus_message_append(m, "s", *property);
4125                         if (r < 0)
4126                                 return r;
4127                 }
4128         }
4129
4130         r = sd_bus_message_close_container(m);
4131         if (r < 0)
4132                 return r;
4133
4134         r = sd_bus_send(bus, m, NULL);
4135         if (r < 0)
4136                 return r;
4137
4138         return 1;
4139 }
4140
4141 int sd_bus_emit_properties_changed_strv(sd_bus *bus, const char *path, const char *interface, char **names) {
4142         size_t pl;
4143         int r;
4144
4145         if (!bus)
4146                 return -EINVAL;
4147         if (!object_path_is_valid(path))
4148                 return -EINVAL;
4149         if (!interface_name_is_valid(interface))
4150                 return -EINVAL;
4151
4152         r = emit_properties_changed_on_interface(bus, path, path, interface, false, names);
4153         if (r != 0)
4154                 return r;
4155
4156         pl = strlen(path);
4157         if (pl > 1 ) {
4158                 char p[pl+1];
4159
4160                 strcpy(p, path);
4161                 for (;;) {
4162                         char *e;
4163
4164                         if (streq(p, "/"))
4165                                 break;
4166
4167                         e = strrchr(p, '/');
4168                         assert(e);
4169                         if (e == p)
4170                                 *(e+1) = 0;
4171                         else
4172                                 *e = 0;
4173
4174                         r = emit_properties_changed_on_interface(bus, p, path, interface, true, names);
4175                         if (r != 0)
4176                                 return r;
4177                 }
4178         }
4179
4180         return -ENOENT;
4181 }
4182
4183 int sd_bus_emit_properties_changed(sd_bus *bus, const char *path, const char *interface, const char *name, ...)  {
4184         _cleanup_strv_free_ char **names = NULL;
4185         va_list ap;
4186
4187         va_start(ap, name);
4188         names = strv_new_ap(name, ap);
4189         va_end(ap);
4190
4191         if (!names)
4192                 return -ENOMEM;
4193
4194         return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
4195 }
4196
4197 int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interfaces, ...) {
4198         return -ENOSYS;
4199 }
4200
4201 int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interfaces, ...) {
4202         return -ENOSYS;
4203 }
4204
4205 int sd_bus_get_property(
4206                 sd_bus *bus,
4207                 const char *destination,
4208                 const char *path,
4209                 const char *interface,
4210                 const char *member,
4211                 sd_bus_error *error,
4212                 sd_bus_message **reply,
4213                 const char *type) {
4214
4215         sd_bus_message *rep = NULL;
4216         int r;
4217
4218         if (interface && !interface_name_is_valid(interface))
4219                 return -EINVAL;
4220         if (!member_name_is_valid(member))
4221                 return -EINVAL;
4222         if (!signature_is_single(type, false))
4223                 return -EINVAL;
4224         if (!reply)
4225                 return -EINVAL;
4226
4227         r = sd_bus_call_method(bus, destination, path, "org.freedesktop.DBus.Properties", "Get", error, &rep, "ss", strempty(interface), member);
4228         if (r < 0)
4229                 return r;
4230
4231         r = sd_bus_message_enter_container(rep, 'v', type);
4232         if (r < 0) {
4233                 sd_bus_message_unref(rep);
4234                 return r;
4235         }
4236
4237         *reply = rep;
4238         return 0;
4239 }
4240
4241 int sd_bus_set_property(
4242                 sd_bus *bus,
4243                 const char *destination,
4244                 const char *path,
4245                 const char *interface,
4246                 const char *member,
4247                 sd_bus_error *error,
4248                 const char *type, ...) {
4249
4250         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
4251         va_list ap;
4252         int r;
4253
4254         if (interface && !interface_name_is_valid(interface))
4255                 return -EINVAL;
4256         if (!member_name_is_valid(member))
4257                 return -EINVAL;
4258         if (!signature_is_single(type, false))
4259                 return -EINVAL;
4260
4261         r = sd_bus_message_new_method_call(bus, destination, path, "org.freedesktop.DBus.Properties", "Set", &m);
4262         if (r < 0)
4263                 return r;
4264
4265         r = sd_bus_message_append(m, "ss", strempty(interface), member);
4266         if (r < 0)
4267                 return r;
4268
4269         r = sd_bus_message_open_container(m, 'v', type);
4270         if (r < 0)
4271                 return r;
4272
4273         va_start(ap, type);
4274         r = bus_message_append_ap(m, type, ap);
4275         va_end(ap);
4276         if (r < 0)
4277                 return r;
4278
4279         r = sd_bus_message_close_container(m);
4280         if (r < 0)
4281                 return r;
4282
4283         return sd_bus_send_with_reply_and_block(bus, m, 0, error, NULL);
4284 }
4285
4286 int sd_bus_add_object_manager(sd_bus *bus, const char *path) {
4287         struct node *n;
4288
4289         if (!bus)
4290                 return -EINVAL;
4291         if (!object_path_is_valid(path))
4292                 return -EINVAL;
4293         if (bus_pid_changed(bus))
4294                 return -ECHILD;
4295
4296         n = bus_node_allocate(bus, path);
4297         if (!n)
4298                 return -ENOMEM;
4299
4300         n->object_manager = true;
4301         return 0;
4302 }
4303
4304 int sd_bus_remove_object_manager(sd_bus *bus, const char *path) {
4305         struct node *n;
4306
4307         if (!bus)
4308                 return -EINVAL;
4309         if (!object_path_is_valid(path))
4310                 return -EINVAL;
4311         if (bus_pid_changed(bus))
4312                 return -ECHILD;
4313
4314         n = hashmap_get(bus->nodes, path);
4315         if (!n)
4316                 return 0;
4317
4318         if (!n->object_manager)
4319                 return 0;
4320
4321         n->object_manager = false;
4322         bus_node_gc(bus, n);
4323         return 1;
4324 }