2005-07-12 Colin Walters <walters@verbum.org>
[platform/upstream/dbus.git] / tools / dbus-launch.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-launch.c  dbus-launch utility
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23 #include <config.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <sys/select.h>
35 #ifdef DBUS_BUILD_X11
36 #include <X11/Xlib.h>
37 #endif
38
39 #ifndef TRUE
40 #define TRUE (1)
41 #endif
42
43 #ifndef FALSE
44 #define FALSE (0)
45 #endif
46
47 #undef  MAX
48 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
49
50 static void
51 verbose (const char *format,
52          ...)
53 {
54   va_list args;
55   static int verbose = TRUE;
56   static int verbose_initted = FALSE;
57   
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.
61    */
62   if (!verbose)
63     return;
64   
65   if (!verbose_initted)
66     {
67       verbose = getenv ("DBUS_VERBOSE") != NULL;
68       verbose_initted = TRUE;
69       if (!verbose)
70         return;
71     }
72
73   fprintf (stderr, "%lu: ", (unsigned long) getpid ());
74   
75   va_start (args, format);
76   vfprintf (stderr, format, args);
77   va_end (args);
78 }
79
80 static void
81 usage (int ecode)
82 {
83   fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax] [--auto-syntax] [--exit-with-session]\n");
84   exit (ecode);
85 }
86
87 static void
88 version (void)
89 {
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",
94           VERSION);
95   exit (0);
96 }
97
98 static char *
99 xstrdup (const char *str)
100 {
101   int len;
102   char *copy;
103   
104   if (str == NULL)
105     return NULL;
106   
107   len = strlen (str);
108
109   copy = malloc (len + 1);
110   if (copy == NULL)
111     return NULL;
112
113   memcpy (copy, str, len + 1);
114   
115   return copy;
116 }
117
118 typedef enum
119 {
120   READ_STATUS_OK,    /**< Read succeeded */
121   READ_STATUS_ERROR, /**< Some kind of error */
122   READ_STATUS_EOF    /**< EOF returned */
123 } ReadStatus;
124
125 static ReadStatus
126 read_line (int        fd,
127            char      *buf,
128            size_t     maxlen)
129 {
130   size_t bytes = 0;
131   ReadStatus retval;
132
133   memset (buf, '\0', maxlen);
134   maxlen -= 1; /* ensure nul term */
135   
136   retval = READ_STATUS_OK;
137   
138   while (TRUE)
139     {
140       size_t chunk;    
141       size_t to_read;
142       
143     again:
144       to_read = maxlen - bytes;
145
146       if (to_read == 0)
147         break;
148       
149       chunk = read (fd,
150                     buf + bytes,
151                     to_read);
152       if (chunk < 0 && errno == EINTR)
153         goto again;
154           
155       if (chunk < 0)
156         {
157           retval = READ_STATUS_ERROR;
158           break;
159         }
160       else if (chunk == 0)
161         {
162           retval = READ_STATUS_EOF;
163           break; /* EOF */
164         }
165       else /* chunk > 0 */
166         bytes += chunk;
167     }
168
169   if (retval == READ_STATUS_EOF &&
170       bytes > 0)
171     retval = READ_STATUS_OK;
172   
173   /* whack newline */
174   if (retval != READ_STATUS_ERROR &&
175       bytes > 0 &&
176       buf[bytes-1] == '\n')
177     buf[bytes-1] = '\0';
178   
179   return retval;
180 }
181
182 static ReadStatus
183 read_pid (int        fd,
184           pid_t     *buf)
185 {
186   size_t bytes = 0;
187   ReadStatus retval;
188
189   retval = READ_STATUS_OK;
190   
191   while (TRUE)
192     {
193       size_t chunk;    
194       size_t to_read;
195       
196     again:
197       to_read = sizeof (pid_t) - bytes;
198
199       if (to_read == 0)
200         break;
201       
202       chunk = read (fd,
203                     ((char*)buf) + bytes,
204                     to_read);
205       if (chunk < 0 && errno == EINTR)
206         goto again;
207           
208       if (chunk < 0)
209         {
210           retval = READ_STATUS_ERROR;
211           break;
212         }
213       else if (chunk == 0)
214         {
215           retval = READ_STATUS_EOF;
216           break; /* EOF */
217         }
218       else /* chunk > 0 */
219         bytes += chunk;
220     }
221
222   return retval;
223 }
224
225 static void
226 do_write (int fd, const void *buf, size_t count)
227 {
228   size_t bytes_written;
229   int ret;
230   
231   bytes_written = 0;
232   
233  again:
234   
235   ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
236
237   if (ret < 0)
238     {
239       if (errno == EINTR)
240         goto again;
241       else
242         {
243           fprintf (stderr, "Failed to write data to pipe! %s\n",
244                    strerror (errno));
245           exit (1); /* give up, we suck */
246         }
247     }
248   else
249     bytes_written += ret;
250   
251   if (bytes_written < count)
252     goto again;
253 }
254
255 static void
256 write_pid (int   fd,
257            pid_t pid)
258 {
259   do_write (fd, &pid, sizeof (pid));
260 }
261
262 static int
263 do_waitpid (pid_t pid)
264 {
265   int ret;
266   
267  again:
268   ret = waitpid (pid, NULL, 0);
269
270   if (ret < 0 &&
271       errno == EINTR)
272     goto again;
273
274   return ret;
275 }
276
277 static pid_t bus_pid_to_kill = -1;
278
279 static void
280 kill_bus_and_exit (void)
281 {
282   verbose ("Killing message bus and exiting babysitter\n");
283   
284   /* in case these point to any NFS mounts, get rid of them immediately */
285   close (0);
286   close (1);
287   close (2);
288
289   kill (bus_pid_to_kill, SIGTERM);
290   sleep (3);
291   kill (bus_pid_to_kill, SIGKILL);
292
293   exit (0);
294 }
295
296 #ifdef DBUS_BUILD_X11
297 static int
298 x_io_error_handler (Display *xdisplay)
299 {
300   verbose ("X IO error\n");
301   kill_bus_and_exit ();
302   return 0;
303 }
304 #endif
305
306 static int got_sighup = FALSE;
307
308 static void
309 signal_handler (int sig)
310 {
311   switch (sig)
312     {
313     case SIGHUP:
314       got_sighup = TRUE;
315       break;
316     }
317 }
318
319 static void
320 kill_bus_when_session_ends (void)
321 {
322   int tty_fd;
323   int x_fd;
324   fd_set read_set;
325   fd_set err_set;
326   int ret;
327   struct sigaction act;
328   sigset_t empty_mask;
329 #ifdef DBUS_BUILD_X11
330   Display *xdisplay;
331 #endif
332   
333   /* install SIGHUP handler */
334   got_sighup = FALSE;
335   sigemptyset (&empty_mask);
336   act.sa_handler = signal_handler;
337   act.sa_mask    = empty_mask;
338   act.sa_flags   = 0;
339   sigaction (SIGHUP,  &act, 0);
340   
341 #ifdef DBUS_BUILD_X11
342   xdisplay = XOpenDisplay (NULL);
343   if (xdisplay != NULL)
344     {
345       verbose ("Successfully opened X display\n");
346       x_fd = ConnectionNumber (xdisplay);
347       XSetIOErrorHandler (x_io_error_handler);
348     }
349   else
350     x_fd = -1;
351 #else
352   verbose ("Compiled without X11 support\n");
353   x_fd = -1;
354 #endif
355
356   if (isatty (0))
357     tty_fd = 0;
358   else
359     tty_fd = -1;
360
361   if (tty_fd >= 0)
362     verbose ("stdin isatty(), monitoring it\n");
363   else
364     verbose ("stdin was not a TTY, not monitoring it\n");  
365   
366   if (tty_fd < 0 && x_fd < 0)
367     {
368       fprintf (stderr, "No terminal on standard input and no X display; cannot attach message bus to session lifetime\n");
369       exit (1);
370     }
371   
372   while (TRUE)
373     {
374       FD_ZERO (&read_set);
375       FD_ZERO (&err_set);
376
377       if (tty_fd >= 0)
378         {
379           FD_SET (tty_fd, &read_set);
380           FD_SET (tty_fd, &err_set);
381         }
382
383       if (x_fd >= 0)
384         {
385           FD_SET (x_fd, &read_set);
386           FD_SET (x_fd, &err_set);
387         }
388       
389       ret = select (MAX (tty_fd, x_fd) + 1,
390                     &read_set, NULL, &err_set, NULL);
391
392       if (got_sighup)
393         {
394           verbose ("Got SIGHUP, exiting\n");
395           kill_bus_and_exit ();
396         }
397       
398 #ifdef DBUS_BUILD_X11
399       /* Dump events on the floor, and let
400        * IO error handler run if we lose
401        * the X connection
402        */
403       if (x_fd >= 0)
404         verbose ("X fd condition reading = %d error = %d\n",
405                  FD_ISSET (x_fd, &read_set),
406                  FD_ISSET (x_fd, &err_set));
407       
408       if (xdisplay != NULL)
409         {      
410           while (XPending (xdisplay))
411             {
412               XEvent ignored;
413               XNextEvent (xdisplay, &ignored);
414             }
415         }
416 #endif
417
418       if (tty_fd >= 0)
419         {
420           if (FD_ISSET (tty_fd, &read_set))
421             {
422               int bytes_read;
423               char discard[512];
424
425               verbose ("TTY ready for reading\n");
426               
427               bytes_read = read (tty_fd, discard, sizeof (discard));
428
429               verbose ("Read %d bytes from TTY errno = %d\n",
430                        bytes_read, errno);
431               
432               if (bytes_read == 0)
433                 kill_bus_and_exit (); /* EOF */
434               else if (bytes_read < 0 && errno != EINTR)
435                 {
436                   /* This shouldn't happen I don't think; to avoid
437                    * spinning on the fd forever we exit.
438                    */
439                   fprintf (stderr, "dbus-launch: error reading from stdin: %s\n",
440                            strerror (errno));
441                   kill_bus_and_exit ();
442                 }
443             }
444           else if (FD_ISSET (tty_fd, &err_set))
445             {
446               verbose ("TTY has error condition\n");
447               
448               kill_bus_and_exit ();
449             }
450         }
451     }
452 }
453
454 static void
455 babysit (int   exit_with_session,
456          pid_t child_pid,
457          int   read_bus_pid_fd,  /* read pid from here */
458          int   write_bus_pid_fd) /* forward pid to here */
459 {
460   int ret;
461 #define MAX_PID_LEN 64
462   char buf[MAX_PID_LEN];
463   long val;
464   char *end;
465   int dev_null_fd;
466   const char *s;
467
468   verbose ("babysitting, exit_with_session = %d, child_pid = %ld, read_bus_pid_fd = %d, write_bus_pid_fd = %d\n",
469            exit_with_session, (long) child_pid, read_bus_pid_fd, write_bus_pid_fd);
470   
471   /* We chdir ("/") since we are persistent and daemon-like, and fork
472    * again so dbus-launch can reap the parent.  However, we don't
473    * setsid() or close fd 0 because the idea is to remain attached
474    * to the tty and the X server in order to kill the message bus
475    * when the session ends.
476    */
477
478   if (chdir ("/") < 0)
479     {
480       fprintf (stderr, "Could not change to root directory: %s\n",
481                strerror (errno));
482       exit (1);
483     }
484
485   /* Close stdout/stderr so we don't block an "eval" or otherwise
486    * lock up. stdout is still chaining through to dbus-launch
487    * and in turn to the parent shell.
488    */
489   dev_null_fd = open ("/dev/null", O_RDWR);
490   if (dev_null_fd >= 0)
491     {
492       if (!exit_with_session)
493         dup2 (dev_null_fd, 0);
494       dup2 (dev_null_fd, 1);
495       s = getenv ("DBUS_DEBUG_OUTPUT");
496       if (s == NULL || *s == '\0')
497         dup2 (dev_null_fd, 2);
498     }
499   else
500     {
501       fprintf (stderr, "Failed to open /dev/null: %s\n",
502                strerror (errno));
503       /* continue, why not */
504     }
505   
506   ret = fork ();
507
508   if (ret < 0)
509     {
510       fprintf (stderr, "fork() failed in babysitter: %s\n",
511                strerror (errno));
512       exit (1);
513     }
514
515   if (ret > 0)
516     {
517       /* Parent reaps pre-fork part of bus daemon, then exits and is
518        * reaped so the babysitter isn't a zombie
519        */
520
521       verbose ("=== Babysitter's intermediate parent continues again\n");
522       
523       if (do_waitpid (child_pid) < 0)
524         {
525           /* shouldn't happen */
526           fprintf (stderr, "Failed waitpid() waiting for bus daemon's parent\n");
527           exit (1);
528         }
529
530       verbose ("Babysitter's intermediate parent exiting\n");
531       
532       exit (0);
533     }
534
535   /* Child continues */
536   verbose ("=== Babysitter process created\n");
537
538   verbose ("Reading PID from daemon\n");
539   /* Now read data */
540   switch (read_line (read_bus_pid_fd, buf, MAX_PID_LEN))
541     {
542     case READ_STATUS_OK:
543       break;
544     case READ_STATUS_EOF:
545       fprintf (stderr, "EOF reading PID from bus daemon\n");
546       exit (1);
547       break;
548     case READ_STATUS_ERROR:
549       fprintf (stderr, "Error reading PID from bus daemon: %s\n",
550                strerror (errno));
551       exit (1);
552       break;
553     }
554
555   end = NULL;
556   val = strtol (buf, &end, 0);
557   if (buf == end || end == NULL)
558     {
559       fprintf (stderr, "Failed to parse bus PID \"%s\": %s\n",
560                buf, strerror (errno));
561       exit (1);
562     }
563
564   bus_pid_to_kill = val;
565
566   verbose ("Got PID %ld from daemon\n",
567            (long) bus_pid_to_kill);
568   
569   /* Write data to launcher */
570   write_pid (write_bus_pid_fd, bus_pid_to_kill);
571   close (write_bus_pid_fd);
572   
573   if (exit_with_session)
574     {
575       /* Bus is now started and launcher has needed info;
576        * we connect to X display and tty and wait to
577        * kill bus if requested.
578        */
579       
580       kill_bus_when_session_ends ();
581     }
582
583   verbose ("Babysitter exiting\n");
584   
585   exit (0);
586 }
587
588 #define READ_END  0
589 #define WRITE_END 1
590
591 int
592 main (int argc, char **argv)
593 {
594   const char *prev_arg;
595   const char *shname;
596   const char *runprog = NULL;
597   int remaining_args = 0;
598   int exit_with_session;
599   int c_shell_syntax = FALSE;
600   int bourne_shell_syntax = FALSE;
601   int auto_shell_syntax = FALSE;
602   int i;  
603   int ret;
604   int bus_pid_to_launcher_pipe[2];
605   int bus_pid_to_babysitter_pipe[2];
606   int bus_address_to_launcher_pipe[2];
607   char *config_file;
608   
609   exit_with_session = FALSE;
610   config_file = NULL;
611   
612   prev_arg = NULL;
613   i = 1;
614   while (i < argc)
615     {
616       const char *arg = argv[i];
617       
618       if (strcmp (arg, "--help") == 0 ||
619           strcmp (arg, "-h") == 0 ||
620           strcmp (arg, "-?") == 0)
621         usage (0);
622       else if (strcmp (arg, "--auto-syntax") == 0)
623         auto_shell_syntax = TRUE;
624       else if (strcmp (arg, "-c") == 0 ||
625                strcmp (arg, "--csh-syntax") == 0)
626         c_shell_syntax = TRUE;
627       else if (strcmp (arg, "-s") == 0 ||
628                strcmp (arg, "--sh-syntax") == 0)
629         bourne_shell_syntax = TRUE;
630       else if (strcmp (arg, "--version") == 0)
631         version ();
632       else if (strcmp (arg, "--exit-with-session") == 0)
633         exit_with_session = TRUE;
634       else if (strstr (arg, "--config-file=") == arg)
635         {
636           const char *file;
637
638           if (config_file != NULL)
639             {
640               fprintf (stderr, "--config-file given twice\n");
641               exit (1);
642             }
643           
644           file = strchr (arg, '=');
645           ++file;
646
647           config_file = xstrdup (file);
648         }
649       else if (prev_arg &&
650                strcmp (prev_arg, "--config-file") == 0)
651         {
652           if (config_file != NULL)
653             {
654               fprintf (stderr, "--config-file given twice\n");
655               exit (1);
656             }
657
658           config_file = xstrdup (arg);
659         }
660       else if (strcmp (arg, "--config-file") == 0)
661         ; /* wait for next arg */
662       else
663         {
664           runprog = arg;
665           remaining_args = i+1;
666           break;
667         }
668       
669       prev_arg = arg;
670       
671       ++i;
672     }
673
674   if (exit_with_session)
675     verbose ("--exit-with-session enabled\n");
676
677   if (auto_shell_syntax)
678     {
679       if ((shname = getenv ("SHELL")) != NULL)
680        {
681          if (!strncmp (shname + strlen (shname) - 3, "csh", 3))
682            c_shell_syntax = TRUE;
683          else
684            bourne_shell_syntax = TRUE;
685        }
686       else
687        bourne_shell_syntax = TRUE;
688     }  
689
690   if (pipe (bus_pid_to_launcher_pipe) < 0 ||
691       pipe (bus_address_to_launcher_pipe) < 0)
692     {
693       fprintf (stderr,
694                "Failed to create pipe: %s\n",
695                strerror (errno));
696       exit (1);
697     }
698
699   bus_pid_to_babysitter_pipe[READ_END] = -1;
700   bus_pid_to_babysitter_pipe[WRITE_END] = -1;
701   
702   ret = fork ();
703   if (ret < 0)
704     {
705       fprintf (stderr, "Failed to fork: %s\n",
706                strerror (errno));
707       exit (1);
708     }
709
710   if (ret == 0)
711     {
712       /* Child */
713 #define MAX_FD_LEN 64
714       char write_pid_fd_as_string[MAX_FD_LEN];
715       char write_address_fd_as_string[MAX_FD_LEN];
716
717       verbose ("=== Babysitter's intermediate parent created\n");
718       
719       /* Fork once more to create babysitter */
720       
721       if (pipe (bus_pid_to_babysitter_pipe) < 0)
722         {
723           fprintf (stderr,
724                    "Failed to create pipe: %s\n",
725                    strerror (errno));
726           exit (1);              
727         }
728       
729       ret = fork ();
730       if (ret < 0)
731         {
732           fprintf (stderr, "Failed to fork: %s\n",
733                    strerror (errno));
734           exit (1);
735         }
736       
737       if (ret > 0)
738         {
739           /* In babysitter */
740           verbose ("=== Babysitter's intermediate parent continues\n");
741           
742           close (bus_pid_to_launcher_pipe[READ_END]);
743           close (bus_address_to_launcher_pipe[READ_END]);
744           close (bus_address_to_launcher_pipe[WRITE_END]);
745           close (bus_pid_to_babysitter_pipe[WRITE_END]);
746           
747           /* babysit() will fork *again*
748            * and will also reap the pre-forked bus
749            * daemon
750            */
751           babysit (exit_with_session, ret,
752                    bus_pid_to_babysitter_pipe[READ_END],
753                    bus_pid_to_launcher_pipe[WRITE_END]);
754           exit (0);
755         }
756
757       verbose ("=== Bus exec process created\n");
758       
759       /* Now we are the bus process (well, almost;
760        * dbus-daemon itself forks again)
761        */
762       close (bus_pid_to_launcher_pipe[READ_END]);
763       close (bus_address_to_launcher_pipe[READ_END]);
764       close (bus_pid_to_babysitter_pipe[READ_END]);
765       close (bus_pid_to_launcher_pipe[WRITE_END]);
766
767       sprintf (write_pid_fd_as_string,
768                "%d", bus_pid_to_babysitter_pipe[WRITE_END]);
769
770       sprintf (write_address_fd_as_string,
771                "%d", bus_address_to_launcher_pipe[WRITE_END]);
772
773       verbose ("Calling exec()\n");
774       
775       execlp ("dbus-daemon",
776               "dbus-daemon",
777               "--fork",
778               "--print-pid", write_pid_fd_as_string,
779               "--print-address", write_address_fd_as_string,
780               config_file ? "--config-file" : "--session",
781               config_file, /* has to be last in this varargs list */
782               NULL);
783
784       fprintf (stderr,
785                "Failed to execute message bus daemon: %s\n",
786                strerror (errno));
787       exit (1);
788     }
789   else
790     {
791       /* Parent */
792 #define MAX_ADDR_LEN 512
793       pid_t bus_pid;  
794       char bus_address[MAX_ADDR_LEN];
795
796       verbose ("=== Parent dbus-launch continues\n");
797       
798       close (bus_pid_to_launcher_pipe[WRITE_END]);
799       close (bus_address_to_launcher_pipe[WRITE_END]);
800
801       verbose ("Waiting for babysitter's intermediate parent\n");
802       
803       /* Immediately reap parent of babysitter
804        * (which was created just for us to reap)
805        */
806       if (do_waitpid (ret) < 0)
807         {
808           fprintf (stderr, "Failed to waitpid() for babysitter intermediate process: %s\n",
809                    strerror (errno));
810           exit (1);
811         }
812
813       verbose ("Reading address from bus\n");
814       
815       /* Read the pipe data, print, and exit */
816       switch (read_line (bus_address_to_launcher_pipe[READ_END],
817                          bus_address, MAX_ADDR_LEN))
818         {
819         case READ_STATUS_OK:
820           break;
821         case READ_STATUS_EOF:
822           fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
823           exit (1);
824           break;
825         case READ_STATUS_ERROR:
826           fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
827                    strerror (errno));
828           exit (1);
829           break;
830         }
831         
832       close (bus_address_to_launcher_pipe[READ_END]);
833
834       verbose ("Reading PID from babysitter\n");
835       
836       switch (read_pid (bus_pid_to_launcher_pipe[READ_END], &bus_pid))
837         {
838         case READ_STATUS_OK:
839           break;
840         case READ_STATUS_EOF:
841           fprintf (stderr, "EOF in dbus-launch reading address from bus daemon\n");
842           exit (1);
843           break;
844         case READ_STATUS_ERROR:
845           fprintf (stderr, "Error in dbus-launch reading address from bus daemon: %s\n",
846                    strerror (errno));
847           exit (1);
848           break;
849         }
850
851       close (bus_pid_to_launcher_pipe[READ_END]);
852       
853       if (runprog)
854         {
855           char *envvar;
856           char **args;
857
858           envvar = malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=") + strlen (bus_address) + 1);
859           args = malloc (sizeof (char *) * ((argc-remaining_args)+2));
860
861           if (envvar == NULL || args == NULL)
862             goto oom;
863
864           args[0] = xstrdup (runprog);
865           if (!args[0])
866             goto oom;
867           for (i = 1; i <= (argc-remaining_args); i++)
868             {
869               size_t len = strlen (argv[remaining_args+i-1])+1;
870               args[i] = malloc (len);
871               if (!args[i])
872                 goto oom;
873               strncpy (args[i], argv[remaining_args+i-1], len);
874             }
875           args[i] = NULL;
876
877           strcpy (envvar, "DBUS_SESSION_BUS_ADDRESS=");
878           strcat (envvar, bus_address);
879           putenv (envvar);
880
881           execvp (runprog, args);
882           fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
883           exit (1);
884         }
885       else
886         {
887           if (c_shell_syntax)
888             printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s'\n", bus_address);     
889           else
890             {
891               printf ("DBUS_SESSION_BUS_ADDRESS='%s'\n", bus_address);
892               if (bourne_shell_syntax)
893                 printf ("export DBUS_SESSION_BUS_ADDRESS\n");
894             }
895           if (c_shell_syntax)
896             printf ("set DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
897           else
898             printf ("DBUS_SESSION_BUS_PID=%ld\n", (long) bus_pid);
899         }
900           
901       verbose ("dbus-launch exiting\n");
902
903       fflush (stdout);
904       fflush (stderr);
905       close (1);
906       close (2);
907       
908       exit (0);
909     } 
910   
911   return 0;
912  oom:
913   fprintf (stderr, "Out of memory!");
914   exit (1);
915 }