Exemplo n.º 1
0
// may need to ignore STILL_ACTIVE (error code 259) here
// http://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx
DWORD execute(const BOOL wait) {
	STARTUPINFO si;
    memset(&pi, 0, sizeof(pi));
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);

	DWORD dwExitCode = -1;
	char cmdline[MAX_ARGS];
    strcpy(cmdline, "\"");
	strcat(cmdline, cmd);
	strcat(cmdline, "\" ");
	strcat(cmdline, args);
	if (CreateProcess(NULL, cmdline, NULL, NULL,
			TRUE, priority, NULL, NULL, &si, &pi)) {
		if (wait) {
			WaitForSingleObject(pi.hProcess, INFINITE);
			GetExitCodeProcess(pi.hProcess, &dwExitCode);
			debug("Exit code:\t%d\n", dwExitCode);
			closeHandles();
		} else {
			dwExitCode = 0;
		}
	}
	return dwExitCode;
}
// syncrhonized
void InitiationDispatcher::close(void)
{
    BOOST_LOG_TRIVIAL(trace) << "InitiationDispatcher entering close.";

    lock_guard<mutex> grd_lock(m_mutex);

    closeHandles();
    m_is_closed = true;
}
int CExecCommand::execWait( LPCTSTR lpstrCommand, LPCTSTR lpstrPath, BOOL bCapture)
{
	ASSERT( lpstrCommand);

	try
	{
		initialize();
		if (bCapture)
		{
			// Initialize environnement for capturing stdout and stderr
			// then start process
			if (!startProcessCapture( lpstrCommand, lpstrPath))
			{
				closeHandles();
				return EXEC_ERROR_START_COMMAND;
			}
		}
		else
		{
			// Just start process
			if (realCreateProcess( lpstrCommand, lpstrPath) == 0) 
			{
				closeHandles();
				return EXEC_ERROR_START_COMMAND;
			}
		}
		// Wait for process ending, capturing stdout/stderr if needed
		if (!wait( bCapture))
		{
			closeHandles();
			return EXEC_ERROR_WAIT_COMMAND;
		}
		closeHandles();
		return EXEC_SUCCESSFULL;
	}
	catch( CException *pEx)
	{
		pEx->Delete();
		closeHandles();
		m_csOutput = "Unhandled exception Error";
		return EXEC_ERROR_START_COMMAND;
	}
}
Exemplo n.º 4
0
void
HippoHTTPContext::enqueueError(HRESULT error)
{
    closeHandles();

    EnterCriticalSection(&criticalSection_);
    responseError_ = error;
    responseState_ = RESPONSE_STATE_ERROR;
    ensureResponseIdle();
    LeaveCriticalSection(&criticalSection_);
}
int CExecCommand::execNoWait( LPCTSTR lpstrCommand, LPCTSTR lpstrPath)
{
	try
	{
		ASSERT( lpstrCommand);
		
		// Start process
		initialize();
		if (realCreateProcess( lpstrCommand, lpstrPath) == 0) 
		{
			closeHandles();
			return EXEC_ERROR_START_COMMAND;
		}
		closeHandles();
		return EXEC_SUCCESSFULL;
	}
	catch (CException *pEx)
	{
		pEx->Delete();
		closeHandles();
		m_csOutput = "Unhandled exception Error";
		return EXEC_ERROR_START_COMMAND;
	}
}
Exemplo n.º 6
0
    void reset()
    {
	while ( !stdinBuf.isEmpty() ) {
	    delete stdinBuf.dequeue();
	}
	closeHandles();
	stdinBufRead = 0;
	pipeStdin[0] = 0;
	pipeStdin[1] = 0;
	pipeStdout[0] = 0;
	pipeStdout[1] = 0;
	pipeStderr[0] = 0;
	pipeStderr[1] = 0;
	exitValuesCalculated = false;

	deletePid();
    }
Exemplo n.º 7
0
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow) {

	int result = prepare(lpCmdLine);
	debug("prepare() result %d\n", result);

	if (result == ERROR_ALREADY_EXISTS) {
		HWND handle = getInstanceWindow();
		ShowWindow(handle, SW_SHOW);
		SetForegroundWindow(handle);
		closeLogFile();
		return 2;
	}

	if (result != TRUE) {
		signalError();
		return 1;
	}

	splash = loadBool(SHOW_SPLASH)
			&& strstr(lpCmdLine, "--l4j-no-splash") == NULL;
	stayAlive = loadBool(GUI_HEADER_STAYS_ALIVE)
			&& strstr(lpCmdLine, "--l4j-dont-wait") == NULL;
	if (splash || stayAlive) {
		hWnd = CreateWindowEx(WS_EX_TOOLWINDOW, "STATIC", "",
				WS_POPUP | SS_BITMAP,
				0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
		if (splash) {
			char timeout[10] = {0};
			if (loadString(SPLASH_TIMEOUT, timeout)) {
				splashTimeout = atoi(timeout);
				if (splashTimeout <= 0 || splashTimeout > MAX_SPLASH_TIMEOUT) {
					splashTimeout = DEFAULT_SPLASH_TIMEOUT;
				}
			}
			splashTimeoutErr = loadBool(SPLASH_TIMEOUT_ERR)
					&& strstr(lpCmdLine, "--l4j-no-splash-err") == NULL;
			waitForWindow = loadBool(SPLASH_WAITS_FOR_WINDOW);
			HANDLE hImage = LoadImage(hInstance,	// handle of the instance containing the image
					MAKEINTRESOURCE(SPLASH_BITMAP),	// name or identifier of image
					IMAGE_BITMAP,					// type of image
					0,								// desired width
					0,								// desired height
					LR_DEFAULTSIZE);
			if (hImage == NULL) {
				debug("hImage was NULL signalError(), exiting\n");
				signalError();
				return 1;
			}
			SendMessage(hWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImage);
			RECT rect;
			GetWindowRect(hWnd, &rect);
			int x = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
			int y = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
			SetWindowPos(hWnd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
			ShowWindow(hWnd, nCmdShow);

			UpdateWindow (hWnd);
		}
		if (!SetTimer (hWnd, ID_TIMER, 1000 /* 1s */, TimerProc)) {
			debug("SetTimer() signalError(), exiting\n");
			signalError();
			return 1;
		}
	}
	if (execute(FALSE) == -1) {
		debug("execute(FALSE) signalError(), exiting\n");
		signalError();
		return 1;
	}
	if (!(splash || stayAlive)) {
		debug("Exit code:\t0\n");
		closeHandles();
		return 0;
	}

	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	debug("Exit code:\t%d\n", dwExitCode);
	closeHandles();
	return dwExitCode;
}
NTStatsServer::~NTStatsServer(void)
{
    closeHandles();
}
Exemplo n.º 9
0
void
HippoHTTPContext::readData()
{
    if (!requestOpenHandle_) {
        hippoDebugLogW(L"readData called with closed handle");
        return;
    }

    while (responseSize_ == -1 || (responseSize_ - responseBytesRead_) != 0) {
        DWORD bytesRead;

        // Things seem to work badly if we call neglect to call InternetQueryDataAvailable()
        // before trying to read
        DWORD bytesAvailable = 0;
        if (!InternetQueryDataAvailable(requestOpenHandle_, &bytesAvailable, 0, 0)) {
            // ERROR_IO_PENDING means no data currently available; return and we'll get
            // called again later
            if (GetLastError() != ERROR_IO_PENDING)
                enqueueError(GetLastError());

            return;
        }

        EnterCriticalSection(&criticalSection_);

        DWORD toRead;
        if (responseSize_ > 0) {
            toRead = responseSize_ - responseBytesRead_;
        } else {
            toRead = 4096;
        }
        void *readLocation;
        HRESULT allocResult;

        if (toRead > bytesAvailable)
            toRead = bytesAvailable;

        if (((long)toRead + responseBytesRead_) > responseBufferSize_) {
            responseBuffer_ = realloc(responseBuffer_, responseBufferSize_ *= 2);
            allocResult = GetLastError();
        }

        readLocation = ((char*)responseBuffer_) + responseBytesRead_;

        LeaveCriticalSection(&criticalSection_);

        if (responseBuffer_ == NULL) {
            enqueueError(allocResult);
            return;
        }

        if (!InternetReadFile(requestOpenHandle_,
                              readLocation, toRead, &bytesRead))
        {
            // ERROR_IO_PENDING really shouldn't happen here since
            // InternetQueryDataAvailable told us there was data available
            // before.
            if (GetLastError() != ERROR_IO_PENDING)
                enqueueError(GetLastError());

            return;
        } else {
            EnterCriticalSection(&criticalSection_);
            char *current = (char*)responseBuffer_;
            responseBytesRead_ += bytesRead;
            ensureResponseIdle();
            LeaveCriticalSection(&criticalSection_);

            if (bytesRead == 0)
                break;
        }
    }

    closeHandles();

    EnterCriticalSection(&criticalSection_);
    if (responseSize_ < 0 || responseSize_ - responseBytesRead_ != 0) {
        // Missing or invalid Content-Length
        responseSize_ = responseBytesRead_;
    }
    responseState_ = HippoHTTPContext::RESPONSE_STATE_DONE;
    ensureResponseIdle();
    LeaveCriticalSection(&criticalSection_);

    return;
}
int CExecCommand::execWaitForAllChilds( LPCTSTR lpstrCommand, LPCTSTR lpstrPath)
{
	CObArray		myProcessList;
	CProcessProps	*pProcess;
	DWORD			dwExitCode,
					dwProcessID,
					dwTime = 0;

	try
	{
		ASSERT( lpstrCommand);

		// Start process
		initialize();
		if ((dwProcessID = realCreateProcess( lpstrCommand, lpstrPath)) == 0) 
		{
			closeHandles();
			return EXEC_ERROR_START_COMMAND;
		}
		// We need high priority on OS to follow thread/process created by main command
		SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS);
		// Store first process
		pProcess = new CProcessProps();
		pProcess->set( dwProcessID, GetCurrentProcessId(), lpstrCommand);
		myProcessList.Add( pProcess);
		// While there is running processes or timeout not reached
		while ((myProcessList.GetCount() > 0) && (dwTime < m_dwTimeout))
		{
			// Parse memory processes for new childs process or terminated processes
			if (!parseRunningProcesses( &myProcessList))
			{
				SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
				m_csOutput.Format( "Parse running processes Error: %s", GetAnsiFromUnicode( LookupError( GetLastError())));
				freeProcessList( &myProcessList);
				closeHandles();
				return EXEC_ERROR_WAIT_COMMAND;
			}
			Sleep( EXEC_WAIT_CHECK_LATENCY);
			dwTime += EXEC_WAIT_CHECK_LATENCY;
		}
		freeProcessList( &myProcessList);
		// Now return to normal prioity
		SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
		// Get exit code
		if (GetExitCodeProcess( m_hProcessHandle, &dwExitCode)) 
		{
			m_nExitValue = dwExitCode;
		}
		else
		{
			m_nExitValue = -1;
			m_csOutput.Format( "GetExitCode Error: %s", GetAnsiFromUnicode( LookupError( GetLastError())));
			closeHandles();
			return EXEC_ERROR_WAIT_COMMAND;
		}
		closeHandles();
		return EXEC_SUCCESSFULL;
	}
	catch (CException *pEx)
	{
		pEx->Delete();
		closeHandles();
		m_csOutput = "Unhandled exception Error";
		return EXEC_ERROR_START_COMMAND;
	}
}
CExecCommand::~CExecCommand()
{
	closeHandles();
}