int main() { // 初始化守护进程 bool ret = initDaemon(); if (!ret) { printf("Init daemon failed\n"); return 1; } FILE* file = NULL; time_t t = 0; // 每隔10秒向test.log报告运行状态 while (true) { sleep(10); file = fopen("./var/test.log", "a+"); if (file != NULL) { t = time(NULL); fprintf(file, "I am here at %s\n", asctime(localtime(&t))); fclose(file); } } return 0; }
/** * @brief Constructor */ PnorRP::PnorRP() : iv_TOC_used(0) ,iv_msgQ(NULL) ,iv_startupRC(0) { TRACFCOMP(g_trac_pnor, "PnorRP::PnorRP> " ); // setup everything in a separate function initDaemon(); TRACFCOMP(g_trac_pnor, "< PnorRP::PnorRP : Startup Errors=%X ", iv_startupRC ); }
int main(int argc, char **argv) { // question -- should we log errors w/syslog? Since we // are a daemon, the printf's aren't really going anywhere... if (signal(SIGINT, sighandler) == SIG_ERR) debug("warning, can't catch SIGINT"); if (signal(SIGTERM, sighandler) == SIG_ERR) debug("warning, can't catch SIGTERM"); if (signal(SIGHUP, sighandler) == SIG_ERR) debug("warning, can't catch SIGHUP"); if (signal(SIGUSR1, sighandler) == SIG_ERR) debug("warning, can't catch SIGUSR1"); // Only start up if we aren't running already. // initDaemon will exit if another is already running initDaemon(); KApplication app(argc, argv, "alarmd"); // if no filename is supplied, read from config file. if (argc < 2) { QString cfgPath = KApplication::localconfigdir(); cfgPath += "/korganizerrc"; KConfig *config = new KConfig(cfgPath.data()); config->setGroup("General"); QString newFileName = config->readEntry("Current Calendar"); delete config; ad = new AlarmDaemon(newFileName.data()); } else ad = new AlarmDaemon(argv[1]); ad->dock(); app.enableSessionManagement(TRUE); app.setTopWidget(new QWidget); app.exec(); delete ad; return 0; }
//----------------------------------------------------------------------------- // 描述: 应用程序初始化 (若初始化失败则抛出异常) //----------------------------------------------------------------------------- void IseApplication::initialize() { try { #ifdef ISE_LINUX // 在初始化阶段要屏蔽退出信号 SignalMasker sigMasker(true); sigMasker.setSignals(1, SIGTERM); sigMasker.block(); #endif networkInitialize(); initExeName(); iseBusiness_->beforeInit(); iseBusiness_->initIseOptions(iseOptions_); processStandardArgs(true); checkMultiInstance(); if (iseOptions_.getIsDaemon()) initDaemon(); initSignals(); initNewOperHandler(); applyIseOptions(); createMainServer(); iseBusiness_->initialize(); mainServer_->initialize(); iseBusiness_->afterInit(); if (iseOptions_.getIsDaemon()) closeTerminal(); initialized_ = true; } catch (Exception& e) { openTerminal(); iseBusiness_->onInitFailed(e); doFinalize(); throw; } }
int main(int , char **) { initDaemon(); return 0; }
int main(int argc,char **argv) { InitModuleObjects(); EnableSEHtoExceptionMapping(); #ifndef __64BIT__ // Restrict stack sizes on 32-bit systems Thread::setDefaultStackSize(0x10000); // 64K stack (also set in windows DSP) #endif Owned<IFile> sentinelFile = createSentinelTarget(); removeSentinelFile(sentinelFile); SocketEndpoint listenep; unsigned sendbufsize = 0; unsigned recvbufsize = 0; int i = 1; bool isdaemon = (memicmp(argv[0]+strlen(argv[0])-4,".exe",4)==0); // bit of a kludge for windows - if .exe not specified then not daemon bool locallisten = false; const char *logdir=NULL; bool requireauthenticate = false; StringBuffer logDir; StringBuffer instanceName; //Get SSL Settings const char * sslCertFile; bool useSSL; unsigned short dafsPort;//DAFILESRV_PORT or SECURE_DAFILESRV_PORT querySecuritySettings(&useSSL, &dafsPort, &sslCertFile, NULL); unsigned parallelRequestLimit = DEFAULT_PARALLELREQUESTLIMIT; unsigned throttleDelayMs = DEFAULT_THROTTLEDELAYMS; unsigned throttleCPULimit = DEFAULT_THROTTLECPULIMIT; Owned<IPropertyTree> env = getHPCCEnvironment(); if (env) { StringBuffer dafilesrvPath("Software/DafilesrvProcess"); if (instanceName.length()) dafilesrvPath.appendf("[@name=\"%s\"]", instanceName.str()); IPropertyTree *daFileSrv = env->queryPropTree(dafilesrvPath); if (daFileSrv) { // global DaFileSrv settings: parallelRequestLimit = daFileSrv->getPropInt("@parallelRequestLimit", DEFAULT_PARALLELREQUESTLIMIT); throttleDelayMs = daFileSrv->getPropInt("@throttleDelayMs", DEFAULT_THROTTLEDELAYMS); throttleCPULimit = daFileSrv->getPropInt("@throttleCPULimit", DEFAULT_THROTTLECPULIMIT); // any overrides by Instance definitions? // NB: This won't work if netAddress is "." or if we start supporting hostnames there StringBuffer ipStr; queryHostIP().getIpText(ipStr); VStringBuffer daFileSrvPath("Instance[@netAddress=\"%s\"]", ipStr.str()); IPropertyTree *dafileSrvInstance = daFileSrv->queryPropTree(daFileSrvPath); if (dafileSrvInstance) { parallelRequestLimit = dafileSrvInstance->getPropInt("@parallelRequestLimit", parallelRequestLimit); throttleDelayMs = dafileSrvInstance->getPropInt("@throttleDelayMs", throttleDelayMs); throttleCPULimit = dafileSrvInstance->getPropInt("@throttleCPULimit", throttleCPULimit); } } } while (argc>i) { if (stricmp(argv[i],"-D")==0) { i++; isdaemon = true; } else if (stricmp(argv[i],"-R")==0) { // for remote run i++; #ifdef _WIN32 isdaemon = false; #else isdaemon = true; #endif } else if (stricmp(argv[i],"-A")==0) { i++; requireauthenticate = true; } else if ((argv[i][0]=='-')&&(toupper(argv[i][1])=='T')&&(!argv[i][2]||isdigit(argv[i][2]))) { if (argv[i][2]) setDafsTrace(NULL,(byte)atoi(argv[i]+2)); i++; isdaemon = false; } else if ((argc>i+1)&&(stricmp(argv[i],"-L")==0)) { i++; logDir.clear().append(argv[i++]); } else if ((argc>i+1)&&(stricmp(argv[i],"-I")==0)) { i++; instanceName.clear().append(argv[i++]); } else if (stricmp(argv[i],"-LOCAL")==0) { i++; locallisten = true; } else if (stricmp(argv[i],"-NOSSL")==0) {//overrides config setting i++; if (useSSL) { PROGLOG("DaFileSrv SSL specified in config but overridden by -NOSSL in command line"); useSSL = false; dafsPort = DAFILESRV_PORT; } } else break; } if (useSSL && !sslCertFile) { ERRLOG("DaFileSrv SSL specified but certificate file information missing from environment.conf"); exit(-1); } if (0 == logDir.length()) { getConfigurationDirectory(NULL,"log","dafilesrv",instanceName.str(),logDir); if (0 == logDir.length()) logDir.append("."); } if (instanceName.length()) { addPathSepChar(logDir); logDir.append(instanceName.str()); } #ifdef _WIN32 if ((argc>i)&&(stricmp(argv[i],"-install")==0)) { if (installService(DAFS_SERVICE_NAME,DAFS_SERVICE_DISPLAY_NAME,NULL)) { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Installed"); return 0; } return 1; } if ((argc>i)&&(stricmp(argv[i],"-remove")==0)) { if (uninstallService(DAFS_SERVICE_NAME,DAFS_SERVICE_DISPLAY_NAME)) { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Uninstalled"); return 0; } return 1; } #endif if (argc == i) listenep.port = dafsPort; else { if (strchr(argv[i],'.')||!isdigit(argv[i][0])) listenep.set(argv[i], dafsPort); else listenep.port = atoi(argv[i]); if (listenep.port==0) { usage(); exit(-1); } sendbufsize = (argc>i+1)?(atoi(argv[i+1])*1024):0; recvbufsize = (argc>i+2)?(atoi(argv[i+2])*1024):0; } if (isdaemon) { #ifdef _WIN32 class cserv: public CService { bool stopped; bool started; SocketEndpoint listenep; bool useSSL; bool requireauthenticate; class cpollthread: public Thread { cserv *parent; public: cpollthread( cserv *_parent ) : Thread("CService::cpollthread"), parent(_parent) { } int run() { while (parent->poll()) Sleep(1000); return 1; } } pollthread; Owned<IRemoteFileServer> server; public: cserv(SocketEndpoint _listenep, bool _useSSL) : listenep(_listenep),useSSL(_useSSL),pollthread(this) { stopped = false; started = false; } virtual ~cserv() { stopped = true; if (started) pollthread.join(); } bool init() { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Initialized"); started = true; pollthread.start(); return true; } bool poll() { if (stopped||!running()) { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Stopping"); if (server) { server->stop(); server.clear(); } return false; } return true; } void run() { // Get params from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DaFileSrv\Parameters int requireauthenticate=0; HKEY hkey; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\DaFileSrv\\Parameters", 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { DWORD dwType = 0; DWORD dwSize = sizeof(requireauthenticate); RegQueryValueEx(hkey, "RequireAuthentication", NULL, &dwType, (BYTE*)&requireauthenticate, &dwSize); RegCloseKey(hkey); } StringBuffer eps; if (listenep.isNull()) eps.append(listenep.port); else listenep.getUrlStr(eps); enableDafsAuthentication(requireauthenticate!=0); PROGLOG("Opening " DAFS_SERVICE_DISPLAY_NAME " on %s%s", useSSL?"SECURE ":"",eps.str()); const char * verstring = remoteServerVersionString(); PROGLOG("Version: %s", verstring); PROGLOG("Authentication:%s required",requireauthenticate?"":" not"); PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Running"); server.setown(createRemoteFileServer(parallelRequestLimit, throttleDelayMs, throttleCPULimit)); try { server->run(listenep, useSSL); } catch (IException *e) { EXCLOG(e,DAFS_SERVICE_NAME); e->Release(); } PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Stopped"); stopped = true; } } service(listenep, useSSL); service.start(); return 0; #else int ret = initDaemon(); if (ret) return ret; #endif } { Owned<IComponentLogFileCreator> lf = createComponentLogFileCreator(logDir.str(), "DAFILESRV"); lf->setCreateAliasFile(false); lf->setMaxDetail(TopDetail); lf->beginLogging(); } PROGLOG("Parallel request limit = %d, throttleDelayMs = %d, throttleCPULimit = %d", parallelRequestLimit, throttleDelayMs, throttleCPULimit); const char * verstring = remoteServerVersionString(); StringBuffer eps; if (listenep.isNull()) eps.append(listenep.port); else listenep.getUrlStr(eps); enableDafsAuthentication(requireauthenticate); PROGLOG("Opening Dali File Server on %s%s", useSSL?"SECURE ":"",eps.str()); PROGLOG("Version: %s", verstring); PROGLOG("Authentication:%s required",requireauthenticate?"":" not"); startPerformanceMonitor(10*60*1000, PerfMonStandard); server.setown(createRemoteFileServer(parallelRequestLimit, throttleDelayMs, throttleCPULimit)); writeSentinelFile(sentinelFile); try { server->run(listenep, useSSL); } catch (IException *e) { EXCLOG(e,"DAFILESRV"); e->Release(); } if (server) server->stop(); server.clear(); PROGLOG("Stopped Dali File Server"); return 0; }
int main(int argc,char **argv) { InitModuleObjects(); EnableSEHtoExceptionMapping(); #ifndef __64BIT__ Thread::setDefaultStackSize(0x10000); // 64K stack (also set in windows DSP) #endif StringBuffer logDir; #ifdef _WIN32 logDir.append("c:\\"); #else if (checkDirExists("/c$")) logDir.append("/c$/"); #endif Owned<IFile> sentinelFile = createSentinelTarget(); removeSentinelFile(sentinelFile); SocketEndpoint listenep; unsigned sendbufsize = 0; unsigned recvbufsize = 0; int i = 1; bool isdaemon = (memicmp(argv[0]+strlen(argv[0])-4,".exe",4)==0); // bit of a kludge for windows - if .exe not specified then not daemon bool locallisten = false; const char *logdir=NULL; bool requireauthenticate = false; while (argc>i) { if (stricmp(argv[i],"-D")==0) { i++; isdaemon = true; } else if (stricmp(argv[i],"-R")==0) { // for remote run i++; #ifdef _WIN32 isdaemon = false; #else isdaemon = true; #endif } else if (stricmp(argv[i],"-A")==0) { i++; requireauthenticate = true; } else if ((argv[i][0]=='-')&&(toupper(argv[i][1])=='T')&&(!argv[i][2]||isdigit(argv[i][2]))) { if (argv[i][2]) setDafsTrace(NULL,(byte)atoi(argv[i]+2)); i++; isdaemon = false; } else if ((argc>i+1)&&(stricmp(argv[i],"-L")==0)) { i++; logDir.clear().append(argv[i++]); addPathSepChar(logDir); } else if (stricmp(argv[i],"-LOCAL")==0) { i++; locallisten = true; } else break; } #ifdef _WIN32 if ((argc>i)&&(stricmp(argv[i],"-install")==0)) { if (installService(DAFS_SERVICE_NAME,DAFS_SERVICE_DISPLAY_NAME,NULL)) { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Installed"); return 0; } return 1; } if ((argc>i)&&(stricmp(argv[i],"-remove")==0)) { if (uninstallService(DAFS_SERVICE_NAME,DAFS_SERVICE_DISPLAY_NAME)) { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Uninstalled"); return 0; } return 1; } #endif if (argc == i) listenep.port = DAFILESRV_PORT; else { if (strchr(argv[i],'.')||!isdigit(argv[i][0])) listenep.set(argv[i],DAFILESRV_PORT); else listenep.port = atoi(argv[i]); if (listenep.port==0) { usage(); exit(-1); } sendbufsize = (argc>i+1)?(atoi(argv[i+1])*1024):0; recvbufsize = (argc>i+2)?(atoi(argv[i+2])*1024):0; } if (isdaemon) { #ifdef _WIN32 class cserv: public CService { bool stopped; bool started; SocketEndpoint listenep; bool requireauthenticate; class cpollthread: public Thread { cserv *parent; public: cpollthread( cserv *_parent ) : Thread("CService::cpollthread"), parent(_parent) { } int run() { while (parent->poll()) Sleep(1000); return 1; } } pollthread; Owned<IRemoteFileServer> server; public: cserv(SocketEndpoint _listenep) : listenep(_listenep),pollthread(this) { stopped = false; started = false; } virtual ~cserv() { stopped = true; if (started) pollthread.join(); } bool init() { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Initialized"); started = true; pollthread.start(); return true; } bool poll() { if (stopped||!running()) { PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Stopping"); if (server) { server->stop(); server.clear(); } return false; } return true; } void run() { // Get params from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DaFileSrv\Parameters int requireauthenticate=0; HKEY hkey; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\DaFileSrv\\Parameters", 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { DWORD dwType = 0; DWORD dwSize = sizeof(requireauthenticate); RegQueryValueEx(hkey, "RequireAuthentication", NULL, &dwType, (BYTE*)&requireauthenticate, &dwSize); RegCloseKey(hkey); } StringBuffer eps; if (listenep.isNull()) eps.append(listenep.port); else listenep.getUrlStr(eps); enableDafsAuthentication(requireauthenticate!=0); PROGLOG("Opening "DAFS_SERVICE_DISPLAY_NAME" on %s", eps.str()); const char * verstring = remoteServerVersionString(); PROGLOG("Version: %s", verstring); PROGLOG("Authentication:%s required",requireauthenticate?"":" not"); PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Running"); server.setown(createRemoteFileServer()); try { server->run(listenep); } catch (IException *e) { EXCLOG(e,DAFS_SERVICE_NAME); e->Release(); } PROGLOG(DAFS_SERVICE_DISPLAY_NAME " Stopped"); stopped = true; } } service(listenep); service.start(); return 0; #else int ret = initDaemon(); if (ret) return ret; #endif } { Owned<IComponentLogFileCreator> lf = createComponentLogFileCreator(logDir.str(), "DAFILESRV"); lf->setCreateAliasFile(false); lf->setMaxDetail(TopDetail); lf->beginLogging(); } const char * verstring = remoteServerVersionString(); StringBuffer eps; if (listenep.isNull()) eps.append(listenep.port); else listenep.getUrlStr(eps); enableDafsAuthentication(requireauthenticate); PROGLOG("Opening Dali File Server on %s", eps.str()); PROGLOG("Version: %s", verstring); PROGLOG("Authentication:%s required",requireauthenticate?"":" not"); startPerformanceMonitor(10*60*1000, PerfMonStandard); server.setown(createRemoteFileServer()); writeSentinelFile(sentinelFile); try { server->run(listenep); } catch (IException *e) { EXCLOG(e,"DAFILESRV"); e->Release(); } if (server) server->stop(); server.clear(); PROGLOG("Stopped Dali File Server"); return 0; }