Implement support for loading modules

This commit is contained in:
XANTRONIX Development 2019-05-19 16:01:19 -05:00
parent 318704622c
commit b0c23e7413
3 changed files with 132 additions and 2 deletions

18
include/hexagram/module.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef _HEXAGRAM_MODULE_H
#define _HEXAGRAM_MODULE_H
#include <hexagram/can.h>
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 */

View file

@ -7,10 +7,10 @@ CC = $(CROSS)cc
CFLAGS += -fPIC -I$(INCLUDE_PATH) CFLAGS += -fPIC -I$(INCLUDE_PATH)
LDFLAGS = 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 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_MAJOR = 0
VERSION_MINOR = 0.1 VERSION_MINOR = 0.1

112
src/module.c Normal file
View file

@ -0,0 +1,112 @@
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <hexagram/module.h>
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;
}