ERRCODE Config::load( std::string filename, bool createnew ) { std::ifstream configfile; std::string lineinput, category( "Global" ), item, value; ERRCODE ret = OZ_OK; if( isloaded()) clear(); configfile.open( filename.c_str()); if( !configfile.is_open()) { if( FileExists( filename ) || !createnew ) { returncode( OZ_INI_FAILED_TO_OPEN ); } else { return( create( filename )); } } m_isloaded = true; while( configfile.good()) { std::getline( configfile, lineinput ); trim( lineinput ); if( matchcategory( lineinput, category )) { } else if( matchline( lineinput, item, value )) { if(( ret = set( category + ":" + item, value )) != 0 ) { configfile.close(); m_isloaded = false; returncode( ret ); } } } configfile.close(); m_configfilename = filename; returncode( OZ_OK ); }
ERRCODE Config::save() { std::ofstream configfile; std::string setcategory( "Global" ), item, category; dictionaryiterator itemiterator; if( !isloaded()) returncode( OZ_INI_NO_DICT ); configfile.open( m_configfilename.c_str()); if( !configfile.is_open()) returncode( OZ_INI_FAILED_TO_WRITE ); for( itemiterator = m_dictionary.begin(); itemiterator != m_dictionary.end(); ++itemiterator ) { if( splitkey( itemiterator->first, category, item )) { if( setcategory != category ) { setcategory = category; configfile << "[" << category << "]" << std::endl; } configfile << item << " = " << itemiterator->second << "\n"; } else { configfile << itemiterator->first << " = " << itemiterator->second << std::endl; } } configfile.close(); returncode( OZ_OK ); }
ERRCODE Config::clear() { if( !isloaded()) returncode( OZ_INI_NO_DICT ); m_dictionary.clear(); m_isloaded = false; m_configfilename = ""; returncode( OZ_OK ); }
/* Set the reload mark on the indicated module, if loaded, and all its * children */ void setreloadmark(struct module_dep *mdp) { unsigned int i; if (!isloaded(mdp->name->content)) return; for (i=0;i<mdp->numchildren;i++) setreloadmark(mdp->children[i]); mdp->reloading=1; }
int rmmod(char *modulename, int close) { int i,j; module *mods; struct module_dep *mdp; char modulebuf[1024]; strlcpy(modulebuf, modulename, sizeof(modulebuf)); delchars(modulebuf,"./\\;"); i=getindex(modulebuf); if (i<0) return 1; if ((mdp=getmoduledep(modulebuf))) { for (j=0;j<mdp->numchildren;j++) { if (isloaded(mdp->children[j]->name->content)) { if (rmmod(mdp->children[j]->name->content, close)) { Error("core",ERR_WARNING,"Unable to remove child module %s (depends on %s)", mdp->children[j]->name->content, modulebuf); return 1; } } } /* We may have removed other modules - reaquire the index number in case it has changed. */ i=getindex(modulebuf); if (i<0) return 1; } else { Error("core",ERR_WARNING,"Removing module %s without dependency information",modulebuf); } mods=(module *)(modules.content); if (!close #ifdef BROKEN_DLCLOSE || 1 #endif ) { void (*fini)(); fini = dlsym(mods[i].handle, "__fini"); if(!dlerror()) fini(); } else dlclose(mods[i].handle); freesstring(mods[i].name); array_delslot(&modules,i); Error("core",ERR_INFO,"Removed module %s.",modulebuf); return 0; }
ERRCODE Config::create( std::string filename, bool overwrite ) { if( isloaded()) clear(); if( FileExists( filename ) && !overwrite) { returncode( OZ_INI_FILE_EXISTS ); } m_configfilename = filename; m_isloaded = true; returncode( OZ_OK ); }
/** * \brief Get a pointer to given symbol */ void *Module::getSymbol(const std::string& symbolname) { debug(LOG_DEBUG, DEBUG_LOG, 0, "looking up symbol %s", symbolname.c_str()); // make sure the module is already loaded if (!isloaded()) { this->open(); } // find the symbol for the getDescriptor function void *s = dlsym(handle, symbolname.c_str()); if (NULL == s) { std::string msg = stringprintf("module %s lacks symbol %s", _modulename.c_str(), symbolname.c_str()); debug(LOG_ERR, DEBUG_LOG, 0, "%s", msg.c_str()); throw std::invalid_argument(msg); } debug(LOG_DEBUG, DEBUG_LOG, 0, "symbol found at %p", s); return s; }
/** * \brief Open the module by loading and initializing it * * This method uses dlopen() to load the code file into the address space, * initializes the library and keeps a handle to the library for later * use. This method must be called before any module functions can be called. */ void Module::open() { debug(LOG_DEBUG, DEBUG_LOG, 0, "opening module"); if (isloaded()) { debug(LOG_DEBUG, DEBUG_LOG, 0, "already open"); return; } debug(LOG_DEBUG, DEBUG_LOG, 0, "really loading now"); dlerror(); // clear error conditions debug(LOG_DEBUG, DEBUG_LOG, 0, "loading library %s", dlname.c_str()); handle = dlopen(dlname.c_str(), RTLD_NOW); if (NULL == handle) { std::string msg = stringprintf("cannot load %s: %s", dlname.c_str(), dlerror()); debug(LOG_ERR, DEBUG_LOG, 0, "%s", msg.c_str()); throw std::runtime_error(msg); } debug(LOG_DEBUG, DEBUG_LOG, 0, "library opened: handle = %p", handle); }
int insmod(char *modulename) { int i; module *mods; char buf[1024], modulebuf[1024]; const char *(*verinfo)(const char **); struct module_dep *mdp; strlcpy(modulebuf, modulename, sizeof(modulebuf)); delchars(modulebuf,"./\\;"); if (isloaded(modulebuf)) { Error("core",ERR_DEBUG,"Tried to load already loaded module: %s",modulebuf); return 1; } if (strlen(modulebuf)>100) { Error("core",ERR_WARNING,"Module name too long: %s",modulebuf); return 1; } if ((mdp=getmoduledep(modulebuf))) { for (i=0;i<mdp->numparents;i++) { if (!isloaded(mdp->parents[i]->name->content)) { if (insmod(mdp->parents[i]->name->content)) { Error("core",ERR_WARNING,"Error loading dependant module %s (needed by %s)", mdp->parents[i]->name->content,modulebuf); return 1; } } } } else { Error("core",ERR_WARNING,"Loading module %s without dependency information.",modulebuf); } i=array_getfreeslot(&modules); mods=(module *)(modules.content); sprintf(buf,"%s/%s%s",moddir->content,modulebuf,modsuffix->content); mods[i].handle=dlopen(buf,RTLD_NOW|RTLD_GLOBAL); if(mods[i].handle==NULL) { Error("core",ERR_ERROR,"Loading module %s failed: %s",modulebuf,dlerror()); array_delslot(&modules,i); return -1; } mods[i].name=getsstring(modulebuf,MODULENAMELEN); verinfo=dlsym(mods[i].handle,"_version"); if(verinfo) { mods[i].buildid=verinfo(&mods[i].version); } else { mods[i].version=NULL; mods[i].buildid=NULL; } mods[i].loadedsince = time(NULL); Error("core",ERR_INFO,"Loaded module %s OK.",modulebuf); return 0; }
inline catalog_object * catalog_object_header::load() { if (!isloaded()) { object = catalog_deserialize_object(p, CATALOG_PERSISTENT_CONTEXT); }; return object; };
ERRCODE Config::set( std::string item, std::string value ) { if( !isloaded()) returncode( OZ_INI_NO_DICT ); m_dictionary[ item ] = value; returncode( OZ_OK ); }
Config::~Config() { if( isloaded()) clear(); }