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 1.2
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
47 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
50 verbose (const char *format,
54 static int verbose = TRUE;
55 static int verbose_initted = FALSE;
57 /* things are written a bit oddly here so that
58 * in the non-verbose case we just have the one
59 * conditional and return immediately.
66 verbose = getenv ("DBUS_VERBOSE") != NULL;
67 verbose_initted = TRUE;
72 fprintf (stderr, "%lu: ", (unsigned long) getpid ());
74 va_start (args, format);
75 vfprintf (stderr, format, args);
82 fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session]\n");
89 printf ("D-BUS Message Bus Launcher %s\n"
90 "Copyright (C) 2003 Red Hat, Inc.\n"
91 "This is free software; see the source for copying conditions.\n"
92 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
98 xstrdup (const char *str)
108 copy = malloc (len + 1);
112 memcpy (copy, str, len + 1);
119 READ_STATUS_OK, /**< Read succeeded */
120 READ_STATUS_ERROR, /**< Some kind of error */
121 READ_STATUS_EOF /**< EOF returned */
132 memset (buf, '\0', maxlen);
133 maxlen -= 1; /* ensure nul term */
135 retval = READ_STATUS_OK;
143 to_read = maxlen - bytes;
151 if (chunk < 0 && errno == EINTR)
156 retval = READ_STATUS_ERROR;
161 retval = READ_STATUS_EOF;
168 if (retval == READ_STATUS_EOF &&
170 retval = READ_STATUS_OK;
173 if (retval != READ_STATUS_ERROR &&
175 buf[bytes-1] == '\n')
188 retval = READ_STATUS_OK;
196 to_read = sizeof (pid_t) - bytes;
202 ((char*)buf) + bytes,
204 if (chunk < 0 && errno == EINTR)
209 retval = READ_STATUS_ERROR;
214 retval = READ_STATUS_EOF;
225 do_write (int fd, const void *buf, size_t count)
227 size_t bytes_written;
234 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
242 fprintf (stderr, "Failed to write data to pipe!\n");
243 exit (1); /* give up, we suck */
247 bytes_written += ret;
249 if (bytes_written < count)
257 do_write (fd, &pid, sizeof (pid));
261 do_waitpid (pid_t pid)
266 ret = waitpid (pid, NULL, 0);
275 static pid_t bus_pid_to_kill = -1;
278 kill_bus_and_exit (void)
280 verbose ("Killing message bus and exiting babysitter\n");
282 /* in case these point to any NFS mounts, get rid of them immediately */
287 kill (bus_pid_to_kill, SIGTERM);
289 kill (bus_pid_to_kill, SIGKILL);
294 #ifdef DBUS_BUILD_X11
296 x_io_error_handler (Display *xdisplay)
298 verbose ("X IO error\n");
299 kill_bus_and_exit ();
304 static int got_sighup = FALSE;
307 signal_handler (int sig)
318 kill_bus_when_session_ends (void)
325 struct sigaction act;
327 #ifdef DBUS_BUILD_X11
331 /* install SIGHUP handler */
333 sigemptyset (&empty_mask);
334 act.sa_handler = signal_handler;
335 act.sa_mask = empty_mask;
337 sigaction (SIGHUP, &act, 0);
339 #ifdef DBUS_BUILD_X11
340 xdisplay = XOpenDisplay (NULL);
341 if (xdisplay != NULL)
343 verbose ("Successfully opened X display\n");
344 x_fd = ConnectionNumber (xdisplay);
345 XSetIOErrorHandler (x_io_error_handler);
350 verbose ("Compiled without X11 support\n");
360 verbose ("stdin isatty(), monitoring it\n");
362 verbose ("stdin was not a TTY, not monitoring it\n");
364 if (tty_fd < 0 && x_fd < 0)
366 fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
377 FD_SET (tty_fd, &read_set);
378 FD_SET (tty_fd, &err_set);
383 FD_SET (x_fd, &read_set);
384 FD_SET (x_fd, &err_set);
387 ret = select (MAX (tty_fd, x_fd) + 1,
388 &read_set, NULL, &err_set, NULL);
392 verbose ("Got SIGHUP, exiting\n");
393 kill_bus_and_exit ();
396 #ifdef DBUS_BUILD_X11
397 /* Dump events on the floor, and let
398 * IO error handler run if we lose
402 verbose ("X fd condition reading = %d error = %d\n",
403 FD_ISSET (x_fd, &read_set),
404 FD_ISSET (x_fd, &err_set));
406 if (xdisplay != NULL)
408 while (XPending (xdisplay))
411 XNextEvent (xdisplay, &ignored);
418 if (FD_ISSET (tty_fd, &read_set))
423 verbose ("TTY ready for reading\n");
425 bytes_read = read (tty_fd, discard, sizeof (discard));
427 verbose ("Read %d bytes from TTY errno = %d\n",
431 kill_bus_and_exit (); /* EOF */
432 else if (bytes_read < 0 && errno != EINTR)
434 /* This shouldn't happen I don't think; to avoid
435 * spinning on the fd forever we exit.
437 fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
439 kill_bus_and_exit ();
442 else if (FD_ISSET (tty_fd, &err_set))
444 verbose ("TTY has error condition\n");
446 kill_bus_and_exit ();
453 babysit (int exit_with_session,
455 int read_bus_pid_fd, /* read pid from here */
456 int write_bus_pid_fd) /* forward pid to here */
459 #define MAX_PID_LEN 64
460 char buf[MAX_PID_LEN];
464 /* We chdir ("/") since we are persistent and daemon-like, and fork
465 * again so dbus-launch can reap the parent. However, we don't
466 * setsid() or close fd 0,1,2 because the idea is to remain attached
467 * to the tty and the X server in order to kill the message bus
468 * when the session ends.
473 fprintf (stderr, "Could not change to root directory: %s\n",
482 fprintf (stderr, "fork() failed in babysitter: %s\n",
489 /* Parent reaps pre-fork part of bus daemon, then exits and is
490 * reaped so the babysitter isn't a zombie
493 verbose ("=== Babysitter's intermediate parent continues again\n");
495 if (do_waitpid (child_pid) < 0)
497 /* shouldn't happen */
498 fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
502 verbose ("Babysitter's intermediate parent exiting\n");
507 /* Child continues */
508 verbose ("=== Babysitter process created\n");
510 verbose ("Reading PID from daemon\n");
512 switch (read_line (read_bus_pid_fd, buf, MAX_PID_LEN))
516 case READ_STATUS_EOF:
517 fprintf (stderr, "EOF reading PID from bus daemon\n");
520 case READ_STATUS_ERROR:
521 fprintf (stderr, "Error reading PID from bus daemon: %s\n",
528 val = strtol (buf, &end, 0);
529 if (buf == end || end == NULL)
531 fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
532 buf, strerror (errno));
536 bus_pid_to_kill = val;
538 verbose ("Got PID %ld from daemon\n",
539 (long) bus_pid_to_kill);
541 /* Write data to launcher */
542 write_pid (write_bus_pid_fd, bus_pid_to_kill);
543 close (write_bus_pid_fd);
545 if (exit_with_session)
547 /* Bus is now started and launcher has needed info;
548 * we connect to X display and tty and wait to
549 * kill bus if requested.
552 kill_bus_when_session_ends ();
555 verbose ("Babysitter exiting\n");
564 main (int argc, char **argv)
566 const char *prev_arg;
568 const char *runprog = NULL;
569 int remaining_args = 0;
570 int exit_with_session;
571 int c_shell_syntax = FALSE;
572 int bourne_shell_syntax = FALSE;
573 int auto_shell_syntax = FALSE;
576 int bus_pid_to_launcher_pipe[2];
577 int bus_pid_to_babysitter_pipe[2];
578 int bus_address_to_launcher_pipe[2];
580 exit_with_session = FALSE;
586 const char *arg = argv[i];
588 if (strcmp (arg, "--help") == 0 ||
589 strcmp (arg, "-h") == 0 ||
590 strcmp (arg, "-?") == 0)
592 else if (strcmp (arg, "--auto-syntax") == 0)
593 auto_shell_syntax = TRUE;
594 else if (strcmp (arg, "-c") == 0 ||
595 strcmp (arg, "--csh-syntax") == 0)
596 c_shell_syntax = TRUE;
597 else if (strcmp (arg, "-s") == 0 ||
598 strcmp (arg, "--sh-syntax") == 0)
599 bourne_shell_syntax = TRUE;
600 else if (strcmp (arg, "--version") == 0)
602 else if (strcmp (arg, "--exit-with-session") == 0)
603 exit_with_session = TRUE;
609 remaining_args = i+1;
618 if (exit_with_session)
619 verbose ("--exit-with-session enabled\n");
621 if (auto_shell_syntax)
623 if ((shname = getenv ("SHELL")) != NULL)
625 if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
626 c_shell_syntax = TRUE;
628 bourne_shell_syntax = TRUE;
631 bourne_shell_syntax = TRUE;
634 if (pipe (bus_pid_to_launcher_pipe) < 0 ||
635 pipe (bus_address_to_launcher_pipe) < 0)
638 "Failed to create pipe: %s\n",
643 bus_pid_to_babysitter_pipe[READ_END] = -1;
644 bus_pid_to_babysitter_pipe[WRITE_END] = -1;
649 fprintf (stderr, "Failed to fork: %s\n",
657 #define MAX_FD_LEN 64
658 char write_pid_fd_as_string[MAX_FD_LEN];
659 char write_address_fd_as_string[MAX_FD_LEN];
661 verbose ("=== Babysitter's intermediate parent created\n");
663 /* Fork once more to create babysitter */
665 if (pipe (bus_pid_to_babysitter_pipe) < 0)
668 "Failed to create pipe: %s\n",
676 fprintf (stderr, "Failed to fork: %s\n",
684 verbose ("=== Babysitter's intermediate parent continues\n");
686 close (bus_pid_to_launcher_pipe[READ_END]);
687 close (bus_address_to_launcher_pipe[READ_END]);
688 close (bus_address_to_launcher_pipe[WRITE_END]);
689 close (bus_pid_to_babysitter_pipe[WRITE_END]);
691 /* babysit() will fork *again*
692 * and will also reap the pre-forked bus
695 babysit (exit_with_session, ret,
696 bus_pid_to_babysitter_pipe[READ_END],
697 bus_pid_to_launcher_pipe[WRITE_END]);
701 verbose ("=== Bus exec process created\n");
703 /* Now we are the bus process (well, almost;
704 * dbus-daemon-1 itself forks again)
706 close (bus_pid_to_launcher_pipe[READ_END]);
707 close (bus_address_to_launcher_pipe[READ_END]);
708 close (bus_pid_to_babysitter_pipe[READ_END]);
709 close (bus_pid_to_launcher_pipe[WRITE_END]);
711 sprintf (write_pid_fd_as_string,
712 "%d", bus_pid_to_babysitter_pipe[WRITE_END]);
714 sprintf (write_address_fd_as_string,
715 "%d", bus_address_to_launcher_pipe[WRITE_END]);
717 verbose ("Calling exec()\n");
719 execlp ("dbus-daemon-1",
723 "--print-pid", write_pid_fd_as_string,
724 "--print-address", write_address_fd_as_string,
728 "Failed to execute message bus daemon: %s\n",
735 #define MAX_ADDR_LEN 512
737 char bus_address[MAX_ADDR_LEN];
739 verbose ("=== Parent dbus-launch continues\n");
741 close (bus_pid_to_launcher_pipe[WRITE_END]);
742 close (bus_address_to_launcher_pipe[WRITE_END]);
744 verbose ("Waiting for babysitter's intermediate parent\n");
746 /* Immediately reap parent of babysitter
747 * (which was created just for us to reap)
749 if (do_waitpid (ret) < 0)
751 fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
756 verbose ("Reading address from bus\n");
758 /* Read the pipe data, print, and exit */
759 switch (read_line (bus_address_to_launcher_pipe[READ_END],
760 bus_address, MAX_ADDR_LEN))
764 case READ_STATUS_EOF:
765 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
768 case READ_STATUS_ERROR:
769 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
775 close (bus_address_to_launcher_pipe[READ_END]);
777 verbose ("Reading PID from babysitter\n");
779 switch (read_pid (bus_pid_to_launcher_pipe[READ_END], &bus_pid))
783 case READ_STATUS_EOF:
784 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
787 case READ_STATUS_ERROR:
788 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
794 close (bus_pid_to_launcher_pipe[READ_END]);
801 envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") + strlen (bus_address) + 1);
802 args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
804 if (envvar == NULL || args == NULL)
807 args[0] = xstrdup (runprog);
810 for (i = 1; i <= (argc-remaining_args); i++)
812 size_t len = strlen (argv[remaining_args+i-1])+1;
813 args[i] = malloc (len);
816 strncpy (args[i], argv[remaining_args+i-1], len);
820 strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
821 strcat (envvar, bus_address);
824 execvp (runprog, args);
825 fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
831 printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s'\n", bus_address);
834 printf ("DBUS_SESSION_BUS_ADDRESS='%s'\n", bus_address);
835 if (bourne_shell_syntax)
836 printf ("export DBUS_SESSION_BUS_ADDRESS\n");
839 printf ("set DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
841 printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
844 verbose ("dbus-launch exiting\n");
851 fprintf (stderr, "Out of memory!");