static int load_mods(config_t *config, config_setting_t *setting) { int err; unsigned int mod_count; int i; const char* mod_name; const char* mod_so; const char* mod_ident; config_setting_t *mod_setting; err = 0; fprintf(stderr, "load mods from config\n"); setting = config_lookup(config, "mods"); if (setting != NULL) { mod_count = config_setting_length(setting); for (i = 0; i < mod_count; ++i) { mod_setting = config_setting_get_elem(setting, i); if (mod_setting) { if (!config_setting_lookup_string(mod_setting, "name", &mod_name) || !config_setting_lookup_string(mod_setting, "so", &mod_so)) { continue; } if (!config_setting_lookup_string(mod_setting, "ident", &mod_ident)) { mod_ident = NULL; } fprintf(stderr, "load module %s - %s - [%s]\n", mod_name, mod_so, mod_ident); module_t *mod = module_open(mod_name, mod_ident, mod_so, RTLD_NOW); if (!mod) { err = 1; break; } if (module_map_insert(&g_config.module_root, mod) == NULL) { err = 1; module_close(mod); break; } if (module_call_init_func(mod, &g_config)) { fprintf(stderr, "ERROR %s returned not 0\n", mod->name); err = 1; module_close(mod); break; } } } } return err; }
/* Process an HTTP "GET" request for PAGE, and send the results to * the file descriptor CONNECTION_FD. */ static void handle_get(int connection_fd, const char* page) { struct server_module* module = NULL; /* Make sure the requested page begins with a slash and does not * contain any additional slashes -- we don't support any * subdirectories. */ if (*page == '/' && strchr(page+1, '/') == NULL) { char module_file_name[64]; /* The page name looks OK. Construct the module name by appending * ".so" to the page name. */ snprintf(module_file_name, sizeof(module_file_name), "%s.so", page+1); /* Try to open the module. */ module = module_open(module_file_name); } if (module == NULL) { /* Either the requested page was malformed, or we couldn't open * a module with the indicated name. Either way, return the HTTP * response 404, Not Found. */ char response[1024]; /* Generate the response message. */ snprintf(response, sizeof(response), not_found_response_template, page); /* Send it to the client. */ write(connection_fd, response, strlen(response)); } else { /* The requested module was loaded successfully. */ /* Send the HTTP response indicating success, and the HTTP * header for an HTML page. */ write(connection_fd, ok_response, strlen(ok_response)); /* Invoke the module, which will generate HTML output and send * it to the client file descriptor. */ (*module->generate_function) (connection_fd); /* We're done with the module. */ module_close(module); } }
BOOL APIENTRY DllMain( HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: if (!module_init()) return FALSE; break; case DLL_PROCESS_DETACH: if (lpReserved == NULL) { if (!module_close()) return FALSE; } break; } return TRUE; }
static void internal_cleanup( #ifndef HAVE_GMODULE G_GNUC_UNUSED #endif /* !HAVE_GMODULE */ bool signaled) { #ifdef HAVE_GMODULE module_close(signaled ? 0 : 1); #endif /* HAVE_GMODULE */ conf_free(); if (cfd != NULL) { g_key_file_free(cfd); cfd = NULL; } if (loop != NULL) { g_main_loop_quit(loop); g_main_loop_unref(loop); loop = NULL; } }
void module_enumerate(enum module_type_t type, void (*callback)(const char* name, const module_handle mod)){ char* ctx = NULL; char* path_list = strdup(pluginpath()); char* path = strtok_r(path_list, ":", &ctx); while ( path ){ int n; struct dirent **namelist; if ( (n=scandir(path, &namelist, filter, NULL)) >= 0 ){ for ( int i = 0; i < n; i++ ){ char* name = namelist[i]->d_name; char* ext = strrchr(name, '.'); /* remove extension from name */ if ( ext ) *ext = 0; module_handle context = module_open(name, ANY_MODULE, MODULE_CALLER_INIT); if ( !context ) continue; if ( !(type == ANY_MODULE || module_type(context) == type) ){ continue; } callback(name, context); module_close(context); } for ( int i = 0; i < n; i++ ){ free(namelist[i]); } free(namelist); } else { log_message(Log_Warning, "Failed to read %s: %s\n", path, strerror(errno)); } path = strtok_r(NULL, ":", &ctx); } free(path_list); }
bool module_reload(module_t *module, module_manager_t *manager) { if (module->close) module->close(module->instance); dlclose(module->handle); /* Save old address for unloaded module */ list_push(manager->unloaded, module->handle); if (!(module->handle = dlopen(module->file, RTLD_LAZY))) goto module_reload_error; if (!module_load(module)) goto module_reload_error; /* Reload the PRNG as well */ mt_destroy(module->random); module->random = mt_create(); return true; module_reload_error: module_close(module, manager); return false; }
static void handle_get (int connection_fd, const char* page) { struct server_module* module = NULL; /* Make sure the requested page begins with a slash and does not contain any additional slashes -- we don't support any subdirectories. */ printf("page:%s\n", page); if (strcmp(page, "/") == 0) { /*打开索引文件*/ #if 0 int fd = open("./index.html", O_RDONLY); if (fd == -1) { char response[1024]; /* Generate the response message. */ snprintf (response, sizeof (response), not_found_response_template, page); /* Send it to the client. */ write (connection_fd, response, strlen (response)); } else { write (connection_fd, ok_response, strlen (ok_response)); struct stat stat_buf = {0}; fstat(fd, &stat_buf); char *pbuf = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); write(connection_fd, pbuf, stat_buf.st_size); } #endif my_send_file(connection_fd, "./index.html", ok_response); } else if (*page == '/' && strstr(page + 1, ".png") != NULL) { #if 0 char response[1024] = {0}; const char *fname = page + 1; int fd = open(fname, O_RDONLY); if (fd == -1) { /* Generate the response message. */ snprintf (response, sizeof (response), not_found_response_template, page); /* Send it to the client. */ write (connection_fd, response, strlen (response)); } else { struct stat stat_buf = {0}; fstat(fd, &stat_buf); int n = snprintf (response, sizeof(response), ok_pic_response, "png"); write(connection_fd, response, n); sendfile(connection_fd, fd, NULL, stat_buf.st_size); } #endif my_send_file(connection_fd, page + 1, ok_pic_response); } else if (strstr(page + 1, ".mp3")){ my_send_file(connection_fd, page + 1, ok_mp3_response); } else if (*page == '/' && strchr (page + 1, '.') == NULL) { //"没有后缀的表示加载模块" char module_file_name[64]; /* The page name looks OK. Construct the module name by appending ".so" to the page name. */ snprintf (module_file_name, sizeof (module_file_name), "%s.so", page + 1); printf("open module: %s\n", module_file_name); /* Try to open the module. */ module = module_open (module_file_name); if (module == NULL) { /* Either the requested page was malformed, or we couldn't open a module with the indicated name. Either way, return the HTTP response 404, Not Found. */ /*TODO file*/ char response[1024]; /* Generate the response message. */ snprintf (response, sizeof (response), not_found_response_template, page); /* Send it to the client. */ write (connection_fd, response, strlen (response)); } else { /* The requested module was loaded successfully. */ /* Send the HTTP response indicating success, and the HTTP header for an HTML page. */ write (connection_fd, ok_response, strlen (ok_response)); /* Invoke the module, which will generate HTML output and send it to the client file descriptor. */ (*module->generate_function) (connection_fd); /* We're done with the module. */ module_close (module); } } else { //ignore '/' of '/1.txt' == './1.txt' my_send_file(connection_fd, page + 1, ok_text_response); } }
char * module_load(const char *filename, const char *symbname, int (*shouldload)(void *symb, void *misc, char **err), void *misc, void **dll, void **symb) { dlltype intdll = NULL; void * intsym = NULL; char * interr = NULL; if (dll) *dll = NULL; if (symb) *symb = NULL; /* Open the module library */ #ifdef WIN32 /* NOTE: DONT_RESOLVE_DLL_REFERENCES is evil. Don't use this in your own * code. However, our design pattern avoids all the issues surrounding a * more general use of this evil flag. */ intdll = LoadLibraryEx(filename, NULL, DONT_RESOLVE_DLL_REFERENCES); #else /* WIN32 */ intdll = dlopen(filename, RTLD_LAZY | RTLD_LOCAL); #endif /* WIN32 */ if (!intdll) return dllerror(); /* Get the module symbol */ #ifdef WIN32 intsym = (void *) GetProcAddress(intdll, symbname); #else /* WIN32 */ intsym = dlsym(intdll, symbname); #endif /* WIN32 */ if (!intsym) { module_close(intdll); return dllerror(); } /* Figure out whether or not to load this module */ if (!shouldload(intsym, misc, &interr)) { module_close(intdll); return interr; } /* Re-open the module */ module_close(intdll); #ifdef WIN32 intdll = LoadLibrary(filename); #else /* WIN32 */ intdll = dlopen(filename, RTLD_NOW | RTLD_LOCAL); #endif /* WIN32 */ if (!intdll) { return dllerror(); } /* Get the symbol again */ #ifdef WIN32 intsym = (void *) GetProcAddress(intdll, symbname); #else /* WIN32 */ intsym = dlsym(intdll, symbname); #endif /* WIN32 */ if (!intsym) { module_close(intdll); return dllerror(); } if (dll) *dll = intdll; if (symb) *symb = intsym; return NULL; }