VirtualBox

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

Last change on this file since 71179 was 71177, checked in by vboxsync, 7 years ago

Guest Control/GuestProcessImpl: Unified VERR_FILE_NOT_FOUND and VERR_PATH_NOT_FOUND guest errors in i_guestErrorToString().

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