Add support for Windows CE to the code base.
[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 build_commandline (char **argv)
475 {
476   int i;
477   int n = 0;
478   char *buf;
479   char *p;
480   const char *ptr;
481   
482   for (i = 0; argv[i]; i++)
483     n += strlen (argv[i]) + 1;
484   n++;
485
486   buf = p = malloc (n);
487   if (!buf)
488     return NULL;
489   for (i = 0; argv[i]; i++)
490     {
491       strcpy (p, argv[i]);
492       p += strlen (argv[i]);
493       *(p++) = ' ';
494     }
495   if (i)
496     p--;
497   *p = '\0';
498
499   return buf;
500 }
501
502
503 static HANDLE
504 spawn_program (const char* name, char** argv, char** envp)
505 {
506   PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
507   STARTUPINFOA si;
508   char *arg_string;
509   BOOL result;
510
511 #ifdef DBUS_WINCE
512   arg_string = build_commandline (argv + 1);
513 #else
514   arg_string = build_commandline (argv);
515 #endif
516   if (!arg_string)
517     return INVALID_HANDLE_VALUE;
518
519   memset (&si, 0, sizeof (si));
520   si.cb = sizeof (si);
521   result = CreateProcessA (name, arg_string, NULL, NULL, FALSE, 0,
522                            envp, NULL, &si, &pi);
523   free (arg_string);
524   if (!result)
525     return INVALID_HANDLE_VALUE;
526
527   CloseHandle (pi.hThread);
528   return pi.hProcess;
529 }
530
531
532 static DWORD __stdcall
533 babysitter (void *parameter)
534 {
535   DBusBabysitter *sitter = (DBusBabysitter *) parameter;
536   int fd;
537   PING();
538   _dbus_babysitter_ref (sitter);
539
540   if (sitter->child_setup)
541     {
542       PING();
543       (*sitter->child_setup) (sitter->user_data);
544     }
545
546   _dbus_verbose ("babysitter: spawning %s\n", sitter->executable);
547
548   PING();
549   sitter->child_handle = spawn_program (sitter->executable,
550                                         sitter->argv, sitter->envp);
551
552   PING();
553   if (sitter->child_handle == (HANDLE) -1)
554     {
555       sitter->child_handle = NULL;
556       sitter->have_spawn_errno = TRUE;
557       sitter->spawn_errno = GetLastError();
558     }
559   
560   PING();
561   SetEvent (sitter->start_sync_event);
562
563   if (sitter->child_handle != NULL)
564     {
565       int ret;
566       DWORD status;
567
568       PING();
569       WaitForSingleObject (sitter->child_handle, INFINITE);
570
571       PING();
572       ret = GetExitCodeProcess (sitter->child_handle, &status);
573
574       sitter->child_status = status;
575       sitter->have_child_status = TRUE;
576
577       CloseHandle (sitter->child_handle);
578       sitter->child_handle = NULL;
579     }
580
581 #ifdef DBUS_BUILD_TESTS
582   SetEvent (sitter->end_sync_event);
583 #endif
584
585   PING();
586   send (sitter->socket_to_main, " ", 1, 0);
587
588   _dbus_babysitter_unref (sitter);
589
590   return 0;
591 }
592
593 dbus_bool_t
594 _dbus_spawn_async_with_babysitter (DBusBabysitter           **sitter_p,
595                                    char                     **argv,
596                                    char                     **envp,
597                                    DBusSpawnChildSetupFunc    child_setup,
598                                    void                      *user_data,
599                                    DBusError                 *error)
600 {
601   DBusBabysitter *sitter;
602   HANDLE sitter_thread;
603   DWORD sitter_thread_id;
604   
605   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
606
607   *sitter_p = NULL;
608
609   PING();
610   sitter = _dbus_babysitter_new ();
611   if (sitter == NULL)
612     {
613       _DBUS_SET_OOM (error);
614       return FALSE;
615     }
616
617   sitter->child_setup = child_setup;
618   sitter->user_data = user_data;
619
620   sitter->executable = _dbus_strdup (argv[0]);
621   if (sitter->executable == NULL)
622     {
623       _DBUS_SET_OOM (error);
624       goto out0;
625     }
626
627   PING();
628   if (!_dbus_full_duplex_pipe (&sitter->socket_to_babysitter,
629                                &sitter->socket_to_main,
630                                FALSE, error))
631     goto out0;
632
633   sitter->sitter_watch = _dbus_watch_new (sitter->socket_to_babysitter,
634                                           DBUS_WATCH_READABLE,
635                                           TRUE, handle_watch, sitter, NULL);
636   PING();
637   if (sitter->sitter_watch == NULL)
638     {
639       _DBUS_SET_OOM (error);
640       goto out0;
641     }
642
643   PING();
644   if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->sitter_watch))
645     {
646       _DBUS_SET_OOM (error);
647       goto out0;
648     }
649
650   sitter->argc = protect_argv (argv, &sitter->argv);
651   if (sitter->argc == -1)
652     {
653       _DBUS_SET_OOM (error);
654       goto out0;
655     }
656   sitter->envp = envp;
657
658   PING();
659   sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter,
660                   sitter, 0, &sitter_thread_id);
661
662   if (sitter_thread == 0)
663     {
664       PING();
665       dbus_set_error_const (error, DBUS_ERROR_SPAWN_FORK_FAILED,
666                             "Failed to create new thread");
667       goto out0;
668     }
669   CloseHandle (sitter_thread);
670
671   PING();
672   WaitForSingleObject (sitter->start_sync_event, INFINITE);
673
674   PING();
675   if (sitter_p != NULL)
676     *sitter_p = sitter;
677   else
678     _dbus_babysitter_unref (sitter);
679
680   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
681
682   PING();
683   return TRUE;
684
685 out0:
686   _dbus_babysitter_unref (sitter);
687
688   return FALSE;
689 }
690
691 #ifdef DBUS_BUILD_TESTS
692
693 #define LIVE_CHILDREN(sitter) ((sitter)->child_handle != NULL)
694
695 static void
696 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
697 {
698   if (sitter->child_handle == NULL)
699     return;
700
701   WaitForSingleObject (sitter->end_sync_event, INFINITE);
702 }
703
704 static dbus_bool_t
705 check_spawn_nonexistent (void *data)
706 {
707   char *argv[4] = { NULL, NULL, NULL, NULL };
708   DBusBabysitter *sitter;
709   DBusError error;
710
711   sitter = NULL;
712
713   dbus_error_init (&error);
714
715   /*** Test launching nonexistent binary */
716
717   argv[0] = "/this/does/not/exist/32542sdgafgafdg";
718   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
719                                          NULL, NULL,
720                                          &error))
721     {
722       _dbus_babysitter_block_for_child_exit (sitter);
723       _dbus_babysitter_set_child_exit_error (sitter, &error);
724     }
725
726   if (sitter)
727     _dbus_babysitter_unref (sitter);
728
729   if (!dbus_error_is_set (&error))
730     {
731       _dbus_warn ("Did not get an error launching nonexistent executable\n");
732       return FALSE;
733     }
734
735   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
736         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
737     {
738       _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
739                   error.name, error.message);
740       dbus_error_free (&error);
741       return FALSE;
742     }
743
744   dbus_error_free (&error);
745
746   return TRUE;
747 }
748
749 static dbus_bool_t
750 check_spawn_segfault (void *data)
751 {
752   char *argv[4] = { NULL, NULL, NULL, NULL };
753   DBusBabysitter *sitter;
754   DBusError error;
755
756   sitter = NULL;
757
758   dbus_error_init (&error);
759
760   /*** Test launching segfault binary */
761
762   argv[0] = TEST_SEGFAULT_BINARY;
763   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
764                                          NULL, NULL,
765                                          &error))
766     {
767       _dbus_babysitter_block_for_child_exit (sitter);
768       _dbus_babysitter_set_child_exit_error (sitter, &error);
769     }
770
771   if (sitter)
772     _dbus_babysitter_unref (sitter);
773
774   if (!dbus_error_is_set (&error))
775     {
776       _dbus_warn ("Did not get an error launching segfaulting binary\n");
777       return FALSE;
778     }
779
780   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
781         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
782     {
783       _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
784                   error.name, error.message);
785       dbus_error_free (&error);
786       return FALSE;
787     }
788
789   dbus_error_free (&error);
790
791   return TRUE;
792 }
793
794 static dbus_bool_t
795 check_spawn_exit (void *data)
796 {
797   char *argv[4] = { NULL, NULL, NULL, NULL };
798   DBusBabysitter *sitter;
799   DBusError error;
800
801   sitter = NULL;
802
803   dbus_error_init (&error);
804
805   /*** Test launching exit failure binary */
806
807   argv[0] = TEST_EXIT_BINARY;
808   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
809                                          NULL, NULL,
810                                          &error))
811     {
812       _dbus_babysitter_block_for_child_exit (sitter);
813       _dbus_babysitter_set_child_exit_error (sitter, &error);
814     }
815
816   if (sitter)
817     _dbus_babysitter_unref (sitter);
818
819   if (!dbus_error_is_set (&error))
820     {
821       _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
822       return FALSE;
823     }
824
825   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
826         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
827     {
828       _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
829                   error.name, error.message);
830       dbus_error_free (&error);
831       return FALSE;
832     }
833
834   dbus_error_free (&error);
835
836   return TRUE;
837 }
838
839 static dbus_bool_t
840 check_spawn_and_kill (void *data)
841 {
842   char *argv[4] = { NULL, NULL, NULL, NULL };
843   DBusBabysitter *sitter;
844   DBusError error;
845
846   sitter = NULL;
847
848   dbus_error_init (&error);
849
850   /*** Test launching sleeping binary then killing it */
851
852   argv[0] = TEST_SLEEP_FOREVER_BINARY;
853   if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
854                                          NULL, NULL,
855                                          &error))
856     {
857       _dbus_babysitter_kill_child (sitter);
858
859       _dbus_babysitter_block_for_child_exit (sitter);
860
861       _dbus_babysitter_set_child_exit_error (sitter, &error);
862     }
863
864   if (sitter)
865     _dbus_babysitter_unref (sitter);
866
867   if (!dbus_error_is_set (&error))
868     {
869       _dbus_warn ("Did not get an error after killing spawned binary\n");
870       return FALSE;
871     }
872
873   if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
874         dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
875     {
876       _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
877                   error.name, error.message);
878       dbus_error_free (&error);
879       return FALSE;
880     }
881
882   dbus_error_free (&error);
883
884   return TRUE;
885 }
886
887 dbus_bool_t
888 _dbus_spawn_test (const char *test_data_dir)
889 {
890   if (!_dbus_test_oom_handling ("spawn_nonexistent",
891                                 check_spawn_nonexistent,
892                                 NULL))
893     return FALSE;
894
895   /* Don't run the obnoxious segfault test by default,
896    * it's a pain to have to click all those error boxes.
897    */
898   if (getenv ("DO_SEGFAULT_TEST"))
899     if (!_dbus_test_oom_handling ("spawn_segfault",
900                                   check_spawn_segfault,
901                                   NULL))
902       return FALSE;
903
904   if (!_dbus_test_oom_handling ("spawn_exit",
905                                 check_spawn_exit,
906                                 NULL))
907     return FALSE;
908
909   if (!_dbus_test_oom_handling ("spawn_and_kill",
910                                 check_spawn_and_kill,
911                                 NULL))
912     return FALSE;
913
914   return TRUE;
915 }
916 #endif