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>
49 #define KDBUS_PART_FOREACH(part, head, first) \
50 for (part = (head)->first; \
51 (uint8_t *)(part) < (uint8_t *)(head) + (head)->size; \
52 part = KDBUS_PART_NEXT(part))
53 #define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU)
54 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used
56 #define KDBUS_MSG_DECODE_DEBUG 0
57 //#define DBUS_AUTHENTICATION
59 #define ITER_APPEND_STR(string) \
60 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) \
66 #define MSG_ITEM_BUILD_VEC(data, datasize) \
67 item->type = KDBUS_MSG_PAYLOAD_VEC; \
68 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec); \
69 item->vec.address = (unsigned long) data; \
70 item->vec.size = datasize;
73 * Opaque object representing a transport. In kdbus transport it has nothing common
74 * with socket, but the name was preserved to comply with upper functions.
76 typedef struct DBusTransportKdbus DBusTransportKdbus;
79 * Implementation details of DBusTransportSocket. All members are private.
81 struct DBusTransportKdbus
83 DBusTransport base; /**< Parent instance */
84 int fd; /**< File descriptor. */
85 DBusWatch *read_watch; /**< Watch for readability. */
86 DBusWatch *write_watch; /**< Watch for writability. */
88 int max_bytes_read_per_iteration; /**< In kdbus only to control overall message size*/
89 int max_bytes_written_per_iteration; /**< In kdbus only to control overall message size*/
91 int message_bytes_written; /**< Number of bytes of current
92 * outgoing message that have
95 DBusString encoded_outgoing; /**< Encoded version of current
98 DBusString encoded_incoming; /**< Encoded version of current
101 void* kdbus_mmap_ptr; /**< Mapped memory where Kdbus (kernel) writes
102 * messages incoming to us.
104 int memfd; /**< File descriptor to special
105 * memory pool for bulk data
106 * transfer. Retrieved from
107 * Kdbus kernel module.
109 __u64 bloom_size; /**< bloom filter field size */
110 char* sender; /**< uniqe name of the sender */
114 kdbus_get_kdbus_fd (DBusTransport *transport,
117 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
119 *fd_p = kdbus_transport->fd;
125 * Adds locally generated message to received messages queue
128 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* connection)
130 DBusList *message_link;
132 message_link = _dbus_list_alloc_link (message);
133 if (message_link == NULL)
135 dbus_message_unref (message);
139 _dbus_connection_queue_synthesized_message_link(connection, message_link);
145 * Generates local error message as a reply to message given as parameter
146 * and adds generated error message to received messages queue.
148 static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
150 DBusMessage *errMessage;
151 char* error_msg = "";
155 error_msg = alloca(strlen(template) + strlen(object));
156 sprintf(error_msg, template, object);
159 error_msg = (char*)object;
161 errMessage = generate_local_error_message(dbus_message_get_serial(message), error_type, error_msg);
162 if(errMessage == NULL)
164 if (add_message_to_received(errMessage, connection))
171 * Generates reply to the message given as a parameter with one item in the reply body
172 * and adds generated reply message to received messages queue.
174 static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
176 DBusMessageIter args;
179 reply = dbus_message_new_method_return(message);
182 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
183 dbus_message_iter_init_append(reply, &args);
184 if (!dbus_message_iter_append_basic(&args, data_type, pData))
186 dbus_message_unref(reply);
189 if(add_message_to_received(reply, connection))
196 static int reply_ack(DBusMessage *message, DBusConnection* connection)
200 reply = dbus_message_new_method_return(message);
203 if(add_message_to_received(reply, connection))
209 * Retrieves file descriptor to memory pool from kdbus module.
210 * It is then used for bulk data sending.
211 * Triggered when message payload is over MEMFD_SIZE_THRESHOLD
214 static int kdbus_init_memfd(DBusTransportKdbus* socket_transport)
218 if (ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) {
219 _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
223 socket_transport->memfd = memfd;
224 _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd);
229 * Initiates Kdbus message structure.
230 * Calculates it's size, allocates memory and fills some fields.
231 * @param name Well-known name or NULL
232 * @param dst_id Numeric id of recipient
233 * @param body_size size of message body if present
234 * @param use_memfd flag to build memfd message
235 * @param fds_count number of file descriptors used
236 * @param transport transport
237 * @return initialized kdbus message
239 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)
241 struct kdbus_msg* msg;
244 msg_size = sizeof(struct kdbus_msg);
246 if(use_memfd == TRUE) // bulk data - memfd - encoded and plain
247 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
249 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
251 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
255 msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
258 msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
259 else if (dst_id == KDBUS_DST_ID_BROADCAST)
260 msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
262 msg = malloc(msg_size);
265 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
269 memset(msg, 0, msg_size);
270 msg->size = msg_size;
271 msg->payload_type = KDBUS_PAYLOAD_DBUS1;
272 msg->dst_id = name ? 0 : dst_id;
273 msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
279 * Builds and sends kdbus message using DBus message.
280 * Decide whether used payload vector or memfd memory pool.
281 * Handles broadcasts and unicast messages, and passing of Unix fds.
282 * Does error handling.
283 * TODO refactor to be more compact
285 * @param transport transport
286 * @param message DBus message to be sent
287 * @param encoded flag if the message is encoded
288 * @return size of data sent
290 static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination, dbus_bool_t encoded)
292 struct kdbus_msg *msg;
293 struct kdbus_item *item;
294 uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
295 const DBusString *header;
296 const DBusString *body;
297 uint64_t ret_size = 0;
298 uint64_t body_size = 0;
299 uint64_t header_size = 0;
300 dbus_bool_t use_memfd;
303 dbus_bool_t autostart;
305 // determine destination and destination id
308 dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
309 if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
311 dst_id = strtoull(&destination[3], NULL, 10);
318 ret_size = _dbus_string_get_length (&transport->encoded_outgoing);
321 _dbus_message_get_network_data (message, &header, &body);
322 header_size = _dbus_string_get_length(header);
323 body_size = _dbus_string_get_length(body);
324 ret_size = header_size + body_size;
327 // check if message size is big enough to use memfd kdbus transport
328 use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
329 if(use_memfd) kdbus_init_memfd(transport);
331 _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
333 // init basic message fields
334 msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport);
335 msg->cookie = dbus_message_get_serial(message);
336 autostart = dbus_message_get_auto_start (message);
338 msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START;
340 // build message contents
343 // case 1 - bulk data transfer - memfd - encoded and plain
348 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
350 _dbus_verbose("memfd sealing failed: \n");
354 buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
355 if (buf == MAP_FAILED)
357 _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
362 memcpy(buf, &transport->encoded_outgoing, ret_size);
365 memcpy(buf, _dbus_string_get_const_data(header), header_size);
368 memcpy(buf, _dbus_string_get_const_data(body), body_size);
373 munmap(buf, ret_size);
375 // seal data - kdbus module needs it
376 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) {
377 _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
382 item->type = KDBUS_MSG_PAYLOAD_MEMFD;
383 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
384 item->memfd.size = ret_size;
385 item->memfd.fd = transport->memfd;
386 // case 2 - small encoded - don't use memfd
388 _dbus_verbose("sending encoded data\n");
389 MSG_ITEM_BUILD_VEC(&transport->encoded_outgoing, _dbus_string_get_length (&transport->encoded_outgoing));
391 // case 3 - small not encoded - don't use memfd
393 _dbus_verbose("sending normal vector data\n");
394 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size);
398 _dbus_verbose("body attaching\n");
399 item = KDBUS_PART_NEXT(item);
400 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(body), body_size);
406 item = KDBUS_PART_NEXT(item);
407 item->type = KDBUS_MSG_FDS;
408 item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
409 memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
414 item = KDBUS_PART_NEXT(item);
415 item->type = KDBUS_MSG_DST_NAME;
416 item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1;
417 memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE);
419 else if (dst_id == KDBUS_DST_ID_BROADCAST)
421 item = KDBUS_PART_NEXT(item);
422 item->type = KDBUS_MSG_BLOOM;
423 item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
424 strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
428 if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
432 else if(errno == ENXIO) //no such id on the bus
434 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
437 else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus
441 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))
445 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
448 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
454 close(transport->memfd);
460 * Performs kdbus query of id of the given name
462 * @param name name to query for
463 * @param transport transport
464 * @param pInfo nameInfo structure address to store info about the name
465 * @return 0 on success, -errno if failed
467 int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
469 struct kdbus_cmd_name_info *msg;
470 struct kdbus_item *item;
475 pInfo->sec_label_len = 0;
476 pInfo->sec_label = NULL;
478 item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
479 item_size = (item_size < 56) ? 56 : item_size; //at least 56 bytes are needed by kernel to place info about name, otherwise error
480 size = sizeof(struct kdbus_cmd_name_info) + item_size;
485 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
489 memset(msg, 0, size);
491 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
492 msg->id = strtoull(&name[3], NULL, 10);
497 item->type = KDBUS_NAME_INFO_ITEM_NAME;
498 item->size = item_size;
499 memcpy(item->str, name, strlen(name) + 1);
502 ret = ioctl(((DBusTransportKdbus*)transport)->fd, KDBUS_CMD_NAME_QUERY, msg);
509 else if(ret == -ENOBUFS)
511 msg = realloc(msg, msg->size); //prepare memory
520 pInfo->uniqueId = msg->id;
521 pInfo->userId = msg->creds.uid;
522 pInfo->processId = msg->creds.pid;
524 while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
526 if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
528 pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
529 if(pInfo->sec_label_len != 0)
531 pInfo->sec_label = malloc(pInfo->sec_label_len);
532 if(pInfo->sec_label == NULL)
535 memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
539 item = KDBUS_PART_NEXT(item);
548 * Performs kdbus hello - registration on the kdbus bus
550 * @param name place to store unique name given by bus
551 * @param transportS transport structure
552 * @returns #TRUE on success
554 static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS)
556 struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
558 hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
559 KDBUS_HELLO_ATTACH_COMM |
560 KDBUS_HELLO_ATTACH_EXE |
561 KDBUS_HELLO_ATTACH_CMDLINE |
562 KDBUS_HELLO_ATTACH_CAPS |
563 KDBUS_HELLO_ATTACH_CGROUP |
564 KDBUS_HELLO_ATTACH_SECLABEL |
565 KDBUS_HELLO_ATTACH_AUDIT*/;
566 hello.size = sizeof(struct kdbus_cmd_hello);
567 hello.pool_size = RECEIVE_POOL_SIZE;
569 if (ioctl(transportS->fd, KDBUS_CMD_HELLO, &hello))
571 _dbus_verbose ("Failed to send hello: %m, %d",errno);
575 sprintf(name, "%llu", (unsigned long long)hello.id);
576 _dbus_verbose("-- Our peer ID is: %s\n", name);
577 transportS->bloom_size = hello.bloom_size;
579 transportS->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, transportS->fd, 0);
580 if (transportS->kdbus_mmap_ptr == MAP_FAILED)
582 _dbus_verbose("Error when mmap: %m, %d",errno);
590 * Seeks key in rule string, and duplicates value of the key into pValue.
591 * If value is "org.freedesktop.DBus" it is indicated by returning -1, because it
592 * needs to be handled in different manner.
593 * Value is duplicated from rule string to newly allocated memory pointe by pValue,
594 * so it must be freed after use.
596 * @param rule rule to look through
597 * @param key key to look for
598 * @param pValue pointer to value of the key found
599 * @return length of the value string, 0 means not found, -1 means "org.freedesktop.DBus"
601 static int parse_match_key(const char *rule, const char* key, char** pValue)
604 const char* pValueEnd;
605 int value_length = 0;
607 pBegin = strstr(rule, key);
610 pBegin += strlen(key);
611 pValueEnd = strchr(pBegin, '\'');
614 value_length = pValueEnd - pBegin;
615 *pValue = strndup(pBegin, value_length);
618 if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
620 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
628 * Adds a match rule to match broadcast messages going through the message bus.
629 * Do no affect messages addressed directly.
631 * The "rule" argument is the string form of a match rule.
633 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
635 * interface match key mapped to bloom
636 * sender match key mapped to src_name
637 * also handled org.freedesktop.dbus member: NameOwnerChanged
638 * and separately NameLost, NameAcquired, IdAdded, IdRemoved
640 * @param transport transport
641 * @param id id of connection for which the rule is to be added
642 * @param rule textual form of match rule
644 dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
646 struct kdbus_cmd_match* pCmd_match;
647 struct kdbus_item *pItem;
648 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
650 unsigned int kernel_item = 0;
653 char* pInterface = NULL;
654 dbus_bool_t ret_value = FALSE;
655 DBusTransportKdbus* transportS = (DBusTransportKdbus*)transport;
657 /*parsing rule and calculating size of command*/
658 size = sizeof(struct kdbus_cmd_match);
660 if(strstr(rule, "member='NameOwnerChanged'"))
663 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /*std DBus: 3 name related items plus 2 id related items*/
665 else if(strstr(rule, "member='NameChanged'"))
667 kernel_item = KDBUS_MATCH_NAME_CHANGE;
668 size += KDBUS_ITEM_SIZE(1);
670 else if(strstr(rule, "member='NameLost'"))
672 kernel_item = KDBUS_MATCH_NAME_REMOVE;
673 size += KDBUS_ITEM_SIZE(1);
675 else if(strstr(rule, "member='NameAcquired'"))
677 kernel_item = KDBUS_MATCH_NAME_ADD;
678 size += KDBUS_ITEM_SIZE(1);
680 else if(strstr(rule, "member='IdAdded'"))
682 kernel_item = KDBUS_MATCH_ID_ADD;
683 size += KDBUS_ITEM_SIZE(sizeof(__u64));
685 else if(strstr(rule, "member='IdRemoved'"))
687 kernel_item = KDBUS_MATCH_ID_REMOVE;
688 size += KDBUS_ITEM_SIZE(sizeof(__u64));
691 name_size = parse_match_key(rule, "interface='", &pInterface);
692 if((name_size == -1) && (kernel_item == 0)) //means org.freedesktop.DBus without specified member
695 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /* 3 above name related items plus 2 id related items*/
697 else if(name_size > 0) /*actual size is not important for interface because bloom size is defined by bus*/
698 size += KDBUS_PART_HEADER_SIZE + transportS->bloom_size;
700 name_size = parse_match_key(rule, "sender='", &pName);
701 if((name_size == -1) && (kernel_item == 0)) //means org.freedesktop.DBus without specified name - same as interface few line above
704 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /* 3 above name related items plus 2 id related items*/
706 else if(name_size > 0)
708 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
710 src_id = strtoull(&pName[3], NULL, 10);
715 size += KDBUS_ITEM_SIZE(name_size + 1); //well known name
718 pCmd_match = alloca(size);
719 if(pCmd_match == NULL)
723 pCmd_match->cookie = id;
724 pCmd_match->size = size;
726 pItem = pCmd_match->items;
727 if(kernel_item == ~0) //all signals from kernel
729 pCmd_match->src_id = 0;
730 pItem->type = KDBUS_MATCH_NAME_CHANGE;
731 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
732 pItem = KDBUS_PART_NEXT(pItem);
733 pItem->type = KDBUS_MATCH_NAME_ADD;
734 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
735 pItem = KDBUS_PART_NEXT(pItem);
736 pItem->type = KDBUS_MATCH_NAME_REMOVE;
737 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
738 pItem = KDBUS_PART_NEXT(pItem);
739 pItem->type = KDBUS_MATCH_ID_ADD;
740 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
741 pItem = KDBUS_PART_NEXT(pItem);
742 pItem->type = KDBUS_MATCH_ID_REMOVE;
743 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
745 else if(kernel_item) //only one item
747 pCmd_match->src_id = 0;
748 pItem->type = kernel_item;
749 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
753 pCmd_match->src_id = src_id;
756 pItem->type = KDBUS_MATCH_SRC_NAME;
757 pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
758 memcpy(pItem->str, pName, strlen(pName) + 1);
759 pItem = KDBUS_PART_NEXT(pItem);
764 pItem->type = KDBUS_MATCH_BLOOM;
765 pItem->size = KDBUS_PART_HEADER_SIZE + transportS->bloom_size;
766 strncpy(pItem->data, pInterface, transportS->bloom_size);
770 if(ioctl(transportS->fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
771 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
774 _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
787 * Opposing to dbus, in kdbus removes all match rules with given
788 * cookie, which in this implementation is equal to uniqe id.
790 * @param transport transport
791 * @param id connection id for which rules are to be removed
793 dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
795 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
796 DBusTransportKdbus* transportS = (DBusTransportKdbus*) transport;
800 cmd.size = sizeof(struct kdbus_cmd_match);
802 if(ioctl(transportS->fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
804 _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
809 _dbus_verbose("Match rule removed correctly.\n");
815 * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate
816 * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue.
818 * Now it captures only Hello message, which must be handled locally.
819 * All the rest is passed to dbus-daemon.
821 static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
823 #ifdef DBUS_SERVICES_IN_LIB
825 struct nameInfo info;
829 if(!strcmp(dbus_message_get_member(message), "Hello"))
834 name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + 1);
837 if(!bus_register_kdbus(name, (DBusTransportKdbus*)transport))
839 if(!register_kdbus_policy(name, ((DBusTransportKdbus*)transport)->fd))
842 sender = malloc (strlen(name) + 4);
845 sprintf(sender, ":1.%s", name);
846 ((DBusTransportKdbus*)transport)->sender = sender;
848 if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
849 return 0; //todo why we cannot free name after sending reply, shouldn't we?
856 #ifdef DBUS_SERVICES_IN_LIB
857 else if(!strcmp(dbus_message_get_member(message), "RequestName"))
863 if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
866 if(!register_kdbus_policy(name, ((DBusTransportKdbus*)transport)->fd))
869 result = request_kdbus_name(((DBusTransportKdbus*)transport)->fd, name, flags, 0);
871 return reply_with_error(DBUS_ERROR_ACCESS_DENIED,
872 "Connection is not allowed to own the service \"%s\" due to security policies in the configuration file",
873 name, message, transport->connection);
875 return reply_with_error(DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name, message, transport->connection);
877 return reply_1_data(message, DBUS_TYPE_UINT32, &result, transport->connection);
879 else if(!strcmp(dbus_message_get_member(message), "AddMatch"))
883 if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &rule, DBUS_TYPE_INVALID))
886 if(!add_match_kdbus(transport, 0, rule))
889 return reply_ack(message,transport->connection);
891 else if(!strcmp(dbus_message_get_member(message), "RemoveMatch"))
893 if(!remove_match_kdbus(transport, 0))
895 return reply_ack(message, transport->connection);
897 else if(!strcmp(dbus_message_get_member(message), "GetNameOwner")) //returns id of the well known name
901 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
902 inter_ret = kdbus_NameQuery(name, transport, &info);
903 if(inter_ret == 0) //unique id of the name
905 char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
906 const char* pString = unique_name;
908 sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
909 _dbus_verbose("Unique name discovered:%s\n", unique_name);
910 ret_value = reply_1_data(message, DBUS_TYPE_STRING, &pString, transport->connection);
912 else if(inter_ret == -ENOENT) //name has no owner
913 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get owner of name '%s': no such name", name, message, transport->connection);
916 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
917 ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine unique name for '%s'", name, message, transport->connection);
920 else if(!strcmp(dbus_message_get_member(message), "NameHasOwner")) //returns if name is currently registered on the bus
925 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
926 inter_ret = kdbus_NameQuery(name, transport, &info);
927 if((inter_ret == 0) || (inter_ret == -ENOENT))
929 result = (inter_ret == 0) ? TRUE : FALSE;
930 ret_value = reply_1_data(message, DBUS_TYPE_BOOLEAN, &result, transport->connection);
934 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
935 ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name, message, transport->connection);
938 else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
942 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
943 inter_ret = kdbus_NameQuery(name, transport, &info);
944 if(inter_ret == 0) //name found
946 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
947 ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.userId, transport->connection);
949 else if(inter_ret == -ENOENT) //name has no owner
950 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get UID of name '%s': no such name", name, message, transport->connection);
953 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
954 ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name, message, transport->connection);
957 else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixProcessID"))
961 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
962 inter_ret = kdbus_NameQuery(name, transport, &info);
963 if(inter_ret == 0) //name found
964 ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.processId, transport->connection);
965 else if(inter_ret == -ENOENT) //name has no owner
966 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get PID of name '%s': no such name", name, message, transport->connection);
969 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
970 ret_value = reply_with_error(DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,"Could not determine PID for '%s'", name, message, transport->connection);
973 else if(!strcmp(dbus_message_get_member(message), "ListNames")) //return all well known names on he bus
975 struct kdbus_cmd_names* pCmd;
978 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
979 pCmd = malloc(cmd_size);
982 pCmd->size = cmd_size;
986 if(ioctl(((DBusTransportKdbus*)transport)->fd, KDBUS_CMD_NAME_LIST, pCmd))
990 if(errno == ENOBUFS) //buffer to small to put all names into it
991 cmd_size = pCmd->size; //here kernel tells how much memory it needs
994 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
998 if(cmd_size) //kernel needs more memory
1000 pCmd = realloc(pCmd, cmd_size); //prepare memory
1003 goto again; //and try again
1008 DBusMessageIter iter, sub;
1009 struct kdbus_cmd_name* pCmd_name;
1012 reply = dbus_message_new_method_return(message);
1015 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
1016 dbus_message_iter_init_append(reply, &iter);
1017 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &sub))
1019 dbus_message_unref(reply);
1022 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
1024 pName = pCmd_name->name;
1025 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &pName))
1027 dbus_message_unref(reply);
1032 if (!dbus_message_iter_close_container (&iter, &sub))
1034 dbus_message_unref (reply);
1038 if(add_message_to_received(reply, transport->connection))
1046 else if(!strcmp(dbus_message_get_member(message), "GetId"))
1049 char uuid[DBUS_UUID_LENGTH_BYTES];
1052 DBusString binary, encoded;
1054 path = &transport->address[11]; //start of kdbus bus path
1055 if(stat(path, &stats) < -1)
1057 _dbus_verbose("kdbus error reading stats of bus: err %d (%m)\n", errno);
1058 return reply_with_error(DBUS_ERROR_FAILED, "Could not determine bus '%s' uuid", path, message, transport->connection);
1062 MD5_Update(&md5, path, strlen(path));
1063 MD5_Update(&md5, &stats.st_ctim.tv_sec, sizeof(stats.st_ctim.tv_sec));
1064 MD5_Final(uuid, &md5);
1066 if(!_dbus_string_init (&encoded))
1068 _dbus_string_init_const_len (&binary, uuid, DBUS_UUID_LENGTH_BYTES);
1069 if(!_dbus_string_hex_encode (&binary, 0, &encoded, _dbus_string_get_length (&encoded)))
1071 path = (char*)_dbus_string_get_const_data (&encoded);
1072 ret_value = reply_1_data(message, DBUS_TYPE_STRING, &path, transport->connection);
1075 _dbus_string_free(&binary);
1076 _dbus_string_free(&encoded);
1081 /* else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData")) //todo to be implemented if needed and possible
1085 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1086 return reply_with_error(DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN, "Could not determine audit session data for '%s'", name, message, transport->connection);
1088 #ifdef DBUS_SERVICES_IN_LIB
1089 else if(!strcmp(dbus_message_get_member(message), "GetConnectionSELinuxSecurityContext"))
1093 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1094 inter_ret = kdbus_NameQuery(name, transport, &info);
1095 if(inter_ret == -ENOENT) //name has no owner
1096 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get security context of name '%s': no such name", name, message, transport->connection);
1097 else if(inter_ret < 0)
1098 return reply_with_error(DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN, "Could not determine security context for '%s'", name, message, transport->connection);
1103 reply = dbus_message_new_method_return(message);
1106 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
1107 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
1108 dbus_message_unref(reply);
1109 else if(add_message_to_received(reply, transport->connection))
1116 return 1; //send to daemon
1118 #ifdef DBUS_SERVICES_IN_LIB
1120 free(info.sec_label);
1125 #if KDBUS_MSG_DECODE_DEBUG == 1
1126 static char *msg_id(uint64_t id, char *buf)
1132 sprintf(buf, "%llu", (unsigned long long)id);
1136 struct kdbus_enum_table {
1140 #define _STRINGIFY(x) #x
1141 #define STRINGIFY(x) _STRINGIFY(x)
1142 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
1143 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
1144 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
1145 #define LOOKUP(what) \
1146 const char *enum_##what(long long id) { \
1148 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
1149 if (id == kdbus_table_##what[i].id) \
1150 return kdbus_table_##what[i].name; \
1153 const char *enum_MSG(long long id);
1155 ENUM(_KDBUS_MSG_NULL),
1156 ENUM(KDBUS_MSG_PAYLOAD_VEC),
1157 ENUM(KDBUS_MSG_PAYLOAD_OFF),
1158 ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
1159 ENUM(KDBUS_MSG_FDS),
1160 ENUM(KDBUS_MSG_BLOOM),
1161 ENUM(KDBUS_MSG_DST_NAME),
1162 ENUM(KDBUS_MSG_SRC_CREDS),
1163 ENUM(KDBUS_MSG_SRC_PID_COMM),
1164 ENUM(KDBUS_MSG_SRC_TID_COMM),
1165 ENUM(KDBUS_MSG_SRC_EXE),
1166 ENUM(KDBUS_MSG_SRC_CMDLINE),
1167 ENUM(KDBUS_MSG_SRC_CGROUP),
1168 ENUM(KDBUS_MSG_SRC_CAPS),
1169 ENUM(KDBUS_MSG_SRC_SECLABEL),
1170 ENUM(KDBUS_MSG_SRC_AUDIT),
1171 ENUM(KDBUS_MSG_SRC_NAMES),
1172 ENUM(KDBUS_MSG_TIMESTAMP),
1173 ENUM(KDBUS_MSG_NAME_ADD),
1174 ENUM(KDBUS_MSG_NAME_REMOVE),
1175 ENUM(KDBUS_MSG_NAME_CHANGE),
1176 ENUM(KDBUS_MSG_ID_ADD),
1177 ENUM(KDBUS_MSG_ID_REMOVE),
1178 ENUM(KDBUS_MSG_REPLY_TIMEOUT),
1179 ENUM(KDBUS_MSG_REPLY_DEAD),
1182 const char *enum_PAYLOAD(long long id);
1184 ENUM(KDBUS_PAYLOAD_KERNEL),
1185 ENUM(KDBUS_PAYLOAD_DBUS1),
1186 ENUM(KDBUS_PAYLOAD_GVARIANT),
1191 * Puts locally generated message into received data buffer.
1192 * Use only during receiving phase!
1194 * @param message message to load
1195 * @param data place to load message
1196 * @return size of message
1198 static int put_message_into_data(DBusMessage *message, char* data)
1201 const DBusString *header;
1202 const DBusString *body;
1205 dbus_message_set_serial(message, 1);
1206 dbus_message_lock (message);
1207 _dbus_message_get_network_data (message, &header, &body);
1208 ret_size = _dbus_string_get_length(header);
1209 memcpy(data, _dbus_string_get_const_data(header), ret_size);
1211 size = _dbus_string_get_length(body);
1212 memcpy(data, _dbus_string_get_const_data(body), size);
1219 * Get the length of the kdbus message content.
1221 * @param msg kdbus message
1222 * @return the length of the kdbus message
1224 static int kdbus_message_size(const struct kdbus_msg* msg)
1226 const struct kdbus_item *item;
1229 KDBUS_PART_FOREACH(item, msg, items)
1231 if (item->size <= KDBUS_PART_HEADER_SIZE)
1233 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
1238 case KDBUS_MSG_PAYLOAD_OFF:
1239 ret_size += item->vec.size;
1241 case KDBUS_MSG_PAYLOAD_MEMFD:
1242 ret_size += item->memfd.size;
1253 * Decodes kdbus message in order to extract dbus message and put it into data and fds.
1254 * Also captures and decodes kdbus error messages and kdbus kernel broadcasts and converts
1255 * all of them into dbus messages.
1257 * @param msg kdbus message
1258 * @param data place to copy dbus message to
1259 * @param kdbus_transport transport
1260 * @param fds place to store file descriptors received
1261 * @param n_fds place to store quantity of file descriptor
1262 * @return number of dbus message's bytes received or -1 on error
1264 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportKdbus* kdbus_transport, int* fds, int* n_fds)
1266 const struct kdbus_item *item;
1268 DBusMessage *message = NULL;
1269 DBusMessageIter args;
1270 const char* emptyString = "";
1271 const char* pString = NULL;
1272 char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1273 const char* pDBusName = dbus_name;
1274 #if KDBUS_MSG_DECODE_DEBUG == 1
1278 #if KDBUS_MSG_DECODE_DEBUG == 1
1279 _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
1280 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
1281 (unsigned long long) msg->flags,
1282 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
1283 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
1288 KDBUS_PART_FOREACH(item, msg, items)
1290 if (item->size <= KDBUS_PART_HEADER_SIZE)
1292 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
1293 break; //??? continue (because dbus will find error) or break
1298 case KDBUS_MSG_PAYLOAD_OFF:
1299 memcpy(data, (char *)kdbus_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
1300 data += item->vec.size;
1301 ret_size += item->vec.size;
1303 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
1304 enum_MSG(item->type), item->size,
1305 (unsigned long long)item->vec.offset,
1306 (unsigned long long)item->vec.size);
1309 case KDBUS_MSG_PAYLOAD_MEMFD:
1314 size = item->memfd.size;
1315 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
1317 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
1318 if (buf == MAP_FAILED)
1320 _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
1324 memcpy(data, buf, size);
1330 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
1331 enum_MSG(item->type), item->size,
1332 (unsigned long long)item->vec.offset,
1333 (unsigned long long)item->vec.size);
1341 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
1342 memcpy(fds, item->fds, *n_fds * sizeof(int));
1343 for (i = 0; i < *n_fds; i++)
1344 _dbus_fd_set_close_on_exec(fds[i]);
1348 #if KDBUS_MSG_DECODE_DEBUG == 1
1349 case KDBUS_MSG_SRC_CREDS:
1350 _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
1351 enum_MSG(item->type), item->size,
1352 item->creds.uid, item->creds.gid,
1353 item->creds.pid, item->creds.tid,
1354 item->creds.starttime);
1357 case KDBUS_MSG_SRC_PID_COMM:
1358 case KDBUS_MSG_SRC_TID_COMM:
1359 case KDBUS_MSG_SRC_EXE:
1360 case KDBUS_MSG_SRC_CGROUP:
1361 case KDBUS_MSG_SRC_SECLABEL:
1362 case KDBUS_MSG_DST_NAME:
1363 _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
1364 enum_MSG(item->type), item->size, item->str, strlen(item->str));
1367 case KDBUS_MSG_SRC_CMDLINE:
1368 case KDBUS_MSG_SRC_NAMES: {
1369 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
1370 const char *str = item->str;
1373 _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
1375 _dbus_verbose("'%s' ", str);
1376 size -= strlen(str) + 1;
1377 str += strlen(str) + 1;
1381 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
1385 case KDBUS_MSG_SRC_AUDIT:
1386 _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
1387 enum_MSG(item->type), item->size,
1388 (unsigned long long)item->data64[0],
1389 (unsigned long long)item->data64[1]);
1392 case KDBUS_MSG_SRC_CAPS: {
1394 const uint32_t *cap;
1397 _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
1398 enum_MSG(item->type), item->size,
1399 (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
1402 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
1404 _dbus_verbose(" CapInh=");
1405 for (i = 0; i < n; i++)
1406 _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
1408 _dbus_verbose(" CapPrm=");
1409 for (i = 0; i < n; i++)
1410 _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
1412 _dbus_verbose(" CapEff=");
1413 for (i = 0; i < n; i++)
1414 _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
1416 _dbus_verbose(" CapInh=");
1417 for (i = 0; i < n; i++)
1418 _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
1419 _dbus_verbose("\n");
1423 case KDBUS_MSG_TIMESTAMP:
1424 _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
1425 enum_MSG(item->type), item->size,
1426 (unsigned long long)item->timestamp.realtime_ns,
1427 (unsigned long long)item->timestamp.monotonic_ns);
1431 case KDBUS_MSG_REPLY_TIMEOUT:
1432 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
1433 enum_MSG(item->type), item->size, msg->cookie_reply);
1435 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
1442 ret_size = put_message_into_data(message, data);
1445 case KDBUS_MSG_REPLY_DEAD:
1446 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
1447 enum_MSG(item->type), item->size, msg->cookie_reply);
1449 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
1456 ret_size = put_message_into_data(message, data);
1459 case KDBUS_MSG_NAME_ADD:
1460 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
1461 enum_MSG(item->type), (unsigned long long) item->size,
1462 item->name_change.name, item->name_change.old_id,
1463 item->name_change.new_id, item->name_change.flags);
1465 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1472 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
1473 pString = item->name_change.name;
1474 _dbus_verbose ("Name added: %s\n", pString);
1475 dbus_message_iter_init_append(message, &args);
1476 ITER_APPEND_STR(pString)
1477 ITER_APPEND_STR(emptyString)
1478 ITER_APPEND_STR(pDBusName)
1479 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1481 ret_size = put_message_into_data(message, data);
1484 case KDBUS_MSG_NAME_REMOVE:
1485 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
1486 enum_MSG(item->type), (unsigned long long) item->size,
1487 item->name_change.name, item->name_change.old_id,
1488 item->name_change.new_id, item->name_change.flags);
1490 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
1497 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
1498 pString = item->name_change.name;
1499 _dbus_verbose ("Name removed: %s\n", pString);
1500 dbus_message_iter_init_append(message, &args);
1501 ITER_APPEND_STR(pString)
1502 ITER_APPEND_STR(pDBusName)
1503 ITER_APPEND_STR(emptyString)
1504 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1506 ret_size = put_message_into_data(message, data);
1509 case KDBUS_MSG_NAME_CHANGE:
1510 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
1511 enum_MSG(item->type), (unsigned long long) item->size,
1512 item->name_change.name, item->name_change.old_id,
1513 item->name_change.new_id, item->name_change.flags);
1515 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1522 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
1523 pString = item->name_change.name;
1524 _dbus_verbose ("Name changed: %s\n", pString);
1525 dbus_message_iter_init_append(message, &args);
1526 ITER_APPEND_STR(pString)
1527 ITER_APPEND_STR(pDBusName)
1528 sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
1529 _dbus_verbose ("New id: %s\n", pDBusName);
1530 ITER_APPEND_STR(pDBusName)
1531 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1533 ret_size = put_message_into_data(message, data);
1536 case KDBUS_MSG_ID_ADD:
1537 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1538 enum_MSG(item->type), (unsigned long long) item->size,
1539 (unsigned long long) item->id_change.id,
1540 (unsigned long long) item->id_change.flags);
1542 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1549 sprintf(dbus_name,":1.%llu",item->id_change.id);
1550 dbus_message_iter_init_append(message, &args);
1551 ITER_APPEND_STR(pDBusName)
1552 ITER_APPEND_STR(emptyString)
1553 ITER_APPEND_STR(pDBusName)
1554 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1556 ret_size = put_message_into_data(message, data);
1559 case KDBUS_MSG_ID_REMOVE:
1560 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1561 enum_MSG(item->type), (unsigned long long) item->size,
1562 (unsigned long long) item->id_change.id,
1563 (unsigned long long) item->id_change.flags);
1565 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1572 sprintf(dbus_name,":1.%llu",item->id_change.id);
1573 dbus_message_iter_init_append(message, &args);
1574 ITER_APPEND_STR(pDBusName)
1575 ITER_APPEND_STR(pDBusName)
1576 ITER_APPEND_STR(emptyString)
1577 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1579 ret_size = put_message_into_data(message, data);
1581 #if KDBUS_MSG_DECODE_DEBUG == 1
1583 _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
1589 #if KDBUS_MSG_DECODE_DEBUG == 1
1591 if ((char *)item - ((char *)msg + msg->size) >= 8)
1592 _dbus_verbose("invalid padding at end of message\n");
1597 dbus_message_unref(message);
1602 * Reads message from kdbus and puts it into dbus buffer and fds
1604 * @param transport transport
1605 * @param buffer place to copy received message to
1606 * @param fds place to store file descriptors sent in the message
1607 * @param n_fds place to store number of file descriptors
1608 * @return size of received message on success, -1 on error
1610 static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString *buffer, int* fds, int* n_fds)
1612 int ret_size, buf_size;
1613 uint64_t __attribute__ ((__aligned__(8))) offset;
1614 struct kdbus_msg *msg;
1618 start = _dbus_string_get_length (buffer);
1621 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1625 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1626 _dbus_string_set_length (buffer, start);
1630 msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
1632 buf_size = kdbus_message_size(msg);
1635 _dbus_verbose("kdbus error - too short message: %d (%m)\n", errno);
1639 /* What is the maximum size of the locally generated message?
1640 I just assume 2048 bytes */
1641 buf_size = MAX(buf_size, 2048);
1643 if (!_dbus_string_lengthen (buffer, buf_size))
1648 data = _dbus_string_get_data_len (buffer, start, buf_size);
1650 ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
1652 if(ret_size == -1) /* error */
1654 _dbus_string_set_length (buffer, start);
1657 else if (buf_size != ret_size) /* case of locally generated message */
1659 _dbus_string_set_length (buffer, start + ret_size);
1663 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
1667 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1674 * copy-paste from socket transport with needed renames only.
1677 free_watches (DBusTransport *transport)
1679 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1681 _dbus_verbose ("start\n");
1683 if (kdbus_transport->read_watch)
1685 if (transport->connection)
1686 _dbus_connection_remove_watch_unlocked (transport->connection,
1687 kdbus_transport->read_watch);
1688 _dbus_watch_invalidate (kdbus_transport->read_watch);
1689 _dbus_watch_unref (kdbus_transport->read_watch);
1690 kdbus_transport->read_watch = NULL;
1693 if (kdbus_transport->write_watch)
1695 if (transport->connection)
1696 _dbus_connection_remove_watch_unlocked (transport->connection,
1697 kdbus_transport->write_watch);
1698 _dbus_watch_invalidate (kdbus_transport->write_watch);
1699 _dbus_watch_unref (kdbus_transport->write_watch);
1700 kdbus_transport->write_watch = NULL;
1703 _dbus_verbose ("end\n");
1707 * copy-paste from socket transport with needed renames only.
1710 transport_finalize (DBusTransport *transport)
1712 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1714 _dbus_verbose ("\n");
1716 free_watches (transport);
1718 _dbus_string_free (&kdbus_transport->encoded_outgoing);
1719 _dbus_string_free (&kdbus_transport->encoded_incoming);
1721 _dbus_transport_finalize_base (transport);
1723 _dbus_assert (kdbus_transport->read_watch == NULL);
1724 _dbus_assert (kdbus_transport->write_watch == NULL);
1726 dbus_free (transport);
1730 * copy-paste from socket transport with needed renames only.
1733 check_write_watch (DBusTransport *transport)
1735 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1738 if (transport->connection == NULL)
1741 if (transport->disconnected)
1743 _dbus_assert (kdbus_transport->write_watch == NULL);
1747 _dbus_transport_ref (transport);
1749 #ifdef DBUS_AUTHENTICATION
1750 if (_dbus_transport_try_to_authenticate (transport))
1752 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1753 #ifdef DBUS_AUTHENTICATION
1756 if (transport->send_credentials_pending)
1760 DBusAuthState auth_state;
1762 auth_state = _dbus_auth_do_work (transport->auth);
1764 /* If we need memory we install the write watch just in case,
1765 * if there's no need for it, it will get de-installed
1766 * next time we try reading.
1768 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
1769 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1776 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1777 needed, transport->connection, kdbus_transport->write_watch,
1778 kdbus_transport->fd,
1779 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1781 _dbus_connection_toggle_watch_unlocked (transport->connection,
1782 kdbus_transport->write_watch,
1785 _dbus_transport_unref (transport);
1789 * copy-paste from socket transport with needed renames only.
1792 check_read_watch (DBusTransport *transport)
1794 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1795 dbus_bool_t need_read_watch;
1797 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1799 if (transport->connection == NULL)
1802 if (transport->disconnected)
1804 _dbus_assert (kdbus_transport->read_watch == NULL);
1808 _dbus_transport_ref (transport);
1810 #ifdef DBUS_AUTHENTICATION
1811 if (_dbus_transport_try_to_authenticate (transport))
1814 (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1815 (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1816 #ifdef DBUS_AUTHENTICATION
1819 if (transport->receive_credentials_pending)
1820 need_read_watch = TRUE;
1823 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
1824 * is to avoid spinning on the file descriptor when we're waiting
1825 * to write or for some other part of the auth process
1827 DBusAuthState auth_state;
1829 auth_state = _dbus_auth_do_work (transport->auth);
1831 /* If we need memory we install the read watch just in case,
1832 * if there's no need for it, it will get de-installed
1833 * next time we try reading. If we're authenticated we
1834 * install it since we normally have it installed while
1837 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
1838 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
1839 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
1840 need_read_watch = TRUE;
1842 need_read_watch = FALSE;
1847 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
1848 _dbus_connection_toggle_watch_unlocked (transport->connection,
1849 kdbus_transport->read_watch,
1852 _dbus_transport_unref (transport);
1856 * copy-paste from socket transport.
1859 do_io_error (DBusTransport *transport)
1861 _dbus_transport_ref (transport);
1862 _dbus_transport_disconnect (transport);
1863 _dbus_transport_unref (transport);
1866 #ifdef DBUS_AUTHENTICATION
1867 /* return value is whether we successfully read any new data. */
1869 read_data_into_auth (DBusTransport *transport,
1872 DBusTransportKdbus *socket_transport = (DBusTransportKdbus*) transport;
1879 _dbus_auth_get_buffer (transport->auth, &buffer);
1881 bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
1883 _dbus_auth_return_buffer (transport->auth, buffer,
1884 bytes_read > 0 ? bytes_read : 0);
1888 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
1891 else if (bytes_read < 0)
1893 /* EINTR already handled for us */
1895 if (_dbus_get_is_errno_enomem ())
1899 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1900 ; /* do nothing, just return FALSE below */
1903 _dbus_verbose ("Error reading from remote app: %s\n",
1904 _dbus_strerror_from_errno ());
1905 do_io_error (transport);
1912 _dbus_assert (bytes_read == 0);
1914 _dbus_verbose ("Disconnected from remote app\n");
1915 do_io_error (transport);
1921 static int kdbus_send_auth (DBusTransport *transport, const DBusString *buffer)
1924 int bytes_written = -1;
1925 struct kdbus_msg *msg;
1926 struct kdbus_item *item;
1928 len = _dbus_string_get_length (buffer);
1929 // data = _dbus_string_get_const_data_len (buffer, 0, len);
1931 msg = kdbus_init_msg(NULL, 1, 0, FALSE, 0, (DBusTransportKdbus*)transport);
1933 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data_len (buffer, 0, len), len);
1936 if(ioctl(((DBusTransportKdbus*)transport)->fd, KDBUS_CMD_MSG_SEND, msg))
1940 _dbus_verbose ("Error writing auth: %d, %m\n", errno);
1943 bytes_written = len;
1945 return bytes_written;
1948 /* Return value is whether we successfully wrote any bytes */
1950 write_data_from_auth (DBusTransport *transport)
1952 // DBusTransportKdbus *socket_transport = (DBusTransportSocket*) transport;
1954 const DBusString *buffer;
1956 if (!_dbus_auth_get_bytes_to_send (transport->auth,
1960 bytes_written = kdbus_send_auth (transport, buffer);
1962 if (bytes_written > 0)
1964 _dbus_auth_bytes_sent (transport->auth, bytes_written);
1967 else if (bytes_written < 0)
1969 /* EINTR already handled for us */
1971 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1975 _dbus_verbose ("Error writing to remote app: %s\n",
1976 _dbus_strerror_from_errno ());
1977 do_io_error (transport);
1986 exchange_credentials (DBusTransport *transport,
1987 dbus_bool_t do_reading,
1988 dbus_bool_t do_writing)
1990 DBusTransportKdbus *socket_transport = (DBusTransportKdbus*) transport;
1991 DBusError error = DBUS_ERROR_INIT;
1993 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
1994 do_reading, do_writing);
1996 if (do_writing && transport->send_credentials_pending)
1998 if (_dbus_send_credentials_socket (socket_transport->fd,
2001 transport->send_credentials_pending = FALSE;
2005 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
2006 dbus_error_free (&error);
2007 do_io_error (transport);
2011 if (do_reading && transport->receive_credentials_pending)
2013 /* FIXME this can fail due to IO error _or_ OOM, broken
2014 * (somewhat tricky to fix since the OOM error can be set after
2015 * we already read the credentials byte, so basically we need to
2016 * separate reading the byte and storing it in the
2017 * transport->credentials). Does not really matter for now
2018 * because storing in credentials never actually fails on unix.
2020 if (_dbus_read_credentials_socket (socket_transport->fd,
2021 transport->credentials,
2024 transport->receive_credentials_pending = FALSE;
2028 _dbus_verbose ("Failed to read credentials %s\n", error.message);
2029 dbus_error_free (&error);
2030 do_io_error (transport);
2034 if (!(transport->send_credentials_pending ||
2035 transport->receive_credentials_pending))
2037 if (!_dbus_auth_set_credentials (transport->auth,
2038 transport->credentials))
2046 do_authentication (DBusTransport *transport,
2047 dbus_bool_t do_reading,
2048 dbus_bool_t do_writing,
2049 dbus_bool_t *auth_completed)
2052 dbus_bool_t orig_auth_state;
2056 orig_auth_state = _dbus_transport_try_to_authenticate (transport);
2058 /* This is essential to avoid the check_write_watch() at the end,
2059 * we don't want to add a write watch in do_iteration before
2060 * we try writing and get EAGAIN
2062 if (orig_auth_state)
2065 *auth_completed = FALSE;
2069 _dbus_transport_ref (transport);
2071 while (!_dbus_transport_try_to_authenticate (transport) &&
2072 _dbus_transport_get_is_connected (transport))
2074 if (!exchange_credentials (transport, do_reading, do_writing))
2080 if (transport->send_credentials_pending ||
2081 transport->receive_credentials_pending)
2083 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
2084 transport->send_credentials_pending,
2085 transport->receive_credentials_pending);
2089 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
2090 switch (_dbus_auth_do_work (transport->auth))
2092 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
2093 _dbus_verbose (" %s auth state: waiting for input\n",
2094 TRANSPORT_SIDE (transport));
2095 if (!do_reading || !read_data_into_auth (transport, &oom))
2099 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
2100 _dbus_verbose (" %s auth state: waiting for memory\n",
2101 TRANSPORT_SIDE (transport));
2106 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
2107 _dbus_verbose (" %s auth state: bytes to send\n",
2108 TRANSPORT_SIDE (transport));
2109 if (!do_writing || !write_data_from_auth (transport))
2113 case DBUS_AUTH_STATE_NEED_DISCONNECT:
2114 _dbus_verbose (" %s auth state: need to disconnect\n",
2115 TRANSPORT_SIDE (transport));
2116 do_io_error (transport);
2119 case DBUS_AUTH_STATE_AUTHENTICATED:
2120 _dbus_verbose (" %s auth state: authenticated\n",
2121 TRANSPORT_SIDE (transport));
2128 *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
2130 check_read_watch (transport);
2131 check_write_watch (transport);
2132 _dbus_transport_unref (transport);
2141 /* returns false on oom */
2143 do_writing (DBusTransport *transport)
2145 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
2148 #ifdef DBUS_AUTHENTICATION
2149 /* No messages without authentication! */
2150 if (!_dbus_transport_try_to_authenticate (transport))
2152 _dbus_verbose ("Not authenticated, not writing anything\n");
2157 if (transport->disconnected)
2159 _dbus_verbose ("Not connected, not writing anything\n");
2164 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
2165 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
2166 kdbus_transport->fd);
2171 while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
2174 DBusMessage *message;
2175 const DBusString *header;
2176 const DBusString *body;
2177 int total_bytes_to_write;
2178 const char* pDestination;
2180 message = _dbus_connection_get_message_to_send (transport->connection);
2181 _dbus_assert (message != NULL);
2182 if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages
2184 dbus_message_unlock(message);
2185 dbus_message_set_sender(message, kdbus_transport->sender);
2186 dbus_message_lock (message);
2188 _dbus_message_get_network_data (message, &header, &body);
2189 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
2190 pDestination = dbus_message_get_destination(message);
2194 if(!strcmp(pDestination, DBUS_SERVICE_DBUS))
2196 if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
2200 ret = emulateOrgFreedesktopDBus(transport, message);
2208 bytes_written = total_bytes_to_write;
2211 //else send to "daemon" as to normal recipient
2215 if (_dbus_auth_needs_encoding (transport->auth))
2217 if (_dbus_string_get_length (&kdbus_transport->encoded_outgoing) == 0)
2219 if (!_dbus_auth_encode_data (transport->auth,
2220 header, &kdbus_transport->encoded_outgoing))
2226 if (!_dbus_auth_encode_data (transport->auth,
2227 body, &kdbus_transport->encoded_outgoing))
2229 _dbus_string_set_length (&kdbus_transport->encoded_outgoing, 0);
2235 total_bytes_to_write = _dbus_string_get_length (&kdbus_transport->encoded_outgoing);
2236 if(total_bytes_to_write > kdbus_transport->max_bytes_written_per_iteration)
2239 bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination, TRUE);
2243 if(total_bytes_to_write > kdbus_transport->max_bytes_written_per_iteration)
2246 bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination, FALSE);
2250 if (bytes_written < 0)
2252 /* EINTR already handled for us */
2254 /* For some discussion of why we also ignore EPIPE here, see
2255 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
2258 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
2262 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
2263 do_io_error (transport);
2269 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
2270 total_bytes_to_write);
2272 kdbus_transport->message_bytes_written += bytes_written;
2274 _dbus_assert (kdbus_transport->message_bytes_written <=
2275 total_bytes_to_write);
2277 if (kdbus_transport->message_bytes_written == total_bytes_to_write)
2279 kdbus_transport->message_bytes_written = 0;
2280 _dbus_string_set_length (&kdbus_transport->encoded_outgoing, 0);
2281 _dbus_string_compact (&kdbus_transport->encoded_outgoing, 2048);
2283 _dbus_connection_message_sent_unlocked (transport->connection,
2295 /* returns false on out-of-memory */
2297 do_reading (DBusTransport *transport)
2299 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
2302 dbus_bool_t oom = FALSE;
2305 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
2307 #ifdef DBUS_AUTHENTICATION
2308 /* No messages without authentication! */
2309 if (!_dbus_transport_try_to_authenticate (transport))
2315 /* See if we've exceeded max messages and need to disable reading */
2316 check_read_watch (transport);
2318 _dbus_assert (kdbus_transport->read_watch != NULL ||
2319 transport->disconnected);
2321 if (transport->disconnected)
2324 if (!dbus_watch_get_enabled (kdbus_transport->read_watch))
2327 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
2329 _dbus_verbose ("Out of memory reading file descriptors\n");
2333 _dbus_message_loader_get_buffer (transport->loader, &buffer);
2335 if (_dbus_auth_needs_decoding (transport->auth))
2337 bytes_read = kdbus_read_message(kdbus_transport, &kdbus_transport->encoded_incoming, fds, &n_fds);
2339 _dbus_assert (_dbus_string_get_length (&kdbus_transport->encoded_incoming) == bytes_read);
2343 if (!_dbus_auth_decode_data (transport->auth,
2344 &kdbus_transport->encoded_incoming,
2347 _dbus_verbose ("Out of memory decoding incoming data\n");
2348 _dbus_message_loader_return_buffer (transport->loader,
2350 _dbus_string_get_length (buffer));
2355 _dbus_string_set_length (&kdbus_transport->encoded_incoming, 0);
2356 _dbus_string_compact (&kdbus_transport->encoded_incoming, 2048);
2360 bytes_read = kdbus_read_message(kdbus_transport, buffer, fds, &n_fds);
2362 if (bytes_read >= 0 && n_fds > 0)
2363 _dbus_verbose("Read %i unix fds\n", n_fds);
2365 _dbus_message_loader_return_buffer (transport->loader,
2367 bytes_read < 0 ? 0 : bytes_read);
2368 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
2372 /* EINTR already handled for us */
2374 if (_dbus_get_is_errno_enomem ())
2376 _dbus_verbose ("Out of memory in read()/do_reading()\n");
2380 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
2384 _dbus_verbose ("Error reading from remote app: %s\n",
2385 _dbus_strerror_from_errno ());
2386 do_io_error (transport);
2390 else if (bytes_read == 0)
2392 _dbus_verbose ("Disconnected from remote app\n");
2393 do_io_error (transport);
2398 _dbus_verbose (" read %d bytes\n", bytes_read);
2400 if (!_dbus_transport_queue_messages (transport))
2403 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
2407 /* Try reading more data until we get EAGAIN and return, or
2408 * exceed max bytes per iteration. If in blocking mode of
2409 * course we'll block instead of returning.
2421 * copy-paste from socket transport.
2424 unix_error_with_read_to_come (DBusTransport *itransport,
2428 DBusTransportKdbus *transport = (DBusTransportKdbus *) itransport;
2430 if (!((flags & DBUS_WATCH_HANGUP) || (flags & DBUS_WATCH_ERROR)))
2433 /* If we have a read watch enabled ...
2434 we -might have data incoming ... => handle the HANGUP there */
2435 if (watch != transport->read_watch && _dbus_watch_get_enabled (transport->read_watch))
2442 * copy-paste from socket transport with needed renames only.
2445 kdbus_handle_watch (DBusTransport *transport,
2449 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
2451 _dbus_assert (watch == kdbus_transport->read_watch ||
2452 watch == kdbus_transport->write_watch);
2453 _dbus_assert (watch != NULL);
2455 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
2456 * still be in the buffer and do_reading may need several iteration to read
2457 * it all (because of its max_bytes_read_per_iteration limit).
2459 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
2461 _dbus_verbose ("Hang up or error on watch\n");
2462 _dbus_transport_disconnect (transport);
2466 if (watch == kdbus_transport->read_watch &&
2467 (flags & DBUS_WATCH_READABLE))
2469 #ifdef DBUS_AUTHENTICATION
2470 dbus_bool_t auth_finished;
2473 _dbus_verbose ("handling read watch %p flags = %x\n",
2476 #ifdef DBUS_AUTHENTICATION
2477 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
2480 /* We don't want to do a read immediately following
2481 * a successful authentication. This is so we
2482 * have a chance to propagate the authentication
2483 * state further up. Specifically, we need to
2484 * process any pending data from the auth object.
2489 if (!do_reading (transport))
2491 _dbus_verbose ("no memory to read\n");
2494 #ifdef DBUS_AUTHENTICATION
2498 _dbus_verbose ("Not reading anything since we just completed the authentication\n");
2502 else if (watch == kdbus_transport->write_watch &&
2503 (flags & DBUS_WATCH_WRITABLE))
2506 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
2507 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
2509 #ifdef DBUS_AUTHENTICATION
2510 if (!do_authentication (transport, FALSE, TRUE, NULL))
2513 if (!do_writing (transport))
2515 _dbus_verbose ("no memory to write\n");
2519 /* See if we still need the write watch */
2520 check_write_watch (transport);
2522 #ifdef DBUS_ENABLE_VERBOSE_MODE
2525 if (watch == kdbus_transport->read_watch)
2526 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
2528 else if (watch == kdbus_transport->write_watch)
2529 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
2532 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
2533 watch, dbus_watch_get_socket (watch));
2535 #endif /* DBUS_ENABLE_VERBOSE_MODE */
2541 * copy-paste from socket transport with needed renames only.
2544 kdbus_disconnect (DBusTransport *transport)
2546 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
2548 _dbus_verbose ("\n");
2550 free_watches (transport);
2552 _dbus_close_socket (kdbus_transport->fd, NULL);
2553 kdbus_transport->fd = -1;
2557 * copy-paste from socket transport with needed renames only.
2560 kdbus_connection_set (DBusTransport *transport)
2562 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
2564 dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus
2566 _dbus_watch_set_handler (kdbus_transport->write_watch,
2567 _dbus_connection_handle_watch,
2568 transport->connection, NULL);
2570 _dbus_watch_set_handler (kdbus_transport->read_watch,
2571 _dbus_connection_handle_watch,
2572 transport->connection, NULL);
2574 if (!_dbus_connection_add_watch_unlocked (transport->connection,
2575 kdbus_transport->write_watch))
2578 if (!_dbus_connection_add_watch_unlocked (transport->connection,
2579 kdbus_transport->read_watch))
2581 _dbus_connection_remove_watch_unlocked (transport->connection,
2582 kdbus_transport->write_watch);
2586 check_read_watch (transport);
2587 check_write_watch (transport);
2592 /** original dbus copy-pasted comment
2593 * @todo We need to have a way to wake up the select sleep if
2594 * a new iteration request comes in with a flag (read/write) that
2595 * we're not currently serving. Otherwise a call that just reads
2596 * could block a write call forever (if there are no incoming
2600 kdbus_do_iteration (DBusTransport *transport,
2602 int timeout_milliseconds)
2604 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
2609 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
2610 flags & DBUS_ITERATION_DO_READING ? "read" : "",
2611 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
2612 timeout_milliseconds,
2613 kdbus_transport->read_watch,
2614 kdbus_transport->write_watch,
2615 kdbus_transport->fd);
2617 /* the passed in DO_READING/DO_WRITING flags indicate whether to
2618 * read/write messages, but regardless of those we may need to block
2619 * for reading/writing to do auth. But if we do reading for auth,
2620 * we don't want to read any messages yet if not given DO_READING.
2623 poll_fd.fd = kdbus_transport->fd;
2626 if (_dbus_transport_try_to_authenticate (transport))
2628 /* This is kind of a hack; if we have stuff to write, then try
2629 * to avoid the poll. This is probably about a 5% speedup on an
2630 * echo client/server.
2632 * If both reading and writing were requested, we want to avoid this
2633 * since it could have funky effects:
2634 * - both ends spinning waiting for the other one to read
2635 * data so they can finish writing
2636 * - prioritizing all writing ahead of reading
2638 if ((flags & DBUS_ITERATION_DO_WRITING) &&
2639 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
2640 !transport->disconnected &&
2641 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
2643 do_writing (transport);
2645 if (transport->disconnected ||
2646 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
2650 /* If we get here, we decided to do the poll() after all */
2651 _dbus_assert (kdbus_transport->read_watch);
2652 if (flags & DBUS_ITERATION_DO_READING)
2653 poll_fd.events |= _DBUS_POLLIN;
2655 _dbus_assert (kdbus_transport->write_watch);
2656 if (flags & DBUS_ITERATION_DO_WRITING)
2657 poll_fd.events |= _DBUS_POLLOUT;
2661 DBusAuthState auth_state;
2663 auth_state = _dbus_auth_do_work (transport->auth);
2665 if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
2666 poll_fd.events |= _DBUS_POLLIN;
2668 if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
2669 poll_fd.events |= _DBUS_POLLOUT;
2674 if (flags & DBUS_ITERATION_BLOCK)
2675 poll_timeout = timeout_milliseconds;
2679 /* For blocking selects we drop the connection lock here
2680 * to avoid blocking out connection access during a potentially
2681 * indefinite blocking call. The io path is still protected
2682 * by the io_path_cond condvar, so we won't reenter this.
2684 if (flags & DBUS_ITERATION_BLOCK)
2686 _dbus_verbose ("unlock pre poll\n");
2687 _dbus_connection_unlock (transport->connection);
2691 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
2693 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
2695 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
2699 if (flags & DBUS_ITERATION_BLOCK)
2701 _dbus_verbose ("lock post poll\n");
2702 _dbus_connection_lock (transport->connection);
2708 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
2709 * valgrind flags it as an error. though it probably
2710 * is guaranteed on linux at least.
2713 if (poll_fd.revents & _DBUS_POLLERR)
2714 do_io_error (transport);
2717 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
2718 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
2719 #ifdef DBUS_AUTHENTICATION
2720 dbus_bool_t authentication_completed;
2723 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
2724 need_read, need_write);
2725 #ifdef DBUS_AUTHENTICATION
2726 do_authentication (transport, need_read, need_write,
2727 &authentication_completed);
2729 /* See comment in socket_handle_watch. */
2730 if (authentication_completed)
2733 if (need_read && (flags & DBUS_ITERATION_DO_READING))
2734 do_reading (transport);
2735 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
2736 do_writing (transport);
2740 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
2744 /* We need to install the write watch only if we did not
2745 * successfully write everything. Note we need to be careful that we
2746 * don't call check_write_watch *before* do_writing, since it's
2747 * inefficient to add the write watch, and we can avoid it most of
2748 * the time since we can write immediately.
2750 * However, we MUST always call check_write_watch(); DBusConnection code
2751 * relies on the fact that running an iteration will notice that
2752 * messages are pending.
2754 check_write_watch (transport);
2756 _dbus_verbose (" ... leaving do_iteration()\n");
2760 * copy-paste from socket transport with needed renames only.
2763 kdbus_live_messages_changed (DBusTransport *transport)
2765 /* See if we should look for incoming messages again */
2766 check_read_watch (transport);
2769 static const DBusTransportVTable kdbus_vtable = {
2773 kdbus_connection_set,
2775 kdbus_live_messages_changed,
2780 * Creates a new transport for the given kdbus file descriptor. The file
2781 * descriptor must be nonblocking.
2783 * @param fd the file descriptor.
2784 * @param address the transport's address
2785 * @returns the new transport, or #NULL if no memory.
2787 static DBusTransport*
2788 _dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
2790 DBusTransportKdbus *kdbus_transport;
2792 kdbus_transport = dbus_new0 (DBusTransportKdbus, 1);
2793 if (kdbus_transport == NULL)
2796 if (!_dbus_string_init (&kdbus_transport->encoded_outgoing))
2799 if (!_dbus_string_init (&kdbus_transport->encoded_incoming))
2802 kdbus_transport->write_watch = _dbus_watch_new (fd,
2803 DBUS_WATCH_WRITABLE,
2806 if (kdbus_transport->write_watch == NULL)
2809 kdbus_transport->read_watch = _dbus_watch_new (fd,
2810 DBUS_WATCH_READABLE,
2813 if (kdbus_transport->read_watch == NULL)
2816 if (!_dbus_transport_init_base (&kdbus_transport->base,
2821 #ifdef DBUS_AUTHENTICATION
2822 #ifdef HAVE_UNIX_FD_PASSING
2823 _dbus_auth_set_unix_fd_possible(kdbus_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
2827 kdbus_transport->fd = fd;
2828 kdbus_transport->message_bytes_written = 0;
2830 /* These values should probably be tunable or something. */
2831 kdbus_transport->max_bytes_read_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
2832 kdbus_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
2834 kdbus_transport->kdbus_mmap_ptr = NULL;
2835 kdbus_transport->memfd = -1;
2837 return (DBusTransport*) kdbus_transport;
2840 _dbus_watch_invalidate (kdbus_transport->read_watch);
2841 _dbus_watch_unref (kdbus_transport->read_watch);
2843 _dbus_watch_invalidate (kdbus_transport->write_watch);
2844 _dbus_watch_unref (kdbus_transport->write_watch);
2846 _dbus_string_free (&kdbus_transport->encoded_incoming);
2848 _dbus_string_free (&kdbus_transport->encoded_outgoing);
2850 dbus_free (kdbus_transport);
2855 * Opens a connection to the kdbus bus
2857 * This will set FD_CLOEXEC for the socket returned.
2859 * @param path the path to UNIX domain socket
2860 * @param error return location for error code
2861 * @returns connection file descriptor or -1 on error
2863 static int _dbus_connect_kdbus (const char *path, DBusError *error)
2867 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2868 _dbus_verbose ("connecting to kdbus bus %s\n", path);
2870 fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
2872 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
2878 * Creates a new transport for kdbus.
2879 * This creates a client-side of a transport.
2881 * @param path the path to the bus.
2882 * @param error address where an error can be returned.
2883 * @returns a new transport, or #NULL on failure.
2885 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
2888 DBusTransport *transport;
2891 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2893 if (!_dbus_string_init (&address))
2895 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2901 if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
2903 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2907 fd = _dbus_connect_kdbus (path, error);
2910 _DBUS_ASSERT_ERROR_IS_SET (error);
2914 _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
2916 transport = _dbus_transport_new_kdbus_transport (fd, &address);
2917 if (transport == NULL)
2919 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2923 _dbus_string_free (&address);
2928 _dbus_close_socket (fd, NULL);
2930 _dbus_string_free (&address);
2936 * Opens kdbus transport if method from address entry is kdbus
2938 * @param entry the address entry to try opening
2939 * @param transport_p return location for the opened transport
2940 * @param error error to be set
2941 * @returns result of the attempt
2943 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry,
2944 DBusTransport **transport_p,
2949 method = dbus_address_entry_get_method (entry);
2950 _dbus_assert (method != NULL);
2952 if (strcmp (method, "kdbus") == 0)
2954 const char *path = dbus_address_entry_get_value (entry, "path");
2958 _dbus_set_bad_address (error, "kdbus", "path", NULL);
2959 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
2962 *transport_p = _dbus_transport_new_for_kdbus (path, error);
2964 if (*transport_p == NULL)
2966 _DBUS_ASSERT_ERROR_IS_SET (error);
2967 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
2971 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2972 return DBUS_TRANSPORT_OPEN_OK;
2977 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2978 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;