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
34 #include <sys/select.h>
48 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
51 verbose (const char *format,
55 static int verbose = TRUE;
56 static int verbose_initted = FALSE;
58 /* things are written a bit oddly here so that
59 * in the non-verbose case we just have the one
60 * conditional and return immediately.
67 verbose = getenv ("DBUS_VERBOSE") != NULL;
68 verbose_initted = TRUE;
73 fprintf (stderr, "%lu: ", (unsigned long) getpid ());
75 va_start (args, format);
76 vfprintf (stderr, format, args);
83 fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session]\n");
90 printf ("D-Bus Message Bus Launcher %s\n"
91 "Copyright (C) 2003 Red Hat, Inc.\n"
92 "This is free software; see the source for copying conditions.\n"
93 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
99 xstrdup (const char *str)
109 copy = malloc (len + 1);
113 memcpy (copy, str, len + 1);
120 READ_STATUS_OK, /**< Read succeeded */
121 READ_STATUS_ERROR, /**< Some kind of error */
122 READ_STATUS_EOF /**< EOF returned */
133 memset (buf, '\0', maxlen);
134 maxlen -= 1; /* ensure nul term */
136 retval = READ_STATUS_OK;
144 to_read = maxlen - bytes;
152 if (chunk < 0 && errno == EINTR)
157 retval = READ_STATUS_ERROR;
162 retval = READ_STATUS_EOF;
169 if (retval == READ_STATUS_EOF &&
171 retval = READ_STATUS_OK;
174 if (retval != READ_STATUS_ERROR &&
176 buf[bytes-1] == '\n')
189 retval = READ_STATUS_OK;
197 to_read = sizeof (pid_t) - bytes;
203 ((char*)buf) + bytes,
205 if (chunk < 0 && errno == EINTR)
210 retval = READ_STATUS_ERROR;
215 retval = READ_STATUS_EOF;
226 do_write (int fd, const void *buf, size_t count)
228 size_t bytes_written;
235 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
243 fprintf (stderr, "Failed to write data to pipe! %s\n",
245 exit (1); /* give up, we suck */
249 bytes_written += ret;
251 if (bytes_written < count)
259 do_write (fd, &pid, sizeof (pid));
263 do_waitpid (pid_t pid)
268 ret = waitpid (pid, NULL, 0);
277 static pid_t bus_pid_to_kill = -1;
280 kill_bus_and_exit (void)
282 verbose ("Killing message bus and exiting babysitter\n");
284 /* in case these point to any NFS mounts, get rid of them immediately */
289 kill (bus_pid_to_kill, SIGTERM);
291 kill (bus_pid_to_kill, SIGKILL);
296 #ifdef DBUS_BUILD_X11
298 x_io_error_handler (Display *xdisplay)
300 verbose ("X IO error\n");
301 kill_bus_and_exit ();
306 static int got_sighup = FALSE;
309 signal_handler (int sig)
321 kill_bus_when_session_ends (void)
327 struct sigaction act;
329 #ifdef DBUS_BUILD_X11
333 /* install SIGHUP handler */
335 sigemptyset (&empty_mask);
336 act.sa_handler = signal_handler;
337 act.sa_mask = empty_mask;
339 sigaction (SIGHUP, &act, NULL);
340 sigaction (SIGTERM, &act, NULL);
342 #ifdef DBUS_BUILD_X11
343 xdisplay = XOpenDisplay (NULL);
344 if (xdisplay != NULL)
346 verbose ("Successfully opened X display\n");
347 x_fd = ConnectionNumber (xdisplay);
348 XSetIOErrorHandler (x_io_error_handler);
353 verbose ("Compiled without X11 support\n");
363 verbose ("stdin isatty(), monitoring it\n");
365 verbose ("stdin was not a TTY, not monitoring it\n");
367 if (tty_fd < 0 && x_fd < 0)
369 fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
380 FD_SET (tty_fd, &read_set);
381 FD_SET (tty_fd, &err_set);
386 FD_SET (x_fd, &read_set);
387 FD_SET (x_fd, &err_set);
390 select (MAX (tty_fd, x_fd) + 1,
391 &read_set, NULL, &err_set, NULL);
395 verbose ("Got SIGHUP, exiting\n");
396 kill_bus_and_exit ();
399 #ifdef DBUS_BUILD_X11
400 /* Dump events on the floor, and let
401 * IO error handler run if we lose
405 verbose ("X fd condition reading = %d error = %d\n",
406 FD_ISSET (x_fd, &read_set),
407 FD_ISSET (x_fd, &err_set));
409 if (xdisplay != NULL)
411 while (XPending (xdisplay))
414 XNextEvent (xdisplay, &ignored);
421 if (FD_ISSET (tty_fd, &read_set))
426 verbose ("TTY ready for reading\n");
428 bytes_read = read (tty_fd, discard, sizeof (discard));
430 verbose ("Read %d bytes from TTY errno = %d\n",
434 kill_bus_and_exit (); /* EOF */
435 else if (bytes_read < 0 && errno != EINTR)
437 /* This shouldn't happen I don't think; to avoid
438 * spinning on the fd forever we exit.
440 fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
442 kill_bus_and_exit ();
445 else if (FD_ISSET (tty_fd, &err_set))
447 verbose ("TTY has error condition\n");
449 kill_bus_and_exit ();
456 babysit (int exit_with_session,
458 int read_bus_pid_fd, /* read pid from here */
459 int write_bus_pid_fd) /* forward pid to here */
462 #define MAX_PID_LEN 64
463 char buf[MAX_PID_LEN];
469 verbose ("babysitting, exit_with_session = %d, child_pid = %ld, read_bus_pid_fd = %d, write_bus_pid_fd = %d\n",
470 exit_with_session, (long) child_pid, read_bus_pid_fd, write_bus_pid_fd);
472 /* We chdir ("/") since we are persistent and daemon-like, and fork
473 * again so dbus-launch can reap the parent. However, we don't
474 * setsid() or close fd 0 because the idea is to remain attached
475 * to the tty and the X server in order to kill the message bus
476 * when the session ends.
481 fprintf (stderr, "Could not change to root directory: %s\n",
486 /* Close stdout/stderr so we don't block an "eval" or otherwise
487 * lock up. stdout is still chaining through to dbus-launch
488 * and in turn to the parent shell.
490 dev_null_fd = open ("/dev/null", O_RDWR);
491 if (dev_null_fd >= 0)
493 if (!exit_with_session)
494 dup2 (dev_null_fd, 0);
495 dup2 (dev_null_fd, 1);
496 s = getenv ("DBUS_DEBUG_OUTPUT");
497 if (s == NULL || *s == '\0')
498 dup2 (dev_null_fd, 2);
502 fprintf (stderr, "Failed to open /dev/null: %s\n",
504 /* continue, why not */
511 fprintf (stderr, "fork() failed in babysitter: %s\n",
518 /* Parent reaps pre-fork part of bus daemon, then exits and is
519 * reaped so the babysitter isn't a zombie
522 verbose ("=== Babysitter's intermediate parent continues again\n");
524 if (do_waitpid (child_pid) < 0)
526 /* shouldn't happen */
527 fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
531 verbose ("Babysitter's intermediate parent exiting\n");
536 /* Child continues */
537 verbose ("=== Babysitter process created\n");
539 verbose ("Reading PID from daemon\n");
541 switch (read_line (read_bus_pid_fd, buf, MAX_PID_LEN))
545 case READ_STATUS_EOF:
546 fprintf (stderr, "EOF reading PID from bus daemon\n");
549 case READ_STATUS_ERROR:
550 fprintf (stderr, "Error reading PID from bus daemon: %s\n",
557 val = strtol (buf, &end, 0);
558 if (buf == end || end == NULL)
560 fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
561 buf, strerror (errno));
565 bus_pid_to_kill = val;
567 verbose ("Got PID %ld from daemon\n",
568 (long) bus_pid_to_kill);
570 /* Write data to launcher */
571 write_pid (write_bus_pid_fd, bus_pid_to_kill);
572 close (write_bus_pid_fd);
574 if (exit_with_session)
576 /* Bus is now started and launcher has needed info;
577 * we connect to X display and tty and wait to
578 * kill bus if requested.
581 kill_bus_when_session_ends ();
584 verbose ("Babysitter exiting\n");
593 main (int argc, char **argv)
595 const char *prev_arg;
597 const char *runprog = NULL;
598 int remaining_args = 0;
599 int exit_with_session;
600 int c_shell_syntax = FALSE;
601 int bourne_shell_syntax = FALSE;
602 int auto_shell_syntax = FALSE;
605 int bus_pid_to_launcher_pipe[2];
606 int bus_pid_to_babysitter_pipe[2];
607 int bus_address_to_launcher_pipe[2];
610 exit_with_session = FALSE;
617 const char *arg = argv[i];
619 if (strcmp (arg, "--help") == 0 ||
620 strcmp (arg, "-h") == 0 ||
621 strcmp (arg, "-?") == 0)
623 else if (strcmp (arg, "--auto-syntax") == 0)
624 auto_shell_syntax = TRUE;
625 else if (strcmp (arg, "-c") == 0 ||
626 strcmp (arg, "--csh-syntax") == 0)
627 c_shell_syntax = TRUE;
628 else if (strcmp (arg, "-s") == 0 ||
629 strcmp (arg, "--sh-syntax") == 0)
630 bourne_shell_syntax = TRUE;
631 else if (strcmp (arg, "--version") == 0)
633 else if (strcmp (arg, "--exit-with-session") == 0)
634 exit_with_session = TRUE;
635 else if (strstr (arg, "--config-file=") == arg)
639 if (config_file != NULL)
641 fprintf (stderr, "--config-file given twice\n");
645 file = strchr (arg, '=');
648 config_file = xstrdup (file);
651 strcmp (prev_arg, "--config-file") == 0)
653 if (config_file != NULL)
655 fprintf (stderr, "--config-file given twice\n");
659 config_file = xstrdup (arg);
661 else if (strcmp (arg, "--config-file") == 0)
662 ; /* wait for next arg */
666 remaining_args = i+1;
675 if (exit_with_session)
676 verbose ("--exit-with-session enabled\n");
678 if (auto_shell_syntax)
680 if ((shname = getenv ("SHELL")) != NULL)
682 if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
683 c_shell_syntax = TRUE;
685 bourne_shell_syntax = TRUE;
688 bourne_shell_syntax = TRUE;
691 if (pipe (bus_pid_to_launcher_pipe) < 0 ||
692 pipe (bus_address_to_launcher_pipe) < 0)
695 "Failed to create pipe: %s\n",
700 bus_pid_to_babysitter_pipe[READ_END] = -1;
701 bus_pid_to_babysitter_pipe[WRITE_END] = -1;
706 fprintf (stderr, "Failed to fork: %s\n",
714 #define MAX_FD_LEN 64
715 char write_pid_fd_as_string[MAX_FD_LEN];
716 char write_address_fd_as_string[MAX_FD_LEN];
718 verbose ("=== Babysitter's intermediate parent created\n");
720 /* Fork once more to create babysitter */
722 if (pipe (bus_pid_to_babysitter_pipe) < 0)
725 "Failed to create pipe: %s\n",
733 fprintf (stderr, "Failed to fork: %s\n",
741 verbose ("=== Babysitter's intermediate parent continues\n");
743 close (bus_pid_to_launcher_pipe[READ_END]);
744 close (bus_address_to_launcher_pipe[READ_END]);
745 close (bus_address_to_launcher_pipe[WRITE_END]);
746 close (bus_pid_to_babysitter_pipe[WRITE_END]);
748 /* babysit() will fork *again*
749 * and will also reap the pre-forked bus
752 babysit (exit_with_session, ret,
753 bus_pid_to_babysitter_pipe[READ_END],
754 bus_pid_to_launcher_pipe[WRITE_END]);
758 verbose ("=== Bus exec process created\n");
760 /* Now we are the bus process (well, almost;
761 * dbus-daemon itself forks again)
763 close (bus_pid_to_launcher_pipe[READ_END]);
764 close (bus_address_to_launcher_pipe[READ_END]);
765 close (bus_pid_to_babysitter_pipe[READ_END]);
766 close (bus_pid_to_launcher_pipe[WRITE_END]);
768 sprintf (write_pid_fd_as_string,
769 "%d", bus_pid_to_babysitter_pipe[WRITE_END]);
771 sprintf (write_address_fd_as_string,
772 "%d", bus_address_to_launcher_pipe[WRITE_END]);
774 verbose ("Calling exec()\n");
776 execl (DBUS_DAEMONDIR"/dbus-daemon",
777 DBUS_DAEMONDIR"/dbus-daemon",
779 "--print-pid", write_pid_fd_as_string,
780 "--print-address", write_address_fd_as_string,
781 config_file ? "--config-file" : "--session",
782 config_file, /* has to be last in this varargs list */
786 "Failed to execute message bus daemon %s: %s. Will try again without full path.\n",
787 DBUS_DAEMONDIR"/dbus-daemon", strerror (errno));
790 * If it failed, try running without full PATH. Note this is needed
791 * because the build process builds the run-with-tmp-session-bus.conf
792 * file and the dbus-daemon will not be in the install location during
795 execlp ("dbus-daemon",
798 "--print-pid", write_pid_fd_as_string,
799 "--print-address", write_address_fd_as_string,
800 config_file ? "--config-file" : "--session",
801 config_file, /* has to be last in this varargs list */
805 "Failed to execute message bus daemon: %s\n",
812 #define MAX_ADDR_LEN 512
814 char bus_address[MAX_ADDR_LEN];
816 verbose ("=== Parent dbus-launch continues\n");
818 close (bus_pid_to_launcher_pipe[WRITE_END]);
819 close (bus_address_to_launcher_pipe[WRITE_END]);
821 verbose ("Waiting for babysitter's intermediate parent\n");
823 /* Immediately reap parent of babysitter
824 * (which was created just for us to reap)
826 if (do_waitpid (ret) < 0)
828 fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
833 verbose ("Reading address from bus\n");
835 /* Read the pipe data, print, and exit */
836 switch (read_line (bus_address_to_launcher_pipe[READ_END],
837 bus_address, MAX_ADDR_LEN))
841 case READ_STATUS_EOF:
842 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
845 case READ_STATUS_ERROR:
846 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
852 close (bus_address_to_launcher_pipe[READ_END]);
854 verbose ("Reading PID from babysitter\n");
856 switch (read_pid (bus_pid_to_launcher_pipe[READ_END], &bus_pid))
860 case READ_STATUS_EOF:
861 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
864 case READ_STATUS_ERROR:
865 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
871 close (bus_pid_to_launcher_pipe[READ_END]);
878 envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") + strlen (bus_address) + 1);
879 args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
881 if (envvar == NULL || args == NULL)
884 args[0] = xstrdup (runprog);
887 for (i = 1; i <= (argc-remaining_args); i++)
889 size_t len = strlen (argv[remaining_args+i-1])+1;
890 args[i] = malloc (len);
893 strncpy (args[i], argv[remaining_args+i-1], len);
897 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
898 strcat (envvar, bus_address);
901 execvp (runprog, args);
902 fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
908 printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s';\n", bus_address);
911 printf ("DBUS_SESSION_BUS_ADDRESS='%s';\n", bus_address);
912 if (bourne_shell_syntax)
913 printf ("export DBUS_SESSION_BUS_ADDRESS;\n");
916 printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
918 printf ("DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
921 verbose ("dbus-launch exiting\n");
933 fprintf (stderr, "Out of memory!");