static STLW::string GetBaseDir(const STLW::string & szTemplateName, STLW::string & sNormalizedFileName) { if (szTemplateName.length() == 0) { return ""; } STLW::vector<STLW::string> vCurrentDir; CCHAR_P sBegin = szTemplateName.c_str(); CCHAR_P szEnd = szTemplateName.c_str() + szTemplateName.length(); CCHAR_P sIter = sBegin; while (sIter != szEnd) { if (*sIter == '/') { if (sIter != sBegin) { STLW::string sTMP(sBegin, sIter); if (sTMP == "/." || sTMP == "/") { ;; } else if (sTMP == "/..") { STLW::vector<STLW::string>::iterator itEnd = vCurrentDir.end(); if (vCurrentDir.begin() == itEnd) { return ""; } vCurrentDir.erase(--itEnd); } else { vCurrentDir.push_back(sTMP); } } sBegin = sIter; } ++sIter; } STLW::string sTMP(sBegin, sIter); if (sTMP == "/") { return ""; } STLW::string sResult; for (UINT_32 iI = 0; iI < vCurrentDir.size(); ++iI) { sResult.append(vCurrentDir[iI]); } sNormalizedFileName.assign(sResult); sNormalizedFileName.append(sTMP); sResult.append("/"); return sResult; }
int main(int argc, char ** argv) { const char * szConfigFile = NULL; if (argc == 1) { fprintf(stderr, "Global config not given, "); szConfigFile = getenv("CAS_GLOBAL_CONFIG"); if (szConfigFile != NULL) { fprintf(stderr, " using %s from ENVIRONMENT", szConfigFile); } else { szConfigFile = CAS_GLOBAL_CONFIG_FILE; fprintf(stderr, " using %s as DEFAULT\n", szConfigFile); } } else if (argc == 2) { szConfigFile = argv[1]; } else { fprintf(stderr, "usage: %s [global-config.xml]\n", argv[0]); return EX_USAGE; } FILE * F = fopen(szConfigFile, "rb"); if (F == NULL) { fprintf(stderr, "ERROR: Cannot open `%s` for reading: %s\n", szConfigFile, strerror(errno)); return EX_SOFTWARE; } // Store path to file as include directory CCHAR_P szTMP = szConfigFile + strlen(szConfigFile); while (szTMP != szConfigFile && *szTMP != '/' && *szTMP != '\\') { --szTMP; } STLW::vector<STLW::string> vIncludeDirs; if (szTMP != szConfigFile) { vIncludeDirs.push_back(STLW::string(szConfigFile, (szTMP - szConfigFile))); } try { ASGlobalConfig oGlobalConfig; ASGlobalConfigHandler oHandler(oGlobalConfig, vIncludeDirs); ASXMLParser oParser(&oHandler); if (oParser.ParseFile(F) == -1) { fprintf(stderr, "ERROR: In file %s: %s\n", szConfigFile, oHandler.GetError().c_str()); return EX_CONFIG; } fclose(F); fprintf(stdout, " Libexec dirs:\n"); UINT_32 iI = 0; for(; iI < oGlobalConfig.libexec_dirs.size(); ++iI) { fprintf(stdout, " %s\n", oGlobalConfig.libexec_dirs[iI].c_str()); } fprintf(stdout, "\n Modules:\n"); for(iI = 0; iI < oGlobalConfig.modules_list.size(); ++iI) { fprintf(stdout, " Name: %s\n" " Type: %s\n", oGlobalConfig.modules_list[iI].name.c_str(), oGlobalConfig.modules_list[iI].moduletype.c_str()); STLW::string sTMP = CheckFile(oGlobalConfig.libexec_dirs, oGlobalConfig.modules_list[iI].library); if (sTMP.size() == 0) { fprintf(stdout, " *** ERROR: Cannot find Library file: %s\n", oGlobalConfig.modules_list[iI].library.c_str()); } else { fprintf(stdout, " Library file: %s\n", CheckFile(oGlobalConfig.libexec_dirs, oGlobalConfig.modules_list[iI].library).c_str()); } STLW::string sData = Dump(oGlobalConfig.modules_list[iI].configuration); if (!sData.empty() && sData != "\"\"\n") { fprintf(stdout, " Configuration: %s\n", sData.c_str()); } // TBD fprintf(stdout, "\n"); } } catch(STLW::exception &e) { fprintf(stderr, "ERROR: %s\n", e.what()); return EX_SOFTWARE; } catch(...) { fprintf(stderr, "ERROR: Ouch!\n"); return EX_SOFTWARE; } fclose(stdin); fclose(stdout); fclose(stderr); return EX_OK; }
// // Run main process // INT_32 MainProcess::Run() { // Create main event context MainLoopContext oMainContext(oGlobalContext); // Check number of listeners Listeners::iterator itvListeners = vListeners.begin(); if (itvListeners == vListeners.end()) { oGlobalContext.error_log -> Emerg("No listeners found"); return -1; } // Find smallest timeout oMainContext.timeout_check_interval = itvListeners -> service_config -> io_timeout; while(itvListeners != vListeners.end()) { if (oMainContext.timeout_check_interval > itvListeners -> service_config -> io_timeout) { oMainContext.timeout_check_interval = itvListeners -> service_config -> io_timeout; } ++itvListeners; } // Need double precision if (oMainContext.timeout_check_interval != -1) { oMainContext.timeout_check_interval /= 2; } oGlobalContext.error_log -> Info("Timeout check interval set to %f sec", oMainContext.timeout_check_interval); // Contextes for thread watchers STLW::vector<AcceptLoopContext *> vContextes; // Shutdown accept sockets when SIGTERM catched and shutdown all sockets when SIGINT catched MainLoopSignalHandler oMainLoopSignalHandler(oMainContext, vContextes); oSigHandler.RegisterHandler(SIGINT, &oMainLoopSignalHandler); oSigHandler.RegisterHandler(SIGTERM, &oMainLoopSignalHandler); // Create group of threads with network listeners ThreadGroup<AcceptLoopWorker> oThreadGroup; // Create threads for(UINT_32 iPos = 0; iPos < oGlobalContext.config.network_threads; ++iPos) { AcceptLoopContext * pAcceptLoopContext = new AcceptLoopContext(oMainContext); vContextes.push_back(pAcceptLoopContext); INT_32 iCPUNum = -1; if (oGlobalContext.config.bind_network_threads_to_cpu) { iCPUNum = iPos; } oThreadGroup.CreateThread(new AcceptLoopWorker(pAcceptLoopContext, iCPUNum)); } // Create watcher one per each server socket STLW::vector<MainLoopEventWatcher *> vWatchers; itvListeners = vListeners.begin(); while(itvListeners != vListeners.end()) { vWatchers.push_back(new MainLoopEventWatcher(oMainContext, vContextes, itvListeners -> socket, itvListeners -> service_config)); ++itvListeners; } // Wait for events oGlobalContext.error_log -> Info("Staring main loop"); oMainContext.main_loop.Loop(); oGlobalContext.error_log -> Info("Shutting down all watchers"); // Clear garbage and exit STLW::vector<MainLoopEventWatcher *>::iterator itvWatchers = vWatchers.begin(); while (itvWatchers != vWatchers.end()) { delete *itvWatchers; ++itvWatchers; } // Remove signal handlers oSigHandler.RemoveHandler(SIGINT, &oMainLoopSignalHandler); oSigHandler.RemoveHandler(SIGTERM, &oMainLoopSignalHandler); oGlobalContext.error_log -> Info("Waiting for network threads"); return 0; }