static VOID WINAPI ServiceCtrl(DWORD dwCtrlCode) { switch (dwCtrlCode) { case SERVICE_CONTROL_SHUTDOWN: case SERVICE_CONTROL_STOP: { ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, SERVER_STOP_WAIT); /* Signal the server to stop and wait for completion */ SvrStopServer(false); while (SvrInShutdown()) { Sleep(SERVER_STOP_WAIT / 2); ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, SERVER_STOP_WAIT); } ReportStatusToSCMgr(SERVICE_STOPPED, 0, 0); } break; default: ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0); } }
static void WINAPI ServiceMain(DWORD dwArgc, LPTSTR lpszArgv[]) { if ((sshStatusHandle = RegisterServiceCtrlHandler(szServiceName, ServiceCtrl)) != NULL) { ZeroData(ssStatus); ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; if (ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, SERVER_START_WAIT)) { ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0); /* Run server */ int iSvrResult = SvrMain((int) dwArgc, lpszArgv); if (iSvrResult < 0) { AddToMessageLog(ErrGetErrorString(iSvrResult)); } } ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0); } else AddToMessageLog(_T("RegisterServiceCtrlHandler")); }
/*==========================================================================*/ void WINAPI SLPDServiceMain(DWORD argc, LPTSTR *argv) /*==========================================================================*/ { /* register our service control handler: */ sshStatusHandle = RegisterServiceCtrlHandler( G_SERVICENAME, ServiceCtrl); if(sshStatusHandle != 0) { /* SERVICE_STATUS members that don't change */ ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; /* report the status to the service control manager. */ if(ReportStatusToSCMgr(SERVICE_START_PENDING, /* service state */ NO_ERROR, /* exit code */ 3000)) /* wait hint */ { ServiceStart(argc, argv); } } /* try to report the stopped status to the service control manager. */ if(sshStatusHandle) (void)ReportStatusToSCMgr(SERVICE_STOPPED, 0, 0); }
// // FUNCTION: service_ctrl // // PURPOSE: This function is called by the SCM whenever // ControlService() is called on this service. // // PARAMETERS: // dwCtrlCode - type of control requested // // RETURN VALUE: // none // // COMMENTS: // VOID WINAPI service_ctrl(DWORD dwCtrlCode) { // Handle the requested control code. // switch (dwCtrlCode) { // Stop the service. // // SERVICE_STOP_PENDING should be reported before // setting the Stop Event - hServerStopEvent - in // ServiceStop(). This avoids a race condition // which may result in a 1053 - The Service did not respond... // error. case SERVICE_CONTROL_STOP: ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0); ServiceStop(); return; // Update the service status. // case SERVICE_CONTROL_INTERROGATE: break; // invalid control code // default: break; } ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0); }
// // FUNCTION: service_ctrl // // PURPOSE: This function is called by the SCM whenever // ControlService() is called on this service. // // PARAMETERS: // dwCtrlCode - type of control requested // // RETURN VALUE: // none // // COMMENTS: See the user-defined Handler() entry in the PSDK // VOID WINAPI service_ctrl(DWORD dwCtrlCode) { switch(dwCtrlCode) { // Stop the service. // case SERVICE_CONTROL_SHUTDOWN: case SERVICE_CONTROL_STOP: ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, NULL, "Service Stop/Shutdown signaled, shutting down server."); ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 15000); ap_start_shutdown(); break; case SERVICE_APACHE_RESTART: ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, NULL, "Service Restart signaled, shutting down server."); ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 15000); ap_start_restart(1); break; // Update the service status. // case SERVICE_CONTROL_INTERROGATE: ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, NO_ERROR, 0); break; // invalid control code, ignored // default: break; } }
VOID CNdasService::ServiceMain(DWORD dwArgc, LPTSTR* lpArgs) { m_bDebugMode = FALSE; BOOL fSuccess = this->ximeta::CTask::Initialize(); if (!fSuccess) { // TODO: Return appropriate error code for stopped // TODO: Event Log ReportStatusToSCMgr(SERVICE_STOPPED, 0, ::GetLastError()); return; } fSuccess = this->Run(); if (!fSuccess) { // TODO: Return appropriate error code for stopped // TODO: Event Log ReportStatusToSCMgr(SERVICE_STOPPED, 0, ::GetLastError()); return; } DBGPRT_INFO(_FT("NDAS Service is running...\n")); ReportStatusToSCMgr(SERVICE_RUNNING); return; }
int irodsNtStartService() { int LocalStatus; if (!ReportStatusToSCMgr(SERVICE_START_PENDING,NO_ERROR,3000)) { (VOID)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0); return -1; } if (!ReportStatusToSCMgr(SERVICE_RUNNING,NO_ERROR,0)) { (VOID)ReportStatusToSCMgr(SERVICE_STOPPED,dwErr,0); return -1; } /* since we are going to run as a WIndows service. We pause for 1 minutes for TCP/IP service to be ready. */ /* Sleep(60000); */ LocalStatus = irodsWinMain(aArgc,aArgv); fprintf(stderr,"irodsWinMain exit, status = %d\n", LocalStatus); return 0; }
VOID CNdasDDUService::ServiceMain(DWORD dwArgc, LPTSTR* lpArgs) { ReportStatusToSCMgr(SERVICE_START_PENDING, 0, GetLastError()); #ifdef NDAS_DDUS_USE_DEVICE_EVENTS DEV_BROADCAST_DEVICEINTERFACE NotificationFilter = {0}; NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; NotificationFilter.dbcc_classguid = StoragePortClassGuid; #define DEVICE_NOTIFY_ALL_INTERFACE_CLASSES 0x00000004 m_hDevNotify = ::RegisterDeviceNotification( m_sshStatusHandle, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES); _ASSERTE(m_hDevNotify); #endif m_hStopTask = ::CreateEvent(NULL, TRUE, FALSE, NULL); DebugPrint(1, _T("Starting service task thread...\n")); StartTask(); ReportStatusToSCMgr(SERVICE_RUNNING, 0, GetLastError()); }
void WINAPI service_main(DWORD dwArgc, LPSTR *lpszArgv) { // register our service control handler: sshStatusHandle=RegisterServiceCtrlHandler(lpszArgv[0], service_ctrl); if (!sshStatusHandle) goto cleanup; // SERVICE_STATUS members that don't change in example ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; // report the status to the service control manager. if (!ReportStatusToSCMgr( SERVICE_START_PENDING, // service state NO_ERROR, // exit code 3000)) // wait hint goto cleanup; ServiceStart(dwArgc, lpszArgv); cleanup: // try to report the stopped status to the service control manager. // if (sshStatusHandle) (VOID)ReportStatusToSCMgr( SERVICE_STOPPED, dwErr, 0); return; }
/* that the worker thread will signal at its termination. */ static VOID service_main(DWORD dwArgc, LPTSTR * lpszArgv) { DWORD dwWait; /* register our service control handler: */ sshStatusHandle = RegisterServiceCtrlHandler(THIS_SERVICE, service_ctrl); if (!sshStatusHandle) goto cleanup; /* SERVICE_STATUS members that don't change in example */ ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; /* report the status to Service Control Manager. */ if (!ReportStatusToSCMgr(SERVICE_START_PENDING, /* service state */ NO_ERROR, /* exit code */ 1, /* checkpoint */ 3000)) /* wait hint */ goto cleanup; /* start the thread that performs the work of the service. */ threadHandle = (HANDLE) _beginthreadex(NULL, /* security attributes */ 0, /* stack size (0 means inherit parent's stack size) */ (LPTHREAD_START_ROUTINE) worker_thread, NULL, /* argument to thread */ 0, /* thread creation flags */ &TID); /* pointer to thread ID */ if (!threadHandle) goto cleanup; /* report the status to the service control manager. */ if (!ReportStatusToSCMgr(SERVICE_RUNNING, /* service state */ NO_ERROR, /* exit code */ 0, /* checkpoint */ 0)) /* wait hint */ goto cleanup; /* wait indefinitely until threadHandle is signaled. */ /* The thread handle is signalled when the thread terminates */ dwWait = WaitForSingleObject(threadHandle, /* event object */ INFINITE); /* wait indefinitely */ cleanup: /* try to report the stopped status to the service control manager. */ if (sshStatusHandle) (VOID) ReportStatusToSCMgr(SERVICE_STOPPED, dwGlobalErr, 0, 0); /* When SERVICE MAIN FUNCTION returns in a single service */ /* process, the StartServiceCtrlDispatcher function in */ /* the main thread returns, terminating the process. */ return; } /* end of service_main */
/* someone calls ControlService in reference to our service. */ static VOID WINAPI service_ctrl(DWORD dwCtrlCode) { DWORD dwState = SERVICE_RUNNING; /* Handle the requested control code. */ switch (dwCtrlCode) { /* Pause the service if it is running. */ case SERVICE_CONTROL_PAUSE: if (ssStatus.dwCurrentState == SERVICE_RUNNING) { SuspendThread(threadHandle); dwState = SERVICE_PAUSED; } break; /* Resume the paused service. */ case SERVICE_CONTROL_CONTINUE: if (ssStatus.dwCurrentState == SERVICE_PAUSED) { ResumeThread(threadHandle); dwState = SERVICE_RUNNING; } break; /* Stop the service. */ case SERVICE_CONTROL_STOP: dwState = SERVICE_STOP_PENDING; /* Report the status, specifying the checkpoint and waithint, */ /* before setting the termination event. */ ReportStatusToSCMgr(SERVICE_STOP_PENDING, /* current state */ NO_ERROR, /* exit code */ 1, /* checkpoint */ 10000); /* waithint (10 secs) */ shutdown_flag = 1; flag_broadcast(0, 0, T("GAME: Game shutdown by system operator")); return; /* Update the service status. */ case SERVICE_CONTROL_INTERROGATE: break; /* invalid control code */ default: break; } /* end of switch */ /* send a status response. */ ReportStatusToSCMgr(dwState, NO_ERROR, 0, 0); } /* end of service_ctrl */
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 ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv) { HANDLE hEvents[2] = {NULL, NULL}; if(!ReportStatusToSCMgr( SERVICE_START_PENDING, // service state NO_ERROR, // exit code 3000)) // wait hint goto cleanup; hServerStopEvent = CreateEvent( NULL, // no security attributes TRUE, // manual reset event FALSE, // not-signalled NULL); // no name if( hServerStopEvent == NULL) goto cleanup; hEvents[0] = hServerStopEvent; if(!ReportStatusToSCMgr( SERVICE_START_PENDING, // service state NO_ERROR, // exit code 3000)) // wait hint goto cleanup; hEvents[1] = CreateEvent( NULL, // no security attributes TRUE, // manual reset event FALSE, // not-signalled NULL); // no name if( hEvents[1] == NULL) goto cleanup; if(!ReportStatusToSCMgr( SERVICE_RUNNING, // service state NO_ERROR, // exit code 0)) // wait hint goto cleanup; mainloop(); cleanup: if(hServerStopEvent) CloseHandle(hServerStopEvent); if(hEvents[1]) // overlapped i/o event CloseHandle(hEvents[1]); }
DWORD CService:: OnServiceInterrogate() { (VOID) ReportStatusToSCMgr(m_ssStatus.dwCurrentState); return NO_ERROR; }
// // FUNCTION: service_ctrl // // PURPOSE: This function is called by the SCM whenever // ControlService() is called on this service. // // PARAMETERS: // dwCtrlCode - type of control requested // // RETURN VALUE: // none // VOID WINAPI service_ctrl(DWORD dwCtrlCode) { // Handle the requested control code. // switch(dwCtrlCode) { // Stop the service. // case SERVICE_CONTROL_STOP: ssStatus.dwCurrentState = SERVICE_STOP_PENDING; ServiceStop(); break; // Update the service status. // case SERVICE_CONTROL_INTERROGATE: break; // invalid control code // default: break; } ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0); }
// // FUNCTION: service_ctrl // // PURPOSE: This function is called by the SCM whenever // ControlService() is called on this service. // // PARAMETERS: // dwCtrlCode - type of control requested // // RETURN VALUE: // none // // COMMENTS: // VOID WINAPI service_ctrl(DWORD dwCtrlCode) { int state; state = globdat.ssStatus.dwCurrentState; switch(dwCtrlCode) { // Stop the service. // case SERVICE_CONTROL_STOP: state = SERVICE_STOP_PENDING; ap_start_shutdown(); break; // Update the service status. // case SERVICE_CONTROL_INTERROGATE: break; // invalid control code // default: break; } ReportStatusToSCMgr(state, NO_ERROR, 0); }
DWORD CNdasDDUService::OnServiceStop() { (VOID) ReportStatusToSCMgr(SERVICE_STOP_PENDING); ::SetEvent(m_hStopTask); return NO_ERROR; }
VOID service_main (DWORD dwArgc, LPTSTR * lpszArgv) { DWORD dwWait; sshStatusHandle = RegisterServiceCtrlHandler (TEXT ("Antinat"), (LPHANDLER_FUNCTION) service_ctrl); if (!sshStatusHandle) goto cleanup; ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; if (!ReportStatusToSCMgr (SERVICE_START_PENDING, NO_ERROR, 1, 1000)) goto cleanup; hServDoneEvent = CreateEvent (NULL, TRUE, FALSE, NULL); if (hServDoneEvent == (HANDLE) NULL) goto cleanup; if (!ReportStatusToSCMgr (SERVICE_START_PENDING, NO_ERROR, 2, 1000)) goto cleanup; os_thread_init (&threadHandle); if (!os_thread_exec (&threadHandle, worker_thread, NULL)) goto cleanup; if (!ReportStatusToSCMgr (SERVICE_RUNNING, NO_ERROR, 0, 0)) goto cleanup; dwWait = WaitForSingleObject (hServDoneEvent, INFINITE); cleanup: if (hServDoneEvent != NULL) CloseHandle (hServDoneEvent); if (sshStatusHandle != 0) (VOID) ReportStatusToSCMgr (SERVICE_STOPPED, 0, 0, 0); return; }
void irodsNtServiceMain( DWORD dwArgc, LPTSTR *lpszArgv ) { ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; sshStatusHandle = RegisterServiceCtrlHandler( irods_service_name, service_ctrl ); if ( !sshStatusHandle ) { return; } if ( !ReportStatusToSCMgr( SERVICE_START_PENDING, NO_ERROR, 3000 ) ) { ( VOID )ReportStatusToSCMgr( SERVICE_STOPPED, dwErr, 0 ); return; } ( VOID )irodsNtStartService(); }
/*--------------------------------------------------------------------------*/ void ServiceStop() /*--------------------------------------------------------------------------*/ { G_SIGTERM = 1; ReportStatusToSCMgr(SERVICE_STOPPED, /* service state */ NO_ERROR, /* exit code */ 3000); /* wait hint */ }
void open_log_files() { const char dfile[] = "nfsddbg.log"; const char efile[] = "nfsderr.log"; const char mode[] = "w"; if (g_debug_level > 0) { dlog_file = fopen(dfile, mode); if (dlog_file == NULL) { ReportStatusToSCMgr(SERVICE_STOPPED, GetLastError(), 0); exit (GetLastError()); } } elog_file = fopen(efile, mode); if (elog_file == NULL) { ReportStatusToSCMgr(SERVICE_STOPPED, GetLastError(), 0); exit (GetLastError()); } }
VOID service_ctrl (DWORD dwCtrlCode) { DWORD dwState = SERVICE_RUNNING; switch (dwCtrlCode) { case SERVICE_CONTROL_STOP: dwState = SERVICE_STOP_PENDING; ReportStatusToSCMgr (SERVICE_STOP_PENDING, NO_ERROR, 1, 3000); SetEvent (hServDoneEvent); return; case SERVICE_CONTROL_INTERROGATE: break; default: break; } ReportStatusToSCMgr (dwState, NO_ERROR, 0, 0); }
void CNTService::SServiceCtrl(DWORD dwCtrlCode) { // Handle the requested control code. switch(dwCtrlCode) { // Stop the service. // case SERVICE_CONTROL_STOP: // Update the service status. ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0); ServiceStop(); return; case SERVICE_CONTROL_INTERROGATE: // invalid control code break; default: break; } ReportStatusToSCMgr(m_ServiceStatus.dwCurrentState, NO_ERROR, 0); }
VOID WINAPI ServiceStop() { debug(D_NOTICE, "ServiceStop"); bStop=TRUE; disconnect_pipe(); unmount(); ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 0); storeKey(KEY_DELETE, ""); }
VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv) { debug(D_NOTICE, "Called service main %d .", bStop); if (bStop) return; sshStatusHandle = RegisterServiceCtrlHandler(SZSERVICENAME, service_ctrl); if (sshStatusHandle) { ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; if (ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000)) ServiceStart(NULL); ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0); storeKey(KEY_DELETE, ""); } }
DWORD CNdasService::OnServiceShutdown() { DBGPRT_INFO(_FT("System is shutting down...\n")); ReportStatusToSCMgr(SERVICE_STOP_PENDING, 1000); CNdasLogicalDeviceManager* pLogDevMan = pGetNdasLogicalDeviceManager(); pLogDevMan->OnShutdown(); ReportStatusToSCMgr(SERVICE_STOP_PENDING, 1000); (VOID) ::LfsFiltCtlShutdown(); // TODO: Do I have to stop here? this->Stop(); return NO_ERROR; }
VOID WINAPI service_ctrl(DWORD dwCtrlCode) { switch(dwCtrlCode) { case SERVICE_CONTROL_STOP: storeKey(KEY_DELETE, "+"); debug(D_NOTICE, "SERVICE_CONTROL_STOP"); ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0); ServiceStop(); return; case SERVICE_CONTROL_SHUTDOWN: debug(D_NOTICE, "SERVICE_CONTROL_SHUTDOWN"); ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 2000); ServiceStop(); return; default: break; } ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0); }
void shutdown_checkpoint(void) { static DWORD checkpoint = 1; if (threadHandle && shutdown_flag) ReportStatusToSCMgr(SERVICE_STOP_PENDING, /* current state */ NO_ERROR, /* exit code */ ++checkpoint, /* checkpoint */ 3000); /* waithint (3 seconds) */ } /* end of shutdown_checkpoint */
// // FUNCTION: service_main // // PURPOSE: To perform actual initialization of the service // // PARAMETERS: // dwArgc - number of command line arguments // lpszArgv - array of command line arguments // // RETURN VALUE: // none // // COMMENTS: // This routine performs the service initialization and then calls // the user defined ServiceStart() routine to perform majority // of the work. // void CNTService::SServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) { // register our service control handler: m_ServiceStatusHandle = RegisterServiceCtrlHandlerA( m_ServiceName.GetBuffer(), ServiceCtrl); if (m_ServiceStatusHandle) { m_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; m_ServiceStatus.dwServiceSpecificExitCode = 0; // report the status to the service control manager. if (ReportStatusToSCMgr( SERVICE_START_PENDING, // service state NO_ERROR, // exit code 3000)) {// wait hint ServiceStart(); } } // try to report the stopped status to the service control manager. if (m_ServiceStatusHandle) ReportStatusToSCMgr(SERVICE_STOPPED, 0, 0); }
void __stdcall service_main_fn(DWORD argc, char **argv) { if(!(globdat.hServiceStatus = RegisterServiceCtrlHandler( globdat.name, service_ctrl))) { globdat.exit_status = -1; return; } ReportStatusToSCMgr( SERVICE_START_PENDING, // service state NO_ERROR, // exit code 3000); // wait hint globdat.exit_status = (*globdat.main_fn)( argc, argv ); ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 0); return; }