Add dbus-run-session
[platform/upstream/dbus.git] / tools / dbus-launch.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-launch.c  dbus-launch utility
3  *
4  * Copyright (C) 2003, 2006 Red Hat, Inc.
5  * Copyright (C) 2006 Thiago Macieira <thiago@kde.org>
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
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.
13  *
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.
18  * 
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
22  *
23  */
24
25 #include <config.h>
26 #include "dbus-launch.h"
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <sys/wait.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <sys/select.h>
39 #include <time.h>
40
41 #ifdef DBUS_BUILD_X11
42 #include <X11/Xlib.h>
43 extern Display *xdisplay;
44 #endif
45
46 /* PROCESSES
47  *
48  * If you are in a shell and run "dbus-launch myapp", here is what happens:
49  *
50  * shell [*]
51  *   \- main()               --exec--> myapp[*]
52  *      \- "intermediate parent"
53  *         \- bus-runner     --exec--> dbus-daemon --fork
54  *         \- babysitter[*]            \- final dbus-daemon[*]
55  *
56  * Processes marked [*] survive the initial flurry of activity.
57  *
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.
60  *
61  * PIPES
62  *
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
66  *
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.
74  *
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.
80  *
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.
85  */
86
87 static char* machine_uuid = NULL;
88
89 const char*
90 get_machine_uuid (void)
91 {
92   return machine_uuid;
93 }
94
95 static void
96 save_machine_uuid (const char *uuid_arg)
97 {
98   if (strlen (uuid_arg) != 32)
99     {
100       fprintf (stderr, "machine ID '%s' looks like it's the wrong length, should be 32 hex digits",
101                uuid_arg);
102       exit (1);
103     }
104
105   machine_uuid = xstrdup (uuid_arg);
106 }
107
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 */
111 static int
112 read_machine_uuid_if_needed (void)
113 {
114   FILE *f;
115   char uuid[UUID_MAXLEN];
116   size_t len;
117   int ret = FALSE;
118
119   if (machine_uuid != NULL)
120     return TRUE;
121
122   f = fopen (DBUS_MACHINE_UUID_FILE, "r");
123   if (f == NULL)
124     return FALSE;
125
126   if (fgets (uuid, UUID_MAXLEN, f) == NULL)
127     goto out;
128
129   len = strlen (uuid);
130   if (len < 32)
131     goto out;
132
133   /* rstrip the read uuid */
134   while (len > 31 && isspace(uuid[len - 1]))
135     len--;
136
137   if (len != 32)
138     goto out;
139
140   uuid[len] = '\0';
141   machine_uuid = xstrdup (uuid);
142   verbose ("UID: %s\n", machine_uuid);
143   ret = TRUE;
144
145 out:
146   fclose(f);
147   return ret;
148 }
149
150
151 void
152 verbose (const char *format,
153          ...)
154 {
155   va_list args;
156   static int verbose = TRUE;
157   static int verbose_initted = FALSE;
158   
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.
162    */
163   if (!verbose)
164     return;
165   
166   if (!verbose_initted)
167     {
168       verbose = getenv ("DBUS_VERBOSE") != NULL;
169       verbose_initted = TRUE;
170       if (!verbose)
171         return;
172     }
173
174   fprintf (stderr, "%lu: ", (unsigned long) getpid ());
175   
176   va_start (args, format);
177   vfprintf (stderr, format, args);
178   va_end (args);
179 }
180
181 static void
182 usage (int ecode)
183 {
184   fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session]\n");
185   exit (ecode);
186 }
187
188 static void
189 version (void)
190 {
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",
195           VERSION);
196   exit (0);
197 }
198
199 char *
200 xstrdup (const char *str)
201 {
202   int len;
203   char *copy;
204   
205   if (str == NULL)
206     return NULL;
207   
208   len = strlen (str);
209
210   copy = malloc (len + 1);
211   if (copy == NULL)
212     return NULL;
213
214   memcpy (copy, str, len + 1);
215   
216   return copy;
217 }
218
219 typedef enum
220 {
221   READ_STATUS_OK,    /**< Read succeeded */
222   READ_STATUS_ERROR, /**< Some kind of error */
223   READ_STATUS_EOF    /**< EOF returned */
224 } ReadStatus;
225
226 static ReadStatus
227 read_line (int        fd,
228            char      *buf,
229            size_t     maxlen)
230 {
231   size_t bytes = 0;
232   ReadStatus retval;
233
234   memset (buf, '\0', maxlen);
235   maxlen -= 1; /* ensure nul term */
236   
237   retval = READ_STATUS_OK;
238   
239   while (TRUE)
240     {
241       ssize_t chunk;    
242       size_t to_read;
243       
244     again:
245       to_read = maxlen - bytes;
246
247       if (to_read == 0)
248         break;
249       
250       chunk = read (fd,
251                     buf + bytes,
252                     to_read);
253       if (chunk < 0 && errno == EINTR)
254         goto again;
255           
256       if (chunk < 0)
257         {
258           retval = READ_STATUS_ERROR;
259           break;
260         }
261       else if (chunk == 0)
262         {
263           retval = READ_STATUS_EOF;
264           break; /* EOF */
265         }
266       else /* chunk > 0 */
267         bytes += chunk;
268     }
269
270   if (retval == READ_STATUS_EOF &&
271       bytes > 0)
272     retval = READ_STATUS_OK;
273   
274   /* whack newline */
275   if (retval != READ_STATUS_ERROR &&
276       bytes > 0 &&
277       buf[bytes-1] == '\n')
278     buf[bytes-1] = '\0';
279   
280   return retval;
281 }
282
283 static ReadStatus
284 read_pid (int        fd,
285           pid_t     *buf)
286 {
287   size_t bytes = 0;
288   ReadStatus retval;
289
290   retval = READ_STATUS_OK;
291   
292   while (TRUE)
293     {
294       ssize_t chunk;    
295       size_t to_read;
296       
297     again:
298       to_read = sizeof (pid_t) - bytes;
299
300       if (to_read == 0)
301         break;
302       
303       chunk = read (fd,
304                     ((char*)buf) + bytes,
305                     to_read);
306       if (chunk < 0 && errno == EINTR)
307         goto again;
308           
309       if (chunk < 0)
310         {
311           retval = READ_STATUS_ERROR;
312           break;
313         }
314       else if (chunk == 0)
315         {
316           retval = READ_STATUS_EOF;
317           break; /* EOF */
318         }
319       else /* chunk > 0 */
320         bytes += chunk;
321     }
322
323   return retval;
324 }
325
326 static void
327 do_write (int fd, const void *buf, size_t count)
328 {
329   size_t bytes_written;
330   int ret;
331   
332   bytes_written = 0;
333   
334  again:
335   
336   ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
337
338   if (ret < 0)
339     {
340       if (errno == EINTR)
341         goto again;
342       else
343         {
344           fprintf (stderr, "Failed to write data to pipe! %s\n",
345                    strerror (errno));
346           exit (1); /* give up, we suck */
347         }
348     }
349   else
350     bytes_written += ret;
351   
352   if (bytes_written < count)
353     goto again;
354 }
355
356 static void
357 write_pid (int   fd,
358            pid_t pid)
359 {
360   do_write (fd, &pid, sizeof (pid));
361 }
362
363 static int
364 do_waitpid (pid_t pid)
365 {
366   int ret;
367   
368  again:
369   ret = waitpid (pid, NULL, 0);
370
371   if (ret < 0 &&
372       errno == EINTR)
373     goto again;
374
375   return ret;
376 }
377
378 static pid_t bus_pid_to_kill = -1;
379
380 static void
381 kill_bus(void)
382 {
383   verbose ("Killing message bus and exiting babysitter\n");
384   kill (bus_pid_to_kill, SIGTERM);
385   sleep (3);
386   kill (bus_pid_to_kill, SIGKILL);
387 }
388
389 void
390 kill_bus_and_exit (int exitcode)
391 {
392   /* in case these point to any NFS mounts, get rid of them immediately */
393   close (0);
394   close (1);
395   close (2);
396
397   kill_bus();
398
399   exit (exitcode);
400 }
401
402 static void
403 print_variables (const char *bus_address, pid_t bus_pid, long bus_wid,
404                  int c_shell_syntax, int bourne_shell_syntax,
405                  int binary_syntax)
406 {
407   if (binary_syntax)
408     {
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);
412       return;
413     }
414   else if (c_shell_syntax)
415     {
416       printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s';\n", bus_address);  
417       printf ("set DBUS_SESSION_BUS_PID=%ld;\n", (long) bus_pid);
418       if (bus_wid)
419         printf ("set DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
420       fflush (stdout);
421     }
422   else if (bourne_shell_syntax)
423     {
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);
427       if (bus_wid)
428         printf ("DBUS_SESSION_BUS_WINDOWID=%ld;\n", (long) bus_wid);
429       fflush (stdout);
430     }
431   else
432     {
433       printf ("DBUS_SESSION_BUS_ADDRESS=%s\n", bus_address);
434       printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
435       if (bus_wid)
436         printf ("DBUS_SESSION_BUS_WINDOWID=%ld\n", (long) bus_wid);
437       fflush (stdout);
438     }
439 }
440
441 static int got_sighup = FALSE;
442
443 static void
444 signal_handler (int sig)
445 {
446   switch (sig)
447     {
448 #ifdef SIGHUP
449     case SIGHUP:
450 #endif
451     case SIGINT:
452     case SIGTERM:
453       got_sighup = TRUE;
454       break;
455     }
456 }
457
458 static void
459 kill_bus_when_session_ends (void)
460 {
461   int tty_fd;
462   int x_fd;
463   fd_set read_set;
464   fd_set err_set;
465   struct sigaction act;
466   sigset_t empty_mask;
467   
468   /* install SIGHUP handler */
469   got_sighup = FALSE;
470   sigemptyset (&empty_mask);
471   act.sa_handler = signal_handler;
472   act.sa_mask    = empty_mask;
473   act.sa_flags   = 0;
474   sigaction (SIGHUP,  &act, NULL);
475   sigaction (SIGTERM,  &act, NULL);
476   sigaction (SIGINT,  &act, NULL);
477   
478 #ifdef DBUS_BUILD_X11
479   x11_init();
480   if (xdisplay != NULL)
481     {
482       x_fd = ConnectionNumber (xdisplay);
483     }
484   else
485     x_fd = -1;
486 #else
487   x_fd = -1;
488 #endif
489
490   if (isatty (0))
491     tty_fd = 0;
492   else
493     tty_fd = -1;
494
495   if (x_fd >= 0)
496     {
497       verbose ("session lifetime is defined by X, not monitoring stdin\n");
498       tty_fd = -1;
499     }
500   else if (tty_fd >= 0)
501     {
502       verbose ("stdin isatty(), monitoring it\n");
503     }
504   else
505     {
506       verbose ("stdin was not a TTY, not monitoring it\n");
507     }
508
509   if (tty_fd < 0 && x_fd < 0)
510     {
511       fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
512       exit (1);
513     }
514   
515   while (TRUE)
516     {
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
523        * events.
524        */
525       x11_handle_event ();
526 #endif
527       
528       FD_ZERO (&read_set);
529       FD_ZERO (&err_set);
530
531       if (tty_fd >= 0)
532         {
533           FD_SET (tty_fd, &read_set);
534           FD_SET (tty_fd, &err_set);
535         }
536
537       if (x_fd >= 0)
538         {
539           FD_SET (x_fd, &read_set);
540           FD_SET (x_fd, &err_set);
541         }
542
543       select (MAX (tty_fd, x_fd) + 1,
544               &read_set, NULL, &err_set, NULL);
545
546       if (got_sighup)
547         {
548           verbose ("Got SIGHUP, exiting\n");
549           kill_bus_and_exit (0);
550         }
551       
552 #ifdef DBUS_BUILD_X11
553       /* Events will be processed before we select again
554        */
555       if (x_fd >= 0)
556         verbose ("X fd condition reading = %d error = %d\n",
557                  FD_ISSET (x_fd, &read_set),
558                  FD_ISSET (x_fd, &err_set));
559 #endif
560
561       if (tty_fd >= 0)
562         {
563           if (FD_ISSET (tty_fd, &read_set))
564             {
565               int bytes_read;
566               char discard[512];
567
568               verbose ("TTY ready for reading\n");
569               
570               bytes_read = read (tty_fd, discard, sizeof (discard));
571
572               verbose ("Read %d bytes from TTY errno = %d\n",
573                        bytes_read, errno);
574               
575               if (bytes_read == 0)
576                 kill_bus_and_exit (0); /* EOF */
577               else if (bytes_read < 0 && errno != EINTR)
578                 {
579                   /* This shouldn't happen I don't think; to avoid
580                    * spinning on the fd forever we exit.
581                    */
582                   fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
583                            strerror (errno));
584                   kill_bus_and_exit (0);
585                 }
586             }
587           else if (FD_ISSET (tty_fd, &err_set))
588             {
589               verbose ("TTY has error condition\n");
590               
591               kill_bus_and_exit (0);
592             }
593         }
594     }
595 }
596
597 static void
598 babysit (int   exit_with_session,
599          pid_t child_pid,
600          int   read_bus_pid_fd)  /* read pid from here */
601 {
602   int ret;
603   int dev_null_fd;
604   const char *s;
605
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);
608   
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.
614    */
615
616   if (chdir ("/") < 0)
617     {
618       fprintf (stderr, "Could not change to root directory: %s\n",
619                strerror (errno));
620       exit (1);
621     }
622
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.
626    */
627   dev_null_fd = open ("/dev/null", O_RDWR);
628   if (dev_null_fd >= 0)
629     {
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);
636       close (dev_null_fd);
637     }
638   else
639     {
640       fprintf (stderr, "Failed to open /dev/null: %s\n",
641                strerror (errno));
642       /* continue, why not */
643     }
644   
645   ret = fork ();
646
647   if (ret < 0)
648     {
649       fprintf (stderr, "fork() failed in babysitter: %s\n",
650                strerror (errno));
651       exit (1);
652     }
653
654   if (ret > 0)
655     {
656       /* Parent reaps pre-fork part of bus daemon, then exits and is
657        * reaped so the babysitter isn't a zombie
658        */
659
660       verbose ("=== Babysitter's intermediate parent continues again\n");
661       
662       if (do_waitpid (child_pid) < 0)
663         {
664           /* shouldn't happen */
665           fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
666           exit (1);
667         }
668
669       verbose ("Babysitter's intermediate parent exiting\n");
670       
671       exit (0);
672     }
673
674   /* Child continues */
675   verbose ("=== Babysitter process created\n");
676
677   verbose ("Reading PID from bus\n");
678       
679   switch (read_pid (read_bus_pid_fd, &bus_pid_to_kill))
680     {
681     case READ_STATUS_OK:
682       break;
683     case READ_STATUS_EOF:
684       fprintf (stderr, "EOF in dbus-launch reading PID from bus daemon\n");
685       exit (1);
686       break;
687     case READ_STATUS_ERROR:
688       fprintf (stderr, "Error in dbus-launch reading PID from bus daemon: %s\n",
689                strerror (errno));
690       exit (1);
691       break;
692     }
693
694   verbose ("Got PID %ld from daemon\n",
695            (long) bus_pid_to_kill);
696   
697   if (exit_with_session)
698     {
699       /* Bus is now started and launcher has needed info;
700        * we connect to X display and tty and wait to
701        * kill bus if requested.
702        */
703       
704       kill_bus_when_session_ends ();
705     }
706
707   verbose ("Babysitter exiting\n");
708   
709   exit (0);
710 }
711
712 static void
713 do_close_stderr (void)
714 {
715   int fd;
716
717   fflush (stderr);
718
719   /* dbus-launch is a Unix-only program, so we can rely on /dev/null being there.
720    * We're including unistd.h and we're dealing with sh/csh launch sequences...
721    */
722   fd = open ("/dev/null", O_RDWR);
723   if (fd == -1)
724     {
725       fprintf (stderr, "Internal error: cannot open /dev/null: %s", strerror (errno));
726       exit (1);
727     }
728
729   close (2);
730   if (dup2 (fd, 2) == -1)
731     {
732       /* error; we can't report an error anymore... */
733       exit (1);
734     }
735   close (fd);
736 }
737
738 static void
739 pass_info (const char *runprog, const char *bus_address, pid_t bus_pid,
740            long bus_wid, int c_shell_syntax, int bourne_shell_syntax,
741            int binary_syntax,
742            int argc, char **argv, int remaining_args)
743 {
744   char *envvar = NULL;
745   char **args = NULL;
746
747   if (runprog)
748     {
749       int i;
750
751       envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") +
752           strlen (bus_address) + 1);
753       args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
754
755       if (envvar == NULL || args == NULL)
756         goto oom;
757
758      args[0] = xstrdup (runprog);
759       if (!args[0])
760         goto oom;
761      for (i = 1; i <= (argc-remaining_args); i++)
762       {
763         size_t len = strlen (argv[remaining_args+i-1])+1;
764         args[i] = malloc (len);
765         if (!args[i])
766           goto oom;
767         strncpy (args[i], argv[remaining_args+i-1], len);
768        }
769      args[i] = NULL;
770
771      strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
772      strcat (envvar, bus_address);
773      putenv (envvar);
774
775      execvp (runprog, args);
776      fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
777      exit (1);
778     }
779    else
780     {
781       print_variables (bus_address, bus_pid, bus_wid, c_shell_syntax,
782          bourne_shell_syntax, binary_syntax);
783     }
784   verbose ("dbus-launch exiting\n");
785
786   fflush (stdout);
787   fflush (stderr);
788   close (1);
789   close (2);
790   exit (0);
791 oom:
792   if (envvar)
793     free (envvar);
794
795   if (args)
796     free (args);
797
798   fprintf (stderr, "Out of memory!");
799   exit (1);
800 }
801
802 #define READ_END  0
803 #define WRITE_END 1
804
805 int
806 main (int argc, char **argv)
807 {
808   const char *prev_arg;
809   const char *shname;
810   const char *runprog = NULL;
811   int remaining_args = 0;
812   int exit_with_session;
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;
820   int i;
821   int ret;
822   int bus_pid_to_launcher_pipe[2];
823   int bus_pid_to_babysitter_pipe[2];
824   int bus_address_to_launcher_pipe[2];
825   char *config_file;
826   
827   exit_with_session = FALSE;
828   config_file = NULL;
829   
830   prev_arg = NULL;
831   i = 1;
832   while (i < argc)
833     {
834       const char *arg = argv[i];
835  
836       if (strcmp (arg, "--help") == 0 ||
837           strcmp (arg, "-h") == 0 ||
838           strcmp (arg, "-?") == 0)
839         usage (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)
851         version ();
852       else if (strcmp (arg, "--exit-with-session") == 0)
853         exit_with_session = TRUE;
854       else if (strcmp (arg, "--close-stderr") == 0)
855         close_stderr = TRUE;
856       else if (strstr (arg, "--autolaunch=") == arg)
857         {
858           const char *s;
859
860           if (autolaunch)
861             {
862               fprintf (stderr, "--autolaunch given twice\n");
863               exit (1);
864             }
865           
866           autolaunch = TRUE;
867
868           s = strchr (arg, '=');
869           ++s;
870
871           save_machine_uuid (s);
872         }
873       else if (prev_arg &&
874                strcmp (prev_arg, "--autolaunch") == 0)
875         {
876           if (autolaunch)
877             {
878               fprintf (stderr, "--autolaunch given twice\n");
879               exit (1);
880             }
881           
882           autolaunch = TRUE;
883
884           save_machine_uuid (arg);
885           requires_arg = FALSE;
886         }
887       else if (strcmp (arg, "--autolaunch") == 0)
888         requires_arg = TRUE;
889       else if (strstr (arg, "--config-file=") == arg)
890         {
891           const char *file;
892
893           if (config_file != NULL)
894             {
895               fprintf (stderr, "--config-file given twice\n");
896               exit (1);
897             }
898           
899           file = strchr (arg, '=');
900           ++file;
901
902           config_file = xstrdup (file);
903         }
904       else if (prev_arg &&
905                strcmp (prev_arg, "--config-file") == 0)
906         {
907           if (config_file != NULL)
908             {
909               fprintf (stderr, "--config-file given twice\n");
910               exit (1);
911             }
912
913           config_file = xstrdup (arg);
914           requires_arg = FALSE;
915         }
916       else if (strcmp (arg, "--config-file") == 0)
917         requires_arg = TRUE;
918       else if (arg[0] == '-')
919         {
920           if (strcmp (arg, "--") != 0)
921             {
922               fprintf (stderr, "Option `%s' is unknown.\n", arg);
923               exit (1);
924             }
925           else
926             {
927               runprog = argv[i+1];
928               remaining_args = i+2;
929               break;
930             }
931         }
932       else
933         {
934           runprog = arg;
935           remaining_args = i+1;
936           break;
937         }
938       
939       prev_arg = arg;
940       
941       ++i;
942     }
943   if (requires_arg)
944     {
945       fprintf (stderr, "Option `%s' requires an argument.\n", prev_arg);
946       exit (1);
947     }
948
949   if (auto_shell_syntax)
950     {
951       if ((shname = getenv ("SHELL")) != NULL)
952        {
953          if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
954            c_shell_syntax = TRUE;
955          else
956            bourne_shell_syntax = TRUE;
957        }
958       else
959        bourne_shell_syntax = TRUE;
960     }  
961
962   if (exit_with_session)
963     verbose ("--exit-with-session enabled\n");
964
965   if (autolaunch)
966     {      
967 #ifndef DBUS_BUILD_X11
968       fprintf (stderr, "Autolaunch requested, but X11 support not compiled in.\n"
969                "Cannot continue.\n");
970       exit (1);
971 #else /* DBUS_BUILD_X11 */
972 #ifndef DBUS_ENABLE_X11_AUTOLAUNCH
973       fprintf (stderr, "X11 autolaunch support disabled at compile time.\n");
974       exit (1);
975 #else /* DBUS_ENABLE_X11_AUTOLAUNCH */
976       char *address;
977       pid_t pid;
978       long wid;
979       
980       if (get_machine_uuid () == NULL)
981         {
982           fprintf (stderr, "Machine UUID not provided as arg to --autolaunch\n");
983           exit (1);
984         }
985
986       verbose ("Autolaunch enabled (using X11).\n");
987       if (!exit_with_session)
988         {
989           verbose ("--exit-with-session automatically enabled\n");
990           exit_with_session = TRUE;
991         }
992
993       if (!x11_init ())
994         {
995           fprintf (stderr, "Autolaunch error: X11 initialization failed.\n");
996           exit (1);
997         }
998
999       if (!x11_get_address (&address, &pid, &wid))
1000         {
1001           fprintf (stderr, "Autolaunch error: X11 communication error.\n");
1002           exit (1);
1003         }
1004
1005       if (address != NULL)
1006         {
1007           verbose ("dbus-daemon is already running. Returning existing parameters.\n");
1008           pass_info (runprog, address, pid, wid, c_shell_syntax,
1009                            bourne_shell_syntax, binary_syntax, argc, argv, remaining_args);
1010           exit (0);
1011         }
1012 #endif /* DBUS_ENABLE_X11_AUTOLAUNCH */
1013     }
1014   else if (read_machine_uuid_if_needed())
1015     {
1016       x11_init();
1017 #endif /* DBUS_BUILD_X11 */
1018     }
1019
1020
1021   if (pipe (bus_pid_to_launcher_pipe) < 0 ||
1022       pipe (bus_address_to_launcher_pipe) < 0 ||
1023       pipe (bus_pid_to_babysitter_pipe) < 0)
1024     {
1025       fprintf (stderr,
1026                "Failed to create pipe: %s\n",
1027                strerror (errno));
1028       exit (1);
1029     }
1030
1031   ret = fork ();
1032   if (ret < 0)
1033     {
1034       fprintf (stderr, "Failed to fork: %s\n",
1035                strerror (errno));
1036       exit (1);
1037     }
1038
1039   if (ret == 0)
1040     {
1041       /* Child */
1042 #define MAX_FD_LEN 64
1043       char write_pid_fd_as_string[MAX_FD_LEN];
1044       char write_address_fd_as_string[MAX_FD_LEN];
1045
1046 #ifdef DBUS_BUILD_X11
1047       xdisplay = NULL;
1048 #endif
1049
1050       if (close_stderr)
1051         do_close_stderr ();
1052
1053       verbose ("=== Babysitter's intermediate parent created\n");
1054
1055       /* Fork once more to create babysitter */
1056       
1057       ret = fork ();
1058       if (ret < 0)
1059         {
1060           fprintf (stderr, "Failed to fork: %s\n",
1061                    strerror (errno));
1062           exit (1);
1063         }
1064       
1065       if (ret > 0)
1066         {
1067           /* In babysitter */
1068           verbose ("=== Babysitter's intermediate parent continues\n");
1069           
1070           close (bus_pid_to_launcher_pipe[READ_END]);
1071           close (bus_pid_to_launcher_pipe[WRITE_END]);
1072           close (bus_address_to_launcher_pipe[READ_END]);
1073           close (bus_address_to_launcher_pipe[WRITE_END]);
1074           close (bus_pid_to_babysitter_pipe[WRITE_END]);
1075
1076           /* babysit() will fork *again*
1077            * and will also reap the pre-forked bus
1078            * daemon
1079            */
1080           babysit (exit_with_session, ret,
1081                    bus_pid_to_babysitter_pipe[READ_END]);
1082           exit (0);
1083         }
1084
1085       verbose ("=== Bus exec process created\n");
1086       
1087       /* Now we are the bus process (well, almost;
1088        * dbus-daemon itself forks again)
1089        */
1090       close (bus_pid_to_launcher_pipe[READ_END]);
1091       close (bus_address_to_launcher_pipe[READ_END]);
1092       close (bus_pid_to_babysitter_pipe[READ_END]);
1093       close (bus_pid_to_babysitter_pipe[WRITE_END]);
1094
1095       sprintf (write_pid_fd_as_string,
1096                "%d", bus_pid_to_launcher_pipe[WRITE_END]);
1097
1098       sprintf (write_address_fd_as_string,
1099                "%d", bus_address_to_launcher_pipe[WRITE_END]);
1100
1101       verbose ("Calling exec()\n");
1102  
1103 #ifdef DBUS_BUILD_TESTS 
1104       /* exec from testdir */
1105       if (getenv("DBUS_USE_TEST_BINARY") != NULL)
1106         {
1107           execl (TEST_BUS_BINARY,
1108                  TEST_BUS_BINARY,
1109                  "--fork",
1110                  "--print-pid", write_pid_fd_as_string,
1111                  "--print-address", write_address_fd_as_string,
1112                  config_file ? "--config-file" : "--session",
1113                  config_file, /* has to be last in this varargs list */
1114                  NULL); 
1115
1116           fprintf (stderr,
1117                    "Failed to execute test message bus daemon %s: %s. Will try again with the system path.\n",
1118                    TEST_BUS_BINARY, strerror (errno));
1119         }
1120  #endif /* DBUS_BUILD_TESTS */
1121
1122       execl (DBUS_DAEMONDIR"/dbus-daemon",
1123              DBUS_DAEMONDIR"/dbus-daemon",
1124              "--fork",
1125              "--print-pid", write_pid_fd_as_string,
1126              "--print-address", write_address_fd_as_string,
1127              config_file ? "--config-file" : "--session",
1128              config_file, /* has to be last in this varargs list */
1129              NULL);
1130
1131       fprintf (stderr,
1132                "Failed to execute message bus daemon %s: %s.  Will try again without full path.\n",
1133                DBUS_DAEMONDIR"/dbus-daemon", strerror (errno));
1134       
1135       /*
1136        * If it failed, try running without full PATH.  Note this is needed
1137        * because the build process builds the run-with-tmp-session-bus.conf
1138        * file and the dbus-daemon will not be in the install location during
1139        * build time.
1140        */
1141       execlp ("dbus-daemon",
1142               "dbus-daemon",
1143               "--fork",
1144               "--print-pid", write_pid_fd_as_string,
1145               "--print-address", write_address_fd_as_string,
1146               config_file ? "--config-file" : "--session",
1147               config_file, /* has to be last in this varargs list */
1148               NULL);
1149
1150       fprintf (stderr,
1151                "Failed to execute message bus daemon: %s\n",
1152                strerror (errno));
1153       exit (1);
1154     }
1155   else
1156     {
1157       /* Parent */
1158 #define MAX_PID_LEN 64
1159       pid_t bus_pid;  
1160       char bus_address[MAX_ADDR_LEN];
1161       char buf[MAX_PID_LEN];
1162       char *end;
1163       long wid = 0;
1164       long val;
1165
1166       verbose ("=== Parent dbus-launch continues\n");
1167       
1168       close (bus_pid_to_launcher_pipe[WRITE_END]);
1169       close (bus_address_to_launcher_pipe[WRITE_END]);
1170       close (bus_pid_to_babysitter_pipe[READ_END]);
1171
1172       verbose ("Waiting for babysitter's intermediate parent\n");
1173       
1174       /* Immediately reap parent of babysitter
1175        * (which was created just for us to reap)
1176        */
1177       if (do_waitpid (ret) < 0)
1178         {
1179           fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
1180                    strerror (errno));
1181           exit (1);
1182         }
1183
1184       verbose ("Reading address from bus\n");
1185       
1186       /* Read the pipe data, print, and exit */
1187       switch (read_line (bus_address_to_launcher_pipe[READ_END],
1188                          bus_address, MAX_ADDR_LEN))
1189         {
1190         case READ_STATUS_OK:
1191           break;
1192         case READ_STATUS_EOF:
1193           fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
1194           exit (1);
1195           break;
1196         case READ_STATUS_ERROR:
1197           fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
1198                    strerror (errno));
1199           exit (1);
1200           break;
1201         }
1202         
1203       close (bus_address_to_launcher_pipe[READ_END]);
1204
1205       verbose ("Reading PID from daemon\n");
1206       /* Now read data */
1207       switch (read_line (bus_pid_to_launcher_pipe[READ_END], buf, MAX_PID_LEN))
1208         {
1209         case READ_STATUS_OK:
1210           break;
1211         case READ_STATUS_EOF:
1212           fprintf (stderr, "EOF reading PID from bus daemon\n");
1213           exit (1);
1214           break;
1215         case READ_STATUS_ERROR:
1216           fprintf (stderr, "Error reading PID from bus daemon: %s\n",
1217                    strerror (errno));
1218           exit (1);
1219           break;
1220         }
1221
1222       end = NULL;
1223       val = strtol (buf, &end, 0);
1224       if (buf == end || end == NULL)
1225         {
1226           fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
1227                    buf, strerror (errno));
1228           exit (1);
1229         }
1230
1231       bus_pid = val;
1232
1233       close (bus_pid_to_launcher_pipe[READ_END]);
1234
1235 #ifdef DBUS_ENABLE_X11_AUTOLAUNCH
1236       if (xdisplay != NULL)
1237         {
1238           int ret2;
1239
1240           verbose("Saving x11 address\n");
1241           ret2 = x11_save_address (bus_address, bus_pid, &wid);
1242           /* Only get an existing dbus session when autolaunching */
1243           if (autolaunch)
1244             {
1245               if (ret2 == 0)
1246                 {
1247                   char *address = NULL;
1248                   /* another window got added. Return its address */
1249                   bus_pid_to_kill = bus_pid;
1250                   if (x11_get_address (&address, &bus_pid, &wid)
1251                        && address != NULL)
1252                     {
1253                       verbose ("dbus-daemon is already running. Returning existing parameters.\n");
1254                       /* Kill the old bus */
1255                       kill_bus();
1256                       pass_info (runprog, address, bus_pid, wid,
1257                          c_shell_syntax, bourne_shell_syntax, binary_syntax,
1258                          argc, argv, remaining_args);
1259                     }
1260                   }
1261               if (ret2 < 0)
1262                 {
1263                   fprintf (stderr, "Error saving bus information.\n");
1264                   bus_pid_to_kill = bus_pid;
1265                   kill_bus_and_exit (1);
1266                 }
1267             }
1268         }
1269 #endif
1270
1271       /* Forward the pid to the babysitter */
1272       write_pid (bus_pid_to_babysitter_pipe[WRITE_END], bus_pid);
1273       close (bus_pid_to_babysitter_pipe[WRITE_END]);
1274
1275        pass_info (runprog, bus_address, bus_pid, wid, c_shell_syntax,
1276               bourne_shell_syntax, binary_syntax, argc, argv, remaining_args);
1277     }
1278
1279   return 0;
1280 }