2 * dbus-transport-kdbus.c
4 * Transport layer using kdbus
6 * Created on: Jun 20, 2013
12 #include "dbus-transport.h"
13 #include "dbus-transport-kdbus.h"
14 #include <dbus/dbus-transport-protected.h>
15 #include "dbus-connection-internal.h"
17 #include "dbus-watch.h"
18 #include "dbus-errors.h"
20 #include <linux/types.h>
24 #include <sys/ioctl.h>
32 #define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
33 #define KDBUS_PART_HEADER_SIZE offsetof(struct kdbus_item, data)
34 #define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
36 #define KDBUS_PART_NEXT(part) \
37 (typeof(part))(((uint8_t *)part) + KDBUS_ALIGN8((part)->size))
38 #define KDBUS_PART_FOREACH(part, head, first) \
39 for (part = (head)->first; \
40 (uint8_t *)(part) < (uint8_t *)(head) + (head)->size; \
41 part = KDBUS_PART_NEXT(part))
42 #define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU)
43 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used
45 #define KDBUS_MSG_DECODE_DEBUG 0
49 * Opaque object representing a socket file descriptor transport.
51 typedef struct DBusTransportSocket DBusTransportSocket;
54 * Implementation details of DBusTransportSocket. All members are private.
56 struct DBusTransportSocket
58 DBusTransport base; /**< Parent instance */
59 int fd; /**< File descriptor. */
60 DBusWatch *read_watch; /**< Watch for readability. */
61 DBusWatch *write_watch; /**< Watch for writability. */
63 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
64 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
66 int message_bytes_written; /**< Number of bytes of current
67 * outgoing message that have
70 DBusString encoded_outgoing; /**< Encoded version of current
73 DBusString encoded_incoming; /**< Encoded version of current
77 int memfd; /* File descriptor to memory
78 * pool from Kdbus kernel module */
84 socket_get_socket_fd (DBusTransport *transport,
87 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
89 *fd_p = socket_transport->fd;
94 //static void check_read_watch (DBusTransport *transport);
97 * Adds locally generated message to received messages queue
100 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusTransport *transport)
102 DBusList *message_link;
104 message_link = _dbus_list_alloc_link (message);
105 if (message_link == NULL)
107 /* it's OK to unref this, nothing that could have attached a callback
108 * has ever seen it */
109 dbus_message_unref (message);
113 _dbus_connection_queue_synthesized_message_link(transport->connection, message_link);
114 // check_read_watch(transport);
119 /*static void kdbus_debug_print_bytes(struct DBusString *data, int size)
123 fprintf (stderr, "\ndata:\n");
124 for(i=0; i < size; i++)
126 fprintf (stderr, "%02x", _dbus_string_get_byte(data,i));
128 fprintf (stderr, "\nsize: %d, i: %lu\n", size, i);
132 static int kdbus_init_memfd(DBusTransportSocket* socket_transport)
137 if(socket_transport->memfd == -1)
139 ret = ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd);
141 _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
145 socket_transport->memfd = memfd;
146 _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd);
152 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)
154 struct kdbus_msg* msg;
157 msg_size = sizeof(struct kdbus_msg);
159 if(use_memfd == TRUE) // bulk data - memfd - encoded and plain
160 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
162 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
164 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
168 msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
171 msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
172 else if (dst_id == KDBUS_DST_ID_BROADCAST)
173 msg_size += KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
175 msg = malloc(msg_size);
178 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
182 memset(msg, 0, msg_size);
183 msg->size = msg_size;
184 msg->payload_type = KDBUS_PAYLOAD_DBUS1;
185 msg->dst_id = name ? 0 : dst_id;
190 static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message, dbus_bool_t encoded)
192 struct kdbus_msg *msg;
193 struct kdbus_item *item;
195 uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
196 const DBusString *header;
197 const DBusString *body;
198 uint64_t ret_size = 0;
199 uint64_t body_size = 0;
200 uint64_t header_size = 0;
202 dbus_bool_t use_memfd;
207 // determine name and destination id
208 if((name = dbus_message_get_destination(message)))
210 dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
212 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
214 dst_id = strtoll(&name[3], NULL, 10);
221 ret_size = _dbus_string_get_length (&transport->encoded_outgoing);
224 _dbus_message_get_network_data (message, &header, &body);
225 header_size = _dbus_string_get_length(header);
226 body_size = _dbus_string_get_length(body);
227 ret_size = header_size + body_size;
230 // check if message size is big enough to use memfd kdbus transport
231 use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
232 if(use_memfd) kdbus_init_memfd(transport);
234 _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
236 // init basic message fields
237 msg = kdbus_init_msg(name, dst_id, body_size, use_memfd, fds_count);
238 msg->cookie = dbus_message_get_serial(message);
239 msg->src_id = strtoll(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
241 // build message contents
244 // case 1 - bulk data transfer - memfd - encoded and plain
247 ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0);
250 _dbus_verbose("memfd sealing failed: \n");
254 buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
255 if (buf == MAP_FAILED)
257 _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
262 memcpy(buf, &transport->encoded_outgoing, ret_size);
265 memcpy(buf, _dbus_string_get_const_data(header), header_size);
268 memcpy(buf, _dbus_string_get_const_data(body), body_size);
273 munmap(buf, ret_size);
275 // seal data - kdbus module needs it
276 ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1);
278 _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
283 item->type = KDBUS_MSG_PAYLOAD_MEMFD;
284 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
285 item->memfd.size = ret_size;
286 item->memfd.fd = transport->memfd;
287 // case 2 - small encoded - don't use memfd
289 _dbus_verbose("sending encoded data\n");
291 item->type = KDBUS_MSG_PAYLOAD_VEC;
292 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
293 item->vec.address = (unsigned long) &transport->encoded_outgoing;
294 item->vec.size = _dbus_string_get_length (&transport->encoded_outgoing);
296 // case 3 - small not encoded - don't use memfd
298 _dbus_verbose("sending normal vector data\n");
300 item->type = KDBUS_MSG_PAYLOAD_VEC;
301 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
302 item->vec.address = (unsigned long) _dbus_string_get_const_data(header);
303 item->vec.size = header_size;
307 _dbus_verbose("body attaching\n");
308 item = KDBUS_PART_NEXT(item);
309 item->type = KDBUS_MSG_PAYLOAD_VEC;
310 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
311 item->vec.address = (unsigned long) _dbus_string_get_const_data(body);
312 item->vec.size = body_size;
318 item = KDBUS_PART_NEXT(item);
319 item->type = KDBUS_MSG_FDS;
320 item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
321 memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
326 item = KDBUS_PART_NEXT(item);
327 item->type = KDBUS_MSG_DST_NAME;
328 item->size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
329 strcpy(item->str, name);
331 else if (dst_id == KDBUS_DST_ID_BROADCAST)
333 item = KDBUS_PART_NEXT(item);
334 item->type = KDBUS_MSG_BLOOM;
335 item->size = KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
336 strncpy(item->data, dbus_message_get_interface(message), KDBUS_BLOOM_SIZE_BYTES);
340 if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
344 if((errno == ESRCH) || (errno == ENXIO)) //when recipient is not available on the bus
346 DBusMessage *errMessage = NULL;
347 dbus_uint32_t replySerial;
349 errMessage = generate_local_error_message(msg->cookie, DBUS_ERROR_SERVICE_UNKNOWN, (char*)dbus_message_get_destination(message));
350 if(errMessage == NULL)
355 replySerial = dbus_message_get_reply_serial(message);
357 dbus_message_set_reply_serial(errMessage, replySerial);
358 if (!add_message_to_received(errMessage, (DBusTransport*)transport))
362 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
371 static int64_t kdbus_NameQuery(DBusTransport *transport, char* name, int fd)
373 struct kdbus_cmd_name_info *msg;
374 struct kdbus_item *item;
379 item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
380 item_size = (item_size < 56) ? 56 : item_size;
381 size = sizeof(struct kdbus_cmd_name_info) + item_size;
386 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
390 memset(msg, 0, size);
394 item->type = KDBUS_NAME_INFO_ITEM_NAME;
395 item->size = item_size;
396 strcpy(item->str, name);
399 if ((ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg)))
412 static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message, int fd)
416 if(!strcmp(dbus_message_get_member(message), "GetNameOwner"))
420 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
421 _dbus_verbose ("Name to discover: %s !!!! !!!!\n", name);
422 ret = kdbus_NameQuery(transport, name, fd);
423 if(ret > 0) //unique id of the name
426 DBusMessageIter args;
427 char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + 4)]; //+3 prefix ":1." and +1 closing NULL
428 const char* pString = unique_name;
430 sprintf(unique_name, ":1.%lld", (long long int)ret);
431 _dbus_verbose("Unique name discovered:%s!!! !!!!\n", unique_name);
432 reply = dbus_message_new_method_return(message);
435 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
436 dbus_message_iter_init_append(reply, &args);
437 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pString))
439 if(add_message_to_received(reply, transport))
442 else if(ret == -ENOENT) //name has no owner
444 DBusMessage *errMessage;
445 dbus_uint32_t replySerial;
447 errMessage = generate_local_error_message(1, DBUS_ERROR_NAME_HAS_NO_OWNER, name);
448 if(errMessage == NULL)
450 replySerial = dbus_message_get_reply_serial(message);
452 dbus_message_set_reply_serial(errMessage, replySerial);
453 if (add_message_to_received(errMessage, transport))
457 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
459 else if(!strcmp(dbus_message_get_member(message), "NameHasOwner"))
463 DBusMessageIter args;
466 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
467 _dbus_verbose ("Name to discover: %s !!!! !!!!\n", name);
468 ret = kdbus_NameQuery(transport, name, fd);
470 result = (ret > 0) ? TRUE : FALSE;
471 _dbus_verbose("Discovery: %d !!! !!!!\n", (int)result);
472 reply = dbus_message_new_method_return(message);
475 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
476 dbus_message_iter_init_append(reply, &args);
477 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &result))
479 if(add_message_to_received(reply, transport))
482 else if(!strcmp(dbus_message_get_member(message), "ListNames"))
484 struct kdbus_cmd_names* pCmd;
487 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
488 pCmd = malloc(cmd_size);
491 pCmd->size = cmd_size;
495 if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
500 cmd_size = pCmd->size;
503 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
509 pCmd = realloc(pCmd, cmd_size);
517 DBusMessageIter args;
518 struct kdbus_cmd_name* pCmd_name;
521 reply = dbus_message_new_method_return(message);
524 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
525 dbus_message_iter_init_append(reply, &args);
527 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
529 pName = pCmd_name->name;
530 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pName))
534 if(add_message_to_received(reply, transport))
545 else //temporarily we send that info but methods below should be implemented
548 dbus_uint32_t replySerial;
550 reply = generate_local_error_message(1, DBUS_ERROR_UNKNOWN_METHOD, NULL);
553 replySerial = dbus_message_get_reply_serial(message);
555 dbus_message_set_reply_serial(reply, replySerial);
556 if(add_message_to_received(reply, transport))
559 /* else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames"))
563 else if(!strcmp(dbus_message_get_member(message), "StartServiceByName"))
567 else if(!strcmp(dbus_message_get_member(message), "UpdateActivationEnvironment"))
571 else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
575 else if(!strcmp(dbus_message_get_member(message), "GetId"))
583 #if KDBUS_MSG_DECODE_DEBUG == 1
584 static char *msg_id(uint64_t id, char *buf)
590 sprintf(buf, "%llu", (unsigned long long)id);
594 struct kdbus_enum_table {
598 #define _STRINGIFY(x) #x
599 #define STRINGIFY(x) _STRINGIFY(x)
600 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
601 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
602 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
603 #define LOOKUP(what) \
604 const char *enum_##what(long long id) { \
606 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
607 if (id == kdbus_table_##what[i].id) \
608 return kdbus_table_##what[i].name; \
611 const char *enum_MSG(long long id);
613 ENUM(_KDBUS_MSG_NULL),
614 ENUM(KDBUS_MSG_PAYLOAD_VEC),
615 ENUM(KDBUS_MSG_PAYLOAD_OFF),
616 ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
618 ENUM(KDBUS_MSG_BLOOM),
619 ENUM(KDBUS_MSG_DST_NAME),
620 ENUM(KDBUS_MSG_SRC_CREDS),
621 ENUM(KDBUS_MSG_SRC_PID_COMM),
622 ENUM(KDBUS_MSG_SRC_TID_COMM),
623 ENUM(KDBUS_MSG_SRC_EXE),
624 ENUM(KDBUS_MSG_SRC_CMDLINE),
625 ENUM(KDBUS_MSG_SRC_CGROUP),
626 ENUM(KDBUS_MSG_SRC_CAPS),
627 ENUM(KDBUS_MSG_SRC_SECLABEL),
628 ENUM(KDBUS_MSG_SRC_AUDIT),
629 ENUM(KDBUS_MSG_SRC_NAMES),
630 ENUM(KDBUS_MSG_TIMESTAMP),
631 ENUM(KDBUS_MSG_NAME_ADD),
632 ENUM(KDBUS_MSG_NAME_REMOVE),
633 ENUM(KDBUS_MSG_NAME_CHANGE),
634 ENUM(KDBUS_MSG_ID_ADD),
635 ENUM(KDBUS_MSG_ID_REMOVE),
636 ENUM(KDBUS_MSG_REPLY_TIMEOUT),
637 ENUM(KDBUS_MSG_REPLY_DEAD),
640 const char *enum_PAYLOAD(long long id);
642 ENUM(KDBUS_PAYLOAD_KERNEL),
643 ENUM(KDBUS_PAYLOAD_DBUS1),
644 ENUM(KDBUS_PAYLOAD_GVARIANT),
648 static int put_message_into_data(DBusMessage *message, char* data)
651 const DBusString *header;
652 const DBusString *body;
655 dbus_message_lock (message);
656 _dbus_message_get_network_data (message, &header, &body);
657 ret_size = _dbus_string_get_length(header);
658 memcpy(data, _dbus_string_get_const_data(header), ret_size);
660 size = _dbus_string_get_length(body);
661 memcpy(data, _dbus_string_get_const_data(body), size);
667 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportSocket* socket_transport, int* fds, int* n_fds)
669 const struct kdbus_item *item = msg->items;
671 DBusMessage *message = NULL;
672 DBusMessageIter args;
673 const char* emptyString = "";
674 const char* pString = NULL;
675 char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + 4)]; //+3 prefix ":1." and +1 closing NULL
676 const char* pDBusName = dbus_name;
678 #if KDBUS_MSG_DECODE_DEBUG == 1
682 #if KDBUS_MSG_DECODE_DEBUG == 1
683 _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
684 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
685 (unsigned long long) msg->flags,
686 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
687 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
691 mmap_ptr = socket_transport->kdbus_mmap_ptr;
693 KDBUS_PART_FOREACH(item, msg, items)
695 if (item->size <= KDBUS_PART_HEADER_SIZE)
697 _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
698 break; //??? continue (because dbus will find error) or break
703 case KDBUS_MSG_PAYLOAD_OFF:
705 uint64_t size = item->vec.size;
707 memcpy(data, (char *)mmap_ptr + item->vec.offset, item->vec.size);
708 data += item->vec.size;
709 ret_size += item->vec.size;
711 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
712 enum_MSG(item->type), item->size,
713 (unsigned long long)item->vec.offset,
714 (unsigned long long)size);
718 case KDBUS_MSG_PAYLOAD_MEMFD:
723 size = item->memfd.size;
725 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
727 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
729 if (buf == MAP_FAILED)
731 _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
735 memcpy(data, buf, size);
741 _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
742 enum_MSG(item->type), item->size,
743 (unsigned long long)item->vec.offset,
744 (unsigned long long)item->vec.size);
752 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
753 memcpy(fds, item->fds, *n_fds * sizeof(int));
754 for (i = 0; i < *n_fds; i++)
755 _dbus_fd_set_close_on_exec(fds[i]);
759 #if KDBUS_MSG_DECODE_DEBUG == 1
760 case KDBUS_MSG_SRC_CREDS:
761 _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
762 enum_MSG(item->type), item->size,
763 item->creds.uid, item->creds.gid,
764 item->creds.pid, item->creds.tid,
765 item->creds.starttime);
768 case KDBUS_MSG_SRC_PID_COMM:
769 case KDBUS_MSG_SRC_TID_COMM:
770 case KDBUS_MSG_SRC_EXE:
771 case KDBUS_MSG_SRC_CGROUP:
772 case KDBUS_MSG_SRC_SECLABEL:
773 case KDBUS_MSG_DST_NAME:
774 _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
775 enum_MSG(item->type), item->size, item->str, strlen(item->str));
778 case KDBUS_MSG_SRC_CMDLINE:
779 case KDBUS_MSG_SRC_NAMES: {
780 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
781 const char *str = item->str;
784 _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
786 _dbus_verbose("'%s' ", str);
787 size -= strlen(str) + 1;
788 str += strlen(str) + 1;
792 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
796 case KDBUS_MSG_SRC_AUDIT:
797 _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
798 enum_MSG(item->type), item->size,
799 (unsigned long long)item->data64[0],
800 (unsigned long long)item->data64[1]);
803 case KDBUS_MSG_SRC_CAPS: {
808 _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
809 enum_MSG(item->type), item->size,
810 (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
813 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
815 _dbus_verbose(" CapInh=");
816 for (i = 0; i < n; i++)
817 _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
819 _dbus_verbose(" CapPrm=");
820 for (i = 0; i < n; i++)
821 _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
823 _dbus_verbose(" CapEff=");
824 for (i = 0; i < n; i++)
825 _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
827 _dbus_verbose(" CapInh=");
828 for (i = 0; i < n; i++)
829 _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
834 case KDBUS_MSG_TIMESTAMP:
835 _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
836 enum_MSG(item->type), item->size,
837 (unsigned long long)item->timestamp.realtime_ns,
838 (unsigned long long)item->timestamp.monotonic_ns);
842 case KDBUS_MSG_REPLY_TIMEOUT:
843 _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
844 enum_MSG(item->type), item->size, msg->cookie_reply);
846 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
853 ret_size = put_message_into_data(message, data);
856 case KDBUS_MSG_NAME_ADD:
857 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
858 enum_MSG(item->type), (unsigned long long) item->size,
859 item->name_change.name, item->name_change.old_id,
860 item->name_change.new_id, item->name_change.flags);
862 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
863 DBUS_INTERFACE_DBUS, // interface name of the signal
864 "NameOwnerChanged"); // name of the signal
871 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
872 pString = item->name_change.name;
873 _dbus_verbose ("Name added: %s\n", pString);
874 dbus_message_iter_init_append(message, &args);
875 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,&pString))
880 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
885 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
891 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
892 dbus_message_set_serial(message, 1);
894 ret_size = put_message_into_data(message, data);
897 case KDBUS_MSG_NAME_REMOVE:
898 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
899 enum_MSG(item->type), (unsigned long long) item->size,
900 item->name_change.name, item->name_change.old_id,
901 item->name_change.new_id, item->name_change.flags);
903 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
904 DBUS_INTERFACE_DBUS, // interface name of the signal
905 "NameOwnerChanged"); // name of the signal
912 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
913 pString = item->name_change.name;
914 _dbus_verbose ("Name removed: %s\n", pString);
915 dbus_message_iter_init_append(message, &args);
916 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,&pString))
921 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
926 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
932 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
933 dbus_message_set_serial(message, 1);
935 ret_size = put_message_into_data(message, data);
938 case KDBUS_MSG_NAME_CHANGE:
939 _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
940 enum_MSG(item->type), (unsigned long long) item->size,
941 item->name_change.name, item->name_change.old_id,
942 item->name_change.new_id, item->name_change.flags);
944 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
945 DBUS_INTERFACE_DBUS, // interface name of the signal
946 "NameOwnerChanged"); // name of the signal
953 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
954 pString = item->name_change.name;
955 _dbus_verbose ("Name changed: %s\n", pString);
956 dbus_message_iter_init_append(message, &args);
957 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,&pString))
962 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
967 sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
968 _dbus_verbose ("New id: %s\n", pDBusName);
969 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
975 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
976 dbus_message_set_serial(message, 1);
978 ret_size = put_message_into_data(message, data);
981 case KDBUS_MSG_ID_ADD:
982 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
983 enum_MSG(item->type), (unsigned long long) item->size,
984 (unsigned long long) item->id_change.id,
985 (unsigned long long) item->id_change.flags);
987 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
988 DBUS_INTERFACE_DBUS, // interface name of the signal
989 "NameOwnerChanged"); // name of the signal
996 sprintf(dbus_name,":1.%llu",item->id_change.id);
997 dbus_message_iter_init_append(message, &args);
998 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1003 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
1008 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1014 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1015 dbus_message_set_serial(message, 1);
1017 ret_size = put_message_into_data(message, data);
1020 case KDBUS_MSG_ID_REMOVE:
1021 _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
1022 enum_MSG(item->type), (unsigned long long) item->size,
1023 (unsigned long long) item->id_change.id,
1024 (unsigned long long) item->id_change.flags);
1026 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
1027 DBUS_INTERFACE_DBUS, // interface name of the signal
1028 "NameOwnerChanged"); // name of the signal
1035 sprintf(dbus_name,":1.%llu",item->id_change.id);
1036 dbus_message_iter_init_append(message, &args);
1037 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1042 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1047 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
1053 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1054 dbus_message_set_serial(message, 1);
1056 ret_size = put_message_into_data(message, data);
1058 #if KDBUS_MSG_DECODE_DEBUG == 1
1060 _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
1066 #if KDBUS_MSG_DECODE_DEBUG == 1
1068 if ((char *)item - ((char *)msg + msg->size) >= 8)
1069 _dbus_verbose("invalid padding at end of message\n");
1074 dbus_message_unref(message);
1078 static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString *buffer, int* fds, int* n_fds)
1081 uint64_t __attribute__ ((__aligned__(8))) offset;
1082 struct kdbus_msg *msg;
1085 _dbus_assert (socket_transport->max_bytes_read_per_iteration >= 0);
1086 if (!_dbus_string_lengthen (buffer, socket_transport->max_bytes_read_per_iteration))
1091 data = _dbus_string_get_data_len (buffer, 0, socket_transport->max_bytes_read_per_iteration);
1094 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1098 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1099 _dbus_string_set_length (buffer, 0);
1103 msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
1105 ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
1106 _dbus_string_set_length (buffer, ret_size);
1109 if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
1113 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1121 free_watches (DBusTransport *transport)
1123 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1125 _dbus_verbose ("start\n");
1127 if (socket_transport->read_watch)
1129 if (transport->connection)
1130 _dbus_connection_remove_watch_unlocked (transport->connection,
1131 socket_transport->read_watch);
1132 _dbus_watch_invalidate (socket_transport->read_watch);
1133 _dbus_watch_unref (socket_transport->read_watch);
1134 socket_transport->read_watch = NULL;
1137 if (socket_transport->write_watch)
1139 if (transport->connection)
1140 _dbus_connection_remove_watch_unlocked (transport->connection,
1141 socket_transport->write_watch);
1142 _dbus_watch_invalidate (socket_transport->write_watch);
1143 _dbus_watch_unref (socket_transport->write_watch);
1144 socket_transport->write_watch = NULL;
1147 _dbus_verbose ("end\n");
1151 socket_finalize (DBusTransport *transport)
1153 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1155 _dbus_verbose ("\n");
1157 free_watches (transport);
1159 _dbus_string_free (&socket_transport->encoded_outgoing);
1160 _dbus_string_free (&socket_transport->encoded_incoming);
1162 _dbus_transport_finalize_base (transport);
1164 _dbus_assert (socket_transport->read_watch == NULL);
1165 _dbus_assert (socket_transport->write_watch == NULL);
1167 dbus_free (transport);
1171 check_write_watch (DBusTransport *transport)
1173 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1176 if (transport->connection == NULL)
1179 if (transport->disconnected)
1181 _dbus_assert (socket_transport->write_watch == NULL);
1185 _dbus_transport_ref (transport);
1187 if (_dbus_transport_get_is_authenticated (transport))
1188 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1191 if (transport->send_credentials_pending)
1195 DBusAuthState auth_state;
1197 auth_state = _dbus_auth_do_work (transport->auth);
1199 /* If we need memory we install the write watch just in case,
1200 * if there's no need for it, it will get de-installed
1201 * next time we try reading.
1203 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
1204 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1211 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1212 needed, transport->connection, socket_transport->write_watch,
1213 socket_transport->fd,
1214 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1216 _dbus_connection_toggle_watch_unlocked (transport->connection,
1217 socket_transport->write_watch,
1220 _dbus_transport_unref (transport);
1224 check_read_watch (DBusTransport *transport)
1226 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1227 dbus_bool_t need_read_watch;
1229 _dbus_verbose ("fd = %d\n",socket_transport->fd);
1231 if (transport->connection == NULL)
1234 if (transport->disconnected)
1236 _dbus_assert (socket_transport->read_watch == NULL);
1240 _dbus_transport_ref (transport);
1242 if (_dbus_transport_get_is_authenticated (transport))
1244 (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1245 (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1248 if (transport->receive_credentials_pending)
1249 need_read_watch = TRUE;
1252 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
1253 * is to avoid spinning on the file descriptor when we're waiting
1254 * to write or for some other part of the auth process
1256 DBusAuthState auth_state;
1258 auth_state = _dbus_auth_do_work (transport->auth);
1260 /* If we need memory we install the read watch just in case,
1261 * if there's no need for it, it will get de-installed
1262 * next time we try reading. If we're authenticated we
1263 * install it since we normally have it installed while
1266 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
1267 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
1268 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
1269 need_read_watch = TRUE;
1271 need_read_watch = FALSE;
1275 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
1276 _dbus_connection_toggle_watch_unlocked (transport->connection,
1277 socket_transport->read_watch,
1280 _dbus_transport_unref (transport);
1284 do_io_error (DBusTransport *transport)
1286 _dbus_transport_ref (transport);
1287 _dbus_transport_disconnect (transport);
1288 _dbus_transport_unref (transport);
1291 #ifdef DBUS_AUTHENTICATION
1292 /* return value is whether we successfully read any new data. */
1294 read_data_into_auth (DBusTransport *transport,
1297 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1303 _dbus_auth_get_buffer (transport->auth, &buffer);
1305 bytes_read = kdbus_read_message(socket_transport, buffer);
1307 _dbus_auth_return_buffer (transport->auth, buffer,
1308 bytes_read > 0 ? bytes_read : 0);
1312 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
1315 else if (bytes_read < 0)
1317 /* EINTR already handled for us */
1319 if (_dbus_get_is_errno_enomem ())
1323 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1324 ; /* do nothing, just return FALSE below */
1327 _dbus_verbose ("Error reading from remote app: %s\n",
1328 _dbus_strerror_from_errno ());
1329 do_io_error (transport);
1336 _dbus_assert (bytes_read == 0);
1338 _dbus_verbose ("Disconnected from remote app\n");
1339 do_io_error (transport);
1345 /* Return value is whether we successfully wrote any bytes */
1347 write_data_from_auth (DBusTransport *transport)
1349 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1351 const DBusString *buffer;
1353 if (!_dbus_auth_get_bytes_to_send (transport->auth,
1357 bytes_written = _dbus_write_socket (socket_transport->fd,
1359 0, _dbus_string_get_length (buffer));
1361 if (bytes_written > 0)
1363 _dbus_auth_bytes_sent (transport->auth, bytes_written);
1366 else if (bytes_written < 0)
1368 /* EINTR already handled for us */
1370 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1374 _dbus_verbose ("Error writing to remote app: %s\n",
1375 _dbus_strerror_from_errno ());
1376 do_io_error (transport);
1385 exchange_credentials (DBusTransport *transport,
1386 dbus_bool_t do_reading,
1387 dbus_bool_t do_writing)
1389 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1390 DBusError error = DBUS_ERROR_INIT;
1392 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
1393 do_reading, do_writing);
1395 if (do_writing && transport->send_credentials_pending)
1397 if (_dbus_send_credentials_socket (socket_transport->fd,
1400 transport->send_credentials_pending = FALSE;
1404 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
1405 dbus_error_free (&error);
1406 do_io_error (transport);
1410 if (do_reading && transport->receive_credentials_pending)
1412 /* FIXME this can fail due to IO error _or_ OOM, broken
1413 * (somewhat tricky to fix since the OOM error can be set after
1414 * we already read the credentials byte, so basically we need to
1415 * separate reading the byte and storing it in the
1416 * transport->credentials). Does not really matter for now
1417 * because storing in credentials never actually fails on unix.
1419 if (_dbus_read_credentials_socket (socket_transport->fd,
1420 transport->credentials,
1423 transport->receive_credentials_pending = FALSE;
1427 _dbus_verbose ("Failed to read credentials %s\n", error.message);
1428 dbus_error_free (&error);
1429 do_io_error (transport);
1433 if (!(transport->send_credentials_pending ||
1434 transport->receive_credentials_pending))
1436 if (!_dbus_auth_set_credentials (transport->auth,
1437 transport->credentials))
1445 do_authentication (DBusTransport *transport,
1446 dbus_bool_t do_reading,
1447 dbus_bool_t do_writing,
1448 dbus_bool_t *auth_completed)
1451 dbus_bool_t orig_auth_state;
1455 orig_auth_state = _dbus_transport_get_is_authenticated (transport);
1457 /* This is essential to avoid the check_write_watch() at the end,
1458 * we don't want to add a write watch in do_iteration before
1459 * we try writing and get EAGAIN
1461 if (orig_auth_state)
1464 *auth_completed = FALSE;
1468 _dbus_transport_ref (transport);
1470 while (!_dbus_transport_get_is_authenticated (transport) &&
1471 _dbus_transport_get_is_connected (transport))
1473 if (!exchange_credentials (transport, do_reading, do_writing))
1479 if (transport->send_credentials_pending ||
1480 transport->receive_credentials_pending)
1482 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
1483 transport->send_credentials_pending,
1484 transport->receive_credentials_pending);
1488 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
1489 switch (_dbus_auth_do_work (transport->auth))
1491 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
1492 _dbus_verbose (" %s auth state: waiting for input\n",
1493 TRANSPORT_SIDE (transport));
1494 if (!do_reading || !read_data_into_auth (transport, &oom))
1498 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
1499 _dbus_verbose (" %s auth state: waiting for memory\n",
1500 TRANSPORT_SIDE (transport));
1505 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
1506 _dbus_verbose (" %s auth state: bytes to send\n",
1507 TRANSPORT_SIDE (transport));
1508 if (!do_writing || !write_data_from_auth (transport))
1512 case DBUS_AUTH_STATE_NEED_DISCONNECT:
1513 _dbus_verbose (" %s auth state: need to disconnect\n",
1514 TRANSPORT_SIDE (transport));
1515 do_io_error (transport);
1518 case DBUS_AUTH_STATE_AUTHENTICATED:
1519 _dbus_verbose (" %s auth state: authenticated\n",
1520 TRANSPORT_SIDE (transport));
1527 *auth_completed = (orig_auth_state != _dbus_transport_get_is_authenticated (transport));
1529 check_read_watch (transport);
1530 check_write_watch (transport);
1531 _dbus_transport_unref (transport);
1540 /* returns false on oom */
1542 do_writing (DBusTransport *transport)
1545 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1548 /* No messages without authentication! */
1549 if (!_dbus_transport_get_is_authenticated (transport))
1551 _dbus_verbose ("Not authenticated, not writing anything\n");
1555 if (transport->disconnected)
1557 _dbus_verbose ("Not connected, not writing anything\n");
1562 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
1563 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
1564 socket_transport->fd);
1570 while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1573 DBusMessage *message;
1574 const DBusString *header;
1575 const DBusString *body;
1576 int total_bytes_to_write;
1577 const char* pDestination;
1579 /* if (total > socket_transport->max_bytes_written_per_iteration)
1581 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
1582 total, socket_transport->max_bytes_written_per_iteration);
1586 message = _dbus_connection_get_message_to_send (transport->connection);
1587 _dbus_assert (message != NULL);
1588 dbus_message_lock (message);
1589 _dbus_message_get_network_data (message, &header, &body);
1590 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
1591 pDestination = dbus_message_get_destination(message);
1595 if(!strcmp(pDestination, "org.freedesktop.DBus"))
1597 if(emulateOrgFreedesktopDBus(transport, message, socket_transport->fd))
1598 bytes_written = total_bytes_to_write;
1604 if (_dbus_auth_needs_encoding (transport->auth))
1606 if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
1608 if (!_dbus_auth_encode_data (transport->auth,
1609 header, &socket_transport->encoded_outgoing))
1615 if (!_dbus_auth_encode_data (transport->auth,
1616 body, &socket_transport->encoded_outgoing))
1618 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
1624 total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
1625 if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
1628 bytes_written = kdbus_write_msg(socket_transport, message, TRUE);
1632 if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
1635 bytes_written = kdbus_write_msg(socket_transport, message, FALSE);
1639 if (bytes_written < 0)
1641 /* EINTR already handled for us */
1643 /* For some discussion of why we also ignore EPIPE here, see
1644 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
1647 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
1651 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
1652 do_io_error (transport);
1658 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
1659 total_bytes_to_write);
1661 // total += bytes_written;
1662 socket_transport->message_bytes_written += bytes_written;
1664 _dbus_assert (socket_transport->message_bytes_written <=
1665 total_bytes_to_write);
1667 if (socket_transport->message_bytes_written == total_bytes_to_write)
1669 socket_transport->message_bytes_written = 0;
1670 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
1671 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
1673 _dbus_connection_message_sent_unlocked (transport->connection,
1686 /* returns false on out-of-memory */
1688 do_reading (DBusTransport *transport)
1690 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1696 _dbus_verbose ("fd = %d\n",socket_transport->fd);
1698 /* No messages without authentication! */
1699 if (!_dbus_transport_get_is_authenticated (transport))
1706 /* See if we've exceeded max messages and need to disable reading */
1707 check_read_watch (transport);
1709 _dbus_assert (socket_transport->read_watch != NULL ||
1710 transport->disconnected);
1712 if (transport->disconnected)
1715 if (!dbus_watch_get_enabled (socket_transport->read_watch))
1718 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1720 _dbus_verbose ("Out of memory reading file descriptors\n");
1724 _dbus_message_loader_get_buffer (transport->loader, &buffer);
1726 if (_dbus_auth_needs_decoding (transport->auth))
1728 bytes_read = kdbus_read_message(socket_transport, &socket_transport->encoded_incoming, fds, &n_fds);
1730 _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) == bytes_read);
1734 if (!_dbus_auth_decode_data (transport->auth,
1735 &socket_transport->encoded_incoming,
1738 _dbus_verbose ("Out of memory decoding incoming data\n");
1739 _dbus_message_loader_return_buffer (transport->loader,
1741 _dbus_string_get_length (buffer));
1746 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
1747 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
1751 bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
1753 if (bytes_read >= 0 && n_fds > 0)
1754 _dbus_verbose("Read %i unix fds\n", n_fds);
1756 _dbus_message_loader_return_buffer (transport->loader,
1758 bytes_read < 0 ? 0 : _dbus_string_get_length (buffer));
1759 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1761 /* if (_dbus_auth_needs_decoding (transport->auth))
1763 bytes_read = kdbus_read_message(socket_transport, &socket_transport->encoded_incoming);
1765 _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
1772 _dbus_message_loader_get_buffer (transport->loader, &buffer);
1773 orig_len = _dbus_string_get_length (buffer);
1774 if (!_dbus_auth_decode_data (transport->auth,
1775 &socket_transport->encoded_incoming,
1778 _dbus_verbose ("Out of memory decoding incoming data\n");
1779 _dbus_message_loader_return_buffer (transport->loader,
1781 _dbus_string_get_length (buffer) - orig_len);
1786 _dbus_message_loader_return_buffer (transport->loader,
1788 _dbus_string_get_length (buffer) - orig_len);
1789 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
1790 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
1797 if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1799 _dbus_verbose ("Out of memory reading file descriptors\n");
1800 _dbus_message_loader_return_buffer (transport->loader, buffer, 0);
1804 _dbus_message_loader_get_buffer (transport->loader, &buffer);
1806 bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
1808 if (bytes_read >= 0 && n_fds > 0)
1809 _dbus_verbose("Read %i unix fds\n", n_fds);
1811 _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1812 _dbus_message_loader_return_buffer (transport->loader,
1814 bytes_read < 0 ? 0 : bytes_read);
1819 /* EINTR already handled for us */
1821 if (_dbus_get_is_errno_enomem ())
1823 _dbus_verbose ("Out of memory in read()/do_reading()\n");
1827 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1831 _dbus_verbose ("Error reading from remote app: %s\n",
1832 _dbus_strerror_from_errno ());
1833 do_io_error (transport);
1837 else if (bytes_read == 0)
1839 _dbus_verbose ("Disconnected from remote app\n");
1840 do_io_error (transport);
1845 _dbus_verbose (" read %d bytes\n", bytes_read);
1847 // total += bytes_read;
1849 if (!_dbus_transport_queue_messages (transport))
1852 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
1856 /* Try reading more data until we get EAGAIN and return, or
1857 * exceed max bytes per iteration. If in blocking mode of
1858 * course we'll block instead of returning.
1871 unix_error_with_read_to_come (DBusTransport *itransport,
1875 DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
1877 if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
1880 /* If we have a read watch enabled ...
1881 we -might have data incoming ... => handle the HANGUP there */
1882 if (watch != transport->read_watch &&
1883 _dbus_watch_get_enabled (transport->read_watch))
1890 socket_handle_watch (DBusTransport *transport,
1894 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1896 _dbus_assert (watch == socket_transport->read_watch ||
1897 watch == socket_transport->write_watch);
1898 _dbus_assert (watch != NULL);
1900 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
1901 * still be in the buffer and do_reading may need several iteration to read
1902 * it all (because of its max_bytes_read_per_iteration limit).
1904 if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
1906 _dbus_verbose ("Hang up or error on watch\n");
1907 _dbus_transport_disconnect (transport);
1911 if (watch == socket_transport->read_watch &&
1912 (flags & DBUS_WATCH_READABLE))
1914 #ifdef DBUS_AUTHENTICATION
1915 dbus_bool_t auth_finished;
1918 _dbus_verbose ("handling read watch %p flags = %x\n",
1921 #ifdef DBUS_AUTHENTICATION
1922 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
1925 /* We don't want to do a read immediately following
1926 * a successful authentication. This is so we
1927 * have a chance to propagate the authentication
1928 * state further up. Specifically, we need to
1929 * process any pending data from the auth object.
1934 if (!do_reading (transport))
1936 _dbus_verbose ("no memory to read\n");
1939 #ifdef DBUS_AUTHENTICATION
1943 _dbus_verbose ("Not reading anything since we just completed the authentication\n");
1947 else if (watch == socket_transport->write_watch &&
1948 (flags & DBUS_WATCH_WRITABLE))
1951 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1952 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1954 #ifdef DBUS_AUTHENTICATION
1955 if (!do_authentication (transport, FALSE, TRUE, NULL))
1958 if (!do_writing (transport))
1960 _dbus_verbose ("no memory to write\n");
1964 /* See if we still need the write watch */
1965 check_write_watch (transport);
1967 #ifdef DBUS_ENABLE_VERBOSE_MODE
1970 if (watch == socket_transport->read_watch)
1971 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
1973 else if (watch == socket_transport->write_watch)
1974 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1977 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
1978 watch, dbus_watch_get_socket (watch));
1980 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1986 socket_disconnect (DBusTransport *transport)
1988 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1990 _dbus_verbose ("\n");
1992 free_watches (transport);
1994 _dbus_close_socket (socket_transport->fd, NULL);
1995 socket_transport->fd = -1;
1999 socket_connection_set (DBusTransport *transport)
2001 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2003 _dbus_watch_set_handler (socket_transport->write_watch,
2004 _dbus_connection_handle_watch,
2005 transport->connection, NULL);
2007 _dbus_watch_set_handler (socket_transport->read_watch,
2008 _dbus_connection_handle_watch,
2009 transport->connection, NULL);
2011 if (!_dbus_connection_add_watch_unlocked (transport->connection,
2012 socket_transport->write_watch))
2015 if (!_dbus_connection_add_watch_unlocked (transport->connection,
2016 socket_transport->read_watch))
2018 _dbus_connection_remove_watch_unlocked (transport->connection,
2019 socket_transport->write_watch);
2023 check_read_watch (transport);
2024 check_write_watch (transport);
2030 * @todo We need to have a way to wake up the select sleep if
2031 * a new iteration request comes in with a flag (read/write) that
2032 * we're not currently serving. Otherwise a call that just reads
2033 * could block a write call forever (if there are no incoming
2037 kdbus_do_iteration (DBusTransport *transport,
2039 int timeout_milliseconds)
2041 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2046 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
2047 flags & DBUS_ITERATION_DO_READING ? "read" : "",
2048 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
2049 timeout_milliseconds,
2050 socket_transport->read_watch,
2051 socket_transport->write_watch,
2052 socket_transport->fd);
2054 /* the passed in DO_READING/DO_WRITING flags indicate whether to
2055 * read/write messages, but regardless of those we may need to block
2056 * for reading/writing to do auth. But if we do reading for auth,
2057 * we don't want to read any messages yet if not given DO_READING.
2060 poll_fd.fd = socket_transport->fd;
2063 if (_dbus_transport_get_is_authenticated (transport))
2065 /* This is kind of a hack; if we have stuff to write, then try
2066 * to avoid the poll. This is probably about a 5% speedup on an
2067 * echo client/server.
2069 * If both reading and writing were requested, we want to avoid this
2070 * since it could have funky effects:
2071 * - both ends spinning waiting for the other one to read
2072 * data so they can finish writing
2073 * - prioritizing all writing ahead of reading
2075 if ((flags & DBUS_ITERATION_DO_WRITING) &&
2076 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
2077 !transport->disconnected &&
2078 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
2080 do_writing (transport);
2082 if (transport->disconnected ||
2083 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
2087 /* If we get here, we decided to do the poll() after all */
2088 _dbus_assert (socket_transport->read_watch);
2089 if (flags & DBUS_ITERATION_DO_READING)
2090 poll_fd.events |= _DBUS_POLLIN;
2092 _dbus_assert (socket_transport->write_watch);
2093 if (flags & DBUS_ITERATION_DO_WRITING)
2094 poll_fd.events |= _DBUS_POLLOUT;
2098 DBusAuthState auth_state;
2100 auth_state = _dbus_auth_do_work (transport->auth);
2102 if (transport->receive_credentials_pending ||
2103 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
2104 poll_fd.events |= _DBUS_POLLIN;
2106 if (transport->send_credentials_pending ||
2107 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
2108 poll_fd.events |= _DBUS_POLLOUT;
2113 if (flags & DBUS_ITERATION_BLOCK)
2114 poll_timeout = timeout_milliseconds;
2118 /* For blocking selects we drop the connection lock here
2119 * to avoid blocking out connection access during a potentially
2120 * indefinite blocking call. The io path is still protected
2121 * by the io_path_cond condvar, so we won't reenter this.
2123 if (flags & DBUS_ITERATION_BLOCK)
2125 _dbus_verbose ("unlock pre poll\n");
2126 _dbus_connection_unlock (transport->connection);
2130 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
2132 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
2134 _dbus_verbose ("Error from _dbus_poll(): %s\n",
2135 _dbus_strerror_from_errno ());
2139 if (flags & DBUS_ITERATION_BLOCK)
2141 _dbus_verbose ("lock post poll\n");
2142 _dbus_connection_lock (transport->connection);
2148 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
2149 * valgrind flags it as an error. though it probably
2150 * is guaranteed on linux at least.
2153 if (poll_fd.revents & _DBUS_POLLERR)
2154 do_io_error (transport);
2157 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
2158 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
2159 #ifdef DBUS_AUTHENTICATION
2160 dbus_bool_t authentication_completed;
2163 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
2164 need_read, need_write);
2165 #ifdef DBUS_AUTHENTICATION
2166 do_authentication (transport, need_read, need_write,
2167 &authentication_completed);
2169 /* See comment in socket_handle_watch. */
2170 if (authentication_completed)
2173 if (need_read && (flags & DBUS_ITERATION_DO_READING))
2174 do_reading (transport);
2175 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
2176 do_writing (transport);
2181 _dbus_verbose ("Error from _dbus_poll(): %s\n",
2182 _dbus_strerror_from_errno ());
2188 /* We need to install the write watch only if we did not
2189 * successfully write everything. Note we need to be careful that we
2190 * don't call check_write_watch *before* do_writing, since it's
2191 * inefficient to add the write watch, and we can avoid it most of
2192 * the time since we can write immediately.
2194 * However, we MUST always call check_write_watch(); DBusConnection code
2195 * relies on the fact that running an iteration will notice that
2196 * messages are pending.
2198 check_write_watch (transport);
2200 _dbus_verbose (" ... leaving do_iteration()\n");
2204 socket_live_messages_changed (DBusTransport *transport)
2206 /* See if we should look for incoming messages again */
2207 check_read_watch (transport);
2210 static const DBusTransportVTable kdbus_vtable = {
2212 socket_handle_watch,
2214 socket_connection_set,
2216 socket_live_messages_changed,
2217 socket_get_socket_fd
2221 * Creates a new transport for the given kdbus file descriptor. The file
2222 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
2225 * @param fd the file descriptor.
2226 * @param server_guid non-#NULL if this transport is on the server side of a connection
2227 * @param address the transport's address
2228 * @returns the new transport, or #NULL if no memory.
2230 static DBusTransport*
2231 _dbus_transport_new_for_socket_kdbus (int fd,
2232 const DBusString *server_guid,
2233 const DBusString *address)
2235 DBusTransportSocket *socket_transport;
2237 socket_transport = dbus_new0 (DBusTransportSocket, 1);
2238 if (socket_transport == NULL)
2241 if (!_dbus_string_init (&socket_transport->encoded_outgoing))
2244 if (!_dbus_string_init (&socket_transport->encoded_incoming))
2247 socket_transport->write_watch = _dbus_watch_new (fd,
2248 DBUS_WATCH_WRITABLE,
2251 if (socket_transport->write_watch == NULL)
2254 socket_transport->read_watch = _dbus_watch_new (fd,
2255 DBUS_WATCH_READABLE,
2258 if (socket_transport->read_watch == NULL)
2261 if (!_dbus_transport_init_base (&socket_transport->base,
2263 server_guid, address))
2266 #ifdef HAVE_UNIX_FD_PASSING
2267 _dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
2270 socket_transport->fd = fd;
2271 socket_transport->message_bytes_written = 0;
2273 /* These values should probably be tunable or something. */
2274 socket_transport->max_bytes_read_per_iteration = RECEIVE_POOL_SIZE;
2275 socket_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
2277 socket_transport->kdbus_mmap_ptr = NULL;
2278 socket_transport->memfd = -1;
2280 return (DBusTransport*) socket_transport;
2283 _dbus_watch_invalidate (socket_transport->read_watch);
2284 _dbus_watch_unref (socket_transport->read_watch);
2286 _dbus_watch_invalidate (socket_transport->write_watch);
2287 _dbus_watch_unref (socket_transport->write_watch);
2289 _dbus_string_free (&socket_transport->encoded_incoming);
2291 _dbus_string_free (&socket_transport->encoded_outgoing);
2293 dbus_free (socket_transport);
2299 * Creates a connection to the kdbus bus
2301 * This will set FD_CLOEXEC for the socket returned.
2303 * @param path the path to UNIX domain socket
2304 * @param error return location for error code
2305 * @returns connection file descriptor or -1 on error
2307 static int _dbus_connect_kdbus (const char *path, DBusError *error)
2311 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2312 _dbus_verbose ("connecting to kdbus bus %s\n", path);
2314 fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
2316 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
2321 static dbus_bool_t kdbus_mmap(DBusTransport* transport)
2323 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2325 socket_transport->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, socket_transport->fd, 0);
2326 if (socket_transport->kdbus_mmap_ptr == MAP_FAILED)
2333 * Creates a new transport for kdbus.
2334 * This creates a client-side of a transport.
2336 * @param path the path to the domain socket.
2337 * @param error address where an error can be returned.
2338 * @returns a new transport, or #NULL on failure.
2340 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
2343 DBusTransport *transport;
2346 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2348 if (!_dbus_string_init (&address))
2350 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2356 if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
2358 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2362 fd = _dbus_connect_kdbus (path, error);
2365 _DBUS_ASSERT_ERROR_IS_SET (error);
2369 _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
2371 transport = _dbus_transport_new_for_socket_kdbus (fd, NULL, &address);
2372 if (transport == NULL)
2374 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2378 _dbus_string_free (&address);
2383 _dbus_close_socket (fd, NULL);
2385 _dbus_string_free (&address);
2391 * Opens kdbus transport.
2393 * @param entry the address entry to try opening
2394 * @param transport_p return location for the opened transport
2395 * @param error error to be set
2396 * @returns result of the attempt
2398 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry,
2399 DBusTransport **transport_p,
2404 method = dbus_address_entry_get_method (entry);
2405 _dbus_assert (method != NULL);
2407 if (strcmp (method, "kdbus") == 0)
2409 const char *path = dbus_address_entry_get_value (entry, "path");
2413 _dbus_set_bad_address (error, "kdbus", "path", NULL);
2414 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
2417 *transport_p = _dbus_transport_new_for_kdbus (path, error);
2419 if (*transport_p == NULL)
2421 _DBUS_ASSERT_ERROR_IS_SET (error);
2422 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
2426 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2427 return DBUS_TRANSPORT_OPEN_OK;
2432 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2433 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
2437 static struct kdbus_policy *make_policy_name(const char *name)
2439 struct kdbus_policy *p;
2442 size = offsetof(struct kdbus_policy, name) + strlen(name) + 1;
2448 p->type = KDBUS_POLICY_NAME;
2449 strcpy(p->name, name);
2454 static struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id)
2456 struct kdbus_policy *p;
2457 __u64 size = sizeof(*p);
2465 p->type = KDBUS_POLICY_ACCESS;
2466 p->access.type = type;
2467 p->access.bits = bits;
2473 static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size)
2475 struct kdbus_policy *dst = (struct kdbus_policy *) ((char *) cmd_policy + cmd_policy->size);
2477 if (cmd_policy->size + policy->size > max_size)
2480 memcpy(dst, policy, policy->size);
2481 cmd_policy->size += KDBUS_ALIGN8(policy->size);
2485 dbus_bool_t bus_register_policy_kdbus(const char* name, DBusConnection *connection, DBusError *error)
2487 struct kdbus_cmd_policy *cmd_policy;
2488 struct kdbus_policy *policy;
2492 if(!dbus_connection_get_socket(connection, &fd))
2494 dbus_set_error (error, "Failed to get fd for registering policy", NULL);
2498 cmd_policy = (struct kdbus_cmd_policy *) alloca(size);
2499 memset(cmd_policy, 0, size);
2501 policy = (struct kdbus_policy *) cmd_policy->policies;
2502 cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);
2504 policy = make_policy_name(name);
2505 append_policy(cmd_policy, policy, size);
2507 policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, getuid());
2508 append_policy(cmd_policy, policy, size);
2510 policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
2511 append_policy(cmd_policy, policy, size);
2513 policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
2514 append_policy(cmd_policy, policy, size);
2516 if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
2518 dbus_set_error(error,_dbus_error_from_errno (errno), "Error setting EP policy: %s", _dbus_strerror (errno));
2522 _dbus_verbose("Policy %s set correctly\n", name);
2526 dbus_bool_t bus_register_kdbus(char* name, DBusConnection *connection, DBusError *error)
2528 struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
2531 memset(&hello, 0, sizeof(hello));
2532 hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
2533 KDBUS_HELLO_ATTACH_COMM |
2534 KDBUS_HELLO_ATTACH_EXE |
2535 KDBUS_HELLO_ATTACH_CMDLINE |
2536 KDBUS_HELLO_ATTACH_CAPS |
2537 KDBUS_HELLO_ATTACH_CGROUP |
2538 KDBUS_HELLO_ATTACH_SECLABEL |
2539 KDBUS_HELLO_ATTACH_AUDIT*/;
2540 hello.size = sizeof(struct kdbus_cmd_hello);
2541 hello.pool_size = RECEIVE_POOL_SIZE;
2543 if(!dbus_connection_get_socket(connection, &fd))
2545 dbus_set_error (error, "failed to get fd for bus registration", NULL);
2548 if (ioctl(fd, KDBUS_CMD_HELLO, &hello))
2550 dbus_set_error(error,_dbus_error_from_errno (errno), "Failed to send hello: %s", _dbus_strerror (errno));
2554 _dbus_verbose("-- Our peer ID is: %llu\n", (unsigned long long)hello.id);
2555 sprintf(name, "%llu", (unsigned long long)hello.id);
2557 if(!kdbus_mmap(dbus_connection_get_transport(connection)))
2559 dbus_set_error(error,_dbus_error_from_errno (errno), "Error when mmap: %s", _dbus_strerror (errno));
2566 int bus_request_name_kdbus(DBusConnection *connection, const char *name, const uint64_t flags, DBusError *error)
2568 struct kdbus_cmd_name *cmd_name;
2570 uint64_t size = sizeof(*cmd_name) + strlen(name) + 1;
2571 uint64_t flags_kdbus = 0;
2573 cmd_name = alloca(size);
2575 memset(cmd_name, 0, size);
2576 strcpy(cmd_name->name, name);
2577 cmd_name->size = size;
2579 if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
2580 flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
2581 if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
2582 flags_kdbus |= KDBUS_NAME_QUEUE;
2583 if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
2584 flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
2586 cmd_name->conn_flags = flags_kdbus;
2588 if(!dbus_connection_get_socket(connection, &fd))
2590 dbus_set_error (error, "failed to get fd for name request", NULL);
2594 _dbus_verbose("Request name - flags sent: 0x%llx !!!!!!!!!\n", cmd_name->conn_flags);
2596 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2597 if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
2599 dbus_set_error(error,_dbus_error_from_errno (errno), "error acquiring name: %s", _dbus_strerror (errno));
2601 return DBUS_REQUEST_NAME_REPLY_EXISTS;
2605 _dbus_verbose("Request name - received flag: 0x%llx !!!!!!!!!\n", cmd_name->conn_flags);
2607 if(cmd_name->conn_flags & KDBUS_NAME_IN_QUEUE)
2608 return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
2610 return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
2611 //todo now 1 code is never returned - DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
2615 * Checks if the connection's transport is kdbus on the basis of its address
2617 * @param pointer to the connection
2618 * @returns TRUE if kdbus transport, otherwise FALSE
2620 dbus_bool_t dbus_transport_is_kdbus(DBusConnection *connection)
2622 const char* address = _dbus_connection_get_address(connection);
2624 if(address == strstr(address, "kdbus:path="))
2630 static int parse_match_key(const char *rule, const char* key, char** pValue)
2633 const char* pValueEnd;
2634 int value_length = 0;
2636 pBegin = strstr(rule, key);
2639 pBegin += strlen(key);
2640 pValueEnd = strchr(pBegin, '\'');
2643 value_length = pValueEnd - pBegin;
2644 *pValue = strndup(pBegin, value_length);
2647 if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
2649 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
2653 return value_length;
2656 /* only part of the dbus's matching capabilities implemented in kdbus now because of different mechanism
2657 * interface match key mapped to bloom
2658 * sender match key mapped to src_name
2659 * also handled org.freedesktop.dbus members: NameOwnerChanged, NameLost, NameAcquired
2661 void dbus_bus_add_match_kdbus (DBusConnection *connection, const char *rule, DBusError *error)
2663 struct kdbus_cmd_match* pCmd_match;
2664 struct kdbus_item *pItem;
2666 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
2668 unsigned int kernel_item = 0;
2671 char* pInterface = NULL;
2673 dbus_connection_get_socket(connection, &fd);
2675 /*parsing rule and calculating size of command*/
2676 size = sizeof(struct kdbus_cmd_match);
2678 if(strstr(rule, "member='NameOwnerChanged'"))
2680 kernel_item = KDBUS_MATCH_NAME_CHANGE;
2681 size += KDBUS_ITEM_SIZE(1);
2683 else if(strstr(rule, "member='NameLost'"))
2685 kernel_item = KDBUS_MATCH_NAME_REMOVE;
2686 size += KDBUS_ITEM_SIZE(1);
2688 else if(strstr(rule, "member='NameAcquired'"))
2690 kernel_item = KDBUS_MATCH_NAME_ADD;
2691 size += KDBUS_ITEM_SIZE(1);
2694 name_size = parse_match_key(rule, "interface='", &pInterface); /*actual size is not important for interface*/
2695 if((name_size == -1) && (kernel_item == 0))
2698 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;
2700 else if(name_size > 0)
2701 size += KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
2703 name_size = parse_match_key(rule, "sender='", &pName);
2704 if((name_size == -1) && (kernel_item == 0))
2707 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;
2711 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
2713 src_id = strtoll(&pName[3], NULL, 10);
2718 size += KDBUS_ITEM_SIZE(name_size + 1);
2721 pCmd_match = malloc(size);
2722 if(pCmd_match == NULL)
2724 memset(pCmd_match, 0, size);
2726 pCmd_match->size = size;
2727 pCmd_match->cookie = strtoll(dbus_bus_get_unique_name(connection), NULL , 10);
2729 pItem = pCmd_match->items;
2730 if(kernel_item == ~0) //all signals from kernel
2732 pCmd_match->src_id = 0;
2733 pItem->type = KDBUS_MATCH_NAME_CHANGE;
2734 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2735 pItem = KDBUS_PART_NEXT(pItem);
2736 pItem->type = KDBUS_MATCH_NAME_ADD;
2737 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2738 pItem = KDBUS_PART_NEXT(pItem);
2739 pItem->type = KDBUS_MATCH_NAME_REMOVE;
2740 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2741 pItem = KDBUS_PART_NEXT(pItem);
2742 pItem->type = KDBUS_MATCH_ID_ADD;
2743 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
2744 pItem = KDBUS_PART_NEXT(pItem);
2745 pItem->type = KDBUS_MATCH_ID_REMOVE;
2746 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
2748 else if(kernel_item)
2750 pCmd_match->src_id = 0;
2751 pItem->type = kernel_item;
2752 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2756 pCmd_match->src_id = src_id;
2759 pItem->type = KDBUS_MATCH_SRC_NAME;
2760 pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
2761 strcpy(pItem->str, pName);
2762 pItem = KDBUS_PART_NEXT(pItem);
2767 pItem->type = KDBUS_MATCH_BLOOM;
2768 pItem->size = KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
2769 strncpy(pItem->data, pInterface, KDBUS_BLOOM_SIZE_BYTES);
2773 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
2776 dbus_set_error(error,_dbus_error_from_errno (errno), "error adding match: %s", _dbus_strerror (errno));
2777 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
2780 _dbus_verbose("Added match bus rule %s\n", rule);
2790 void dbus_bus_remove_match_kdbus (DBusConnection *connection, const char *rule, DBusError *error)
2792 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
2795 dbus_connection_get_socket(connection, &fd);
2796 cmd.cookie = strtoll(dbus_bus_get_unique_name(connection), NULL , 10);
2797 cmd.id = cmd.cookie;
2798 cmd.size = sizeof(struct kdbus_cmd_match);
2800 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, &cmd))
2803 dbus_set_error(error,_dbus_error_from_errno (errno), "error removing match: %s", _dbus_strerror (errno));
2804 _dbus_verbose("Failed removing match rule; error: %d, %m\n", errno);
2807 _dbus_verbose("Match rule removed correctly.\n");