X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=bus%2Fkdbus-d.c;h=a8bcf3d934271ceb8dbcd7e0e8b1342458fb790d;hb=7d9239c9c78cb6d0b9c282376fcf3cda1de23209;hp=fea4db4c521e21cb037bdb75a6ac744f6c7164f3;hpb=bd6d8d261df61ed8b396434e5a80285d43184d6c;p=platform%2Fupstream%2Fdbus.git diff --git a/bus/kdbus-d.c b/bus/kdbus-d.c index fea4db4..a8bcf3d 100644 --- a/bus/kdbus-d.c +++ b/bus/kdbus-d.c @@ -1,10 +1,26 @@ -/* - * kdbus-d.c +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* kdbus-d.c kdbus related daemon functions + * + * Copyright (C) 2013 Samsung Electronics + * + * Licensed under the Academic Free License version 2.1 * - * Created on: Sep 4, 2013 - * Author: r.pajak + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version and under the terms of the GNU + * Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. * - * kdbus add-on to dbus daemon + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ @@ -18,6 +34,7 @@ #include #include "connection.h" #include "activation.h" +#include "services.h" #include #include @@ -50,14 +67,12 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error) int fdc, ret; char *bus; - /*TODO Distinguish session and system bus make*/ - /*TODO Add dbus_set_error(error, DBUS_ERROR_FAILED, "...") (?)*/ - _dbus_verbose("Opening /dev/kdbus/control\n"); fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC); if (fdc < 0) { _dbus_verbose("--- error %d (%m)\n", fdc); + dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc); return NULL; } @@ -65,7 +80,13 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error) bus_make.head.bloom_size = 64; bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD; - snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid()); + if(type == DBUS_BUS_SYSTEM) + snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system"); + else if(type == DBUS_BUS_SESSION) + snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid()); + else + snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid()); + bus_make.n_type = KDBUS_MAKE_NAME; bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1; bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size; @@ -75,6 +96,7 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error) if (ret) { _dbus_verbose("--- error %d (%m)\n", ret); + dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc); return NULL; } @@ -96,7 +118,6 @@ DBusServer* empty_server_init(char* address) DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error) { DBusConnection* connection; - DBusString daemon_name; dbus_bus_set_bus_connection_address(type, address); @@ -104,18 +125,26 @@ DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *err if(connection == NULL) return NULL; - _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS); - if(!kdbus_register_policy (&daemon_name, connection)) - goto failed; - - if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) - goto failed; - - if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'")) //todo handle this in dispatch - { - dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ()); - return FALSE; - } + if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'")) + { + dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ()); + goto failed; + } + if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'")) + { + dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ()); + goto failed; + } + if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameLost'")) + { + dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ()); + goto failed; + } + if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameAcquired'")) + { + dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ()); + goto failed; + } if(dbus_error_is_set(error)) { @@ -130,6 +159,39 @@ failed: return connection; } +dbus_bool_t register_daemon_name(DBusConnection* connection) +{ + DBusString daemon_name; + dbus_bool_t retval = FALSE; + BusTransaction *transaction; + + _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS); + if(!kdbus_register_policy (&daemon_name, connection)) + return FALSE; + + if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) + return FALSE; + + transaction = bus_transaction_new (bus_connection_get_context(connection)); + if (transaction == NULL) + { + kdbus_release_name(connection, &daemon_name, 0); + goto out; + } + + if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL)) + { + kdbus_release_name(connection, &daemon_name, 0); + goto out; + } + + retval = TRUE; + +out: + bus_transaction_free(transaction); + return retval; +} + dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection) { int fd; @@ -301,7 +363,7 @@ DBusConnection* create_phantom_connection(DBusConnection* connection, const char } if(!bus_connection_complete(phantom_connection, &name, error)) { - dbus_connection_unref_phantom(phantom_connection); + bus_connection_disconnected(phantom_connection); phantom_connection = NULL; goto out; } @@ -314,15 +376,22 @@ out: dbus_bool_t register_kdbus_starters(DBusConnection* connection) { - int i, len; + int i,j, len; char **services; dbus_bool_t retval = FALSE; int fd; + BusTransaction *transaction; + DBusString name; + + transaction = bus_transaction_new (bus_connection_get_context(connection)); + if (transaction == NULL) + return FALSE; if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len)) return FALSE; - _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd); + _dbus_transport_get_socket_fd (dbus_connection_get_transport(connection), &fd); + _dbus_string_init(&name); for(i=0; idata; + if(service == NULL) + goto out; + + ret = release_kdbus_name(fd, bus_service_get_name(service), 0); + + if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED) + { + if(!bus_service_remove_owner(service, connection, transaction, NULL)) + _dbus_verbose ("Unable to remove\n"); + } + else if(ret < 0) + goto out; + + link = _dbus_list_get_next_link (services_old, link); + } + + if(!register_kdbus_starters(connection)) + { + _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n"); + goto out; + } + retval = TRUE; + +out: + bus_transaction_free(transaction); + return retval; +} + +/* +static dbus_bool_t remove_conn_if_name_match (DBusConnection *connection, void *data) +{ + if(!strcmp(bus_connection_get_name(connection), (char*)data)) + { + bus_connection_disconnected(connection); + return FALSE; //this is to break foreach function + } + return TRUE; +}*/ + +void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection) +{ + const char *name, *old, *new; + + if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) + { + _dbus_verbose ("Couldn't get args of NameOwnerChanged signal: .\n");//, error.message); + return; + } + + _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new); + + if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/ + { + if(!strcmp(name, old)) //yes it is - someone has disconnected + { + DBusConnection* conn; + + conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name); + if(conn) + bus_connection_disconnected(conn); + } + } + else //it is well-known name + { + if((*old != 0) && (strcmp(old, ":1.1"))) + { + DBusMessage *message; + + if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL) + goto next; + + _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name); + + message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost"); + if (message == NULL) + goto next; + + if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_INVALID)) + { + dbus_message_unref (message); + goto next; + } + + bus_transaction_send_from_driver (transaction, connection, message); + dbus_message_unref (message); + } + next: + if((*new != 0) && (strcmp(new, ":1.1"))) + { + DBusMessage *message; + + _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name); + + message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired"); + if (message == NULL) + return; + + if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_INVALID)) + { + dbus_message_unref (message); + return; + } + + bus_transaction_send_from_driver (transaction, connection, message); + dbus_message_unref (message); + } + } +}