X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dbus%2Fdbus-transport-kdbus.c;h=72e3d5643c29a268a2a3fc53e5c5e87afff31e23;hb=bb8dd7fec5389db4df9b5e8863974149e8a650dc;hp=6c1d05a2bc2cd8ba49f8075b7f68e96be5e28617;hpb=a99107c804c94704ef13845a8224e2f455c7b37b;p=platform%2Fupstream%2Fdbus.git diff --git a/dbus/dbus-transport-kdbus.c b/dbus/dbus-transport-kdbus.c index 6c1d05a..72e3d56 100644 --- a/dbus/dbus-transport-kdbus.c +++ b/dbus/dbus-transport-kdbus.c @@ -46,15 +46,35 @@ #include #include -#define KDBUS_PART_FOREACH(part, head, first) \ - for (part = (head)->first; \ - (uint8_t *)(part) < (uint8_t *)(head) + (head)->size; \ - part = KDBUS_PART_NEXT(part)) +/** + * @defgroup DBusTransportKdbus DBusTransport implementations for kdbus + * @ingroup DBusInternals + * @brief Implementation details of DBusTransport on kdbus + * + * @{ + */ + +/** Size of the memory area for received non-memfd messages. */ #define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU) -#define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used +/** Over this memfd is used to send (if it is not broadcast). */ +#define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) + +/** Define max bytes read or written in one iteration. +* This is to avoid blocking on reading or writing for too long. It is checked after each message is sent or received, +* so if message is bigger than MAX_BYTES_PER_ITERATION it will be handled in one iteration, but sending/writing +* will break after that message. +**/ +#define MAX_BYTES_PER_ITERATION 16384 + +#if (MEMFD_SIZE_THRESHOLD > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE) + #error Memfd size threshold higher than max kdbus message payload vector size +#endif + +/** Enables verbosing more information about kdbus message. + * Works only if DBUS_VERBOSE=1 is used. + */ #define KDBUS_MSG_DECODE_DEBUG 0 -//#define DBUS_AUTHENTICATION #define ITER_APPEND_STR(string) \ if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) \ @@ -64,19 +84,23 @@ if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) \ }\ #define MSG_ITEM_BUILD_VEC(data, datasize) \ - item->type = KDBUS_MSG_PAYLOAD_VEC; \ - item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec); \ + item->type = KDBUS_ITEM_PAYLOAD_VEC; \ + item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec); \ item->vec.address = (unsigned long) data; \ item->vec.size = datasize; +#define KDBUS_PART_FOREACH(part, head, first) \ + for (part = (head)->first; \ + (uint8_t *)(part) < (uint8_t *)(head) + (head)->size; \ + part = KDBUS_PART_NEXT(part)) + /** - * Opaque object representing a transport. In kdbus transport it has nothing common - * with socket, but the name was preserved to comply with upper functions. + * Opaque object representing a transport. */ typedef struct DBusTransportKdbus DBusTransportKdbus; /** - * Implementation details of DBusTransportSocket. All members are private. + * Implementation details of DBusTransportKdbus. All members are private. */ struct DBusTransportKdbus { @@ -85,20 +109,10 @@ struct DBusTransportKdbus DBusWatch *read_watch; /**< Watch for readability. */ DBusWatch *write_watch; /**< Watch for writability. */ - int max_bytes_read_per_iteration; /**< In kdbus only to control overall message size*/ - int max_bytes_written_per_iteration; /**< In kdbus only to control overall message size*/ + int max_bytes_read_per_iteration; /**< To avoid blocking too long. */ + int max_bytes_written_per_iteration; /**< To avoid blocking too long. */ - int message_bytes_written; /**< Number of bytes of current - * outgoing message that have - * been written. - */ - DBusString encoded_outgoing; /**< Encoded version of current - * outgoing message. - */ - DBusString encoded_incoming; /**< Encoded version of current - * incoming data. - */ - void* kdbus_mmap_ptr; /**< Mapped memory where Kdbus (kernel) writes + void* kdbus_mmap_ptr; /**< Mapped memory where kdbus (kernel) writes * messages incoming to us. */ int memfd; /**< File descriptor to special @@ -106,18 +120,37 @@ struct DBusTransportKdbus * transfer. Retrieved from * Kdbus kernel module. */ - __u64 bloom_size; /**< bloom filter field size */ - char* sender; /**< uniqe name of the sender */ + __u64 bloom_size; /**< bloom filter field size */ + char* sender; /**< unique name of the sender */ }; +/** + * Gets size in bytes of bloom filter field. + * This size is got from the bus during connection procedure. + * @param transport transport + * @returns size of bloom + */ __u64 dbus_transport_get_bloom_size(DBusTransport* transport) { return ((DBusTransportKdbus*)transport)->bloom_size; } -/* - * Adds locally generated message to received messages queue - * +/** + * Gets pointer to the memory pool, wher received messages are + * placed and some ioctls return their info + * @param transport transport + * @returns pointer to the pool + */ +void* dbus_transport_get_pool_pointer(DBusTransport* transport) +{ + return ((DBusTransportKdbus*)transport)->kdbus_mmap_ptr; +} + +/** + * Puts locally generated message into received messages queue + * @param message message that will be added + * @param connection connection to which message will be added + * @returns TRUE on success, FALSE on memory allocation error */ static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* connection) { @@ -135,9 +168,18 @@ static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* return TRUE; } -/* +/** * Generates local error message as a reply to message given as parameter * and adds generated error message to received messages queue. + * @param error_type type of error, preferably DBUS_ERROR_(...) + * @param template Template of error description. It can has formatting + * characters to print object string into it. Can be NULL. + * @param object String to print into error description. Can be NULL. + * If object is not NULL while template is NULL, the object string + * will be the only error description. + * @param message Message for which the error reply is generated. + * @param connection The connection. + * @returns 0 on success, otherwise -1 */ static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection) { @@ -161,9 +203,14 @@ static int reply_with_error(char* error_type, const char* template, const char* return -1; } -/* +/** * Generates reply to the message given as a parameter with one item in the reply body * and adds generated reply message to received messages queue. + * @param message The message we are replying to. + * @param data_type Type of data sent in the reply.Use DBUS_TYPE_(...) + * @param pData Address of data sent in the reply. + * @param connection The connection + * @returns 0 on success, otherwise -1 */ static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection) { @@ -200,35 +247,35 @@ static int reply_ack(DBusMessage *message, DBusConnection* connection) }*/ /** - * Retrieves file descriptor to memory pool from kdbus module. - * It is then used for bulk data sending. + * Retrieves file descriptor to memory pool from kdbus module and stores + * it in kdbus_transport->memfd. It is then used to send large message. * Triggered when message payload is over MEMFD_SIZE_THRESHOLD - * + * @param kdbus_transport DBusTransportKdbus transport structure + * @returns 0 on success, otherwise -1 */ -static int kdbus_init_memfd(DBusTransportKdbus* socket_transport) +static int kdbus_init_memfd(DBusTransportKdbus* kdbus_transport) { int memfd; - if (ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) { + if (ioctl(kdbus_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) { _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n"); return -1; } - socket_transport->memfd = memfd; - _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd); + kdbus_transport->memfd = memfd; + _dbus_verbose("kdbus_init_memfd: %d!!\n", kdbus_transport->memfd); return 0; } /** - * Initiates Kdbus message structure. - * Calculates it's size, allocates memory and fills some fields. - * @param name Well-known name or NULL - * @param dst_id Numeric id of recipient - * @param body_size size of message body if present - * @param use_memfd flag to build memfd message - * @param fds_count number of file descriptors used + * Allocates and initializes kdbus message structure. + * @param name Well-known name or NULL. If NULL, dst_id must be supplied. + * @param dst_id Numeric id of recipient. Ignored if name is not NULL. + * @param body_size Size of message body (May be 0). + * @param use_memfd Flag to build memfd message. + * @param fds_count Number of file descriptors sent in the message. * @param transport transport - * @return initialized kdbus message + * @returns initialized kdbus message or NULL if malloc failed */ 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) { @@ -237,13 +284,19 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t msg_size = sizeof(struct kdbus_msg); - if(use_memfd == TRUE) // bulk data - memfd - encoded and plain + if(use_memfd == TRUE) // bulk data - memfd msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd)); - else { - msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec)); - if(body_size) - msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec)); - } + else + { + msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec)); //header is a must + while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE) + { + msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec)); + body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE; + } + if(body_size) + msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec)); + } if(fds_count) msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count); @@ -251,7 +304,7 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t if (name) msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1); else if (dst_id == KDBUS_DST_ID_BROADCAST) - msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size; + msg_size += KDBUS_ITEM_HEADER_SIZE + transport->bloom_size; msg = malloc(msg_size); if (!msg) @@ -262,7 +315,7 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t memset(msg, 0, msg_size); msg->size = msg_size; - msg->payload_type = KDBUS_PAYLOAD_DBUS1; + msg->payload_type = KDBUS_PAYLOAD_DBUS; msg->dst_id = name ? 0 : dst_id; msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10); @@ -270,196 +323,202 @@ static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t } /** - * Builds and sends kdbus message using DBus message. - * Decide whether used payload vector or memfd memory pool. + * Sends DBus message using kdbus. * Handles broadcasts and unicast messages, and passing of Unix fds. - * Does error handling. - * TODO refactor to be more compact + * Also can locally generate error replies on some error returned by kernel. * - * @param transport transport + * TODO refactor to be more compact - maybe we can send header always as a payload vector + * and only message body as memfd if needed. + * + * @param transport Transport. * @param message DBus message to be sent - * @param encoded flag if the message is encoded - * @return size of data sent + * @param destination Destination of the message. + * @returns bytes sent or -1 if sending failed */ -static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination, dbus_bool_t encoded) +static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination) { - struct kdbus_msg *msg; - struct kdbus_item *item; - uint64_t dst_id = KDBUS_DST_ID_BROADCAST; - const DBusString *header; - const DBusString *body; - uint64_t ret_size = 0; - uint64_t body_size = 0; - uint64_t header_size = 0; - dbus_bool_t use_memfd; - const int *unix_fds; - unsigned fds_count; - dbus_bool_t autostart; - - // determine destination and destination id - if(destination) + struct kdbus_msg *msg; + struct kdbus_item *item; + uint64_t dst_id = KDBUS_DST_ID_BROADCAST; + const DBusString *header; + const DBusString *body; + uint64_t ret_size = 0; + uint64_t body_size = 0; + uint64_t header_size = 0; + dbus_bool_t use_memfd = FALSE; + const int *unix_fds; + unsigned fds_count; + dbus_bool_t autostart; + + // determine destination and destination id + if(destination) { - dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME; - if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */ - { - dst_id = strtoull(&destination[3], NULL, 10); - destination = NULL; - } + dst_id = KDBUS_DST_ID_NAME; + if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */ + { + dst_id = strtoull(&destination[3], NULL, 10); + destination = NULL; + } } - - // get size of data - if(encoded) - ret_size = _dbus_string_get_length (&transport->encoded_outgoing); - else + + _dbus_message_get_network_data (message, &header, &body); + header_size = _dbus_string_get_length(header); + body_size = _dbus_string_get_length(body); + ret_size = header_size + body_size; + + // check whether we can and should use memfd + if((dst_id != KDBUS_DST_ID_BROADCAST) && (ret_size > MEMFD_SIZE_THRESHOLD)) { - _dbus_message_get_network_data (message, &header, &body); - header_size = _dbus_string_get_length(header); - body_size = _dbus_string_get_length(body); - ret_size = header_size + body_size; + use_memfd = TRUE; + kdbus_init_memfd(transport); } - // check if message size is big enough to use memfd kdbus transport - use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE; - if(use_memfd) kdbus_init_memfd(transport); - - _dbus_message_get_unix_fds(message, &unix_fds, &fds_count); - - // init basic message fields - msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport); - msg->cookie = dbus_message_get_serial(message); - autostart = dbus_message_get_auto_start (message); - if(!autostart) - msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START; - - // build message contents - item = msg->items; - - // case 1 - bulk data transfer - memfd - encoded and plain - if(use_memfd) - { - char *buf; + _dbus_message_get_unix_fds(message, &unix_fds, &fds_count); - if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0) - { - _dbus_verbose("memfd sealing failed: \n"); - goto out; - } + // init basic message fields + msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport); + msg->cookie = dbus_message_get_serial(message); + autostart = dbus_message_get_auto_start (message); + if(!autostart) + msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START; - buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0); - if (buf == MAP_FAILED) - { - _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd); - goto out; - } + // build message contents + item = msg->items; - if(encoded) - memcpy(buf, &transport->encoded_outgoing, ret_size); - else - { - memcpy(buf, _dbus_string_get_const_data(header), header_size); - if(body_size) { - buf+=header_size; - memcpy(buf, _dbus_string_get_const_data(body), body_size); - buf-=header_size; - } - } + if(use_memfd) + { + char *buf; - munmap(buf, ret_size); + if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0) + { + _dbus_verbose("memfd sealing failed: \n"); + goto out; + } - // seal data - kdbus module needs it - if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) { - _dbus_verbose("memfd sealing failed: %d (%m)\n", errno); - ret_size = -1; - goto out; - } + buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0); + if (buf == MAP_FAILED) + { + _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd); + goto out; + } - item->type = KDBUS_MSG_PAYLOAD_MEMFD; - item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd); - item->memfd.size = ret_size; - item->memfd.fd = transport->memfd; - // case 2 - small encoded - don't use memfd - } else if(encoded) { - _dbus_verbose("sending encoded data\n"); - MSG_ITEM_BUILD_VEC(&transport->encoded_outgoing, _dbus_string_get_length (&transport->encoded_outgoing)); + memcpy(buf, _dbus_string_get_const_data(header), header_size); + if(body_size) { + buf+=header_size; + memcpy(buf, _dbus_string_get_const_data(body), body_size); + buf-=header_size; + } - // case 3 - small not encoded - don't use memfd - } else { - _dbus_verbose("sending normal vector data\n"); - MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size); + munmap(buf, ret_size); - if(body_size) + // seal data - kdbus module needs it + if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) { + _dbus_verbose("memfd sealing failed: %d (%m)\n", errno); + ret_size = -1; + goto out; + } + + item->type = KDBUS_ITEM_PAYLOAD_MEMFD; + item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_memfd); + item->memfd.size = ret_size; + item->memfd.fd = transport->memfd; + } + else + { + _dbus_verbose("sending normal vector data\n"); + MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size); + + if(body_size) { - _dbus_verbose("body attaching\n"); - item = KDBUS_PART_NEXT(item); - MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(body), body_size); + const char* body_data; + + body_data = _dbus_string_get_const_data(body); + while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE) + { + _dbus_verbose("body attaching\n"); + item = KDBUS_PART_NEXT(item); + MSG_ITEM_BUILD_VEC(body_data, KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE); + body_data += KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE; + body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE; + } + if(body_size) + { + _dbus_verbose("body attaching\n"); + item = KDBUS_PART_NEXT(item); + MSG_ITEM_BUILD_VEC(body_data, body_size); + } } } - if(fds_count) + if(fds_count) { - item = KDBUS_PART_NEXT(item); - item->type = KDBUS_MSG_FDS; - item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count); - memcpy(item->fds, unix_fds, sizeof(int) * fds_count); + item = KDBUS_PART_NEXT(item); + item->type = KDBUS_ITEM_FDS; + item->size = KDBUS_ITEM_HEADER_SIZE + (sizeof(int) * fds_count); + memcpy(item->fds, unix_fds, sizeof(int) * fds_count); } - if (destination) - { - item = KDBUS_PART_NEXT(item); - item->type = KDBUS_MSG_DST_NAME; - item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1; - memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE); - } - else if (dst_id == KDBUS_DST_ID_BROADCAST) - { - item = KDBUS_PART_NEXT(item); - item->type = KDBUS_MSG_BLOOM; - item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size; - strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size); - } + if (destination) + { + item = KDBUS_PART_NEXT(item); + item->type = KDBUS_ITEM_DST_NAME; + item->size = KDBUS_ITEM_HEADER_SIZE + strlen(destination) + 1; + memcpy(item->str, destination, item->size - KDBUS_ITEM_HEADER_SIZE); + } + else if (dst_id == KDBUS_DST_ID_BROADCAST) + { + item = KDBUS_PART_NEXT(item); + item->type = KDBUS_ITEM_BLOOM; + item->size = KDBUS_ITEM_HEADER_SIZE + transport->bloom_size; + strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size); + } - again: - if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg)) - { - if(errno == EINTR) - goto again; - else if(errno == ENXIO) //no such id on the bus - { - if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection)) + again: + if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg)) + { + if(errno == EINTR) + goto again; + else if(errno == ENXIO) //no such id on the bus + { + if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection)) + goto out; + } + else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus + { + if(autostart) + { + 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)) goto out; - } - else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus - { - if(autostart) - { - 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)) - goto out; - } - else - if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection)) - goto out; - } - _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno); - ret_size = -1; - } -out: - free(msg); - if(use_memfd) - close(transport->memfd); + } + else + if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection)) + goto out; + } + _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno); + ret_size = -1; + } + out: + free(msg); + if(use_memfd) + close(transport->memfd); - return ret_size; + return ret_size; } /** * Performs kdbus hello - registration on the kdbus bus + * needed to send and receive messages on the bus, + * and configures transport. + * As a result unique id on he bus is obtained. * - * @param name place to store unique name given by bus + * @param name place to print id given by bus * @param transportS transport structure * @returns #TRUE on success */ static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS) { struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello; + memset(&hello, 0, sizeof(hello)); hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* | KDBUS_HELLO_ATTACH_COMM | @@ -493,39 +552,50 @@ static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS } /** - * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate - * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue. + * Looks over messages sent to org.freedesktop.DBus. Hello message, which performs + * registration on the bus, is captured as it must be locally converted into + * appropriate ioctl. All the rest org.freedesktop.DBus methods are left untouched + * and they are sent to dbus-daemon in the same way as every other messages. * - * Now it captures only Hello message, which must be handled locally. - * All the rest is passed to dbus-daemon. + * @param transport Transport + * @param message Message being sent. + * @returns 1 if it is not Hello message and it should be passed to daemon + * 0 if Hello message was handled correctly, + * -1 if Hello message was not handle correctly. */ -static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message) +static int capture_hello_message(DBusTransport *transport, const char* destination, DBusMessage *message) { - if(!strcmp(dbus_message_get_member(message), "Hello")) + if(!strcmp(destination, DBUS_SERVICE_DBUS)) { - char* name = NULL; - - name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1.")); - if(name == NULL) - return -1; - strcpy(name, ":1."); - if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport)) - goto out; - if(!register_kdbus_policy(&name[3], transport, geteuid())) - goto out; + if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS)) + { + if(!strcmp(dbus_message_get_member(message), "Hello")) + { + char* name = NULL; - ((DBusTransportKdbus*)transport)->sender = name; + name = malloc(snprintf(name, 0, ":1.%llu0", ULLONG_MAX)); + if(name == NULL) + return -1; + strcpy(name, ":1."); + if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport)) + goto out; +#ifdef POLICY_TO_KDBUS + if(!register_kdbus_policy(&name[3], transport, geteuid())) + goto out; +#endif + ((DBusTransportKdbus*)transport)->sender = name; - if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection)) - return 0; + if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection)) + return 0; //on success we can not free name - out: - free(name); + out: + free(name); + return -1; + } + } } - else - return 1; //send to daemon - return -1; + return 1; //send message to daemon } #if KDBUS_MSG_DECODE_DEBUG == 1 @@ -558,48 +628,47 @@ struct kdbus_enum_table { } const char *enum_MSG(long long id); TABLE(MSG) = { - ENUM(_KDBUS_MSG_NULL), - ENUM(KDBUS_MSG_PAYLOAD_VEC), - ENUM(KDBUS_MSG_PAYLOAD_OFF), - ENUM(KDBUS_MSG_PAYLOAD_MEMFD), - ENUM(KDBUS_MSG_FDS), - ENUM(KDBUS_MSG_BLOOM), - ENUM(KDBUS_MSG_DST_NAME), - ENUM(KDBUS_MSG_SRC_CREDS), - ENUM(KDBUS_MSG_SRC_PID_COMM), - ENUM(KDBUS_MSG_SRC_TID_COMM), - ENUM(KDBUS_MSG_SRC_EXE), - ENUM(KDBUS_MSG_SRC_CMDLINE), - ENUM(KDBUS_MSG_SRC_CGROUP), - ENUM(KDBUS_MSG_SRC_CAPS), - ENUM(KDBUS_MSG_SRC_SECLABEL), - ENUM(KDBUS_MSG_SRC_AUDIT), - ENUM(KDBUS_MSG_SRC_NAMES), - ENUM(KDBUS_MSG_TIMESTAMP), - ENUM(KDBUS_MSG_NAME_ADD), - ENUM(KDBUS_MSG_NAME_REMOVE), - ENUM(KDBUS_MSG_NAME_CHANGE), - ENUM(KDBUS_MSG_ID_ADD), - ENUM(KDBUS_MSG_ID_REMOVE), - ENUM(KDBUS_MSG_REPLY_TIMEOUT), - ENUM(KDBUS_MSG_REPLY_DEAD), + ENUM(_KDBUS_ITEM_NULL), + ENUM(KDBUS_ITEM_PAYLOAD_VEC), + ENUM(KDBUS_ITEM_PAYLOAD_OFF), + ENUM(KDBUS_ITEM_PAYLOAD_MEMFD), + ENUM(KDBUS_ITEM_FDS), + ENUM(KDBUS_ITEM_BLOOM), + ENUM(KDBUS_ITEM_DST_NAME), + ENUM(KDBUS_ITEM_CREDS), + ENUM(KDBUS_ITEM_PID_COMM), + ENUM(KDBUS_ITEM_TID_COMM), + ENUM(KDBUS_ITEM_EXE), + ENUM(KDBUS_ITEM_CMDLINE), + ENUM(KDBUS_ITEM_CGROUP), + ENUM(KDBUS_ITEM_CAPS), + ENUM(KDBUS_ITEM_SECLABEL), + ENUM(KDBUS_ITEM_AUDIT), + ENUM(KDBUS_ITEM_NAME), + ENUM(KDBUS_ITEM_TIMESTAMP), + ENUM(KDBUS_ITEM_NAME_ADD), + ENUM(KDBUS_ITEM_NAME_REMOVE), + ENUM(KDBUS_ITEM_NAME_CHANGE), + ENUM(KDBUS_ITEM_ID_ADD), + ENUM(KDBUS_ITEM_ID_REMOVE), + ENUM(KDBUS_ITEM_REPLY_TIMEOUT), + ENUM(KDBUS_ITEM_REPLY_DEAD), }; LOOKUP(MSG); const char *enum_PAYLOAD(long long id); TABLE(PAYLOAD) = { ENUM(KDBUS_PAYLOAD_KERNEL), - ENUM(KDBUS_PAYLOAD_DBUS1), - ENUM(KDBUS_PAYLOAD_GVARIANT), + ENUM(KDBUS_PAYLOAD_DBUS), }; LOOKUP(PAYLOAD); /** - * Puts locally generated message into received data buffer. - * Use only during receiving phase! + * Finalizes locally generated DBus message + * and puts it into data buffer. * - * @param message message to load - * @param data place to load message - * @return size of message + * @param message Message to load. + * @param data Place to load message. + * @returns Size of message loaded. */ static int put_message_into_data(DBusMessage *message, char* data) { @@ -622,10 +691,10 @@ static int put_message_into_data(DBusMessage *message, char* data) } /** - * Get the length of the kdbus message content. + * Calculates length of the kdbus message content (payload). * * @param msg kdbus message - * @return the length of the kdbus message + * @return the length of the kdbus message's payload. */ static int kdbus_message_size(const struct kdbus_msg* msg) { @@ -634,17 +703,17 @@ static int kdbus_message_size(const struct kdbus_msg* msg) KDBUS_PART_FOREACH(item, msg, items) { - if (item->size <= KDBUS_PART_HEADER_SIZE) + if (item->size < KDBUS_ITEM_HEADER_SIZE) { _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size); return -1; } switch (item->type) { - case KDBUS_MSG_PAYLOAD_OFF: + case KDBUS_ITEM_PAYLOAD_OFF: ret_size += item->vec.size; break; - case KDBUS_MSG_PAYLOAD_MEMFD: + case KDBUS_ITEM_PAYLOAD_MEMFD: ret_size += item->memfd.size; break; default: @@ -656,16 +725,16 @@ static int kdbus_message_size(const struct kdbus_msg* msg) } /** - * Decodes kdbus message in order to extract dbus message and put it into data and fds. - * Also captures and decodes kdbus error messages and kdbus kernel broadcasts and converts - * all of them into dbus messages. + * Decodes kdbus message in order to extract DBus message and puts it into received data buffer + * and file descriptor's buffer. Also captures kdbus error messages and kdbus kernel broadcasts + * and converts all of them into appropriate DBus messages. * * @param msg kdbus message - * @param data place to copy dbus message to + * @param data place to copy DBus message to * @param kdbus_transport transport * @param fds place to store file descriptors received - * @param n_fds place to store quantity of file descriptor - * @return number of dbus message's bytes received or -1 on error + * @param n_fds place to store quantity of file descriptors received + * @return number of DBus message's bytes received or -1 on error */ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportKdbus* kdbus_transport, int* fds, int* n_fds) { @@ -674,8 +743,8 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo DBusMessage *message = NULL; DBusMessageIter args; const char* emptyString = ""; - const char* pString = NULL; - char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + sizeof(":1."))]; + const char* pString = NULL; + char dbus_name[(unsigned int)(snprintf((char*)pString, 0, ":1.%llu0", ULLONG_MAX))]; const char* pDBusName = dbus_name; #if KDBUS_MSG_DECODE_DEBUG == 1 char buf[32]; @@ -693,7 +762,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo KDBUS_PART_FOREACH(item, msg, items) { - if (item->size <= KDBUS_PART_HEADER_SIZE) + if (item->size < KDBUS_ITEM_HEADER_SIZE) { _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size); break; //??? continue (because dbus will find error) or break @@ -701,7 +770,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo switch (item->type) { - case KDBUS_MSG_PAYLOAD_OFF: + case KDBUS_ITEM_PAYLOAD_OFF: memcpy(data, (char *)kdbus_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size); data += item->vec.size; ret_size += item->vec.size; @@ -712,7 +781,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo (unsigned long long)item->vec.size); break; - case KDBUS_MSG_PAYLOAD_MEMFD: + case KDBUS_ITEM_PAYLOAD_MEMFD: { char *buf; uint64_t size; @@ -740,11 +809,11 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo break; } - case KDBUS_MSG_FDS: + case KDBUS_ITEM_FDS: { int i; - *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int); + *n_fds = (item->size - KDBUS_ITEM_HEADER_SIZE) / sizeof(int); memcpy(fds, item->fds, *n_fds * sizeof(int)); for (i = 0; i < *n_fds; i++) _dbus_fd_set_close_on_exec(fds[i]); @@ -752,7 +821,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo } #if KDBUS_MSG_DECODE_DEBUG == 1 - case KDBUS_MSG_SRC_CREDS: + case KDBUS_ITEM_CREDS: _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n", enum_MSG(item->type), item->size, item->creds.uid, item->creds.gid, @@ -760,19 +829,19 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo item->creds.starttime); break; - case KDBUS_MSG_SRC_PID_COMM: - case KDBUS_MSG_SRC_TID_COMM: - case KDBUS_MSG_SRC_EXE: - case KDBUS_MSG_SRC_CGROUP: - case KDBUS_MSG_SRC_SECLABEL: - case KDBUS_MSG_DST_NAME: + case KDBUS_ITEM_PID_COMM: + case KDBUS_ITEM_TID_COMM: + case KDBUS_ITEM_EXE: + case KDBUS_ITEM_CGROUP: + case KDBUS_ITEM_SECLABEL: + case KDBUS_ITEM_DST_NAME: _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n", enum_MSG(item->type), item->size, item->str, strlen(item->str)); break; - case KDBUS_MSG_SRC_CMDLINE: - case KDBUS_MSG_SRC_NAMES: { - __u64 size = item->size - KDBUS_PART_HEADER_SIZE; + case KDBUS_ITEM_CMDLINE: + case KDBUS_ITEM_NAME: { + __u64 size = item->size - KDBUS_ITEM_HEADER_SIZE; const char *str = item->str; int count = 0; @@ -788,24 +857,24 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo break; } - case KDBUS_MSG_SRC_AUDIT: + case KDBUS_ITEM_AUDIT: _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n", enum_MSG(item->type), item->size, (unsigned long long)item->data64[0], (unsigned long long)item->data64[1]); break; - case KDBUS_MSG_SRC_CAPS: { + case KDBUS_ITEM_CAPS: { int n; const uint32_t *cap; int i; _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n", enum_MSG(item->type), item->size, - (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE); + (unsigned long long)item->size - KDBUS_ITEM_HEADER_SIZE); cap = item->data32; - n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t); + n = (item->size - KDBUS_ITEM_HEADER_SIZE) / 4 / sizeof(uint32_t); _dbus_verbose(" CapInh="); for (i = 0; i < n; i++) @@ -826,7 +895,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo break; } - case KDBUS_MSG_TIMESTAMP: + case KDBUS_ITEM_TIMESTAMP: _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n", enum_MSG(item->type), item->size, (unsigned long long)item->timestamp.realtime_ns, @@ -834,7 +903,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo break; #endif - case KDBUS_MSG_REPLY_TIMEOUT: + case KDBUS_ITEM_REPLY_TIMEOUT: _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n", enum_MSG(item->type), item->size, msg->cookie_reply); @@ -848,7 +917,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo ret_size = put_message_into_data(message, data); break; - case KDBUS_MSG_REPLY_DEAD: + case KDBUS_ITEM_REPLY_DEAD: _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n", enum_MSG(item->type), item->size, msg->cookie_reply); @@ -862,11 +931,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo ret_size = put_message_into_data(message, data); break; - case KDBUS_MSG_NAME_ADD: - _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n", + case KDBUS_ITEM_NAME_ADD: + _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, old flags=0x%llx, new flags=0x%llx\n", enum_MSG(item->type), (unsigned long long) item->size, item->name_change.name, item->name_change.old_id, - item->name_change.new_id, item->name_change.flags); + item->name_change.new_id, item->name_change.old_flags, + item->name_change.new_flags); message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); if(message == NULL) @@ -887,11 +957,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo ret_size = put_message_into_data(message, data); break; - case KDBUS_MSG_NAME_REMOVE: - _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n", + case KDBUS_ITEM_NAME_REMOVE: + _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, old flags=0x%llx, new flags=0x%llx\n", enum_MSG(item->type), (unsigned long long) item->size, item->name_change.name, item->name_change.old_id, - item->name_change.new_id, item->name_change.flags); + item->name_change.new_id, item->name_change.old_flags, + item->name_change.new_flags); message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal if(message == NULL) @@ -912,11 +983,12 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo ret_size = put_message_into_data(message, data); break; - case KDBUS_MSG_NAME_CHANGE: - _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n", + case KDBUS_ITEM_NAME_CHANGE: + _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, old flags=0x%llx, new flags=0x%llx\n", enum_MSG(item->type), (unsigned long long) item->size, item->name_change.name, item->name_change.old_id, - item->name_change.new_id, item->name_change.flags); + item->name_change.new_id, item->name_change.old_flags, + item->name_change.new_flags); message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); if(message == NULL) @@ -939,7 +1011,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo ret_size = put_message_into_data(message, data); break; - case KDBUS_MSG_ID_ADD: + case KDBUS_ITEM_ID_ADD: _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n", enum_MSG(item->type), (unsigned long long) item->size, (unsigned long long) item->id_change.id, @@ -962,7 +1034,7 @@ static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTranspo ret_size = put_message_into_data(message, data); break; - case KDBUS_MSG_ID_REMOVE: + case KDBUS_ITEM_ID_REMOVE: _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n", enum_MSG(item->type), (unsigned long long) item->size, (unsigned long long) item->id_change.id, @@ -1005,15 +1077,15 @@ out: } /** - * Reads message from kdbus and puts it into dbus buffer and fds + * Reads message from kdbus and puts it into DBus buffers * - * @param transport transport + * @param kdbus_transport transport * @param buffer place to copy received message to - * @param fds place to store file descriptors sent in the message - * @param n_fds place to store number of file descriptors + * @param fds place to store file descriptors received with the message + * @param n_fds place to store quantity of file descriptors received * @return size of received message on success, -1 on error */ -static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString *buffer, int* fds, int* n_fds) +static int kdbus_read_message(DBusTransportKdbus *kdbus_transport, DBusString *buffer, int* fds, int* n_fds) { int ret_size, buf_size; uint64_t __attribute__ ((__aligned__(8))) offset; @@ -1024,7 +1096,7 @@ static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString * start = _dbus_string_get_length (buffer); again: - if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0) + if (ioctl(kdbus_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0) { if(errno == EINTR) goto again; @@ -1033,7 +1105,7 @@ static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString * return -1; } - msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset); + msg = (struct kdbus_msg *)((char*)kdbus_transport->kdbus_mmap_ptr + offset); buf_size = kdbus_message_size(msg); if (buf_size == -1) @@ -1053,7 +1125,7 @@ static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString * } data = _dbus_string_get_data_len (buffer, start, buf_size); - ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds); + ret_size = kdbus_decode_msg(msg, data, kdbus_transport, fds, n_fds); if(ret_size == -1) /* error */ { @@ -1066,7 +1138,7 @@ static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString * } again2: - if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0) + if (ioctl(kdbus_transport->fd, KDBUS_CMD_FREE, &offset) < 0) { if(errno == EINTR) goto again2; @@ -1076,8 +1148,9 @@ static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString * return ret_size; } -/* - * copy-paste from socket transport with needed renames only. + +/** + * Copy-paste from socket transport. Only renames done. */ static void free_watches (DBusTransport *transport) @@ -1109,31 +1182,28 @@ free_watches (DBusTransport *transport) _dbus_verbose ("end\n"); } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport. Only done needed renames and removed + * lines related to encoded messages. */ static void transport_finalize (DBusTransport *transport) { - DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport; - _dbus_verbose ("\n"); free_watches (transport); - _dbus_string_free (&kdbus_transport->encoded_outgoing); - _dbus_string_free (&kdbus_transport->encoded_incoming); - _dbus_transport_finalize_base (transport); - _dbus_assert (kdbus_transport->read_watch == NULL); - _dbus_assert (kdbus_transport->write_watch == NULL); + _dbus_assert (((DBusTransportKdbus*) transport)->read_watch == NULL); + _dbus_assert (((DBusTransportKdbus*) transport)->write_watch == NULL); dbus_free (transport); } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport. Removed code related to authentication, + * socket_transport replaced by kdbus_transport. */ static void check_write_watch (DBusTransport *transport) @@ -1152,33 +1222,8 @@ check_write_watch (DBusTransport *transport) _dbus_transport_ref (transport); -#ifdef DBUS_AUTHENTICATION - if (_dbus_transport_try_to_authenticate (transport)) -#endif - needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection); -#ifdef DBUS_AUTHENTICATION - else - { - if (transport->send_credentials_pending) - needed = TRUE; - else - { - DBusAuthState auth_state; - - auth_state = _dbus_auth_do_work (transport->auth); + needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection); - /* If we need memory we install the write watch just in case, - * if there's no need for it, it will get de-installed - * next time we try reading. - */ - if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND || - auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY) - needed = TRUE; - else - needed = FALSE; - } - } -#endif _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n", needed, transport->connection, kdbus_transport->write_watch, kdbus_transport->fd, @@ -1191,8 +1236,9 @@ check_write_watch (DBusTransport *transport) _dbus_transport_unref (transport); } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport. Removed code related to authentication, + * socket_transport replaced by kdbus_transport. */ static void check_read_watch (DBusTransport *transport) @@ -1213,42 +1259,9 @@ check_read_watch (DBusTransport *transport) _dbus_transport_ref (transport); -#ifdef DBUS_AUTHENTICATION - if (_dbus_transport_try_to_authenticate (transport)) -#endif - need_read_watch = + need_read_watch = (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) && (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds); -#ifdef DBUS_AUTHENTICATION - else - { - if (transport->receive_credentials_pending) - need_read_watch = TRUE; - else - { - /* The reason to disable need_read_watch when not WAITING_FOR_INPUT - * is to avoid spinning on the file descriptor when we're waiting - * to write or for some other part of the auth process - */ - DBusAuthState auth_state; - - auth_state = _dbus_auth_do_work (transport->auth); - - /* If we need memory we install the read watch just in case, - * if there's no need for it, it will get de-installed - * next time we try reading. If we're authenticated we - * install it since we normally have it installed while - * authenticated. - */ - if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT || - auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY || - auth_state == DBUS_AUTH_STATE_AUTHENTICATED) - need_read_watch = TRUE; - else - need_read_watch = FALSE; - } - } -#endif _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch); _dbus_connection_toggle_watch_unlocked (transport->connection, @@ -1258,8 +1271,8 @@ check_read_watch (DBusTransport *transport) _dbus_transport_unref (transport); } -/* - * copy-paste from socket transport. +/** + * Copy-paste from socket transport. */ static void do_io_error (DBusTransport *transport) @@ -1269,436 +1282,119 @@ do_io_error (DBusTransport *transport) _dbus_transport_unref (transport); } -#ifdef DBUS_AUTHENTICATION -/* return value is whether we successfully read any new data. */ -static dbus_bool_t -read_data_into_auth (DBusTransport *transport, - dbus_bool_t *oom) -{ - DBusTransportKdbus *socket_transport = (DBusTransportKdbus*) transport; - DBusString *buffer; - int bytes_read; - int *fds, n_fds; - - *oom = FALSE; - - _dbus_auth_get_buffer (transport->auth, &buffer); - - bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds); - - _dbus_auth_return_buffer (transport->auth, buffer, - bytes_read > 0 ? bytes_read : 0); - - if (bytes_read > 0) - { - _dbus_verbose (" read %d bytes in auth phase\n", bytes_read); - return TRUE; - } - else if (bytes_read < 0) - { - /* EINTR already handled for us */ - - if (_dbus_get_is_errno_enomem ()) - { - *oom = TRUE; - } - else if (_dbus_get_is_errno_eagain_or_ewouldblock ()) - ; /* do nothing, just return FALSE below */ - else - { - _dbus_verbose ("Error reading from remote app: %s\n", - _dbus_strerror_from_errno ()); - do_io_error (transport); - } - - return FALSE; - } - else - { - _dbus_assert (bytes_read == 0); - - _dbus_verbose ("Disconnected from remote app\n"); - do_io_error (transport); - - return FALSE; - } -} - -static int kdbus_send_auth (DBusTransport *transport, const DBusString *buffer) -{ - int len; - int bytes_written = -1; - struct kdbus_msg *msg; - struct kdbus_item *item; - - len = _dbus_string_get_length (buffer); -// data = _dbus_string_get_const_data_len (buffer, 0, len); - - msg = kdbus_init_msg(NULL, 1, 0, FALSE, 0, (DBusTransportKdbus*)transport); - item = msg->items; - MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data_len (buffer, 0, len), len); - - again: - if(ioctl(((DBusTransportKdbus*)transport)->fd, KDBUS_CMD_MSG_SEND, msg)) - { - if(errno == EINTR) - goto again; - _dbus_verbose ("Error writing auth: %d, %m\n", errno); - } - else - bytes_written = len; - - return bytes_written; -} - -/* Return value is whether we successfully wrote any bytes */ -static dbus_bool_t -write_data_from_auth (DBusTransport *transport) -{ -// DBusTransportKdbus *socket_transport = (DBusTransportSocket*) transport; - int bytes_written; - const DBusString *buffer; - - if (!_dbus_auth_get_bytes_to_send (transport->auth, - &buffer)) - return FALSE; - - bytes_written = kdbus_send_auth (transport, buffer); - - if (bytes_written > 0) - { - _dbus_auth_bytes_sent (transport->auth, bytes_written); - return TRUE; - } - else if (bytes_written < 0) - { - /* EINTR already handled for us */ - - if (_dbus_get_is_errno_eagain_or_ewouldblock ()) - ; - else - { - _dbus_verbose ("Error writing to remote app: %s\n", - _dbus_strerror_from_errno ()); - do_io_error (transport); - } - } - - return FALSE; -} - -/* FALSE on OOM */ -static dbus_bool_t -exchange_credentials (DBusTransport *transport, - dbus_bool_t do_reading, - dbus_bool_t do_writing) -{ - DBusTransportKdbus *socket_transport = (DBusTransportKdbus*) transport; - DBusError error = DBUS_ERROR_INIT; - - _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n", - do_reading, do_writing); - - if (do_writing && transport->send_credentials_pending) - { - if (_dbus_send_credentials_socket (socket_transport->fd, - &error)) - { - transport->send_credentials_pending = FALSE; - } - else - { - _dbus_verbose ("Failed to write credentials: %s\n", error.message); - dbus_error_free (&error); - do_io_error (transport); - } - } - - if (do_reading && transport->receive_credentials_pending) - { - /* FIXME this can fail due to IO error _or_ OOM, broken - * (somewhat tricky to fix since the OOM error can be set after - * we already read the credentials byte, so basically we need to - * separate reading the byte and storing it in the - * transport->credentials). Does not really matter for now - * because storing in credentials never actually fails on unix. - */ - if (_dbus_read_credentials_socket (socket_transport->fd, - transport->credentials, - &error)) - { - transport->receive_credentials_pending = FALSE; - } - else - { - _dbus_verbose ("Failed to read credentials %s\n", error.message); - dbus_error_free (&error); - do_io_error (transport); - } - } - - if (!(transport->send_credentials_pending || - transport->receive_credentials_pending)) - { - if (!_dbus_auth_set_credentials (transport->auth, - transport->credentials)) - return FALSE; - } - - return TRUE; -} - +/** + * Based on do_writing from socket transport. + * Removed authentication code and code related to encoded messages + * and adapted to kdbus transport. + * In socket transport returns false on out-of-memory. Here this won't happen, + * so it always returns TRUE. + */ static dbus_bool_t -do_authentication (DBusTransport *transport, - dbus_bool_t do_reading, - dbus_bool_t do_writing, - dbus_bool_t *auth_completed) +do_writing (DBusTransport *transport) { - dbus_bool_t oom; - dbus_bool_t orig_auth_state; - - oom = FALSE; - - orig_auth_state = _dbus_transport_try_to_authenticate (transport); + DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport; + int total = 0; - /* This is essential to avoid the check_write_watch() at the end, - * we don't want to add a write watch in do_iteration before - * we try writing and get EAGAIN - */ - if (orig_auth_state) + if (transport->disconnected) { - if (auth_completed) - *auth_completed = FALSE; + _dbus_verbose ("Not connected, not writing anything\n"); return TRUE; } - _dbus_transport_ref (transport); + _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n", + _dbus_connection_has_messages_to_send_unlocked (transport->connection), + kdbus_transport->fd); - while (!_dbus_transport_try_to_authenticate (transport) && - _dbus_transport_get_is_connected (transport)) + while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection)) { - if (!exchange_credentials (transport, do_reading, do_writing)) + int bytes_written; + DBusMessage *message; + const DBusString *header; + const DBusString *body; + int total_bytes_to_write; + const char* pDestination; + + if (total > kdbus_transport->max_bytes_written_per_iteration) { - oom = TRUE; + _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n", + total, kdbus_transport->max_bytes_written_per_iteration); goto out; } - if (transport->send_credentials_pending || - transport->receive_credentials_pending) + message = _dbus_connection_get_message_to_send (transport->connection); + _dbus_assert (message != NULL); + if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages { - _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n", - transport->send_credentials_pending, - transport->receive_credentials_pending); - goto out; + dbus_message_unlock(message); + dbus_message_set_sender(message, kdbus_transport->sender); + dbus_message_lock (message); } + _dbus_message_get_network_data (message, &header, &body); + total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body); + pDestination = dbus_message_get_destination(message); -#define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client") - switch (_dbus_auth_do_work (transport->auth)) + if(pDestination) { - case DBUS_AUTH_STATE_WAITING_FOR_INPUT: - _dbus_verbose (" %s auth state: waiting for input\n", - TRANSPORT_SIDE (transport)); - if (!do_reading || !read_data_into_auth (transport, &oom)) - goto out; - break; - - case DBUS_AUTH_STATE_WAITING_FOR_MEMORY: - _dbus_verbose (" %s auth state: waiting for memory\n", - TRANSPORT_SIDE (transport)); - oom = TRUE; - goto out; - break; - - case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND: - _dbus_verbose (" %s auth state: bytes to send\n", - TRANSPORT_SIDE (transport)); - if (!do_writing || !write_data_from_auth (transport)) - goto out; - break; - - case DBUS_AUTH_STATE_NEED_DISCONNECT: - _dbus_verbose (" %s auth state: need to disconnect\n", - TRANSPORT_SIDE (transport)); - do_io_error (transport); - break; - - case DBUS_AUTH_STATE_AUTHENTICATED: - _dbus_verbose (" %s auth state: authenticated\n", - TRANSPORT_SIDE (transport)); - break; - } - } - - out: - if (auth_completed) - *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport)); - - check_read_watch (transport); - check_write_watch (transport); - _dbus_transport_unref (transport); - - if (oom) - return FALSE; - else - return TRUE; -} -#endif - -/* returns false on oom */ -static dbus_bool_t -do_writing (DBusTransport *transport) -{ - DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport; - dbus_bool_t oom; - -#ifdef DBUS_AUTHENTICATION - /* No messages without authentication! */ - if (!_dbus_transport_try_to_authenticate (transport)) - { - _dbus_verbose ("Not authenticated, not writing anything\n"); - return TRUE; - } -#endif - - if (transport->disconnected) - { - _dbus_verbose ("Not connected, not writing anything\n"); - return TRUE; - } - -#if 1 - _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n", - _dbus_connection_has_messages_to_send_unlocked (transport->connection), - kdbus_transport->fd); -#endif - - oom = FALSE; - - while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection)) - { - int bytes_written; - DBusMessage *message; - const DBusString *header; - const DBusString *body; - int total_bytes_to_write; - const char* pDestination; - - message = _dbus_connection_get_message_to_send (transport->connection); - _dbus_assert (message != NULL); - if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages - { - dbus_message_unlock(message); - dbus_message_set_sender(message, kdbus_transport->sender); - dbus_message_lock (message); - } - _dbus_message_get_network_data (message, &header, &body); - total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body); - pDestination = dbus_message_get_destination(message); + int ret; - if(pDestination) - { - if(!strcmp(pDestination, DBUS_SERVICE_DBUS)) - { - if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS)) - { - int ret; - - ret = emulateOrgFreedesktopDBus(transport, message); - if(ret < 0) - { - bytes_written = -1; - goto written; - } - else if(ret == 0) - { - bytes_written = total_bytes_to_write; - goto written; - } - //else send to "daemon" as to normal recipient - } - } - } - if (_dbus_auth_needs_encoding (transport->auth)) - { - if (_dbus_string_get_length (&kdbus_transport->encoded_outgoing) == 0) + ret = capture_hello_message(transport, pDestination, message); + if(ret < 0) //error { - if (!_dbus_auth_encode_data (transport->auth, - header, &kdbus_transport->encoded_outgoing)) - { - oom = TRUE; - goto out; - } - - if (!_dbus_auth_encode_data (transport->auth, - body, &kdbus_transport->encoded_outgoing)) - { - _dbus_string_set_length (&kdbus_transport->encoded_outgoing, 0); - oom = TRUE; - goto out; - } + bytes_written = -1; + goto written; } - - total_bytes_to_write = _dbus_string_get_length (&kdbus_transport->encoded_outgoing); - if(total_bytes_to_write > kdbus_transport->max_bytes_written_per_iteration) - return -E2BIG; - - bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination, TRUE); + else if(ret == 0) //hello message captured and handled correctly + { + bytes_written = total_bytes_to_write; + goto written; + } + //else send as regular message } - else - { - if(total_bytes_to_write > kdbus_transport->max_bytes_written_per_iteration) - return -E2BIG; - bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination, FALSE); - } + bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination); -written: - if (bytes_written < 0) - { - /* EINTR already handled for us */ + written: + if (bytes_written < 0) + { + /* EINTR already handled for us */ /* For some discussion of why we also ignore EPIPE here, see * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html */ - if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ()) - goto out; - else - { - _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ()); - do_io_error (transport); - goto out; - } - } - else - { - _dbus_verbose (" wrote %d bytes of %d\n", bytes_written, - total_bytes_to_write); - - kdbus_transport->message_bytes_written += bytes_written; + if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ()) + goto out; + else + { + _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ()); + do_io_error (transport); + goto out; + } + } + else + { + _dbus_verbose (" wrote %d bytes of %d\n", bytes_written, + total_bytes_to_write); - _dbus_assert (kdbus_transport->message_bytes_written <= - total_bytes_to_write); + total += bytes_written; - if (kdbus_transport->message_bytes_written == total_bytes_to_write) - { - kdbus_transport->message_bytes_written = 0; - _dbus_string_set_length (&kdbus_transport->encoded_outgoing, 0); - _dbus_string_compact (&kdbus_transport->encoded_outgoing, 2048); + _dbus_assert (bytes_written == total_bytes_to_write); - _dbus_connection_message_sent_unlocked (transport->connection, - message); - } - } + _dbus_connection_message_sent_unlocked (transport->connection, + message); + } } - out: - if (oom) - return FALSE; - return TRUE; + out: + return TRUE; } -/* returns false on out-of-memory */ +/** + * Based on do_reading from socket transport. + * Removed authentication code and code related to encoded messages + * and adapted to kdbus transport. + * returns false on out-of-memory + */ static dbus_bool_t do_reading (DBusTransport *transport) { @@ -1707,20 +1403,22 @@ do_reading (DBusTransport *transport) int bytes_read; dbus_bool_t oom = FALSE; int *fds, n_fds; + int total = 0; _dbus_verbose ("fd = %d\n",kdbus_transport->fd); -#ifdef DBUS_AUTHENTICATION - /* No messages without authentication! */ - if (!_dbus_transport_try_to_authenticate (transport)) - return TRUE; -#endif - again: /* See if we've exceeded max messages and need to disable reading */ check_read_watch (transport); + if (total > kdbus_transport->max_bytes_read_per_iteration) + { + _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n", + total, kdbus_transport->max_bytes_read_per_iteration); + goto out; + } + _dbus_assert (kdbus_transport->read_watch != NULL || transport->disconnected); @@ -1738,32 +1436,7 @@ do_reading (DBusTransport *transport) } _dbus_message_loader_get_buffer (transport->loader, &buffer); - if (_dbus_auth_needs_decoding (transport->auth)) - { - bytes_read = kdbus_read_message(kdbus_transport, &kdbus_transport->encoded_incoming, fds, &n_fds); - - _dbus_assert (_dbus_string_get_length (&kdbus_transport->encoded_incoming) == bytes_read); - - if (bytes_read > 0) - { - if (!_dbus_auth_decode_data (transport->auth, - &kdbus_transport->encoded_incoming, - buffer)) - { - _dbus_verbose ("Out of memory decoding incoming data\n"); - _dbus_message_loader_return_buffer (transport->loader, - buffer, - _dbus_string_get_length (buffer)); - oom = TRUE; - goto out; - } - - _dbus_string_set_length (&kdbus_transport->encoded_incoming, 0); - _dbus_string_compact (&kdbus_transport->encoded_incoming, 2048); - } - } - else - bytes_read = kdbus_read_message(kdbus_transport, buffer, fds, &n_fds); + bytes_read = kdbus_read_message(kdbus_transport, buffer, fds, &n_fds); if (bytes_read >= 0 && n_fds > 0) _dbus_verbose("Read %i unix fds\n", n_fds); @@ -1803,6 +1476,8 @@ do_reading (DBusTransport *transport) { _dbus_verbose (" read %d bytes\n", bytes_read); + total += bytes_read; + if (!_dbus_transport_queue_messages (transport)) { oom = TRUE; @@ -1823,8 +1498,8 @@ do_reading (DBusTransport *transport) return TRUE; } -/* - * copy-paste from socket transport. +/** + * Copy-paste from socket transport, with socket replaced by kdbus. */ static dbus_bool_t unix_error_with_read_to_come (DBusTransport *itransport, @@ -1844,8 +1519,9 @@ unix_error_with_read_to_come (DBusTransport *itransport, return TRUE; } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport. Removed authentication related code + * and renamed socket_transport to kdbus_transport. */ static dbus_bool_t kdbus_handle_watch (DBusTransport *transport, @@ -1872,50 +1548,22 @@ kdbus_handle_watch (DBusTransport *transport, if (watch == kdbus_transport->read_watch && (flags & DBUS_WATCH_READABLE)) { -#ifdef DBUS_AUTHENTICATION - dbus_bool_t auth_finished; -#endif -#if 1 _dbus_verbose ("handling read watch %p flags = %x\n", watch, flags); -#endif -#ifdef DBUS_AUTHENTICATION - if (!do_authentication (transport, TRUE, FALSE, &auth_finished)) - return FALSE; - - /* We don't want to do a read immediately following - * a successful authentication. This is so we - * have a chance to propagate the authentication - * state further up. Specifically, we need to - * process any pending data from the auth object. - */ - if (!auth_finished) - { -#endif + if (!do_reading (transport)) { _dbus_verbose ("no memory to read\n"); return FALSE; } -#ifdef DBUS_AUTHENTICATION - } - else - { - _dbus_verbose ("Not reading anything since we just completed the authentication\n"); - } -#endif + } else if (watch == kdbus_transport->write_watch && (flags & DBUS_WATCH_WRITABLE)) { -#if 1 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n", _dbus_connection_has_messages_to_send_unlocked (transport->connection)); -#endif -#ifdef DBUS_AUTHENTICATION - if (!do_authentication (transport, FALSE, TRUE, NULL)) - return FALSE; -#endif + if (!do_writing (transport)) { _dbus_verbose ("no memory to write\n"); @@ -1925,26 +1573,13 @@ kdbus_handle_watch (DBusTransport *transport, /* See if we still need the write watch */ check_write_watch (transport); } -#ifdef DBUS_ENABLE_VERBOSE_MODE - else - { - if (watch == kdbus_transport->read_watch) - _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n", - flags); - else if (watch == kdbus_transport->write_watch) - _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n", - flags); - else - _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n", - watch, dbus_watch_get_socket (watch)); - } -#endif /* DBUS_ENABLE_VERBOSE_MODE */ return TRUE; } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport, but socket_transport renamed to kdbus_transport + * and _dbus_close_socket replaced with close(). */ static void kdbus_disconnect (DBusTransport *transport) @@ -1955,19 +1590,28 @@ kdbus_disconnect (DBusTransport *transport) free_watches (transport); - _dbus_close_socket (kdbus_transport->fd, NULL); + again: + if (close (kdbus_transport->fd) < 0) + { + if (errno == EINTR) + goto again; + } + kdbus_transport->fd = -1; } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport. Renamed socket_transport to + * kdbus_transport and added dbus_connection_set_is_authenticated, because + * we do not perform authentication in kdbus, so we have mark is as already done + * to make everything work. */ static dbus_bool_t kdbus_connection_set (DBusTransport *transport) { DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport; - dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus + dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus, so mark it done _dbus_watch_set_handler (kdbus_transport->write_watch, _dbus_connection_handle_watch, @@ -1995,7 +1639,11 @@ kdbus_connection_set (DBusTransport *transport) return TRUE; } -/** original dbus copy-pasted comment +/** + * Copy-paste from socket_transport. + * Socket_transport renamed to kdbus_transport + * + * Original dbus copy-pasted @todo comment below. * @todo We need to have a way to wake up the select sleep if * a new iteration request comes in with a flag (read/write) that * we're not currently serving. Otherwise a call that just reads @@ -2020,60 +1668,39 @@ kdbus_do_iteration (DBusTransport *transport, kdbus_transport->write_watch, kdbus_transport->fd); - /* the passed in DO_READING/DO_WRITING flags indicate whether to - * read/write messages, but regardless of those we may need to block - * for reading/writing to do auth. But if we do reading for auth, - * we don't want to read any messages yet if not given DO_READING. - */ - poll_fd.fd = kdbus_transport->fd; poll_fd.events = 0; - if (_dbus_transport_try_to_authenticate (transport)) - { - /* This is kind of a hack; if we have stuff to write, then try - * to avoid the poll. This is probably about a 5% speedup on an - * echo client/server. - * - * If both reading and writing were requested, we want to avoid this - * since it could have funky effects: - * - both ends spinning waiting for the other one to read - * data so they can finish writing - * - prioritizing all writing ahead of reading - */ - if ((flags & DBUS_ITERATION_DO_WRITING) && - !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) && - !transport->disconnected && - _dbus_connection_has_messages_to_send_unlocked (transport->connection)) - { - do_writing (transport); - - if (transport->disconnected || - !_dbus_connection_has_messages_to_send_unlocked (transport->connection)) - goto out; - } - - /* If we get here, we decided to do the poll() after all */ - _dbus_assert (kdbus_transport->read_watch); - if (flags & DBUS_ITERATION_DO_READING) - poll_fd.events |= _DBUS_POLLIN; - - _dbus_assert (kdbus_transport->write_watch); - if (flags & DBUS_ITERATION_DO_WRITING) - poll_fd.events |= _DBUS_POLLOUT; - } - else - { - DBusAuthState auth_state; - - auth_state = _dbus_auth_do_work (transport->auth); - - if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT) - poll_fd.events |= _DBUS_POLLIN; - - if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND) - poll_fd.events |= _DBUS_POLLOUT; - } + /* This is kind of a hack; if we have stuff to write, then try + * to avoid the poll. This is probably about a 5% speedup on an + * echo client/server. + * + * If both reading and writing were requested, we want to avoid this + * since it could have funky effects: + * - both ends spinning waiting for the other one to read + * data so they can finish writing + * - prioritizing all writing ahead of reading + */ + if ((flags & DBUS_ITERATION_DO_WRITING) && + !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) && + !transport->disconnected && + _dbus_connection_has_messages_to_send_unlocked (transport->connection)) + { + do_writing (transport); + + if (transport->disconnected || + !_dbus_connection_has_messages_to_send_unlocked (transport->connection)) + goto out; + } + + /* If we get here, we decided to do the poll() after all */ + _dbus_assert (kdbus_transport->read_watch); + if (flags & DBUS_ITERATION_DO_READING) + poll_fd.events |= _DBUS_POLLIN; + + _dbus_assert (kdbus_transport->write_watch); + if (flags & DBUS_ITERATION_DO_WRITING) + poll_fd.events |= _DBUS_POLLOUT; if (poll_fd.events) { @@ -2097,10 +1724,7 @@ kdbus_do_iteration (DBusTransport *transport, poll_res = _dbus_poll (&poll_fd, 1, poll_timeout); if (poll_res < 0 && _dbus_get_is_errno_eintr ()) - { - _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ()); - goto again; - } + goto again; if (flags & DBUS_ITERATION_BLOCK) { @@ -2122,20 +1746,10 @@ kdbus_do_iteration (DBusTransport *transport, { dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0; dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0; -#ifdef DBUS_AUTHENTICATION - dbus_bool_t authentication_completed; -#endif _dbus_verbose ("in iteration, need_read=%d need_write=%d\n", need_read, need_write); -#ifdef DBUS_AUTHENTICATION - do_authentication (transport, need_read, need_write, - &authentication_completed); - /* See comment in socket_handle_watch. */ - if (authentication_completed) - goto out; -#endif if (need_read && (flags & DBUS_ITERATION_DO_READING)) do_reading (transport); if (need_write && (flags & DBUS_ITERATION_DO_WRITING)) @@ -2162,8 +1776,8 @@ kdbus_do_iteration (DBusTransport *transport, _dbus_verbose (" ... leaving do_iteration()\n"); } -/* - * copy-paste from socket transport with needed renames only. +/** + * Copy-paste from socket transport. */ static void kdbus_live_messages_changed (DBusTransport *transport) @@ -2172,6 +1786,12 @@ kdbus_live_messages_changed (DBusTransport *transport) check_read_watch (transport); } +/** + * Gets file descriptor of the kdbus bus. + * @param transport transport + * @param fd_p place to write fd to + * @returns always TRUE + */ static dbus_bool_t kdbus_get_kdbus_fd (DBusTransport *transport, int *fd_p) @@ -2194,15 +1814,17 @@ static const DBusTransportVTable kdbus_vtable = { }; /** - * Creates a new transport for the given kdbus file descriptor. The file - * descriptor must be nonblocking. + * Copy-paste from dbus_transport_socket with needed changes. + * + * Creates a new transport for the given kdbus file descriptor and address. + * The file descriptor must be nonblocking. * * @param fd the file descriptor. * @param address the transport's address * @returns the new transport, or #NULL if no memory. */ static DBusTransport* -_dbus_transport_new_kdbus_transport (int fd, const DBusString *address) +new_kdbus_transport (int fd, const DBusString *address) { DBusTransportKdbus *kdbus_transport; @@ -2210,12 +1832,6 @@ _dbus_transport_new_kdbus_transport (int fd, const DBusString *address) if (kdbus_transport == NULL) return NULL; - if (!_dbus_string_init (&kdbus_transport->encoded_outgoing)) - goto failed_0; - - if (!_dbus_string_init (&kdbus_transport->encoded_incoming)) - goto failed_1; - kdbus_transport->write_watch = _dbus_watch_new (fd, DBUS_WATCH_WRITABLE, FALSE, @@ -2235,18 +1851,11 @@ _dbus_transport_new_kdbus_transport (int fd, const DBusString *address) NULL, address)) goto failed_4; -#ifdef DBUS_AUTHENTICATION -#ifdef HAVE_UNIX_FD_PASSING - _dbus_auth_set_unix_fd_possible(kdbus_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd)); -#endif -#endif - kdbus_transport->fd = fd; - kdbus_transport->message_bytes_written = 0; /* These values should probably be tunable or something. */ - kdbus_transport->max_bytes_read_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH; - kdbus_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH; + kdbus_transport->max_bytes_read_per_iteration = MAX_BYTES_PER_ITERATION; + kdbus_transport->max_bytes_written_per_iteration = MAX_BYTES_PER_ITERATION; kdbus_transport->kdbus_mmap_ptr = NULL; kdbus_transport->memfd = -1; @@ -2260,10 +1869,6 @@ _dbus_transport_new_kdbus_transport (int fd, const DBusString *address) _dbus_watch_invalidate (kdbus_transport->write_watch); _dbus_watch_unref (kdbus_transport->write_watch); failed_2: - _dbus_string_free (&kdbus_transport->encoded_incoming); - failed_1: - _dbus_string_free (&kdbus_transport->encoded_outgoing); - failed_0: dbus_free (kdbus_transport); return NULL; } @@ -2271,9 +1876,7 @@ _dbus_transport_new_kdbus_transport (int fd, const DBusString *address) /** * Opens a connection to the kdbus bus * - * This will set FD_CLOEXEC for the socket returned. - * - * @param path the path to UNIX domain socket + * @param path the path to kdbus bus * @param error return location for error code * @returns connection file descriptor or -1 on error */ @@ -2292,8 +1895,7 @@ static int _dbus_connect_kdbus (const char *path, DBusError *error) } /** - * Creates a new transport for kdbus. - * This creates a client-side of a transport. + * Connects to kdbus, creates and sets-up transport. * * @param path the path to the bus. * @param error address where an error can be returned. @@ -2330,7 +1932,7 @@ static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError _dbus_verbose ("Successfully connected to kdbus bus %s\n", path); - transport = _dbus_transport_new_kdbus_transport (fd, &address); + transport = new_kdbus_transport (fd, &address); if (transport == NULL) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); @@ -2342,20 +1944,25 @@ static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError return transport; failed_1: - _dbus_close_socket (fd, NULL); - failed_0: - _dbus_string_free (&address); - return NULL; + again: + if (close (fd) < 0) + { + if (errno == EINTR) + goto again; + } + failed_0: + _dbus_string_free (&address); + return NULL; } /** * Opens kdbus transport if method from address entry is kdbus * - * @param entry the address entry to try opening + * @param entry the address entry to open * @param transport_p return location for the opened transport - * @param error error to be set - * @returns result of the attempt + * @param error place to store error + * @returns result of the attempt as a DBusTransportOpenResult enum */ DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry, DBusTransport **transport_p, @@ -2395,3 +2002,5 @@ DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry, return DBUS_TRANSPORT_OPEN_NOT_HANDLED; } } + +/** @} */