Merge "[lib-fix] Fixed return value of kdbus_write_msg Change-Id: Ia3a45a6a79b4078929...
[platform/upstream/dbus.git] / bus / main.c
index 594e3fb..960a9c7 100644 (file)
 
 static BusContext *context;
 
+#ifdef DBUS_UNIX
+
 static int reload_pipe[2];
 #define RELOAD_READ_END 0
 #define RELOAD_WRITE_END 1
 
 static void close_reload_pipe (DBusWatch **);
 
-#ifdef DBUS_UNIX
+typedef enum
+ {
+   ACTION_RELOAD = 'r',
+   ACTION_QUIT = 'q'
+ } SignalAction;
+
 static void
 signal_handler (int sig)
 {
-
   switch (sig)
     {
-#ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
-    case SIGIO:
-      /* explicit fall-through */
-#endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX  */
 #ifdef SIGHUP
     case SIGHUP:
       {
         DBusString str;
-        _dbus_string_init_const (&str, "foo");
+        char action[2] = { ACTION_RELOAD, '\0' };
+
+        _dbus_string_init_const (&str, action);
         if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
             !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
           {
@@ -83,11 +87,39 @@ signal_handler (int sig)
             static const char message[] =
               "Unable to write to reload pipe - buffer full?\n";
 
-            write (STDERR_FILENO, message, strlen (message));
+            if (write (STDERR_FILENO, message, strlen (message)) != strlen (message))
+              {
+                /* ignore failure to write out a warning */
+              }
           }
       }
       break;
 #endif
+
+    case SIGTERM:
+      {
+        DBusString str;
+        char action[2] = { ACTION_QUIT, '\0' };
+        _dbus_string_init_const (&str, action);
+        if ((reload_pipe[RELOAD_WRITE_END] < 0) ||
+            !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
+          {
+            /* If we can't write to the socket, dying seems a more
+             * important response to SIGTERM than cleaning up sockets,
+             * so we exit. We'd use exit(), but that's not async-signal-safe,
+             * so we'll have to resort to _exit(). */
+            static const char message[] =
+              "Unable to write termination signal to pipe - buffer full?\n"
+              "Will exit instead.\n";
+
+            if (write (STDERR_FILENO, message, strlen (message)) != strlen (message))
+              {
+                /* ignore failure to write out a warning */
+              }
+            _exit (1);
+          }
+      }
+      break;
     }
 }
 #endif /* DBUS_UNIX */
@@ -95,7 +127,23 @@ signal_handler (int sig)
 static void
 usage (void)
 {
-  fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect] [--address=ADDRESS] [--systemd-activation]\n");
+  fprintf (stderr,
+      DBUS_DAEMON_NAME
+      " [--version]"
+      " [--session]"
+      " [--system]"
+      " [--config-file=FILE]"
+      " [--print-address[=DESCRIPTOR]]"
+      " [--print-pid[=DESCRIPTOR]]"
+      " [--introspect]"
+      " [--address=ADDRESS]"
+      " [--nopidfile]"
+      " [--nofork]"
+#ifdef DBUS_UNIX
+      " [--fork]"
+      " [--systemd-activation]"
+#endif
+      "\n");
   exit (1);
 }
 
@@ -183,6 +231,7 @@ check_two_pid_descriptors (const DBusString *pid_fd,
     }
 }
 
+#ifdef DBUS_UNIX
 static dbus_bool_t
 handle_reload_watch (DBusWatch    *watch,
                     unsigned int  flags,
@@ -190,6 +239,8 @@ handle_reload_watch (DBusWatch    *watch,
 {
   DBusError error;
   DBusString str;
+  char *action_str;
+  char action = '\0';
 
   while (!_dbus_string_init (&str))
     _dbus_wait_for_memory ();
@@ -201,6 +252,12 @@ handle_reload_watch (DBusWatch    *watch,
       close_reload_pipe (&watch);
       return TRUE;
     }
+
+  action_str = _dbus_string_get_data (&str);
+  if (action_str != NULL)
+    {
+      action = action_str[0];
+    }
   _dbus_string_free (&str);
 
   /* this can only fail if we don't understand the config file
@@ -208,15 +265,42 @@ handle_reload_watch (DBusWatch    *watch,
    * loaded config.
    */
   dbus_error_init (&error);
-  if (! bus_context_reload_config (context, &error))
+
+  switch (action)
     {
-      _DBUS_ASSERT_ERROR_IS_SET (&error);
-      _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
-                   dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
-      _dbus_warn ("Unable to reload configuration: %s\n",
-                 error.message);
-      dbus_error_free (&error);
+    case ACTION_RELOAD:
+      if (! bus_context_reload_config (context, &error))
+        {
+          _DBUS_ASSERT_ERROR_IS_SET (&error);
+          _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
+                        dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
+          _dbus_warn ("Unable to reload configuration: %s\n",
+                      error.message);
+          dbus_error_free (&error);
+        }
+      break;
+
+    case ACTION_QUIT:
+      {
+        DBusLoop *loop;
+        /*
+         * On OSs without abstract sockets, we want to quit
+         * gracefully rather than being killed by SIGTERM,
+         * so that DBusServer gets a chance to clean up the
+         * sockets from the filesystem. fd.o #38656
+         */
+        loop = bus_context_get_loop (context);
+        if (loop != NULL)
+          {
+            _dbus_loop_quit (loop);
+          }
+      }
+      break;
+
+    default:
+      break;
     }
+
   return TRUE;
 }
 
@@ -273,6 +357,7 @@ close_reload_pipe (DBusWatch **watch)
     _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
     reload_pipe[RELOAD_WRITE_END] = -1;
 }
+#endif /* DBUS_UNIX */
 
 int
 main (int argc, char **argv)
@@ -288,8 +373,7 @@ main (int argc, char **argv)
   int i;
   dbus_bool_t print_address;
   dbus_bool_t print_pid;
-  int force_fork;
-  dbus_bool_t systemd_activation;
+  BusContextFlags flags;
 
   if (!_dbus_string_init (&config_file))
     return 1;
@@ -305,8 +389,8 @@ main (int argc, char **argv)
 
   print_address = FALSE;
   print_pid = FALSE;
-  force_fork = FORK_FOLLOW_CONFIG_FILE;
-  systemd_activation = FALSE;
+
+  flags = BUS_CONTEXT_FLAG_WRITE_PID_FILE;
 
   prev_arg = NULL;
   i = 1;
@@ -317,17 +401,37 @@ main (int argc, char **argv)
       if (strcmp (arg, "--help") == 0 ||
           strcmp (arg, "-h") == 0 ||
           strcmp (arg, "-?") == 0)
-        usage ();
+        {
+          usage ();
+        }
       else if (strcmp (arg, "--version") == 0)
-        version ();
+        {
+          version ();
+        }
       else if (strcmp (arg, "--introspect") == 0)
-        introspect ();
+        {
+          introspect ();
+        }
       else if (strcmp (arg, "--nofork") == 0)
-        force_fork = FORK_NEVER;
+        {
+          flags &= ~BUS_CONTEXT_FLAG_FORK_ALWAYS;
+          flags |= BUS_CONTEXT_FLAG_FORK_NEVER;
+        }
+#ifdef DBUS_UNIX
       else if (strcmp (arg, "--fork") == 0)
-        force_fork = FORK_ALWAYS;
+        {
+          flags &= ~BUS_CONTEXT_FLAG_FORK_NEVER;
+          flags |= BUS_CONTEXT_FLAG_FORK_ALWAYS;
+        }
       else if (strcmp (arg, "--systemd-activation") == 0)
-        systemd_activation = TRUE;
+        {
+          flags |= BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION;
+        }
+#endif
+      else if (strcmp (arg, "--nopidfile") == 0)
+        {
+          flags &= ~BUS_CONTEXT_FLAG_WRITE_PID_FILE;
+        }
       else if (strcmp (arg, "--system") == 0)
         {
           check_two_config_files (&config_file, "system");
@@ -363,7 +467,9 @@ main (int argc, char **argv)
             exit (1);
         }
       else if (strcmp (arg, "--config-file") == 0)
-        ; /* wait for next arg */
+        {
+          /* wait for next arg */
+        }
       else if (strstr (arg, "--address=") == arg)
         {
           const char *file;
@@ -385,7 +491,9 @@ main (int argc, char **argv)
             exit (1);
         }
       else if (strcmp (arg, "--address") == 0)
-        ; /* wait for next arg */
+        {
+          /* wait for next arg */
+        }
       else if (strstr (arg, "--print-address=") == arg)
         {
           const char *desc;
@@ -411,7 +519,9 @@ main (int argc, char **argv)
           print_address = TRUE;
         }
       else if (strcmp (arg, "--print-address") == 0)
-        print_address = TRUE; /* and we'll get the next arg if appropriate */
+        {
+          print_address = TRUE; /* and we'll get the next arg if appropriate */
+        }
       else if (strstr (arg, "--print-pid=") == arg)
         {
           const char *desc;
@@ -437,9 +547,13 @@ main (int argc, char **argv)
           print_pid = TRUE;
         }
       else if (strcmp (arg, "--print-pid") == 0)
-        print_pid = TRUE; /* and we'll get the next arg if appropriate */
+        {
+          print_pid = TRUE; /* and we'll get the next arg if appropriate */
+        }
       else
-        usage ();
+        {
+          usage ();
+        }
 
       prev_arg = arg;
 
@@ -503,10 +617,9 @@ main (int argc, char **argv)
     }
 
   dbus_error_init (&error);
-  context = bus_context_new (&config_file, force_fork,
+  context = bus_context_new (&config_file, flags,
                              &print_addr_pipe, &print_pid_pipe,
                              _dbus_string_get_length(&address) > 0 ? &address : NULL,
-                             systemd_activation,
                              &error);
   _dbus_string_free (&config_file);
   if (context == NULL)
@@ -521,20 +634,31 @@ main (int argc, char **argv)
    * print_pid_pipe
    */
 
+#ifdef DBUS_UNIX
   setup_reload_pipe (bus_context_get_loop (context));
 
-#ifdef DBUS_UNIX
   /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
    * unimplemented (and probably unimplementable) on Windows, so there's
    * no point in trying to make the handler portable to non-Unix. */
+
+  _dbus_set_signal_handler (SIGTERM, signal_handler);
 #ifdef SIGHUP
   _dbus_set_signal_handler (SIGHUP, signal_handler);
 #endif
-#ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
-  _dbus_set_signal_handler (SIGIO, signal_handler);
-#endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
 #endif /* DBUS_UNIX */
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+  if (bus_context_get_systemd_activation(context) == TRUE)
+    {
+      if (strncmp(bus_context_get_address(context), "kdbus:", strlen("kdbus:")) == 0 &&
+                  !strcmp(bus_context_get_type(context), "system") &&
+                  getuid() == 0)
+        {
+          kill (1, SIGUSR1);
+        }
+    }
+#endif
+
   _dbus_verbose ("We are on D-Bus...\n");
   _dbus_loop_run (bus_context_get_loop (context));