5 #ifndef SPAWN_DEBUG || defined(_MSC_VER)
8 #define PING() fprintf (stderr, "%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__); fflush (stderr)
16 /* -*- mode: C; c-file-style: "gnu" -*- */
17 /* dbus-spawn-win32.c Wrapper around g_spawn
19 * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
20 * Copyright (C) 2003 CodeFactory AB
21 * Copyright (C) 2005 Novell, Inc.
23 * Licensed under the Academic Free License version 2.1
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include "dbus-spawn.h"
41 #include "dbus-sysdeps.h"
42 #include "dbus-sysdeps-win.h"
43 #include "dbus-internals.h"
44 #include "dbus-test.h"
45 #include "dbus-protocol.h"
47 #define WIN32_LEAN_AND_MEAN
49 //#include <windows.h>
59 * Babysitter implementation details
65 HANDLE start_sync_event;
66 #ifdef DBUS_BUILD_TESTS
68 HANDLE end_sync_event;
72 DBusSpawnChildSetupFunc child_setup;
80 int socket_to_babysitter; /* Connection to the babysitter thread */
83 DBusWatchList *watches;
84 DBusWatch *sitter_watch;
86 dbus_bool_t have_spawn_errno;
88 dbus_bool_t have_child_status;
92 static DBusBabysitter*
93 _dbus_babysitter_new (void)
95 DBusBabysitter *sitter;
97 sitter = dbus_new0 (DBusBabysitter, 1);
101 sitter->refcount = 1;
103 sitter->start_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL);
104 if (sitter->start_sync_event == NULL)
106 _dbus_babysitter_unref (sitter);
110 #ifdef DBUS_BUILD_TESTS
111 sitter->end_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL);
112 if (sitter->end_sync_event == NULL)
114 _dbus_babysitter_unref (sitter);
119 sitter->child_handle = NULL;
121 sitter->socket_to_babysitter = sitter->socket_to_main = -1;
127 sitter->watches = _dbus_watch_list_new ();
128 if (sitter->watches == NULL)
130 _dbus_babysitter_unref (sitter);
134 sitter->have_spawn_errno = FALSE;
135 sitter->have_child_status = FALSE;
141 * Increment the reference count on the babysitter object.
143 * @param sitter the babysitter
144 * @returns the babysitter
147 _dbus_babysitter_ref (DBusBabysitter *sitter)
150 _dbus_assert (sitter != NULL);
151 _dbus_assert (sitter->refcount > 0);
153 sitter->refcount += 1;
159 * Decrement the reference count on the babysitter object.
161 * @param sitter the babysitter
164 _dbus_babysitter_unref (DBusBabysitter *sitter)
169 _dbus_assert (sitter != NULL);
170 _dbus_assert (sitter->refcount > 0);
172 sitter->refcount -= 1;
174 if (sitter->refcount == 0)
176 if (sitter->socket_to_babysitter != -1)
178 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
179 sitter->socket_to_babysitter = -1;
182 if (sitter->socket_to_main != -1)
184 _dbus_close_socket (sitter->socket_to_main, NULL);
185 sitter->socket_to_main = -1;
189 if (sitter->argv != NULL)
191 for (i = 0; i < sitter->argc; i++)
192 if (sitter->argv[i] != NULL)
194 dbus_free (sitter->argv[i]);
195 sitter->argv[i] = NULL;
197 dbus_free (sitter->argv);
201 if (sitter->envp != NULL)
203 char **e = sitter->envp;
207 dbus_free (sitter->envp);
211 if (sitter->child_handle != NULL)
213 CloseHandle (sitter->child_handle);
214 sitter->child_handle = NULL;
217 if (sitter->sitter_watch)
219 _dbus_watch_invalidate (sitter->sitter_watch);
220 _dbus_watch_unref (sitter->sitter_watch);
221 sitter->sitter_watch = NULL;
225 _dbus_watch_list_free (sitter->watches);
227 if (sitter->start_sync_event != NULL)
230 CloseHandle (sitter->start_sync_event);
231 sitter->end_sync_event = NULL;
234 #ifdef DBUS_BUILD_TESTS
235 if (sitter->end_sync_event != NULL)
237 CloseHandle (sitter->end_sync_event);
238 sitter->end_sync_event = NULL;
242 dbus_free (sitter->executable);
249 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
252 if (sitter->child_handle == NULL)
253 return; /* child is already dead, or we're so hosed we'll never recover */
256 TerminateProcess (sitter->child_handle, 12345);
260 * Checks whether the child has exited, without blocking.
262 * @param sitter the babysitter
265 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
268 return (sitter->child_handle == NULL);
272 * Sets the #DBusError with an explanation of why the spawned
273 * child process exited (on a signal, or whatever). If
274 * the child process has not exited, does nothing (error
275 * will remain unset).
277 * @param sitter the babysitter
278 * @param error an error to fill in
281 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
285 if (!_dbus_babysitter_get_child_exited (sitter))
289 if (sitter->have_spawn_errno)
291 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
292 "Failed to execute program %s: %s",
293 sitter->executable, _dbus_strerror (sitter->spawn_errno));
295 else if (sitter->have_child_status)
298 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
299 "Process %s exited with status %d",
300 sitter->executable, sitter->child_status);
305 dbus_set_error (error, DBUS_ERROR_FAILED,
306 "Process %s exited, status unknown",
313 _dbus_babysitter_set_watch_functions (DBusBabysitter *sitter,
314 DBusAddWatchFunction add_function,
315 DBusRemoveWatchFunction remove_function,
316 DBusWatchToggledFunction toggled_function,
318 DBusFreeFunction free_data_function)
321 return _dbus_watch_list_set_functions (sitter->watches,
330 handle_watch (DBusWatch *watch,
331 unsigned int condition,
334 DBusBabysitter *sitter = data;
336 /* On Unix dbus-spawn uses a babysitter *process*, thus it has to
337 * actually send the exit statuses, error codes and whatnot through
338 * sockets and/or pipes. On Win32, the babysitter is jus a thread,
339 * so it can set the status fields directly in the babysitter struct
340 * just fine. The socket pipe is used just so we can watch it with
341 * select(), as soon as anything is written to it we know that the
342 * babysitter thread has recorded the status in the babysitter
347 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
349 sitter->socket_to_babysitter = -1;
354 /* protect_argv lifted from GLib, relicensed by author, Tor Lillqvist */
356 protect_argv (char **argv,
364 *new_argv = dbus_malloc ((argc + 1) * sizeof (char *));
365 if (*new_argv == NULL)
368 for (i = 0; i < argc; i++)
369 (*new_argv)[i] = NULL;
371 /* Quote each argv element if necessary, so that it will get
372 * reconstructed correctly in the C runtime startup code. Note that
373 * the unquoting algorithm in the C runtime is really weird, and
374 * rather different than what Unix shells do. See stdargv.c in the C
375 * runtime sources (in the Platform SDK, in src/crt).
377 * Note that an new_argv[0] constructed by this function should
378 * *not* be passed as the filename argument to a spawn* or exec*
379 * family function. That argument should be the real file name
380 * without any quoting.
382 for (i = 0; i < argc; i++)
387 int need_dblquotes = FALSE;
390 if (*p == ' ' || *p == '\t')
391 need_dblquotes = TRUE;
397 while (*pp && *pp == '\\')
406 q = (*new_argv)[i] = dbus_malloc (len + need_dblquotes*2 + 1);
424 while (*pp && *pp == '\\')
436 /* printf ("argv[%d]:%s, need_dblquotes:%s len:%d => %s\n", i, argv[i], need_dblquotes?"TRUE":"FALSE", len, (*new_argv)[i]); */
438 (*new_argv)[argc] = NULL;
443 static unsigned __stdcall
444 babysitter (void *parameter)
446 DBusBabysitter *sitter = (DBusBabysitter *) parameter;
449 _dbus_babysitter_ref (sitter);
451 if (sitter->child_setup)
454 (*sitter->child_setup) (sitter->user_data);
457 _dbus_verbose ("babysitter: spawning %s\n", sitter->executable);
460 if (sitter->envp != NULL)
461 sitter->child_handle = (HANDLE) spawnve (P_NOWAIT, sitter->executable,
462 (const char * const *) sitter->argv,
463 (const char * const *) sitter->envp);
465 sitter->child_handle = (HANDLE) spawnv (P_NOWAIT, sitter->executable,
466 (const char * const *) sitter->argv);
469 if (sitter->child_handle == (HANDLE) -1)
471 sitter->child_handle = NULL;
472 sitter->have_spawn_errno = TRUE;
473 sitter->spawn_errno = errno;
477 SetEvent (sitter->start_sync_event);
479 if (sitter->child_handle != NULL)
485 WaitForSingleObject (sitter->child_handle, INFINITE);
488 ret = GetExitCodeProcess (sitter->child_handle, &status);
490 sitter->child_status = status;
491 sitter->have_child_status = TRUE;
493 CloseHandle (sitter->child_handle);
494 sitter->child_handle = NULL;
497 #ifdef DBUS_BUILD_TESTS
498 SetEvent (sitter->end_sync_event);
502 _dbus_handle_to_socket (sitter->socket_to_main, &sock);
503 send (sock->fd, " ", 1, 0);
505 _dbus_babysitter_unref (sitter);
511 _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
514 DBusSpawnChildSetupFunc child_setup,
518 DBusBabysitter *sitter;
519 HANDLE sitter_thread;
520 int sitter_thread_id;
522 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
527 sitter = _dbus_babysitter_new ();
530 _DBUS_SET_OOM (error);
534 sitter->child_setup = child_setup;
535 sitter->user_data = user_data;
537 sitter->executable = _dbus_strdup (argv[0]);
538 if (sitter->executable == NULL)
540 _DBUS_SET_OOM (error);
545 if (!_dbus_full_duplex_pipe (&sitter->socket_to_babysitter,
546 &sitter->socket_to_main,
550 sitter->sitter_watch = _dbus_watch_new (sitter->socket_to_babysitter,
552 TRUE, handle_watch, sitter, NULL);
554 if (sitter->sitter_watch == NULL)
556 _DBUS_SET_OOM (error);
561 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
563 _DBUS_SET_OOM (error);
567 sitter->argc = protect_argv (argv, &sitter->argv);
568 if (sitter->argc == -1)
570 _DBUS_SET_OOM (error);
576 sitter_thread = (HANDLE) _beginthreadex (NULL, 0, babysitter,
577 sitter, 0, &sitter_thread_id);
579 if (sitter_thread == 0)
582 dbus_set_error_const (error, DBUS_ERROR_SPAWN_FORK_FAILED,
583 "Failed to create new thread");
586 CloseHandle (sitter_thread);
589 WaitForSingleObject (sitter->start_sync_event, INFINITE);
592 if (sitter_p != NULL)
595 _dbus_babysitter_unref (sitter);
597 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
603 _dbus_babysitter_unref (sitter);
608 #ifdef DBUS_BUILD_TESTS
610 #define LIVE_CHILDREN(sitter) ((sitter)->child_handle != NULL)
613 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
615 if (sitter->child_handle == NULL)
618 WaitForSingleObject (sitter->end_sync_event, INFINITE);
622 check_spawn_nonexistent (void *data)
624 char *argv[4] = { NULL, NULL, NULL, NULL };
625 DBusBabysitter *sitter;
630 dbus_error_init (&error);
632 /*** Test launching nonexistent binary */
634 argv[0] = "/this/does/not/exist/32542sdgafgafdg";
635 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
639 _dbus_babysitter_block_for_child_exit (sitter);
640 _dbus_babysitter_set_child_exit_error (sitter, &error);
644 _dbus_babysitter_unref (sitter);
646 if (!dbus_error_is_set (&error))
648 _dbus_warn ("Did not get an error launching nonexistent executable\n");
652 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
653 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
655 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
656 error.name, error.message);
657 dbus_error_free (&error);
661 dbus_error_free (&error);
667 check_spawn_segfault (void *data)
669 char *argv[4] = { NULL, NULL, NULL, NULL };
670 DBusBabysitter *sitter;
675 dbus_error_init (&error);
677 /*** Test launching segfault binary */
679 argv[0] = TEST_SEGFAULT_BINARY;
680 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
684 _dbus_babysitter_block_for_child_exit (sitter);
685 _dbus_babysitter_set_child_exit_error (sitter, &error);
689 _dbus_babysitter_unref (sitter);
691 if (!dbus_error_is_set (&error))
693 _dbus_warn ("Did not get an error launching segfaulting binary\n");
697 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
698 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
700 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
701 error.name, error.message);
702 dbus_error_free (&error);
706 dbus_error_free (&error);
712 check_spawn_exit (void *data)
714 char *argv[4] = { NULL, NULL, NULL, NULL };
715 DBusBabysitter *sitter;
720 dbus_error_init (&error);
722 /*** Test launching exit failure binary */
724 argv[0] = TEST_EXIT_BINARY;
725 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
729 _dbus_babysitter_block_for_child_exit (sitter);
730 _dbus_babysitter_set_child_exit_error (sitter, &error);
734 _dbus_babysitter_unref (sitter);
736 if (!dbus_error_is_set (&error))
738 _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
742 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
743 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
745 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
746 error.name, error.message);
747 dbus_error_free (&error);
751 dbus_error_free (&error);
757 check_spawn_and_kill (void *data)
759 char *argv[4] = { NULL, NULL, NULL, NULL };
760 DBusBabysitter *sitter;
765 dbus_error_init (&error);
767 /*** Test launching sleeping binary then killing it */
769 argv[0] = TEST_SLEEP_FOREVER_BINARY;
770 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
774 _dbus_babysitter_kill_child (sitter);
776 _dbus_babysitter_block_for_child_exit (sitter);
778 _dbus_babysitter_set_child_exit_error (sitter, &error);
782 _dbus_babysitter_unref (sitter);
784 if (!dbus_error_is_set (&error))
786 _dbus_warn ("Did not get an error after killing spawned binary\n");
790 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
791 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
793 _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
794 error.name, error.message);
795 dbus_error_free (&error);
799 dbus_error_free (&error);
805 _dbus_spawn_test (const char *test_data_dir)
807 if (!_dbus_test_oom_handling ("spawn_nonexistent",
808 check_spawn_nonexistent,
812 /* Don't run the obnoxious segfault test by default,
813 * it's a pain to have to click all those error boxes.
815 if (getenv ("DO_SEGFAULT_TEST"))
816 if (!_dbus_test_oom_handling ("spawn_segfault",
817 check_spawn_segfault,
821 if (!_dbus_test_oom_handling ("spawn_exit",
826 if (!_dbus_test_oom_handling ("spawn_and_kill",
827 check_spawn_and_kill,