Skip to content

Commit

Permalink
DAOS-14532 gurt: Replace environment APIs hook
Browse files Browse the repository at this point in the history
Integrate reviewers comments:
- Factorize d_getenv_uint() functions.

Required-githooks: true
Signed-off-by: Cedric Koch-Hofer <cedric.koch-hofer@intel.com>
  • Loading branch information
kanard38 authored and Cedric Koch-Hofer committed Jan 2, 2024
1 parent 1ade13e commit 193c24c
Showing 1 changed file with 68 additions and 95 deletions.
163 changes: 68 additions & 95 deletions src/gurt/misc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright 2016-2023 Intel Corporation.
* (C) Copyright 2016-2024 Intel Corporation.
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
Expand Down Expand Up @@ -1177,24 +1177,16 @@ d_getenv_char(char *char_val, const char *name)
return rc;
}

/**
* get an unsigned integer type environment variables.
*
* \param[in,out] uint_val returned value of the ENV. Will not change the original
* value if ENV is not set or set as a non-integer value.
* \param[in] name name of the environment variable.
* \return 0 on success, a negative value on error.
*/
int
d_getenv_uint(unsigned *uint_val, const char *name)
static int
d_getenv_ull(unsigned long long *val, const char* name)
{
char *env;
char *endptr;
unsigned long val;
int rc;
char *env;
char *endptr;
unsigned long long tmp;
int rc;

assert(val != NULL);
assert(name != NULL);
assert(uint_val != NULL);

pthread_rwlock_rdlock(&d_env_lock);
env = getenv(name);
Expand All @@ -1209,21 +1201,14 @@ d_getenv_uint(unsigned *uint_val, const char *name)
}

errno = 0;
val = strtoul(env, &endptr, 0);
tmp = strtoull(env, &endptr, 0);
if (errno != 0 || endptr == env || *endptr != '\0') {
rc = -DER_INVAL;
goto out;
}

#if UINT_MAX != ULONG_MAX
assert(sizeof(unsigned) < sizeof(unsigned long));
if (val > UINT_MAX) {
rc = -DER_INVAL;
goto out;
}
#endif
*uint_val = (unsigned)val;
rc = -DER_SUCCESS;
*val = tmp;
rc = -DER_SUCCESS;

out:
pthread_rwlock_unlock(&d_env_lock);
Expand All @@ -1232,111 +1217,99 @@ d_getenv_uint(unsigned *uint_val, const char *name)
}

/**
* get a 32bits unsigned integer type environment variables
* get an unsigned integer type environment variables.
*
* \param[in,out] val returned value of the ENV. Will not change the original value if ENV
* is not set or set as a non-integer value.
* \param[in] name name of the environment variable.
* \return 0 on success, a negative value on error.
* \param[in,out] uint_val returned value of the ENV. Will not change the original
* value if ENV is not set or set as a non-integer value.
* \param[in] name name of the environment variable.
* \return 0 on success, a negative value on error.
*/
int
d_getenv_uint32_t(uint32_t *uint32_val, const char *name)
d_getenv_uint(unsigned *uint_val, const char *name)
{
char *env;
char *endptr;
unsigned long long val;
int rc;
unsigned long long tmp;

assert(uint_val != NULL);
assert(name != NULL);
assert(uint32_val != NULL);

pthread_rwlock_rdlock(&d_env_lock);
env = getenv(name);
if (env == NULL) {
rc = -DER_NONEXIST;
goto out;
}
rc = d_getenv_ull(&tmp, name);
if (rc != -DER_SUCCESS)
return rc;

if (!dis_unsigned_str(env)) {
rc = -DER_INVAL;
goto out;
#if UINT_MAX != ULLONG_MAX
assert(sizeof(unsigned) < sizeof(unsigned long long));
if (tmp > UINT_MAX) {
return -DER_INVAL;
}
#endif

errno = 0;
val = strtoull(env, &endptr, 0);
if (errno != 0 || endptr == env || *endptr != '\0') {
rc = -DER_INVAL;
goto out;
}
*uint_val = (unsigned)tmp;
return -DER_SUCCESS;
}

/**
* get a 32bits unsigned integer type environment variables
*
* \param[in,out] uint32_val returned value of the ENV. Will not change the original
* value if ENV is not set or set as a non-integer value.
* \param[in] name name of the environment variable.
* \return 0 on success, a negative value on error.
*/
int
d_getenv_uint32_t(uint32_t *uint32_val, const char *name)
{
int rc;
unsigned long long tmp;

assert(uint32_val != NULL);
assert(name != NULL);

rc = d_getenv_ull(&tmp, name);
if (rc != -DER_SUCCESS)
return rc;

#if UINT32_MAX != ULLONG_MAX
assert(sizeof(uint32_t) < sizeof(unsigned long long));
if (val > UINT32_MAX) {
rc = -DER_INVAL;
goto out;
if (tmp > UINT32_MAX) {
return -DER_INVAL;
}
#endif
*uint32_val = (uint64_t)val;
rc = -DER_SUCCESS;

out:
pthread_rwlock_unlock(&d_env_lock);

return rc;
*uint32_val = (uint32_t)tmp;
return -DER_SUCCESS;
}

/**
* get a 64bits unsigned integer type environment variables
*
* \param[in,out] val returned value of the ENV. Will not change the original value if ENV
* is not set or set as a non-integer value.
* \param[in] name name of the environment variable.
* \return 0 on success, a negative value on error.
* \param[in,out] uint64_val returned value of the ENV. Will not change the original
* value if ENV is not set or set as a non-integer value.
* \param[in] name name of the environment variable.
* \return 0 on success, a negative value on error.
*/
int
d_getenv_uint64_t(uint64_t *uint64_val, const char *name)
{
char *env;
char *endptr;
unsigned long long val;
int rc;
unsigned long long tmp;

assert(name != NULL);
assert(uint64_val != NULL);
assert(name != NULL);

pthread_rwlock_rdlock(&d_env_lock);
env = getenv(name);
if (env == NULL) {
rc = -DER_NONEXIST;
goto out;
}

if (!dis_unsigned_str(env)) {
rc = -DER_INVAL;
goto out;
}

errno = 0;
val = strtoull(env, &endptr, 0);
if (errno != 0 || endptr == env || *endptr != '\0') {
rc = -DER_INVAL;
goto out;
}
rc = d_getenv_ull(&tmp, name);
if (rc != -DER_SUCCESS)
return rc;

#if UINT64_MAX != ULLONG_MAX
assert(sizeof(uint64_t) < sizeof(unsigned long long));
if (val > UINT64_MAX) {
rc = -DER_INVAL;
goto out;
if (tmp > UINT64_MAX) {
return -DER_INVAL;
}
#endif
*uint64_val = (uint64_t)val;
rc = -DER_SUCCESS;

out:
pthread_rwlock_unlock(&d_env_lock);

return rc;
*uint64_val = (uint64_t)tmp;
return -DER_SUCCESS;
}

/**
Expand Down

0 comments on commit 193c24c

Please sign in to comment.