Skip to content

Commit

Permalink
(#1108) IPC mechanism - server part
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbiesek committed Nov 28, 2022
1 parent 92780ed commit beb3fc2
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 3 deletions.
1 change: 1 addition & 0 deletions os/linux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ coretest: export USER ?= $(shell id -u -n)
coretest: $(C_FILES) $(YAML_AR) $(JSON_AR) $(TEST_LIB)
@echo "$${CI:+::group::}Building Tests"
$(CC) -c $(TEST_CFLAGS) $(C_FILES) $(INCLUDES) $(TEST_INCLUDES)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/ipctest ipctest.o ipc.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/vdsotest vdsotest.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/libvertest libvertest.o libver.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
$(CC) $(TEST_CFLAGS) -o test/$(OS)/libdirtest libdirtest.o libdir.o nsfile.o libver.o scopestdlib.o dbg.o test.o $(TEST_AR) $(TEST_LD_FLAGS)
Expand Down
2 changes: 1 addition & 1 deletion os/linux/aarch64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARCH_OBJ=$(ARCH)
LD_FLAGS=$(MUSL_AR) $(UNWIND_AR) $(PCRE2_AR) $(LS_HPACK_AR) $(YAML_AR) $(JSON_AR) -ldl -lpthread -lrt -lresolv -Lcontrib/build/funchook -lfunchook -Lcontrib/build/funchook/capstone_src-prefix/src/capstone_src-build -lcapstone -z noexecstack
INCLUDES=-I./contrib/libyaml/include -I./contrib/cJSON -I./os/$(OS) -I./contrib/pcre2/src -I./contrib/build/pcre2 -I./contrib/funchook/capstone_src/include/ -I./contrib/jni -I./contrib/jni/linux/ -I./contrib/openssl/include -I./contrib/build/openssl/include -I./contrib/build/libunwind/include -I./contrib/libunwind/include/

$(LIBSCOPE): src/wrap.c src/state.c src/httpstate.c src/metriccapture.c src/report.c src/httpagg.c src/plattime.c src/fn.c os/$(OS)/os.c src/cfgutils.c src/cfg.c src/transport.c src/log.c src/mtc.c src/circbuf.c src/linklist.c src/evtformat.c src/ctl.c src/mtcformat.c src/com.c src/scopestdlib.c src/dbg.c src/search.c src/scopeelf.c src/utils.c src/strset.c src/javabci.c src/javaagent.c
$(LIBSCOPE): src/wrap.c src/state.c src/httpstate.c src/metriccapture.c src/report.c src/httpagg.c src/plattime.c src/fn.c os/$(OS)/os.c src/cfgutils.c src/cfg.c src/transport.c src/log.c src/mtc.c src/circbuf.c src/linklist.c src/evtformat.c src/ctl.c src/mtcformat.c src/com.c src/scopestdlib.c src/dbg.c src/search.c src/scopeelf.c src/utils.c src/strset.c src/javabci.c src/javaagent.c src/ipc.c
@$(MAKE) -C contrib funchook pcre2 openssl ls-hpack musl libyaml libunwind cJSON
@echo "$${CI:+::group::}Building $@"
$(CC) $(CFLAGS) \
Expand Down
2 changes: 1 addition & 1 deletion os/linux/x86_64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ INCLUDES=-I./contrib/libyaml/include -I./contrib/cJSON -I./os/$(OS) -I./contrib/
# objcopy -I binary -O elf64-x86-64 -B i386 ./lib/$(OS)/libscope.so ./lib/$(OS)/libscope.o && \
# rm -f ./lib/$(OS)/libscope.so

$(LIBSCOPE): src/wrap.c src/state.c src/httpstate.c src/metriccapture.c src/report.c src/httpagg.c src/plattime.c src/fn.c os/$(OS)/os.c src/cfgutils.c src/cfg.c src/transport.c src/log.c src/mtc.c src/circbuf.c src/linklist.c src/evtformat.c src/ctl.c src/mtcformat.c src/com.c src/scopestdlib.c src/dbg.c src/search.c src/sysexec.c src/gocontext.S src/scopeelf.c src/wrap_go.c src/utils.c src/strset.c src/javabci.c src/javaagent.c
$(LIBSCOPE): src/wrap.c src/state.c src/httpstate.c src/metriccapture.c src/report.c src/httpagg.c src/plattime.c src/fn.c os/$(OS)/os.c src/cfgutils.c src/cfg.c src/transport.c src/log.c src/mtc.c src/circbuf.c src/linklist.c src/evtformat.c src/ctl.c src/mtcformat.c src/com.c src/scopestdlib.c src/dbg.c src/search.c src/sysexec.c src/gocontext.S src/scopeelf.c src/wrap_go.c src/utils.c src/strset.c src/javabci.c src/javaagent.c src/ipc.c
@$(MAKE) -C contrib funchook pcre2 openssl ls-hpack musl libyaml cJSON libunwind
@echo "$${CI:+::group::}Building $@"
$(CC) $(CFLAGS) $(ARCH_CFLAGS) \
Expand Down
2 changes: 1 addition & 1 deletion os/macOS/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $(PCRE2_AR):
cd contrib/pcre2/build && cmake ..
cd contrib/pcre2/build && make

libscope.so: src/wrap.c src/state.c src/httpstate.c src/report.c src/httpagg.c src/plattime.c src/fn.c os/$(OS)/os.c src/cfgutils.c src/cfg.c src/transport.c src/log.c src/mtc.c src/circbuf.c src/linklist.c src/evtformat.c src/ctl.c src/mtcformat.c src/com.c src/scopestdlib.c src/dbg.c src/search.c src/utils.c $(YAML_SRC) contrib/cJSON/cJSON.c
libscope.so: src/wrap.c src/state.c src/httpstate.c src/report.c src/httpagg.c src/plattime.c src/fn.c os/$(OS)/os.c src/cfgutils.c src/cfg.c src/transport.c src/log.c src/mtc.c src/circbuf.c src/linklist.c src/evtformat.c src/ctl.c src/mtcformat.c src/com.c src/scopestdlib.c src/dbg.c src/search.c src/utils.c $(YAML_SRC) contrib/cJSON/cJSON.c src/ipc.c
@echo "Building libscope.so ..."
make $(PCRE2_AR)
$(CC) $(CFLAGS) -shared -fvisibility=hidden -DSCOPE_VER=\"$(SCOPE_VER)\" $(YAML_DEFINES) -o ./lib/$(OS)/$@ $(INCLUDES) $^ -e,prog_version $(LD_FLAGS)
Expand Down
55 changes: 55 additions & 0 deletions src/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "runtimecfg.h"
#include "javaagent.h"
#include "inject.h"
#include "ipc.h"
#include "scopestdlib.h"
#include "../contrib/libmusl/musl.h"

Expand All @@ -55,6 +56,7 @@ static const char *g_cmddir;
static list_t *g_nsslist;
static uint64_t reentrancy_guard = 0ULL;
static rlim_t g_max_fds = 0;
static mqd_t g_receiver_msg_q = (mqd_t) -1;

typedef int (*ssl_rdfunc_t)(SSL *, void *, int);
typedef int (*ssl_wrfunc_t)(SSL *, const void *, int);
Expand Down Expand Up @@ -423,6 +425,45 @@ cmdAttach(void)
return TRUE;
}

/*
* Handle IPC communication
*/
static void
ipcCommunication(void) {
const char *listenMqMsgName = "ScopeCLI";
size_t reqMqSize;
if (ipcIsActive(g_receiver_msg_q, &reqMqSize) == FALSE) {
return;
}

long msgCount = ipcInfoMsgCount(g_receiver_msg_q);
if (msgCount <= 0) {
return;
}

ipc_cmd_t cmd = -1;
// scopeLogError("Request incoming");
if (ipcRequestMsgHandler(g_receiver_msg_q, reqMqSize, &cmd) == FALSE) {
// scopeLogError("Request NOK");
return;
}
// scopeLogError("Request OK");

// Handle response
size_t respMqSize;
mqd_t cliMqDes = ipcOpenWriteConnection(listenMqMsgName);
if (ipcIsActive(cliMqDes, &respMqSize) == FALSE) {
scopeLogError("%s is missing.", listenMqMsgName);
return;
}

if (ipcResponseMsgHandler(cliMqDes, respMqSize, cmd) == FALSE) {
scopeLogError("Error: with sending answet to %s for cmd %d", listenMqMsgName, cmd);
}

ipcCloseConnection(cliMqDes);
}

static void
remoteConfig()
{
Expand Down Expand Up @@ -715,6 +756,11 @@ threadNow(int sig)
return;
}

// Create IPC connection message queue
char name[256];
scope_snprintf(name, sizeof(name), "/ScopeIPC.%d", g_proc.pid);
g_receiver_msg_q = ipcCreateNonBlockReadConnection(name);

g_thread.once = TRUE;

// Restore a handler if one exists
Expand Down Expand Up @@ -1002,6 +1048,14 @@ handleExit(void)
ctlDisconnect(g_ctl, CFG_CTL);
logFlush(g_log);
logDisconnect(g_log);
size_t mqSize;
if (ipcIsActive(g_receiver_msg_q, &mqSize)) {
ipcCloseConnection(g_receiver_msg_q);
g_receiver_msg_q = (mqd_t) -1;
char name[256];
scope_snprintf(name, sizeof(name), "/ScopeIPC.%d", g_proc.pid);
ipcDestroyConnection(name);
}
}

static void *
Expand Down Expand Up @@ -1081,6 +1135,7 @@ periodic(void *arg)
}
}
remoteConfig();
ipcCommunication();
}

return NULL;
Expand Down
1 change: 1 addition & 0 deletions test/execute.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fi
declare -i ERR=0

run_test test/${OS}/vdsotest
run_test test/${OS}/ipctest
run_test test/${OS}/libvertest
run_test test/${OS}/libdirtest
run_test test/${OS}/nsinfotest
Expand Down
Loading

0 comments on commit beb3fc2

Please sign in to comment.