chiark / gitweb /
bus: properly check if kdbus connection worked
[elogind.git] / src / libsystemd-bus / sd-bus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <endian.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <netdb.h>
27 #include <sys/poll.h>
28 #include <byteswap.h>
29
30 #include "util.h"
31 #include "macro.h"
32 #include "strv.h"
33 #include "set.h"
34
35 #include "sd-bus.h"
36 #include "bus-internal.h"
37 #include "bus-message.h"
38 #include "bus-type.h"
39 #include "bus-socket.h"
40 #include "bus-kernel.h"
41 #include "bus-control.h"
42
43 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
44
45 static void bus_free(sd_bus *b) {
46         struct filter_callback *f;
47         struct object_callback *c;
48         unsigned i;
49
50         assert(b);
51
52         sd_bus_close(b);
53
54         free(b->rbuffer);
55         free(b->unique_name);
56         free(b->auth_buffer);
57         free(b->address);
58
59         free(b->exec_path);
60         strv_free(b->exec_argv);
61
62         close_many(b->fds, b->n_fds);
63         free(b->fds);
64
65         for (i = 0; i < b->rqueue_size; i++)
66                 sd_bus_message_unref(b->rqueue[i]);
67         free(b->rqueue);
68
69         for (i = 0; i < b->wqueue_size; i++)
70                 sd_bus_message_unref(b->wqueue[i]);
71         free(b->wqueue);
72
73         hashmap_free_free(b->reply_callbacks);
74         prioq_free(b->reply_callbacks_prioq);
75
76         while ((f = b->filter_callbacks)) {
77                 LIST_REMOVE(struct filter_callback, callbacks, b->filter_callbacks, f);
78                 free(f);
79         }
80
81         while ((c = hashmap_steal_first(b->object_callbacks))) {
82                 free(c->path);
83                 free(c);
84         }
85
86         hashmap_free(b->object_callbacks);
87
88         bus_match_free(&b->match_callbacks);
89
90         free(b);
91 }
92
93 int sd_bus_new(sd_bus **ret) {
94         sd_bus *r;
95
96         if (!ret)
97                 return -EINVAL;
98
99         r = new0(sd_bus, 1);
100         if (!r)
101                 return -ENOMEM;
102
103         r->n_ref = 1;
104         r->input_fd = r->output_fd = -1;
105         r->message_version = 1;
106         r->negotiate_fds = true;
107
108         /* We guarantee that wqueue always has space for at least one
109          * entry */
110         r->wqueue = new(sd_bus_message*, 1);
111         if (!r->wqueue) {
112                 free(r);
113                 return -ENOMEM;
114         }
115
116         *ret = r;
117         return 0;
118 }
119
120 int sd_bus_set_address(sd_bus *bus, const char *address) {
121         char *a;
122
123         if (!bus)
124                 return -EINVAL;
125         if (bus->state != BUS_UNSET)
126                 return -EPERM;
127         if (!address)
128                 return -EINVAL;
129
130         a = strdup(address);
131         if (!a)
132                 return -ENOMEM;
133
134         free(bus->address);
135         bus->address = a;
136
137         return 0;
138 }
139
140 int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
141         if (!bus)
142                 return -EINVAL;
143         if (bus->state != BUS_UNSET)
144                 return -EPERM;
145         if (input_fd < 0)
146                 return -EINVAL;
147         if (output_fd < 0)
148                 return -EINVAL;
149
150         bus->input_fd = input_fd;
151         bus->output_fd = output_fd;
152         return 0;
153 }
154
155 int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
156         char *p, **a;
157
158         if (!bus)
159                 return -EINVAL;
160         if (bus->state != BUS_UNSET)
161                 return -EPERM;
162         if (!path)
163                 return -EINVAL;
164         if (strv_isempty(argv))
165                 return -EINVAL;
166
167         p = strdup(path);
168         if (!p)
169                 return -ENOMEM;
170
171         a = strv_copy(argv);
172         if (!a) {
173                 free(p);
174                 return -ENOMEM;
175         }
176
177         free(bus->exec_path);
178         strv_free(bus->exec_argv);
179
180         bus->exec_path = p;
181         bus->exec_argv = a;
182
183         return 0;
184 }
185
186 int sd_bus_set_bus_client(sd_bus *bus, int b) {
187         if (!bus)
188                 return -EINVAL;
189         if (bus->state != BUS_UNSET)
190                 return -EPERM;
191
192         bus->bus_client = !!b;
193         return 0;
194 }
195
196 int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
197         if (!bus)
198                 return -EINVAL;
199         if (bus->state != BUS_UNSET)
200                 return -EPERM;
201
202         bus->negotiate_fds = !!b;
203         return 0;
204 }
205
206 int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
207         if (!bus)
208                 return -EINVAL;
209         if (!b && !sd_id128_equal(server_id, SD_ID128_NULL))
210                 return -EINVAL;
211         if (bus->state != BUS_UNSET)
212                 return -EPERM;
213
214         bus->is_server = !!b;
215         bus->server_id = server_id;
216         return 0;
217 }
218
219 int sd_bus_set_anonymous(sd_bus *bus, int b) {
220         if (!bus)
221                 return -EINVAL;
222         if (bus->state != BUS_UNSET)
223                 return -EPERM;
224
225         bus->anonymous_auth = !!b;
226         return 0;
227 }
228
229 static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) {
230         const char *s;
231         int r;
232
233         assert(bus);
234         assert(bus->state == BUS_HELLO);
235
236         if (error != 0)
237                 return -error;
238
239         assert(reply);
240
241         r = sd_bus_message_read(reply, "s", &s);
242         if (r < 0)
243                 return r;
244
245         if (!service_name_is_valid(s) || s[0] != ':')
246                 return -EBADMSG;
247
248         bus->unique_name = strdup(s);
249         if (!bus->unique_name)
250                 return -ENOMEM;
251
252         bus->state = BUS_RUNNING;
253
254         return 1;
255 }
256
257 static int bus_send_hello(sd_bus *bus) {
258         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
259         int r;
260
261         assert(bus);
262
263         if (!bus->bus_client || bus->is_kernel)
264                 return 0;
265
266         r = sd_bus_message_new_method_call(
267                         bus,
268                         "org.freedesktop.DBus",
269                         "/",
270                         "org.freedesktop.DBus",
271                         "Hello",
272                         &m);
273         if (r < 0)
274                 return r;
275
276         return sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
277 }
278
279 int bus_start_running(sd_bus *bus) {
280         assert(bus);
281
282         if (bus->bus_client) {
283                 bus->state = BUS_HELLO;
284                 return 1;
285         }
286
287         bus->state = BUS_RUNNING;
288         return 1;
289 }
290
291 static int parse_address_key(const char **p, const char *key, char **value) {
292         size_t l, n = 0;
293         const char *a;
294         char *r = NULL;
295
296         assert(p);
297         assert(*p);
298         assert(value);
299
300         if (key) {
301                 l = strlen(key);
302                 if (strncmp(*p, key, l) != 0)
303                         return 0;
304
305                 if ((*p)[l] != '=')
306                         return 0;
307
308                 if (*value)
309                         return -EINVAL;
310
311                 a = *p + l + 1;
312         } else
313                 a = *p;
314
315         while (*a != ';' && *a != ',' && *a != 0) {
316                 char c, *t;
317
318                 if (*a == '%') {
319                         int x, y;
320
321                         x = unhexchar(a[1]);
322                         if (x < 0) {
323                                 free(r);
324                                 return x;
325                         }
326
327                         y = unhexchar(a[2]);
328                         if (y < 0) {
329                                 free(r);
330                                 return y;
331                         }
332
333                         c = (char) ((x << 4) | y);
334                         a += 3;
335                 } else {
336                         c = *a;
337                         a++;
338                 }
339
340                 t = realloc(r, n + 2);
341                 if (!t) {
342                         free(r);
343                         return -ENOMEM;
344                 }
345
346                 r = t;
347                 r[n++] = c;
348         }
349
350         if (!r) {
351                 r = strdup("");
352                 if (!r)
353                         return -ENOMEM;
354         } else
355                 r[n] = 0;
356
357         if (*a == ',')
358                 a++;
359
360         *p = a;
361
362         free(*value);
363         *value = r;
364
365         return 1;
366 }
367
368 static void skip_address_key(const char **p) {
369         assert(p);
370         assert(*p);
371
372         *p += strcspn(*p, ",");
373
374         if (**p == ',')
375                 (*p) ++;
376 }
377
378 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
379         _cleanup_free_ char *path = NULL, *abstract = NULL;
380         size_t l;
381         int r;
382
383         assert(b);
384         assert(p);
385         assert(*p);
386         assert(guid);
387
388         while (**p != 0 && **p != ';') {
389                 r = parse_address_key(p, "guid", guid);
390                 if (r < 0)
391                         return r;
392                 else if (r > 0)
393                         continue;
394
395                 r = parse_address_key(p, "path", &path);
396                 if (r < 0)
397                         return r;
398                 else if (r > 0)
399                         continue;
400
401                 r = parse_address_key(p, "abstract", &abstract);
402                 if (r < 0)
403                         return r;
404                 else if (r > 0)
405                         continue;
406
407                 skip_address_key(p);
408         }
409
410         if (!path && !abstract)
411                 return -EINVAL;
412
413         if (path && abstract)
414                 return -EINVAL;
415
416         if (path) {
417                 l = strlen(path);
418                 if (l > sizeof(b->sockaddr.un.sun_path))
419                         return -E2BIG;
420
421                 b->sockaddr.un.sun_family = AF_UNIX;
422                 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
423                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
424         } else if (abstract) {
425                 l = strlen(abstract);
426                 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
427                         return -E2BIG;
428
429                 b->sockaddr.un.sun_family = AF_UNIX;
430                 b->sockaddr.un.sun_path[0] = 0;
431                 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
432                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
433         }
434
435         return 0;
436 }
437
438 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
439         _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
440         int r;
441         struct addrinfo *result, hints = {
442                 .ai_socktype = SOCK_STREAM,
443                 .ai_flags = AI_ADDRCONFIG,
444         };
445
446         assert(b);
447         assert(p);
448         assert(*p);
449         assert(guid);
450
451         while (**p != 0 && **p != ';') {
452                 r = parse_address_key(p, "guid", guid);
453                 if (r < 0)
454                         return r;
455                 else if (r > 0)
456                         continue;
457
458                 r = parse_address_key(p, "host", &host);
459                 if (r < 0)
460                         return r;
461                 else if (r > 0)
462                         continue;
463
464                 r = parse_address_key(p, "port", &port);
465                 if (r < 0)
466                         return r;
467                 else if (r > 0)
468                         continue;
469
470                 r = parse_address_key(p, "family", &family);
471                 if (r < 0)
472                         return r;
473                 else if (r > 0)
474                         continue;
475
476                 skip_address_key(p);
477         }
478
479         if (!host || !port)
480                 return -EINVAL;
481
482         if (family) {
483                 if (streq(family, "ipv4"))
484                         hints.ai_family = AF_INET;
485                 else if (streq(family, "ipv6"))
486                         hints.ai_family = AF_INET6;
487                 else
488                         return -EINVAL;
489         }
490
491         r = getaddrinfo(host, port, &hints, &result);
492         if (r == EAI_SYSTEM)
493                 return -errno;
494         else if (r != 0)
495                 return -EADDRNOTAVAIL;
496
497         memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
498         b->sockaddr_size = result->ai_addrlen;
499
500         freeaddrinfo(result);
501
502         return 0;
503 }
504
505 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
506         char *path = NULL;
507         unsigned n_argv = 0, j;
508         char **argv = NULL;
509         int r;
510
511         assert(b);
512         assert(p);
513         assert(*p);
514         assert(guid);
515
516         while (**p != 0 && **p != ';') {
517                 r = parse_address_key(p, "guid", guid);
518                 if (r < 0)
519                         goto fail;
520                 else if (r > 0)
521                         continue;
522
523                 r = parse_address_key(p, "path", &path);
524                 if (r < 0)
525                         goto fail;
526                 else if (r > 0)
527                         continue;
528
529                 if (startswith(*p, "argv")) {
530                         unsigned ul;
531
532                         errno = 0;
533                         ul = strtoul(*p + 4, (char**) p, 10);
534                         if (errno > 0 || **p != '=' || ul > 256) {
535                                 r = -EINVAL;
536                                 goto fail;
537                         }
538
539                         (*p) ++;
540
541                         if (ul >= n_argv) {
542                                 char **x;
543
544                                 x = realloc(argv, sizeof(char*) * (ul + 2));
545                                 if (!x) {
546                                         r = -ENOMEM;
547                                         goto fail;
548                                 }
549
550                                 memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
551
552                                 argv = x;
553                                 n_argv = ul + 1;
554                         }
555
556                         r = parse_address_key(p, NULL, argv + ul);
557                         if (r < 0)
558                                 goto fail;
559
560                         continue;
561                 }
562
563                 skip_address_key(p);
564         }
565
566         if (!path) {
567                 r = -EINVAL;
568                 goto fail;
569         }
570
571         /* Make sure there are no holes in the array, with the
572          * exception of argv[0] */
573         for (j = 1; j < n_argv; j++)
574                 if (!argv[j]) {
575                         r = -EINVAL;
576                         goto fail;
577                 }
578
579         if (argv && argv[0] == NULL) {
580                 argv[0] = strdup(path);
581                 if (!argv[0]) {
582                         r = -ENOMEM;
583                         goto fail;
584                 }
585         }
586
587         b->exec_path = path;
588         b->exec_argv = argv;
589         return 0;
590
591 fail:
592         for (j = 0; j < n_argv; j++)
593                 free(argv[j]);
594
595         free(argv);
596         free(path);
597         return r;
598 }
599
600 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
601         _cleanup_free_ char *path = NULL;
602         int r;
603
604         assert(b);
605         assert(p);
606         assert(*p);
607         assert(guid);
608
609         while (**p != 0 && **p != ';') {
610                 r = parse_address_key(p, "guid", guid);
611                 if (r < 0)
612                         return r;
613                 else if (r > 0)
614                         continue;
615
616                 r = parse_address_key(p, "path", &path);
617                 if (r < 0)
618                         return r;
619                 else if (r > 0)
620                         continue;
621
622                 skip_address_key(p);
623         }
624
625         if (!path)
626                 return -EINVAL;
627
628         free(b->kernel);
629         b->kernel = path;
630         path = NULL;
631
632         return 0;
633 }
634
635 static void bus_reset_parsed_address(sd_bus *b) {
636         assert(b);
637
638         zero(b->sockaddr);
639         b->sockaddr_size = 0;
640         strv_free(b->exec_argv);
641         free(b->exec_path);
642         b->exec_path = NULL;
643         b->exec_argv = NULL;
644         b->server_id = SD_ID128_NULL;
645         free(b->kernel);
646         b->kernel = NULL;
647 }
648
649 static int bus_parse_next_address(sd_bus *b) {
650         _cleanup_free_ char *guid = NULL;
651         const char *a;
652         int r;
653
654         assert(b);
655
656         if (!b->address)
657                 return 0;
658         if (b->address[b->address_index] == 0)
659                 return 0;
660
661         bus_reset_parsed_address(b);
662
663         a = b->address + b->address_index;
664
665         while (*a != 0) {
666
667                 if (*a == ';') {
668                         a++;
669                         continue;
670                 }
671
672                 if (startswith(a, "unix:")) {
673                         a += 5;
674
675                         r = parse_unix_address(b, &a, &guid);
676                         if (r < 0)
677                                 return r;
678                         break;
679
680                 } else if (startswith(a, "tcp:")) {
681
682                         a += 4;
683                         r = parse_tcp_address(b, &a, &guid);
684                         if (r < 0)
685                                 return r;
686
687                         break;
688
689                 } else if (startswith(a, "unixexec:")) {
690
691                         a += 9;
692                         r = parse_exec_address(b, &a, &guid);
693                         if (r < 0)
694                                 return r;
695
696                         break;
697
698                 } else if (startswith(a, "kernel:")) {
699
700                         a += 7;
701                         r = parse_kernel_address(b, &a, &guid);
702                         if (r < 0)
703                                 return r;
704
705                         break;
706                 }
707
708                 a = strchr(a, ';');
709                 if (!a)
710                         return 0;
711         }
712
713         if (guid) {
714                 r = sd_id128_from_string(guid, &b->server_id);
715                 if (r < 0)
716                         return r;
717         }
718
719         b->address_index = a - b->address;
720         return 1;
721 }
722
723 static int bus_start_address(sd_bus *b) {
724         int r;
725
726         assert(b);
727
728         for (;;) {
729                 sd_bus_close(b);
730
731                 if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
732
733                         r = bus_socket_connect(b);
734                         if (r >= 0)
735                                 return r;
736
737                         b->last_connect_error = -r;
738
739                 } else if (b->exec_path) {
740
741                         r = bus_socket_exec(b);
742                         if (r >= 0)
743                                 return r;
744
745                         b->last_connect_error = -r;
746                 } else if (b->kernel) {
747
748                         r = bus_kernel_connect(b);
749                         if (r >= 0)
750                                 return r;
751
752                         b->last_connect_error = -r;
753                 }
754
755                 r = bus_parse_next_address(b);
756                 if (r < 0)
757                         return r;
758                 if (r == 0)
759                         return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
760         }
761 }
762
763 int bus_next_address(sd_bus *b) {
764         assert(b);
765
766         bus_reset_parsed_address(b);
767         return bus_start_address(b);
768 }
769
770 static int bus_start_fd(sd_bus *b) {
771         struct stat st;
772         int r;
773
774         assert(b);
775         assert(b->input_fd >= 0);
776         assert(b->output_fd >= 0);
777
778         r = fd_nonblock(b->input_fd, true);
779         if (r < 0)
780                 return r;
781
782         r = fd_cloexec(b->input_fd, true);
783         if (r < 0)
784                 return r;
785
786         if (b->input_fd != b->output_fd) {
787                 r = fd_nonblock(b->output_fd, true);
788                 if (r < 0)
789                         return r;
790
791                 r = fd_cloexec(b->output_fd, true);
792                 if (r < 0)
793                         return r;
794         }
795
796         if (fstat(b->input_fd, &st) < 0)
797                 return -errno;
798
799         if (S_ISCHR(b->input_fd))
800                 return bus_kernel_take_fd(b);
801         else
802                 return bus_socket_take_fd(b);
803 }
804
805 int sd_bus_start(sd_bus *bus) {
806         int r;
807
808         if (!bus)
809                 return -EINVAL;
810         if (bus->state != BUS_UNSET)
811                 return -EPERM;
812
813         bus->state = BUS_OPENING;
814
815         if (bus->is_server && bus->bus_client)
816                 return -EINVAL;
817
818         if (bus->input_fd >= 0)
819                 r = bus_start_fd(bus);
820         else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel)
821                 r = bus_start_address(bus);
822         else
823                 return -EINVAL;
824
825         if (r < 0)
826                 return r;
827
828         return bus_send_hello(bus);
829 }
830
831 int sd_bus_open_system(sd_bus **ret) {
832         const char *e;
833         sd_bus *b;
834         int r;
835
836         if (!ret)
837                 return -EINVAL;
838
839         r = sd_bus_new(&b);
840         if (r < 0)
841                 return r;
842
843         e = getenv("DBUS_SYSTEM_BUS_ADDRESS");
844         if (e) {
845                 r = sd_bus_set_address(b, e);
846                 if (r < 0)
847                         goto fail;
848         } else {
849                 b->sockaddr.un.sun_family = AF_UNIX;
850                 strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
851                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
852         }
853
854         b->bus_client = true;
855
856         r = sd_bus_start(b);
857         if (r < 0)
858                 goto fail;
859
860         *ret = b;
861         return 0;
862
863 fail:
864         bus_free(b);
865         return r;
866 }
867
868 int sd_bus_open_user(sd_bus **ret) {
869         const char *e;
870         sd_bus *b;
871         size_t l;
872         int r;
873
874         if (!ret)
875                 return -EINVAL;
876
877         r = sd_bus_new(&b);
878         if (r < 0)
879                 return r;
880
881         e = getenv("DBUS_SESSION_BUS_ADDRESS");
882         if (e) {
883                 r = sd_bus_set_address(b, e);
884                 if (r < 0)
885                         goto fail;
886         } else {
887                 e = getenv("XDG_RUNTIME_DIR");
888                 if (!e) {
889                         r = -ENOENT;
890                         goto fail;
891                 }
892
893                 l = strlen(e);
894                 if (l + 4 > sizeof(b->sockaddr.un.sun_path)) {
895                         r = -E2BIG;
896                         goto fail;
897                 }
898
899                 b->sockaddr.un.sun_family = AF_UNIX;
900                 memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
901                 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
902         }
903
904         b->bus_client = true;
905
906         r = sd_bus_start(b);
907         if (r < 0)
908                 goto fail;
909
910         *ret = b;
911         return 0;
912
913 fail:
914         bus_free(b);
915         return r;
916 }
917
918 void sd_bus_close(sd_bus *bus) {
919         if (!bus)
920                 return;
921
922         if (bus->input_fd >= 0)
923                 close_nointr_nofail(bus->input_fd);
924         if (bus->output_fd >= 0 && bus->output_fd != bus->input_fd)
925                 close_nointr_nofail(bus->output_fd);
926
927         bus->input_fd = bus->output_fd = -1;
928 }
929
930 sd_bus *sd_bus_ref(sd_bus *bus) {
931         if (!bus)
932                 return NULL;
933
934         assert(bus->n_ref > 0);
935
936         bus->n_ref++;
937         return bus;
938 }
939
940 sd_bus *sd_bus_unref(sd_bus *bus) {
941         if (!bus)
942                 return NULL;
943
944         assert(bus->n_ref > 0);
945         bus->n_ref--;
946
947         if (bus->n_ref <= 0)
948                 bus_free(bus);
949
950         return NULL;
951 }
952
953 int sd_bus_is_open(sd_bus *bus) {
954         if (!bus)
955                 return -EINVAL;
956
957         return bus->state != BUS_UNSET && bus->input_fd >= 0;
958 }
959
960 int sd_bus_can_send(sd_bus *bus, char type) {
961         int r;
962
963         if (!bus)
964                 return -EINVAL;
965         if (bus->output_fd < 0)
966                 return -ENOTCONN;
967
968         if (type == SD_BUS_TYPE_UNIX_FD) {
969                 if (!bus->negotiate_fds)
970                         return 0;
971
972                 r = bus_ensure_running(bus);
973                 if (r < 0)
974                         return r;
975
976                 return bus->can_fds;
977         }
978
979         return bus_type_is_valid(type);
980 }
981
982 int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
983         int r;
984
985         if (!bus)
986                 return -EINVAL;
987         if (!server_id)
988                 return -EINVAL;
989
990         r = bus_ensure_running(bus);
991         if (r < 0)
992                 return r;
993
994         *server_id = bus->server_id;
995         return 0;
996 }
997
998 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
999         assert(m);
1000
1001         if (m->header->version > b->message_version)
1002                 return -EPERM;
1003
1004         if (m->sealed)
1005                 return 0;
1006
1007         return bus_message_seal(m, ++b->serial);
1008 }
1009
1010 static int dispatch_wqueue(sd_bus *bus) {
1011         int r, ret = 0;
1012
1013         assert(bus);
1014         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1015
1016         if (bus->output_fd < 0)
1017                 return -ENOTCONN;
1018
1019         while (bus->wqueue_size > 0) {
1020
1021                 if (bus->is_kernel)
1022                         r = bus_kernel_write_message(bus, bus->wqueue[0]);
1023                 else
1024                         r = bus_socket_write_message(bus, bus->wqueue[0], &bus->windex);
1025
1026                 if (r < 0) {
1027                         sd_bus_close(bus);
1028                         return r;
1029                 } else if (r == 0)
1030                         /* Didn't do anything this time */
1031                         return ret;
1032                 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1033                         /* Fully written. Let's drop the entry from
1034                          * the queue.
1035                          *
1036                          * This isn't particularly optimized, but
1037                          * well, this is supposed to be our worst-case
1038                          * buffer only, and the socket buffer is
1039                          * supposed to be our primary buffer, and if
1040                          * it got full, then all bets are off
1041                          * anyway. */
1042
1043                         sd_bus_message_unref(bus->wqueue[0]);
1044                         bus->wqueue_size --;
1045                         memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1046                         bus->windex = 0;
1047
1048                         ret = 1;
1049                 }
1050         }
1051
1052         return ret;
1053 }
1054
1055 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1056         sd_bus_message *z = NULL;
1057         int r, ret = 0;
1058
1059         assert(bus);
1060         assert(m);
1061         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1062
1063         if (bus->input_fd < 0)
1064                 return -ENOTCONN;
1065
1066         if (bus->rqueue_size > 0) {
1067                 /* Dispatch a queued message */
1068
1069                 *m = bus->rqueue[0];
1070                 bus->rqueue_size --;
1071                 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1072                 return 1;
1073         }
1074
1075         /* Try to read a new message */
1076         do {
1077                 if (bus->is_kernel)
1078                         r = bus_kernel_read_message(bus, &z);
1079                 else
1080                         r = bus_socket_read_message(bus, &z);
1081
1082                 if (r < 0) {
1083                         sd_bus_close(bus);
1084                         return r;
1085                 }
1086                 if (r == 0)
1087                         return ret;
1088
1089                 r = 1;
1090         } while (!z);
1091
1092         *m = z;
1093         return 1;
1094 }
1095
1096 int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1097         int r;
1098
1099         if (!bus)
1100                 return -EINVAL;
1101         if (bus->state == BUS_UNSET)
1102                 return -ENOTCONN;
1103         if (bus->output_fd < 0)
1104                 return -ENOTCONN;
1105         if (!m)
1106                 return -EINVAL;
1107
1108         if (m->n_fds > 0) {
1109                 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1110                 if (r < 0)
1111                         return r;
1112                 if (r == 0)
1113                         return -ENOTSUP;
1114         }
1115
1116         /* If the serial number isn't kept, then we know that no reply
1117          * is expected */
1118         if (!serial && !m->sealed)
1119                 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1120
1121         r = bus_seal_message(bus, m);
1122         if (r < 0)
1123                 return r;
1124
1125         /* If this is a reply and no reply was requested, then let's
1126          * suppress this, if we can */
1127         if (m->dont_send && !serial)
1128                 return 0;
1129
1130         if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1131                 size_t idx = 0;
1132
1133                 if (bus->is_kernel)
1134                         r = bus_kernel_write_message(bus, m);
1135                 else
1136                         r = bus_socket_write_message(bus, m, &idx);
1137
1138                 if (r < 0) {
1139                         sd_bus_close(bus);
1140                         return r;
1141                 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m))  {
1142                         /* Wasn't fully written. So let's remember how
1143                          * much was written. Note that the first entry
1144                          * of the wqueue array is always allocated so
1145                          * that we always can remember how much was
1146                          * written. */
1147                         bus->wqueue[0] = sd_bus_message_ref(m);
1148                         bus->wqueue_size = 1;
1149                         bus->windex = idx;
1150                 }
1151         } else {
1152                 sd_bus_message **q;
1153
1154                 /* Just append it to the queue. */
1155
1156                 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1157                         return -ENOBUFS;
1158
1159                 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1160                 if (!q)
1161                         return -ENOMEM;
1162
1163                 bus->wqueue = q;
1164                 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1165         }
1166
1167         if (serial)
1168                 *serial = BUS_MESSAGE_SERIAL(m);
1169
1170         return 0;
1171 }
1172
1173 static usec_t calc_elapse(uint64_t usec) {
1174         if (usec == (uint64_t) -1)
1175                 return 0;
1176
1177         if (usec == 0)
1178                 usec = BUS_DEFAULT_TIMEOUT;
1179
1180         return now(CLOCK_MONOTONIC) + usec;
1181 }
1182
1183 static int timeout_compare(const void *a, const void *b) {
1184         const struct reply_callback *x = a, *y = b;
1185
1186         if (x->timeout != 0 && y->timeout == 0)
1187                 return -1;
1188
1189         if (x->timeout == 0 && y->timeout != 0)
1190                 return 1;
1191
1192         if (x->timeout < y->timeout)
1193                 return -1;
1194
1195         if (x->timeout > y->timeout)
1196                 return 1;
1197
1198         return 0;
1199 }
1200
1201 int sd_bus_send_with_reply(
1202                 sd_bus *bus,
1203                 sd_bus_message *m,
1204                 sd_bus_message_handler_t callback,
1205                 void *userdata,
1206                 uint64_t usec,
1207                 uint64_t *serial) {
1208
1209         struct reply_callback *c;
1210         int r;
1211
1212         if (!bus)
1213                 return -EINVAL;
1214         if (bus->state == BUS_UNSET)
1215                 return -ENOTCONN;
1216         if (bus->output_fd < 0)
1217                 return -ENOTCONN;
1218         if (!m)
1219                 return -EINVAL;
1220         if (!callback)
1221                 return -EINVAL;
1222         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1223                 return -EINVAL;
1224         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1225                 return -EINVAL;
1226
1227         r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1228         if (r < 0)
1229                 return r;
1230
1231         if (usec != (uint64_t) -1) {
1232                 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1233                 if (r < 0)
1234                         return r;
1235         }
1236
1237         r = bus_seal_message(bus, m);
1238         if (r < 0)
1239                 return r;
1240
1241         c = new0(struct reply_callback, 1);
1242         if (!c)
1243                 return -ENOMEM;
1244
1245         c->callback = callback;
1246         c->userdata = userdata;
1247         c->serial = BUS_MESSAGE_SERIAL(m);
1248         c->timeout = calc_elapse(usec);
1249
1250         r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1251         if (r < 0) {
1252                 free(c);
1253                 return r;
1254         }
1255
1256         if (c->timeout != 0) {
1257                 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1258                 if (r < 0) {
1259                         c->timeout = 0;
1260                         sd_bus_send_with_reply_cancel(bus, c->serial);
1261                         return r;
1262                 }
1263         }
1264
1265         r = sd_bus_send(bus, m, serial);
1266         if (r < 0) {
1267                 sd_bus_send_with_reply_cancel(bus, c->serial);
1268                 return r;
1269         }
1270
1271         return r;
1272 }
1273
1274 int sd_bus_send_with_reply_cancel(sd_bus *bus, uint64_t serial) {
1275         struct reply_callback *c;
1276
1277         if (!bus)
1278                 return -EINVAL;
1279         if (serial == 0)
1280                 return -EINVAL;
1281
1282         c = hashmap_remove(bus->reply_callbacks, &serial);
1283         if (!c)
1284                 return 0;
1285
1286         if (c->timeout != 0)
1287                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1288
1289         free(c);
1290         return 1;
1291 }
1292
1293 int bus_ensure_running(sd_bus *bus) {
1294         int r;
1295
1296         assert(bus);
1297
1298         if (bus->input_fd < 0)
1299                 return -ENOTCONN;
1300         if (bus->state == BUS_UNSET)
1301                 return -ENOTCONN;
1302
1303         if (bus->state == BUS_RUNNING)
1304                 return 1;
1305
1306         for (;;) {
1307                 r = sd_bus_process(bus, NULL);
1308                 if (r < 0)
1309                         return r;
1310                 if (bus->state == BUS_RUNNING)
1311                         return 1;
1312                 if (r > 0)
1313                         continue;
1314
1315                 r = sd_bus_wait(bus, (uint64_t) -1);
1316                 if (r < 0)
1317                         return r;
1318         }
1319 }
1320
1321 int sd_bus_send_with_reply_and_block(
1322                 sd_bus *bus,
1323                 sd_bus_message *m,
1324                 uint64_t usec,
1325                 sd_bus_error *error,
1326                 sd_bus_message **reply) {
1327
1328         int r;
1329         usec_t timeout;
1330         uint64_t serial;
1331         bool room = false;
1332
1333         if (!bus)
1334                 return -EINVAL;
1335         if (bus->output_fd < 0)
1336                 return -ENOTCONN;
1337         if (bus->state == BUS_UNSET)
1338                 return -ENOTCONN;
1339         if (!m)
1340                 return -EINVAL;
1341         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1342                 return -EINVAL;
1343         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1344                 return -EINVAL;
1345         if (bus_error_is_dirty(error))
1346                 return -EINVAL;
1347
1348         r = bus_ensure_running(bus);
1349         if (r < 0)
1350                 return r;
1351
1352         r = sd_bus_send(bus, m, &serial);
1353         if (r < 0)
1354                 return r;
1355
1356         timeout = calc_elapse(usec);
1357
1358         for (;;) {
1359                 usec_t left;
1360                 sd_bus_message *incoming = NULL;
1361
1362                 if (!room) {
1363                         sd_bus_message **q;
1364
1365                         if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1366                                 return -ENOBUFS;
1367
1368                         /* Make sure there's room for queuing this
1369                          * locally, before we read the message */
1370
1371                         q = realloc(bus->rqueue, (bus->rqueue_size + 1) * sizeof(sd_bus_message*));
1372                         if (!q)
1373                                 return -ENOMEM;
1374
1375                         bus->rqueue = q;
1376                         room = true;
1377                 }
1378
1379                 if (bus->is_kernel)
1380                         r = bus_kernel_read_message(bus, &incoming);
1381                 else
1382                         r = bus_socket_read_message(bus, &incoming);
1383                 if (r < 0)
1384                         return r;
1385                 if (incoming) {
1386
1387                         if (incoming->reply_serial == serial) {
1388                                 /* Found a match! */
1389
1390                                 if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_RETURN) {
1391
1392                                         if (reply)
1393                                                 *reply = incoming;
1394                                         else
1395                                                 sd_bus_message_unref(incoming);
1396
1397                                         return 0;
1398                                 }
1399
1400                                 if (incoming->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR) {
1401                                         int k;
1402
1403                                         r = sd_bus_error_copy(error, &incoming->error);
1404                                         if (r < 0) {
1405                                                 sd_bus_message_unref(incoming);
1406                                                 return r;
1407                                         }
1408
1409                                         k = bus_error_to_errno(&incoming->error);
1410                                         sd_bus_message_unref(incoming);
1411                                         return k;
1412                                 }
1413
1414                                 sd_bus_message_unref(incoming);
1415                                 return -EIO;
1416                         }
1417
1418                         /* There's already guaranteed to be room for
1419                          * this, so need to resize things here */
1420                         bus->rqueue[bus->rqueue_size ++] = incoming;
1421                         room = false;
1422
1423                         /* Try to read more, right-away */
1424                         continue;
1425                 }
1426                 if (r != 0)
1427                         continue;
1428
1429                 if (timeout > 0) {
1430                         usec_t n;
1431
1432                         n = now(CLOCK_MONOTONIC);
1433                         if (n >= timeout)
1434                                 return -ETIMEDOUT;
1435
1436                         left = timeout - n;
1437                 } else
1438                         left = (uint64_t) -1;
1439
1440                 r = bus_poll(bus, true, left);
1441                 if (r < 0)
1442                         return r;
1443
1444                 r = dispatch_wqueue(bus);
1445                 if (r < 0)
1446                         return r;
1447         }
1448 }
1449
1450 int sd_bus_get_fd(sd_bus *bus) {
1451         if (!bus)
1452                 return -EINVAL;
1453         if (bus->input_fd < 0)
1454                 return -ENOTCONN;
1455         if (bus->input_fd != bus->output_fd)
1456                 return -EPERM;
1457
1458         return bus->input_fd;
1459 }
1460
1461 int sd_bus_get_events(sd_bus *bus) {
1462         int flags = 0;
1463
1464         if (!bus)
1465                 return -EINVAL;
1466         if (bus->state == BUS_UNSET)
1467                 return -ENOTCONN;
1468         if (bus->input_fd < 0)
1469                 return -ENOTCONN;
1470
1471         if (bus->state == BUS_OPENING)
1472                 flags |= POLLOUT;
1473         else if (bus->state == BUS_AUTHENTICATING) {
1474
1475                 if (bus_socket_auth_needs_write(bus))
1476                         flags |= POLLOUT;
1477
1478                 flags |= POLLIN;
1479
1480         } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1481                 if (bus->rqueue_size <= 0)
1482                         flags |= POLLIN;
1483                 if (bus->wqueue_size > 0)
1484                         flags |= POLLOUT;
1485         }
1486
1487         return flags;
1488 }
1489
1490 int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1491         struct reply_callback *c;
1492
1493         if (!bus)
1494                 return -EINVAL;
1495         if (!timeout_usec)
1496                 return -EINVAL;
1497         if (bus->state == BUS_UNSET)
1498                 return -ENOTCONN;
1499         if (bus->input_fd < 0)
1500                 return -ENOTCONN;
1501
1502         if (bus->state == BUS_AUTHENTICATING) {
1503                 *timeout_usec = bus->auth_timeout;
1504                 return 1;
1505         }
1506
1507         if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1508                 *timeout_usec = (uint64_t) -1;
1509                 return 0;
1510         }
1511
1512         c = prioq_peek(bus->reply_callbacks_prioq);
1513         if (!c) {
1514                 *timeout_usec = (uint64_t) -1;
1515                 return 0;
1516         }
1517
1518         *timeout_usec = c->timeout;
1519         return 1;
1520 }
1521
1522 static int process_timeout(sd_bus *bus) {
1523         struct reply_callback *c;
1524         usec_t n;
1525         int r;
1526
1527         assert(bus);
1528
1529         c = prioq_peek(bus->reply_callbacks_prioq);
1530         if (!c)
1531                 return 0;
1532
1533         n = now(CLOCK_MONOTONIC);
1534         if (c->timeout > n)
1535                 return 0;
1536
1537         assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1538         hashmap_remove(bus->reply_callbacks, &c->serial);
1539
1540         r = c->callback(bus, ETIMEDOUT, NULL, c->userdata);
1541         free(c);
1542
1543         return r < 0 ? r : 1;
1544 }
1545
1546 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1547         assert(bus);
1548         assert(m);
1549
1550         if (bus->state != BUS_HELLO)
1551                 return 0;
1552
1553         /* Let's make sure the first message on the bus is the HELLO
1554          * reply. But note that we don't actually parse the message
1555          * here (we leave that to the usual handling), we just verify
1556          * we don't let any earlier msg through. */
1557
1558         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
1559             m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
1560                 return -EIO;
1561
1562         if (m->reply_serial != bus->hello_serial)
1563                 return -EIO;
1564
1565         return 0;
1566 }
1567
1568 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1569         struct reply_callback *c;
1570         int r;
1571
1572         assert(bus);
1573         assert(m);
1574
1575         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
1576             m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
1577                 return 0;
1578
1579         c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1580         if (!c)
1581                 return 0;
1582
1583         if (c->timeout != 0)
1584                 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1585
1586         r = c->callback(bus, 0, m, c->userdata);
1587         free(c);
1588
1589         return r;
1590 }
1591
1592 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1593         struct filter_callback *l;
1594         int r;
1595
1596         assert(bus);
1597         assert(m);
1598
1599         do {
1600                 bus->filter_callbacks_modified = false;
1601
1602                 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1603
1604                         if (bus->filter_callbacks_modified)
1605                                 break;
1606
1607                         /* Don't run this more than once per iteration */
1608                         if (l->last_iteration == bus->iteration_counter)
1609                                 continue;
1610
1611                         l->last_iteration = bus->iteration_counter;
1612
1613                         r = l->callback(bus, 0, m, l->userdata);
1614                         if (r != 0)
1615                                 return r;
1616
1617                 }
1618
1619         } while (bus->filter_callbacks_modified);
1620
1621         return 0;
1622 }
1623
1624 static int process_match(sd_bus *bus, sd_bus_message *m) {
1625         int r;
1626
1627         assert(bus);
1628         assert(m);
1629
1630         do {
1631                 bus->match_callbacks_modified = false;
1632
1633                 r = bus_match_run(bus, &bus->match_callbacks, 0, m);
1634                 if (r != 0)
1635                         return r;
1636
1637         } while (bus->match_callbacks_modified);
1638
1639         return 0;
1640 }
1641
1642 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1643         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1644         int r;
1645
1646         assert(bus);
1647         assert(m);
1648
1649         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1650                 return 0;
1651
1652         if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1653                 return 0;
1654
1655         if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1656                 return 1;
1657
1658         if (streq_ptr(m->member, "Ping"))
1659                 r = sd_bus_message_new_method_return(bus, m, &reply);
1660         else if (streq_ptr(m->member, "GetMachineId")) {
1661                 sd_id128_t id;
1662                 char sid[33];
1663
1664                 r = sd_id128_get_machine(&id);
1665                 if (r < 0)
1666                         return r;
1667
1668                 r = sd_bus_message_new_method_return(bus, m, &reply);
1669                 if (r < 0)
1670                         return r;
1671
1672                 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
1673         } else {
1674                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1675
1676                 sd_bus_error_set(&error,
1677                                  "org.freedesktop.DBus.Error.UnknownMethod",
1678                                  "Unknown method '%s' on interface '%s'.", m->member, m->interface);
1679
1680                 r = sd_bus_message_new_method_error(bus, m, &error, &reply);
1681         }
1682
1683         if (r < 0)
1684                 return r;
1685
1686         r = sd_bus_send(bus, reply, NULL);
1687         if (r < 0)
1688                 return r;
1689
1690         return 1;
1691 }
1692
1693 static int process_object(sd_bus *bus, sd_bus_message *m) {
1694         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1695         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1696         struct object_callback *c;
1697         int r;
1698         bool found = false;
1699         size_t pl;
1700
1701         assert(bus);
1702         assert(m);
1703
1704         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
1705                 return 0;
1706
1707         if (hashmap_isempty(bus->object_callbacks))
1708                 return 0;
1709
1710         pl = strlen(m->path);
1711
1712         do {
1713                 char p[pl+1];
1714
1715                 bus->object_callbacks_modified = false;
1716
1717                 c = hashmap_get(bus->object_callbacks, m->path);
1718                 if (c && c->last_iteration != bus->iteration_counter) {
1719
1720                         c->last_iteration = bus->iteration_counter;
1721
1722                         r = c->callback(bus, 0, m, c->userdata);
1723                         if (r != 0)
1724                                 return r;
1725
1726                         found = true;
1727                 }
1728
1729                 /* Look for fallback prefixes */
1730                 strcpy(p, m->path);
1731                 for (;;) {
1732                         char *e;
1733
1734                         if (bus->object_callbacks_modified)
1735                                 break;
1736
1737                         e = strrchr(p, '/');
1738                         if (e == p || !e)
1739                                 break;
1740
1741                         *e = 0;
1742
1743                         c = hashmap_get(bus->object_callbacks, p);
1744                         if (c && c->last_iteration != bus->iteration_counter && c->is_fallback) {
1745
1746                                 c->last_iteration = bus->iteration_counter;
1747
1748                                 r = c->callback(bus, 0, m, c->userdata);
1749                                 if (r != 0)
1750                                         return r;
1751
1752                                 found = true;
1753                         }
1754                 }
1755
1756         } while (bus->object_callbacks_modified);
1757
1758         /* We found some handlers but none wanted to take this, then
1759          * return this -- with one exception, we can handle
1760          * introspection minimally ourselves */
1761         if (!found || sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect"))
1762                 return 0;
1763
1764         sd_bus_error_set(&error,
1765                          "org.freedesktop.DBus.Error.UnknownMethod",
1766                          "Unknown method '%s' or interface '%s'.", m->member, m->interface);
1767
1768         r = sd_bus_message_new_method_error(bus, m, &error, &reply);
1769         if (r < 0)
1770                 return r;
1771
1772         r = sd_bus_send(bus, reply, NULL);
1773         if (r < 0)
1774                 return r;
1775
1776         return 1;
1777 }
1778
1779 static int process_introspect(sd_bus *bus, sd_bus_message *m) {
1780         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1781         _cleanup_free_ char *introspection = NULL;
1782         _cleanup_set_free_free_ Set *s = NULL;
1783         _cleanup_fclose_ FILE *f = NULL;
1784         struct object_callback *c;
1785         Iterator i;
1786         size_t size = 0;
1787         char *node;
1788         int r;
1789
1790         assert(bus);
1791         assert(m);
1792
1793         if (!sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect"))
1794                 return 0;
1795
1796         if (!m->path)
1797                 return 0;
1798
1799         s = set_new(string_hash_func, string_compare_func);
1800         if (!s)
1801                 return -ENOMEM;
1802
1803         HASHMAP_FOREACH(c, bus->object_callbacks, i) {
1804                 const char *e;
1805                 char *a, *p;
1806
1807                 if (streq(c->path, "/"))
1808                         continue;
1809
1810                 if (streq(m->path, "/"))
1811                         e = c->path;
1812                 else {
1813                         e = startswith(c->path, m->path);
1814                         if (!e || *e != '/')
1815                                 continue;
1816                 }
1817
1818                 a = strdup(e+1);
1819                 if (!a)
1820                         return -ENOMEM;
1821
1822                 p = strchr(a, '/');
1823                 if (p)
1824                         *p = 0;
1825
1826                 r = set_put(s, a);
1827                 if (r < 0) {
1828                         free(a);
1829
1830                         if (r != -EEXIST)
1831                                 return r;
1832                 }
1833         }
1834
1835         f = open_memstream(&introspection, &size);
1836         if (!f)
1837                 return -ENOMEM;
1838
1839         fputs(SD_BUS_INTROSPECT_DOCTYPE, f);
1840         fputs("<node>\n", f);
1841         fputs(SD_BUS_INTROSPECT_INTERFACE_PEER, f);
1842         fputs(SD_BUS_INTROSPECT_INTERFACE_INTROSPECTABLE, f);
1843
1844         while ((node = set_steal_first(s))) {
1845                 fprintf(f, " <node name=\"%s\"/>\n", node);
1846                 free(node);
1847         }
1848
1849         fputs("</node>\n", f);
1850
1851         fflush(f);
1852
1853         if (ferror(f))
1854                 return -ENOMEM;
1855
1856         r = sd_bus_message_new_method_return(bus, m, &reply);
1857         if (r < 0)
1858                 return r;
1859
1860         r = sd_bus_message_append(reply, "s", introspection);
1861         if (r < 0)
1862                 return r;
1863
1864         r = sd_bus_send(bus, reply, NULL);
1865         if (r < 0)
1866                 return r;
1867
1868         return 1;
1869 }
1870
1871 static int process_message(sd_bus *bus, sd_bus_message *m) {
1872         int r;
1873
1874         assert(bus);
1875         assert(m);
1876
1877         bus->iteration_counter++;
1878
1879         r = process_hello(bus, m);
1880         if (r != 0)
1881                 return r;
1882
1883         r = process_reply(bus, m);
1884         if (r != 0)
1885                 return r;
1886
1887         r = process_filter(bus, m);
1888         if (r != 0)
1889                 return r;
1890
1891         r = process_match(bus, m);
1892         if (r != 0)
1893                 return r;
1894
1895         r = process_builtin(bus, m);
1896         if (r != 0)
1897                 return r;
1898
1899         r = process_object(bus, m);
1900         if (r != 0)
1901                 return r;
1902
1903         return process_introspect(bus, m);
1904 }
1905
1906 static int process_running(sd_bus *bus, sd_bus_message **ret) {
1907         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
1908         int r;
1909
1910         assert(bus);
1911         assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1912
1913         r = process_timeout(bus);
1914         if (r != 0)
1915                 goto null_message;
1916
1917         r = dispatch_wqueue(bus);
1918         if (r != 0)
1919                 goto null_message;
1920
1921         r = dispatch_rqueue(bus, &m);
1922         if (r < 0)
1923                 return r;
1924         if (!m)
1925                 goto null_message;
1926
1927         r = process_message(bus, m);
1928         if (r != 0)
1929                 goto null_message;
1930
1931         if (ret) {
1932                 *ret = m;
1933                 m = NULL;
1934                 return 1;
1935         }
1936
1937         if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL) {
1938                 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1939                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
1940
1941                 sd_bus_error_set(&error, "org.freedesktop.DBus.Error.UnknownObject", "Unknown object '%s'.", m->path);
1942
1943                 r = sd_bus_message_new_method_error(bus, m, &error, &reply);
1944                 if (r < 0)
1945                         return r;
1946
1947                 r = sd_bus_send(bus, reply, NULL);
1948                 if (r < 0)
1949                         return r;
1950         }
1951
1952         return 1;
1953
1954 null_message:
1955         if (r >= 0 && ret)
1956                 *ret = NULL;
1957
1958         return r;
1959 }
1960
1961 int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
1962         int r;
1963
1964         /* Returns 0 when we didn't do anything. This should cause the
1965          * caller to invoke sd_bus_wait() before returning the next
1966          * time. Returns > 0 when we did something, which possibly
1967          * means *ret is filled in with an unprocessed message. */
1968
1969         if (!bus)
1970                 return -EINVAL;
1971         if (bus->input_fd < 0)
1972                 return -ENOTCONN;
1973
1974         /* We don't allow recursively invoking sd_bus_process(). */
1975         if (bus->processing)
1976                 return -EBUSY;
1977
1978         switch (bus->state) {
1979
1980         case BUS_UNSET:
1981                 return -ENOTCONN;
1982
1983         case BUS_OPENING:
1984                 r = bus_socket_process_opening(bus);
1985                 if (r < 0)
1986                         return r;
1987                 if (ret)
1988                         *ret = NULL;
1989                 return r;
1990
1991         case BUS_AUTHENTICATING:
1992
1993                 r = bus_socket_process_authenticating(bus);
1994                 if (r < 0)
1995                         return r;
1996                 if (ret)
1997                         *ret = NULL;
1998                 return r;
1999
2000         case BUS_RUNNING:
2001         case BUS_HELLO:
2002
2003                 bus->processing = true;
2004                 r = process_running(bus, ret);
2005                 bus->processing = false;
2006
2007                 return r;
2008         }
2009
2010         assert_not_reached("Unknown state");
2011 }
2012
2013 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2014         struct pollfd p[2] = {};
2015         int r, e, n;
2016         struct timespec ts;
2017         usec_t until, m;
2018
2019         assert(bus);
2020
2021         if (bus->input_fd < 0)
2022                 return -ENOTCONN;
2023
2024         e = sd_bus_get_events(bus);
2025         if (e < 0)
2026                 return e;
2027
2028         if (need_more)
2029                 e |= POLLIN;
2030
2031         r = sd_bus_get_timeout(bus, &until);
2032         if (r < 0)
2033                 return r;
2034         if (r == 0)
2035                 m = (uint64_t) -1;
2036         else {
2037                 usec_t nw;
2038                 nw = now(CLOCK_MONOTONIC);
2039                 m = until > nw ? until - nw : 0;
2040         }
2041
2042         if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2043                 m = timeout_usec;
2044
2045         p[0].fd = bus->input_fd;
2046         if (bus->output_fd == bus->input_fd) {
2047                 p[0].events = e;
2048                 n = 1;
2049         } else {
2050                 p[0].events = e & POLLIN;
2051                 p[1].fd = bus->output_fd;
2052                 p[1].events = e & POLLOUT;
2053                 n = 2;
2054         }
2055
2056         r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2057         if (r < 0)
2058                 return -errno;
2059
2060         return r > 0 ? 1 : 0;
2061 }
2062
2063 int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2064
2065         if (!bus)
2066                 return -EINVAL;
2067         if (bus->state == BUS_UNSET)
2068                 return -ENOTCONN;
2069         if (bus->input_fd < 0)
2070                 return -ENOTCONN;
2071         if (bus->rqueue_size > 0)
2072                 return 0;
2073
2074         return bus_poll(bus, false, timeout_usec);
2075 }
2076
2077 int sd_bus_flush(sd_bus *bus) {
2078         int r;
2079
2080         if (!bus)
2081                 return -EINVAL;
2082         if (bus->state == BUS_UNSET)
2083                 return -ENOTCONN;
2084         if (bus->output_fd < 0)
2085                 return -ENOTCONN;
2086
2087         r = bus_ensure_running(bus);
2088         if (r < 0)
2089                 return r;
2090
2091         if (bus->wqueue_size <= 0)
2092                 return 0;
2093
2094         for (;;) {
2095                 r = dispatch_wqueue(bus);
2096                 if (r < 0)
2097                         return r;
2098
2099                 if (bus->wqueue_size <= 0)
2100                         return 0;
2101
2102                 r = bus_poll(bus, false, (uint64_t) -1);
2103                 if (r < 0)
2104                         return r;
2105         }
2106 }
2107
2108 int sd_bus_add_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) {
2109         struct filter_callback *f;
2110
2111         if (!bus)
2112                 return -EINVAL;
2113         if (!callback)
2114                 return -EINVAL;
2115
2116         f = new0(struct filter_callback, 1);
2117         if (!f)
2118                 return -ENOMEM;
2119         f->callback = callback;
2120         f->userdata = userdata;
2121
2122         bus->filter_callbacks_modified = true;
2123         LIST_PREPEND(struct filter_callback, callbacks, bus->filter_callbacks, f);
2124         return 0;
2125 }
2126
2127 int sd_bus_remove_filter(sd_bus *bus, sd_bus_message_handler_t callback, void *userdata) {
2128         struct filter_callback *f;
2129
2130         if (!bus)
2131                 return -EINVAL;
2132         if (!callback)
2133                 return -EINVAL;
2134
2135         LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2136                 if (f->callback == callback && f->userdata == userdata) {
2137                         bus->filter_callbacks_modified = true;
2138                         LIST_REMOVE(struct filter_callback, callbacks, bus->filter_callbacks, f);
2139                         free(f);
2140                         return 1;
2141                 }
2142         }
2143
2144         return 0;
2145 }
2146
2147 static int bus_add_object(
2148                 sd_bus *bus,
2149                 bool fallback,
2150                 const char *path,
2151                 sd_bus_message_handler_t callback,
2152                 void *userdata) {
2153
2154         struct object_callback *c;
2155         int r;
2156
2157         if (!bus)
2158                 return -EINVAL;
2159         if (!path)
2160                 return -EINVAL;
2161         if (!callback)
2162                 return -EINVAL;
2163
2164         r = hashmap_ensure_allocated(&bus->object_callbacks, string_hash_func, string_compare_func);
2165         if (r < 0)
2166                 return r;
2167
2168         c = new0(struct object_callback, 1);
2169         if (!c)
2170                 return -ENOMEM;
2171
2172         c->path = strdup(path);
2173         if (!c->path) {
2174                 free(c);
2175                 return -ENOMEM;
2176         }
2177
2178         c->callback = callback;
2179         c->userdata = userdata;
2180         c->is_fallback = fallback;
2181
2182         bus->object_callbacks_modified = true;
2183         r = hashmap_put(bus->object_callbacks, c->path, c);
2184         if (r < 0) {
2185                 free(c->path);
2186                 free(c);
2187                 return r;
2188         }
2189
2190         return 0;
2191 }
2192
2193 static int bus_remove_object(
2194                 sd_bus *bus,
2195                 bool fallback,
2196                 const char *path,
2197                 sd_bus_message_handler_t callback,
2198                 void *userdata) {
2199
2200         struct object_callback *c;
2201
2202         if (!bus)
2203                 return -EINVAL;
2204         if (!path)
2205                 return -EINVAL;
2206         if (!callback)
2207                 return -EINVAL;
2208
2209         c = hashmap_get(bus->object_callbacks, path);
2210         if (!c)
2211                 return 0;
2212
2213         if (c->callback != callback || c->userdata != userdata || c->is_fallback != fallback)
2214                 return 0;
2215
2216         bus->object_callbacks_modified = true;
2217         assert_se(c == hashmap_remove(bus->object_callbacks, c->path));
2218
2219         free(c->path);
2220         free(c);
2221
2222         return 1;
2223 }
2224
2225 int sd_bus_add_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
2226         return bus_add_object(bus, false, path, callback, userdata);
2227 }
2228
2229 int sd_bus_remove_object(sd_bus *bus, const char *path, sd_bus_message_handler_t callback, void *userdata) {
2230         return bus_remove_object(bus, false, path, callback, userdata);
2231 }
2232
2233 int sd_bus_add_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
2234         return bus_add_object(bus, true, prefix, callback, userdata);
2235 }
2236
2237 int sd_bus_remove_fallback(sd_bus *bus, const char *prefix, sd_bus_message_handler_t callback, void *userdata) {
2238         return bus_remove_object(bus, true, prefix, callback, userdata);
2239 }
2240
2241 int sd_bus_add_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
2242         int r = 0;
2243
2244         if (!bus)
2245                 return -EINVAL;
2246         if (!match)
2247                 return -EINVAL;
2248
2249         if (bus->bus_client) {
2250                 r = bus_add_match_internal(bus, match);
2251                 if (r < 0)
2252                         return r;
2253         }
2254
2255         if (callback) {
2256                 bus->match_callbacks_modified = true;
2257                 r = bus_match_add(&bus->match_callbacks, match, callback, userdata, NULL);
2258                 if (r < 0) {
2259
2260                         if (bus->bus_client)
2261                                 bus_remove_match_internal(bus, match);
2262                 }
2263         }
2264
2265         return r;
2266 }
2267
2268 int sd_bus_remove_match(sd_bus *bus, const char *match, sd_bus_message_handler_t callback, void *userdata) {
2269         int r = 0, q = 0;
2270
2271         if (!bus)
2272                 return -EINVAL;
2273         if (!match)
2274                 return -EINVAL;
2275
2276         if (bus->bus_client)
2277                 r = bus_remove_match_internal(bus, match);
2278
2279         if (callback) {
2280                 bus->match_callbacks_modified = true;
2281                 q = bus_match_remove(&bus->match_callbacks, match, callback, userdata);
2282         }
2283
2284         if (r < 0)
2285                 return r;
2286         return q;
2287 }
2288
2289 int sd_bus_emit_signal(
2290                 sd_bus *bus,
2291                 const char *path,
2292                 const char *interface,
2293                 const char *member,
2294                 const char *types, ...) {
2295
2296         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2297         va_list ap;
2298         int r;
2299
2300         if (!bus)
2301                 return -EINVAL;
2302
2303         r = sd_bus_message_new_signal(bus, path, interface, member, &m);
2304         if (r < 0)
2305                 return r;
2306
2307         va_start(ap, types);
2308         r = bus_message_append_ap(m, types, ap);
2309         va_end(ap);
2310         if (r < 0)
2311                 return r;
2312
2313         return sd_bus_send(bus, m, NULL);
2314 }
2315
2316 int sd_bus_call_method(
2317                 sd_bus *bus,
2318                 const char *destination,
2319                 const char *path,
2320                 const char *interface,
2321                 const char *member,
2322                 sd_bus_error *error,
2323                 sd_bus_message **reply,
2324                 const char *types, ...) {
2325
2326         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2327         va_list ap;
2328         int r;
2329
2330         if (!bus)
2331                 return -EINVAL;
2332
2333         r = sd_bus_message_new_method_call(bus, destination, path, interface, member, &m);
2334         if (r < 0)
2335                 return r;
2336
2337         va_start(ap, types);
2338         r = bus_message_append_ap(m, types, ap);
2339         va_end(ap);
2340         if (r < 0)
2341                 return r;
2342
2343         return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
2344 }
2345
2346 int sd_bus_reply_method_return(
2347                 sd_bus *bus,
2348                 sd_bus_message *call,
2349                 const char *types, ...) {
2350
2351         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2352         va_list ap;
2353         int r;
2354
2355         if (!bus)
2356                 return -EINVAL;
2357         if (!call)
2358                 return -EINVAL;
2359         if (!call->sealed)
2360                 return -EPERM;
2361         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
2362                 return -EINVAL;
2363
2364         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
2365                 return 0;
2366
2367         r = sd_bus_message_new_method_return(bus, call, &m);
2368         if (r < 0)
2369                 return r;
2370
2371         va_start(ap, types);
2372         r = bus_message_append_ap(m, types, ap);
2373         va_end(ap);
2374         if (r < 0)
2375                 return r;
2376
2377         return sd_bus_send(bus, m, NULL);
2378 }
2379
2380 int sd_bus_reply_method_error(
2381                 sd_bus *bus,
2382                 sd_bus_message *call,
2383                 const sd_bus_error *e) {
2384
2385         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2386         int r;
2387
2388         if (!bus)
2389                 return -EINVAL;
2390         if (!call)
2391                 return -EINVAL;
2392         if (!call->sealed)
2393                 return -EPERM;
2394         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
2395                 return -EINVAL;
2396         if (!sd_bus_error_is_set(e))
2397                 return -EINVAL;
2398
2399         if (call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
2400                 return 0;
2401
2402         r = sd_bus_message_new_method_error(bus, call, e, &m);
2403         if (r < 0)
2404                 return r;
2405
2406         return sd_bus_send(bus, m, NULL);
2407 }