void main(int argc, char *argv[]) { for (int arg = 1; arg < argc; arg++) { if (strcmp(argv[arg], "-v") == 0) { debug = LOG_NOTICE; } else if (strcmp(argv[arg], "-vv") == 0) { debug = LOG_INFO; } else if (strcmp(argv[arg], "-l") == 0) { if (arg < argc-1) { logFilename = argv[++arg]; } else { usage(argv[0]); } } else { // Unknown parameter usage(argv[0]); } } SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = HSP_SERVICE_NAME; ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; ServiceTable[1].lpServiceName = NULL; ServiceTable[1].lpServiceProc = NULL; if (0 == StartServiceCtrlDispatcher(ServiceTable)) { //else ServiceMain is called by the dispatcher if (ERROR_FAILED_SERVICE_CONTROLLER_CONNECT == GetLastError()){ //invoked from the commandline. //Turn on debug output at debug level defined by commandline. isService = FALSE; ServiceMain(0, NULL); } } }
int main( int argc, char** argv ) { if( argc > 1 ) { ServiceController sc; std::string arg( argv[1] ); if( arg.compare("-i") == 0 ) { std::cout << "Going to install service... "; TCHAR path[ MAX_PATH ]; GetModuleFileName( NULL, path, sizeof(path) ); if( sc.registerService( SPEPSERVICE_NAME, SPEPSERVICE_NAME, path ) ) { std::cout << "done." << std::endl; } else { std::cout << "FAILED." << std::endl; } } else if( arg.compare("-u") == 0 ) { std::cout << "Going to uninstall service... "; if( sc.unregisterService( SPEPSERVICE_NAME ) ) { std::cout << "done." << std::endl; } else { std::cout << "FAILED." << std::endl; } } else if( arg.compare("-x") == 0 ) { SPEPService::debug = true; ServiceMain(argc,argv); } else { std::cout << "Usage: " << argv[0] << " [-i | -u | -x]" << std::endl; std::cout << " -i Install service" << std::endl; std::cout << " -u Uninstall service" << std::endl; std::cout << " -x Debug service" << std::endl; return 1; } return 0; } SERVICE_TABLE_ENTRY serviceTable[] = { { "SPEP Service", &ServiceMain }, { NULL, NULL } }; StartServiceCtrlDispatcher( serviceTable ); }
VOID CService:: ServiceMain_(DWORD argc,LPTSTR *argv) { // // The Service Control Manager (SCM) waits until the service reports // a status of SERVICE_RUNNING. It is recommended that the service // reports this status as quickly as possible, as other components in // the system that require interaction with SCM will be blocked during // this time. Some functions may require interaction with the SCM // either directly or indirectly. // // The SCM locks the service control database during initialization, // so if a service attempts to call StartService during initialization, // the call will block. When the service reports to the SCM that it has // successfully started, it can call StartService. If the service // requires another service to be running, the service should set // the required dependencies. // // Furthermore, you should not call any system functions during service // initialization. The service code should call system functions only // after it reports a status of SERVICE_RUNNING. // m_sshStatusHandle = ::RegisterServiceCtrlHandlerEx( m_szServiceName, (LPHANDLER_FUNCTION_EX) CService::S_ServiceCtrlHandlerEx, (LPVOID) this); if (!m_sshStatusHandle) { ReportStatusToSCMgr(SERVICE_STOPPED, 0, GetLastError()); return; } // SERVICE_STATUS members that don't change in example // m_ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; m_ssStatus.dwServiceSpecificExitCode = 0; // report the status to the service control manager. // if (!ReportStatusToSCMgr(SERVICE_START_PENDING)) { ReportStatusToSCMgr(SERVICE_STOPPED, 0, GetLastError()); return; } // Call ServiceMain of the instance ServiceMain(argc, argv); return; }
void main() { SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName = "hsflowd"; ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; ServiceTable[1].lpServiceName = NULL; ServiceTable[1].lpServiceProc = NULL; if(0 == StartServiceCtrlDispatcher(ServiceTable)){ if(ERROR_FAILED_SERVICE_CONTROLLER_CONNECT == GetLastError()){ //invoked from the commandline. turn on debug output. debug = 1; ServiceMain(0,NULL); } } }
/* * Main entry point for walsendserver controller process. * * This code is heavily based on pgarch.c, q.v. */ int walsendserver_start(void) { pid_t WalSendServerPID; #ifdef EXEC_BACKEND switch ((WalSendServerPID = walsendserver_forkexec())) #else switch ((WalSendServerPID = fork_process())) #endif { case -1: ereport(LOG, (errmsg("could not fork walsendserver process: %m"))); return 0; #ifndef EXEC_BACKEND case 0: /* in postmaster child ... */ /* Close the postmaster's sockets */ ClosePostmasterPorts(false); ServiceInit(&WalSendServer_ServiceConfig, &WalSendServer_ServiceCtrl); ServiceMain(&WalSendServer_ServiceCtrl); break; #endif default: return (int) WalSendServerPID; } /* shouldn't get here */ return 0; }
int main(int argc, char* argv[]) { SC_HANDLE hSCManager = NULL, hService = NULL; char szImagePath[MAX_PATH]; SERVICE_TABLE_ENTRY ServiceTable[] = { { SERVICE_NAME, ServiceMain }, { NULL, NULL } }; if(argc==1) { // Attempt to start service. If this fails we're probably // not running as a service if(!StartServiceCtrlDispatcher(ServiceTable)) return 0; } if(argc!=2 || (strcmp(argv[1],"-i") && strcmp(argv[1],"-u") && strcmp(argv[1],"-test") && strcmp(argv[1],"-v") && strcmp(argv[1],"-systray") )) { fprintf(stderr, "NT CVS Service Handler\n\n" "Arguments:\n" "\t%s -i\tInstall\n" "\t%s -u\tUninstall\n" "\t%s -test\tInteractive run" "\t%s -v\tReport version number", basename(argv[0]),basename(argv[0]), basename(argv[0]), basename(argv[0])); return -1; } if (!strcmp(argv[1],"-v")) { puts(NTSERVICE_VERSION_STRING); return 0; } if(!strcmp(argv[1],"-i")) { // connect to the service control manager if((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) { fprintf(stderr,"OpenSCManager Failed\n"); return -1; } if((hService=OpenService(hSCManager,SERVICE_NAME,DELETE))!=NULL) { DeleteService(hService); CloseServiceHandle(hService); } GetModuleFileName(NULL,szImagePath,MAX_PATH); if ((hService = CreateService(hSCManager,SERVICE_NAME,DISPLAY_NAME, STANDARD_RIGHTS_REQUIRED|SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szImagePath, NULL, NULL, NULL, NULL, NULL)) == NULL) { fprintf(stderr,"CreateService Failed: %s\n",GetErrorString()); return -1; } { BOOL (WINAPI *pChangeServiceConfig2)(SC_HANDLE,DWORD,LPVOID); pChangeServiceConfig2=(BOOL (WINAPI *)(SC_HANDLE,DWORD,LPVOID))GetProcAddress(GetModuleHandle("advapi32"),"ChangeServiceConfig2A"); if(pChangeServiceConfig2) { SERVICE_DESCRIPTION sd = { NTSERVICE_VERSION_STRING }; pChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,&sd); } } CloseServiceHandle(hService); CloseServiceHandle(hSCManager); ReportError(FALSE,DISPLAY_NAME " installed successfully"); printf(DISPLAY_NAME " installed successfully\n"); } if(!strcmp(argv[1],"-u")) { // connect to the service control manager if((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) { fprintf(stderr,"OpenSCManager Failed\n"); return -1; } if((hService=OpenService(hSCManager,SERVICE_NAME,DELETE))==NULL) { fprintf(stderr,"OpenService Failed: %s\n",GetErrorString()); return -1; } if(!DeleteService(hService)) { fprintf(stderr,"DeleteService Failed: %s\n",GetErrorString()); return -1; } CloseServiceHandle(hService); CloseServiceHandle(hSCManager); ReportError(FALSE,DISPLAY_NAME " uninstalled successfully"); printf(DISPLAY_NAME " uninstalled successfully\n"); } else if(!strcmp(argv[1],"-test")) { ServiceMain(999,NULL); } return 0; }
/* in ntservice.c */ int main( int argc, LPTSTR *argv ) { int length; char filename[MAX_PATH], *fname_start; /* * Because the service was registered as SERVICE_WIN32_OWN_PROCESS, * the lpServiceName element of the SERVICE_TABLE_ENTRY will be * ignored. */ SERVICE_TABLE_ENTRY DispatchTable[] = { { "", (LPSERVICE_MAIN_FUNCTION) ServiceMain }, { NULL, NULL } }; /* * set the service's current directory to the installation directory * for the service. this way we don't have to write absolute paths * in the configuration files */ GetModuleFileName( NULL, filename, sizeof( filename ) ); fname_start = strrchr( filename, *LDAP_DIRSEP ); if ( argc > 1 ) { if ( _stricmp( "install", argv[1] ) == 0 ) { char *svcName = SERVICE_NAME; char *displayName = "OpenLDAP Directory Service"; BOOL auto_start = FALSE; if ( (argc > 2) && (argv[2] != NULL) ) svcName = argv[2]; if ( argc > 3 && argv[3]) displayName = argv[3]; if ( argc > 4 && stricmp(argv[4], "auto") == 0) auto_start = TRUE; strcat(filename, " service"); if ( !lutil_srv_install(svcName, displayName, filename, auto_start) ) { fputs( "service failed installation ...\n", stderr ); return EXIT_FAILURE; } fputs( "service has been installed ...\n", stderr ); return EXIT_SUCCESS; } if ( _stricmp( "remove", argv[1] ) == 0 ) { char *svcName = SERVICE_NAME; if ( (argc > 2) && (argv[2] != NULL) ) svcName = argv[2]; if ( !lutil_srv_remove(svcName, filename) ) { fputs( "failed to remove the service ...\n", stderr ); return EXIT_FAILURE; } fputs( "service has been removed ...\n", stderr ); return EXIT_SUCCESS; } if ( _stricmp( "service", argv[1] ) == 0 ) { is_NT_Service = 1; *fname_start = '\0'; SetCurrentDirectory( filename ); } } if (is_NT_Service) { StartServiceCtrlDispatcher(DispatchTable); } else { ServiceMain( argc, argv ); } return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { SC_HANDLE hSCManager = NULL, hService = NULL; TCHAR szImagePath[MAX_PATH]; HKEY hk; DWORD dwType; SERVICE_TABLE_ENTRY ServiceTable[] = { { SERVICE_NAME, ServiceMain }, { NULL, NULL } }; LPSTR szRoot; if(argc==1) { // Attempt to start service. If this fails we're probably // not running as a service if(!StartServiceCtrlDispatcher(ServiceTable)) return 0; } if(argc<2 || (strcmp(argv[1],"-i") && strcmp(argv[1],"-reglsa") && strcmp(argv[1],"-u") && strcmp(argv[1],"-unreglsa") && strcmp(argv[1],"-test") && strcmp(argv[1],"-v") )) { fprintf(stderr, "CVSNT Service Handler\n\n" "Arguments:\n" "\t%s -i [cvsroot]\tInstall\n" "\t%s -reglsa\tRegister LSA helper\n" "\t%s -u\tUninstall\n" "\t%s -unreglsa\tUnregister LSA helper\n" "\t%s -test\tInteractive run\n" "\t%s -v\tReport version number\n", basename(argv[0]),basename(argv[0]), basename(argv[0]), basename(argv[0]), basename(argv[0]), basename(argv[0]) ); return -1; } if(!strcmp(argv[1],"-reglsa")) { TCHAR lsaBuf[10240]; DWORD dwLsaBuf; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SYSTEM\\CurrentControlSet\\Control\\Lsa"),0,KEY_ALL_ACCESS,&hk)) { fprintf(stderr,"Couldn't open LSA registry key, error %d\n",GetLastError()); return -1; } dwLsaBuf=sizeof(lsaBuf); if(RegQueryValueEx(hk,_T("Authentication Packages"),NULL,&dwType,(BYTE*)lsaBuf,&dwLsaBuf)) { fprintf(stderr,"Couldn't read LSA registry key, error %d\n",GetLastError()); return -1; } if(dwType!=REG_MULTI_SZ) { fprintf(stderr,"LSA key isn't REG_MULTI_SZ!!!\n"); return -1; } lsaBuf[dwLsaBuf]='\0'; TCHAR *p = lsaBuf; while(*p) { if(!_tcscmp(p,"setuid")) break; p+=strlen(p)+1; } if(!*p) { strcpy(p,"setuid"); dwLsaBuf+=strlen(p)+1; lsaBuf[dwLsaBuf]='\0'; if(RegSetValueEx(hk,_T("Authentication Packages"),NULL,dwType,(BYTE*)lsaBuf,dwLsaBuf)) { fprintf(stderr,"Couldn't write LSA registry key, error %d\n",GetLastError()); return -1; } } return 0; } if(!strcmp(argv[1],"-unreglsa")) { TCHAR lsaBuf[10240]; DWORD dwLsaBuf; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("SYSTEM\\CurrentControlSet\\Control\\Lsa"),0,KEY_ALL_ACCESS,&hk)) { fprintf(stderr,"Couldn't open LSA registry key, error %d\n",GetLastError()); return -1; } dwLsaBuf=sizeof(lsaBuf); if(RegQueryValueEx(hk,_T("Authentication Packages"),NULL,&dwType,(BYTE*)lsaBuf,&dwLsaBuf)) { fprintf(stderr,"Couldn't read LSA registry key, error %d\n",GetLastError()); return -1; } if(dwType!=REG_MULTI_SZ) { fprintf(stderr,"LSA key isn't REG_MULTI_SZ!!!\n"); return -1; } lsaBuf[dwLsaBuf]='\0'; TCHAR *p = lsaBuf; while(*p) { if(!_tcscmp(p,"setuid")) break; p+=strlen(p)+1; } if(*p) { size_t l = strlen(p)+1; memcpy(p,p+l,(dwLsaBuf-((p+l)-lsaBuf))+1); dwLsaBuf-=l; if(RegSetValueEx(hk,_T("Authentication Packages"),NULL,dwType,(BYTE*)lsaBuf,dwLsaBuf)) { fprintf(stderr,"Couldn't write LSA registry key, error %d\n",GetLastError()); return -1; } } return 0; } if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,_T("Software\\CVS\\Pserver"),NULL,_T(""),REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hk,NULL)) { fprintf(stderr,"Couldn't create HKLM\\Software\\CVS\\Pserver key, error %d\n",GetLastError()); return -1; } if (!strcmp(argv[1],"-v")) { puts(NTSERVICE_VERSION_STRING); return 0; } if(!strcmp(argv[1],"-i")) { if(argc==3) { szRoot = argv[2]; if(GetFileAttributesA(szRoot)==(DWORD)-1) { fprintf(stderr,"Repository directory '%s' not found\n",szRoot); return -1; } dwType=REG_SZ; RegSetValueExA(hk,"Repository0",NULL,dwType,(BYTE*)szRoot,strlen(szRoot)+1); } // connect to the service control manager if((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) { fprintf(stderr,"OpenSCManager Failed\n"); return -1; } if((hService=OpenService(hSCManager,SERVICE_NAME,DELETE))!=NULL) { DeleteService(hService); CloseServiceHandle(hService); } GetModuleFileName(NULL,szImagePath,MAX_PATH); if ((hService = CreateService(hSCManager,SERVICE_NAME,DISPLAY_NAME, STANDARD_RIGHTS_REQUIRED|SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szImagePath, NULL, NULL, NULL, NULL, NULL)) == NULL) { fprintf(stderr,"CreateService Failed: %s\n",GetErrorString()); return -1; } { BOOL (WINAPI *pChangeServiceConfig2)(SC_HANDLE,DWORD,LPVOID); pChangeServiceConfig2=(BOOL (WINAPI *)(SC_HANDLE,DWORD,LPVOID))GetProcAddress(GetModuleHandle("advapi32"),"ChangeServiceConfig2A"); if(pChangeServiceConfig2) { SERVICE_DESCRIPTION sd = { NTSERVICE_VERSION_STRING }; if(!pChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,&sd)) { 0; } } } CloseServiceHandle(hService); CloseServiceHandle(hSCManager); ReportError(FALSE,DISPLAY_NAMEA " installed successfully"); printf(DISPLAY_NAMEA " installed successfully\n"); } RegCloseKey(hk); if(!strcmp(argv[1],"-u")) { // connect to the service control manager if((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) { fprintf(stderr,"OpenSCManager Failed\n"); return -1; } if((hService=OpenService(hSCManager,SERVICE_NAME,DELETE))==NULL) { fprintf(stderr,"OpenService Failed: %s\n",GetErrorString()); return -1; } if(!DeleteService(hService)) { fprintf(stderr,"DeleteService Failed: %s\n",GetErrorString()); return -1; } CloseServiceHandle(hService); CloseServiceHandle(hSCManager); ReportError(FALSE,DISPLAY_NAMEA " uninstalled successfully"); printf(DISPLAY_NAMEA " uninstalled successfully\n"); } else if(!strcmp(argv[1],"-test")) { ServiceMain(999,NULL); } return 0; }
//*================================================================================= //*原型: void TServer::RunService(LPCTSTR pszServiceName, //* LPCTSTR pszDisplayName, LPSTR lpCmdLine) //*功能: 服务器入口 //*参数: pszServiceName -- 服务器名称 //* pszDisplayName -- 服务器显示名称 //* lpCmdLine -- 命令行 //*返回: 无 //*说明: WINNT服务器基类 //*================================================================================= void TServer::RunService(LPCTSTR pszServiceName, LPCTSTR pszDisplayName, LPSTR lpCmdLine) { //检测操作系统版本 CheckOSVersion(); m_pszServiceName = pszServiceName ; m_pszDisplayName = pszDisplayName ; bDebug = TRUE; //add by lina 20050311 //帮助 if( !lstrcmp(lpCmdLine, "help") ) { PrintHelp(); return ; } //调试模式(仅供现场测试及内部测试用) else if( !lstrcmp(lpCmdLine, "debug") ) { bDebug = TRUE ; } //安装为服务器 else if( !lstrcmp(lpCmdLine, "install") ) { InstallService(m_pszServiceName, m_pszDisplayName); return ; } //删除服务器 else if( !lstrcmp(lpCmdLine, "delete") ) { DeleteService(m_pszServiceName); return ; } //Start服务器 else if( !lstrcmp(lpCmdLine, "start") ) { StartServer(m_pszServiceName); return ; } //停止服务器 else if( !lstrcmp(lpCmdLine, "stop") ) { WriteLog("停止服务器!!!!........\n"); printf("停止服务器!!!!........\n"); //StopServer(m_pszServiceName); EndService(m_pszServiceName); return ; } CheckInstance(); /*SECURITY_ATTRIBUTES sa; SECURITY_DESCRIPTOR sd; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = &sd; if( InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) ) { SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE); } m_hShutdown = CreateEvent(&sa, FALSE, FALSE, "__SMART_SERVER_SHUTDOWN__"); if( m_hShutdown == NULL ) { throw TException("Call CreateEvent() Faild!", GetLastError()); }*/ if( bDebug ) { SetConsole(); ServiceMain(0, NULL); } else { SERVICE_TABLE_ENTRY DispatchTable[] = { {(LPTSTR)m_pszServiceName, ServiceMain}, {NULL, NULL} }; //先试着从服务管理器中启动, 如失败, 则用无窗口的方式启动 if( !StartServiceCtrlDispatcher(DispatchTable)) { /* bDebug = TRUE; ServiceMain(0, NULL); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); */ } } }