diff options
author | NIIBE Yutaka <[email protected]> | 2018-02-27 09:43:40 +0000 |
---|---|---|
committer | NIIBE Yutaka <[email protected]> | 2018-02-27 09:43:40 +0000 |
commit | 59fe3f26c1ca0fba16f76738cd05aaf80fb735ef (patch) | |
tree | e5940dcf290a6c7af06d795b9a088b89b62bef83 | |
parent | WS removal (diff) | |
download | gpgme-59fe3f26c1ca0fba16f76738cd05aaf80fb735ef.tar.gz gpgme-59fe3f26c1ca0fba16f76738cd05aaf80fb735ef.zip |
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 <[email protected]>
-rw-r--r-- | src/get-env.c | 56 |
1 files changed, 36 insertions, 20 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 +#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; -#error No thread-safe getenv nor getenv_r +#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 |