chore: complete modules cleanup

main
Sean McBride 5 years ago
parent 1f988bd113
commit 267a04350f

@ -7,23 +7,13 @@
struct module { struct module {
char name[MOD_NAME_MAX]; char name[MOD_NAME_MAX];
char path[MOD_PATH_MAX]; char path[MOD_PATH_MAX];
void *dynamic_library_handle; // Handle to the *.so of the serverless function void *dynamic_library_handle; // Handle to the *.so of the serverless function
mod_main_fn_t entry_fn;
mod_glb_fn_t glb_init_fn;
mod_mem_fn_t mem_init_fn;
mod_tbl_fn_t tbl_init_fn;
mod_libc_fn_t libc_init_fn;
struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE];
i32 argument_count; i32 argument_count;
u32 stack_size; // a specification? u32 stack_size; // a specification?
u64 max_memory; // perhaps a specification of the module. (max 4GB) u64 max_memory; // perhaps a specification of the module. (max 4GB)
u32 timeout; // again part of the module specification. u32 timeout; // again part of the module specification.
u32 reference_count; // ref count how many instances exist here. u32 reference_count; // ref count how many instances exist here.
struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE];
struct sockaddr_in socket_address; struct sockaddr_in socket_address;
int socket_descriptor; int socket_descriptor;
int port; int port;
@ -35,27 +25,46 @@ struct module {
// so, using direct epoll for accepting connections. // so, using direct epoll for accepting connections.
// uv_handle_t srvuv; // uv_handle_t srvuv;
// req/resp from http, (resp size including headers!)..
unsigned long max_request_size; unsigned long max_request_size;
unsigned long max_response_size;
// Equals the largest of either max_request_size or max_response_size
unsigned long max_request_or_response_size;
int request_header_count;
int response_header_count;
char request_headers[HTTP_HEADERS_MAX][HTTP_HEADER_MAXSZ]; char request_headers[HTTP_HEADERS_MAX][HTTP_HEADER_MAXSZ];
int request_header_count;
char request_content_type[HTTP_HEADERVAL_MAXSZ]; char request_content_type[HTTP_HEADERVAL_MAXSZ];
// resp size including headers!
unsigned long max_response_size;
int response_header_count;
char response_content_type[HTTP_HEADERVAL_MAXSZ]; char response_content_type[HTTP_HEADERVAL_MAXSZ];
char response_headers[HTTP_HEADERS_MAX][HTTP_HEADER_MAXSZ]; char response_headers[HTTP_HEADERS_MAX][HTTP_HEADER_MAXSZ];
// Equals the largest of either max_request_size or max_response_size
unsigned long max_request_or_response_size;
// Functions to initialize aspects of sandbox
mod_glb_fn_t initialize_globals;
mod_mem_fn_t initialize_memory;
mod_tbl_fn_t initialize_tables;
mod_libc_fn_t initialize_libc;
// Entry Function to invoke serverless function
mod_main_fn_t main;
}; };
struct module *module_alloc(char *mod_name, char *mod_path, i32 argument_count, u32 stack_sz, u32 max_heap, u32 timeout,
int port, int req_sz, int resp_sz);
// frees only if reference_count == 0
void module_free(struct module *module);
struct module *module_find_by_name(char *name); struct module *module_find_by_name(char *name);
struct module *module_find_by_socket_descriptor(int sock); struct module *module_find_by_socket_descriptor(int sock);
struct module *module_alloc(char *mod_name, char *mod_path, i32 argument_count, u32 stack_sz, u32 max_heap, u32 timeout, int port, int req_sz, int resp_sz);
void module_free(struct module *module);
/**
* Sets the HTTP Request and Response Headers and Content type on a module
* @param module
* @param request_count
* @param request_headers
* @param request_content_type
* @param response_count
* @param response_headers
* @param response_content_type
**/
static inline void static inline void
module_http_info(struct module *module, int request_count, char *request_headers, char request_content_type[], module_http_info(struct module *module, int request_count, char *request_headers, char request_content_type[],
int response_count, char *response_headers, char response_content_type[]) int response_count, char *response_headers, char response_content_type[])
@ -77,53 +86,55 @@ module_http_info(struct module *module, int request_count, char *request_headers
static inline int static inline int
module_is_valid(struct module *module) module_is_valid(struct module *module)
{ {
if (module && module->dynamic_library_handle && module->entry_fn) return 1; if (module && module->dynamic_library_handle && module->main) return 1;
return 0; return 0;
} }
// TODO: What is the point of these wrapper functions. They seem exceptionally silly
// We have the module, so why not just invoke the function pointer directly?
/** /**
* Invoke a module's glb_init_fn * Invoke a module's initialize_globals
* @param module * @param module
**/ **/
static inline void static inline void
module_globals_init(struct module *module) module_globals_init(struct module *module)
{ {
// called in a sandbox. // called in a sandbox.
module->glb_init_fn(); module->initialize_globals();
} }
/** /**
* Invoke a module's tbl_init_fn * Invoke a module's initialize_tables
* @param module * @param module
**/ **/
static inline void static inline void
module_table_init(struct module *module) module_table_init(struct module *module)
{ {
// called at module creation time (once only per module). // called at module creation time (once only per module).
module->tbl_init_fn(); module->initialize_tables();
} }
/** /**
* Invoke a module's libc_init_fn * Invoke a module's initialize_libc
* @param module * @param module
**/ **/
static inline void static inline void
module_libc_init(struct module *module, i32 env, i32 args) module_libc_init(struct module *module, i32 env, i32 args)
{ {
// called in a sandbox. // called in a sandbox.
module->libc_init_fn(env, args); module->initialize_libc(env, args);
} }
/** /**
* Invoke a module's mem_init_fn * Invoke a module's initialize_memory
* @param module * @param module
**/ **/
static inline void static inline void
module_memory_init(struct module *module) module_memory_init(struct module *module)
{ {
// called in a sandbox. // called in a sandbox.
module->mem_init_fn(); module->initialize_memory();
} }
/** /**
@ -135,7 +146,7 @@ module_memory_init(struct module *module)
static inline i32 static inline i32
module_entry(struct module *module, i32 argc, i32 argv) module_entry(struct module *module, i32 argc, i32 argv)
{ {
return module->entry_fn(argc, argv); return module->main(argc, argv);
} }
/** /**

@ -8,6 +8,7 @@
// In-memory representation of all active modules // In-memory representation of all active modules
static struct module *__mod_db[MOD_MAX] = { NULL }; static struct module *__mod_db[MOD_MAX] = { NULL };
// TODO: What is this?
static int __mod_free_off = 0; static int __mod_free_off = 0;
/** /**
@ -52,6 +53,7 @@ module_add(struct module *module)
{ {
assert(module->socket_descriptor == -1); assert(module->socket_descriptor == -1);
// TODO: Where does this function code from?
int f = __sync_fetch_and_add(&__mod_free_off, 1); int f = __sync_fetch_and_add(&__mod_free_off, 1);
assert(f < MOD_MAX); assert(f < MOD_MAX);
__mod_db[f] = module; __mod_db[f] = module;
@ -90,9 +92,9 @@ module_server_init(struct module *module)
// Listen to the interface? Check that it is live? // Listen to the interface? Check that it is live?
if (listen(socket_descriptor, MOD_BACKLOG) < 0) assert(0); if (listen(socket_descriptor, MOD_BACKLOG) < 0) assert(0);
module->socket_descriptor = socket_descriptor;
// Register the socket descriptor with our global epoll instance to monitor for incoming HTTP requests // Set the socket descriptor and register with our global epoll instance to monitor for incoming HTTP requests
module->socket_descriptor = socket_descriptor;
struct epoll_event accept_evt; struct epoll_event accept_evt;
accept_evt.data.ptr = (void *)module; accept_evt.data.ptr = (void *)module;
accept_evt.events = EPOLLIN; accept_evt.events = EPOLLIN;
@ -101,7 +103,7 @@ module_server_init(struct module *module)
/** /**
* Module Mega Setup Function * Module Mega Setup Function
* Creates a new module, invokes tbl_init_fn to initialize the indirect table, adds it to the module DB, and starts * Creates a new module, invokes initialize_tables to initialize the indirect table, adds it to the module DB, and starts
*listening for HTTP Requests *listening for HTTP Requests
* *
* @param name * @param name
@ -128,20 +130,20 @@ module_alloc(char *name, char *path, i32 argument_count, u32 stack_size, u32 max
if (module->dynamic_library_handle == NULL) goto dl_open_error; if (module->dynamic_library_handle == NULL) goto dl_open_error;
// Resolve the symbols in the dynamic library *.so file // Resolve the symbols in the dynamic library *.so file
module->entry_fn = (mod_main_fn_t)dlsym(module->dynamic_library_handle, MOD_MAIN_FN); module->main = (mod_main_fn_t)dlsym(module->dynamic_library_handle, MOD_MAIN_FN);
if (module->entry_fn == NULL) goto dl_error; if (module->main == NULL) goto dl_error;
module->glb_init_fn = (mod_glb_fn_t)dlsym(module->dynamic_library_handle, MOD_GLB_FN); module->initialize_globals = (mod_glb_fn_t)dlsym(module->dynamic_library_handle, MOD_GLB_FN);
if (module->glb_init_fn == NULL) goto dl_error; if (module->initialize_globals == NULL) goto dl_error;
module->mem_init_fn = (mod_mem_fn_t)dlsym(module->dynamic_library_handle, MOD_MEM_FN); module->initialize_memory = (mod_mem_fn_t)dlsym(module->dynamic_library_handle, MOD_MEM_FN);
if (module->mem_init_fn == NULL) goto dl_error; if (module->initialize_memory == NULL) goto dl_error;
module->tbl_init_fn = (mod_tbl_fn_t)dlsym(module->dynamic_library_handle, MOD_TBL_FN); module->initialize_tables = (mod_tbl_fn_t)dlsym(module->dynamic_library_handle, MOD_TBL_FN);
if (module->tbl_init_fn == NULL) goto dl_error; if (module->initialize_tables == NULL) goto dl_error;
module->libc_init_fn = (mod_libc_fn_t)dlsym(module->dynamic_library_handle, MOD_LIBC_FN); module->initialize_libc = (mod_libc_fn_t)dlsym(module->dynamic_library_handle, MOD_LIBC_FN);
if (module->libc_init_fn == NULL) goto dl_error; if (module->initialize_libc == NULL) goto dl_error;
// Set fields in the module struct // Set fields in the module struct
strncpy(module->name, name, MOD_NAME_MAX); strncpy(module->name, name, MOD_NAME_MAX);
@ -195,6 +197,7 @@ dl_open_error:
/** /**
* Module Mega Teardown Function * Module Mega Teardown Function
* Closes the socket and dynamic library, and then frees the module * Closes the socket and dynamic library, and then frees the module
* Returns harmlessly if there are outstanding references
* @param module - the module to teardown * @param module - the module to teardown
**/ **/
void void

Loading…
Cancel
Save