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
58 #define ITER_APPEND_STR(string) \
59 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) \
65 #define MSG_ITEM_BUILD_VEC(data, datasize) \
66 item->type = KDBUS_MSG_PAYLOAD_VEC; \
67 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec); \
68 item->vec.address = (unsigned long) data; \
69 item->vec.size = datasize;
72 * Opaque object representing a transport. In kdbus transport it has nothing common
73 * with socket, but the name was preserved to comply with upper functions.
75 typedef struct DBusTransportKdbus DBusTransportKdbus;
78 * Implementation details of DBusTransportSocket. All members are private.
80 struct DBusTransportKdbus
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 void* kdbus_mmap_ptr; /**< Mapped memory where Kdbus (kernel) writes
95 * messages incoming to us.
97 int memfd; /**< File descriptor to special
98 * memory pool for bulk data
99 * transfer. Retrieved from
100 * Kdbus kernel module.
102 __u64 bloom_size; /**< bloom filter field size */
103 char* sender; /**< uniqe name of the sender */
106 __u64 dbus_transport_get_bloom_size(DBusTransport* transport)
108 return ((DBusTransportKdbus*)transport)->bloom_size;
112 * Adds locally generated message to received messages queue
115 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* connection)
117 DBusList *message_link;
119 message_link = _dbus_list_alloc_link (message);
120 if (message_link == NULL)
122 dbus_message_unref (message);
126 _dbus_connection_queue_synthesized_message_link(connection, message_link);
132 * Generates local error message as a reply to message given as parameter
133 * and adds generated error message to received messages queue.
135 static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
137 DBusMessage *errMessage;
138 char* error_msg = "";
142 error_msg = alloca(strlen(template) + strlen(object));
143 sprintf(error_msg, template, object);
146 error_msg = (char*)object;
148 errMessage = generate_local_error_message(dbus_message_get_serial(message), error_type, error_msg);
149 if(errMessage == NULL)
151 if (add_message_to_received(errMessage, connection))
158 * Generates reply to the message given as a parameter with one item in the reply body
159 * and adds generated reply message to received messages queue.
161 static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
163 DBusMessageIter args;
166 reply = dbus_message_new_method_return(message);
169 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
170 dbus_message_iter_init_append(reply, &args);
171 if (!dbus_message_iter_append_basic(&args, data_type, pData))
173 dbus_message_unref(reply);
176 if(add_message_to_received(reply, connection))
183 static int reply_ack(DBusMessage *message, DBusConnection* connection)
187 reply = dbus_message_new_method_return(message);
190 if(add_message_to_received(reply, connection))
196 * Retrieves file descriptor to memory pool from kdbus module.
197 * It is then used for bulk data sending.
198 * Triggered when message payload is over MEMFD_SIZE_THRESHOLD
201 static int kdbus_init_memfd(DBusTransportKdbus* socket_transport)
205 if (ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) {
206 _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
210 socket_transport->memfd = memfd;
211 _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd);
216 * Initiates Kdbus message structure.
217 * Calculates it's size, allocates memory and fills some fields.
218 * @param name Well-known name or NULL
219 * @param dst_id Numeric id of recipient
220 * @param body_size size of message body if present
221 * @param use_memfd flag to build memfd message
222 * @param fds_count number of file descriptors used
223 * @param transport transport
224 * @return initialized kdbus message
226 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)
228 struct kdbus_msg* msg;
231 msg_size = sizeof(struct kdbus_msg);
233 if(use_memfd == TRUE) // bulk data - memfd
234 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
236 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
238 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
242 msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
245 msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
246 else if (dst_id == KDBUS_DST_ID_BROADCAST)
247 msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
249 msg = malloc(msg_size);
252 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
256 memset(msg, 0, msg_size);
257 msg->size = msg_size;
258 msg->payload_type = KDBUS_PAYLOAD_DBUS1;
259 msg->dst_id = name ? 0 : dst_id;
260 msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
266 * Builds and sends kdbus message using DBus message.
267 * Decide whether used payload vector or memfd memory pool.
268 * Handles broadcasts and unicast messages, and passing of Unix fds.
269 * Does error handling.
270 * TODO refactor to be more compact
272 * @param transport transport
273 * @param message DBus message to be sent
274 * @return size of data sent
276 static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination)
278 struct kdbus_msg *msg;
279 struct kdbus_item *item;
280 uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
281 const DBusString *header;
282 const DBusString *body;
283 uint64_t ret_size = 0;
284 uint64_t body_size = 0;
285 uint64_t header_size = 0;
286 dbus_bool_t use_memfd;
289 dbus_bool_t autostart;
291 // determine destination and destination id
294 dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
295 if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
297 dst_id = strtoull(&destination[3], NULL, 10);
302 _dbus_message_get_network_data (message, &header, &body);
303 header_size = _dbus_string_get_length(header);
304 body_size = _dbus_string_get_length(body);
305 ret_size = header_size + body_size;
307 // check if message size is big enough to use memfd kdbus transport
308 use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
309 if(use_memfd) kdbus_init_memfd(transport);
311 _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
313 // init basic message fields
314 msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport);
315 msg->cookie = dbus_message_get_serial(message);
316 autostart = dbus_message_get_auto_start (message);
318 msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START;
320 // build message contents
323 // case 1 - bulk data transfer - memfd
328 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
330 _dbus_verbose("memfd sealing failed: \n");
334 buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
335 if (buf == MAP_FAILED)
337 _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
341 memcpy(buf, _dbus_string_get_const_data(header), header_size);
344 memcpy(buf, _dbus_string_get_const_data(body), body_size);
348 munmap(buf, ret_size);
350 // seal data - kdbus module needs it
351 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) {
352 _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
357 item->type = KDBUS_MSG_PAYLOAD_MEMFD;
358 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
359 item->memfd.size = ret_size;
360 item->memfd.fd = transport->memfd;
364 _dbus_verbose("sending normal vector data\n");
365 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size);
369 _dbus_verbose("body attaching\n");
370 item = KDBUS_PART_NEXT(item);
371 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(body), body_size);
377 item = KDBUS_PART_NEXT(item);
378 item->type = KDBUS_MSG_FDS;
379 item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
380 memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
385 item = KDBUS_PART_NEXT(item);
386 item->type = KDBUS_MSG_DST_NAME;
387 item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1;
388 memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE);
390 else if (dst_id == KDBUS_DST_ID_BROADCAST)
392 item = KDBUS_PART_NEXT(item);
393 item->type = KDBUS_MSG_BLOOM;
394 item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
395 strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
399 if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
403 else if(errno == ENXIO) //no such id on the bus
405 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
408 else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus
412 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))
416 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
419 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
425 close(transport->memfd);
431 * Performs kdbus hello - registration on the kdbus bus
433 * @param name place to store unique name given by bus
434 * @param transportS transport structure
435 * @returns #TRUE on success
437 static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS)
439 struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
441 hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
442 KDBUS_HELLO_ATTACH_COMM |
443 KDBUS_HELLO_ATTACH_EXE |
444 KDBUS_HELLO_ATTACH_CMDLINE |
445 KDBUS_HELLO_ATTACH_CAPS |
446 KDBUS_HELLO_ATTACH_CGROUP |
447 KDBUS_HELLO_ATTACH_SECLABEL |
448 KDBUS_HELLO_ATTACH_AUDIT*/;
449 hello.size = sizeof(struct kdbus_cmd_hello);
450 hello.pool_size = RECEIVE_POOL_SIZE;
452 if (ioctl(transportS->fd, KDBUS_CMD_HELLO, &hello))
454 _dbus_verbose ("Failed to send hello: %m, %d",errno);
458 sprintf(name, "%llu", (unsigned long long)hello.id);
459 _dbus_verbose("-- Our peer ID is: %s\n", name);
460 transportS->bloom_size = hello.bloom_size;
462 transportS->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, transportS->fd, 0);
463 if (transportS->kdbus_mmap_ptr == MAP_FAILED)
465 _dbus_verbose("Error when mmap: %m, %d",errno);
473 * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate
474 * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue.
476 * Now it captures only Hello message, which must be handled locally.
477 * All the rest is passed to dbus-daemon.
479 static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
481 if(!strcmp(dbus_message_get_member(message), "Hello"))
485 name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."));
489 if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
491 if(!register_kdbus_policy(&name[3], transport, geteuid()))
494 ((DBusTransportKdbus*)transport)->sender = name;
496 if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
503 return 1; //send to daemon
508 #if KDBUS_MSG_DECODE_DEBUG == 1
509 static char *msg_id(uint64_t id, char *buf)
515 sprintf(buf, "%llu", (unsigned long long)id);
519 struct kdbus_enum_table {
523 #define _STRINGIFY(x) #x
524 #define STRINGIFY(x) _STRINGIFY(x)
525 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
526 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
527 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
528 #define LOOKUP(what) \
529 const char *enum_##what(long long id) { \
531 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
532 if (id == kdbus_table_##what[i].id) \
533 return kdbus_table_##what[i].name; \
536 const char *enum_MSG(long long id);
538 ENUM(_KDBUS_MSG_NULL),
539 ENUM(KDBUS_MSG_PAYLOAD_VEC),
540 ENUM(KDBUS_MSG_PAYLOAD_OFF),
541 ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
543 ENUM(KDBUS_MSG_BLOOM),
544 ENUM(KDBUS_MSG_DST_NAME),
545 ENUM(KDBUS_MSG_SRC_CREDS),
546 ENUM(KDBUS_MSG_SRC_PID_COMM),
547 ENUM(KDBUS_MSG_SRC_TID_COMM),
548 ENUM(KDBUS_MSG_SRC_EXE),
549 ENUM(KDBUS_MSG_SRC_CMDLINE),
550 ENUM(KDBUS_MSG_SRC_CGROUP),
551 ENUM(KDBUS_MSG_SRC_CAPS),
552 ENUM(KDBUS_MSG_SRC_SECLABEL),
553 ENUM(KDBUS_MSG_SRC_AUDIT),
554 ENUM(KDBUS_MSG_SRC_NAMES),
555 ENUM(KDBUS_MSG_TIMESTAMP),
556 ENUM(KDBUS_MSG_NAME_ADD),
557 ENUM(KDBUS_MSG_NAME_REMOVE),
558 ENUM(KDBUS_MSG_NAME_CHANGE),
559 ENUM(KDBUS_MSG_ID_ADD),
560 ENUM(KDBUS_MSG_ID_REMOVE),
561 ENUM(KDBUS_MSG_REPLY_TIMEOUT),
562 ENUM(KDBUS_MSG_REPLY_DEAD),
565 const char *enum_PAYLOAD(long long id);
567 ENUM(KDBUS_PAYLOAD_KERNEL),
568 ENUM(KDBUS_PAYLOAD_DBUS1),
569 ENUM(KDBUS_PAYLOAD_GVARIANT),
574 * Puts locally generated message into received data buffer.
575 * Use only during receiving phase!
577 * @param message message to load
578 * @param data place to load message
579 * @return size of message
581 static int put_message_into_data(DBusMessage *message, char* data)
584 const DBusString *header;
585 const DBusString *body;
588 dbus_message_set_serial(message, 1);
589 dbus_message_lock (message);
590 _dbus_message_get_network_data (message, &header, &body);
591 ret_size = _dbus_string_get_length(header);
592 memcpy(data, _dbus_string_get_const_data(header), ret_size);
594 size = _dbus_string_get_length(body);
595 memcpy(data, _dbus_string_get_const_data(body), size);
602 * Get the length of the kdbus message content.
604 * @param msg kdbus message
605 * @return the length of the kdbus message
607 static int kdbus_message_size(const struct kdbus_msg* msg)
609 const struct kdbus_item *item;
612 KDBUS_PART_FOREACH(item, msg, items)
614 if (item->size <= KDBUS_PART_HEADER_SIZE)
616 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
621 case KDBUS_MSG_PAYLOAD_OFF:
622 ret_size += item->vec.size;
624 case KDBUS_MSG_PAYLOAD_MEMFD:
625 ret_size += item->memfd.size;
636 * Decodes kdbus message in order to extract dbus message and put it into data and fds.
637 * Also captures and decodes kdbus error messages and kdbus kernel broadcasts and converts
638 * all of them into dbus messages.
640 * @param msg kdbus message
641 * @param data place to copy dbus message to
642 * @param kdbus_transport transport
643 * @param fds place to store file descriptors received
644 * @param n_fds place to store quantity of file descriptor
645 * @return number of dbus message's bytes received or -1 on error
647 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportKdbus* kdbus_transport, int* fds, int* n_fds)
649 const struct kdbus_item *item;
651 DBusMessage *message = NULL;
652 DBusMessageIter args;
653 const char* emptyString = "";
654 const char* pString = NULL;
655 char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
656 const char* pDBusName = dbus_name;
657 #if KDBUS_MSG_DECODE_DEBUG == 1
661 #if KDBUS_MSG_DECODE_DEBUG == 1
662 _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
663 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
664 (unsigned long long) msg->flags,
665 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
666 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
671 KDBUS_PART_FOREACH(item, msg, items)
673 if (item->size <= KDBUS_PART_HEADER_SIZE)
675 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
676 break; //??? continue (because dbus will find error) or break
681 case KDBUS_MSG_PAYLOAD_OFF:
682 memcpy(data, (char *)kdbus_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
683 data += item->vec.size;
684 ret_size += item->vec.size;
686 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
687 enum_MSG(item->type), item->size,
688 (unsigned long long)item->vec.offset,
689 (unsigned long long)item->vec.size);
692 case KDBUS_MSG_PAYLOAD_MEMFD:
697 size = item->memfd.size;
698 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
700 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
701 if (buf == MAP_FAILED)
703 _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
707 memcpy(data, buf, size);
713 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
714 enum_MSG(item->type), item->size,
715 (unsigned long long)item->vec.offset,
716 (unsigned long long)item->vec.size);
724 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
725 memcpy(fds, item->fds, *n_fds * sizeof(int));
726 for (i = 0; i < *n_fds; i++)
727 _dbus_fd_set_close_on_exec(fds[i]);
731 #if KDBUS_MSG_DECODE_DEBUG == 1
732 case KDBUS_MSG_SRC_CREDS:
733 _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
734 enum_MSG(item->type), item->size,
735 item->creds.uid, item->creds.gid,
736 item->creds.pid, item->creds.tid,
737 item->creds.starttime);
740 case KDBUS_MSG_SRC_PID_COMM:
741 case KDBUS_MSG_SRC_TID_COMM:
742 case KDBUS_MSG_SRC_EXE:
743 case KDBUS_MSG_SRC_CGROUP:
744 case KDBUS_MSG_SRC_SECLABEL:
745 case KDBUS_MSG_DST_NAME:
746 _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
747 enum_MSG(item->type), item->size, item->str, strlen(item->str));
750 case KDBUS_MSG_SRC_CMDLINE:
751 case KDBUS_MSG_SRC_NAMES: {
752 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
753 const char *str = item->str;
756 _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
758 _dbus_verbose("'%s' ", str);
759 size -= strlen(str) + 1;
760 str += strlen(str) + 1;
764 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
768 case KDBUS_MSG_SRC_AUDIT:
769 _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
770 enum_MSG(item->type), item->size,
771 (unsigned long long)item->data64[0],
772 (unsigned long long)item->data64[1]);
775 case KDBUS_MSG_SRC_CAPS: {
780 _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
781 enum_MSG(item->type), item->size,
782 (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
785 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
787 _dbus_verbose(" CapInh=");
788 for (i = 0; i < n; i++)
789 _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
791 _dbus_verbose(" CapPrm=");
792 for (i = 0; i < n; i++)
793 _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
795 _dbus_verbose(" CapEff=");
796 for (i = 0; i < n; i++)
797 _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
799 _dbus_verbose(" CapInh=");
800 for (i = 0; i < n; i++)
801 _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
806 case KDBUS_MSG_TIMESTAMP:
807 _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
808 enum_MSG(item->type), item->size,
809 (unsigned long long)item->timestamp.realtime_ns,
810 (unsigned long long)item->timestamp.monotonic_ns);
814 case KDBUS_MSG_REPLY_TIMEOUT:
815 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
816 enum_MSG(item->type), item->size, msg->cookie_reply);
818 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
825 ret_size = put_message_into_data(message, data);
828 case KDBUS_MSG_REPLY_DEAD:
829 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
830 enum_MSG(item->type), item->size, msg->cookie_reply);
832 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
839 ret_size = put_message_into_data(message, data);
842 case KDBUS_MSG_NAME_ADD:
843 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
844 enum_MSG(item->type), (unsigned long long) item->size,
845 item->name_change.name, item->name_change.old_id,
846 item->name_change.new_id, item->name_change.flags);
848 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
855 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
856 pString = item->name_change.name;
857 _dbus_verbose ("Name added: %s\n", pString);
858 dbus_message_iter_init_append(message, &args);
859 ITER_APPEND_STR(pString)
860 ITER_APPEND_STR(emptyString)
861 ITER_APPEND_STR(pDBusName)
862 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
864 ret_size = put_message_into_data(message, data);
867 case KDBUS_MSG_NAME_REMOVE:
868 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
869 enum_MSG(item->type), (unsigned long long) item->size,
870 item->name_change.name, item->name_change.old_id,
871 item->name_change.new_id, item->name_change.flags);
873 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
880 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
881 pString = item->name_change.name;
882 _dbus_verbose ("Name removed: %s\n", pString);
883 dbus_message_iter_init_append(message, &args);
884 ITER_APPEND_STR(pString)
885 ITER_APPEND_STR(pDBusName)
886 ITER_APPEND_STR(emptyString)
887 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
889 ret_size = put_message_into_data(message, data);
892 case KDBUS_MSG_NAME_CHANGE:
893 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
894 enum_MSG(item->type), (unsigned long long) item->size,
895 item->name_change.name, item->name_change.old_id,
896 item->name_change.new_id, item->name_change.flags);
898 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
905 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
906 pString = item->name_change.name;
907 _dbus_verbose ("Name changed: %s\n", pString);
908 dbus_message_iter_init_append(message, &args);
909 ITER_APPEND_STR(pString)
910 ITER_APPEND_STR(pDBusName)
911 sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
912 _dbus_verbose ("New id: %s\n", pDBusName);
913 ITER_APPEND_STR(pDBusName)
914 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
916 ret_size = put_message_into_data(message, data);
919 case KDBUS_MSG_ID_ADD:
920 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
921 enum_MSG(item->type), (unsigned long long) item->size,
922 (unsigned long long) item->id_change.id,
923 (unsigned long long) item->id_change.flags);
925 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
932 sprintf(dbus_name,":1.%llu",item->id_change.id);
933 dbus_message_iter_init_append(message, &args);
934 ITER_APPEND_STR(pDBusName)
935 ITER_APPEND_STR(emptyString)
936 ITER_APPEND_STR(pDBusName)
937 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
939 ret_size = put_message_into_data(message, data);
942 case KDBUS_MSG_ID_REMOVE:
943 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
944 enum_MSG(item->type), (unsigned long long) item->size,
945 (unsigned long long) item->id_change.id,
946 (unsigned long long) item->id_change.flags);
948 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
955 sprintf(dbus_name,":1.%llu",item->id_change.id);
956 dbus_message_iter_init_append(message, &args);
957 ITER_APPEND_STR(pDBusName)
958 ITER_APPEND_STR(pDBusName)
959 ITER_APPEND_STR(emptyString)
960 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
962 ret_size = put_message_into_data(message, data);
964 #if KDBUS_MSG_DECODE_DEBUG == 1
966 _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
972 #if KDBUS_MSG_DECODE_DEBUG == 1
974 if ((char *)item - ((char *)msg + msg->size) >= 8)
975 _dbus_verbose("invalid padding at end of message\n");
980 dbus_message_unref(message);
985 * Reads message from kdbus and puts it into dbus buffer and fds
987 * @param transport transport
988 * @param buffer place to copy received message to
989 * @param fds place to store file descriptors sent in the message
990 * @param n_fds place to store number of file descriptors
991 * @return size of received message on success, -1 on error
993 static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString *buffer, int* fds, int* n_fds)
995 int ret_size, buf_size;
996 uint64_t __attribute__ ((__aligned__(8))) offset;
997 struct kdbus_msg *msg;
1001 start = _dbus_string_get_length (buffer);
1004 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1008 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1009 _dbus_string_set_length (buffer, start);
1013 msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
1015 buf_size = kdbus_message_size(msg);
1018 _dbus_verbose("kdbus error - too short message: %d (%m)\n", errno);
1022 /* What is the maximum size of the locally generated message?
1023 I just assume 2048 bytes */
1024 buf_size = MAX(buf_size, 2048);
1026 if (!_dbus_string_lengthen (buffer, buf_size))
1031 data = _dbus_string_get_data_len (buffer, start, buf_size);
1033 ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
1035 if(ret_size == -1) /* error */
1037 _dbus_string_set_length (buffer, start);
1040 else if (buf_size != ret_size) /* case of locally generated message */
1042 _dbus_string_set_length (buffer, start + ret_size);
1046 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
1050 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1057 * copy-paste from socket transport with needed renames only.
1060 free_watches (DBusTransport *transport)
1062 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1064 _dbus_verbose ("start\n");
1066 if (kdbus_transport->read_watch)
1068 if (transport->connection)
1069 _dbus_connection_remove_watch_unlocked (transport->connection,
1070 kdbus_transport->read_watch);
1071 _dbus_watch_invalidate (kdbus_transport->read_watch);
1072 _dbus_watch_unref (kdbus_transport->read_watch);
1073 kdbus_transport->read_watch = NULL;
1076 if (kdbus_transport->write_watch)
1078 if (transport->connection)
1079 _dbus_connection_remove_watch_unlocked (transport->connection,
1080 kdbus_transport->write_watch);
1081 _dbus_watch_invalidate (kdbus_transport->write_watch);
1082 _dbus_watch_unref (kdbus_transport->write_watch);
1083 kdbus_transport->write_watch = NULL;
1086 _dbus_verbose ("end\n");
1090 * copy-paste from socket transport with needed renames only.
1093 transport_finalize (DBusTransport *transport)
1095 _dbus_verbose ("\n");
1097 free_watches (transport);
1099 _dbus_transport_finalize_base (transport);
1101 _dbus_assert (((DBusTransportKdbus*) transport)->read_watch == NULL);
1102 _dbus_assert (((DBusTransportKdbus*) transport)->write_watch == NULL);
1104 dbus_free (transport);
1108 * copy-paste from socket transport with needed renames only.
1111 check_write_watch (DBusTransport *transport)
1113 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1116 if (transport->connection == NULL)
1119 if (transport->disconnected)
1121 _dbus_assert (kdbus_transport->write_watch == NULL);
1125 _dbus_transport_ref (transport);
1127 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1129 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1130 needed, transport->connection, kdbus_transport->write_watch,
1131 kdbus_transport->fd,
1132 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1134 _dbus_connection_toggle_watch_unlocked (transport->connection,
1135 kdbus_transport->write_watch,
1138 _dbus_transport_unref (transport);
1142 * copy-paste from socket transport with needed renames only.
1145 check_read_watch (DBusTransport *transport)
1147 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1148 dbus_bool_t need_read_watch;
1150 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1152 if (transport->connection == NULL)
1155 if (transport->disconnected)
1157 _dbus_assert (kdbus_transport->read_watch == NULL);
1161 _dbus_transport_ref (transport);
1164 (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1165 (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1167 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
1168 _dbus_connection_toggle_watch_unlocked (transport->connection,
1169 kdbus_transport->read_watch,
1172 _dbus_transport_unref (transport);
1176 * copy-paste from socket transport.
1179 do_io_error (DBusTransport *transport)
1181 _dbus_transport_ref (transport);
1182 _dbus_transport_disconnect (transport);
1183 _dbus_transport_unref (transport);
1186 /* returns false on oom */
1188 do_writing (DBusTransport *transport)
1190 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1193 if (transport->disconnected)
1195 _dbus_verbose ("Not connected, not writing anything\n");
1200 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
1201 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
1202 kdbus_transport->fd);
1207 while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1210 DBusMessage *message;
1211 const DBusString *header;
1212 const DBusString *body;
1213 int total_bytes_to_write;
1214 const char* pDestination;
1216 message = _dbus_connection_get_message_to_send (transport->connection);
1217 _dbus_assert (message != NULL);
1218 if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages
1220 dbus_message_unlock(message);
1221 dbus_message_set_sender(message, kdbus_transport->sender);
1222 dbus_message_lock (message);
1224 _dbus_message_get_network_data (message, &header, &body);
1225 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
1226 pDestination = dbus_message_get_destination(message);
1230 if(!strcmp(pDestination, DBUS_SERVICE_DBUS))
1232 if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
1236 ret = emulateOrgFreedesktopDBus(transport, message);
1244 bytes_written = total_bytes_to_write;
1247 //else send to "daemon" as to normal recipient
1252 if(total_bytes_to_write > kdbus_transport->max_bytes_written_per_iteration)
1255 bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination);
1259 if (bytes_written < 0)
1261 /* EINTR already handled for us */
1263 /* For some discussion of why we also ignore EPIPE here, see
1264 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
1267 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
1271 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
1272 do_io_error (transport);
1278 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
1279 total_bytes_to_write);
1281 kdbus_transport->message_bytes_written += bytes_written;
1283 _dbus_assert (kdbus_transport->message_bytes_written <=
1284 total_bytes_to_write);
1286 if (kdbus_transport->message_bytes_written == total_bytes_to_write)
1288 kdbus_transport->message_bytes_written = 0;
1289 _dbus_connection_message_sent_unlocked (transport->connection,
1301 /* returns false on out-of-memory */
1303 do_reading (DBusTransport *transport)
1305 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1308 dbus_bool_t oom = FALSE;
1311 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1315 /* See if we've exceeded max messages and need to disable reading */
1316 check_read_watch (transport);
1318 _dbus_assert (kdbus_transport->read_watch != NULL ||
1319 transport->disconnected);
1321 if (transport->disconnected)
1324 if (!dbus_watch_get_enabled (kdbus_transport->read_watch))
1327 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1329 _dbus_verbose ("Out of memory reading file descriptors\n");
1333 _dbus_message_loader_get_buffer (transport->loader, &buffer);
1335 bytes_read = kdbus_read_message(kdbus_transport, buffer, fds, &n_fds);
1337 if (bytes_read >= 0 && n_fds > 0)
1338 _dbus_verbose("Read %i unix fds\n", n_fds);
1340 _dbus_message_loader_return_buffer (transport->loader,
1342 bytes_read < 0 ? 0 : bytes_read);
1343 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1347 /* EINTR already handled for us */
1349 if (_dbus_get_is_errno_enomem ())
1351 _dbus_verbose ("Out of memory in read()/do_reading()\n");
1355 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1359 _dbus_verbose ("Error reading from remote app: %s\n",
1360 _dbus_strerror_from_errno ());
1361 do_io_error (transport);
1365 else if (bytes_read == 0)
1367 _dbus_verbose ("Disconnected from remote app\n");
1368 do_io_error (transport);
1373 _dbus_verbose (" read %d bytes\n", bytes_read);
1375 if (!_dbus_transport_queue_messages (transport))
1378 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
1382 /* Try reading more data until we get EAGAIN and return, or
1383 * exceed max bytes per iteration. If in blocking mode of
1384 * course we'll block instead of returning.
1396 * copy-paste from socket transport.
1399 unix_error_with_read_to_come (DBusTransport *itransport,
1403 DBusTransportKdbus *transport = (DBusTransportKdbus *) itransport;
1405 if (!((flags & DBUS_WATCH_HANGUP) || (flags & DBUS_WATCH_ERROR)))
1408 /* If we have a read watch enabled ...
1409 we -might have data incoming ... => handle the HANGUP there */
1410 if (watch != transport->read_watch && _dbus_watch_get_enabled (transport->read_watch))
1417 * copy-paste from socket transport with needed renames only.
1420 kdbus_handle_watch (DBusTransport *transport,
1424 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1426 _dbus_assert (watch == kdbus_transport->read_watch ||
1427 watch == kdbus_transport->write_watch);
1428 _dbus_assert (watch != NULL);
1430 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
1431 * still be in the buffer and do_reading may need several iteration to read
1432 * it all (because of its max_bytes_read_per_iteration limit).
1434 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
1436 _dbus_verbose ("Hang up or error on watch\n");
1437 _dbus_transport_disconnect (transport);
1441 if (watch == kdbus_transport->read_watch &&
1442 (flags & DBUS_WATCH_READABLE))
1444 _dbus_verbose ("handling read watch %p flags = %x\n",
1447 if (!do_reading (transport))
1449 _dbus_verbose ("no memory to read\n");
1454 else if (watch == kdbus_transport->write_watch &&
1455 (flags & DBUS_WATCH_WRITABLE))
1457 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1458 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1460 if (!do_writing (transport))
1462 _dbus_verbose ("no memory to write\n");
1466 /* See if we still need the write watch */
1467 check_write_watch (transport);
1474 * copy-paste from socket transport with needed renames only.
1477 kdbus_disconnect (DBusTransport *transport)
1479 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1481 _dbus_verbose ("\n");
1483 free_watches (transport);
1485 _dbus_close_socket (kdbus_transport->fd, NULL);
1486 kdbus_transport->fd = -1;
1490 * copy-paste from socket transport with needed renames only.
1493 kdbus_connection_set (DBusTransport *transport)
1495 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1497 dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus
1499 _dbus_watch_set_handler (kdbus_transport->write_watch,
1500 _dbus_connection_handle_watch,
1501 transport->connection, NULL);
1503 _dbus_watch_set_handler (kdbus_transport->read_watch,
1504 _dbus_connection_handle_watch,
1505 transport->connection, NULL);
1507 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1508 kdbus_transport->write_watch))
1511 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1512 kdbus_transport->read_watch))
1514 _dbus_connection_remove_watch_unlocked (transport->connection,
1515 kdbus_transport->write_watch);
1519 check_read_watch (transport);
1520 check_write_watch (transport);
1525 /** original dbus copy-pasted comment
1526 * @todo We need to have a way to wake up the select sleep if
1527 * a new iteration request comes in with a flag (read/write) that
1528 * we're not currently serving. Otherwise a call that just reads
1529 * could block a write call forever (if there are no incoming
1533 kdbus_do_iteration (DBusTransport *transport,
1535 int timeout_milliseconds)
1537 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1542 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
1543 flags & DBUS_ITERATION_DO_READING ? "read" : "",
1544 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1545 timeout_milliseconds,
1546 kdbus_transport->read_watch,
1547 kdbus_transport->write_watch,
1548 kdbus_transport->fd);
1550 /* the passed in DO_READING/DO_WRITING flags indicate whether to
1551 * read/write messages, but regardless of those we may need to block
1552 * for reading/writing to do auth. But if we do reading for auth,
1553 * we don't want to read any messages yet if not given DO_READING.
1556 poll_fd.fd = kdbus_transport->fd;
1559 if (_dbus_transport_try_to_authenticate (transport))
1561 /* This is kind of a hack; if we have stuff to write, then try
1562 * to avoid the poll. This is probably about a 5% speedup on an
1563 * echo client/server.
1565 * If both reading and writing were requested, we want to avoid this
1566 * since it could have funky effects:
1567 * - both ends spinning waiting for the other one to read
1568 * data so they can finish writing
1569 * - prioritizing all writing ahead of reading
1571 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1572 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1573 !transport->disconnected &&
1574 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1576 do_writing (transport);
1578 if (transport->disconnected ||
1579 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
1583 /* If we get here, we decided to do the poll() after all */
1584 _dbus_assert (kdbus_transport->read_watch);
1585 if (flags & DBUS_ITERATION_DO_READING)
1586 poll_fd.events |= _DBUS_POLLIN;
1588 _dbus_assert (kdbus_transport->write_watch);
1589 if (flags & DBUS_ITERATION_DO_WRITING)
1590 poll_fd.events |= _DBUS_POLLOUT;
1594 DBusAuthState auth_state;
1596 auth_state = _dbus_auth_do_work (transport->auth);
1598 if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1599 poll_fd.events |= _DBUS_POLLIN;
1601 if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1602 poll_fd.events |= _DBUS_POLLOUT;
1607 if (flags & DBUS_ITERATION_BLOCK)
1608 poll_timeout = timeout_milliseconds;
1612 /* For blocking selects we drop the connection lock here
1613 * to avoid blocking out connection access during a potentially
1614 * indefinite blocking call. The io path is still protected
1615 * by the io_path_cond condvar, so we won't reenter this.
1617 if (flags & DBUS_ITERATION_BLOCK)
1619 _dbus_verbose ("unlock pre poll\n");
1620 _dbus_connection_unlock (transport->connection);
1624 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1626 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1628 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
1632 if (flags & DBUS_ITERATION_BLOCK)
1634 _dbus_verbose ("lock post poll\n");
1635 _dbus_connection_lock (transport->connection);
1641 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1642 * valgrind flags it as an error. though it probably
1643 * is guaranteed on linux at least.
1646 if (poll_fd.revents & _DBUS_POLLERR)
1647 do_io_error (transport);
1650 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1651 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1653 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1654 need_read, need_write);
1656 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1657 do_reading (transport);
1658 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1659 do_writing (transport);
1663 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
1667 /* We need to install the write watch only if we did not
1668 * successfully write everything. Note we need to be careful that we
1669 * don't call check_write_watch *before* do_writing, since it's
1670 * inefficient to add the write watch, and we can avoid it most of
1671 * the time since we can write immediately.
1673 * However, we MUST always call check_write_watch(); DBusConnection code
1674 * relies on the fact that running an iteration will notice that
1675 * messages are pending.
1677 check_write_watch (transport);
1679 _dbus_verbose (" ... leaving do_iteration()\n");
1683 * copy-paste from socket transport with needed renames only.
1686 kdbus_live_messages_changed (DBusTransport *transport)
1688 /* See if we should look for incoming messages again */
1689 check_read_watch (transport);
1693 kdbus_get_kdbus_fd (DBusTransport *transport,
1696 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1698 *fd_p = kdbus_transport->fd;
1703 static const DBusTransportVTable kdbus_vtable = {
1707 kdbus_connection_set,
1709 kdbus_live_messages_changed,
1714 * Creates a new transport for the given kdbus file descriptor. The file
1715 * descriptor must be nonblocking.
1717 * @param fd the file descriptor.
1718 * @param address the transport's address
1719 * @returns the new transport, or #NULL if no memory.
1721 static DBusTransport*
1722 _dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
1724 DBusTransportKdbus *kdbus_transport;
1726 kdbus_transport = dbus_new0 (DBusTransportKdbus, 1);
1727 if (kdbus_transport == NULL)
1730 kdbus_transport->write_watch = _dbus_watch_new (fd,
1731 DBUS_WATCH_WRITABLE,
1734 if (kdbus_transport->write_watch == NULL)
1737 kdbus_transport->read_watch = _dbus_watch_new (fd,
1738 DBUS_WATCH_READABLE,
1741 if (kdbus_transport->read_watch == NULL)
1744 if (!_dbus_transport_init_base (&kdbus_transport->base,
1749 kdbus_transport->fd = fd;
1750 kdbus_transport->message_bytes_written = 0;
1752 /* These values should probably be tunable or something. */
1753 kdbus_transport->max_bytes_read_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
1754 kdbus_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
1756 kdbus_transport->kdbus_mmap_ptr = NULL;
1757 kdbus_transport->memfd = -1;
1759 return (DBusTransport*) kdbus_transport;
1762 _dbus_watch_invalidate (kdbus_transport->read_watch);
1763 _dbus_watch_unref (kdbus_transport->read_watch);
1765 _dbus_watch_invalidate (kdbus_transport->write_watch);
1766 _dbus_watch_unref (kdbus_transport->write_watch);
1768 dbus_free (kdbus_transport);
1773 * Opens a connection to the kdbus bus
1775 * This will set FD_CLOEXEC for the socket returned.
1777 * @param path the path to UNIX domain socket
1778 * @param error return location for error code
1779 * @returns connection file descriptor or -1 on error
1781 static int _dbus_connect_kdbus (const char *path, DBusError *error)
1785 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1786 _dbus_verbose ("connecting to kdbus bus %s\n", path);
1788 fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
1790 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
1796 * Creates a new transport for kdbus.
1797 * This creates a client-side of a transport.
1799 * @param path the path to the bus.
1800 * @param error address where an error can be returned.
1801 * @returns a new transport, or #NULL on failure.
1803 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
1806 DBusTransport *transport;
1809 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1811 if (!_dbus_string_init (&address))
1813 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1819 if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
1821 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1825 fd = _dbus_connect_kdbus (path, error);
1828 _DBUS_ASSERT_ERROR_IS_SET (error);
1832 _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
1834 transport = _dbus_transport_new_kdbus_transport (fd, &address);
1835 if (transport == NULL)
1837 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1841 _dbus_string_free (&address);
1846 _dbus_close_socket (fd, NULL);
1848 _dbus_string_free (&address);
1854 * Opens kdbus transport if method from address entry is kdbus
1856 * @param entry the address entry to try opening
1857 * @param transport_p return location for the opened transport
1858 * @param error error to be set
1859 * @returns result of the attempt
1861 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry,
1862 DBusTransport **transport_p,
1867 method = dbus_address_entry_get_method (entry);
1868 _dbus_assert (method != NULL);
1870 if (strcmp (method, "kdbus") == 0)
1872 const char *path = dbus_address_entry_get_value (entry, "path");
1876 _dbus_set_bad_address (error, "kdbus", "path", NULL);
1877 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1880 *transport_p = _dbus_transport_new_for_kdbus (path, error);
1882 if (*transport_p == NULL)
1884 _DBUS_ASSERT_ERROR_IS_SET (error);
1885 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1889 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1890 return DBUS_TRANSPORT_OPEN_OK;
1895 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1896 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;