chiark / gitweb /
shared/bus-util: format uid==-1 and gid==-1 as [not set]
[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 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef 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 #ifdef ENABLE_POLKIT
558         AsyncPolkitQuery *q;
559
560         while ((q = hashmap_steal_first(registry)))
561                 async_polkit_query_free(q);
562
563         hashmap_free(registry);
564 #endif
565 }
566
567 #if 0 /// UNNEEDED by elogind
568 int bus_check_peercred(sd_bus *c) {
569         struct ucred ucred;
570         socklen_t l;
571         int fd;
572
573         assert(c);
574
575         fd = sd_bus_get_fd(c);
576         if (fd < 0)
577                 return fd;
578
579         l = sizeof(struct ucred);
580         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
581                 return -errno;
582
583         if (l != sizeof(struct ucred))
584                 return -E2BIG;
585
586         if (ucred.uid != 0 && ucred.uid != geteuid())
587                 return -EPERM;
588
589         return 1;
590 }
591
592 int bus_connect_system_systemd(sd_bus **_bus) {
593         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
594         int r;
595
596         assert(_bus);
597
598         if (geteuid() != 0)
599                 return sd_bus_default_system(_bus);
600
601         /* If we are root then let's talk directly to the system
602          * instance, instead of going via the bus */
603
604         r = sd_bus_new(&bus);
605         if (r < 0)
606                 return r;
607
608         r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
609         if (r < 0)
610                 return r;
611
612         r = sd_bus_start(bus);
613         if (r < 0)
614                 return sd_bus_default_system(_bus);
615
616         r = bus_check_peercred(bus);
617         if (r < 0)
618                 return r;
619
620         *_bus = bus;
621         bus = NULL;
622
623         return 0;
624 }
625
626 int bus_connect_user_systemd(sd_bus **_bus) {
627         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
628         _cleanup_free_ char *ee = NULL;
629         const char *e;
630         int r;
631
632         assert(_bus);
633
634         e = secure_getenv("XDG_RUNTIME_DIR");
635         if (!e)
636                 return sd_bus_default_user(_bus);
637
638         ee = bus_address_escape(e);
639         if (!ee)
640                 return -ENOMEM;
641
642         r = sd_bus_new(&bus);
643         if (r < 0)
644                 return r;
645
646         bus->address = strjoin("unix:path=", ee, "/systemd/private");
647         if (!bus->address)
648                 return -ENOMEM;
649
650         r = sd_bus_start(bus);
651         if (r < 0)
652                 return sd_bus_default_user(_bus);
653
654         r = bus_check_peercred(bus);
655         if (r < 0)
656                 return r;
657
658         *_bus = bus;
659         bus = NULL;
660
661         return 0;
662 }
663 #endif // 0
664
665 #define print_property(name, fmt, ...)                                  \
666         do {                                                            \
667                 if (value)                                              \
668                         printf(fmt "\n", __VA_ARGS__);                  \
669                 else                                                    \
670                         printf("%s=" fmt "\n", name, __VA_ARGS__);      \
671         } while(0)
672
673 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
674         char type;
675         const char *contents;
676         int r;
677
678         assert(name);
679         assert(property);
680
681         r = sd_bus_message_peek_type(property, &type, &contents);
682         if (r < 0)
683                 return r;
684
685         switch (type) {
686
687         case SD_BUS_TYPE_STRING: {
688                 const char *s;
689
690                 r = sd_bus_message_read_basic(property, type, &s);
691                 if (r < 0)
692                         return r;
693
694                 if (all || !isempty(s)) {
695                         bool good;
696
697                         /* This property has a single value, so we need to take
698                          * care not to print a new line, everything else is OK. */
699                         good = !strchr(s, '\n');
700                         print_property(name, "%s", good ? s : "[unprintable]");
701                 }
702
703                 return 1;
704         }
705
706         case SD_BUS_TYPE_BOOLEAN: {
707                 int b;
708
709                 r = sd_bus_message_read_basic(property, type, &b);
710                 if (r < 0)
711                         return r;
712
713                 print_property(name, "%s", yes_no(b));
714
715                 return 1;
716         }
717
718         case SD_BUS_TYPE_UINT64: {
719                 uint64_t u;
720
721                 r = sd_bus_message_read_basic(property, type, &u);
722                 if (r < 0)
723                         return r;
724
725                 /* Yes, heuristics! But we can change this check
726                  * should it turn out to not be sufficient */
727
728                 if (endswith(name, "Timestamp")) {
729                         char timestamp[FORMAT_TIMESTAMP_MAX], *t;
730
731                         t = format_timestamp(timestamp, sizeof(timestamp), u);
732                         if (t || all)
733                                 print_property(name, "%s", strempty(t));
734
735                 } else if (strstr(name, "USec")) {
736                         char timespan[FORMAT_TIMESPAN_MAX];
737
738                         print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
739                 } else if (streq(name, "RestrictNamespaces")) {
740                         _cleanup_free_ char *s = NULL;
741                         const char *result;
742
743                         if ((u & NAMESPACE_FLAGS_ALL) == 0)
744                                 result = "yes";
745                         else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
746                                 result = "no";
747                         else {
748                                 r = namespace_flag_to_string_many(u, &s);
749                                 if (r < 0)
750                                         return r;
751
752                                 result = s;
753                         }
754
755                         print_property(name, "%s", result);
756
757                 } else if (streq(name, "MountFlags")) {
758                         const char *result;
759
760                         result = mount_propagation_flags_to_string(u);
761                         if (!result)
762                                 return -EINVAL;
763
764                         print_property(name, "%s", result);
765
766                 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
767                         _cleanup_free_ char *s = NULL;
768
769                         r = capability_set_to_string_alloc(u, &s);
770                         if (r < 0)
771                                 return r;
772
773                         print_property(name, "%s", s);
774
775                 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
776                            (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
777                            (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
778                            (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
779                            (endswith(name, "NSec") && u == (uint64_t) -1))
780
781                         print_property(name, "%s", "[not set]");
782
783                 else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
784                          (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
785                          (startswith(name, "Limit") && u == (uint64_t) -1) ||
786                          (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
787
788                         print_property(name, "%s", "infinity");
789                 else
790                         print_property(name, "%"PRIu64, u);
791
792                 return 1;
793         }
794
795         case SD_BUS_TYPE_INT64: {
796                 int64_t i;
797
798                 r = sd_bus_message_read_basic(property, type, &i);
799                 if (r < 0)
800                         return r;
801
802                 print_property(name, "%"PRIi64, i);
803
804                 return 1;
805         }
806
807         case SD_BUS_TYPE_UINT32: {
808                 uint32_t u;
809
810                 r = sd_bus_message_read_basic(property, type, &u);
811                 if (r < 0)
812                         return r;
813
814                 if (strstr(name, "UMask") || strstr(name, "Mode"))
815                         print_property(name, "%04o", u);
816                 else if (streq(name, "UID")) {
817                         if (u == UID_INVALID)
818                                 print_property(name, "%s", "[not set]");
819                         else
820                                 print_property(name, "%"PRIu32, u);
821                 } else if (streq(name, "GID")) {
822                         if (u == GID_INVALID)
823                                 print_property(name, "%s", "[not set]");
824                         else
825                                 print_property(name, "%"PRIu32, u);
826                 } else
827                         print_property(name, "%"PRIu32, u);
828
829                 return 1;
830         }
831
832         case SD_BUS_TYPE_INT32: {
833                 int32_t i;
834
835                 r = sd_bus_message_read_basic(property, type, &i);
836                 if (r < 0)
837                         return r;
838
839                 print_property(name, "%"PRIi32, i);
840                 return 1;
841         }
842
843         case SD_BUS_TYPE_DOUBLE: {
844                 double d;
845
846                 r = sd_bus_message_read_basic(property, type, &d);
847                 if (r < 0)
848                         return r;
849
850                 print_property(name, "%g", d);
851                 return 1;
852         }
853
854         case SD_BUS_TYPE_ARRAY:
855                 if (streq(contents, "s")) {
856                         bool first = true;
857                         const char *str;
858
859                         r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
860                         if (r < 0)
861                                 return r;
862
863                         while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
864                                 bool good;
865
866                                 if (first && !value)
867                                         printf("%s=", name);
868
869                                 /* This property has multiple space-separated values, so
870                                  * neither spaces not newlines can be allowed in a value. */
871                                 good = str[strcspn(str, " \n")] == '\0';
872
873                                 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
874
875                                 first = false;
876                         }
877                         if (r < 0)
878                                 return r;
879
880                         if (first && all && !value)
881                                 printf("%s=", name);
882                         if (!first || all)
883                                 puts("");
884
885                         r = sd_bus_message_exit_container(property);
886                         if (r < 0)
887                                 return r;
888
889                         return 1;
890
891                 } else if (streq(contents, "y")) {
892                         const uint8_t *u;
893                         size_t n;
894
895                         r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
896                         if (r < 0)
897                                 return r;
898
899                         if (all || n > 0) {
900                                 unsigned int i;
901
902                                 if (!value)
903                                         printf("%s=", name);
904
905                                 for (i = 0; i < n; i++)
906                                         printf("%02x", u[i]);
907
908                                 puts("");
909                         }
910
911                         return 1;
912
913                 } else if (streq(contents, "u")) {
914                         uint32_t *u;
915                         size_t n;
916
917                         r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
918                         if (r < 0)
919                                 return r;
920
921                         if (all || n > 0) {
922                                 unsigned int i;
923
924                                 if (!value)
925                                         printf("%s=", name);
926
927                                 for (i = 0; i < n; i++)
928                                         printf("%08x", u[i]);
929
930                                 puts("");
931                         }
932
933                         return 1;
934                 }
935
936                 break;
937         }
938
939         return 0;
940 }
941
942 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
943         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
944         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
945         int r;
946
947         assert(bus);
948         assert(path);
949
950         r = sd_bus_call_method(bus,
951                         dest,
952                         path,
953                         "org.freedesktop.DBus.Properties",
954                         "GetAll",
955                         &error,
956                         &reply,
957                         "s", "");
958         if (r < 0)
959                 return r;
960
961         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
962         if (r < 0)
963                 return r;
964
965         while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
966                 const char *name;
967                 const char *contents;
968
969                 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
970                 if (r < 0)
971                         return r;
972
973                 if (!filter || strv_find(filter, name)) {
974                         r = sd_bus_message_peek_type(reply, NULL, &contents);
975                         if (r < 0)
976                                 return r;
977
978                         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
979                         if (r < 0)
980                                 return r;
981
982                         r = bus_print_property(name, reply, value, all);
983                         if (r < 0)
984                                 return r;
985                         if (r == 0) {
986                                 if (all)
987                                         printf("%s=[unprintable]\n", name);
988                                 /* skip what we didn't read */
989                                 r = sd_bus_message_skip(reply, contents);
990                                 if (r < 0)
991                                         return r;
992                         }
993
994                         r = sd_bus_message_exit_container(reply);
995                         if (r < 0)
996                                 return r;
997                 } else {
998                         r = sd_bus_message_skip(reply, "v");
999                         if (r < 0)
1000                                 return r;
1001                 }
1002
1003                 r = sd_bus_message_exit_container(reply);
1004                 if (r < 0)
1005                         return r;
1006         }
1007         if (r < 0)
1008                 return r;
1009
1010         r = sd_bus_message_exit_container(reply);
1011         if (r < 0)
1012                 return r;
1013
1014         return 0;
1015 }
1016
1017 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1018         sd_id128_t *p = userdata;
1019         const void *v;
1020         size_t n;
1021         int r;
1022
1023         r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1024         if (r < 0)
1025                 return r;
1026
1027         if (n == 0)
1028                 *p = SD_ID128_NULL;
1029         else if (n == 16)
1030                 memcpy((*p).bytes, v, n);
1031         else
1032                 return -EINVAL;
1033
1034         return 0;
1035 }
1036
1037 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1038         char type;
1039         int r;
1040
1041         r = sd_bus_message_peek_type(m, &type, NULL);
1042         if (r < 0)
1043                 return r;
1044
1045         switch (type) {
1046
1047         case SD_BUS_TYPE_STRING: {
1048                 char **p = userdata;
1049                 const char *s;
1050
1051                 r = sd_bus_message_read_basic(m, type, &s);
1052                 if (r < 0)
1053                         return r;
1054
1055                 if (isempty(s))
1056                         s = NULL;
1057
1058                 return free_and_strdup(p, s);
1059         }
1060
1061         case SD_BUS_TYPE_ARRAY: {
1062                 _cleanup_strv_free_ char **l = NULL;
1063                 char ***p = userdata;
1064
1065                 r = bus_message_read_strv_extend(m, &l);
1066                 if (r < 0)
1067                         return r;
1068
1069                 strv_free(*p);
1070                 *p = l;
1071                 l = NULL;
1072                 return 0;
1073         }
1074
1075         case SD_BUS_TYPE_BOOLEAN: {
1076                 unsigned b;
1077                 int *p = userdata;
1078
1079                 r = sd_bus_message_read_basic(m, type, &b);
1080                 if (r < 0)
1081                         return r;
1082
1083                 *p = b;
1084                 return 0;
1085         }
1086
1087         case SD_BUS_TYPE_INT32:
1088         case SD_BUS_TYPE_UINT32: {
1089                 uint32_t u, *p = userdata;
1090
1091                 r = sd_bus_message_read_basic(m, type, &u);
1092                 if (r < 0)
1093                         return r;
1094
1095                 *p = u;
1096                 return 0;
1097         }
1098
1099         case SD_BUS_TYPE_INT64:
1100         case SD_BUS_TYPE_UINT64: {
1101                 uint64_t t, *p = userdata;
1102
1103                 r = sd_bus_message_read_basic(m, type, &t);
1104                 if (r < 0)
1105                         return r;
1106
1107                 *p = t;
1108                 return 0;
1109         }
1110
1111         case SD_BUS_TYPE_DOUBLE: {
1112                 double d, *p = userdata;
1113
1114                 r = sd_bus_message_read_basic(m, type, &d);
1115                 if (r < 0)
1116                         return r;
1117
1118                 *p = d;
1119                 return 0;
1120         }}
1121
1122         return -EOPNOTSUPP;
1123 }
1124
1125 int bus_message_map_all_properties(
1126                 sd_bus_message *m,
1127                 const struct bus_properties_map *map,
1128                 sd_bus_error *error,
1129                 void *userdata) {
1130
1131         int r;
1132
1133         assert(m);
1134         assert(map);
1135
1136         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1137         if (r < 0)
1138                 return r;
1139
1140         while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1141                 const struct bus_properties_map *prop;
1142                 const char *member;
1143                 const char *contents;
1144                 void *v;
1145                 unsigned i;
1146
1147                 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1148                 if (r < 0)
1149                         return r;
1150
1151                 for (i = 0, prop = NULL; map[i].member; i++)
1152                         if (streq(map[i].member, member)) {
1153                                 prop = &map[i];
1154                                 break;
1155                         }
1156
1157                 if (prop) {
1158                         r = sd_bus_message_peek_type(m, NULL, &contents);
1159                         if (r < 0)
1160                                 return r;
1161
1162                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1163                         if (r < 0)
1164                                 return r;
1165
1166                         v = (uint8_t *)userdata + prop->offset;
1167                         if (map[i].set)
1168                                 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1169                         else
1170                                 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v);
1171                         if (r < 0)
1172                                 return r;
1173
1174                         r = sd_bus_message_exit_container(m);
1175                         if (r < 0)
1176                                 return r;
1177                 } else {
1178                         r = sd_bus_message_skip(m, "v");
1179                         if (r < 0)
1180                                 return r;
1181                 }
1182
1183                 r = sd_bus_message_exit_container(m);
1184                 if (r < 0)
1185                         return r;
1186         }
1187         if (r < 0)
1188                 return r;
1189
1190         return sd_bus_message_exit_container(m);
1191 }
1192
1193 #if 0 /// UNNEEDED by elogind
1194 int bus_message_map_properties_changed(
1195                 sd_bus_message *m,
1196                 const struct bus_properties_map *map,
1197                 sd_bus_error *error,
1198                 void *userdata) {
1199
1200         const char *member;
1201         int r, invalidated, i;
1202
1203         assert(m);
1204         assert(map);
1205
1206         r = bus_message_map_all_properties(m, map, error, userdata);
1207         if (r < 0)
1208                 return r;
1209
1210         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1211         if (r < 0)
1212                 return r;
1213
1214         invalidated = 0;
1215         while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1216                 for (i = 0; map[i].member; i++)
1217                         if (streq(map[i].member, member)) {
1218                                 ++invalidated;
1219                                 break;
1220                         }
1221         if (r < 0)
1222                 return r;
1223
1224         r = sd_bus_message_exit_container(m);
1225         if (r < 0)
1226                 return r;
1227
1228         return invalidated;
1229 }
1230 #endif // 0
1231
1232 int bus_map_all_properties(
1233                 sd_bus *bus,
1234                 const char *destination,
1235                 const char *path,
1236                 const struct bus_properties_map *map,
1237                 sd_bus_error *error,
1238                 void *userdata) {
1239
1240         _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1241         int r;
1242
1243         assert(bus);
1244         assert(destination);
1245         assert(path);
1246         assert(map);
1247
1248         r = sd_bus_call_method(
1249                         bus,
1250                         destination,
1251                         path,
1252                         "org.freedesktop.DBus.Properties",
1253                         "GetAll",
1254                         error,
1255                         &m,
1256                         "s", "");
1257         if (r < 0)
1258                 return r;
1259
1260         return bus_message_map_all_properties(m, map, error, userdata);
1261 }
1262
1263 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1264         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1265         int r;
1266
1267         assert(transport >= 0);
1268         assert(transport < _BUS_TRANSPORT_MAX);
1269         assert(ret);
1270
1271         assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1272         assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1273
1274         switch (transport) {
1275
1276         case BUS_TRANSPORT_LOCAL:
1277 #if 0 /// elogind does not support a user bus
1278                 if (user)
1279                         r = sd_bus_default_user(&bus);
1280                 else
1281 #endif // 0
1282                         r = sd_bus_default_system(&bus);
1283
1284                 break;
1285
1286         case BUS_TRANSPORT_REMOTE:
1287                 r = sd_bus_open_system_remote(&bus, host);
1288                 break;
1289
1290         case BUS_TRANSPORT_MACHINE:
1291                 r = sd_bus_open_system_machine(&bus, host);
1292                 break;
1293
1294         default:
1295                 assert_not_reached("Hmm, unknown transport type.");
1296         }
1297         if (r < 0)
1298                 return r;
1299
1300         r = sd_bus_set_exit_on_disconnect(bus, true);
1301         if (r < 0)
1302                 return r;
1303
1304         *ret = bus;
1305         bus = NULL;
1306
1307         return 0;
1308 }
1309
1310 #if 0 /// UNNEEDED by elogind
1311 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1312         int r;
1313
1314         assert(transport >= 0);
1315         assert(transport < _BUS_TRANSPORT_MAX);
1316         assert(bus);
1317
1318         assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1319         assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1320
1321         switch (transport) {
1322
1323         case BUS_TRANSPORT_LOCAL:
1324                 if (user)
1325                         r = bus_connect_user_systemd(bus);
1326                 else
1327                         r = bus_connect_system_systemd(bus);
1328
1329                 break;
1330
1331         case BUS_TRANSPORT_REMOTE:
1332                 r = sd_bus_open_system_remote(bus, host);
1333                 break;
1334
1335         case BUS_TRANSPORT_MACHINE:
1336                 r = sd_bus_open_system_machine(bus, host);
1337                 break;
1338
1339         default:
1340                 assert_not_reached("Hmm, unknown transport type.");
1341         }
1342
1343         return r;
1344 }
1345 #endif // 0
1346
1347 int bus_property_get_bool(
1348                 sd_bus *bus,
1349                 const char *path,
1350                 const char *interface,
1351                 const char *property,
1352                 sd_bus_message *reply,
1353                 void *userdata,
1354                 sd_bus_error *error) {
1355
1356         int b = *(bool*) userdata;
1357
1358         return sd_bus_message_append_basic(reply, 'b', &b);
1359 }
1360
1361 #if 0 /// UNNEEDED by elogind
1362 int bus_property_get_id128(
1363                 sd_bus *bus,
1364                 const char *path,
1365                 const char *interface,
1366                 const char *property,
1367                 sd_bus_message *reply,
1368                 void *userdata,
1369                 sd_bus_error *error) {
1370
1371         sd_id128_t *id = userdata;
1372
1373         if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1374                 return sd_bus_message_append(reply, "ay", 0);
1375         else
1376                 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1377 }
1378 #endif // 0
1379
1380 #if __SIZEOF_SIZE_T__ != 8
1381 int bus_property_get_size(
1382                 sd_bus *bus,
1383                 const char *path,
1384                 const char *interface,
1385                 const char *property,
1386                 sd_bus_message *reply,
1387                 void *userdata,
1388                 sd_bus_error *error) {
1389
1390         uint64_t sz = *(size_t*) userdata;
1391
1392         return sd_bus_message_append_basic(reply, 't', &sz);
1393 }
1394 #endif
1395
1396 #if __SIZEOF_LONG__ != 8
1397 int bus_property_get_long(
1398                 sd_bus *bus,
1399                 const char *path,
1400                 const char *interface,
1401                 const char *property,
1402                 sd_bus_message *reply,
1403                 void *userdata,
1404                 sd_bus_error *error) {
1405
1406         int64_t l = *(long*) userdata;
1407
1408         return sd_bus_message_append_basic(reply, 'x', &l);
1409 }
1410
1411 int bus_property_get_ulong(
1412                 sd_bus *bus,
1413                 const char *path,
1414                 const char *interface,
1415                 const char *property,
1416                 sd_bus_message *reply,
1417                 void *userdata,
1418                 sd_bus_error *error) {
1419
1420         uint64_t ul = *(unsigned long*) userdata;
1421
1422         return sd_bus_message_append_basic(reply, 't', &ul);
1423 }
1424 #endif
1425
1426 int bus_log_parse_error(int r) {
1427         return log_error_errno(r, "Failed to parse bus message: %m");
1428 }
1429
1430 #if 0 /// UNNEEDED by elogind
1431 int bus_log_create_error(int r) {
1432         return log_error_errno(r, "Failed to create bus message: %m");
1433 }
1434
1435 #endif // 0
1436 #if 0 /// UNNEEDED by elogind
1437 /**
1438  * bus_path_encode_unique() - encode unique object path
1439  * @b: bus connection or NULL
1440  * @prefix: object path prefix
1441  * @sender_id: unique-name of client, or NULL
1442  * @external_id: external ID to be chosen by client, or NULL
1443  * @ret_path: storage for encoded object path pointer
1444  *
1445  * Whenever we provide a bus API that allows clients to create and manage
1446  * server-side objects, we need to provide a unique name for these objects. If
1447  * we let the server choose the name, we suffer from a race condition: If a
1448  * client creates an object asynchronously, it cannot destroy that object until
1449  * it received the method reply. It cannot know the name of the new object,
1450  * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1451  *
1452  * Therefore, many APIs allow the client to choose the unique name for newly
1453  * created objects. There're two problems to solve, though:
1454  *    1) Object names are usually defined via dbus object paths, which are
1455  *       usually globally namespaced. Therefore, multiple clients must be able
1456  *       to choose unique object names without interference.
1457  *    2) If multiple libraries share the same bus connection, they must be
1458  *       able to choose unique object names without interference.
1459  * The first problem is solved easily by prefixing a name with the
1460  * unique-bus-name of a connection. The server side must enforce this and
1461  * reject any other name. The second problem is solved by providing unique
1462  * suffixes from within sd-bus.
1463  *
1464  * This helper allows clients to create unique object-paths. It uses the
1465  * template '/prefix/sender_id/external_id' and returns the new path in
1466  * @ret_path (must be freed by the caller).
1467  * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1468  * NULL, this function allocates a unique suffix via @b (by requesting a new
1469  * cookie). If both @sender_id and @external_id are given, @b can be passed as
1470  * NULL.
1471  *
1472  * Returns: 0 on success, negative error code on failure.
1473  */
1474 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1475         _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1476         char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1477         int r;
1478
1479         assert_return(b || (sender_id && external_id), -EINVAL);
1480         assert_return(object_path_is_valid(prefix), -EINVAL);
1481         assert_return(ret_path, -EINVAL);
1482
1483         if (!sender_id) {
1484                 r = sd_bus_get_unique_name(b, &sender_id);
1485                 if (r < 0)
1486                         return r;
1487         }
1488
1489         if (!external_id) {
1490                 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1491                 external_id = external_buf;
1492         }
1493
1494         sender_label = bus_label_escape(sender_id);
1495         if (!sender_label)
1496                 return -ENOMEM;
1497
1498         external_label = bus_label_escape(external_id);
1499         if (!external_label)
1500                 return -ENOMEM;
1501
1502         p = strjoin(prefix, "/", sender_label, "/", external_label);
1503         if (!p)
1504                 return -ENOMEM;
1505
1506         *ret_path = p;
1507         return 0;
1508 }
1509
1510 /**
1511  * bus_path_decode_unique() - decode unique object path
1512  * @path: object path to decode
1513  * @prefix: object path prefix
1514  * @ret_sender: output parameter for sender-id label
1515  * @ret_external: output parameter for external-id label
1516  *
1517  * This does the reverse of bus_path_encode_unique() (see its description for
1518  * details). Both trailing labels, sender-id and external-id, are unescaped and
1519  * returned in the given output parameters (the caller must free them).
1520  *
1521  * Note that this function returns 0 if the path does not match the template
1522  * (see bus_path_encode_unique()), 1 if it matched.
1523  *
1524  * Returns: Negative error code on failure, 0 if the given object path does not
1525  *          match the template (return parameters are set to NULL), 1 if it was
1526  *          parsed successfully (return parameters contain allocated labels).
1527  */
1528 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1529         const char *p, *q;
1530         char *sender, *external;
1531
1532         assert(object_path_is_valid(path));
1533         assert(object_path_is_valid(prefix));
1534         assert(ret_sender);
1535         assert(ret_external);
1536
1537         p = object_path_startswith(path, prefix);
1538         if (!p) {
1539                 *ret_sender = NULL;
1540                 *ret_external = NULL;
1541                 return 0;
1542         }
1543
1544         q = strchr(p, '/');
1545         if (!q) {
1546                 *ret_sender = NULL;
1547                 *ret_external = NULL;
1548                 return 0;
1549         }
1550
1551         sender = bus_label_unescape_n(p, q - p);
1552         external = bus_label_unescape(q + 1);
1553         if (!sender || !external) {
1554                 free(sender);
1555                 free(external);
1556                 return -ENOMEM;
1557         }
1558
1559         *ret_sender = sender;
1560         *ret_external = external;
1561         return 1;
1562 }
1563 #endif // 0
1564
1565 #if 0 /// UNNEEDED by elogind
1566 int bus_property_get_rlimit(
1567                 sd_bus *bus,
1568                 const char *path,
1569                 const char *interface,
1570                 const char *property,
1571                 sd_bus_message *reply,
1572                 void *userdata,
1573                 sd_bus_error *error) {
1574
1575         struct rlimit *rl;
1576         uint64_t u;
1577         rlim_t x;
1578         const char *is_soft;
1579
1580         assert(bus);
1581         assert(reply);
1582         assert(userdata);
1583
1584         is_soft = endswith(property, "Soft");
1585         rl = *(struct rlimit**) userdata;
1586         if (rl)
1587                 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1588         else {
1589                 struct rlimit buf = {};
1590                 int z;
1591                 const char *s;
1592
1593                 s = is_soft ? strndupa(property, is_soft - property) : property;
1594
1595                 z = rlimit_from_string(strstr(s, "Limit"));
1596                 assert(z >= 0);
1597
1598                 getrlimit(z, &buf);
1599                 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1600         }
1601
1602         /* rlim_t might have different sizes, let's map
1603          * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1604          * all archs */
1605         u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1606
1607         return sd_bus_message_append(reply, "t", u);
1608 }
1609
1610 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1611         int r = 0;
1612         char **i;
1613
1614         assert(t);
1615
1616         /* Continues adding after failure, and returns the first failure. */
1617
1618         STRV_FOREACH(i, l) {
1619                 int k;
1620
1621                 k = sd_bus_track_add_name(t, *i);
1622                 if (k < 0 && r >= 0)
1623                         r = k;
1624         }
1625
1626         return r;
1627 }
1628 #endif // 0