From 1b7960f4216c553e8a51005d39b8fb32647155f2 Mon Sep 17 00:00:00 2001 From: Jon Ashburn Date: Mon, 4 Jan 2016 14:01:38 -0700 Subject: [PATCH] loader: Convert getenv on Windows to use GetEnvironmentVariable This allows loader to get updated environment variables. That is after the CRT has been initialized. --- layers/screenshot.cpp | 3 ++- loader/loader.c | 20 ++++++++++++++------ loader/vk_loader_platform.h | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/layers/screenshot.cpp b/layers/screenshot.cpp index edccb85..b3bd9af 100644 --- a/layers/screenshot.cpp +++ b/layers/screenshot.cpp @@ -588,7 +588,7 @@ VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, if (!screenshotEnvQueried) { - const char *_vk_screenshot = getenv("_VK_SCREENSHOT"); + const char *_vk_screenshot = loader_getenv("_VK_SCREENSHOT"); if (_vk_screenshot && *_vk_screenshot) { string spec(_vk_screenshot), word; @@ -614,6 +614,7 @@ VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR(VkQueue queue, start = comma + 1; } } + loader_free_getenv(_vk_screenshot); screenshotEnvQueried = true; } diff --git a/loader/loader.c b/loader/loader.c index 9ab4729..3b29efb 100644 --- a/loader/loader.c +++ b/loader/loader.c @@ -1366,7 +1366,7 @@ static bool loader_icd_init_entrys(struct loader_icd *icd, static void loader_debug_init(void) { - const char *env; + const char *env, *orig; if (g_loader_debug > 0) return; @@ -1374,7 +1374,7 @@ static void loader_debug_init(void) g_loader_debug = 0; /* parse comma-separated debug options */ - env = getenv("VK_LOADER_DEBUG"); + orig = env = loader_getenv("VK_LOADER_DEBUG"); while (env) { const char *p = strchr(env, ','); size_t len; @@ -1411,6 +1411,8 @@ static void loader_debug_init(void) env = p + 1; } + + loader_free_getenv(orig); } void loader_initialize(void) @@ -1939,10 +1941,11 @@ static void loader_get_manifest_files(const struct loader_instance *inst, out_files->count = 0; out_files->filename_list = NULL; - if (env_override != NULL && (override = getenv(env_override))) { + if (env_override != NULL && (override = loader_getenv(env_override))) { #if !defined(_WIN32) if (geteuid() != getuid()) { /* Don't allow setuid apps to use the env var: */ + loader_free_getenv(override); override = NULL; } #endif @@ -1984,6 +1987,7 @@ static void loader_get_manifest_files(const struct loader_instance *inst, return; } strcpy(loc, override); + loader_free_getenv(override); } // Print out the paths being searched if debugging is enabled @@ -2674,17 +2678,19 @@ static void loader_add_layer_implicit( if (prop->enable_env_var.name[0] == 0) { enable = true; } else { - env_value = getenv(prop->enable_env_var.name); + env_value = loader_getenv(prop->enable_env_var.name); if (env_value && !strcmp(prop->enable_env_var.value, env_value)) enable = true; + loader_free_getenv(env_value); } // disable_environment has priority, i.e. if both enable and disable // environment variables are set, the layer is disabled. Implicit layers // are required to have a disable_environment variables - env_value = getenv(prop->disable_env_var.name); + env_value = loader_getenv(prop->disable_env_var.name); if (env_value) enable = false; + loader_free_getenv(env_value); if (enable) loader_add_to_layer_list(inst, list, 1, prop); @@ -2708,7 +2714,7 @@ static void loader_add_layer_env( char *layerEnv; char *next, *name; - layerEnv = getenv(env_name); + layerEnv = loader_getenv(env_name); if (layerEnv == NULL) { return; } @@ -2718,6 +2724,8 @@ static void loader_add_layer_env( } strcpy(name, layerEnv); + loader_free_getenv(layerEnv); + while (name && *name ) { next = loader_get_next_path(name); loader_find_layer_name_add_list(inst, name, type, search_list, layer_list); diff --git a/loader/vk_loader_platform.h b/loader/vk_loader_platform.h index 55b84db..52d90a3 100644 --- a/loader/vk_loader_platform.h +++ b/loader/vk_loader_platform.h @@ -120,6 +120,17 @@ static inline char *loader_platform_dirname(char *path) return dirname(path); } +// Environment variables + +static inline char *loader_getenv(const char *name) +{ + return getenv(name); +} + +static inline void loader_free_getenv(const char *val) +{ +} + // Dynamic Loading of libraries: typedef void * loader_platform_dl_handle; static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) @@ -298,6 +309,33 @@ static char *loader_platform_basename(char *pathname) return current; } +// Environment variables + +static inline char *loader_getenv(const char *name) +{ + char *retVal; + DWORD valSize; + + valSize = GetEnvironmentVariableA(name, NULL, 0); + + // valSize DOES include the null terminator, so for any set variable + // will always be at least 1. If it's 0, the variable wasn't set. + if (valSize == 0) + return NULL; + + //TODO; FIXME This should be using any app defined memory allocation + retVal = (char *)malloc(valSize); + + GetEnvironmentVariableA(name, retVal, valSize); + + return retVal; +} + +static inline void loader_free_getenv(const char *val) +{ + free((void *)val); +} + // Dynamic Loading: typedef HMODULE loader_platform_dl_handle; static loader_platform_dl_handle loader_platform_open_library(const char* libPath) -- 2.7.4