Skip to content

Commit

Permalink
fullGetEnv update
Browse files Browse the repository at this point in the history
- use only environ variant
- replace getenv calls with fullGetEnv
  • Loading branch information
michalbiesek committed Jun 6, 2023
1 parent 0bb2198 commit 441a08a
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/cfgutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ cfgPathSearch(const char* cfgname)

char path[1024]; // Somewhat arbitrary choice for MAX_PATH

const char *homedir = getenv("HOME");
const char *scope_home = getenv("SCOPE_HOME");
const char *homedir = fullGetEnv("HOME");
const char *scope_home = fullGetEnv("SCOPE_HOME");
if (scope_home &&
(scope_snprintf(path, sizeof(path), "%s/conf/%s", scope_home, cfgname) > 0) &&
!scope_access(path, R_OK)) {
Expand Down Expand Up @@ -371,7 +371,7 @@ doEnvVariableSubstitution(const char* value)
char env_name[match_size + 1];
scope_strncpy(env_name, &inptr[match.rm_so], match_size);
env_name[match_size] = '\0';
char* env_value = getenv(&env_name[1]); // offset of 1 skips the $
char* env_value = fullGetEnv(&env_name[1]); // offset of 1 skips the $

// Grow outval buffer any time env_value is bigger than env_name
int size_growth = (!env_value) ? 0 : scope_strlen(env_value) - match_size;
Expand Down Expand Up @@ -1946,7 +1946,7 @@ processCustomFilterEnv(config_t* config, yaml_document_t* doc, yaml_node_t* node
if (equal) *equal = '\0';
char *envName = valueStr;
char *envVal = equal ? equal+1 : NULL;
char *env = getenv(envName);
char *env = fullGetEnv(envName);
if (env) {
if (envVal) {
if (!scope_strcmp(env, envVal)) {
Expand Down
2 changes: 1 addition & 1 deletion src/com.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ jsonEnvironmentObject()

if (!(root = cJSON_CreateObject())) goto err;

char *env_cribl_k8s_pod = getenv("CRIBL_K8S_POD");
char *env_cribl_k8s_pod = fullGetEnv("CRIBL_K8S_POD");
if (env_cribl_k8s_pod) {
if (!cJSON_AddStringToObjLN(root, "CRIBL_K8S_POD",
env_cribl_k8s_pod)) goto err;
Expand Down
2 changes: 1 addition & 1 deletion src/ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ ctlCreate()

size_t buf_size = DEFAULT_CBUF_SIZE;
char *qlen_str;
if ((qlen_str = getenv("SCOPE_QUEUE_LENGTH")) != NULL) {
if ((qlen_str = fullGetEnv("SCOPE_QUEUE_LENGTH")) != NULL) {
unsigned long qlen;
scope_errno = 0;
qlen = scope_strtoul(qlen_str, NULL, 10);
Expand Down
1 change: 0 additions & 1 deletion src/fn.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ initFn(void)

GETADDR(g_fn.vsyslog, "vsyslog");
GETADDR(g_fn.fork, "fork");
GETADDR(g_fn.getenv, "getenv");
GETADDR(g_fn.open, "open");
GETADDR(g_fn.openat, "openat");
GETADDR(g_fn.fopen, "fopen");
Expand Down
1 change: 0 additions & 1 deletion src/fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ typedef unsigned int nfds_t;
typedef struct {
void (*vsyslog)(int, const char *, va_list);
pid_t (*fork)(void);
char *(*getenv)(const char *);
int (*open)(const char *, int, ...);
int (*openat)(int, const char *, int, ...);
FILE *(*fopen)(const char *, const char *);
Expand Down
3 changes: 2 additions & 1 deletion src/metriccapture.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "circbuf.h"
#include "com.h"
#include "dbg.h"
#include "utils.h"
#include "metriccapture.h"
#include "scopestdlib.h"

Expand Down Expand Up @@ -42,7 +43,7 @@ initMetricCapture(void)

size_t buf_size = DEFAULT_CBUF_SIZE;
char *qlen_str;
if ((qlen_str = getenv("SCOPE_QUEUE_LENGTH")) != NULL) {
if ((qlen_str = fullGetEnv("SCOPE_QUEUE_LENGTH")) != NULL) {
unsigned long qlen;
scope_errno = 0;
qlen = scope_strtoul(qlen_str, NULL, 10);
Expand Down
2 changes: 1 addition & 1 deletion src/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ initState()
{
// g_http_guard_enable is always false unless
// SCOPE_HTTP_SERIALIZE_ENABLE is defined and is "true"
char *spin_env = getenv("SCOPE_HTTP_SERIALIZE_ENABLE");
char *spin_env = fullGetEnv("SCOPE_HTTP_SERIALIZE_ENABLE");
g_http_guard_enabled = (spin_env && !scope_strcmp(spin_env, "true"));
}

Expand Down
3 changes: 2 additions & 1 deletion src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "os.h"
#include "scopestdlib.h"
#include "fn.h"
#include "utils.h"
#include "transport.h"

// Yuck. Avoids naming conflict between our src/wrap.c and libssl.a
Expand Down Expand Up @@ -1030,7 +1031,7 @@ edgePath(void){

// 2) If CRIBL_HOME is defined and can be accessed,
// return $CRIBL_HOME/state/appscope.sock
const char *cribl_home = getenv("CRIBL_HOME");
const char *cribl_home = fullGetEnv("CRIBL_HOME");
if (cribl_home) {
char *new_path = NULL;
if (scope_asprintf(&new_path, "%s/%s", cribl_home, "state/appscope.sock") > 0) {
Expand Down
17 changes: 9 additions & 8 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int
checkEnv(char *env, char *val)
{
char *estr;
if (((estr = getenv(env)) != NULL) &&
if (((estr = fullGetEnv(env)) != NULL) &&
(scope_strncmp(estr, val, scope_strlen(estr)) == 0)) {
return TRUE;
}
Expand All @@ -62,19 +62,20 @@ checkEnv(char *env, char *val)

extern char** environ;

/*
* fullGetEnv allows to get an environment variable directly from environ
* Note:
* The implementation of fullGetEnv is corresponding to the getenv defined in
* our internal library
*/
char *
fullGetEnv(char *name) {
if (g_fn.getenv) {
return g_fn.getenv(name);
}

// if g_fn.getenv was not yet defined try to parse environ
size_t l = scope_strchrnul(name, '=') - name;
if (l && !name[l] && environ)
for (char **e = environ; *e; e++)
if (!scope_strncmp(name, *e, l) && l[*e] == '=')
return *e + l+1;
return NULL;
return NULL;
}

int
Expand Down Expand Up @@ -171,7 +172,7 @@ getpath(const char *cmd)
}

// try to resolve the cmd from PATH env variable
char *path_env_ptr = getenv("PATH");
char *path_env_ptr = fullGetEnv("PATH");
if (!path_env_ptr) goto out;
path_env = scope_strdup(path_env_ptr); // create a copy for strtok below
if (!path_env) goto out;
Expand Down
11 changes: 5 additions & 6 deletions src/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ threadInit()
{
// for debugging... if SCOPE_NO_SIGNAL is defined, then don't create
// a signal handler, nor a timer to send a signal.
if (getenv("SCOPE_NO_SIGNAL")) return;
if (fullGetEnv("SCOPE_NO_SIGNAL")) return;
if (!g_ctl) return;

if (osThreadInit(threadNow, g_thread.interval) == FALSE) {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ handleExit(void)
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0}; // 1 s

char *wait;
if ((wait = getenv("SCOPE_CONNECT_TIMEOUT_SECS")) != NULL) {
if ((wait = fullGetEnv("SCOPE_CONNECT_TIMEOUT_SECS")) != NULL) {
// wait for a connection to be established
// before we emit data
int wait_time;
Expand Down Expand Up @@ -1736,7 +1736,7 @@ init(void)
} else {
cfg = cfgCreateDefault();
// First try to use env variable
char *envFilterVal = getenv("SCOPE_FILTER");
char *envFilterVal = fullGetEnv("SCOPE_FILTER");
filter_status_t res = FILTER_SCOPED;
if (envFilterVal) {
/*
Expand Down Expand Up @@ -2890,11 +2890,11 @@ getScopeExec(const char *pathname)
* Note: the isgo check is strictly for Go dynamic execs.
* In this case we use scope only to force the use of HTTP 1.1.
*/
if (getenv("LD_PRELOAD") && (isstat == FALSE) && (isgo == FALSE)) {
if (fullGetEnv("LD_PRELOAD") && (isstat == FALSE) && (isgo == FALSE)) {
return NULL;
}

scopexec = getenv("SCOPE_EXEC_PATH");
scopexec = fullGetEnv("SCOPE_EXEC_PATH");
if (((scopexec = getpath(scopexec)) == NULL) &&
((scopexec = getpath("scope")) == NULL)) {

Expand Down Expand Up @@ -5717,7 +5717,6 @@ static got_list_t inject_hook_list[] = {
{"dup3", dup3, &g_fn.dup3},
{"vsyslog", vsyslog, &g_fn.vsyslog},
{"fork", fork, &g_fn.fork},
{"getenv", getenv, &g_fn.getenv},
{"socket", socket, &g_fn.socket},
{"shutdown", shutdown, &g_fn.shutdown},
{"listen", listen, &g_fn.listen},
Expand Down
2 changes: 1 addition & 1 deletion src/wrap_go.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ void
createGoStructFile(void) {
char* debug_file;
int fd;
if ((debug_file = getenv("SCOPE_GO_STRUCT_PATH")) &&
if ((debug_file = fullGetEnv("SCOPE_GO_STRUCT_PATH")) &&
((fd = scope_open(debug_file, O_CREAT|O_WRONLY|O_CLOEXEC, 0666)) != -1)) {
scope_dprintf(fd, "runtime.g|m=%d|\n", g_go_schema->struct_offsets.g_to_m);
scope_dprintf(fd, "runtime.m|tls=%d|\n", g_go_schema->struct_offsets.m_to_tls);
Expand Down
8 changes: 1 addition & 7 deletions test/unit/library/utilstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ main(int argc, char* argv[])
{
printf("running %s\n", argv[0]);

const struct CMUnitTest preInitFnTests[] = {
cmocka_unit_test(testGetEnvNonExisting),
};
int initTestStatus = cmocka_run_group_tests(preInitFnTests, NULL, NULL);

initFn();

const struct CMUnitTest tests[] = {
Expand All @@ -101,6 +96,5 @@ main(int argc, char* argv[])
cmocka_unit_test(testSigSafeUtoa),
cmocka_unit_test(testGetEnvNonExisting),
};
int testStatus = cmocka_run_group_tests(tests, groupSetup, groupTeardown);
return initTestStatus || testStatus;
return cmocka_run_group_tests(tests, groupSetup, groupTeardown);
}

0 comments on commit 441a08a

Please sign in to comment.