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
26 #include <dbus/dbus-connection-internal.h>
28 #include <dbus/kdbus.h>
29 #include <dbus/dbus-bus.h>
31 #include <dbus/dbus-transport.h>
32 #include <dbus/dbus-transport-kdbus.h>
33 #include "connection.h"
34 #include "activation.h"
36 #include <dbus/dbus-connection.h>
46 * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
48 __u64 sender_name_to_id(const char* name, DBusError* error)
52 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
53 sender_id = strtoull(&name[3], NULL, 10);
55 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
61 * Seeks key in rule string, and duplicates value of the key into pValue.
62 * Because of the duplication, pValue must be freed after use.
64 * @param rule rule to look through
65 * @param key key to look for
66 * @param pValue pointer to value of the key found
67 * @return length of the value string, 0 means not found
69 static int parse_match_key(const char *rule, const char* key, char** pValue)
72 const char* pValueEnd;
75 pBegin = strstr(rule, key);
78 pBegin += strlen(key);
79 pValueEnd = strchr(pBegin, '\'');
82 value_length = pValueEnd - pBegin;
83 *pValue = strndup(pBegin, value_length);
85 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
92 * Adds a match rule to match broadcast messages going through the message bus.
93 * Do no affect messages addressed directly.
95 * The "rule" argument is the string form of a match rule.
97 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
99 * interface match key mapped to bloom
100 * sender match key mapped to src_name
102 * @param transport transport
103 * @param id id of connection for which the rule is to be added
104 * @param rule textual form of match rule
106 dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
108 struct kdbus_cmd_match* pCmd_match;
109 struct kdbus_item *pItem;
110 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
114 char* pInterface = NULL;
115 dbus_bool_t ret_value = FALSE;
119 if(!_dbus_transport_get_socket_fd(transport, &fd))
122 bloom_size = dbus_transport_get_bloom_size(transport);
124 /*parsing rule and calculating size of command*/
125 size = sizeof(struct kdbus_cmd_match);
126 if(parse_match_key(rule, "interface='", &pInterface)) /*actual size is not important for interface because bloom size is defined by bus*/
127 size += KDBUS_ITEM_HEADER_SIZE + bloom_size;
128 name_size = parse_match_key(rule, "sender='", &pName);
131 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
133 src_id = strtoull(&pName[3], NULL, 10);
138 size += KDBUS_ITEM_SIZE(name_size + 1); //well known name
141 pCmd_match = alloca(size);
142 if(pCmd_match == NULL)
146 pCmd_match->cookie = id;
147 pCmd_match->size = size;
148 pCmd_match->src_id = src_id;
150 pItem = pCmd_match->items;
153 pItem->type = KDBUS_MATCH_SRC_NAME;
154 pItem->size = KDBUS_ITEM_HEADER_SIZE + name_size + 1;
155 memcpy(pItem->str, pName, strlen(pName) + 1);
156 pItem = KDBUS_PART_NEXT(pItem);
160 pItem->type = KDBUS_MATCH_BLOOM;
161 pItem->size = KDBUS_ITEM_HEADER_SIZE + bloom_size;
162 strncpy(pItem->data, pInterface, bloom_size);
165 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
166 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
169 _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
182 * Opposing to dbus, in kdbus removes all match rules with given
183 * cookie, which in this implementation is equal to uniqe id.
185 * @param transport transport
186 * @param id connection id for which rules are to be removed
188 dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
190 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
193 if(!_dbus_transport_get_socket_fd(transport, &fd))
198 cmd.size = sizeof(struct kdbus_cmd_match);
200 if(ioctl(fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
202 _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
207 _dbus_verbose("Match rule removed correctly.\n");
213 * Creates kdbus bus of given type.
215 char* make_kdbus_bus(DBusBusType type, const char* address, DBusError *error)
217 // TODO Function alloca() used. In upstream there was a patch proposing to
218 // replace alloca() with malloc() to assure memory alignment. If there will be
219 // suggestion to use malloc instead of alloca this function has to be modified
220 struct kdbus_cmd_make *bus_make;
221 struct kdbus_item *item;
222 __u64 name_size, bus_make_size;
224 char *addr_value = NULL;
228 if(type == DBUS_BUS_SYSTEM)
229 name_size = snprintf(name, 0, "%u-kdbus-%s", getuid(), "system") + 1;
230 else if(type == DBUS_BUS_SESSION)
231 name_size = snprintf(name, 0, "%u-kdbus", getuid()) + 1;
233 name_size = snprintf(name, 0, "%u-kdbus-%u", getuid(), getpid()) + 1;
235 name = alloca(name_size);
241 bus_make_size = sizeof(struct kdbus_cmd_make) + KDBUS_ITEM_SIZE(name_size) + KDBUS_ITEM_SIZE(sizeof(__u64));
242 bus_make = alloca(bus_make_size);
248 bus_make->size = bus_make_size;
249 #ifdef POLICY_TO_KDBUS
250 bus_make->flags = KDBUS_MAKE_ACCESS_WORLD;
252 bus_make->flags = KDBUS_MAKE_ACCESS_WORLD | KDBUS_MAKE_POLICY_OPEN;
254 item = bus_make->items;
256 item->type = KDBUS_ITEM_MAKE_NAME;
257 item->size = KDBUS_ITEM_HEADER_SIZE + name_size;
258 if(type == DBUS_BUS_SYSTEM)
259 sprintf(name, "%u-kdbus-%s", getuid(), "system");
260 else if(type == DBUS_BUS_SESSION)
261 sprintf(name, "%u-kdbus", getuid());
263 sprintf(name, "%u-kdbus-%u", getuid(), getpid());
264 memcpy((bus_make->items)->str, name, name_size);
266 item = KDBUS_PART_NEXT(item);
267 item->type = KDBUS_ITEM_BLOOM_SIZE;
268 item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
269 item->data64[0] = 64;
272 addr_value = strchr(address, ':') + 1;
275 if(!strcmp(addr_value, "sbb"))
276 bus_make->flags |= KDBUS_MAKE_SBB_OFFSET;
279 dbus_set_error_const(error, DBUS_ERROR_BAD_ADDRESS, "Invalid address parameter.");
285 _dbus_verbose("Opening /dev/kdbus/control\n");
286 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
289 _dbus_verbose("--- error %d (%m)\n", fdc);
290 dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
294 _dbus_verbose("Creating bus '%s'\n", (bus_make->items[0]).str);
295 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, bus_make);
298 _dbus_verbose("--- error %d (%m)\n", errno);
299 dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)",
300 (bus_make->items[0]).str, errno);
304 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", (bus_make->items[0]).str) < 0)
310 _dbus_verbose("Return value '%s'\n", bus);
315 * Minimal server init needed by context to go further.
317 DBusServer* empty_server_init(char* address)
319 return dbus_server_init_mini(address);
322 static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusConnection* connection)
324 struct kdbus_cmd_match* pCmd_match;
325 struct kdbus_item *pItem;
328 DBusTransport *transport;
330 transport = dbus_connection_get_transport(connection);
332 if(!_dbus_transport_get_socket_fd(transport, &fd))
338 size = sizeof(struct kdbus_cmd_match);
339 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /*3 name related items plus 2 id related items*/
341 pCmd_match = alloca(size);
342 if(pCmd_match == NULL)
349 pCmd_match->cookie = 1;
350 pCmd_match->size = size;
351 pCmd_match->src_id = 0;
353 pItem = pCmd_match->items;
354 pItem->type = KDBUS_MATCH_NAME_CHANGE;
355 pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
356 pItem = KDBUS_PART_NEXT(pItem);
357 pItem->type = KDBUS_MATCH_NAME_ADD;
358 pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
359 pItem = KDBUS_PART_NEXT(pItem);
360 pItem->type = KDBUS_MATCH_NAME_REMOVE;
361 pItem->size = KDBUS_ITEM_HEADER_SIZE + 1;
362 pItem = KDBUS_PART_NEXT(pItem);
363 pItem->type = KDBUS_MATCH_ID_ADD;
364 pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
365 pItem->id = KDBUS_MATCH_SRC_ID_ANY;
366 pItem = KDBUS_PART_NEXT(pItem);
367 pItem->type = KDBUS_MATCH_ID_REMOVE;
368 pItem->size = KDBUS_ITEM_HEADER_SIZE + sizeof(__u64);
369 pItem->id = KDBUS_MATCH_SRC_ID_ANY;
371 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
373 _dbus_verbose("Failed adding match rule for daemon, error: %d, %m\n", errno);
377 _dbus_verbose("Added match rule for daemon correctly.\n");
382 * Connects daemon to bus created by him and adds matches for "system" broadcasts.
383 * Do not requests org.freedesktop.DBus name, because it's to early
384 * (some structures of BusContext are not ready yet).
386 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
388 DBusConnection* connection;
390 dbus_bus_set_bus_connection_address(type, address);
392 connection = dbus_bus_get_private(type, error); /*todo possibly could be optimised by using lower functions*/
393 if(connection == NULL)
396 if(!add_matches_for_kdbus_broadcasts(connection))
398 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for daemon, %s", _dbus_strerror_from_errno ());
402 if(dbus_error_is_set(error))
405 _dbus_connection_close_possibly_shared (connection);
406 dbus_connection_unref (connection);
410 _dbus_verbose ("Daemon connected as kdbus client.\n");
416 * Asks bus for org.freedesktop.DBus well-known name.
418 dbus_bool_t register_daemon_name(DBusConnection* connection)
420 DBusString daemon_name;
421 dbus_bool_t retval = FALSE;
422 BusTransaction *transaction;
424 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
425 #ifdef POLICY_TO_KDBUS
426 if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
430 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
433 transaction = bus_transaction_new (bus_connection_get_context(connection));
434 if (transaction == NULL)
436 kdbus_release_name(connection, &daemon_name, 0);
440 if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
442 kdbus_release_name(connection, &daemon_name, 0);
450 bus_transaction_execute_and_free(transaction);
452 bus_transaction_cancel_and_free(transaction);
457 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
461 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
463 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
466 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
470 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
472 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
476 * Asks kdbus for well-known names registered on the bus
478 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
481 struct kdbus_cmd_name_list __attribute__ ((__aligned__(8))) cmd;
482 struct kdbus_name_list *name_list;
483 struct kdbus_cmd_name *name;
484 DBusTransport *transport = dbus_connection_get_transport(connection);
485 dbus_bool_t ret_val = FALSE;
491 if(!_dbus_transport_get_socket_fd(transport, &fd))
494 cmd.flags = KDBUS_NAME_LIST_NAMES | KDBUS_NAME_LIST_UNIQUE;
497 if(ioctl(fd, KDBUS_CMD_NAME_LIST, &cmd))
503 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
508 name_list = (struct kdbus_name_list *)((char*)dbus_transport_get_pool_pointer(transport) + cmd.offset);
510 for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
513 _dbus_verbose ("List len: %d\n", list_len);
515 list = malloc(sizeof(char*) * (list_len + 1));
519 for (name = name_list->names; (uint8_t *)(name) < (uint8_t *)(name_list) + name_list->size; name = KDBUS_PART_NEXT(name))
521 if(name->size > sizeof(struct kdbus_cmd_name) )
523 list[i] = strdup(name->name);
529 list[i] = malloc(snprintf(list[i], 0, ":1.%llu0", (unsigned long long)name->id));
532 sprintf(list[i], ":1.%llu", (unsigned long long int)name->id);
534 _dbus_verbose ("Name %d: %s\n", i, list[i]);
539 *array_len = list_len;
544 if (ioctl(fd, KDBUS_CMD_FREE, &cmd.offset) < 0)
548 _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
564 * Asks kdbus for list of connections being in the queue to own
565 * given well-known name. The list includes the owner of the name on the
568 dbus_bool_t kdbus_list_queued (DBusConnection *connection, DBusList **return_list,
569 const char *name, DBusError *error)
572 dbus_bool_t ret_val = FALSE;
574 struct kdbus_cmd_conn_info *pCmd;
576 DBusTransport *transport = dbus_connection_get_transport(connection);
577 struct kdbus_name_list *name_list;
578 struct kdbus_cmd_name *owner;
580 _dbus_assert (*return_list == NULL);
582 name_length = strlen(name) + 1;
583 cmd_size = sizeof(struct kdbus_cmd_conn_info) + name_length;
584 pCmd = alloca(cmd_size);
587 pCmd->size = cmd_size;
589 memcpy(pCmd->name, name, name_length);
591 _dbus_verbose ("Asking for queued owners of %s\n", pCmd->name);
593 _dbus_transport_get_socket_fd(transport, &fd);
596 if(ioctl(fd, KDBUS_CMD_NAME_LIST_QUEUED, pCmd))
600 else if(errno == ESRCH)
602 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
603 "Could not get owners of name '%s': no such name", name);
608 _dbus_verbose("kdbus error asking for queued owners list: err %d (%m)\n",errno);
613 name_list = (struct kdbus_name_list *)((char*)dbus_transport_get_pool_pointer(transport) + pCmd->offset);
615 for (owner = name_list->names; (uint8_t *)(owner) < (uint8_t *)(name_list) + name_list->size; owner = KDBUS_PART_NEXT(owner))
619 _dbus_verbose ("Got queued owner id: %llu\n", (unsigned long long)owner->id);
620 uname = malloc(snprintf(uname, 0, ":1.%llu0", (unsigned long long)owner->id));
623 sprintf(uname, ":1.%llu", (unsigned long long int)owner->id);
624 if (!_dbus_list_append (return_list, uname))
631 if (ioctl(fd, KDBUS_CMD_FREE, &pCmd->offset) < 0)
635 _dbus_verbose("kdbus error freeing pool: %d (%m)\n", errno);
642 dbus_set_error (error, _dbus_error_from_errno (errno),
643 "Failed to list queued owners of \"%s\": %s",
644 name, _dbus_strerror (errno));
646 link = _dbus_list_get_first_link (return_list);
649 DBusList *next = _dbus_list_get_next_link (return_list, link);
651 if(link->data != NULL)
654 _dbus_list_free_link (link);
663 * Register match rule in kdbus on behalf of sender of the message
665 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
669 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
670 if(dbus_error_is_set(error))
673 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
675 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%llu, %s",
676 sender_id, _dbus_strerror_from_errno ());
684 * Removes match rule in kdbus on behalf of sender of the message
686 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
690 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
691 if(dbus_error_is_set(error))
694 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
696 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%llu", sender_id);
703 int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
706 struct nameInfo info;
708 ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
709 if(ret == 0) //unique id of the name
711 sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
712 _dbus_verbose("Unique name discovered:%s\n", owner);
714 else if((ret != -ENOENT) && (ret != -ENXIO))
715 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", ret);
721 * Asks kdbus for selinux_security_context of the owner of the name given in the message
723 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
726 struct nameInfo info;
728 dbus_bool_t ret = FALSE;
730 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
731 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
732 if((inter_ret == -ENOENT) || (inter_ret == -ENXIO)) //name has no owner
733 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
734 else if(inter_ret < 0)
736 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
737 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
741 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
743 _DBUS_SET_OOM (error);
753 * Create connection structure for given name. It is needed to control starters - activatable services
754 * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
755 * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
757 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
759 DBusConnection *phantom_connection;
762 _dbus_string_init_const(&Sname, name);
764 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
765 if(phantom_connection == NULL)
767 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
769 dbus_connection_unref_phantom(phantom_connection);
770 phantom_connection = NULL;
771 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
774 dbus_bus_set_unique_name(phantom_connection, name);
775 if(!bus_connection_complete(phantom_connection, &Sname, error))
777 bus_connection_disconnected(phantom_connection);
778 phantom_connection = NULL;
782 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
785 return phantom_connection;
789 * Registers activatable services as kdbus starters.
791 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
795 dbus_bool_t retval = FALSE;
797 BusTransaction *transaction;
799 DBusTransport* transport;
801 transaction = bus_transaction_new (bus_connection_get_context(connection));
802 if (transaction == NULL)
805 if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
808 transport = dbus_connection_get_transport(connection);
810 if(!_dbus_transport_get_socket_fd (transport, &fd))
813 _dbus_string_init(&name);
817 #ifdef POLICY_TO_KDBUS
818 if(!register_kdbus_policy(services[i], transport, geteuid()))
822 if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME) , 0) < 0)
825 if(!_dbus_string_append(&name, services[i]))
827 if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
828 (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME), transaction, NULL))
830 if(!_dbus_string_set_length(&name, 0))
839 release_kdbus_name(fd, services[j], 0);
841 dbus_free_string_array (services);
842 _dbus_string_free(&name);
844 bus_transaction_execute_and_free(transaction);
846 bus_transaction_cancel_and_free(transaction);
852 * Updates kdbus starters (activatable services) after configuration was reloaded.
853 * It releases all previous starters and registers all new.
855 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
857 dbus_bool_t retval = FALSE;
858 DBusList **services_old;
860 BusService *service = NULL;
861 BusTransaction *transaction;
864 transaction = bus_transaction_new (bus_connection_get_context(connection));
865 if (transaction == NULL)
868 if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
871 services_old = bus_connection_get_services_owned(connection);
872 link = _dbus_list_get_first_link(services_old);
873 link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
879 service = (BusService*) link->data;
883 ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
885 if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
887 if(!bus_service_remove_owner(service, connection, transaction, NULL))
888 _dbus_verbose ("Unable to remove\n");
893 link = _dbus_list_get_next_link (services_old, link);
896 if(!register_kdbus_starters(connection))
898 _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
905 bus_transaction_execute_and_free(transaction);
907 bus_transaction_cancel_and_free(transaction);
913 * Analyzes system broadcasts about id and name changes.
914 * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
916 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
918 const char *name, *old, *new;
920 if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
922 _dbus_verbose ("Couldn't get args of NameOwnerChanged signal.\n");//, error.message);
926 _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
928 if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
930 if(!strcmp(name, old)) //yes it is - someone has disconnected
932 DBusConnection* conn;
934 conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
936 bus_connection_disconnected(conn);
939 else //it is well-known name
941 if((*old != 0) && (strcmp(old, bus_connection_get_name(connection))))
943 DBusMessage *message;
945 if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
948 _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
950 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
954 if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
955 DBUS_TYPE_STRING, &name,
958 dbus_message_unref (message);
962 bus_transaction_send_from_driver (transaction, connection, message);
963 dbus_message_unref (message);
966 if((*new != 0) && (strcmp(new, bus_connection_get_name(connection))))
968 DBusMessage *message;
970 _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
972 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
976 if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
977 DBUS_TYPE_STRING, &name,
980 dbus_message_unref (message);
984 bus_transaction_send_from_driver (transaction, connection, message);
985 dbus_message_unref (message);
989 if(bus_transaction_send(transaction, connection, msg))
990 _dbus_verbose ("NameOwnerChanged sent\n");
992 _dbus_verbose ("Sending NameOwnerChanged failed\n");