[daemon-fix] fixed getting uid and pid when transport is not kdbus
[platform/upstream/dbus.git] / tools / dbus-launch.c
index dcce646..2a2abbb 100644 (file)
@@ -105,6 +105,7 @@ save_machine_uuid (const char *uuid_arg)
   machine_uuid = xstrdup (uuid_arg);
 }
 
+#ifdef DBUS_BUILD_X11
 #define UUID_MAXLEN 40
 /* Read the machine uuid from file if needed. Returns TRUE if machine_uuid is
  * set after this function */
@@ -146,12 +147,13 @@ out:
   fclose(f);
   return ret;
 }
-
+#endif /* DBUS_BUILD_X11 */
 
 void
 verbose (const char *format,
          ...)
 {
+#ifdef DBUS_ENABLE_VERBOSE_MODE
   va_list args;
   static int verbose = TRUE;
   static int verbose_initted = FALSE;
@@ -176,12 +178,16 @@ verbose (const char *format,
   va_start (args, format);
   vfprintf (stderr, format, args);
   va_end (args);
+#endif /* DBUS_ENABLE_VERBOSE_MODE */
 }
 
 static void
 usage (int ecode)
 {
-  fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session] [--exit-with-x11]\n");
+  fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax]"
+           " [--csh-syntax] [--auto-syntax] [--binary-syntax] [--close-stderr]"
+           " [--exit-with-session] [--autolaunch=MACHINEID]"
+           " [--config-file=FILENAME] [PROGRAM] [ARGS...]\n");
   exit (ecode);
 }
 
@@ -633,6 +639,7 @@ babysit (int   exit_with_session,
       s = getenv ("DBUS_DEBUG_OUTPUT");
       if (s == NULL || *s == '\0')
         dup2 (dev_null_fd, 2);
+      close (dev_null_fd);
     }
   else
     {
@@ -754,31 +761,31 @@ pass_info (const char *runprog, const char *bus_address, pid_t bus_pid,
       if (envvar == NULL || args == NULL)
         goto oom;
 
-     args[0] = xstrdup (runprog);
+      args[0] = xstrdup (runprog);
       if (!args[0])
         goto oom;
-     for (i = 1; i <= (argc-remaining_args); i++)
-      {
-        size_t len = strlen (argv[remaining_args+i-1])+1;
-        args[i] = malloc (len);
-        if (!args[i])
-          goto oom;
-        strncpy (args[i], argv[remaining_args+i-1], len);
-       }
-     args[i] = NULL;
+      for (i = 1; i <= (argc-remaining_args); i++)
+        {
+          size_t len = strlen (argv[remaining_args+i-1])+1;
+          args[i] = malloc (len);
+          if (!args[i])
+            goto oom;
+          strncpy (args[i], argv[remaining_args+i-1], len);
+        }
+      args[i] = NULL;
 
-     strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
-     strcat (envvar, bus_address);
-     putenv (envvar);
+      strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
+      strcat (envvar, bus_address);
+      putenv (envvar);
 
-     execvp (runprog, args);
-     fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
-     exit (1);
+      execvp (runprog, args);
+      fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
+      exit (1);
     }
    else
     {
       print_variables (bus_address, bus_pid, bus_wid, c_shell_syntax,
-         bourne_shell_syntax, binary_syntax);
+          bourne_shell_syntax, binary_syntax);
     }
   verbose ("dbus-launch exiting\n");
 
@@ -809,7 +816,6 @@ main (int argc, char **argv)
   const char *runprog = NULL;
   int remaining_args = 0;
   int exit_with_session;
-  int exit_with_x11 = FALSE;
   int binary_syntax = FALSE;
   int c_shell_syntax = FALSE;
   int bourne_shell_syntax = FALSE;
@@ -817,6 +823,7 @@ main (int argc, char **argv)
   int autolaunch = FALSE;
   int requires_arg = FALSE;
   int close_stderr = FALSE;
+  int kdbus = FALSE;
   int i;
   int ret;
   int bus_pid_to_launcher_pipe[2];
@@ -851,10 +858,10 @@ main (int argc, char **argv)
         version ();
       else if (strcmp (arg, "--exit-with-session") == 0)
         exit_with_session = TRUE;
-      else if (strcmp (arg, "--exit-with-x11") == 0)
-        exit_with_x11 = TRUE;
       else if (strcmp (arg, "--close-stderr") == 0)
         close_stderr = TRUE;
+      else if (strcmp (arg, "--kdbus") == 0)
+        kdbus = TRUE;
       else if (strstr (arg, "--autolaunch=") == arg)
         {
           const char *s;
@@ -964,9 +971,6 @@ main (int argc, char **argv)
   if (exit_with_session)
     verbose ("--exit-with-session enabled\n");
 
-  if (exit_with_x11)
-    verbose ("--exit-with-x11 enabled\n");
-
   if (autolaunch)
     {      
 #ifndef DBUS_BUILD_X11
@@ -989,10 +993,10 @@ main (int argc, char **argv)
         }
 
       verbose ("Autolaunch enabled (using X11).\n");
-      if (!exit_with_x11)
+      if (!exit_with_session)
        {
-          verbose ("--exit-with-x11 automatically enabled\n");
-          exit_with_x11 = TRUE;
+         verbose ("--exit-with-session automatically enabled\n");
+         exit_with_session = TRUE;
        }
 
       if (!x11_init ())
@@ -1015,27 +1019,12 @@ main (int argc, char **argv)
          exit (0);
        }
 #endif /* DBUS_ENABLE_X11_AUTOLAUNCH */
-#endif /* DBUS_BUILD_X11 */
     }
-  else if (exit_with_x11)
-    {
-#ifndef DBUS_BUILD_X11
-      fprintf (stderr, "Session lifetime based on X11 requested, but X11 support not compiled in.\n");
-      exit (1);
-#else /* DBUS_BUILD_X11 */
-      if (!x11_init ())
-        {
-          fprintf (stderr, "Session lifetime based on X11 requested, but X11 initialization failed.\n");
-          exit (1);
-        }
-#endif /* DBUS_BUILD_X11 */
-    }
-#ifdef DBUS_BUILD_X11
   else if (read_machine_uuid_if_needed())
     {
       x11_init();
-    }
 #endif /* DBUS_BUILD_X11 */
+    }
 
 
   if (pipe (bus_pid_to_launcher_pipe) < 0 ||
@@ -1097,7 +1086,7 @@ main (int argc, char **argv)
            * and will also reap the pre-forked bus
            * daemon
            */
-          babysit (exit_with_session || exit_with_x11, ret,
+          babysit (exit_with_session, ret,
                    bus_pid_to_babysitter_pipe[READ_END]);
           exit (0);
         }
@@ -1120,7 +1109,7 @@ main (int argc, char **argv)
 
       verbose ("Calling exec()\n");
  
-#ifdef DBUS_BUILD_TESTS 
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       /* exec from testdir */
       if (getenv("DBUS_USE_TEST_BINARY") != NULL)
         {
@@ -1137,16 +1126,32 @@ main (int argc, char **argv)
                    "Failed to execute test message bus daemon %s: %s. Will try again with the system path.\n",
                    TEST_BUS_BINARY, strerror (errno));
         }
- #endif /* DBUS_BUILD_TESTS */
+ #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
 
-      execl (DBUS_DAEMONDIR"/dbus-daemon",
+      if (kdbus)
+        {
+          execl (DBUS_DAEMONDIR"/dbus-daemon",
              DBUS_DAEMONDIR"/dbus-daemon",
              "--fork",
              "--print-pid", write_pid_fd_as_string,
              "--print-address", write_address_fd_as_string,
+             "--address=kdbus:",
              config_file ? "--config-file" : "--session",
              config_file, /* has to be last in this varargs list */
              NULL);
+        }
+      else
+        {
+          execl (DBUS_DAEMONDIR"/dbus-daemon",
+             DBUS_DAEMONDIR"/dbus-daemon",
+             "--fork",
+             "--print-pid", write_pid_fd_as_string,
+             "--print-address", write_address_fd_as_string,
+             config_file ? "--config-file" : "--session",
+             config_file, /* has to be last in this varargs list */
+             NULL);
+        }
+
 
       fprintf (stderr,
                "Failed to execute message bus daemon %s: %s.  Will try again without full path.\n",
@@ -1158,14 +1163,29 @@ main (int argc, char **argv)
        * file and the dbus-daemon will not be in the install location during
        * build time.
        */
-      execlp ("dbus-daemon",
+      if (kdbus)
+        {
+          execlp ("dbus-daemon",
               "dbus-daemon",
               "--fork",
               "--print-pid", write_pid_fd_as_string,
               "--print-address", write_address_fd_as_string,
+              "--address=kdbus:",
               config_file ? "--config-file" : "--session",
               config_file, /* has to be last in this varargs list */
               NULL);
+        }
+      else
+        {
+          execlp ("dbus-daemon",
+              "dbus-daemon",
+              "--fork",
+              "--print-pid", write_pid_fd_as_string,
+              "--print-address", write_address_fd_as_string,
+              config_file ? "--config-file" : "--session",
+              config_file, /* has to be last in this varargs list */
+              NULL);
+        }
 
       fprintf (stderr,
                "Failed to execute message bus daemon: %s\n",
@@ -1182,7 +1202,6 @@ main (int argc, char **argv)
       char *end;
       long wid = 0;
       long val;
-      int ret2;
 
       verbose ("=== Parent dbus-launch continues\n");
       
@@ -1256,6 +1275,8 @@ main (int argc, char **argv)
 #ifdef DBUS_ENABLE_X11_AUTOLAUNCH
       if (xdisplay != NULL)
         {
+          int ret2;
+
           verbose("Saving x11 address\n");
           ret2 = x11_save_address (bus_address, bus_pid, &wid);
           /* Only get an existing dbus session when autolaunching */