mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-16 23:06:03 +00:00
Added thread-safe environment functions
Also marked the existing functions as unsafe, as they can cause crashes if used in multi-threaded applications. As a bonus, since the new functions are hashtable based, hint environment lookups are much faster.
This commit is contained in:
@@ -35,15 +35,6 @@
|
||||
#include "../SDL_sysprocess.h"
|
||||
#include "../../file/SDL_iostream_c.h"
|
||||
|
||||
#if defined(SDL_PLATFORM_MACOS)
|
||||
#include <crt_externs.h>
|
||||
#define environ (*_NSGetEnviron())
|
||||
#elif defined(SDL_PLATFORM_FREEBSD)
|
||||
#include <dlfcn.h>
|
||||
#define environ ((char **)dlsym(RTLD_DEFAULT, "environ"))
|
||||
#else
|
||||
extern char **environ;
|
||||
#endif
|
||||
|
||||
#define READ_END 0
|
||||
#define WRITE_END 1
|
||||
@@ -114,7 +105,7 @@ static bool GetStreamFD(SDL_PropertiesID props, const char *property, int *resul
|
||||
bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID props)
|
||||
{
|
||||
char * const *args = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, NULL);
|
||||
char * const *env = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, environ);
|
||||
char * const *env = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ENVIRONMENT_POINTER, NULL);
|
||||
SDL_ProcessIO stdin_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_NULL);
|
||||
SDL_ProcessIO stdout_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_INHERITED);
|
||||
SDL_ProcessIO stderr_option = (SDL_ProcessIO)SDL_GetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDERR_NUMBER, SDL_PROCESS_STDIO_INHERITED);
|
||||
@@ -124,6 +115,7 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
|
||||
int stdout_pipe[2] = { -1, -1 };
|
||||
int stderr_pipe[2] = { -1, -1 };
|
||||
int fd = -1;
|
||||
char **env_copy = NULL;
|
||||
|
||||
// Keep the malloc() before exec() so that an OOM won't run a process at all
|
||||
SDL_ProcessData *data = SDL_calloc(1, sizeof(*data));
|
||||
@@ -278,6 +270,11 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
|
||||
}
|
||||
}
|
||||
|
||||
if (!env) {
|
||||
env_copy = SDL_GetEnvironmentVariables(SDL_GetEnvironment());
|
||||
env = env_copy;
|
||||
}
|
||||
|
||||
// Spawn the new process
|
||||
if (posix_spawnp(&data->pid, args[0], &fa, &attr, args, env) != 0) {
|
||||
SDL_SetError("posix_spawn failed: %s", strerror(errno));
|
||||
@@ -308,6 +305,7 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
|
||||
|
||||
posix_spawn_file_actions_destroy(&fa);
|
||||
posix_spawnattr_destroy(&attr);
|
||||
SDL_free(env_copy);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -338,6 +336,7 @@ posix_spawn_fail_none:
|
||||
if (stderr_pipe[WRITE_END] >= 0) {
|
||||
close(stderr_pipe[WRITE_END]);
|
||||
}
|
||||
SDL_free(env_copy);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user