From 59fe3f26c1ca0fba16f76738cd05aaf80fb735ef Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Tue, 27 Feb 2018 18:43:40 +0900 Subject: [PATCH] core: Support non-thread-safe getenv. * src/get-env.c (_gpgme_getenv): Use gpgrt_lock_lock and gpgrt_lock_unlock to protect call of getnev. Signed-off-by: NIIBE Yutaka --- src/get-env.c | 60 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/get-env.c b/src/get-env.c index 1adbeee7..b13706f9 100644 --- a/src/get-env.c +++ b/src/get-env.c @@ -28,29 +28,11 @@ #include "util.h" -#if defined(HAVE_THREAD_SAFE_GETENV) -/* We prefer using getenv() if it is thread-safe. */ - /* Retrieve the environment variable NAME and return a copy of it in a malloc()'ed buffer in *VALUE. If the environment variable is not set, return NULL in *VALUE. */ -gpgme_error_t -_gpgme_getenv (const char *name, char **value) -{ - char *env_value; - env_value = getenv (name); - if (!env_value) - *value = NULL; - else - { - *value = strdup (env_value); - if (!*value) - return gpg_error_from_syserror (); - } - return 0; -} -#elif defined (HAVE_GETENV_R) +#ifdef HAVE_GETENV_R #define INITIAL_GETENV_SIZE 32 gpgme_error_t @@ -91,7 +73,41 @@ _gpgme_getenv (const char *name, char **value) return 0; } #else - -#error No thread-safe getenv nor getenv_r - +#ifndef HAVE_THREAD_SAFE_GETENV +GPGRT_LOCK_DEFINE (environ_lock); +#endif + +gpgme_error_t +_gpgme_getenv (const char *name, char **value) +{ + char *env_value; + gpgme_error_t err = 0; + +#ifndef HAVE_THREAD_SAFE_GETENV + gpg_err_code_t rc; + + rc= gpgrt_lock_lock (&environ_lock); + if (rc) + { + err = gpg_error (rc); + goto leave; + } +#endif + env_value = getenv (name); + if (!env_value) + *value = NULL; + else + { + *value = strdup (env_value); + if (!*value) + err = gpg_error_from_syserror (); + } +#ifndef HAVE_THREAD_SAFE_GETENV + rc = gpgrt_lock_unlock (&environ_lock); + if (rc) + err = gpg_error (rc); + leave: +#endif + return err; +} #endif