diff options
author | Ralf Habacker <ralf.habacker@freenet.de> | 2021-11-24 09:49:47 +0100 |
---|---|---|
committer | Ralf Habacker <ralf.habacker@freenet.de> | 2021-11-29 13:07:04 +0100 |
commit | 0eef54ea1b00c8e078da7294b8466a1ff7ae8fea (patch) | |
tree | 9ead8d3f61ca6002bb051613ede7292b4dac0723 | |
parent | ba1dc32667ac4bf892d4219ec8e3a5a712d008db (diff) | |
download | dbus-0eef54ea1b00c8e078da7294b8466a1ff7ae8fea.tar.gz |
_dbus_spawn_program(): Add additional parameter to return error
By specifying an error instance via the additional parameter, errors that
occur in it are transported to the caller in a coordinated manner.
This eliminates the need to query GetLastError() outside the function,
which can return an incorrect value if the implementation changes.
-rw-r--r-- | dbus/dbus-spawn-win.c | 29 | ||||
-rw-r--r-- | dbus/dbus-sysdeps-win.h | 3 | ||||
-rw-r--r-- | tools/dbus-run-session.c | 14 |
3 files changed, 31 insertions, 15 deletions
diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index f0773aa7..5d444b58 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -494,11 +494,23 @@ build_env_string (char** envp) return compose_string (envp, '\0'); } +/** + * Creates a process with arguments and environment variables + * + * @param name name of the program + * @param argv list of char* pointers for the arguments + * @param envp list of char pointers for the environment + * @param inherit_handles specifies whether handles should be inherited by the child process + * @param error the error to set, if NULL no error will be set + * @return #NULL if an error occurred, the reason is returned in \p error + * @note The call to GetLastError() after this function may not return the expected value. + */ HANDLE _dbus_spawn_program (const char *name, char **argv, char **envp, - dbus_bool_t inherit_handles) + dbus_bool_t inherit_handles, + DBusError *error) { PROCESS_INFORMATION pi = { NULL, 0, 0, 0 }; STARTUPINFOA si; @@ -514,7 +526,10 @@ _dbus_spawn_program (const char *name, arg_string = build_commandline (argv); #endif if (!arg_string) - return NULL; + { + dbus_set_error (error, DBUS_ERROR_FAILED, "No arguments given to start '%s' or out of memory", name); + goto out; + } env_string = build_env_string(envp); @@ -545,7 +560,13 @@ _dbus_spawn_program (const char *name, free (env_string); if (!result) - return NULL; + { + _dbus_win_set_error_from_last_error (error, "Unable to start '%s' with arguments '%s'", name, arg_string); + goto out; + } + +out: + _DBUS_ASSERT_ERROR_XOR_BOOL (error, result); CloseHandle (pi.hThread); return pi.hProcess; @@ -672,7 +693,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, _dbus_verbose ("babysitter: spawn child '%s'\n", my_argv[0]); PING(); - handle = _dbus_spawn_program (sitter->log_name, my_argv, (char **) envp, FALSE); + handle = _dbus_spawn_program (sitter->log_name, my_argv, (char **) envp, FALSE, NULL); if (my_argv != NULL) { diff --git a/dbus/dbus-sysdeps-win.h b/dbus/dbus-sysdeps-win.h index d63fb4f1..52db915b 100644 --- a/dbus/dbus-sysdeps-win.h +++ b/dbus/dbus-sysdeps-win.h @@ -93,7 +93,8 @@ dbus_bool_t _dbus_getsid(char **sid, dbus_pid_t process_id); HANDLE _dbus_spawn_program (const char *name, char **argv, char **envp, - dbus_bool_t inherit_handles); + dbus_bool_t inherit_handles, + DBusError *error); DBUS_PRIVATE_EXPORT void _dbus_win_set_error_from_last_error (DBusError *error, diff --git a/tools/dbus-run-session.c b/tools/dbus-run-session.c index f481d7ce..a622fb3d 100644 --- a/tools/dbus-run-session.c +++ b/tools/dbus-run-session.c @@ -441,12 +441,9 @@ run_session (const char *dbus_daemon, dbus_daemon_argv[3] = _dbus_string_get_data (&argv_strings[3]); dbus_daemon_argv[4] = NULL; - server_handle = _dbus_spawn_program (dbus_daemon, dbus_daemon_argv, NULL, TRUE); + server_handle = _dbus_spawn_program (dbus_daemon, dbus_daemon_argv, NULL, TRUE, &error); if (server_handle == NULL) - { - _dbus_win_set_error_from_last_error (&error, "Could not start dbus daemon"); - goto out; - } + goto out; /* wait until dbus-daemon is ready for connections */ if (ready_event_handle != NULL) @@ -529,12 +526,9 @@ run_session (const char *dbus_daemon, if (!env) goto out; - app_handle = _dbus_spawn_program (argv[prog_arg], argv + prog_arg, env, FALSE); + app_handle = _dbus_spawn_program (argv[prog_arg], argv + prog_arg, env, FALSE, &error); if (app_handle == NULL) - { - _dbus_win_set_error_from_last_error (&error, "Unable to start child process"); - goto out; - } + goto out; WaitForSingleObject (app_handle, INFINITE); if (!GetExitCodeProcess (app_handle, &exit_code)) |