chiark / gitweb /
19d97b7e0016b5ef86608e146caef980e133deac
[elogind.git] / src / libsystemd-bus / bus-kernel.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #ifdef HAVE_VALGRIND_MEMCHECK_H
23 #include <valgrind/memcheck.h>
24 #endif
25
26 #include <fcntl.h>
27 #include <malloc.h>
28 #include <sys/mman.h>
29
30 #include "util.h"
31 #include "strv.h"
32
33 #include "bus-internal.h"
34 #include "bus-message.h"
35 #include "bus-kernel.h"
36 #include "bus-bloom.h"
37 #include "bus-util.h"
38
39 #define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t))
40
41 int bus_kernel_parse_unique_name(const char *s, uint64_t *id) {
42         int r;
43
44         assert(s);
45         assert(id);
46
47         if (!startswith(s, ":1."))
48                 return 0;
49
50         r = safe_atou64(s + 3, id);
51         if (r < 0)
52                 return r;
53
54         return 1;
55 }
56
57 static void append_payload_vec(struct kdbus_item **d, const void *p, size_t sz) {
58         assert(d);
59         assert(sz > 0);
60
61         *d = ALIGN8_PTR(*d);
62
63         /* Note that p can be NULL, which encodes a region full of
64          * zeroes, which is useful to optimize certain padding
65          * conditions */
66
67         (*d)->size = offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec);
68         (*d)->type = KDBUS_ITEM_PAYLOAD_VEC;
69         (*d)->vec.address = PTR_TO_UINT64(p);
70         (*d)->vec.size = sz;
71
72         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
73 }
74
75 static void append_payload_memfd(struct kdbus_item **d, int memfd, size_t sz) {
76         assert(d);
77         assert(memfd >= 0);
78         assert(sz > 0);
79
80         *d = ALIGN8_PTR(*d);
81         (*d)->size = offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd);
82         (*d)->type = KDBUS_ITEM_PAYLOAD_MEMFD;
83         (*d)->memfd.fd = memfd;
84         (*d)->memfd.size = sz;
85
86         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
87 }
88
89 static void append_destination(struct kdbus_item **d, const char *s, size_t length) {
90         assert(d);
91         assert(s);
92
93         *d = ALIGN8_PTR(*d);
94
95         (*d)->size = offsetof(struct kdbus_item, str) + length + 1;
96         (*d)->type = KDBUS_ITEM_DST_NAME;
97         memcpy((*d)->str, s, length + 1);
98
99         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
100 }
101
102 static void* append_bloom(struct kdbus_item **d, size_t length) {
103         void *r;
104
105         assert(d);
106
107         *d = ALIGN8_PTR(*d);
108
109         (*d)->size = offsetof(struct kdbus_item, data) + length;
110         (*d)->type = KDBUS_ITEM_BLOOM;
111         r = (*d)->data;
112
113         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
114
115         return r;
116 }
117
118 static void append_fds(struct kdbus_item **d, const int fds[], unsigned n_fds) {
119         assert(d);
120         assert(fds);
121         assert(n_fds > 0);
122
123         *d = ALIGN8_PTR(*d);
124         (*d)->size = offsetof(struct kdbus_item, fds) + sizeof(int) * n_fds;
125         (*d)->type = KDBUS_ITEM_FDS;
126         memcpy((*d)->fds, fds, sizeof(int) * n_fds);
127
128         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
129 }
130
131 static int bus_message_setup_bloom(sd_bus_message *m, void *bloom) {
132         unsigned i;
133         int r;
134
135         assert(m);
136         assert(bloom);
137
138         memset(bloom, 0, BLOOM_SIZE);
139
140         bloom_add_pair(bloom, "message-type", bus_message_type_to_string(m->header->type));
141
142         if (m->interface)
143                 bloom_add_pair(bloom, "interface", m->interface);
144         if (m->member)
145                 bloom_add_pair(bloom, "member", m->member);
146         if (m->path) {
147                 bloom_add_pair(bloom, "path", m->path);
148                 bloom_add_pair(bloom, "path-slash-prefix", m->path);
149                 bloom_add_prefixes(bloom, "path-slash-prefix", m->path, '/');
150         }
151
152         r = sd_bus_message_rewind(m, true);
153         if (r < 0)
154                 return r;
155
156         for (i = 0; i < 64; i++) {
157                 char type;
158                 const char *t;
159                 char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
160                 char *e;
161
162                 r = sd_bus_message_peek_type(m, &type, NULL);
163                 if (r < 0)
164                         return r;
165
166                 if (type != SD_BUS_TYPE_STRING &&
167                     type != SD_BUS_TYPE_OBJECT_PATH &&
168                     type != SD_BUS_TYPE_SIGNATURE)
169                         break;
170
171                 r = sd_bus_message_read_basic(m, type, &t);
172                 if (r < 0)
173                         return r;
174
175                 e = stpcpy(buf, "arg");
176                 if (i < 10)
177                         *(e++) = '0' + i;
178                 else {
179                         *(e++) = '0' + (i / 10);
180                         *(e++) = '0' + (i % 10);
181                 }
182
183                 *e = 0;
184                 bloom_add_pair(bloom, buf, t);
185
186                 strcpy(e, "-dot-prefix");
187                 bloom_add_prefixes(bloom, buf, t, '.');
188                 strcpy(e, "-slash-prefix");
189                 bloom_add_prefixes(bloom, buf, t, '/');
190         }
191
192         return 0;
193 }
194
195 static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
196         struct bus_body_part *part;
197         struct kdbus_item *d;
198         bool well_known;
199         uint64_t unique;
200         size_t sz, dl;
201         unsigned i;
202         int r;
203
204         assert(b);
205         assert(m);
206         assert(m->sealed);
207
208         if (m->kdbus)
209                 return 0;
210
211         if (m->destination) {
212                 r = bus_kernel_parse_unique_name(m->destination, &unique);
213                 if (r < 0)
214                         return r;
215
216                 well_known = r == 0;
217         } else
218                 well_known = false;
219
220         sz = offsetof(struct kdbus_msg, items);
221
222         assert_cc(ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec)) ==
223                   ALIGN8(offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd)));
224
225         /* Add in fixed header, fields header and payload */
226         sz += (1 + m->n_body_parts) *
227                 ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec));
228
229         /* Add space for bloom filter */
230         sz += ALIGN8(offsetof(struct kdbus_item, data) + BLOOM_SIZE);
231
232         /* Add in well-known destination header */
233         if (well_known) {
234                 dl = strlen(m->destination);
235                 sz += ALIGN8(offsetof(struct kdbus_item, str) + dl + 1);
236         }
237
238         /* Add space for unix fds */
239         if (m->n_fds > 0)
240                 sz += ALIGN8(offsetof(struct kdbus_item, fds) + sizeof(int)*m->n_fds);
241
242         m->kdbus = memalign(8, sz);
243         if (!m->kdbus) {
244                 r = -ENOMEM;
245                 goto fail;
246         }
247
248         m->free_kdbus = true;
249         memset(m->kdbus, 0, sz);
250
251         m->kdbus->flags =
252                 ((m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED) ? 0 : KDBUS_MSG_FLAGS_EXPECT_REPLY) |
253                 ((m->header->flags & BUS_MESSAGE_NO_AUTO_START) ? KDBUS_MSG_FLAGS_NO_AUTO_START : 0);
254         m->kdbus->dst_id =
255                 well_known ? 0 :
256                 m->destination ? unique : KDBUS_DST_ID_BROADCAST;
257         m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS;
258         m->kdbus->cookie = m->header->serial;
259
260         m->kdbus->timeout_ns = m->timeout * NSEC_PER_USEC;
261
262         d = m->kdbus->items;
263
264         if (well_known)
265                 append_destination(&d, m->destination, dl);
266
267         append_payload_vec(&d, m->header, BUS_MESSAGE_BODY_BEGIN(m));
268
269         MESSAGE_FOREACH_PART(part, i, m) {
270                 if (part->is_zero) {
271                         /* If this is padding then simply send a
272                          * vector with a NULL data pointer which the
273                          * kernel will just pass through. This is the
274                          * most efficient way to encode zeroes */
275
276                         append_payload_vec(&d, NULL, part->size);
277                         continue;
278                 }
279
280                 if (part->memfd >= 0 && part->sealed && m->destination) {
281                         /* Try to send a memfd, if the part is
282                          * sealed and this is not a broadcast. Since we can only  */
283
284                         append_payload_memfd(&d, part->memfd, part->size);
285                         continue;
286                 }
287
288                 /* Otherwise let's send a vector to the actual data,
289                  * for that we need to map it first. */
290                 r = bus_body_part_map(part);
291                 if (r < 0)
292                         goto fail;
293
294                 append_payload_vec(&d, part->data, part->size);
295         }
296
297         if (m->kdbus->dst_id == KDBUS_DST_ID_BROADCAST) {
298                 void *p;
299
300                 p = append_bloom(&d, BLOOM_SIZE);
301                 r = bus_message_setup_bloom(m, p);
302                 if (r < 0)
303                         goto fail;
304         }
305
306         if (m->n_fds > 0)
307                 append_fds(&d, m->fds, m->n_fds);
308
309         m->kdbus->size = (uint8_t*) d - (uint8_t*) m->kdbus;
310         assert(m->kdbus->size <= sz);
311
312         return 0;
313
314 fail:
315         m->poisoned = true;
316         return r;
317 }
318
319 int bus_kernel_take_fd(sd_bus *b) {
320         struct kdbus_cmd_hello *hello;
321         struct kdbus_item *item;
322         size_t l = 0, sz;
323         int r;
324
325         assert(b);
326
327         if (b->is_server)
328                 return -EINVAL;
329
330         b->use_memfd = 1;
331
332         sz = ALIGN8(offsetof(struct kdbus_cmd_hello, items));
333
334         if (b->fake_creds_valid)
335                 sz += ALIGN8(offsetof(struct kdbus_item, creds)) + sizeof(struct kdbus_creds);
336
337         if (b->fake_label) {
338                 l = strlen(b->fake_label);
339                 sz += ALIGN8(offsetof(struct kdbus_item, str) + l + 1);
340         }
341
342         hello = alloca0(sz);
343         hello->size = sz;
344         hello->conn_flags = b->hello_flags;
345         hello->attach_flags = b->attach_flags;
346         hello->pool_size = KDBUS_POOL_SIZE;
347
348         item = hello->items;
349
350         if (b->fake_creds_valid) {
351                 item->size = offsetof(struct kdbus_item, creds) + sizeof(struct kdbus_creds);
352                 item->type = KDBUS_ITEM_CREDS;
353                 item->creds = b->fake_creds;
354
355                 item = KDBUS_ITEM_NEXT(item);
356         }
357
358         if (b->fake_label) {
359                 item->size = offsetof(struct kdbus_item, str) + l + 1;
360                 memcpy(item->str, b->fake_label, l+1);
361         }
362
363         r = ioctl(b->input_fd, KDBUS_CMD_HELLO, hello);
364         if (r < 0)
365                 return -errno;
366
367         if (!b->kdbus_buffer) {
368                 b->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ, MAP_SHARED, b->input_fd, 0);
369                 if (b->kdbus_buffer == MAP_FAILED) {
370                         b->kdbus_buffer = NULL;
371                         return -errno;
372                 }
373         }
374
375         /* The higher 32bit of both flags fields are considered
376          * 'incompatible flags'. Refuse them all for now. */
377         if (hello->bus_flags > 0xFFFFFFFFULL ||
378             hello->conn_flags > 0xFFFFFFFFULL)
379                 return -ENOTSUP;
380
381         if (hello->bloom_size != BLOOM_SIZE)
382                 return -ENOTSUP;
383
384         if (asprintf(&b->unique_name, ":1.%llu", (unsigned long long) hello->id) < 0)
385                 return -ENOMEM;
386
387         b->unique_id = hello->id;
388
389         b->is_kernel = true;
390         b->bus_client = true;
391         b->can_fds = !!(hello->conn_flags & KDBUS_HELLO_ACCEPT_FD);
392         b->message_version = 2;
393         b->message_endian = BUS_NATIVE_ENDIAN;
394
395         /* the kernel told us the UUID of the underlying bus */
396         memcpy(b->server_id.bytes, hello->id128, sizeof(b->server_id.bytes));
397
398         return bus_start_running(b);
399 }
400
401 int bus_kernel_connect(sd_bus *b) {
402         assert(b);
403         assert(b->input_fd < 0);
404         assert(b->output_fd < 0);
405         assert(b->kernel);
406
407         if (b->is_server)
408                 return -EINVAL;
409
410         b->input_fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
411         if (b->input_fd < 0)
412                 return -errno;
413
414         b->output_fd = b->input_fd;
415
416         return bus_kernel_take_fd(b);
417 }
418
419 int bus_kernel_write_message(sd_bus *bus, sd_bus_message *m) {
420         int r;
421
422         assert(bus);
423         assert(m);
424         assert(bus->state == BUS_RUNNING);
425
426         /* If we can't deliver, we want room for the error message */
427         r = bus_rqueue_make_room(bus);
428         if (r < 0)
429                 return r;
430
431         r = bus_message_setup_kmsg(bus, m);
432         if (r < 0)
433                 return r;
434
435         r = ioctl(bus->output_fd, KDBUS_CMD_MSG_SEND, m->kdbus);
436         if (r < 0) {
437                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
438                 sd_bus_message *reply;
439
440                 if (errno == EAGAIN || errno == EINTR)
441                         return 0;
442                 else if (errno == ENXIO || errno == ESRCH) {
443
444                         /* ENXIO: unique name not known
445                          * ESRCH: well-known name not known */
446
447                         if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
448                                 sd_bus_error_setf(&error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Destination %s not known", m->destination);
449                         else {
450                                 log_debug("Could not deliver message to %s as destination is not known. Ignoring.", m->destination);
451                                 return 0;
452                         }
453
454                 } else if (errno == EADDRNOTAVAIL) {
455
456                         /* EADDRNOTAVAIL: activation is possible, but turned off in request flags */
457
458                         if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
459                                 sd_bus_error_setf(&error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Activation of %s not requested", m->destination);
460                         else {
461                                 log_debug("Could not deliver message to %s as destination is not activated. Ignoring.", m->destination);
462                                 return 0;
463                         }
464                 } else
465                         return -errno;
466
467                 r = bus_message_new_synthetic_error(
468                                 bus,
469                                 BUS_MESSAGE_SERIAL(m),
470                                 &error,
471                                 &reply);
472
473                 if (r < 0)
474                         return r;
475
476                 r = bus_seal_synthetic_message(bus, reply);
477                 if (r < 0)
478                         return r;
479
480                 bus->rqueue[bus->rqueue_size++] = reply;
481
482                 return 0;
483         }
484
485         return 1;
486 }
487
488 static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) {
489         uint64_t off;
490         struct kdbus_item *d;
491
492         assert(bus);
493         assert(k);
494
495         off = (uint8_t *)k - (uint8_t *)bus->kdbus_buffer;
496         ioctl(bus->input_fd, KDBUS_CMD_FREE, &off);
497
498         KDBUS_ITEM_FOREACH(d, k, items) {
499
500                 if (d->type == KDBUS_ITEM_FDS)
501                         close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int));
502                 else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD)
503                         close_nointr_nofail(d->memfd.fd);
504         }
505 }
506
507 static int push_name_owner_changed(sd_bus *bus, const char *name, const char *old_owner, const char *new_owner) {
508         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
509         int r;
510
511         assert(bus);
512
513         r = sd_bus_message_new_signal(
514                         bus,
515                         "/org/freedesktop/DBus",
516                         "org.freedesktop.DBus",
517                         "NameOwnerChanged",
518                         &m);
519         if (r < 0)
520                 return r;
521
522         r = sd_bus_message_append(m, "sss", name, old_owner, new_owner);
523         if (r < 0)
524                 return r;
525
526         m->sender = "org.freedesktop.DBus";
527
528         r = bus_seal_synthetic_message(bus, m);
529         if (r < 0)
530                 return r;
531
532         bus->rqueue[bus->rqueue_size++] = m;
533         m = NULL;
534
535         return 1;
536 }
537
538 static int translate_name_change(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) {
539         char new_owner[UNIQUE_NAME_MAX], old_owner[UNIQUE_NAME_MAX];
540
541         assert(bus);
542         assert(k);
543         assert(d);
544
545         if (d->type == KDBUS_ITEM_NAME_ADD || (d->name_change.old.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR)))
546                 old_owner[0] = 0;
547         else
548                 sprintf(old_owner, ":1.%llu", (unsigned long long) d->name_change.old.id);
549
550         if (d->type == KDBUS_ITEM_NAME_REMOVE || (d->name_change.new.flags & (KDBUS_NAME_IN_QUEUE|KDBUS_NAME_ACTIVATOR))) {
551
552                 if (isempty(old_owner))
553                         return 0;
554
555                 new_owner[0] = 0;
556         } else
557                 sprintf(new_owner, ":1.%llu", (unsigned long long) d->name_change.new.id);
558
559         return push_name_owner_changed(bus, d->name_change.name, old_owner, new_owner);
560 }
561
562 static int translate_id_change(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) {
563         char owner[UNIQUE_NAME_MAX];
564
565         assert(bus);
566         assert(k);
567         assert(d);
568
569         sprintf(owner, ":1.%llu", d->id_change.id);
570
571         return push_name_owner_changed(
572                         bus, owner,
573                         d->type == KDBUS_ITEM_ID_ADD ? NULL : owner,
574                         d->type == KDBUS_ITEM_ID_ADD ? owner : NULL);
575 }
576
577 static int translate_reply(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) {
578         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
579         int r;
580
581         assert(bus);
582         assert(k);
583         assert(d);
584
585         r = bus_message_new_synthetic_error(
586                         bus,
587                         k->cookie_reply,
588                         d->type == KDBUS_ITEM_REPLY_TIMEOUT ?
589                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out") :
590                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call peer died"),
591                         &m);
592         if (r < 0)
593                 return r;
594
595         m->sender = "org.freedesktop.DBus";
596
597         r = bus_seal_synthetic_message(bus, m);
598         if (r < 0)
599                 return r;
600
601         bus->rqueue[bus->rqueue_size++] = m;
602         m = NULL;
603
604         return 1;
605 }
606
607 static int bus_kernel_translate_message(sd_bus *bus, struct kdbus_msg *k) {
608         struct kdbus_item *d, *found = NULL;
609
610         static int (* const translate[])(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) = {
611                 [KDBUS_ITEM_NAME_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
612                 [KDBUS_ITEM_NAME_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
613                 [KDBUS_ITEM_NAME_CHANGE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
614
615                 [KDBUS_ITEM_ID_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change,
616                 [KDBUS_ITEM_ID_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change,
617
618                 [KDBUS_ITEM_REPLY_TIMEOUT - _KDBUS_ITEM_KERNEL_BASE] = translate_reply,
619                 [KDBUS_ITEM_REPLY_DEAD - _KDBUS_ITEM_KERNEL_BASE] = translate_reply,
620         };
621
622         assert(bus);
623         assert(k);
624         assert(k->payload_type == KDBUS_PAYLOAD_KERNEL);
625
626         KDBUS_ITEM_FOREACH(d, k, items) {
627                 if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) {
628                         if (found)
629                                 return -EBADMSG;
630                         found = d;
631                 } else
632                         log_debug("Got unknown field from kernel %llu", d->type);
633         }
634
635         if (!found) {
636                 log_debug("Didn't find a kernel message to translate.");
637                 return 0;
638         }
639
640         return translate[found->type - _KDBUS_ITEM_KERNEL_BASE](bus, k, found);
641 }
642
643 static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
644         sd_bus_message *m = NULL;
645         struct kdbus_item *d;
646         unsigned n_fds = 0;
647         _cleanup_free_ int *fds = NULL;
648         struct bus_header *h = NULL;
649         size_t total, n_bytes = 0, idx = 0;
650         const char *destination = NULL, *seclabel = NULL;
651         int r;
652
653         assert(bus);
654         assert(k);
655         assert(k->payload_type == KDBUS_PAYLOAD_DBUS);
656
657         KDBUS_ITEM_FOREACH(d, k, items) {
658                 size_t l;
659
660                 l = d->size - offsetof(struct kdbus_item, data);
661
662                 switch (d->type) {
663
664                 case KDBUS_ITEM_PAYLOAD_OFF:
665                         if (!h) {
666                                 h = (struct bus_header *)((uint8_t *)k + d->vec.offset);
667
668                                 if (!bus_header_is_complete(h, d->vec.size))
669                                         return -EBADMSG;
670                         }
671
672                         n_bytes += d->vec.size;
673                         break;
674
675                 case KDBUS_ITEM_PAYLOAD_MEMFD:
676                         if (!h)
677                                 return -EBADMSG;
678
679                         n_bytes += d->memfd.size;
680                         break;
681
682                 case KDBUS_ITEM_FDS: {
683                         int *f;
684                         unsigned j;
685
686                         j = l / sizeof(int);
687                         f = realloc(fds, sizeof(int) * (n_fds + j));
688                         if (!f)
689                                 return -ENOMEM;
690
691                         fds = f;
692                         memcpy(fds + n_fds, d->fds, sizeof(int) * j);
693                         n_fds += j;
694                         break;
695                 }
696
697                 case KDBUS_ITEM_SECLABEL:
698                         seclabel = d->str;
699                         break;
700                 }
701         }
702
703         if (!h)
704                 return -EBADMSG;
705
706         r = bus_header_message_size(h, &total);
707         if (r < 0)
708                 return r;
709
710         if (n_bytes != total)
711                 return -EBADMSG;
712
713         /* on kdbus we only speak native endian gvariant, never dbus1
714          * marshalling or reverse endian */
715         if (h->version != 2 ||
716             h->endian != BUS_NATIVE_ENDIAN)
717                 return -EPROTOTYPE;
718
719         r = bus_message_from_header(bus, h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m);
720         if (r < 0)
721                 return r;
722
723         /* The well-known names list is different from the other
724         credentials. If we asked for it, but nothing is there, this
725         means that the list of well-known names is simply empty, not
726         that we lack any data */
727
728         m->creds.mask |= (SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_WELL_KNOWN_NAMES) & bus->creds_mask;
729
730         KDBUS_ITEM_FOREACH(d, k, items) {
731                 size_t l;
732
733                 l = d->size - offsetof(struct kdbus_item, data);
734
735                 switch (d->type) {
736
737                 case KDBUS_ITEM_PAYLOAD_OFF: {
738                         size_t begin_body;
739
740                         begin_body = BUS_MESSAGE_BODY_BEGIN(m);
741
742                         if (idx + d->vec.size > begin_body) {
743                                 struct bus_body_part *part;
744
745                                 /* Contains body material */
746
747                                 part = message_append_part(m);
748                                 if (!part) {
749                                         r = -ENOMEM;
750                                         goto fail;
751                                 }
752
753                                 /* A -1 offset is NUL padding. */
754                                 part->is_zero = d->vec.offset == ~0ULL;
755
756                                 if (idx >= begin_body) {
757                                         if (!part->is_zero)
758                                                 part->data = (uint8_t *)k + d->vec.offset;
759                                         part->size = d->vec.size;
760                                 } else {
761                                         if (!part->is_zero)
762                                                 part->data = (uint8_t *)k + d->vec.offset + (begin_body - idx);
763                                         part->size = d->vec.size - (begin_body - idx);
764                                 }
765
766                                 part->sealed = true;
767                         }
768
769                         idx += d->vec.size;
770                         break;
771                 }
772
773                 case KDBUS_ITEM_PAYLOAD_MEMFD: {
774                         struct bus_body_part *part;
775
776                         if (idx < BUS_MESSAGE_BODY_BEGIN(m)) {
777                                 r = -EBADMSG;
778                                 goto fail;
779                         }
780
781                         part = message_append_part(m);
782                         if (!part) {
783                                 r = -ENOMEM;
784                                 goto fail;
785                         }
786
787                         part->memfd = d->memfd.fd;
788                         part->size = d->memfd.size;
789                         part->sealed = true;
790
791                         idx += d->memfd.size;
792                         break;
793                 }
794
795                 case KDBUS_ITEM_CREDS:
796                         /* UID/GID/PID are always valid */
797                         m->creds.uid = d->creds.uid;
798                         m->creds.gid = d->creds.gid;
799                         m->creds.pid = d->creds.pid;
800                         m->creds.mask |= (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID) & bus->creds_mask;
801
802                         /* The PID starttime/TID might be missing
803                          * however, when the data is faked by some
804                          * data bus proxy and it lacks that
805                          * information about the real client since
806                          * SO_PEERCRED is used for that */
807
808                         if (d->creds.starttime > 0) {
809                                 m->creds.pid_starttime = d->creds.starttime / NSEC_PER_USEC;
810                                 m->creds.mask |= SD_BUS_CREDS_PID_STARTTIME & bus->creds_mask;
811                         }
812
813                         if (d->creds.tid > 0) {
814                                 m->creds.tid = d->creds.tid;
815                                 m->creds.mask |= SD_BUS_CREDS_TID & bus->creds_mask;
816                         }
817                         break;
818
819                 case KDBUS_ITEM_TIMESTAMP:
820                         m->realtime = d->timestamp.realtime_ns / NSEC_PER_USEC;
821                         m->monotonic = d->timestamp.monotonic_ns / NSEC_PER_USEC;
822                         break;
823
824                 case KDBUS_ITEM_PID_COMM:
825                         m->creds.comm = d->str;
826                         m->creds.mask |= SD_BUS_CREDS_COMM & bus->creds_mask;
827                         break;
828
829                 case KDBUS_ITEM_TID_COMM:
830                         m->creds.tid_comm = d->str;
831                         m->creds.mask |= SD_BUS_CREDS_TID_COMM & bus->creds_mask;
832                         break;
833
834                 case KDBUS_ITEM_EXE:
835                         m->creds.exe = d->str;
836                         m->creds.mask |= SD_BUS_CREDS_EXE & bus->creds_mask;
837                         break;
838
839                 case KDBUS_ITEM_CMDLINE:
840                         m->creds.cmdline = d->str;
841                         m->creds.cmdline_size = l;
842                         m->creds.mask |= SD_BUS_CREDS_CMDLINE & bus->creds_mask;
843                         break;
844
845                 case KDBUS_ITEM_CGROUP:
846                         m->creds.cgroup = d->str;
847                         m->creds.mask |= (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID) & bus->creds_mask;
848                         break;
849
850                 case KDBUS_ITEM_AUDIT:
851                         m->creds.audit_session_id = d->audit.sessionid;
852                         m->creds.audit_login_uid = d->audit.loginuid;
853                         m->creds.mask |= (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID) & bus->creds_mask;
854                         break;
855
856                 case KDBUS_ITEM_CAPS:
857                         m->creds.capability = d->data;
858                         m->creds.capability_size = l;
859                         m->creds.mask |= (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS) & bus->creds_mask;
860                         break;
861
862                 case KDBUS_ITEM_DST_NAME:
863                         destination = d->str;
864                         break;
865
866                 case KDBUS_ITEM_NAME:
867                         r = strv_extend(&m->creds.well_known_names, d->name.name);
868                         if (r < 0)
869                                 goto fail;
870                         break;
871
872                 case KDBUS_ITEM_FDS:
873                 case KDBUS_ITEM_SECLABEL:
874                         break;
875
876                 default:
877                         log_debug("Got unknown field from kernel %llu", d->type);
878                 }
879         }
880
881         r = bus_message_parse_fields(m);
882         if (r < 0)
883                 goto fail;
884
885         /* Override information from the user header with data from the kernel */
886         if (k->src_id == KDBUS_SRC_ID_KERNEL)
887                 m->sender = m->creds.unique_name = (char*) "org.freedesktop.DBus";
888         else {
889                 snprintf(m->sender_buffer, sizeof(m->sender_buffer), ":1.%llu", (unsigned long long) k->src_id);
890                 m->sender = m->creds.unique_name = m->sender_buffer;
891         }
892
893         if (destination)
894                 m->destination = destination;
895         else if (k->dst_id == KDBUS_DST_ID_BROADCAST)
896                 m->destination = NULL;
897         else if (k->dst_id == KDBUS_DST_ID_NAME)
898                 m->destination = bus->unique_name; /* fill in unique name if the well-known name is missing */
899         else {
900                 snprintf(m->destination_buffer, sizeof(m->destination_buffer), ":1.%llu", (unsigned long long) k->dst_id);
901                 m->destination = m->destination_buffer;
902         }
903
904         /* We take possession of the kmsg struct now */
905         m->kdbus = k;
906         m->release_kdbus = true;
907         m->free_fds = true;
908         fds = NULL;
909
910         bus->rqueue[bus->rqueue_size++] = m;
911
912         return 1;
913
914 fail:
915         if (m) {
916                 struct bus_body_part *part;
917                 unsigned i;
918
919                 /* Make sure the memfds are not freed twice */
920                 MESSAGE_FOREACH_PART(part, i, m)
921                         if (part->memfd >= 0)
922                                 part->memfd = -1;
923
924                 sd_bus_message_unref(m);
925         }
926
927         return r;
928 }
929
930 int bus_kernel_read_message(sd_bus *bus) {
931         struct kdbus_msg *k;
932         uint64_t off;
933         int r;
934
935         assert(bus);
936
937         r = bus_rqueue_make_room(bus);
938         if (r < 0)
939                 return r;
940
941         r = ioctl(bus->input_fd, KDBUS_CMD_MSG_RECV, &off);
942         if (r < 0) {
943                 if (errno == EAGAIN)
944                         return 0;
945
946                 return -errno;
947         }
948         k = (struct kdbus_msg *)((uint8_t *)bus->kdbus_buffer + off);
949
950         if (k->payload_type == KDBUS_PAYLOAD_DBUS) {
951                 r = bus_kernel_make_message(bus, k);
952
953                 /* Anybody can send us invalid messages, let's just drop them. */
954                 if (r == -EBADMSG || r == -EPROTOTYPE) {
955                         log_debug("Ignoring invalid message: %s", strerror(-r));
956                         r = 0;
957                 }
958
959         } else if (k->payload_type == KDBUS_PAYLOAD_KERNEL)
960                 r = bus_kernel_translate_message(bus, k);
961         else
962                 r = 0;
963
964         if (r <= 0)
965                 close_kdbus_msg(bus, k);
966
967         return r < 0 ? r : 1;
968 }
969
970 int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *mapped, size_t *allocated) {
971         struct memfd_cache *c;
972         int fd;
973
974         assert(address);
975         assert(mapped);
976         assert(allocated);
977
978         if (!bus || !bus->is_kernel)
979                 return -ENOTSUP;
980
981         assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) >= 0);
982
983         if (bus->n_memfd_cache <= 0) {
984                 int r;
985
986                 assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
987
988                 r = ioctl(bus->input_fd, KDBUS_CMD_MEMFD_NEW, &fd);
989                 if (r < 0)
990                         return -errno;
991
992                 *address = NULL;
993                 *mapped = 0;
994                 *allocated = 0;
995                 return fd;
996         }
997
998         c = &bus->memfd_cache[--bus->n_memfd_cache];
999
1000         assert(c->fd >= 0);
1001         assert(c->mapped == 0 || c->address);
1002
1003         *address = c->address;
1004         *mapped = c->mapped;
1005         *allocated = c->allocated;
1006         fd = c->fd;
1007
1008         assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1009
1010         return fd;
1011 }
1012
1013 static void close_and_munmap(int fd, void *address, size_t size) {
1014         if (size > 0)
1015                 assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
1016
1017         close_nointr_nofail(fd);
1018 }
1019
1020 void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t mapped, size_t allocated) {
1021         struct memfd_cache *c;
1022         uint64_t max_mapped = PAGE_ALIGN(MEMFD_CACHE_ITEM_SIZE_MAX);
1023
1024         assert(fd >= 0);
1025         assert(mapped == 0 || address);
1026
1027         if (!bus || !bus->is_kernel) {
1028                 close_and_munmap(fd, address, mapped);
1029                 return;
1030         }
1031
1032         assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) >= 0);
1033
1034         if (bus->n_memfd_cache >= ELEMENTSOF(bus->memfd_cache)) {
1035                 assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1036
1037                 close_and_munmap(fd, address, mapped);
1038                 return;
1039         }
1040
1041         c = &bus->memfd_cache[bus->n_memfd_cache++];
1042         c->fd = fd;
1043         c->address = address;
1044
1045         /* If overly long, let's return a bit to the OS */
1046         if (mapped > max_mapped) {
1047                 assert_se(ioctl(fd, KDBUS_CMD_MEMFD_SIZE_SET, &max_mapped) >= 0);
1048                 assert_se(munmap((uint8_t*) address + max_mapped, PAGE_ALIGN(mapped - max_mapped)) >= 0);
1049                 c->mapped = c->allocated = max_mapped;
1050         } else {
1051                 c->mapped = mapped;
1052                 c->allocated = allocated;
1053         }
1054
1055         assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1056 }
1057
1058 void bus_kernel_flush_memfd(sd_bus *b) {
1059         unsigned i;
1060
1061         assert(b);
1062
1063         for (i = 0; i < b->n_memfd_cache; i++)
1064                 close_and_munmap(b->memfd_cache[i].fd, b->memfd_cache[i].address, b->memfd_cache[i].mapped);
1065 }
1066
1067 int kdbus_translate_request_name_flags(uint64_t flags, uint64_t *kdbus_flags) {
1068         uint64_t f = 0;
1069
1070         assert(kdbus_flags);
1071
1072         if (flags & SD_BUS_NAME_ALLOW_REPLACEMENT)
1073                 f |= KDBUS_NAME_ALLOW_REPLACEMENT;
1074
1075         if (flags & SD_BUS_NAME_REPLACE_EXISTING)
1076                 f |= KDBUS_NAME_REPLACE_EXISTING;
1077
1078         if (flags & SD_BUS_NAME_QUEUE)
1079                 f |= KDBUS_NAME_QUEUE;
1080
1081         *kdbus_flags = f;
1082         return 0;
1083 }
1084
1085 int kdbus_translate_attach_flags(uint64_t mask, uint64_t *kdbus_mask) {
1086         uint64_t m = 0;
1087
1088         assert(kdbus_mask);
1089
1090         if (mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID))
1091                 m |= KDBUS_ATTACH_CREDS;
1092
1093         if (mask & (SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM))
1094                 m |= KDBUS_ATTACH_COMM;
1095
1096         if (mask & SD_BUS_CREDS_EXE)
1097                 m |= KDBUS_ATTACH_EXE;
1098
1099         if (mask & SD_BUS_CREDS_CMDLINE)
1100                 m |= KDBUS_ATTACH_CMDLINE;
1101
1102         if (mask & (SD_BUS_CREDS_CGROUP|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_USER_UNIT|SD_BUS_CREDS_SLICE|SD_BUS_CREDS_SESSION|SD_BUS_CREDS_OWNER_UID))
1103                 m |= KDBUS_ATTACH_CGROUP;
1104
1105         if (mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS))
1106                 m |= KDBUS_ATTACH_CAPS;
1107
1108         if (mask & SD_BUS_CREDS_SELINUX_CONTEXT)
1109                 m |= KDBUS_ATTACH_SECLABEL;
1110
1111         if (mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID))
1112                 m |= KDBUS_ATTACH_AUDIT;
1113
1114         if (mask & SD_BUS_CREDS_WELL_KNOWN_NAMES)
1115                 m |= KDBUS_ATTACH_NAMES;
1116
1117         *kdbus_mask = m;
1118         return 0;
1119 }
1120
1121 int bus_kernel_create_bus(const char *name, char **s) {
1122         struct kdbus_cmd_make *make;
1123         struct kdbus_item *n;
1124         int fd;
1125
1126         assert(name);
1127         assert(s);
1128
1129         fd = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
1130         if (fd < 0)
1131                 return -errno;
1132
1133         make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_make, items) +
1134                               offsetof(struct kdbus_item, data64) + sizeof(uint64_t) +
1135                               offsetof(struct kdbus_item, str) +
1136                               DECIMAL_STR_MAX(uid_t) + 1 + strlen(name) + 1));
1137
1138         make->size = offsetof(struct kdbus_cmd_make, items);
1139
1140         n = make->items;
1141         n->size = offsetof(struct kdbus_item, data64) + sizeof(uint64_t);
1142         n->type = KDBUS_ITEM_BLOOM_SIZE;
1143         n->data64[0] = BLOOM_SIZE;
1144         assert_cc(BLOOM_SIZE % 8 == 0);
1145         make->size += ALIGN8(n->size);
1146
1147         n = KDBUS_ITEM_NEXT(n);
1148         sprintf(n->str, "%lu-%s", (unsigned long) getuid(), name);
1149         n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
1150         n->type = KDBUS_ITEM_MAKE_NAME;
1151         make->size += ALIGN8(n->size);
1152
1153         make->flags = KDBUS_MAKE_POLICY_OPEN;
1154
1155         if (ioctl(fd, KDBUS_CMD_BUS_MAKE, make) < 0) {
1156                 close_nointr_nofail(fd);
1157                 return -errno;
1158         }
1159
1160         /* The higher 32bit of the flags field are considered
1161          * 'incompatible flags'. Refuse them all for now. */
1162         if (make->flags > 0xFFFFFFFFULL) {
1163                 close_nointr_nofail(fd);
1164                 return -ENOTSUP;
1165         }
1166
1167         if (s) {
1168                 char *p;
1169
1170                 p = strjoin("/dev/kdbus/", n->str, "/bus", NULL);
1171                 if (!p) {
1172                         close_nointr_nofail(fd);
1173                         return -ENOMEM;
1174                 }
1175
1176                 *s = p;
1177         }
1178
1179         return fd;
1180 }
1181
1182 int bus_kernel_create_starter(const char *bus, const char *name) {
1183         struct kdbus_cmd_hello *hello;
1184         struct kdbus_item *n;
1185         char *p;
1186         int fd;
1187
1188         assert(bus);
1189         assert(name);
1190
1191         p = alloca(sizeof("/dev/kdbus/") - 1 + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + sizeof("/bus"));
1192         sprintf(p, "/dev/kdbus/%lu-%s/bus", (unsigned long) getuid(), bus);
1193
1194         fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
1195         if (fd < 0)
1196                 return -errno;
1197
1198         hello = alloca0(ALIGN8(offsetof(struct kdbus_cmd_hello, items) +
1199                                offsetof(struct kdbus_item, str) +
1200                                strlen(name) + 1));
1201
1202         n = hello->items;
1203         strcpy(n->str, name);
1204         n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
1205         n->type = KDBUS_ITEM_NAME;
1206
1207         hello->size = ALIGN8(offsetof(struct kdbus_cmd_hello, items) + n->size);
1208         hello->conn_flags = KDBUS_HELLO_ACTIVATOR;
1209         hello->pool_size = KDBUS_POOL_SIZE;
1210
1211         if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
1212                 close_nointr_nofail(fd);
1213                 return -errno;
1214         }
1215
1216         /* The higher 32bit of both flags fields are considered
1217          * 'incompatible flags'. Refuse them all for now. */
1218         if (hello->bus_flags > 0xFFFFFFFFULL ||
1219             hello->conn_flags > 0xFFFFFFFFULL) {
1220                 close_nointr_nofail(fd);
1221                 return -ENOTSUP;
1222         }
1223
1224         if (hello->bloom_size != BLOOM_SIZE) {
1225                 close_nointr_nofail(fd);
1226                 return -ENOTSUP;
1227         }
1228
1229         return fd;
1230 }
1231
1232 int bus_kernel_create_namespace(const char *name, char **s) {
1233         struct kdbus_cmd_make *make;
1234         struct kdbus_item *n;
1235         int fd;
1236
1237         assert(name);
1238         assert(s);
1239
1240         fd = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
1241         if (fd < 0)
1242                 return -errno;
1243
1244         make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_make, items) +
1245                               offsetof(struct kdbus_item, str) +
1246                               strlen(name) + 1));
1247
1248         n = make->items;
1249         strcpy(n->str, name);
1250         n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
1251         n->type = KDBUS_ITEM_MAKE_NAME;
1252
1253         make->size = ALIGN8(offsetof(struct kdbus_cmd_make, items) + n->size);
1254         make->flags = KDBUS_MAKE_POLICY_OPEN | KDBUS_MAKE_ACCESS_WORLD;
1255
1256         if (ioctl(fd, KDBUS_CMD_NS_MAKE, make) < 0) {
1257                 close_nointr_nofail(fd);
1258                 return -errno;
1259         }
1260
1261         /* The higher 32bit of the flags field are considered
1262          * 'incompatible flags'. Refuse them all for now. */
1263         if (make->flags > 0xFFFFFFFFULL) {
1264                 close_nointr_nofail(fd);
1265                 return -ENOTSUP;
1266         }
1267
1268         if (s) {
1269                 char *p;
1270
1271                 p = strappend("/dev/kdbus/ns/", name);
1272                 if (!p) {
1273                         close_nointr_nofail(fd);
1274                         return -ENOMEM;
1275                 }
1276
1277                 *s = p;
1278         }
1279
1280         return fd;
1281 }
1282
1283 int bus_kernel_create_monitor(const char *bus) {
1284         struct kdbus_cmd_hello *hello;
1285         char *p;
1286         int fd;
1287
1288         assert(bus);
1289
1290         p = alloca(sizeof("/dev/kdbus/") - 1 + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + sizeof("/bus"));
1291         sprintf(p, "/dev/kdbus/%lu-%s/bus", (unsigned long) getuid(), bus);
1292
1293         fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
1294         if (fd < 0)
1295                 return -errno;
1296
1297         hello = alloca0(sizeof(struct kdbus_cmd_hello));
1298         hello->size = sizeof(struct kdbus_cmd_hello);
1299         hello->conn_flags = KDBUS_HELLO_ACTIVATOR;
1300         hello->pool_size = KDBUS_POOL_SIZE;
1301
1302         if (ioctl(fd, KDBUS_CMD_HELLO, hello) < 0) {
1303                 close_nointr_nofail(fd);
1304                 return -errno;
1305         }
1306
1307         /* The higher 32bit of both flags fields are considered
1308          * 'incompatible flags'. Refuse them all for now. */
1309         if (hello->bus_flags > 0xFFFFFFFFULL ||
1310             hello->conn_flags > 0xFFFFFFFFULL) {
1311                 close_nointr_nofail(fd);
1312                 return -ENOTSUP;
1313         }
1314
1315         return fd;
1316 }
1317
1318 int bus_kernel_try_close(sd_bus *bus) {
1319         assert(bus);
1320         assert(bus->is_kernel);
1321
1322         if (ioctl(bus->input_fd, KDBUS_CMD_BYEBYE) < 0)
1323                 return -errno;
1324
1325         return 0;
1326 }