2005-01-27 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-spawn.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-spawn.c Wrapper around fork/exec
3  * 
4  * Copyright (C) 2002, 2003, 2004  Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #include "dbus-spawn.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-internals.h"
27 #include "dbus-test.h"
28 #include "dbus-protocol.h"
29
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <sys/wait.h>
34 #include <errno.h>
35 #include <stdlib.h>
36
37 /**
38  * @addtogroup DBusInternalsUtils
39  * @{
40  */
41
42 /*
43  * I'm pretty sure this whole spawn file could be made simpler,
44  * if you thought about it a bit.
45  */
46
47 /**
48  * Enumeration for status of a read()
49  */
50 typedef enum
51 {
52   READ_STATUS_OK,    /**< Read succeeded */
53   READ_STATUS_ERROR, /**< Some kind of error */
54   READ_STATUS_EOF    /**< EOF returned */
55 } ReadStatus;
56
57 static ReadStatus
58 read_ints (int        fd,
59            int       *buf,
60            int        n_ints_in_buf,
61            int       *n_ints_read,
62            DBusError *error)
63 {
64   size_t bytes = 0;    
65   ReadStatus retval;
66   
67   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
68
69   retval = READ_STATUS_OK;
70   
71   while (TRUE)
72     {
73       size_t chunk;
74       size_t to_read;
75
76       to_read = sizeof (int) * n_ints_in_buf - bytes;
77
78       if (to_read == 0)
79         break;
80
81     again:
82       
83       chunk = read (fd,
84                     ((char*)buf) + bytes,
85                     to_read);
86       
87       if (chunk < 0 && errno == EINTR)
88         goto again;
89           
90       if (chunk < 0)
91         {
92           dbus_set_error (error,
93                           DBUS_ERROR_SPAWN_FAILED,
94                           "Failed to read from child pipe (%s)",
95                           _dbus_strerror (errno));
96
97           retval = READ_STATUS_ERROR;
98           break;
99         }
100       else if (chunk == 0)
101         {
102           retval = READ_STATUS_EOF;
103           break; /* EOF */
104         }
105       else /* chunk > 0 */
106         bytes += chunk;
107     }
108
109   *n_ints_read = (int)(bytes / sizeof(int));
110
111   return retval;
112 }
113
114 static ReadStatus
115 read_pid (int        fd,
116           pid_t     *buf,
117           DBusError *error)
118 {
119   size_t bytes = 0;    
120   ReadStatus retval;
121   
122   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
123
124   retval = READ_STATUS_OK;
125   
126   while (TRUE)
127     {
128       size_t chunk;    
129       size_t to_read;
130       
131       to_read = sizeof (pid_t) - bytes;
132
133       if (to_read == 0)
134         break;
135
136     again:
137       
138       chunk = read (fd,
139                     ((char*)buf) + bytes,
140                     to_read);
141       if (chunk < 0 && errno == EINTR)
142         goto again;
143           
144       if (chunk < 0)
145         {
146           dbus_set_error (error,
147                           DBUS_ERROR_SPAWN_FAILED,
148                           "Failed to read from child pipe (%s)",
149                           _dbus_strerror (errno));
150
151           retval = READ_STATUS_ERROR;
152           break;
153         }
154       else if (chunk == 0)
155         {
156           retval = READ_STATUS_EOF;
157           break; /* EOF */
158         }
159       else /* chunk > 0 */
160         bytes += chunk;
161     }
162
163   return retval;
164 }
165
166 /* The implementation uses an intermediate child between the main process
167  * and the grandchild. The grandchild is our spawned process. The intermediate
168  * child is a babysitter process; it keeps track of when the grandchild
169  * exits/crashes, and reaps the grandchild.
170  */
171
172 /* Messages from children to parents */
173 enum
174 {
175   CHILD_EXITED,            /* This message is followed by the exit status int */
176   CHILD_FORK_FAILED,       /* Followed by errno */
177   CHILD_EXEC_FAILED,       /* Followed by errno */
178   CHILD_PID                /* Followed by pid_t */
179 };
180
181 /**
182  * Babysitter implementation details
183  */
184 struct DBusBabysitter
185 {
186   int refcount; /**< Reference count */
187
188   char *executable; /**< executable name to use in error messages */
189   
190   int socket_to_babysitter; /**< Connection to the babysitter process */
191   int error_pipe_from_child; /**< Connection to the process that does the exec() */
192   
193   pid_t sitter_pid;  /**< PID Of the babysitter */
194   pid_t grandchild_pid; /**< PID of the grandchild */
195
196   DBusWatchList *watches; /**< Watches */
197
198   DBusWatch *error_watch; /**< Error pipe watch */
199   DBusWatch *sitter_watch; /**< Sitter pipe watch */
200
201   int errnum; /**< Error number */
202   int status; /**< Exit status code */
203   unsigned int have_child_status : 1; /**< True if child status has been reaped */
204   unsigned int have_fork_errnum : 1; /**< True if we have an error code from fork() */
205   unsigned int have_exec_errnum : 1; /**< True if we have an error code from exec() */
206 };
207
208 static DBusBabysitter*
209 _dbus_babysitter_new (void)
210 {
211   DBusBabysitter *sitter;
212
213   sitter = dbus_new0 (DBusBabysitter, 1);
214   if (sitter == NULL)
215     return NULL;
216
217   sitter->refcount = 1;
218
219   sitter->socket_to_babysitter = -1;
220   sitter->error_pipe_from_child = -1;
221   
222   sitter->sitter_pid = -1;
223   sitter->grandchild_pid = -1;
224
225   sitter->watches = _dbus_watch_list_new ();
226   if (sitter->watches == NULL)
227     goto failed;
228   
229   return sitter;
230
231  failed:
232   _dbus_babysitter_unref (sitter);
233   return NULL;
234 }
235
236 /**
237  * Increment the reference count on the babysitter object.
238  *
239  * @param sitter the babysitter
240  * @returns the babysitter
241  */
242 DBusBabysitter *
243 _dbus_babysitter_ref (DBusBabysitter *sitter)
244 {
245   _dbus_assert (sitter != NULL);
246   _dbus_assert (sitter->refcount > 0);
247   
248   sitter->refcount += 1;
249
250   return sitter;
251 }
252
253 /**
254  * Decrement the reference count on the babysitter object.
255  *
256  * @param sitter the babysitter
257  */
258 void
259 _dbus_babysitter_unref (DBusBabysitter *sitter)
260 {
261   _dbus_assert (sitter != NULL);
262   _dbus_assert (sitter->refcount > 0);
263   
264   sitter->refcount -= 1;
265   if (sitter->refcount == 0)
266     {      
267       if (sitter->socket_to_babysitter >= 0)
268         {
269           close (sitter->socket_to_babysitter);
270           sitter->socket_to_babysitter = -1;
271         }
272
273       if (sitter->error_pipe_from_child >= 0)
274         {
275           close (sitter->error_pipe_from_child);
276           sitter->error_pipe_from_child = -1;
277         }
278
279       if (sitter->sitter_pid != -1)
280         {
281           int status;
282           int ret;
283
284           /* Reap the babysitter */
285         again:
286           ret = waitpid (sitter->sitter_pid, &status, 0);
287           if (ret < 0)
288             {
289               if (errno == EINTR)
290                 goto again;
291               else if (errno == ECHILD)
292                 _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
293               else
294                 _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
295                             errno, _dbus_strerror (errno));
296             }
297           else
298             {
299               _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
300                              (long) ret, (long) sitter->sitter_pid);
301               
302               if (WIFEXITED (sitter->status))
303                 _dbus_verbose ("Babysitter exited with status %d\n",
304                                WEXITSTATUS (sitter->status));
305               else if (WIFSIGNALED (sitter->status))
306                 _dbus_verbose ("Babysitter received signal %d\n",
307                                WTERMSIG (sitter->status));
308               else
309                 _dbus_verbose ("Babysitter exited abnormally\n");
310             }
311
312           sitter->sitter_pid = -1;
313         }
314       
315       if (sitter->error_watch)
316         {
317           _dbus_watch_invalidate (sitter->error_watch);
318           _dbus_watch_unref (sitter->error_watch);
319           sitter->error_watch = NULL;
320         }
321
322       if (sitter->sitter_watch)
323         {
324           _dbus_watch_invalidate (sitter->sitter_watch);
325           _dbus_watch_unref (sitter->sitter_watch);
326           sitter->sitter_watch = NULL;
327         }
328       
329       if (sitter->watches)
330         _dbus_watch_list_free (sitter->watches);
331
332       dbus_free (sitter->executable);
333       
334       dbus_free (sitter);
335     }
336 }
337
338 static ReadStatus
339 read_data (DBusBabysitter *sitter,
340            int             fd)
341 {
342   int what;
343   int got;
344   DBusError error;
345   ReadStatus r;
346   
347   dbus_error_init (&error);
348   
349   r = read_ints (fd, &what, 1, &got, &error);
350
351   switch (r)
352     {
353     case READ_STATUS_ERROR:
354       _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
355       dbus_error_free (&error);
356       return r;
357
358     case READ_STATUS_EOF:
359       return r;
360
361     case READ_STATUS_OK:
362       break;
363     }
364   
365   if (got == 1)
366     {
367       switch (what)
368         {
369         case CHILD_EXITED:
370         case CHILD_FORK_FAILED:
371         case CHILD_EXEC_FAILED:
372           {
373             int arg;
374             
375             r = read_ints (fd, &arg, 1, &got, &error);
376
377             switch (r)
378               {
379               case READ_STATUS_ERROR:
380                 _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
381                 dbus_error_free (&error);
382                 return r;
383               case READ_STATUS_EOF:
384                 return r;
385               case READ_STATUS_OK:
386                 break;
387               }
388             
389             if (got == 1)
390               {
391                 if (what == CHILD_EXITED)
392                   {
393                     sitter->have_child_status = TRUE;
394                     sitter->status = arg;
395                     _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
396                                    WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
397                                    WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
398                   }
399                 else if (what == CHILD_FORK_FAILED)
400                   {
401                     sitter->have_fork_errnum = TRUE;
402                     sitter->errnum = arg;
403                     _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
404                   }
405                 else if (what == CHILD_EXEC_FAILED)
406                   {
407                     sitter->have_exec_errnum = TRUE;
408                     sitter->errnum = arg;
409                     _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
410                   }
411               }
412           }
413           break;
414         case CHILD_PID:
415           {
416             pid_t pid = -1;
417
418             r = read_pid (fd, &pid, &error);
419             
420             switch (r)
421               {
422               case READ_STATUS_ERROR:
423                 _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
424                 dbus_error_free (&error);
425                 return r;
426               case READ_STATUS_EOF:
427                 return r;
428               case READ_STATUS_OK:
429                 break;
430               }
431             
432             sitter->grandchild_pid = pid;
433             
434             _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
435           }
436           break;
437         default:
438           _dbus_warn ("Unknown message received from babysitter process\n");
439           break;
440         }
441     }
442
443   return r;
444 }
445
446 static void
447 close_socket_to_babysitter (DBusBabysitter *sitter)
448 {
449   _dbus_verbose ("Closing babysitter\n");
450   close (sitter->socket_to_babysitter);
451   sitter->socket_to_babysitter = -1;
452 }
453
454 static void
455 close_error_pipe_from_child (DBusBabysitter *sitter)
456 {
457   _dbus_verbose ("Closing child error\n");
458   close (sitter->error_pipe_from_child);
459   sitter->error_pipe_from_child = -1;
460 }
461
462 static void
463 handle_babysitter_socket (DBusBabysitter *sitter,
464                           int             revents)
465 {
466   /* Even if we have POLLHUP, we want to keep reading
467    * data until POLLIN goes away; so this function only
468    * looks at HUP/ERR if no IN is set.
469    */
470   if (revents & _DBUS_POLLIN)
471     {
472       _dbus_verbose ("Reading data from babysitter\n");
473       if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
474         close_socket_to_babysitter (sitter);
475     }
476   else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
477     {
478       close_socket_to_babysitter (sitter);
479     }
480 }
481
482 static void
483 handle_error_pipe (DBusBabysitter *sitter,
484                    int             revents)
485 {
486   if (revents & _DBUS_POLLIN)
487     {
488       _dbus_verbose ("Reading data from child error\n");
489       if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
490         close_error_pipe_from_child (sitter);
491     }
492   else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
493     {
494       close_error_pipe_from_child (sitter);
495     }
496 }
497
498 /* returns whether there were any poll events handled */
499 static dbus_bool_t
500 babysitter_iteration (DBusBabysitter *sitter,
501                       dbus_bool_t     block)
502 {
503   DBusPollFD fds[2];
504   int i;
505   dbus_bool_t descriptors_ready;
506
507   descriptors_ready = FALSE;
508   
509   i = 0;
510
511   if (sitter->error_pipe_from_child >= 0)
512     {
513       fds[i].fd = sitter->error_pipe_from_child;
514       fds[i].events = _DBUS_POLLIN;
515       fds[i].revents = 0;
516       ++i;
517     }
518   
519   if (sitter->socket_to_babysitter >= 0)
520     {
521       fds[i].fd = sitter->socket_to_babysitter;
522       fds[i].events = _DBUS_POLLIN;
523       fds[i].revents = 0;
524       ++i;
525     }
526
527   if (i > 0)
528     {
529       int ret;
530
531       ret = _dbus_poll (fds, i, 0);
532       if (ret == 0 && block)
533         ret = _dbus_poll (fds, i, -1);
534       
535       if (ret > 0)
536         {
537           descriptors_ready = TRUE;
538           
539           while (i > 0)
540             {
541               --i;
542               if (fds[i].fd == sitter->error_pipe_from_child)
543                 handle_error_pipe (sitter, fds[i].revents);
544               else if (fds[i].fd == sitter->socket_to_babysitter)
545                 handle_babysitter_socket (sitter, fds[i].revents);
546             }
547         }
548     }
549
550   return descriptors_ready;
551 }
552
553 /**
554  * Macro returns #TRUE if the babysitter still has live sockets open to the
555  * babysitter child or the grandchild.
556  */
557 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
558
559 /**
560  * Blocks until the babysitter process gives us the PID of the spawned grandchild,
561  * then kills the spawned grandchild.
562  *
563  * @param sitter the babysitter object
564  */
565 void
566 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
567 {
568   /* be sure we have the PID of the child */
569   while (LIVE_CHILDREN (sitter) &&
570          sitter->grandchild_pid == -1)
571     babysitter_iteration (sitter, TRUE);
572
573   _dbus_verbose ("Got child PID %ld for killing\n",
574                  (long) sitter->grandchild_pid);
575   
576   if (sitter->grandchild_pid == -1)
577     return; /* child is already dead, or we're so hosed we'll never recover */
578
579   kill (sitter->grandchild_pid, SIGKILL);
580 }
581
582 /**
583  * Checks whether the child has exited, without blocking.
584  *
585  * @param sitter the babysitter
586  */
587 dbus_bool_t
588 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
589 {
590
591   /* Be sure we're up-to-date */
592   while (LIVE_CHILDREN (sitter) &&
593          babysitter_iteration (sitter, FALSE))
594     ;
595
596   /* We will have exited the babysitter when the child has exited */
597   return sitter->socket_to_babysitter < 0;
598 }
599
600 /**
601  * Sets the #DBusError with an explanation of why the spawned
602  * child process exited (on a signal, or whatever). If
603  * the child process has not exited, does nothing (error
604  * will remain unset).
605  *
606  * @param sitter the babysitter
607  * @param error an error to fill in
608  */
609 void
610 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
611                                        DBusError      *error)
612 {
613   if (!_dbus_babysitter_get_child_exited (sitter))
614     return;
615
616   /* Note that if exec fails, we will also get a child status
617    * from the babysitter saying the child exited,
618    * so we need to give priority to the exec error
619    */
620   if (sitter->have_exec_errnum)
621     {
622       dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
623                       "Failed to execute program %s: %s",
624                       sitter->executable, _dbus_strerror (sitter->errnum));
625     }
626   else if (sitter->have_fork_errnum)
627     {
628       dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
629                       "Failed to fork a new process %s: %s",
630                       sitter->executable, _dbus_strerror (sitter->errnum));
631     }
632   else if (sitter->have_child_status)
633     {
634       if (WIFEXITED (sitter->status))
635         dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
636                         "Process %s exited with status %d",
637                         sitter->executable, WEXITSTATUS (sitter->status));
638       else if (WIFSIGNALED (sitter->status))
639         dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
640                         "Process %s received signal %d",
641                         sitter->executable, WTERMSIG (sitter->status));
642       else
643         dbus_set_error (error, DBUS_ERROR_FAILED,
644                         "Process %s exited abnormally",
645                         sitter->executable);
646     }
647   else
648     {
649       dbus_set_error (error, DBUS_ERROR_FAILED,
650                       "Process %s exited, reason unknown",
651                       sitter->executable);
652     }
653 }
654
655 /**
656  * Sets watch functions to notify us when the
657  * babysitter object needs to read/write file descriptors.
658  *
659  * @param sitter the babysitter
660  * @param add_function function to begin monitoring a new descriptor.
661  * @param remove_function function to stop monitoring a descriptor.
662  * @param toggled_function function to notify when the watch is enabled/disabled
663  * @param data data to pass to add_function and remove_function.
664  * @param free_data_function function to be called to free the data.
665  * @returns #FALSE on failure (no memory)
666  */
667 dbus_bool_t
668 _dbus_babysitter_set_watch_functions (DBusBabysitter            *sitter,
669                                       DBusAddWatchFunction       add_function,
670                                       DBusRemoveWatchFunction    remove_function,
671                                       DBusWatchToggledFunction   toggled_function,
672                                       void                      *data,
673                                       DBusFreeFunction           free_data_function)
674 {
675   return _dbus_watch_list_set_functions (sitter->watches,
676                                          add_function,
677                                          remove_function,
678                                          toggled_function,
679                                          data,
680                                          free_data_function);
681 }
682
683 static dbus_bool_t
684 handle_watch (DBusWatch       *watch,
685               unsigned int     condition,
686               void            *data)
687 {
688   DBusBabysitter *sitter = data;
689   int revents;
690   int fd;
691   
692   revents = 0;
693   if (condition & DBUS_WATCH_READABLE)
694     revents |= _DBUS_POLLIN;
695   if (condition & DBUS_WATCH_ERROR)
696     revents |= _DBUS_POLLERR;
697   if (condition & DBUS_WATCH_HANGUP)
698     revents |= _DBUS_POLLHUP;
699
700   fd = dbus_watch_get_fd (watch);
701
702   if (fd == sitter->error_pipe_from_child)
703     handle_error_pipe (sitter, revents);
704   else if (fd == sitter->socket_to_babysitter)
705     handle_babysitter_socket (sitter, revents);
706
707   while (LIVE_CHILDREN (sitter) &&
708          babysitter_iteration (sitter, FALSE))
709     ;
710   
711   return TRUE;
712 }
713
714 /** Helps remember which end of the pipe is which */
715 #define READ_END 0
716 /** Helps remember which end of the pipe is which */
717 #define WRITE_END 1
718
719
720 /* Avoids a danger in threaded situations (calling close()
721  * on a file descriptor twice, and another thread has
722  * re-opened it since the first close)
723  */
724 static int
725 close_and_invalidate (int *fd)
726 {
727   int ret;
728
729   if (*fd < 0)
730     return -1;
731   else
732     {
733       ret = close (*fd);
734       *fd = -1;
735     }
736
737   return ret;
738 }
739
740 static dbus_bool_t
741 make_pipe (int         p[2],
742            DBusError  *error)
743 {
744   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
745   
746   if (pipe (p) < 0)
747     {
748       dbus_set_error (error,
749                       DBUS_ERROR_SPAWN_FAILED,
750                       "Failed to create pipe for communicating with child process (%s)",
751                       _dbus_strerror (errno));
752       return FALSE;
753     }
754
755   return TRUE;
756 }
757
758 static void
759 do_write (int fd, const void *buf, size_t count)
760 {
761   size_t bytes_written;
762   int ret;
763   
764   bytes_written = 0;
765   
766  again:
767   
768   ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
769
770   if (ret < 0)
771     {
772       if (errno == EINTR)
773         goto again;
774       else
775         {
776           _dbus_warn ("Failed to write data to pipe!\n");
777           exit (1); /* give up, we suck */
778         }
779     }
780   else
781     bytes_written += ret;
782   
783   if (bytes_written < count)
784     goto again;
785 }
786
787 static void
788 write_err_and_exit (int fd, int msg)
789 {
790   int en = errno;
791
792   do_write (fd, &msg, sizeof (msg));
793   do_write (fd, &en, sizeof (en));
794   
795   exit (1);
796 }
797
798 static void
799 write_pid (int fd, pid_t pid)
800 {
801   int msg = CHILD_PID;
802   
803   do_write (fd, &msg, sizeof (msg));
804   do_write (fd, &pid, sizeof (pid));
805 }
806
807 static void
808 write_status_and_exit (int fd, int status)
809 {
810   int msg = CHILD_EXITED;
811   
812   do_write (fd, &msg, sizeof (msg));
813   do_write (fd, &status, sizeof (status));
814   
815   exit (0);
816 }
817
818 static void
819 do_exec (int                       child_err_report_fd,
820          char                    **argv,
821          DBusSpawnChildSetupFunc   child_setup,
822          void                     *user_data)
823 {
824 #ifdef DBUS_BUILD_TESTS
825   int i, max_open;
826 #endif
827
828   _dbus_verbose_reset ();
829   _dbus_verbose ("Child process has PID %lu\n",
830                  _dbus_getpid ());
831   
832   if (child_setup)
833     (* child_setup) (user_data);
834
835 #ifdef DBUS_BUILD_TESTS
836   max_open = sysconf (_SC_OPEN_MAX);
837   
838   for (i = 3; i < max_open; i++)
839     {
840       int retval;
841
842       if (i == child_err_report_fd)
843         continue;
844       
845       retval = fcntl (i, F_GETFD);
846
847       if (retval != -1 && !(retval & FD_CLOEXEC))
848         _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
849     }
850 #endif
851   
852   execv (argv[0], argv);
853   
854   /* Exec failed */
855   write_err_and_exit (child_err_report_fd,
856                       CHILD_EXEC_FAILED);
857 }
858
859 static void
860 check_babysit_events (pid_t grandchild_pid,
861                       int   parent_pipe,
862                       int   revents)
863 {
864   pid_t ret;
865   int status;
866   
867   do
868     {
869       ret = waitpid (grandchild_pid, &status, WNOHANG);
870       /* The man page says EINTR can't happen with WNOHANG,
871        * but there are reports of it (maybe only with valgrind?)
872        */
873     }
874   while (ret < 0 && errno == EINTR);
875
876   if (ret == 0)
877     {
878       _dbus_verbose ("no child exited\n");
879       
880       ; /* no child exited */
881     }
882   else if (ret < 0)
883     {
884       /* This isn't supposed to happen. */
885       _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
886                   _dbus_strerror (errno));
887       exit (1);
888     }
889   else if (ret == grandchild_pid)
890     {
891       /* Child exited */
892       _dbus_verbose ("reaped child pid %ld\n", (long) ret);
893       
894       write_status_and_exit (parent_pipe, status);
895     }
896   else
897     {
898       _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
899                   (int) ret);
900       exit (1);
901     }
902
903   if (revents & _DBUS_POLLIN)
904     {
905       _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
906     }
907
908   if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
909     {
910       /* Parent is gone, so we just exit */
911       _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
912       exit (0);
913     }
914 }
915
916 static int babysit_sigchld_pipe = -1;
917
918 static void
919 babysit_signal_handler (int signo)
920 {
921   char b = '\0';
922  again:
923   write (babysit_sigchld_pipe, &b, 1);
924   if (errno == EINTR)
925     goto again;
926 }
927
928 static void
929 babysit (pid_t grandchild_pid,
930          int   parent_pipe)
931 {
932   int sigchld_pipe[2];
933
934   /* We don't exec, so we keep parent state, such as the pid that
935    * _dbus_verbose() uses. Reset the pid here.
936    */
937   _dbus_verbose_reset ();
938   
939   /* I thought SIGCHLD would just wake up the poll, but
940    * that didn't seem to work, so added this pipe.
941    * Probably the pipe is more likely to work on busted
942    * operating systems anyhow.
943    */
944   if (pipe (sigchld_pipe) < 0)
945     {
946       _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
947       exit (1);
948     }
949
950   babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
951
952   _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
953   
954   write_pid (parent_pipe, grandchild_pid);
955
956   check_babysit_events (grandchild_pid, parent_pipe, 0);
957
958   while (TRUE)
959     {
960       DBusPollFD pfds[2];
961       
962       pfds[0].fd = parent_pipe;
963       pfds[0].events = _DBUS_POLLIN;
964       pfds[0].revents = 0;
965
966       pfds[1].fd = sigchld_pipe[READ_END];
967       pfds[1].events = _DBUS_POLLIN;
968       pfds[1].revents = 0;
969       
970       _dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1);
971
972       if (pfds[0].revents != 0)
973         {
974           check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
975         }
976       else if (pfds[1].revents & _DBUS_POLLIN)
977         {
978           char b;
979           read (sigchld_pipe[READ_END], &b, 1);
980           /* do waitpid check */
981           check_babysit_events (grandchild_pid, parent_pipe, 0);
982         }
983     }
984   
985   exit (1);
986 }
987
988 /**
989  * Spawns a new process. The executable name and argv[0]
990  * are the same, both are provided in argv[0]. The child_setup
991  * function is passed the given user_data and is run in the child
992  * just before calling exec().
993  *
994  * Also creates a "babysitter" which tracks the status of the
995  * child process, advising the parent if the child exits.
996  * If the spawn fails, no babysitter is created.
997  * If sitter_p is #NULL, no babysitter is kept.
998  *
999  * @param sitter_p return location for babysitter or #NULL
1000  * @param argv the executable and arguments
1001  * @param child_setup function to call in child pre-exec()
1002  * @param user_data user data for setup function
1003  * @param error error object to be filled in if function fails
1004  * @returns #TRUE on success, #FALSE if error is filled in
1005  */
1006 dbus_bool_t
1007 _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
1008                                    char                    **argv,
1009                                    DBusSpawnChildSetupFunc   child_setup,
1010                                    void                     *user_data,
1011                                    DBusError                *error)
1012 {
1013   DBusBabysitter *sitter;
1014   int child_err_report_pipe[2] = { -1, -1 };
1015   int babysitter_pipe[2] = { -1, -1 };
1016   pid_t pid;
1017   
1018   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1019
1020   *sitter_p = NULL;
1021   sitter = NULL;
1022
1023   sitter = _dbus_babysitter_new ();
1024   if (sitter == NULL)
1025     {
1026       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1027       return FALSE;
1028     }
1029
1030   sitter->executable = _dbus_strdup (argv[0]);
1031   if (sitter->executable == NULL)
1032     {
1033       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1034       goto cleanup_and_fail;
1035     }
1036   
1037   if (!make_pipe (child_err_report_pipe, error))
1038     goto cleanup_and_fail;
1039
1040   _dbus_fd_set_close_on_exec (child_err_report_pipe[READ_END]);
1041   
1042   if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1043     goto cleanup_and_fail;
1044
1045   _dbus_fd_set_close_on_exec (babysitter_pipe[0]);
1046   _dbus_fd_set_close_on_exec (babysitter_pipe[1]);
1047
1048   /* Setting up the babysitter is only useful in the parent,
1049    * but we don't want to run out of memory and fail
1050    * after we've already forked, since then we'd leak
1051    * child processes everywhere.
1052    */
1053   sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1054                                          DBUS_WATCH_READABLE,
1055                                          TRUE, handle_watch, sitter, NULL);
1056   if (sitter->error_watch == NULL)
1057     {
1058       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1059       goto cleanup_and_fail;
1060     }
1061         
1062   if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->error_watch))
1063     {
1064       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1065       goto cleanup_and_fail;
1066     }
1067       
1068   sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1069                                           DBUS_WATCH_READABLE,
1070                                           TRUE, handle_watch, sitter, NULL);
1071   if (sitter->sitter_watch == NULL)
1072     {
1073       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1074       goto cleanup_and_fail;
1075     }
1076       
1077   if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->sitter_watch))
1078     {
1079       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1080       goto cleanup_and_fail;
1081     }
1082
1083   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1084   
1085   pid = fork ();
1086   
1087   if (pid < 0)
1088     {
1089       dbus_set_error (error,
1090                       DBUS_ERROR_SPAWN_FORK_FAILED,
1091                       "Failed to fork (%s)",
1092                       _dbus_strerror (errno));
1093       goto cleanup_and_fail;
1094     }
1095   else if (pid == 0)
1096     {
1097       /* Immediate child, this is the babysitter process. */
1098       int grandchild_pid;
1099       
1100       /* Be sure we crash if the parent exits
1101        * and we write to the err_report_pipe
1102        */
1103       signal (SIGPIPE, SIG_DFL);
1104
1105       /* Close the parent's end of the pipes. */
1106       close_and_invalidate (&child_err_report_pipe[READ_END]);
1107       close_and_invalidate (&babysitter_pipe[0]);
1108       
1109       /* Create the child that will exec () */
1110       grandchild_pid = fork ();
1111       
1112       if (grandchild_pid < 0)
1113         {
1114           write_err_and_exit (babysitter_pipe[1],
1115                               CHILD_FORK_FAILED);
1116           _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1117         }
1118       else if (grandchild_pid == 0)
1119         {
1120           do_exec (child_err_report_pipe[WRITE_END],
1121                    argv,
1122                    child_setup, user_data);
1123           _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1124         }
1125       else
1126         {
1127           babysit (grandchild_pid, babysitter_pipe[1]);
1128           _dbus_assert_not_reached ("Got to code after babysit()");
1129         }
1130     }
1131   else
1132     {      
1133       /* Close the uncared-about ends of the pipes */
1134       close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1135       close_and_invalidate (&babysitter_pipe[1]);
1136
1137       sitter->socket_to_babysitter = babysitter_pipe[0];
1138       babysitter_pipe[0] = -1;
1139       
1140       sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1141       child_err_report_pipe[READ_END] = -1;
1142
1143       sitter->sitter_pid = pid;
1144
1145       if (sitter_p != NULL)
1146         *sitter_p = sitter;
1147       else
1148         _dbus_babysitter_unref (sitter);
1149
1150       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1151       
1152       return TRUE;
1153     }
1154
1155  cleanup_and_fail:
1156
1157   _DBUS_ASSERT_ERROR_IS_SET (error);
1158   
1159   close_and_invalidate (&child_err_report_pipe[READ_END]);
1160   close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1161   close_and_invalidate (&babysitter_pipe[0]);
1162   close_and_invalidate (&babysitter_pipe[1]);
1163
1164   if (sitter != NULL)
1165     _dbus_babysitter_unref (sitter);
1166   
1167   return FALSE;
1168 }
1169
1170 /** @} */
1171
1172 #ifdef DBUS_BUILD_TESTS
1173
1174 static void
1175 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1176 {
1177   while (LIVE_CHILDREN (sitter))
1178     babysitter_iteration (sitter, TRUE);
1179 }
1180
1181 static dbus_bool_t
1182 check_spawn_nonexistent (void *data)
1183 {
1184   char *argv[4] = { NULL, NULL, NULL, NULL };
1185   DBusBabysitter *sitter;
1186   DBusError error;
1187   
1188   sitter = NULL;
1189   
1190   dbus_error_init (&error);
1191
1192   /*** Test launching nonexistent binary */
1193   
1194   argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1195   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1196                                          NULL, NULL,
1197                                          &error))
1198     {
1199       _dbus_babysitter_block_for_child_exit (sitter);
1200       _dbus_babysitter_set_child_exit_error (sitter, &error);
1201     }
1202
1203   if (sitter)
1204     _dbus_babysitter_unref (sitter);
1205
1206   if (!dbus_error_is_set (&error))
1207     {
1208       _dbus_warn ("Did not get an error launching nonexistent executable\n");
1209       return FALSE;
1210     }
1211
1212   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1213         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
1214     {
1215       _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1216                   error.name, error.message);
1217       dbus_error_free (&error);
1218       return FALSE;
1219     }
1220
1221   dbus_error_free (&error);
1222   
1223   return TRUE;
1224 }
1225
1226 static dbus_bool_t
1227 check_spawn_segfault (void *data)
1228 {
1229   char *argv[4] = { NULL, NULL, NULL, NULL };
1230   DBusBabysitter *sitter;
1231   DBusError error;
1232   
1233   sitter = NULL;
1234   
1235   dbus_error_init (&error);
1236
1237   /*** Test launching segfault binary */
1238   
1239   argv[0] = TEST_SEGFAULT_BINARY;
1240   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1241                                          NULL, NULL,
1242                                          &error))
1243     {
1244       _dbus_babysitter_block_for_child_exit (sitter);
1245       _dbus_babysitter_set_child_exit_error (sitter, &error);
1246     }
1247
1248   if (sitter)
1249     _dbus_babysitter_unref (sitter);
1250
1251   if (!dbus_error_is_set (&error))
1252     {
1253       _dbus_warn ("Did not get an error launching segfaulting binary\n");
1254       return FALSE;
1255     }
1256
1257   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1258         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1259     {
1260       _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1261                   error.name, error.message);
1262       dbus_error_free (&error);
1263       return FALSE;
1264     }
1265
1266   dbus_error_free (&error);
1267   
1268   return TRUE;
1269 }
1270
1271 static dbus_bool_t
1272 check_spawn_exit (void *data)
1273 {
1274   char *argv[4] = { NULL, NULL, NULL, NULL };
1275   DBusBabysitter *sitter;
1276   DBusError error;
1277   
1278   sitter = NULL;
1279   
1280   dbus_error_init (&error);
1281
1282   /*** Test launching exit failure binary */
1283   
1284   argv[0] = TEST_EXIT_BINARY;
1285   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1286                                          NULL, NULL,
1287                                          &error))
1288     {
1289       _dbus_babysitter_block_for_child_exit (sitter);
1290       _dbus_babysitter_set_child_exit_error (sitter, &error);
1291     }
1292
1293   if (sitter)
1294     _dbus_babysitter_unref (sitter);
1295
1296   if (!dbus_error_is_set (&error))
1297     {
1298       _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1299       return FALSE;
1300     }
1301
1302   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1303         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
1304     {
1305       _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1306                   error.name, error.message);
1307       dbus_error_free (&error);
1308       return FALSE;
1309     }
1310
1311   dbus_error_free (&error);
1312   
1313   return TRUE;
1314 }
1315
1316 static dbus_bool_t
1317 check_spawn_and_kill (void *data)
1318 {
1319   char *argv[4] = { NULL, NULL, NULL, NULL };
1320   DBusBabysitter *sitter;
1321   DBusError error;
1322   
1323   sitter = NULL;
1324   
1325   dbus_error_init (&error);
1326
1327   /*** Test launching sleeping binary then killing it */
1328
1329   argv[0] = TEST_SLEEP_FOREVER_BINARY;
1330   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1331                                          NULL, NULL,
1332                                          &error))
1333     {
1334       _dbus_babysitter_kill_child (sitter);
1335       
1336       _dbus_babysitter_block_for_child_exit (sitter);
1337       
1338       _dbus_babysitter_set_child_exit_error (sitter, &error);
1339     }
1340
1341   if (sitter)
1342     _dbus_babysitter_unref (sitter);
1343
1344   if (!dbus_error_is_set (&error))
1345     {
1346       _dbus_warn ("Did not get an error after killing spawned binary\n");
1347       return FALSE;
1348     }
1349
1350   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1351         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1352     {
1353       _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1354                   error.name, error.message);
1355       dbus_error_free (&error);
1356       return FALSE;
1357     }
1358
1359   dbus_error_free (&error);
1360   
1361   return TRUE;
1362 }
1363
1364 dbus_bool_t
1365 _dbus_spawn_test (const char *test_data_dir)
1366 {
1367   if (!_dbus_test_oom_handling ("spawn_nonexistent",
1368                                 check_spawn_nonexistent,
1369                                 NULL))
1370     return FALSE;
1371
1372   if (!_dbus_test_oom_handling ("spawn_segfault",
1373                                 check_spawn_segfault,
1374                                 NULL))
1375     return FALSE;
1376
1377   if (!_dbus_test_oom_handling ("spawn_exit",
1378                                 check_spawn_exit,
1379                                 NULL))
1380     return FALSE;
1381
1382   if (!_dbus_test_oom_handling ("spawn_and_kill",
1383                                 check_spawn_and_kill,
1384                                 NULL))
1385     return FALSE;
1386   
1387   return TRUE;
1388 }
1389 #endif