Net() : conv1(torch::nn::Conv2dOptions(1, 10, /*kernel_size=*/5)), conv2(torch::nn::Conv2dOptions(10, 20, /*kernel_size=*/5)), fc1(320, 50), fc2(50, 10) { register_module("conv1", conv1); register_module("conv2", conv2); register_module("conv2_drop", conv2_drop); register_module("fc1", fc1); register_module("fc2", fc2); }
/* returns 0 on success , <0 on error */ int sr_load_module(char* path) { void* handle; unsigned int moddlflags; char* error; struct module_exports* exp; struct sr_module* t; /* load module */ handle=dlopen(path, OPENSIPS_DLFLAGS); /* resolve all symbols now */ if (handle==0){ LM_ERR("could not open module <%s>: %s\n", path, dlerror() ); goto error; } /* check for duplicates */ for(t=modules;t; t=t->next){ if (t->handle==handle){ LM_WARN("attempting to load the same module twice (%s)\n", path); goto skip; } } /* import module interface */ exp = (struct module_exports*)dlsym(handle, DLSYM_PREFIX "exports"); if ( (error =(char*)dlerror())!=0 ){ LM_ERR("load_module: %s\n", error); goto error1; } if(exp->dlflags!=DEFAULT_DLFLAGS && exp->dlflags!=OPENSIPS_DLFLAGS) { moddlflags = exp->dlflags; dlclose(handle); LM_DBG("reloading module %s with flags %d\n", path, moddlflags); handle = dlopen(path, moddlflags); if (handle==0){ LM_ERR("could not open module <%s>: %s\n", path, dlerror() ); goto error; } exp = (struct module_exports*)dlsym(handle, DLSYM_PREFIX "exports"); if ( (error =(char*)dlerror())!=0 ){ LM_ERR("failed to load module : %s\n", error); goto error1; } } /* version control */ if (!version_control(exp, path)) { exit(0); } /* launch register */ if (register_module(exp, path, handle)<0) goto error1; return 0; error1: dlclose(handle); error: skip: return -1; }
static gboolean gegl_module_load (GTypeModule *module) { GeglModule *gegl_module = GEGL_MODULE (module); gpointer func; g_return_val_if_fail (gegl_module->filename != NULL, FALSE); g_return_val_if_fail (gegl_module->module == NULL, FALSE); if (gegl_module->verbose) g_print ("Loading module '%s'\n", gegl_filename_to_utf8 (gegl_module->filename)); if (! gegl_module_open (gegl_module)) return FALSE; if (! gegl_module_query_module (gegl_module)) return FALSE; /* find the gegl_module_register symbol */ if (! g_module_symbol (gegl_module->module, "gegl_module_register", &func)) { gegl_module_set_last_error (gegl_module, "Missing gegl_module_register() symbol"); g_message (_("Module '%s' load error: %s"), gegl_filename_to_utf8 (gegl_module->filename), gegl_module->last_module_error); gegl_module_close (gegl_module); gegl_module->state = GEGL_MODULE_STATE_ERROR; return FALSE; } gegl_module->register_module = func; if (! gegl_module->register_module (module)) { gegl_module_set_last_error (gegl_module, "gegl_module_register() returned FALSE"); g_message (_("Module '%s' load error: %s"), gegl_filename_to_utf8 (gegl_module->filename), gegl_module->last_module_error); gegl_module_close (gegl_module); gegl_module->state = GEGL_MODULE_STATE_LOAD_FAILED; return FALSE; } gegl_module->state = GEGL_MODULE_STATE_LOADED; return TRUE; }
void init() { register_module(); send("./bin/test.so", (void *)1); void *ret = recv(); if(ret) fprintf(stderr, "got recv\n"); return; }
int main(){ car = register_module(&bicycle); car->start(); car->stop(); /* car = register_module(&bicycle); car->start(); car->stop();*/ return 0; }
static PyObject * api_register_module (PyObject *self, PyObject *args) { int status; char *name = NULL; if (!PyArg_ParseTuple (args, "|s", &name)) return NULL; status = register_module (name); return _ro (PyInt_FromLong (status)); }
void sys_pipe_init(void) { mod_pipe.family = FAMILY_DEV; strcpy(mod_pipe.name,"pipe"); mod_pipe.ops.poll = pipe_poll; mod_pipe.ops.close = pipe_close; mod_pipe.ops.read = pipe_read; mod_pipe.ops.write = pipe_write; register_module(&mod_pipe); }
static void load_static_modules(void) { const DAQ_Module_t *dm; int i; DEBUG("Static modules: %d\n", num_static_modules); for (i = 0; i < num_static_modules; i++) { dm = static_modules[i]; if (register_module(dm, NULL) != DAQ_SUCCESS) fprintf(stderr, "%s (%d): Failed to register static DAQ module.\n", dm->name, i); } }
int LoadModule(char *directive,char **argv,void *setdata){ if(argv==NULL || argv[0]==NULL || argv[1]==NULL){ debug_printf(1,"Missing arguments in LoadModule directive\n"); return 0; } debug_printf(1,"Loading service :%s path %s\n",argv[0],argv[1]); if (!register_module(argv[1],argv[0])){ debug_printf(1,"Error loading service\n"); return 0; } return 1; }
PyObject *init_guava(void) { PyObject *guava_module = NULL; PyObject *request_module = NULL; PyObject *server_module = NULL; PyObject *handler_module = NULL; PyObject *controller_module = NULL; PyObject *router_module = NULL; PyObject *session_module = NULL; PyObject *cookie_module = NULL; PyEval_InitThreads(); guava_module = Py_InitModule("guava", NULL); request_module = init_request(); if (!register_module(guava_module, "request", request_module)) { return NULL; } server_module = init_server(); if (!register_module(guava_module, "server", server_module)) { return NULL; } handler_module = init_handler(); if (!register_module(guava_module, "handler", handler_module)) { return NULL; } router_module = init_router(); if (!register_module(guava_module, "router", router_module)) { return NULL; } controller_module = init_controller(); if (!register_module(guava_module, "controller", controller_module)) { return NULL; } session_module = init_session(); if (!register_module(guava_module, "session", session_module)) { return NULL; } cookie_module = init_cookie(); if (!register_module(guava_module, "cookie", cookie_module)) { return NULL; } PyModule_AddStringConstant(guava_module, "version", GUAVA_VERSION); return guava_module; }
void memfs_init(void) { mod_memfs.family = FAMILY_FILE; strcpy(mod_memfs.name,"memfs"); mod_memfs.mount = memfs_mount; mod_memfs.ops.read = memfs_read; mod_memfs.ops.poll = memfs_poll; mod_memfs.ops.write = memfs_write; mod_memfs.ops.seek = memfs_seek; mod_memfs.ops.creat = memfs_creat; mod_memfs.ops.unlink = memfs_unlink; mod_memfs.ops.close = memfs_close; register_module(&mod_memfs); }
krb5_error_code k5_plugin_register(krb5_context context, int interface_id, const char *modname, krb5_plugin_initvt_fn module) { struct plugin_interface *interface = get_interface(context, interface_id); if (interface == NULL) return EINVAL; /* Disallow registering plugins after load. We may need to reconsider * this, but it simplifies the design. */ if (interface->configured) return EINVAL; return register_module(context, interface, modname, NULL, module); }
hEvent_t I2C_Init(hardware_bit_t* i2c_interrupt, REGISTER i2c_con, REGISTER i2c_stat, REGISTER i2c_trn, REGISTER i2c_rcv, I2C_callbackFunc resetCallback) { I2C_INTERRUPT = i2c_interrupt; I2C_CON = i2c_con; I2C_STAT = i2c_stat; I2C_TRN = i2c_trn; I2C_RCV = i2c_rcv; res_Callback = resetCallback; /// Register module hModule_t i2c_module = register_module(&_MODULE_I2C); /// Register event I2C_service_handle = register_event_p(i2c_module, &serviceI2C, EVENT_PRIORITY_LOW); I2C_load(); return I2C_service_handle; }
void sysfs_init(void) { mod_sysfs.family = FAMILY_FILE; strcpy(mod_sysfs.name, "sysfs"); mod_sysfs.mount = sysfs_mount; mod_sysfs.ops.read = sysfs_read; mod_sysfs.ops.poll = sysfs_poll; mod_sysfs.ops.write = sysfs_write; mod_sysfs.ops.close = sysfs_close; sysfs = fno_search("/sys"); register_module(&mod_sysfs); fno_mkdir(&mod_sysfs, "net", sysfs); sysfs_mutex = mutex_init(); }
GF_EXPORT GF_Err gf_module_load_static(GF_ModuleManager *pm, GF_InterfaceRegister *(*register_module)()) { GF_InterfaceRegister *pr = register_module(); GF_Err rc; if (!pr) { GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("Failed to statically load module\n")); return GF_NOT_SUPPORTED; } rc = gf_list_add(pm->plugin_registry, pr); if (rc != GF_OK) { GF_LOG(GF_LOG_ERROR, GF_LOG_CORE, ("Failed to statically load module\n")); return rc; } return GF_OK; }
krb5_error_code k5_plugin_register_dyn(krb5_context context, int interface_id, const char *modname, const char *modsubdir) { krb5_error_code ret; struct plugin_interface *interface = get_interface(context, interface_id); char *path; /* Disallow registering plugins after load. */ if (interface == NULL || interface->configured) return EINVAL; if (asprintf(&path, "%s/%s%s", modsubdir, modname, PLUGIN_EXT) < 0) return ENOMEM; ret = register_module(context, interface, modname, path, NULL); free(path); return ret; }
static int load_static_module(char *path) { int len = strlen(path); char *end = path + len; struct sr_module* t; unsigned int i; /* eliminate the .so, if found */ if (len > 3 && strncmp(end - 3, ".so", 3)==0) { end -= 3; len -= 3; } /* we check whether the protocol is found within the static_modules */ for (i = 0; i < (sizeof(static_modules)/sizeof(static_modules[0])); i++) { if (len >= static_modules[i].name.len && /* the path ends in the module's name */ memcmp(end - static_modules[i].name.len, static_modules[i].name.s, static_modules[i].name.len) == 0 && /* check if the previous char is '/' or nothing */ (len == static_modules[i].name.len || (*(end-len-1) == '/'))) { /* yey, found the module - check if it was loaded twice */ for(t=modules;t; t=t->next){ if (t->handle==static_modules[i].exp){ LM_WARN("attempting to load the same module twice (%s)\n", path); return 0; } } /* version control */ if (!version_control(static_modules[i].exp, path)) { exit(0); } /* launch register */ if (register_module(static_modules[i].exp, path, static_modules[i].exp)<0) return -1; return 0; } } return -1; }
/* initializes statically built (compiled in) modules*/ int register_builtin_modules(void) { int ret; ret=0; #ifdef STATIC_TM ret=register_module(&tm_exports,"built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_EXEC ret=register_module(&exec_exports,"built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_MAXFWD ret=register_module(&maxfwd_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_AUTH ret=register_module(&auth_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_RR ret=register_module(&rr_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_USRLOC ret=register_module(&usrloc_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_SL ret=register_module(&sl_exports, "built-in", 0); if (ret<0) return ret; #endif return ret; }
/* initializes statically built (compiled in) modules*/ int register_builtin_modules() { int ret; ret=0; #ifdef STATIC_TM ret=register_module(MODULE_INTERFACE_VER, &tm_exports,"built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_EXEC ret=register_module(MODULE_INTERFACE_VER, &exec_exports,"built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_MAXFWD ret=register_module(MODULE_INTERFACE_VER, &maxfwd_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_AUTH ret=register_module(MODULE_INTERFACE_VER, &auth_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_RR ret=register_module(MODULE_INTERFACE_VER, &rr_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_USRLOC ret=register_module(MODULE_INTERFACE_VER, &usrloc_exports, "built-in", 0); if (ret<0) return ret; #endif #ifdef STATIC_SL ret=register_module(MODULE_INTERFACE_VER, &sl_exports, "built-in", 0); if (ret<0) return ret; #endif return ret; }
static int daq_load_module(const char *filename) { const DAQ_Module_t *dm; struct stat fs; void *dl_handle; int rval; if (filename == NULL) return DAQ_ERROR_INVAL; if ((stat(filename, &fs)) != 0 || !(fs.st_mode & S_IFREG)) { fprintf(stderr, "%s: File does not exist.\n", filename); return DAQ_ERROR; } if ((dl_handle = dlopen(filename, RTLD_NOW)) == NULL) { fprintf(stderr, "%s: %s: %s\n", filename, dlopen_func_name, dlerror()); return DAQ_ERROR; } if ((dm = (const DAQ_Module_t*)dlsym(dl_handle, "DAQ_MODULE_DATA")) == NULL) { fprintf(stderr, "%s: %s: %s\n", filename, dlsym_func_name, dlerror()); dlclose(dl_handle); return DAQ_ERROR; } if ((rval = register_module(dm, dl_handle)) != DAQ_SUCCESS) { if (rval != DAQ_ERROR_EXISTS) fprintf(stderr, "%s: Failed to register DAQ module.\n", filename); dlclose(dl_handle); return DAQ_ERROR; } return DAQ_SUCCESS; }
static void load_module(const char *filename) { void *dl = dlopen(filename, RTLD_LAZY); if (dl == NULL) { printf("Cannot load module: %s\n", filename); return; } struct SupportModule *module = calloc(1, sizeof(struct SupportModule)); module->global = &global; module->filename = strdup(filename); apkenv_module_init_t module_init = (apkenv_module_init_t)dlsym(dl, APKENV_MODULE_INIT); if (module_init != NULL) { int version = module_init(APKENV_MODULE_VERSION, module); switch (version) { case 0: printf("Cannot init module: %s\n", filename); break; case APKENV_MODULE_VERSION: printf("Loaded module: %s (%d)\n", filename, module->priority); register_module(module); return; default: printf("Wrong module ABI version: %x (need %x)\n", version, APKENV_MODULE_VERSION); break; } } else { printf("Cannot find %s(): %s\n", APKENV_MODULE_INIT, filename); } free(module->filename); free(module); dlclose(dl); }
void luaopen_graphics(lua_State *L) { register_module(L, "graphics", lua_graphics_methods); }
static void __s2e_init_env(int *argcPtr, char ***argvPtr) { int argc = *argcPtr; char** argv = *argvPtr; int new_argc = 0, n_args; char* new_argv[1024]; unsigned max_len, min_argvs, max_argvs; char** final_argv; char sym_arg_name[5] = "arg"; unsigned sym_arg_num = 0; int k=0, i; int concolic_mode = 0; sym_arg_name[4] = '\0'; // Load the process map and get the info about the current process procmap_entry_t* proc_map = load_process_map(); display_process_map(proc_map); register_module(proc_map, argv[0]); register_module(proc_map, "init_env.so"); #ifndef DEBUG_NATIVE s2e_codeselector_select_module("init_env.so"); #endif // Recognize --help when it is the sole argument. if (argc == 2 && __streq(argv[1], "--help")) { __emit_error("s2e_init_env\n\n" "usage: (s2e_init_env) [options] [program arguments]\n" "\n" " -select-process - Enable forking in the current process only\n" " -select-process-userspace - Enable forking in userspace-code of the\n" " current process only\n" " -select-process-code - Enable forking in the code section of the current binary only\n" " -concolic - Augment existing concrete arguments with symbolic values\n" " -sym-arg <N> - Replace by a symbolic argument of length N\n" " -sym-args <MIN> <MAX> <N> - Replace by at least MIN arguments and at most\n" " MAX arguments, each with maximum length N\n\n"); } #ifndef DEBUG_NATIVE s2e_enable_forking(); #endif while (k < argc) { if (__streq(argv[k], "--concolic") || __streq(argv[k], "-concolic")) { concolic_mode = 1; ++k; } else if (__streq(argv[k], "--sym-arg") || __streq(argv[k], "-sym-arg")) { const char *msg = "--sym-arg expects an integer argument <max-len>"; if (++k == argc) __emit_error(msg); max_len = __str_to_int(argv[k++], msg); sym_arg_name[3] = '0' + sym_arg_num++; __add_arg(&new_argc, new_argv, __get_sym_str(max_len, sym_arg_name), 1024); } else if (__streq(argv[k], "--sym-args") || __streq(argv[k], "-sym-args")) { const char *msg = "--sym-args expects three integer arguments <min-argvs> <max-argvs> <max-len>"; if (k+3 >= argc) __emit_error(msg); k++; min_argvs = __str_to_int(argv[k++], msg); max_argvs = __str_to_int(argv[k++], msg); max_len = __str_to_int(argv[k++], msg); n_args = s2e_range(min_argvs, max_argvs+1, "n_args"); for (i=0; i < n_args; i++) { sym_arg_name[3] = '0' + sym_arg_num++; __add_arg(&new_argc, new_argv, __get_sym_str(max_len, sym_arg_name), 1024); } } else if (__streq(argv[k], "--select-process") || __streq(argv[k], "-select-process")) { k++; myprintf("Forks will be restricted to the current address space\n"); s2e_codeselector_enable_address_space(0); } else if (__streq(argv[k], "--select-process-userspace") || __streq(argv[k], "-select-process-userspace")) { k++; myprintf("Forks will be restricted to the user-mode portion of the current address space\n"); s2e_codeselector_enable_address_space(1); } else if (__streq(argv[k], "--select-process-code") || __streq(argv[k], "-select-process-code")) { k++; const char *process_base_name = __base_name(argv[0]); myprintf("Forks will be restricted to %s\n", process_base_name); s2e_codeselector_select_module(process_base_name); } else { /* simply copy arguments */ if (concolic_mode) { sym_arg_name[3] = '0' + k; s2e_make_concolic(argv[k], strlen(argv[k]), sym_arg_name); } __add_arg(&new_argc, new_argv, argv[k++], 1024); } } final_argv = (char**) malloc((new_argc+1) * sizeof(*final_argv)); memcpy(final_argv, new_argv, new_argc * sizeof(*final_argv)); final_argv[new_argc] = 0; *argcPtr = new_argc; *argvPtr = final_argv; }
/** * Load the dynamic library related to a gateway module. The routine * will look for library files in the current directory, * $MAXSCALE_HOME/modules and /usr/local/skysql/MaxScale/modules. * * @param module Name of the module to load * @param type Type of module, used purely for registration * @return The module specific entry point structure or NULL */ void * load_module(const char *module, const char *type) { char *home, *version; char fname[MAXPATHLEN]; void *dlhandle, *sym; char *(*ver)(); void *(*ep)(), *modobj; MODULES *mod; MODULE_INFO *mod_info = NULL; if ((mod = find_module(module)) == NULL) { /*< * The module is not already loaded * * Search of the shared object. */ sprintf(fname, "./lib%s.so", module); if (access(fname, F_OK) == -1) { home = get_maxscale_home (); sprintf(fname, "%s/modules/lib%s.so", home, module); if (access(fname, F_OK) == -1) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Unable to find library for " "module: %s.", module))); return NULL; } } if ((dlhandle = dlopen(fname, RTLD_NOW|RTLD_LOCAL)) == NULL) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Unable to load library for module: " "%s\n\n\t\t %s." "\n\n", module, dlerror()))); return NULL; } if ((sym = dlsym(dlhandle, "version")) == NULL) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Version interface not supported by " "module: %s\n\t\t\t %s.", module, dlerror()))); dlclose(dlhandle); return NULL; } ver = sym; version = ver(); /* * If the module has a ModuleInit function cal it now. */ if ((sym = dlsym(dlhandle, "ModuleInit")) != NULL) { void (*ModuleInit)() = sym; ModuleInit(); } if ((sym = dlsym(dlhandle, "info")) != NULL) { int fatal = 0; mod_info = sym; if (strcmp(type, MODULE_PROTOCOL) == 0 && mod_info->modapi != MODULE_API_PROTOCOL) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Module '%s' does not implement " "the protocol API.\n", module))); fatal = 1; } if (strcmp(type, MODULE_ROUTER) == 0 && mod_info->modapi != MODULE_API_ROUTER) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Module '%s' does not implement " "the router API.\n", module))); fatal = 1; } if (strcmp(type, MODULE_MONITOR) == 0 && mod_info->modapi != MODULE_API_MONITOR) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Module '%s' does not implement " "the monitor API.\n", module))); fatal = 1; } if (strcmp(type, MODULE_FILTER) == 0 && mod_info->modapi != MODULE_API_FILTER) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Module '%s' does not implement " "the filter API.\n", module))); fatal = 1; } if (fatal) { dlclose(dlhandle); return NULL; } } if ((sym = dlsym(dlhandle, "GetModuleObject")) == NULL) { LOGIF(LE, (skygw_log_write_flush( LOGFILE_ERROR, "Error : Expected entry point interface missing " "from module: %s\n\t\t\t %s.", module, dlerror()))); dlclose(dlhandle); return NULL; } ep = sym; modobj = ep(); LOGIF(LM, (skygw_log_write_flush( LOGFILE_MESSAGE, "Loaded module %s: %s from %s", module, version, fname))); register_module(module, type, dlhandle, version, modobj, mod_info); } else { /* * The module is already loaded, get the entry points again and * return a reference to the already loaded module. */ modobj = mod->modobj; } return modobj; }
void luaopen_directory(lua_State *L) { register_module(L, "directory", lua_directory_methods); }
void register_module_themehax() { register_module(MODULE_MAKE_CVER(10, 2, 0), themehax_install, themehax_delete); }
void luaopen_matrix4x4(lua_State *L) { register_metatable(L, "glomp.matrix4x4", lua_matrix4x4_metamethods); register_module(L, "matrix4x4", lua_matrix4x4_methods); }
/** * \brief load a sr module * * tries to load the module specified by mod_path. * If mod_path is 'modname' or 'modname.so' then * \<MODS_DIR\>/\<modname\>.so will be tried and if this fails * \<MODS_DIR\>/\<modname\>/\<modname\>.so * If mod_path contain a '/' it is assumed to be the * path to the module and tried first. If fails and mod_path is not * absolute path (not starting with '/') then will try: * \<MODS_DIR\>/mod_path * @param mod_path path or module name * @return 0 on success , <0 on error */ int load_module(char* mod_path) { void* handle; char* error; mod_register_function mr; module_exports_t* exp; struct sr_module* t; struct stat stat_buf; str modname; char* mdir; char* nxt_mdir; char* path; int mdir_len; int len; int dlflags; int new_dlflags; int retries; int path_type; str expref; char exbuf[64]; #ifndef RTLD_NOW /* for openbsd */ #define RTLD_NOW DL_LAZY #endif path=mod_path; path_type = 0; modname.s = path; modname.len = strlen(mod_path); if(modname.len>3 && strcmp(modname.s+modname.len-3, ".so")==0) { path_type = 1; modname.len -= 3; } if (!strchr(path, '/')) path_type |= 2; if((path_type&2) || path[0] != '/') { /* module name was given, we try to construct the path */ mdir=mods_dir; /* search path */ do{ nxt_mdir=strchr(mdir, ':'); if (nxt_mdir) mdir_len=(int)(nxt_mdir-mdir); else mdir_len=strlen(mdir); if(path_type&2) { /* try path <MODS_DIR>/<modname>.so */ path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ + modname.len + 3 /* ".so" */ + 1); if (path==0) goto error; memcpy(path, mdir, mdir_len); len = mdir_len; if (len != 0 && path[len - 1] != '/'){ path[len]='/'; len++; } path[len]=0; strcat(path, modname.s); if(!(path_type&1)) strcat(path, ".so"); if (stat(path, &stat_buf) == -1) { LM_DBG("module file not found <%s>\n", path); pkg_free(path); /* try path <MODS_DIR>/<modname>/<modname>.so */ path = (char*)pkg_malloc( mdir_len + 1 /* "/" */ + modname.len + 1 /* "/" */ + modname.len + 3 /* ".so" */ + 1); if (path==0) goto error; memcpy(path, mdir, mdir_len); len = mdir_len; if (len != 0 && path[len - 1] != '/') { path[len]='/'; len++; } path[len]=0; strncat(path, modname.s, modname.len); strcat(path, "/"); strcat(path, modname.s); if(!(path_type&1)) strcat(path, ".so"); if (stat(path, &stat_buf) == -1) { LM_DBG("module file not found <%s>\n", path); pkg_free(path); path=0; } } } else { /* try mod_path - S compat */ if(path==mod_path) { if (stat(path, &stat_buf) == -1) { LM_DBG("module file not found <%s>\n", path); path=0; } } if(path==0) { /* try path <MODS_DIR>/mod_path - K compat */ path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ + strlen(mod_path) + 1); if (path==0) goto error; memcpy(path, mdir, mdir_len); len = mdir_len; if (len != 0 && path[len - 1] != '/'){ path[len]='/'; len++; } path[len]=0; strcat(path, mod_path); if (stat(path, &stat_buf) == -1) { LM_DBG("module file not found <%s>\n", path); pkg_free(path); path=0; } } } mdir=nxt_mdir?nxt_mdir+1:0; }while(path==0 && mdir); if (path==0){ LM_ERR("could not find module <%.*s> in <%s>\n", modname.len, modname.s, mods_dir); goto error; } } LM_DBG("trying to load <%s>\n", path); retries=2; dlflags=RTLD_NOW; reload: handle=dlopen(path, dlflags); /* resolve all symbols now */ if (handle==0){ LM_ERR("could not open module <%s>: %s\n", path, dlerror()); goto error; } for(t=modules;t; t=t->next){ if (t->handle==handle){ LM_WARN("attempting to load the same module twice (%s)\n", path); goto skip; } } /* version control */ if (!version_control(handle, path)) { exit(-1); } /* launch register */ mr = (mod_register_function)dlsym(handle, "mod_register"); if (((error =(char*)dlerror())==0) && mr) { /* no error call it */ new_dlflags=dlflags; if (mr(path, &new_dlflags, 0, 0)!=0) { LM_ERR("%s: mod_register failed\n", path); goto error1; } if (new_dlflags!=dlflags && new_dlflags!=0) { /* we have to reload the module */ dlclose(handle); dlflags=new_dlflags; retries--; if (retries>0) goto reload; LM_ERR("%s: cannot agree on the dlflags\n", path); goto error; } } exp = (module_exports_t*)dlsym(handle, "exports"); if(exp==NULL) { error =(char*)dlerror(); LM_DBG("attempt to lookup exports structure failed - dlerror: %s\n", (error)?error:"none"); /* 'exports' structure not found, look up for '_modulename_exports' */ mdir = strrchr(mod_path, '/'); if (!mdir) { expref.s = mod_path; } else { expref.s = mdir+1; } expref.len = strlen(expref.s); if(expref.len>3 && strcmp(expref.s+expref.len-3, ".so")==0) expref.len -= 3; snprintf(exbuf, 62, "_%.*s_exports", expref.len, expref.s); LM_DBG("looking up exports with name: %s\n", exbuf); exp = (module_exports_t*)dlsym(handle, exbuf); if(exp==NULL || (error =(char*)dlerror())!=0 ){ LM_ERR("failure for exports symbol: %s - dlerror: %s\n", exbuf, (error)?error:"none"); goto error1; } } /* hack to allow for kamailio style dlflags inside exports */ new_dlflags = exp->dlflags; if (new_dlflags!=dlflags && new_dlflags!=DEFAULT_DLFLAGS) { /* we have to reload the module */ dlclose(handle); DEBUG("%s: exports dlflags interface is deprecated and it will not" " be supported in newer versions; consider using" " mod_register() instead\n", path); dlflags=new_dlflags; retries--; if (retries>0) goto reload; LM_ERR("%s: cannot agree on the dlflags\n", path); goto error; } if (register_module(exp, path, handle)<0) goto error1; return 0; error1: dlclose(handle); error: skip: if (path && path!=mod_path) pkg_free(path); return -1; }
void luaopen_image(lua_State *L) { register_metatable(L, "glomp.image", lua_image_metamethods); register_module(L, "image", lua_image_methods); }
/****************************************************************************** * * * Function: load_modules * * * * Purpose: load loadable modules (dynamic libraries) * * It skips a module in case of any errors * * * * Parameters: path - directory where modules are located * * file_names - list of module names * * timeout - timeout in seconds for processing of items by module * * verbose - output list of loaded modules * * * * Return value: SUCCEED - all modules is successfully loaded * * FAIL - loading of modules failed * * * ******************************************************************************/ int load_modules(const char *path, char **file_names, int timeout, int verbose) { const char *__function_name = "load_modules"; char **file_name, *buffer = NULL; void *lib; char full_name[MAX_STRING_LEN], error[MAX_STRING_LEN]; int (*func_init)(), (*func_version)(); ZBX_METRIC *(*func_list)(); void (*func_timeout)(); int i, ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); for (file_name = file_names; NULL != *file_name; file_name++) { zbx_snprintf(full_name, sizeof(full_name), "%s/%s", path, *file_name); zabbix_log(LOG_LEVEL_DEBUG, "loading module \"%s\"", full_name); if (NULL == (lib = dlopen(full_name, RTLD_NOW))) { zabbix_log(LOG_LEVEL_CRIT, "cannot load module \"%s\": %s", *file_name, dlerror()); goto fail; } *(void **)(&func_version) = dlsym(lib, ZBX_MODULE_FUNC_API_VERSION); if (NULL == func_version) { zabbix_log(LOG_LEVEL_CRIT, "cannot find \"" ZBX_MODULE_FUNC_API_VERSION "()\"" " function in module \"%s\": %s", *file_name, dlerror()); dlclose(lib); goto fail; } if (ZBX_MODULE_API_VERSION_ONE != (i = func_version())) { zabbix_log(LOG_LEVEL_CRIT, "unsupported module \"%s\" version: %d", *file_name, i); dlclose(lib); goto fail; } *(void **)(&func_init) = dlsym(lib, ZBX_MODULE_FUNC_INIT); if (NULL == func_init) { zabbix_log(LOG_LEVEL_CRIT, "cannot find \"" ZBX_MODULE_FUNC_INIT "()\"" " function in module \"%s\": %s", *file_name, dlerror()); dlclose(lib); goto fail; } if (ZBX_MODULE_OK != func_init()) { zabbix_log(LOG_LEVEL_CRIT, "cannot initialize module \"%s\"", *file_name); dlclose(lib); goto fail; } /* the function is optional, zabbix will load the module ieven if it is missing */ *(void **)(&func_timeout) = dlsym(lib, ZBX_MODULE_FUNC_ITEM_TIMEOUT); if (NULL == func_timeout) { zabbix_log(LOG_LEVEL_DEBUG, "cannot find \"" ZBX_MODULE_FUNC_ITEM_TIMEOUT "()\"" " function in module \"%s\": %s", *file_name, dlerror()); } else func_timeout(timeout); *(void **)(&func_list) = dlsym(lib, ZBX_MODULE_FUNC_ITEM_LIST); if (NULL == func_list) { zabbix_log(LOG_LEVEL_WARNING, "cannot find \"" ZBX_MODULE_FUNC_ITEM_LIST "()\"" " function in module \"%s\": %s", *file_name, dlerror()); dlclose(lib); continue; } if (SUCCEED == register_module(lib)) { ZBX_METRIC *metrics; metrics = func_list(); for (i = 0; NULL != metrics[i].key; i++) { /* accept only CF_HAVEPARAMS flag from module items */ metrics[i].flags &= CF_HAVEPARAMS; /* the flag means that the items comes from a loadable module */ metrics[i].flags |= CF_MODULE; if (SUCCEED != add_metric(&metrics[i], error, sizeof(error))) { zabbix_log(LOG_LEVEL_CRIT, "cannot load module \"%s\": %s", *file_name, error); exit(EXIT_FAILURE); } } if (1 == verbose) { if (NULL != buffer) buffer = zbx_strdcat(buffer, ", "); buffer = zbx_strdcat(buffer, *file_name); } } } if (NULL != buffer) zabbix_log(LOG_LEVEL_WARNING, "loaded modules: %s", buffer); ret = SUCCEED; fail: zbx_free(buffer); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }