Flesh out hexagram_module more

This commit is contained in:
XANTRONIX Development 2019-05-21 09:27:17 -05:00
parent e9162b67d1
commit 13cc094cba
2 changed files with 35 additions and 7 deletions

View file

@ -1,6 +1,9 @@
#ifndef _HEXAGRAM_MODULE_H
#define _HEXAGRAM_MODULE_H
#include <sys/types.h>
#include <hexagram/dict.h>
#include <hexagram/can.h>
typedef struct _hexagram_module hexagram_module;
@ -11,7 +14,8 @@ 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_run(hexagram_module *module,
hexagram_dict *buses);
int hexagram_module_stop(hexagram_module *module);

View file

@ -1,8 +1,8 @@
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <dlfcn.h>
#include <sys/wait.h>
#include <hexagram/module.h>
@ -11,8 +11,8 @@ struct _hexagram_module {
const char *soname;
void *handle;
char *(*name)();
int (*run)(hexagram_can_if *can_if);
int pid;
int (*run)(hexagram_dict *buses);
pid_t pid;
};
hexagram_module *hexagram_module_open(const char *soname) {
@ -50,10 +50,19 @@ error_malloc:
}
const char *hexagram_module_name(hexagram_module *module) {
if (module->name == NULL) {
return NULL;
}
return module->name();
}
int hexagram_module_run(hexagram_module *module, hexagram_can_if *can_if) {
int hexagram_module_run(hexagram_module *module,
hexagram_dict *buses) {
if (module->run == NULL) {
return -1;
}
/*
* Module already stopped
*/
@ -61,7 +70,18 @@ int hexagram_module_run(hexagram_module *module, hexagram_can_if *can_if) {
return -1;
}
return module->run(can_if);
if ((module->pid = fork()) < 0) {
goto error_fork;
}
if (module->pid == 0) {
exit(module->run(buses));
}
return 0;
error_fork:
return -1;
}
int hexagram_module_stop(hexagram_module *module) {
@ -102,11 +122,15 @@ int hexagram_module_close(hexagram_module *module) {
memset(module, '\0', sizeof(*module));
free(module);
return 0;
error_dlclose:
memset(module, '\0', sizeof(*module));
free(module);
error_stop_module:
return -1;
}