Exemple #1
0
//!
//!
//!
//! @param[in] startService
//! @param[in] env pointer to the AXIS2 environment structure
//!
//! @return
//!
//! @pre
//!
//! @note
//!
adb_StartServiceResponse_t *StartServiceMarshal(adb_StartService_t * startService, const axutil_env_t * env)
{
    adb_StartServiceResponse_t *ret = NULL;
    adb_startServiceResponseType_t *adbresp = NULL;
    adb_startServiceType_t *adbinput = NULL;

    int rc, i;
    axis2_bool_t status = AXIS2_TRUE;
    char statusMessage[256];
    ncMetadata ccMeta;

    adbinput = adb_StartService_get_StartService(startService, env);
    adbresp = adb_startServiceResponseType_create(env);

    // unmarshal eucalyptusMessage into ccMeta
    EUCA_MESSAGE_UNMARSHAL(startServiceType, adbinput, (&ccMeta));

    // set the fields that are simply carried through between input and output messages
    adb_startServiceResponseType_set_correlationId(adbresp, env, adb_startServiceType_get_correlationId(adbinput, env));
    adb_startServiceResponseType_set_userId(adbresp, env, adb_startServiceType_get_userId(adbinput, env));
    for (i = 0; i < adb_startServiceType_sizeof_serviceIds(adbinput, env); i++) {
        adb_startServiceResponseType_add_serviceIds(adbresp, env, adb_startServiceType_get_serviceIds_at(adbinput, env, i));
    }

    status = AXIS2_TRUE;
    rc = doStartService(&ccMeta);
    if (rc) {
        logprintf("ERROR: doStartService() returned FAIL\n");
        status = AXIS2_FALSE;
        snprintf(statusMessage, 255, "ERROR");
    }

    adb_startServiceResponseType_set_return(adbresp, env, status);
    if (status == AXIS2_FALSE) {
        adb_startServiceResponseType_set_statusMessage(adbresp, env, statusMessage);
    }

    ret = adb_StartServiceResponse_create(env);
    adb_StartServiceResponse_set_StartServiceResponse(ret, env, adbresp);

    return (ret);
}
void startService(const char* szName, std::vector<std::string> &args)
{
	gcAssert(szName);
	gcWString wName(szName);

	SC_HANDLE scm, Service;
	SERVICE_STATUS ssStatus; 
	DWORD dwWaitTime;
	
	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	//open service
	Service = OpenService(scm, wName.c_str(), SERVICE_START|SERVICE_QUERY_STATUS);
	if (!Service)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName));
	}

	// Check the status until the service is no longer start pending. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}

	while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
	{
		gcSleep(1000);
	}

	if (ssStatus.dwCurrentState != SERVICE_STOPPED) 
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: Service {0} is not stopped [{1}]", szName, ssStatus.dwCurrentState));
	}

	BOOL res = doStartService(Service, szName, args);

	if (res == 0)
	{
		if (GetLastError() == 1058)
			throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service {0} as the service is disabled. Please use msconfig and renable service.", szName));

		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: {0}", szName));
	}

	// Check the status until the service is no longer start pending. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}
		
	DWORD totalTime = 0;

	while (ssStatus.dwCurrentState != SERVICE_RUNNING) 
	{ 
		if (ssStatus.dwCurrentState == SERVICE_STOPPED)
		{
			//start service
			BOOL res = doStartService(Service, szName, args);

			if (res == 0)
				throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to start service: {0}", szName));
		}

		dwWaitTime = 1000;
		totalTime += dwWaitTime;

		gcSleep( dwWaitTime );

		// Check the status again. 
		if (!QueryServiceStatus( Service, &ssStatus) )
		{
			CloseServiceHandle(Service);
			CloseServiceHandle(scm);
			throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
		}

		if (totalTime > 30000)
		{
			CloseServiceHandle(Service);
			CloseServiceHandle(scm);
			throw gcException(ERR_SERVICE, gcString("Service {0} Startup timed out after 30 seconds.", szName));
		}
	}
		
	// Check the status again. 
	if (!QueryServiceStatus( Service, &ssStatus) )
	{
		CloseServiceHandle(Service);
		CloseServiceHandle(scm);
		throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to Query service: {0}", szName));
	}

	CloseServiceHandle(scm);
	CloseServiceHandle(Service); 

	if (ssStatus.dwCurrentState != SERVICE_RUNNING) 
		throw gcException(ERR_SERVICE, gcString("Failed to start service: {0} [{1}]", szName, ssStatus.dwCurrentState));
}