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] [--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",
99 READ_STATUS_OK, /**< Read succeeded */
100 READ_STATUS_ERROR, /**< Some kind of error */
101 READ_STATUS_EOF /**< EOF returned */
112 memset (buf, '\0', maxlen);
113 maxlen -= 1; /* ensure nul term */
115 retval = READ_STATUS_OK;
123 to_read = maxlen - bytes;
131 if (chunk < 0 && errno == EINTR)
136 retval = READ_STATUS_ERROR;
141 retval = READ_STATUS_EOF;
148 if (retval == READ_STATUS_EOF &&
150 retval = READ_STATUS_OK;
153 if (retval != READ_STATUS_ERROR &&
155 buf[bytes-1] == '\n')
168 retval = READ_STATUS_OK;
176 to_read = sizeof (pid_t) - bytes;
182 ((char*)buf) + bytes,
184 if (chunk < 0 && errno == EINTR)
189 retval = READ_STATUS_ERROR;
194 retval = READ_STATUS_EOF;
205 do_write (int fd, const void *buf, size_t count)
207 size_t bytes_written;
214 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
222 fprintf (stderr, "Failed to write data to pipe!\n");
223 exit (1); /* give up, we suck */
227 bytes_written += ret;
229 if (bytes_written < count)
237 do_write (fd, &pid, sizeof (pid));
241 do_waitpid (pid_t pid)
246 ret = waitpid (pid, NULL, 0);
255 static pid_t bus_pid_to_kill = -1;
258 kill_bus_and_exit (void)
260 verbose ("Killing message bus and exiting babysitter\n");
262 /* in case these point to any NFS mounts, get rid of them immediately */
267 kill (bus_pid_to_kill, SIGTERM);
269 kill (bus_pid_to_kill, SIGKILL);
274 #ifdef DBUS_BUILD_X11
276 x_io_error_handler (Display *xdisplay)
278 verbose ("X IO error\n");
279 kill_bus_and_exit ();
283 static int got_sighup = FALSE;
286 signal_handler (int sig)
297 kill_bus_when_session_ends (void)
304 struct sigaction act;
306 #ifdef DBUS_BUILD_X11
310 /* install SIGHUP handler */
312 sigemptyset (&empty_mask);
313 act.sa_handler = signal_handler;
314 act.sa_mask = empty_mask;
316 sigaction (SIGHUP, &act, 0);
318 #ifdef DBUS_BUILD_X11
319 xdisplay = XOpenDisplay (NULL);
320 if (xdisplay != NULL)
322 verbose ("Successfully opened X display\n");
323 x_fd = ConnectionNumber (xdisplay);
324 XSetIOErrorHandler (x_io_error_handler);
329 verbose ("Compiled without X11 support\n");
339 verbose ("stdin isatty(), monitoring it\n");
341 verbose ("stdin was not a TTY, not monitoring it\n");
343 if (tty_fd < 0 && x_fd < 0)
345 fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
356 FD_SET (tty_fd, &read_set);
357 FD_SET (tty_fd, &err_set);
362 FD_SET (x_fd, &read_set);
363 FD_SET (x_fd, &err_set);
366 ret = select (MAX (tty_fd, x_fd) + 1,
367 &read_set, NULL, &err_set, NULL);
371 verbose ("Got SIGHUP, exiting\n");
372 kill_bus_and_exit ();
375 #ifdef DBUS_BUILD_X11
376 /* Dump events on the floor, and let
377 * IO error handler run if we lose
381 verbose ("X fd condition reading = %d error = %d\n",
382 FD_ISSET (x_fd, &read_set),
383 FD_ISSET (x_fd, &err_set));
385 if (xdisplay != NULL)
387 while (XPending (xdisplay))
390 XNextEvent (xdisplay, &ignored);
397 if (FD_ISSET (tty_fd, &read_set))
402 verbose ("TTY ready for reading\n");
404 bytes_read = read (tty_fd, discard, sizeof (discard));
406 verbose ("Read %d bytes from TTY errno = %d\n",
410 kill_bus_and_exit (); /* EOF */
411 else if (bytes_read < 0 && errno != EINTR)
413 /* This shouldn't happen I don't think; to avoid
414 * spinning on the fd forever we exit.
416 fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
418 kill_bus_and_exit ();
421 else if (FD_ISSET (tty_fd, &err_set))
423 verbose ("TTY has error condition\n");
425 kill_bus_and_exit ();
432 babysit (int exit_with_session,
434 int read_bus_pid_fd, /* read pid from here */
435 int write_bus_pid_fd) /* forward pid to here */
438 #define MAX_PID_LEN 64
439 char buf[MAX_PID_LEN];
443 /* We chdir ("/") since we are persistent and daemon-like, and fork
444 * again so dbus-launch can reap the parent. However, we don't
445 * setsid() or close fd 0,1,2 because the idea is to remain attached
446 * to the tty and the X server in order to kill the message bus
447 * when the session ends.
452 fprintf (stderr, "Could not change to root directory: %s\n",
461 fprintf (stderr, "fork() failed in babysitter: %s\n",
468 /* Parent reaps pre-fork part of bus daemon, then exits and is
469 * reaped so the babysitter isn't a zombie
472 verbose ("=== Babysitter's intermediate parent continues again\n");
474 if (do_waitpid (child_pid) < 0)
476 /* shouldn't happen */
477 fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
481 verbose ("Babysitter's intermediate parent exiting\n");
486 /* Child continues */
487 verbose ("=== Babysitter process created\n");
489 verbose ("Reading PID from daemon\n");
491 switch (read_line (read_bus_pid_fd, buf, MAX_PID_LEN))
495 case READ_STATUS_EOF:
496 fprintf (stderr, "EOF reading PID from bus daemon\n");
499 case READ_STATUS_ERROR:
500 fprintf (stderr, "Error reading PID from bus daemon: %s\n",
507 val = strtol (buf, &end, 0);
508 if (buf == end || end == NULL)
510 fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
511 buf, strerror (errno));
515 bus_pid_to_kill = val;
517 verbose ("Got PID %ld from daemon\n",
518 (long) bus_pid_to_kill);
520 /* Write data to launcher */
521 write_pid (write_bus_pid_fd, bus_pid_to_kill);
522 close (write_bus_pid_fd);
524 if (exit_with_session)
526 /* Bus is now started and launcher has needed info;
527 * we connect to X display and tty and wait to
528 * kill bus if requested.
531 kill_bus_when_session_ends ();
534 verbose ("Babysitter exiting\n");
543 main (int argc, char **argv)
545 const char *prev_arg;
546 int exit_with_session;
549 int bus_pid_to_launcher_pipe[2];
550 int bus_pid_to_babysitter_pipe[2];
551 int bus_address_to_launcher_pipe[2];
553 exit_with_session = FALSE;
559 const char *arg = argv[i];
561 if (strcmp (arg, "--help") == 0 ||
562 strcmp (arg, "-h") == 0 ||
563 strcmp (arg, "-?") == 0)
565 else if (strcmp (arg, "--version") == 0)
567 else if (strcmp (arg, "--exit-with-session") == 0)
568 exit_with_session = TRUE;
577 verbose ("--exit-with-session provided\n");
579 if (pipe (bus_pid_to_launcher_pipe) < 0 ||
580 pipe (bus_address_to_launcher_pipe) < 0)
583 "Failed to create pipe: %s\n",
588 bus_pid_to_babysitter_pipe[READ_END] = -1;
589 bus_pid_to_babysitter_pipe[WRITE_END] = -1;
594 fprintf (stderr, "Failed to fork: %s\n",
602 #define MAX_FD_LEN 64
603 char write_pid_fd_as_string[MAX_FD_LEN];
604 char write_address_fd_as_string[MAX_FD_LEN];
606 verbose ("=== Babysitter's intermediate parent created\n");
608 /* Fork once more to create babysitter */
610 if (pipe (bus_pid_to_babysitter_pipe) < 0)
613 "Failed to create pipe: %s\n",
621 fprintf (stderr, "Failed to fork: %s\n",
629 verbose ("=== Babysitter's intermediate parent continues\n");
631 close (bus_pid_to_launcher_pipe[READ_END]);
632 close (bus_address_to_launcher_pipe[READ_END]);
633 close (bus_address_to_launcher_pipe[WRITE_END]);
634 close (bus_pid_to_babysitter_pipe[WRITE_END]);
636 /* babysit() will fork *again*
637 * and will also reap the pre-forked bus
640 babysit (exit_with_session, ret,
641 bus_pid_to_babysitter_pipe[READ_END],
642 bus_pid_to_launcher_pipe[WRITE_END]);
646 verbose ("=== Bus exec process created\n");
648 /* Now we are the bus process (well, almost;
649 * dbus-daemon-1 itself forks again)
651 close (bus_pid_to_launcher_pipe[READ_END]);
652 close (bus_address_to_launcher_pipe[READ_END]);
653 close (bus_pid_to_babysitter_pipe[READ_END]);
654 close (bus_pid_to_launcher_pipe[WRITE_END]);
656 sprintf (write_pid_fd_as_string,
657 "%d", bus_pid_to_babysitter_pipe[WRITE_END]);
659 sprintf (write_address_fd_as_string,
660 "%d", bus_address_to_launcher_pipe[WRITE_END]);
662 verbose ("Calling exec()\n");
664 execlp ("dbus-daemon-1",
668 "--print-pid", write_pid_fd_as_string,
669 "--print-address", write_address_fd_as_string,
673 "Failed to execute message bus daemon: %s\n",
680 #define MAX_ADDR_LEN 512
682 char bus_address[MAX_ADDR_LEN];
684 verbose ("=== Parent dbus-launch continues\n");
686 close (bus_pid_to_launcher_pipe[WRITE_END]);
687 close (bus_address_to_launcher_pipe[WRITE_END]);
689 verbose ("Waiting for babysitter's intermediate parent\n");
691 /* Immediately reap parent of babysitter
692 * (which was created just for us to reap)
694 if (do_waitpid (ret) < 0)
696 fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
701 verbose ("Reading address from bus\n");
703 /* Read the pipe data, print, and exit */
704 switch (read_line (bus_address_to_launcher_pipe[READ_END],
705 bus_address, MAX_ADDR_LEN))
709 case READ_STATUS_EOF:
710 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
713 case READ_STATUS_ERROR:
714 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
720 close (bus_address_to_launcher_pipe[READ_END]);
722 verbose ("Reading PID from babysitter\n");
724 switch (read_pid (bus_pid_to_launcher_pipe[READ_END], &bus_pid))
728 case READ_STATUS_EOF:
729 fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
732 case READ_STATUS_ERROR:
733 fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
739 close (bus_pid_to_launcher_pipe[READ_END]);
741 printf ("DBUS_SESSION_BUS_ADDRESS='%s'\n",
744 printf ("DBUS_SESSION_BUS_PID=%ld\n",
747 verbose ("dbus-launch exiting\n");