GuestProcessImpl.cpp revision cd4a988686e583349e3326d6316d68f1657b8f34
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * VirtualBox Main - Guest process handling.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Copyright (C) 2012-2013 Oracle Corporation
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * available from http://www.virtualbox.org. This file is free software;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * you can redistribute it and/or modify it under the terms of the GNU
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * General Public License (GPL) as published by the Free Software
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Locking rules:
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * - When the main dispatcher (callbackDispatcher) is called it takes the
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * WriteLock while dispatching to the various on* methods.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * - All other outer functions (accessible by Main) must not own a lock
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * while waiting for a callback or for an event.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * - Only keep Read/WriteLocks as short as possible and only when necessary.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/*******************************************************************************
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync* Header Files *
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync*******************************************************************************/
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync virtual ~GuestProcessTask(void) { }
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync const ComObjPtr<GuestProcess> &Process(void) const { return mProcess; }
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncclass GuestProcessStartTask : public GuestProcessTask
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Internal listener class to serve events in an
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * active manner, e.g. without polling delays.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int rc2 = mProcess->signalWaitEvents(aType, aEvent);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Signalling events of type=%ld, process=%p resulted in rc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsynctypedef ListenerImpl<GuestProcessListener, GuestProcess*> GuestProcessListenerImpl;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync// constructor / destructor
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/////////////////////////////////////////////////////////////////////////////
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync// public initializer/uninitializer for internal purposes only
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/////////////////////////////////////////////////////////////////////////////
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::init(Console *aConsole, GuestSession *aSession,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ULONG aProcessID, const GuestProcessStartupInfo &aProcInfo)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("aConsole=%p, aSession=%p, aProcessID=%RU32\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Enclose the state transition NotReady->InInit->Ready. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = bindToSession(aConsole, aSession, aProcessID /* Object ID */);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = mEventSource->init(static_cast<IGuestProcess*>(this));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync GuestProcessListener *pListener = new GuestProcessListener();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Everything else will be set by the actual starting routine. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Confirm a successful initialization when it's the case. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Uninitializes the instance.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Called from FinalRelease().
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Enclose the state transition Ready->InUninit->NotReady. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Terminate process if not already done yet. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = terminateProcess(30 * 1000, &guestRc); /** @todo Make timeouts configurable. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Note: Don't return here yet; first uninit all other stuff in
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * case of failure. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Returning rc=%Rrc, guestRc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync// implementation of public getters/setters for attributes
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/////////////////////////////////////////////////////////////////////////////
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(Arguments)(ComSafeArrayOut(BSTR, aArguments))
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync com::SafeArray<BSTR> collection(mData.mProcess.mArguments.size());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync for (ProcessArguments::const_iterator it = mData.mProcess.mArguments.begin();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync collection.detachTo(ComSafeArrayOutArg(aArguments));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(Environment)(ComSafeArrayOut(BSTR, aEnvironment))
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync com::SafeArray<BSTR> arguments(mData.mProcess.mEnvironment.Size());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync arguments.detachTo(ComSafeArrayOutArg(aEnvironment));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(EventSource)(IEventSource ** aEventSource)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync // no need to lock - lifetime constant
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(ExecutablePath)(BSTR *aExecutablePath)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(ExitCode)(LONG *aExitCode)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(Name)(BSTR *aName)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(PID)(ULONG *aPID)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::COMGETTER(Status)(ProcessStatus_T *aStatus)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync// private methods
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/////////////////////////////////////////////////////////////////////////////
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uPID=%RU32, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mPID, pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Silently ignore not implemented functions. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Checks if the current assigned PID matches another PID (from a callback).
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * In protocol v1 we don't have the possibility to terminate/kill
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * processes so it can happen that a formerly started process A
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * (which has the context ID 0 (session=0, process=0, count=0) will
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * send a delayed message to the host if this process has already
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * been discarded there and the same context ID was reused by
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * a process B. Process B in turn then has a different guest PID.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * @return IPRT status code.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * @param uPID PID to check.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Was there a PID assigned yet? */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Simply ignore the stale requests. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* This should never happen! */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertReleaseMsg(mData.mPID == uPID, ("Unterminated guest process (guest PID %RU32) sent data to a newly started process (host PID %RU32)\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/* static */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncUtf8Str GuestProcess::guestErrorToString(int guestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync case VERR_FILE_NOT_FOUND: /* This is the most likely error. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The specified file was not found on guest"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("VMM device is not available (is the VM running?)"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The guest execution service is not available"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("Could not resolve path to specified file was not found on guest"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The specified file is not an executable format on guest"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The specified user was not able to logon on guest"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The specified file is an invalid name"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The guest did not respond within time"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The execution operation was canceled"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("Invalid user/password credentials"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("Maximum number of concurrent guest processes has been reached"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync case VERR_NOT_EQUAL: /** @todo Imprecise to the user; can mean anything and all. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("Unable to retrieve requested information"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync strError += Utf8StrFmt(tr("The guest execution service is not ready (yet)"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return true;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return false;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = setProcessStatus(ProcessStatus_Down, VINF_SUCCESS);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pCallback is optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pSvcCb->mpaParms[0] always contains the context ID. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uStatus);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[4].getUInt32(&dataCb.uProcessed);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RI32, cbProcessed=%RU32\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync dataCb.uPID, dataCb.uStatus, dataCb.uFlags, dataCb.uProcessed));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ProcessInputStatus_T inputStatus = ProcessInputStatus_Undefined;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Fall through is intentional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsg(!dataCb.uProcessed, ("Processed data is not 0 in undefined input state\n"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync fireGuestProcessInputNotifyEvent(mEventSource, mSession, this,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mPID, 0 /* StdIn */, dataCb.uProcessed, inputStatus);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pSvcCb->mpaParms[0] always contains the context ID. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uStatus);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RU32\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ProcessStatus_T procStatus = ProcessStatus_Undefined;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mPID = dataCb.uPID; /* Set the process PID. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mExitCode = dataCb.uFlags; /* Contains the exit code. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mExitCode = dataCb.uFlags; /* Contains the signal. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync procRc = dataCb.uFlags; /* mFlags contains the IPRT error sent from the guest. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Silently skip this request. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Got rc=%Rrc, procSts=%ld, procRc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Set the process status. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pSvcCb->mpaParms[0] always contains the context ID. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uHandle);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pSvcCbData->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RI32, pvData=%p, cbData=%RU32\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync dataCb.uPID, dataCb.uHandle, dataCb.uFlags, dataCb.pvData, dataCb.cbData));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync data.initFrom((BYTE*)dataCb.pvData, dataCb.cbData);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync fireGuestProcessOutputEvent(mEventSource, mSession, this,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mPID, dataCb.uHandle, dataCb.cbData, ComSafeArrayAsInParam(data));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync void *pvData, size_t cbData, uint32_t *pcbRead, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%RU32, pGuestRc=%p\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mPID, uHandle, uSize, uTimeoutMS, pvData, cbData, pGuestRc));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertReturn(cbData >= uSize, VERR_INVALID_PARAMETER);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pcbRead is optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Skip reading if the process wasn't started with the appropriate
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * flags. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdOut))
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdErr))
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return VINF_SUCCESS; /* Nothing to read anymore. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * On Guest Additions < 4.3 there is no guarantee that the process status
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * change arrives *after* the output event, e.g. if this was the last output
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * block being read and the process will report status "terminate".
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * So just skip checking for process status change and only wait for the
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * output event.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setUInt32(0 /* Flags, none set yet. */);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync alock.release(); /* Drop the write lock before sending. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = sendCommand(HOST_EXEC_GET_OUTPUT, i, paParms);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/* Does not do locking; caller is responsible for that! */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::setProcessStatus(ProcessStatus_T procStatus, int procRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("oldStatus=%ld, newStatus=%ld, procRc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsg(RT_FAILURE(procRc), ("Guest rc must be an error (%Rrc)\n", procRc));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Do not allow overwriting an already set error. If this happens
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * this means we forgot some error checking/locking somewhere. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync //AssertMsg(RT_SUCCESS(mData.mRC), ("Guest rc already set (to %Rrc)\n", mData.mRC));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsg(RT_SUCCESS(procRc), ("Guest rc must not be an error (%Rrc)\n", procRc));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (mData.mStatus != procStatus) /* Was there a process status change? */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int rc2 = errorInfo->initEx(VBOX_E_IPRT_ERROR, mData.mRC,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync fireGuestProcessStateChangedEvent(mEventSource, mSession, this,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/* static */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncHRESULT GuestProcess::setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return pInterface->setError(VBOX_E_IPRT_ERROR, GuestProcess::guestErrorToString(guestRc).c_str());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::startProcess(uint32_t uTimeoutMS, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uTimeoutMS=%RU32, procCmd=%s, procTimeoutMS=%RU32, procFlags=%x\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync uTimeoutMS, mData.mProcess.mCommand.c_str(), mData.mProcess.mTimeoutMS, mData.mProcess.mFlags));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Wait until the caller function (if kicked off by a thread)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * has returned and continue operation. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync const GuestCredentials &sessionCreds = pSession->getCredentials();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Prepare arguments. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync char **papszArgv = (char**)RTMemAlloc((cArgs + 1) * sizeof(char*));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync for (size_t i = 0; i < cArgs && RT_SUCCESS(vrc); i++)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync const char *pszCurArg = mData.mProcess.mArguments[i].c_str();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = RTGetOptArgvToString(&pszArgs, papszArgv, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Calculate arguments size (in bytes). */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Prepare environment. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = mData.mProcess.mEnvironment.BuildEnvironmentBlock(&pvEnv, &cbEnv, NULL /* cEnv */);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync uint32_t uProtocol = mSession->getProtocolVersion();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Prepare HGCM call. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setPointer((void*)mData.mProcess.mCommand.c_str(),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setUInt32((uint32_t)mData.mProcess.mArguments.size());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setPointer((void*)pszArgs, (uint32_t)cbArgs);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setUInt32((uint32_t)mData.mProcess.mEnvironment.Size());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setPointer((void*)pvEnv, (uint32_t)cbEnv);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* In protocol v1 (VBox < 4.3) the credentials were part of the execution
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * call. In newer protocols these credentials are part of the opened guest
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * session, so not needed anymore here. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setPointer((void*)sessionCreds.mUser.c_str(), (ULONG)sessionCreds.mUser.length() + 1);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setPointer((void*)sessionCreds.mPassword.c_str(), (ULONG)sessionCreds.mPassword.length() + 1);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * If the WaitForProcessStartOnly flag is set, we only want to define and wait for a timeout
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * until the process was started - the process itself then gets an infinite timeout for execution.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * This is handy when we want to start a process inside a worker thread within a certain timeout
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * but let the started process perform lengthly operations then.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (mData.mProcess.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setUInt32(UINT32_MAX /* Infinite timeout */);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* CPU affinity: We only support one CPU affinity block at the moment,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * so that makes up to 64 CPUs total. This can be more in the future. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* The actual CPU affinity blocks. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync paParms[i++].setPointer((void*)&mData.mProcess.mAffinity, sizeof(mData.mProcess.mAffinity));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync alock.release(); /* Drop the write lock before sending. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int rc2 = setProcessStatus(ProcessStatus_Error, vrc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Asynchronously start the process on the guest by kicking off a
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * worker thread. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync std::auto_ptr<GuestProcessStartTask> pTask(new GuestProcessStartTask(this));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = RTThreadCreate(NULL, GuestProcess::startProcessThread,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync "gctlPrcStart");
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pTask is now owned by startProcessThread(), so release it. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/* static */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncDECLCALLBACK(int) GuestProcess::startProcessThread(RTTHREAD Thread, void *pvUser)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync std::auto_ptr<GuestProcessStartTask> pTask(static_cast<GuestProcessStartTask*>(pvUser));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync const ComObjPtr<GuestProcess> pProcess(pTask->Process());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = pProcess->startProcess(30 * 1000 /* 30s timeout */,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Nothing to do here anymore. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::terminateProcess(uint32_t uTimeoutMS, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pGuestRc is optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uTimeoutMS=%RU32\n", uTimeoutMS));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Process not started (yet), nothing to terminate\n"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return VINF_SUCCESS; /* Nothing to do (anymore). */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Note: VBox < 4.3 (aka protocol version 1) does not
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * support this, so just skip. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync alock.release(); /* Drop the write lock before sending. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = sendCommand(HOST_EXEC_TERMINATE, i, paParms);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/* static */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncProcessWaitResult_T GuestProcess::waitFlagsToResultEx(uint32_t fWaitFlags,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ProcessWaitResult_T waitResult = ProcessWaitResult_None;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Handled above. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * If ProcessCreateFlag_WaitForProcessStartOnly was specified on process creation the
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * caller is not interested in getting further process statuses -- so just don't notify
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * anything here anymore and return.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (uProcFlags & ProcessCreateFlag_WaitForProcessStartOnly)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* No result available yet. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsgFailed(("Unhandled process status %ld\n", procStatus));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* No result available yet. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsgFailed(("Unhandled process status %ld\n", procStatus));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Filter out waits which are *not* supported using
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * older guest control Guest Additions.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ** @todo ProcessWaitForFlag_Std* flags are not implemented yet.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* We don't support waiting for stdin, out + err,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * just skip waiting then. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Use _WaitFlagNotSupported because we don't know what to tell the caller. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync waitResult = ProcessWaitResult_WaitFlagNotSupported;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncProcessWaitResult_T GuestProcess::waitFlagsToResult(uint32_t fWaitFlags)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return GuestProcess::waitFlagsToResultEx(fWaitFlags, mData.mStatus, mData.mProcess.mFlags,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS, ProcessWaitResult_T &waitResult, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("fWaitFlags=0x%x, uTimeoutMS=%RU32, mStatus=%RU32, pGuestRc=%p\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Did some error occur before? Then skip waiting and return. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertMsg(RT_FAILURE(mData.mRC), ("No error rc (%Rrc) set when guest process indicated an error\n", mData.mRC));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync *pGuestRc = mData.mRC; /* Return last set error. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("procStatus=%ld, procRc=%Rrc, waitResult=%ld\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* No waiting needed? Return immediately using the last set error. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync *pGuestRc = mData.mRC; /* Return last set error (if any). */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return RT_SUCCESS(mData.mRC) ? VINF_SUCCESS : VERR_GSTCTL_GUEST_ERROR;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync alock.release(); /* Release lock before waiting. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Do the actual waiting.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ProcessStatus_T processStatus = ProcessStatus_Undefined;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync uint64_t u32ElapsedMS = RTTimeMilliTS() - u64StartMS;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync waitResult = waitFlagsToResultEx(fWaitFlags, processStatus,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mProcess.mFlags, mSession->getProtocolVersion());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Got new status change: waitResult=%ld, processStatus=%ld\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (ProcessWaitResult_None != waitResult) /* We got a waiting result. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync else /* Waiting failed, bail out. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("waitResult=%ld, processStatus=%ld, rc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (evtType == VBoxEventType_OnGuestProcessInputNotify)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ComPtr<IGuestProcessInputNotifyEvent> pProcessEvent = pIEvent;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync HRESULT hr2 = pProcessEvent->COMGETTER(Status)(pInputStatus);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync HRESULT hr2 = pProcessEvent->COMGETTER(Processed)((ULONG*)pcbProcessed);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Returning pEvent=%p, uHandle=%RU32, rc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ComPtr<IGuestProcessOutputEvent> pProcessEvent = pIEvent;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync HRESULT hr = pProcessEvent->COMGETTER(Handle)(&uHandleEvent);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = pProcessEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Copy data from event into our buffer. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pProcessStatus is optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pGuestRc is optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync Assert(evtType == VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ComPtr<IGuestProcessStateChangedEvent> pProcessEvent = pIEvent;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = pProcessEvent->COMGETTER(Status)(pProcessStatus);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = pProcessEvent->COMGETTER(Error)(errorInfo.asOutParam());
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/* static */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncbool GuestProcess::waitResultImpliesEx(ProcessWaitResult_T waitResult,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync fImplies = ( procStatus == ProcessStatus_TerminatedNormally
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync || procStatus == ProcessStatus_TerminatedAbnormally
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcess::writeData(uint32_t uHandle, uint32_t uFlags,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync void *pvData, size_t cbData, uint32_t uTimeoutMS, uint32_t *puWritten, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RU32, pvData=%p, cbData=%RU32, uTimeoutMS=%RU32, puWritten=%p, pGuestRc=%p\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync mData.mPID, uHandle, uFlags, pvData, cbData, uTimeoutMS, puWritten, pGuestRc));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* All is optional. There can be 0 byte writes. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return VINF_SUCCESS; /* Not available for writing (anymore). */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * On Guest Additions < 4.3 there is no guarantee that the process status
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * change arrives *after* the input event, e.g. if this was the last input
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * block being written and the process will report status "terminate".
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * So just skip checking for process status change and only wait for the
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * input event.
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync alock.release(); /* Drop the write lock before sending. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = sendCommand(HOST_EXEC_SET_INPUT, i, paParms);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = waitForInputNotify(pEvent, uHandle, uTimeoutMS,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /** @todo Set guestRc. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /** @todo Error handling. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Returning cbProcessed=%RU32, rc=%Rrc\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync// implementation of public methods
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync/////////////////////////////////////////////////////////////////////////////
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::Read(ULONG aHandle, ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return setError(E_INVALIDARG, tr("The size to read is zero"));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = readData(aHandle, aToRead, aTimeoutMS, data.raw(), aToRead, &cbRead, &guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = GuestProcess::setErrorExternal(this, guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync tr("Reading from process \"%s\" (PID %RU32) failed: %Rrc"),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("rc=%Rrc, cbRead=%RU32\n", vrc, cbRead));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = terminateProcess(30 * 1000 /* Timeout in ms */,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = GuestProcess::setErrorExternal(this, guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync tr("Terminating process \"%s\" (PID %RU32) not supported by installed Guest Additions"),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync tr("Terminating process \"%s\" (PID %RU32) failed: %Rrc"),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Remove the process from our internal session list. Only an API client
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * now may hold references to it. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::WaitFor(ULONG aWaitFlags, ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Note: Do not hold any locks here while waiting!
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = waitFor(aWaitFlags, aTimeoutMS, waitResult, &guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = GuestProcess::setErrorExternal(this, guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync tr("Waiting for process \"%s\" (PID %RU32) failed: %Rrc"),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::WaitForArray(ComSafeArrayIn(ProcessWaitForFlag_T, aFlags), ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Note: Do not hold any locks here while waiting!
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync com::SafeArray<ProcessWaitForFlag_T> flags(ComSafeArrayInArg(aFlags));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::Write(ULONG aHandle, ULONG aFlags,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = writeData(aHandle, aFlags, data.raw(), data.size(), aTimeoutMS, &cbWritten, &guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync hr = GuestProcess::setErrorExternal(this, guestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync tr("Writing to process \"%s\" (PID %RU32) failed: %Rrc"),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, cbWritten));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncSTDMETHODIMP GuestProcess::WriteArray(ULONG aHandle, ComSafeArrayIn(ProcessInputFlag_T, aFlags),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (FAILED(autoCaller.rc())) return autoCaller.rc();
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * Note: Do not hold any locks here while writing!
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync com::SafeArray<ProcessInputFlag_T> flags(ComSafeArrayInArg(aFlags));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return Write(aHandle, fWrite, ComSafeArrayInArg(aData), aTimeoutMS, aWritten);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync#endif /* VBOX_WITH_GUEST_CONTROL */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync///////////////////////////////////////////////////////////////////////////////
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcessTool::Init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("pGuestSession=%p, szCmd=%s, fAsync=%RTbool\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync pGuestSession, startupInfo.mCommand.c_str(), fAsync));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertPtrReturn(pGuestSession, VERR_INVALID_POINTER);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Make sure the process is hidden. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync int vrc = pSession->processCreateExInteral(mStartupInfo, pProcess);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = fAsync ? pProcess->startProcessAsync() : pProcess->startProcess(30 * 1000 /* 30s timeout */,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcessTool::GetCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Try parsing the data to see if the current block is complete. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync ProcessStatus_T procStatus = ProcessStatus_Undefined;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync HRESULT hr = pProcess->COMGETTER(Status(&procStatus));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return false;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return true;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* pExitCode is optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync HRESULT hr = pProcess->COMGETTER(ExitCode(&exitCode));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return VERR_NOT_EQUAL; /** @todo Special guest control rc needed! */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return VERR_INVALID_STATE; /** @todo Special guest control rc needed! */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcessTool::Wait(uint32_t fFlags, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync return WaitEx(fFlags, NULL /* pStreamBlock */, pGuestRc);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcessTool::WaitEx(uint32_t fFlags, GuestProcessStreamBlock *pStreamBlock, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("pSession=%p, fFlags=0x%x, pStreamBlock=%p, pGuestRc=%p\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Other parameters are optional. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Can we parse the next block without waiting? */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = GetCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, *pStreamBlock);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Do the waiting. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync uint32_t fWaitFlags = ProcessWaitForFlag_Terminate;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdErr)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /** @todo Decrease timeout. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync bool fDone = false;
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Since waiting for stdout / stderr is not supported by the guest,
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync * wait a bit to not hog the CPU too much when polling for data. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync /* Not used here, just skip. */
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync AssertReleaseMsgFailed(("Unhandled process wait result %ld\n", waitRes));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Received %RU64 bytes from stdout\n", cbRead));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = GetCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, *pStreamBlock);
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync vrc = pProcess->readData(OUTPUT_HANDLE_ID_STDERR, sizeof(byBuf),
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Received %RU64 bytes from stderr\n", cbRead));
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync LogFlowThisFunc(("Loop ended with rc=%Rrc, guestRc=%Rrc, waitRes=%ld\n",
f9a51917495bc8ba8b60632219652a7b122c1190vboxsyncint GuestProcessTool::Terminate(uint32_t uTimeoutMS, int *pGuestRc)
f9a51917495bc8ba8b60632219652a7b122c1190vboxsync rc = pProcess->terminateProcess(uTimeoutMS, pGuestRc);