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>
43 extern Display *xdisplay;
48 * If you are in a shell and run "dbus-launch myapp", here is what happens:
51 * \- main() --exec--> myapp[*]
52 * \- "intermediate parent"
53 * \- bus-runner --exec--> dbus-daemon --fork
54 * \- babysitter[*] \- final dbus-daemon[*]
56 * Processes marked [*] survive the initial flurry of activity.
58 * If you run "dbus-launch --sh-syntax" then the diagram is the same, except
59 * that main() prints variables and exits 0 instead of exec'ing myapp.
63 * dbus-daemon --print-pid -> bus_pid_to_launcher_pipe -> main
64 * dbus-daemon --print-address -> bus_address_to_launcher_pipe -> main
65 * main -> bus_pid_to_babysitter_pipe -> babysitter
67 * The intermediate parent looks pretty useless at first glance. Its purpose
68 * is to avoid the bus-runner becoming a zombie: when the intermediate parent
69 * terminates, the bus-runner and babysitter are reparented to init, which
70 * reaps them if they have finished. We can't rely on main() to reap arbitrary
71 * children because it might exec myapp, after which it can't be relied on to
72 * reap its children. We *can* rely on main() to reap the intermediate parent,
73 * because that happens before it execs myapp.
75 * It's unclear why dbus-daemon needs to fork, but we explicitly tell it to
76 * for some reason, then wait for it. If we left it undefined, a forking
77 * dbus-daemon would get the parent process reparented to init and reaped
78 * when the intermediate parent terminated, and a non-forking dbus-daemon
79 * would get reparented to init and carry on there.
81 * myapp is exec'd by the process that initially ran main() so that it's
82 * the shell's child, so the shell knows how to do job control and stuff.
83 * This is desirable for the "dbus-launch an application" use-case, less so
84 * for the "dbus-launch a test suite in an isolated session" use-case.
87 static char* machine_uuid = NULL;
90 get_machine_uuid (void)
96 save_machine_uuid (const char *uuid_arg)
98 if (strlen (uuid_arg) != 32)
100 fprintf (stderr, "machine ID '%s' looks like it's the wrong length, should be 32 hex digits",
105 machine_uuid = xstrdup (uuid_arg);
108 #define UUID_MAXLEN 40
109 /* Read the machine uuid from file if needed. Returns TRUE if machine_uuid is
110 * set after this function */
112 read_machine_uuid_if_needed (void)
115 char uuid[UUID_MAXLEN];
119 if (machine_uuid != NULL)
122 f = fopen (DBUS_MACHINE_UUID_FILE, "r");
126 if (fgets (uuid, UUID_MAXLEN, f) == NULL)
133 /* rstrip the read uuid */
134 while (len > 31 && isspace(uuid[len - 1]))
141 machine_uuid = xstrdup (uuid);
142 verbose ("UID: %s\n", machine_uuid);
152 verbose (const char *format,
156 static int verbose = TRUE;
157 static int verbose_initted = FALSE;
159 /* things are written a bit oddly here so that
160 * in the non-verbose case we just have the one
161 * conditional and return immediately.
166 if (!verbose_initted)
168 verbose = getenv ("DBUS_VERBOSE") != NULL;
169 verbose_initted = TRUE;
174 fprintf (stderr, "%lu: ", (unsigned long) getpid ());
176 va_start (args, format);
177 vfprintf (stderr, format, args);
184 fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session] [--exit-with-x11]\n");
191 printf ("D-Bus Message Bus Launcher %s\n"
192 "Copyright (C) 2003 Red Hat, Inc.\n"
193 "This is free software; see the source for copying conditions.\n"
194 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
200 xstrdup (const char *str)
210 copy = malloc (len + 1);
214 memcpy (copy, str, len + 1);
221 READ_STATUS_OK, /**< Read succeeded */
222 READ_STATUS_ERROR, /**< Some kind of error */
223 READ_STATUS_EOF /**< EOF returned */
234 memset (buf, '\0', maxlen);
235 maxlen -= 1; /* ensure nul term */
237 retval = READ_STATUS_OK;
245 to_read = maxlen - bytes;
253 if (chunk < 0 && errno == EINTR)
258 retval = READ_STATUS_ERROR;
263 retval = READ_STATUS_EOF;
270 if (retval == READ_STATUS_EOF &&
272 retval = READ_STATUS_OK;
275 if (retval != READ_STATUS_ERROR &&
277 buf[bytes-1] == '\n')
290 retval = READ_STATUS_OK;
298 to_read = sizeof (pid_t) - bytes;
304 ((char*)buf) + bytes,
306 if (chunk < 0 && errno == EINTR)
311 retval = READ_STATUS_ERROR;
316 retval = READ_STATUS_EOF;
327 do_write (int fd, const void *buf, size_t count)
329 size_t bytes_written;
336 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
344 fprintf (stderr, "Failed to write data to pipe! %s\n",
346 exit (1); /* give up, we suck */
350 bytes_written += ret;
352 if (bytes_written < count)
360 do_write (fd, &pid, sizeof (pid));
364 do_waitpid (pid_t pid)
369 ret = waitpid (pid, NULL, 0);
378 static pid_t bus_pid_to_kill = -1;
383 verbose ("Killing message bus and exiting babysitter\n");
384 kill (bus_pid_to_kill, SIGTERM);
386 kill (bus_pid_to_kill, SIGKILL);
390 kill_bus_and_exit (int exitcode)
392 /* in case these point to any NFS mounts, get rid of them immediately */
403 print_variables (const char *bus_address, pid_t bus_pid, long bus_wid,
404 int c_shell_syntax, int bourne_shell_syntax,
409 do_write (1, bus_address, strlen (bus_address) + 1);
410 do_write (1, &bus_pid, sizeof bus_pid);
411 do_write (1, &bus_wid, sizeof bus_wid);
414 else if (c_shell_syntax)
416 printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s';\n", bus_address);
417 printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
419 printf ("set DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
422 else if (bourne_shell_syntax)
424 printf ("DBUS_SESSION_BUS_ADDRESS='%s';\n", bus_address);
425 printf ("export DBUS_SESSION_BUS_ADDRESS;\n");
426 printf ("DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
428 printf ("DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
433 printf ("DBUS_SESSION_BUS_ADDRESS=%s\n", bus_address);
434 printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
436 printf ("DBUS_SESSION_BUS_WINDOWID=%ld\n", (long) bus_wid);
441 static int got_sighup = FALSE;
444 signal_handler (int sig)
459 kill_bus_when_session_ends (void)
465 struct sigaction act;
468 /* install SIGHUP handler */
470 sigemptyset (&empty_mask);
471 act.sa_handler = signal_handler;
472 act.sa_mask = empty_mask;
474 sigaction (SIGHUP, &act, NULL);
475 sigaction (SIGTERM, &act, NULL);
476 sigaction (SIGINT, &act, NULL);
478 #ifdef DBUS_BUILD_X11
480 if (xdisplay != NULL)
482 x_fd = ConnectionNumber (xdisplay);
497 verbose ("session lifetime is defined by X, not monitoring stdin\n");
500 else if (tty_fd >= 0)
502 verbose ("stdin isatty(), monitoring it\n");
506 verbose ("stdin was not a TTY, not monitoring it\n");
509 if (tty_fd < 0 && x_fd < 0)
511 fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
517 #ifdef DBUS_BUILD_X11
518 /* Dump events on the floor, and let
519 * IO error handler run if we lose
520 * the X connection. It's important to
521 * run this before going into select() since
522 * we might have queued outgoing messages or
533 FD_SET (tty_fd, &read_set);
534 FD_SET (tty_fd, &err_set);
539 FD_SET (x_fd, &read_set);
540 FD_SET (x_fd, &err_set);
543 select (MAX (tty_fd, x_fd) + 1,
544 &read_set, NULL, &err_set, NULL);
548 verbose ("Got SIGHUP, exiting\n");
549 kill_bus_and_exit (0);
552 #ifdef DBUS_BUILD_X11
553 /* Events will be processed before we select again
556 verbose ("X fd condition reading = %d error = %d\n",
557 FD_ISSET (x_fd, &read_set),
558 FD_ISSET (x_fd, &err_set));
563 if (FD_ISSET (tty_fd, &read_set))
568 verbose ("TTY ready for reading\n");
570 bytes_read = read (tty_fd, discard, sizeof (discard));
572 verbose ("Read %d bytes from TTY errno = %d\n",
576 kill_bus_and_exit (0); /* EOF */
577 else if (bytes_read < 0 && errno != EINTR)
579 /* This shouldn't happen I don't think; to avoid
580 * spinning on the fd forever we exit.
582 fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
584 kill_bus_and_exit (0);
587 else if (FD_ISSET (tty_fd, &err_set))
589 verbose ("TTY has error condition\n");
591 kill_bus_and_exit (0);
598 babysit (int exit_with_session,
600 int read_bus_pid_fd) /* read pid from here */
606 verbose ("babysitting, exit_with_session = %d, child_pid = %ld, read_bus_pid_fd = %d\n",
607 exit_with_session, (long) child_pid, read_bus_pid_fd);
609 /* We chdir ("/") since we are persistent and daemon-like, and fork
610 * again so dbus-launch can reap the parent. However, we don't
611 * setsid() or close fd 0 because the idea is to remain attached
612 * to the tty and the X server in order to kill the message bus
613 * when the session ends.
618 fprintf (stderr, "Could not change to root directory: %s\n",
623 /* Close stdout/stderr so we don't block an "eval" or otherwise
624 * lock up. stdout is still chaining through to dbus-launch
625 * and in turn to the parent shell.
627 dev_null_fd = open ("/dev/null", O_RDWR);
628 if (dev_null_fd >= 0)
630 if (!exit_with_session)
631 dup2 (dev_null_fd, 0);
632 dup2 (dev_null_fd, 1);
633 s = getenv ("DBUS_DEBUG_OUTPUT");
634 if (s == NULL || *s == '\0')
635 dup2 (dev_null_fd, 2);
639 fprintf (stderr, "Failed to open /dev/null: %s\n",
641 /* continue, why not */
648 fprintf (stderr, "fork() failed in babysitter: %s\n",
655 /* Parent reaps pre-fork part of bus daemon, then exits and is
656 * reaped so the babysitter isn't a zombie
659 verbose ("=== Babysitter's intermediate parent continues again\n");
661 if (do_waitpid (child_pid) < 0)
663 /* shouldn't happen */
664 fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
668 verbose ("Babysitter's intermediate parent exiting\n");
673 /* Child continues */
674 verbose ("=== Babysitter process created\n");
676 verbose ("Reading PID from bus\n");
678 switch (read_pid (read_bus_pid_fd, &bus_pid_to_kill))
682 case READ_STATUS_EOF:
683 fprintf (stderr, "EOF in dbus-launch reading PID from bus daemon\n");
686 case READ_STATUS_ERROR:
687 fprintf (stderr, "Error in dbus-launch reading PID from bus daemon: %s\n",
693 verbose ("Got PID %ld from daemon\n",
694 (long) bus_pid_to_kill);
696 if (exit_with_session)
698 /* Bus is now started and launcher has needed info;
699 * we connect to X display and tty and wait to
700 * kill bus if requested.
703 kill_bus_when_session_ends ();
706 verbose ("Babysitter exiting\n");
712 do_close_stderr (void)
718 /* dbus-launch is a Unix-only program, so we can rely on /dev/null being there.
719 * We're including unistd.h and we're dealing with sh/csh launch sequences...
721 fd = open ("/dev/null", O_RDWR);
724 fprintf (stderr, "Internal error: cannot open /dev/null: %s", strerror (errno));
729 if (dup2 (fd, 2) == -1)
731 /* error; we can't report an error anymore... */
738 pass_info (const char *runprog, const char *bus_address, pid_t bus_pid,
739 long bus_wid, int c_shell_syntax, int bourne_shell_syntax,
741 int argc, char **argv, int remaining_args)
750 envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") +
751 strlen (bus_address) + 1);
752 args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
754 if (envvar == NULL || args == NULL)
757 args[0] = xstrdup (runprog);
760 for (i = 1; i <= (argc-remaining_args); i++)
762 size_t len = strlen (argv[remaining_args+i-1])+1;
763 args[i] = malloc (len);
766 strncpy (args[i], argv[remaining_args+i-1], len);
770 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
771 strcat (envvar, bus_address);
774 execvp (runprog, args);
775 fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
780 print_variables (bus_address, bus_pid, bus_wid, c_shell_syntax,
781 bourne_shell_syntax, binary_syntax);
783 verbose ("dbus-launch exiting\n");
797 fprintf (stderr, "Out of memory!");
805 main (int argc, char **argv)
807 const char *prev_arg;
809 const char *runprog = NULL;
810 int remaining_args = 0;
811 int exit_with_session;
812 int exit_with_x11 = FALSE;
813 int binary_syntax = FALSE;
814 int c_shell_syntax = FALSE;
815 int bourne_shell_syntax = FALSE;
816 int auto_shell_syntax = FALSE;
817 int autolaunch = FALSE;
818 int requires_arg = FALSE;
819 int close_stderr = FALSE;
822 int bus_pid_to_launcher_pipe[2];
823 int bus_pid_to_babysitter_pipe[2];
824 int bus_address_to_launcher_pipe[2];
827 exit_with_session = FALSE;
834 const char *arg = argv[i];
836 if (strcmp (arg, "--help") == 0 ||
837 strcmp (arg, "-h") == 0 ||
838 strcmp (arg, "-?") == 0)
840 else if (strcmp (arg, "--auto-syntax") == 0)
841 auto_shell_syntax = TRUE;
842 else if (strcmp (arg, "-c") == 0 ||
843 strcmp (arg, "--csh-syntax") == 0)
844 c_shell_syntax = TRUE;
845 else if (strcmp (arg, "-s") == 0 ||
846 strcmp (arg, "--sh-syntax") == 0)
847 bourne_shell_syntax = TRUE;
848 else if (strcmp (arg, "--binary-syntax") == 0)
849 binary_syntax = TRUE;
850 else if (strcmp (arg, "--version") == 0)
852 else if (strcmp (arg, "--exit-with-session") == 0)
853 exit_with_session = TRUE;
854 else if (strcmp (arg, "--exit-with-x11") == 0)
855 exit_with_x11 = TRUE;
856 else if (strcmp (arg, "--close-stderr") == 0)
858 else if (strstr (arg, "--autolaunch=") == arg)
864 fprintf (stderr, "--autolaunch given twice\n");
870 s = strchr (arg, '=');
873 save_machine_uuid (s);
876 strcmp (prev_arg, "--autolaunch") == 0)
880 fprintf (stderr, "--autolaunch given twice\n");
886 save_machine_uuid (arg);
887 requires_arg = FALSE;
889 else if (strcmp (arg, "--autolaunch") == 0)
891 else if (strstr (arg, "--config-file=") == arg)
895 if (config_file != NULL)
897 fprintf (stderr, "--config-file given twice\n");
901 file = strchr (arg, '=');
904 config_file = xstrdup (file);
907 strcmp (prev_arg, "--config-file") == 0)
909 if (config_file != NULL)
911 fprintf (stderr, "--config-file given twice\n");
915 config_file = xstrdup (arg);
916 requires_arg = FALSE;
918 else if (strcmp (arg, "--config-file") == 0)
920 else if (arg[0] == '-')
922 if (strcmp (arg, "--") != 0)
924 fprintf (stderr, "Option `%s' is unknown.\n", arg);
930 remaining_args = i+2;
937 remaining_args = i+1;
947 fprintf (stderr, "Option `%s' requires an argument.\n", prev_arg);
951 if (auto_shell_syntax)
953 if ((shname = getenv ("SHELL")) != NULL)
955 if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
956 c_shell_syntax = TRUE;
958 bourne_shell_syntax = TRUE;
961 bourne_shell_syntax = TRUE;
964 if (exit_with_session)
965 verbose ("--exit-with-session enabled\n");
968 verbose ("--exit-with-x11 enabled\n");
972 #ifndef DBUS_BUILD_X11
973 fprintf (stderr, "Autolaunch requested, but X11 support not compiled in.\n"
974 "Cannot continue.\n");
976 #else /* DBUS_BUILD_X11 */
977 #ifndef DBUS_ENABLE_X11_AUTOLAUNCH
978 fprintf (stderr, "X11 autolaunch support disabled at compile time.\n");
980 #else /* DBUS_ENABLE_X11_AUTOLAUNCH */
985 if (get_machine_uuid () == NULL)
987 fprintf (stderr, "Machine UUID not provided as arg to --autolaunch\n");
991 verbose ("Autolaunch enabled (using X11).\n");
994 verbose ("--exit-with-x11 automatically enabled\n");
995 exit_with_x11 = TRUE;
1000 fprintf (stderr, "Autolaunch error: X11 initialization failed.\n");
1004 if (!x11_get_address (&address, &pid, &wid))
1006 fprintf (stderr, "Autolaunch error: X11 communication error.\n");
1010 if (address != NULL)
1012 verbose ("dbus-daemon is already running. Returning existing parameters.\n");
1013 pass_info (runprog, address, pid, wid, c_shell_syntax,
1014 bourne_shell_syntax, binary_syntax, argc, argv, remaining_args);
1017 #endif /* DBUS_ENABLE_X11_AUTOLAUNCH */
1018 #endif /* DBUS_BUILD_X11 */
1020 else if (exit_with_x11)
1022 #ifndef DBUS_BUILD_X11
1023 fprintf (stderr, "Session lifetime based on X11 requested, but X11 support not compiled in.\n");
1025 #else /* DBUS_BUILD_X11 */
1028 fprintf (stderr, "Session lifetime based on X11 requested, but X11 initialization failed.\n");
1031 #endif /* DBUS_BUILD_X11 */
1033 #ifdef DBUS_BUILD_X11
1034 else if (read_machine_uuid_if_needed())
1038 #endif /* DBUS_BUILD_X11 */
1041 if (pipe (bus_pid_to_launcher_pipe) < 0 ||
1042 pipe (bus_address_to_launcher_pipe) < 0 ||
1043 pipe (bus_pid_to_babysitter_pipe) < 0)
1046 "Failed to create pipe: %s\n",
1054 fprintf (stderr, "Failed to fork: %s\n",
1062 #define MAX_FD_LEN 64
1063 char write_pid_fd_as_string[MAX_FD_LEN];
1064 char write_address_fd_as_string[MAX_FD_LEN];
1066 #ifdef DBUS_BUILD_X11
1073 verbose ("=== Babysitter's intermediate parent created\n");
1075 /* Fork once more to create babysitter */
1080 fprintf (stderr, "Failed to fork: %s\n",
1088 verbose ("=== Babysitter's intermediate parent continues\n");
1090 close (bus_pid_to_launcher_pipe[READ_END]);
1091 close (bus_pid_to_launcher_pipe[WRITE_END]);
1092 close (bus_address_to_launcher_pipe[READ_END]);
1093 close (bus_address_to_launcher_pipe[WRITE_END]);
1094 close (bus_pid_to_babysitter_pipe[WRITE_END]);
1096 /* babysit() will fork *again*
1097 * and will also reap the pre-forked bus
1100 babysit (exit_with_session || exit_with_x11, ret,
1101 bus_pid_to_babysitter_pipe[READ_END]);
1105 verbose ("=== Bus exec process created\n");
1107 /* Now we are the bus process (well, almost;
1108 * dbus-daemon itself forks again)
1110 close (bus_pid_to_launcher_pipe[READ_END]);
1111 close (bus_address_to_launcher_pipe[READ_END]);
1112 close (bus_pid_to_babysitter_pipe[READ_END]);
1113 close (bus_pid_to_babysitter_pipe[WRITE_END]);
1115 sprintf (write_pid_fd_as_string,
1116 "%d", bus_pid_to_launcher_pipe[WRITE_END]);
1118 sprintf (write_address_fd_as_string,
1119 "%d", bus_address_to_launcher_pipe[WRITE_END]);
1121 verbose ("Calling exec()\n");
1123 #ifdef DBUS_BUILD_TESTS
1124 /* exec from testdir */
1125 if (getenv("DBUS_USE_TEST_BINARY") != NULL)
1127 execl (TEST_BUS_BINARY,
1130 "--print-pid", write_pid_fd_as_string,
1131 "--print-address", write_address_fd_as_string,
1132 config_file ? "--config-file" : "--session",
1133 config_file, /* has to be last in this varargs list */
1137 "Failed to execute test message bus daemon %s: %s. Will try again with the system path.\n",
1138 TEST_BUS_BINARY, strerror (errno));
1140 #endif /* DBUS_BUILD_TESTS */
1142 execl (DBUS_DAEMONDIR"/dbus-daemon",
1143 DBUS_DAEMONDIR"/dbus-daemon",
1145 "--print-pid", write_pid_fd_as_string,
1146 "--print-address", write_address_fd_as_string,
1147 config_file ? "--config-file" : "--session",
1148 config_file, /* has to be last in this varargs list */
1152 "Failed to execute message bus daemon %s: %s. Will try again without full path.\n",
1153 DBUS_DAEMONDIR"/dbus-daemon", strerror (errno));
1156 * If it failed, try running without full PATH. Note this is needed
1157 * because the build process builds the run-with-tmp-session-bus.conf
1158 * file and the dbus-daemon will not be in the install location during
1161 execlp ("dbus-daemon",
1164 "--print-pid", write_pid_fd_as_string,
1165 "--print-address", write_address_fd_as_string,
1166 config_file ? "--config-file" : "--session",
1167 config_file, /* has to be last in this varargs list */
1171 "Failed to execute message bus daemon: %s\n",
1178 #define MAX_PID_LEN 64
1180 char bus_address[MAX_ADDR_LEN];
1181 char buf[MAX_PID_LEN];
1187 verbose ("=== Parent dbus-launch continues\n");
1189 close (bus_pid_to_launcher_pipe[WRITE_END]);
1190 close (bus_address_to_launcher_pipe[WRITE_END]);
1191 close (bus_pid_to_babysitter_pipe[READ_END]);
1193 verbose ("Waiting for babysitter's intermediate parent\n");
1195 /* Immediately reap parent of babysitter
1196 * (which was created just for us to reap)
1198 if (do_waitpid (ret) < 0)
1200 fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
1205 verbose ("Reading address from bus\n");
1207 /* Read the pipe data, print, and exit */
1208 switch (read_line (bus_address_to_launcher_pipe[READ_END],
1209 bus_address, MAX_ADDR_LEN))
1211 case READ_STATUS_OK:
1213 case READ_STATUS_EOF:
1214 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
1217 case READ_STATUS_ERROR:
1218 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
1224 close (bus_address_to_launcher_pipe[READ_END]);
1226 verbose ("Reading PID from daemon\n");
1228 switch (read_line (bus_pid_to_launcher_pipe[READ_END], buf, MAX_PID_LEN))
1230 case READ_STATUS_OK:
1232 case READ_STATUS_EOF:
1233 fprintf (stderr, "EOF reading PID from bus daemon\n");
1236 case READ_STATUS_ERROR:
1237 fprintf (stderr, "Error reading PID from bus daemon: %s\n",
1244 val = strtol (buf, &end, 0);
1245 if (buf == end || end == NULL)
1247 fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
1248 buf, strerror (errno));
1254 close (bus_pid_to_launcher_pipe[READ_END]);
1256 #ifdef DBUS_ENABLE_X11_AUTOLAUNCH
1257 if (xdisplay != NULL)
1259 verbose("Saving x11 address\n");
1260 ret2 = x11_save_address (bus_address, bus_pid, &wid);
1261 /* Only get an existing dbus session when autolaunching */
1266 char *address = NULL;
1267 /* another window got added. Return its address */
1268 bus_pid_to_kill = bus_pid;
1269 if (x11_get_address (&address, &bus_pid, &wid)
1272 verbose ("dbus-daemon is already running. Returning existing parameters.\n");
1273 /* Kill the old bus */
1275 pass_info (runprog, address, bus_pid, wid,
1276 c_shell_syntax, bourne_shell_syntax, binary_syntax,
1277 argc, argv, remaining_args);
1282 fprintf (stderr, "Error saving bus information.\n");
1283 bus_pid_to_kill = bus_pid;
1284 kill_bus_and_exit (1);
1290 /* Forward the pid to the babysitter */
1291 write_pid (bus_pid_to_babysitter_pipe[WRITE_END], bus_pid);
1292 close (bus_pid_to_babysitter_pipe[WRITE_END]);
1294 pass_info (runprog, bus_address, bus_pid, wid, c_shell_syntax,
1295 bourne_shell_syntax, binary_syntax, argc, argv, remaining_args);