chiark / gitweb /
sd-bus: mark sd_bus_unref() as broken regarding self-refs
[elogind.git] / src / libsystemd / sd-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 #include "def.h"
38 #include "cgroup-util.h"
39 #include "bus-label.h"
40
41 #include "sd-bus.h"
42 #include "bus-internal.h"
43 #include "bus-message.h"
44 #include "bus-type.h"
45 #include "bus-socket.h"
46 #include "bus-kernel.h"
47 #include "bus-control.h"
48 #include "bus-introspect.h"
49 #include "bus-signature.h"
50 #include "bus-objects.h"
51 #include "bus-util.h"
52 #include "bus-container.h"
53 #include "bus-protocol.h"
54 #include "bus-track.h"
55
56 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
57 static int attach_io_events(sd_bus *b);
58 static void detach_io_events(sd_bus *b);
59
60 static void bus_close_fds(sd_bus *b) {
61         assert(b);
62
63         detach_io_events(b);
64
65         if (b->input_fd >= 0)
66                 safe_close(b->input_fd);
67
68         if (b->output_fd >= 0 && b->output_fd != b->input_fd)
69                 safe_close(b->output_fd);
70
71         b->input_fd = b->output_fd = -1;
72 }
73
74 static void bus_node_destroy(sd_bus *b, struct node *n) {
75         struct node_callback *c;
76         struct node_vtable *v;
77         struct node_enumerator *e;
78
79         assert(b);
80
81         if (!n)
82                 return;
83
84         while (n->child)
85                 bus_node_destroy(b, n->child);
86
87         while ((c = n->callbacks)) {
88                 LIST_REMOVE(callbacks, n->callbacks, c);
89                 free(c);
90         }
91
92         while ((v = n->vtables)) {
93                 LIST_REMOVE(vtables, n->vtables, v);
94                 free(v->interface);
95                 free(v);
96         }
97
98         while ((e = n->enumerators)) {
99                 LIST_REMOVE(enumerators, n->enumerators, e);
100                 free(e);
101         }
102
103         if (n->parent)
104                 LIST_REMOVE(siblings, n->parent->child, n);
105
106         assert_se(hashmap_remove(b->nodes, n->path) == n);
107         free(n->path);
108         free(n);
109 }
110
111 static void bus_reset_queues(sd_bus *b) {
112         assert(b);
113
114         /* NOTE: We _must_ decrement b->Xqueue_size before calling
115          * sd_bus_message_unref() for _each_ message. Otherwise the
116          * self-reference checks in sd_bus_unref() will fire for each message.
117          * We would thus recurse into sd_bus_message_unref() and trigger the
118          * assert(m->n_ref > 0) */
119
120         while (b->rqueue_size > 0)
121                 sd_bus_message_unref(b->rqueue[--b->rqueue_size]);
122
123         free(b->rqueue);
124         b->rqueue = NULL;
125         b->rqueue_allocated = 0;
126
127         while (b->wqueue_size > 0)
128                 sd_bus_message_unref(b->wqueue[--b->wqueue_size]);
129
130         free(b->wqueue);
131         b->wqueue = NULL;
132         b->wqueue_allocated = 0;
133 }
134
135 static void bus_free(sd_bus *b) {
136         struct filter_callback *f;
137         struct node *n;
138
139         assert(b);
140
141         assert(!b->track_queue);
142
143         sd_bus_detach_event(b);
144
145         if (b->default_bus_ptr)
146                 *b->default_bus_ptr = NULL;
147
148         bus_close_fds(b);
149
150         if (b->kdbus_buffer)
151                 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
152
153         free(b->rbuffer);
154         free(b->unique_name);
155         free(b->auth_buffer);
156         free(b->address);
157         free(b->kernel);
158         free(b->machine);
159         free(b->fake_label);
160         free(b->cgroup_root);
161         free(b->connection_name);
162
163         free(b->exec_path);
164         strv_free(b->exec_argv);
165
166         close_many(b->fds, b->n_fds);
167         free(b->fds);
168
169         bus_reset_queues(b);
170
171         hashmap_free_free(b->reply_callbacks);
172         prioq_free(b->reply_callbacks_prioq);
173
174         while ((f = b->filter_callbacks)) {
175                 LIST_REMOVE(callbacks, b->filter_callbacks, f);
176                 free(f);
177         }
178
179         bus_match_free(&b->match_callbacks);
180
181         hashmap_free_free(b->vtable_methods);
182         hashmap_free_free(b->vtable_properties);
183
184         while ((n = hashmap_first(b->nodes)))
185                 bus_node_destroy(b, n);
186
187         hashmap_free(b->nodes);
188
189         bus_kernel_flush_memfd(b);
190
191         assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
192
193         free(b);
194 }
195
196 _public_ int sd_bus_new(sd_bus **ret) {
197         sd_bus *r;
198
199         assert_return(ret, -EINVAL);
200
201         r = new0(sd_bus, 1);
202         if (!r)
203                 return -ENOMEM;
204
205         r->n_ref = REFCNT_INIT;
206         r->input_fd = r->output_fd = -1;
207         r->message_version = 1;
208         r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
209         r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
210         r->attach_flags |= KDBUS_ATTACH_NAMES;
211         r->original_pid = getpid();
212
213         assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
214
215         /* We guarantee that wqueue always has space for at least one
216          * entry */
217         if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
218                 free(r);
219                 return -ENOMEM;
220         }
221
222         *ret = r;
223         return 0;
224 }
225
226 _public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
227         char *a;
228
229         assert_return(bus, -EINVAL);
230         assert_return(bus->state == BUS_UNSET, -EPERM);
231         assert_return(address, -EINVAL);
232         assert_return(!bus_pid_changed(bus), -ECHILD);
233
234         a = strdup(address);
235         if (!a)
236                 return -ENOMEM;
237
238         free(bus->address);
239         bus->address = a;
240
241         return 0;
242 }
243
244 _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
245         assert_return(bus, -EINVAL);
246         assert_return(bus->state == BUS_UNSET, -EPERM);
247         assert_return(input_fd >= 0, -EINVAL);
248         assert_return(output_fd >= 0, -EINVAL);
249         assert_return(!bus_pid_changed(bus), -ECHILD);
250
251         bus->input_fd = input_fd;
252         bus->output_fd = output_fd;
253         return 0;
254 }
255
256 _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
257         char *p, **a;
258
259         assert_return(bus, -EINVAL);
260         assert_return(bus->state == BUS_UNSET, -EPERM);
261         assert_return(path, -EINVAL);
262         assert_return(!strv_isempty(argv), -EINVAL);
263         assert_return(!bus_pid_changed(bus), -ECHILD);
264
265         p = strdup(path);
266         if (!p)
267                 return -ENOMEM;
268
269         a = strv_copy(argv);
270         if (!a) {
271                 free(p);
272                 return -ENOMEM;
273         }
274
275         free(bus->exec_path);
276         strv_free(bus->exec_argv);
277
278         bus->exec_path = p;
279         bus->exec_argv = a;
280
281         return 0;
282 }
283
284 _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
285         assert_return(bus, -EINVAL);
286         assert_return(bus->state == BUS_UNSET, -EPERM);
287         assert_return(!bus_pid_changed(bus), -ECHILD);
288
289         bus->bus_client = !!b;
290         return 0;
291 }
292
293 _public_ int sd_bus_set_monitor(sd_bus *bus, int b) {
294         assert_return(bus, -EINVAL);
295         assert_return(bus->state == BUS_UNSET, -EPERM);
296         assert_return(!bus_pid_changed(bus), -ECHILD);
297
298         SET_FLAG(bus->hello_flags, KDBUS_HELLO_MONITOR, b);
299         return 0;
300 }
301
302 _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
303         assert_return(bus, -EINVAL);
304         assert_return(bus->state == BUS_UNSET, -EPERM);
305         assert_return(!bus_pid_changed(bus), -ECHILD);
306
307         SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
308         return 0;
309 }
310
311 _public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) {
312         assert_return(bus, -EINVAL);
313         assert_return(bus->state == BUS_UNSET, -EPERM);
314         assert_return(!bus_pid_changed(bus), -ECHILD);
315
316         SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b);
317         return 0;
318 }
319
320 _public_ int sd_bus_negotiate_creds(sd_bus *bus, uint64_t mask) {
321         assert_return(bus, -EINVAL);
322         assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
323         assert_return(bus->state == BUS_UNSET, -EPERM);
324         assert_return(!bus_pid_changed(bus), -ECHILD);
325
326         /* The well knowns we need unconditionally, so that matches can work */
327         bus->creds_mask = mask | SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
328
329         return kdbus_translate_attach_flags(bus->creds_mask, &bus->attach_flags);
330 }
331
332 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
333         assert_return(bus, -EINVAL);
334         assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
335         assert_return(bus->state == BUS_UNSET, -EPERM);
336         assert_return(!bus_pid_changed(bus), -ECHILD);
337
338         bus->is_server = !!b;
339         bus->server_id = server_id;
340         return 0;
341 }
342
343 _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
344         assert_return(bus, -EINVAL);
345         assert_return(bus->state == BUS_UNSET, -EPERM);
346         assert_return(!bus_pid_changed(bus), -ECHILD);
347
348         bus->anonymous_auth = !!b;
349         return 0;
350 }
351
352 _public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
353         assert_return(bus, -EINVAL);
354         assert_return(bus->state == BUS_UNSET, -EPERM);
355         assert_return(!bus_pid_changed(bus), -ECHILD);
356
357         bus->trusted = !!b;
358         return 0;
359 }
360
361 _public_ int sd_bus_set_name(sd_bus *bus, const char *name) {
362         char *n;
363
364         assert_return(bus, -EINVAL);
365         assert_return(name, -EINVAL);
366         assert_return(bus->state == BUS_UNSET, -EPERM);
367         assert_return(!bus_pid_changed(bus), -ECHILD);
368
369         n = strdup(name);
370         if (!n)
371                 return -ENOMEM;
372
373         free(bus->connection_name);
374         bus->connection_name = n;
375
376         return 0;
377 }
378
379 static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
380         const char *s;
381         int r;
382
383         assert(bus);
384         assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
385         assert(reply);
386
387         r = sd_bus_message_get_errno(reply);
388         if (r < 0)
389                 return r;
390         if (r > 0)
391                 return -r;
392
393         r = sd_bus_message_read(reply, "s", &s);
394         if (r < 0)
395                 return r;
396
397         if (!service_name_is_valid(s) || s[0] != ':')
398                 return -EBADMSG;
399
400         bus->unique_name = strdup(s);
401         if (!bus->unique_name)
402                 return -ENOMEM;
403
404         if (bus->state == BUS_HELLO)
405                 bus->state = BUS_RUNNING;
406
407         return 1;
408 }
409
410 static int bus_send_hello(sd_bus *bus) {
411         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
412         int r;
413
414         assert(bus);
415
416         if (!bus->bus_client || bus->is_kernel)
417                 return 0;
418
419         r = sd_bus_message_new_method_call(
420                         bus,
421                         &m,
422                         "org.freedesktop.DBus",
423                         "/org/freedesktop/DBus",
424                         "org.freedesktop.DBus",
425                         "Hello");
426         if (r < 0)
427                 return r;
428
429         return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_cookie);
430 }
431
432 int bus_start_running(sd_bus *bus) {
433         assert(bus);
434
435         if (bus->bus_client && !bus->is_kernel) {
436                 bus->state = BUS_HELLO;
437                 return 1;
438         }
439
440         bus->state = BUS_RUNNING;
441         return 1;
442 }
443
444 static int parse_address_key(const char **p, const char *key, char **value) {
445         size_t l, n = 0, allocated = 0;
446         const char *a;
447         char *r = NULL;
448
449         assert(p);
450         assert(*p);
451         assert(value);
452
453         if (key) {
454                 l = strlen(key);
455                 if (strncmp(*p, key, l) != 0)
456                         return 0;
457
458                 if ((*p)[l] != '=')
459                         return 0;
460
461                 if (*value)
462                         return -EINVAL;
463
464                 a = *p + l + 1;
465         } else
466                 a = *p;
467
468         while (*a != ';' && *a != ',' && *a != 0) {
469                 char c;
470
471                 if (*a == '%') {
472                         int x, y;
473
474                         x = unhexchar(a[1]);
475                         if (x < 0) {
476                                 free(r);
477                                 return x;
478                         }
479
480                         y = unhexchar(a[2]);
481                         if (y < 0) {
482                                 free(r);
483                                 return y;
484                         }
485
486                         c = (char) ((x << 4) | y);
487                         a += 3;
488                 } else {
489                         c = *a;
490                         a++;
491                 }
492
493                 if (!GREEDY_REALLOC(r, allocated, n + 2))
494                         return -ENOMEM;
495
496                 r[n++] = c;
497         }
498
499         if (!r) {
500                 r = strdup("");
501                 if (!r)
502                         return -ENOMEM;
503         } else
504                 r[n] = 0;
505
506         if (*a == ',')
507                 a++;
508
509         *p = a;
510
511         free(*value);
512         *value = r;
513
514         return 1;
515 }
516
517 static void skip_address_key(const char **p) {
518         assert(p);
519         assert(*p);
520
521         *p += strcspn(*p, ",");
522
523         if (**p == ',')
524                 (*p) ++;
525 }
526
527 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
528         _cleanup_free_ char *path = NULL, *abstract = NULL;
529         size_t l;
530         int r;
531
532         assert(b);
533         assert(p);
534         assert(*p);
535         assert(guid);
536
537         while (**p != 0 && **p != ';') {
538                 r = parse_address_key(p, "guid", guid);
539                 if (r < 0)
540                         return r;
541                 else if (r > 0)
542                         continue;
543
544                 r = parse_address_key(p, "path", &path);
545                 if (r < 0)
546                         return r;
547                 else if (r > 0)
548                         continue;
549
550                 r = parse_address_key(p, "abstract", &abstract);
551                 if (r < 0)
552                         return r;
553                 else if (r > 0)
554                         continue;
555
556                 skip_address_key(p);
557         }
558
559         if (!path && !abstract)
560                 return -EINVAL;
561
562         if (path && abstract)
563                 return -EINVAL;
564
565         if (path) {
566                 l = strlen(path);
567                 if (l > sizeof(b->sockaddr.un.sun_path))
568                         return -E2BIG;
569
570                 b->sockaddr.un.sun_family = AF_UNIX;
571                 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
572                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
573         } else if (abstract) {
574                 l = strlen(abstract);
575                 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
576                         return -E2BIG;
577
578                 b->sockaddr.un.sun_family = AF_UNIX;
579                 b->sockaddr.un.sun_path[0] = 0;
580                 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
581                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
582         }
583
584         return 0;
585 }
586
587 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
588         _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
589         int r;
590         struct addrinfo *result, hints = {
591                 .ai_socktype = SOCK_STREAM,
592                 .ai_flags = AI_ADDRCONFIG,
593         };
594
595         assert(b);
596         assert(p);
597         assert(*p);
598         assert(guid);
599
600         while (**p != 0 && **p != ';') {
601                 r = parse_address_key(p, "guid", guid);
602                 if (r < 0)
603                         return r;
604                 else if (r > 0)
605                         continue;
606
607                 r = parse_address_key(p, "host", &host);
608                 if (r < 0)
609                         return r;
610                 else if (r > 0)
611                         continue;
612
613                 r = parse_address_key(p, "port", &port);
614                 if (r < 0)
615                         return r;
616                 else if (r > 0)
617                         continue;
618
619                 r = parse_address_key(p, "family", &family);
620                 if (r < 0)
621                         return r;
622                 else if (r > 0)
623                         continue;
624
625                 skip_address_key(p);
626         }
627
628         if (!host || !port)
629                 return -EINVAL;
630
631         if (family) {
632                 if (streq(family, "ipv4"))
633                         hints.ai_family = AF_INET;
634                 else if (streq(family, "ipv6"))
635                         hints.ai_family = AF_INET6;
636                 else
637                         return -EINVAL;
638         }
639
640         r = getaddrinfo(host, port, &hints, &result);
641         if (r == EAI_SYSTEM)
642                 return -errno;
643         else if (r != 0)
644                 return -EADDRNOTAVAIL;
645
646         memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
647         b->sockaddr_size = result->ai_addrlen;
648
649         freeaddrinfo(result);
650
651         return 0;
652 }
653
654 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
655         char *path = NULL;
656         unsigned n_argv = 0, j;
657         char **argv = NULL;
658         size_t allocated = 0;
659         int r;
660
661         assert(b);
662         assert(p);
663         assert(*p);
664         assert(guid);
665
666         while (**p != 0 && **p != ';') {
667                 r = parse_address_key(p, "guid", guid);
668                 if (r < 0)
669                         goto fail;
670                 else if (r > 0)
671                         continue;
672
673                 r = parse_address_key(p, "path", &path);
674                 if (r < 0)
675                         goto fail;
676                 else if (r > 0)
677                         continue;
678
679                 if (startswith(*p, "argv")) {
680                         unsigned ul;
681
682                         errno = 0;
683                         ul = strtoul(*p + 4, (char**) p, 10);
684                         if (errno > 0 || **p != '=' || ul > 256) {
685                                 r = -EINVAL;
686                                 goto fail;
687                         }
688
689                         (*p) ++;
690
691                         if (ul >= n_argv) {
692                                 if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
693                                         r = -ENOMEM;
694                                         goto fail;
695                                 }
696
697                                 n_argv = ul + 1;
698                         }
699
700                         r = parse_address_key(p, NULL, argv + ul);
701                         if (r < 0)
702                                 goto fail;
703
704                         continue;
705                 }
706
707                 skip_address_key(p);
708         }
709
710         if (!path) {
711                 r = -EINVAL;
712                 goto fail;
713         }
714
715         /* Make sure there are no holes in the array, with the
716          * exception of argv[0] */
717         for (j = 1; j < n_argv; j++)
718                 if (!argv[j]) {
719                         r = -EINVAL;
720                         goto fail;
721                 }
722
723         if (argv && argv[0] == NULL) {
724                 argv[0] = strdup(path);
725                 if (!argv[0]) {
726                         r = -ENOMEM;
727                         goto fail;
728                 }
729         }
730
731         b->exec_path = path;
732         b->exec_argv = argv;
733         return 0;
734
735 fail:
736         for (j = 0; j < n_argv; j++)
737                 free(argv[j]);
738
739         free(argv);
740         free(path);
741         return r;
742 }
743
744 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
745         _cleanup_free_ char *path = NULL;
746         int r;
747
748         assert(b);
749         assert(p);
750         assert(*p);
751         assert(guid);
752
753         while (**p != 0 && **p != ';') {
754                 r = parse_address_key(p, "guid", guid);
755                 if (r < 0)
756                         return r;
757                 else if (r > 0)
758                         continue;
759
760                 r = parse_address_key(p, "path", &path);
761                 if (r < 0)
762                         return r;
763                 else if (r > 0)
764                         continue;
765
766                 skip_address_key(p);
767         }
768
769         if (!path)
770                 return -EINVAL;
771
772         free(b->kernel);
773         b->kernel = path;
774         path = NULL;
775
776         return 0;
777 }
778
779 static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
780         _cleanup_free_ char *machine = NULL;
781         int r;
782
783         assert(b);
784         assert(p);
785         assert(*p);
786         assert(guid);
787
788         while (**p != 0 && **p != ';') {
789                 r = parse_address_key(p, "guid", guid);
790                 if (r < 0)
791                         return r;
792                 else if (r > 0)
793                         continue;
794
795                 r = parse_address_key(p, "machine", &machine);
796                 if (r < 0)
797                         return r;
798                 else if (r > 0)
799                         continue;
800
801                 skip_address_key(p);
802         }
803
804         if (!machine)
805                 return -EINVAL;
806
807         if (!filename_is_safe(machine))
808                 return -EINVAL;
809
810         free(b->machine);
811         b->machine = machine;
812         machine = NULL;
813
814         b->sockaddr.un.sun_family = AF_UNIX;
815         strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
816         b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen("/var/run/dbus/system_bus_socket");
817
818         return 0;
819 }
820
821 static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid) {
822         _cleanup_free_ char *machine = NULL;
823         int r;
824
825         assert(b);
826         assert(p);
827         assert(*p);
828         assert(guid);
829
830         while (**p != 0 && **p != ';') {
831                 r = parse_address_key(p, "guid", guid);
832                 if (r < 0)
833                         return r;
834                 else if (r > 0)
835                         continue;
836
837                 r = parse_address_key(p, "machine", &machine);
838                 if (r < 0)
839                         return r;
840                 else if (r > 0)
841                         continue;
842
843                 skip_address_key(p);
844         }
845
846         if (!machine)
847                 return -EINVAL;
848
849         if (!filename_is_safe(machine))
850                 return -EINVAL;
851
852         free(b->machine);
853         b->machine = machine;
854         machine = NULL;
855
856         free(b->kernel);
857         b->kernel = strdup("/dev/kdbus/0-system/bus");
858         if (!b->kernel)
859                 return -ENOMEM;
860
861         return 0;
862 }
863
864 static void bus_reset_parsed_address(sd_bus *b) {
865         assert(b);
866
867         zero(b->sockaddr);
868         b->sockaddr_size = 0;
869         strv_free(b->exec_argv);
870         free(b->exec_path);
871         b->exec_path = NULL;
872         b->exec_argv = NULL;
873         b->server_id = SD_ID128_NULL;
874         free(b->kernel);
875         b->kernel = NULL;
876         free(b->machine);
877         b->machine = NULL;
878 }
879
880 static int bus_parse_next_address(sd_bus *b) {
881         _cleanup_free_ char *guid = NULL;
882         const char *a;
883         int r;
884
885         assert(b);
886
887         if (!b->address)
888                 return 0;
889         if (b->address[b->address_index] == 0)
890                 return 0;
891
892         bus_reset_parsed_address(b);
893
894         a = b->address + b->address_index;
895
896         while (*a != 0) {
897
898                 if (*a == ';') {
899                         a++;
900                         continue;
901                 }
902
903                 if (startswith(a, "unix:")) {
904                         a += 5;
905
906                         r = parse_unix_address(b, &a, &guid);
907                         if (r < 0)
908                                 return r;
909                         break;
910
911                 } else if (startswith(a, "tcp:")) {
912
913                         a += 4;
914                         r = parse_tcp_address(b, &a, &guid);
915                         if (r < 0)
916                                 return r;
917
918                         break;
919
920                 } else if (startswith(a, "unixexec:")) {
921
922                         a += 9;
923                         r = parse_exec_address(b, &a, &guid);
924                         if (r < 0)
925                                 return r;
926
927                         break;
928
929                 } else if (startswith(a, "kernel:")) {
930
931                         a += 7;
932                         r = parse_kernel_address(b, &a, &guid);
933                         if (r < 0)
934                                 return r;
935
936                         break;
937                 } else if (startswith(a, "x-container-unix:")) {
938
939                         a += 17;
940                         r = parse_container_unix_address(b, &a, &guid);
941                         if (r < 0)
942                                 return r;
943
944                         break;
945                 } else if (startswith(a, "x-container-kernel:")) {
946
947                         a += 19;
948                         r = parse_container_kernel_address(b, &a, &guid);
949                         if (r < 0)
950                                 return r;
951
952                         break;
953                 }
954
955                 a = strchr(a, ';');
956                 if (!a)
957                         return 0;
958         }
959
960         if (guid) {
961                 r = sd_id128_from_string(guid, &b->server_id);
962                 if (r < 0)
963                         return r;
964         }
965
966         b->address_index = a - b->address;
967         return 1;
968 }
969
970 static int bus_start_address(sd_bus *b) {
971         int r;
972
973         assert(b);
974
975         for (;;) {
976                 bool skipped = false;
977
978                 bus_close_fds(b);
979
980                 if (b->exec_path)
981                         r = bus_socket_exec(b);
982                 else if (b->machine && b->kernel)
983                         r = bus_container_connect_kernel(b);
984                 else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC)
985                         r = bus_container_connect_socket(b);
986                 else if (b->kernel)
987                         r = bus_kernel_connect(b);
988                 else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
989                         r = bus_socket_connect(b);
990                 else
991                         skipped = true;
992
993                 if (!skipped) {
994                         if (r >= 0) {
995                                 r = attach_io_events(b);
996                                 if (r >= 0)
997                                         return r;
998                         }
999
1000                         b->last_connect_error = -r;
1001                 }
1002
1003                 r = bus_parse_next_address(b);
1004                 if (r < 0)
1005                         return r;
1006                 if (r == 0)
1007                         return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
1008         }
1009 }
1010
1011 int bus_next_address(sd_bus *b) {
1012         assert(b);
1013
1014         bus_reset_parsed_address(b);
1015         return bus_start_address(b);
1016 }
1017
1018 static int bus_start_fd(sd_bus *b) {
1019         struct stat st;
1020         int r;
1021
1022         assert(b);
1023         assert(b->input_fd >= 0);
1024         assert(b->output_fd >= 0);
1025
1026         r = fd_nonblock(b->input_fd, true);
1027         if (r < 0)
1028                 return r;
1029
1030         r = fd_cloexec(b->input_fd, true);
1031         if (r < 0)
1032                 return r;
1033
1034         if (b->input_fd != b->output_fd) {
1035                 r = fd_nonblock(b->output_fd, true);
1036                 if (r < 0)
1037                         return r;
1038
1039                 r = fd_cloexec(b->output_fd, true);
1040                 if (r < 0)
1041                         return r;
1042         }
1043
1044         if (fstat(b->input_fd, &st) < 0)
1045                 return -errno;
1046
1047         if (S_ISCHR(b->input_fd))
1048                 return bus_kernel_take_fd(b);
1049         else
1050                 return bus_socket_take_fd(b);
1051 }
1052
1053 _public_ int sd_bus_start(sd_bus *bus) {
1054         int r;
1055
1056         assert_return(bus, -EINVAL);
1057         assert_return(bus->state == BUS_UNSET, -EPERM);
1058         assert_return(!bus_pid_changed(bus), -ECHILD);
1059
1060         bus->state = BUS_OPENING;
1061
1062         if (bus->is_server && bus->bus_client)
1063                 return -EINVAL;
1064
1065         if (bus->input_fd >= 0)
1066                 r = bus_start_fd(bus);
1067         else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
1068                 r = bus_start_address(bus);
1069         else
1070                 return -EINVAL;
1071
1072         if (r < 0)
1073                 return r;
1074
1075         return bus_send_hello(bus);
1076 }
1077
1078 _public_ int sd_bus_open(sd_bus **ret) {
1079         const char *e;
1080         sd_bus *b;
1081         int r;
1082
1083         assert_return(ret, -EINVAL);
1084
1085         /* Let's connect to the starter bus if it is set, and
1086          * otherwise to the bus that is appropropriate for the scope
1087          * we are running in */
1088
1089         e = secure_getenv("DBUS_STARTER_BUS_TYPE");
1090         if (e) {
1091                 if (streq(e, "system"))
1092                         return sd_bus_open_system(ret);
1093                 else if (STR_IN_SET(e, "session", "user"))
1094                         return sd_bus_open_user(ret);
1095         }
1096
1097         e = secure_getenv("DBUS_STARTER_ADDRESS");
1098         if (!e) {
1099                 if (cg_pid_get_owner_uid(0, NULL) >= 0)
1100                         return sd_bus_open_user(ret);
1101                 else
1102                         return sd_bus_open_system(ret);
1103         }
1104
1105         r = sd_bus_new(&b);
1106         if (r < 0)
1107                 return r;
1108
1109         r = sd_bus_set_address(b, e);
1110         if (r < 0)
1111                 goto fail;
1112
1113         b->bus_client = true;
1114
1115         /* We don't know whether the bus is trusted or not, so better
1116          * be safe, and authenticate everything */
1117         b->trusted = false;
1118         b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1119
1120         r = sd_bus_start(b);
1121         if (r < 0)
1122                 goto fail;
1123
1124         *ret = b;
1125         return 0;
1126
1127 fail:
1128         bus_free(b);
1129         return r;
1130 }
1131
1132 int bus_set_address_system(sd_bus *b) {
1133         const char *e;
1134         assert(b);
1135
1136         e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1137         if (e)
1138                 return sd_bus_set_address(b, e);
1139
1140         return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_PATH);
1141 }
1142
1143 _public_ int sd_bus_open_system(sd_bus **ret) {
1144         sd_bus *b;
1145         int r;
1146
1147         assert_return(ret, -EINVAL);
1148
1149         r = sd_bus_new(&b);
1150         if (r < 0)
1151                 return r;
1152
1153         r = bus_set_address_system(b);
1154         if (r < 0)
1155                 goto fail;
1156
1157         b->bus_client = true;
1158         b->is_system = true;
1159
1160         /* Let's do per-method access control on the system bus. We
1161          * need the caller's UID and capability set for that. */
1162         b->trusted = false;
1163         b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1164
1165         r = sd_bus_start(b);
1166         if (r < 0)
1167                 goto fail;
1168
1169         *ret = b;
1170         return 0;
1171
1172 fail:
1173         bus_free(b);
1174         return r;
1175 }
1176
1177 int bus_set_address_user(sd_bus *b) {
1178         const char *e;
1179
1180         assert(b);
1181
1182         e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1183         if (e)
1184                 return sd_bus_set_address(b, e);
1185
1186         e = secure_getenv("XDG_RUNTIME_DIR");
1187         if (e) {
1188                 _cleanup_free_ char *ee = NULL;
1189
1190                 ee = bus_address_escape(e);
1191                 if (!ee)
1192                         return -ENOMEM;
1193
1194 #ifdef ENABLE_KDBUS
1195                 asprintf(&b->address, KERNEL_USER_BUS_FMT ";" UNIX_USER_BUS_FMT, (unsigned long) getuid(), ee);
1196 #else
1197                 asprintf(&b->address, UNIX_USER_BUS_FMT, ee);
1198 #endif
1199         } else {
1200 #ifdef ENABLE_KDBUS
1201                 asprintf(&b->address, KERNEL_USER_BUS_FMT, (unsigned long) getuid());
1202 #else
1203                 return -ECONNREFUSED;
1204 #endif
1205         }
1206
1207         if (!b->address)
1208                 return -ENOMEM;
1209
1210         return 0;
1211 }
1212
1213 _public_ int sd_bus_open_user(sd_bus **ret) {
1214         sd_bus *b;
1215         int r;
1216
1217         assert_return(ret, -EINVAL);
1218
1219         r = sd_bus_new(&b);
1220         if (r < 0)
1221                 return r;
1222
1223         r = bus_set_address_user(b);
1224         if (r < 0)
1225                 return r;
1226
1227         b->bus_client = true;
1228         b->is_user = true;
1229
1230         /* We don't do any per-method access control on the user
1231          * bus. */
1232         b->trusted = true;
1233
1234         r = sd_bus_start(b);
1235         if (r < 0)
1236                 goto fail;
1237
1238         *ret = b;
1239         return 0;
1240
1241 fail:
1242         bus_free(b);
1243         return r;
1244 }
1245
1246 int bus_set_address_system_remote(sd_bus *b, const char *host) {
1247         _cleanup_free_ char *e = NULL;
1248
1249         assert(b);
1250         assert(host);
1251
1252         e = bus_address_escape(host);
1253         if (!e)
1254                 return -ENOMEM;
1255
1256         b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1257         if (!b->address)
1258                 return -ENOMEM;
1259
1260         return 0;
1261  }
1262
1263 _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
1264         sd_bus *bus;
1265         int r;
1266
1267         assert_return(host, -EINVAL);
1268         assert_return(ret, -EINVAL);
1269
1270         r = sd_bus_new(&bus);
1271         if (r < 0)
1272                 return r;
1273
1274         r = bus_set_address_system_remote(bus, host);
1275         if (r < 0)
1276                 goto fail;
1277
1278         bus->bus_client = true;
1279         bus->trusted = false;
1280
1281         r = sd_bus_start(bus);
1282         if (r < 0)
1283                 goto fail;
1284
1285         *ret = bus;
1286         return 0;
1287
1288 fail:
1289         bus_free(bus);
1290         return r;
1291 }
1292
1293 int bus_set_address_system_container(sd_bus *b, const char *machine) {
1294         _cleanup_free_ char *e = NULL;
1295
1296         assert(b);
1297         assert(machine);
1298
1299         e = bus_address_escape(machine);
1300         if (!e)
1301                 return -ENOMEM;
1302
1303 #ifdef ENABLE_KDBUS
1304         b->address = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL);
1305 #else
1306         b->address = strjoin("x-container-unix:machine=", e, NULL);
1307 #endif
1308         if (!b->address)
1309                 return -ENOMEM;
1310
1311         return 0;
1312 }
1313
1314 _public_ int sd_bus_open_system_container(sd_bus **ret, const char *machine) {
1315         sd_bus *bus;
1316         int r;
1317
1318         assert_return(machine, -EINVAL);
1319         assert_return(ret, -EINVAL);
1320         assert_return(filename_is_safe(machine), -EINVAL);
1321
1322         r = sd_bus_new(&bus);
1323         if (r < 0)
1324                 return r;
1325
1326         r = bus_set_address_system_container(bus, machine);
1327         if (r < 0)
1328                 goto fail;
1329
1330         bus->bus_client = true;
1331         bus->trusted = false;
1332
1333         r = sd_bus_start(bus);
1334         if (r < 0)
1335                 goto fail;
1336
1337         *ret = bus;
1338         return 0;
1339
1340 fail:
1341         bus_free(bus);
1342         return r;
1343 }
1344
1345 _public_ void sd_bus_close(sd_bus *bus) {
1346
1347         if (!bus)
1348                 return;
1349         if (bus->state == BUS_CLOSED)
1350                 return;
1351         if (bus_pid_changed(bus))
1352                 return;
1353
1354         bus->state = BUS_CLOSED;
1355
1356         sd_bus_detach_event(bus);
1357
1358         /* Drop all queued messages so that they drop references to
1359          * the bus object and the bus may be freed */
1360         bus_reset_queues(bus);
1361
1362         if (!bus->is_kernel)
1363                 bus_close_fds(bus);
1364
1365         /* We'll leave the fd open in case this is a kernel bus, since
1366          * there might still be memblocks around that reference this
1367          * bus, and they might need to invoke the KDBUS_CMD_FREE
1368          * ioctl on the fd when they are freed. */
1369 }
1370
1371 static void bus_enter_closing(sd_bus *bus) {
1372         assert(bus);
1373
1374         if (bus->state != BUS_OPENING &&
1375             bus->state != BUS_AUTHENTICATING &&
1376             bus->state != BUS_HELLO &&
1377             bus->state != BUS_RUNNING)
1378                 return;
1379
1380         bus->state = BUS_CLOSING;
1381 }
1382
1383 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1384         assert_return(bus, NULL);
1385
1386         assert_se(REFCNT_INC(bus->n_ref) >= 2);
1387
1388         return bus;
1389 }
1390
1391 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1392         unsigned i;
1393
1394         if (!bus)
1395                 return NULL;
1396
1397         /* TODO/FIXME: It's naive to think REFCNT_GET() is thread-safe in any
1398          * way but exclusive REFCNT_DEC(). The current logic _must_ lock around
1399          * REFCNT_GET() until REFCNT_DEC() or two threads might end up in
1400          * parallel in bus_reset_queues(). But locking would totally break the
1401          * recursion we introduce by bus_reset_queues()...
1402          * (Imagine one thread in sd_bus_message_unref() setting n_ref to 0 and
1403          * thus calling into sd_bus_unref(). If at the same time the real
1404          * thread calls sd_bus_unref(), both end up with "q == true" and will
1405          * call into bus_reset_queues().
1406          * If we require the main bus to be alive until all dispatch threads
1407          * are done, there is no need to do ref-counts at all. So in both ways,
1408          * the REFCNT thing is humbug.)
1409          *
1410          * On a second note: messages are *not* required to have ->bus set nor
1411          * does it have to be _this_ bus that they're assigned to. This whole
1412          * ref-cnt checking breaks apart if a message is not assigned to us.
1413          * (which is _very_ easy to trigger with the current API). */
1414
1415         if (REFCNT_GET(bus->n_ref) == bus->rqueue_size + bus->wqueue_size + 1) {
1416                 bool q = true;
1417
1418                 for (i = 0; i < bus->rqueue_size; i++)
1419                         if (bus->rqueue[i]->n_ref > 1) {
1420                                 q = false;
1421                                 break;
1422                         }
1423
1424                 if (q) {
1425                         for (i = 0; i < bus->wqueue_size; i++)
1426                                 if (bus->wqueue[i]->n_ref > 1) {
1427                                         q = false;
1428                                         break;
1429                                 }
1430                 }
1431
1432                 /* We are the only holders on the messages, and the
1433                  * messages are the only holders on us, so let's drop
1434                  * the messages and thus implicitly also kill our own
1435                  * last references.
1436                  * bus_reset_queues() decrements the queue-size before
1437                  * calling into sd_bus_message_unref(). Thus, it
1438                  * protects us from recursion. */
1439
1440                 if (q)
1441                         bus_reset_queues(bus);
1442         }
1443
1444         i = REFCNT_DEC(bus->n_ref);
1445         if (i > 0)
1446                 return NULL;
1447
1448         bus_free(bus);
1449         return NULL;
1450 }
1451
1452 _public_ int sd_bus_is_open(sd_bus *bus) {
1453
1454         assert_return(bus, -EINVAL);
1455         assert_return(!bus_pid_changed(bus), -ECHILD);
1456
1457         return BUS_IS_OPEN(bus->state);
1458 }
1459
1460 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1461         int r;
1462
1463         assert_return(bus, -EINVAL);
1464         assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1465         assert_return(!bus_pid_changed(bus), -ECHILD);
1466
1467         if (bus->hello_flags & KDBUS_HELLO_MONITOR)
1468                 return 0;
1469
1470         if (type == SD_BUS_TYPE_UNIX_FD) {
1471                 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1472                         return 0;
1473
1474                 r = bus_ensure_running(bus);
1475                 if (r < 0)
1476                         return r;
1477
1478                 return bus->can_fds;
1479         }
1480
1481         return bus_type_is_valid(type);
1482 }
1483
1484 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1485         int r;
1486
1487         assert_return(bus, -EINVAL);
1488         assert_return(server_id, -EINVAL);
1489         assert_return(!bus_pid_changed(bus), -ECHILD);
1490
1491         r = bus_ensure_running(bus);
1492         if (r < 0)
1493                 return r;
1494
1495         *server_id = bus->server_id;
1496         return 0;
1497 }
1498
1499 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1500         assert(b);
1501         assert(m);
1502
1503         if (m->sealed) {
1504                 /* If we copy the same message to multiple
1505                  * destinations, avoid using the same cookie
1506                  * numbers. */
1507                 b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
1508                 return 0;
1509         }
1510
1511         if (timeout == 0)
1512                 timeout = BUS_DEFAULT_TIMEOUT;
1513
1514         return bus_message_seal(m, ++b->cookie, timeout);
1515 }
1516
1517 static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
1518         assert(b);
1519
1520         /* Do packet version and endianness already match? */
1521         if ((b->message_version == 0 || b->message_version == (*m)->header->version) &&
1522             (b->message_endian == 0 || b->message_endian == (*m)->header->endian))
1523                 return 0;
1524
1525         /* No? Then remarshal! */
1526         return bus_message_remarshal(b, m);
1527 }
1528
1529 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1530         assert(b);
1531         assert(m);
1532
1533         /* The bus specification says the serial number cannot be 0,
1534          * hence let's fill something in for synthetic messages. Since
1535          * synthetic messages might have a fake sender and we don't
1536          * want to interfere with the real sender's serial numbers we
1537          * pick a fixed, artificial one. We use (uint32_t) -1 rather
1538          * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1539          * even though kdbus can do 64bit. */
1540
1541         return bus_message_seal(m, 0xFFFFFFFFULL, 0);
1542 }
1543
1544 static int bus_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call, size_t *idx) {
1545         int r;
1546
1547         assert(bus);
1548         assert(m);
1549
1550         if (bus->is_kernel)
1551                 r = bus_kernel_write_message(bus, m, hint_sync_call);
1552         else
1553                 r = bus_socket_write_message(bus, m, idx);
1554
1555         if (r <= 0)
1556                 return r;
1557
1558         if (bus->is_kernel || *idx >= BUS_MESSAGE_SIZE(m))
1559                 log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
1560                           bus_message_type_to_string(m->header->type),
1561                           strna(sd_bus_message_get_sender(m)),
1562                           strna(sd_bus_message_get_destination(m)),
1563                           strna(sd_bus_message_get_path(m)),
1564                           strna(sd_bus_message_get_interface(m)),
1565                           strna(sd_bus_message_get_member(m)),
1566                           BUS_MESSAGE_COOKIE(m),
1567                           m->reply_cookie,
1568                           strna(m->error.message));
1569
1570         return r;
1571 }
1572
1573 static int dispatch_wqueue(sd_bus *bus) {
1574         int r, ret = 0;
1575
1576         assert(bus);
1577         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1578
1579         while (bus->wqueue_size > 0) {
1580
1581                 r = bus_write_message(bus, bus->wqueue[0], false, &bus->windex);
1582                 if (r < 0)
1583                         return r;
1584                 else if (r == 0)
1585                         /* Didn't do anything this time */
1586                         return ret;
1587                 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1588                         /* Fully written. Let's drop the entry from
1589                          * the queue.
1590                          *
1591                          * This isn't particularly optimized, but
1592                          * well, this is supposed to be our worst-case
1593                          * buffer only, and the socket buffer is
1594                          * supposed to be our primary buffer, and if
1595                          * it got full, then all bets are off
1596                          * anyway. */
1597
1598                         bus->wqueue_size --;
1599                         sd_bus_message_unref(bus->wqueue[0]);
1600                         memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1601                         bus->windex = 0;
1602
1603                         ret = 1;
1604                 }
1605         }
1606
1607         return ret;
1608 }
1609
1610 static int bus_read_message(sd_bus *bus, bool hint_priority, int64_t priority) {
1611         assert(bus);
1612
1613         if (bus->is_kernel)
1614                 return bus_kernel_read_message(bus, hint_priority, priority);
1615         else
1616                 return bus_socket_read_message(bus);
1617 }
1618
1619 int bus_rqueue_make_room(sd_bus *bus) {
1620         assert(bus);
1621
1622         if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1623                 return -ENOBUFS;
1624
1625         if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
1626                 return -ENOMEM;
1627
1628         return 0;
1629 }
1630
1631 static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) {
1632         int r, ret = 0;
1633
1634         assert(bus);
1635         assert(m);
1636         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1637
1638         /* Note that the priority logic is only available on kdbus,
1639          * where the rqueue is unused. We check the rqueue here
1640          * anyway, because it's simple... */
1641
1642         for (;;) {
1643                 if (bus->rqueue_size > 0) {
1644                         /* Dispatch a queued message */
1645
1646                         *m = bus->rqueue[0];
1647                         bus->rqueue_size --;
1648                         memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1649                         return 1;
1650                 }
1651
1652                 /* Try to read a new message */
1653                 r = bus_read_message(bus, hint_priority, priority);
1654                 if (r < 0)
1655                         return r;
1656                 if (r == 0)
1657                         return ret;
1658
1659                 ret = 1;
1660         }
1661 }
1662
1663 static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) {
1664         _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
1665         int r;
1666
1667         assert_return(bus, -EINVAL);
1668         assert_return(m, -EINVAL);
1669         assert_return(!bus_pid_changed(bus), -ECHILD);
1670         assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS);
1671
1672         if (!BUS_IS_OPEN(bus->state))
1673                 return -ENOTCONN;
1674
1675         if (m->n_fds > 0) {
1676                 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1677                 if (r < 0)
1678                         return r;
1679                 if (r == 0)
1680                         return -ENOTSUP;
1681         }
1682
1683         /* If the cookie number isn't kept, then we know that no reply
1684          * is expected */
1685         if (!cookie && !m->sealed)
1686                 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1687
1688         r = bus_seal_message(bus, m, 0);
1689         if (r < 0)
1690                 return r;
1691
1692         /* Remarshall if we have to. This will possibly unref the
1693          * message and place a replacement in m */
1694         r = bus_remarshal_message(bus, &m);
1695         if (r < 0)
1696                 return r;
1697
1698         /* If this is a reply and no reply was requested, then let's
1699          * suppress this, if we can */
1700         if (m->dont_send && !cookie)
1701                 return 1;
1702
1703         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1704                 size_t idx = 0;
1705
1706                 r = bus_write_message(bus, m, hint_sync_call, &idx);
1707                 if (r < 0) {
1708                         if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
1709                                 bus_enter_closing(bus);
1710                                 return -ECONNRESET;
1711                         }
1712
1713                         return r;
1714                 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m))  {
1715                         /* Wasn't fully written. So let's remember how
1716                          * much was written. Note that the first entry
1717                          * of the wqueue array is always allocated so
1718                          * that we always can remember how much was
1719                          * written. */
1720                         bus->wqueue[0] = sd_bus_message_ref(m);
1721                         bus->wqueue_size = 1;
1722                         bus->windex = idx;
1723                 }
1724         } else {
1725                 /* Just append it to the queue. */
1726
1727                 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1728                         return -ENOBUFS;
1729
1730                 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
1731                         return -ENOMEM;
1732
1733                 bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m);
1734         }
1735
1736         if (cookie)
1737                 *cookie = BUS_MESSAGE_COOKIE(m);
1738
1739         return 1;
1740 }
1741
1742 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *cookie) {
1743         return bus_send_internal(bus, m, cookie, false);
1744 }
1745
1746 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
1747         int r;
1748
1749         assert_return(bus, -EINVAL);
1750         assert_return(m, -EINVAL);
1751         assert_return(!bus_pid_changed(bus), -ECHILD);
1752
1753         if (!BUS_IS_OPEN(bus->state))
1754                 return -ENOTCONN;
1755
1756         if (!streq_ptr(m->destination, destination)) {
1757
1758                 if (!destination)
1759                         return -EEXIST;
1760
1761                 r = sd_bus_message_set_destination(m, destination);
1762                 if (r < 0)
1763                         return r;
1764         }
1765
1766         return sd_bus_send(bus, m, cookie);
1767 }
1768
1769 static usec_t calc_elapse(uint64_t usec) {
1770         if (usec == (uint64_t) -1)
1771                 return 0;
1772
1773         return now(CLOCK_MONOTONIC) + usec;
1774 }
1775
1776 static int timeout_compare(const void *a, const void *b) {
1777         const struct reply_callback *x = a, *y = b;
1778
1779         if (x->timeout != 0 && y->timeout == 0)
1780                 return -1;
1781
1782         if (x->timeout == 0 && y->timeout != 0)
1783                 return 1;
1784
1785         if (x->timeout < y->timeout)
1786                 return -1;
1787
1788         if (x->timeout > y->timeout)
1789                 return 1;
1790
1791         return 0;
1792 }
1793
1794 _public_ int sd_bus_call_async(
1795                 sd_bus *bus,
1796                 sd_bus_message *_m,
1797                 sd_bus_message_handler_t callback,
1798                 void *userdata,
1799                 uint64_t usec,
1800                 uint64_t *cookie) {
1801
1802         _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
1803         struct reply_callback *c;
1804         int r;
1805
1806         assert_return(bus, -EINVAL);
1807         assert_return(m, -EINVAL);
1808         assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1809         assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1810         assert_return(callback, -EINVAL);
1811         assert_return(!bus_pid_changed(bus), -ECHILD);
1812         assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS);
1813
1814         if (!BUS_IS_OPEN(bus->state))
1815                 return -ENOTCONN;
1816
1817         r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1818         if (r < 0)
1819                 return r;
1820
1821         r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1822         if (r < 0)
1823                 return r;
1824
1825         r = bus_seal_message(bus, m, usec);
1826         if (r < 0)
1827                 return r;
1828
1829         r = bus_remarshal_message(bus, &m);
1830         if (r < 0)
1831                 return r;
1832
1833         c = new0(struct reply_callback, 1);
1834         if (!c)
1835                 return -ENOMEM;
1836
1837         c->callback = callback;
1838         c->userdata = userdata;
1839         c->cookie = BUS_MESSAGE_COOKIE(m);
1840         c->timeout = calc_elapse(m->timeout);
1841
1842         r = hashmap_put(bus->reply_callbacks, &c->cookie, c);
1843         if (r < 0) {
1844                 free(c);
1845                 return r;
1846         }
1847
1848         if (c->timeout != 0) {
1849                 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1850                 if (r < 0) {
1851                         c->timeout = 0;
1852                         sd_bus_call_async_cancel(bus, c->cookie);
1853                         return r;
1854                 }
1855         }
1856
1857         r = sd_bus_send(bus, m, cookie);
1858         if (r < 0) {
1859                 sd_bus_call_async_cancel(bus, c->cookie);
1860                 return r;
1861         }
1862
1863         return r;
1864 }
1865
1866 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t cookie) {
1867         struct reply_callback *c;
1868
1869         assert_return(bus, -EINVAL);
1870         assert_return(cookie != 0, -EINVAL);
1871         assert_return(!bus_pid_changed(bus), -ECHILD);
1872
1873         c = hashmap_remove(bus->reply_callbacks, &cookie);
1874         if (!c)
1875                 return 0;
1876
1877         if (c->timeout != 0)
1878                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1879
1880         free(c);
1881         return 1;
1882 }
1883
1884 int bus_ensure_running(sd_bus *bus) {
1885         int r;
1886
1887         assert(bus);
1888
1889         if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1890                 return -ENOTCONN;
1891         if (bus->state == BUS_RUNNING)
1892                 return 1;
1893
1894         for (;;) {
1895                 r = sd_bus_process(bus, NULL);
1896                 if (r < 0)
1897                         return r;
1898                 if (bus->state == BUS_RUNNING)
1899                         return 1;
1900                 if (r > 0)
1901                         continue;
1902
1903                 r = sd_bus_wait(bus, (uint64_t) -1);
1904                 if (r < 0)
1905                         return r;
1906         }
1907 }
1908
1909 _public_ int sd_bus_call(
1910                 sd_bus *bus,
1911                 sd_bus_message *_m,
1912                 uint64_t usec,
1913                 sd_bus_error *error,
1914                 sd_bus_message **reply) {
1915
1916         _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
1917         usec_t timeout;
1918         uint64_t cookie;
1919         unsigned i;
1920         int r;
1921
1922         assert_return(bus, -EINVAL);
1923         assert_return(m, -EINVAL);
1924         assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1925         assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1926         assert_return(!bus_error_is_dirty(error), -EINVAL);
1927         assert_return(!bus_pid_changed(bus), -ECHILD);
1928         assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS);
1929
1930         if (!BUS_IS_OPEN(bus->state))
1931                 return -ENOTCONN;
1932
1933         r = bus_ensure_running(bus);
1934         if (r < 0)
1935                 return r;
1936
1937         i = bus->rqueue_size;
1938
1939         r = bus_seal_message(bus, m, usec);
1940         if (r < 0)
1941                 return r;
1942
1943         r = bus_remarshal_message(bus, &m);
1944         if (r < 0)
1945                 return r;
1946
1947         r = bus_send_internal(bus, m, &cookie, true);
1948         if (r < 0)
1949                 return r;
1950
1951         timeout = calc_elapse(m->timeout);
1952
1953         for (;;) {
1954                 usec_t left;
1955
1956                 while (i < bus->rqueue_size) {
1957                         sd_bus_message *incoming = NULL;
1958
1959                         incoming = bus->rqueue[i];
1960
1961                         if (incoming->reply_cookie == cookie) {
1962                                 /* Found a match! */
1963
1964                                 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1965                                 bus->rqueue_size--;
1966
1967                                 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1968
1969                                         if (incoming->n_fds <= 0 || (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
1970                                                 if (reply)
1971                                                         *reply = incoming;
1972                                                 else
1973                                                         sd_bus_message_unref(incoming);
1974
1975                                                 return 1;
1976                                         }
1977
1978                                         r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
1979
1980                                 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1981                                         r = sd_bus_error_copy(error, &incoming->error);
1982                                 else
1983                                         r = -EIO;
1984
1985                                 sd_bus_message_unref(incoming);
1986                                 return r;
1987
1988                         } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
1989                                    bus->unique_name &&
1990                                    incoming->sender &&
1991                                    streq(bus->unique_name, incoming->sender)) {
1992
1993                                 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1994                                 bus->rqueue_size--;
1995
1996                                 /* Our own message? Somebody is trying
1997                                  * to send its own client a message,
1998                                  * let's not dead-lock, let's fail
1999                                  * immediately. */
2000
2001                                 sd_bus_message_unref(incoming);
2002                                 return -ELOOP;
2003                         }
2004
2005                         /* Try to read more, right-away */
2006                         i++;
2007                 }
2008
2009                 r = bus_read_message(bus, false, 0);
2010                 if (r < 0) {
2011                         if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2012                                 bus_enter_closing(bus);
2013                                 return -ECONNRESET;
2014                         }
2015
2016                         return r;
2017                 }
2018                 if (r > 0)
2019                         continue;
2020
2021                 if (timeout > 0) {
2022                         usec_t n;
2023
2024                         n = now(CLOCK_MONOTONIC);
2025                         if (n >= timeout)
2026                                 return -ETIMEDOUT;
2027
2028                         left = timeout - n;
2029                 } else
2030                         left = (uint64_t) -1;
2031
2032                 r = bus_poll(bus, true, left);
2033                 if (r < 0)
2034                         return r;
2035                 if (r == 0)
2036                         return -ETIMEDOUT;
2037
2038                 r = dispatch_wqueue(bus);
2039                 if (r < 0) {
2040                         if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2041                                 bus_enter_closing(bus);
2042                                 return -ECONNRESET;
2043                         }
2044
2045                         return r;
2046                 }
2047         }
2048 }
2049
2050 _public_ int sd_bus_get_fd(sd_bus *bus) {
2051
2052         assert_return(bus, -EINVAL);
2053         assert_return(bus->input_fd == bus->output_fd, -EPERM);
2054         assert_return(!bus_pid_changed(bus), -ECHILD);
2055
2056         return bus->input_fd;
2057 }
2058
2059 _public_ int sd_bus_get_events(sd_bus *bus) {
2060         int flags = 0;
2061
2062         assert_return(bus, -EINVAL);
2063         assert_return(!bus_pid_changed(bus), -ECHILD);
2064
2065         if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2066                 return -ENOTCONN;
2067
2068         if (bus->state == BUS_OPENING)
2069                 flags |= POLLOUT;
2070         else if (bus->state == BUS_AUTHENTICATING) {
2071
2072                 if (bus_socket_auth_needs_write(bus))
2073                         flags |= POLLOUT;
2074
2075                 flags |= POLLIN;
2076
2077         } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
2078                 if (bus->rqueue_size <= 0)
2079                         flags |= POLLIN;
2080                 if (bus->wqueue_size > 0)
2081                         flags |= POLLOUT;
2082         }
2083
2084         return flags;
2085 }
2086
2087 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
2088         struct reply_callback *c;
2089
2090         assert_return(bus, -EINVAL);
2091         assert_return(timeout_usec, -EINVAL);
2092         assert_return(!bus_pid_changed(bus), -ECHILD);
2093
2094         if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2095                 return -ENOTCONN;
2096
2097         if (bus->track_queue) {
2098                 *timeout_usec = 0;
2099                 return 1;
2100         }
2101
2102         if (bus->state == BUS_CLOSING) {
2103                 *timeout_usec = 0;
2104                 return 1;
2105         }
2106
2107         if (bus->state == BUS_AUTHENTICATING) {
2108                 *timeout_usec = bus->auth_timeout;
2109                 return 1;
2110         }
2111
2112         if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
2113                 *timeout_usec = (uint64_t) -1;
2114                 return 0;
2115         }
2116
2117         if (bus->rqueue_size > 0) {
2118                 *timeout_usec = 0;
2119                 return 1;
2120         }
2121
2122         c = prioq_peek(bus->reply_callbacks_prioq);
2123         if (!c) {
2124                 *timeout_usec = (uint64_t) -1;
2125                 return 0;
2126         }
2127
2128         *timeout_usec = c->timeout;
2129         return 1;
2130 }
2131
2132 static int process_timeout(sd_bus *bus) {
2133         _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2134         _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
2135         struct reply_callback *c;
2136         usec_t n;
2137         int r;
2138
2139         assert(bus);
2140
2141         c = prioq_peek(bus->reply_callbacks_prioq);
2142         if (!c)
2143                 return 0;
2144
2145         n = now(CLOCK_MONOTONIC);
2146         if (c->timeout > n)
2147                 return 0;
2148
2149         r = bus_message_new_synthetic_error(
2150                         bus,
2151                         c->cookie,
2152                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
2153                         &m);
2154         if (r < 0)
2155                 return r;
2156
2157         m->sender = "org.freedesktop.DBus";
2158
2159         r = bus_seal_synthetic_message(bus, m);
2160         if (r < 0)
2161                 return r;
2162
2163         assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
2164         hashmap_remove(bus->reply_callbacks, &c->cookie);
2165
2166         bus->current = m;
2167         bus->iteration_counter ++;
2168
2169         r = c->callback(bus, m, c->userdata, &error_buffer);
2170         r = bus_maybe_reply_error(m, r, &error_buffer);
2171         free(c);
2172
2173         bus->current = NULL;
2174
2175         return r;
2176 }
2177
2178 static int process_hello(sd_bus *bus, sd_bus_message *m) {
2179         assert(bus);
2180         assert(m);
2181
2182         if (bus->state != BUS_HELLO)
2183                 return 0;
2184
2185         /* Let's make sure the first message on the bus is the HELLO
2186          * reply. But note that we don't actually parse the message
2187          * here (we leave that to the usual handling), we just verify
2188          * we don't let any earlier msg through. */
2189
2190         if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
2191             m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
2192                 return -EIO;
2193
2194         if (m->reply_cookie != bus->hello_cookie)
2195                 return -EIO;
2196
2197         return 0;
2198 }
2199
2200 static int process_reply(sd_bus *bus, sd_bus_message *m) {
2201         _cleanup_bus_message_unref_ sd_bus_message *synthetic_reply = NULL;
2202         _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2203         _cleanup_free_ struct reply_callback *c = NULL;
2204         int r;
2205
2206         assert(bus);
2207         assert(m);
2208
2209         if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
2210             m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
2211                 return 0;
2212
2213         if (bus->is_kernel && (bus->hello_flags & KDBUS_HELLO_MONITOR))
2214                 return 0;
2215
2216         if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
2217                 return 0;
2218
2219         c = hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
2220         if (!c)
2221                 return 0;
2222
2223         if (c->timeout != 0)
2224                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2225
2226         if (m->n_fds > 0 && !(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
2227
2228                 /* If the reply contained a file descriptor which we
2229                  * didn't want we pass an error instead. */
2230
2231                 r = bus_message_new_synthetic_error(
2232                                 bus,
2233                                 m->reply_cookie,
2234                                 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
2235                                 &synthetic_reply);
2236                 if (r < 0)
2237                         return r;
2238
2239                 r = bus_seal_synthetic_message(bus, synthetic_reply);
2240                 if (r < 0)
2241                         return r;
2242
2243                 m = synthetic_reply;
2244         } else {
2245                 r = sd_bus_message_rewind(m, true);
2246                 if (r < 0)
2247                         return r;
2248         }
2249
2250         r = c->callback(bus, m, c->userdata, &error_buffer);
2251         r = bus_maybe_reply_error(m, r, &error_buffer);
2252
2253         return r;
2254 }
2255
2256 static int process_filter(sd_bus *bus, sd_bus_message *m) {
2257         _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2258         struct filter_callback *l;
2259         int r;
2260
2261         assert(bus);
2262         assert(m);
2263
2264         do {
2265                 bus->filter_callbacks_modified = false;
2266
2267                 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2268
2269                         if (bus->filter_callbacks_modified)
2270                                 break;
2271
2272                         /* Don't run this more than once per iteration */
2273                         if (l->last_iteration == bus->iteration_counter)
2274                                 continue;
2275
2276                         l->last_iteration = bus->iteration_counter;
2277
2278                         r = sd_bus_message_rewind(m, true);
2279                         if (r < 0)
2280                                 return r;
2281
2282                         r = l->callback(bus, m, l->userdata, &error_buffer);
2283                         r = bus_maybe_reply_error(m, r, &error_buffer);
2284                         if (r != 0)
2285                                 return r;
2286
2287                 }
2288
2289         } while (bus->filter_callbacks_modified);
2290
2291         return 0;
2292 }
2293
2294 static int process_match(sd_bus *bus, sd_bus_message *m) {
2295         int r;
2296
2297         assert(bus);
2298         assert(m);
2299
2300         do {
2301                 bus->match_callbacks_modified = false;
2302
2303                 r = bus_match_run(bus, &bus->match_callbacks, m);
2304                 if (r != 0)
2305                         return r;
2306
2307         } while (bus->match_callbacks_modified);
2308
2309         return 0;
2310 }
2311
2312 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2313         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2314         int r;
2315
2316         assert(bus);
2317         assert(m);
2318
2319         if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2320                 return 0;
2321
2322         if (bus->manual_peer_interface)
2323                 return 0;
2324
2325         if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2326                 return 0;
2327
2328         if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2329                 return 0;
2330
2331         if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2332                 return 1;
2333
2334         if (streq_ptr(m->member, "Ping"))
2335                 r = sd_bus_message_new_method_return(m, &reply);
2336         else if (streq_ptr(m->member, "GetMachineId")) {
2337                 sd_id128_t id;
2338                 char sid[33];
2339
2340                 r = sd_id128_get_machine(&id);
2341                 if (r < 0)
2342                         return r;
2343
2344                 r = sd_bus_message_new_method_return(m, &reply);
2345                 if (r < 0)
2346                         return r;
2347
2348                 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2349         } else {
2350                 r = sd_bus_message_new_method_errorf(
2351                                 m, &reply,
2352                                 SD_BUS_ERROR_UNKNOWN_METHOD,
2353                                  "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2354         }
2355
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 int process_fd_check(sd_bus *bus, sd_bus_message *m) {
2367         assert(bus);
2368         assert(m);
2369
2370         /* If we got a message with a file descriptor which we didn't
2371          * want to accept, then let's drop it. How can this even
2372          * happen? For example, when the kernel queues a message into
2373          * an activatable names's queue which allows fds, and then is
2374          * delivered to us later even though we ourselves did not
2375          * negotiate it. */
2376
2377         if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2378                 return 0;
2379
2380         if (m->n_fds <= 0)
2381                 return 0;
2382
2383         if (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)
2384                 return 0;
2385
2386         if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2387                 return 1; /* just eat it up */
2388
2389         return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
2390 }
2391
2392 static int process_message(sd_bus *bus, sd_bus_message *m) {
2393         int r;
2394
2395         assert(bus);
2396         assert(m);
2397
2398         bus->current = m;
2399         bus->iteration_counter++;
2400
2401         log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
2402                   bus_message_type_to_string(m->header->type),
2403                   strna(sd_bus_message_get_sender(m)),
2404                   strna(sd_bus_message_get_destination(m)),
2405                   strna(sd_bus_message_get_path(m)),
2406                   strna(sd_bus_message_get_interface(m)),
2407                   strna(sd_bus_message_get_member(m)),
2408                   BUS_MESSAGE_COOKIE(m),
2409                   m->reply_cookie,
2410                   strna(m->error.message));
2411
2412         r = process_hello(bus, m);
2413         if (r != 0)
2414                 goto finish;
2415
2416         r = process_reply(bus, m);
2417         if (r != 0)
2418                 goto finish;
2419
2420         r = process_fd_check(bus, m);
2421         if (r != 0)
2422                 goto finish;
2423
2424         r = process_filter(bus, m);
2425         if (r != 0)
2426                 goto finish;
2427
2428         r = process_match(bus, m);
2429         if (r != 0)
2430                 goto finish;
2431
2432         r = process_builtin(bus, m);
2433         if (r != 0)
2434                 goto finish;
2435
2436         r = bus_process_object(bus, m);
2437
2438 finish:
2439         bus->current = NULL;
2440         return r;
2441 }
2442
2443 static int dispatch_track(sd_bus *bus) {
2444         assert(bus);
2445
2446         if (!bus->track_queue)
2447                 return 0;
2448
2449         bus_track_dispatch(bus->track_queue);
2450         return 1;
2451 }
2452
2453 static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2454         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2455         int r;
2456
2457         assert(bus);
2458         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2459
2460         r = process_timeout(bus);
2461         if (r != 0)
2462                 goto null_message;
2463
2464         r = dispatch_wqueue(bus);
2465         if (r != 0)
2466                 goto null_message;
2467
2468         r = dispatch_track(bus);
2469         if (r != 0)
2470                 goto null_message;
2471
2472         r = dispatch_rqueue(bus, hint_priority, priority, &m);
2473         if (r < 0)
2474                 return r;
2475         if (!m)
2476                 goto null_message;
2477
2478         r = process_message(bus, m);
2479         if (r != 0)
2480                 goto null_message;
2481
2482         if (ret) {
2483                 r = sd_bus_message_rewind(m, true);
2484                 if (r < 0)
2485                         return r;
2486
2487                 *ret = m;
2488                 m = NULL;
2489                 return 1;
2490         }
2491
2492         if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2493
2494                 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2495                           strna(sd_bus_message_get_sender(m)),
2496                           strna(sd_bus_message_get_path(m)),
2497                           strna(sd_bus_message_get_interface(m)),
2498                           strna(sd_bus_message_get_member(m)));
2499
2500                 r = sd_bus_reply_method_errorf(
2501                                 m,
2502                                 SD_BUS_ERROR_UNKNOWN_OBJECT,
2503                                 "Unknown object '%s'.", m->path);
2504                 if (r < 0)
2505                         return r;
2506         }
2507
2508         return 1;
2509
2510 null_message:
2511         if (r >= 0 && ret)
2512                 *ret = NULL;
2513
2514         return r;
2515 }
2516
2517 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2518         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2519         struct reply_callback *c;
2520         int r;
2521
2522         assert(bus);
2523         assert(bus->state == BUS_CLOSING);
2524
2525         c = hashmap_first(bus->reply_callbacks);
2526         if (c) {
2527                 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2528
2529                 /* First, fail all outstanding method calls */
2530                 r = bus_message_new_synthetic_error(
2531                                 bus,
2532                                 c->cookie,
2533                                 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2534                                 &m);
2535                 if (r < 0)
2536                         return r;
2537
2538                 r = bus_seal_synthetic_message(bus, m);
2539                 if (r < 0)
2540                         return r;
2541
2542                 if (c->timeout != 0)
2543                         prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2544
2545                 hashmap_remove(bus->reply_callbacks, &c->cookie);
2546
2547                 bus->current = m;
2548                 bus->iteration_counter++;
2549
2550                 r = c->callback(bus, m, c->userdata, &error_buffer);
2551                 r = bus_maybe_reply_error(m, r, &error_buffer);
2552                 free(c);
2553
2554                 goto finish;
2555         }
2556
2557         /* Then, synthesize a Disconnected message */
2558         r = sd_bus_message_new_signal(
2559                         bus,
2560                         &m,
2561                         "/org/freedesktop/DBus/Local",
2562                         "org.freedesktop.DBus.Local",
2563                         "Disconnected");
2564         if (r < 0)
2565                 return r;
2566
2567         m->sender = "org.freedesktop.DBus.Local";
2568
2569         r = bus_seal_synthetic_message(bus, m);
2570         if (r < 0)
2571                 return r;
2572
2573         sd_bus_close(bus);
2574
2575         bus->current = m;
2576         bus->iteration_counter++;
2577
2578         r = process_filter(bus, m);
2579         if (r != 0)
2580                 goto finish;
2581
2582         r = process_match(bus, m);
2583         if (r != 0)
2584                 goto finish;
2585
2586         if (ret) {
2587                 *ret = m;
2588                 m = NULL;
2589         }
2590
2591         r = 1;
2592
2593 finish:
2594         bus->current = NULL;
2595         return r;
2596 }
2597
2598 static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2599         BUS_DONT_DESTROY(bus);
2600         int r;
2601
2602         /* Returns 0 when we didn't do anything. This should cause the
2603          * caller to invoke sd_bus_wait() before returning the next
2604          * time. Returns > 0 when we did something, which possibly
2605          * means *ret is filled in with an unprocessed message. */
2606
2607         assert_return(bus, -EINVAL);
2608         assert_return(!bus_pid_changed(bus), -ECHILD);
2609
2610         /* We don't allow recursively invoking sd_bus_process(). */
2611         assert_return(!bus->current, -EBUSY);
2612
2613         switch (bus->state) {
2614
2615         case BUS_UNSET:
2616                 return -ENOTCONN;
2617
2618         case BUS_CLOSED:
2619                 return -ECONNRESET;
2620
2621         case BUS_OPENING:
2622                 r = bus_socket_process_opening(bus);
2623                 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2624                         bus_enter_closing(bus);
2625                         r = 1;
2626                 } else if (r < 0)
2627                         return r;
2628                 if (ret)
2629                         *ret = NULL;
2630                 return r;
2631
2632         case BUS_AUTHENTICATING:
2633                 r = bus_socket_process_authenticating(bus);
2634                 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2635                         bus_enter_closing(bus);
2636                         r = 1;
2637                 } else if (r < 0)
2638                         return r;
2639
2640                 if (ret)
2641                         *ret = NULL;
2642
2643                 return r;
2644
2645         case BUS_RUNNING:
2646         case BUS_HELLO:
2647                 r = process_running(bus, hint_priority, priority, ret);
2648                 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2649                         bus_enter_closing(bus);
2650                         r = 1;
2651
2652                         if (ret)
2653                                 *ret = NULL;
2654                 }
2655
2656                 return r;
2657
2658         case BUS_CLOSING:
2659                 return process_closing(bus, ret);
2660         }
2661
2662         assert_not_reached("Unknown state");
2663 }
2664
2665 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2666         return bus_process_internal(bus, false, 0, ret);
2667 }
2668
2669 _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
2670         return bus_process_internal(bus, true, priority, ret);
2671 }
2672
2673 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2674         struct pollfd p[2] = {};
2675         int r, e, n;
2676         struct timespec ts;
2677         usec_t m = (usec_t) -1;
2678
2679         assert(bus);
2680
2681         if (bus->state == BUS_CLOSING)
2682                 return 1;
2683
2684         if (!BUS_IS_OPEN(bus->state))
2685                 return -ENOTCONN;
2686
2687         e = sd_bus_get_events(bus);
2688         if (e < 0)
2689                 return e;
2690
2691         if (need_more)
2692                 /* The caller really needs some more data, he doesn't
2693                  * care about what's already read, or any timeouts
2694                  * except its own.*/
2695                 e |= POLLIN;
2696         else {
2697                 usec_t until;
2698                 /* The caller wants to process if there's something to
2699                  * process, but doesn't care otherwise */
2700
2701                 r = sd_bus_get_timeout(bus, &until);
2702                 if (r < 0)
2703                         return r;
2704                 if (r > 0) {
2705                         usec_t nw;
2706                         nw = now(CLOCK_MONOTONIC);
2707                         m = until > nw ? until - nw : 0;
2708                 }
2709         }
2710
2711         if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2712                 m = timeout_usec;
2713
2714         p[0].fd = bus->input_fd;
2715         if (bus->output_fd == bus->input_fd) {
2716                 p[0].events = e;
2717                 n = 1;
2718         } else {
2719                 p[0].events = e & POLLIN;
2720                 p[1].fd = bus->output_fd;
2721                 p[1].events = e & POLLOUT;
2722                 n = 2;
2723         }
2724
2725         r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2726         if (r < 0)
2727                 return -errno;
2728
2729         return r > 0 ? 1 : 0;
2730 }
2731
2732 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2733
2734         assert_return(bus, -EINVAL);
2735         assert_return(!bus_pid_changed(bus), -ECHILD);
2736
2737         if (bus->state == BUS_CLOSING)
2738                 return 0;
2739
2740         if (!BUS_IS_OPEN(bus->state))
2741                 return -ENOTCONN;
2742
2743         if (bus->rqueue_size > 0)
2744                 return 0;
2745
2746         return bus_poll(bus, false, timeout_usec);
2747 }
2748
2749 _public_ int sd_bus_flush(sd_bus *bus) {
2750         int r;
2751
2752         assert_return(bus, -EINVAL);
2753         assert_return(!bus_pid_changed(bus), -ECHILD);
2754
2755         if (bus->state == BUS_CLOSING)
2756                 return 0;
2757
2758         if (!BUS_IS_OPEN(bus->state))
2759                 return -ENOTCONN;
2760
2761         r = bus_ensure_running(bus);
2762         if (r < 0)
2763                 return r;
2764
2765         if (bus->wqueue_size <= 0)
2766                 return 0;
2767
2768         for (;;) {
2769                 r = dispatch_wqueue(bus);
2770                 if (r < 0) {
2771                         if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2772                                 bus_enter_closing(bus);
2773                                 return -ECONNRESET;
2774                         }
2775
2776                         return r;
2777                 }
2778
2779                 if (bus->wqueue_size <= 0)
2780                         return 0;
2781
2782                 r = bus_poll(bus, false, (uint64_t) -1);
2783                 if (r < 0)
2784                         return r;
2785         }
2786 }
2787
2788 _public_ int sd_bus_add_filter(sd_bus *bus,
2789                                sd_bus_message_handler_t callback,
2790                                void *userdata) {
2791
2792         struct filter_callback *f;
2793
2794         assert_return(bus, -EINVAL);
2795         assert_return(callback, -EINVAL);
2796         assert_return(!bus_pid_changed(bus), -ECHILD);
2797
2798         f = new0(struct filter_callback, 1);
2799         if (!f)
2800                 return -ENOMEM;
2801         f->callback = callback;
2802         f->userdata = userdata;
2803
2804         bus->filter_callbacks_modified = true;
2805         LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2806         return 0;
2807 }
2808
2809 _public_ int sd_bus_remove_filter(sd_bus *bus,
2810                                   sd_bus_message_handler_t callback,
2811                                   void *userdata) {
2812
2813         struct filter_callback *f;
2814
2815         assert_return(bus, -EINVAL);
2816         assert_return(callback, -EINVAL);
2817         assert_return(!bus_pid_changed(bus), -ECHILD);
2818
2819         LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2820                 if (f->callback == callback && f->userdata == userdata) {
2821                         bus->filter_callbacks_modified = true;
2822                         LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2823                         free(f);
2824                         return 1;
2825                 }
2826         }
2827
2828         return 0;
2829 }
2830
2831 _public_ int sd_bus_add_match(sd_bus *bus,
2832                               const char *match,
2833                               sd_bus_message_handler_t callback,
2834                               void *userdata) {
2835
2836         struct bus_match_component *components = NULL;
2837         unsigned n_components = 0;
2838         uint64_t cookie = 0;
2839         int r = 0;
2840
2841         assert_return(bus, -EINVAL);
2842         assert_return(match, -EINVAL);
2843         assert_return(!bus_pid_changed(bus), -ECHILD);
2844
2845         r = bus_match_parse(match, &components, &n_components);
2846         if (r < 0)
2847                 goto finish;
2848
2849         if (bus->bus_client) {
2850                 cookie = ++bus->match_cookie;
2851
2852                 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2853                 if (r < 0)
2854                         goto finish;
2855         }
2856
2857         bus->match_callbacks_modified = true;
2858         r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2859         if (r < 0) {
2860                 if (bus->bus_client)
2861                         bus_remove_match_internal(bus, match, cookie);
2862         }
2863
2864 finish:
2865         bus_match_parse_free(components, n_components);
2866         return r;
2867 }
2868
2869 _public_ int sd_bus_remove_match(sd_bus *bus,
2870                                  const char *match,
2871                                  sd_bus_message_handler_t callback,
2872                                  void *userdata) {
2873
2874         struct bus_match_component *components = NULL;
2875         unsigned n_components = 0;
2876         int r = 0, q = 0;
2877         uint64_t cookie = 0;
2878
2879         assert_return(bus, -EINVAL);
2880         assert_return(match, -EINVAL);
2881         assert_return(!bus_pid_changed(bus), -ECHILD);
2882
2883         r = bus_match_parse(match, &components, &n_components);
2884         if (r < 0)
2885                 return r;
2886
2887         bus->match_callbacks_modified = true;
2888         r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2889
2890         if (bus->bus_client)
2891                 q = bus_remove_match_internal(bus, match, cookie);
2892
2893         bus_match_parse_free(components, n_components);
2894
2895         return r < 0 ? r : q;
2896 }
2897
2898 bool bus_pid_changed(sd_bus *bus) {
2899         assert(bus);
2900
2901         /* We don't support people creating a bus connection and
2902          * keeping it around over a fork(). Let's complain. */
2903
2904         return bus->original_pid != getpid();
2905 }
2906
2907 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2908         sd_bus *bus = userdata;
2909         int r;
2910
2911         assert(bus);
2912
2913         r = sd_bus_process(bus, NULL);
2914         if (r < 0)
2915                 return r;
2916
2917         return 1;
2918 }
2919
2920 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2921         sd_bus *bus = userdata;
2922         int r;
2923
2924         assert(bus);
2925
2926         r = sd_bus_process(bus, NULL);
2927         if (r < 0)
2928                 return r;
2929
2930         return 1;
2931 }
2932
2933 static int prepare_callback(sd_event_source *s, void *userdata) {
2934         sd_bus *bus = userdata;
2935         int r, e;
2936         usec_t until;
2937
2938         assert(s);
2939         assert(bus);
2940
2941         e = sd_bus_get_events(bus);
2942         if (e < 0)
2943                 return e;
2944
2945         if (bus->output_fd != bus->input_fd) {
2946
2947                 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2948                 if (r < 0)
2949                         return r;
2950
2951                 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2952                 if (r < 0)
2953                         return r;
2954         } else {
2955                 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2956                 if (r < 0)
2957                         return r;
2958         }
2959
2960         r = sd_bus_get_timeout(bus, &until);
2961         if (r < 0)
2962                 return r;
2963         if (r > 0) {
2964                 int j;
2965
2966                 j = sd_event_source_set_time(bus->time_event_source, until);
2967                 if (j < 0)
2968                         return j;
2969         }
2970
2971         r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2972         if (r < 0)
2973                 return r;
2974
2975         return 1;
2976 }
2977
2978 static int quit_callback(sd_event_source *event, void *userdata) {
2979         sd_bus *bus = userdata;
2980
2981         assert(event);
2982
2983         sd_bus_flush(bus);
2984
2985         return 1;
2986 }
2987
2988 static int attach_io_events(sd_bus *bus) {
2989         int r;
2990
2991         assert(bus);
2992
2993         if (bus->input_fd < 0)
2994                 return 0;
2995
2996         if (!bus->event)
2997                 return 0;
2998
2999         if (!bus->input_io_event_source) {
3000                 r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
3001                 if (r < 0)
3002                         return r;
3003
3004                 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
3005                 if (r < 0)
3006                         return r;
3007
3008                 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
3009         } else
3010                 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
3011
3012         if (r < 0)
3013                 return r;
3014
3015         if (bus->output_fd != bus->input_fd) {
3016                 assert(bus->output_fd >= 0);
3017
3018                 if (!bus->output_io_event_source) {
3019                         r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
3020                         if (r < 0)
3021                                 return r;
3022
3023                         r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
3024                 } else
3025                         r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
3026
3027                 if (r < 0)
3028                         return r;
3029         }
3030
3031         return 0;
3032 }
3033
3034 static void detach_io_events(sd_bus *bus) {
3035         assert(bus);
3036
3037         if (bus->input_io_event_source) {
3038                 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
3039                 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
3040         }
3041
3042         if (bus->output_io_event_source) {
3043                 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
3044                 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
3045         }
3046 }
3047
3048 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
3049         int r;
3050
3051         assert_return(bus, -EINVAL);
3052         assert_return(!bus->event, -EBUSY);
3053
3054         assert(!bus->input_io_event_source);
3055         assert(!bus->output_io_event_source);
3056         assert(!bus->time_event_source);
3057
3058         if (event)
3059                 bus->event = sd_event_ref(event);
3060         else  {
3061                 r = sd_event_default(&bus->event);
3062                 if (r < 0)
3063                         return r;
3064         }
3065
3066         bus->event_priority = priority;
3067
3068         r = sd_event_add_monotonic(bus->event, &bus->time_event_source, 0, 0, time_callback, bus);
3069         if (r < 0)
3070                 goto fail;
3071
3072         r = sd_event_source_set_priority(bus->time_event_source, priority);
3073         if (r < 0)
3074                 goto fail;
3075
3076         r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
3077         if (r < 0)
3078                 goto fail;
3079
3080         r = attach_io_events(bus);
3081         if (r < 0)
3082                 goto fail;
3083
3084         return 0;
3085
3086 fail:
3087         sd_bus_detach_event(bus);
3088         return r;
3089 }
3090
3091 _public_ int sd_bus_detach_event(sd_bus *bus) {
3092         assert_return(bus, -EINVAL);
3093
3094         if (!bus->event)
3095                 return 0;
3096
3097         detach_io_events(bus);
3098
3099         if (bus->time_event_source) {
3100                 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
3101                 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
3102         }
3103
3104         if (bus->quit_event_source) {
3105                 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
3106                 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
3107         }
3108
3109         if (bus->event)
3110                 bus->event = sd_event_unref(bus->event);
3111
3112         return 1;
3113 }
3114
3115 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
3116         assert_return(bus, NULL);
3117
3118         return bus->event;
3119 }
3120
3121 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
3122         assert_return(bus, NULL);
3123
3124         return bus->current;
3125 }
3126
3127 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
3128         sd_bus *b = NULL;
3129         int r;
3130
3131         assert(bus_open);
3132         assert(default_bus);
3133
3134         if (!ret)
3135                 return !!*default_bus;
3136
3137         if (*default_bus) {
3138                 *ret = sd_bus_ref(*default_bus);
3139                 return 0;
3140         }
3141
3142         r = bus_open(&b);
3143         if (r < 0)
3144                 return r;
3145
3146         b->default_bus_ptr = default_bus;
3147         b->tid = gettid();
3148         *default_bus = b;
3149
3150         *ret = b;
3151         return 1;
3152 }
3153
3154 _public_ int sd_bus_default_system(sd_bus **ret) {
3155         static thread_local sd_bus *default_system_bus = NULL;
3156
3157         return bus_default(sd_bus_open_system, &default_system_bus, ret);
3158 }
3159
3160 _public_ int sd_bus_default_user(sd_bus **ret) {
3161         static thread_local sd_bus *default_user_bus = NULL;
3162
3163         return bus_default(sd_bus_open_user, &default_user_bus, ret);
3164 }
3165
3166 _public_ int sd_bus_default(sd_bus **ret) {
3167
3168         const char *e;
3169
3170         /* Let's try our best to reuse another cached connection. If
3171          * the starter bus type is set, connect via our normal
3172          * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3173          * we can share the connection with the user/system default
3174          * bus. */
3175
3176         e = secure_getenv("DBUS_STARTER_BUS_TYPE");
3177         if (e) {
3178                 if (streq(e, "system"))
3179                         return sd_bus_default_system(ret);
3180                 else if (STR_IN_SET(e, "user", "session"))
3181                         return sd_bus_default_user(ret);
3182         }
3183
3184         /* No type is specified, so we have not other option than to
3185          * use the starter address if it is set. */
3186
3187         e = secure_getenv("DBUS_STARTER_ADDRESS");
3188         if (e) {
3189                 static thread_local sd_bus *default_starter_bus = NULL;
3190
3191                 return bus_default(sd_bus_open, &default_starter_bus, ret);
3192         }
3193
3194         /* Finally, if nothing is set use the cached connection for
3195          * the right scope */
3196
3197         if (cg_pid_get_owner_uid(0, NULL) >= 0)
3198                 return sd_bus_default_user(ret);
3199         else
3200                 return sd_bus_default_system(ret);
3201 }
3202
3203 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
3204         assert_return(b, -EINVAL);
3205         assert_return(tid, -EINVAL);
3206         assert_return(!bus_pid_changed(b), -ECHILD);
3207
3208         if (b->tid != 0) {
3209                 *tid = b->tid;
3210                 return 0;
3211         }
3212
3213         if (b->event)
3214                 return sd_event_get_tid(b->event, tid);
3215
3216         return -ENXIO;
3217 }
3218
3219 _public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
3220         _cleanup_free_ char *e = NULL;
3221         char *ret;
3222
3223         assert_return(object_path_is_valid(prefix), -EINVAL);
3224         assert_return(external_id, -EINVAL);
3225         assert_return(ret_path, -EINVAL);
3226
3227         e = bus_label_escape(external_id);
3228         if (!e)
3229                 return -ENOMEM;
3230
3231         ret = strjoin(prefix, "/", e, NULL);
3232         if (!ret)
3233                 return -ENOMEM;
3234
3235         *ret_path = ret;
3236         return 0;
3237 }
3238
3239 _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
3240         const char *e;
3241         char *ret;
3242
3243         assert_return(object_path_is_valid(path), -EINVAL);
3244         assert_return(object_path_is_valid(prefix), -EINVAL);
3245         assert_return(external_id, -EINVAL);
3246
3247         e = object_path_startswith(path, prefix);
3248         if (!e) {
3249                 *external_id = NULL;
3250                 return 0;
3251         }
3252
3253         ret = bus_label_unescape(e);
3254         if (!ret)
3255                 return -ENOMEM;
3256
3257         *external_id = ret;
3258         return 1;
3259 }
3260
3261 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
3262         sd_bus_creds *c;
3263         pid_t pid = 0;
3264         int r;
3265
3266         assert_return(bus, -EINVAL);
3267         assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
3268         assert_return(ret, -EINVAL);
3269         assert_return(!bus_pid_changed(bus), -ECHILD);
3270
3271         if (!bus->is_kernel)
3272                 return -ENOTSUP;
3273
3274         if (!BUS_IS_OPEN(bus->state))
3275                 return -ENOTCONN;
3276
3277         if (!bus->ucred_valid && !isempty(bus->label))
3278                 return -ENODATA;
3279
3280         c = bus_creds_new();
3281         if (!c)
3282                 return -ENOMEM;
3283
3284         if (bus->ucred_valid) {
3285                 pid = c->pid = bus->ucred.pid;
3286                 c->uid = bus->ucred.uid;
3287                 c->gid = bus->ucred.gid;
3288
3289                 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
3290         }
3291
3292         if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
3293                 c->label = strdup(bus->label);
3294                 if (!c->label) {
3295                         sd_bus_creds_unref(c);
3296                         return -ENOMEM;
3297                 }
3298
3299                 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
3300         }
3301
3302         r = bus_creds_add_more(c, mask, pid, 0);
3303         if (r < 0)
3304                 return r;
3305
3306         *ret = c;
3307         return 0;
3308 }
3309
3310 _public_ int sd_bus_try_close(sd_bus *bus) {
3311         int r;
3312
3313         assert_return(bus, -EINVAL);
3314         assert_return(!bus_pid_changed(bus), -ECHILD);
3315
3316         if (!bus->is_kernel)
3317                 return -ENOTSUP;
3318
3319         if (!BUS_IS_OPEN(bus->state))
3320                 return -ENOTCONN;
3321
3322         if (bus->rqueue_size > 0)
3323                 return -EBUSY;
3324
3325         if (bus->wqueue_size > 0)
3326                 return -EBUSY;
3327
3328         r = bus_kernel_try_close(bus);
3329         if (r < 0)
3330                 return r;
3331
3332         sd_bus_close(bus);
3333         return 0;
3334 }
3335
3336 _public_ int sd_bus_get_name(sd_bus *bus, const char **name) {
3337         assert_return(bus, -EINVAL);
3338         assert_return(name, -EINVAL);
3339         assert_return(!bus_pid_changed(bus), -ECHILD);
3340
3341         *name = bus->connection_name;
3342         return 0;
3343 }