[daemon-dev][lib-opt] Fix in freeing memory, cleanup, comments, renames
[platform/upstream/dbus.git] / bus / kdbus-d.c
index 6dabbc9..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
+ *
+ * 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.
  *
- *  Created on: Sep 4, 2013
- *      Author: r.pajak
+ * 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.
  *
- *  kdbus add-on to dbus daemon
+ * 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 <dbus/dbus-transport-kdbus.h>
 #include "connection.h"
 #include "activation.h"
+#include "services.h"
 
 #include <utils.h>
 #include <stdlib.h>
@@ -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,32 +125,25 @@ 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, "member='IdRemoved'"))
+       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 ());
-          return FALSE;
+          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 ());
-          return FALSE;
+          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 ());
-          return FALSE;
+          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 ());
-          return FALSE;
+          goto failed;
     }
 
        if(dbus_error_is_set(error))
@@ -145,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;
@@ -329,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; i<len; i++)
     {
@@ -346,11 +400,79 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
 
         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;
 }