[daemon-dev][lib-opt] Fix in freeing memory, cleanup, comments, renames
[platform/upstream/dbus.git] / bus / kdbus-d.c
index d42ed19..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
  *
  */
 
@@ -51,8 +67,6 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error)
     int fdc, ret;
     char *bus;
 
-    /*TODO Distinguish session and system bus make*/
-
     _dbus_verbose("Opening /dev/kdbus/control\n");
     fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
     if (fdc < 0)
@@ -68,8 +82,11 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error)
 
     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;
@@ -101,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);
 
@@ -109,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))
@@ -150,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;
@@ -348,7 +390,7 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
     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++)
@@ -377,7 +419,60 @@ out:
     }
     dbus_free_string_array (services);
     _dbus_string_free(&name);
-    bus_transaction_free(transaction);
+    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;
 }