[daemon-dev][lib-fix] ListNames in libdbus fix, daemon development
[platform/upstream/dbus.git] / bus / bus.c
index ae2a61a..8f8674a 100644 (file)
--- a/bus/bus.c
+++ b/bus/bus.c
@@ -39,6 +39,8 @@
 #include <dbus/dbus-hash.h>
 #include <dbus/dbus-credentials.h>
 #include <dbus/dbus-internals.h>
+#include "kdbus-d.h"
+#include <stdlib.h>
 
 #ifdef DBUS_CYGWIN
 #include <signal.h>
@@ -68,6 +70,8 @@ struct BusContext
   unsigned int keep_umask : 1;
   unsigned int allow_anonymous : 1;
   unsigned int systemd_activation : 1;
+  unsigned int is_kdbus : 1;
+  DBusConnection *myConnection;
 };
 
 static dbus_int32_t server_data_slot = -1;
@@ -269,8 +273,7 @@ static dbus_bool_t
 process_config_first_time_only (BusContext       *context,
                                BusConfigParser  *parser,
                                 const DBusString *address,
-                                dbus_bool_t      systemd_activation,
-                                dbus_bool_t      write_pidfile,
+                                BusContextFlags   flags,
                                DBusError        *error)
 {
   DBusString log_prefix;
@@ -288,9 +291,12 @@ process_config_first_time_only (BusContext       *context,
   auth_mechanisms = NULL;
   pidfile = NULL;
 
-  _dbus_init_system_log ();
+  _dbus_init_system_log (TRUE);
 
-  context->systemd_activation = systemd_activation;
+  if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION)
+    context->systemd_activation = TRUE;
+  else
+    context->systemd_activation = FALSE;
 
   /* Check for an existing pid file. Of course this is a race;
    * we'd have to use fcntl() locks on the pid file to
@@ -298,7 +304,7 @@ process_config_first_time_only (BusContext       *context,
    * before overwriting any existing sockets, etc.
    */
 
-  if (write_pidfile)
+  if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE)
     pidfile = bus_config_parser_get_pidfile (parser);
 
   if (pidfile != NULL)
@@ -421,22 +427,61 @@ process_config_first_time_only (BusContext       *context,
 
   if (address)
     {
-      DBusServer *server;
-
-      server = dbus_server_listen (_dbus_string_get_const_data(address), error);
-      if (server == NULL)
-        {
-          _DBUS_ASSERT_ERROR_IS_SET (error);
-          goto failed;
-        }
-      else if (!setup_server (context, server, auth_mechanisms, error))
-        {
-          _DBUS_ASSERT_ERROR_IS_SET (error);
-          goto failed;
-        }
-
-      if (!_dbus_list_append (&context->servers, server))
-        goto oom;
+      if(!strcmp(_dbus_string_get_const_data(address), "kdbus"))
+      {
+         DBusBusType type;
+         DBusServer* server;
+         char* bus_address;
+
+         context->is_kdbus = TRUE;
+
+         if(!strcmp (context->type, "system"))
+                 type = DBUS_BUS_SYSTEM;
+         else if(!strcmp (context->type, "session"))
+                 type = DBUS_BUS_SESSION;
+         else
+                 type = DBUS_BUS_STARTER;
+
+         bus_address = make_kdbus_bus(type, error);
+         if(bus_address == NULL)
+                 goto failed;
+
+         server = empty_server_init(bus_address);
+         if(server == NULL)
+         {
+                 free(bus_address);
+                 goto failed;
+         }
+
+         if (!_dbus_list_append (&context->servers, server))
+         {
+                 free(bus_address);
+                 goto oom;
+         }
+
+         context->myConnection = daemon_as_client(type, bus_address, error);
+         if(context->myConnection == NULL)
+                 goto failed;
+      }
+      else
+      {
+                 DBusServer *server;
+
+                 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
+                 if (server == NULL)
+                       {
+                         _DBUS_ASSERT_ERROR_IS_SET (error);
+                         goto failed;
+                       }
+                 else if (!setup_server (context, server, auth_mechanisms, error))
+                       {
+                         _DBUS_ASSERT_ERROR_IS_SET (error);
+                         goto failed;
+                       }
+
+                 if (!_dbus_list_append (&context->servers, server))
+                       goto oom;
+      }
     }
   else
     {
@@ -698,17 +743,18 @@ process_config_postinit (BusContext      *context,
 
 BusContext*
 bus_context_new (const DBusString *config_file,
-                 ForceForkSetting  force_fork,
+                 BusContextFlags   flags,
                  DBusPipe         *print_addr_pipe,
                  DBusPipe         *print_pid_pipe,
                  const DBusString *address,
-                 dbus_bool_t      systemd_activation,
-                 dbus_bool_t      write_pidfile,
                  DBusError        *error)
 {
   BusContext *context;
   BusConfigParser *parser;
 
+  _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
+                (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   context = NULL;
@@ -757,7 +803,7 @@ bus_context_new (const DBusString *config_file,
       goto failed;
     }
 
-  if (!process_config_first_time_only (context, parser, address, systemd_activation, write_pidfile, error))
+  if (!process_config_first_time_only (context, parser, address, flags, error))
     {
       _DBUS_ASSERT_ERROR_IS_SET (error);
       goto failed;
@@ -852,7 +898,8 @@ bus_context_new (const DBusString *config_file,
     if (context->pidfile)
       _dbus_string_init_const (&u, context->pidfile);
 
-    if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
+    if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
+        (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
       {
         _dbus_verbose ("Forking and becoming daemon\n");
 
@@ -924,6 +971,18 @@ bus_context_new (const DBusString *config_file,
 
   dbus_server_free_data_slot (&server_data_slot);
 
+  if(context->myConnection)
+  {
+         DBusString unique_name;
+
+         bus_connections_setup_connection(context->connections, context->myConnection);
+         _dbus_string_init_const(&unique_name, ":1.1");
+         if(!bus_connection_complete(context->myConnection, &unique_name, error))
+         {
+                 _dbus_verbose ("bus connection complete failed\n");
+         }
+  }
+
   return context;
 
  failed:
@@ -1171,6 +1230,11 @@ bus_context_get_loop (BusContext *context)
   return context->loop;
 }
 
+DBusConnection* bus_context_get_myConnection(BusContext *context)
+{
+  return context->myConnection;
+}
+
 dbus_bool_t
 bus_context_allow_unix_user (BusContext   *context,
                              unsigned long uid)