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_ENABLE_EMBEDDED_TESTS 68 HANDLE end_sync_event;
83 DBusBabysitterFinishedFunc finished_cb;
93 _dbus_babysitter_new (
void)
104 if (sitter->start_sync_event ==
NULL)
110 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 112 if (sitter->end_sync_event ==
NULL)
119 sitter->child_handle =
NULL;
134 sitter->have_spawn_errno =
FALSE;
135 sitter->have_child_status =
FALSE;
161 _dbus_verbose (
"Closing babysitter\n");
197 close_socket_to_babysitter (sitter);
199 if (sitter->socket_to_main.sock != INVALID_SOCKET)
202 sitter->socket_to_main.sock = INVALID_SOCKET;
206 if (sitter->argv !=
NULL)
208 for (i = 0; i < sitter->argc; i++)
209 if (sitter->argv[i] !=
NULL)
212 sitter->argv[i] =
NULL;
218 if (sitter->envp !=
NULL)
220 char **e = sitter->envp;
228 if (sitter->child_handle !=
NULL)
230 CloseHandle (sitter->child_handle);
231 sitter->child_handle =
NULL;
244 if (sitter->start_sync_event !=
NULL)
247 CloseHandle (sitter->start_sync_event);
248 sitter->start_sync_event =
NULL;
251 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 252 if (sitter->end_sync_event !=
NULL)
254 CloseHandle (sitter->end_sync_event);
255 sitter->end_sync_event =
NULL;
269 if (sitter->child_handle ==
NULL)
273 TerminateProcess (sitter->child_handle, 12345);
285 return (sitter->child_handle ==
NULL);
307 if (!sitter->have_child_status ||
308 sitter->child_status == STILL_ACTIVE)
311 *status = sitter->child_status;
333 if (sitter->have_spawn_errno)
335 char *emsg = _dbus_win_error_string (sitter->spawn_errno);
337 "Failed to execute program %s: %s",
339 _dbus_win_free_error_string (emsg);
341 else if (sitter->have_child_status)
345 "Process %s exited with status %d",
346 sitter->
log_name, sitter->child_status);
352 "Process %s exited, status unknown",
377 unsigned int condition,
393 close_socket_to_babysitter (sitter);
397 sitter->finished_cb !=
NULL)
399 sitter->finished_cb (sitter, sitter->finished_data);
400 sitter->finished_cb =
NULL;
408 protect_argv (
char **argv,
416 *new_argv =
dbus_malloc ((argc + 1) *
sizeof (
char *));
417 if (*new_argv ==
NULL)
420 for (i = 0; i < argc; i++)
421 (*new_argv)[i] =
NULL;
434 for (i = 0; i < argc; i++)
439 int need_dblquotes =
FALSE;
442 if (*p ==
' ' || *p ==
'\t')
443 need_dblquotes =
TRUE;
449 while (*pp && *pp ==
'\\')
458 q = (*new_argv)[i] =
dbus_malloc (len + need_dblquotes*2 + 1);
476 while (*pp && *pp ==
'\\')
490 (*new_argv)[argc] =
NULL;
498 compose_string (
char **strings,
char separator)
505 if (!strings || !strings[0])
507 for (i = 0; strings[i]; i++)
508 n += strlen (strings[i]) + 1;
511 buf = p = malloc (n);
514 for (i = 0; strings[i]; i++)
516 strcpy (p, strings[i]);
517 p += strlen (strings[i]);
528 build_commandline (
char **argv)
530 return compose_string (argv,
' ');
534 build_env_string (
char** envp)
536 return compose_string (envp,
'\0');
540 spawn_program (
char* name,
char** argv,
char** envp)
542 PROCESS_INFORMATION pi = {
NULL, 0, 0, 0 };
544 char *arg_string, *env_string;
549 arg_string = build_commandline (argv + 1);
553 arg_string = build_commandline (argv);
556 return INVALID_HANDLE_VALUE;
558 env_string = build_env_string(envp);
560 memset (&si, 0,
sizeof (si));
563 result = CreateProcessA (name, arg_string,
NULL,
NULL,
FALSE, 0,
567 (LPVOID)env_string,
NULL, &si, &pi);
573 return INVALID_HANDLE_VALUE;
575 CloseHandle (pi.hThread);
580 static DWORD __stdcall
581 babysitter (
void *parameter)
589 _dbus_verbose (
"babysitter: spawning %s\n", sitter->
log_name);
592 sitter->child_handle = spawn_program (sitter->
log_name,
593 sitter->argv, sitter->envp);
596 if (sitter->child_handle == (HANDLE) -1)
598 sitter->child_handle =
NULL;
599 sitter->have_spawn_errno =
TRUE;
600 sitter->spawn_errno = GetLastError();
604 SetEvent (sitter->start_sync_event);
606 if (sitter->child_handle !=
NULL)
612 WaitForSingleObject (sitter->child_handle, INFINITE);
615 ret = GetExitCodeProcess (sitter->child_handle, &status);
618 sitter->child_status =
status;
619 sitter->have_child_status =
TRUE;
622 CloseHandle (sitter->child_handle);
623 sitter->child_handle =
NULL;
626 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 627 SetEvent (sitter->end_sync_event);
631 send (sitter->socket_to_main.sock,
" ", 1, 0);
643 DBusSpawnChildSetupFunc child_setup _DBUS_GNUC_UNUSED,
644 void *user_data _DBUS_GNUC_UNUSED,
648 HANDLE sitter_thread;
649 DWORD sitter_thread_id;
651 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
657 sitter = _dbus_babysitter_new ();
660 _DBUS_SET_OOM (error);
667 _DBUS_SET_OOM (error);
676 _DBUS_SET_OOM (error);
682 &sitter->socket_to_main,
692 _DBUS_SET_OOM (error);
705 _DBUS_SET_OOM (error);
709 sitter->argc = protect_argv (argv, &sitter->argv);
710 if (sitter->argc == -1)
712 _DBUS_SET_OOM (error);
718 sitter_thread = (HANDLE) CreateThread (
NULL, 0, babysitter,
719 sitter, 0, &sitter_thread_id);
721 if (sitter_thread == 0)
725 "Failed to create new thread");
728 CloseHandle (sitter_thread);
731 WaitForSingleObject (sitter->start_sync_event, INFINITE);
734 if (sitter_p !=
NULL)
739 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
752 DBusBabysitterFinishedFunc finished,
755 sitter->finished_cb = finished;
756 sitter->finished_data = user_data;
759 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 762 get_test_exec (
const char *exe,
765 const char *dbus_test_exec;
769 if (dbus_test_exec ==
NULL)
770 dbus_test_exec = DBUS_TEST_EXEC;
776 dbus_test_exec, exe, DBUS_EXEEXT))
782 return _dbus_string_get_data (scratch_space);
785 #define LIVE_CHILDREN(sitter) ((sitter)->child_handle != NULL) 790 if (sitter->child_handle ==
NULL)
793 WaitForSingleObject (sitter->end_sync_event, INFINITE);
797 check_spawn_nonexistent (
void *data)
809 argv[0] =
"/this/does/not/exist/32542sdgafgafdg";
814 _dbus_babysitter_block_for_child_exit (sitter);
823 _dbus_warn (
"Did not get an error launching nonexistent executable\n");
830 _dbus_warn (
"Not expecting error when launching nonexistent executable: %s: %s\n",
842 check_spawn_segfault (
void *data)
855 argv[0] = get_test_exec (
"test-segfault", &argv0);
867 _dbus_babysitter_block_for_child_exit (sitter);
878 _dbus_warn (
"Did not get an error launching segfaulting binary\n");
885 _dbus_warn (
"Not expecting error when launching segfaulting executable: %s: %s\n",
897 check_spawn_exit (
void *data)
910 argv[0] = get_test_exec (
"test-exit", &argv0);
922 _dbus_babysitter_block_for_child_exit (sitter);
933 _dbus_warn (
"Did not get an error launching binary that exited with failure code\n");
940 _dbus_warn (
"Not expecting error when launching exiting executable: %s: %s\n",
952 check_spawn_and_kill (
void *data)
965 argv[0] = get_test_exec (
"test-sleep-forever", &argv0);
979 _dbus_babysitter_block_for_child_exit (sitter);
991 _dbus_warn (
"Did not get an error after killing spawned binary\n");
998 _dbus_warn (
"Not expecting error when killing executable: %s: %s\n",
1010 _dbus_spawn_test (
const char *test_data_dir)
1012 if (!_dbus_test_oom_handling (
"spawn_nonexistent",
1013 check_spawn_nonexistent,
1020 if (getenv (
"DO_SEGFAULT_TEST"))
1021 if (!_dbus_test_oom_handling (
"spawn_segfault",
1022 check_spawn_segfault,
1026 if (!_dbus_test_oom_handling (
"spawn_exit",
1031 if (!_dbus_test_oom_handling (
"spawn_and_kill",
1032 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
DBusWatch * _dbus_watch_new(DBusPollable fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
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 dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
dbus_bool_t _dbus_socketpair(DBusSocket *fd1, DBusSocket *fd2, dbus_bool_t blocking, DBusError *error)
Creates pair of connect sockets (as in socketpair()).
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
#define DBUS_ERROR_SPAWN_CHILD_EXITED
While starting a new process, the child exited with a status code.
int status
Exit status code.
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...
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
dbus_bool_t _dbus_close_socket(DBusSocket fd, DBusError *error)
Closes a socket.
dbus_bool_t _dbus_babysitter_get_child_exited(DBusBabysitter *sitter)
Checks whether the child has exited, without blocking.
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.
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.
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.
dbus_bool_t _dbus_spawn_async_with_babysitter(DBusBabysitter **sitter_p, const char *log_name, char **argv, char **env, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error)
Spawns a new process.
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.
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
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.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
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_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
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...
const char * name
public error name field
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
DBusWatchList implementation details.
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...
void _dbus_babysitter_unref(DBusBabysitter *sitter)
Decrement the reference count on the babysitter object.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
#define DBUS_ERROR_SPAWN_FORK_FAILED
While starting a new process, the fork() call failed.
char * log_name
the name under which to log messages about this process being spawned
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
#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.
DBusSocket socket_to_babysitter
Connection to the babysitter process.
char * _dbus_strdup(const char *str)
Duplicates a string.
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...
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
DBusBabysitter * _dbus_babysitter_ref(DBusBabysitter *sitter)
Increment the reference count on the babysitter object.