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 socket file descriptor transport.
75 typedef struct DBusTransportSocket DBusTransportSocket;
78 * Implementation details of DBusTransportSocket. All members are private.
80 struct DBusTransportSocket
82 DBusTransport base; /**< Parent instance */
83 int fd; /**< File descriptor. */
84 DBusWatch *read_watch; /**< Watch for readability. */
85 DBusWatch *write_watch; /**< Watch for writability. */
87 int max_bytes_read_per_iteration; /**< In kdbus only to control overall message size*/
88 int max_bytes_written_per_iteration; /**< In kdbus only to control overall message size*/
90 int message_bytes_written; /**< Number of bytes of current
91 * outgoing message that have
94 DBusString encoded_outgoing; /**< Encoded version of current
97 DBusString encoded_incoming; /**< Encoded version of current
100 void* kdbus_mmap_ptr; /**< Mapped memory where Kdbus (kernel) writes
101 * messages incoming to us.
103 int memfd; /**< File descriptor to special
104 * memory pool for bulk data
105 * transfer. Retrieved from
106 * Kdbus kernel module.
108 __u64 bloom_size; /**< bloom filter field size */
109 char* sender; /**< uniqe name of the sender */
113 socket_get_socket_fd (DBusTransport *transport,
116 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
118 *fd_p = socket_transport->fd;
124 * Adds locally generated message to received messages queue
127 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* connection)
129 DBusList *message_link;
131 message_link = _dbus_list_alloc_link (message);
132 if (message_link == NULL)
134 dbus_message_unref (message);
138 _dbus_connection_queue_synthesized_message_link(connection, message_link);
143 static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
145 DBusMessage *errMessage;
146 char* error_msg = "";
150 error_msg = alloca(strlen(template) + strlen(object));
151 sprintf(error_msg, template, object);
154 error_msg = (char*)object;
156 errMessage = generate_local_error_message(dbus_message_get_serial(message), error_type, error_msg);
157 if(errMessage == NULL)
159 if (add_message_to_received(errMessage, connection))
165 static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
167 DBusMessageIter args;
170 reply = dbus_message_new_method_return(message);
173 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
174 dbus_message_iter_init_append(reply, &args);
175 if (!dbus_message_iter_append_basic(&args, data_type, pData))
177 dbus_message_unref(reply);
180 if(add_message_to_received(reply, connection))
187 static int reply_ack(DBusMessage *message, DBusConnection* connection)
191 reply = dbus_message_new_method_return(message);
194 if(add_message_to_received(reply, connection))
200 * Retrieves file descriptor to memory pool from kdbus module.
201 * It is then used for bulk data sending.
202 * Triggered when message payload is over MEMFD_SIZE_THRESHOLD
205 static int kdbus_init_memfd(DBusTransportSocket* socket_transport)
209 if (ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) {
210 _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
214 socket_transport->memfd = memfd;
215 _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd);
220 * Initiates Kdbus message structure.
221 * Calculates it's size, allocates memory and fills some fields.
222 * @param name Well-known name or NULL
223 * @param dst_id Numeric id of recipient
224 * @param body_size size of message body if present
225 * @param use_memfd flag to build memfd message
226 * @param fds_count number of file descriptors used
227 * @param transport transport
228 * @return initialized kdbus message
230 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, DBusTransportSocket *transport)
232 struct kdbus_msg* msg;
235 msg_size = sizeof(struct kdbus_msg);
237 if(use_memfd == TRUE) // bulk data - memfd - encoded and plain
238 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
240 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
242 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
246 msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
249 msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
250 else if (dst_id == KDBUS_DST_ID_BROADCAST)
251 msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
253 msg = malloc(msg_size);
256 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
260 memset(msg, 0, msg_size);
261 msg->size = msg_size;
262 msg->payload_type = KDBUS_PAYLOAD_DBUS1;
263 msg->dst_id = name ? 0 : dst_id;
264 msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
270 * Builds and sends kdbus message using Dbus message.
271 * Decide whether used payload vector or memfd memory pool.
272 * Handles broadcasts and unicast messages, and passing of Unix fds.
273 * Does error handling.
274 * TODO refactor to be more compact
276 * @param transport transport
277 * @param message DBus message to be sent
278 * @param encoded flag if the message is encoded
279 * @return size of data sent
281 static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message, const char* destination, dbus_bool_t encoded)
283 struct kdbus_msg *msg;
284 struct kdbus_item *item;
285 uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
286 const DBusString *header;
287 const DBusString *body;
288 uint64_t ret_size = 0;
289 uint64_t body_size = 0;
290 uint64_t header_size = 0;
291 dbus_bool_t use_memfd;
294 dbus_bool_t autostart;
296 // determine destination and destination id
299 dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
300 if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
302 dst_id = strtoull(&destination[3], NULL, 10);
309 ret_size = _dbus_string_get_length (&transport->encoded_outgoing);
312 _dbus_message_get_network_data (message, &header, &body);
313 header_size = _dbus_string_get_length(header);
314 body_size = _dbus_string_get_length(body);
315 ret_size = header_size + body_size;
318 // check if message size is big enough to use memfd kdbus transport
319 use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
320 if(use_memfd) kdbus_init_memfd(transport);
322 _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
324 // init basic message fields
325 msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport);
326 msg->cookie = dbus_message_get_serial(message);
327 autostart = dbus_message_get_auto_start (message);
329 msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START;
331 // build message contents
334 // case 1 - bulk data transfer - memfd - encoded and plain
339 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
341 _dbus_verbose("memfd sealing failed: \n");
345 buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
346 if (buf == MAP_FAILED)
348 _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
353 memcpy(buf, &transport->encoded_outgoing, ret_size);
356 memcpy(buf, _dbus_string_get_const_data(header), header_size);
359 memcpy(buf, _dbus_string_get_const_data(body), body_size);
364 munmap(buf, ret_size);
366 // seal data - kdbus module needs it
367 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) {
368 _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
373 item->type = KDBUS_MSG_PAYLOAD_MEMFD;
374 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
375 item->memfd.size = ret_size;
376 item->memfd.fd = transport->memfd;
377 // case 2 - small encoded - don't use memfd
379 _dbus_verbose("sending encoded data\n");
380 MSG_ITEM_BUILD_VEC(&transport->encoded_outgoing, _dbus_string_get_length (&transport->encoded_outgoing));
382 // case 3 - small not encoded - don't use memfd
384 _dbus_verbose("sending normal vector data\n");
385 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size);
389 _dbus_verbose("body attaching\n");
390 item = KDBUS_PART_NEXT(item);
391 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(body), body_size);
397 item = KDBUS_PART_NEXT(item);
398 item->type = KDBUS_MSG_FDS;
399 item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
400 memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
405 item = KDBUS_PART_NEXT(item);
406 item->type = KDBUS_MSG_DST_NAME;
407 item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1;
408 memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE);
410 else if (dst_id == KDBUS_DST_ID_BROADCAST)
412 item = KDBUS_PART_NEXT(item);
413 item->type = KDBUS_MSG_BLOOM;
414 item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
415 strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
419 if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
423 else if(errno == ENXIO) //no such id on the bus
425 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
428 else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus
432 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))
436 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
439 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
445 close(transport->memfd);
451 * Performs kdbus query of id of the given name
453 * @param name name to query for
455 * @param ownerID place to store id of the name
456 * @return 0 on success, -errno if failed
458 int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
460 struct kdbus_cmd_name_info *msg;
461 struct kdbus_item *item;
466 pInfo->sec_label_len = 0;
467 pInfo->sec_label = NULL;
469 item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
470 item_size = (item_size < 56) ? 56 : item_size; //at least 56 bytes are needed by kernel to place info about name, otherwise error
471 size = sizeof(struct kdbus_cmd_name_info) + item_size;
476 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
480 memset(msg, 0, size);
482 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
483 msg->id = strtoull(&name[3], NULL, 10);
488 item->type = KDBUS_NAME_INFO_ITEM_NAME;
489 item->size = item_size;
490 memcpy(item->str, name, strlen(name) + 1);
493 ret = ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_QUERY, msg);
500 else if(ret == -ENOBUFS)
502 msg = realloc(msg, msg->size); //prepare memory
511 pInfo->uniqueId = msg->id;
512 pInfo->userId = msg->creds.uid;
513 pInfo->processId = msg->creds.pid;
515 while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
517 if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
519 pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
520 if(pInfo->sec_label_len != 0)
522 pInfo->sec_label = malloc(pInfo->sec_label_len);
523 if(pInfo->sec_label == NULL)
526 memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
530 item = KDBUS_PART_NEXT(item);
539 * Kdbus part of dbus_bus_register.
540 * Shouldn't be used separately because it needs to be surrounded
541 * by other functions as it is done in dbus_bus_register.
543 * @param name place to store unique name given by bus
544 * @param connection the connection
545 * @param error place to store errors
546 * @returns #TRUE on success
548 static dbus_bool_t bus_register_kdbus(char* name, DBusTransportSocket* transportS)
550 struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
552 hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
553 KDBUS_HELLO_ATTACH_COMM |
554 KDBUS_HELLO_ATTACH_EXE |
555 KDBUS_HELLO_ATTACH_CMDLINE |
556 KDBUS_HELLO_ATTACH_CAPS |
557 KDBUS_HELLO_ATTACH_CGROUP |
558 KDBUS_HELLO_ATTACH_SECLABEL |
559 KDBUS_HELLO_ATTACH_AUDIT*/;
560 hello.size = sizeof(struct kdbus_cmd_hello);
561 hello.pool_size = RECEIVE_POOL_SIZE;
563 if (ioctl(transportS->fd, KDBUS_CMD_HELLO, &hello))
565 _dbus_verbose ("Failed to send hello: %m, %d",errno);
569 sprintf(name, "%llu", (unsigned long long)hello.id);
570 _dbus_verbose("-- Our peer ID is: %s\n", name);
571 transportS->bloom_size = hello.bloom_size;
573 transportS->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, transportS->fd, 0);
574 if (transportS->kdbus_mmap_ptr == MAP_FAILED)
576 _dbus_verbose("Error when mmap: %m, %d",errno);
584 * Seeks key in rule string, and duplicates value of the key into pValue.
585 * If value is "org.freedesktop.DBus" it is indicated by returning -1, because it
586 * needs to be handled in different manner.
587 * Value is duplicated from rule string to newly allocated memory pointe by pValue,
588 * so it must be freed after use.
590 * @param rule rule to look through
591 * @param key key to look for
592 * @param pValue pointer to value of the key found
593 * @return length of the value string, 0 means not found, -1 means "org.freedesktop.DBus"
595 static int parse_match_key(const char *rule, const char* key, char** pValue)
598 const char* pValueEnd;
599 int value_length = 0;
601 pBegin = strstr(rule, key);
604 pBegin += strlen(key);
605 pValueEnd = strchr(pBegin, '\'');
608 value_length = pValueEnd - pBegin;
609 *pValue = strndup(pBegin, value_length);
612 if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
614 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
622 * Adds a match rule to match broadcast messages going through the message bus.
623 * Do no affect messages addressed directly.
625 * The "rule" argument is the string form of a match rule.
627 * In kdbus function do not blocks.
629 * Upper function returns nothing because of blocking issues
630 * so there is no point to return true or false here.
632 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
634 * interface match key mapped to bloom
635 * sender match key mapped to src_name
636 * also handled org.freedesktop.dbus members: NameOwnerChanged, NameLost, NameAcquired
638 * @param connection connection to the message bus
639 * @param rule textual form of match rule
640 * @param error location to store any errors - may be NULL
642 dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
644 struct kdbus_cmd_match* pCmd_match;
645 struct kdbus_item *pItem;
646 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
648 unsigned int kernel_item = 0;
651 char* pInterface = NULL;
652 dbus_bool_t ret_value = FALSE;
653 DBusTransportSocket* transportS = (DBusTransportSocket*)transport;
655 /*parsing rule and calculating size of command*/
656 size = sizeof(struct kdbus_cmd_match);
658 if(strstr(rule, "member='NameOwnerChanged'"))
661 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /*std DBus: 3 name related items plus 2 id related items*/
663 else if(strstr(rule, "member='NameChanged'"))
665 kernel_item = KDBUS_MATCH_NAME_CHANGE;
666 size += KDBUS_ITEM_SIZE(1);
668 else if(strstr(rule, "member='NameLost'"))
670 kernel_item = KDBUS_MATCH_NAME_REMOVE;
671 size += KDBUS_ITEM_SIZE(1);
673 else if(strstr(rule, "member='NameAcquired'"))
675 kernel_item = KDBUS_MATCH_NAME_ADD;
676 size += KDBUS_ITEM_SIZE(1);
678 else if(strstr(rule, "member='IdAdded'"))
680 kernel_item = KDBUS_MATCH_ID_ADD;
681 size += KDBUS_ITEM_SIZE(sizeof(__u64));
683 else if(strstr(rule, "member='IdRemoved'"))
685 kernel_item = KDBUS_MATCH_ID_REMOVE;
686 size += KDBUS_ITEM_SIZE(sizeof(__u64));
689 name_size = parse_match_key(rule, "interface='", &pInterface);
690 if((name_size == -1) && (kernel_item == 0)) //means org.freedesktop.DBus without specified member
693 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /* 3 above name related items plus 2 id related items*/
695 else if(name_size > 0) /*actual size is not important for interface because bloom size is defined by bus*/
696 size += KDBUS_PART_HEADER_SIZE + transportS->bloom_size;
698 name_size = parse_match_key(rule, "sender='", &pName);
699 if((name_size == -1) && (kernel_item == 0)) //means org.freedesktop.DBus without specified name - same as interface few line above
702 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /* 3 above name related items plus 2 id related items*/
704 else if(name_size > 0)
706 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
708 src_id = strtoull(&pName[3], NULL, 10);
713 size += KDBUS_ITEM_SIZE(name_size + 1); //well known name
716 pCmd_match = alloca(size);
717 if(pCmd_match == NULL)
721 pCmd_match->cookie = id;
722 pCmd_match->size = size;
724 pItem = pCmd_match->items;
725 if(kernel_item == ~0) //all signals from kernel
727 pCmd_match->src_id = 0;
728 pItem->type = KDBUS_MATCH_NAME_CHANGE;
729 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
730 pItem = KDBUS_PART_NEXT(pItem);
731 pItem->type = KDBUS_MATCH_NAME_ADD;
732 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
733 pItem = KDBUS_PART_NEXT(pItem);
734 pItem->type = KDBUS_MATCH_NAME_REMOVE;
735 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
736 pItem = KDBUS_PART_NEXT(pItem);
737 pItem->type = KDBUS_MATCH_ID_ADD;
738 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
739 pItem = KDBUS_PART_NEXT(pItem);
740 pItem->type = KDBUS_MATCH_ID_REMOVE;
741 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
743 else if(kernel_item) //only one item
745 pCmd_match->src_id = 0;
746 pItem->type = kernel_item;
747 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
751 pCmd_match->src_id = src_id;
754 pItem->type = KDBUS_MATCH_SRC_NAME;
755 pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
756 memcpy(pItem->str, pName, strlen(pName) + 1);
757 pItem = KDBUS_PART_NEXT(pItem);
762 pItem->type = KDBUS_MATCH_BLOOM;
763 pItem->size = KDBUS_PART_HEADER_SIZE + transportS->bloom_size;
764 strncpy(pItem->data, pInterface, transportS->bloom_size);
768 if(ioctl(transportS->fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
769 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
772 _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
785 * Opposing to dbus, in kdbus removes all match rules with given
786 * cookie, which now is equal to uniqe id.
788 * In kdbus this function will not block
790 * @param connection connection to the message bus
791 * @param error location to store any errors - may be NULL
793 dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
795 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
796 DBusTransportSocket* transportS = (DBusTransportSocket*) 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 * !!! Not all methods are handled !!! Doubt if it is even possible.
819 * If method is not handled, returns error reply org.freedesktop.DBus.Error.UnknownMethod
826 * Not handled methods:
827 * - ListActivatableNames
828 * - StartServiceByName
829 * - UpdateActivationEnvironment
830 * - GetConnectionUnixUser
833 static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
835 #ifdef DBUS_SERVICES_IN_LIB
837 struct nameInfo info;
841 if(!strcmp(dbus_message_get_member(message), "Hello"))
846 name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + 1);
849 if(!bus_register_kdbus(name, (DBusTransportSocket*)transport))
851 if(!register_kdbus_policy(name, ((DBusTransportSocket*)transport)->fd))
854 sender = malloc (strlen(name) + 4);
857 sprintf(sender, ":1.%s", name);
858 ((DBusTransportSocket*)transport)->sender = sender;
860 if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
861 return 0; //todo why we cannot free name after sending reply, shouldn't we?
868 #ifdef DBUS_SERVICES_IN_LIB
869 else if(!strcmp(dbus_message_get_member(message), "RequestName"))
875 if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
878 if(!register_kdbus_policy(name, ((DBusTransportSocket*)transport)->fd))
881 result = request_kdbus_name(((DBusTransportSocket*)transport)->fd, name, flags, 0);
883 return reply_with_error(DBUS_ERROR_ACCESS_DENIED,
884 "Connection is not allowed to own the service \"%s\" due to security policies in the configuration file",
885 name, message, transport->connection);
887 return reply_with_error(DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name, message, transport->connection);
889 return reply_1_data(message, DBUS_TYPE_UINT32, &result, transport->connection);
891 else if(!strcmp(dbus_message_get_member(message), "AddMatch"))
895 if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &rule, DBUS_TYPE_INVALID))
898 if(!add_match_kdbus(transport, 0, rule))
901 return reply_ack(message,transport->connection);
903 else if(!strcmp(dbus_message_get_member(message), "RemoveMatch"))
905 if(!remove_match_kdbus(transport, 0))
907 return reply_ack(message, transport->connection);
909 else if(!strcmp(dbus_message_get_member(message), "GetNameOwner")) //returns id of the well known name
913 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
914 inter_ret = kdbus_NameQuery(name, transport, &info);
915 if(inter_ret == 0) //unique id of the name
917 char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
918 const char* pString = unique_name;
920 sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
921 _dbus_verbose("Unique name discovered:%s\n", unique_name);
922 ret_value = reply_1_data(message, DBUS_TYPE_STRING, &pString, transport->connection);
924 else if(inter_ret == -ENOENT) //name has no owner
925 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get owner of name '%s': no such name", name, message, transport->connection);
928 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
929 ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine unique name for '%s'", name, message, transport->connection);
932 else if(!strcmp(dbus_message_get_member(message), "NameHasOwner")) //returns if name is currently registered on the bus
937 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
938 inter_ret = kdbus_NameQuery(name, transport, &info);
939 if((inter_ret == 0) || (inter_ret == -ENOENT))
941 result = (inter_ret == 0) ? TRUE : FALSE;
942 ret_value = reply_1_data(message, DBUS_TYPE_BOOLEAN, &result, transport->connection);
946 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
947 ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name, message, transport->connection);
950 else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
954 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
955 inter_ret = kdbus_NameQuery(name, transport, &info);
956 if(inter_ret == 0) //name found
958 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
959 ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.userId, transport->connection);
961 else if(inter_ret == -ENOENT) //name has no owner
962 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get UID of name '%s': no such name", name, message, transport->connection);
965 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
966 ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name, message, transport->connection);
969 else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixProcessID"))
973 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
974 inter_ret = kdbus_NameQuery(name, transport, &info);
975 if(inter_ret == 0) //name found
976 ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.processId, transport->connection);
977 else if(inter_ret == -ENOENT) //name has no owner
978 return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get PID of name '%s': no such name", name, message, transport->connection);
981 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
982 ret_value = reply_with_error(DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,"Could not determine PID for '%s'", name, message, transport->connection);
985 else if(!strcmp(dbus_message_get_member(message), "ListNames")) //return all well known names on he bus
987 struct kdbus_cmd_names* pCmd;
990 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
991 pCmd = malloc(cmd_size);
994 pCmd->size = cmd_size;
998 if(ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_LIST, pCmd))
1002 if(errno == ENOBUFS) //buffer to small to put all names into it
1003 cmd_size = pCmd->size; //here kernel tells how much memory it needs
1006 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
1010 if(cmd_size) //kernel needs more memory
1012 pCmd = realloc(pCmd, cmd_size); //prepare memory
1015 goto again; //and try again
1020 DBusMessageIter iter, sub;
1021 struct kdbus_cmd_name* pCmd_name;
1024 reply = dbus_message_new_method_return(message);
1027 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
1028 dbus_message_iter_init_append(reply, &iter);
1029 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &sub))
1031 dbus_message_unref(reply);
1034 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
1036 pName = pCmd_name->name;
1037 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &pName))
1039 dbus_message_unref(reply);
1044 if (!dbus_message_iter_close_container (&iter, &sub))
1046 dbus_message_unref (reply);
1050 if(add_message_to_received(reply, transport->connection))
1058 else if(!strcmp(dbus_message_get_member(message), "GetId"))
1061 char uuid[DBUS_UUID_LENGTH_BYTES];
1064 DBusString binary, encoded;
1066 path = &transport->address[11]; //start of kdbus bus path
1067 if(stat(path, &stats) < -1)
1069 _dbus_verbose("kdbus error reading stats of bus: err %d (%m)\n", errno);
1070 return reply_with_error(DBUS_ERROR_FAILED, "Could not determine bus '%s' uuid", path, message, transport->connection);
1074 MD5_Update(&md5, path, strlen(path));
1075 MD5_Update(&md5, &stats.st_ctim.tv_sec, sizeof(stats.st_ctim.tv_sec));
1076 MD5_Final(uuid, &md5);
1078 if(!_dbus_string_init (&encoded))
1080 _dbus_string_init_const_len (&binary, uuid, DBUS_UUID_LENGTH_BYTES);
1081 if(!_dbus_string_hex_encode (&binary, 0, &encoded, _dbus_string_get_length (&encoded)))
1083 path = (char*)_dbus_string_get_const_data (&encoded);
1084 ret_value = reply_1_data(message, DBUS_TYPE_STRING, &path, transport->connection);
1087 _dbus_string_free(&binary);
1088 _dbus_string_free(&encoded);
1093 /* else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData")) //todo to be implemented if needed and possible
1097 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1098 return reply_with_error(DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN, "Could not determine audit session data for '%s'", name, message, transport->connection);
1100 #ifdef DBUS_SERVICES_IN_LIB
1101 else if(!strcmp(dbus_message_get_member(message), "GetConnectionSELinuxSecurityContext"))
1105 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1106 inter_ret = kdbus_NameQuery(name, transport, &info);
1107 if(inter_ret == -ENOENT) //name has no owner
1108 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);
1109 else if(inter_ret < 0)
1110 return reply_with_error(DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN, "Could not determine security context for '%s'", name, message, transport->connection);
1115 reply = dbus_message_new_method_return(message);
1118 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
1119 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
1120 dbus_message_unref(reply);
1121 else if(add_message_to_received(reply, transport->connection))
1128 return 1; //send to daemon
1130 #ifdef DBUS_SERVICES_IN_LIB
1132 free(info.sec_label);
1137 #if KDBUS_MSG_DECODE_DEBUG == 1
1138 static char *msg_id(uint64_t id, char *buf)
1144 sprintf(buf, "%llu", (unsigned long long)id);
1148 struct kdbus_enum_table {
1152 #define _STRINGIFY(x) #x
1153 #define STRINGIFY(x) _STRINGIFY(x)
1154 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
1155 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
1156 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
1157 #define LOOKUP(what) \
1158 const char *enum_##what(long long id) { \
1160 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
1161 if (id == kdbus_table_##what[i].id) \
1162 return kdbus_table_##what[i].name; \
1165 const char *enum_MSG(long long id);
1167 ENUM(_KDBUS_MSG_NULL),
1168 ENUM(KDBUS_MSG_PAYLOAD_VEC),
1169 ENUM(KDBUS_MSG_PAYLOAD_OFF),
1170 ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
1171 ENUM(KDBUS_MSG_FDS),
1172 ENUM(KDBUS_MSG_BLOOM),
1173 ENUM(KDBUS_MSG_DST_NAME),
1174 ENUM(KDBUS_MSG_SRC_CREDS),
1175 ENUM(KDBUS_MSG_SRC_PID_COMM),
1176 ENUM(KDBUS_MSG_SRC_TID_COMM),
1177 ENUM(KDBUS_MSG_SRC_EXE),
1178 ENUM(KDBUS_MSG_SRC_CMDLINE),
1179 ENUM(KDBUS_MSG_SRC_CGROUP),
1180 ENUM(KDBUS_MSG_SRC_CAPS),
1181 ENUM(KDBUS_MSG_SRC_SECLABEL),
1182 ENUM(KDBUS_MSG_SRC_AUDIT),
1183 ENUM(KDBUS_MSG_SRC_NAMES),
1184 ENUM(KDBUS_MSG_TIMESTAMP),
1185 ENUM(KDBUS_MSG_NAME_ADD),
1186 ENUM(KDBUS_MSG_NAME_REMOVE),
1187 ENUM(KDBUS_MSG_NAME_CHANGE),
1188 ENUM(KDBUS_MSG_ID_ADD),
1189 ENUM(KDBUS_MSG_ID_REMOVE),
1190 ENUM(KDBUS_MSG_REPLY_TIMEOUT),
1191 ENUM(KDBUS_MSG_REPLY_DEAD),
1194 const char *enum_PAYLOAD(long long id);
1196 ENUM(KDBUS_PAYLOAD_KERNEL),
1197 ENUM(KDBUS_PAYLOAD_DBUS1),
1198 ENUM(KDBUS_PAYLOAD_GVARIANT),
1203 * Puts locally generated message into received data buffer.
1204 * Use only during receiving phase!
1206 * @param message message to load
1207 * @param data place to load message
1208 * @return size of message
1210 static int put_message_into_data(DBusMessage *message, char* data)
1213 const DBusString *header;
1214 const DBusString *body;
1217 dbus_message_set_serial(message, 1);
1218 dbus_message_lock (message);
1219 _dbus_message_get_network_data (message, &header, &body);
1220 ret_size = _dbus_string_get_length(header);
1221 memcpy(data, _dbus_string_get_const_data(header), ret_size);
1223 size = _dbus_string_get_length(body);
1224 memcpy(data, _dbus_string_get_const_data(body), size);
1231 * Get the length of the kdbus message content.
1233 * @param msg kdbus message
1234 * @return the length of the kdbus message
1236 static int kdbus_message_size(const struct kdbus_msg* msg)
1238 const struct kdbus_item *item;
1241 KDBUS_PART_FOREACH(item, msg, items)
1243 if (item->size <= KDBUS_PART_HEADER_SIZE)
1245 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
1250 case KDBUS_MSG_PAYLOAD_OFF:
1251 ret_size += item->vec.size;
1253 case KDBUS_MSG_PAYLOAD_MEMFD:
1254 ret_size += item->memfd.size;
1265 * Decodes kdbus message in order to extract dbus message and put it into data and fds.
1266 * Also captures and decodes kdbus error messages and kdbus kernel broadcasts and converts
1267 * all of them into dbus messages.
1269 * @param msg kdbus message
1270 * @param data place to copy dbus message to
1271 * @param socket_transport transport
1272 * @param fds place to store file descriptors received
1273 * @param n_fds place to store quantity of file descriptor
1274 * @return number of dbus message's bytes received or -1 on error
1276 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportSocket* socket_transport, int* fds, int* n_fds)
1278 const struct kdbus_item *item;
1280 DBusMessage *message = NULL;
1281 DBusMessageIter args;
1282 const char* emptyString = "";
1283 const char* pString = NULL;
1284 char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1285 const char* pDBusName = dbus_name;
1286 #if KDBUS_MSG_DECODE_DEBUG == 1
1290 #if KDBUS_MSG_DECODE_DEBUG == 1
1291 _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
1292 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
1293 (unsigned long long) msg->flags,
1294 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
1295 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
1300 KDBUS_PART_FOREACH(item, msg, items)
1302 if (item->size <= KDBUS_PART_HEADER_SIZE)
1304 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
1305 break; //??? continue (because dbus will find error) or break
1310 case KDBUS_MSG_PAYLOAD_OFF:
1311 memcpy(data, (char *)socket_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
1312 data += item->vec.size;
1313 ret_size += item->vec.size;
1315 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
1316 enum_MSG(item->type), item->size,
1317 (unsigned long long)item->vec.offset,
1318 (unsigned long long)item->vec.size);
1321 case KDBUS_MSG_PAYLOAD_MEMFD:
1326 size = item->memfd.size;
1327 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
1329 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
1330 if (buf == MAP_FAILED)
1332 _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
1336 memcpy(data, buf, size);
1342 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
1343 enum_MSG(item->type), item->size,
1344 (unsigned long long)item->vec.offset,
1345 (unsigned long long)item->vec.size);
1353 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
1354 memcpy(fds, item->fds, *n_fds * sizeof(int));
1355 for (i = 0; i < *n_fds; i++)
1356 _dbus_fd_set_close_on_exec(fds[i]);
1360 #if KDBUS_MSG_DECODE_DEBUG == 1
1361 case KDBUS_MSG_SRC_CREDS:
1362 _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
1363 enum_MSG(item->type), item->size,
1364 item->creds.uid, item->creds.gid,
1365 item->creds.pid, item->creds.tid,
1366 item->creds.starttime);
1369 case KDBUS_MSG_SRC_PID_COMM:
1370 case KDBUS_MSG_SRC_TID_COMM:
1371 case KDBUS_MSG_SRC_EXE:
1372 case KDBUS_MSG_SRC_CGROUP:
1373 case KDBUS_MSG_SRC_SECLABEL:
1374 case KDBUS_MSG_DST_NAME:
1375 _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
1376 enum_MSG(item->type), item->size, item->str, strlen(item->str));
1379 case KDBUS_MSG_SRC_CMDLINE:
1380 case KDBUS_MSG_SRC_NAMES: {
1381 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
1382 const char *str = item->str;
1385 _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
1387 _dbus_verbose("'%s' ", str);
1388 size -= strlen(str) + 1;
1389 str += strlen(str) + 1;
1393 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
1397 case KDBUS_MSG_SRC_AUDIT:
1398 _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
1399 enum_MSG(item->type), item->size,
1400 (unsigned long long)item->data64[0],
1401 (unsigned long long)item->data64[1]);
1404 case KDBUS_MSG_SRC_CAPS: {
1406 const uint32_t *cap;
1409 _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
1410 enum_MSG(item->type), item->size,
1411 (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
1414 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
1416 _dbus_verbose(" CapInh=");
1417 for (i = 0; i < n; i++)
1418 _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
1420 _dbus_verbose(" CapPrm=");
1421 for (i = 0; i < n; i++)
1422 _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
1424 _dbus_verbose(" CapEff=");
1425 for (i = 0; i < n; i++)
1426 _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
1428 _dbus_verbose(" CapInh=");
1429 for (i = 0; i < n; i++)
1430 _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
1431 _dbus_verbose("\n");
1435 case KDBUS_MSG_TIMESTAMP:
1436 _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
1437 enum_MSG(item->type), item->size,
1438 (unsigned long long)item->timestamp.realtime_ns,
1439 (unsigned long long)item->timestamp.monotonic_ns);
1443 case KDBUS_MSG_REPLY_TIMEOUT:
1444 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
1445 enum_MSG(item->type), item->size, msg->cookie_reply);
1447 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
1454 ret_size = put_message_into_data(message, data);
1457 case KDBUS_MSG_REPLY_DEAD:
1458 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
1459 enum_MSG(item->type), item->size, msg->cookie_reply);
1461 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
1468 ret_size = put_message_into_data(message, data);
1471 case KDBUS_MSG_NAME_ADD:
1472 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
1473 enum_MSG(item->type), (unsigned long long) item->size,
1474 item->name_change.name, item->name_change.old_id,
1475 item->name_change.new_id, item->name_change.flags);
1477 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1484 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
1485 pString = item->name_change.name;
1486 _dbus_verbose ("Name added: %s\n", pString);
1487 dbus_message_iter_init_append(message, &args);
1488 ITER_APPEND_STR(pString)
1489 ITER_APPEND_STR(emptyString)
1490 ITER_APPEND_STR(pDBusName)
1491 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1493 ret_size = put_message_into_data(message, data);
1496 case KDBUS_MSG_NAME_REMOVE:
1497 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
1498 enum_MSG(item->type), (unsigned long long) item->size,
1499 item->name_change.name, item->name_change.old_id,
1500 item->name_change.new_id, item->name_change.flags);
1502 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
1509 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
1510 pString = item->name_change.name;
1511 _dbus_verbose ("Name removed: %s\n", pString);
1512 dbus_message_iter_init_append(message, &args);
1513 ITER_APPEND_STR(pString)
1514 ITER_APPEND_STR(pDBusName)
1515 ITER_APPEND_STR(emptyString)
1516 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1518 ret_size = put_message_into_data(message, data);
1521 case KDBUS_MSG_NAME_CHANGE:
1522 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
1523 enum_MSG(item->type), (unsigned long long) item->size,
1524 item->name_change.name, item->name_change.old_id,
1525 item->name_change.new_id, item->name_change.flags);
1527 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1534 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
1535 pString = item->name_change.name;
1536 _dbus_verbose ("Name changed: %s\n", pString);
1537 dbus_message_iter_init_append(message, &args);
1538 ITER_APPEND_STR(pString)
1539 ITER_APPEND_STR(pDBusName)
1540 sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
1541 _dbus_verbose ("New id: %s\n", pDBusName);
1542 ITER_APPEND_STR(pDBusName)
1543 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1545 ret_size = put_message_into_data(message, data);
1548 case KDBUS_MSG_ID_ADD:
1549 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1550 enum_MSG(item->type), (unsigned long long) item->size,
1551 (unsigned long long) item->id_change.id,
1552 (unsigned long long) item->id_change.flags);
1554 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1561 sprintf(dbus_name,":1.%llu",item->id_change.id);
1562 dbus_message_iter_init_append(message, &args);
1563 ITER_APPEND_STR(pDBusName)
1564 ITER_APPEND_STR(emptyString)
1565 ITER_APPEND_STR(pDBusName)
1566 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1568 ret_size = put_message_into_data(message, data);
1571 case KDBUS_MSG_ID_REMOVE:
1572 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1573 enum_MSG(item->type), (unsigned long long) item->size,
1574 (unsigned long long) item->id_change.id,
1575 (unsigned long long) item->id_change.flags);
1577 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
1584 sprintf(dbus_name,":1.%llu",item->id_change.id);
1585 dbus_message_iter_init_append(message, &args);
1586 ITER_APPEND_STR(pDBusName)
1587 ITER_APPEND_STR(pDBusName)
1588 ITER_APPEND_STR(emptyString)
1589 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1591 ret_size = put_message_into_data(message, data);
1593 #if KDBUS_MSG_DECODE_DEBUG == 1
1595 _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
1601 #if KDBUS_MSG_DECODE_DEBUG == 1
1603 if ((char *)item - ((char *)msg + msg->size) >= 8)
1604 _dbus_verbose("invalid padding at end of message\n");
1609 dbus_message_unref(message);
1614 * Reads message from kdbus and puts it into dbus buffer and fds
1616 * @param transport transport
1617 * @param buffer place to copy received message to
1618 * @param fds place to store file descriptors sent in the message
1619 * @param n_fds place to store number of file descriptors
1620 * @return size of received message on success, -1 on error
1622 static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString *buffer, int* fds, int* n_fds)
1624 int ret_size, buf_size;
1625 uint64_t __attribute__ ((__aligned__(8))) offset;
1626 struct kdbus_msg *msg;
1630 start = _dbus_string_get_length (buffer);
1633 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1637 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1638 _dbus_string_set_length (buffer, start);
1642 msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
1644 buf_size = kdbus_message_size(msg);
1647 _dbus_verbose("kdbus error - too short message: %d (%m)\n", errno);
1651 /* What is the maximum size of the locally generated message?
1652 I just assume 2048 bytes */
1653 buf_size = MAX(buf_size, 2048);
1655 if (!_dbus_string_lengthen (buffer, buf_size))
1660 data = _dbus_string_get_data_len (buffer, start, buf_size);
1662 ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
1664 if(ret_size == -1) /* error */
1666 _dbus_string_set_length (buffer, start);
1669 else if (buf_size != ret_size) /* case of locally generated message */
1671 _dbus_string_set_length (buffer, start + ret_size);
1675 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
1679 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1687 free_watches (DBusTransport *transport)
1689 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1691 _dbus_verbose ("start\n");
1693 if (socket_transport->read_watch)
1695 if (transport->connection)
1696 _dbus_connection_remove_watch_unlocked (transport->connection,
1697 socket_transport->read_watch);
1698 _dbus_watch_invalidate (socket_transport->read_watch);
1699 _dbus_watch_unref (socket_transport->read_watch);
1700 socket_transport->read_watch = NULL;
1703 if (socket_transport->write_watch)
1705 if (transport->connection)
1706 _dbus_connection_remove_watch_unlocked (transport->connection,
1707 socket_transport->write_watch);
1708 _dbus_watch_invalidate (socket_transport->write_watch);
1709 _dbus_watch_unref (socket_transport->write_watch);
1710 socket_transport->write_watch = NULL;
1713 _dbus_verbose ("end\n");
1717 socket_finalize (DBusTransport *transport)
1719 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1721 _dbus_verbose ("\n");
1723 free_watches (transport);
1725 _dbus_string_free (&socket_transport->encoded_outgoing);
1726 _dbus_string_free (&socket_transport->encoded_incoming);
1728 _dbus_transport_finalize_base (transport);
1730 _dbus_assert (socket_transport->read_watch == NULL);
1731 _dbus_assert (socket_transport->write_watch == NULL);
1733 dbus_free (transport);
1737 check_write_watch (DBusTransport *transport)
1739 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1742 if (transport->connection == NULL)
1745 if (transport->disconnected)
1747 _dbus_assert (socket_transport->write_watch == NULL);
1751 _dbus_transport_ref (transport);
1753 #ifdef DBUS_AUTHENTICATION
1754 if (_dbus_transport_try_to_authenticate (transport))
1756 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1757 #ifdef DBUS_AUTHENTICATION
1760 if (transport->send_credentials_pending)
1764 DBusAuthState auth_state;
1766 auth_state = _dbus_auth_do_work (transport->auth);
1768 /* If we need memory we install the write watch just in case,
1769 * if there's no need for it, it will get de-installed
1770 * next time we try reading.
1772 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
1773 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1780 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1781 needed, transport->connection, socket_transport->write_watch,
1782 socket_transport->fd,
1783 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1785 _dbus_connection_toggle_watch_unlocked (transport->connection,
1786 socket_transport->write_watch,
1789 _dbus_transport_unref (transport);
1793 check_read_watch (DBusTransport *transport)
1795 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1796 dbus_bool_t need_read_watch;
1798 _dbus_verbose ("fd = %d\n",socket_transport->fd);
1800 if (transport->connection == NULL)
1803 if (transport->disconnected)
1805 _dbus_assert (socket_transport->read_watch == NULL);
1809 _dbus_transport_ref (transport);
1811 #ifdef DBUS_AUTHENTICATION
1812 if (_dbus_transport_try_to_authenticate (transport))
1815 (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1816 (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1817 #ifdef DBUS_AUTHENTICATION
1820 if (transport->receive_credentials_pending)
1821 need_read_watch = TRUE;
1824 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
1825 * is to avoid spinning on the file descriptor when we're waiting
1826 * to write or for some other part of the auth process
1828 DBusAuthState auth_state;
1830 auth_state = _dbus_auth_do_work (transport->auth);
1832 /* If we need memory we install the read watch just in case,
1833 * if there's no need for it, it will get de-installed
1834 * next time we try reading. If we're authenticated we
1835 * install it since we normally have it installed while
1838 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
1839 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
1840 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
1841 need_read_watch = TRUE;
1843 need_read_watch = FALSE;
1848 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
1849 _dbus_connection_toggle_watch_unlocked (transport->connection,
1850 socket_transport->read_watch,
1853 _dbus_transport_unref (transport);
1857 do_io_error (DBusTransport *transport)
1859 _dbus_transport_ref (transport);
1860 _dbus_transport_disconnect (transport);
1861 _dbus_transport_unref (transport);
1864 #ifdef DBUS_AUTHENTICATION
1865 /* return value is whether we successfully read any new data. */
1867 read_data_into_auth (DBusTransport *transport,
1870 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1877 _dbus_auth_get_buffer (transport->auth, &buffer);
1879 bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
1881 _dbus_auth_return_buffer (transport->auth, buffer,
1882 bytes_read > 0 ? bytes_read : 0);
1886 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
1889 else if (bytes_read < 0)
1891 /* EINTR already handled for us */
1893 if (_dbus_get_is_errno_enomem ())
1897 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1898 ; /* do nothing, just return FALSE below */
1901 _dbus_verbose ("Error reading from remote app: %s\n",
1902 _dbus_strerror_from_errno ());
1903 do_io_error (transport);
1910 _dbus_assert (bytes_read == 0);
1912 _dbus_verbose ("Disconnected from remote app\n");
1913 do_io_error (transport);
1919 static int kdbus_send_auth (DBusTransport *transport, const DBusString *buffer)
1922 int bytes_written = -1;
1923 struct kdbus_msg *msg;
1924 struct kdbus_item *item;
1926 len = _dbus_string_get_length (buffer);
1927 // data = _dbus_string_get_const_data_len (buffer, 0, len);
1929 msg = kdbus_init_msg(NULL, 1, 0, FALSE, 0, (DBusTransportSocket*)transport);
1931 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data_len (buffer, 0, len), len);
1934 if(ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_MSG_SEND, msg))
1938 _dbus_verbose ("Error writing auth: %d, %m\n", errno);
1941 bytes_written = len;
1943 return bytes_written;
1946 /* Return value is whether we successfully wrote any bytes */
1948 write_data_from_auth (DBusTransport *transport)
1950 // DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1952 const DBusString *buffer;
1954 if (!_dbus_auth_get_bytes_to_send (transport->auth,
1958 bytes_written = kdbus_send_auth (transport, buffer);
1960 if (bytes_written > 0)
1962 _dbus_auth_bytes_sent (transport->auth, bytes_written);
1965 else if (bytes_written < 0)
1967 /* EINTR already handled for us */
1969 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1973 _dbus_verbose ("Error writing to remote app: %s\n",
1974 _dbus_strerror_from_errno ());
1975 do_io_error (transport);
1984 exchange_credentials (DBusTransport *transport,
1985 dbus_bool_t do_reading,
1986 dbus_bool_t do_writing)
1988 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1989 DBusError error = DBUS_ERROR_INIT;
1991 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
1992 do_reading, do_writing);
1994 if (do_writing && transport->send_credentials_pending)
1996 if (_dbus_send_credentials_socket (socket_transport->fd,
1999 transport->send_credentials_pending = FALSE;
2003 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
2004 dbus_error_free (&error);
2005 do_io_error (transport);
2009 if (do_reading && transport->receive_credentials_pending)
2011 /* FIXME this can fail due to IO error _or_ OOM, broken
2012 * (somewhat tricky to fix since the OOM error can be set after
2013 * we already read the credentials byte, so basically we need to
2014 * separate reading the byte and storing it in the
2015 * transport->credentials). Does not really matter for now
2016 * because storing in credentials never actually fails on unix.
2018 if (_dbus_read_credentials_socket (socket_transport->fd,
2019 transport->credentials,
2022 transport->receive_credentials_pending = FALSE;
2026 _dbus_verbose ("Failed to read credentials %s\n", error.message);
2027 dbus_error_free (&error);
2028 do_io_error (transport);
2032 if (!(transport->send_credentials_pending ||
2033 transport->receive_credentials_pending))
2035 if (!_dbus_auth_set_credentials (transport->auth,
2036 transport->credentials))
2044 do_authentication (DBusTransport *transport,
2045 dbus_bool_t do_reading,
2046 dbus_bool_t do_writing,
2047 dbus_bool_t *auth_completed)
2050 dbus_bool_t orig_auth_state;
2054 orig_auth_state = _dbus_transport_try_to_authenticate (transport);
2056 /* This is essential to avoid the check_write_watch() at the end,
2057 * we don't want to add a write watch in do_iteration before
2058 * we try writing and get EAGAIN
2060 if (orig_auth_state)
2063 *auth_completed = FALSE;
2067 _dbus_transport_ref (transport);
2069 while (!_dbus_transport_try_to_authenticate (transport) &&
2070 _dbus_transport_get_is_connected (transport))
2072 if (!exchange_credentials (transport, do_reading, do_writing))
2078 if (transport->send_credentials_pending ||
2079 transport->receive_credentials_pending)
2081 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
2082 transport->send_credentials_pending,
2083 transport->receive_credentials_pending);
2087 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
2088 switch (_dbus_auth_do_work (transport->auth))
2090 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
2091 _dbus_verbose (" %s auth state: waiting for input\n",
2092 TRANSPORT_SIDE (transport));
2093 if (!do_reading || !read_data_into_auth (transport, &oom))
2097 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
2098 _dbus_verbose (" %s auth state: waiting for memory\n",
2099 TRANSPORT_SIDE (transport));
2104 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
2105 _dbus_verbose (" %s auth state: bytes to send\n",
2106 TRANSPORT_SIDE (transport));
2107 if (!do_writing || !write_data_from_auth (transport))
2111 case DBUS_AUTH_STATE_NEED_DISCONNECT:
2112 _dbus_verbose (" %s auth state: need to disconnect\n",
2113 TRANSPORT_SIDE (transport));
2114 do_io_error (transport);
2117 case DBUS_AUTH_STATE_AUTHENTICATED:
2118 _dbus_verbose (" %s auth state: authenticated\n",
2119 TRANSPORT_SIDE (transport));
2126 *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
2128 check_read_watch (transport);
2129 check_write_watch (transport);
2130 _dbus_transport_unref (transport);
2139 /* returns false on oom */
2141 do_writing (DBusTransport *transport)
2143 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2146 #ifdef DBUS_AUTHENTICATION
2147 /* No messages without authentication! */
2148 if (!_dbus_transport_try_to_authenticate (transport))
2150 _dbus_verbose ("Not authenticated, not writing anything\n");
2155 if (transport->disconnected)
2157 _dbus_verbose ("Not connected, not writing anything\n");
2162 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
2163 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
2164 socket_transport->fd);
2169 while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
2172 DBusMessage *message;
2173 const DBusString *header;
2174 const DBusString *body;
2175 int total_bytes_to_write;
2176 const char* pDestination;
2178 message = _dbus_connection_get_message_to_send (transport->connection);
2179 _dbus_assert (message != NULL);
2180 if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages
2182 dbus_message_unlock(message);
2183 dbus_message_set_sender(message, socket_transport->sender);
2184 dbus_message_lock (message);
2186 _dbus_message_get_network_data (message, &header, &body);
2187 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
2188 pDestination = dbus_message_get_destination(message);
2192 if(!strcmp(pDestination, DBUS_SERVICE_DBUS))
2194 if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
2198 ret = emulateOrgFreedesktopDBus(transport, message);
2206 bytes_written = total_bytes_to_write;
2209 //else send to "daemon" as to normal recipient
2213 if (_dbus_auth_needs_encoding (transport->auth))
2215 if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
2217 if (!_dbus_auth_encode_data (transport->auth,
2218 header, &socket_transport->encoded_outgoing))
2224 if (!_dbus_auth_encode_data (transport->auth,
2225 body, &socket_transport->encoded_outgoing))
2227 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
2233 total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
2234 if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
2237 bytes_written = kdbus_write_msg(socket_transport, message, pDestination, TRUE);
2241 if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
2244 bytes_written = kdbus_write_msg(socket_transport, message, pDestination, FALSE);
2248 if (bytes_written < 0)
2250 /* EINTR already handled for us */
2252 /* For some discussion of why we also ignore EPIPE here, see
2253 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
2256 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
2260 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
2261 do_io_error (transport);
2267 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
2268 total_bytes_to_write);
2270 socket_transport->message_bytes_written += bytes_written;
2272 _dbus_assert (socket_transport->message_bytes_written <=
2273 total_bytes_to_write);
2275 if (socket_transport->message_bytes_written == total_bytes_to_write)
2277 socket_transport->message_bytes_written = 0;
2278 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
2279 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
2281 _dbus_connection_message_sent_unlocked (transport->connection,
2293 /* returns false on out-of-memory */
2295 do_reading (DBusTransport *transport)
2297 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2300 dbus_bool_t oom = FALSE;
2303 _dbus_verbose ("fd = %d\n",socket_transport->fd);
2305 #ifdef DBUS_AUTHENTICATION
2306 /* No messages without authentication! */
2307 if (!_dbus_transport_try_to_authenticate (transport))
2313 /* See if we've exceeded max messages and need to disable reading */
2314 check_read_watch (transport);
2316 _dbus_assert (socket_transport->read_watch != NULL ||
2317 transport->disconnected);
2319 if (transport->disconnected)
2322 if (!dbus_watch_get_enabled (socket_transport->read_watch))
2325 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
2327 _dbus_verbose ("Out of memory reading file descriptors\n");
2331 _dbus_message_loader_get_buffer (transport->loader, &buffer);
2333 if (_dbus_auth_needs_decoding (transport->auth))
2335 bytes_read = kdbus_read_message(socket_transport, &socket_transport->encoded_incoming, fds, &n_fds);
2337 _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) == bytes_read);
2341 if (!_dbus_auth_decode_data (transport->auth,
2342 &socket_transport->encoded_incoming,
2345 _dbus_verbose ("Out of memory decoding incoming data\n");
2346 _dbus_message_loader_return_buffer (transport->loader,
2348 _dbus_string_get_length (buffer));
2353 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
2354 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
2358 bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
2360 if (bytes_read >= 0 && n_fds > 0)
2361 _dbus_verbose("Read %i unix fds\n", n_fds);
2363 _dbus_message_loader_return_buffer (transport->loader,
2365 bytes_read < 0 ? 0 : bytes_read);
2366 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
2370 /* EINTR already handled for us */
2372 if (_dbus_get_is_errno_enomem ())
2374 _dbus_verbose ("Out of memory in read()/do_reading()\n");
2378 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
2382 _dbus_verbose ("Error reading from remote app: %s\n",
2383 _dbus_strerror_from_errno ());
2384 do_io_error (transport);
2388 else if (bytes_read == 0)
2390 _dbus_verbose ("Disconnected from remote app\n");
2391 do_io_error (transport);
2396 _dbus_verbose (" read %d bytes\n", bytes_read);
2398 if (!_dbus_transport_queue_messages (transport))
2401 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
2405 /* Try reading more data until we get EAGAIN and return, or
2406 * exceed max bytes per iteration. If in blocking mode of
2407 * course we'll block instead of returning.
2419 unix_error_with_read_to_come (DBusTransport *itransport,
2423 DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
2425 if (!((flags & DBUS_WATCH_HANGUP) || (flags & DBUS_WATCH_ERROR)))
2428 /* If we have a read watch enabled ...
2429 we -might have data incoming ... => handle the HANGUP there */
2430 if (watch != transport->read_watch && _dbus_watch_get_enabled (transport->read_watch))
2437 socket_handle_watch (DBusTransport *transport,
2441 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2443 _dbus_assert (watch == socket_transport->read_watch ||
2444 watch == socket_transport->write_watch);
2445 _dbus_assert (watch != NULL);
2447 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
2448 * still be in the buffer and do_reading may need several iteration to read
2449 * it all (because of its max_bytes_read_per_iteration limit).
2451 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
2453 _dbus_verbose ("Hang up or error on watch\n");
2454 _dbus_transport_disconnect (transport);
2458 if (watch == socket_transport->read_watch &&
2459 (flags & DBUS_WATCH_READABLE))
2461 #ifdef DBUS_AUTHENTICATION
2462 dbus_bool_t auth_finished;
2465 _dbus_verbose ("handling read watch %p flags = %x\n",
2468 #ifdef DBUS_AUTHENTICATION
2469 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
2472 /* We don't want to do a read immediately following
2473 * a successful authentication. This is so we
2474 * have a chance to propagate the authentication
2475 * state further up. Specifically, we need to
2476 * process any pending data from the auth object.
2481 if (!do_reading (transport))
2483 _dbus_verbose ("no memory to read\n");
2486 #ifdef DBUS_AUTHENTICATION
2490 _dbus_verbose ("Not reading anything since we just completed the authentication\n");
2494 else if (watch == socket_transport->write_watch &&
2495 (flags & DBUS_WATCH_WRITABLE))
2498 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
2499 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
2501 #ifdef DBUS_AUTHENTICATION
2502 if (!do_authentication (transport, FALSE, TRUE, NULL))
2505 if (!do_writing (transport))
2507 _dbus_verbose ("no memory to write\n");
2511 /* See if we still need the write watch */
2512 check_write_watch (transport);
2514 #ifdef DBUS_ENABLE_VERBOSE_MODE
2517 if (watch == socket_transport->read_watch)
2518 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
2520 else if (watch == socket_transport->write_watch)
2521 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
2524 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
2525 watch, dbus_watch_get_socket (watch));
2527 #endif /* DBUS_ENABLE_VERBOSE_MODE */
2533 socket_disconnect (DBusTransport *transport)
2535 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2537 _dbus_verbose ("\n");
2539 free_watches (transport);
2541 _dbus_close_socket (socket_transport->fd, NULL);
2542 socket_transport->fd = -1;
2546 kdbus_connection_set (DBusTransport *transport)
2548 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2550 dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus
2552 _dbus_watch_set_handler (socket_transport->write_watch,
2553 _dbus_connection_handle_watch,
2554 transport->connection, NULL);
2556 _dbus_watch_set_handler (socket_transport->read_watch,
2557 _dbus_connection_handle_watch,
2558 transport->connection, NULL);
2560 if (!_dbus_connection_add_watch_unlocked (transport->connection,
2561 socket_transport->write_watch))
2564 if (!_dbus_connection_add_watch_unlocked (transport->connection,
2565 socket_transport->read_watch))
2567 _dbus_connection_remove_watch_unlocked (transport->connection,
2568 socket_transport->write_watch);
2572 check_read_watch (transport);
2573 check_write_watch (transport);
2578 /** original dbus copy-pasted
2579 * @todo We need to have a way to wake up the select sleep if
2580 * a new iteration request comes in with a flag (read/write) that
2581 * we're not currently serving. Otherwise a call that just reads
2582 * could block a write call forever (if there are no incoming
2586 kdbus_do_iteration (DBusTransport *transport,
2588 int timeout_milliseconds)
2590 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2595 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
2596 flags & DBUS_ITERATION_DO_READING ? "read" : "",
2597 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
2598 timeout_milliseconds,
2599 socket_transport->read_watch,
2600 socket_transport->write_watch,
2601 socket_transport->fd);
2603 /* the passed in DO_READING/DO_WRITING flags indicate whether to
2604 * read/write messages, but regardless of those we may need to block
2605 * for reading/writing to do auth. But if we do reading for auth,
2606 * we don't want to read any messages yet if not given DO_READING.
2609 poll_fd.fd = socket_transport->fd;
2612 if (_dbus_transport_try_to_authenticate (transport))
2614 /* This is kind of a hack; if we have stuff to write, then try
2615 * to avoid the poll. This is probably about a 5% speedup on an
2616 * echo client/server.
2618 * If both reading and writing were requested, we want to avoid this
2619 * since it could have funky effects:
2620 * - both ends spinning waiting for the other one to read
2621 * data so they can finish writing
2622 * - prioritizing all writing ahead of reading
2624 if ((flags & DBUS_ITERATION_DO_WRITING) &&
2625 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
2626 !transport->disconnected &&
2627 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
2629 do_writing (transport);
2631 if (transport->disconnected ||
2632 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
2636 /* If we get here, we decided to do the poll() after all */
2637 _dbus_assert (socket_transport->read_watch);
2638 if (flags & DBUS_ITERATION_DO_READING)
2639 poll_fd.events |= _DBUS_POLLIN;
2641 _dbus_assert (socket_transport->write_watch);
2642 if (flags & DBUS_ITERATION_DO_WRITING)
2643 poll_fd.events |= _DBUS_POLLOUT;
2647 DBusAuthState auth_state;
2649 auth_state = _dbus_auth_do_work (transport->auth);
2651 if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
2652 poll_fd.events |= _DBUS_POLLIN;
2654 if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
2655 poll_fd.events |= _DBUS_POLLOUT;
2660 if (flags & DBUS_ITERATION_BLOCK)
2661 poll_timeout = timeout_milliseconds;
2665 /* For blocking selects we drop the connection lock here
2666 * to avoid blocking out connection access during a potentially
2667 * indefinite blocking call. The io path is still protected
2668 * by the io_path_cond condvar, so we won't reenter this.
2670 if (flags & DBUS_ITERATION_BLOCK)
2672 _dbus_verbose ("unlock pre poll\n");
2673 _dbus_connection_unlock (transport->connection);
2677 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
2679 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
2681 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
2685 if (flags & DBUS_ITERATION_BLOCK)
2687 _dbus_verbose ("lock post poll\n");
2688 _dbus_connection_lock (transport->connection);
2694 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
2695 * valgrind flags it as an error. though it probably
2696 * is guaranteed on linux at least.
2699 if (poll_fd.revents & _DBUS_POLLERR)
2700 do_io_error (transport);
2703 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
2704 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
2705 #ifdef DBUS_AUTHENTICATION
2706 dbus_bool_t authentication_completed;
2709 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
2710 need_read, need_write);
2711 #ifdef DBUS_AUTHENTICATION
2712 do_authentication (transport, need_read, need_write,
2713 &authentication_completed);
2715 /* See comment in socket_handle_watch. */
2716 if (authentication_completed)
2719 if (need_read && (flags & DBUS_ITERATION_DO_READING))
2720 do_reading (transport);
2721 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
2722 do_writing (transport);
2726 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
2730 /* We need to install the write watch only if we did not
2731 * successfully write everything. Note we need to be careful that we
2732 * don't call check_write_watch *before* do_writing, since it's
2733 * inefficient to add the write watch, and we can avoid it most of
2734 * the time since we can write immediately.
2736 * However, we MUST always call check_write_watch(); DBusConnection code
2737 * relies on the fact that running an iteration will notice that
2738 * messages are pending.
2740 check_write_watch (transport);
2742 _dbus_verbose (" ... leaving do_iteration()\n");
2746 socket_live_messages_changed (DBusTransport *transport)
2748 /* See if we should look for incoming messages again */
2749 check_read_watch (transport);
2752 static const DBusTransportVTable kdbus_vtable = {
2754 socket_handle_watch,
2756 kdbus_connection_set,
2758 socket_live_messages_changed,
2759 socket_get_socket_fd
2763 * Creates a new transport for the given kdbus file descriptor. The file
2764 * descriptor must be nonblocking.
2766 * @param fd the file descriptor.
2767 * @param address the transport's address
2768 * @returns the new transport, or #NULL if no memory.
2770 static DBusTransport*
2771 _dbus_transport_new_for_socket_kdbus (int fd,
2772 const DBusString *address)
2774 DBusTransportSocket *socket_transport;
2776 socket_transport = dbus_new0 (DBusTransportSocket, 1);
2777 if (socket_transport == NULL)
2780 if (!_dbus_string_init (&socket_transport->encoded_outgoing))
2783 if (!_dbus_string_init (&socket_transport->encoded_incoming))
2786 socket_transport->write_watch = _dbus_watch_new (fd,
2787 DBUS_WATCH_WRITABLE,
2790 if (socket_transport->write_watch == NULL)
2793 socket_transport->read_watch = _dbus_watch_new (fd,
2794 DBUS_WATCH_READABLE,
2797 if (socket_transport->read_watch == NULL)
2800 if (!_dbus_transport_init_base (&socket_transport->base,
2805 #ifdef DBUS_AUTHENTICATION
2806 #ifdef HAVE_UNIX_FD_PASSING
2807 _dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
2811 socket_transport->fd = fd;
2812 socket_transport->message_bytes_written = 0;
2814 /* These values should probably be tunable or something. */
2815 socket_transport->max_bytes_read_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
2816 socket_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
2818 socket_transport->kdbus_mmap_ptr = NULL;
2819 socket_transport->memfd = -1;
2821 return (DBusTransport*) socket_transport;
2824 _dbus_watch_invalidate (socket_transport->read_watch);
2825 _dbus_watch_unref (socket_transport->read_watch);
2827 _dbus_watch_invalidate (socket_transport->write_watch);
2828 _dbus_watch_unref (socket_transport->write_watch);
2830 _dbus_string_free (&socket_transport->encoded_incoming);
2832 _dbus_string_free (&socket_transport->encoded_outgoing);
2834 dbus_free (socket_transport);
2840 * Opens a connection to the kdbus bus
2842 * This will set FD_CLOEXEC for the socket returned.
2844 * @param path the path to UNIX domain socket
2845 * @param error return location for error code
2846 * @returns connection file descriptor or -1 on error
2848 static int _dbus_connect_kdbus (const char *path, DBusError *error)
2852 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2853 _dbus_verbose ("connecting to kdbus bus %s\n", path);
2855 fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
2857 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
2863 * Creates a new transport for kdbus.
2864 * This creates a client-side of a transport.
2866 * @param path the path to the bus.
2867 * @param error address where an error can be returned.
2868 * @returns a new transport, or #NULL on failure.
2870 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
2873 DBusTransport *transport;
2876 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2878 if (!_dbus_string_init (&address))
2880 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2886 if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
2888 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2892 fd = _dbus_connect_kdbus (path, error);
2895 _DBUS_ASSERT_ERROR_IS_SET (error);
2899 _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
2901 transport = _dbus_transport_new_for_socket_kdbus (fd, &address);
2902 if (transport == NULL)
2904 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2908 _dbus_string_free (&address);
2913 _dbus_close_socket (fd, NULL);
2915 _dbus_string_free (&address);
2921 * Opens kdbus transport if method from address entry is kdbus
2923 * @param entry the address entry to try opening
2924 * @param transport_p return location for the opened transport
2925 * @param error error to be set
2926 * @returns result of the attempt
2928 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry,
2929 DBusTransport **transport_p,
2934 method = dbus_address_entry_get_method (entry);
2935 _dbus_assert (method != NULL);
2937 if (strcmp (method, "kdbus") == 0)
2939 const char *path = dbus_address_entry_get_value (entry, "path");
2943 _dbus_set_bad_address (error, "kdbus", "path", NULL);
2944 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
2947 *transport_p = _dbus_transport_new_for_kdbus (path, error);
2949 if (*transport_p == NULL)
2951 _DBUS_ASSERT_ERROR_IS_SET (error);
2952 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
2956 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2957 return DBUS_TRANSPORT_OPEN_OK;
2962 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2963 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;