diff --git a/include/hexagram/module.h b/include/hexagram/module.h new file mode 100644 index 0000000..a1bcd0f --- /dev/null +++ b/include/hexagram/module.h @@ -0,0 +1,18 @@ +#ifndef _HEXAGRAM_MODULE_H +#define _HEXAGRAM_MODULE_H + +#include + +typedef struct _hexagram_module hexagram_module; + +hexagram_module *hexagram_module_open(const char *soname); + +int hexagram_module_close(hexagram_module *module); + +const char *hexagram_module_name(hexagram_module *module); + +int hexagram_module_run(hexagram_module *module, hexagram_can_if *can_if); + +int hexagram_module_stop(hexagram_module *module); + +#endif /* _HEXAGRAM_MODULE_H */ diff --git a/src/Makefile b/src/Makefile index ad4cc1b..5969c28 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,10 +7,10 @@ CC = $(CROSS)cc CFLAGS += -fPIC -I$(INCLUDE_PATH) LDFLAGS = -HEADERS = dict.h hash.h can.h capture.h pcapng.h +HEADERS = dict.h hash.h can.h capture.h pcapng.h module.h HEADERS_LOCAL = util.h -OBJS = dict.o hash.o can.o capture.o pcapng.o +OBJS = dict.o hash.o can.o capture.o pcapng.o module.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/module.c b/src/module.c new file mode 100644 index 0000000..0489b45 --- /dev/null +++ b/src/module.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include + +#include + +struct _hexagram_module { + const char *soname; + void *handle; + char *(*name)(); + int (*run)(hexagram_can_if *can_if); + int pid; +}; + +hexagram_module *hexagram_module_open(const char *soname) { + hexagram_module *module; + + if ((module = malloc(sizeof(*module))) == NULL) { + goto error_malloc; + } + + if ((module->handle = dlopen(soname, RTLD_LAZY)) == NULL) { + goto error_dlopen; + } + + if ((module->run = dlsym(module->handle, "_hexagram_module_run")) == NULL) { + goto error_dlsym; + } + + if ((module->name = dlsym(module->handle, "_hexagram_module_name")) == NULL) { + goto error_dlsym; + } + + module->soname = soname; + module->pid = 0; + + return module; + +error_dlsym: + dlclose(module->handle); + +error_dlopen: + free(module); + +error_malloc: + return NULL; +} + +const char *hexagram_module_name(hexagram_module *module) { + return module->name(); +} + +int hexagram_module_run(hexagram_module *module, hexagram_can_if *can_if) { + /* + * Module already stopped + */ + if (module->pid < 0) { + return -1; + } + + return module->run(can_if); +} + +int hexagram_module_stop(hexagram_module *module) { + if (module->pid <= 0) { + return 0; + } + + if (kill(module->pid, SIGTERM) < 0) { + if (kill(module->pid, SIGKILL) < 0) { + goto error_kill; + } + } + + if (waitpid(module->pid, NULL, 0) < 0) { + goto error_waitpid; + } + + /* + * Mark the module as stopped + */ + module->pid = -1; + + return 0; + +error_waitpid: +error_kill: + return -1; +} + +int hexagram_module_close(hexagram_module *module) { + if (hexagram_module_stop(module) < 0) { + goto error_stop_module; + } + + if (dlclose(module->handle) < 0) { + goto error_dlclose; + } + + memset(module, '\0', sizeof(*module)); + + return 0; + +error_dlclose: + memset(module, '\0', sizeof(*module)); + +error_stop_module: + return -1; +}