1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-kdbus.c kdbus subclasses of DBusTransport
4 * Copyright (C) 2002, 2003, 2004, 2006 Red Hat Inc
5 * Copyright (C) 2013 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version and under the terms of the GNU
13 * Lesser General Public License as published by the
14 * Free Software Foundation; either version 2.1 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "dbus-transport.h"
28 #include "dbus-transport-kdbus.h"
29 #include "dbus-transport-protected.h"
30 #include "dbus-connection-internal.h"
32 #include "dbus-watch.h"
33 #include "dbus-errors.h"
35 #include "kdbus-common.h"
36 #include <linux/types.h>
40 #include <sys/ioctl.h>
50 * @defgroup DBusTransportKdbus DBusTransport implementations for kdbus
51 * @ingroup DBusInternals
52 * @brief Implementation details of DBusTransport on kdbus
57 /** Size of the memory area for received non-memfd messages. */
58 #define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU)
60 /** Over this memfd is used to send (if it is not broadcast). */
61 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU)
63 /** Define max bytes read or written in one iteration.
64 * This is to avoid blocking on reading or writing for too long. It is checked after each message is sent or received,
65 * so if message is bigger than MAX_BYTES_PER_ITERATION it will be handled in one iteration, but sending/writing
66 * will break after that message.
68 #define MAX_BYTES_PER_ITERATION 16384
70 #if (MEMFD_SIZE_THRESHOLD > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
71 #error Memfd size threshold higher than max kdbus message payload vector size
74 /** Enables verbosing more information about kdbus message.
75 * Works only if DBUS_VERBOSE=1 is used.
77 #define KDBUS_MSG_DECODE_DEBUG 0
79 #define ITER_APPEND_STR(string) \
80 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) \
86 #define MSG_ITEM_BUILD_VEC(data, datasize) \
87 item->type = KDBUS_ITEM_PAYLOAD_VEC; \
88 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec); \
89 item->vec.address = (unsigned long) data; \
90 item->vec.size = datasize;
92 #define KDBUS_PART_FOREACH(part, head, first) \
93 for (part = (head)->first; \
94 (uint8_t *)(part) < (uint8_t *)(head) + (head)->size; \
95 part = KDBUS_PART_NEXT(part))
98 * Opaque object representing a transport.
100 typedef struct DBusTransportKdbus DBusTransportKdbus;
103 * Implementation details of DBusTransportKdbus. All members are private.
105 struct DBusTransportKdbus
107 DBusTransport base; /**< Parent instance */
108 int fd; /**< File descriptor. */
109 DBusWatch *read_watch; /**< Watch for readability. */
110 DBusWatch *write_watch; /**< Watch for writability. */
112 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
113 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
115 void* kdbus_mmap_ptr; /**< Mapped memory where kdbus (kernel) writes
116 * messages incoming to us.
118 int memfd; /**< File descriptor to special
119 * memory pool for bulk data
120 * transfer. Retrieved from
121 * Kdbus kernel module.
123 __u64 bloom_size; /**< bloom filter field size */
124 char* sender; /**< unique name of the sender */
128 * Gets size in bytes of bloom filter field.
129 * This size is got from the bus during connection procedure.
130 * @param transport transport
131 * @returns size of bloom
133 __u64 dbus_transport_get_bloom_size(DBusTransport* transport)
135 return ((DBusTransportKdbus*)transport)->bloom_size;
139 * Gets pointer to the memory pool, wher received messages are
140 * placed and some ioctls return their info
141 * @param transport transport
142 * @returns pointer to the pool
144 void* dbus_transport_get_pool_pointer(DBusTransport* transport)
146 return ((DBusTransportKdbus*)transport)->kdbus_mmap_ptr;
150 * Puts locally generated message into received messages queue
151 * @param message message that will be added
152 * @param connection connection to which message will be added
153 * @returns TRUE on success, FALSE on memory allocation error
155 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* connection)
157 DBusList *message_link;
159 message_link = _dbus_list_alloc_link (message);
160 if (message_link == NULL)
162 dbus_message_unref (message);
166 _dbus_connection_queue_synthesized_message_link(connection, message_link);
172 * Generates local error message as a reply to message given as parameter
173 * and adds generated error message to received messages queue.
174 * @param error_type type of error, preferably DBUS_ERROR_(...)
175 * @param template Template of error description. It can has formatting
176 * characters to print object string into it. Can be NULL.
177 * @param object String to print into error description. Can be NULL.
178 * If object is not NULL while template is NULL, the object string
179 * will be the only error description.
180 * @param message Message for which the error reply is generated.
181 * @param connection The connection.
182 * @returns 0 on success, otherwise -1
184 static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
186 DBusMessage *errMessage;
187 char* error_msg = "";
191 error_msg = alloca(strlen(template) + strlen(object));
192 sprintf(error_msg, template, object);
195 error_msg = (char*)object;
197 errMessage = generate_local_error_message(dbus_message_get_serial(message), error_type, error_msg);
198 if(errMessage == NULL)
200 if (add_message_to_received(errMessage, connection))
207 * Generates reply to the message given as a parameter with one item in the reply body
208 * and adds generated reply message to received messages queue.
209 * @param message The message we are replying to.
210 * @param data_type Type of data sent in the reply.Use DBUS_TYPE_(...)
211 * @param pData Address of data sent in the reply.
212 * @param connection The connection
213 * @returns 0 on success, otherwise -1
215 static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
217 DBusMessageIter args;
220 reply = dbus_message_new_method_return(message);
223 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
224 dbus_message_iter_init_append(reply, &args);
225 if (!dbus_message_iter_append_basic(&args, data_type, pData))
227 dbus_message_unref(reply);
230 if(add_message_to_received(reply, connection))
237 static int reply_ack(DBusMessage *message, DBusConnection* connection)
241 reply = dbus_message_new_method_return(message);
244 if(add_message_to_received(reply, connection))
250 * Retrieves file descriptor to memory pool from kdbus module and stores
251 * it in kdbus_transport->memfd. It is then used to send large message.
252 * Triggered when message payload is over MEMFD_SIZE_THRESHOLD
253 * @param kdbus_transport DBusTransportKdbus transport structure
254 * @returns 0 on success, otherwise -1
256 static int kdbus_init_memfd(DBusTransportKdbus* kdbus_transport)
260 if (ioctl(kdbus_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) {
261 _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
265 kdbus_transport->memfd = memfd;
266 _dbus_verbose("kdbus_init_memfd: %d!!\n", kdbus_transport->memfd);
271 * Allocates and initializes kdbus message structure.
272 * @param name Well-known name or NULL. If NULL, dst_id must be supplied.
273 * @param dst_id Numeric id of recipient. Ignored if name is not NULL.
274 * @param body_size Size of message body (May be 0).
275 * @param use_memfd Flag to build memfd message.
276 * @param fds_count Number of file descriptors sent in the message.
277 * @param transport transport
278 * @returns initialized kdbus message or NULL if malloc failed
280 static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t body_size, dbus_bool_t use_memfd, int fds_count, DBusTransportKdbus *transport)
282 struct kdbus_msg* msg;
285 msg_size = sizeof(struct kdbus_msg);
287 if(use_memfd == TRUE) // bulk data - memfd
288 msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_memfd));
291 msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec)); //header is a must
292 while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
294 msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
295 body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
298 msg_size += KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
302 msg_size += KDBUS_PART_SIZE(sizeof(int)*fds_count);
305 msg_size += KDBUS_PART_SIZE(strlen(name) + 1);
306 else if (dst_id == KDBUS_DST_ID_BROADCAST)
307 msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
309 msg = malloc(msg_size);
312 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
316 memset(msg, 0, msg_size);
317 msg->size = msg_size;
318 msg->payload_type = KDBUS_PAYLOAD_DBUS;
319 msg->dst_id = name ? 0 : dst_id;
320 msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
326 * Sends DBus message using kdbus.
327 * Handles broadcasts and unicast messages, and passing of Unix fds.
328 * Also can locally generate error replies on some error returned by kernel.
330 * TODO refactor to be more compact - maybe we can send header always as a payload vector
331 * and only message body as memfd if needed.
333 * @param transport Transport.
334 * @param message DBus message to be sent
335 * @param destination Destination of the message.
336 * @returns bytes sent or -1 if sending failed
338 static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination)
340 struct kdbus_msg *msg;
341 struct kdbus_item *item;
342 uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
343 const DBusString *header;
344 const DBusString *body;
345 uint64_t ret_size = 0;
346 uint64_t body_size = 0;
347 uint64_t header_size = 0;
348 dbus_bool_t use_memfd = FALSE;
351 dbus_bool_t autostart;
353 // determine destination and destination id
356 dst_id = KDBUS_DST_ID_NAME;
357 if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
359 dst_id = strtoull(&destination[3], NULL, 10);
364 _dbus_message_get_network_data (message, &header, &body);
365 header_size = _dbus_string_get_length(header);
366 body_size = _dbus_string_get_length(body);
367 ret_size = header_size + body_size;
369 // check whether we can and should use memfd
370 if((dst_id != KDBUS_DST_ID_BROADCAST) && (ret_size > MEMFD_SIZE_THRESHOLD))
373 kdbus_init_memfd(transport);
376 _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
378 // init basic message fields
379 msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport);
380 msg->cookie = dbus_message_get_serial(message);
381 autostart = dbus_message_get_auto_start (message);
383 msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START;
385 // build message contents
392 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
394 _dbus_verbose("memfd sealing failed: \n");
398 buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
399 if (buf == MAP_FAILED)
401 _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
405 memcpy(buf, _dbus_string_get_const_data(header), header_size);
408 memcpy(buf, _dbus_string_get_const_data(body), body_size);
412 munmap(buf, ret_size);
414 // seal data - kdbus module needs it
415 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) {
416 _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
421 item->type = KDBUS_ITEM_PAYLOAD_MEMFD;
422 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
423 item->memfd.size = ret_size;
424 item->memfd.fd = transport->memfd;
428 _dbus_verbose("sending normal vector data\n");
429 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size);
433 const char* body_data;
435 body_data = _dbus_string_get_const_data(body);
436 while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
438 _dbus_verbose("body attaching\n");
439 item = KDBUS_PART_NEXT(item);
440 MSG_ITEM_BUILD_VEC(body_data, KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE);
441 body_data += KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
442 body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
446 _dbus_verbose("body attaching\n");
447 item = KDBUS_PART_NEXT(item);
448 MSG_ITEM_BUILD_VEC(body_data, body_size);
455 item = KDBUS_PART_NEXT(item);
456 item->type = KDBUS_ITEM_FDS;
457 item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
458 memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
463 item = KDBUS_PART_NEXT(item);
464 item->type = KDBUS_ITEM_DST_NAME;
465 item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1;
466 memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE);
468 else if (dst_id == KDBUS_DST_ID_BROADCAST)
470 item = KDBUS_PART_NEXT(item);
471 item->type = KDBUS_ITEM_BLOOM;
472 item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
473 strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
477 if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
481 else if(errno == ENXIO) //no such id on the bus
483 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
486 else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus
490 if(!reply_with_error(DBUS_ERROR_SERVICE_UNKNOWN, "The name %s was not provided by any .service files", dbus_message_get_destination(message), message, transport->base.connection))
494 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
497 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
503 close(transport->memfd);
509 * Performs kdbus hello - registration on the kdbus bus
510 * needed to send and receive messages on the bus,
511 * and configures transport.
512 * As a result unique id on he bus is obtained.
514 * @param name place to print id given by bus
515 * @param transportS transport structure
516 * @returns #TRUE on success
518 static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS)
520 struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
521 memset(&hello, 0, sizeof(hello));
523 hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
524 KDBUS_HELLO_ATTACH_COMM |
525 KDBUS_HELLO_ATTACH_EXE |
526 KDBUS_HELLO_ATTACH_CMDLINE |
527 KDBUS_HELLO_ATTACH_CAPS |
528 KDBUS_HELLO_ATTACH_CGROUP |
529 KDBUS_HELLO_ATTACH_SECLABEL |
530 KDBUS_HELLO_ATTACH_AUDIT*/;
531 hello.size = sizeof(struct kdbus_cmd_hello);
532 hello.pool_size = RECEIVE_POOL_SIZE;
534 if (ioctl(transportS->fd, KDBUS_CMD_HELLO, &hello))
536 _dbus_verbose ("Failed to send hello: %m, %d",errno);
540 sprintf(name, "%llu", (unsigned long long)hello.id);
541 _dbus_verbose("-- Our peer ID is: %s\n", name);
542 transportS->bloom_size = hello.bloom_size;
544 transportS->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, transportS->fd, 0);
545 if (transportS->kdbus_mmap_ptr == MAP_FAILED)
547 _dbus_verbose("Error when mmap: %m, %d",errno);
555 * Looks over messages sent to org.freedesktop.DBus. Hello message, which performs
556 * registration on the bus, is captured as it must be locally converted into
557 * appropriate ioctl. All the rest org.freedesktop.DBus methods are left untouched
558 * and they are sent to dbus-daemon in the same way as every other messages.
560 * @param transport Transport
561 * @param message Message being sent.
562 * @returns 1 if it is not Hello message and it should be passed to daemon
563 * 0 if Hello message was handled correctly,
564 * -1 if Hello message was not handle correctly.
566 static int capture_hello_message(DBusTransport *transport, const char* destination, DBusMessage *message)
568 if(!strcmp(destination, DBUS_SERVICE_DBUS))
570 if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
572 if(!strcmp(dbus_message_get_member(message), "Hello"))
576 name = malloc(snprintf(name, 0, ":1.%llu0", ULLONG_MAX));
580 if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
582 #ifdef POLICY_TO_KDBUS
583 if(!register_kdbus_policy(&name[3], transport, geteuid()))
586 ((DBusTransportKdbus*)transport)->sender = name;
588 if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
589 return 0; //on success we can not free name
598 return 1; //send message to daemon
601 #if KDBUS_MSG_DECODE_DEBUG == 1
602 static char *msg_id(uint64_t id, char *buf)
608 sprintf(buf, "%llu", (unsigned long long)id);
612 struct kdbus_enum_table {
616 #define _STRINGIFY(x) #x
617 #define STRINGIFY(x) _STRINGIFY(x)
618 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
619 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
620 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
621 #define LOOKUP(what) \
622 const char *enum_##what(long long id) { \
624 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
625 if (id == kdbus_table_##what[i].id) \
626 return kdbus_table_##what[i].name; \
629 const char *enum_MSG(long long id);
631 ENUM(_KDBUS_ITEM_NULL),
632 ENUM(KDBUS_ITEM_PAYLOAD_VEC),
633 ENUM(KDBUS_ITEM_PAYLOAD_OFF),
634 ENUM(KDBUS_ITEM_PAYLOAD_MEMFD),
635 ENUM(KDBUS_ITEM_FDS),
636 ENUM(KDBUS_ITEM_BLOOM),
637 ENUM(KDBUS_ITEM_DST_NAME),
638 ENUM(KDBUS_ITEM_CREDS),
639 ENUM(KDBUS_ITEM_PID_COMM),
640 ENUM(KDBUS_ITEM_TID_COMM),
641 ENUM(KDBUS_ITEM_EXE),
642 ENUM(KDBUS_ITEM_CMDLINE),
643 ENUM(KDBUS_ITEM_CGROUP),
644 ENUM(KDBUS_ITEM_CAPS),
645 ENUM(KDBUS_ITEM_SECLABEL),
646 ENUM(KDBUS_ITEM_AUDIT),
647 ENUM(KDBUS_ITEM_NAME),
648 ENUM(KDBUS_ITEM_TIMESTAMP),
649 ENUM(KDBUS_ITEM_NAME_ADD),
650 ENUM(KDBUS_ITEM_NAME_REMOVE),
651 ENUM(KDBUS_ITEM_NAME_CHANGE),
652 ENUM(KDBUS_ITEM_ID_ADD),
653 ENUM(KDBUS_ITEM_ID_REMOVE),
654 ENUM(KDBUS_ITEM_REPLY_TIMEOUT),
655 ENUM(KDBUS_ITEM_REPLY_DEAD),
658 const char *enum_PAYLOAD(long long id);
660 ENUM(KDBUS_PAYLOAD_KERNEL),
661 ENUM(KDBUS_PAYLOAD_DBUS),
666 * Finalizes locally generated DBus message
667 * and puts it into data buffer.
669 * @param message Message to load.
670 * @param data Place to load message.
671 * @returns Size of message loaded.
673 static int put_message_into_data(DBusMessage *message, char* data)
676 const DBusString *header;
677 const DBusString *body;
680 dbus_message_set_serial(message, 1);
681 dbus_message_lock (message);
682 _dbus_message_get_network_data (message, &header, &body);
683 ret_size = _dbus_string_get_length(header);
684 memcpy(data, _dbus_string_get_const_data(header), ret_size);
686 size = _dbus_string_get_length(body);
687 memcpy(data, _dbus_string_get_const_data(body), size);
694 * Calculates length of the kdbus message content (payload).
696 * @param msg kdbus message
697 * @return the length of the kdbus message's payload.
699 static int kdbus_message_size(const struct kdbus_msg* msg)
701 const struct kdbus_item *item;
704 KDBUS_PART_FOREACH(item, msg, items)
706 if (item->size <= KDBUS_PART_HEADER_SIZE)
708 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
713 case KDBUS_ITEM_PAYLOAD_OFF:
714 ret_size += item->vec.size;
716 case KDBUS_ITEM_PAYLOAD_MEMFD:
717 ret_size += item->memfd.size;
728 * Decodes kdbus message in order to extract DBus message and puts it into received data buffer
729 * and file descriptor's buffer. Also captures kdbus error messages and kdbus kernel broadcasts
730 * and converts all of them into appropriate DBus messages.
732 * @param msg kdbus message
733 * @param data place to copy DBus message to
734 * @param kdbus_transport transport
735 * @param fds place to store file descriptors received
736 * @param n_fds place to store quantity of file descriptors received
737 * @return number of DBus message's bytes received or -1 on error
739 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportKdbus* kdbus_transport, int* fds, int* n_fds)
741 const struct kdbus_item *item;
743 DBusMessage *message = NULL;
744 DBusMessageIter args;
745 const char* emptyString = "";
746 const char* pString = NULL;
747 char dbus_name[(unsigned int)(snprintf((char*)pString, 0, ":1.%llu0", ULLONG_MAX))];
748 const char* pDBusName = dbus_name;
749 #if KDBUS_MSG_DECODE_DEBUG == 1
753 #if KDBUS_MSG_DECODE_DEBUG == 1
754 _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
755 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
756 (unsigned long long) msg->flags,
757 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
758 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
763 KDBUS_PART_FOREACH(item, msg, items)
765 if (item->size <= KDBUS_PART_HEADER_SIZE)
767 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
768 break; //??? continue (because dbus will find error) or break
773 case KDBUS_ITEM_PAYLOAD_OFF:
774 memcpy(data, (char *)kdbus_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
775 data += item->vec.size;
776 ret_size += item->vec.size;
778 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
779 enum_MSG(item->type), item->size,
780 (unsigned long long)item->vec.offset,
781 (unsigned long long)item->vec.size);
784 case KDBUS_ITEM_PAYLOAD_MEMFD:
789 size = item->memfd.size;
790 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
792 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
793 if (buf == MAP_FAILED)
795 _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
799 memcpy(data, buf, size);
805 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
806 enum_MSG(item->type), item->size,
807 (unsigned long long)item->vec.offset,
808 (unsigned long long)item->vec.size);
816 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
817 memcpy(fds, item->fds, *n_fds * sizeof(int));
818 for (i = 0; i < *n_fds; i++)
819 _dbus_fd_set_close_on_exec(fds[i]);
823 #if KDBUS_MSG_DECODE_DEBUG == 1
824 case KDBUS_ITEM_CREDS:
825 _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
826 enum_MSG(item->type), item->size,
827 item->creds.uid, item->creds.gid,
828 item->creds.pid, item->creds.tid,
829 item->creds.starttime);
832 case KDBUS_ITEM_PID_COMM:
833 case KDBUS_ITEM_TID_COMM:
835 case KDBUS_ITEM_CGROUP:
836 case KDBUS_ITEM_SECLABEL:
837 case KDBUS_ITEM_DST_NAME:
838 _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
839 enum_MSG(item->type), item->size, item->str, strlen(item->str));
842 case KDBUS_ITEM_CMDLINE:
843 case KDBUS_ITEM_NAME: {
844 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
845 const char *str = item->str;
848 _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
850 _dbus_verbose("'%s' ", str);
851 size -= strlen(str) + 1;
852 str += strlen(str) + 1;
856 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
860 case KDBUS_ITEM_AUDIT:
861 _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
862 enum_MSG(item->type), item->size,
863 (unsigned long long)item->data64[0],
864 (unsigned long long)item->data64[1]);
867 case KDBUS_ITEM_CAPS: {
872 _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
873 enum_MSG(item->type), item->size,
874 (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
877 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
879 _dbus_verbose(" CapInh=");
880 for (i = 0; i < n; i++)
881 _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
883 _dbus_verbose(" CapPrm=");
884 for (i = 0; i < n; i++)
885 _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
887 _dbus_verbose(" CapEff=");
888 for (i = 0; i < n; i++)
889 _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
891 _dbus_verbose(" CapInh=");
892 for (i = 0; i < n; i++)
893 _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
898 case KDBUS_ITEM_TIMESTAMP:
899 _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
900 enum_MSG(item->type), item->size,
901 (unsigned long long)item->timestamp.realtime_ns,
902 (unsigned long long)item->timestamp.monotonic_ns);
906 case KDBUS_ITEM_REPLY_TIMEOUT:
907 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
908 enum_MSG(item->type), item->size, msg->cookie_reply);
910 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
917 ret_size = put_message_into_data(message, data);
920 case KDBUS_ITEM_REPLY_DEAD:
921 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
922 enum_MSG(item->type), item->size, msg->cookie_reply);
924 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
931 ret_size = put_message_into_data(message, data);
934 case KDBUS_ITEM_NAME_ADD:
935 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
936 enum_MSG(item->type), (unsigned long long) item->size,
937 item->name_change.name, item->name_change.old_id,
938 item->name_change.new_id, item->name_change.flags);
940 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
947 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
948 pString = item->name_change.name;
949 _dbus_verbose ("Name added: %s\n", pString);
950 dbus_message_iter_init_append(message, &args);
951 ITER_APPEND_STR(pString)
952 ITER_APPEND_STR(emptyString)
953 ITER_APPEND_STR(pDBusName)
954 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
956 ret_size = put_message_into_data(message, data);
959 case KDBUS_ITEM_NAME_REMOVE:
960 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
961 enum_MSG(item->type), (unsigned long long) item->size,
962 item->name_change.name, item->name_change.old_id,
963 item->name_change.new_id, item->name_change.flags);
965 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
972 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
973 pString = item->name_change.name;
974 _dbus_verbose ("Name removed: %s\n", pString);
975 dbus_message_iter_init_append(message, &args);
976 ITER_APPEND_STR(pString)
977 ITER_APPEND_STR(pDBusName)
978 ITER_APPEND_STR(emptyString)
979 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
981 ret_size = put_message_into_data(message, data);
984 case KDBUS_ITEM_NAME_CHANGE:
985 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
986 enum_MSG(item->type), (unsigned long long) item->size,
987 item->name_change.name, item->name_change.old_id,
988 item->name_change.new_id, item->name_change.flags);
990 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
997 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
998 pString = item->name_change.name;
999 _dbus_verbose ("Name changed: %s\n", pString);
1000 dbus_message_iter_init_append(message, &args);
1001 ITER_APPEND_STR(pString)
1002 ITER_APPEND_STR(pDBusName)
1003 sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
1004 _dbus_verbose ("New id: %s\n", pDBusName);
1005 ITER_APPEND_STR(pDBusName)
1006 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1008 ret_size = put_message_into_data(message, data);
1011 case KDBUS_ITEM_ID_ADD:
1012 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1013 enum_MSG(item->type), (unsigned long long) item->size,
1014 (unsigned long long) item->id_change.id,
1015 (unsigned long long) item->id_change.flags);
1017 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1024 sprintf(dbus_name,":1.%llu",item->id_change.id);
1025 dbus_message_iter_init_append(message, &args);
1026 ITER_APPEND_STR(pDBusName)
1027 ITER_APPEND_STR(emptyString)
1028 ITER_APPEND_STR(pDBusName)
1029 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1031 ret_size = put_message_into_data(message, data);
1034 case KDBUS_ITEM_ID_REMOVE:
1035 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1036 enum_MSG(item->type), (unsigned long long) item->size,
1037 (unsigned long long) item->id_change.id,
1038 (unsigned long long) item->id_change.flags);
1040 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1047 sprintf(dbus_name,":1.%llu",item->id_change.id);
1048 dbus_message_iter_init_append(message, &args);
1049 ITER_APPEND_STR(pDBusName)
1050 ITER_APPEND_STR(pDBusName)
1051 ITER_APPEND_STR(emptyString)
1052 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1054 ret_size = put_message_into_data(message, data);
1056 #if KDBUS_MSG_DECODE_DEBUG == 1
1058 _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
1064 #if KDBUS_MSG_DECODE_DEBUG == 1
1066 if ((char *)item - ((char *)msg + msg->size) >= 8)
1067 _dbus_verbose("invalid padding at end of message\n");
1072 dbus_message_unref(message);
1077 * Reads message from kdbus and puts it into DBus buffers
1079 * @param kdbus_transport transport
1080 * @param buffer place to copy received message to
1081 * @param fds place to store file descriptors received with the message
1082 * @param n_fds place to store quantity of file descriptors received
1083 * @return size of received message on success, -1 on error
1085 static int kdbus_read_message(DBusTransportKdbus *kdbus_transport, DBusString *buffer, int* fds, int* n_fds)
1087 int ret_size, buf_size;
1088 uint64_t __attribute__ ((__aligned__(8))) offset;
1089 struct kdbus_msg *msg;
1093 start = _dbus_string_get_length (buffer);
1096 if (ioctl(kdbus_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1100 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1101 _dbus_string_set_length (buffer, start);
1105 msg = (struct kdbus_msg *)((char*)kdbus_transport->kdbus_mmap_ptr + offset);
1107 buf_size = kdbus_message_size(msg);
1110 _dbus_verbose("kdbus error - too short message: %d (%m)\n", errno);
1114 /* What is the maximum size of the locally generated message?
1115 I just assume 2048 bytes */
1116 buf_size = MAX(buf_size, 2048);
1118 if (!_dbus_string_lengthen (buffer, buf_size))
1123 data = _dbus_string_get_data_len (buffer, start, buf_size);
1125 ret_size = kdbus_decode_msg(msg, data, kdbus_transport, fds, n_fds);
1127 if(ret_size == -1) /* error */
1129 _dbus_string_set_length (buffer, start);
1132 else if (buf_size != ret_size) /* case of locally generated message */
1134 _dbus_string_set_length (buffer, start + ret_size);
1138 if (ioctl(kdbus_transport->fd, KDBUS_CMD_FREE, &offset) < 0)
1142 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1150 * Copy-paste from socket transport. Only renames done.
1153 free_watches (DBusTransport *transport)
1155 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1157 _dbus_verbose ("start\n");
1159 if (kdbus_transport->read_watch)
1161 if (transport->connection)
1162 _dbus_connection_remove_watch_unlocked (transport->connection,
1163 kdbus_transport->read_watch);
1164 _dbus_watch_invalidate (kdbus_transport->read_watch);
1165 _dbus_watch_unref (kdbus_transport->read_watch);
1166 kdbus_transport->read_watch = NULL;
1169 if (kdbus_transport->write_watch)
1171 if (transport->connection)
1172 _dbus_connection_remove_watch_unlocked (transport->connection,
1173 kdbus_transport->write_watch);
1174 _dbus_watch_invalidate (kdbus_transport->write_watch);
1175 _dbus_watch_unref (kdbus_transport->write_watch);
1176 kdbus_transport->write_watch = NULL;
1179 _dbus_verbose ("end\n");
1183 * Copy-paste from socket transport. Only done needed renames and removed
1184 * lines related to encoded messages.
1187 transport_finalize (DBusTransport *transport)
1189 _dbus_verbose ("\n");
1191 free_watches (transport);
1193 _dbus_transport_finalize_base (transport);
1195 _dbus_assert (((DBusTransportKdbus*) transport)->read_watch == NULL);
1196 _dbus_assert (((DBusTransportKdbus*) transport)->write_watch == NULL);
1198 dbus_free (transport);
1202 * Copy-paste from socket transport. Removed code related to authentication,
1203 * socket_transport replaced by kdbus_transport.
1206 check_write_watch (DBusTransport *transport)
1208 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1211 if (transport->connection == NULL)
1214 if (transport->disconnected)
1216 _dbus_assert (kdbus_transport->write_watch == NULL);
1220 _dbus_transport_ref (transport);
1222 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1224 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1225 needed, transport->connection, kdbus_transport->write_watch,
1226 kdbus_transport->fd,
1227 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1229 _dbus_connection_toggle_watch_unlocked (transport->connection,
1230 kdbus_transport->write_watch,
1233 _dbus_transport_unref (transport);
1237 * Copy-paste from socket transport. Removed code related to authentication,
1238 * socket_transport replaced by kdbus_transport.
1241 check_read_watch (DBusTransport *transport)
1243 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1244 dbus_bool_t need_read_watch;
1246 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1248 if (transport->connection == NULL)
1251 if (transport->disconnected)
1253 _dbus_assert (kdbus_transport->read_watch == NULL);
1257 _dbus_transport_ref (transport);
1260 (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1261 (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1263 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
1264 _dbus_connection_toggle_watch_unlocked (transport->connection,
1265 kdbus_transport->read_watch,
1268 _dbus_transport_unref (transport);
1272 * Copy-paste from socket transport.
1275 do_io_error (DBusTransport *transport)
1277 _dbus_transport_ref (transport);
1278 _dbus_transport_disconnect (transport);
1279 _dbus_transport_unref (transport);
1283 * Based on do_writing from socket transport.
1284 * Removed authentication code and code related to encoded messages
1285 * and adapted to kdbus transport.
1286 * In socket transport returns false on out-of-memory. Here this won't happen,
1287 * so it always returns TRUE.
1290 do_writing (DBusTransport *transport)
1292 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1295 if (transport->disconnected)
1297 _dbus_verbose ("Not connected, not writing anything\n");
1301 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
1302 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
1303 kdbus_transport->fd);
1305 while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1308 DBusMessage *message;
1309 const DBusString *header;
1310 const DBusString *body;
1311 int total_bytes_to_write;
1312 const char* pDestination;
1314 if (total > kdbus_transport->max_bytes_written_per_iteration)
1316 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
1317 total, kdbus_transport->max_bytes_written_per_iteration);
1321 message = _dbus_connection_get_message_to_send (transport->connection);
1322 _dbus_assert (message != NULL);
1323 if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages
1325 dbus_message_unlock(message);
1326 dbus_message_set_sender(message, kdbus_transport->sender);
1327 dbus_message_lock (message);
1329 _dbus_message_get_network_data (message, &header, &body);
1330 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
1331 pDestination = dbus_message_get_destination(message);
1337 ret = capture_hello_message(transport, pDestination, message);
1343 else if(ret == 0) //hello message captured and handled correctly
1345 bytes_written = total_bytes_to_write;
1348 //else send as regular message
1351 bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination);
1354 if (bytes_written < 0)
1356 /* EINTR already handled for us */
1358 /* For some discussion of why we also ignore EPIPE here, see
1359 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
1362 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
1366 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
1367 do_io_error (transport);
1373 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
1374 total_bytes_to_write);
1376 total += bytes_written;
1378 _dbus_assert (bytes_written == total_bytes_to_write);
1380 _dbus_connection_message_sent_unlocked (transport->connection,
1390 * Based on do_reading from socket transport.
1391 * Removed authentication code and code related to encoded messages
1392 * and adapted to kdbus transport.
1393 * returns false on out-of-memory
1396 do_reading (DBusTransport *transport)
1398 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1401 dbus_bool_t oom = FALSE;
1405 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1409 /* See if we've exceeded max messages and need to disable reading */
1410 check_read_watch (transport);
1412 if (total > kdbus_transport->max_bytes_read_per_iteration)
1414 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
1415 total, kdbus_transport->max_bytes_read_per_iteration);
1419 _dbus_assert (kdbus_transport->read_watch != NULL ||
1420 transport->disconnected);
1422 if (transport->disconnected)
1425 if (!dbus_watch_get_enabled (kdbus_transport->read_watch))
1428 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1430 _dbus_verbose ("Out of memory reading file descriptors\n");
1434 _dbus_message_loader_get_buffer (transport->loader, &buffer);
1436 bytes_read = kdbus_read_message(kdbus_transport, buffer, fds, &n_fds);
1438 if (bytes_read >= 0 && n_fds > 0)
1439 _dbus_verbose("Read %i unix fds\n", n_fds);
1441 _dbus_message_loader_return_buffer (transport->loader,
1443 bytes_read < 0 ? 0 : bytes_read);
1444 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1448 /* EINTR already handled for us */
1450 if (_dbus_get_is_errno_enomem ())
1452 _dbus_verbose ("Out of memory in read()/do_reading()\n");
1456 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1460 _dbus_verbose ("Error reading from remote app: %s\n",
1461 _dbus_strerror_from_errno ());
1462 do_io_error (transport);
1466 else if (bytes_read == 0)
1468 _dbus_verbose ("Disconnected from remote app\n");
1469 do_io_error (transport);
1474 _dbus_verbose (" read %d bytes\n", bytes_read);
1476 total += bytes_read;
1478 if (!_dbus_transport_queue_messages (transport))
1481 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
1485 /* Try reading more data until we get EAGAIN and return, or
1486 * exceed max bytes per iteration. If in blocking mode of
1487 * course we'll block instead of returning.
1499 * Copy-paste from socket transport, with socket replaced by kdbus.
1502 unix_error_with_read_to_come (DBusTransport *itransport,
1506 DBusTransportKdbus *transport = (DBusTransportKdbus *) itransport;
1508 if (!((flags & DBUS_WATCH_HANGUP) || (flags & DBUS_WATCH_ERROR)))
1511 /* If we have a read watch enabled ...
1512 we -might have data incoming ... => handle the HANGUP there */
1513 if (watch != transport->read_watch && _dbus_watch_get_enabled (transport->read_watch))
1520 * Copy-paste from socket transport. Removed authentication related code
1521 * and renamed socket_transport to kdbus_transport.
1524 kdbus_handle_watch (DBusTransport *transport,
1528 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1530 _dbus_assert (watch == kdbus_transport->read_watch ||
1531 watch == kdbus_transport->write_watch);
1532 _dbus_assert (watch != NULL);
1534 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
1535 * still be in the buffer and do_reading may need several iteration to read
1536 * it all (because of its max_bytes_read_per_iteration limit).
1538 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
1540 _dbus_verbose ("Hang up or error on watch\n");
1541 _dbus_transport_disconnect (transport);
1545 if (watch == kdbus_transport->read_watch &&
1546 (flags & DBUS_WATCH_READABLE))
1548 _dbus_verbose ("handling read watch %p flags = %x\n",
1551 if (!do_reading (transport))
1553 _dbus_verbose ("no memory to read\n");
1558 else if (watch == kdbus_transport->write_watch &&
1559 (flags & DBUS_WATCH_WRITABLE))
1561 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1562 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1564 if (!do_writing (transport))
1566 _dbus_verbose ("no memory to write\n");
1570 /* See if we still need the write watch */
1571 check_write_watch (transport);
1578 * Copy-paste from socket transport, but socket_transport renamed to kdbus_transport
1579 * and _dbus_close_socket replaced with close().
1582 kdbus_disconnect (DBusTransport *transport)
1584 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1586 _dbus_verbose ("\n");
1588 free_watches (transport);
1591 if (close (kdbus_transport->fd) < 0)
1597 kdbus_transport->fd = -1;
1601 * Copy-paste from socket transport. Renamed socket_transport to
1602 * kdbus_transport and added dbus_connection_set_is_authenticated, because
1603 * we do not perform authentication in kdbus, so we have mark is as already done
1604 * to make everything work.
1607 kdbus_connection_set (DBusTransport *transport)
1609 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1611 dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus, so mark it done
1613 _dbus_watch_set_handler (kdbus_transport->write_watch,
1614 _dbus_connection_handle_watch,
1615 transport->connection, NULL);
1617 _dbus_watch_set_handler (kdbus_transport->read_watch,
1618 _dbus_connection_handle_watch,
1619 transport->connection, NULL);
1621 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1622 kdbus_transport->write_watch))
1625 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1626 kdbus_transport->read_watch))
1628 _dbus_connection_remove_watch_unlocked (transport->connection,
1629 kdbus_transport->write_watch);
1633 check_read_watch (transport);
1634 check_write_watch (transport);
1640 * Copy-paste from socket_transport.
1641 * Socket_transport renamed to kdbus_transport
1643 * Original dbus copy-pasted @todo comment below.
1644 * @todo We need to have a way to wake up the select sleep if
1645 * a new iteration request comes in with a flag (read/write) that
1646 * we're not currently serving. Otherwise a call that just reads
1647 * could block a write call forever (if there are no incoming
1651 kdbus_do_iteration (DBusTransport *transport,
1653 int timeout_milliseconds)
1655 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1660 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
1661 flags & DBUS_ITERATION_DO_READING ? "read" : "",
1662 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1663 timeout_milliseconds,
1664 kdbus_transport->read_watch,
1665 kdbus_transport->write_watch,
1666 kdbus_transport->fd);
1668 poll_fd.fd = kdbus_transport->fd;
1671 /* This is kind of a hack; if we have stuff to write, then try
1672 * to avoid the poll. This is probably about a 5% speedup on an
1673 * echo client/server.
1675 * If both reading and writing were requested, we want to avoid this
1676 * since it could have funky effects:
1677 * - both ends spinning waiting for the other one to read
1678 * data so they can finish writing
1679 * - prioritizing all writing ahead of reading
1681 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1682 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1683 !transport->disconnected &&
1684 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1686 do_writing (transport);
1688 if (transport->disconnected ||
1689 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
1693 /* If we get here, we decided to do the poll() after all */
1694 _dbus_assert (kdbus_transport->read_watch);
1695 if (flags & DBUS_ITERATION_DO_READING)
1696 poll_fd.events |= _DBUS_POLLIN;
1698 _dbus_assert (kdbus_transport->write_watch);
1699 if (flags & DBUS_ITERATION_DO_WRITING)
1700 poll_fd.events |= _DBUS_POLLOUT;
1704 if (flags & DBUS_ITERATION_BLOCK)
1705 poll_timeout = timeout_milliseconds;
1709 /* For blocking selects we drop the connection lock here
1710 * to avoid blocking out connection access during a potentially
1711 * indefinite blocking call. The io path is still protected
1712 * by the io_path_cond condvar, so we won't reenter this.
1714 if (flags & DBUS_ITERATION_BLOCK)
1716 _dbus_verbose ("unlock pre poll\n");
1717 _dbus_connection_unlock (transport->connection);
1721 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1723 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1726 if (flags & DBUS_ITERATION_BLOCK)
1728 _dbus_verbose ("lock post poll\n");
1729 _dbus_connection_lock (transport->connection);
1735 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1736 * valgrind flags it as an error. though it probably
1737 * is guaranteed on linux at least.
1740 if (poll_fd.revents & _DBUS_POLLERR)
1741 do_io_error (transport);
1744 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1745 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1747 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1748 need_read, need_write);
1750 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1751 do_reading (transport);
1752 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1753 do_writing (transport);
1757 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
1761 /* We need to install the write watch only if we did not
1762 * successfully write everything. Note we need to be careful that we
1763 * don't call check_write_watch *before* do_writing, since it's
1764 * inefficient to add the write watch, and we can avoid it most of
1765 * the time since we can write immediately.
1767 * However, we MUST always call check_write_watch(); DBusConnection code
1768 * relies on the fact that running an iteration will notice that
1769 * messages are pending.
1771 check_write_watch (transport);
1773 _dbus_verbose (" ... leaving do_iteration()\n");
1777 * Copy-paste from socket transport.
1780 kdbus_live_messages_changed (DBusTransport *transport)
1782 /* See if we should look for incoming messages again */
1783 check_read_watch (transport);
1787 * Gets file descriptor of the kdbus bus.
1788 * @param transport transport
1789 * @param fd_p place to write fd to
1790 * @returns always TRUE
1793 kdbus_get_kdbus_fd (DBusTransport *transport,
1796 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1798 *fd_p = kdbus_transport->fd;
1803 static const DBusTransportVTable kdbus_vtable = {
1807 kdbus_connection_set,
1809 kdbus_live_messages_changed,
1814 * Copy-paste from dbus_transport_socket with needed changes.
1816 * Creates a new transport for the given kdbus file descriptor and address.
1817 * The file descriptor must be nonblocking.
1819 * @param fd the file descriptor.
1820 * @param address the transport's address
1821 * @returns the new transport, or #NULL if no memory.
1823 static DBusTransport*
1824 new_kdbus_transport (int fd, const DBusString *address)
1826 DBusTransportKdbus *kdbus_transport;
1828 kdbus_transport = dbus_new0 (DBusTransportKdbus, 1);
1829 if (kdbus_transport == NULL)
1832 kdbus_transport->write_watch = _dbus_watch_new (fd,
1833 DBUS_WATCH_WRITABLE,
1836 if (kdbus_transport->write_watch == NULL)
1839 kdbus_transport->read_watch = _dbus_watch_new (fd,
1840 DBUS_WATCH_READABLE,
1843 if (kdbus_transport->read_watch == NULL)
1846 if (!_dbus_transport_init_base (&kdbus_transport->base,
1851 kdbus_transport->fd = fd;
1853 /* These values should probably be tunable or something. */
1854 kdbus_transport->max_bytes_read_per_iteration = MAX_BYTES_PER_ITERATION;
1855 kdbus_transport->max_bytes_written_per_iteration = MAX_BYTES_PER_ITERATION;
1857 kdbus_transport->kdbus_mmap_ptr = NULL;
1858 kdbus_transport->memfd = -1;
1860 return (DBusTransport*) kdbus_transport;
1863 _dbus_watch_invalidate (kdbus_transport->read_watch);
1864 _dbus_watch_unref (kdbus_transport->read_watch);
1866 _dbus_watch_invalidate (kdbus_transport->write_watch);
1867 _dbus_watch_unref (kdbus_transport->write_watch);
1869 dbus_free (kdbus_transport);
1874 * Opens a connection to the kdbus bus
1876 * @param path the path to kdbus bus
1877 * @param error return location for error code
1878 * @returns connection file descriptor or -1 on error
1880 static int _dbus_connect_kdbus (const char *path, DBusError *error)
1884 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1885 _dbus_verbose ("connecting to kdbus bus %s\n", path);
1887 fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
1889 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
1895 * Connects to kdbus, creates and sets-up transport.
1897 * @param path the path to the bus.
1898 * @param error address where an error can be returned.
1899 * @returns a new transport, or #NULL on failure.
1901 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
1904 DBusTransport *transport;
1907 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1909 if (!_dbus_string_init (&address))
1911 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1917 if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
1919 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1923 fd = _dbus_connect_kdbus (path, error);
1926 _DBUS_ASSERT_ERROR_IS_SET (error);
1930 _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
1932 transport = new_kdbus_transport (fd, &address);
1933 if (transport == NULL)
1935 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1939 _dbus_string_free (&address);
1951 _dbus_string_free (&address);
1957 * Opens kdbus transport if method from address entry is kdbus
1959 * @param entry the address entry to open
1960 * @param transport_p return location for the opened transport
1961 * @param error place to store error
1962 * @returns result of the attempt as a DBusTransportOpenResult enum
1964 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry,
1965 DBusTransport **transport_p,
1970 method = dbus_address_entry_get_method (entry);
1971 _dbus_assert (method != NULL);
1973 if (strcmp (method, "kdbus") == 0)
1975 const char *path = dbus_address_entry_get_value (entry, "path");
1979 _dbus_set_bad_address (error, "kdbus", "path", NULL);
1980 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1983 *transport_p = _dbus_transport_new_for_kdbus (path, error);
1985 if (*transport_p == NULL)
1987 _DBUS_ASSERT_ERROR_IS_SET (error);
1988 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1992 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1993 return DBUS_TRANSPORT_OPEN_OK;
1998 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1999 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;