1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-launch.c dbus-launch utility
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-launch.h"
34 #include <sys/select.h>
39 extern Display *xdisplay;
43 verbose (const char *format,
47 static int verbose = TRUE;
48 static int verbose_initted = FALSE;
50 /* things are written a bit oddly here so that
51 * in the non-verbose case we just have the one
52 * conditional and return immediately.
59 verbose = getenv ("DBUS_VERBOSE") != NULL;
60 verbose_initted = TRUE;
65 fprintf (stderr, "%lu: ", (unsigned long) getpid ());
67 va_start (args, format);
68 vfprintf (stderr, format, args);
75 fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session]\n");
82 printf ("D-Bus Message Bus Launcher %s\n"
83 "Copyright (C) 2003 Red Hat, Inc.\n"
84 "This is free software; see the source for copying conditions.\n"
85 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
91 xstrdup (const char *str)
101 copy = malloc (len + 1);
105 memcpy (copy, str, len + 1);
112 READ_STATUS_OK, /**< Read succeeded */
113 READ_STATUS_ERROR, /**< Some kind of error */
114 READ_STATUS_EOF /**< EOF returned */
125 memset (buf, '\0', maxlen);
126 maxlen -= 1; /* ensure nul term */
128 retval = READ_STATUS_OK;
136 to_read = maxlen - bytes;
144 if (chunk < 0 && errno == EINTR)
149 retval = READ_STATUS_ERROR;
154 retval = READ_STATUS_EOF;
161 if (retval == READ_STATUS_EOF &&
163 retval = READ_STATUS_OK;
166 if (retval != READ_STATUS_ERROR &&
168 buf[bytes-1] == '\n')
181 retval = READ_STATUS_OK;
189 to_read = sizeof (pid_t) - bytes;
195 ((char*)buf) + bytes,
197 if (chunk < 0 && errno == EINTR)
202 retval = READ_STATUS_ERROR;
207 retval = READ_STATUS_EOF;
218 do_write (int fd, const void *buf, size_t count)
220 size_t bytes_written;
227 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
235 fprintf (stderr, "Failed to write data to pipe! %s\n",
237 exit (1); /* give up, we suck */
241 bytes_written += ret;
243 if (bytes_written < count)
251 do_write (fd, &pid, sizeof (pid));
255 do_waitpid (pid_t pid)
260 ret = waitpid (pid, NULL, 0);
269 static pid_t bus_pid_to_kill = -1;
272 kill_bus_and_exit (int exitcode)
274 verbose ("Killing message bus and exiting babysitter\n");
276 /* in case these point to any NFS mounts, get rid of them immediately */
281 kill (bus_pid_to_kill, SIGTERM);
283 kill (bus_pid_to_kill, SIGKILL);
289 print_variables (const char *bus_address, pid_t bus_pid, long bus_wid,
290 int c_shell_syntax, int bourne_shell_syntax,
295 write (1, bus_address, strlen (bus_address) + 1);
296 write (1, &bus_pid, sizeof bus_pid);
297 write (1, &bus_wid, sizeof bus_wid);
300 else if (c_shell_syntax)
302 printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s';\n", bus_address);
303 printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
305 printf ("set DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
309 printf ("DBUS_SESSION_BUS_ADDRESS='%s';\n", bus_address);
310 if (bourne_shell_syntax)
311 printf ("export DBUS_SESSION_BUS_ADDRESS;\n");
312 printf ("DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
314 printf ("DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
318 static int got_sighup = FALSE;
321 signal_handler (int sig)
333 kill_bus_when_session_ends (void)
339 struct sigaction act;
342 /* install SIGHUP handler */
344 sigemptyset (&empty_mask);
345 act.sa_handler = signal_handler;
346 act.sa_mask = empty_mask;
348 sigaction (SIGHUP, &act, NULL);
349 sigaction (SIGTERM, &act, NULL);
351 #ifdef DBUS_BUILD_X11
353 if (xdisplay != NULL)
355 x_fd = ConnectionNumber (xdisplay);
369 verbose ("stdin isatty(), monitoring it\n");
371 verbose ("stdin was not a TTY, not monitoring it\n");
373 if (tty_fd < 0 && x_fd < 0)
375 fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
386 FD_SET (tty_fd, &read_set);
387 FD_SET (tty_fd, &err_set);
392 FD_SET (x_fd, &read_set);
393 FD_SET (x_fd, &err_set);
396 select (MAX (tty_fd, x_fd) + 1,
397 &read_set, NULL, &err_set, NULL);
401 verbose ("Got SIGHUP, exiting\n");
402 kill_bus_and_exit (0);
405 #ifdef DBUS_BUILD_X11
406 /* Dump events on the floor, and let
407 * IO error handler run if we lose
411 verbose ("X fd condition reading = %d error = %d\n",
412 FD_ISSET (x_fd, &read_set),
413 FD_ISSET (x_fd, &err_set));
419 if (FD_ISSET (tty_fd, &read_set))
424 verbose ("TTY ready for reading\n");
426 bytes_read = read (tty_fd, discard, sizeof (discard));
428 verbose ("Read %d bytes from TTY errno = %d\n",
432 kill_bus_and_exit (0); /* EOF */
433 else if (bytes_read < 0 && errno != EINTR)
435 /* This shouldn't happen I don't think; to avoid
436 * spinning on the fd forever we exit.
438 fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
440 kill_bus_and_exit (0);
443 else if (FD_ISSET (tty_fd, &err_set))
445 verbose ("TTY has error condition\n");
447 kill_bus_and_exit (0);
454 babysit (int exit_with_session,
456 int read_bus_pid_fd) /* read pid from here */
462 verbose ("babysitting, exit_with_session = %d, child_pid = %ld, read_bus_pid_fd = %d\n",
463 exit_with_session, (long) child_pid, read_bus_pid_fd);
465 /* We chdir ("/") since we are persistent and daemon-like, and fork
466 * again so dbus-launch can reap the parent. However, we don't
467 * setsid() or close fd 0 because the idea is to remain attached
468 * to the tty and the X server in order to kill the message bus
469 * when the session ends.
474 fprintf (stderr, "Could not change to root directory: %s\n",
479 /* Close stdout/stderr so we don't block an "eval" or otherwise
480 * lock up. stdout is still chaining through to dbus-launch
481 * and in turn to the parent shell.
483 dev_null_fd = open ("/dev/null", O_RDWR);
484 if (dev_null_fd >= 0)
486 if (!exit_with_session)
487 dup2 (dev_null_fd, 0);
488 dup2 (dev_null_fd, 1);
489 s = getenv ("DBUS_DEBUG_OUTPUT");
490 if (s == NULL || *s == '\0')
491 dup2 (dev_null_fd, 2);
495 fprintf (stderr, "Failed to open /dev/null: %s\n",
497 /* continue, why not */
504 fprintf (stderr, "fork() failed in babysitter: %s\n",
511 /* Parent reaps pre-fork part of bus daemon, then exits and is
512 * reaped so the babysitter isn't a zombie
515 verbose ("=== Babysitter's intermediate parent continues again\n");
517 if (do_waitpid (child_pid) < 0)
519 /* shouldn't happen */
520 fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
524 verbose ("Babysitter's intermediate parent exiting\n");
529 /* Child continues */
530 verbose ("=== Babysitter process created\n");
532 verbose ("Reading PID from bus\n");
534 switch (read_pid (read_bus_pid_fd, &bus_pid_to_kill))
538 case READ_STATUS_EOF:
539 fprintf (stderr, "EOF in dbus-launch reading PID from bus daemon\n");
542 case READ_STATUS_ERROR:
543 fprintf (stderr, "Error in dbus-launch reading PID from bus daemon: %s\n",
549 verbose ("Got PID %ld from daemon\n",
550 (long) bus_pid_to_kill);
552 if (exit_with_session)
554 /* Bus is now started and launcher has needed info;
555 * we connect to X display and tty and wait to
556 * kill bus if requested.
559 kill_bus_when_session_ends ();
562 verbose ("Babysitter exiting\n");
571 main (int argc, char **argv)
573 const char *prev_arg;
575 const char *runprog = NULL;
576 int remaining_args = 0;
577 int exit_with_session;
578 int binary_syntax = FALSE;
579 int c_shell_syntax = FALSE;
580 int bourne_shell_syntax = FALSE;
581 int auto_shell_syntax = FALSE;
582 int autolaunch = FALSE;
585 int bus_pid_to_launcher_pipe[2];
586 int bus_pid_to_babysitter_pipe[2];
587 int bus_address_to_launcher_pipe[2];
590 exit_with_session = FALSE;
597 const char *arg = argv[i];
599 if (strcmp (arg, "--help") == 0 ||
600 strcmp (arg, "-h") == 0 ||
601 strcmp (arg, "-?") == 0)
603 else if (strcmp (arg, "--auto-syntax") == 0)
604 auto_shell_syntax = TRUE;
605 else if (strcmp (arg, "-c") == 0 ||
606 strcmp (arg, "--csh-syntax") == 0)
607 c_shell_syntax = TRUE;
608 else if (strcmp (arg, "-s") == 0 ||
609 strcmp (arg, "--sh-syntax") == 0)
610 bourne_shell_syntax = TRUE;
611 else if (strcmp (arg, "--binary-syntax") == 0)
612 binary_syntax = TRUE;
613 else if (strcmp (arg, "--version") == 0)
615 else if (strcmp (arg, "--exit-with-session") == 0)
616 exit_with_session = TRUE;
617 else if (strcmp (arg, "--autolaunch") == 0)
619 else if (strstr (arg, "--config-file=") == arg)
623 if (config_file != NULL)
625 fprintf (stderr, "--config-file given twice\n");
629 file = strchr (arg, '=');
632 config_file = xstrdup (file);
635 strcmp (prev_arg, "--config-file") == 0)
637 if (config_file != NULL)
639 fprintf (stderr, "--config-file given twice\n");
643 config_file = xstrdup (arg);
645 else if (strcmp (arg, "--config-file") == 0)
646 ; /* wait for next arg */
650 remaining_args = i+1;
659 if (auto_shell_syntax)
661 if ((shname = getenv ("SHELL")) != NULL)
663 if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
664 c_shell_syntax = TRUE;
666 bourne_shell_syntax = TRUE;
669 bourne_shell_syntax = TRUE;
672 if (exit_with_session)
673 verbose ("--exit-with-session enabled\n");
677 #ifndef DBUS_BUILD_X11
678 fprintf (stderr, "Autolaunch requested, but X11 support not compiled in.\n"
679 "Cannot continue.\n");
686 verbose ("Autolaunch enabled (using X11).\n");
687 if (!exit_with_session)
689 verbose ("--exit-with-session automatically enabled\n");
690 exit_with_session = TRUE;
695 fprintf (stderr, "Autolaunch error: X11 initialization failed.\n");
699 if (!x11_get_address (&address, &pid, &wid))
701 fprintf (stderr, "Autolaunch error: X11 communication error.\n");
707 verbose ("dbus-daemon is already running. Returning existing parameters.\n");
708 print_variables (address, pid, wid, c_shell_syntax,
709 bourne_shell_syntax, binary_syntax);
715 if (pipe (bus_pid_to_launcher_pipe) < 0 ||
716 pipe (bus_address_to_launcher_pipe) < 0 ||
717 pipe (bus_pid_to_babysitter_pipe) < 0)
720 "Failed to create pipe: %s\n",
728 fprintf (stderr, "Failed to fork: %s\n",
736 #define MAX_FD_LEN 64
737 char write_pid_fd_as_string[MAX_FD_LEN];
738 char write_address_fd_as_string[MAX_FD_LEN];
740 verbose ("=== Babysitter's intermediate parent created\n");
742 /* Fork once more to create babysitter */
747 fprintf (stderr, "Failed to fork: %s\n",
755 verbose ("=== Babysitter's intermediate parent continues\n");
757 close (bus_pid_to_launcher_pipe[READ_END]);
758 close (bus_pid_to_launcher_pipe[WRITE_END]);
759 close (bus_address_to_launcher_pipe[READ_END]);
760 close (bus_address_to_launcher_pipe[WRITE_END]);
761 close (bus_pid_to_babysitter_pipe[WRITE_END]);
763 /* babysit() will fork *again*
764 * and will also reap the pre-forked bus
767 babysit (exit_with_session, ret,
768 bus_pid_to_babysitter_pipe[READ_END]);
772 verbose ("=== Bus exec process created\n");
774 /* Now we are the bus process (well, almost;
775 * dbus-daemon itself forks again)
777 close (bus_pid_to_launcher_pipe[READ_END]);
778 close (bus_address_to_launcher_pipe[READ_END]);
779 close (bus_pid_to_babysitter_pipe[READ_END]);
780 close (bus_pid_to_babysitter_pipe[WRITE_END]);
782 sprintf (write_pid_fd_as_string,
783 "%d", bus_pid_to_launcher_pipe[WRITE_END]);
785 sprintf (write_address_fd_as_string,
786 "%d", bus_address_to_launcher_pipe[WRITE_END]);
788 verbose ("Calling exec()\n");
790 execl (DBUS_DAEMONDIR"/dbus-daemon",
791 DBUS_DAEMONDIR"/dbus-daemon",
793 "--print-pid", write_pid_fd_as_string,
794 "--print-address", write_address_fd_as_string,
795 config_file ? "--config-file" : "--session",
796 config_file, /* has to be last in this varargs list */
800 "Failed to execute message bus daemon %s: %s. Will try again without full path.\n",
801 DBUS_DAEMONDIR"/dbus-daemon", strerror (errno));
804 * If it failed, try running without full PATH. Note this is needed
805 * because the build process builds the run-with-tmp-session-bus.conf
806 * file and the dbus-daemon will not be in the install location during
809 execlp ("dbus-daemon",
812 "--print-pid", write_pid_fd_as_string,
813 "--print-address", write_address_fd_as_string,
814 config_file ? "--config-file" : "--session",
815 config_file, /* has to be last in this varargs list */
819 "Failed to execute message bus daemon: %s\n",
826 #define MAX_PID_LEN 64
828 char bus_address[MAX_ADDR_LEN];
829 char buf[MAX_PID_LEN];
835 verbose ("=== Parent dbus-launch continues\n");
837 close (bus_pid_to_launcher_pipe[WRITE_END]);
838 close (bus_address_to_launcher_pipe[WRITE_END]);
839 close (bus_pid_to_babysitter_pipe[READ_END]);
841 verbose ("Waiting for babysitter's intermediate parent\n");
843 /* Immediately reap parent of babysitter
844 * (which was created just for us to reap)
846 if (do_waitpid (ret) < 0)
848 fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
853 verbose ("Reading address from bus\n");
855 /* Read the pipe data, print, and exit */
856 switch (read_line (bus_address_to_launcher_pipe[READ_END],
857 bus_address, MAX_ADDR_LEN))
861 case READ_STATUS_EOF:
862 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
865 case READ_STATUS_ERROR:
866 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
872 close (bus_address_to_launcher_pipe[READ_END]);
874 verbose ("Reading PID from daemon\n");
876 switch (read_line (bus_pid_to_launcher_pipe[READ_END], buf, MAX_PID_LEN))
880 case READ_STATUS_EOF:
881 fprintf (stderr, "EOF reading PID from bus daemon\n");
884 case READ_STATUS_ERROR:
885 fprintf (stderr, "Error reading PID from bus daemon: %s\n",
892 val = strtol (buf, &end, 0);
893 if (buf == end || end == NULL)
895 fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
896 buf, strerror (errno));
902 close (bus_pid_to_launcher_pipe[READ_END]);
904 #ifdef DBUS_BUILD_X11
905 ret2 = x11_save_address (bus_address, bus_pid, &wid);
908 /* another window got added. Return its address */
913 if (x11_get_address (&address, &pid, &wid) && address != NULL)
915 verbose ("dbus-daemon is already running. Returning existing parameters.\n");
916 print_variables (address, pid, wid, c_shell_syntax,
917 bourne_shell_syntax, binary_syntax);
920 bus_pid_to_kill = bus_pid;
921 kill_bus_and_exit (0);
924 /* if failed, fall through */
928 fprintf (stderr, "Error saving bus information.\n");
929 bus_pid_to_kill = bus_pid;
930 kill_bus_and_exit (1);
934 /* Forward the pid to the babysitter */
935 write_pid (bus_pid_to_babysitter_pipe[WRITE_END], bus_pid);
936 close (bus_pid_to_babysitter_pipe[WRITE_END]);
943 envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") + strlen (bus_address) + 1);
944 args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
946 if (envvar == NULL || args == NULL)
949 args[0] = xstrdup (runprog);
952 for (i = 1; i <= (argc-remaining_args); i++)
954 size_t len = strlen (argv[remaining_args+i-1])+1;
955 args[i] = malloc (len);
958 strncpy (args[i], argv[remaining_args+i-1], len);
962 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
963 strcat (envvar, bus_address);
966 execvp (runprog, args);
967 fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
972 print_variables (bus_address, bus_pid, wid, c_shell_syntax,
973 bourne_shell_syntax, binary_syntax);
976 verbose ("dbus-launch exiting\n");
988 fprintf (stderr, "Out of memory!");