+static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message, dbus_bool_t encoded)
+{
+ struct kdbus_msg *msg;
+ struct kdbus_item *item;
+ const char *name;
+ uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
+ const DBusString *header;
+ const DBusString *body;
+ uint64_t ret_size = 0;
+ uint64_t body_size = 0;
+ uint64_t header_size = 0;
+ dbus_bool_t use_memfd;
+ const int *unix_fds;
+ unsigned fds_count;
+ dbus_bool_t autostart;
+
+ // determine name and destination id
+ if((name = dbus_message_get_destination(message)))
+ {
+ dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
+ if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
+ {
+ dst_id = strtoull(&name[3], NULL, 10);
+ name = NULL;
+ }
+ }
+
+ // get size of data
+ if(encoded)
+ ret_size = _dbus_string_get_length (&transport->encoded_outgoing);
+ else
+ {
+ _dbus_message_get_network_data (message, &header, &body);
+ header_size = _dbus_string_get_length(header);
+ body_size = _dbus_string_get_length(body);
+ ret_size = header_size + body_size;
+ }
+
+ // check if message size is big enough to use memfd kdbus transport
+ use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
+ if(use_memfd) kdbus_init_memfd(transport);
+
+ _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
+
+ // init basic message fields
+ msg = kdbus_init_msg(name, dst_id, body_size, use_memfd, fds_count, transport);
+ msg->cookie = dbus_message_get_serial(message);
+ autostart = dbus_message_get_auto_start (message);
+ if(!autostart)
+ msg->flags |= KDBUS_MSG_FLAGS_NO_AUTO_START;
+
+ // build message contents
+ item = msg->items;
+
+ // case 1 - bulk data transfer - memfd - encoded and plain
+ if(use_memfd)
+ {
+ char *buf;
+
+ if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0) < 0)
+ {
+ _dbus_verbose("memfd sealing failed: \n");
+ goto out;
+ }
+
+ buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
+ if (buf == MAP_FAILED)
+ {
+ _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
+ goto out;
+ }
+
+ if(encoded)
+ memcpy(buf, &transport->encoded_outgoing, ret_size);
+ else
+ {
+ memcpy(buf, _dbus_string_get_const_data(header), header_size);
+ if(body_size) {
+ buf+=header_size;
+ memcpy(buf, _dbus_string_get_const_data(body), body_size);
+ buf-=header_size;
+ }
+ }
+
+ munmap(buf, ret_size);
+
+ // seal data - kdbus module needs it
+ if(ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) < 0) {
+ _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
+ ret_size = -1;
+ goto out;
+ }
+
+ item->type = KDBUS_MSG_PAYLOAD_MEMFD;
+ item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
+ item->memfd.size = ret_size;
+ item->memfd.fd = transport->memfd;
+ // case 2 - small encoded - don't use memfd
+ } else if(encoded) {
+ _dbus_verbose("sending encoded data\n");
+ MSG_ITEM_BUILD_VEC(&transport->encoded_outgoing, _dbus_string_get_length (&transport->encoded_outgoing));
+
+ // case 3 - small not encoded - don't use memfd
+ } else {
+ _dbus_verbose("sending normal vector data\n");
+ MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(header), header_size);
+
+ if(body_size)
+ {
+ _dbus_verbose("body attaching\n");
+ item = KDBUS_PART_NEXT(item);
+ MSG_ITEM_BUILD_VEC(_dbus_string_get_const_data(body), body_size);
+ }
+ }
+
+ if(fds_count)
+ {
+ item = KDBUS_PART_NEXT(item);
+ item->type = KDBUS_MSG_FDS;
+ item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
+ memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
+ }
+
+ if (name)
+ {
+ item = KDBUS_PART_NEXT(item);
+ item->type = KDBUS_MSG_DST_NAME;
+ item->size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
+ strcpy(item->str, name);
+ }
+ else if (dst_id == KDBUS_DST_ID_BROADCAST)
+ {
+ item = KDBUS_PART_NEXT(item);
+ item->type = KDBUS_MSG_BLOOM;
+ item->size = KDBUS_PART_HEADER_SIZE + transport->bloom_size;
+ strncpy(item->data, dbus_message_get_interface(message), transport->bloom_size);
+ }
+
+ again:
+ if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
+ {
+ if(errno == EINTR)
+ goto again;
+ if((errno == ESRCH) || (errno == ENXIO) || (errno = EADDRNOTAVAIL)) //when recipient is not available on the bus
+ {
+ if(autostart)
+ {
+ //todo start service here, otherwise
+ 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))
+ goto out;
+ }
+ else
+ if(reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Name \"%s\" does not exist", dbus_message_get_destination(message), message, transport->base.connection))
+ goto out;
+
+ }
+ _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
+ ret_size = -1;
+ }
+out:
+ free(msg);
+ close(transport->memfd);
+
+ return ret_size;
+}
+
+struct nameInfo
+{
+ __u64 uniqueId;
+ __u64 userId;
+ __u64 processId;
+ __u32 sec_label_len;
+ char *sec_label;
+};
+
+/**
+ * Performs kdbus query of id of the given name
+ *
+ * @param name name to query for
+ * @param fd bus file
+ * @param ownerID place to store id of the name
+ * @return 0 on success, -errno if failed
+ */
+static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
+{
+ struct kdbus_cmd_name_info *msg;
+ struct kdbus_item *item;
+ uint64_t size;
+ int ret;
+ uint64_t item_size;
+
+ pInfo->sec_label_len = 0;
+ pInfo->sec_label = NULL;
+
+ item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
+ item_size = (item_size < 56) ? 56 : item_size; //at least 56 bytes are needed by kernel to place info about name, otherwise error
+ size = sizeof(struct kdbus_cmd_name_info) + item_size;
+
+ msg = malloc(size);
+ if (!msg)
+ {
+ _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
+ return -1;
+ }
+
+ memset(msg, 0, size);
+ msg->size = size;
+ if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
+ msg->id = strtoull(&name[3], NULL, 10);
+ else
+ msg->id = 0;
+
+ item = msg->items;
+ item->type = KDBUS_NAME_INFO_ITEM_NAME;
+ item->size = item_size;
+ strcpy(item->str, name);
+
+ again:
+ ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
+ if (ret < 0)
+ {
+ if(errno == EINTR)
+ goto again;
+ else if(ret == -ENOBUFS)
+ {
+ msg = realloc(msg, msg->size); //prepare memory
+ if(msg != NULL)
+ goto again;
+ }
+ pInfo->uniqueId = 0;
+ ret = -errno;
+ }
+ else
+ {
+ pInfo->uniqueId = msg->id;
+ pInfo->userId = msg->creds.uid;
+ pInfo->processId = msg->creds.pid;
+_dbus_verbose ("I'm alive 1\n");
+ item = msg->items;
+ while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
+ {
+ if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
+ {
+ pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
+ if(pInfo->sec_label_len != 0)
+ pInfo->sec_label = malloc(pInfo->sec_label_len);
+ if(pInfo->sec_label == NULL)
+ ret = -1;
+ else
+ memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
+
+ break;
+ }
+ item = KDBUS_PART_NEXT(item);
+ }
+ }
+
+ free(msg);
+ return ret;
+}
+
+/**
+ * Handles messages sent to bus daemon - "org.freedesktop.DBus" and translates them to appropriate
+ * kdbus ioctl commands. Than translate kdbus reply into dbus message and put it into recived messages queue.
+ *
+ * !!! Not all methods are handled !!! Doubt if it is even possible.
+ * If method is not handled, returns error reply org.freedesktop.DBus.Error.UnknownMethod
+ *
+ * Handled methods:
+ * - GetNameOwner
+ * - NameHasOwner
+ * - ListNames
+ *
+ * Not handled methods:
+ * - ListActivatableNames
+ * - StartServiceByName
+ * - UpdateActivationEnvironment
+ * - GetConnectionUnixUser
+ * - GetId
+ */
+static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
+{
+ int inter_ret;
+ struct nameInfo info;
+ dbus_bool_t ret_value;
+
+ if(!strcmp(dbus_message_get_member(message), "GetNameOwner")) //returns id of the well known name
+ {
+ char* name = NULL;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+ inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+ if(inter_ret == 0) //unique id of the name
+ {
+ char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
+ const char* pString = unique_name;
+
+ sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
+ _dbus_verbose("Unique name discovered:%s\n", unique_name);
+ ret_value = reply_1_data(message, DBUS_TYPE_STRING, &pString, transport->connection);
+ }
+ else if(inter_ret == -ENOENT) //name has no owner
+ return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get owner of name '%s': no such name", name, message, transport->connection);
+ else
+ {
+ _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
+ ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine unique name for '%s'", name, message, transport->connection);
+ }
+ }
+ else if(!strcmp(dbus_message_get_member(message), "NameHasOwner")) //returns if name is currently registered on the bus
+ {
+ char* name = NULL;
+ dbus_bool_t result;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+ inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+ if((inter_ret == 0) || (inter_ret == -ENOENT))
+ {
+ result = (inter_ret == 0) ? TRUE : FALSE;
+ ret_value = reply_1_data(message, DBUS_TYPE_BOOLEAN, &result, transport->connection);
+ }
+ else
+ {
+ _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
+ ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name, message, transport->connection);
+ }
+ }
+ else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
+ {
+ char* name = NULL;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+ inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+ if(inter_ret == 0) //name found
+ {
+ _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
+ ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.userId, transport->connection);
+ }
+ else if(inter_ret == -ENOENT) //name has no owner
+ return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get UID of name '%s': no such name", name, message, transport->connection);
+ else
+ {
+ _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
+ ret_value = reply_with_error(DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name, message, transport->connection);
+ }
+ }
+ else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixProcessID"))
+ {
+ char* name = NULL;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+ inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+ if(inter_ret == 0) //name found
+ ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.processId, transport->connection);
+ else if(inter_ret == -ENOENT) //name has no owner
+ return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get PID of name '%s': no such name", name, message, transport->connection);
+ else
+ {
+ _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
+ ret_value = reply_with_error(DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,"Could not determine PID for '%s'", name, message, transport->connection);
+ }
+ }
+ else if(!strcmp(dbus_message_get_member(message), "ListNames")) //return all well known names on he bus
+ {
+ struct kdbus_cmd_names* pCmd;
+ uint64_t cmd_size;
+
+ cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
+ pCmd = malloc(cmd_size);
+ if(pCmd == NULL)
+ goto out;
+ pCmd->size = cmd_size;
+
+ again:
+ cmd_size = 0;
+ if(ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_LIST, pCmd))
+ {
+ if(errno == EINTR)
+ goto again;
+ if(errno == ENOBUFS) //buffer to small to put all names into it
+ cmd_size = pCmd->size; //here kernel tells how much memory it needs
+ else
+ {
+ _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
+ goto out;
+ }
+ }
+ if(cmd_size) //kernel needs more memory
+ {
+ pCmd = realloc(pCmd, cmd_size); //prepare memory
+ if(pCmd == NULL)
+ return FALSE;
+ goto again; //and try again
+ }
+ else
+ {
+ DBusMessage *reply;
+ DBusMessageIter args;
+ struct kdbus_cmd_name* pCmd_name;
+ char* pName;
+
+ reply = dbus_message_new_method_return(message);
+ if(reply == NULL)
+ goto out;
+ dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
+ dbus_message_iter_init_append(reply, &args);
+
+ for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
+ {
+ pName = pCmd_name->name;
+ if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pName))
+ {
+ dbus_message_unref(reply);
+ goto out;
+ }
+ }
+
+ if(add_message_to_received(reply, transport->connection))
+ {
+ free(pCmd);
+ return TRUE;
+ }
+ }
+out:
+ if(pCmd)
+ free(pCmd);
+ return FALSE;
+ }
+ else if(!strcmp(dbus_message_get_member(message), "GetId"))
+ {
+ char* path;
+ char uuid[DBUS_UUID_LENGTH_BYTES];
+ struct stat stats;
+ MD5_CTX md5;
+ DBusString binary, encoded;
+
+ ret_value = FALSE;
+ path = &transport->address[11]; //start of kdbus bus path
+ if(stat(path, &stats) < -1)
+ {
+ _dbus_verbose("kdbus error reading stats of bus: err %d (%m)\n", errno);
+ return reply_with_error(DBUS_ERROR_FAILED, "Could not determine bus '%s' uuid", path, message, transport->connection);
+ }
+
+ MD5_Init(&md5);
+ MD5_Update(&md5, path, strlen(path));
+ MD5_Update(&md5, &stats.st_ctim.tv_sec, sizeof(stats.st_ctim.tv_sec));
+ MD5_Final(uuid, &md5);
+
+ if(!_dbus_string_init (&encoded))
+ goto outgid;
+ _dbus_string_init_const_len (&binary, uuid, DBUS_UUID_LENGTH_BYTES);
+ if(!_dbus_string_hex_encode (&binary, 0, &encoded, _dbus_string_get_length (&encoded)))
+ goto outb;
+ path = (char*)_dbus_string_get_const_data (&encoded);
+ ret_value = reply_1_data(message, DBUS_TYPE_STRING, &path, transport->connection);
+
+ outb:
+ _dbus_string_free(&binary);
+ _dbus_string_free(&encoded);
+ outgid:
+ return ret_value;
+ }
+ else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData"))
+ {
+ char* name = NULL;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+ return reply_with_error(DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN, "Could not determine audit session data for '%s'", name, message, transport->connection);
+ }
+ else if(!strcmp(dbus_message_get_member(message), "GetConnectionSELinuxSecurityContext"))
+ {
+ char* name = NULL;
+
+ dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+ inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+ if(inter_ret == -ENOENT) //name has no owner
+ return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get security context of name '%s': no such name", name, message, transport->connection);
+ else if(inter_ret < 0)
+ return reply_with_error(DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN, "Could not determine security context for '%s'", name, message, transport->connection);
+ else
+ {
+ DBusMessage *reply;
+
+ ret_value = FALSE;
+ reply = dbus_message_new_method_return(message);
+ if(reply != NULL)
+ {
+ dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
+ if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
+ dbus_message_unref(reply);
+ else if(add_message_to_received(reply, transport->connection))
+ ret_value = TRUE;
+ }
+ }
+ }
+ else
+ return reply_with_error(DBUS_ERROR_UNKNOWN_METHOD, NULL, (char*)dbus_message_get_member(message), message, transport->connection);
+/* else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames")) //todo
+ {
+
+ }
+ else if(!strcmp(dbus_message_get_member(message), "StartServiceByName"))
+ {
+
+ }
+ else if(!strcmp(dbus_message_get_member(message), "UpdateActivationEnvironment"))
+ {
+
+ }
+ else if(!strcmp(dbus_message_get_member(message), "ReloadConfig"))
+ {
+
+ }
+ */
+
+ if(info.sec_label)
+ free(info.sec_label);
+ return ret_value;
+}
+
+#if KDBUS_MSG_DECODE_DEBUG == 1
+static char *msg_id(uint64_t id, char *buf)
+{
+ if (id == 0)
+ return "KERNEL";
+ if (id == ~0ULL)
+ return "BROADCAST";
+ sprintf(buf, "%llu", (unsigned long long)id);
+ return buf;
+}
+#endif
+struct kdbus_enum_table {
+ long long id;
+ const char *name;
+};
+#define _STRINGIFY(x) #x
+#define STRINGIFY(x) _STRINGIFY(x)
+#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
+#define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
+#define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
+#define LOOKUP(what) \
+ const char *enum_##what(long long id) { \
+ size_t i; \
+ for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++) \
+ if (id == kdbus_table_##what[i].id) \
+ return kdbus_table_##what[i].name; \
+ return "UNKNOWN"; \
+ }
+const char *enum_MSG(long long id);
+TABLE(MSG) = {
+ ENUM(_KDBUS_MSG_NULL),
+ ENUM(KDBUS_MSG_PAYLOAD_VEC),
+ ENUM(KDBUS_MSG_PAYLOAD_OFF),
+ ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
+ ENUM(KDBUS_MSG_FDS),
+ ENUM(KDBUS_MSG_BLOOM),
+ ENUM(KDBUS_MSG_DST_NAME),
+ ENUM(KDBUS_MSG_SRC_CREDS),
+ ENUM(KDBUS_MSG_SRC_PID_COMM),
+ ENUM(KDBUS_MSG_SRC_TID_COMM),
+ ENUM(KDBUS_MSG_SRC_EXE),
+ ENUM(KDBUS_MSG_SRC_CMDLINE),
+ ENUM(KDBUS_MSG_SRC_CGROUP),
+ ENUM(KDBUS_MSG_SRC_CAPS),
+ ENUM(KDBUS_MSG_SRC_SECLABEL),
+ ENUM(KDBUS_MSG_SRC_AUDIT),
+ ENUM(KDBUS_MSG_SRC_NAMES),
+ ENUM(KDBUS_MSG_TIMESTAMP),
+ ENUM(KDBUS_MSG_NAME_ADD),
+ ENUM(KDBUS_MSG_NAME_REMOVE),
+ ENUM(KDBUS_MSG_NAME_CHANGE),
+ ENUM(KDBUS_MSG_ID_ADD),
+ ENUM(KDBUS_MSG_ID_REMOVE),
+ ENUM(KDBUS_MSG_REPLY_TIMEOUT),
+ ENUM(KDBUS_MSG_REPLY_DEAD),
+};
+LOOKUP(MSG);
+const char *enum_PAYLOAD(long long id);
+TABLE(PAYLOAD) = {
+ ENUM(KDBUS_PAYLOAD_KERNEL),
+ ENUM(KDBUS_PAYLOAD_DBUS1),
+ ENUM(KDBUS_PAYLOAD_GVARIANT),
+};
+LOOKUP(PAYLOAD);
+
+/**
+ * Puts locally generated message into received data buffer.
+ * Use only during receiving phase!
+ *
+ * @param message message to load
+ * @param data place to load message
+ * @return size of message
+ */
+static int put_message_into_data(DBusMessage *message, char* data)
+{
+ int ret_size;
+ const DBusString *header;
+ const DBusString *body;
+ int size;
+
+ dbus_message_lock (message);
+ _dbus_message_get_network_data (message, &header, &body);
+ ret_size = _dbus_string_get_length(header);
+ memcpy(data, _dbus_string_get_const_data(header), ret_size);
+ data += ret_size;
+ size = _dbus_string_get_length(body);
+ memcpy(data, _dbus_string_get_const_data(body), size);
+ ret_size += size;
+ dbus_message_unref(message);
+
+ return ret_size;
+}
+/**
+ * Decodes kdbus message in order to extract dbus message and put it into data and fds.
+ * Also captures and decodes kdbus error messages and kdbus kernel broadcasts and converts
+ * all of them into dbus messages.
+ *
+ * @param msg kdbus message
+ * @param data place to copy dbus message to
+ * @param socket_transport transport
+ * @param fds place to store file descriptors received
+ * @param n_fds place to store quantity of file descriptor
+ * @return number of dbus message's bytes received or -1 on error
+ */
+static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportSocket* socket_transport, int* fds, int* n_fds)
+{
+ const struct kdbus_item *item;
+ int ret_size = 0;
+ DBusMessage *message = NULL;
+ DBusMessageIter args;
+ const char* emptyString = "";
+ const char* pString = NULL;
+ char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
+ const char* pDBusName = dbus_name;
+#if KDBUS_MSG_DECODE_DEBUG == 1
+ char buf[32];
+#endif
+
+#if KDBUS_MSG_DECODE_DEBUG == 1
+ _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s → %s, cookie=%llu, timeout=%llu\n",
+ enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
+ (unsigned long long) msg->flags,
+ msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
+ (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
+#endif
+
+ *n_fds = 0;
+
+ KDBUS_PART_FOREACH(item, msg, items)
+ {
+ if (item->size <= KDBUS_PART_HEADER_SIZE)
+ {
+ _dbus_verbose(" +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
+ break; //??? continue (because dbus will find error) or break
+ }
+
+ switch (item->type)
+ {
+ case KDBUS_MSG_PAYLOAD_OFF:
+ memcpy(data, (char *)socket_transport->kdbus_mmap_ptr + item->vec.offset, item->vec.size);
+ data += item->vec.size;
+ ret_size += item->vec.size;
+
+ _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
+ enum_MSG(item->type), item->size,
+ (unsigned long long)item->vec.offset,
+ (unsigned long long)item->vec.size);
+ break;
+
+ case KDBUS_MSG_PAYLOAD_MEMFD:
+ {
+ char *buf;
+ uint64_t size;
+
+ size = item->memfd.size;
+ _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
+
+ buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
+ if (buf == MAP_FAILED)
+ {
+ _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
+ return -1;
+ }
+
+ memcpy(data, buf, size);
+ data += size;
+ ret_size += size;
+
+ munmap(buf, size);
+
+ _dbus_verbose(" +%s (%llu bytes) off=%llu size=%llu\n",
+ enum_MSG(item->type), item->size,
+ (unsigned long long)item->vec.offset,
+ (unsigned long long)item->vec.size);
+ break;
+ }
+
+ case KDBUS_MSG_FDS:
+ {
+ int i;
+
+ *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
+ memcpy(fds, item->fds, *n_fds * sizeof(int));
+ for (i = 0; i < *n_fds; i++)
+ _dbus_fd_set_close_on_exec(fds[i]);
+ break;
+ }
+
+#if KDBUS_MSG_DECODE_DEBUG == 1
+ case KDBUS_MSG_SRC_CREDS:
+ _dbus_verbose(" +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
+ enum_MSG(item->type), item->size,
+ item->creds.uid, item->creds.gid,
+ item->creds.pid, item->creds.tid,
+ item->creds.starttime);
+ break;
+
+ case KDBUS_MSG_SRC_PID_COMM:
+ case KDBUS_MSG_SRC_TID_COMM:
+ case KDBUS_MSG_SRC_EXE:
+ case KDBUS_MSG_SRC_CGROUP:
+ case KDBUS_MSG_SRC_SECLABEL:
+ case KDBUS_MSG_DST_NAME:
+ _dbus_verbose(" +%s (%llu bytes) '%s' (%zu)\n",
+ enum_MSG(item->type), item->size, item->str, strlen(item->str));
+ break;
+
+ case KDBUS_MSG_SRC_CMDLINE:
+ case KDBUS_MSG_SRC_NAMES: {
+ __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
+ const char *str = item->str;
+ int count = 0;
+
+ _dbus_verbose(" +%s (%llu bytes) ", enum_MSG(item->type), item->size);
+ while (size) {
+ _dbus_verbose("'%s' ", str);
+ size -= strlen(str) + 1;
+ str += strlen(str) + 1;
+ count++;
+ }
+
+ _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
+ break;
+ }
+
+ case KDBUS_MSG_SRC_AUDIT:
+ _dbus_verbose(" +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
+ enum_MSG(item->type), item->size,
+ (unsigned long long)item->data64[0],
+ (unsigned long long)item->data64[1]);
+ break;
+
+ case KDBUS_MSG_SRC_CAPS: {
+ int n;
+ const uint32_t *cap;
+ int i;
+
+ _dbus_verbose(" +%s (%llu bytes) len=%llu bytes)\n",
+ enum_MSG(item->type), item->size,
+ (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
+
+ cap = item->data32;
+ n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
+
+ _dbus_verbose(" CapInh=");
+ for (i = 0; i < n; i++)
+ _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
+
+ _dbus_verbose(" CapPrm=");
+ for (i = 0; i < n; i++)
+ _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
+
+ _dbus_verbose(" CapEff=");
+ for (i = 0; i < n; i++)
+ _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
+
+ _dbus_verbose(" CapInh=");
+ for (i = 0; i < n; i++)
+ _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
+ _dbus_verbose("\n");
+ break;
+ }
+
+ case KDBUS_MSG_TIMESTAMP:
+ _dbus_verbose(" +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
+ enum_MSG(item->type), item->size,
+ (unsigned long long)item->timestamp.realtime_ns,
+ (unsigned long long)item->timestamp.monotonic_ns);
+ break;
+#endif
+
+ case KDBUS_MSG_REPLY_TIMEOUT:
+ _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
+ enum_MSG(item->type), item->size, msg->cookie_reply);
+
+ message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ ret_size = put_message_into_data(message, data);
+ break;
+
+ case KDBUS_MSG_REPLY_DEAD:
+ _dbus_verbose(" +%s (%llu bytes) cookie=%llu\n",
+ enum_MSG(item->type), item->size, msg->cookie_reply);
+
+ message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NAME_HAS_NO_OWNER, NULL);
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ ret_size = put_message_into_data(message, data);
+ break;
+
+ case KDBUS_MSG_NAME_ADD:
+ _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+ enum_MSG(item->type), (unsigned long long) item->size,
+ item->name_change.name, item->name_change.old_id,
+ item->name_change.new_id, item->name_change.flags);
+
+ message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ sprintf(dbus_name,":1.%llu",item->name_change.new_id);
+ pString = item->name_change.name;
+ _dbus_verbose ("Name added: %s\n", pString);
+ dbus_message_iter_init_append(message, &args);
+ ITER_APPEND_STR(pString)
+ ITER_APPEND_STR(emptyString)
+ ITER_APPEND_STR(pDBusName)
+ dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
+
+ ret_size = put_message_into_data(message, data);
+ break;
+
+ case KDBUS_MSG_NAME_REMOVE:
+ _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+ enum_MSG(item->type), (unsigned long long) item->size,
+ item->name_change.name, item->name_change.old_id,
+ item->name_change.new_id, item->name_change.flags);
+
+ message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged"); // name of the signal
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ sprintf(dbus_name,":1.%llu",item->name_change.old_id);
+ pString = item->name_change.name;
+ _dbus_verbose ("Name removed: %s\n", pString);
+ dbus_message_iter_init_append(message, &args);
+ ITER_APPEND_STR(pString)
+ ITER_APPEND_STR(pDBusName)
+ ITER_APPEND_STR(emptyString)
+ dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
+
+ ret_size = put_message_into_data(message, data);
+ break;
+
+ case KDBUS_MSG_NAME_CHANGE:
+ _dbus_verbose(" +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
+ enum_MSG(item->type), (unsigned long long) item->size,
+ item->name_change.name, item->name_change.old_id,
+ item->name_change.new_id, item->name_change.flags);
+
+ message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ sprintf(dbus_name,":1.%llu",item->name_change.old_id);
+ pString = item->name_change.name;
+ _dbus_verbose ("Name changed: %s\n", pString);
+ dbus_message_iter_init_append(message, &args);
+ ITER_APPEND_STR(pString)
+ ITER_APPEND_STR(pDBusName)
+ sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
+ _dbus_verbose ("New id: %s\n", pDBusName);
+ ITER_APPEND_STR(pDBusName)
+ dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
+
+ ret_size = put_message_into_data(message, data);
+ break;
+
+ case KDBUS_MSG_ID_ADD:
+ _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
+ enum_MSG(item->type), (unsigned long long) item->size,
+ (unsigned long long) item->id_change.id,
+ (unsigned long long) item->id_change.flags);
+
+ message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ sprintf(dbus_name,":1.%llu",item->id_change.id);
+ dbus_message_iter_init_append(message, &args);
+ ITER_APPEND_STR(pDBusName)
+ ITER_APPEND_STR(emptyString)
+ ITER_APPEND_STR(pDBusName)
+ dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
+
+ ret_size = put_message_into_data(message, data);
+ break;
+
+ case KDBUS_MSG_ID_REMOVE:
+ _dbus_verbose(" +%s (%llu bytes) id=%llu flags=%llu\n",
+ enum_MSG(item->type), (unsigned long long) item->size,
+ (unsigned long long) item->id_change.id,
+ (unsigned long long) item->id_change.flags);
+
+ message = dbus_message_new_signal(DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameOwnerChanged");
+ if(message == NULL)
+ {
+ ret_size = -1;
+ goto out;
+ }
+
+ sprintf(dbus_name,":1.%llu",item->id_change.id);
+ dbus_message_iter_init_append(message, &args);
+ ITER_APPEND_STR(pDBusName)
+ ITER_APPEND_STR(pDBusName)
+ ITER_APPEND_STR(emptyString)
+ dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
+
+ ret_size = put_message_into_data(message, data);
+ break;
+#if KDBUS_MSG_DECODE_DEBUG == 1
+ default:
+ _dbus_verbose(" +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
+ break;
+#endif
+ }
+ }
+
+#if KDBUS_MSG_DECODE_DEBUG == 1
+
+ if ((char *)item - ((char *)msg + msg->size) >= 8)
+ _dbus_verbose("invalid padding at end of message\n");
+#endif
+
+out:
+ if(message)
+ dbus_message_unref(message);
+ return ret_size;
+}
+
+/**
+ * Reads message from kdbus and puts it into dbus buffer and fds
+ *
+ * @param transport transport
+ * @param buffer place to copy received message to
+ * @param fds place to store file descriptors sent in the message
+ * @param n_fds place to store number of file descriptors
+ * @return size of received message on success, -1 on error
+ */
+static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString *buffer, int* fds, int* n_fds)
+{
+ int ret_size;
+ uint64_t __attribute__ ((__aligned__(8))) offset;
+ struct kdbus_msg *msg;
+ char *data;
+ int start;
+
+ start = _dbus_string_get_length (buffer);
+ if (!_dbus_string_lengthen (buffer, socket_transport->max_bytes_read_per_iteration))
+ {
+ errno = ENOMEM;
+ return -1;
+ }
+ data = _dbus_string_get_data_len (buffer, start, socket_transport->max_bytes_read_per_iteration);
+
+ again:
+ if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
+ {
+ if(errno == EINTR)
+ goto again;
+ _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
+ _dbus_string_set_length (buffer, start);
+ return -1;
+ }
+
+ msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
+
+ ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
+
+ if(ret_size == -1) /* error */
+ {
+ _dbus_string_set_length (buffer, start);
+ return -1;
+ }
+ else
+ _dbus_string_set_length (buffer, start + ret_size);
+
+
+ again2:
+ if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
+ {
+ if(errno == EINTR)
+ goto again2;
+ _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
+ return -1;
+ }
+
+ return ret_size;
+}
+