5 #if !defined(SPAWN_DEBUG) || defined(_MSC_VER)
8 #define PING() fprintf (stderr, "%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__); fflush (stderr)
13 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
14 /* dbus-spawn-win32.c Wrapper around g_spawn
16 * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
17 * Copyright (C) 2003 CodeFactory AB
18 * Copyright (C) 2005 Novell, Inc.
20 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
44 #define WIN32_LEAN_AND_MEAN
47 //#include <windows.h>
59 * Babysitter implementation details
65 HANDLE start_sync_event;
66 #ifdef DBUS_ENABLE_EMBEDDED_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;
85 DBusBabysitterFinishedFunc finished_cb;
88 dbus_bool_t have_spawn_errno;
90 dbus_bool_t have_child_status;
94 static DBusBabysitter*
95 _dbus_babysitter_new (void)
97 DBusBabysitter *sitter;
99 sitter = dbus_new0 (DBusBabysitter, 1);
103 sitter->refcount = 1;
105 sitter->start_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL);
106 if (sitter->start_sync_event == NULL)
108 _dbus_babysitter_unref (sitter);
112 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
113 sitter->end_sync_event = CreateEvent (NULL, FALSE, FALSE, NULL);
114 if (sitter->end_sync_event == NULL)
116 _dbus_babysitter_unref (sitter);
121 sitter->child_handle = NULL;
123 sitter->socket_to_babysitter = sitter->socket_to_main = -1;
129 sitter->watches = _dbus_watch_list_new ();
130 if (sitter->watches == NULL)
132 _dbus_babysitter_unref (sitter);
136 sitter->have_spawn_errno = FALSE;
137 sitter->have_child_status = FALSE;
143 * Increment the reference count on the babysitter object.
145 * @param sitter the babysitter
146 * @returns the babysitter
149 _dbus_babysitter_ref (DBusBabysitter *sitter)
152 _dbus_assert (sitter != NULL);
153 _dbus_assert (sitter->refcount > 0);
155 sitter->refcount += 1;
161 close_socket_to_babysitter (DBusBabysitter *sitter)
163 _dbus_verbose ("Closing babysitter\n");
165 if (sitter->sitter_watch != NULL)
167 _dbus_assert (sitter->watches != NULL);
168 _dbus_watch_list_remove_watch (sitter->watches, sitter->sitter_watch);
169 _dbus_watch_invalidate (sitter->sitter_watch);
170 _dbus_watch_unref (sitter->sitter_watch);
171 sitter->sitter_watch = NULL;
174 if (sitter->socket_to_babysitter != -1)
176 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
177 sitter->socket_to_babysitter = -1;
182 * Decrement the reference count on the babysitter object.
184 * @param sitter the babysitter
187 _dbus_babysitter_unref (DBusBabysitter *sitter)
192 _dbus_assert (sitter != NULL);
193 _dbus_assert (sitter->refcount > 0);
195 sitter->refcount -= 1;
197 if (sitter->refcount == 0)
199 close_socket_to_babysitter (sitter);
201 if (sitter->socket_to_main != -1)
203 _dbus_close_socket (sitter->socket_to_main, NULL);
204 sitter->socket_to_main = -1;
208 if (sitter->argv != NULL)
210 for (i = 0; i < sitter->argc; i++)
211 if (sitter->argv[i] != NULL)
213 dbus_free (sitter->argv[i]);
214 sitter->argv[i] = NULL;
216 dbus_free (sitter->argv);
220 if (sitter->envp != NULL)
222 char **e = sitter->envp;
226 dbus_free (sitter->envp);
230 if (sitter->child_handle != NULL)
232 CloseHandle (sitter->child_handle);
233 sitter->child_handle = NULL;
236 if (sitter->sitter_watch)
238 _dbus_watch_invalidate (sitter->sitter_watch);
239 _dbus_watch_unref (sitter->sitter_watch);
240 sitter->sitter_watch = NULL;
244 _dbus_watch_list_free (sitter->watches);
246 if (sitter->start_sync_event != NULL)
249 CloseHandle (sitter->start_sync_event);
250 sitter->start_sync_event = NULL;
253 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
254 if (sitter->end_sync_event != NULL)
256 CloseHandle (sitter->end_sync_event);
257 sitter->end_sync_event = NULL;
261 dbus_free (sitter->executable);
268 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
271 if (sitter->child_handle == NULL)
272 return; /* child is already dead, or we're so hosed we'll never recover */
275 TerminateProcess (sitter->child_handle, 12345);
279 * Checks whether the child has exited, without blocking.
281 * @param sitter the babysitter
284 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
287 return (sitter->child_handle == NULL);
291 * Gets the exit status of the child. We do this so implementation specific
292 * detail is not cluttering up dbus, for example the system launcher code.
293 * This can only be called if the child has exited, i.e. call
294 * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
295 * did not return a status code, e.g. because the child was signaled
296 * or we failed to ever launch the child in the first place.
298 * @param sitter the babysitter
299 * @param status the returned status code
300 * @returns #FALSE on failure
303 _dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
306 if (!_dbus_babysitter_get_child_exited (sitter))
307 _dbus_assert_not_reached ("Child has not exited");
309 if (!sitter->have_child_status ||
310 sitter->child_status == STILL_ACTIVE)
313 *status = sitter->child_status;
318 * Sets the #DBusError with an explanation of why the spawned
319 * child process exited (on a signal, or whatever). If
320 * the child process has not exited, does nothing (error
321 * will remain unset).
323 * @param sitter the babysitter
324 * @param error an error to fill in
327 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
331 if (!_dbus_babysitter_get_child_exited (sitter))
335 if (sitter->have_spawn_errno)
337 char *emsg = _dbus_win_error_string (sitter->spawn_errno);
338 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
339 "Failed to execute program %s: %s",
340 sitter->executable, emsg);
341 _dbus_win_free_error_string (emsg);
343 else if (sitter->have_child_status)
346 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
347 "Process %s exited with status %d",
348 sitter->executable, sitter->child_status);
353 dbus_set_error (error, DBUS_ERROR_FAILED,
354 "Process %s exited, status unknown",
361 _dbus_babysitter_set_watch_functions (DBusBabysitter *sitter,
362 DBusAddWatchFunction add_function,
363 DBusRemoveWatchFunction remove_function,
364 DBusWatchToggledFunction toggled_function,
366 DBusFreeFunction free_data_function)
369 return _dbus_watch_list_set_functions (sitter->watches,
378 handle_watch (DBusWatch *watch,
379 unsigned int condition,
382 DBusBabysitter *sitter = data;
384 /* On Unix dbus-spawn uses a babysitter *process*, thus it has to
385 * actually send the exit statuses, error codes and whatnot through
386 * sockets and/or pipes. On Win32, the babysitter is jus a thread,
387 * so it can set the status fields directly in the babysitter struct
388 * just fine. The socket pipe is used just so we can watch it with
389 * select(), as soon as anything is written to it we know that the
390 * babysitter thread has recorded the status in the babysitter
395 close_socket_to_babysitter (sitter);
398 if (_dbus_babysitter_get_child_exited (sitter) &&
399 sitter->finished_cb != NULL)
401 sitter->finished_cb (sitter, sitter->finished_data);
402 sitter->finished_cb = NULL;
408 /* protect_argv lifted from GLib, relicensed by author, Tor Lillqvist */
410 protect_argv (char **argv,
418 *new_argv = dbus_malloc ((argc + 1) * sizeof (char *));
419 if (*new_argv == NULL)
422 for (i = 0; i < argc; i++)
423 (*new_argv)[i] = NULL;
425 /* Quote each argv element if necessary, so that it will get
426 * reconstructed correctly in the C runtime startup code. Note that
427 * the unquoting algorithm in the C runtime is really weird, and
428 * rather different than what Unix shells do. See stdargv.c in the C
429 * runtime sources (in the Platform SDK, in src/crt).
431 * Note that an new_argv[0] constructed by this function should
432 * *not* be passed as the filename argument to a spawn* or exec*
433 * family function. That argument should be the real file name
434 * without any quoting.
436 for (i = 0; i < argc; i++)
441 int need_dblquotes = FALSE;
444 if (*p == ' ' || *p == '\t')
445 need_dblquotes = TRUE;
451 while (*pp && *pp == '\\')
460 q = (*new_argv)[i] = dbus_malloc (len + need_dblquotes*2 + 1);
478 while (*pp && *pp == '\\')
490 /* printf ("argv[%d]:%s, need_dblquotes:%s len:%d => %s\n", i, argv[i], need_dblquotes?"TRUE":"FALSE", len, (*new_argv)[i]); */
492 (*new_argv)[argc] = NULL;
498 /* From GPGME, relicensed by g10 Code GmbH. */
500 compose_string (char **strings, char separator)
507 if (!strings || !strings[0])
509 for (i = 0; strings[i]; i++)
510 n += strlen (strings[i]) + 1;
513 buf = p = malloc (n);
516 for (i = 0; strings[i]; i++)
518 strcpy (p, strings[i]);
519 p += strlen (strings[i]);
530 build_commandline (char **argv)
532 return compose_string (argv, ' ');
536 build_env_string (char** envp)
538 return compose_string (envp, '\0');
542 spawn_program (char* name, char** argv, char** envp)
544 PROCESS_INFORMATION pi = { NULL, 0, 0, 0 };
546 char *arg_string, *env_string;
551 arg_string = build_commandline (argv + 1);
555 arg_string = build_commandline (argv);
558 return INVALID_HANDLE_VALUE;
560 env_string = build_env_string(envp);
562 memset (&si, 0, sizeof (si));
565 result = CreateProcessA (name, arg_string, NULL, NULL, FALSE, 0,
567 result = CreateProcessA (NULL, arg_string, NULL, NULL, FALSE, 0,
569 (LPVOID)env_string, NULL, &si, &pi);
575 return INVALID_HANDLE_VALUE;
577 CloseHandle (pi.hThread);
582 static DWORD __stdcall
583 babysitter (void *parameter)
585 DBusBabysitter *sitter = (DBusBabysitter *) parameter;
588 _dbus_babysitter_ref (sitter);
590 if (sitter->child_setup)
593 (*sitter->child_setup) (sitter->user_data);
596 _dbus_verbose ("babysitter: spawning %s\n", sitter->executable);
599 sitter->child_handle = spawn_program (sitter->executable,
600 sitter->argv, sitter->envp);
603 if (sitter->child_handle == (HANDLE) -1)
605 sitter->child_handle = NULL;
606 sitter->have_spawn_errno = TRUE;
607 sitter->spawn_errno = GetLastError();
611 SetEvent (sitter->start_sync_event);
613 if (sitter->child_handle != NULL)
619 WaitForSingleObject (sitter->child_handle, INFINITE);
622 ret = GetExitCodeProcess (sitter->child_handle, &status);
624 sitter->child_status = status;
625 sitter->have_child_status = TRUE;
627 CloseHandle (sitter->child_handle);
628 sitter->child_handle = NULL;
631 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
632 SetEvent (sitter->end_sync_event);
636 send (sitter->socket_to_main, " ", 1, 0);
638 _dbus_babysitter_unref (sitter);
644 _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
647 DBusSpawnChildSetupFunc child_setup,
651 DBusBabysitter *sitter;
652 HANDLE sitter_thread;
653 DWORD sitter_thread_id;
655 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
660 sitter = _dbus_babysitter_new ();
663 _DBUS_SET_OOM (error);
667 sitter->child_setup = child_setup;
668 sitter->user_data = user_data;
670 sitter->executable = _dbus_strdup (argv[0]);
671 if (sitter->executable == NULL)
673 _DBUS_SET_OOM (error);
678 if (!_dbus_full_duplex_pipe (&sitter->socket_to_babysitter,
679 &sitter->socket_to_main,
683 sitter->sitter_watch = _dbus_watch_new (sitter->socket_to_babysitter,
685 TRUE, handle_watch, sitter, NULL);
687 if (sitter->sitter_watch == NULL)
689 _DBUS_SET_OOM (error);
694 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
696 /* we need to free it early so the destructor won't try to remove it
697 * without it having been added, which DBusLoop doesn't allow */
698 _dbus_watch_invalidate (sitter->sitter_watch);
699 _dbus_watch_unref (sitter->sitter_watch);
700 sitter->sitter_watch = NULL;
702 _DBUS_SET_OOM (error);
706 sitter->argc = protect_argv (argv, &sitter->argv);
707 if (sitter->argc == -1)
709 _DBUS_SET_OOM (error);
715 sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter,
716 sitter, 0, &sitter_thread_id);
718 if (sitter_thread == 0)
721 dbus_set_error_const (error, DBUS_ERROR_SPAWN_FORK_FAILED,
722 "Failed to create new thread");
725 CloseHandle (sitter_thread);
728 WaitForSingleObject (sitter->start_sync_event, INFINITE);
731 if (sitter_p != NULL)
734 _dbus_babysitter_unref (sitter);
736 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
742 _dbus_babysitter_unref (sitter);
748 _dbus_babysitter_set_result_function (DBusBabysitter *sitter,
749 DBusBabysitterFinishedFunc finished,
752 sitter->finished_cb = finished;
753 sitter->finished_data = user_data;
756 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
759 get_test_exec (const char *exe,
760 DBusString *scratch_space)
762 const char *dbus_test_exec;
764 dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
766 if (dbus_test_exec == NULL)
767 dbus_test_exec = DBUS_TEST_EXEC;
769 if (!_dbus_string_init (scratch_space))
772 if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
773 dbus_test_exec, exe, DBUS_EXEEXT))
775 _dbus_string_free (scratch_space);
779 return _dbus_string_get_data (scratch_space);
782 #define LIVE_CHILDREN(sitter) ((sitter)->child_handle != NULL)
785 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
787 if (sitter->child_handle == NULL)
790 WaitForSingleObject (sitter->end_sync_event, INFINITE);
794 check_spawn_nonexistent (void *data)
796 char *argv[4] = { NULL, NULL, NULL, NULL };
797 DBusBabysitter *sitter;
802 dbus_error_init (&error);
804 /*** Test launching nonexistent binary */
806 argv[0] = "/this/does/not/exist/32542sdgafgafdg";
807 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
811 _dbus_babysitter_block_for_child_exit (sitter);
812 _dbus_babysitter_set_child_exit_error (sitter, &error);
816 _dbus_babysitter_unref (sitter);
818 if (!dbus_error_is_set (&error))
820 _dbus_warn ("Did not get an error launching nonexistent executable\n");
824 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
825 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
827 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
828 error.name, error.message);
829 dbus_error_free (&error);
833 dbus_error_free (&error);
839 check_spawn_segfault (void *data)
841 char *argv[4] = { NULL, NULL, NULL, NULL };
842 DBusBabysitter *sitter;
848 dbus_error_init (&error);
850 /*** Test launching segfault binary */
852 argv[0] = get_test_exec ("test-segfault", &argv0);
856 /* OOM was simulated, never mind */
860 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
864 _dbus_babysitter_block_for_child_exit (sitter);
865 _dbus_babysitter_set_child_exit_error (sitter, &error);
868 _dbus_string_free (&argv0);
871 _dbus_babysitter_unref (sitter);
873 if (!dbus_error_is_set (&error))
875 _dbus_warn ("Did not get an error launching segfaulting binary\n");
879 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
880 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
882 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
883 error.name, error.message);
884 dbus_error_free (&error);
888 dbus_error_free (&error);
894 check_spawn_exit (void *data)
896 char *argv[4] = { NULL, NULL, NULL, NULL };
897 DBusBabysitter *sitter;
903 dbus_error_init (&error);
905 /*** Test launching exit failure binary */
907 argv[0] = get_test_exec ("test-exit", &argv0);
911 /* OOM was simulated, never mind */
915 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
919 _dbus_babysitter_block_for_child_exit (sitter);
920 _dbus_babysitter_set_child_exit_error (sitter, &error);
923 _dbus_string_free (&argv0);
926 _dbus_babysitter_unref (sitter);
928 if (!dbus_error_is_set (&error))
930 _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
934 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
935 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
937 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
938 error.name, error.message);
939 dbus_error_free (&error);
943 dbus_error_free (&error);
949 check_spawn_and_kill (void *data)
951 char *argv[4] = { NULL, NULL, NULL, NULL };
952 DBusBabysitter *sitter;
958 dbus_error_init (&error);
960 /*** Test launching sleeping binary then killing it */
962 argv[0] = get_test_exec ("test-sleep-forever", &argv0);
966 /* OOM was simulated, never mind */
970 if (_dbus_spawn_async_with_babysitter (&sitter, argv, NULL,
974 _dbus_babysitter_kill_child (sitter);
976 _dbus_babysitter_block_for_child_exit (sitter);
978 _dbus_babysitter_set_child_exit_error (sitter, &error);
981 _dbus_string_free (&argv0);
984 _dbus_babysitter_unref (sitter);
986 if (!dbus_error_is_set (&error))
988 _dbus_warn ("Did not get an error after killing spawned binary\n");
992 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
993 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
995 _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
996 error.name, error.message);
997 dbus_error_free (&error);
1001 dbus_error_free (&error);
1007 _dbus_spawn_test (const char *test_data_dir)
1009 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1010 check_spawn_nonexistent,
1014 /* Don't run the obnoxious segfault test by default,
1015 * it's a pain to have to click all those error boxes.
1017 if (getenv ("DO_SEGFAULT_TEST"))
1018 if (!_dbus_test_oom_handling ("spawn_segfault",
1019 check_spawn_segfault,
1023 if (!_dbus_test_oom_handling ("spawn_exit",
1028 if (!_dbus_test_oom_handling ("spawn_and_kill",
1029 check_spawn_and_kill,