5 #if !defined(SPAWN_DEBUG) || defined(_MSC_VER)
8 #define PING() fprintf (stderr, "%s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__); fflush (stderr)
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
65 HANDLE start_sync_event;
66 #ifdef DBUS_BUILD_TESTS
68 HANDLE end_sync_event;
72 DBusSpawnChildSetupFunc child_setup;
85 DBusBabysitterFinishedFunc finished_cb;
95 _dbus_babysitter_new (
void)
106 if (sitter->start_sync_event ==
NULL)
112 #ifdef DBUS_BUILD_TESTS
114 if (sitter->end_sync_event ==
NULL)
121 sitter->child_handle =
NULL;
136 sitter->have_spawn_errno =
FALSE;
137 sitter->have_child_status =
FALSE;
163 _dbus_verbose (
"Closing babysitter\n");
199 close_socket_to_babysitter (sitter);
201 if (sitter->socket_to_main != -1)
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)
214 sitter->argv[i] =
NULL;
220 if (sitter->envp !=
NULL)
222 char **e = sitter->envp;
230 if (sitter->child_handle !=
NULL)
232 CloseHandle (sitter->child_handle);
233 sitter->child_handle =
NULL;
246 if (sitter->start_sync_event !=
NULL)
249 CloseHandle (sitter->start_sync_event);
250 sitter->start_sync_event =
NULL;
253 #ifdef DBUS_BUILD_TESTS
254 if (sitter->end_sync_event !=
NULL)
256 CloseHandle (sitter->end_sync_event);
257 sitter->end_sync_event =
NULL;
271 if (sitter->child_handle ==
NULL)
275 TerminateProcess (sitter->child_handle, 12345);
287 return (sitter->child_handle ==
NULL);
309 if (!sitter->have_child_status ||
310 sitter->child_status == STILL_ACTIVE)
313 *status = sitter->child_status;
335 if (sitter->have_spawn_errno)
337 char *emsg = _dbus_win_error_string (sitter->spawn_errno);
339 "Failed to execute program %s: %s",
341 _dbus_win_free_error_string (emsg);
343 else if (sitter->have_child_status)
347 "Process %s exited with status %d",
354 "Process %s exited, status unknown",
379 unsigned int condition,
395 close_socket_to_babysitter (sitter);
399 sitter->finished_cb !=
NULL)
401 sitter->finished_cb (sitter, sitter->finished_data);
402 sitter->finished_cb =
NULL;
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;
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 ==
'\\')
492 (*new_argv)[argc] =
NULL;
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,
569 (LPVOID)env_string,
NULL, &si, &pi);
575 return INVALID_HANDLE_VALUE;
577 CloseHandle (pi.hThread);
582 static DWORD __stdcall
583 babysitter (
void *parameter)
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_BUILD_TESTS
632 SetEvent (sitter->end_sync_event);
636 send (sitter->socket_to_main,
" ", 1, 0);
647 DBusSpawnChildSetupFunc child_setup,
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;
673 _DBUS_SET_OOM (error);
679 &sitter->socket_to_main,
689 _DBUS_SET_OOM (error);
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)
722 "Failed to create new thread");
725 CloseHandle (sitter_thread);
728 WaitForSingleObject (sitter->start_sync_event, INFINITE);
731 if (sitter_p !=
NULL)
736 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
749 DBusBabysitterFinishedFunc finished,
752 sitter->finished_cb = finished;
753 sitter->finished_data = user_data;
756 #ifdef DBUS_BUILD_TESTS
759 get_test_exec (
const char *exe,
762 const char *dbus_test_exec;
766 if (dbus_test_exec ==
NULL)
767 dbus_test_exec = DBUS_TEST_EXEC;
773 dbus_test_exec, exe, DBUS_EXEEXT))
782 #define LIVE_CHILDREN(sitter) ((sitter)->child_handle != NULL)
787 if (sitter->child_handle ==
NULL)
790 WaitForSingleObject (sitter->end_sync_event, INFINITE);
794 check_spawn_nonexistent (
void *data)
806 argv[0] =
"/this/does/not/exist/32542sdgafgafdg";
811 _dbus_babysitter_block_for_child_exit (sitter);
820 _dbus_warn (
"Did not get an error launching nonexistent executable\n");
827 _dbus_warn (
"Not expecting error when launching nonexistent executable: %s: %s\n",
839 check_spawn_segfault (
void *data)
852 argv[0] = get_test_exec (
"test-segfault", &argv0);
864 _dbus_babysitter_block_for_child_exit (sitter);
875 _dbus_warn (
"Did not get an error launching segfaulting binary\n");
882 _dbus_warn (
"Not expecting error when launching segfaulting executable: %s: %s\n",
894 check_spawn_exit (
void *data)
907 argv[0] = get_test_exec (
"test-exit", &argv0);
919 _dbus_babysitter_block_for_child_exit (sitter);
930 _dbus_warn (
"Did not get an error launching binary that exited with failure code\n");
937 _dbus_warn (
"Not expecting error when launching exiting executable: %s: %s\n",
949 check_spawn_and_kill (
void *data)
962 argv[0] = get_test_exec (
"test-sleep-forever", &argv0);
976 _dbus_babysitter_block_for_child_exit (sitter);
988 _dbus_warn (
"Did not get an error after killing spawned binary\n");
995 _dbus_warn (
"Not expecting error when killing executable: %s: %s\n",
1007 _dbus_spawn_test (
const char *test_data_dir)
1009 if (!_dbus_test_oom_handling (
"spawn_nonexistent",
1010 check_spawn_nonexistent,
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,
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
const char * message
public error message field
Implementation of DBusWatch.
#define NULL
A null pointer, defined appropriately for C or C++.
#define DBUS_ERROR_SPAWN_EXEC_FAILED
While starting a new process, the exec() call failed.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
#define DBUS_ERROR_SPAWN_CHILD_EXITED
While starting a new process, the child exited with a status code.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
int socket_to_babysitter
Connection to the babysitter process.
dbus_bool_t _dbus_full_duplex_pipe(int *fd1, int *fd2, dbus_bool_t blocking, DBusError *error)
Creates a full-duplex pipe (as in socketpair()).
dbus_bool_t _dbus_watch_list_add_watch(DBusWatchList *watch_list, DBusWatch *watch)
Adds a new watch to the watch list, invoking the application DBusAddWatchFunction if appropriate...
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
char * executable
executable name to use in error messages
dbus_bool_t _dbus_babysitter_get_child_exited(DBusBabysitter *sitter)
Checks whether the child has exited, without blocking.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, char **argv, char **env, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
DBusWatchList * watches
Watches.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
DBusWatch * sitter_watch
Sitter pipe watch.
char * _dbus_string_get_data(DBusString *str)
Gets the raw character buffer from the string.
dbus_bool_t _dbus_babysitter_get_child_exit_status(DBusBabysitter *sitter, int *status)
Gets the exit status of the child.
void _dbus_babysitter_kill_child(DBusBabysitter *sitter)
Blocks until the babysitter process gives us the PID of the spawned grandchild, then kills the spawne...
Babysitter implementation details.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
dbus_bool_t _dbus_babysitter_set_watch_functions(DBusBabysitter *sitter, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets watch functions to notify us when the babysitter object needs to read/write file descriptors...
Object representing an exception.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
int refcount
Reference count.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
const char * name
public error name field
DBusWatchList implementation details.
void _dbus_babysitter_unref(DBusBabysitter *sitter)
Decrement the reference count on the babysitter object.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
dbus_bool_t _dbus_watch_list_set_functions(DBusWatchList *watch_list, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions.
#define DBUS_ERROR_SPAWN_FORK_FAILED
While starting a new process, the fork() call failed.
DBusWatch * _dbus_watch_new(int fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
void dbus_set_error_const(DBusError *error, const char *name, const char *message)
Assigns an error name and message to a DBusError.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
char * _dbus_strdup(const char *str)
Duplicates a string.
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
void _dbus_babysitter_set_child_exit_error(DBusBabysitter *sitter, DBusError *error)
Sets the DBusError with an explanation of why the spawned child process exited (on a signal...
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
void _dbus_watch_list_remove_watch(DBusWatchList *watch_list, DBusWatch *watch)
Removes a watch from the watch list, invoking the application's DBusRemoveWatchFunction if appropriat...
DBusBabysitter * _dbus_babysitter_ref(DBusBabysitter *sitter)
Increment the reference count on the babysitter object.