Пример #1
0
int main(int argc, char* argv[])
{
   int iCode = 1;
   BOOL bRun = FALSE;
   CNTService ntService (SHTTPD_SERVICE_NAME);

   if (ntService.IsInstalled()) {
       // uninstall or run
       bRun = TRUE;
       if (argc > 1)
           if (_stricmp(argv[1], "-u") == 0) {
               bRun = FALSE;
               if (ntService.Uninstall())
                   iCode = 0;
           }
   } else {
       // install before run
       if (ntService.Install()) bRun = TRUE;
   }

   if (bRun) {
       if (ntService.StartService())
           iCode = 0;
       else if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
           printf("Service installed; to start it, type this command: net start %s\n\n",
                   ntService.m_szServiceName );
           iCode = 0;
       }
   }

   return iCode;
}
/*
* CNTService::Handler --> Service Handler Function
* Function that processes command messages from the service manager. 
*/
void CNTService::Handler (DWORD dwOpcode) {
    Log("Entering CNTService::Handler\n");
    CNTService *pService = m_pThis;
    BOOL err;

    if ((dwOpcode == SERVICE_CONTROL_STOP) || (dwOpcode == SERVICE_CONTROL_SHUTDOWN)) {
        // Stop the web server when you recieve the stop command
        StopGemfireServer();

        SC_HANDLE hSCM =::OpenSCManagerA (NULL, NULL, SC_MANAGER_ALL_ACCESS);
        if (!hSCM)
            return;

        SC_HANDLE hService =::OpenServiceA (hSCM, (LPCSTR) pService->m_szServiceName, SC_MANAGER_ALL_ACCESS);
        if (hService){
            pService->SetStatus (SERVICE_STOP_PENDING);
            pService->m_bIsRunning = FALSE;
            err = ControlService (hService, SERVICE_CONTROL_STOP, &pService->m_Status);
            pService->SetStatus (SERVICE_CONTROL_STOP);
            CloseServiceHandle (hService);
            CloseServiceHandle (hSCM);
        }
    }
    ::SetServiceStatus (pService->m_hServiceStatus, &pService->m_Status);
    Log("Exiting CNTService::Handler\n");
}
Пример #3
0
void CNTService::Handler(DWORD dwOpcode)
{
    CNTService* pService = m_pThis;
    
    if ((dwOpcode == SERVICE_CONTROL_STOP) ||
        (dwOpcode == SERVICE_CONTROL_SHUTDOWN))
    {
        pService->SetStatus(SERVICE_STOP_PENDING);
        pService->m_bIsRunning = FALSE; // SERVICE_STOPPED set when Run returns in ServiceMain (above)
    }
    ::SetServiceStatus(pService->m_hServiceStatus, &pService->m_Status);
}
Пример #4
0
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    CNTService* pService = m_pThis;
    
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName, Handler);
    if (pService->m_hServiceStatus != NULL) {
        if (pService->Initialize()) {
            pService->m_bIsRunning = TRUE;
            pService->m_Status.dwWin32ExitCode = 0;
            pService->m_Status.dwCheckPoint = 0;
            pService->m_Status.dwWaitHint = 0;
            pService->Run();
        }
        pService->SetStatus(SERVICE_STOPPED);
    }
}
Пример #5
0
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    // Get a pointer to the C++ object
    CNTService* pService = m_pThis;

    pService->DebugMsg("Entering CNTService::ServiceMain()");
    // Register the control request handler
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandlerA(pService->m_szServiceName,
                                                           Handler);
    if (pService->m_hServiceStatus == NULL) {
        pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_CTRLHANDLERNOTINSTALLED);
        return;
    }

    // Start the initialisation
    if (pService->Initialize()) {

        // Do the real work. 
        // When the Run function returns, the service has stopped.
        pService->m_bIsRunning = TRUE;
        pService->m_Status.dwWin32ExitCode = 0;
        pService->m_Status.dwCheckPoint = 0;
        pService->m_Status.dwWaitHint = 0;
        pService->Run();
    }

    // Tell the service manager we are stopped
    pService->SetStatus(SERVICE_STOPPED);

    pService->DebugMsg("Leaving CNTService::ServiceMain()");
}
Пример #6
0
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
    // Get a pointer to the C++ object
    CNTService* pService = m_pThis;
    
    pService->DebugMsg(_("Entering CNTService::ServiceMain()"));

    // Register the control request handler
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName,
                                                           Handler);
    if ( pService->m_hServiceStatus == NULL ) {
        pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_CTRLHANDLERNOTINSTALLED);
        return;
    }

    // Start the initialisation
    if ( pService->Initialize() )  {
        // Do the real work. 
        // When the Run function returns, the service has stopped.
        pService->m_bIsRunning = true;
        pService->m_Status.dwWin32ExitCode = 0;
        pService->m_Status.dwCheckPoint = 0;
        pService->m_Status.dwWaitHint = 0;
        pService->Run();
    }


	// Now wait for threads to exit.
	DWORD dwWaitRes;
    if ( ( dwWaitRes = WaitForMultipleObjects( MAX_THREADS, ghThreads, TRUE, 3000 ) )
					== WAIT_OBJECT_0 ) {
		// This is OK! Nothing more to do.....
		;
	}
    else if ( ( dwWaitRes == WAIT_FAILED ) || ( dwWaitRes == WAIT_ABANDONED ) ) {
		// We failed to kill the threads we have to
		// abort anyway but we tell the world that we do so....
		pService->DebugMsg(_("Failed to terminate all threads in CNTService::ServiceMain()"));
	}    

	// close the event handle and the thread handle
	CloseHandle( ghStopEvent );
	for (int i=0; i<MAX_THREADS; i++ ) {
		if ( ghThreads[i] ) {
			CloseHandle( ghThreads[i] );	 
		}
	}

    // Tell the service manager we are stopped
    pService->SetStatus( SERVICE_STOPPED );

    pService->DebugMsg(_("Leaving CNTService::ServiceMain()"));
}
Пример #7
0
// static member function (callback)
void CNTService::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
#ifdef _DEBUG
    cout << "in NTService ServiceMain" << endl;
#endif
    // Get a pointer to the C++ object
    CNTService* pService = m_pThis;
    
    // Register the control request handler
    pService->m_Status.dwCurrentState = SERVICE_START_PENDING;
    pService->m_hServiceStatus = RegisterServiceCtrlHandler(pService->m_szServiceName,
                                                           Handler);
    if (pService->m_hServiceStatus == NULL) {
        pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_CTRLHANDLERNOTINSTALLED);
#ifdef _DEBUG
    cout << "NTService:could not register control request handler" << endl;
#endif
        return;
    }

    // Start the initialisation
    if (pService->Initialize()) {

#ifdef _DEBUG
    cout << "NTService: Initialization was successful - ready to go" << endl;
#endif
        // Do the real work. 
        // When the Run function returns, the service has stopped.
        pService->m_bIsRunning = TRUE;
        pService->m_Status.dwWin32ExitCode = 0;
        pService->m_Status.dwCheckPoint = 0;
        pService->m_Status.dwWaitHint = 0;
        pService->Run();
    }

#ifdef _DEBUG
    cout << "NTService: Initialize failed!" << endl;
#endif

    // Tell the service manager we are stopped
    pService->SetStatus(SERVICE_STOPPED);

}
Пример #8
0
// static member function (callback) to handle commands from the
// service control manager
void CNTService::Handler(DWORD dwOpcode)
{
    // Get a pointer to the object
    CNTService* pService = m_pThis;
    
    pService->DebugMsg("CNTService::Handler(%lu)", dwOpcode);
    switch (dwOpcode) {
    case SERVICE_CONTROL_STOP: // 1
        pService->SetStatus(SERVICE_STOP_PENDING);
        pService->OnStop();
        pService->m_bIsRunning = FALSE;
        pService->LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_STOPPED);
        break;

    case SERVICE_CONTROL_PAUSE: // 2
        pService->OnPause();
        break;

    case SERVICE_CONTROL_CONTINUE: // 3
        pService->OnContinue();
        break;

    case SERVICE_CONTROL_INTERROGATE: // 4
        pService->OnInterrogate();
        break;

    case SERVICE_CONTROL_SHUTDOWN: // 5
        pService->OnShutdown();
        break;

    default:
        if (dwOpcode >= SERVICE_CONTROL_USER) {
            if (!pService->OnUserControl(dwOpcode)) {
                pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_BADREQUEST);
            }
        } else {
            pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_BADREQUEST);
        }
        break;
    }

    // Report current status
    pService->DebugMsg("Updating status (%lu, %lu)",
                       pService->m_hServiceStatus,
                       pService->m_Status.dwCurrentState);
    ::SetServiceStatus(pService->m_hServiceStatus, &pService->m_Status);
}
Пример #9
0
//	PURPOSE:  This function is called by the SCM whenever ControlService() is called on this service.  The
//		SCM does not start the service through this function.
void WINAPI CNTService::service_ctrl(DWORD dwCtrlCode) // static
{
	if ( dwCtrlCode == SERVICE_CONTROL_STOP )
		g_Service.ServiceStop();
	g_Service.SetServiceStatus(g_Service.m_sStatus.dwCurrentState, NO_ERROR, 0);
}
Пример #10
0
/////////////////////////////////////////////////////////////////////////////////////
//
//	FUNCTION: main()
//
/////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);

	TCHAR	*argv[32];
	argv[0] = NULL;
	int argc = Str_ParseCmds(lpCmdLine, &argv[1], COUNTOF(argv)-1, " \t") + 1;

	if ( GRAY_GetOSInfo()->dwPlatformId != VER_PLATFORM_WIN32_NT )
	{
		// We are running Win9x - So we are not an NT service.
do_not_nt_service:
		NTWindow_Init(hInstance, lpCmdLine, nCmdShow);
		int iRet = Sphere_MainEntryPoint(argc, argv);
		NTWindow_Exit();
		TerminateProcess(GetCurrentProcess(), iRet);
		return iRet;
	}

	// We need to find out what the server name is....look it up in the .ini file
	if ( !g_Cfg.LoadIni(true) )
	{
		// Try to determine the name and path of this application.
		char szPath[_MAX_PATH];

		GetModuleFileName(NULL, szPath, sizeof(szPath));

		if ( !szPath[0] )
			return -2;

		ExtractPath(szPath);
		g_Cfg.LoadIni(false);
	}

	if ( !g_Cfg.m_fUseNTService )	// since there is no way to detect how did we start, use config for that
		goto do_not_nt_service;

	g_Service.SetServiceStatus(SERVICE_START_PENDING, NO_ERROR, 5000);

	// process the command line arguments...
	if (( argc > 1 ) && _IS_SWITCH(*argv[1]) )
	{
		if ( argv[1][1] == 'k' )		// service control
		{
			if ( argc < 3 )
			{
				printf("Use \"-k command\" with operation to proceed (install/remove)\n");
			}
			else if ( !strcmp(argv[2], "install") )
			{
				g_Service.CmdInstallService();
			}
			else if ( !strcmp(argv[2], "remove") )
			{
				g_Service.CmdRemoveService();
			}
			return 0;
		}
	}

	// If the argument does not match any of the above parameters, the Service Control Manager (SCM) may
	// be attempting to start the service, so we must call StartServiceCtrlDispatcher.
	g_Service.ReportEvent(EVENTLOG_INFORMATION_TYPE, 0, "Starting Service.");

	g_Service.CmdMainStart();
	g_Service.SetServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
	return -1;
}
Пример #11
0
//	PURPOSE: is called by the SCM, and takes care of some initialization and calls ServiceStart().
void WINAPI CNTService::service_main(DWORD dwArgc, LPTSTR *lpszArgv) // static
{
	g_Service.ServiceStartMain(dwArgc, lpszArgv);
}
Пример #12
0
// static member function (callback) to handle commands from the
// service control manager
void CNTService::Handler(DWORD dwOpcode)
{
#ifdef _DEBUG
  cout << "NTService: handling service control manager command:";
#endif
    // Get a pointer to the object
    CNTService* pService = m_pThis;
    
    switch (dwOpcode) {
    case SERVICE_CONTROL_STOP: // 1
#ifdef _DEBUG
      cout << "stop" << endl;
#endif
        pService->SetStatus(SERVICE_STOP_PENDING);
        pService->OnStop();
        pService->m_bIsRunning = FALSE;
        pService->LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_STOPPED);
        break;

    case SERVICE_CONTROL_PAUSE: // 2
#ifdef _DEBUG
      cout << "pause" << endl;
#endif
        pService->OnPause();
        break;

    case SERVICE_CONTROL_CONTINUE: // 3
#ifdef _DEBUG
      cout << "continue" << endl;
#endif
        pService->OnContinue();
        break;

    case SERVICE_CONTROL_INTERROGATE: // 4
#ifdef _DEBUG
      cout << "interrogate" << endl;
#endif
        pService->OnInterrogate();
        break;

    case SERVICE_CONTROL_SHUTDOWN: // 5
#ifdef _DEBUG
      cout << "shutdown" << endl;
#endif
        pService->OnShutdown();
        break;

    default:
        if (dwOpcode >= SERVICE_CONTROL_USER) {
#ifdef _DEBUG
      cout << "user control" << endl;
#endif
            if (!pService->OnUserControl(dwOpcode)) {
                pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_BADREQUEST);
            }
        } else {
#ifdef _DEBUG
      cout << "unknown" << endl;
#endif
            pService->LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_BADREQUEST);
        }
        break;
    }

    // Report current status
    ::SetServiceStatus(pService->m_hServiceStatus, &pService->m_Status);
}