VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestProcessImpl.cpp@ 49629

Last change on this file since 49629 was 49610, checked in by vboxsync, 11 years ago

Main/GuestCtrl: Don't keep ComObjPtr of parents in internal listeners.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.0 KB
Line 
1/* $Id: GuestProcessImpl.cpp 49610 2013-11-21 16:01:00Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest process handling.
4 */
5
6/*
7 * Copyright (C) 2012-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/**
19 * Locking rules:
20 * - When the main dispatcher (callbackDispatcher) is called it takes the
21 * WriteLock while dispatching to the various on* methods.
22 * - All other outer functions (accessible by Main) must not own a lock
23 * while waiting for a callback or for an event.
24 * - Only keep Read/WriteLocks as short as possible and only when necessary.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "GuestProcessImpl.h"
31#include "GuestSessionImpl.h"
32#include "GuestCtrlImplPrivate.h"
33#include "ConsoleImpl.h"
34#include "VirtualBoxErrorInfoImpl.h"
35
36#include "Global.h"
37#include "AutoCaller.h"
38#include "VBoxEvents.h"
39
40#include <memory> /* For auto_ptr. */
41
42#include <iprt/asm.h>
43#include <iprt/cpp/utils.h> /* For unconst(). */
44#include <iprt/getopt.h>
45
46#include <VBox/com/listeners.h>
47
48#include <VBox/com/array.h>
49
50#ifdef LOG_GROUP
51 #undef LOG_GROUP
52#endif
53#define LOG_GROUP LOG_GROUP_GUEST_CONTROL
54#include <VBox/log.h>
55
56
57class GuestProcessTask
58{
59public:
60
61 GuestProcessTask(GuestProcess *pProcess)
62 : mProcess(pProcess),
63 mRC(VINF_SUCCESS) { }
64
65 virtual ~GuestProcessTask(void) { }
66
67 int rc(void) const { return mRC; }
68 bool isOk(void) const { return RT_SUCCESS(mRC); }
69 const ComObjPtr<GuestProcess> &Process(void) const { return mProcess; }
70
71protected:
72
73 const ComObjPtr<GuestProcess> mProcess;
74 int mRC;
75};
76
77class GuestProcessStartTask : public GuestProcessTask
78{
79public:
80
81 GuestProcessStartTask(GuestProcess *pProcess)
82 : GuestProcessTask(pProcess) { }
83};
84
85/**
86 * Internal listener class to serve events in an
87 * active manner, e.g. without polling delays.
88 */
89class GuestProcessListener
90{
91public:
92
93 GuestProcessListener(void)
94 {
95 }
96
97 HRESULT init(GuestProcess *pProcess)
98 {
99 AssertPtrReturn(pProcess, E_POINTER);
100 mProcess = pProcess;
101 return S_OK;
102 }
103
104 void uninit(void)
105 {
106 mProcess = NULL;
107 }
108
109 STDMETHOD(HandleEvent)(VBoxEventType_T aType, IEvent *aEvent)
110 {
111 switch (aType)
112 {
113 case VBoxEventType_OnGuestProcessStateChanged:
114 case VBoxEventType_OnGuestProcessInputNotify:
115 case VBoxEventType_OnGuestProcessOutput:
116 {
117 AssertPtrReturn(mProcess, E_POINTER);
118 int rc2 = mProcess->signalWaitEvent(aType, aEvent);
119#ifdef DEBUG
120 LogFlowThisFunc(("Signalling events of type=%RU32, pProcess=%p resulted in rc=%Rrc\n",
121 aType, &mProcess, rc2));
122#endif
123 break;
124 }
125
126 default:
127 AssertMsgFailed(("Unhandled event %RU32\n", aType));
128 break;
129 }
130
131 return S_OK;
132 }
133
134private:
135
136 GuestProcess *mProcess;
137};
138typedef ListenerImpl<GuestProcessListener, GuestProcess*> GuestProcessListenerImpl;
139
140VBOX_LISTENER_DECLARE(GuestProcessListenerImpl)
141
142// constructor / destructor
143/////////////////////////////////////////////////////////////////////////////
144
145DEFINE_EMPTY_CTOR_DTOR(GuestProcess)
146
147HRESULT GuestProcess::FinalConstruct(void)
148{
149 LogFlowThisFuncEnter();
150 return BaseFinalConstruct();
151}
152
153void GuestProcess::FinalRelease(void)
154{
155 LogFlowThisFuncEnter();
156 uninit();
157 BaseFinalRelease();
158 LogFlowThisFuncLeave();
159}
160
161// public initializer/uninitializer for internal purposes only
162/////////////////////////////////////////////////////////////////////////////
163
164int GuestProcess::init(Console *aConsole, GuestSession *aSession,
165 ULONG aProcessID, const GuestProcessStartupInfo &aProcInfo)
166{
167 LogFlowThisFunc(("aConsole=%p, aSession=%p, aProcessID=%RU32\n",
168 aConsole, aSession, aProcessID));
169
170 AssertPtrReturn(aConsole, VERR_INVALID_POINTER);
171 AssertPtrReturn(aSession, VERR_INVALID_POINTER);
172
173 /* Enclose the state transition NotReady->InInit->Ready. */
174 AutoInitSpan autoInitSpan(this);
175 AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
176
177#ifndef VBOX_WITH_GUEST_CONTROL
178 autoInitSpan.setSucceeded();
179 return VINF_SUCCESS;
180#else
181 HRESULT hr;
182
183 int vrc = bindToSession(aConsole, aSession, aProcessID /* Object ID */);
184 if (RT_SUCCESS(vrc))
185 {
186 hr = unconst(mEventSource).createObject();
187 if (FAILED(hr))
188 vrc = VERR_NO_MEMORY;
189 else
190 {
191 hr = mEventSource->init(static_cast<IGuestProcess*>(this));
192 if (FAILED(hr))
193 vrc = VERR_COM_UNEXPECTED;
194 }
195 }
196
197 if (RT_SUCCESS(vrc))
198 {
199 try
200 {
201 GuestProcessListener *pListener = new GuestProcessListener();
202 ComObjPtr<GuestProcessListenerImpl> thisListener;
203 hr = thisListener.createObject();
204 if (SUCCEEDED(hr))
205 hr = thisListener->init(pListener, this);
206
207 if (SUCCEEDED(hr))
208 {
209 com::SafeArray <VBoxEventType_T> eventTypes;
210 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
211 eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
212 eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
213 hr = mEventSource->RegisterListener(thisListener,
214 ComSafeArrayAsInParam(eventTypes),
215 TRUE /* Active listener */);
216 if (SUCCEEDED(hr))
217 {
218 vrc = baseInit();
219 if (RT_SUCCESS(vrc))
220 {
221 mLocalListener = thisListener;
222 }
223 }
224 else
225 vrc = VERR_COM_UNEXPECTED;
226 }
227 else
228 vrc = VERR_COM_UNEXPECTED;
229 }
230 catch(std::bad_alloc &)
231 {
232 vrc = VERR_NO_MEMORY;
233 }
234 }
235
236 if (RT_SUCCESS(vrc))
237 {
238 mData.mProcess = aProcInfo;
239 mData.mExitCode = 0;
240 mData.mPID = 0;
241 mData.mLastError = VINF_SUCCESS;
242 mData.mStatus = ProcessStatus_Undefined;
243 /* Everything else will be set by the actual starting routine. */
244
245 /* Confirm a successful initialization when it's the case. */
246 autoInitSpan.setSucceeded();
247
248 return vrc;
249 }
250
251 autoInitSpan.setFailed();
252 return vrc;
253#endif
254}
255
256/**
257 * Uninitializes the instance.
258 * Called from FinalRelease() or IGuestSession::uninit().
259 */
260void GuestProcess::uninit(void)
261{
262 /* Enclose the state transition Ready->InUninit->NotReady. */
263 AutoUninitSpan autoUninitSpan(this);
264 if (autoUninitSpan.uninitDone())
265 return;
266
267#ifdef VBOX_WITH_GUEST_CONTROL
268 LogFlowThisFunc(("mCmd=%s, PID=%RU32\n",
269 mData.mProcess.mCommand.c_str(), mData.mPID));
270
271 /* Terminate process if not already done yet. */
272 int guestRc = VINF_SUCCESS;
273 int vrc = terminateProcess(30 * 1000, &guestRc); /** @todo Make timeouts configurable. */
274 /* Note: Don't return here yet; first uninit all other stuff in
275 * case of failure. */
276
277 baseUninit();
278
279 LogFlowThisFunc(("Returning rc=%Rrc, guestRc=%Rrc\n",
280 vrc, guestRc));
281#endif
282}
283
284// implementation of public getters/setters for attributes
285/////////////////////////////////////////////////////////////////////////////
286
287STDMETHODIMP GuestProcess::COMGETTER(Arguments)(ComSafeArrayOut(BSTR, aArguments))
288{
289#ifndef VBOX_WITH_GUEST_CONTROL
290 ReturnComNotImplemented();
291#else
292 LogFlowThisFuncEnter();
293
294 CheckComArgOutSafeArrayPointerValid(aArguments);
295
296 AutoCaller autoCaller(this);
297 if (FAILED(autoCaller.rc())) return autoCaller.rc();
298
299 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
300
301 com::SafeArray<BSTR> collection(mData.mProcess.mArguments.size());
302 size_t s = 0;
303 for (ProcessArguments::const_iterator it = mData.mProcess.mArguments.begin();
304 it != mData.mProcess.mArguments.end();
305 it++, s++)
306 {
307 Bstr tmp = *it;
308 tmp.cloneTo(&collection[s]);
309 }
310
311 collection.detachTo(ComSafeArrayOutArg(aArguments));
312
313 return S_OK;
314#endif /* VBOX_WITH_GUEST_CONTROL */
315}
316
317STDMETHODIMP GuestProcess::COMGETTER(Environment)(ComSafeArrayOut(BSTR, aEnvironment))
318{
319#ifndef VBOX_WITH_GUEST_CONTROL
320 ReturnComNotImplemented();
321#else
322 LogFlowThisFuncEnter();
323
324 CheckComArgOutSafeArrayPointerValid(aEnvironment);
325
326 AutoCaller autoCaller(this);
327 if (FAILED(autoCaller.rc())) return autoCaller.rc();
328
329 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
330
331 com::SafeArray<BSTR> arguments(mData.mProcess.mEnvironment.Size());
332 for (size_t i = 0; i < arguments.size(); i++)
333 {
334 Bstr tmp = mData.mProcess.mEnvironment.Get(i);
335 tmp.cloneTo(&arguments[i]);
336 }
337 arguments.detachTo(ComSafeArrayOutArg(aEnvironment));
338
339 return S_OK;
340#endif /* VBOX_WITH_GUEST_CONTROL */
341}
342
343STDMETHODIMP GuestProcess::COMGETTER(EventSource)(IEventSource ** aEventSource)
344{
345#ifndef VBOX_WITH_GUEST_CONTROL
346 ReturnComNotImplemented();
347#else
348 LogFlowThisFuncEnter();
349
350 CheckComArgOutPointerValid(aEventSource);
351
352 AutoCaller autoCaller(this);
353 if (FAILED(autoCaller.rc())) return autoCaller.rc();
354
355 // no need to lock - lifetime constant
356 mEventSource.queryInterfaceTo(aEventSource);
357
358 LogFlowThisFuncLeave();
359 return S_OK;
360#endif /* VBOX_WITH_GUEST_CONTROL */
361}
362
363STDMETHODIMP GuestProcess::COMGETTER(ExecutablePath)(BSTR *aExecutablePath)
364{
365#ifndef VBOX_WITH_GUEST_CONTROL
366 ReturnComNotImplemented();
367#else
368 LogFlowThisFuncEnter();
369
370 CheckComArgOutPointerValid(aExecutablePath);
371
372 AutoCaller autoCaller(this);
373 if (FAILED(autoCaller.rc())) return autoCaller.rc();
374
375 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
376
377 mData.mProcess.mCommand.cloneTo(aExecutablePath);
378
379 return S_OK;
380#endif /* VBOX_WITH_GUEST_CONTROL */
381}
382
383STDMETHODIMP GuestProcess::COMGETTER(ExitCode)(LONG *aExitCode)
384{
385#ifndef VBOX_WITH_GUEST_CONTROL
386 ReturnComNotImplemented();
387#else
388 LogFlowThisFuncEnter();
389
390 CheckComArgOutPointerValid(aExitCode);
391
392 AutoCaller autoCaller(this);
393 if (FAILED(autoCaller.rc())) return autoCaller.rc();
394
395 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
396
397 *aExitCode = mData.mExitCode;
398
399 return S_OK;
400#endif /* VBOX_WITH_GUEST_CONTROL */
401}
402
403STDMETHODIMP GuestProcess::COMGETTER(Name)(BSTR *aName)
404{
405#ifndef VBOX_WITH_GUEST_CONTROL
406 ReturnComNotImplemented();
407#else
408 LogFlowThisFuncEnter();
409
410 CheckComArgOutPointerValid(aName);
411
412 AutoCaller autoCaller(this);
413 if (FAILED(autoCaller.rc())) return autoCaller.rc();
414
415 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
416
417 mData.mProcess.mName.cloneTo(aName);
418
419 return S_OK;
420#endif /* VBOX_WITH_GUEST_CONTROL */
421}
422
423STDMETHODIMP GuestProcess::COMGETTER(PID)(ULONG *aPID)
424{
425#ifndef VBOX_WITH_GUEST_CONTROL
426 ReturnComNotImplemented();
427#else
428 LogFlowThisFuncEnter();
429
430 CheckComArgOutPointerValid(aPID);
431
432 AutoCaller autoCaller(this);
433 if (FAILED(autoCaller.rc())) return autoCaller.rc();
434
435 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
436
437 *aPID = mData.mPID;
438
439 return S_OK;
440#endif /* VBOX_WITH_GUEST_CONTROL */
441}
442
443STDMETHODIMP GuestProcess::COMGETTER(Status)(ProcessStatus_T *aStatus)
444{
445#ifndef VBOX_WITH_GUEST_CONTROL
446 ReturnComNotImplemented();
447#else
448 LogFlowThisFuncEnter();
449
450 AutoCaller autoCaller(this);
451 if (FAILED(autoCaller.rc())) return autoCaller.rc();
452
453 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
454
455 *aStatus = mData.mStatus;
456
457 return S_OK;
458#endif /* VBOX_WITH_GUEST_CONTROL */
459}
460
461// private methods
462/////////////////////////////////////////////////////////////////////////////
463
464int GuestProcess::callbackDispatcher(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCb)
465{
466 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
467 AssertPtrReturn(pSvcCb, VERR_INVALID_POINTER);
468#ifdef DEBUG
469 LogFlowThisFunc(("uPID=%RU32, uContextID=%RU32, uFunction=%RU32, pSvcCb=%p\n",
470 mData.mPID, pCbCtx->uContextID, pCbCtx->uFunction, pSvcCb));
471#endif
472
473 int vrc;
474 switch (pCbCtx->uFunction)
475 {
476 case GUEST_DISCONNECTED:
477 {
478 vrc = onGuestDisconnected(pCbCtx, pSvcCb);
479 break;
480 }
481
482 case GUEST_EXEC_STATUS:
483 {
484 vrc = onProcessStatusChange(pCbCtx, pSvcCb);
485 break;
486 }
487
488 case GUEST_EXEC_OUTPUT:
489 {
490 vrc = onProcessOutput(pCbCtx, pSvcCb);
491 break;
492 }
493
494 case GUEST_EXEC_INPUT_STATUS:
495 {
496 vrc = onProcessInputStatus(pCbCtx, pSvcCb);
497 break;
498 }
499
500 default:
501 /* Silently ignore not implemented functions. */
502 vrc = VERR_NOT_SUPPORTED;
503 break;
504 }
505
506#ifdef DEBUG
507 LogFlowFuncLeaveRC(vrc);
508#endif
509 return vrc;
510}
511
512/**
513 * Checks if the current assigned PID matches another PID (from a callback).
514 *
515 * In protocol v1 we don't have the possibility to terminate/kill
516 * processes so it can happen that a formerly started process A
517 * (which has the context ID 0 (session=0, process=0, count=0) will
518 * send a delayed message to the host if this process has already
519 * been discarded there and the same context ID was reused by
520 * a process B. Process B in turn then has a different guest PID.
521 *
522 * Note: This also can happen when restoring from a saved state which
523 * had a guest process running.
524 *
525 * @return IPRT status code.
526 * @param uPID PID to check.
527 */
528inline int GuestProcess::checkPID(uint32_t uPID)
529{
530 int rc = VINF_SUCCESS;
531
532 /* Was there a PID assigned yet? */
533 if (mData.mPID)
534 {
535 if (RT_UNLIKELY(mData.mPID != uPID))
536 {
537 LogFlowFunc(("Stale guest process (PID=%RU32) sent data to a newly started process (pProcesS=%p, PID=%RU32, status=%RU32)\n",
538 uPID, this, mData.mPID, mData.mStatus));
539 rc = VERR_NOT_FOUND;
540 }
541 }
542
543 return rc;
544}
545
546/* static */
547Utf8Str GuestProcess::guestErrorToString(int guestRc)
548{
549 Utf8Str strError;
550
551 /** @todo pData->u32Flags: int vs. uint32 -- IPRT errors are *negative* !!! */
552 switch (guestRc)
553 {
554 case VERR_FILE_NOT_FOUND: /* This is the most likely error. */
555 strError += Utf8StrFmt(tr("The specified file was not found on guest"));
556 break;
557
558 case VERR_INVALID_VM_HANDLE:
559 strError += Utf8StrFmt(tr("VMM device is not available (is the VM running?)"));
560 break;
561
562 case VERR_HGCM_SERVICE_NOT_FOUND:
563 strError += Utf8StrFmt(tr("The guest execution service is not available"));
564 break;
565
566 case VERR_PATH_NOT_FOUND:
567 strError += Utf8StrFmt(tr("Could not resolve path to specified file was not found on guest"));
568 break;
569
570 case VERR_BAD_EXE_FORMAT:
571 strError += Utf8StrFmt(tr("The specified file is not an executable format on guest"));
572 break;
573
574 case VERR_AUTHENTICATION_FAILURE:
575 strError += Utf8StrFmt(tr("The specified user was not able to logon on guest"));
576 break;
577
578 case VERR_INVALID_NAME:
579 strError += Utf8StrFmt(tr("The specified file is an invalid name"));
580 break;
581
582 case VERR_TIMEOUT:
583 strError += Utf8StrFmt(tr("The guest did not respond within time"));
584 break;
585
586 case VERR_CANCELLED:
587 strError += Utf8StrFmt(tr("The execution operation was canceled"));
588 break;
589
590 case VERR_PERMISSION_DENIED:
591 strError += Utf8StrFmt(tr("Invalid user/password credentials"));
592 break;
593
594 case VERR_MAX_PROCS_REACHED:
595 strError += Utf8StrFmt(tr("Maximum number of concurrent guest processes has been reached"));
596 break;
597
598 case VERR_NOT_EQUAL: /** @todo Imprecise to the user; can mean anything and all. */
599 strError += Utf8StrFmt(tr("Unable to retrieve requested information"));
600 break;
601
602 case VERR_NOT_FOUND:
603 strError += Utf8StrFmt(tr("The guest execution service is not ready (yet)"));
604 break;
605
606 default:
607 strError += Utf8StrFmt("%Rrc", guestRc);
608 break;
609 }
610
611 return strError;
612}
613
614inline bool GuestProcess::isAlive(void)
615{
616 return ( mData.mStatus == ProcessStatus_Started
617 || mData.mStatus == ProcessStatus_Paused
618 || mData.mStatus == ProcessStatus_Terminating);
619}
620
621inline bool GuestProcess::hasEnded(void)
622{
623 return ( mData.mStatus == ProcessStatus_TerminatedNormally
624 || mData.mStatus == ProcessStatus_TerminatedSignal
625 || mData.mStatus == ProcessStatus_TerminatedAbnormally
626 || mData.mStatus == ProcessStatus_TimedOutKilled
627 || mData.mStatus == ProcessStatus_TimedOutAbnormally
628 || mData.mStatus == ProcessStatus_Down
629 || mData.mStatus == ProcessStatus_Error);
630}
631
632int GuestProcess::onGuestDisconnected(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
633{
634 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
635 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
636
637 int vrc = setProcessStatus(ProcessStatus_Down, VINF_SUCCESS);
638
639 LogFlowFuncLeaveRC(vrc);
640 return vrc;
641}
642
643int GuestProcess::onProcessInputStatus(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
644{
645 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
646 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
647 /* pCallback is optional. */
648
649 if (pSvcCbData->mParms < 5)
650 return VERR_INVALID_PARAMETER;
651
652 CALLBACKDATA_PROC_INPUT dataCb;
653 /* pSvcCb->mpaParms[0] always contains the context ID. */
654 int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
655 AssertRCReturn(vrc, vrc);
656 vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uStatus);
657 AssertRCReturn(vrc, vrc);
658 vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
659 AssertRCReturn(vrc, vrc);
660 vrc = pSvcCbData->mpaParms[4].getUInt32(&dataCb.uProcessed);
661 AssertRCReturn(vrc, vrc);
662
663 LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RI32, cbProcessed=%RU32\n",
664 dataCb.uPID, dataCb.uStatus, dataCb.uFlags, dataCb.uProcessed));
665
666 vrc = checkPID(dataCb.uPID);
667 if (RT_SUCCESS(vrc))
668 {
669 ProcessInputStatus_T inputStatus = ProcessInputStatus_Undefined;
670 switch (dataCb.uStatus)
671 {
672 case INPUT_STS_WRITTEN:
673 inputStatus = ProcessInputStatus_Written;
674 break;
675 case INPUT_STS_ERROR:
676 inputStatus = ProcessInputStatus_Broken;
677 break;
678 case INPUT_STS_TERMINATED:
679 inputStatus = ProcessInputStatus_Broken;
680 break;
681 case INPUT_STS_OVERFLOW:
682 inputStatus = ProcessInputStatus_Overflow;
683 break;
684 case INPUT_STS_UNDEFINED:
685 /* Fall through is intentional. */
686 default:
687 AssertMsg(!dataCb.uProcessed, ("Processed data is not 0 in undefined input state\n"));
688 break;
689 }
690
691 if (inputStatus != ProcessInputStatus_Undefined)
692 {
693 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
694
695 /* Copy over necessary data before releasing lock again. */
696 uint32_t uPID = mData.mPID;
697 /** @todo Also handle mSession? */
698
699 alock.release(); /* Release lock before firing off event. */
700
701 fireGuestProcessInputNotifyEvent(mEventSource, mSession, this,
702 uPID, 0 /* StdIn */, dataCb.uProcessed, inputStatus);
703 }
704 }
705
706 LogFlowFuncLeaveRC(vrc);
707 return vrc;
708}
709
710int GuestProcess::onProcessNotifyIO(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
711{
712 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
713 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
714
715 return VERR_NOT_IMPLEMENTED;
716}
717
718int GuestProcess::onProcessStatusChange(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
719{
720 AssertPtrReturn(pCbCtx, VERR_INVALID_POINTER);
721 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
722
723 if (pSvcCbData->mParms < 5)
724 return VERR_INVALID_PARAMETER;
725
726 CALLBACKDATA_PROC_STATUS dataCb;
727 /* pSvcCb->mpaParms[0] always contains the context ID. */
728 int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
729 AssertRCReturn(vrc, vrc);
730 vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uStatus);
731 AssertRCReturn(vrc, vrc);
732 vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
733 AssertRCReturn(vrc, vrc);
734 vrc = pSvcCbData->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
735 AssertRCReturn(vrc, vrc);
736
737 LogFlowThisFunc(("uPID=%RU32, uStatus=%RU32, uFlags=%RU32\n",
738 dataCb.uPID, dataCb.uStatus, dataCb.uFlags));
739
740 vrc = checkPID(dataCb.uPID);
741 if (RT_SUCCESS(vrc))
742 {
743 ProcessStatus_T procStatus = ProcessStatus_Undefined;
744 int procRc = VINF_SUCCESS;
745
746 switch (dataCb.uStatus)
747 {
748 case PROC_STS_STARTED:
749 {
750 procStatus = ProcessStatus_Started;
751
752 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
753 mData.mPID = dataCb.uPID; /* Set the process PID. */
754 break;
755 }
756
757 case PROC_STS_TEN:
758 {
759 procStatus = ProcessStatus_TerminatedNormally;
760
761 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
762 mData.mExitCode = dataCb.uFlags; /* Contains the exit code. */
763 break;
764 }
765
766 case PROC_STS_TES:
767 {
768 procStatus = ProcessStatus_TerminatedSignal;
769
770 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
771 mData.mExitCode = dataCb.uFlags; /* Contains the signal. */
772 break;
773 }
774
775 case PROC_STS_TEA:
776 {
777 procStatus = ProcessStatus_TerminatedAbnormally;
778 break;
779 }
780
781 case PROC_STS_TOK:
782 {
783 procStatus = ProcessStatus_TimedOutKilled;
784 break;
785 }
786
787 case PROC_STS_TOA:
788 {
789 procStatus = ProcessStatus_TimedOutAbnormally;
790 break;
791 }
792
793 case PROC_STS_DWN:
794 {
795 procStatus = ProcessStatus_Down;
796 break;
797 }
798
799 case PROC_STS_ERROR:
800 {
801 procRc = dataCb.uFlags; /* mFlags contains the IPRT error sent from the guest. */
802 procStatus = ProcessStatus_Error;
803 break;
804 }
805
806 case PROC_STS_UNDEFINED:
807 default:
808 {
809 /* Silently skip this request. */
810 procStatus = ProcessStatus_Undefined;
811 break;
812 }
813 }
814
815 LogFlowThisFunc(("Got rc=%Rrc, procSts=%RU32, procRc=%Rrc\n",
816 vrc, procStatus, procRc));
817
818 /* Set the process status. */
819 int rc2 = setProcessStatus(procStatus, procRc);
820 if (RT_SUCCESS(vrc))
821 vrc = rc2;
822 }
823
824 LogFlowFuncLeaveRC(vrc);
825 return vrc;
826}
827
828int GuestProcess::onProcessOutput(PVBOXGUESTCTRLHOSTCBCTX pCbCtx, PVBOXGUESTCTRLHOSTCALLBACK pSvcCbData)
829{
830 AssertPtrReturn(pSvcCbData, VERR_INVALID_POINTER);
831
832 if (pSvcCbData->mParms < 5)
833 return VERR_INVALID_PARAMETER;
834
835 CALLBACKDATA_PROC_OUTPUT dataCb;
836 /* pSvcCb->mpaParms[0] always contains the context ID. */
837 int vrc = pSvcCbData->mpaParms[1].getUInt32(&dataCb.uPID);
838 AssertRCReturn(vrc, vrc);
839 vrc = pSvcCbData->mpaParms[2].getUInt32(&dataCb.uHandle);
840 AssertRCReturn(vrc, vrc);
841 vrc = pSvcCbData->mpaParms[3].getUInt32(&dataCb.uFlags);
842 AssertRCReturn(vrc, vrc);
843 vrc = pSvcCbData->mpaParms[4].getPointer(&dataCb.pvData, &dataCb.cbData);
844 AssertRCReturn(vrc, vrc);
845
846 LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RI32, pvData=%p, cbData=%RU32\n",
847 dataCb.uPID, dataCb.uHandle, dataCb.uFlags, dataCb.pvData, dataCb.cbData));
848
849 vrc = checkPID(dataCb.uPID);
850 if (RT_SUCCESS(vrc))
851 {
852 com::SafeArray<BYTE> data((size_t)dataCb.cbData);
853 if (dataCb.cbData)
854 data.initFrom((BYTE*)dataCb.pvData, dataCb.cbData);
855
856 fireGuestProcessOutputEvent(mEventSource, mSession, this,
857 mData.mPID, dataCb.uHandle, dataCb.cbData, ComSafeArrayAsInParam(data));
858 }
859
860 LogFlowFuncLeaveRC(vrc);
861 return vrc;
862}
863
864/**
865 * Called by IGuestSession right before this process gets
866 * removed from the public process list.
867 */
868int GuestProcess::onRemove(void)
869{
870 LogFlowThisFuncEnter();
871
872 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
873
874 int vrc = VINF_SUCCESS;
875
876 /*
877 * Note: The event source stuff holds references to this object,
878 * so make sure that this is cleaned up *before* calling uninit().
879 */
880 if (!mEventSource.isNull())
881 {
882 mEventSource->UnregisterListener(mLocalListener);
883
884 mLocalListener.setNull();
885 unconst(mEventSource).setNull();
886 }
887
888 LogFlowFuncLeaveRC(vrc);
889 return vrc;
890}
891
892int GuestProcess::readData(uint32_t uHandle, uint32_t uSize, uint32_t uTimeoutMS,
893 void *pvData, size_t cbData, uint32_t *pcbRead, int *pGuestRc)
894{
895 LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%RU32, pGuestRc=%p\n",
896 mData.mPID, uHandle, uSize, uTimeoutMS, pvData, cbData, pGuestRc));
897 AssertReturn(uSize, VERR_INVALID_PARAMETER);
898 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
899 AssertReturn(cbData >= uSize, VERR_INVALID_PARAMETER);
900 /* pcbRead is optional. */
901
902 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
903
904 if ( mData.mStatus != ProcessStatus_Started
905 /* Skip reading if the process wasn't started with the appropriate
906 * flags. */
907 || ( ( uHandle == OUTPUT_HANDLE_ID_STDOUT
908 || uHandle == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED)
909 && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdOut))
910 || ( uHandle == OUTPUT_HANDLE_ID_STDERR
911 && !(mData.mProcess.mFlags & ProcessCreateFlag_WaitForStdErr))
912 )
913 {
914 if (pcbRead)
915 *pcbRead = 0;
916 if (pGuestRc)
917 *pGuestRc = VINF_SUCCESS;
918 return VINF_SUCCESS; /* Nothing to read anymore. */
919 }
920
921 int vrc;
922
923 GuestWaitEvent *pEvent = NULL;
924 GuestEventTypes eventTypes;
925 try
926 {
927 /*
928 * On Guest Additions < 4.3 there is no guarantee that the process status
929 * change arrives *after* the output event, e.g. if this was the last output
930 * block being read and the process will report status "terminate".
931 * So just skip checking for process status change and only wait for the
932 * output event.
933 */
934 if (mSession->getProtocolVersion() >= 2)
935 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
936 eventTypes.push_back(VBoxEventType_OnGuestProcessOutput);
937
938 vrc = registerWaitEvent(eventTypes, &pEvent);
939 }
940 catch (std::bad_alloc)
941 {
942 vrc = VERR_NO_MEMORY;
943 }
944
945 if (RT_FAILURE(vrc))
946 return vrc;
947
948 if (RT_SUCCESS(vrc))
949 {
950 VBOXHGCMSVCPARM paParms[8];
951 int i = 0;
952 paParms[i++].setUInt32(pEvent->ContextID());
953 paParms[i++].setUInt32(mData.mPID);
954 paParms[i++].setUInt32(uHandle);
955 paParms[i++].setUInt32(0 /* Flags, none set yet. */);
956
957 alock.release(); /* Drop the write lock before sending. */
958
959 vrc = sendCommand(HOST_EXEC_GET_OUTPUT, i, paParms);
960 }
961
962 if (RT_SUCCESS(vrc))
963 vrc = waitForOutput(pEvent, uHandle, uTimeoutMS,
964 pvData, cbData, pcbRead);
965
966 unregisterWaitEvent(pEvent);
967
968 LogFlowFuncLeaveRC(vrc);
969 return vrc;
970}
971
972/* Does not do locking; caller is responsible for that! */
973int GuestProcess::setProcessStatus(ProcessStatus_T procStatus, int procRc)
974{
975 LogFlowThisFuncEnter();
976
977 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
978
979 LogFlowThisFunc(("oldStatus=%RU32, newStatus=%RU32, procRc=%Rrc\n",
980 mData.mStatus, procStatus, procRc));
981
982 if (procStatus == ProcessStatus_Error)
983 {
984 AssertMsg(RT_FAILURE(procRc), ("Guest rc must be an error (%Rrc)\n", procRc));
985 /* Do not allow overwriting an already set error. If this happens
986 * this means we forgot some error checking/locking somewhere. */
987 AssertMsg(RT_SUCCESS(mData.mLastError), ("Guest rc already set (to %Rrc)\n", mData.mLastError));
988 }
989 else
990 AssertMsg(RT_SUCCESS(procRc), ("Guest rc must not be an error (%Rrc)\n", procRc));
991
992 int rc = VINF_SUCCESS;
993
994 if (mData.mStatus != procStatus) /* Was there a process status change? */
995 {
996 mData.mStatus = procStatus;
997 mData.mLastError = procRc;
998
999 ComObjPtr<VirtualBoxErrorInfo> errorInfo;
1000 HRESULT hr = errorInfo.createObject();
1001 ComAssertComRC(hr);
1002 if (RT_FAILURE(mData.mLastError))
1003 {
1004 hr = errorInfo->initEx(VBOX_E_IPRT_ERROR, mData.mLastError,
1005 COM_IIDOF(IGuestProcess), getComponentName(),
1006 guestErrorToString(mData.mLastError));
1007 ComAssertComRC(hr);
1008 }
1009
1010 /* Copy over necessary data before releasing lock again. */
1011 uint32_t uPID = mData.mPID;
1012 /** @todo Also handle mSession? */
1013
1014 alock.release(); /* Release lock before firing off event. */
1015
1016 fireGuestProcessStateChangedEvent(mEventSource, mSession, this,
1017 uPID, procStatus, errorInfo);
1018#if 0
1019 /*
1020 * On Guest Additions < 4.3 there is no guarantee that outstanding
1021 * requests will be delivered to the host after the process has ended,
1022 * so just cancel all waiting events here to not let clients run
1023 * into timeouts.
1024 */
1025 if ( mSession->getProtocolVersion() < 2
1026 && hasEnded())
1027 {
1028 LogFlowThisFunc(("Process ended, canceling outstanding wait events ...\n"));
1029 rc = cancelWaitEvents();
1030 }
1031#endif
1032 }
1033
1034 return rc;
1035}
1036
1037/* static */
1038HRESULT GuestProcess::setErrorExternal(VirtualBoxBase *pInterface, int guestRc)
1039{
1040 AssertPtr(pInterface);
1041 AssertMsg(RT_FAILURE(guestRc), ("Guest rc does not indicate a failure when setting error\n"));
1042
1043 return pInterface->setError(VBOX_E_IPRT_ERROR, GuestProcess::guestErrorToString(guestRc).c_str());
1044}
1045
1046int GuestProcess::startProcess(uint32_t uTimeoutMS, int *pGuestRc)
1047{
1048 LogFlowThisFunc(("uTimeoutMS=%RU32, procCmd=%s, procTimeoutMS=%RU32, procFlags=%x, sessionID=%RU32\n",
1049 uTimeoutMS, mData.mProcess.mCommand.c_str(), mData.mProcess.mTimeoutMS, mData.mProcess.mFlags,
1050 mSession->getId()));
1051
1052 /* Wait until the caller function (if kicked off by a thread)
1053 * has returned and continue operation. */
1054 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1055
1056 mData.mStatus = ProcessStatus_Starting;
1057
1058 int vrc;
1059
1060 GuestWaitEvent *pEvent = NULL;
1061 GuestEventTypes eventTypes;
1062 try
1063 {
1064 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
1065
1066 vrc = registerWaitEvent(eventTypes, &pEvent);
1067 }
1068 catch (std::bad_alloc)
1069 {
1070 vrc = VERR_NO_MEMORY;
1071 }
1072
1073 if (RT_FAILURE(vrc))
1074 return vrc;
1075
1076 GuestSession *pSession = mSession;
1077 AssertPtr(pSession);
1078
1079 const GuestCredentials &sessionCreds = pSession->getCredentials();
1080
1081 /* Prepare arguments. */
1082 char *pszArgs = NULL;
1083 size_t cArgs = mData.mProcess.mArguments.size();
1084 if (cArgs >= UINT32_MAX)
1085 vrc = VERR_BUFFER_OVERFLOW;
1086
1087 if ( RT_SUCCESS(vrc)
1088 && cArgs)
1089 {
1090 char **papszArgv = (char**)RTMemAlloc((cArgs + 1) * sizeof(char*));
1091 AssertReturn(papszArgv, VERR_NO_MEMORY);
1092
1093 for (size_t i = 0; i < cArgs && RT_SUCCESS(vrc); i++)
1094 {
1095 const char *pszCurArg = mData.mProcess.mArguments[i].c_str();
1096 AssertPtr(pszCurArg);
1097 vrc = RTStrDupEx(&papszArgv[i], pszCurArg);
1098 }
1099 papszArgv[cArgs] = NULL;
1100
1101 if (RT_SUCCESS(vrc))
1102 vrc = RTGetOptArgvToString(&pszArgs, papszArgv, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
1103
1104 if (papszArgv)
1105 {
1106 size_t i = 0;
1107 while (papszArgv[i])
1108 RTStrFree(papszArgv[i++]);
1109 RTMemFree(papszArgv);
1110 }
1111 }
1112
1113 /* Calculate arguments size (in bytes). */
1114 size_t cbArgs = 0;
1115 if (RT_SUCCESS(vrc))
1116 cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
1117
1118 /* Prepare environment. */
1119 void *pvEnv = NULL;
1120 size_t cbEnv = 0;
1121 if (RT_SUCCESS(vrc))
1122 vrc = mData.mProcess.mEnvironment.BuildEnvironmentBlock(&pvEnv, &cbEnv, NULL /* cEnv */);
1123
1124 if (RT_SUCCESS(vrc))
1125 {
1126 AssertPtr(mSession);
1127 uint32_t uProtocol = mSession->getProtocolVersion();
1128
1129 /* Prepare HGCM call. */
1130 VBOXHGCMSVCPARM paParms[16];
1131 int i = 0;
1132 paParms[i++].setUInt32(pEvent->ContextID());
1133 paParms[i++].setPointer((void*)mData.mProcess.mCommand.c_str(),
1134 (ULONG)mData.mProcess.mCommand.length() + 1);
1135 paParms[i++].setUInt32(mData.mProcess.mFlags);
1136 paParms[i++].setUInt32((uint32_t)mData.mProcess.mArguments.size());
1137 paParms[i++].setPointer((void*)pszArgs, (uint32_t)cbArgs);
1138 paParms[i++].setUInt32((uint32_t)mData.mProcess.mEnvironment.Size());
1139 paParms[i++].setUInt32((uint32_t)cbEnv);
1140 paParms[i++].setPointer((void*)pvEnv, (uint32_t)cbEnv);
1141 if (uProtocol < 2)
1142 {
1143 /* In protocol v1 (VBox < 4.3) the credentials were part of the execution
1144 * call. In newer protocols these credentials are part of the opened guest
1145 * session, so not needed anymore here. */
1146 paParms[i++].setPointer((void*)sessionCreds.mUser.c_str(), (ULONG)sessionCreds.mUser.length() + 1);
1147 paParms[i++].setPointer((void*)sessionCreds.mPassword.c_str(), (ULONG)sessionCreds.mPassword.length() + 1);
1148 }
1149 /*
1150 * If the WaitForProcessStartOnly flag is set, we only want to define and wait for a timeout
1151 * until the process was started - the process itself then gets an infinite timeout for execution.
1152 * This is handy when we want to start a process inside a worker thread within a certain timeout
1153 * but let the started process perform lengthly operations then.
1154 */
1155 if (mData.mProcess.mFlags & ProcessCreateFlag_WaitForProcessStartOnly)
1156 paParms[i++].setUInt32(UINT32_MAX /* Infinite timeout */);
1157 else
1158 paParms[i++].setUInt32(mData.mProcess.mTimeoutMS);
1159 if (uProtocol >= 2)
1160 {
1161 paParms[i++].setUInt32(mData.mProcess.mPriority);
1162 /* CPU affinity: We only support one CPU affinity block at the moment,
1163 * so that makes up to 64 CPUs total. This can be more in the future. */
1164 paParms[i++].setUInt32(1);
1165 /* The actual CPU affinity blocks. */
1166 paParms[i++].setPointer((void*)&mData.mProcess.mAffinity, sizeof(mData.mProcess.mAffinity));
1167 }
1168
1169 alock.release(); /* Drop the write lock before sending. */
1170
1171 vrc = sendCommand(HOST_EXEC_CMD, i, paParms);
1172 if (RT_FAILURE(vrc))
1173 {
1174 int rc2 = setProcessStatus(ProcessStatus_Error, vrc);
1175 AssertRC(rc2);
1176 }
1177 }
1178
1179 GuestEnvironment::FreeEnvironmentBlock(pvEnv);
1180 if (pszArgs)
1181 RTStrFree(pszArgs);
1182
1183 if (RT_SUCCESS(vrc))
1184 vrc = waitForStatusChange(pEvent, uTimeoutMS,
1185 NULL /* Process status */, pGuestRc);
1186 unregisterWaitEvent(pEvent);
1187
1188 LogFlowFuncLeaveRC(vrc);
1189 return vrc;
1190}
1191
1192int GuestProcess::startProcessAsync(void)
1193{
1194 LogFlowThisFuncEnter();
1195
1196 int vrc;
1197
1198 try
1199 {
1200 /* Asynchronously start the process on the guest by kicking off a
1201 * worker thread. */
1202 std::auto_ptr<GuestProcessStartTask> pTask(new GuestProcessStartTask(this));
1203 AssertReturn(pTask->isOk(), pTask->rc());
1204
1205 vrc = RTThreadCreate(NULL, GuestProcess::startProcessThread,
1206 (void *)pTask.get(), 0,
1207 RTTHREADTYPE_MAIN_WORKER, 0,
1208 "gctlPrcStart");
1209 if (RT_SUCCESS(vrc))
1210 {
1211 /* pTask is now owned by startProcessThread(), so release it. */
1212 pTask.release();
1213 }
1214 }
1215 catch(std::bad_alloc &)
1216 {
1217 vrc = VERR_NO_MEMORY;
1218 }
1219
1220 LogFlowFuncLeaveRC(vrc);
1221 return vrc;
1222}
1223
1224/* static */
1225DECLCALLBACK(int) GuestProcess::startProcessThread(RTTHREAD Thread, void *pvUser)
1226{
1227 LogFlowFunc(("pvUser=%p\n", pvUser));
1228
1229 std::auto_ptr<GuestProcessStartTask> pTask(static_cast<GuestProcessStartTask*>(pvUser));
1230 AssertPtr(pTask.get());
1231
1232 const ComObjPtr<GuestProcess> pProcess(pTask->Process());
1233 Assert(!pProcess.isNull());
1234
1235 AutoCaller autoCaller(pProcess);
1236 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1237
1238 int vrc = pProcess->startProcess(30 * 1000 /* 30s timeout */,
1239 NULL /* Guest rc, ignored */);
1240 /* Nothing to do here anymore. */
1241
1242 LogFlowFunc(("pProcess=%p returning rc=%Rrc\n", (GuestProcess *)pProcess, vrc));
1243 return vrc;
1244}
1245
1246int GuestProcess::terminateProcess(uint32_t uTimeoutMS, int *pGuestRc)
1247{
1248 /* pGuestRc is optional. */
1249 LogFlowThisFunc(("uTimeoutMS=%RU32\n", uTimeoutMS));
1250
1251 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1252
1253 int vrc = VINF_SUCCESS;
1254
1255 if (mData.mStatus != ProcessStatus_Started)
1256 {
1257 LogFlowThisFunc(("Process not in started state (state is %RU32), skipping termination\n",
1258 mData.mStatus));
1259 }
1260 else
1261 {
1262 AssertPtr(mSession);
1263 /* Note: VBox < 4.3 (aka protocol version 1) does not
1264 * support this, so just skip. */
1265 if (mSession->getProtocolVersion() < 2)
1266 vrc = VERR_NOT_SUPPORTED;
1267
1268 if (RT_SUCCESS(vrc))
1269 {
1270 GuestWaitEvent *pEvent = NULL;
1271 GuestEventTypes eventTypes;
1272 try
1273 {
1274 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
1275
1276 vrc = registerWaitEvent(eventTypes, &pEvent);
1277 }
1278 catch (std::bad_alloc)
1279 {
1280 vrc = VERR_NO_MEMORY;
1281 }
1282
1283 if (RT_FAILURE(vrc))
1284 return vrc;
1285
1286 VBOXHGCMSVCPARM paParms[4];
1287 int i = 0;
1288 paParms[i++].setUInt32(pEvent->ContextID());
1289 paParms[i++].setUInt32(mData.mPID);
1290
1291 alock.release(); /* Drop the write lock before sending. */
1292
1293 vrc = sendCommand(HOST_EXEC_TERMINATE, i, paParms);
1294 if (RT_SUCCESS(vrc))
1295 vrc = waitForStatusChange(pEvent, uTimeoutMS,
1296 NULL /* ProcessStatus */, pGuestRc);
1297 unregisterWaitEvent(pEvent);
1298 }
1299 }
1300
1301 LogFlowFuncLeaveRC(vrc);
1302 return vrc;
1303}
1304
1305/* static */
1306ProcessWaitResult_T GuestProcess::waitFlagsToResultEx(uint32_t fWaitFlags,
1307 ProcessStatus_T oldStatus, ProcessStatus_T newStatus,
1308 uint32_t uProcFlags, uint32_t uProtocol)
1309{
1310 ProcessWaitResult_T waitResult = ProcessWaitResult_None;
1311
1312 switch (newStatus)
1313 {
1314 case ProcessStatus_TerminatedNormally:
1315 case ProcessStatus_TerminatedSignal:
1316 case ProcessStatus_TerminatedAbnormally:
1317 case ProcessStatus_Down:
1318 /* Nothing to wait for anymore. */
1319 waitResult = ProcessWaitResult_Terminate;
1320 break;
1321
1322 case ProcessStatus_TimedOutKilled:
1323 case ProcessStatus_TimedOutAbnormally:
1324 /* Dito. */
1325 waitResult = ProcessWaitResult_Timeout;
1326 break;
1327
1328 case ProcessStatus_Started:
1329 switch (oldStatus)
1330 {
1331 case ProcessStatus_Undefined:
1332 case ProcessStatus_Starting:
1333 /* Also wait for process start. */
1334 if (fWaitFlags & ProcessWaitForFlag_Start)
1335 waitResult = ProcessWaitResult_Start;
1336 else
1337 {
1338 /*
1339 * If ProcessCreateFlag_WaitForProcessStartOnly was specified on process creation the
1340 * caller is not interested in getting further process statuses -- so just don't notify
1341 * anything here anymore and return.
1342 */
1343 if (uProcFlags & ProcessCreateFlag_WaitForProcessStartOnly)
1344 waitResult = ProcessWaitResult_Start;
1345 }
1346 break;
1347
1348 case ProcessStatus_Started:
1349 /* Only wait for process start. */
1350 if (fWaitFlags == ProcessWaitForFlag_Start)
1351 waitResult = ProcessWaitResult_Start;
1352 break;
1353
1354 default:
1355 AssertMsgFailed(("Unhandled old status %RU32 before new status 'started'\n",
1356 oldStatus));
1357 waitResult = ProcessWaitResult_Start;
1358 break;
1359 }
1360 break;
1361
1362 case ProcessStatus_Error:
1363 /* Nothing to wait for anymore. */
1364 waitResult = ProcessWaitResult_Error;
1365 break;
1366
1367 case ProcessStatus_Undefined:
1368 case ProcessStatus_Starting:
1369 /* No result available yet, leave wait
1370 * flags untouched. */
1371 break;
1372 }
1373
1374 if (newStatus == ProcessStatus_Started)
1375 {
1376 /* Filter out waits which are *not* supported using
1377 * older guest control Guest Additions.
1378 *
1379 ** @todo ProcessWaitForFlag_Std* flags are not implemented yet.
1380 */
1381 if (uProtocol < 99) /* See @todo above. */
1382 {
1383 if ( waitResult == ProcessWaitResult_None
1384 /* We don't support waiting for stdin, out + err,
1385 * just skip waiting then. */
1386 && ( (fWaitFlags & ProcessWaitForFlag_StdIn)
1387 || (fWaitFlags & ProcessWaitForFlag_StdOut)
1388 || (fWaitFlags & ProcessWaitForFlag_StdErr)
1389 )
1390 )
1391 {
1392 /* Use _WaitFlagNotSupported because we don't know what to tell the caller. */
1393 waitResult = ProcessWaitResult_WaitFlagNotSupported;
1394 }
1395 }
1396 }
1397
1398#ifdef DEBUG
1399 LogFlowFunc(("oldStatus=%RU32, newStatus=%RU32, fWaitFlags=0x%x, waitResult=%RU32\n",
1400 oldStatus, newStatus, fWaitFlags, waitResult));
1401#endif
1402 return waitResult;
1403}
1404
1405ProcessWaitResult_T GuestProcess::waitFlagsToResult(uint32_t fWaitFlags)
1406{
1407 AssertPtr(mSession);
1408 return GuestProcess::waitFlagsToResultEx(fWaitFlags,
1409 mData.mStatus /* curStatus */, mData.mStatus /* newStatus */,
1410 mData.mProcess.mFlags, mSession->getProtocolVersion());
1411}
1412
1413int GuestProcess::waitFor(uint32_t fWaitFlags, ULONG uTimeoutMS,
1414 ProcessWaitResult_T &waitResult, int *pGuestRc)
1415{
1416 AssertReturn(fWaitFlags, VERR_INVALID_PARAMETER);
1417
1418 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1419
1420 LogFlowThisFunc(("fWaitFlags=0x%x, uTimeoutMS=%RU32, procStatus=%RU32, procRc=%Rrc, pGuestRc=%p\n",
1421 fWaitFlags, uTimeoutMS, mData.mStatus, mData.mLastError, pGuestRc));
1422
1423 /* Did some error occur before? Then skip waiting and return. */
1424 ProcessStatus_T curStatus = mData.mStatus;
1425 if (curStatus == ProcessStatus_Error)
1426 {
1427 waitResult = ProcessWaitResult_Error;
1428 AssertMsg(RT_FAILURE(mData.mLastError), ("No error rc (%Rrc) set when guest process indicated an error\n", mData.mLastError));
1429 if (pGuestRc)
1430 *pGuestRc = mData.mLastError; /* Return last set error. */
1431 LogFlowThisFunc(("Process is in error state (guestRc=%Rrc)\n", mData.mLastError));
1432 return VERR_GSTCTL_GUEST_ERROR;
1433 }
1434
1435 waitResult = waitFlagsToResult(fWaitFlags);
1436
1437 /* No waiting needed? Return immediately using the last set error. */
1438 if (waitResult != ProcessWaitResult_None)
1439 {
1440 if (pGuestRc)
1441 *pGuestRc = mData.mLastError; /* Return last set error (if any). */
1442 LogFlowThisFunc(("Nothing to wait for (guestRc=%Rrc)\n", mData.mLastError));
1443 return RT_SUCCESS(mData.mLastError) ? VINF_SUCCESS : VERR_GSTCTL_GUEST_ERROR;
1444 }
1445
1446 /* Adjust timeout. Passing 0 means RT_INDEFINITE_WAIT. */
1447 if (!uTimeoutMS)
1448 uTimeoutMS = RT_INDEFINITE_WAIT;
1449
1450 int vrc;
1451
1452 GuestWaitEvent *pEvent = NULL;
1453 GuestEventTypes eventTypes;
1454 try
1455 {
1456 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
1457
1458 vrc = registerWaitEvent(eventTypes, &pEvent);
1459 }
1460 catch (std::bad_alloc)
1461 {
1462 vrc = VERR_NO_MEMORY;
1463 }
1464
1465 if (RT_FAILURE(vrc))
1466 return vrc;
1467
1468 alock.release(); /* Release lock before waiting. */
1469
1470 /*
1471 * Do the actual waiting.
1472 */
1473 ProcessStatus_T newStatus = ProcessStatus_Undefined;
1474 uint64_t u64StartMS = RTTimeMilliTS();
1475 for (;;)
1476 {
1477 uint64_t u64ElapsedMS = RTTimeMilliTS() - u64StartMS;
1478 if ( uTimeoutMS != RT_INDEFINITE_WAIT
1479 && u64ElapsedMS >= uTimeoutMS)
1480 {
1481 vrc = VERR_TIMEOUT;
1482 break;
1483 }
1484
1485 vrc = waitForStatusChange(pEvent,
1486 uTimeoutMS == RT_INDEFINITE_WAIT
1487 ? RT_INDEFINITE_WAIT : uTimeoutMS - (uint32_t)u64ElapsedMS,
1488 &newStatus, pGuestRc);
1489 if (RT_SUCCESS(vrc))
1490 {
1491 alock.acquire();
1492
1493 waitResult = waitFlagsToResultEx(fWaitFlags, curStatus, newStatus,
1494 mData.mProcess.mFlags, mSession->getProtocolVersion());
1495#ifdef DEBUG
1496 LogFlowThisFunc(("Got new status change: fWaitFlags=0x%x, newStatus=%RU32, waitResult=%RU32\n",
1497 fWaitFlags, newStatus, waitResult));
1498#endif
1499 if (ProcessWaitResult_None != waitResult) /* We got a waiting result. */
1500 break;
1501 }
1502 else /* Waiting failed, bail out. */
1503 break;
1504
1505 alock.release(); /* Don't hold lock in next waiting round. */
1506 }
1507
1508 unregisterWaitEvent(pEvent);
1509
1510 LogFlowThisFunc(("Returned waitResult=%RU32, newStatus=%RU32, rc=%Rrc\n",
1511 waitResult, newStatus, vrc));
1512 return vrc;
1513}
1514
1515int GuestProcess::waitForInputNotify(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
1516 ProcessInputStatus_T *pInputStatus, uint32_t *pcbProcessed)
1517{
1518 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1519
1520 VBoxEventType_T evtType;
1521 ComPtr<IEvent> pIEvent;
1522 int vrc = waitForEvent(pEvent, uTimeoutMS,
1523 &evtType, pIEvent.asOutParam());
1524 if (RT_SUCCESS(vrc))
1525 {
1526 if (evtType == VBoxEventType_OnGuestProcessInputNotify)
1527 {
1528 ComPtr<IGuestProcessInputNotifyEvent> pProcessEvent = pIEvent;
1529 Assert(!pProcessEvent.isNull());
1530
1531 if (pInputStatus)
1532 {
1533 HRESULT hr2 = pProcessEvent->COMGETTER(Status)(pInputStatus);
1534 ComAssertComRC(hr2);
1535 }
1536 if (pcbProcessed)
1537 {
1538 HRESULT hr2 = pProcessEvent->COMGETTER(Processed)((ULONG*)pcbProcessed);
1539 ComAssertComRC(hr2);
1540 }
1541 }
1542 else
1543 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1544 }
1545
1546 LogFlowThisFunc(("Returning pEvent=%p, uHandle=%RU32, rc=%Rrc\n",
1547 pEvent, uHandle, vrc));
1548 return vrc;
1549}
1550
1551int GuestProcess::waitForOutput(GuestWaitEvent *pEvent, uint32_t uHandle, uint32_t uTimeoutMS,
1552 void *pvData, size_t cbData, uint32_t *pcbRead)
1553{
1554 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1555 /* pvData is optional. */
1556 /* cbData is optional. */
1557 /* pcbRead is optional. */
1558
1559 LogFlowThisFunc(("cEventTypes=%zu, pEvent=%p, uHandle=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu, pcbRead=%p\n",
1560 pEvent->TypeCount(), pEvent, uHandle, uTimeoutMS, pvData, cbData, pcbRead));
1561
1562 int vrc;
1563
1564 VBoxEventType_T evtType;
1565 ComPtr<IEvent> pIEvent;
1566 do
1567 {
1568 vrc = waitForEvent(pEvent, uTimeoutMS,
1569 &evtType, pIEvent.asOutParam());
1570 if (RT_SUCCESS(vrc))
1571 {
1572 if (evtType == VBoxEventType_OnGuestProcessOutput)
1573 {
1574 ComPtr<IGuestProcessOutputEvent> pProcessEvent = pIEvent;
1575 Assert(!pProcessEvent.isNull());
1576
1577 ULONG uHandleEvent;
1578 HRESULT hr = pProcessEvent->COMGETTER(Handle)(&uHandleEvent);
1579 if ( SUCCEEDED(hr)
1580 && uHandleEvent == uHandle)
1581 {
1582 if (pvData)
1583 {
1584 com::SafeArray <BYTE> data;
1585 hr = pProcessEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
1586 ComAssertComRC(hr);
1587 size_t cbRead = data.size();
1588 if (cbRead)
1589 {
1590 if (cbRead <= cbData)
1591 {
1592 /* Copy data from event into our buffer. */
1593 memcpy(pvData, data.raw(), data.size());
1594 }
1595 else
1596 vrc = VERR_BUFFER_OVERFLOW;
1597
1598 LogFlowThisFunc(("Read %zu bytes (uHandle=%RU32), rc=%Rrc\n",
1599 cbRead, uHandleEvent, vrc));
1600 }
1601 }
1602
1603 if ( RT_SUCCESS(vrc)
1604 && pcbRead)
1605 {
1606 ULONG cbRead;
1607 hr = pProcessEvent->COMGETTER(Processed)(&cbRead);
1608 ComAssertComRC(hr);
1609 *pcbRead = (uint32_t)cbRead;
1610 }
1611
1612 break;
1613 }
1614 else if (FAILED(hr))
1615 vrc = VERR_COM_UNEXPECTED;
1616 }
1617 else
1618 vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
1619 }
1620
1621 } while (vrc == VINF_SUCCESS);
1622
1623 if ( vrc != VINF_SUCCESS
1624 && pcbRead)
1625 {
1626 *pcbRead = 0;
1627 }
1628
1629 LogFlowFuncLeaveRC(vrc);
1630 return vrc;
1631}
1632
1633int GuestProcess::waitForStatusChange(GuestWaitEvent *pEvent, uint32_t uTimeoutMS,
1634 ProcessStatus_T *pProcessStatus, int *pGuestRc)
1635{
1636 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1637 /* pProcessStatus is optional. */
1638 /* pGuestRc is optional. */
1639
1640 VBoxEventType_T evtType;
1641 ComPtr<IEvent> pIEvent;
1642 int vrc = waitForEvent(pEvent, uTimeoutMS,
1643 &evtType, pIEvent.asOutParam());
1644 if (RT_SUCCESS(vrc))
1645 {
1646 Assert(evtType == VBoxEventType_OnGuestProcessStateChanged);
1647 ComPtr<IGuestProcessStateChangedEvent> pProcessEvent = pIEvent;
1648 Assert(!pProcessEvent.isNull());
1649
1650 ProcessStatus_T procStatus;
1651 HRESULT hr = pProcessEvent->COMGETTER(Status)(&procStatus);
1652 ComAssertComRC(hr);
1653 if (pProcessStatus)
1654 *pProcessStatus = procStatus;
1655
1656 ComPtr<IVirtualBoxErrorInfo> errorInfo;
1657 hr = pProcessEvent->COMGETTER(Error)(errorInfo.asOutParam());
1658 ComAssertComRC(hr);
1659
1660 LONG lGuestRc;
1661 hr = errorInfo->COMGETTER(ResultDetail)(&lGuestRc);
1662 ComAssertComRC(hr);
1663
1664 LogFlowThisFunc(("Got procStatus=%RU32, guestRc=%RI32 (%Rrc)\n",
1665 procStatus, lGuestRc, lGuestRc));
1666
1667 if (RT_FAILURE((int)lGuestRc))
1668 vrc = VERR_GSTCTL_GUEST_ERROR;
1669
1670 if (pGuestRc)
1671 *pGuestRc = (int)lGuestRc;
1672 }
1673
1674 LogFlowFuncLeaveRC(vrc);
1675 return vrc;
1676}
1677
1678/* static */
1679bool GuestProcess::waitResultImpliesEx(ProcessWaitResult_T waitResult,
1680 ProcessStatus_T procStatus, uint32_t uProcFlags,
1681 uint32_t uProtocol)
1682{
1683 bool fImplies;
1684
1685 switch (waitResult)
1686 {
1687 case ProcessWaitResult_Start:
1688 fImplies = procStatus == ProcessStatus_Started;
1689 break;
1690
1691 case ProcessWaitResult_Terminate:
1692 fImplies = ( procStatus == ProcessStatus_TerminatedNormally
1693 || procStatus == ProcessStatus_TerminatedSignal
1694 || procStatus == ProcessStatus_TerminatedAbnormally
1695 || procStatus == ProcessStatus_TimedOutKilled
1696 || procStatus == ProcessStatus_TimedOutAbnormally
1697 || procStatus == ProcessStatus_Down
1698 || procStatus == ProcessStatus_Error);
1699 break;
1700
1701 default:
1702 fImplies = false;
1703 break;
1704 }
1705
1706 return fImplies;
1707}
1708
1709int GuestProcess::writeData(uint32_t uHandle, uint32_t uFlags,
1710 void *pvData, size_t cbData, uint32_t uTimeoutMS, uint32_t *puWritten, int *pGuestRc)
1711{
1712 LogFlowThisFunc(("uPID=%RU32, uHandle=%RU32, uFlags=%RU32, pvData=%p, cbData=%RU32, uTimeoutMS=%RU32, puWritten=%p, pGuestRc=%p\n",
1713 mData.mPID, uHandle, uFlags, pvData, cbData, uTimeoutMS, puWritten, pGuestRc));
1714 /* All is optional. There can be 0 byte writes. */
1715
1716 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1717
1718 if (mData.mStatus != ProcessStatus_Started)
1719 {
1720 if (puWritten)
1721 *puWritten = 0;
1722 if (pGuestRc)
1723 *pGuestRc = VINF_SUCCESS;
1724 return VINF_SUCCESS; /* Not available for writing (anymore). */
1725 }
1726
1727 int vrc;
1728
1729 GuestWaitEvent *pEvent = NULL;
1730 GuestEventTypes eventTypes;
1731 try
1732 {
1733 /*
1734 * On Guest Additions < 4.3 there is no guarantee that the process status
1735 * change arrives *after* the input event, e.g. if this was the last input
1736 * block being written and the process will report status "terminate".
1737 * So just skip checking for process status change and only wait for the
1738 * input event.
1739 */
1740 if (mSession->getProtocolVersion() >= 2)
1741 eventTypes.push_back(VBoxEventType_OnGuestProcessStateChanged);
1742 eventTypes.push_back(VBoxEventType_OnGuestProcessInputNotify);
1743
1744 vrc = registerWaitEvent(eventTypes, &pEvent);
1745 }
1746 catch (std::bad_alloc)
1747 {
1748 vrc = VERR_NO_MEMORY;
1749 }
1750
1751 if (RT_FAILURE(vrc))
1752 return vrc;
1753
1754 VBOXHGCMSVCPARM paParms[5];
1755 int i = 0;
1756 paParms[i++].setUInt32(pEvent->ContextID());
1757 paParms[i++].setUInt32(mData.mPID);
1758 paParms[i++].setUInt32(uFlags);
1759 paParms[i++].setPointer(pvData, (uint32_t)cbData);
1760 paParms[i++].setUInt32((uint32_t)cbData);
1761
1762 alock.release(); /* Drop the write lock before sending. */
1763
1764 uint32_t cbProcessed = 0;
1765 vrc = sendCommand(HOST_EXEC_SET_INPUT, i, paParms);
1766 if (RT_SUCCESS(vrc))
1767 {
1768 ProcessInputStatus_T inputStatus;
1769 vrc = waitForInputNotify(pEvent, uHandle, uTimeoutMS,
1770 &inputStatus, &cbProcessed);
1771 if (RT_SUCCESS(vrc))
1772 {
1773 /** @todo Set guestRc. */
1774
1775 if (puWritten)
1776 *puWritten = cbProcessed;
1777 }
1778 /** @todo Error handling. */
1779 }
1780
1781 unregisterWaitEvent(pEvent);
1782
1783 LogFlowThisFunc(("Returning cbProcessed=%RU32, rc=%Rrc\n",
1784 cbProcessed, vrc));
1785 return vrc;
1786}
1787
1788// implementation of public methods
1789/////////////////////////////////////////////////////////////////////////////
1790
1791STDMETHODIMP GuestProcess::Read(ULONG aHandle, ULONG aToRead, ULONG aTimeoutMS, ComSafeArrayOut(BYTE, aData))
1792{
1793#ifndef VBOX_WITH_GUEST_CONTROL
1794 ReturnComNotImplemented();
1795#else
1796 LogFlowThisFuncEnter();
1797
1798 if (aToRead == 0)
1799 return setError(E_INVALIDARG, tr("The size to read is zero"));
1800 CheckComArgOutSafeArrayPointerValid(aData);
1801
1802 AutoCaller autoCaller(this);
1803 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1804
1805 com::SafeArray<BYTE> data((size_t)aToRead);
1806 Assert(data.size() >= aToRead);
1807
1808 HRESULT hr = S_OK;
1809
1810 uint32_t cbRead; int guestRc;
1811 int vrc = readData(aHandle, aToRead, aTimeoutMS, data.raw(), aToRead, &cbRead, &guestRc);
1812 if (RT_SUCCESS(vrc))
1813 {
1814 if (data.size() != cbRead)
1815 data.resize(cbRead);
1816 data.detachTo(ComSafeArrayOutArg(aData));
1817 }
1818 else
1819 {
1820 switch (vrc)
1821 {
1822 case VERR_GSTCTL_GUEST_ERROR:
1823 hr = GuestProcess::setErrorExternal(this, guestRc);
1824 break;
1825
1826 default:
1827 hr = setError(VBOX_E_IPRT_ERROR,
1828 tr("Reading from process \"%s\" (PID %RU32) failed: %Rrc"),
1829 mData.mProcess.mCommand.c_str(), mData.mPID, vrc);
1830 break;
1831 }
1832 }
1833
1834 LogFlowThisFunc(("rc=%Rrc, cbRead=%RU32\n", vrc, cbRead));
1835
1836 LogFlowFuncLeaveRC(vrc);
1837 return hr;
1838#endif /* VBOX_WITH_GUEST_CONTROL */
1839}
1840
1841STDMETHODIMP GuestProcess::Terminate(void)
1842{
1843#ifndef VBOX_WITH_GUEST_CONTROL
1844 ReturnComNotImplemented();
1845#else
1846 LogFlowThisFuncEnter();
1847
1848 AutoCaller autoCaller(this);
1849 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1850
1851 HRESULT hr = S_OK;
1852
1853 int guestRc;
1854 int vrc = terminateProcess(30 * 1000 /* Timeout in ms */,
1855 &guestRc);
1856 if (RT_FAILURE(vrc))
1857 {
1858 switch (vrc)
1859 {
1860 case VERR_GSTCTL_GUEST_ERROR:
1861 hr = GuestProcess::setErrorExternal(this, guestRc);
1862 break;
1863
1864 case VERR_NOT_SUPPORTED:
1865 hr = setError(VBOX_E_IPRT_ERROR,
1866 tr("Terminating process \"%s\" (PID %RU32) not supported by installed Guest Additions"),
1867 mData.mProcess.mCommand.c_str(), mData.mPID);
1868 break;
1869
1870 default:
1871 hr = setError(VBOX_E_IPRT_ERROR,
1872 tr("Terminating process \"%s\" (PID %RU32) failed: %Rrc"),
1873 mData.mProcess.mCommand.c_str(), mData.mPID, vrc);
1874 break;
1875 }
1876 }
1877
1878 /* Remove process from guest session list. Now only API clients
1879 * still can hold references to it. */
1880 AssertPtr(mSession);
1881 mSession->processRemoveFromList(this);
1882
1883 LogFlowFuncLeaveRC(vrc);
1884 return hr;
1885#endif /* VBOX_WITH_GUEST_CONTROL */
1886}
1887
1888STDMETHODIMP GuestProcess::WaitFor(ULONG aWaitFlags, ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
1889{
1890#ifndef VBOX_WITH_GUEST_CONTROL
1891 ReturnComNotImplemented();
1892#else
1893 LogFlowThisFuncEnter();
1894
1895 CheckComArgOutPointerValid(aReason);
1896
1897 AutoCaller autoCaller(this);
1898 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1899
1900 /*
1901 * Note: Do not hold any locks here while waiting!
1902 */
1903 HRESULT hr = S_OK;
1904
1905 int guestRc; ProcessWaitResult_T waitResult;
1906 int vrc = waitFor(aWaitFlags, aTimeoutMS, waitResult, &guestRc);
1907 if (RT_SUCCESS(vrc))
1908 {
1909 *aReason = waitResult;
1910 }
1911 else
1912 {
1913 switch (vrc)
1914 {
1915 case VERR_GSTCTL_GUEST_ERROR:
1916 hr = GuestProcess::setErrorExternal(this, guestRc);
1917 break;
1918
1919 case VERR_TIMEOUT:
1920 *aReason = ProcessWaitResult_Timeout;
1921 break;
1922
1923 default:
1924 hr = setError(VBOX_E_IPRT_ERROR,
1925 tr("Waiting for process \"%s\" (PID %RU32) failed: %Rrc"),
1926 mData.mProcess.mCommand.c_str(), mData.mPID, vrc);
1927 break;
1928 }
1929 }
1930
1931 LogFlowFuncLeaveRC(vrc);
1932 return hr;
1933#endif /* VBOX_WITH_GUEST_CONTROL */
1934}
1935
1936STDMETHODIMP GuestProcess::WaitForArray(ComSafeArrayIn(ProcessWaitForFlag_T, aFlags), ULONG aTimeoutMS, ProcessWaitResult_T *aReason)
1937{
1938#ifndef VBOX_WITH_GUEST_CONTROL
1939 ReturnComNotImplemented();
1940#else
1941 LogFlowThisFuncEnter();
1942
1943 CheckComArgOutPointerValid(aReason);
1944
1945 AutoCaller autoCaller(this);
1946 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1947
1948 /*
1949 * Note: Do not hold any locks here while waiting!
1950 */
1951 uint32_t fWaitFor = ProcessWaitForFlag_None;
1952 com::SafeArray<ProcessWaitForFlag_T> flags(ComSafeArrayInArg(aFlags));
1953 for (size_t i = 0; i < flags.size(); i++)
1954 fWaitFor |= flags[i];
1955
1956 return WaitFor(fWaitFor, aTimeoutMS, aReason);
1957#endif /* VBOX_WITH_GUEST_CONTROL */
1958}
1959
1960STDMETHODIMP GuestProcess::Write(ULONG aHandle, ULONG aFlags,
1961 ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
1962{
1963#ifndef VBOX_WITH_GUEST_CONTROL
1964 ReturnComNotImplemented();
1965#else
1966 LogFlowThisFuncEnter();
1967
1968 CheckComArgSafeArrayNotNull(aData);
1969 CheckComArgOutPointerValid(aWritten);
1970
1971 AutoCaller autoCaller(this);
1972 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1973
1974 com::SafeArray<BYTE> data(ComSafeArrayInArg(aData));
1975
1976 HRESULT hr = S_OK;
1977
1978 uint32_t cbWritten; int guestRc;
1979 int vrc = writeData(aHandle, aFlags, data.raw(), data.size(), aTimeoutMS, &cbWritten, &guestRc);
1980 if (RT_FAILURE(vrc))
1981 {
1982 switch (vrc)
1983 {
1984 case VERR_GSTCTL_GUEST_ERROR:
1985 hr = GuestProcess::setErrorExternal(this, guestRc);
1986 break;
1987
1988 default:
1989 hr = setError(VBOX_E_IPRT_ERROR,
1990 tr("Writing to process \"%s\" (PID %RU32) failed: %Rrc"),
1991 mData.mProcess.mCommand.c_str(), mData.mPID, vrc);
1992 break;
1993 }
1994 }
1995
1996 LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, cbWritten));
1997
1998 *aWritten = (ULONG)cbWritten;
1999
2000 LogFlowFuncLeaveRC(vrc);
2001 return hr;
2002#endif /* VBOX_WITH_GUEST_CONTROL */
2003}
2004
2005STDMETHODIMP GuestProcess::WriteArray(ULONG aHandle, ComSafeArrayIn(ProcessInputFlag_T, aFlags),
2006 ComSafeArrayIn(BYTE, aData), ULONG aTimeoutMS, ULONG *aWritten)
2007{
2008#ifndef VBOX_WITH_GUEST_CONTROL
2009 ReturnComNotImplemented();
2010#else
2011 LogFlowThisFuncEnter();
2012
2013 CheckComArgSafeArrayNotNull(aData);
2014 CheckComArgOutPointerValid(aWritten);
2015
2016 AutoCaller autoCaller(this);
2017 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2018
2019 /*
2020 * Note: Do not hold any locks here while writing!
2021 */
2022 ULONG fWrite = ProcessInputFlag_None;
2023 com::SafeArray<ProcessInputFlag_T> flags(ComSafeArrayInArg(aFlags));
2024 for (size_t i = 0; i < flags.size(); i++)
2025 fWrite |= flags[i];
2026
2027 return Write(aHandle, fWrite, ComSafeArrayInArg(aData), aTimeoutMS, aWritten);
2028#endif /* VBOX_WITH_GUEST_CONTROL */
2029}
2030
2031///////////////////////////////////////////////////////////////////////////////
2032
2033GuestProcessTool::GuestProcessTool(void)
2034 : pSession(NULL),
2035 pProcess(NULL)
2036{
2037}
2038
2039GuestProcessTool::~GuestProcessTool(void)
2040{
2041 Terminate(30 * 1000, NULL /* pGuestRc */);
2042}
2043
2044int GuestProcessTool::Init(GuestSession *pGuestSession, const GuestProcessStartupInfo &startupInfo,
2045 bool fAsync, int *pGuestRc)
2046{
2047 LogFlowThisFunc(("pGuestSession=%p, szCmd=%s, fAsync=%RTbool\n",
2048 pGuestSession, startupInfo.mCommand.c_str(), fAsync));
2049
2050 AssertPtrReturn(pGuestSession, VERR_INVALID_POINTER);
2051
2052 pSession = pGuestSession;
2053 mStartupInfo = startupInfo;
2054
2055 /* Make sure the process is hidden. */
2056 mStartupInfo.mFlags |= ProcessCreateFlag_Hidden;
2057
2058 int vrc = pSession->processCreateExInteral(mStartupInfo, pProcess);
2059 if (RT_SUCCESS(vrc))
2060 vrc = fAsync
2061 ? pProcess->startProcessAsync()
2062 : pProcess->startProcess(30 * 1000 /* 30s timeout */, pGuestRc);
2063
2064 if ( RT_SUCCESS(vrc)
2065 && !fAsync
2066 && ( pGuestRc
2067 && RT_FAILURE(*pGuestRc)
2068 )
2069 )
2070 {
2071 vrc = VERR_GSTCTL_GUEST_ERROR;
2072 }
2073
2074 LogFlowFuncLeaveRC(vrc);
2075 return vrc;
2076}
2077
2078int GuestProcessTool::GetCurrentBlock(uint32_t uHandle, GuestProcessStreamBlock &strmBlock)
2079{
2080 const GuestProcessStream *pStream = NULL;
2081 if (uHandle == OUTPUT_HANDLE_ID_STDOUT)
2082 pStream = &mStdOut;
2083 else if (uHandle == OUTPUT_HANDLE_ID_STDERR)
2084 pStream = &mStdErr;
2085
2086 if (!pStream)
2087 return VERR_INVALID_PARAMETER;
2088
2089 int vrc;
2090 do
2091 {
2092 /* Try parsing the data to see if the current block is complete. */
2093 vrc = mStdOut.ParseBlock(strmBlock);
2094 if (strmBlock.GetCount())
2095 break;
2096 } while (RT_SUCCESS(vrc));
2097
2098 LogFlowThisFunc(("rc=%Rrc, %RU64 pairs\n",
2099 vrc, strmBlock.GetCount()));
2100 return vrc;
2101}
2102
2103bool GuestProcessTool::IsRunning(void)
2104{
2105 AssertReturn(!pProcess.isNull(), true);
2106
2107 ProcessStatus_T procStatus = ProcessStatus_Undefined;
2108 HRESULT hr = pProcess->COMGETTER(Status(&procStatus));
2109 Assert(SUCCEEDED(hr));
2110
2111 if ( procStatus == ProcessStatus_Started
2112 || procStatus == ProcessStatus_Paused
2113 || procStatus == ProcessStatus_Terminating)
2114 {
2115 return true;
2116 }
2117
2118 return false;
2119}
2120
2121/* static */
2122int GuestProcessTool::Run( GuestSession *pGuestSession,
2123 const GuestProcessStartupInfo &startupInfo,
2124 int *pGuestRc)
2125{
2126 return RunEx(pGuestSession, startupInfo,
2127 NULL /* pStrmOutObjects */, 0 /* cStrmOutObjects */,
2128 pGuestRc);
2129}
2130
2131/* static */
2132int GuestProcessTool::RunEx( GuestSession *pGuestSession,
2133 const GuestProcessStartupInfo &startupInfo,
2134 GuestCtrlStreamObjects *pStrmOutObjects,
2135 uint32_t cStrmOutObjects,
2136 int *pGuestRc)
2137{
2138 GuestProcessTool procTool; int guestRc;
2139 int vrc = procTool.Init(pGuestSession, startupInfo, false /* Async */, &guestRc);
2140 if (RT_SUCCESS(vrc))
2141 {
2142 while (cStrmOutObjects--)
2143 {
2144 try
2145 {
2146 GuestProcessStreamBlock strmBlk;
2147 vrc = procTool.WaitEx( pStrmOutObjects
2148 ? GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK
2149 : GUESTPROCESSTOOL_FLAG_NONE, &strmBlk, &guestRc);
2150 if (pStrmOutObjects)
2151 pStrmOutObjects->push_back(strmBlk);
2152 }
2153 catch (std::bad_alloc)
2154 {
2155 vrc = VERR_NO_MEMORY;
2156 }
2157 }
2158 }
2159
2160 if (RT_SUCCESS(vrc))
2161 {
2162 /* Make sure the process runs until completion. */
2163 vrc = procTool.Wait(GUESTPROCESSTOOL_FLAG_NONE, &guestRc);
2164 if (RT_SUCCESS(vrc))
2165 {
2166 guestRc = procTool.TerminatedOk(NULL /* Exit code */);
2167 if (RT_FAILURE(guestRc))
2168 vrc = VERR_GSTCTL_GUEST_ERROR;
2169 }
2170 }
2171
2172 if (pGuestRc)
2173 *pGuestRc = guestRc;
2174
2175 LogFlowFunc(("Returned rc=%Rrc, guestRc=%Rrc\n", vrc, guestRc));
2176 return vrc;
2177}
2178
2179int GuestProcessTool::TerminatedOk(LONG *pExitCode)
2180{
2181 Assert(!pProcess.isNull());
2182 /* pExitCode is optional. */
2183
2184 int vrc;
2185 if (!IsRunning())
2186 {
2187 LONG exitCode;
2188 HRESULT hr = pProcess->COMGETTER(ExitCode(&exitCode));
2189 Assert(SUCCEEDED(hr));
2190
2191 if (pExitCode)
2192 *pExitCode = exitCode;
2193
2194 vrc = (exitCode != 0)
2195 /** @todo Special guest control rc needed! */
2196 ? VERR_NOT_EQUAL : VINF_SUCCESS;
2197 }
2198 else
2199 vrc = VERR_INVALID_STATE; /** @todo Special guest control rc needed! */
2200
2201 LogFlowFuncLeaveRC(vrc);
2202 return vrc;
2203}
2204
2205int GuestProcessTool::Wait(uint32_t fFlags, int *pGuestRc)
2206{
2207 return WaitEx(fFlags, NULL /* pStrmBlkOut */, pGuestRc);
2208}
2209
2210int GuestProcessTool::WaitEx(uint32_t fFlags, GuestProcessStreamBlock *pStrmBlkOut, int *pGuestRc)
2211{
2212 LogFlowThisFunc(("fFlags=0x%x, pStreamBlock=%p, pGuestRc=%p\n",
2213 fFlags, pStrmBlkOut, pGuestRc));
2214
2215 /* Can we parse the next block without waiting? */
2216 int vrc;
2217 if (fFlags & GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK)
2218 {
2219 AssertPtr(pStrmBlkOut);
2220 vrc = GetCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, *pStrmBlkOut);
2221 if (RT_SUCCESS(vrc))
2222 return vrc;
2223 /* else do the the waiting below. */
2224 }
2225
2226 /* Do the waiting. */
2227 uint32_t fWaitFlags = ProcessWaitForFlag_Terminate;
2228 if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdOut)
2229 fWaitFlags |= ProcessWaitForFlag_StdOut;
2230 if (mStartupInfo.mFlags & ProcessCreateFlag_WaitForStdErr)
2231 fWaitFlags |= ProcessWaitForFlag_StdErr;
2232
2233 /** @todo Decrease timeout while running. */
2234 uint64_t u64StartMS = RTTimeMilliTS();
2235 uint32_t uTimeoutMS = mStartupInfo.mTimeoutMS;
2236
2237 int guestRc;
2238 bool fDone = false;
2239
2240 BYTE byBuf[_64K];
2241 uint32_t cbRead;
2242
2243 bool fHandleStdOut = false;
2244 bool fHandleStdErr = false;
2245
2246 /**
2247 * Updates the elapsed time and checks if a
2248 * timeout happened, then breaking out of the loop.
2249 */
2250#define UPDATE_AND_CHECK_ELAPSED_TIME() \
2251 u64ElapsedMS = RTTimeMilliTS() - u64StartMS; \
2252 if ( uTimeoutMS != RT_INDEFINITE_WAIT \
2253 && u64ElapsedMS >= uTimeoutMS) \
2254 { \
2255 vrc = VERR_TIMEOUT; \
2256 break; \
2257 }
2258
2259 /**
2260 * Returns the remaining time (in ms).
2261 */
2262#define GET_REMAINING_TIME \
2263 uTimeoutMS == RT_INDEFINITE_WAIT \
2264 ? RT_INDEFINITE_WAIT : uTimeoutMS - (uint32_t)u64ElapsedMS \
2265
2266 ProcessWaitResult_T waitRes;
2267 do
2268 {
2269 uint64_t u64ElapsedMS;
2270 UPDATE_AND_CHECK_ELAPSED_TIME();
2271
2272 vrc = pProcess->waitFor(fWaitFlags, GET_REMAINING_TIME,
2273 waitRes, &guestRc);
2274 if (RT_FAILURE(vrc))
2275 break;
2276
2277 switch (waitRes)
2278 {
2279 case ProcessWaitResult_StdIn:
2280 vrc = VERR_NOT_IMPLEMENTED;
2281 break;
2282
2283 case ProcessWaitResult_StdOut:
2284 fHandleStdOut = true;
2285 break;
2286
2287 case ProcessWaitResult_StdErr:
2288 fHandleStdErr = true;
2289 break;
2290
2291 case ProcessWaitResult_WaitFlagNotSupported:
2292 if (fWaitFlags & ProcessWaitForFlag_StdOut)
2293 fHandleStdOut = true;
2294 if (fWaitFlags & ProcessWaitForFlag_StdErr)
2295 fHandleStdErr = true;
2296 /* Since waiting for stdout / stderr is not supported by the guest,
2297 * wait a bit to not hog the CPU too much when polling for data. */
2298 RTThreadSleep(1); /* Optional, don't check rc. */
2299 break;
2300
2301 case ProcessWaitResult_Error:
2302 vrc = VERR_GSTCTL_GUEST_ERROR;
2303 break;
2304
2305 case ProcessWaitResult_Terminate:
2306 fDone = true;
2307 break;
2308
2309 case ProcessWaitResult_Timeout:
2310 vrc = VERR_TIMEOUT;
2311 break;
2312
2313 case ProcessWaitResult_Start:
2314 case ProcessWaitResult_Status:
2315 /* Not used here, just skip. */
2316 break;
2317
2318 default:
2319 AssertMsgFailed(("Unhandled process wait result %RU32\n", waitRes));
2320 break;
2321 }
2322
2323 if (RT_FAILURE(vrc))
2324 break;
2325
2326 if (fHandleStdOut)
2327 {
2328 UPDATE_AND_CHECK_ELAPSED_TIME();
2329
2330 cbRead = 0;
2331 vrc = pProcess->readData(OUTPUT_HANDLE_ID_STDOUT, sizeof(byBuf),
2332 GET_REMAINING_TIME,
2333 byBuf, sizeof(byBuf),
2334 &cbRead, &guestRc);
2335 if ( RT_FAILURE(vrc)
2336 || vrc == VWRN_GSTCTL_OBJECTSTATE_CHANGED)
2337 break;
2338
2339 if (cbRead)
2340 {
2341 LogFlowThisFunc(("Received %RU32 bytes from stdout\n", cbRead));
2342 vrc = mStdOut.AddData(byBuf, cbRead);
2343
2344 if ( RT_SUCCESS(vrc)
2345 && (fFlags & GUESTPROCESSTOOL_FLAG_STDOUT_BLOCK))
2346 {
2347 AssertPtr(pStrmBlkOut);
2348 vrc = GetCurrentBlock(OUTPUT_HANDLE_ID_STDOUT, *pStrmBlkOut);
2349
2350 /* When successful, break out of the loop because we're done
2351 * with reading the first stream block. */
2352 if (RT_SUCCESS(vrc))
2353 fDone = true;
2354 }
2355 }
2356
2357 fHandleStdOut = false;
2358 }
2359
2360 if (fHandleStdErr)
2361 {
2362 UPDATE_AND_CHECK_ELAPSED_TIME();
2363
2364 cbRead = 0;
2365 vrc = pProcess->readData(OUTPUT_HANDLE_ID_STDERR, sizeof(byBuf),
2366 GET_REMAINING_TIME,
2367 byBuf, sizeof(byBuf),
2368 &cbRead, &guestRc);
2369 if ( RT_FAILURE(vrc)
2370 || vrc == VWRN_GSTCTL_OBJECTSTATE_CHANGED)
2371 break;
2372
2373 if (cbRead)
2374 {
2375 LogFlowThisFunc(("Received %RU32 bytes from stderr\n", cbRead));
2376 vrc = mStdErr.AddData(byBuf, cbRead);
2377 }
2378
2379 fHandleStdErr = false;
2380 }
2381
2382 } while (!fDone && RT_SUCCESS(vrc));
2383
2384#undef UPDATE_AND_CHECK_ELAPSED_TIME
2385#undef GET_REMAINING_TIME
2386
2387 if (RT_FAILURE(guestRc))
2388 vrc = VERR_GSTCTL_GUEST_ERROR;
2389
2390 LogFlowThisFunc(("Loop ended with rc=%Rrc, guestRc=%Rrc, waitRes=%RU32\n",
2391 vrc, guestRc, waitRes));
2392 if (pGuestRc)
2393 *pGuestRc = guestRc;
2394
2395 LogFlowFuncLeaveRC(vrc);
2396 return vrc;
2397}
2398
2399int GuestProcessTool::Terminate(uint32_t uTimeoutMS, int *pGuestRc)
2400{
2401 LogFlowThisFuncEnter();
2402
2403 int rc = VINF_SUCCESS;
2404 if (!pProcess.isNull())
2405 {
2406 rc = pProcess->terminateProcess(uTimeoutMS, pGuestRc);
2407 pProcess.setNull();
2408 }
2409 else
2410 rc = VERR_NOT_FOUND;
2411
2412 LogFlowFuncLeaveRC(rc);
2413 return rc;
2414}
2415
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette