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
55 //todo add compilation-time check if MEMFD_SIZE_THERSHOLD is lower than max payload vector size defined in kdbus.h
57 #define KDBUS_MSG_DECODE_DEBUG 0
59 #define ITER_APPEND_STR(string) \
60 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) \
66 #define MSG_ITEM_BUILD_VEC(data, datasize) \
67 item->type = KDBUS_MSG_PAYLOAD_VEC; \
68 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec); \
69 item->vec.address = (unsigned long) data; \
70 item->vec.size = datasize;
73 * Opaque object representing a transport. In kdbus transport it has nothing common
74 * with socket, but the name was preserved to comply with upper functions.
76 typedef struct DBusTransportKdbus DBusTransportKdbus;
79 * Implementation details of DBusTransportSocket. All members are private.
81 struct DBusTransportKdbus
83 DBusTransport base; /**< Parent instance */
84 int fd; /**< File descriptor. */
85 DBusWatch *read_watch; /**< Watch for readability. */
86 DBusWatch *write_watch; /**< Watch for writability. */
88 int max_bytes_read_per_iteration; /**< In kdbus only to control overall message size*/
89 int max_bytes_written_per_iteration; /**< In kdbus only to control overall message size*/
91 int message_bytes_written; /**< Number of bytes of current
92 * outgoing message that have
95 void* kdbus_mmap_ptr; /**< Mapped memory where Kdbus (kernel) writes
96 * messages incoming to us.
98 int memfd; /**< File descriptor to special
99 * memory pool for bulk data
100 * transfer. Retrieved from
101 * Kdbus kernel module.
103 __u64 bloom_size; /**< bloom filter field size */
104 char* sender; /**< uniqe name of the sender */
107 __u64 dbus_transport_get_bloom_size(DBusTransport* transport)
109 return ((DBusTransportKdbus*)transport)->bloom_size;
113 * Adds locally generated message to received messages queue
116 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection* connection)
118 DBusList *message_link;
120 message_link = _dbus_list_alloc_link (message);
121 if (message_link == NULL)
123 dbus_message_unref (message);
127 _dbus_connection_queue_synthesized_message_link(connection, message_link);
133 * Generates local error message as a reply to message given as parameter
134 * and adds generated error message to received messages queue.
136 static int reply_with_error(char* error_type, const char* template, const char* object, DBusMessage *message, DBusConnection* connection)
138 DBusMessage *errMessage;
139 char* error_msg = "";
143 error_msg = alloca(strlen(template) + strlen(object));
144 sprintf(error_msg, template, object);
147 error_msg = (char*)object;
149 errMessage = generate_local_error_message(dbus_message_get_serial(message), error_type, error_msg);
150 if(errMessage == NULL)
152 if (add_message_to_received(errMessage, connection))
159 * Generates reply to the message given as a parameter with one item in the reply body
160 * and adds generated reply message to received messages queue.
162 static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusConnection* connection)
164 DBusMessageIter args;
167 reply = dbus_message_new_method_return(message);
170 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
171 dbus_message_iter_init_append(reply, &args);
172 if (!dbus_message_iter_append_basic(&args, data_type, pData))
174 dbus_message_unref(reply);
177 if(add_message_to_received(reply, connection))
184 static int reply_ack(DBusMessage *message, DBusConnection* connection)
188 reply = dbus_message_new_method_return(message);
191 if(add_message_to_received(reply, connection))
197 * Retrieves file descriptor to memory pool from kdbus module.
198 * It is then used for bulk data sending.
199 * Triggered when message payload is over MEMFD_SIZE_THRESHOLD
202 static int kdbus_init_memfd(DBusTransportKdbus* socket_transport)
206 if (ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd) < 0) {
207 _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
211 socket_transport->memfd = memfd;
212 _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd);
217 * Initiates Kdbus message structure.
218 * Calculates it's size, allocates memory and fills some fields.
219 * @param name Well-known name or NULL
220 * @param dst_id Numeric id of recipient
221 * @param body_size size of message body if present
222 * @param use_memfd flag to build memfd message
223 * @param fds_count number of file descriptors used
224 * @param transport transport
225 * @return initialized kdbus message
227 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)
229 struct kdbus_msg* msg;
232 msg_size = sizeof(struct kdbus_msg);
234 if(use_memfd == TRUE) // bulk data - memfd
235 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
238 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec)); //header is a must
239 while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
241 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
242 body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
245 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
249 msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
252 msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
253 else if (dst_id == KDBUS_DST_ID_BROADCAST)
254 msg_size += KDBUS_PART_HEADER_SIZE + transport->bloom_size;
256 msg = malloc(msg_size);
259 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
263 memset(msg, 0, msg_size);
264 msg->size = msg_size;
265 msg->payload_type = KDBUS_PAYLOAD_DBUS1;
266 msg->dst_id = name ? 0 : dst_id;
267 msg->src_id = strtoull(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
273 * Builds and sends kdbus message using DBus message.
274 * Decide whether used payload vector or memfd memory pool.
275 * Handles broadcasts and unicast messages, and passing of Unix fds.
276 * Does error handling.
277 * TODO refactor to be more compact
279 * @param transport transport
280 * @param message DBus message to be sent
281 * @return size of data sent
283 static int kdbus_write_msg(DBusTransportKdbus *transport, DBusMessage *message, const char* destination)
285 struct kdbus_msg *msg;
286 struct kdbus_item *item;
287 uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
288 const DBusString *header;
289 const DBusString *body;
290 uint64_t ret_size = 0;
291 uint64_t body_size = 0;
292 uint64_t header_size = 0;
293 dbus_bool_t use_memfd = FALSE;
296 dbus_bool_t autostart;
298 // determine destination and destination id
301 dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
302 if((destination[0] == ':') && (destination[1] == '1') && (destination[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
304 dst_id = strtoull(&destination[3], NULL, 10);
309 _dbus_message_get_network_data (message, &header, &body);
310 header_size = _dbus_string_get_length(header);
311 body_size = _dbus_string_get_length(body);
312 ret_size = header_size + body_size;
314 // check whether we can and should use memfd
315 if((dst_id != KDBUS_DST_ID_BROADCAST) && (ret_size > MEMFD_SIZE_THRESHOLD))
318 kdbus_init_memfd(transport);
321 _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
323 // init basic message fields
324 msg = kdbus_init_msg(destination, dst_id, body_size, use_memfd, fds_count, transport);
325 msg->cookie = dbus_message_get_serial(message);
326 autostart = dbus_message_get_auto_start (message);
328 msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START;
330 // build message contents
337 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
339 _dbus_verbose("memfd sealing failed: \n");
343 buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
344 if (buf == MAP_FAILED)
346 _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
350 memcpy(buf, _dbus_string_get_const_data(header), header_size);
353 memcpy(buf, _dbus_string_get_const_data(body), body_size);
357 munmap(buf, ret_size);
359 // seal data - kdbus module needs it
360 if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) {
361 _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
366 item->type = KDBUS_MSG_PAYLOAD_MEMFD;
367 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
368 item->memfd.size = ret_size;
369 item->memfd.fd = transport->memfd;
373 _dbus_verbose("sending normal vector data\n");
374 MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size);
378 const char* body_data;
380 body_data = _dbus_string_get_const_data(body);
381 while(body_size > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
383 _dbus_verbose("body attaching\n");
384 item = KDBUS_PART_NEXT(item);
385 MSG_ITEM_BUILD_VEC(body_data, KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE);
386 body_data += KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
387 body_size -= KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
391 _dbus_verbose("body attaching\n");
392 item = KDBUS_PART_NEXT(item);
393 MSG_ITEM_BUILD_VEC(body_data, body_size);
400 item = KDBUS_PART_NEXT(item);
401 item->type = KDBUS_MSG_FDS;
402 item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
403 memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
408 item = KDBUS_PART_NEXT(item);
409 item->type = KDBUS_MSG_DST_NAME;
410 item->size = KDBUS_PART_HEADER_SIZE + strlen(destination) + 1;
411 memcpy(item->str, destination, item->size - KDBUS_PART_HEADER_SIZE);
413 else if (dst_id == KDBUS_DST_ID_BROADCAST)
415 item = KDBUS_PART_NEXT(item);
416 item->type = KDBUS_MSG_BLOOM;
417 item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
418 strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
422 if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
426 else if(errno == ENXIO) //no such id on the bus
428 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
431 else if((errno == ESRCH) || (errno = EADDRNOTAVAIL)) //when well known name is not available on the bus
435 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))
439 if(!reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
442 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
448 close(transport->memfd);
454 * Performs kdbus hello - registration on the kdbus bus
456 * @param name place to store unique name given by bus
457 * @param transportS transport structure
458 * @returns #TRUE on success
460 static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS)
462 struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
464 hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
465 KDBUS_HELLO_ATTACH_COMM |
466 KDBUS_HELLO_ATTACH_EXE |
467 KDBUS_HELLO_ATTACH_CMDLINE |
468 KDBUS_HELLO_ATTACH_CAPS |
469 KDBUS_HELLO_ATTACH_CGROUP |
470 KDBUS_HELLO_ATTACH_SECLABEL |
471 KDBUS_HELLO_ATTACH_AUDIT*/;
472 hello.size = sizeof(struct kdbus_cmd_hello);
473 hello.pool_size = RECEIVE_POOL_SIZE;
475 if (ioctl(transportS->fd, KDBUS_CMD_HELLO, &hello))
477 _dbus_verbose ("Failed to send hello: %m, %d",errno);
481 sprintf(name, "%llu", (unsigned long long)hello.id);
482 _dbus_verbose("-- Our peer ID is: %s\n", name);
483 transportS->bloom_size = hello.bloom_size;
485 transportS->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, transportS->fd, 0);
486 if (transportS->kdbus_mmap_ptr == MAP_FAILED)
488 _dbus_verbose("Error when mmap: %m, %d",errno);
496 * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate
497 * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue.
499 * Now it captures only Hello message, which must be handled locally.
500 * All the rest is passed to dbus-daemon.
502 static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
504 if(!strcmp(dbus_message_get_member(message), "Hello"))
508 name = malloc(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."));
512 if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
514 if(!register_kdbus_policy(&name[3], transport, geteuid()))
517 ((DBusTransportKdbus*)transport)->sender = name;
519 if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
526 return 1; //send to daemon
531 #if KDBUS_MSG_DECODE_DEBUG == 1
532 static char *msg_id(uint64_t id, char *buf)
538 sprintf(buf, "%llu", (unsigned long long)id);
542 struct kdbus_enum_table {
546 #define _STRINGIFY(x) #x
547 #define STRINGIFY(x) _STRINGIFY(x)
548 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
549 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
550 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
551 #define LOOKUP(what) \
552 const char *enum_##what(long long id) { \
554 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
555 if (id == kdbus_table_##what[i].id) \
556 return kdbus_table_##what[i].name; \
559 const char *enum_MSG(long long id);
561 ENUM(_KDBUS_MSG_NULL),
562 ENUM(KDBUS_MSG_PAYLOAD_VEC),
563 ENUM(KDBUS_MSG_PAYLOAD_OFF),
564 ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
566 ENUM(KDBUS_MSG_BLOOM),
567 ENUM(KDBUS_MSG_DST_NAME),
568 ENUM(KDBUS_MSG_SRC_CREDS),
569 ENUM(KDBUS_MSG_SRC_PID_COMM),
570 ENUM(KDBUS_MSG_SRC_TID_COMM),
571 ENUM(KDBUS_MSG_SRC_EXE),
572 ENUM(KDBUS_MSG_SRC_CMDLINE),
573 ENUM(KDBUS_MSG_SRC_CGROUP),
574 ENUM(KDBUS_MSG_SRC_CAPS),
575 ENUM(KDBUS_MSG_SRC_SECLABEL),
576 ENUM(KDBUS_MSG_SRC_AUDIT),
577 ENUM(KDBUS_MSG_SRC_NAMES),
578 ENUM(KDBUS_MSG_TIMESTAMP),
579 ENUM(KDBUS_MSG_NAME_ADD),
580 ENUM(KDBUS_MSG_NAME_REMOVE),
581 ENUM(KDBUS_MSG_NAME_CHANGE),
582 ENUM(KDBUS_MSG_ID_ADD),
583 ENUM(KDBUS_MSG_ID_REMOVE),
584 ENUM(KDBUS_MSG_REPLY_TIMEOUT),
585 ENUM(KDBUS_MSG_REPLY_DEAD),
588 const char *enum_PAYLOAD(long long id);
590 ENUM(KDBUS_PAYLOAD_KERNEL),
591 ENUM(KDBUS_PAYLOAD_DBUS1),
592 ENUM(KDBUS_PAYLOAD_GVARIANT),
597 * Puts locally generated message into received data buffer.
598 * Use only during receiving phase!
600 * @param message message to load
601 * @param data place to load message
602 * @return size of message
604 static int put_message_into_data(DBusMessage *message, char* data)
607 const DBusString *header;
608 const DBusString *body;
611 dbus_message_set_serial(message, 1);
612 dbus_message_lock (message);
613 _dbus_message_get_network_data (message, &header, &body);
614 ret_size = _dbus_string_get_length(header);
615 memcpy(data, _dbus_string_get_const_data(header), ret_size);
617 size = _dbus_string_get_length(body);
618 memcpy(data, _dbus_string_get_const_data(body), size);
625 * Get the length of the kdbus message content.
627 * @param msg kdbus message
628 * @return the length of the kdbus message
630 static int kdbus_message_size(const struct kdbus_msg* msg)
632 const struct kdbus_item *item;
635 KDBUS_PART_FOREACH(item, msg, items)
637 if (item->size <= KDBUS_PART_HEADER_SIZE)
639 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
644 case KDBUS_MSG_PAYLOAD_OFF:
645 ret_size += item->vec.size;
647 case KDBUS_MSG_PAYLOAD_MEMFD:
648 ret_size += item->memfd.size;
659 * Decodes kdbus message in order to extract dbus message and put it into data and fds.
660 * Also captures and decodes kdbus error messages and kdbus kernel broadcasts and converts
661 * all of them into dbus messages.
663 * @param msg kdbus message
664 * @param data place to copy dbus message to
665 * @param kdbus_transport transport
666 * @param fds place to store file descriptors received
667 * @param n_fds place to store quantity of file descriptor
668 * @return number of dbus message's bytes received or -1 on error
670 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportKdbus* kdbus_transport, int* fds, int* n_fds)
672 const struct kdbus_item *item;
674 DBusMessage *message = NULL;
675 DBusMessageIter args;
676 const char* emptyString = "";
677 const char* pString = NULL;
678 char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
679 const char* pDBusName = dbus_name;
680 #if KDBUS_MSG_DECODE_DEBUG == 1
684 #if KDBUS_MSG_DECODE_DEBUG == 1
685 _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
686 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
687 (unsigned long long) msg->flags,
688 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
689 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
694 KDBUS_PART_FOREACH(item, msg, items)
696 if (item->size <= KDBUS_PART_HEADER_SIZE)
698 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
699 break; //??? continue (because dbus will find error) or break
704 case KDBUS_MSG_PAYLOAD_OFF:
705 memcpy(data, (char *)kdbus_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
706 data += item->vec.size;
707 ret_size += item->vec.size;
709 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
710 enum_MSG(item->type), item->size,
711 (unsigned long long)item->vec.offset,
712 (unsigned long long)item->vec.size);
715 case KDBUS_MSG_PAYLOAD_MEMFD:
720 size = item->memfd.size;
721 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
723 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
724 if (buf == MAP_FAILED)
726 _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
730 memcpy(data, buf, size);
736 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
737 enum_MSG(item->type), item->size,
738 (unsigned long long)item->vec.offset,
739 (unsigned long long)item->vec.size);
747 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
748 memcpy(fds, item->fds, *n_fds * sizeof(int));
749 for (i = 0; i < *n_fds; i++)
750 _dbus_fd_set_close_on_exec(fds[i]);
754 #if KDBUS_MSG_DECODE_DEBUG == 1
755 case KDBUS_MSG_SRC_CREDS:
756 _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
757 enum_MSG(item->type), item->size,
758 item->creds.uid, item->creds.gid,
759 item->creds.pid, item->creds.tid,
760 item->creds.starttime);
763 case KDBUS_MSG_SRC_PID_COMM:
764 case KDBUS_MSG_SRC_TID_COMM:
765 case KDBUS_MSG_SRC_EXE:
766 case KDBUS_MSG_SRC_CGROUP:
767 case KDBUS_MSG_SRC_SECLABEL:
768 case KDBUS_MSG_DST_NAME:
769 _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
770 enum_MSG(item->type), item->size, item->str, strlen(item->str));
773 case KDBUS_MSG_SRC_CMDLINE:
774 case KDBUS_MSG_SRC_NAMES: {
775 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
776 const char *str = item->str;
779 _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
781 _dbus_verbose("'%s' ", str);
782 size -= strlen(str) + 1;
783 str += strlen(str) + 1;
787 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
791 case KDBUS_MSG_SRC_AUDIT:
792 _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
793 enum_MSG(item->type), item->size,
794 (unsigned long long)item->data64[0],
795 (unsigned long long)item->data64[1]);
798 case KDBUS_MSG_SRC_CAPS: {
803 _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
804 enum_MSG(item->type), item->size,
805 (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
808 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
810 _dbus_verbose(" CapInh=");
811 for (i = 0; i < n; i++)
812 _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
814 _dbus_verbose(" CapPrm=");
815 for (i = 0; i < n; i++)
816 _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
818 _dbus_verbose(" CapEff=");
819 for (i = 0; i < n; i++)
820 _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
822 _dbus_verbose(" CapInh=");
823 for (i = 0; i < n; i++)
824 _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
829 case KDBUS_MSG_TIMESTAMP:
830 _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
831 enum_MSG(item->type), item->size,
832 (unsigned long long)item->timestamp.realtime_ns,
833 (unsigned long long)item->timestamp.monotonic_ns);
837 case KDBUS_MSG_REPLY_TIMEOUT:
838 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
839 enum_MSG(item->type), item->size, msg->cookie_reply);
841 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
848 ret_size = put_message_into_data(message, data);
851 case KDBUS_MSG_REPLY_DEAD:
852 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
853 enum_MSG(item->type), item->size, msg->cookie_reply);
855 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
862 ret_size = put_message_into_data(message, data);
865 case KDBUS_MSG_NAME_ADD:
866 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
867 enum_MSG(item->type), (unsigned long long) item->size,
868 item->name_change.name, item->name_change.old_id,
869 item->name_change.new_id, item->name_change.flags);
871 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
878 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
879 pString = item->name_change.name;
880 _dbus_verbose ("Name added: %s\n", pString);
881 dbus_message_iter_init_append(message, &args);
882 ITER_APPEND_STR(pString)
883 ITER_APPEND_STR(emptyString)
884 ITER_APPEND_STR(pDBusName)
885 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
887 ret_size = put_message_into_data(message, data);
890 case KDBUS_MSG_NAME_REMOVE:
891 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
892 enum_MSG(item->type), (unsigned long long) item->size,
893 item->name_change.name, item->name_change.old_id,
894 item->name_change.new_id, item->name_change.flags);
896 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
903 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
904 pString = item->name_change.name;
905 _dbus_verbose ("Name removed: %s\n", pString);
906 dbus_message_iter_init_append(message, &args);
907 ITER_APPEND_STR(pString)
908 ITER_APPEND_STR(pDBusName)
909 ITER_APPEND_STR(emptyString)
910 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
912 ret_size = put_message_into_data(message, data);
915 case KDBUS_MSG_NAME_CHANGE:
916 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
917 enum_MSG(item->type), (unsigned long long) item->size,
918 item->name_change.name, item->name_change.old_id,
919 item->name_change.new_id, item->name_change.flags);
921 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
928 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
929 pString = item->name_change.name;
930 _dbus_verbose ("Name changed: %s\n", pString);
931 dbus_message_iter_init_append(message, &args);
932 ITER_APPEND_STR(pString)
933 ITER_APPEND_STR(pDBusName)
934 sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
935 _dbus_verbose ("New id: %s\n", pDBusName);
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_ADD:
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(emptyString)
959 ITER_APPEND_STR(pDBusName)
960 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
962 ret_size = put_message_into_data(message, data);
965 case KDBUS_MSG_ID_REMOVE:
966 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
967 enum_MSG(item->type), (unsigned long long) item->size,
968 (unsigned long long) item->id_change.id,
969 (unsigned long long) item->id_change.flags);
971 message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
978 sprintf(dbus_name,":1.%llu",item->id_change.id);
979 dbus_message_iter_init_append(message, &args);
980 ITER_APPEND_STR(pDBusName)
981 ITER_APPEND_STR(pDBusName)
982 ITER_APPEND_STR(emptyString)
983 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
985 ret_size = put_message_into_data(message, data);
987 #if KDBUS_MSG_DECODE_DEBUG == 1
989 _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
995 #if KDBUS_MSG_DECODE_DEBUG == 1
997 if ((char *)item - ((char *)msg + msg->size) >= 8)
998 _dbus_verbose("invalid padding at end of message\n");
1003 dbus_message_unref(message);
1008 * Reads message from kdbus and puts it into dbus buffer and fds
1010 * @param transport transport
1011 * @param buffer place to copy received message to
1012 * @param fds place to store file descriptors sent in the message
1013 * @param n_fds place to store number of file descriptors
1014 * @return size of received message on success, -1 on error
1016 static int kdbus_read_message(DBusTransportKdbus *socket_transport, DBusString *buffer, int* fds, int* n_fds)
1018 int ret_size, buf_size;
1019 uint64_t __attribute__ ((__aligned__(8))) offset;
1020 struct kdbus_msg *msg;
1024 start = _dbus_string_get_length (buffer);
1027 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1031 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1032 _dbus_string_set_length (buffer, start);
1036 msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
1038 buf_size = kdbus_message_size(msg);
1041 _dbus_verbose("kdbus error - too short message: %d (%m)\n", errno);
1045 /* What is the maximum size of the locally generated message?
1046 I just assume 2048 bytes */
1047 buf_size = MAX(buf_size, 2048);
1049 if (!_dbus_string_lengthen (buffer, buf_size))
1054 data = _dbus_string_get_data_len (buffer, start, buf_size);
1056 ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
1058 if(ret_size == -1) /* error */
1060 _dbus_string_set_length (buffer, start);
1063 else if (buf_size != ret_size) /* case of locally generated message */
1065 _dbus_string_set_length (buffer, start + ret_size);
1069 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
1073 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1080 * copy-paste from socket transport with needed renames only.
1083 free_watches (DBusTransport *transport)
1085 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1087 _dbus_verbose ("start\n");
1089 if (kdbus_transport->read_watch)
1091 if (transport->connection)
1092 _dbus_connection_remove_watch_unlocked (transport->connection,
1093 kdbus_transport->read_watch);
1094 _dbus_watch_invalidate (kdbus_transport->read_watch);
1095 _dbus_watch_unref (kdbus_transport->read_watch);
1096 kdbus_transport->read_watch = NULL;
1099 if (kdbus_transport->write_watch)
1101 if (transport->connection)
1102 _dbus_connection_remove_watch_unlocked (transport->connection,
1103 kdbus_transport->write_watch);
1104 _dbus_watch_invalidate (kdbus_transport->write_watch);
1105 _dbus_watch_unref (kdbus_transport->write_watch);
1106 kdbus_transport->write_watch = NULL;
1109 _dbus_verbose ("end\n");
1113 * copy-paste from socket transport with needed renames only.
1116 transport_finalize (DBusTransport *transport)
1118 _dbus_verbose ("\n");
1120 free_watches (transport);
1122 _dbus_transport_finalize_base (transport);
1124 _dbus_assert (((DBusTransportKdbus*) transport)->read_watch == NULL);
1125 _dbus_assert (((DBusTransportKdbus*) transport)->write_watch == NULL);
1127 dbus_free (transport);
1131 * copy-paste from socket transport with needed renames only.
1134 check_write_watch (DBusTransport *transport)
1136 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1139 if (transport->connection == NULL)
1142 if (transport->disconnected)
1144 _dbus_assert (kdbus_transport->write_watch == NULL);
1148 _dbus_transport_ref (transport);
1150 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1152 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1153 needed, transport->connection, kdbus_transport->write_watch,
1154 kdbus_transport->fd,
1155 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1157 _dbus_connection_toggle_watch_unlocked (transport->connection,
1158 kdbus_transport->write_watch,
1161 _dbus_transport_unref (transport);
1165 * copy-paste from socket transport with needed renames only.
1168 check_read_watch (DBusTransport *transport)
1170 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1171 dbus_bool_t need_read_watch;
1173 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1175 if (transport->connection == NULL)
1178 if (transport->disconnected)
1180 _dbus_assert (kdbus_transport->read_watch == NULL);
1184 _dbus_transport_ref (transport);
1187 (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1188 (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1190 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
1191 _dbus_connection_toggle_watch_unlocked (transport->connection,
1192 kdbus_transport->read_watch,
1195 _dbus_transport_unref (transport);
1199 * copy-paste from socket transport.
1202 do_io_error (DBusTransport *transport)
1204 _dbus_transport_ref (transport);
1205 _dbus_transport_disconnect (transport);
1206 _dbus_transport_unref (transport);
1209 /* returns false on oom */
1211 do_writing (DBusTransport *transport)
1213 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1216 if (transport->disconnected)
1218 _dbus_verbose ("Not connected, not writing anything\n");
1223 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
1224 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
1225 kdbus_transport->fd);
1230 while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1233 DBusMessage *message;
1234 const DBusString *header;
1235 const DBusString *body;
1236 int total_bytes_to_write;
1237 const char* pDestination;
1239 message = _dbus_connection_get_message_to_send (transport->connection);
1240 _dbus_assert (message != NULL);
1241 if(dbus_message_get_sender(message) == NULL) //needed for daemon to pass pending activation messages
1243 dbus_message_unlock(message);
1244 dbus_message_set_sender(message, kdbus_transport->sender);
1245 dbus_message_lock (message);
1247 _dbus_message_get_network_data (message, &header, &body);
1248 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
1249 pDestination = dbus_message_get_destination(message);
1253 if(!strcmp(pDestination, DBUS_SERVICE_DBUS))
1255 if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
1259 ret = emulateOrgFreedesktopDBus(transport, message);
1267 bytes_written = total_bytes_to_write;
1270 //else send to "daemon" as to normal recipient
1275 if(total_bytes_to_write > kdbus_transport->max_bytes_written_per_iteration)
1278 bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination);
1282 if (bytes_written < 0)
1284 /* EINTR already handled for us */
1286 /* For some discussion of why we also ignore EPIPE here, see
1287 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
1290 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
1294 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
1295 do_io_error (transport);
1301 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
1302 total_bytes_to_write);
1304 kdbus_transport->message_bytes_written += bytes_written;
1306 _dbus_assert (kdbus_transport->message_bytes_written <=
1307 total_bytes_to_write);
1309 if (kdbus_transport->message_bytes_written == total_bytes_to_write)
1311 kdbus_transport->message_bytes_written = 0;
1312 _dbus_connection_message_sent_unlocked (transport->connection,
1324 /* returns false on out-of-memory */
1326 do_reading (DBusTransport *transport)
1328 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1331 dbus_bool_t oom = FALSE;
1334 _dbus_verbose ("fd = %d\n",kdbus_transport->fd);
1338 /* See if we've exceeded max messages and need to disable reading */
1339 check_read_watch (transport);
1341 _dbus_assert (kdbus_transport->read_watch != NULL ||
1342 transport->disconnected);
1344 if (transport->disconnected)
1347 if (!dbus_watch_get_enabled (kdbus_transport->read_watch))
1350 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1352 _dbus_verbose ("Out of memory reading file descriptors\n");
1356 _dbus_message_loader_get_buffer (transport->loader, &buffer);
1358 bytes_read = kdbus_read_message(kdbus_transport, buffer, fds, &n_fds);
1360 if (bytes_read >= 0 && n_fds > 0)
1361 _dbus_verbose("Read %i unix fds\n", n_fds);
1363 _dbus_message_loader_return_buffer (transport->loader,
1365 bytes_read < 0 ? 0 : bytes_read);
1366 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1370 /* EINTR already handled for us */
1372 if (_dbus_get_is_errno_enomem ())
1374 _dbus_verbose ("Out of memory in read()/do_reading()\n");
1378 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1382 _dbus_verbose ("Error reading from remote app: %s\n",
1383 _dbus_strerror_from_errno ());
1384 do_io_error (transport);
1388 else if (bytes_read == 0)
1390 _dbus_verbose ("Disconnected from remote app\n");
1391 do_io_error (transport);
1396 _dbus_verbose (" read %d bytes\n", bytes_read);
1398 if (!_dbus_transport_queue_messages (transport))
1401 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
1405 /* Try reading more data until we get EAGAIN and return, or
1406 * exceed max bytes per iteration. If in blocking mode of
1407 * course we'll block instead of returning.
1419 * copy-paste from socket transport.
1422 unix_error_with_read_to_come (DBusTransport *itransport,
1426 DBusTransportKdbus *transport = (DBusTransportKdbus *) itransport;
1428 if (!((flags & DBUS_WATCH_HANGUP) || (flags & DBUS_WATCH_ERROR)))
1431 /* If we have a read watch enabled ...
1432 we -might have data incoming ... => handle the HANGUP there */
1433 if (watch != transport->read_watch && _dbus_watch_get_enabled (transport->read_watch))
1440 * copy-paste from socket transport with needed renames only.
1443 kdbus_handle_watch (DBusTransport *transport,
1447 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1449 _dbus_assert (watch == kdbus_transport->read_watch ||
1450 watch == kdbus_transport->write_watch);
1451 _dbus_assert (watch != NULL);
1453 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
1454 * still be in the buffer and do_reading may need several iteration to read
1455 * it all (because of its max_bytes_read_per_iteration limit).
1457 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
1459 _dbus_verbose ("Hang up or error on watch\n");
1460 _dbus_transport_disconnect (transport);
1464 if (watch == kdbus_transport->read_watch &&
1465 (flags & DBUS_WATCH_READABLE))
1467 _dbus_verbose ("handling read watch %p flags = %x\n",
1470 if (!do_reading (transport))
1472 _dbus_verbose ("no memory to read\n");
1477 else if (watch == kdbus_transport->write_watch &&
1478 (flags & DBUS_WATCH_WRITABLE))
1480 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1481 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1483 if (!do_writing (transport))
1485 _dbus_verbose ("no memory to write\n");
1489 /* See if we still need the write watch */
1490 check_write_watch (transport);
1497 * copy-paste from socket transport with needed renames only.
1500 kdbus_disconnect (DBusTransport *transport)
1502 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1504 _dbus_verbose ("\n");
1506 free_watches (transport);
1508 _dbus_close_socket (kdbus_transport->fd, NULL);
1509 kdbus_transport->fd = -1;
1513 * copy-paste from socket transport with needed renames only.
1516 kdbus_connection_set (DBusTransport *transport)
1518 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1520 dbus_connection_set_is_authenticated(transport->connection); //now we don't have authentication in kdbus
1522 _dbus_watch_set_handler (kdbus_transport->write_watch,
1523 _dbus_connection_handle_watch,
1524 transport->connection, NULL);
1526 _dbus_watch_set_handler (kdbus_transport->read_watch,
1527 _dbus_connection_handle_watch,
1528 transport->connection, NULL);
1530 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1531 kdbus_transport->write_watch))
1534 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1535 kdbus_transport->read_watch))
1537 _dbus_connection_remove_watch_unlocked (transport->connection,
1538 kdbus_transport->write_watch);
1542 check_read_watch (transport);
1543 check_write_watch (transport);
1548 /** original dbus copy-pasted comment
1549 * @todo We need to have a way to wake up the select sleep if
1550 * a new iteration request comes in with a flag (read/write) that
1551 * we're not currently serving. Otherwise a call that just reads
1552 * could block a write call forever (if there are no incoming
1556 kdbus_do_iteration (DBusTransport *transport,
1558 int timeout_milliseconds)
1560 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1565 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
1566 flags & DBUS_ITERATION_DO_READING ? "read" : "",
1567 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1568 timeout_milliseconds,
1569 kdbus_transport->read_watch,
1570 kdbus_transport->write_watch,
1571 kdbus_transport->fd);
1573 /* the passed in DO_READING/DO_WRITING flags indicate whether to
1574 * read/write messages, but regardless of those we may need to block
1575 * for reading/writing to do auth. But if we do reading for auth,
1576 * we don't want to read any messages yet if not given DO_READING.
1579 poll_fd.fd = kdbus_transport->fd;
1582 if (_dbus_transport_try_to_authenticate (transport))
1584 /* This is kind of a hack; if we have stuff to write, then try
1585 * to avoid the poll. This is probably about a 5% speedup on an
1586 * echo client/server.
1588 * If both reading and writing were requested, we want to avoid this
1589 * since it could have funky effects:
1590 * - both ends spinning waiting for the other one to read
1591 * data so they can finish writing
1592 * - prioritizing all writing ahead of reading
1594 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1595 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1596 !transport->disconnected &&
1597 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1599 do_writing (transport);
1601 if (transport->disconnected ||
1602 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
1606 /* If we get here, we decided to do the poll() after all */
1607 _dbus_assert (kdbus_transport->read_watch);
1608 if (flags & DBUS_ITERATION_DO_READING)
1609 poll_fd.events |= _DBUS_POLLIN;
1611 _dbus_assert (kdbus_transport->write_watch);
1612 if (flags & DBUS_ITERATION_DO_WRITING)
1613 poll_fd.events |= _DBUS_POLLOUT;
1617 DBusAuthState auth_state;
1619 auth_state = _dbus_auth_do_work (transport->auth);
1621 if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1622 poll_fd.events |= _DBUS_POLLIN;
1624 if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1625 poll_fd.events |= _DBUS_POLLOUT;
1630 if (flags & DBUS_ITERATION_BLOCK)
1631 poll_timeout = timeout_milliseconds;
1635 /* For blocking selects we drop the connection lock here
1636 * to avoid blocking out connection access during a potentially
1637 * indefinite blocking call. The io path is still protected
1638 * by the io_path_cond condvar, so we won't reenter this.
1640 if (flags & DBUS_ITERATION_BLOCK)
1642 _dbus_verbose ("unlock pre poll\n");
1643 _dbus_connection_unlock (transport->connection);
1647 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1649 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1651 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
1655 if (flags & DBUS_ITERATION_BLOCK)
1657 _dbus_verbose ("lock post poll\n");
1658 _dbus_connection_lock (transport->connection);
1664 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1665 * valgrind flags it as an error. though it probably
1666 * is guaranteed on linux at least.
1669 if (poll_fd.revents & _DBUS_POLLERR)
1670 do_io_error (transport);
1673 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1674 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1676 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1677 need_read, need_write);
1679 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1680 do_reading (transport);
1681 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1682 do_writing (transport);
1686 _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
1690 /* We need to install the write watch only if we did not
1691 * successfully write everything. Note we need to be careful that we
1692 * don't call check_write_watch *before* do_writing, since it's
1693 * inefficient to add the write watch, and we can avoid it most of
1694 * the time since we can write immediately.
1696 * However, we MUST always call check_write_watch(); DBusConnection code
1697 * relies on the fact that running an iteration will notice that
1698 * messages are pending.
1700 check_write_watch (transport);
1702 _dbus_verbose (" ... leaving do_iteration()\n");
1706 * copy-paste from socket transport with needed renames only.
1709 kdbus_live_messages_changed (DBusTransport *transport)
1711 /* See if we should look for incoming messages again */
1712 check_read_watch (transport);
1716 kdbus_get_kdbus_fd (DBusTransport *transport,
1719 DBusTransportKdbus *kdbus_transport = (DBusTransportKdbus*) transport;
1721 *fd_p = kdbus_transport->fd;
1726 static const DBusTransportVTable kdbus_vtable = {
1730 kdbus_connection_set,
1732 kdbus_live_messages_changed,
1737 * Creates a new transport for the given kdbus file descriptor. The file
1738 * descriptor must be nonblocking.
1740 * @param fd the file descriptor.
1741 * @param address the transport's address
1742 * @returns the new transport, or #NULL if no memory.
1744 static DBusTransport*
1745 _dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
1747 DBusTransportKdbus *kdbus_transport;
1749 kdbus_transport = dbus_new0 (DBusTransportKdbus, 1);
1750 if (kdbus_transport == NULL)
1753 kdbus_transport->write_watch = _dbus_watch_new (fd,
1754 DBUS_WATCH_WRITABLE,
1757 if (kdbus_transport->write_watch == NULL)
1760 kdbus_transport->read_watch = _dbus_watch_new (fd,
1761 DBUS_WATCH_READABLE,
1764 if (kdbus_transport->read_watch == NULL)
1767 if (!_dbus_transport_init_base (&kdbus_transport->base,
1772 kdbus_transport->fd = fd;
1773 kdbus_transport->message_bytes_written = 0;
1775 /* These values should probably be tunable or something. */
1776 kdbus_transport->max_bytes_read_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
1777 kdbus_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
1779 kdbus_transport->kdbus_mmap_ptr = NULL;
1780 kdbus_transport->memfd = -1;
1782 return (DBusTransport*) kdbus_transport;
1785 _dbus_watch_invalidate (kdbus_transport->read_watch);
1786 _dbus_watch_unref (kdbus_transport->read_watch);
1788 _dbus_watch_invalidate (kdbus_transport->write_watch);
1789 _dbus_watch_unref (kdbus_transport->write_watch);
1791 dbus_free (kdbus_transport);
1796 * Opens a connection to the kdbus bus
1798 * This will set FD_CLOEXEC for the socket returned.
1800 * @param path the path to UNIX domain socket
1801 * @param error return location for error code
1802 * @returns connection file descriptor or -1 on error
1804 static int _dbus_connect_kdbus (const char *path, DBusError *error)
1808 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1809 _dbus_verbose ("connecting to kdbus bus %s\n", path);
1811 fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
1813 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
1819 * Creates a new transport for kdbus.
1820 * This creates a client-side of a transport.
1822 * @param path the path to the bus.
1823 * @param error address where an error can be returned.
1824 * @returns a new transport, or #NULL on failure.
1826 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
1829 DBusTransport *transport;
1832 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1834 if (!_dbus_string_init (&address))
1836 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1842 if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
1844 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1848 fd = _dbus_connect_kdbus (path, error);
1851 _DBUS_ASSERT_ERROR_IS_SET (error);
1855 _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
1857 transport = _dbus_transport_new_kdbus_transport (fd, &address);
1858 if (transport == NULL)
1860 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1864 _dbus_string_free (&address);
1869 _dbus_close_socket (fd, NULL);
1871 _dbus_string_free (&address);
1877 * Opens kdbus transport if method from address entry is kdbus
1879 * @param entry the address entry to try opening
1880 * @param transport_p return location for the opened transport
1881 * @param error error to be set
1882 * @returns result of the attempt
1884 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry,
1885 DBusTransport **transport_p,
1890 method = dbus_address_entry_get_method (entry);
1891 _dbus_assert (method != NULL);
1893 if (strcmp (method, "kdbus") == 0)
1895 const char *path = dbus_address_entry_get_value (entry, "path");
1899 _dbus_set_bad_address (error, "kdbus", "path", NULL);
1900 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1903 *transport_p = _dbus_transport_new_for_kdbus (path, error);
1905 if (*transport_p == NULL)
1907 _DBUS_ASSERT_ERROR_IS_SET (error);
1908 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1912 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1913 return DBUS_TRANSPORT_OPEN_OK;
1918 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1919 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;