int main(int argc, char *argv[]) { mod_t *m; void (*init) (int, char **); int (*deinit) (void); m = module_open(argv[1]); if (!m) { fprintf(stderr, "unable to open module %s: %s\n", argv[1], module_error()); return 1; } if (!module_symbol(m, "init", (void **)&init)) { fprintf(stderr, "unable to find init function: %s\n", module_error()); return 1; } init(argc - 2, &argv[2]); if (!module_symbol(m, "deinit", (void **)&deinit)) { fprintf(stderr, "unable to find deinit function: %s\n", module_error()); return 1; } return deinit(); }
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); } }
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); }
static void cmd_modload(User* u, int ac, char** params) { int status; int i = 0; if ((ac < 1) || (ac > 2)) { sendto_one(s_Guardian,u,"Invalid syntax"); sendto_one(s_Guardian,u," \002SYNTAX: MODLOAD [MODULE]\002"); return; } sendto_logchan("\002Module:\002 Loading module %s",params[0]); status = module_open(params[0],MOD_TYPE_UNKNOWN); sendto_one(s_Guardian,u,"Loading complete - %s",GetModErr(status)); if (status == MOD_CONT) ircd_wallop(s_Guardian->nick,"%s has loaded module %s",u->nick,params[0]); sendto_logchan("%s: %s",params[0],GetModErr(status)); return; }
/* Returns 1 if ok, 0 if error in module and -1 if module wasn't found */ static int module_load_name(const char *path, const char *rootmodule, const char *submodule, int silent) { void (*module_init) (void); void (*module_deinit) (void); GModule *gmodule; MODULE_REC *module; MODULE_FILE_REC *rec; gpointer value1, value2; char *initfunc, *deinitfunc; int found; gmodule = module_open(path, &found); if (gmodule == NULL) { if (!silent || found) { module_error(MODULE_ERROR_LOAD, g_module_error(), rootmodule, submodule); } return found ? 0 : -1; } /* get the module's init() and deinit() functions */ initfunc = module_get_func(rootmodule, submodule, "init"); deinitfunc = module_get_func(rootmodule, submodule, "deinit"); found = g_module_symbol(gmodule, initfunc, &value1) && g_module_symbol(gmodule, deinitfunc, &value2); g_free(initfunc); g_free(deinitfunc); module_init = value1; module_deinit = value2; if (!found) { module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); g_module_close(gmodule); return 0; } /* Call the module's init() function - it should register itself with module_register() function, abort if it doesn't. */ module_init(); module = module_find(rootmodule); rec = module == NULL ? NULL : strcmp(rootmodule, submodule) == 0 ? module_file_find(module, "core") : module_file_find(module, submodule); if (rec == NULL) { rec = module_register_full(rootmodule, submodule, NULL); rec->gmodule = gmodule; module_file_unload(rec); module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); return 0; } rec->module_deinit = module_deinit; rec->gmodule = gmodule; rec->initialized = TRUE; settings_check_module(rec->defined_module_name); signal_emit("module loaded", 2, rec->root, rec); return 1; }
/* Returns 1 if ok, 0 if error in module and -1 if module wasn't found */ static int module_load_name(const char *path, const char *rootmodule, const char *submodule, int silent) { void (*module_init) (void); void (*module_deinit) (void); void (*module_version) (int *); GModule *gmodule; MODULE_REC *module; MODULE_FILE_REC *rec; gpointer value_version = NULL; gpointer value1, value2 = NULL; char *versionfunc, *initfunc, *deinitfunc; int module_abi_version = 0; int found; gmodule = module_open(path, &found); if (gmodule == NULL) { if (!silent || found) { module_error(MODULE_ERROR_LOAD, g_module_error(), rootmodule, submodule); } return found ? 0 : -1; } /* get the module's irssi abi version and bail out on mismatch */ versionfunc = module_get_func(rootmodule, submodule, "abicheck"); if (!g_module_symbol(gmodule, versionfunc, &value_version)) { g_free(versionfunc); module_error(MODULE_ERROR_VERSION_MISMATCH, "0", rootmodule, submodule); g_module_close(gmodule); return 0; } g_free(versionfunc); module_version = value_version; module_version(&module_abi_version); if (module_abi_version != IRSSI_ABI_VERSION) { char *module_abi_versionstr = g_strdup_printf("%d", module_abi_version); module_error(MODULE_ERROR_VERSION_MISMATCH, module_abi_versionstr, rootmodule, submodule); g_free(module_abi_versionstr); g_module_close(gmodule); return 0; } /* get the module's init() and deinit() functions */ initfunc = module_get_func(rootmodule, submodule, "init"); deinitfunc = module_get_func(rootmodule, submodule, "deinit"); found = g_module_symbol(gmodule, initfunc, &value1) && g_module_symbol(gmodule, deinitfunc, &value2); g_free(initfunc); g_free(deinitfunc); if (!found) { module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); g_module_close(gmodule); return 0; } module_init = value1; module_deinit = value2; /* Call the module's init() function - it should register itself with module_register() function, abort if it doesn't. */ module_init(); module = module_find(rootmodule); rec = module == NULL ? NULL : g_strcmp0(rootmodule, submodule) == 0 ? module_file_find(module, "core") : module_file_find(module, submodule); if (rec == NULL) { rec = module_register_full(rootmodule, submodule, NULL); rec->gmodule = gmodule; module_file_unload(rec); module_error(MODULE_ERROR_INVALID, NULL, rootmodule, submodule); return 0; } rec->module_deinit = module_deinit; rec->gmodule = gmodule; rec->initialized = TRUE; settings_check_module(rec->defined_module_name); signal_emit("module loaded", 2, rec->root, rec); return 1; }
static void module_init(const char *kernel_path) { struct bi_modulelist_entry *bi; struct stat st; const char *machine; char kdev[64]; char *buf; boot_module_t *bm; size_t len; off_t off; int err, fd, nfail = 0; extract_device(kernel_path, kdev, sizeof(kdev)); switch (netbsd_elf_class) { case ELFCLASS32: machine = "i386"; break; case ELFCLASS64: machine = "amd64"; break; default: machine = "generic"; break; } if (netbsd_version / 1000000 % 100 == 99) { /* -current */ snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d.%d/modules", machine, netbsd_version / 100000000, netbsd_version / 1000000 % 100, netbsd_version / 100 % 100); } else if (netbsd_version != 0) { /* release */ snprintf(module_base, sizeof(module_base), "/stand/%s/%d.%d/modules", machine, netbsd_version / 100000000, netbsd_version / 1000000 % 100); } /* First, see which modules are valid and calculate btinfo size */ len = sizeof(struct btinfo_modulelist); for (bm = boot_modules; bm; bm = bm->bm_next) { fd = module_open(bm, 0, kdev, false); if (fd == -1) { bm->bm_len = -1; ++nfail; continue; } err = fstat(fd, &st); if (err == -1 || st.st_size == -1) { printf("WARNING: couldn't stat %s\n", bm->bm_path); close(fd); bm->bm_len = -1; ++nfail; continue; } bm->bm_len = st.st_size; close(fd); len += sizeof(struct bi_modulelist_entry); } /* Allocate the module list */ btinfo_modulelist = alloc(len); if (btinfo_modulelist == NULL) { printf("WARNING: couldn't allocate module list\n"); wait_sec(MODULE_WARNING_SEC); return; } memset(btinfo_modulelist, 0, len); btinfo_modulelist_size = len; /* Fill in btinfo structure */ buf = (char *)btinfo_modulelist; btinfo_modulelist->num = 0; off = sizeof(struct btinfo_modulelist); for (bm = boot_modules; bm; bm = bm->bm_next) { if (bm->bm_len == -1) continue; fd = module_open(bm, 0, kdev, true); if (fd == -1) continue; image_end = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); len = pread(fd, (void *)image_end, SSIZE_MAX); if (len < bm->bm_len) { if ((howto & AB_SILENT) != 0) printf("Loading %s ", bm->bm_path); printf(" FAILED\n"); } else { btinfo_modulelist->num++; bi = (struct bi_modulelist_entry *)(buf + off); off += sizeof(struct bi_modulelist_entry); strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1); bi->base = image_end; bi->len = len; switch (bm->bm_type) { case BM_TYPE_KMOD: bi->type = BI_MODULE_ELF; break; case BM_TYPE_IMAGE: bi->type = BI_MODULE_IMAGE; break; case BM_TYPE_RND: default: /* safest -- rnd checks the sha1 */ bi->type = BI_MODULE_RND; break; } if ((howto & AB_SILENT) == 0) printf(" \n"); } if (len > 0) image_end += len; close(fd); } btinfo_modulelist->endpa = image_end; if (nfail > 0) { printf("WARNING: %d module%s failed to load\n", nfail, nfail == 1 ? "" : "s"); #if notyet wait_sec(MODULE_WARNING_SEC); #endif } }
ipc_module_t* IPC::factory(const std::string& name){ ipc_module_t* module = (ipc_module_t*)module_open(name.c_str(), IPC_MODULE, 0); if ( !module ) Log::warning("Failed to load IPC module `%s'.\n", name.c_str()); return module; }
int loadVpiModule (const char* modulename) { static void *libghdlvpi_mod; int i; void *vpimod; fprintf (stderr, "loading VPI module '%s'\n", modulename); /* TODO: on windows, use SetDllDirectory with: - install dir (libdir) => add -DLIBDIR=xxx - exec path\lib => see windows_default_path */ vpimod = module_open (modulename); if (vpimod == NULL) { const char *msg = module_error (); fprintf (stderr, "%s\n", msg == NULL ? "unknown dlopen error" : msg); return -1; } /* Try to load the libghdlvpi library and set the thunk. No need to load the library several times. */ if (libghdlvpi_mod == NULL) { libghdlvpi_mod = module_open (libghdlvpi_name); if (libghdlvpi_mod != NULL) { vpi_thunk **vpi_thunk_ptr; for (i = 0; i < 2; i++) { vpi_thunk_ptr = module_symbol (libghdlvpi_mod, &"_VPI_THUNK"[i]); if (vpi_thunk_ptr != NULL) { *vpi_thunk_ptr = &__ghdl_vpi_thunk_v1; break; } } if (vpi_thunk_ptr == NULL) fprintf (stderr, "warning: VPI_THUNK not found in %s\n", libghdlvpi_name); } } for (i = 0; i < 2; i++) // try with and w/o leading underscores { void *vpitable = module_symbol (vpimod, &"_vlog_startup_routines"[i]); if (vpitable) { unsigned int j; typedef void (*vlog_startup_routines_t)(void); vlog_startup_routines_t *vpifuns; vpifuns = (vlog_startup_routines_t*)vpitable; for (j = 0; vpifuns[j]; j++) vpifuns[j](); fprintf (stderr, "VPI module loaded!\n"); return 0; // successfully registered VPI module } } fprintf (stderr, "vlog_startup_routines not found\n"); return -1; // failed to register VPI module }
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); } }