GuestProcessImpl.cpp revision 7b3ab6f5283a363835e3171c0b1a7e8dd7119786
/* $Id$ */
/** @file
* VirtualBox Main - XXX.
*/
/*
* Copyright (C) 2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/**
* Locking rules:
* - When the main dispatcher (callbackDispatcher) is called it takes the
* WriteLock while dispatching to the various on* methods.
* - All other outer functions (accessible by Main) must not own a lock
* while waiting for a callback or for an event.
* - Only keep Read/WriteLocks as short as possible and only when necessary.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "GuestProcessImpl.h"
#include "GuestSessionImpl.h"
#include "GuestCtrlImplPrivate.h"
#include "ConsoleImpl.h"
#include "Global.h"
#include "AutoCaller.h"
#include "VMMDev.h"
#include <memory> /* For auto_ptr. */
#ifdef LOG_GROUP
#endif
#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
class GuestProcessTask
{
public:
mRC(VINF_SUCCESS) { }
virtual ~GuestProcessTask(void) { }
protected:
int mRC;
};
class GuestProcessStartTask : public GuestProcessTask
{
public:
: GuestProcessTask(pProcess) { }
};
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
{
mData.mNextContextID = 0;
mData.mProcessID = 0;
mData.mWaitCount = 0;
return hr;
}
void GuestProcess::FinalRelease(void)
{
uninit();
}
// public initializer/uninitializer for internal purposes only
/////////////////////////////////////////////////////////////////////////////
int GuestProcess::init(Console *aConsole, GuestSession *aSession, ULONG aProcessID, const GuestProcessStartupInfo &aProcInfo)
{
LogFlowThisFunc(("aConsole=%p, aSession=%p, aProcessID=%RU32\n",
/* Enclose the state transition NotReady->InInit->Ready. */
AutoInitSpan autoInitSpan(this);
/* Everything else will be set by the actual starting routine. */
/* Confirm a successful initialization when it's the case. */
return VINF_SUCCESS;
}
/**
* Uninitializes the instance.
* Called from FinalRelease().
*/
void GuestProcess::uninit(void)
{
LogFlowThisFunc(("mCmd=%s, PID=%RU32\n",
/* Enclose the state transition Ready->InUninit->NotReady. */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
int vrc = VINF_SUCCESS;
#ifdef VBOX_WITH_GUEST_CONTROL
/*
* Cancel all callbacks + waiters.
* Note: Deleting them is the job of the caller!
*/
{
if (RT_SUCCESS(vrc))
}
if (mData.mWaitEvent)
{
if (RT_SUCCESS(vrc))
}
#endif
}
/////////////////////////////////////////////////////////////////////////////
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
size_t s = 0;
it++, s++)
{
}
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
{
}
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
return S_OK;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
// private methods
/////////////////////////////////////////////////////////////////////////////
{
ULONG uSessionID = 0;
/* Create a new context ID and assign it. */
int vrc = VERR_NOT_FOUND;
ULONG uNewContextID = 0;
for (;;)
{
if (uCount == VBOX_GUESTCTRL_MAX_CONTEXTS)
uCount = 0;
/* Create a new context ID ... */
/* Is the context ID already used? Try next ID ... */
if (!callbackExists(uCount))
{
/* Callback with context ID was not found. This means
* we can use this context ID for our new callback we want
* to add below. */
vrc = VINF_SUCCESS;
break;
}
uCount++;
if (++uTries == UINT32_MAX)
break; /* Don't try too hard. */
}
if (RT_SUCCESS(vrc))
{
/* Add callback with new context ID to our callback map.
* Note: This is *not* uNewContextID (which also includes
* the session + process ID), just the context count
* will be used here. */
/* Report back new context ID. */
if (puContextID)
LogFlowThisFunc(("Added new callback (Session: %RU32, Process: %RU32, Count=%RU32) CID=%RU32\n",
}
return vrc;
}
int GuestProcess::callbackDispatcher(uint32_t uContextID, uint32_t uFunction, void *pvData, size_t cbData)
{
#ifdef DEBUG
LogFlowThisFunc(("uPID=%RU32, uContextID=%RU32, uFunction=%RU32, pvData=%p, cbData=%RU32\n",
#endif
int vrc;
/* Get the optional callback associated to this context ID.
* The callback may not be around anymore if just kept locally by the caller when
* doing the actual HGCM sending stuff. */
{
#ifdef DEBUG
#endif
}
switch (uFunction)
{
case GUEST_DISCONNECTED:
{
PCALLBACKDATACLIENTDISCONNECTED pCallbackData = reinterpret_cast<PCALLBACKDATACLIENTDISCONNECTED>(pvData);
AssertReturn(CALLBACKDATAMAGIC_CLIENT_DISCONNECTED == pCallbackData->hdr.u32Magic, VERR_INVALID_PARAMETER);
break;
}
case GUEST_EXEC_SEND_STATUS:
{
break;
}
case GUEST_EXEC_SEND_OUTPUT:
{
break;
}
{
AssertReturn(CALLBACKDATAMAGIC_EXEC_IN_STATUS == pCallbackData->hdr.u32Magic, VERR_INVALID_PARAMETER);
break;
}
default:
/* Silently ignore not implemented functions. */
break;
}
#ifdef DEBUG
#endif
return vrc;
}
{
}
{
{
return VINF_SUCCESS;
}
return VERR_NOT_FOUND;
}
/**
* Checks if the current assigned PID matches another PID (from a callback).
*
* processes so it can happen that a formerly start process A
* (which has the context ID 0 (session=0, process=0, count=0) will
* send a delayed message to the host if this process has already
* been discarded there and the same context ID was reused by
* a process B. Process B in turn then has a different guest PID.
*
* @return IPRT status code.
* @param uPID PID to check.
*/
{
/* Was there a PID assigned yet? */
{
/*
*/
{
/* Simply ignore the stale requests. */
}
/* This should never happen! */
AssertReleaseMsg(mData.mPID == uPID, ("Unterminated guest process (PID %RU32) sent data to a newly started process (PID %RU32)\n",
}
return VINF_SUCCESS;
}
inline bool GuestProcess::isAlive(void)
{
}
bool GuestProcess::isReady(void)
{
{
return true;
}
return false;
}
int GuestProcess::onGuestDisconnected(GuestCtrlCallback *pCallback, PCALLBACKDATACLIENTDISCONNECTED pData)
{
/* pCallback is optional. */
/* First, signal callback in every case. */
if (pCallback)
/* Do we need to report a termination? */
else
/* Signal in any case. */
return vrc;
}
int GuestProcess::onProcessInputStatus(GuestCtrlCallback *pCallback, PCALLBACKDATAEXECINSTATUS pData)
{
/* pCallback is optional. */
LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RU32, cbProcessed=%RU32, pCallback=%p, pData=%p\n",
if (RT_FAILURE(vrc))
return vrc;
/* First, signal callback in every case (if available). */
if (pCallback)
{
if (RT_SUCCESS(vrc))
}
/* Then do the WaitFor signalling stuff. */
{
if (RT_SUCCESS(vrc))
}
return vrc;
}
{
/* pCallback is optional. */
return 0;
}
int GuestProcess::onProcessStatusChange(GuestCtrlCallback *pCallback, PCALLBACKDATAEXECSTATUS pData)
{
/* pCallback is optional. */
LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RU32, pCallback=%p, pData=%p\n",
if (RT_FAILURE(vrc))
return vrc;
{
case PROC_STS_STARTED:
{
/* If the caller only wants to wait until the process has been started,
* notify in any case. */
fSignal = true;
break;
}
case PROC_STS_TEN:
{
break;
}
case PROC_STS_TES:
{
break;
}
case PROC_STS_TEA:
{
break;
}
case PROC_STS_TOK:
{
break;
}
case PROC_STS_TOA:
{
break;
}
case PROC_STS_DWN:
{
/* Do we need to report termination? */
else
break;
}
case PROC_STS_ERROR:
{
Utf8Str strError = Utf8StrFmt(tr("Guest process \"%s\" could not be started: "), mData.mProcess.mCommand.c_str());
/* Note: It's not required that the process has been started before. */
{
}
else
{
/** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
{
case VERR_FILE_NOT_FOUND: /* This is the most likely error. */
break;
case VERR_PATH_NOT_FOUND:
break;
case VERR_BAD_EXE_FORMAT:
break;
break;
case VERR_INVALID_NAME:
break;
case VERR_TIMEOUT:
break;
case VERR_CANCELLED:
break;
case VERR_PERMISSION_DENIED:
break;
case VERR_MAX_PROCS_REACHED:
break;
default:
break;
}
}
break;
}
case PROC_STS_UNDEFINED:
default:
{
/* Silently skip this request. */
break;
}
}
/*
* Now do the signalling stuff.
*/
if (pCallback)
if (fSignal)
{
if (RT_SUCCESS(vrc))
}
return vrc;
}
{
/* pCallback is optional. */
LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RU32, pvData=%p, cbData=%RU32, pCallback=%p, pData=%p\n",
if (RT_FAILURE(vrc))
return vrc;
/* First, signal callback in every case (if available). */
if (pCallback)
{
if (RT_SUCCESS(vrc))
}
/* Then do the WaitFor signalling stuff. */
if ( (uWaitFlags & ProcessWaitForFlag_StdOut)
|| (uWaitFlags & ProcessWaitForFlag_StdErr))
{
}
else if ( (uWaitFlags & ProcessWaitForFlag_StdOut)
{
}
else if ( (uWaitFlags & ProcessWaitForFlag_StdErr)
{
}
if (fSignal)
{
int rc2;
else
if (RT_SUCCESS(vrc))
}
return vrc;
}
{
LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%RU32\n",
/* pcbRead is optional. */
{
if (pcbRead)
*pcbRead = 0;
return VINF_SUCCESS; /* Nothing to read anymore. */
}
uint32_t uContextID = 0;
if (!pCallbackRead)
return VERR_NO_MEMORY;
/* Create callback and add it to the map. */
if (RT_SUCCESS(vrc))
if (RT_SUCCESS(vrc))
{
int i = 0;
}
if (RT_SUCCESS(vrc))
{
/*
* Let's wait for the process being started.
*/
{
if (RT_SUCCESS(vrc))
{
if (cbRead)
{
}
if (pcbRead)
}
}
else
vrc = VERR_TIMEOUT;
}
if (RT_SUCCESS(vrc))
return vrc;
}
{
/* Forward the information to the VMM device. */
if (RT_FAILURE(vrc))
{
int rc2;
if (vrc == VERR_INVALID_VM_HANDLE)
else if (vrc == VERR_NOT_FOUND)
else if (vrc == VERR_HGCM_SERVICE_NOT_FOUND)
else
}
return vrc;
}
/* Does not do locking; caller is responsible for that! */
{
#ifdef DEBUG
/* Do not allow overwriting an already set error. If this happens
#endif
return rc2;
}
{
}
{
LogFlowThisFunc(("enmWaitResult=%d, mWaitCount=%RU32, mWaitEvent=%p\n",
/* Note: No write locking here -- already done in the caller. */
int vrc = VINF_SUCCESS;
if (mData.mWaitEvent)
return vrc;
}
int GuestProcess::startProcess(void)
{
LogFlowThisFunc(("aCmd=%s, aTimeoutMS=%RU32, fFlags=%x\n",
/* Wait until the caller function (if kicked off by a thread)
* has returned and continue operation. */
int vrc;
uint32_t uContextID = 0;
if (!pCallbackStart)
return VERR_NO_MEMORY;
/* Create callback and add it to the map. */
if (RT_SUCCESS(vrc))
if (RT_SUCCESS(vrc))
{
/* Prepare arguments. */
if (cArgs)
{
if (RT_SUCCESS(vrc))
}
/* Prepare environment. */
if (RT_SUCCESS(vrc))
{
/* Prepare HGCM call. */
int i = 0;
paParms[i++].setPointer((void*)sessionCreds.mPassword.c_str(), (ULONG)sessionCreds.mPassword.length() + 1);
/** @todo New command needs the domain as well! */
/*
* If the WaitForProcessStartOnly flag is set, we only want to define and wait for a timeout
* until the process was started - the process itself then gets an infinite timeout for execution.
* This is handy when we want to start a process inside a worker thread within a certain timeout
* but let the started process perform lengthly operations then.
*/
else
/* Note: Don't hold the write lock in here, because setErrorInternal */
}
if (pszArgs)
/* Drop the write lock again before waiting. */
if (RT_SUCCESS(vrc))
{
/*
* Let's wait for the process being started.
*/
{
}
else
vrc = VERR_TIMEOUT;
}
if (RT_SUCCESS(vrc))
}
return vrc;
}
int GuestProcess::startProcessAsync(void)
{
int vrc;
try
{
/* Asynchronously start the process on the guest by kicking off a
* worker thread. */
"gctlPrcStart");
if (RT_SUCCESS(vrc))
{
/* pTask is now owned by startProcessThread(), so release it. */
}
}
{
}
return vrc;
}
/* static */
{
if (RT_FAILURE(vrc))
{
/** @todo What now? */
}
return vrc;
}
int GuestProcess::terminateProcess(void)
{
return VERR_NOT_SUPPORTED;
return VERR_NOT_IMPLEMENTED;
}
{
LogFlowThisFunc(("fWaitFlags=%x, uTimeoutMS=%RU32, mStatus=%RU32, mWaitCount=%RU32, mWaitEvent=%p\n",
/* Did some error occur before? Then skip waiting and return. */
if (curStatus == ProcessStatus_Error)
{
return VINF_SUCCESS;
}
if ( (fWaitFlags & ProcessWaitForFlag_Terminate)
|| (fWaitFlags & ProcessWaitForFlag_StdErr))
{
{
case ProcessStatus_Down:
break;
break;
case ProcessStatus_Error:
break;
case ProcessStatus_Started:
{
/* Filter out waits which are *not* supported using
* older guest control Guest Additions. */
{
/* We don't support waiting for stdin, out + err,
* just skip waiting then. */
if ( (fWaitFlags & ProcessWaitForFlag_StdIn)
|| (fWaitFlags & ProcessWaitForFlag_StdErr))
{
/* Use _WaitFlagNotSupported because we don't know what to tell the caller. */
}
}
/*
* If ProcessCreateFlag_WaitForProcessStartOnly was specified on process creation the
* caller is not interested in getting further process statuses -- so just don't notify
* anything here anymore and return.
*/
break;
}
case ProcessStatus_Undefined:
case ProcessStatus_Starting:
/* Do the waiting below. */
break;
default:
return VERR_NOT_IMPLEMENTED;
}
}
else if (fWaitFlags & ProcessWaitForFlag_Start)
{
{
case ProcessStatus_Started:
case ProcessStatus_Paused:
case ProcessStatus_Down:
break;
case ProcessStatus_Error:
break;
break;
case ProcessStatus_Undefined:
case ProcessStatus_Starting:
/* Do the waiting below. */
break;
default:
return VERR_NOT_IMPLEMENTED;
}
}
/* No waiting needed? Return immediately. */
return VINF_SUCCESS;
if (mData.mWaitCount > 0)
return VERR_ALREADY_EXISTS;
mData.mWaitCount++;
if (RT_SUCCESS(vrc))
{
}
else if (vrc == VERR_TIMEOUT)
{
vrc = VINF_SUCCESS;
}
/* Note: The caller always is responsible of deleting the
* stuff it created before. See close() for more information. */
delete mData.mWaitEvent;
mData.mWaitCount--;
return vrc;
}
{
int vrc = VINF_SUCCESS;
{
if ( RT_FAILURE(vrc)
{
if (RT_SUCCESS(vrc))
}
/** @todo More error handling needed. */
}
return vrc;
}
{
LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RU32, pvData=%p, cbData=%RU32, uTimeoutMS=%RU32, puWritten=%p\n",
/* All is optional. There can be 0 byte writes. */
return VINF_SUCCESS; /* Not available for writing (anymore). */
uint32_t uContextID = 0;
if (!pCallbackWrite)
return VERR_NO_MEMORY;
/* Create callback and add it to the map. */
if (RT_SUCCESS(vrc))
if (RT_SUCCESS(vrc))
{
int i = 0;
}
if (RT_SUCCESS(vrc))
{
/*
* Let's wait for the process being started.
*/
{
LogFlowThisFunc(("Callback returned vrc=%Rrc, cbData=%RU32\n", vrc, pCallbackWrite->GetDataSize()));
if (RT_SUCCESS(vrc))
{
{
case INPUT_STS_WRITTEN:
break;
case INPUT_STS_ERROR:
break;
case INPUT_STS_TERMINATED:
break;
case INPUT_STS_OVERFLOW:
break;
default:
/* Silently skip unknown errors. */
break;
}
if (puWritten)
}
}
else
vrc = VERR_TIMEOUT;
}
if (RT_SUCCESS(vrc))
return vrc;
}
// implementation of public methods
/////////////////////////////////////////////////////////////////////////////
STDMETHODIMP GuestProcess::Read(ULONG aHandle, ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
if (aToRead == 0)
AutoCaller autoCaller(this);
if (RT_SUCCESS(vrc))
{
}
/** @todo Do setError() here. */
return hr;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
int vrc = terminateProcess();
if (RT_FAILURE(vrc))
{
switch (vrc)
{
case VERR_NOT_IMPLEMENTED:
break; /* Never reached. */
case VERR_NOT_SUPPORTED:
tr("Terminating process \"%s\" (PID %RU32) not supported by installed Guest Additions"),
break;
default:
tr("Terminating process \"%s\" (PID %RU32) failed: %Rrc"),
break;
}
}
/*
* Release autocaller before calling uninit.
*/
uninit();
return hr;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
STDMETHODIMP GuestProcess::WaitFor(ULONG aWaitFlags, ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
/*
* Note: Do not hold any locks here while waiting!
*/
if (RT_SUCCESS(vrc))
{
hr = setErrorExternal();
}
else
{
tr("Waiting for process \"%s\" (PID %RU32) failed with vrc=%Rrc"),
}
return hr;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
STDMETHODIMP GuestProcess::WaitForArray(ComSafeArrayIn(ProcessWaitForFlag_T, aFlags), ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
/*
* Note: Do not hold any locks here while waiting!
*/
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
/** @todo Do setError() here. */
return hr;
#endif /* VBOX_WITH_GUEST_CONTROL */
}
{
#ifndef VBOX_WITH_GUEST_CONTROL
#else
AutoCaller autoCaller(this);
/*
* Note: Do not hold any locks here while writing!
*/
#endif /* VBOX_WITH_GUEST_CONTROL */
}