From 54d51a3cba0c5896fdbaac328ba833a31ef61f95 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Jan 12 2018 15:42:03 +0000 Subject: Add environment variables helpers These helpers make it easy to get an environment variable falling back to default value if the environment variable is not set. This will be used for using a special sanlock run directory and disabling privileged operations when running sanlock in the tests. Signed-off-by: Nir Soffer --- diff --git a/src/Makefile b/src/Makefile index 69fcd13..3ca25c9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -37,7 +37,8 @@ CMD_SOURCE = \ monotime.c \ cmd.c \ client_cmd.c \ - sanlock_sock.c + sanlock_sock.c \ + env.c LIB_ENTIRE_SOURCE = \ client.c \ @@ -51,11 +52,13 @@ LIB_ENTIRE_SOURCE = \ task.c \ timeouts.c \ direct_lib.c \ - monotime.c + monotime.c \ + env.c LIB_CLIENT_SOURCE = \ client.c \ - sanlock_sock.c + sanlock_sock.c \ + env.c LIBPC_ENTIRE_SOURCE = libsanlock.pc.in LIBPC_CLIENT_SOURCE = libsanlock_client.pc.in diff --git a/src/env.c b/src/env.c new file mode 100644 index 0000000..e1d2f3a --- /dev/null +++ b/src/env.c @@ -0,0 +1,34 @@ +/* + * Copyright 2018 Red Hat, Inc. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU General Public License v2 or (at your option) any later version. + */ + +#include +#include + +#include "env.h" + +const char *env_get(const char *key, const char *defval) +{ + const char *val; + + val = getenv(key); + if (val == NULL) + return defval; + + return val; +} + +int env_get_bool(const char *key, int defval) +{ + const char *val; + + val = getenv(key); + if (val == NULL) + return defval; + + return strcmp(val, "1") ? 0 : 1; +} diff --git a/src/env.h b/src/env.h new file mode 100644 index 0000000..ca3a3ff --- /dev/null +++ b/src/env.h @@ -0,0 +1,15 @@ +/* + * Copyright 2018 Red Hat, Inc. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU General Public License v2 or (at your option) any later version. + */ + +#ifndef __ENV_H__ +#define __ENV_H__ + +const char *env_get(const char *key, const char *defval); +int env_get_bool(const char *key, int defval); + +#endif