2007-08-03 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / main.c
index 32a6161..3652935 100644 (file)
@@ -1,9 +1,9 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* main.c  main() for message bus
  *
  * Copyright (C) 2003 Red Hat, Inc.
  *
- * Licensed under the Academic Free License version 2.0
+ * 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
  *
  */
 #include "bus.h"
+#include "driver.h"
 #include <dbus/dbus-internals.h>
 #include <dbus/dbus-watch.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
+#ifdef HAVE_ERRNO_H
 #include <errno.h>
+#endif
 #include "selinux.h"
 
 static BusContext *context;
@@ -36,6 +39,7 @@ static int reload_pipe[2];
 #define RELOAD_READ_END 0
 #define RELOAD_WRITE_END 1
 
+static void close_reload_pipe (void);
 
 static void
 signal_handler (int sig)
@@ -44,13 +48,18 @@ 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  */
     case SIGHUP:
       _dbus_string_init_const (&str, "foo");
-      if (!_dbus_write (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
-       {
-         _dbus_warn ("Unable to write to reload pipe.\n");
-         exit (1);
-       }
+      if ((reload_pipe[RELOAD_WRITE_END] > 0) && 
+          !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
+        {
+          _dbus_warn ("Unable to write to reload pipe.\n");
+          close_reload_pipe ();
+        }
       break;
 
     case SIGTERM:
@@ -62,14 +71,14 @@ signal_handler (int sig)
 static void
 usage (void)
 {
-  fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
+  fprintf (stderr, DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect]\n");
   exit (1);
 }
 
 static void
 version (void)
 {
-  printf ("D-BUS Message Bus Daemon %s\n"
+  printf ("D-Bus Message Bus Daemon %s\n"
           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
           "This is free software; see the source for copying conditions.\n"
           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
@@ -78,6 +87,30 @@ version (void)
 }
 
 static void
+introspect (void)
+{
+  DBusString xml;
+  const char *v_STRING;  
+
+  if (!_dbus_string_init (&xml))
+    goto oom;
+
+  if (!bus_driver_generate_introspect_string (&xml))
+    {
+      _dbus_string_free (&xml);
+      goto oom;
+    }
+
+  v_STRING = _dbus_string_get_const_data (&xml);
+  printf ("%s\n", v_STRING); 
+
+  exit (0);
+ oom:
+  _dbus_warn ("Can not introspect - Out of memory\n");
+  exit (1);
+}
+static void
 check_two_config_files (const DBusString *config_file,
                         const char       *extra_arg)
 {
@@ -121,20 +154,28 @@ handle_reload_watch (DBusWatch    *watch,
   DBusError error;
   DBusString str;
   _dbus_string_init (&str);
-  if (_dbus_read (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
+  if ((reload_pipe[RELOAD_READ_END] > 0) &&
+      _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
     {
       _dbus_warn ("Couldn't read from reload pipe.\n");
-      exit (1);
+      close_reload_pipe ();
+      return TRUE;
     }
   _dbus_string_free (&str);
 
+  /* this can only fail if we don't understand the config file
+   * or OOM.  Either way we should just stick with the currently
+   * loaded config.
+   */
   dbus_error_init (&error);
   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);
-      exit (1);
     }
   return TRUE;
 }
@@ -164,6 +205,9 @@ setup_reload_pipe (DBusLoop *loop)
       exit (1);
     }
 
+  _dbus_fd_set_close_on_exec (reload_pipe[0]);
+  _dbus_fd_set_close_on_exec (reload_pipe[1]);
+
   watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
                           DBUS_WATCH_READABLE, TRUE,
                           handle_reload_watch, NULL, NULL);
@@ -187,6 +231,16 @@ setup_reload_pipe (DBusLoop *loop)
 
 }
 
+static void
+close_reload_pipe (void)
+{
+    _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
+    reload_pipe[RELOAD_READ_END] = -1;
+
+    _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
+    reload_pipe[RELOAD_WRITE_END] = -1;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -195,13 +249,13 @@ main (int argc, char **argv)
   DBusString addr_fd;
   DBusString pid_fd;
   const char *prev_arg;
-  int print_addr_fd;
-  int print_pid_fd;
+  DBusPipe print_addr_pipe;
+  DBusPipe print_pid_pipe;
   int i;
   dbus_bool_t print_address;
   dbus_bool_t print_pid;
-  dbus_bool_t force_fork;
-  
+  int force_fork;
+
   if (!_dbus_string_init (&config_file))
     return 1;
 
@@ -210,37 +264,41 @@ main (int argc, char **argv)
 
   if (!_dbus_string_init (&pid_fd))
     return 1;
-  
+
   print_address = FALSE;
   print_pid = FALSE;
-  force_fork = FALSE;
-  
+  force_fork = FORK_FOLLOW_CONFIG_FILE;
+
   prev_arg = NULL;
   i = 1;
   while (i < argc)
     {
       const char *arg = argv[i];
-      
+
       if (strcmp (arg, "--help") == 0 ||
           strcmp (arg, "-h") == 0 ||
           strcmp (arg, "-?") == 0)
         usage ();
       else if (strcmp (arg, "--version") == 0)
         version ();
+      else if (strcmp (arg, "--introspect") == 0)
+        introspect ();
+      else if (strcmp (arg, "--nofork") == 0)
+        force_fork = FORK_NEVER;
       else if (strcmp (arg, "--fork") == 0)
-        force_fork = TRUE;
+        force_fork = FORK_ALWAYS;
       else if (strcmp (arg, "--system") == 0)
         {
           check_two_config_files (&config_file, "system");
 
-          if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
+          if (!_dbus_append_system_config_file (&config_file))
             exit (1);
         }
       else if (strcmp (arg, "--session") == 0)
         {
           check_two_config_files (&config_file, "session");
 
-          if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
+          if (!_dbus_append_session_config_file (&config_file))
             exit (1);
         }
       else if (strstr (arg, "--config-file=") == arg)
@@ -331,10 +389,10 @@ main (int argc, char **argv)
       usage ();
     }
 
-  print_addr_fd = -1;
+  _dbus_pipe_invalidate (&print_addr_pipe);
   if (print_address)
     {
-      print_addr_fd = 1; /* stdout */
+      _dbus_pipe_init_stdout (&print_addr_pipe);
       if (_dbus_string_get_length (&addr_fd) > 0)
         {
           long val;
@@ -348,14 +406,15 @@ main (int argc, char **argv)
               exit (1);
             }
 
-          print_addr_fd = val;
+          _dbus_pipe_init (&print_addr_pipe, val);
         }
     }
+  _dbus_string_free (&addr_fd);
 
-  print_pid_fd = -1;
+  _dbus_pipe_invalidate (&print_pid_pipe);
   if (print_pid)
     {
-      print_pid_fd = 1; /* stdout */
+      _dbus_pipe_init_stdout (&print_pid_pipe);
       if (_dbus_string_get_length (&pid_fd) > 0)
         {
           long val;
@@ -369,19 +428,20 @@ main (int argc, char **argv)
               exit (1);
             }
 
-          print_pid_fd = val;
+          _dbus_pipe_init (&print_pid_pipe, val);
         }
     }
+  _dbus_string_free (&pid_fd);
 
-  if (!bus_selinux_init ())
+  if (!bus_selinux_pre_init ())
     {
-      _dbus_warn ("SELinux initialization failed\n");
+      _dbus_warn ("SELinux pre-initialization failed\n");
       exit (1);
     }
 
   dbus_error_init (&error);
   context = bus_context_new (&config_file, force_fork,
-                             print_addr_fd, print_pid_fd,
+                             &print_addr_pipe, &print_pid_pipe,
                              &error);
   _dbus_string_free (&config_file);
   if (context == NULL)
@@ -393,9 +453,12 @@ main (int argc, char **argv)
     }
 
   setup_reload_pipe (bus_context_get_loop (context));
+
   _dbus_set_signal_handler (SIGHUP, signal_handler);
   _dbus_set_signal_handler (SIGTERM, signal_handler);
+#ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX 
+  _dbus_set_signal_handler (SIGIO, signal_handler);
+#endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
   
   _dbus_verbose ("We are on D-Bus...\n");
   _dbus_loop_run (bus_context_get_loop (context));