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"
47 * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
49 __u64 sender_name_to_id(const char* name, DBusError* error)
53 if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
54 sender_id = strtoull(&name[3], NULL, 10);
56 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
62 * Seeks key in rule string, and duplicates value of the key into pValue.
63 * Because of the duplication, pValue must be freed after use.
65 * @param rule rule to look through
66 * @param key key to look for
67 * @param pValue pointer to value of the key found
68 * @return length of the value string, 0 means not found
70 static int parse_match_key(const char *rule, const char* key, char** pValue)
73 const char* pValueEnd;
76 pBegin = strstr(rule, key);
79 pBegin += strlen(key);
80 pValueEnd = strchr(pBegin, '\'');
83 value_length = pValueEnd - pBegin;
84 *pValue = strndup(pBegin, value_length);
86 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
93 * Adds a match rule to match broadcast messages going through the message bus.
94 * Do no affect messages addressed directly.
96 * The "rule" argument is the string form of a match rule.
98 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
100 * interface match key mapped to bloom
101 * sender match key mapped to src_name
103 * @param transport transport
104 * @param id id of connection for which the rule is to be added
105 * @param rule textual form of match rule
107 dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
109 struct kdbus_cmd_match* pCmd_match;
110 struct kdbus_item *pItem;
111 __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
115 char* pInterface = NULL;
116 dbus_bool_t ret_value = FALSE;
120 if(!_dbus_transport_get_socket_fd(transport, &fd))
123 bloom_size = dbus_transport_get_bloom_size(transport);
125 /*parsing rule and calculating size of command*/
126 size = sizeof(struct kdbus_cmd_match);
127 if(parse_match_key(rule, "interface='", &pInterface)) /*actual size is not important for interface because bloom size is defined by bus*/
128 size += KDBUS_PART_HEADER_SIZE + bloom_size;
129 name_size = parse_match_key(rule, "sender='", &pName);
132 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
134 src_id = strtoull(&pName[3], NULL, 10);
139 size += KDBUS_ITEM_SIZE(name_size + 1); //well known name
142 pCmd_match = alloca(size);
143 if(pCmd_match == NULL)
147 pCmd_match->cookie = id;
148 pCmd_match->size = size;
149 pCmd_match->src_id = src_id;
151 pItem = pCmd_match->items;
154 pItem->type = KDBUS_MATCH_SRC_NAME;
155 pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
156 memcpy(pItem->str, pName, strlen(pName) + 1);
157 pItem = KDBUS_PART_NEXT(pItem);
161 pItem->type = KDBUS_MATCH_BLOOM;
162 pItem->size = KDBUS_PART_HEADER_SIZE + bloom_size;
163 strncpy(pItem->data, pInterface, bloom_size);
166 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
167 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
170 _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
183 * Opposing to dbus, in kdbus removes all match rules with given
184 * cookie, which in this implementation is equal to uniqe id.
186 * @param transport transport
187 * @param id connection id for which rules are to be removed
189 dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
191 struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
194 if(!_dbus_transport_get_socket_fd(transport, &fd))
199 cmd.size = sizeof(struct kdbus_cmd_match);
201 if(ioctl(fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
203 _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
208 _dbus_verbose("Match rule removed correctly.\n");
214 * Performs kdbus query of id of the given name
216 * @param name name to query for
217 * @param transport transport
218 * @param pInfo nameInfo structure address to store info about the name
219 * @return 0 on success, -errno if failed
221 int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
223 struct kdbus_cmd_name_info *msg;
224 struct kdbus_item *item;
230 pInfo->sec_label_len = 0;
231 pInfo->sec_label = NULL;
233 if(!_dbus_transport_get_socket_fd(transport, &fd))
236 item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
237 item_size = (item_size < 56) ? 56 : item_size; //at least 56 bytes are needed by kernel to place info about name, otherwise error
238 size = sizeof(struct kdbus_cmd_name_info) + item_size;
243 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
247 memset(msg, 0, size);
249 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.')) /* if name starts with ":1." it is a unique name and should be send as number */
250 msg->id = strtoull(&name[3], NULL, 10);
255 item->type = KDBUS_NAME_INFO_ITEM_NAME;
256 item->size = item_size;
257 memcpy(item->str, name, strlen(name) + 1);
260 ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
267 else if(ret == -ENOBUFS)
269 msg = realloc(msg, msg->size); //prepare memory
278 pInfo->uniqueId = msg->id;
279 pInfo->userId = msg->creds.uid;
280 pInfo->processId = msg->creds.pid;
282 while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
284 if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
286 pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
287 if(pInfo->sec_label_len != 0)
289 pInfo->sec_label = malloc(pInfo->sec_label_len);
290 if(pInfo->sec_label == NULL)
293 memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
297 item = KDBUS_PART_NEXT(item);
306 * Creates kdbus bus of given type.
308 char* make_kdbus_bus(DBusBusType type, DBusError *error)
311 struct kdbus_cmd_bus_make head;
315 } __attribute__ ((__aligned__(8))) bus_make;
320 _dbus_verbose("Opening /dev/kdbus/control\n");
321 fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
324 _dbus_verbose("--- error %d (%m)\n", fdc);
325 dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
329 memset(&bus_make, 0, sizeof(bus_make));
330 bus_make.head.bloom_size = 64;
331 bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;
333 if(type == DBUS_BUS_SYSTEM)
334 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
335 else if(type == DBUS_BUS_SESSION)
336 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
338 snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());
340 bus_make.n_type = KDBUS_MAKE_NAME;
341 bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
342 bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
344 _dbus_verbose("Creating bus '%s'\n", bus_make.name);
345 ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
348 _dbus_verbose("--- error %d (%m)\n", ret);
349 dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc);
353 if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
359 _dbus_verbose("Return value '%s'\n", bus);
364 * Minimal server init needed by context to go further.
366 DBusServer* empty_server_init(char* address)
368 return dbus_server_init_mini(address);
371 static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusTransport* transport)
373 struct kdbus_cmd_match* pCmd_match;
374 struct kdbus_item *pItem;
378 if(!_dbus_transport_get_socket_fd(transport, &fd))
385 size = sizeof(struct kdbus_cmd_match);
386 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2; /*3 name related items plus 2 id related items*/
388 pCmd_match = alloca(size);
389 if(pCmd_match == NULL)
396 pCmd_match->cookie = 1;
397 pCmd_match->size = size;
399 pItem = pCmd_match->items;
400 pCmd_match->src_id = 0;
401 pItem->type = KDBUS_MATCH_NAME_CHANGE;
402 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
403 pItem = KDBUS_PART_NEXT(pItem);
404 pItem->type = KDBUS_MATCH_NAME_ADD;
405 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
406 pItem = KDBUS_PART_NEXT(pItem);
407 pItem->type = KDBUS_MATCH_NAME_REMOVE;
408 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
409 pItem = KDBUS_PART_NEXT(pItem);
410 pItem->type = KDBUS_MATCH_ID_ADD;
411 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
412 pItem = KDBUS_PART_NEXT(pItem);
413 pItem->type = KDBUS_MATCH_ID_REMOVE;
414 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
416 if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
418 _dbus_verbose("Failed adding match rule for daemon, error: %d, %m\n", errno);
422 _dbus_verbose("Added match rule for daemon correctly.\n");
427 * Connects daemon to bus created by him and adds matches for "system" broadcasts.
428 * Do not requests org.freedesktop.DBus name, because it's to early
429 * (some structures of BusContext are not ready yet).
431 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
433 DBusConnection* connection;
435 dbus_bus_set_bus_connection_address(type, address);
437 connection = dbus_bus_get_private(type, error); /*todo possibly could be optimised by using lower functions*/
438 if(connection == NULL)
441 if(!add_matches_for_kdbus_broadcasts(dbus_connection_get_transport(connection)))
443 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for daemon, %s", _dbus_strerror_from_errno ());
447 if(dbus_error_is_set(error))
450 _dbus_connection_close_possibly_shared (connection);
451 dbus_connection_unref (connection);
455 _dbus_verbose ("Daemon connected as kdbus client.\n");
461 * Asks bus for org.freedesktop.DBus well-known name.
463 dbus_bool_t register_daemon_name(DBusConnection* connection)
465 DBusString daemon_name;
466 dbus_bool_t retval = FALSE;
467 BusTransaction *transaction;
469 _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
470 if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
473 if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
476 transaction = bus_transaction_new (bus_connection_get_context(connection));
477 if (transaction == NULL)
479 kdbus_release_name(connection, &daemon_name, 0);
483 if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
485 kdbus_release_name(connection, &daemon_name, 0);
492 bus_transaction_cancel_and_free(transaction);
496 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
500 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
502 return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
505 dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
509 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
511 return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
515 * Asks kdbus for well-known names registered on the bus
517 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
520 struct kdbus_cmd_names* pCmd;
522 dbus_bool_t ret_val = FALSE;
528 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
529 pCmd = malloc(cmd_size);
532 pCmd->size = cmd_size;
534 _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
538 if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
542 if(errno == ENOBUFS) //buffer to small to put all names into it
543 cmd_size = pCmd->size; //here kernel tells how much memory it needs
546 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
550 if(cmd_size) //kernel needs more memory
552 pCmd = realloc(pCmd, cmd_size); //prepare memory
555 goto again; //and try again
559 struct kdbus_cmd_name* pCmd_name;
561 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
564 list = malloc(sizeof(char*) * (list_len + 1));
568 for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
570 list[i] = strdup(pCmd_name->name);
578 _dbus_verbose ("Name %d: %s\n", i, list[i]);
584 *array_len = list_len;
595 * Register match rule in kdbus on behalf of sender of the message
597 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
601 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
602 if(dbus_error_is_set(error))
605 if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
607 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
608 sender_id, _dbus_strerror_from_errno ());
616 * Removes match rule in kdbus on behalf of sender of the message
618 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
622 sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
623 if(dbus_error_is_set(error))
626 if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
628 dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
635 int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
638 struct nameInfo info;
640 ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
641 if(ret == 0) //unique id of the name
643 sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
644 _dbus_verbose("Unique name discovered:%s\n", owner);
646 else if(ret != -ENOENT)
647 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
653 * Asks kdbus for uid of the owner of the name given in the message
655 dbus_bool_t kdbus_get_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
657 struct nameInfo info;
659 dbus_bool_t ret = FALSE;
661 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
662 if(inter_ret == 0) //name found
664 _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
668 else if(inter_ret == -ENOENT) //name has no owner
670 _dbus_verbose ("Name %s has no owner.\n", name);
671 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
676 _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
677 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
684 * Asks kdbus for pid of the owner of the name given in the message
686 dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, unsigned long* pid, DBusError* error)
688 struct nameInfo info;
690 dbus_bool_t ret = FALSE;
692 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
693 if(inter_ret == 0) //name found
695 _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
696 *pid = info.processId;
699 else if(inter_ret == -ENOENT) //name has no owner
700 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
703 _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
704 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
711 * Asks kdbus for selinux_security_context of the owner of the name given in the message
713 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
716 struct nameInfo info;
718 dbus_bool_t ret = FALSE;
720 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
721 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
722 if(inter_ret == -ENOENT) //name has no owner
723 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
724 else if(inter_ret < 0)
726 _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
727 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
731 if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
733 _DBUS_SET_OOM (error);
743 * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
744 * the uid is filled in. Always returns #FALSE on non-UNIX platforms
745 * for now., though in theory someone could hook Windows to NIS or
746 * something. Always returns #FALSE prior to authenticating the
749 * The UID of is only read by bus daemon from kdbus. You can not
750 * call this function from client side of the connection.
752 * You can ask the bus to tell you the UID of another connection though
753 * if you like; this is done with dbus_bus_get_unix_user().
755 * @param connection the connection
756 * @param uid return location for the user ID
757 * @returns #TRUE if uid is filled in with a valid user ID
760 dbus_connection_get_unix_user (DBusConnection *connection,
763 _dbus_return_val_if_fail (connection != NULL, FALSE);
764 _dbus_return_val_if_fail (uid != NULL, FALSE);
766 return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
770 * Gets the process ID of the connection if any.
771 * Returns #TRUE if the pid is filled in.
773 * @param connection the connection
774 * @param pid return location for the process ID
775 * @returns #TRUE if uid is filled in with a valid process ID
778 dbus_connection_get_unix_process_id (DBusConnection *connection,
781 _dbus_return_val_if_fail (connection != NULL, FALSE);
782 _dbus_return_val_if_fail (pid != NULL, FALSE);
784 return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
788 * Create connection structure for given name. It is needed to control starters - activatable services
789 * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
790 * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
792 DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
794 DBusConnection *phantom_connection;
797 _dbus_string_init_const(&Sname, name);
799 phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
800 if(phantom_connection == NULL)
802 if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
804 dbus_connection_unref_phantom(phantom_connection);
805 phantom_connection = NULL;
806 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
809 if(!bus_connection_complete(phantom_connection, &Sname, error))
811 bus_connection_disconnected(phantom_connection);
812 phantom_connection = NULL;
816 _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
819 return phantom_connection;
823 * Registers activatable services as kdbus starters.
825 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
829 dbus_bool_t retval = FALSE;
831 BusTransaction *transaction;
833 DBusTransport* transport;
834 unsigned long int euid;
836 transaction = bus_transaction_new (bus_connection_get_context(connection));
837 if (transaction == NULL)
840 if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
843 transport = dbus_connection_get_transport(connection);
846 if(!_dbus_transport_get_socket_fd (transport, &fd))
849 _dbus_string_init(&name);
853 if(!register_kdbus_policy(services[i], transport, euid))
856 if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
859 if(!_dbus_string_append(&name, services[i]))
861 if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
862 (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
864 if(!_dbus_string_set_length(&name, 0))
873 release_kdbus_name(fd, services[j], 0);
875 dbus_free_string_array (services);
876 _dbus_string_free(&name);
877 bus_transaction_cancel_and_free(transaction);
882 * Updates kdbus starters (activatable services) after configuration was reloaded.
883 * It releases all previous starters and registers all new.
885 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
887 dbus_bool_t retval = FALSE;
888 DBusList **services_old;
890 BusService *service = NULL;
891 BusTransaction *transaction;
894 transaction = bus_transaction_new (bus_connection_get_context(connection));
895 if (transaction == NULL)
898 if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
901 services_old = bus_connection_get_services_owned(connection);
902 link = _dbus_list_get_first_link(services_old);
903 link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
909 service = (BusService*) link->data;
913 ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
915 if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
917 if(!bus_service_remove_owner(service, connection, transaction, NULL))
918 _dbus_verbose ("Unable to remove\n");
923 link = _dbus_list_get_next_link (services_old, link);
926 if(!register_kdbus_starters(connection))
928 _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
934 bus_transaction_cancel_and_free(transaction);
939 * Analyzes system broadcasts about id and name changes.
940 * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
942 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
944 const char *name, *old, *new;
946 if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
948 _dbus_verbose ("Couldn't get args of NameOwnerChanged signal.\n");//, error.message);
952 _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
954 if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
956 if(!strcmp(name, old)) //yes it is - someone has disconnected
958 DBusConnection* conn;
960 conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
962 bus_connection_disconnected(conn);
965 else //it is well-known name
967 if((*old != 0) && (strcmp(old, ":1.1")))
969 DBusMessage *message;
971 if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
974 _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
976 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
980 if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
981 DBUS_TYPE_STRING, &name,
984 dbus_message_unref (message);
988 bus_transaction_send_from_driver (transaction, connection, message);
989 dbus_message_unref (message);
992 if((*new != 0) && (strcmp(new, ":1.1")))
994 DBusMessage *message;
996 _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
998 message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
1002 if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
1003 DBUS_TYPE_STRING, &name,
1006 dbus_message_unref (message);
1010 bus_transaction_send_from_driver (transaction, connection, message);
1011 dbus_message_unref (message);
1015 if(bus_transaction_send(transaction, connection, msg))
1016 _dbus_verbose ("NameOwnerChanged sent\n");
1018 _dbus_verbose ("Sending NameOwnerChanged failed\n");