chiark / gitweb /
*: fix some inconsistent control statement style
[elogind.git] / src / shared / bus-util.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2013 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
29 #include <unistd.h>
30
31 #include "sd-bus-protocol.h"
32 #include "sd-bus.h"
33 #include "sd-daemon.h"
34 #include "sd-event.h"
35 #include "sd-id128.h"
36
37 #include "alloc-util.h"
38 #include "bus-internal.h"
39 #include "bus-label.h"
40 #include "bus-message.h"
41 #include "bus-util.h"
42 #include "cap-list.h"
43 #include "cgroup-util.h"
44 #include "def.h"
45 #include "escape.h"
46 #include "fd-util.h"
47 #include "missing.h"
48 #include "mount-util.h"
49 #include "nsflags.h"
50 #include "parse-util.h"
51 #include "proc-cmdline.h"
52 //#include "rlimit-util.h"
53 #include "stdio-util.h"
54 #include "strv.h"
55 #include "user-util.h"
56
57 #if 0 /// UNNEEDED by elogind
58 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
59         sd_event *e = userdata;
60
61         assert(m);
62         assert(e);
63
64         sd_bus_close(sd_bus_message_get_bus(m));
65         sd_event_exit(e, 0);
66
67         return 1;
68 }
69
70 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
71         _cleanup_free_ char *match = NULL;
72         const char *unique;
73         int r;
74
75         assert(e);
76         assert(bus);
77         assert(name);
78
79         /* We unregister the name here and then wait for the
80          * NameOwnerChanged signal for this event to arrive before we
81          * quit. We do this in order to make sure that any queued
82          * requests are still processed before we really exit. */
83
84         r = sd_bus_get_unique_name(bus, &unique);
85         if (r < 0)
86                 return r;
87
88         r = asprintf(&match,
89                      "sender='org.freedesktop.DBus',"
90                      "type='signal',"
91                      "interface='org.freedesktop.DBus',"
92                      "member='NameOwnerChanged',"
93                      "path='/org/freedesktop/DBus',"
94                      "arg0='%s',"
95                      "arg1='%s',"
96                      "arg2=''", name, unique);
97         if (r < 0)
98                 return -ENOMEM;
99
100         r = sd_bus_add_match(bus, NULL, match, name_owner_change_callback, e);
101         if (r < 0)
102                 return r;
103
104         r = sd_bus_release_name(bus, name);
105         if (r < 0)
106                 return r;
107
108         return 0;
109 }
110
111 int bus_event_loop_with_idle(
112                 sd_event *e,
113                 sd_bus *bus,
114                 const char *name,
115                 usec_t timeout,
116                 check_idle_t check_idle,
117                 void *userdata) {
118         bool exiting = false;
119         int r, code;
120
121         assert(e);
122         assert(bus);
123         assert(name);
124
125         for (;;) {
126                 bool idle;
127
128                 r = sd_event_get_state(e);
129                 if (r < 0)
130                         return r;
131                 if (r == SD_EVENT_FINISHED)
132                         break;
133
134                 if (check_idle)
135                         idle = check_idle(userdata);
136                 else
137                         idle = true;
138
139                 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
140                 if (r < 0)
141                         return r;
142
143                 if (r == 0 && !exiting && idle) {
144
145                         r = sd_bus_try_close(bus);
146                         if (r == -EBUSY)
147                                 continue;
148
149                         /* Fallback for dbus1 connections: we
150                          * unregister the name and wait for the
151                          * response to come through for it */
152                         if (r == -EOPNOTSUPP) {
153
154                                 /* Inform the service manager that we
155                                  * are going down, so that it will
156                                  * queue all further start requests,
157                                  * instead of assuming we are already
158                                  * running. */
159                                 sd_notify(false, "STOPPING=1");
160
161                                 r = bus_async_unregister_and_exit(e, bus, name);
162                                 if (r < 0)
163                                         return r;
164
165                                 exiting = true;
166                                 continue;
167                         }
168
169                         if (r < 0)
170                                 return r;
171
172                         sd_event_exit(e, 0);
173                         break;
174                 }
175         }
176
177         r = sd_event_get_exit_code(e, &code);
178         if (r < 0)
179                 return r;
180
181         return code;
182 }
183 #endif // 0
184
185 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
186         _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
187         int r, has_owner = 0;
188
189         assert(c);
190         assert(name);
191
192         r = sd_bus_call_method(c,
193                                "org.freedesktop.DBus",
194                                "/org/freedesktop/dbus",
195                                "org.freedesktop.DBus",
196                                "NameHasOwner",
197                                error,
198                                &rep,
199                                "s",
200                                name);
201         if (r < 0)
202                 return r;
203
204         r = sd_bus_message_read_basic(rep, 'b', &has_owner);
205         if (r < 0)
206                 return sd_bus_error_set_errno(error, r);
207
208         return has_owner;
209 }
210
211 static int check_good_user(sd_bus_message *m, uid_t good_user) {
212         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
213         uid_t sender_uid;
214         int r;
215
216         assert(m);
217
218         if (good_user == UID_INVALID)
219                 return 0;
220
221         r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
222         if (r < 0)
223                 return r;
224
225         /* Don't trust augmented credentials for authorization */
226         assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
227
228         r = sd_bus_creds_get_euid(creds, &sender_uid);
229         if (r < 0)
230                 return r;
231
232         return sender_uid == good_user;
233 }
234
235 int bus_test_polkit(
236                 sd_bus_message *call,
237                 int capability,
238                 const char *action,
239                 const char **details,
240                 uid_t good_user,
241                 bool *_challenge,
242                 sd_bus_error *e) {
243
244         int r;
245
246         assert(call);
247         assert(action);
248
249         /* Tests non-interactively! */
250
251         r = check_good_user(call, good_user);
252         if (r != 0)
253                 return r;
254
255         r = sd_bus_query_sender_privilege(call, capability);
256         if (r < 0)
257                 return r;
258         else if (r > 0)
259                 return 1;
260 #if ENABLE_POLKIT
261         else {
262                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
263                 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
264                 int authorized = false, challenge = false;
265                 const char *sender, **k, **v;
266
267                 sender = sd_bus_message_get_sender(call);
268                 if (!sender)
269                         return -EBADMSG;
270
271                 r = sd_bus_message_new_method_call(
272                                 call->bus,
273                                 &request,
274                                 "org.freedesktop.PolicyKit1",
275                                 "/org/freedesktop/PolicyKit1/Authority",
276                                 "org.freedesktop.PolicyKit1.Authority",
277                                 "CheckAuthorization");
278                 if (r < 0)
279                         return r;
280
281                 r = sd_bus_message_append(
282                                 request,
283                                 "(sa{sv})s",
284                                 "system-bus-name", 1, "name", "s", sender,
285                                 action);
286                 if (r < 0)
287                         return r;
288
289                 r = sd_bus_message_open_container(request, 'a', "{ss}");
290                 if (r < 0)
291                         return r;
292
293                 STRV_FOREACH_PAIR(k, v, details) {
294                         r = sd_bus_message_append(request, "{ss}", *k, *v);
295                         if (r < 0)
296                                 return r;
297                 }
298
299                 r = sd_bus_message_close_container(request);
300                 if (r < 0)
301                         return r;
302
303                 r = sd_bus_message_append(request, "us", 0, NULL);
304                 if (r < 0)
305                         return r;
306
307                 r = sd_bus_call(call->bus, request, 0, e, &reply);
308                 if (r < 0) {
309                         /* Treat no PK available as access denied */
310                         if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
311                                 sd_bus_error_free(e);
312                                 return -EACCES;
313                         }
314
315                         return r;
316                 }
317
318                 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
319                 if (r < 0)
320                         return r;
321
322                 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
323                 if (r < 0)
324                         return r;
325
326                 if (authorized)
327                         return 1;
328
329                 if (_challenge) {
330                         *_challenge = challenge;
331                         return 0;
332                 }
333         }
334 #endif
335
336         return -EACCES;
337 }
338
339 #if ENABLE_POLKIT
340
341 typedef struct AsyncPolkitQuery {
342         sd_bus_message *request, *reply;
343         sd_bus_message_handler_t callback;
344         void *userdata;
345         sd_bus_slot *slot;
346         Hashmap *registry;
347 } AsyncPolkitQuery;
348
349 static void async_polkit_query_free(AsyncPolkitQuery *q) {
350
351         if (!q)
352                 return;
353
354         sd_bus_slot_unref(q->slot);
355
356         if (q->registry && q->request)
357                 hashmap_remove(q->registry, q->request);
358
359         sd_bus_message_unref(q->request);
360         sd_bus_message_unref(q->reply);
361
362         free(q);
363 }
364
365 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
366         _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
367         AsyncPolkitQuery *q = userdata;
368         int r;
369
370         assert(reply);
371         assert(q);
372
373         q->slot = sd_bus_slot_unref(q->slot);
374         q->reply = sd_bus_message_ref(reply);
375
376         r = sd_bus_message_rewind(q->request, true);
377         if (r < 0) {
378                 r = sd_bus_reply_method_errno(q->request, r, NULL);
379                 goto finish;
380         }
381
382         r = q->callback(q->request, q->userdata, &error_buffer);
383         r = bus_maybe_reply_error(q->request, r, &error_buffer);
384
385 finish:
386         async_polkit_query_free(q);
387
388         return r;
389 }
390
391 #endif
392
393 int bus_verify_polkit_async(
394                 sd_bus_message *call,
395                 int capability,
396                 const char *action,
397                 const char **details,
398                 bool interactive,
399                 uid_t good_user,
400                 Hashmap **registry,
401                 sd_bus_error *error) {
402
403 #if ENABLE_POLKIT
404         _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
405         AsyncPolkitQuery *q;
406         const char *sender, **k, **v;
407         sd_bus_message_handler_t callback;
408         void *userdata;
409         int c;
410 #endif
411         int r;
412
413         assert(call);
414         assert(action);
415         assert(registry);
416
417         r = check_good_user(call, good_user);
418         if (r != 0)
419                 return r;
420
421 #if ENABLE_POLKIT
422         q = hashmap_get(*registry, call);
423         if (q) {
424                 int authorized, challenge;
425
426                 /* This is the second invocation of this function, and
427                  * there's already a response from polkit, let's
428                  * process it */
429                 assert(q->reply);
430
431                 if (sd_bus_message_is_method_error(q->reply, NULL)) {
432                         const sd_bus_error *e;
433
434                         /* Copy error from polkit reply */
435                         e = sd_bus_message_get_error(q->reply);
436                         sd_bus_error_copy(error, e);
437
438                         /* Treat no PK available as access denied */
439                         if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
440                                 return -EACCES;
441
442                         return -sd_bus_error_get_errno(e);
443                 }
444
445                 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
446                 if (r >= 0)
447                         r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
448
449                 if (r < 0)
450                         return r;
451
452                 if (authorized)
453                         return 1;
454
455                 if (challenge)
456                         return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
457
458                 return -EACCES;
459         }
460 #endif
461
462         r = sd_bus_query_sender_privilege(call, capability);
463         if (r < 0)
464                 return r;
465         else if (r > 0)
466                 return 1;
467
468 #if ENABLE_POLKIT
469         if (sd_bus_get_current_message(call->bus) != call)
470                 return -EINVAL;
471
472         callback = sd_bus_get_current_handler(call->bus);
473         if (!callback)
474                 return -EINVAL;
475
476         userdata = sd_bus_get_current_userdata(call->bus);
477
478         sender = sd_bus_message_get_sender(call);
479         if (!sender)
480                 return -EBADMSG;
481
482         c = sd_bus_message_get_allow_interactive_authorization(call);
483         if (c < 0)
484                 return c;
485         if (c > 0)
486                 interactive = true;
487
488         r = hashmap_ensure_allocated(registry, NULL);
489         if (r < 0)
490                 return r;
491
492         r = sd_bus_message_new_method_call(
493                         call->bus,
494                         &pk,
495                         "org.freedesktop.PolicyKit1",
496                         "/org/freedesktop/PolicyKit1/Authority",
497                         "org.freedesktop.PolicyKit1.Authority",
498                         "CheckAuthorization");
499         if (r < 0)
500                 return r;
501
502         r = sd_bus_message_append(
503                         pk,
504                         "(sa{sv})s",
505                         "system-bus-name", 1, "name", "s", sender,
506                         action);
507         if (r < 0)
508                 return r;
509
510         r = sd_bus_message_open_container(pk, 'a', "{ss}");
511         if (r < 0)
512                 return r;
513
514         STRV_FOREACH_PAIR(k, v, details) {
515                 r = sd_bus_message_append(pk, "{ss}", *k, *v);
516                 if (r < 0)
517                         return r;
518         }
519
520         r = sd_bus_message_close_container(pk);
521         if (r < 0)
522                 return r;
523
524         r = sd_bus_message_append(pk, "us", !!interactive, NULL);
525         if (r < 0)
526                 return r;
527
528         q = new0(AsyncPolkitQuery, 1);
529         if (!q)
530                 return -ENOMEM;
531
532         q->request = sd_bus_message_ref(call);
533         q->callback = callback;
534         q->userdata = userdata;
535
536         r = hashmap_put(*registry, call, q);
537         if (r < 0) {
538                 async_polkit_query_free(q);
539                 return r;
540         }
541
542         q->registry = *registry;
543
544         r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
545         if (r < 0) {
546                 async_polkit_query_free(q);
547                 return r;
548         }
549
550         return 0;
551 #endif
552
553         return -EACCES;
554 }
555
556 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
557 #if ENABLE_POLKIT
558         hashmap_free_with_destructor(registry, async_polkit_query_free);
559 #endif
560 }
561
562 #if 0 /// UNNEEDED by elogind
563 int bus_check_peercred(sd_bus *c) {
564         struct ucred ucred;
565         socklen_t l;
566         int fd;
567
568         assert(c);
569
570         fd = sd_bus_get_fd(c);
571         if (fd < 0)
572                 return fd;
573
574         l = sizeof(struct ucred);
575         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
576                 return -errno;
577
578         if (l != sizeof(struct ucred))
579                 return -E2BIG;
580
581         if (ucred.uid != 0 && ucred.uid != geteuid())
582                 return -EPERM;
583
584         return 1;
585 }
586
587 int bus_connect_system_systemd(sd_bus **_bus) {
588         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
589         int r;
590
591         assert(_bus);
592
593         if (geteuid() != 0)
594                 return sd_bus_default_system(_bus);
595
596         /* If we are root then let's talk directly to the system
597          * instance, instead of going via the bus */
598
599         r = sd_bus_new(&bus);
600         if (r < 0)
601                 return r;
602
603         r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
604         if (r < 0)
605                 return r;
606
607         r = sd_bus_start(bus);
608         if (r < 0)
609                 return sd_bus_default_system(_bus);
610
611         r = bus_check_peercred(bus);
612         if (r < 0)
613                 return r;
614
615         *_bus = bus;
616         bus = NULL;
617
618         return 0;
619 }
620
621 int bus_connect_user_systemd(sd_bus **_bus) {
622         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
623         _cleanup_free_ char *ee = NULL;
624         const char *e;
625         int r;
626
627         assert(_bus);
628
629         e = secure_getenv("XDG_RUNTIME_DIR");
630         if (!e)
631                 return sd_bus_default_user(_bus);
632
633         ee = bus_address_escape(e);
634         if (!ee)
635                 return -ENOMEM;
636
637         r = sd_bus_new(&bus);
638         if (r < 0)
639                 return r;
640
641         bus->address = strjoin("unix:path=", ee, "/systemd/private");
642         if (!bus->address)
643                 return -ENOMEM;
644
645         r = sd_bus_start(bus);
646         if (r < 0)
647                 return sd_bus_default_user(_bus);
648
649         r = bus_check_peercred(bus);
650         if (r < 0)
651                 return r;
652
653         *_bus = bus;
654         bus = NULL;
655
656         return 0;
657 }
658 #endif // 0
659
660 #define print_property(name, fmt, ...)                                  \
661         do {                                                            \
662                 if (value)                                              \
663                         printf(fmt "\n", __VA_ARGS__);                  \
664                 else                                                    \
665                         printf("%s=" fmt "\n", name, __VA_ARGS__);      \
666         } while (0)
667
668 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
669         char type;
670         const char *contents;
671         int r;
672
673         assert(name);
674         assert(property);
675
676         r = sd_bus_message_peek_type(property, &type, &contents);
677         if (r < 0)
678                 return r;
679
680         switch (type) {
681
682         case SD_BUS_TYPE_STRING: {
683                 const char *s;
684
685                 r = sd_bus_message_read_basic(property, type, &s);
686                 if (r < 0)
687                         return r;
688
689                 if (all || !isempty(s)) {
690                         bool good;
691
692                         /* This property has a single value, so we need to take
693                          * care not to print a new line, everything else is OK. */
694                         good = !strchr(s, '\n');
695                         print_property(name, "%s", good ? s : "[unprintable]");
696                 }
697
698                 return 1;
699         }
700
701         case SD_BUS_TYPE_BOOLEAN: {
702                 int b;
703
704                 r = sd_bus_message_read_basic(property, type, &b);
705                 if (r < 0)
706                         return r;
707
708                 print_property(name, "%s", yes_no(b));
709
710                 return 1;
711         }
712
713         case SD_BUS_TYPE_UINT64: {
714                 uint64_t u;
715
716                 r = sd_bus_message_read_basic(property, type, &u);
717                 if (r < 0)
718                         return r;
719
720                 /* Yes, heuristics! But we can change this check
721                  * should it turn out to not be sufficient */
722
723                 if (endswith(name, "Timestamp")) {
724                         char timestamp[FORMAT_TIMESTAMP_MAX], *t;
725
726                         t = format_timestamp(timestamp, sizeof(timestamp), u);
727                         if (t || all)
728                                 print_property(name, "%s", strempty(t));
729
730                 } else if (strstr(name, "USec")) {
731                         char timespan[FORMAT_TIMESPAN_MAX];
732
733                         print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
734                 } else if (streq(name, "RestrictNamespaces")) {
735                         _cleanup_free_ char *s = NULL;
736                         const char *result;
737
738                         if ((u & NAMESPACE_FLAGS_ALL) == 0)
739                                 result = "yes";
740                         else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
741                                 result = "no";
742                         else {
743                                 r = namespace_flag_to_string_many(u, &s);
744                                 if (r < 0)
745                                         return r;
746
747                                 result = s;
748                         }
749
750                         print_property(name, "%s", result);
751
752                 } else if (streq(name, "MountFlags")) {
753                         const char *result;
754
755                         result = mount_propagation_flags_to_string(u);
756                         if (!result)
757                                 return -EINVAL;
758
759                         print_property(name, "%s", result);
760
761                 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
762                         _cleanup_free_ char *s = NULL;
763
764                         r = capability_set_to_string_alloc(u, &s);
765                         if (r < 0)
766                                 return r;
767
768                         print_property(name, "%s", s);
769
770                 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
771                            (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
772                            (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
773                            (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
774                            (endswith(name, "NSec") && u == (uint64_t) -1))
775
776                         print_property(name, "%s", "[not set]");
777
778                 else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
779                          (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
780                          (startswith(name, "Limit") && u == (uint64_t) -1) ||
781                          (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
782
783                         print_property(name, "%s", "infinity");
784                 else
785                         print_property(name, "%"PRIu64, u);
786
787                 return 1;
788         }
789
790         case SD_BUS_TYPE_INT64: {
791                 int64_t i;
792
793                 r = sd_bus_message_read_basic(property, type, &i);
794                 if (r < 0)
795                         return r;
796
797                 print_property(name, "%"PRIi64, i);
798
799                 return 1;
800         }
801
802         case SD_BUS_TYPE_UINT32: {
803                 uint32_t u;
804
805                 r = sd_bus_message_read_basic(property, type, &u);
806                 if (r < 0)
807                         return r;
808
809                 if (strstr(name, "UMask") || strstr(name, "Mode"))
810                         print_property(name, "%04o", u);
811                 else if (streq(name, "UID")) {
812                         if (u == UID_INVALID)
813                                 print_property(name, "%s", "[not set]");
814                         else
815                                 print_property(name, "%"PRIu32, u);
816                 } else if (streq(name, "GID")) {
817                         if (u == GID_INVALID)
818                                 print_property(name, "%s", "[not set]");
819                         else
820                                 print_property(name, "%"PRIu32, u);
821                 } else
822                         print_property(name, "%"PRIu32, u);
823
824                 return 1;
825         }
826
827         case SD_BUS_TYPE_INT32: {
828                 int32_t i;
829
830                 r = sd_bus_message_read_basic(property, type, &i);
831                 if (r < 0)
832                         return r;
833
834                 print_property(name, "%"PRIi32, i);
835                 return 1;
836         }
837
838         case SD_BUS_TYPE_DOUBLE: {
839                 double d;
840
841                 r = sd_bus_message_read_basic(property, type, &d);
842                 if (r < 0)
843                         return r;
844
845                 print_property(name, "%g", d);
846                 return 1;
847         }
848
849         case SD_BUS_TYPE_ARRAY:
850                 if (streq(contents, "s")) {
851                         bool first = true;
852                         const char *str;
853
854                         r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
855                         if (r < 0)
856                                 return r;
857
858                         while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
859                                 bool good;
860
861                                 if (first && !value)
862                                         printf("%s=", name);
863
864                                 /* This property has multiple space-separated values, so
865                                  * neither spaces not newlines can be allowed in a value. */
866                                 good = str[strcspn(str, " \n")] == '\0';
867
868                                 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
869
870                                 first = false;
871                         }
872                         if (r < 0)
873                                 return r;
874
875                         if (first && all && !value)
876                                 printf("%s=", name);
877                         if (!first || all)
878                                 puts("");
879
880                         r = sd_bus_message_exit_container(property);
881                         if (r < 0)
882                                 return r;
883
884                         return 1;
885
886                 } else if (streq(contents, "y")) {
887                         const uint8_t *u;
888                         size_t n;
889
890                         r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
891                         if (r < 0)
892                                 return r;
893
894                         if (all || n > 0) {
895                                 unsigned int i;
896
897                                 if (!value)
898                                         printf("%s=", name);
899
900                                 for (i = 0; i < n; i++)
901                                         printf("%02x", u[i]);
902
903                                 puts("");
904                         }
905
906                         return 1;
907
908                 } else if (streq(contents, "u")) {
909                         uint32_t *u;
910                         size_t n;
911
912                         r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
913                         if (r < 0)
914                                 return r;
915
916                         if (all || n > 0) {
917                                 unsigned int i;
918
919                                 if (!value)
920                                         printf("%s=", name);
921
922                                 for (i = 0; i < n; i++)
923                                         printf("%08x", u[i]);
924
925                                 puts("");
926                         }
927
928                         return 1;
929                 }
930
931                 break;
932         }
933
934         return 0;
935 }
936
937 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
938         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
939         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
940         int r;
941
942         assert(bus);
943         assert(path);
944
945         r = sd_bus_call_method(bus,
946                         dest,
947                         path,
948                         "org.freedesktop.DBus.Properties",
949                         "GetAll",
950                         &error,
951                         &reply,
952                         "s", "");
953         if (r < 0)
954                 return r;
955
956         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
957         if (r < 0)
958                 return r;
959
960         while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
961                 const char *name;
962                 const char *contents;
963
964                 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
965                 if (r < 0)
966                         return r;
967
968                 if (!filter || strv_find(filter, name)) {
969                         r = sd_bus_message_peek_type(reply, NULL, &contents);
970                         if (r < 0)
971                                 return r;
972
973                         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
974                         if (r < 0)
975                                 return r;
976
977                         r = bus_print_property(name, reply, value, all);
978                         if (r < 0)
979                                 return r;
980                         if (r == 0) {
981                                 if (all)
982                                         printf("%s=[unprintable]\n", name);
983                                 /* skip what we didn't read */
984                                 r = sd_bus_message_skip(reply, contents);
985                                 if (r < 0)
986                                         return r;
987                         }
988
989                         r = sd_bus_message_exit_container(reply);
990                         if (r < 0)
991                                 return r;
992                 } else {
993                         r = sd_bus_message_skip(reply, "v");
994                         if (r < 0)
995                                 return r;
996                 }
997
998                 r = sd_bus_message_exit_container(reply);
999                 if (r < 0)
1000                         return r;
1001         }
1002         if (r < 0)
1003                 return r;
1004
1005         r = sd_bus_message_exit_container(reply);
1006         if (r < 0)
1007                 return r;
1008
1009         return 0;
1010 }
1011
1012 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1013         sd_id128_t *p = userdata;
1014         const void *v;
1015         size_t n;
1016         int r;
1017
1018         r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1019         if (r < 0)
1020                 return r;
1021
1022         if (n == 0)
1023                 *p = SD_ID128_NULL;
1024         else if (n == 16)
1025                 memcpy((*p).bytes, v, n);
1026         else
1027                 return -EINVAL;
1028
1029         return 0;
1030 }
1031
1032 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1033         char type;
1034         int r;
1035
1036         r = sd_bus_message_peek_type(m, &type, NULL);
1037         if (r < 0)
1038                 return r;
1039
1040         switch (type) {
1041
1042         case SD_BUS_TYPE_STRING: {
1043                 char **p = userdata;
1044                 const char *s;
1045
1046                 r = sd_bus_message_read_basic(m, type, &s);
1047                 if (r < 0)
1048                         return r;
1049
1050                 if (isempty(s))
1051                         s = NULL;
1052
1053                 return free_and_strdup(p, s);
1054         }
1055
1056         case SD_BUS_TYPE_ARRAY: {
1057                 _cleanup_strv_free_ char **l = NULL;
1058                 char ***p = userdata;
1059
1060                 r = bus_message_read_strv_extend(m, &l);
1061                 if (r < 0)
1062                         return r;
1063
1064                 strv_free(*p);
1065                 *p = l;
1066                 l = NULL;
1067                 return 0;
1068         }
1069
1070         case SD_BUS_TYPE_BOOLEAN: {
1071                 unsigned b;
1072                 int *p = userdata;
1073
1074                 r = sd_bus_message_read_basic(m, type, &b);
1075                 if (r < 0)
1076                         return r;
1077
1078                 *p = b;
1079                 return 0;
1080         }
1081
1082         case SD_BUS_TYPE_INT32:
1083         case SD_BUS_TYPE_UINT32: {
1084                 uint32_t u, *p = userdata;
1085
1086                 r = sd_bus_message_read_basic(m, type, &u);
1087                 if (r < 0)
1088                         return r;
1089
1090                 *p = u;
1091                 return 0;
1092         }
1093
1094         case SD_BUS_TYPE_INT64:
1095         case SD_BUS_TYPE_UINT64: {
1096                 uint64_t t, *p = userdata;
1097
1098                 r = sd_bus_message_read_basic(m, type, &t);
1099                 if (r < 0)
1100                         return r;
1101
1102                 *p = t;
1103                 return 0;
1104         }
1105
1106         case SD_BUS_TYPE_DOUBLE: {
1107                 double d, *p = userdata;
1108
1109                 r = sd_bus_message_read_basic(m, type, &d);
1110                 if (r < 0)
1111                         return r;
1112
1113                 *p = d;
1114                 return 0;
1115         }}
1116
1117         return -EOPNOTSUPP;
1118 }
1119
1120 int bus_message_map_all_properties(
1121                 sd_bus_message *m,
1122                 const struct bus_properties_map *map,
1123                 sd_bus_error *error,
1124                 void *userdata) {
1125
1126         int r;
1127
1128         assert(m);
1129         assert(map);
1130
1131         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1132         if (r < 0)
1133                 return r;
1134
1135         while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1136                 const struct bus_properties_map *prop;
1137                 const char *member;
1138                 const char *contents;
1139                 void *v;
1140                 unsigned i;
1141
1142                 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1143                 if (r < 0)
1144                         return r;
1145
1146                 for (i = 0, prop = NULL; map[i].member; i++)
1147                         if (streq(map[i].member, member)) {
1148                                 prop = &map[i];
1149                                 break;
1150                         }
1151
1152                 if (prop) {
1153                         r = sd_bus_message_peek_type(m, NULL, &contents);
1154                         if (r < 0)
1155                                 return r;
1156
1157                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1158                         if (r < 0)
1159                                 return r;
1160
1161                         v = (uint8_t *)userdata + prop->offset;
1162                         if (map[i].set)
1163                                 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1164                         else
1165                                 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v);
1166                         if (r < 0)
1167                                 return r;
1168
1169                         r = sd_bus_message_exit_container(m);
1170                         if (r < 0)
1171                                 return r;
1172                 } else {
1173                         r = sd_bus_message_skip(m, "v");
1174                         if (r < 0)
1175                                 return r;
1176                 }
1177
1178                 r = sd_bus_message_exit_container(m);
1179                 if (r < 0)
1180                         return r;
1181         }
1182         if (r < 0)
1183                 return r;
1184
1185         return sd_bus_message_exit_container(m);
1186 }
1187
1188 #if 0 /// UNNEEDED by elogind
1189 int bus_message_map_properties_changed(
1190                 sd_bus_message *m,
1191                 const struct bus_properties_map *map,
1192                 sd_bus_error *error,
1193                 void *userdata) {
1194
1195         const char *member;
1196         int r, invalidated, i;
1197
1198         assert(m);
1199         assert(map);
1200
1201         r = bus_message_map_all_properties(m, map, error, userdata);
1202         if (r < 0)
1203                 return r;
1204
1205         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1206         if (r < 0)
1207                 return r;
1208
1209         invalidated = 0;
1210         while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1211                 for (i = 0; map[i].member; i++)
1212                         if (streq(map[i].member, member)) {
1213                                 ++invalidated;
1214                                 break;
1215                         }
1216         if (r < 0)
1217                 return r;
1218
1219         r = sd_bus_message_exit_container(m);
1220         if (r < 0)
1221                 return r;
1222
1223         return invalidated;
1224 }
1225 #endif // 0
1226
1227 int bus_map_all_properties(
1228                 sd_bus *bus,
1229                 const char *destination,
1230                 const char *path,
1231                 const struct bus_properties_map *map,
1232                 sd_bus_error *error,
1233                 void *userdata) {
1234
1235         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1236         int r;
1237
1238         assert(bus);
1239         assert(destination);
1240         assert(path);
1241         assert(map);
1242
1243         r = sd_bus_call_method(
1244                         bus,
1245                         destination,
1246                         path,
1247                         "org.freedesktop.DBus.Properties",
1248                         "GetAll",
1249                         error,
1250                         &m,
1251                         "s", "");
1252         if (r < 0)
1253                 return r;
1254
1255         return bus_message_map_all_properties(m, map, error, userdata);
1256 }
1257
1258 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1259         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1260         int r;
1261
1262         assert(transport >= 0);
1263         assert(transport < _BUS_TRANSPORT_MAX);
1264         assert(ret);
1265
1266         assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1267         assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1268
1269         switch (transport) {
1270
1271         case BUS_TRANSPORT_LOCAL:
1272 #if 0 /// elogind does not support a user bus
1273                 if (user)
1274                         r = sd_bus_default_user(&bus);
1275                 else
1276 #endif // 0
1277                         r = sd_bus_default_system(&bus);
1278
1279                 break;
1280
1281         case BUS_TRANSPORT_REMOTE:
1282                 r = sd_bus_open_system_remote(&bus, host);
1283                 break;
1284
1285         case BUS_TRANSPORT_MACHINE:
1286                 r = sd_bus_open_system_machine(&bus, host);
1287                 break;
1288
1289         default:
1290                 assert_not_reached("Hmm, unknown transport type.");
1291         }
1292         if (r < 0)
1293                 return r;
1294
1295         r = sd_bus_set_exit_on_disconnect(bus, true);
1296         if (r < 0)
1297                 return r;
1298
1299         *ret = bus;
1300         bus = NULL;
1301
1302         return 0;
1303 }
1304
1305 #if 0 /// UNNEEDED by elogind
1306 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1307         int r;
1308
1309         assert(transport >= 0);
1310         assert(transport < _BUS_TRANSPORT_MAX);
1311         assert(bus);
1312
1313         assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1314         assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1315
1316         switch (transport) {
1317
1318         case BUS_TRANSPORT_LOCAL:
1319                 if (user)
1320                         r = bus_connect_user_systemd(bus);
1321                 else
1322                         r = bus_connect_system_systemd(bus);
1323
1324                 break;
1325
1326         case BUS_TRANSPORT_REMOTE:
1327                 r = sd_bus_open_system_remote(bus, host);
1328                 break;
1329
1330         case BUS_TRANSPORT_MACHINE:
1331                 r = sd_bus_open_system_machine(bus, host);
1332                 break;
1333
1334         default:
1335                 assert_not_reached("Hmm, unknown transport type.");
1336         }
1337
1338         return r;
1339 }
1340 #endif // 0
1341
1342 int bus_property_get_bool(
1343                 sd_bus *bus,
1344                 const char *path,
1345                 const char *interface,
1346                 const char *property,
1347                 sd_bus_message *reply,
1348                 void *userdata,
1349                 sd_bus_error *error) {
1350
1351         int b = *(bool*) userdata;
1352
1353         return sd_bus_message_append_basic(reply, 'b', &b);
1354 }
1355
1356 #if 0 /// UNNEEDED by elogind
1357 int bus_property_get_id128(
1358                 sd_bus *bus,
1359                 const char *path,
1360                 const char *interface,
1361                 const char *property,
1362                 sd_bus_message *reply,
1363                 void *userdata,
1364                 sd_bus_error *error) {
1365
1366         sd_id128_t *id = userdata;
1367
1368         if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1369                 return sd_bus_message_append(reply, "ay", 0);
1370         else
1371                 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1372 }
1373 #endif // 0
1374
1375 #if __SIZEOF_SIZE_T__ != 8
1376 int bus_property_get_size(
1377                 sd_bus *bus,
1378                 const char *path,
1379                 const char *interface,
1380                 const char *property,
1381                 sd_bus_message *reply,
1382                 void *userdata,
1383                 sd_bus_error *error) {
1384
1385         uint64_t sz = *(size_t*) userdata;
1386
1387         return sd_bus_message_append_basic(reply, 't', &sz);
1388 }
1389 #endif
1390
1391 #if __SIZEOF_LONG__ != 8
1392 int bus_property_get_long(
1393                 sd_bus *bus,
1394                 const char *path,
1395                 const char *interface,
1396                 const char *property,
1397                 sd_bus_message *reply,
1398                 void *userdata,
1399                 sd_bus_error *error) {
1400
1401         int64_t l = *(long*) userdata;
1402
1403         return sd_bus_message_append_basic(reply, 'x', &l);
1404 }
1405
1406 int bus_property_get_ulong(
1407                 sd_bus *bus,
1408                 const char *path,
1409                 const char *interface,
1410                 const char *property,
1411                 sd_bus_message *reply,
1412                 void *userdata,
1413                 sd_bus_error *error) {
1414
1415         uint64_t ul = *(unsigned long*) userdata;
1416
1417         return sd_bus_message_append_basic(reply, 't', &ul);
1418 }
1419 #endif
1420
1421 int bus_log_parse_error(int r) {
1422         return log_error_errno(r, "Failed to parse bus message: %m");
1423 }
1424
1425 #if 0 /// UNNEEDED by elogind
1426 int bus_log_create_error(int r) {
1427         return log_error_errno(r, "Failed to create bus message: %m");
1428 }
1429
1430 #endif // 0
1431 #if 0 /// UNNEEDED by elogind
1432 /**
1433  * bus_path_encode_unique() - encode unique object path
1434  * @b: bus connection or NULL
1435  * @prefix: object path prefix
1436  * @sender_id: unique-name of client, or NULL
1437  * @external_id: external ID to be chosen by client, or NULL
1438  * @ret_path: storage for encoded object path pointer
1439  *
1440  * Whenever we provide a bus API that allows clients to create and manage
1441  * server-side objects, we need to provide a unique name for these objects. If
1442  * we let the server choose the name, we suffer from a race condition: If a
1443  * client creates an object asynchronously, it cannot destroy that object until
1444  * it received the method reply. It cannot know the name of the new object,
1445  * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1446  *
1447  * Therefore, many APIs allow the client to choose the unique name for newly
1448  * created objects. There're two problems to solve, though:
1449  *    1) Object names are usually defined via dbus object paths, which are
1450  *       usually globally namespaced. Therefore, multiple clients must be able
1451  *       to choose unique object names without interference.
1452  *    2) If multiple libraries share the same bus connection, they must be
1453  *       able to choose unique object names without interference.
1454  * The first problem is solved easily by prefixing a name with the
1455  * unique-bus-name of a connection. The server side must enforce this and
1456  * reject any other name. The second problem is solved by providing unique
1457  * suffixes from within sd-bus.
1458  *
1459  * This helper allows clients to create unique object-paths. It uses the
1460  * template '/prefix/sender_id/external_id' and returns the new path in
1461  * @ret_path (must be freed by the caller).
1462  * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1463  * NULL, this function allocates a unique suffix via @b (by requesting a new
1464  * cookie). If both @sender_id and @external_id are given, @b can be passed as
1465  * NULL.
1466  *
1467  * Returns: 0 on success, negative error code on failure.
1468  */
1469 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1470         _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1471         char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1472         int r;
1473
1474         assert_return(b || (sender_id && external_id), -EINVAL);
1475         assert_return(object_path_is_valid(prefix), -EINVAL);
1476         assert_return(ret_path, -EINVAL);
1477
1478         if (!sender_id) {
1479                 r = sd_bus_get_unique_name(b, &sender_id);
1480                 if (r < 0)
1481                         return r;
1482         }
1483
1484         if (!external_id) {
1485                 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1486                 external_id = external_buf;
1487         }
1488
1489         sender_label = bus_label_escape(sender_id);
1490         if (!sender_label)
1491                 return -ENOMEM;
1492
1493         external_label = bus_label_escape(external_id);
1494         if (!external_label)
1495                 return -ENOMEM;
1496
1497         p = strjoin(prefix, "/", sender_label, "/", external_label);
1498         if (!p)
1499                 return -ENOMEM;
1500
1501         *ret_path = p;
1502         return 0;
1503 }
1504
1505 /**
1506  * bus_path_decode_unique() - decode unique object path
1507  * @path: object path to decode
1508  * @prefix: object path prefix
1509  * @ret_sender: output parameter for sender-id label
1510  * @ret_external: output parameter for external-id label
1511  *
1512  * This does the reverse of bus_path_encode_unique() (see its description for
1513  * details). Both trailing labels, sender-id and external-id, are unescaped and
1514  * returned in the given output parameters (the caller must free them).
1515  *
1516  * Note that this function returns 0 if the path does not match the template
1517  * (see bus_path_encode_unique()), 1 if it matched.
1518  *
1519  * Returns: Negative error code on failure, 0 if the given object path does not
1520  *          match the template (return parameters are set to NULL), 1 if it was
1521  *          parsed successfully (return parameters contain allocated labels).
1522  */
1523 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1524         const char *p, *q;
1525         char *sender, *external;
1526
1527         assert(object_path_is_valid(path));
1528         assert(object_path_is_valid(prefix));
1529         assert(ret_sender);
1530         assert(ret_external);
1531
1532         p = object_path_startswith(path, prefix);
1533         if (!p) {
1534                 *ret_sender = NULL;
1535                 *ret_external = NULL;
1536                 return 0;
1537         }
1538
1539         q = strchr(p, '/');
1540         if (!q) {
1541                 *ret_sender = NULL;
1542                 *ret_external = NULL;
1543                 return 0;
1544         }
1545
1546         sender = bus_label_unescape_n(p, q - p);
1547         external = bus_label_unescape(q + 1);
1548         if (!sender || !external) {
1549                 free(sender);
1550                 free(external);
1551                 return -ENOMEM;
1552         }
1553
1554         *ret_sender = sender;
1555         *ret_external = external;
1556         return 1;
1557 }
1558 #endif // 0
1559
1560 #if 0 /// UNNEEDED by elogind
1561 int bus_property_get_rlimit(
1562                 sd_bus *bus,
1563                 const char *path,
1564                 const char *interface,
1565                 const char *property,
1566                 sd_bus_message *reply,
1567                 void *userdata,
1568                 sd_bus_error *error) {
1569
1570         struct rlimit *rl;
1571         uint64_t u;
1572         rlim_t x;
1573         const char *is_soft;
1574
1575         assert(bus);
1576         assert(reply);
1577         assert(userdata);
1578
1579         is_soft = endswith(property, "Soft");
1580         rl = *(struct rlimit**) userdata;
1581         if (rl)
1582                 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1583         else {
1584                 struct rlimit buf = {};
1585                 int z;
1586                 const char *s;
1587
1588                 s = is_soft ? strndupa(property, is_soft - property) : property;
1589
1590                 z = rlimit_from_string(strstr(s, "Limit"));
1591                 assert(z >= 0);
1592
1593                 getrlimit(z, &buf);
1594                 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1595         }
1596
1597         /* rlim_t might have different sizes, let's map
1598          * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1599          * all archs */
1600         u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1601
1602         return sd_bus_message_append(reply, "t", u);
1603 }
1604
1605 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1606         int r = 0;
1607         char **i;
1608
1609         assert(t);
1610
1611         /* Continues adding after failure, and returns the first failure. */
1612
1613         STRV_FOREACH(i, l) {
1614                 int k;
1615
1616                 k = sd_bus_track_add_name(t, *i);
1617                 if (k < 0 && r >= 0)
1618                         r = k;
1619         }
1620
1621         return r;
1622 }
1623 #endif // 0