chiark / gitweb /
8a0f0859766858ebd54bbfef72c35cdcc88b52a4
[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
32 #include "bus-internal.h"
33 #include "bus-message.h"
34 #include "bus-kernel.h"
35 #include "bus-bloom.h"
36 #include "bus-util.h"
37
38 #define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t))
39
40 int bus_kernel_parse_unique_name(const char *s, uint64_t *id) {
41         int r;
42
43         assert(s);
44         assert(id);
45
46         if (!startswith(s, ":1."))
47                 return 0;
48
49         r = safe_atou64(s + 3, id);
50         if (r < 0)
51                 return r;
52
53         return 1;
54 }
55
56 static void append_payload_vec(struct kdbus_item **d, const void *p, size_t sz) {
57         assert(d);
58         assert(sz > 0);
59
60         *d = ALIGN8_PTR(*d);
61
62         /* Note that p can be NULL, which encodes a region full of
63          * zeroes, which is useful to optimize certain padding
64          * conditions */
65
66         (*d)->size = offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec);
67         (*d)->type = KDBUS_ITEM_PAYLOAD_VEC;
68         (*d)->vec.address = PTR_TO_UINT64(p);
69         (*d)->vec.size = sz;
70
71         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
72 }
73
74 static void append_payload_memfd(struct kdbus_item **d, int memfd, size_t sz) {
75         assert(d);
76         assert(memfd >= 0);
77         assert(sz > 0);
78
79         *d = ALIGN8_PTR(*d);
80         (*d)->size = offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd);
81         (*d)->type = KDBUS_ITEM_PAYLOAD_MEMFD;
82         (*d)->memfd.fd = memfd;
83         (*d)->memfd.size = sz;
84
85         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
86 }
87
88 static void append_destination(struct kdbus_item **d, const char *s, size_t length) {
89         assert(d);
90         assert(s);
91
92         *d = ALIGN8_PTR(*d);
93
94         (*d)->size = offsetof(struct kdbus_item, str) + length + 1;
95         (*d)->type = KDBUS_ITEM_DST_NAME;
96         memcpy((*d)->str, s, length + 1);
97
98         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
99 }
100
101 static void* append_bloom(struct kdbus_item **d, size_t length) {
102         void *r;
103
104         assert(d);
105
106         *d = ALIGN8_PTR(*d);
107
108         (*d)->size = offsetof(struct kdbus_item, data) + length;
109         (*d)->type = KDBUS_ITEM_BLOOM;
110         r = (*d)->data;
111
112         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
113
114         return r;
115 }
116
117 static void append_fds(struct kdbus_item **d, const int fds[], unsigned n_fds) {
118         assert(d);
119         assert(fds);
120         assert(n_fds > 0);
121
122         *d = ALIGN8_PTR(*d);
123         (*d)->size = offsetof(struct kdbus_item, fds) + sizeof(int) * n_fds;
124         (*d)->type = KDBUS_ITEM_FDS;
125         memcpy((*d)->fds, fds, sizeof(int) * n_fds);
126
127         *d = (struct kdbus_item *) ((uint8_t*) *d + (*d)->size);
128 }
129
130 static int bus_message_setup_bloom(sd_bus_message *m, void *bloom) {
131         unsigned i;
132         int r;
133
134         assert(m);
135         assert(bloom);
136
137         memset(bloom, 0, BLOOM_SIZE);
138
139         bloom_add_pair(bloom, "message-type", bus_message_type_to_string(m->header->type));
140
141         if (m->interface)
142                 bloom_add_pair(bloom, "interface", m->interface);
143         if (m->member)
144                 bloom_add_pair(bloom, "member", m->member);
145         if (m->path) {
146                 bloom_add_pair(bloom, "path", m->path);
147                 bloom_add_pair(bloom, "path-slash-prefix", m->path);
148                 bloom_add_prefixes(bloom, "path-slash-prefix", m->path, '/');
149         }
150
151         r = sd_bus_message_rewind(m, true);
152         if (r < 0)
153                 return r;
154
155         for (i = 0; i < 64; i++) {
156                 char type;
157                 const char *t;
158                 char buf[sizeof("arg")-1 + 2 + sizeof("-slash-prefix")];
159                 char *e;
160
161                 r = sd_bus_message_peek_type(m, &type, NULL);
162                 if (r < 0)
163                         return r;
164
165                 if (type != SD_BUS_TYPE_STRING &&
166                     type != SD_BUS_TYPE_OBJECT_PATH &&
167                     type != SD_BUS_TYPE_SIGNATURE)
168                         break;
169
170                 r = sd_bus_message_read_basic(m, type, &t);
171                 if (r < 0)
172                         return r;
173
174                 e = stpcpy(buf, "arg");
175                 if (i < 10)
176                         *(e++) = '0' + i;
177                 else {
178                         *(e++) = '0' + (i / 10);
179                         *(e++) = '0' + (i % 10);
180                 }
181
182                 *e = 0;
183                 bloom_add_pair(bloom, buf, t);
184
185                 strcpy(e, "-dot-prefix");
186                 bloom_add_prefixes(bloom, buf, t, '.');
187                 strcpy(e, "-slash-prefix");
188                 bloom_add_prefixes(bloom, buf, t, '/');
189         }
190
191         return 0;
192 }
193
194 static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
195         struct bus_body_part *part;
196         struct kdbus_item *d;
197         bool well_known;
198         uint64_t unique;
199         size_t sz, dl;
200         unsigned i;
201         int r;
202
203         assert(b);
204         assert(m);
205         assert(m->sealed);
206
207         if (m->kdbus)
208                 return 0;
209
210         if (m->destination) {
211                 r = bus_kernel_parse_unique_name(m->destination, &unique);
212                 if (r < 0)
213                         return r;
214
215                 well_known = r == 0;
216         } else
217                 well_known = false;
218
219         sz = offsetof(struct kdbus_msg, items);
220
221         assert_cc(ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec)) ==
222                   ALIGN8(offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd)));
223
224         /* Add in fixed header, fields header and payload */
225         sz += (1 + m->n_body_parts) *
226                 ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec));
227
228         /* Add space for bloom filter */
229         sz += ALIGN8(offsetof(struct kdbus_item, data) + BLOOM_SIZE);
230
231         /* Add in well-known destination header */
232         if (well_known) {
233                 dl = strlen(m->destination);
234                 sz += ALIGN8(offsetof(struct kdbus_item, str) + dl + 1);
235         }
236
237         /* Add space for unix fds */
238         if (m->n_fds > 0)
239                 sz += ALIGN8(offsetof(struct kdbus_item, fds) + sizeof(int)*m->n_fds);
240
241         m->kdbus = memalign(8, sz);
242         if (!m->kdbus) {
243                 r = -ENOMEM;
244                 goto fail;
245         }
246
247         m->free_kdbus = true;
248         memset(m->kdbus, 0, sz);
249
250         m->kdbus->flags =
251                 ((m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) ? 0 : KDBUS_MSG_FLAGS_EXPECT_REPLY) |
252                 ((m->header->flags & SD_BUS_MESSAGE_NO_AUTO_START) ? KDBUS_MSG_FLAGS_NO_AUTO_START : 0);
253         m->kdbus->dst_id =
254                 well_known ? 0 :
255                 m->destination ? unique : KDBUS_DST_ID_BROADCAST;
256         m->kdbus->payload_type = KDBUS_PAYLOAD_DBUS1;
257         m->kdbus->cookie = m->header->serial;
258
259         m->kdbus->timeout_ns = m->timeout * NSEC_PER_USEC;
260
261         d = m->kdbus->items;
262
263         if (well_known)
264                 append_destination(&d, m->destination, dl);
265
266         append_payload_vec(&d, m->header, BUS_MESSAGE_BODY_BEGIN(m));
267
268         MESSAGE_FOREACH_PART(part, i, m) {
269                 if (part->is_zero) {
270                         /* If this is padding then simply send a
271                          * vector with a NULL data pointer which the
272                          * kernel will just pass through. This is the
273                          * most efficient way to encode zeroes */
274
275                         append_payload_vec(&d, NULL, part->size);
276                         continue;
277                 }
278
279                 if (part->memfd >= 0 && part->sealed && m->destination) {
280                         /* Try to send a memfd, if the part is
281                          * sealed and this is not a broadcast. Since we can only  */
282
283                         append_payload_memfd(&d, part->memfd, part->size);
284                         continue;
285                 }
286
287                 /* Otherwise let's send a vector to the actual data,
288                  * for that we need to map it first. */
289                 r = bus_body_part_map(part);
290                 if (r < 0)
291                         goto fail;
292
293                 append_payload_vec(&d, part->data, part->size);
294         }
295
296         if (m->kdbus->dst_id == KDBUS_DST_ID_BROADCAST) {
297                 void *p;
298
299                 p = append_bloom(&d, BLOOM_SIZE);
300                 r = bus_message_setup_bloom(m, p);
301                 if (r < 0)
302                         goto fail;
303         }
304
305         if (m->n_fds > 0)
306                 append_fds(&d, m->fds, m->n_fds);
307
308         m->kdbus->size = (uint8_t*) d - (uint8_t*) m->kdbus;
309         assert(m->kdbus->size <= sz);
310
311         return 0;
312
313 fail:
314         m->poisoned = true;
315         return r;
316 }
317
318 int bus_kernel_take_fd(sd_bus *b) {
319         struct kdbus_cmd_hello hello;
320         int r;
321
322         assert(b);
323
324         if (b->is_server)
325                 return -EINVAL;
326
327         b->use_memfd = 1;
328
329         zero(hello);
330         hello.size = sizeof(hello);
331         hello.conn_flags = b->hello_flags;
332         hello.attach_flags = b->attach_flags;
333         hello.pool_size = KDBUS_POOL_SIZE;
334
335         r = ioctl(b->input_fd, KDBUS_CMD_HELLO, &hello);
336         if (r < 0)
337                 return -errno;
338
339         if (!b->kdbus_buffer) {
340                 b->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ, MAP_SHARED, b->input_fd, 0);
341                 if (b->kdbus_buffer == MAP_FAILED) {
342                         b->kdbus_buffer = NULL;
343                         return -errno;
344                 }
345         }
346
347         /* The higher 32bit of both flags fields are considered
348          * 'incompatible flags'. Refuse them all for now. */
349         if (hello.bus_flags > 0xFFFFFFFFULL ||
350             hello.conn_flags > 0xFFFFFFFFULL)
351                 return -ENOTSUP;
352
353         if (hello.bloom_size != BLOOM_SIZE)
354                 return -ENOTSUP;
355
356         if (asprintf(&b->unique_name, ":1.%llu", (unsigned long long) hello.id) < 0)
357                 return -ENOMEM;
358
359         b->unique_id = hello.id;
360
361         b->is_kernel = true;
362         b->bus_client = true;
363         b->can_fds = !!(hello.conn_flags & KDBUS_HELLO_ACCEPT_FD);
364
365         /* the kernel told us the UUID of the underlying bus */
366         memcpy(b->server_id.bytes, hello.id128, sizeof(b->server_id.bytes));
367
368         r = bus_start_running(b);
369         if (r < 0)
370                 return r;
371
372         return 1;
373 }
374
375 int bus_kernel_connect(sd_bus *b) {
376         assert(b);
377         assert(b->input_fd < 0);
378         assert(b->output_fd < 0);
379         assert(b->kernel);
380
381         if (b->is_server)
382                 return -EINVAL;
383
384         b->input_fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
385         if (b->input_fd < 0)
386                 return -errno;
387
388         b->output_fd = b->input_fd;
389
390         return bus_kernel_take_fd(b);
391 }
392
393 int bus_kernel_write_message(sd_bus *bus, sd_bus_message *m) {
394         int r;
395
396         assert(bus);
397         assert(m);
398         assert(bus->state == BUS_RUNNING);
399
400         r = bus_message_setup_kmsg(bus, m);
401         if (r < 0)
402                 return r;
403
404         r = ioctl(bus->output_fd, KDBUS_CMD_MSG_SEND, m->kdbus);
405         if (r < 0)
406                 return errno == EAGAIN ? 0 : -errno;
407
408         return 1;
409 }
410
411 static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) {
412         uint64_t off;
413         struct kdbus_item *d;
414
415         assert(bus);
416         assert(k);
417
418         off = (uint8_t *)k - (uint8_t *)bus->kdbus_buffer;
419         ioctl(bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off);
420
421         KDBUS_PART_FOREACH(d, k, items) {
422
423                 if (d->type == KDBUS_ITEM_FDS)
424                         close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int));
425                 else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD)
426                         close_nointr_nofail(d->memfd.fd);
427         }
428 }
429
430 static int push_name_owner_changed(sd_bus *bus, const char *name, const char *old_owner, const char *new_owner) {
431         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
432         int r;
433
434         assert(bus);
435
436         r = sd_bus_message_new_signal(
437                         bus,
438                         "/org/freedesktop/DBus",
439                         "org.freedesktop.DBus",
440                         "NameOwnerChanged",
441                         &m);
442         if (r < 0)
443                 return r;
444
445         r = sd_bus_message_append(m, "sss", name, old_owner, new_owner);
446         if (r < 0)
447                 return r;
448
449         m->sender = "org.freedesktop.DBus";
450
451         r = bus_seal_message(bus, m);
452         if (r < 0)
453                 return r;
454
455         r = bus_rqueue_push(bus, m);
456         if (r < 0)
457                 return r;
458
459         m = NULL;
460         return 1;
461 }
462
463 static int push_name_name_acquired(sd_bus *bus, const char *signal, const char *name) {
464         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
465         int r;
466
467         assert(bus);
468
469         r = sd_bus_message_new_signal(
470                         bus,
471                         "/org/freedesktop/DBus",
472                         "org.freedesktop.DBus",
473                         signal,
474                         &m);
475
476         if (r < 0)
477                 return r;
478
479         r = sd_bus_message_append(m, "ss", name);
480         if (r < 0)
481                 return r;
482
483         m->sender = "org.freedesktop.DBus";
484
485         r = bus_seal_message(bus, m);
486         if (r < 0)
487                 return r;
488
489         r = bus_rqueue_push(bus, m);
490         if (r < 0)
491                 return r;
492
493         m = NULL;
494         return 1;
495 }
496
497 static int translate_name_change(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) {
498         char new_owner[UNIQUE_NAME_MAX], old_owner[UNIQUE_NAME_MAX];
499         int r;
500
501         assert(bus);
502         assert(k);
503         assert(d);
504
505         if (d->name_change.flags != 0)
506                 return 0;
507
508         /* First generate NameAcquired/NameList messages */
509         if (d->type == KDBUS_ITEM_NAME_ADD && d->name_change.new_id == bus->unique_id) {
510                 r = push_name_name_acquired(bus, "NameAcquired", d->name_change.name);
511                 if (r < 0)
512                         return r;
513
514         } else if (d->type == KDBUS_ITEM_NAME_REMOVE && d->name_change.old_id == bus->unique_id) {
515                 r = push_name_name_acquired(bus, "NameLost", d->name_change.name);
516                 if (r < 0)
517                         return r;
518         }
519
520         /* Then, generate NameOwnerChanged messages */
521         if (d->type == KDBUS_ITEM_NAME_ADD)
522                 old_owner[0] = 0;
523         else
524                 sprintf(old_owner, ":1.%llu", (unsigned long long) d->name_change.old_id);
525
526         if (d->type == KDBUS_ITEM_NAME_REMOVE)
527                 new_owner[0] = 0;
528         else
529                 sprintf(new_owner, ":1.%llu", (unsigned long long) d->name_change.new_id);
530
531         return push_name_owner_changed(bus, d->name_change.name, old_owner, new_owner);
532 }
533
534 static int translate_id_change(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) {
535         char owner[UNIQUE_NAME_MAX];
536
537         assert(bus);
538         assert(k);
539         assert(d);
540
541         sprintf(owner, ":1.%llu", d->id_change.id);
542
543         return push_name_owner_changed(
544                         bus, owner,
545                         d->type == KDBUS_ITEM_ID_ADD ? NULL : owner,
546                         d->type == KDBUS_ITEM_ID_ADD ? owner : NULL);
547 }
548
549 static int translate_reply(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) {
550         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
551         int r;
552
553         assert(bus);
554         assert(k);
555         assert(d);
556
557         r = bus_message_new_synthetic_error(
558                         bus,
559                         k->cookie_reply,
560                         d->type == KDBUS_ITEM_REPLY_TIMEOUT ?
561                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out") :
562                         &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call peer died"),
563                         &m);
564         if (r < 0)
565                 return r;
566
567         m->sender = "org.freedesktop.DBus";
568
569         r = bus_seal_message(bus, m);
570         if (r < 0)
571                 return r;
572
573         r = bus_rqueue_push(bus, m);
574         if (r < 0)
575                 return r;
576
577         m = NULL;
578         return 1;
579 }
580
581 static int bus_kernel_translate_message(sd_bus *bus, struct kdbus_msg *k) {
582         struct kdbus_item *d, *found = NULL;
583
584         static int (* const translate[])(sd_bus *bus, struct kdbus_msg *k, struct kdbus_item *d) = {
585                 [KDBUS_ITEM_NAME_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
586                 [KDBUS_ITEM_NAME_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
587                 [KDBUS_ITEM_NAME_CHANGE - _KDBUS_ITEM_KERNEL_BASE] = translate_name_change,
588
589                 [KDBUS_ITEM_ID_ADD - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change,
590                 [KDBUS_ITEM_ID_REMOVE - _KDBUS_ITEM_KERNEL_BASE] = translate_id_change,
591
592                 [KDBUS_ITEM_REPLY_TIMEOUT - _KDBUS_ITEM_KERNEL_BASE] = translate_reply,
593                 [KDBUS_ITEM_REPLY_DEAD - _KDBUS_ITEM_KERNEL_BASE] = translate_reply,
594         };
595
596         assert(bus);
597         assert(k);
598         assert(k->payload_type == KDBUS_PAYLOAD_KERNEL);
599
600         KDBUS_PART_FOREACH(d, k, items) {
601                 if (d->type >= _KDBUS_ITEM_KERNEL_BASE && d->type < _KDBUS_ITEM_KERNEL_BASE + ELEMENTSOF(translate)) {
602                         if (found)
603                                 return -EBADMSG;
604                         found = d;
605                 } else
606                         log_debug("Got unknown field from kernel %llu", d->type);
607         }
608
609         if (!found) {
610                 log_debug("Didn't find a kernel message to translate.");
611                 return 0;
612         }
613
614         return translate[found->type - _KDBUS_ITEM_KERNEL_BASE](bus, k, found);
615 }
616
617 int kdbus_translate_attach_flags(uint64_t mask, uint64_t *kdbus_mask) {
618
619         uint64_t m = 0;
620
621         SET_FLAG(m, KDBUS_ATTACH_CREDS,
622                  !!(mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID)));
623
624         SET_FLAG(m, KDBUS_ATTACH_COMM,
625                  !!(mask & (SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)));
626
627         SET_FLAG(m, KDBUS_ATTACH_EXE,
628                  !!(mask & SD_BUS_CREDS_EXE));
629
630         SET_FLAG(m, KDBUS_ATTACH_CMDLINE,
631                  !!(mask & SD_BUS_CREDS_CMDLINE));
632
633         SET_FLAG(m, KDBUS_ATTACH_CGROUP,
634                  !!(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)));
635
636         SET_FLAG(m, KDBUS_ATTACH_CAPS,
637                  !!(mask & (SD_BUS_CREDS_EFFECTIVE_CAPS|SD_BUS_CREDS_PERMITTED_CAPS|SD_BUS_CREDS_INHERITABLE_CAPS|SD_BUS_CREDS_BOUNDING_CAPS)));
638
639         SET_FLAG(m, KDBUS_ATTACH_SECLABEL,
640                  !!(mask & SD_BUS_CREDS_SELINUX_CONTEXT));
641
642         SET_FLAG(m, KDBUS_ATTACH_AUDIT,
643                  !!(mask & (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID)));
644
645         *kdbus_mask = m;
646
647         return 0;
648 }
649
650 static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
651         sd_bus_message *m = NULL;
652         struct kdbus_item *d;
653         unsigned n_fds = 0;
654         _cleanup_free_ int *fds = NULL;
655         struct bus_header *h = NULL;
656         size_t total, n_bytes = 0, idx = 0;
657         const char *destination = NULL, *seclabel = NULL;
658         int r;
659
660         assert(bus);
661         assert(k);
662         assert(k->payload_type == KDBUS_PAYLOAD_DBUS1);
663
664         KDBUS_PART_FOREACH(d, k, items) {
665                 size_t l;
666
667                 l = d->size - offsetof(struct kdbus_item, data);
668
669                 switch (d->type) {
670
671                 case KDBUS_ITEM_PAYLOAD_OFF:
672                         if (!h) {
673                                 h = (struct bus_header *)((uint8_t *)bus->kdbus_buffer + d->vec.offset);
674
675                                 if (!bus_header_is_complete(h, d->vec.size))
676                                         return -EBADMSG;
677                         }
678
679                         n_bytes += d->vec.size;
680                         break;
681
682                 case KDBUS_ITEM_PAYLOAD_MEMFD:
683                         if (!h)
684                                 return -EBADMSG;
685
686                         n_bytes += d->memfd.size;
687                         break;
688
689                 case KDBUS_ITEM_FDS: {
690                         int *f;
691                         unsigned j;
692
693                         j = l / sizeof(int);
694                         f = realloc(fds, sizeof(int) * (n_fds + j));
695                         if (!f)
696                                 return -ENOMEM;
697
698                         fds = f;
699                         memcpy(fds + n_fds, d->fds, sizeof(int) * j);
700                         n_fds += j;
701                         break;
702                 }
703
704                 case KDBUS_ITEM_SECLABEL:
705                         seclabel = d->str;
706                         break;
707                 }
708         }
709
710         if (!h)
711                 return -EBADMSG;
712
713         r = bus_header_message_size(h, &total);
714         if (r < 0)
715                 return r;
716
717         if (n_bytes != total)
718                 return -EBADMSG;
719
720         r = bus_message_from_header(bus, h, sizeof(struct bus_header), fds, n_fds, NULL, seclabel, 0, &m);
721         if (r < 0)
722                 return r;
723
724         KDBUS_PART_FOREACH(d, k, items) {
725                 size_t l;
726
727                 l = d->size - offsetof(struct kdbus_item, data);
728
729                 switch (d->type) {
730
731                 case KDBUS_ITEM_PAYLOAD_OFF: {
732                         size_t begin_body;
733
734                         begin_body = BUS_MESSAGE_BODY_BEGIN(m);
735
736                         if (idx + d->vec.size > begin_body) {
737                                 struct bus_body_part *part;
738
739                                 /* Contains body material */
740
741                                 part = message_append_part(m);
742                                 if (!part) {
743                                         r = -ENOMEM;
744                                         goto fail;
745                                 }
746
747                                 /* A -1 offset is NUL padding. */
748                                 part->is_zero = d->vec.offset == ~0ULL;
749
750                                 if (idx >= begin_body) {
751                                         if (!part->is_zero)
752                                                 part->data = (uint8_t *)bus->kdbus_buffer + d->vec.offset;
753                                         part->size = d->vec.size;
754                                 } else {
755                                         if (!part->is_zero)
756                                                 part->data = (uint8_t *)bus->kdbus_buffer + d->vec.offset + (begin_body - idx);
757                                         part->size = d->vec.size - (begin_body - idx);
758                                 }
759
760                                 part->sealed = true;
761                         }
762
763                         idx += d->vec.size;
764                         break;
765                 }
766
767                 case KDBUS_ITEM_PAYLOAD_MEMFD: {
768                         struct bus_body_part *part;
769
770                         if (idx < BUS_MESSAGE_BODY_BEGIN(m)) {
771                                 r = -EBADMSG;
772                                 goto fail;
773                         }
774
775                         part = message_append_part(m);
776                         if (!part) {
777                                 r = -ENOMEM;
778                                 goto fail;
779                         }
780
781                         part->memfd = d->memfd.fd;
782                         part->size = d->memfd.size;
783                         part->sealed = true;
784
785                         idx += d->memfd.size;
786                         break;
787                 }
788
789                 case KDBUS_ITEM_CREDS:
790                         m->creds.pid_starttime = d->creds.starttime / NSEC_PER_USEC;
791                         m->creds.uid = d->creds.uid;
792                         m->creds.gid = d->creds.gid;
793                         m->creds.pid = d->creds.pid;
794                         m->creds.tid = d->creds.tid;
795                         m->creds.mask |= (SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID) & bus->creds_mask;
796                         break;
797
798                 case KDBUS_ITEM_TIMESTAMP:
799                         m->realtime = d->timestamp.realtime_ns / NSEC_PER_USEC;
800                         m->monotonic = d->timestamp.monotonic_ns / NSEC_PER_USEC;
801                         break;
802
803                 case KDBUS_ITEM_PID_COMM:
804                         m->creds.comm = d->str;
805                         m->creds.mask |= SD_BUS_CREDS_COMM & bus->creds_mask;
806                         break;
807
808                 case KDBUS_ITEM_TID_COMM:
809                         m->creds.tid_comm = d->str;
810                         m->creds.mask |= SD_BUS_CREDS_TID_COMM & bus->creds_mask;
811                         break;
812
813                 case KDBUS_ITEM_EXE:
814                         m->creds.exe = d->str;
815                         m->creds.mask |= SD_BUS_CREDS_EXE & bus->creds_mask;
816                         break;
817
818                 case KDBUS_ITEM_CMDLINE:
819                         m->creds.cmdline = d->str;
820                         m->creds.cmdline_length = l;
821                         m->creds.mask |= SD_BUS_CREDS_CMDLINE & bus->creds_mask;
822                         break;
823
824                 case KDBUS_ITEM_CGROUP:
825                         m->creds.cgroup = d->str;
826                         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;
827                         break;
828
829                 case KDBUS_ITEM_AUDIT:
830                         m->creds.audit_session_id = d->audit.sessionid;
831                         m->creds.audit_login_uid = d->audit.loginuid;
832                         m->creds.mask |= (SD_BUS_CREDS_AUDIT_SESSION_ID|SD_BUS_CREDS_AUDIT_LOGIN_UID) & bus->creds_mask;
833                         break;
834
835                 case KDBUS_ITEM_CAPS:
836                         m->creds.capability = d->data;
837                         m->creds.capability_size = l;
838                         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;
839                         break;
840
841                 case KDBUS_ITEM_DST_NAME:
842                         destination = d->str;
843                         break;
844
845                 case KDBUS_ITEM_FDS:
846                 case KDBUS_ITEM_SECLABEL:
847                 case KDBUS_ITEM_NAMES:
848                         break;
849
850                 default:
851                         log_debug("Got unknown field from kernel %llu", d->type);
852                 }
853         }
854
855         r = bus_message_parse_fields(m);
856         if (r < 0)
857                 goto fail;
858
859         if (k->src_id == KDBUS_SRC_ID_KERNEL)
860                 m->sender = "org.freedesktop.DBus";
861         else {
862                 snprintf(m->sender_buffer, sizeof(m->sender_buffer), ":1.%llu", (unsigned long long) k->src_id);
863                 m->sender = m->sender_buffer;
864         }
865
866         if (!m->destination) {
867                 if (destination)
868                         m->destination = destination;
869                 else if (k->dst_id != KDBUS_DST_ID_WELL_KNOWN_NAME &&
870                          k->dst_id != KDBUS_DST_ID_BROADCAST) {
871                         snprintf(m->destination_buffer, sizeof(m->destination_buffer), ":1.%llu", (unsigned long long) k->dst_id);
872                         m->destination = m->destination_buffer;
873                 }
874         }
875
876         /* We take possession of the kmsg struct now */
877         m->kdbus = k;
878         m->release_kdbus = true;
879         m->free_fds = true;
880
881         fds = NULL;
882
883         r = bus_rqueue_push(bus, m);
884         if (r < 0)
885                 goto fail;
886
887         return 1;
888
889 fail:
890         if (m) {
891                 struct bus_body_part *part;
892                 unsigned i;
893
894                 /* Make sure the memfds are not freed twice */
895                 MESSAGE_FOREACH_PART(part, i, m)
896                         if (part->memfd >= 0)
897                                 part->memfd = -1;
898
899                 sd_bus_message_unref(m);
900         }
901
902         return r;
903 }
904
905 int bus_kernel_read_message(sd_bus *bus) {
906         struct kdbus_msg *k;
907         uint64_t off;
908         int r;
909
910         assert(bus);
911
912         /* Kernel messages might result in 2 new queued messages in
913          * the worst case (NameOwnerChange and LostName for the same
914          * well-known name, for example). Let's make room in
915          * advance. */
916
917         r = bus_rqueue_make_room(bus, 2);
918         if (r < 0)
919                 return r;
920
921         r = ioctl(bus->input_fd, KDBUS_CMD_MSG_RECV, &off);
922         if (r < 0) {
923                 if (errno == EAGAIN)
924                         return 0;
925
926                 return -errno;
927         }
928         k = (struct kdbus_msg *)((uint8_t *)bus->kdbus_buffer + off);
929
930         if (k->payload_type == KDBUS_PAYLOAD_DBUS1)
931                 r = bus_kernel_make_message(bus, k);
932         else if (k->payload_type == KDBUS_PAYLOAD_KERNEL)
933                 r = bus_kernel_translate_message(bus, k);
934         else
935                 r = 0;
936
937         if (r <= 0)
938                 close_kdbus_msg(bus, k);
939
940         return r < 0 ? r : 1;
941 }
942
943 int bus_kernel_create(const char *name, char **s) {
944         struct kdbus_cmd_bus_make *make;
945         struct kdbus_item *n;
946         size_t l;
947         int fd;
948         char *p;
949
950         assert(name);
951         assert(s);
952
953         fd = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
954         if (fd < 0)
955                 return -errno;
956
957         l = strlen(name);
958         make = alloca0(offsetof(struct kdbus_cmd_bus_make, items) +
959                        KDBUS_PART_HEADER_SIZE + sizeof(uint64_t) +
960                        KDBUS_PART_HEADER_SIZE + DECIMAL_STR_MAX(uid_t) + 1 + l + 1);
961
962         n = make->items;
963         n->type = KDBUS_MAKE_NAME;
964         sprintf(n->str, "%lu-%s", (unsigned long) getuid(), name);
965         n->size = KDBUS_PART_HEADER_SIZE + strlen(n->str) + 1;
966
967         make->size = offsetof(struct kdbus_cmd_bus_make, items) + n->size;
968         make->flags = KDBUS_MAKE_POLICY_OPEN;
969         make->bus_flags = 0;
970         make->bloom_size = BLOOM_SIZE;
971         assert_cc(BLOOM_SIZE % 8 == 0);
972
973         p = strjoin("/dev/kdbus/", n->str, "/bus", NULL);
974         if (!p)
975                 return -ENOMEM;
976
977         if (ioctl(fd, KDBUS_CMD_BUS_MAKE, make) < 0) {
978                 close_nointr_nofail(fd);
979                 free(p);
980                 return -errno;
981         }
982
983         if (s)
984                 *s = p;
985
986         return fd;
987 }
988
989 int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *size) {
990         struct memfd_cache *c;
991         int fd;
992
993         assert(address);
994         assert(size);
995
996         if (!bus || !bus->is_kernel)
997                 return -ENOTSUP;
998
999         assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) >= 0);
1000
1001         if (bus->n_memfd_cache <= 0) {
1002                 int r;
1003
1004                 assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1005
1006                 r = ioctl(bus->input_fd, KDBUS_CMD_MEMFD_NEW, &fd);
1007                 if (r < 0)
1008                         return -errno;
1009
1010                 *address = NULL;
1011                 *size = 0;
1012                 return fd;
1013         }
1014
1015         c = &bus->memfd_cache[--bus->n_memfd_cache];
1016
1017         assert(c->fd >= 0);
1018         assert(c->size == 0 || c->address);
1019
1020         *address = c->address;
1021         *size = c->size;
1022         fd = c->fd;
1023
1024         assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1025
1026         return fd;
1027 }
1028
1029 static void close_and_munmap(int fd, void *address, size_t size) {
1030         if (size > 0)
1031                 assert_se(munmap(address, PAGE_ALIGN(size)) >= 0);
1032
1033         close_nointr_nofail(fd);
1034 }
1035
1036 void bus_kernel_push_memfd(sd_bus *bus, int fd, void *address, size_t size) {
1037         struct memfd_cache *c;
1038         uint64_t max_sz = PAGE_ALIGN(MEMFD_CACHE_ITEM_SIZE_MAX);
1039
1040         assert(fd >= 0);
1041         assert(size == 0 || address);
1042
1043         if (!bus || !bus->is_kernel) {
1044                 close_and_munmap(fd, address, size);
1045                 return;
1046         }
1047
1048         assert_se(pthread_mutex_lock(&bus->memfd_cache_mutex) >= 0);
1049
1050         if (bus->n_memfd_cache >= ELEMENTSOF(bus->memfd_cache)) {
1051                 assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1052
1053                 close_and_munmap(fd, address, size);
1054                 return;
1055         }
1056
1057         c = &bus->memfd_cache[bus->n_memfd_cache++];
1058         c->fd = fd;
1059         c->address = address;
1060
1061         /* If overly long, let's return a bit to the OS */
1062         if (size > max_sz) {
1063                 assert_se(ioctl(fd, KDBUS_CMD_MEMFD_SIZE_SET, &max_sz) >= 0);
1064                 assert_se(munmap((uint8_t*) address + max_sz, PAGE_ALIGN(size - max_sz)) >= 0);
1065                 c->size = max_sz;
1066         } else
1067                 c->size = size;
1068
1069         assert_se(pthread_mutex_unlock(&bus->memfd_cache_mutex) >= 0);
1070 }
1071
1072 void bus_kernel_flush_memfd(sd_bus *b) {
1073         unsigned i;
1074
1075         assert(b);
1076
1077         for (i = 0; i < b->n_memfd_cache; i++)
1078                 close_and_munmap(b->memfd_cache[i].fd, b->memfd_cache[i].address, b->memfd_cache[i].size);
1079 }
1080
1081 int kdbus_translate_request_name_flags(uint64_t sd_bus_flags, uint64_t *kdbus_flags) {
1082
1083         assert_return(kdbus_flags != NULL, -EINVAL);
1084
1085         *kdbus_flags = 0;
1086
1087         if (sd_bus_flags & SD_BUS_NAME_ALLOW_REPLACEMENT)
1088                 *kdbus_flags |= KDBUS_NAME_ALLOW_REPLACEMENT;
1089
1090         if (sd_bus_flags & SD_BUS_NAME_REPLACE_EXISTING)
1091                 *kdbus_flags |= KDBUS_NAME_REPLACE_EXISTING;
1092
1093         if (!(sd_bus_flags & SD_BUS_NAME_DO_NOT_QUEUE))
1094                 *kdbus_flags |= KDBUS_NAME_QUEUE;
1095
1096         return 0;
1097 }