1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-launch.c dbus-launch utility
4 * Copyright (C) 2003, 2006 Red Hat, Inc.
5 * Copyright (C) 2006 Thiago Macieira <thiago@kde.org>
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dbus-launch.h"
38 #include <sys/select.h>
41 #include <dbus/dbus.h>
42 #include "dbus/dbus-internals.h"
46 extern Display *xdisplay;
49 #include "dbus/dbus-internals.h"
50 #include "dbus/dbus-sysdeps-unix.h"
52 #include "tool-common.h"
56 * If you are in a shell and run "dbus-launch myapp", here is what happens:
59 * \- main() --exec--> myapp[*]
60 * \- "intermediate parent"
61 * \- bus-runner --exec--> dbus-daemon --fork
62 * \- babysitter[*] \- final dbus-daemon[*]
64 * Processes marked [*] survive the initial flurry of activity.
66 * If you run "dbus-launch --sh-syntax" then the diagram is the same, except
67 * that main() prints variables and exits 0 instead of exec'ing myapp.
71 * dbus-daemon --print-pid -> bus_pid_to_launcher_pipe -> main
72 * dbus-daemon --print-address -> bus_address_to_launcher_pipe -> main
73 * main -> bus_pid_to_babysitter_pipe -> babysitter
75 * The intermediate parent looks pretty useless at first glance. Its purpose
76 * is to avoid the bus-runner becoming a zombie: when the intermediate parent
77 * terminates, the bus-runner and babysitter are reparented to init, which
78 * reaps them if they have finished. We can't rely on main() to reap arbitrary
79 * children because it might exec myapp, after which it can't be relied on to
80 * reap its children. We *can* rely on main() to reap the intermediate parent,
81 * because that happens before it execs myapp.
83 * It's unclear why dbus-daemon needs to fork, but we explicitly tell it to
84 * for some reason, then wait for it. If we left it undefined, a forking
85 * dbus-daemon would get the parent process reparented to init and reaped
86 * when the intermediate parent terminated, and a non-forking dbus-daemon
87 * would get reparented to init and carry on there.
89 * myapp is exec'd by the process that initially ran main() so that it's
90 * the shell's child, so the shell knows how to do job control and stuff.
91 * This is desirable for the "dbus-launch an application" use-case, less so
92 * for the "dbus-launch a test suite in an isolated session" use-case.
95 static char* machine_uuid = NULL;
98 get_machine_uuid (void)
104 save_machine_uuid (const char *uuid_arg)
106 if (strlen (uuid_arg) != 32)
108 fprintf (stderr, "machine ID '%s' looks like it's the wrong length, should be 32 hex digits",
113 machine_uuid = _dbus_strdup (uuid_arg);
116 #ifdef DBUS_BUILD_X11
117 /* Read the machine uuid from file if needed. Returns TRUE if machine_uuid is
118 * set after this function */
120 read_machine_uuid_if_needed (DBusError *error)
122 if (machine_uuid != NULL)
125 machine_uuid = dbus_try_get_local_machine_id (error);
127 if (machine_uuid == NULL)
130 verbose ("UID: %s\n", machine_uuid);
133 #endif /* DBUS_BUILD_X11 */
136 verbose (const char *format,
139 #ifdef DBUS_ENABLE_VERBOSE_MODE
141 static int verbose = TRUE;
142 static int verbose_initted = FALSE;
144 /* things are written a bit oddly here so that
145 * in the non-verbose case we just have the one
146 * conditional and return immediately.
151 if (!verbose_initted)
153 verbose = getenv ("DBUS_VERBOSE") != NULL;
154 verbose_initted = TRUE;
159 fprintf (stderr, "%lu: ", (unsigned long) getpid ());
161 va_start (args, format);
162 vfprintf (stderr, format, args);
164 #endif /* DBUS_ENABLE_VERBOSE_MODE */
167 static void usage (int ecode) _DBUS_GNUC_NORETURN;
172 fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax]"
173 " [--csh-syntax] [--auto-syntax] [--binary-syntax] [--close-stderr]"
174 " [--exit-with-session|--exit-with-x11] [--autolaunch=MACHINEID]"
175 " [--config-file=FILENAME] [PROGRAM] [ARGS...]\n");
179 static void version (void) _DBUS_GNUC_NORETURN;
184 printf ("D-Bus Message Bus Launcher %s\n"
185 "Copyright (C) 2003 Red Hat, Inc.\n"
186 "This is free software; see the source for copying conditions.\n"
187 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
193 xstrdup (const char *str)
203 copy = malloc (len + 1);
207 memcpy (copy, str, len + 1);
212 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
214 concat2 (const char *a,
223 ret = malloc (la + lb + 1);
229 memcpy (ret + la, b, lb + 1);
232 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
236 READ_STATUS_OK, /**< Read succeeded */
237 READ_STATUS_ERROR, /**< Some kind of error */
238 READ_STATUS_EOF /**< EOF returned */
249 memset (buf, '\0', maxlen);
250 maxlen -= 1; /* ensure nul term */
252 retval = READ_STATUS_OK;
260 to_read = maxlen - bytes;
268 if (chunk < 0 && errno == EINTR)
273 retval = READ_STATUS_ERROR;
278 retval = READ_STATUS_EOF;
285 if (retval == READ_STATUS_EOF &&
287 retval = READ_STATUS_OK;
290 if (retval != READ_STATUS_ERROR &&
292 buf[bytes-1] == '\n')
305 retval = READ_STATUS_OK;
313 to_read = sizeof (pid_t) - bytes;
319 ((char*)buf) + bytes,
321 if (chunk < 0 && errno == EINTR)
326 retval = READ_STATUS_ERROR;
331 retval = READ_STATUS_EOF;
342 do_write (int fd, const void *buf, size_t count)
344 size_t bytes_written;
351 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
359 fprintf (stderr, "Failed to write data to pipe! %s\n",
361 exit (1); /* give up, we suck */
365 bytes_written += ret;
367 if (bytes_written < count)
375 do_write (fd, &pid, sizeof (pid));
379 do_waitpid (pid_t pid)
384 ret = waitpid (pid, NULL, 0);
393 static pid_t bus_pid_to_kill = -1;
398 if (bus_pid_to_kill <= 0)
401 verbose ("Killing message bus and exiting babysitter\n");
402 kill (bus_pid_to_kill, SIGTERM);
404 kill (bus_pid_to_kill, SIGKILL);
408 kill_bus_and_exit (int exitcode)
410 /* in case these point to any NFS mounts, get rid of them immediately */
421 print_variables (const char *bus_address, pid_t bus_pid, long bus_wid,
422 int c_shell_syntax, int bourne_shell_syntax,
427 do_write (1, bus_address, strlen (bus_address) + 1);
428 do_write (1, &bus_pid, sizeof bus_pid);
429 do_write (1, &bus_wid, sizeof bus_wid);
432 else if (c_shell_syntax)
434 printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s';\n", bus_address);
436 printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
438 printf ("set DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
441 else if (bourne_shell_syntax)
443 printf ("DBUS_SESSION_BUS_ADDRESS='%s';\n", bus_address);
444 printf ("export DBUS_SESSION_BUS_ADDRESS;\n");
446 printf ("DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
448 printf ("DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
453 printf ("DBUS_SESSION_BUS_ADDRESS=%s\n", bus_address);
455 printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
457 printf ("DBUS_SESSION_BUS_WINDOWID=%ld\n", (long) bus_wid);
462 static int got_fatal_signal = 0;
465 signal_handler (int sig)
467 got_fatal_signal = sig;
470 static void kill_bus_when_session_ends (void) _DBUS_GNUC_NORETURN;
473 kill_bus_when_session_ends (void)
479 struct sigaction act;
482 /* install SIGHUP handler */
483 got_fatal_signal = 0;
484 sigemptyset (&empty_mask);
485 act.sa_handler = signal_handler;
486 act.sa_mask = empty_mask;
488 sigaction (SIGHUP, &act, NULL);
489 sigaction (SIGTERM, &act, NULL);
490 sigaction (SIGINT, &act, NULL);
492 #ifdef DBUS_BUILD_X11
494 if (xdisplay != NULL)
496 x_fd = ConnectionNumber (xdisplay);
511 verbose ("session lifetime is defined by X, not monitoring stdin\n");
514 else if (tty_fd >= 0)
516 verbose ("stdin isatty(), monitoring it\n");
520 verbose ("stdin was not a TTY, not monitoring it\n");
523 if (tty_fd < 0 && x_fd < 0)
525 fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
526 kill_bus_and_exit (1);
531 #ifdef DBUS_BUILD_X11
532 /* Dump events on the floor, and let
533 * IO error handler run if we lose
534 * the X connection. It's important to
535 * run this before going into select() since
536 * we might have queued outgoing messages or
547 FD_SET (tty_fd, &read_set);
548 FD_SET (tty_fd, &err_set);
553 FD_SET (x_fd, &read_set);
554 FD_SET (x_fd, &err_set);
557 select (MAX (tty_fd, x_fd) + 1,
558 &read_set, NULL, &err_set, NULL);
560 if (got_fatal_signal)
562 verbose ("Got fatal signal %d, killing dbus-daemon\n",
564 kill_bus_and_exit (0);
567 #ifdef DBUS_BUILD_X11
568 /* Events will be processed before we select again
571 verbose ("X fd condition reading = %d error = %d\n",
572 FD_ISSET (x_fd, &read_set),
573 FD_ISSET (x_fd, &err_set));
578 if (FD_ISSET (tty_fd, &read_set))
583 verbose ("TTY ready for reading\n");
585 bytes_read = read (tty_fd, discard, sizeof (discard));
587 verbose ("Read %d bytes from TTY errno = %d\n",
591 kill_bus_and_exit (0); /* EOF */
592 else if (bytes_read < 0 && errno != EINTR)
594 /* This shouldn't happen I don't think; to avoid
595 * spinning on the fd forever we exit.
597 fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
599 kill_bus_and_exit (0);
602 else if (FD_ISSET (tty_fd, &err_set))
604 verbose ("TTY has error condition\n");
606 kill_bus_and_exit (0);
613 _DBUS_GNUC_NORETURN static void
614 babysit (int exit_with_session,
616 int read_bus_pid_fd) /* read pid from here */
618 DBusEnsureStandardFdsFlags flags;
622 verbose ("babysitting, exit_with_session = %d, child_pid = %ld, read_bus_pid_fd = %d\n",
623 exit_with_session, (long) child_pid, read_bus_pid_fd);
625 /* We chdir ("/") since we are persistent and daemon-like, and fork
626 * again so dbus-launch can reap the parent. However, we don't
627 * setsid() or close fd 0 because the idea is to remain attached
628 * to the tty and the X server in order to kill the message bus
629 * when the session ends.
634 fprintf (stderr, "Could not change to root directory: %s\n",
639 flags = DBUS_FORCE_STDOUT_NULL;
641 if (!exit_with_session)
642 flags |= DBUS_FORCE_STDIN_NULL;
644 s = getenv ("DBUS_DEBUG_OUTPUT");
646 if (s == NULL || *s == '\0')
647 flags |= DBUS_FORCE_STDERR_NULL;
649 /* Close stdout/stderr so we don't block an "eval" or otherwise
650 * lock up. stdout is still chaining through to dbus-launch
651 * and in turn to the parent shell.
653 if (!_dbus_ensure_standard_fds (flags, &s))
655 fprintf (stderr, "%s: %s\n", s, strerror (errno));
663 fprintf (stderr, "fork() failed in babysitter: %s\n",
670 /* Parent reaps pre-fork part of bus daemon, then exits and is
671 * reaped so the babysitter isn't a zombie
674 verbose ("=== Babysitter's intermediate parent continues again\n");
676 if (do_waitpid (child_pid) < 0)
678 /* shouldn't happen */
679 fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
683 verbose ("Babysitter's intermediate parent exiting\n");
688 /* Child continues */
689 verbose ("=== Babysitter process created\n");
691 verbose ("Reading PID from bus\n");
693 switch (read_pid (read_bus_pid_fd, &bus_pid_to_kill))
697 case READ_STATUS_EOF:
698 fprintf (stderr, "EOF in dbus-launch reading PID from bus daemon\n");
701 case READ_STATUS_ERROR:
702 fprintf (stderr, "Error in dbus-launch reading PID from bus daemon: %s\n",
707 _dbus_assert_not_reached ("Invalid read result");
710 verbose ("Got PID %ld from daemon\n",
711 (long) bus_pid_to_kill);
713 if (exit_with_session)
715 /* Bus is now started and launcher has needed info;
716 * we connect to X display and tty and wait to
717 * kill bus if requested.
720 kill_bus_when_session_ends ();
723 verbose ("Babysitter exiting\n");
729 do_close_stderr (void)
735 if (!_dbus_ensure_standard_fds (DBUS_FORCE_STDERR_NULL, &err))
737 fprintf (stderr, "%s: %s\n", err, strerror (errno));
742 static void pass_info (const char *runprog, const char *bus_address,
743 pid_t bus_pid, long bus_wid, int c_shell_syntax,
744 int bourne_shell_syntax, int binary_syntax,
745 int argc, char **argv,
746 int remaining_args) _DBUS_GNUC_NORETURN;
749 pass_info (const char *runprog, const char *bus_address, pid_t bus_pid,
750 long bus_wid, int c_shell_syntax, int bourne_shell_syntax,
752 int argc, char **argv, int remaining_args)
761 envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") +
762 strlen (bus_address) + 1);
763 args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
765 if (envvar == NULL || args == NULL)
768 args[0] = xstrdup (runprog);
771 for (i = 1; i <= (argc-remaining_args); i++)
773 size_t len = strlen (argv[remaining_args+i-1])+1;
774 args[i] = malloc (len);
781 strncpy (args[i], argv[remaining_args+i-1], len);
785 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
786 strcat (envvar, bus_address);
789 execvp (runprog, args);
790 fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
795 print_variables (bus_address, bus_pid, bus_wid, c_shell_syntax,
796 bourne_shell_syntax, binary_syntax);
798 verbose ("dbus-launch exiting\n");
812 fprintf (stderr, "Out of memory!");
820 main (int argc, char **argv)
822 const char *prev_arg;
824 const char *runprog = NULL;
825 int remaining_args = 0;
826 int exit_with_session;
827 int exit_with_x11 = FALSE;
828 int binary_syntax = FALSE;
829 int c_shell_syntax = FALSE;
830 int bourne_shell_syntax = FALSE;
831 int auto_shell_syntax = FALSE;
832 int autolaunch = FALSE;
833 int requires_arg = FALSE;
834 int close_stderr = FALSE;
837 int bus_pid_to_launcher_pipe[2];
838 int bus_pid_to_babysitter_pipe[2];
839 int bus_address_to_launcher_pipe[2];
841 dbus_bool_t user_bus_supported = FALSE;
843 const char *error_str;
844 DBusError error = DBUS_ERROR_INIT;
846 exit_with_session = FALSE;
849 /* Ensure that the first three fds are open, to ensure that when we
850 * create other file descriptors (for example for epoll, inotify or
851 * a socket), they never get assigned as fd 0, 1 or 2. If they were,
852 * which could happen if our caller had (incorrectly) closed those
853 * standard fds, then we'd start dbus-daemon with those fds closed,
854 * which is unexpected and could cause it to misbehave. */
855 if (!_dbus_ensure_standard_fds (0, &error_str))
858 "dbus-launch: fatal error setting up standard fds: %s: %s\n",
859 error_str, _dbus_strerror (errno));
867 const char *arg = argv[i];
869 if (strcmp (arg, "--help") == 0 ||
870 strcmp (arg, "-h") == 0 ||
871 strcmp (arg, "-?") == 0)
873 else if (strcmp (arg, "--auto-syntax") == 0)
874 auto_shell_syntax = TRUE;
875 else if (strcmp (arg, "-c") == 0 ||
876 strcmp (arg, "--csh-syntax") == 0)
877 c_shell_syntax = TRUE;
878 else if (strcmp (arg, "-s") == 0 ||
879 strcmp (arg, "--sh-syntax") == 0)
880 bourne_shell_syntax = TRUE;
881 else if (strcmp (arg, "--binary-syntax") == 0)
882 binary_syntax = TRUE;
883 else if (strcmp (arg, "--version") == 0)
885 else if (strcmp (arg, "--exit-with-session") == 0)
886 exit_with_session = TRUE;
887 else if (strcmp (arg, "--exit-with-x11") == 0)
888 exit_with_x11 = TRUE;
889 else if (strcmp (arg, "--close-stderr") == 0)
891 else if (strstr (arg, "--autolaunch=") == arg)
897 fprintf (stderr, "--autolaunch given twice\n");
903 s = strchr (arg, '=');
906 save_machine_uuid (s);
909 strcmp (prev_arg, "--autolaunch") == 0)
913 fprintf (stderr, "--autolaunch given twice\n");
919 save_machine_uuid (arg);
920 requires_arg = FALSE;
922 else if (strcmp (arg, "--autolaunch") == 0)
924 else if (strstr (arg, "--config-file=") == arg)
928 if (config_file != NULL)
930 fprintf (stderr, "--config-file given twice\n");
934 file = strchr (arg, '=');
937 config_file = xstrdup (file);
940 strcmp (prev_arg, "--config-file") == 0)
942 if (config_file != NULL)
944 fprintf (stderr, "--config-file given twice\n");
948 config_file = xstrdup (arg);
949 requires_arg = FALSE;
951 else if (strcmp (arg, "--config-file") == 0)
953 else if (arg[0] == '-')
955 if (strcmp (arg, "--") != 0)
957 fprintf (stderr, "Option `%s' is unknown.\n", arg);
963 remaining_args = i+2;
970 remaining_args = i+1;
980 fprintf (stderr, "Option `%s' requires an argument.\n", prev_arg);
984 if (auto_shell_syntax)
986 if ((shname = getenv ("SHELL")) != NULL)
988 if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
989 c_shell_syntax = TRUE;
991 bourne_shell_syntax = TRUE;
994 bourne_shell_syntax = TRUE;
997 if (exit_with_session)
998 verbose ("--exit-with-session enabled\n");
1001 verbose ("--exit-with-x11 enabled\n");
1005 #ifndef DBUS_BUILD_X11
1006 fprintf (stderr, "Autolaunch requested, but X11 support not compiled in.\n"
1007 "Cannot continue.\n");
1009 #else /* DBUS_BUILD_X11 */
1010 #ifndef DBUS_ENABLE_X11_AUTOLAUNCH
1011 fprintf (stderr, "X11 autolaunch support disabled at compile time.\n");
1013 #else /* DBUS_ENABLE_X11_AUTOLAUNCH */
1018 if (get_machine_uuid () == NULL)
1020 fprintf (stderr, "Machine UUID not provided as arg to --autolaunch\n");
1024 if (!_dbus_string_init (&user_bus))
1025 tool_oom ("initializing");
1027 /* If we have an XDG_RUNTIME_DIR and it contains a suitable socket,
1028 * dbus-launch --autolaunch can use it, since --autolaunch implies
1029 * "I'm OK with getting a bus that is already active".
1031 * (However, plain dbus-launch without --autolaunch must not do so,
1032 * because that would break lots of regression tests, which often
1033 * use dbus-launch instead of the more appropriate dbus-run-session.)
1035 * At this stage, we just save the user bus's address; later on, the
1036 * "babysitter" process will be available to advertise the user-bus
1037 * on the X11 display and in ~/.dbus/session-bus, for full
1038 * backwards compatibility.
1040 if (!_dbus_lookup_user_bus (&user_bus_supported, &user_bus, &error))
1042 fprintf (stderr, "%s\n", error.message);
1045 else if (user_bus_supported)
1047 verbose ("=== Using existing user bus \"%s\"\n",
1048 _dbus_string_get_const_data (&user_bus));
1052 _dbus_string_free (&user_bus);
1055 verbose ("Autolaunch enabled (using X11).\n");
1058 verbose ("--exit-with-x11 automatically enabled\n");
1059 exit_with_x11 = TRUE;
1064 fprintf (stderr, "Autolaunch error: X11 initialization failed.\n");
1068 if (!x11_get_address (&address, &pid, &wid))
1070 fprintf (stderr, "Autolaunch error: X11 communication error.\n");
1074 if (address != NULL)
1076 verbose ("dbus-daemon is already running. Returning existing parameters.\n");
1077 pass_info (runprog, address, pid, wid, c_shell_syntax,
1078 bourne_shell_syntax, binary_syntax, argc, argv, remaining_args);
1081 #endif /* DBUS_ENABLE_X11_AUTOLAUNCH */
1082 #endif /* DBUS_BUILD_X11 */
1084 else if (exit_with_x11)
1086 #ifndef DBUS_BUILD_X11
1087 fprintf (stderr, "Session lifetime based on X11 requested, but X11 support not compiled in.\n");
1089 #else /* DBUS_BUILD_X11 */
1090 if (!read_machine_uuid_if_needed (&error))
1092 fprintf (stderr, "Session lifetime based on X11 requested, but machine UUID unavailable: %s.\n", error.message);
1093 dbus_error_free (&error);
1099 fprintf (stderr, "Session lifetime based on X11 requested, but X11 initialization failed.\n");
1102 #endif /* DBUS_BUILD_X11 */
1104 #ifdef DBUS_BUILD_X11
1105 else if (read_machine_uuid_if_needed (&error))
1111 /* Survive this misconfiguration, but complain about it. */
1112 fprintf (stderr, "%s\n", error.message);
1113 dbus_error_free (&error);
1115 #endif /* DBUS_BUILD_X11 */
1118 if (pipe (bus_pid_to_launcher_pipe) < 0 ||
1119 pipe (bus_address_to_launcher_pipe) < 0 ||
1120 pipe (bus_pid_to_babysitter_pipe) < 0)
1123 "Failed to create pipe: %s\n",
1131 fprintf (stderr, "Failed to fork: %s\n",
1139 #define MAX_FD_LEN 64
1140 char write_pid_fd_as_string[MAX_FD_LEN];
1141 char write_address_fd_as_string[MAX_FD_LEN];
1143 #ifdef DBUS_BUILD_X11
1150 verbose ("=== Babysitter's intermediate parent created\n");
1152 /* Fork once more to create babysitter */
1157 fprintf (stderr, "Failed to fork: %s\n",
1165 verbose ("=== Babysitter's intermediate parent continues\n");
1167 close (bus_pid_to_launcher_pipe[READ_END]);
1168 close (bus_pid_to_launcher_pipe[WRITE_END]);
1169 close (bus_address_to_launcher_pipe[READ_END]);
1170 close (bus_address_to_launcher_pipe[WRITE_END]);
1171 close (bus_pid_to_babysitter_pipe[WRITE_END]);
1173 /* babysit() will fork *again*
1174 * and will also reap the pre-forked bus
1177 babysit (exit_with_session || exit_with_x11, ret,
1178 bus_pid_to_babysitter_pipe[READ_END]);
1182 verbose ("=== Bus exec process created\n");
1184 /* Now we are the bus process (well, almost;
1185 * dbus-daemon itself forks again)
1187 close (bus_pid_to_launcher_pipe[READ_END]);
1188 close (bus_address_to_launcher_pipe[READ_END]);
1189 close (bus_pid_to_babysitter_pipe[READ_END]);
1190 close (bus_pid_to_babysitter_pipe[WRITE_END]);
1192 /* If we have a user bus and want to use it, do so instead of
1193 * exec'ing a new dbus-daemon. */
1194 if (autolaunch && user_bus_supported)
1196 do_write (bus_pid_to_launcher_pipe[WRITE_END], "0\n", 2);
1197 close (bus_pid_to_launcher_pipe[WRITE_END]);
1199 do_write (bus_address_to_launcher_pipe[WRITE_END],
1200 _dbus_string_get_const_data (&user_bus),
1201 _dbus_string_get_length (&user_bus));
1202 do_write (bus_address_to_launcher_pipe[WRITE_END], "\n", 1);
1203 close (bus_address_to_launcher_pipe[WRITE_END]);
1208 sprintf (write_pid_fd_as_string,
1209 "%d", bus_pid_to_launcher_pipe[WRITE_END]);
1211 sprintf (write_address_fd_as_string,
1212 "%d", bus_address_to_launcher_pipe[WRITE_END]);
1214 verbose ("Calling exec()\n");
1216 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1218 /* exec from testdir */
1219 const char *test_daemon = getenv ("DBUS_TEST_DAEMON");
1221 if (test_daemon != NULL)
1223 if (config_file == NULL && getenv ("DBUS_TEST_DATA") != NULL)
1225 config_file = concat2 (getenv ("DBUS_TEST_DATA"),
1226 "/valid-config-files/session.conf");
1228 if (config_file == NULL)
1230 fprintf (stderr, "Out of memory\n");
1237 close_stderr ? "--syslog-only" : "--syslog",
1239 "--print-pid", write_pid_fd_as_string,
1240 "--print-address", write_address_fd_as_string,
1241 config_file ? "--config-file" : "--session",
1242 config_file, /* has to be last in this varargs list */
1246 "Failed to execute test message bus daemon %s: %s.\n",
1247 test_daemon, strerror (errno));
1251 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
1253 execl (DBUS_DAEMONDIR"/dbus-daemon",
1254 DBUS_DAEMONDIR"/dbus-daemon",
1255 close_stderr ? "--syslog-only" : "--syslog",
1257 "--print-pid", write_pid_fd_as_string,
1258 "--print-address", write_address_fd_as_string,
1259 config_file ? "--config-file" : "--session",
1260 config_file, /* has to be last in this varargs list */
1264 "Failed to execute message bus daemon %s: %s. Will try again without full path.\n",
1265 DBUS_DAEMONDIR"/dbus-daemon", strerror (errno));
1268 * If it failed, try running without full PATH. Note this is needed
1269 * because the build process builds the run-with-tmp-session-bus.conf
1270 * file and the dbus-daemon will not be in the install location during
1273 execlp ("dbus-daemon",
1275 close_stderr ? "--syslog-only" : "--syslog",
1277 "--print-pid", write_pid_fd_as_string,
1278 "--print-address", write_address_fd_as_string,
1279 config_file ? "--config-file" : "--session",
1280 config_file, /* has to be last in this varargs list */
1284 "Failed to execute message bus daemon: %s\n",
1291 #define MAX_PID_LEN 64
1293 char bus_address[MAX_ADDR_LEN];
1294 char buf[MAX_PID_LEN];
1299 verbose ("=== Parent dbus-launch continues\n");
1301 close (bus_pid_to_launcher_pipe[WRITE_END]);
1302 close (bus_address_to_launcher_pipe[WRITE_END]);
1303 close (bus_pid_to_babysitter_pipe[READ_END]);
1305 verbose ("Waiting for babysitter's intermediate parent\n");
1307 /* Immediately reap parent of babysitter
1308 * (which was created just for us to reap)
1310 if (do_waitpid (ret) < 0)
1312 fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
1317 verbose ("Reading address from bus\n");
1319 /* Read the pipe data, print, and exit */
1320 switch (read_line (bus_address_to_launcher_pipe[READ_END],
1321 bus_address, MAX_ADDR_LEN))
1323 case READ_STATUS_OK:
1325 case READ_STATUS_EOF:
1326 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
1329 case READ_STATUS_ERROR:
1330 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
1335 _dbus_assert_not_reached ("Invalid read result");
1338 close (bus_address_to_launcher_pipe[READ_END]);
1340 verbose ("Reading PID from daemon\n");
1342 switch (read_line (bus_pid_to_launcher_pipe[READ_END], buf, MAX_PID_LEN))
1344 case READ_STATUS_OK:
1346 case READ_STATUS_EOF:
1347 fprintf (stderr, "EOF reading PID from bus daemon\n");
1350 case READ_STATUS_ERROR:
1351 fprintf (stderr, "Error reading PID from bus daemon: %s\n",
1356 _dbus_assert_not_reached ("Invalid read result");
1360 val = strtol (buf, &end, 0);
1361 if (buf == end || end == NULL)
1363 fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
1364 buf, strerror (errno));
1370 /* Have to initialize bus_pid_to_kill ASAP, so that the
1371 X error callback can kill it if an error happens. */
1372 bus_pid_to_kill = bus_pid;
1374 close (bus_pid_to_launcher_pipe[READ_END]);
1376 #ifdef DBUS_ENABLE_X11_AUTOLAUNCH
1377 if (xdisplay != NULL)
1381 verbose("Saving x11 address\n");
1382 ret2 = x11_save_address (bus_address, bus_pid, &wid);
1383 /* Only get an existing dbus session when autolaunching */
1388 char *address = NULL;
1389 /* another window got added. Return its address */
1390 if (x11_get_address (&address, &bus_pid, &wid)
1393 verbose ("dbus-daemon is already running. Returning existing parameters.\n");
1394 /* Kill the old bus */
1396 pass_info (runprog, address, bus_pid, wid,
1397 c_shell_syntax, bourne_shell_syntax, binary_syntax,
1398 argc, argv, remaining_args);
1403 fprintf (stderr, "Error saving bus information.\n");
1404 bus_pid_to_kill = bus_pid;
1405 kill_bus_and_exit (1);
1411 /* Forward the pid to the babysitter */
1412 write_pid (bus_pid_to_babysitter_pipe[WRITE_END], bus_pid);
1413 close (bus_pid_to_babysitter_pipe[WRITE_END]);
1415 pass_info (runprog, bus_address, bus_pid, wid, c_shell_syntax,
1416 bourne_shell_syntax, binary_syntax, argc, argv, remaining_args);