[daemon-dev][lib-opt] Fix in freeing memory, cleanup, comments, renames
[platform/upstream/dbus.git] / bus / kdbus-d.c
index b0466ee..1f56bc3 100644 (file)
@@ -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
  *
  */
 
@@ -17,6 +33,8 @@
 #include <dbus/dbus-transport.h>
 #include <dbus/dbus-transport-kdbus.h>
 #include "connection.h"
+#include "activation.h"
+#include "services.h"
 
 #include <utils.h>
 #include <stdlib.h>
@@ -25,9 +43,6 @@
 #include <unistd.h>
 #include <errno.h>
 
-//todo there should be no include below - needed functions should be moved to kdbus-common
-#include <dbus/dbus-transport-kdbus.h>
-
 __u64 sender_name_to_id(const char* name, DBusError* error)
 {
        __u64 sender_id = 0;
@@ -52,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;
     }
 
@@ -67,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;
@@ -77,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;
     }
 
@@ -98,28 +118,33 @@ 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);
 
-       connection = dbus_bus_get(type, error);
+       connection = dbus_bus_get_private(type, error);  /*todo possibly could be optimised by using lower functions*/
        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;
-
-//     dbus_bus_add_match(connection, "type='signal', member='NameAcquired'", error);  //not needed if request name ioctled  by daemon not libdbus
-//     dbus_bus_add_match(connection, "type='signal', member='NameLost'", error);  //todo dispatch and digest this  or ioctl about name where daemon checks name presence
-       if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'"))
-       {
-             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))
        {
@@ -134,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_cancel_and_free(transaction);
+    return retval;
+}
+
 dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
 {
        int fd;
@@ -286,11 +344,10 @@ dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* c
        return ret;
 }
 
-DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name)
+DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
 {
     DBusConnection *phantom_connection;
     DBusString name;
-    DBusError error;
 
     _dbus_string_init_const(&name, unique_name);
 
@@ -299,19 +356,207 @@ DBusConnection* create_phantom_connection(DBusConnection* connection, const char
         return FALSE;
     if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
     {
-        /*todo FIXME something should be done to clean up the phantom connection
-         * but we can't use standard disconnect, unref or last_unref because the transport is taken from connection
-         * so we probably should write own function on the basis of _dbus_connection_last_unref
-         */
+        dbus_connection_unref_phantom(phantom_connection);
         phantom_connection = NULL;
+        dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
+        goto out;
     }
-    if(!bus_connection_complete(phantom_connection, &name, &error))
+    if(!bus_connection_complete(phantom_connection, &name, error))
     {
-        /* todo FIXME exactly the same issue as above */
+        bus_connection_disconnected(phantom_connection);
         phantom_connection = NULL;
+        goto out;
     }
 
     _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
 
+out:
     return phantom_connection;
 }
+
+dbus_bool_t register_kdbus_starters(DBusConnection* connection)
+{
+    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_string_init(&name);
+
+    for(i=0; i<len; i++)
+    {
+        if(!register_kdbus_policy(services[i], fd))
+            goto out;
+
+        if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
+            goto out;
+
+        if(!_dbus_string_append(&name, services[i]))
+               goto out;
+        if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
+                       (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
+               goto out;
+        if(!_dbus_string_set_length(&name, 0))
+               goto out;
+    }
+    retval = TRUE;
+
+out:
+    if(retval == FALSE)
+    {
+        for(j=0; j<i; j++)
+            release_kdbus_name(fd, services[j], 0);
+    }
+    dbus_free_string_array (services);
+    _dbus_string_free(&name);
+    bus_transaction_cancel_and_free(transaction);
+    return retval;
+}
+
+dbus_bool_t update_kdbus_starters(DBusConnection* connection)
+{
+    dbus_bool_t retval = FALSE;
+    DBusList **services_old;
+    DBusList *link;
+    BusService *service = NULL;
+    BusTransaction *transaction;
+    int fd;
+
+    transaction = bus_transaction_new (bus_connection_get_context(connection));
+    if (transaction == NULL)
+        return FALSE;
+
+    if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
+        goto out;
+
+    services_old = bus_connection_get_services_owned(connection);
+    link = _dbus_list_get_first_link(services_old);
+    link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
+
+    while (link != NULL)
+    {
+        int ret;
+
+        service = (BusService*) link->data;
+        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_cancel_and_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);
+        }
+    }
+}