1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* kdbus-d.c kdbus related daemon functions
4 * Copyright (C) 2013 Samsung Electronics
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version and under the terms of the GNU
12 * Lesser General Public License as published by the
13 * Free Software Foundation; either version 2.1 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus-connection-internal.h>
29 #include <dbus/kdbus.h>
30 #include <dbus/dbus-bus.h>
32 #include <dbus/kdbus-common.h>
33 #include <dbus/dbus-transport.h>
34 #include <dbus/dbus-transport-kdbus.h>
35 #include "connection.h"
36 #include "activation.h"
38 #include <dbus/dbus-connection.h>
48 * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
50 __u64 sender_name_to_id(const char* name, DBusError* error)
54 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
55 sender_id = strtoull(&name[3], NULL, 10);
57 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
63 * Seeks key in rule string, and duplicates value of the key into pValue.
64 * Because of the duplication, pValue must be freed after use.
66 * @param rule rule to look through
67 * @param key key to look for
68 * @param pValue pointer to value of the key found
69 * @return length of the value string, 0 means not found
71 static int parse_match_key(const char *rule, const char* key, char** pValue)
74 const char* pValueEnd;
77 pBegin = strstr(rule, key);
80 pBegin += strlen(key);
81 pValueEnd = strchr(pBegin, '\'');
84 value_length = pValueEnd - pBegin;
85 *pValue = strndup(pBegin, value_length);
87 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
94 * Adds a match rule to match broadcast messages going through the message bus.
95 * Do no affect messages addressed directly.
97 * The "rule" argument is the string form of a match rule.
99 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
101 * interface match key mapped to bloom
102 * sender match key mapped to src_name
104 * @param transport transport
105 * @param id id of connection for which the rule is to be added
106 * @param rule textual form of match rule
108 dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
110 struct kdbus_cmd_match* pCmd_match;
111 struct kdbus_item *pItem;
112 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
116 char* pInterface = NULL;
117 dbus_bool_t ret_value = FALSE;
121 if(!_dbus_transport_get_socket_fd(transport, &fd))
124 bloom_size = dbus_transport_get_bloom_size(transport);
126 /*parsing rule and calculating size of command*/
127 size = sizeof(struct kdbus_cmd_match);
128 if(parse_match_key(rule, "interface='", &pInterface)) /*actual size is not important for interface because bloom size is defined by bus*/
129 size += KDBUS_PART_HEADER_SIZE + bloom_size;
130 name_size = parse_match_key(rule, "sender='", &pName);
133 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
135 src_id = strtoull(&pName[3], NULL, 10);
140 size += KDBUS_ITEM_SIZE(name_size + 1); //well known name
143 pCmd_match = alloca(size);
144 if(pCmd_match == NULL)
148 pCmd_match->cookie = id;
149 pCmd_match->size = size;
150 pCmd_match->src_id = src_id;
152 pItem = pCmd_match->items;
155 pItem->type = KDBUS_MATCH_SRC_NAME;
156 pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
157 memcpy(pItem->str, pName, strlen(pName) + 1);
158 pItem = KDBUS_PART_NEXT(pItem);
162 pItem->type = KDBUS_MATCH_BLOOM;
163 pItem->size = KDBUS_PART_HEADER_SIZE + bloom_size;
164 strncpy(pItem->data, pInterface, bloom_size);
167 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
168 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
171 _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
184 * Opposing to dbus, in kdbus removes all match rules with given
185 * cookie, which in this implementation is equal to uniqe id.
187 * @param transport transport
188 * @param id connection id for which rules are to be removed
190 dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
192 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
195 if(!_dbus_transport_get_socket_fd(transport, &fd))
200 cmd.size = sizeof(struct kdbus_cmd_match);
202 if(ioctl(fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
204 _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
209 _dbus_verbose("Match rule removed correctly.\n");
215 * Performs kdbus query of id of the given name
217 * @param name name to query for
218 * @param transport transport
219 * @param pInfo nameInfo structure address to store info about the name
220 * @return 0 on success, -errno if failed
222 int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
224 struct kdbus_cmd_name_info *msg;
225 struct kdbus_item *item;
231 pInfo->sec_label_len = 0;
232 pInfo->sec_label = NULL;
234 if(!_dbus_transport_get_socket_fd(transport, &fd))
237 item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
238 item_size = (item_size < 56) ? 56 : item_size; //at least 56 bytes are needed by kernel to place info about name, otherwise error
239 size = sizeof(struct kdbus_cmd_name_info) + item_size;
244 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
248 memset(msg, 0, size);
250 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
251 msg->id = strtoull(&name[3], NULL, 10);
256 item->type = KDBUS_NAME_INFO_ITEM_NAME;
257 item->size = item_size;
258 memcpy(item->str, name, strlen(name) + 1);
261 ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
268 else if(ret == -ENOBUFS)
270 msg = realloc(msg, msg->size); //prepare memory
279 pInfo->uniqueId = msg->id;
280 pInfo->userId = msg->creds.uid;
281 pInfo->processId = msg->creds.pid;
283 while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
285 if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
287 pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
288 if(pInfo->sec_label_len != 0)
290 pInfo->sec_label = malloc(pInfo->sec_label_len);
291 if(pInfo->sec_label == NULL)
294 memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
298 item = KDBUS_PART_NEXT(item);
307 * Creates kdbus bus of given type.
309 char* make_kdbus_bus(DBusBusType type, DBusError *error)
312 struct kdbus_cmd_bus_make head;
316 } __attribute__ ((__aligned__(8))) bus_make;
321 _dbus_verbose("Opening /dev/kdbus/control\n");
322 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
325 _dbus_verbose("--- error %d (%m)\n", fdc);
326 dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
330 memset(&bus_make, 0, sizeof(bus_make));
331 bus_make.head.bloom_size = 64;
332 bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
334 if(type == DBUS_BUS_SYSTEM)
335 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
336 else if(type == DBUS_BUS_SESSION)
337 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
339 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());
341 bus_make.n_type = KDBUS_MAKE_NAME;
342 bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
343 bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
345 _dbus_verbose("Creating bus '%s'\n", bus_make.name);
346 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
349 _dbus_verbose("--- error %d (%m)\n", ret);
350 dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc);
354 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
360 _dbus_verbose("Return value '%s'\n", bus);
365 * Minimal server init needed by context to go further.
367 DBusServer* empty_server_init(char* address)
369 return dbus_server_init_mini(address);
372 static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusTransport* transport)
374 struct kdbus_cmd_match* pCmd_match;
375 struct kdbus_item *pItem;
379 if(!_dbus_transport_get_socket_fd(transport, &fd))
386 size = sizeof(struct kdbus_cmd_match);
387 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /*3 name related items plus 2 id related items*/
389 pCmd_match = alloca(size);
390 if(pCmd_match == NULL)
397 pCmd_match->cookie = 1;
398 pCmd_match->size = size;
400 pItem = pCmd_match->items;
401 pCmd_match->src_id = 0;
402 pItem->type = KDBUS_MATCH_NAME_CHANGE;
403 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
404 pItem = KDBUS_PART_NEXT(pItem);
405 pItem->type = KDBUS_MATCH_NAME_ADD;
406 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
407 pItem = KDBUS_PART_NEXT(pItem);
408 pItem->type = KDBUS_MATCH_NAME_REMOVE;
409 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
410 pItem = KDBUS_PART_NEXT(pItem);
411 pItem->type = KDBUS_MATCH_ID_ADD;
412 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
413 pItem = KDBUS_PART_NEXT(pItem);
414 pItem->type = KDBUS_MATCH_ID_REMOVE;
415 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
417 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
419 _dbus_verbose("Failed adding match rule for daemon, error: %d, %m\n", errno);
423 _dbus_verbose("Added match rule for daemon correctly.\n");
428 * Connects daemon to bus created by him and adds matches for "system" broadcasts.
429 * Do not requests org.freedesktop.DBus name, because it's to early
430 * (some structures of BusContext are not ready yet).
432 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
434 DBusConnection* connection;
436 dbus_bus_set_bus_connection_address(type, address);
438 connection = dbus_bus_get_private(type, error); /*todo possibly could be optimised by using lower functions*/
439 if(connection == NULL)
442 if(!add_matches_for_kdbus_broadcasts(dbus_connection_get_transport(connection)))
444 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for daemon, %s", _dbus_strerror_from_errno ());
448 if(dbus_error_is_set(error))
451 _dbus_connection_close_possibly_shared (connection);
452 dbus_connection_unref (connection);
456 _dbus_verbose ("Daemon connected as kdbus client.\n");
462 * Asks bus for org.freedesktop.DBus well-known name.
464 dbus_bool_t register_daemon_name(DBusConnection* connection)
466 DBusString daemon_name;
467 dbus_bool_t retval = FALSE;
468 BusTransaction *transaction;
470 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
471 if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
474 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
477 transaction = bus_transaction_new (bus_connection_get_context(connection));
478 if (transaction == NULL)
480 kdbus_release_name(connection, &daemon_name, 0);
484 if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
486 kdbus_release_name(connection, &daemon_name, 0);
493 bus_transaction_cancel_and_free(transaction);
497 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
501 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
503 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
506 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
510 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
512 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
516 * Asks kdbus for well-known names registered on the bus
518 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
521 struct kdbus_cmd_names* pCmd;
523 dbus_bool_t ret_val = FALSE;
529 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
530 pCmd = malloc(cmd_size);
533 pCmd->size = cmd_size;
535 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
539 if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
543 if(errno == ENOBUFS) //buffer to small to put all names into it
544 cmd_size = pCmd->size; //here kernel tells how much memory it needs
547 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
551 if(cmd_size) //kernel needs more memory
553 pCmd = realloc(pCmd, cmd_size); //prepare memory
556 goto again; //and try again
560 struct kdbus_cmd_name* pCmd_name;
562 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
565 list = malloc(sizeof(char*) * (list_len + 1));
569 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
571 list[i] = strdup(pCmd_name->name);
579 _dbus_verbose ("Name %d: %s\n", i, list[i]);
585 *array_len = list_len;
596 * Asks kdbus for list of connections being in the queue to own
597 * given well-known name. The list includes the owner of the name on the
600 dbus_bool_t kdbus_list_queued (DBusConnection *connection, DBusList **return_list,
601 const char *name, DBusError *error)
604 struct kdbus_cmd_names* pCmd;
606 dbus_bool_t ret_val = FALSE;
609 _dbus_assert (*return_list == NULL);
611 name_length = strlen(name) + 1;
612 cmd_size = sizeof(struct kdbus_cmd_names) + sizeof(struct kdbus_cmd_name) + name_length;
613 pCmd = malloc(cmd_size);
616 pCmd->size = cmd_size;
617 pCmd->names[0].id = 0;
618 pCmd->names[0].size = sizeof(struct kdbus_cmd_name) + name_length;
619 memcpy(pCmd->names[0].name, name, name_length);
620 _dbus_verbose ("Asking for queued owners of %s\n", pCmd->names[0].name);
622 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
626 if(ioctl(fd, KDBUS_CMD_NAME_LIST_QUEUED, pCmd))
630 if(errno == ENOBUFS) //buffer to small to put all names into it
631 cmd_size = pCmd->size; //here kernel tells how much memory it needs
632 else if(errno == ENOENT)
634 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
635 "Could not get owners of name '%s': no such name", name);
641 _dbus_verbose("kdbus error asking for queued owners list: err %d (%m)\n",errno);
645 if(cmd_size) //kernel needs more memory
647 pCmd = realloc(pCmd, cmd_size); //prepare memory
650 goto again; //and try again
654 struct kdbus_cmd_name* pCmd_name;
656 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
660 _dbus_verbose ("Got queued owner id: %llu\n", (unsigned long long)pCmd_name->id);
661 uname = malloc(snprintf(uname, 0, ":1.%llu0", (unsigned long long)pCmd_name->id));
664 sprintf(uname, ":1.%llu", (unsigned long long int)pCmd_name->id);
665 if (!_dbus_list_append (return_list, uname))
679 dbus_set_error (error, _dbus_error_from_errno (errno),
680 "Failed to list queued owners of \"%s\": %s",
681 name, _dbus_strerror (errno));
683 link = _dbus_list_get_first_link (return_list);
686 DBusList *next = _dbus_list_get_next_link (return_list, link);
688 if(link->data != NULL)
691 _dbus_list_free_link (link);
700 * Register match rule in kdbus on behalf of sender of the message
702 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
706 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
707 if(dbus_error_is_set(error))
710 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
712 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
713 sender_id, _dbus_strerror_from_errno ());
721 * Removes match rule in kdbus on behalf of sender of the message
723 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
727 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
728 if(dbus_error_is_set(error))
731 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
733 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
740 int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
743 struct nameInfo info;
745 ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
746 if(ret == 0) //unique id of the name
748 sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
749 _dbus_verbose("Unique name discovered:%s\n", owner);
751 else if(ret != -ENOENT)
752 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
758 * Asks kdbus for uid of the owner of the name given in the message
760 dbus_bool_t kdbus_get_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
762 struct nameInfo info;
764 dbus_bool_t ret = FALSE;
766 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
767 if(inter_ret == 0) //name found
769 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
773 else if(inter_ret == -ENOENT) //name has no owner
775 _dbus_verbose ("Name %s has no owner.\n", name);
776 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
781 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
782 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
789 * Asks kdbus for pid of the owner of the name given in the message
791 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, unsigned long* pid, DBusError* error)
793 struct nameInfo info;
795 dbus_bool_t ret = FALSE;
797 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
798 if(inter_ret == 0) //name found
800 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
801 *pid = info.processId;
804 else if(inter_ret == -ENOENT) //name has no owner
805 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
808 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
809 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
816 * Asks kdbus for selinux_security_context of the owner of the name given in the message
818 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
821 struct nameInfo info;
823 dbus_bool_t ret = FALSE;
825 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
826 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
827 if(inter_ret == -ENOENT) //name has no owner
828 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
829 else if(inter_ret < 0)
831 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
832 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
836 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
838 _DBUS_SET_OOM (error);
848 * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
849 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
850 * for now., though in theory someone could hook Windows to NIS or
851 * something. Always returns #FALSE prior to authenticating the
854 * The UID of is only read by bus daemon from kdbus. You can not
855 * call this function from client side of the connection.
857 * You can ask the bus to tell you the UID of another connection though
858 * if you like; this is done with dbus_bus_get_unix_user().
860 * @param connection the connection
861 * @param uid return location for the user ID
862 * @returns #TRUE if uid is filled in with a valid user ID
865 dbus_connection_get_unix_user (DBusConnection *connection,
868 _dbus_return_val_if_fail (connection != NULL, FALSE);
869 _dbus_return_val_if_fail (uid != NULL, FALSE);
871 if(bus_context_is_kdbus(bus_connection_get_context (connection)))
872 return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
874 return dbus_connection_get_unix_user_dbus(connection, uid);
878 * Gets the process ID of the connection if any.
879 * Returns #TRUE if the pid is filled in.
881 * @param connection the connection
882 * @param pid return location for the process ID
883 * @returns #TRUE if uid is filled in with a valid process ID
886 dbus_connection_get_unix_process_id (DBusConnection *connection,
889 _dbus_return_val_if_fail (connection != NULL, FALSE);
890 _dbus_return_val_if_fail (pid != NULL, FALSE);
892 if(bus_context_is_kdbus(bus_connection_get_context (connection)))
893 return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
895 return dbus_connection_get_unix_process_id_dbus(connection, pid);
899 * Create connection structure for given name. It is needed to control starters - activatable services
900 * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
901 * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
903 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
905 DBusConnection *phantom_connection;
908 _dbus_string_init_const(&Sname, name);
910 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
911 if(phantom_connection == NULL)
913 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
915 dbus_connection_unref_phantom(phantom_connection);
916 phantom_connection = NULL;
917 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
920 if(!bus_connection_complete(phantom_connection, &Sname, error))
922 bus_connection_disconnected(phantom_connection);
923 phantom_connection = NULL;
927 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
930 return phantom_connection;
934 * Registers activatable services as kdbus starters.
936 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
940 dbus_bool_t retval = FALSE;
942 BusTransaction *transaction;
944 DBusTransport* transport;
945 unsigned long int euid;
947 transaction = bus_transaction_new (bus_connection_get_context(connection));
948 if (transaction == NULL)
951 if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
954 transport = dbus_connection_get_transport(connection);
957 if(!_dbus_transport_get_socket_fd (transport, &fd))
960 _dbus_string_init(&name);
964 if(!register_kdbus_policy(services[i], transport, euid))
967 if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
970 if(!_dbus_string_append(&name, services[i]))
972 if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
973 (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
975 if(!_dbus_string_set_length(&name, 0))
984 release_kdbus_name(fd, services[j], 0);
986 dbus_free_string_array (services);
987 _dbus_string_free(&name);
988 bus_transaction_cancel_and_free(transaction);
993 * Updates kdbus starters (activatable services) after configuration was reloaded.
994 * It releases all previous starters and registers all new.
996 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
998 dbus_bool_t retval = FALSE;
999 DBusList **services_old;
1001 BusService *service = NULL;
1002 BusTransaction *transaction;
1005 transaction = bus_transaction_new (bus_connection_get_context(connection));
1006 if (transaction == NULL)
1009 if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
1012 services_old = bus_connection_get_services_owned(connection);
1013 link = _dbus_list_get_first_link(services_old);
1014 link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
1016 while (link != NULL)
1020 service = (BusService*) link->data;
1024 ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
1026 if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
1028 if(!bus_service_remove_owner(service, connection, transaction, NULL))
1029 _dbus_verbose ("Unable to remove\n");
1034 link = _dbus_list_get_next_link (services_old, link);
1037 if(!register_kdbus_starters(connection))
1039 _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
1045 bus_transaction_cancel_and_free(transaction);
1050 * Analyzes system broadcasts about id and name changes.
1051 * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
1053 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
1055 const char *name, *old, *new;
1057 if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
1059 _dbus_verbose ("Couldn't get args of NameOwnerChanged signal.\n");//, error.message);
1063 _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
1065 if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
1067 if(!strcmp(name, old)) //yes it is - someone has disconnected
1069 DBusConnection* conn;
1071 conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
1073 bus_connection_disconnected(conn);
1076 else //it is well-known name
1078 if((*old != 0) && (strcmp(old, ":1.1")))
1080 DBusMessage *message;
1082 if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
1085 _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
1087 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
1088 if (message == NULL)
1091 if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
1092 DBUS_TYPE_STRING, &name,
1095 dbus_message_unref (message);
1099 bus_transaction_send_from_driver (transaction, connection, message);
1100 dbus_message_unref (message);
1103 if((*new != 0) && (strcmp(new, ":1.1")))
1105 DBusMessage *message;
1107 _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
1109 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
1110 if (message == NULL)
1113 if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
1114 DBUS_TYPE_STRING, &name,
1117 dbus_message_unref (message);
1121 bus_transaction_send_from_driver (transaction, connection, message);
1122 dbus_message_unref (message);
1126 if(bus_transaction_send(transaction, connection, msg))
1127 _dbus_verbose ("NameOwnerChanged sent\n");
1129 _dbus_verbose ("Sending NameOwnerChanged failed\n");