Pass the environment to CreateProcessA correctly and be more defensive
[platform/upstream/dbus.git] / dbus / dbus-spawn-win.c
1 #include <config.h>
2
3 //#define SPAWN_DEBUG
4
5 #if !defined(SPAWN_DEBUG) || defined(_MSC_VER)
6 #define PING()
7 #else
8 #define PING() fprintf (stderr, "%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__); fflush (stderr)
9 #endif
10
11 #include <stdio.h>
12
13 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
14 /* dbus-spawn-win32.c Wrapper around g_spawn
15  * 
16  * Copyright (C) 2002, 2003, 2004  Red Hat, Inc.
17  * Copyright (C) 2003 CodeFactory AB
18  * Copyright (C) 2005 Novell, Inc.
19  *
20  * Licensed under the Academic Free License version 2.1
21  * 
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 2 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  * 
32  * You should have received a copy of the GNU General Public License
33  * along with this program; if not, write to the Free Software
34  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
35  *
36  */
37 #include "dbus-spawn.h"
38 #include "dbus-sysdeps.h"
39 #include "dbus-sysdeps-win.h"
40 #include "dbus-internals.h"
41 #include "dbus-test.h"
42 #include "dbus-protocol.h"
43
44 #define WIN32_LEAN_AND_MEAN
45 //#define STRICT
46 //#include <windows.h>
47 //#undef STRICT
48 #include <winsock2.h>
49 #undef interface
50
51 #include <stdlib.h>
52
53 #ifndef DBUS_WINCE
54 #include <process.h>
55 #endif
56
57 /**
58  * Babysitter implementation details
59  */
60 struct DBusBabysitter
61   {
62     int refcount;
63
64     HANDLE start_sync_event;
65 #ifdef DBUS_BUILD_TESTS
66
67     HANDLE end_sync_event;
68 #endif
69
70     char *executable;
71     DBusSpawnChildSetupFunc child_setup;
72     void *user_data;
73
74     int argc;
75     char **argv;
76     char **envp;
77
78     HANDLE child_handle;
79     int socket_to_babysitter;   /* Connection to the babysitter thread */
80     int socket_to_main;
81
82     DBusWatchList *watches;
83     DBusWatch *sitter_watch;
84
85     dbus_bool_t have_spawn_errno;
86     int spawn_errno;
87     dbus_bool_t have_child_status;
88     int child_status;
89   };
90
91 static DBusBabysitter*
92 _dbus_babysitter_new (void)
93 {
94   DBusBabysitter *sitter;
95
96   sitter = dbus_new0 (DBusBabysitter, 1);
97   if (sitter == NULL)
98     return NULL;
99
100   sitter->refcount = 1;
101
102   sitter->start_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL);
103   if (sitter->start_sync_event == NULL)
104     {
105       _dbus_babysitter_unref (sitter);
106       return NULL;
107     }
108
109 #ifdef DBUS_BUILD_TESTS
110   sitter->end_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL);
111   if (sitter->end_sync_event == NULL)
112     {
113       _dbus_babysitter_unref (sitter);
114       return NULL;
115     }
116 #endif
117
118   sitter->child_handle = NULL;
119
120   sitter->socket_to_babysitter = sitter->socket_to_main = -1;
121
122   sitter->argc = 0;
123   sitter->argv = NULL;
124   sitter->envp = NULL;
125
126   sitter->watches = _dbus_watch_list_new ();
127   if (sitter->watches == NULL)
128     {
129       _dbus_babysitter_unref (sitter);
130       return NULL;
131     }
132
133   sitter->have_spawn_errno = FALSE;
134   sitter->have_child_status = FALSE;
135
136   return sitter;
137 }
138
139 /**
140  * Increment the reference count on the babysitter object.
141  *
142  * @param sitter the babysitter
143  * @returns the babysitter
144  */
145 DBusBabysitter *
146 _dbus_babysitter_ref (DBusBabysitter *sitter)
147 {
148   PING();
149   _dbus_assert (sitter != NULL);
150   _dbus_assert (sitter->refcount > 0);
151
152   sitter->refcount += 1;
153
154   return sitter;
155 }
156
157 /**
158  * Decrement the reference count on the babysitter object.
159  *
160  * @param sitter the babysitter
161  */
162 void
163 _dbus_babysitter_unref (DBusBabysitter *sitter)
164 {
165   int i;
166
167   PING();
168   _dbus_assert (sitter != NULL);
169   _dbus_assert (sitter->refcount > 0);
170
171   sitter->refcount -= 1;
172
173   if (sitter->refcount == 0)
174     {
175       if (sitter->socket_to_babysitter != -1)
176         {
177           _dbus_close_socket (sitter->socket_to_babysitter, NULL);
178           sitter->socket_to_babysitter = -1;
179         }
180
181       if (sitter->socket_to_main != -1)
182         {
183           _dbus_close_socket (sitter->socket_to_main, NULL);
184           sitter->socket_to_main = -1;
185         }
186
187       PING();
188       if (sitter->argv != NULL)
189         {
190           for (i = 0; i < sitter->argc; i++)
191             if (sitter->argv[i] != NULL)
192               {
193                 dbus_free (sitter->argv[i]);
194                 sitter->argv[i] = NULL;
195               }
196           dbus_free (sitter->argv);
197           sitter->argv = NULL;
198         }
199
200       if (sitter->envp != NULL)
201         {
202           char **e = sitter->envp;
203
204           while (*e)
205             dbus_free (*e++);
206           dbus_free (sitter->envp);
207           sitter->envp = NULL;
208         }
209
210       if (sitter->child_handle != NULL)
211         {
212           CloseHandle (sitter->child_handle);
213           sitter->child_handle = NULL;
214         }
215
216       if (sitter->sitter_watch)
217         {
218           _dbus_watch_invalidate (sitter->sitter_watch);
219           _dbus_watch_unref (sitter->sitter_watch);
220           sitter->sitter_watch = NULL;
221         }
222
223       if (sitter->watches)
224         _dbus_watch_list_free (sitter->watches);
225
226       if (sitter->start_sync_event != NULL)
227         {
228           PING();
229           CloseHandle (sitter->start_sync_event);
230           sitter->start_sync_event = NULL;
231         }
232
233 #ifdef DBUS_BUILD_TESTS
234       if (sitter->end_sync_event != NULL)
235         {
236           CloseHandle (sitter->end_sync_event);
237           sitter->end_sync_event = NULL;
238         }
239 #endif
240
241       dbus_free (sitter->executable);
242
243       dbus_free (sitter);
244     }
245 }
246
247 void
248 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
249 {
250   PING();
251   if (sitter->child_handle == NULL)
252     return; /* child is already dead, or we're so hosed we'll never recover */
253
254   PING();
255   TerminateProcess (sitter->child_handle, 12345);
256 }
257
258 /**
259  * Checks whether the child has exited, without blocking.
260  *
261  * @param sitter the babysitter
262  */
263 dbus_bool_t
264 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
265 {
266   PING();
267   return (sitter->child_handle == NULL);
268 }
269
270 /**
271  * Gets the exit status of the child. We do this so implementation specific
272  * detail is not cluttering up dbus, for example the system launcher code.
273  * This can only be called if the child has exited, i.e. call
274  * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
275  * did not return a status code, e.g. because the child was signaled
276  * or we failed to ever launch the child in the first place.
277  *
278  * @param sitter the babysitter
279  * @param status the returned status code
280  * @returns #FALSE on failure
281  */
282 dbus_bool_t
283 _dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
284                                         int            *status)
285 {
286   if (!_dbus_babysitter_get_child_exited (sitter))
287     _dbus_assert_not_reached ("Child has not exited");
288
289   if (!sitter->have_child_status ||
290       sitter->child_status == STILL_ACTIVE)
291     return FALSE;
292
293   *status = sitter->child_status;
294   return TRUE;
295 }
296
297 /**
298  * Sets the #DBusError with an explanation of why the spawned
299  * child process exited (on a signal, or whatever). If
300  * the child process has not exited, does nothing (error
301  * will remain unset).
302  *
303  * @param sitter the babysitter
304  * @param error an error to fill in
305  */
306 void
307 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
308                                        DBusError      *error)
309 {
310   PING();
311   if (!_dbus_babysitter_get_child_exited (sitter))
312     return;
313
314   PING();
315   if (sitter->have_spawn_errno)
316     {
317       char *emsg = _dbus_win_error_string (sitter->spawn_errno);
318       dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
319                       "Failed to execute program %s: %s",
320                       sitter->executable, emsg);
321       _dbus_win_free_error_string (emsg);
322     }
323   else if (sitter->have_child_status)
324     {
325       PING();
326       dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
327                       "Process %s exited with status %d",
328                       sitter->executable, sitter->child_status);
329     }
330   else
331     {
332       PING();
333       dbus_set_error (error, DBUS_ERROR_FAILED,
334                       "Process %s exited, status unknown",
335                       sitter->executable);
336     }
337   PING();
338 }
339
340 dbus_bool_t
341 _dbus_babysitter_set_watch_functions (DBusBabysitter            *sitter,
342                                       DBusAddWatchFunction       add_function,
343                                       DBusRemoveWatchFunction    remove_function,
344                                       DBusWatchToggledFunction   toggled_function,
345                                       void                      *data,
346                                       DBusFreeFunction           free_data_function)
347 {
348   PING();
349   return _dbus_watch_list_set_functions (sitter->watches,
350                                          add_function,
351                                          remove_function,
352                                          toggled_function,
353                                          data,
354                                          free_data_function);
355 }
356
357 static dbus_bool_t
358 handle_watch (DBusWatch       *watch,
359               unsigned int     condition,
360               void            *data)
361 {
362   DBusBabysitter *sitter = data;
363
364   /* On Unix dbus-spawn uses a babysitter *process*, thus it has to
365    * actually send the exit statuses, error codes and whatnot through
366    * sockets and/or pipes. On Win32, the babysitter is jus a thread,
367    * so it can set the status fields directly in the babysitter struct
368    * just fine. The socket pipe is used just so we can watch it with
369    * select(), as soon as anything is written to it we know that the
370    * babysitter thread has recorded the status in the babysitter
371    * struct.
372    */
373
374   PING();
375   _dbus_close_socket (sitter->socket_to_babysitter, NULL);
376   PING();
377   sitter->socket_to_babysitter = -1;
378
379   return TRUE;
380 }
381
382 /* protect_argv lifted from GLib, relicensed by author, Tor Lillqvist */
383 static int
384 protect_argv (char  **argv,
385               char ***new_argv)
386 {
387   int i;
388   int argc = 0;
389
390   while (argv[argc])
391     ++argc;
392   *new_argv = dbus_malloc ((argc + 1) * sizeof (char *));
393   if (*new_argv == NULL)
394     return -1;
395
396   for (i = 0; i < argc; i++)
397     (*new_argv)[i] = NULL;
398
399   /* Quote each argv element if necessary, so that it will get
400    * reconstructed correctly in the C runtime startup code.  Note that
401    * the unquoting algorithm in the C runtime is really weird, and
402    * rather different than what Unix shells do. See stdargv.c in the C
403    * runtime sources (in the Platform SDK, in src/crt).
404    *
405    * Note that an new_argv[0] constructed by this function should
406    * *not* be passed as the filename argument to a spawn* or exec*
407    * family function. That argument should be the real file name
408    * without any quoting.
409    */
410   for (i = 0; i < argc; i++)
411     {
412       char *p = argv[i];
413       char *q;
414       int len = 0;
415       int need_dblquotes = FALSE;
416       while (*p)
417         {
418           if (*p == ' ' || *p == '\t')
419             need_dblquotes = TRUE;
420           else if (*p == '"')
421             len++;
422           else if (*p == '\\')
423             {
424               char *pp = p;
425               while (*pp && *pp == '\\')
426                 pp++;
427               if (*pp == '"')
428                 len++;
429             }
430           len++;
431           p++;
432         }
433
434       q = (*new_argv)[i] = dbus_malloc (len + need_dblquotes*2 + 1);
435
436       if (q == NULL)
437         return -1;
438
439
440       p = argv[i];
441
442       if (need_dblquotes)
443         *q++ = '"';
444
445       while (*p)
446         {
447           if (*p == '"')
448             *q++ = '\\';
449           else if (*p == '\\')
450             {
451               char *pp = p;
452               while (*pp && *pp == '\\')
453                 pp++;
454               if (*pp == '"')
455                 *q++ = '\\';
456             }
457           *q++ = *p;
458           p++;
459         }
460
461       if (need_dblquotes)
462         *q++ = '"';
463       *q++ = '\0';
464       /* printf ("argv[%d]:%s, need_dblquotes:%s len:%d => %s\n", i, argv[i], need_dblquotes?"TRUE":"FALSE", len, (*new_argv)[i]); */
465     }
466   (*new_argv)[argc] = NULL;
467
468   return argc;
469 }
470
471
472 /* From GPGME, relicensed by g10 Code GmbH.  */
473 static char *
474 compose_string (char **strings, char separator)
475 {
476   int i;
477   int n = 0;
478   char *buf;
479   char *p;
480   const char *ptr;
481   
482   if (!strings || !strings[0])
483     return 0;
484   for (i = 0; strings[i]; i++)
485     n += strlen (strings[i]) + 1;
486   n++;
487
488   buf = p = malloc (n);
489   if (!buf)
490     return NULL;
491   for (i = 0; strings[i]; i++)
492     {
493       strcpy (p, strings[i]);
494       p += strlen (strings[i]);
495       *(p++) = separator;
496     }
497   p--;
498   *(p++) = '\0';
499   *p = '\0';
500
501   return buf;
502 }
503
504 static char *
505 build_commandline (char **argv)
506 {
507   return compose_string (argv, ' ');
508 }
509
510 static char *
511 build_env_string (char** envp)
512 {
513   return compose_string (envp, '\0');
514 }
515
516 static HANDLE
517 spawn_program (char* name, char** argv, char** envp)
518 {
519   PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
520   STARTUPINFOA si;
521   char *arg_string, *env_string;
522   BOOL result;
523
524 #ifdef DBUS_WINCE
525   arg_string = build_commandline (argv + 1);
526 #else
527   arg_string = build_commandline (argv);
528 #endif
529   if (!arg_string)
530     return INVALID_HANDLE_VALUE;
531
532   env_string = build_env_string(envp);
533
534   memset (&si, 0, sizeof (si));
535   si.cb = sizeof (si);
536   result = CreateProcessA (name, arg_string, NULL, NULL, FALSE, 0,
537                            (LPVOID)env_string, NULL, &si, &pi);
538   free (arg_string);
539   if (env_string)
540     free (env_string);
541
542   if (!result)
543     return INVALID_HANDLE_VALUE;
544
545   CloseHandle (pi.hThread);
546   return pi.hProcess;
547 }
548
549
550 static DWORD __stdcall
551 babysitter (void *parameter)
552 {
553   DBusBabysitter *sitter = (DBusBabysitter *) parameter;
554   int fd;
555   PING();
556   _dbus_babysitter_ref (sitter);
557
558   if (sitter->child_setup)
559     {
560       PING();
561       (*sitter->child_setup) (sitter->user_data);
562     }
563
564   _dbus_verbose ("babysitter: spawning %s\n", sitter->executable);
565
566   PING();
567   sitter->child_handle = spawn_program (sitter->executable,
568                                         sitter->argv, sitter->envp);
569
570   PING();
571   if (sitter->child_handle == (HANDLE) -1)
572     {
573       sitter->child_handle = NULL;
574       sitter->have_spawn_errno = TRUE;
575       sitter->spawn_errno = GetLastError();
576     }
577   
578   PING();
579   SetEvent (sitter->start_sync_event);
580
581   if (sitter->child_handle != NULL)
582     {
583       int ret;
584       DWORD status;
585
586       PING();
587       WaitForSingleObject (sitter->child_handle, INFINITE);
588
589       PING();
590       ret = GetExitCodeProcess (sitter->child_handle, &status);
591
592       sitter->child_status = status;
593       sitter->have_child_status = TRUE;
594
595       CloseHandle (sitter->child_handle);
596       sitter->child_handle = NULL;
597     }
598
599 #ifdef DBUS_BUILD_TESTS
600   SetEvent (sitter->end_sync_event);
601 #endif
602
603   PING();
604   send (sitter->socket_to_main, " ", 1, 0);
605
606   _dbus_babysitter_unref (sitter);
607
608   return 0;
609 }
610
611 dbus_bool_t
612 _dbus_spawn_async_with_babysitter (DBusBabysitter           **sitter_p,
613                                    char                     **argv,
614                                    char                     **envp,
615                                    DBusSpawnChildSetupFunc    child_setup,
616                                    void                      *user_data,
617                                    DBusError                 *error)
618 {
619   DBusBabysitter *sitter;
620   HANDLE sitter_thread;
621   DWORD sitter_thread_id;
622   
623   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
624
625   *sitter_p = NULL;
626
627   PING();
628   sitter = _dbus_babysitter_new ();
629   if (sitter == NULL)
630     {
631       _DBUS_SET_OOM (error);
632       return FALSE;
633     }
634
635   sitter->child_setup = child_setup;
636   sitter->user_data = user_data;
637
638   sitter->executable = _dbus_strdup (argv[0]);
639   if (sitter->executable == NULL)
640     {
641       _DBUS_SET_OOM (error);
642       goto out0;
643     }
644
645   PING();
646   if (!_dbus_full_duplex_pipe (&sitter->socket_to_babysitter,
647                                &sitter->socket_to_main,
648                                FALSE, error))
649     goto out0;
650
651   sitter->sitter_watch = _dbus_watch_new (sitter->socket_to_babysitter,
652                                           DBUS_WATCH_READABLE,
653                                           TRUE, handle_watch, sitter, NULL);
654   PING();
655   if (sitter->sitter_watch == NULL)
656     {
657       _DBUS_SET_OOM (error);
658       goto out0;
659     }
660
661   PING();
662   if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->sitter_watch))
663     {
664       _DBUS_SET_OOM (error);
665       goto out0;
666     }
667
668   sitter->argc = protect_argv (argv, &sitter->argv);
669   if (sitter->argc == -1)
670     {
671       _DBUS_SET_OOM (error);
672       goto out0;
673     }
674   sitter->envp = envp;
675
676   PING();
677   sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter,
678                   sitter, 0, &sitter_thread_id);
679
680   if (sitter_thread == 0)
681     {
682       PING();
683       dbus_set_error_const (error, DBUS_ERROR_SPAWN_FORK_FAILED,
684                             "Failed to create new thread");
685       goto out0;
686     }
687   CloseHandle (sitter_thread);
688
689   PING();
690   WaitForSingleObject (sitter->start_sync_event, INFINITE);
691
692   PING();
693   if (sitter_p != NULL)
694     *sitter_p = sitter;
695   else
696     _dbus_babysitter_unref (sitter);
697
698   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
699
700   PING();
701   return TRUE;
702
703 out0:
704   _dbus_babysitter_unref (sitter);
705
706   return FALSE;
707 }
708
709 #ifdef DBUS_BUILD_TESTS
710
711 #define LIVE_CHILDREN(sitter) ((sitter)->child_handle != NULL)
712
713 static void
714 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
715 {
716   if (sitter->child_handle == NULL)
717     return;
718
719   WaitForSingleObject (sitter->end_sync_event, INFINITE);
720 }
721
722 static dbus_bool_t
723 check_spawn_nonexistent (void *data)
724 {
725   char *argv[4] = { NULL, NULL, NULL, NULL };
726   DBusBabysitter *sitter;
727   DBusError error;
728
729   sitter = NULL;
730
731   dbus_error_init (&error);
732
733   /*** Test launching nonexistent binary */
734
735   argv[0] = "/this/does/not/exist/32542sdgafgafdg";
736   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
737                                          NULL, NULL,
738                                          &error))
739     {
740       _dbus_babysitter_block_for_child_exit (sitter);
741       _dbus_babysitter_set_child_exit_error (sitter, &error);
742     }
743
744   if (sitter)
745     _dbus_babysitter_unref (sitter);
746
747   if (!dbus_error_is_set (&error))
748     {
749       _dbus_warn ("Did not get an error launching nonexistent executable\n");
750       return FALSE;
751     }
752
753   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
754         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
755     {
756       _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
757                   error.name, error.message);
758       dbus_error_free (&error);
759       return FALSE;
760     }
761
762   dbus_error_free (&error);
763
764   return TRUE;
765 }
766
767 static dbus_bool_t
768 check_spawn_segfault (void *data)
769 {
770   char *argv[4] = { NULL, NULL, NULL, NULL };
771   DBusBabysitter *sitter;
772   DBusError error;
773
774   sitter = NULL;
775
776   dbus_error_init (&error);
777
778   /*** Test launching segfault binary */
779
780   argv[0] = TEST_SEGFAULT_BINARY;
781   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
782                                          NULL, NULL,
783                                          &error))
784     {
785       _dbus_babysitter_block_for_child_exit (sitter);
786       _dbus_babysitter_set_child_exit_error (sitter, &error);
787     }
788
789   if (sitter)
790     _dbus_babysitter_unref (sitter);
791
792   if (!dbus_error_is_set (&error))
793     {
794       _dbus_warn ("Did not get an error launching segfaulting binary\n");
795       return FALSE;
796     }
797
798   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
799         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
800     {
801       _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
802                   error.name, error.message);
803       dbus_error_free (&error);
804       return FALSE;
805     }
806
807   dbus_error_free (&error);
808
809   return TRUE;
810 }
811
812 static dbus_bool_t
813 check_spawn_exit (void *data)
814 {
815   char *argv[4] = { NULL, NULL, NULL, NULL };
816   DBusBabysitter *sitter;
817   DBusError error;
818
819   sitter = NULL;
820
821   dbus_error_init (&error);
822
823   /*** Test launching exit failure binary */
824
825   argv[0] = TEST_EXIT_BINARY;
826   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
827                                          NULL, NULL,
828                                          &error))
829     {
830       _dbus_babysitter_block_for_child_exit (sitter);
831       _dbus_babysitter_set_child_exit_error (sitter, &error);
832     }
833
834   if (sitter)
835     _dbus_babysitter_unref (sitter);
836
837   if (!dbus_error_is_set (&error))
838     {
839       _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
840       return FALSE;
841     }
842
843   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
844         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
845     {
846       _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
847                   error.name, error.message);
848       dbus_error_free (&error);
849       return FALSE;
850     }
851
852   dbus_error_free (&error);
853
854   return TRUE;
855 }
856
857 static dbus_bool_t
858 check_spawn_and_kill (void *data)
859 {
860   char *argv[4] = { NULL, NULL, NULL, NULL };
861   DBusBabysitter *sitter;
862   DBusError error;
863
864   sitter = NULL;
865
866   dbus_error_init (&error);
867
868   /*** Test launching sleeping binary then killing it */
869
870   argv[0] = TEST_SLEEP_FOREVER_BINARY;
871   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
872                                          NULL, NULL,
873                                          &error))
874     {
875       _dbus_babysitter_kill_child (sitter);
876
877       _dbus_babysitter_block_for_child_exit (sitter);
878
879       _dbus_babysitter_set_child_exit_error (sitter, &error);
880     }
881
882   if (sitter)
883     _dbus_babysitter_unref (sitter);
884
885   if (!dbus_error_is_set (&error))
886     {
887       _dbus_warn ("Did not get an error after killing spawned binary\n");
888       return FALSE;
889     }
890
891   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
892         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
893     {
894       _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
895                   error.name, error.message);
896       dbus_error_free (&error);
897       return FALSE;
898     }
899
900   dbus_error_free (&error);
901
902   return TRUE;
903 }
904
905 dbus_bool_t
906 _dbus_spawn_test (const char *test_data_dir)
907 {
908   if (!_dbus_test_oom_handling ("spawn_nonexistent",
909                                 check_spawn_nonexistent,
910                                 NULL))
911     return FALSE;
912
913   /* Don't run the obnoxious segfault test by default,
914    * it's a pain to have to click all those error boxes.
915    */
916   if (getenv ("DO_SEGFAULT_TEST"))
917     if (!_dbus_test_oom_handling ("spawn_segfault",
918                                   check_spawn_segfault,
919                                   NULL))
920       return FALSE;
921
922   if (!_dbus_test_oom_handling ("spawn_exit",
923                                 check_spawn_exit,
924                                 NULL))
925     return FALSE;
926
927   if (!_dbus_test_oom_handling ("spawn_and_kill",
928                                 check_spawn_and_kill,
929                                 NULL))
930     return FALSE;
931
932   return TRUE;
933 }
934 #endif