Skip to content

Commit

Permalink
Merge pull request #1503 from criblio/fix-1502-env
Browse files Browse the repository at this point in the history
Implement `fullGetEnv` function
  • Loading branch information
seanvaleo authored Jun 9, 2023
2 parents 0e25f29 + 441a08a commit a8426bf
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 31 deletions.
10 changes: 5 additions & 5 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 @@ -265,7 +265,7 @@ cfgPathSearch(const char* cfgname)
char *
cfgPath(void)
{
const char* envPath = getenv("SCOPE_CONF_PATH");
const char* envPath = fullGetEnv("SCOPE_CONF_PATH");

// If SCOPE_CONF_PATH is set, and the file can be opened, use it.
char *path;
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
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
6 changes: 6 additions & 0 deletions src/scopestdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ extern int scopelibc_vsscanf(const char *, const char *, va_list
extern int scopelibc_strcmp(const char *, const char *);
extern int scopelibc_strncmp(const char *, const char *, size_t);
extern int scopelibc_strcasecmp(const char *, const char *);
extern char * scopelibc_strchrnul(const char *, int );
extern char * scopelibc_strcpy(char *, const char *);
extern char * scopelibc_strncpy(char *, const char *, size_t);
extern char * scopelibc_stpcpy(char *, const char *);
Expand Down Expand Up @@ -706,6 +707,11 @@ scope_strcasecmp(const char *s1, const char *s2) {
return scopelibc_strcasecmp(s1, s2);
}

char *
scope_strchrnul(const char *s, int c) {
return scopelibc_strchrnul(s, c);
}

size_t
scope_strcspn(const char *s, const char *reject) {
return scopelibc_strcspn(s, reject);
Expand Down
25 changes: 13 additions & 12 deletions src/scopestdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,20 @@ int scope_vprintf(const char *, va_list);
int scope_strcmp(const char *, const char *);
int scope_strncmp(const char *, const char *, size_t);
int scope_strcasecmp(const char *, const char *);
char* scope_strcpy(char *, const char *);
char* scope_strncpy(char *, const char *, size_t);
char* scope_stpcpy(char *, const char *);
char* scope_stpcpy(char *, const char *);
char* scope_stpncpy(char *, const char *, size_t);
char * scope_strchrnul(const char *, int);
char * scope_strcpy(char *, const char *);
char * scope_strncpy(char *, const char *, size_t);
char * scope_stpcpy(char *, const char *);
char * scope_stpcpy(char *, const char *);
char * scope_stpncpy(char *, const char *, size_t);
size_t scope_strcspn(const char *, const char *);
char* scope_strcat(char *, const char *);
char* scope_strncat(char *, const char *, size_t);
char* scope_strpbrk(const char *, const char *);
char* scope_strcasestr(const char *, const char *);
char* scope_strtok(char *, const char *);
char* scope_strtok_r(char *, const char *, char **);
const char* scope_gai_strerror(int);
char * scope_strcat(char *, const char *);
char * scope_strncat(char *, const char *, size_t);
char * scope_strpbrk(const char *, const char *);
char * scope_strcasestr(const char *, const char *);
char * scope_strtok(char *, const char *);
char * scope_strtok_r(char *, const char *, char **);
const char * scope_gai_strerror(int);

// Network handling operations
int scope_gethostname(char *, size_t);
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
21 changes: 19 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,30 @@ 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;
}
return FALSE;
}

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) {
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;
}

int
fullSetenv(const char *key, const char *val, int overwrite)
Expand Down Expand Up @@ -155,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
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ unsigned int strToVal(enum_map_t[], const char *);
const char* valToStr(enum_map_t[], unsigned int);

int checkEnv(char *, char *);
char *fullGetEnv(char *);
int fullSetenv(const char *, const char *, int);
void setPidEnv(int);
char *getpath(const char *);
Expand Down
10 changes: 5 additions & 5 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
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
1 change: 1 addition & 0 deletions test/integration/bash/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN git clone https://git.savannah.gnu.org/git/bash.git && \

COPY ./bash/runbashmem.sh /mybin
COPY ./bash/runbashnomem.sh /mybin
COPY ./bash/scope_bash.yml /opt

ENV SCOPE_CRIBL_ENABLE=false

Expand Down
24 changes: 24 additions & 0 deletions test/integration/bash/scope-test
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

CFG_EVT_FILE="/tmp/event_bash.log"
CFG_METRICS_FILE="/tmp/metrics_bash.log"
CFG_LOG_FILE="/tmp/log_bash.log"

# Check that the test was setup correctly
# (mem/bash is built normally, nomem/bash with --without-bash-malloc)
echo "Checking to see if /mybin/mem/bash defines malloc as expected."
Expand Down Expand Up @@ -55,5 +59,25 @@ fi
echo "/mybin/nomem/bash ./runbashnomem.sh passed"
echo ""

echo "Executing bash with SCOPE_CONF_PATH"
# See if bash will respect SCOPE_CONF_PATH environment variable
LD_PRELOAD=/usr/local/scope/lib/libscope.so SCOPE_CONF_PATH=/opt/scope_bash.yml bash &
sleep 1

if [ ! -f $CFG_EVT_FILE ]; then
echo "$CFG_EVT_FILE not found!"
exit 1
fi

if [ ! -f $CFG_METRICS_FILE ]; then
echo "$CFG_METRICS_FILE not found!"
exit 1
fi

if [ ! -f $CFG_LOG_FILE ]; then
echo "$CFG_LOG_FILE not found!"
exit 1
fi

echo "All bash tests passed"
exit 0
80 changes: 80 additions & 0 deletions test/integration/bash/scope_bash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
metric:
enable: true
format:
type: statsd
statsdprefix:
statsdmaxlen: 512
verbosity : 4
watch:
- type: statsd
- type: fs
- type: net
- type: http
- type: dns
- type: process
transport:
type: file
path: "/tmp/metrics_bash.log"
event:
enable: true
format:
type: ndjson
maxeventpersec: 10000
enhancefs: true
watch:
- type: file
name: (\/logs?\/)|(\.log$)|(\.log[.\d]) # matches the filename
value: .* # matches data read or written
- type: console
name: (stdout)|(stderr) # matches the output stream
value: .* # matches data written
allowbinary: true
- type: net
name: .*
field: .*
value: .*
- type: fs
name: .*
field: .*
value: .*
- type: dns
name: .*
field: .*
value: .*
- type: http
name: .* # event name; http.req or http.resp
field: .* # matches field names; duration, http_status, etc
value: .* # matches field values
headers: # list of filters matched against header names
transport:
type: file
path: "/tmp/event_bash.log"
payload:
enable: false
dir: '/tmp'
libscope:
configevent: true
summaryperiod : 10
commanddir : '/tmp'
log:
level: warning
transport:
type: file
path: "/tmp/log_bash.log"
buffer: line
snapshot:
coredump: false
backtrace: false
cribl:
enable: false
transport:
type: edge
host: 127.0.0.1
port: 10090
tls:
enable: false
validateserver: true
cacertpath: ''
tags:
protocol:
custom:
Loading

0 comments on commit a8426bf

Please sign in to comment.