VirtualBox

source: vbox/trunk/src/VBox/Main/GuestImpl.cpp@ 28345

Last change on this file since 28345 was 28307, checked in by vboxsync, 15 years ago

Guest Control: Update (another error handling).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.0 KB
Line 
1/* $Id: GuestImpl.cpp 28307 2010-04-14 15:00:50Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include "GuestImpl.h"
25
26#include "Global.h"
27#include "ConsoleImpl.h"
28#include "ProgressImpl.h"
29#include "VMMDev.h"
30
31#include "AutoCaller.h"
32#include "Logging.h"
33
34#include <VBox/VMMDev.h>
35#ifdef VBOX_WITH_GUEST_CONTROL
36# include <VBox/com/array.h>
37#endif
38#include <iprt/cpp/utils.h>
39#include <iprt/getopt.h>
40#include <VBox/pgm.h>
41
42// defines
43/////////////////////////////////////////////////////////////////////////////
44
45// constructor / destructor
46/////////////////////////////////////////////////////////////////////////////
47
48DEFINE_EMPTY_CTOR_DTOR (Guest)
49
50HRESULT Guest::FinalConstruct()
51{
52 return S_OK;
53}
54
55void Guest::FinalRelease()
56{
57 uninit ();
58}
59
60// public methods only for internal purposes
61/////////////////////////////////////////////////////////////////////////////
62
63/**
64 * Initializes the guest object.
65 */
66HRESULT Guest::init (Console *aParent)
67{
68 LogFlowThisFunc(("aParent=%p\n", aParent));
69
70 ComAssertRet(aParent, E_INVALIDARG);
71
72 /* Enclose the state transition NotReady->InInit->Ready */
73 AutoInitSpan autoInitSpan(this);
74 AssertReturn(autoInitSpan.isOk(), E_FAIL);
75
76 unconst(mParent) = aParent;
77
78 /* mData.mAdditionsActive is FALSE */
79
80 /* Confirm a successful initialization when it's the case */
81 autoInitSpan.setSucceeded();
82
83 ULONG aMemoryBalloonSize;
84 HRESULT ret = mParent->machine()->COMGETTER(MemoryBalloonSize)(&aMemoryBalloonSize);
85 if (ret == S_OK)
86 mMemoryBalloonSize = aMemoryBalloonSize;
87 else
88 mMemoryBalloonSize = 0; /* Default is no ballooning */
89
90 mStatUpdateInterval = 0; /* Default is not to report guest statistics at all */
91
92 /* Clear statistics. */
93 for (unsigned i = 0 ; i < GUESTSTATTYPE_MAX; i++)
94 mCurrentGuestStat[i] = 0;
95
96#ifdef VBOX_WITH_GUEST_CONTROL
97 /* Init the context ID counter at 1000. */
98 mNextContextID = 1000;
99#endif
100
101 return S_OK;
102}
103
104/**
105 * Uninitializes the instance and sets the ready flag to FALSE.
106 * Called either from FinalRelease() or by the parent when it gets destroyed.
107 */
108void Guest::uninit()
109{
110 LogFlowThisFunc(("\n"));
111
112#ifdef VBOX_WITH_GUEST_CONTROL
113 /* Clean up callback data. */
114 CallbackListIter it;
115 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
116 removeCtrlCallbackContext(it);
117#endif
118
119 /* Enclose the state transition Ready->InUninit->NotReady */
120 AutoUninitSpan autoUninitSpan(this);
121 if (autoUninitSpan.uninitDone())
122 return;
123
124 unconst(mParent) = NULL;
125}
126
127// IGuest properties
128/////////////////////////////////////////////////////////////////////////////
129
130STDMETHODIMP Guest::COMGETTER(OSTypeId) (BSTR *aOSTypeId)
131{
132 CheckComArgOutPointerValid(aOSTypeId);
133
134 AutoCaller autoCaller(this);
135 if (FAILED(autoCaller.rc())) return autoCaller.rc();
136
137 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
138
139 // redirect the call to IMachine if no additions are installed
140 if (mData.mAdditionsVersion.isEmpty())
141 return mParent->machine()->COMGETTER(OSTypeId)(aOSTypeId);
142
143 mData.mOSTypeId.cloneTo(aOSTypeId);
144
145 return S_OK;
146}
147
148STDMETHODIMP Guest::COMGETTER(AdditionsActive) (BOOL *aAdditionsActive)
149{
150 CheckComArgOutPointerValid(aAdditionsActive);
151
152 AutoCaller autoCaller(this);
153 if (FAILED(autoCaller.rc())) return autoCaller.rc();
154
155 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
156
157 *aAdditionsActive = mData.mAdditionsActive;
158
159 return S_OK;
160}
161
162STDMETHODIMP Guest::COMGETTER(AdditionsVersion) (BSTR *aAdditionsVersion)
163{
164 CheckComArgOutPointerValid(aAdditionsVersion);
165
166 AutoCaller autoCaller(this);
167 if (FAILED(autoCaller.rc())) return autoCaller.rc();
168
169 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
170
171 mData.mAdditionsVersion.cloneTo(aAdditionsVersion);
172
173 return S_OK;
174}
175
176STDMETHODIMP Guest::COMGETTER(SupportsSeamless) (BOOL *aSupportsSeamless)
177{
178 CheckComArgOutPointerValid(aSupportsSeamless);
179
180 AutoCaller autoCaller(this);
181 if (FAILED(autoCaller.rc())) return autoCaller.rc();
182
183 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
184
185 *aSupportsSeamless = mData.mSupportsSeamless;
186
187 return S_OK;
188}
189
190STDMETHODIMP Guest::COMGETTER(SupportsGraphics) (BOOL *aSupportsGraphics)
191{
192 CheckComArgOutPointerValid(aSupportsGraphics);
193
194 AutoCaller autoCaller(this);
195 if (FAILED(autoCaller.rc())) return autoCaller.rc();
196
197 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
198
199 *aSupportsGraphics = mData.mSupportsGraphics;
200
201 return S_OK;
202}
203
204STDMETHODIMP Guest::COMGETTER(MemoryBalloonSize) (ULONG *aMemoryBalloonSize)
205{
206 CheckComArgOutPointerValid(aMemoryBalloonSize);
207
208 AutoCaller autoCaller(this);
209 if (FAILED(autoCaller.rc())) return autoCaller.rc();
210
211 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
212
213 *aMemoryBalloonSize = mMemoryBalloonSize;
214
215 return S_OK;
216}
217
218STDMETHODIMP Guest::COMSETTER(MemoryBalloonSize) (ULONG aMemoryBalloonSize)
219{
220 AutoCaller autoCaller(this);
221 if (FAILED(autoCaller.rc())) return autoCaller.rc();
222
223 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
224
225 HRESULT ret = mParent->machine()->COMSETTER(MemoryBalloonSize)(aMemoryBalloonSize);
226 if (ret == S_OK)
227 {
228 mMemoryBalloonSize = aMemoryBalloonSize;
229 /* forward the information to the VMM device */
230 VMMDev *vmmDev = mParent->getVMMDev();
231 if (vmmDev)
232 vmmDev->getVMMDevPort()->pfnSetMemoryBalloon(vmmDev->getVMMDevPort(), aMemoryBalloonSize);
233 }
234
235 return ret;
236}
237
238STDMETHODIMP Guest::COMGETTER(StatisticsUpdateInterval)(ULONG *aUpdateInterval)
239{
240 CheckComArgOutPointerValid(aUpdateInterval);
241
242 AutoCaller autoCaller(this);
243 if (FAILED(autoCaller.rc())) return autoCaller.rc();
244
245 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
246
247 *aUpdateInterval = mStatUpdateInterval;
248 return S_OK;
249}
250
251STDMETHODIMP Guest::COMSETTER(StatisticsUpdateInterval)(ULONG aUpdateInterval)
252{
253 AutoCaller autoCaller(this);
254 if (FAILED(autoCaller.rc())) return autoCaller.rc();
255
256 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
257
258 mStatUpdateInterval = aUpdateInterval;
259 /* forward the information to the VMM device */
260 VMMDev *vmmDev = mParent->getVMMDev();
261 if (vmmDev)
262 vmmDev->getVMMDevPort()->pfnSetStatisticsInterval(vmmDev->getVMMDevPort(), aUpdateInterval);
263
264 return S_OK;
265}
266
267STDMETHODIMP Guest::InternalGetStatistics(ULONG *aCpuUser, ULONG *aCpuKernel, ULONG *aCpuIdle,
268 ULONG *aMemTotal, ULONG *aMemFree, ULONG *aMemBalloon,
269 ULONG *aMemCache, ULONG *aPageTotal,
270 ULONG *aMemAllocTotal, ULONG *aMemFreeTotal, ULONG *aMemBalloonTotal)
271{
272 CheckComArgOutPointerValid(aCpuUser);
273 CheckComArgOutPointerValid(aCpuKernel);
274 CheckComArgOutPointerValid(aCpuIdle);
275 CheckComArgOutPointerValid(aMemTotal);
276 CheckComArgOutPointerValid(aMemFree);
277 CheckComArgOutPointerValid(aMemBalloon);
278 CheckComArgOutPointerValid(aMemCache);
279 CheckComArgOutPointerValid(aPageTotal);
280
281 AutoCaller autoCaller(this);
282 if (FAILED(autoCaller.rc())) return autoCaller.rc();
283
284 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
285
286 *aCpuUser = mCurrentGuestStat[GUESTSTATTYPE_CPUUSER] * (_4K/_1K); /* page (4K) -> 1 KB units */
287 *aCpuKernel = mCurrentGuestStat[GUESTSTATTYPE_CPUKERNEL] * (_4K/_1K);
288 *aCpuIdle = mCurrentGuestStat[GUESTSTATTYPE_CPUIDLE] * (_4K/_1K);
289 *aMemTotal = mCurrentGuestStat[GUESTSTATTYPE_MEMTOTAL] * (_4K/_1K);
290 *aMemFree = mCurrentGuestStat[GUESTSTATTYPE_MEMFREE] * (_4K/_1K);
291 *aMemBalloon = mCurrentGuestStat[GUESTSTATTYPE_MEMBALLOON] * (_4K/_1K);
292 *aMemCache = mCurrentGuestStat[GUESTSTATTYPE_MEMCACHE] * (_4K/_1K);
293 *aPageTotal = mCurrentGuestStat[GUESTSTATTYPE_PAGETOTAL] * (_4K/_1K);
294
295 Console::SafeVMPtr pVM (mParent);
296 if (pVM.isOk())
297 {
298 uint64_t uFreeTotal, uAllocTotal, uBalloonedTotal;
299 *aMemFreeTotal = 0;
300 int rc = PGMR3QueryVMMMemoryStats(pVM.raw(), &uAllocTotal, &uFreeTotal, &uBalloonedTotal);
301 AssertRC(rc);
302 if (rc == VINF_SUCCESS)
303 {
304 *aMemAllocTotal = (ULONG)(uAllocTotal / _1K); /* bytes -> KB */
305 *aMemFreeTotal = (ULONG)(uFreeTotal / _1K);
306 *aMemBalloonTotal = (ULONG)(uBalloonedTotal / _1K);
307 }
308 }
309 else
310 *aMemFreeTotal = 0;
311
312 return S_OK;
313}
314
315HRESULT Guest::SetStatistic(ULONG aCpuId, GUESTSTATTYPE enmType, ULONG aVal)
316{
317 AutoCaller autoCaller(this);
318 if (FAILED(autoCaller.rc())) return autoCaller.rc();
319
320 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
321
322 if (enmType >= GUESTSTATTYPE_MAX)
323 return E_INVALIDARG;
324
325 mCurrentGuestStat[enmType] = aVal;
326 return S_OK;
327}
328
329STDMETHODIMP Guest::SetCredentials(IN_BSTR aUserName, IN_BSTR aPassword,
330 IN_BSTR aDomain, BOOL aAllowInteractiveLogon)
331{
332 AutoCaller autoCaller(this);
333 if (FAILED(autoCaller.rc())) return autoCaller.rc();
334
335 /* forward the information to the VMM device */
336 VMMDev *vmmDev = mParent->getVMMDev();
337 if (vmmDev)
338 {
339 uint32_t u32Flags = VMMDEV_SETCREDENTIALS_GUESTLOGON;
340 if (!aAllowInteractiveLogon)
341 u32Flags = VMMDEV_SETCREDENTIALS_NOLOCALLOGON;
342
343 vmmDev->getVMMDevPort()->pfnSetCredentials(vmmDev->getVMMDevPort(),
344 Utf8Str(aUserName).raw(), Utf8Str(aPassword).raw(),
345 Utf8Str(aDomain).raw(), u32Flags);
346 return S_OK;
347 }
348
349 return setError(VBOX_E_VM_ERROR,
350 tr("VMM device is not available (is the VM running?)"));
351}
352
353#ifdef VBOX_WITH_GUEST_CONTROL
354/**
355 * Creates the argument list as an array used for executing a program.
356 *
357 * @returns VBox status code.
358 *
359 * @todo
360 *
361 * @todo Respect spaces when quoting for arguments, e.g. "c:\\program files\\".
362 * @todo Handle empty ("") argguments.
363 */
364int Guest::prepareExecuteArgs(const char *pszArgs, void **ppvList, uint32_t *pcbList, uint32_t *pcArgs)
365{
366 char **ppaArg;
367 int iArgs;
368 int rc = RTGetOptArgvFromString(&ppaArg, &iArgs, pszArgs, NULL);
369 if (RT_SUCCESS(rc))
370 {
371 char *pszTemp = NULL;
372 *pcbList = 0;
373 for (int i=0; i<iArgs; i++)
374 {
375 if (i > 0) /* Insert space as delimiter. */
376 rc = RTStrAAppendN(&pszTemp, " ", 1);
377
378 if (RT_FAILURE(rc))
379 break;
380 else
381 {
382 rc = RTStrAAppendN(&pszTemp, ppaArg[i], strlen(ppaArg[i]));
383 if (RT_FAILURE(rc))
384 break;
385 }
386 }
387 RTGetOptArgvFree(ppaArg);
388 if (RT_SUCCESS(rc))
389 {
390 *ppvList = pszTemp;
391 *pcArgs = iArgs;
392 if (pszTemp)
393 *pcbList = strlen(pszTemp) + 1; /* Include zero termination. */
394 }
395 else
396 RTStrFree(pszTemp);
397 }
398 return rc;
399}
400
401/**
402 * Appends environment variables to the environment block. Each var=value pair is separated
403 * by NULL (\0) sequence. The whole block will be stored in one blob and disassembled on the
404 * guest side later to fit into the HGCM param structure.
405 *
406 * @returns VBox status code.
407 *
408 * @todo
409 *
410 */
411int Guest::prepareExecuteEnv(const char *pszEnv, void **ppvList, uint32_t *pcbList, uint32_t *pcEnv)
412{
413 int rc = VINF_SUCCESS;
414 uint32_t cbLen = strlen(pszEnv);
415 if (*ppvList)
416 {
417 uint32_t cbNewLen = *pcbList + cbLen + 1; /* Include zero termination. */
418 char *pvTmp = (char*)RTMemRealloc(*ppvList, cbNewLen);
419 if (NULL == pvTmp)
420 {
421 rc = VERR_NO_MEMORY;
422 }
423 else
424 {
425 memcpy(pvTmp + *pcbList, pszEnv, cbLen);
426 pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
427 *ppvList = (void**)pvTmp;
428 }
429 }
430 else
431 {
432 char *pcTmp;
433 if (RTStrAPrintf(&pcTmp, "%s", pszEnv) > 0)
434 {
435 *ppvList = (void**)pcTmp;
436 /* Reset counters. */
437 *pcEnv = 0;
438 *pcbList = 0;
439 }
440 }
441 if (RT_SUCCESS(rc))
442 {
443 *pcbList += cbLen + 1; /* Include zero termination. */
444 *pcEnv += 1; /* Increase env pairs count. */
445 }
446 return rc;
447}
448
449/**
450 * Static callback function for receiving updates on guest control commands
451 * from the guest. Acts as a dispatcher for the actual class instance.
452 *
453 * @returns VBox status code.
454 *
455 * @todo
456 *
457 */
458DECLCALLBACK(int) Guest::doGuestCtrlNotification(void *pvExtension,
459 uint32_t u32Function,
460 void *pvParms,
461 uint32_t cbParms)
462{
463 using namespace guestControl;
464
465 /*
466 * No locking, as this is purely a notification which does not make any
467 * changes to the object state.
468 */
469 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
470 pvExtension, u32Function, pvParms, cbParms));
471 ComObjPtr<Guest> pGuest = reinterpret_cast<Guest *>(pvExtension);
472
473 int rc = VINF_SUCCESS;
474 if (u32Function == GUEST_EXEC_SEND_STATUS)
475 {
476 LogFlowFunc(("GUEST_EXEC_SEND_STATUS\n"));
477
478 PHOSTEXECCALLBACKDATA pCBData = reinterpret_cast<PHOSTEXECCALLBACKDATA>(pvParms);
479 AssertPtr(pCBData);
480 AssertReturn(sizeof(HOSTEXECCALLBACKDATA) == cbParms, VERR_INVALID_PARAMETER);
481 AssertReturn(HOSTEXECCALLBACKDATAMAGIC == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
482
483 rc = pGuest->notifyCtrlExec(u32Function, pCBData);
484 }
485 else
486 rc = VERR_NOT_SUPPORTED;
487 return rc;
488}
489
490/* Notifier function for control execution stuff. */
491int Guest::notifyCtrlExec(uint32_t u32Function,
492 PHOSTEXECCALLBACKDATA pData)
493{
494 int rc = VINF_SUCCESS;
495
496 AssertPtr(pData);
497 CallbackListIter it = getCtrlCallbackContext(pData->hdr.u32ContextID);
498 if (it != mCallbackList.end())
499 {
500 PHOSTEXECCALLBACKDATA pCBData = (HOSTEXECCALLBACKDATA*)it->pvData;
501 AssertPtr(pCBData);
502
503 pCBData->u32PID = pData->u32PID;
504 pCBData->u32Status = pData->u32Status;
505 pCBData->u32Flags = pData->u32Flags;
506 /* @todo Copy void* buffer contents! */
507
508 ASMAtomicWriteBool(&it->bCalled, true);
509 }
510 return rc;
511}
512
513Guest::CallbackListIter Guest::getCtrlCallbackContext(uint32_t u32ContextID)
514{
515 CallbackListIter it;
516 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
517 {
518 if (it->mContextID == u32ContextID)
519 return (it);
520 }
521 return it;
522}
523
524void Guest::removeCtrlCallbackContext(Guest::CallbackListIter it)
525{
526 if (it->cbData)
527 {
528 RTMemFree(it->pvData);
529 it->cbData = 0;
530 it->pvData = NULL;
531 }
532 mCallbackList.erase(it);
533}
534
535uint32_t Guest::addCtrlCallbackContext(void *pvData, uint32_t cbData)
536{
537 uint32_t uNewContext = ASMAtomicIncU32(&mNextContextID);
538 /** @todo Add value clamping! */
539
540 CallbackContext context;
541 context.mContextID = uNewContext;
542 context.bCalled = false;
543 context.pvData = pvData;
544 context.cbData = cbData;
545
546 mCallbackList.push_back(context);
547 if (mCallbackList.size() > 256) /* Don't let the container size get too big! */
548 removeCtrlCallbackContext(mCallbackList.begin());
549
550 return uNewContext;
551}
552#endif /* VBOX_WITH_GUEST_CONTROL */
553
554STDMETHODIMP Guest::ExecuteProcess(IN_BSTR aCommand, ULONG aFlags,
555 ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
556 IN_BSTR aStdIn, IN_BSTR aStdOut, IN_BSTR aStdErr,
557 IN_BSTR aUserName, IN_BSTR aPassword,
558 ULONG aTimeoutMS, ULONG *aPID, IProgress **aProgress)
559{
560#ifndef VBOX_WITH_GUEST_CONTROL
561 ReturnComNotImplemented();
562#else /* VBOX_WITH_GUEST_CONTROL */
563 using namespace guestControl;
564
565 CheckComArgStrNotEmptyOrNull(aCommand);
566 CheckComArgOutPointerValid(aPID);
567 CheckComArgOutPointerValid(aProgress);
568 if (aFlags != 0) /* Flags are not supported at the moment. */
569 return E_INVALIDARG;
570
571 HRESULT rc = S_OK;
572
573 try
574 {
575 AutoCaller autoCaller(this);
576 if (FAILED(autoCaller.rc())) return autoCaller.rc();
577
578 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
579
580 /*
581 * Create progress object.
582 */
583#if 0
584 ComObjPtr <Progress> progress;
585 progress.createObject();
586 HRESULT rc = progress->init(/** @todo How to get the machine here? */
587 static_cast<IGuest*>(this),
588 BstrFmt(tr("Executing process")),
589 FALSE);
590 if (FAILED(rc)) return rc;
591#endif
592 /*
593 * Prepare process execution.
594 */
595 int vrc = VINF_SUCCESS;
596 Utf8Str Utf8Command(aCommand);
597
598 /* Adjust timeout */
599 if (aTimeoutMS == 0)
600 aTimeoutMS = UINT32_MAX;
601
602 /* Prepare arguments. */
603 com::SafeArray<IN_BSTR> args(ComSafeArrayInArg(aArguments));
604 uint32_t uNumArgs = args.size();
605 char **papszArgv = NULL;
606 if(uNumArgs > 0)
607 {
608 papszArgv = (char**)RTMemAlloc(sizeof(char*) * (uNumArgs + 1));
609 AssertPtr(papszArgv);
610 for (unsigned i = 0; RT_SUCCESS(vrc) && i < uNumArgs; i++)
611 vrc = RTStrAPrintf(&papszArgv[i], "%s", Utf8Str(args[i]).raw());
612 papszArgv[uNumArgs] = NULL;
613 }
614
615 if (RT_SUCCESS(vrc))
616 {
617 uint32_t uContextID = 0;
618
619 char *pszArgs = NULL;
620 if (uNumArgs > 0)
621 vrc = RTGetOptArgvToString(&pszArgs, papszArgv, 0);
622 if (RT_SUCCESS(vrc))
623 {
624 uint32_t cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
625
626 /* Prepare environment. */
627 com::SafeArray<IN_BSTR> env(ComSafeArrayInArg(aEnvironment));
628
629 void *pvEnv = NULL;
630 uint32_t uNumEnv = 0;
631 uint32_t cbEnv = 0;
632
633 for (unsigned i = 0; i < env.size(); i++)
634 {
635 vrc = prepareExecuteEnv(Utf8Str(env[i]).raw(), &pvEnv, &cbEnv, &uNumEnv);
636 if (RT_FAILURE(vrc))
637 break;
638 }
639
640 if (RT_SUCCESS(vrc))
641 {
642 Utf8Str Utf8StdIn(aStdIn);
643 Utf8Str Utf8StdOut(aStdOut);
644 Utf8Str Utf8StdErr(aStdErr);
645 Utf8Str Utf8UserName(aUserName);
646 Utf8Str Utf8Password(aPassword);
647
648 PHOSTEXECCALLBACKDATA pData = (HOSTEXECCALLBACKDATA*)RTMemAlloc(sizeof(HOSTEXECCALLBACKDATA));
649 AssertPtr(pData);
650 uContextID = addCtrlCallbackContext(pData, sizeof(HOSTEXECCALLBACKDATA));
651 Assert(uContextID > 0);
652
653 VBOXHGCMSVCPARM paParms[15];
654 int i = 0;
655 paParms[i++].setUInt32(uContextID);
656 paParms[i++].setPointer((void*)Utf8Command.raw(), (uint32_t)strlen(Utf8Command.raw()) + 1);
657 paParms[i++].setUInt32(aFlags);
658 paParms[i++].setUInt32(uNumArgs);
659 paParms[i++].setPointer((void*)pszArgs, cbArgs);
660 paParms[i++].setUInt32(uNumEnv);
661 paParms[i++].setUInt32(cbEnv);
662 paParms[i++].setPointer((void*)pvEnv, cbEnv);
663 paParms[i++].setPointer((void*)Utf8StdIn.raw(), (uint32_t)strlen(Utf8StdIn.raw()) + 1);
664 paParms[i++].setPointer((void*)Utf8StdOut.raw(), (uint32_t)strlen(Utf8StdOut.raw()) + 1);
665 paParms[i++].setPointer((void*)Utf8StdErr.raw(), (uint32_t)strlen(Utf8StdErr.raw()) + 1);
666 paParms[i++].setPointer((void*)Utf8UserName.raw(), (uint32_t)strlen(Utf8UserName.raw()) + 1);
667 paParms[i++].setPointer((void*)Utf8Password.raw(), (uint32_t)strlen(Utf8Password.raw()) + 1);
668 paParms[i++].setUInt32(aTimeoutMS);
669
670 /* Forward the information to the VMM device. */
671 AssertPtr(mParent);
672 VMMDev *vmmDev = mParent->getVMMDev();
673 if (vmmDev)
674 {
675 LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
676 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_CMD,
677 i, paParms);
678 }
679 RTMemFree(pvEnv);
680 }
681 RTStrFree(pszArgs);
682 }
683 if (RT_SUCCESS(vrc))
684 {
685 LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
686
687 /*
688 * Wait for the HGCM low level callback until the process
689 * has been started (or something went wrong). This is necessary to
690 * get the PID.
691 */
692 CallbackListIter it = getCtrlCallbackContext(uContextID);
693 uint64_t u64Started = RTTimeMilliTS();
694 do
695 {
696 unsigned cMsWait;
697 if (aTimeoutMS == RT_INDEFINITE_WAIT)
698 cMsWait = 1000;
699 else
700 {
701 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
702 if (cMsElapsed >= aTimeoutMS)
703 break; /* timed out */
704 cMsWait = RT_MIN(1000, aTimeoutMS - (uint32_t)cMsElapsed);
705 }
706 RTThreadSleep(100);
707 } while (it != mCallbackList.end() && !it->bCalled);
708
709 /* Did we get some status? */
710 PHOSTEXECCALLBACKDATA pData = (HOSTEXECCALLBACKDATA*)it->pvData;
711 Assert(it->cbData == sizeof(HOSTEXECCALLBACKDATA));
712 AssertPtr(pData);
713 if (it->bCalled)
714 {
715 switch (pData->u32Status)
716 {
717 case PROC_STS_STARTED:
718 *aPID = pData->u32PID;
719 break;
720
721 case PROC_STS_ERROR:
722 vrc = pData->u32Flags; /* u32Flags member contains IPRT error code. */
723 break;
724
725 default:
726 vrc = VERR_INVALID_PARAMETER;
727 break;
728 }
729 }
730 else /* If callback not called within time ... well, that's a timeout! */
731 vrc = VERR_TIMEOUT;
732 removeCtrlCallbackContext(it);
733
734 if (RT_FAILURE(vrc))
735 {
736 if (vrc == VERR_FILE_NOT_FOUND) /* This is the most likely error. */
737 {
738 rc = setError(VBOX_E_IPRT_ERROR,
739 tr("The file \"%s\" was not found on guest"), Utf8Command.raw());
740 }
741 else if (vrc == VERR_BAD_EXE_FORMAT)
742 {
743 rc = setError(VBOX_E_IPRT_ERROR,
744 tr("The file \"%s\" is not an executable format on guest"), Utf8Command.raw());
745 }
746 else if (vrc == VERR_TIMEOUT)
747 {
748 rc = setError(VBOX_E_IPRT_ERROR,
749 tr("The guest did not respond within time (%ums)"), aTimeoutMS);
750 }
751 else
752 {
753 rc = setError(E_UNEXPECTED,
754 tr("The service call failed with the error %Rrc"), vrc);
755 }
756 }
757#if 0
758 progress.queryInterfaceTo(aProgress);
759#endif
760 }
761 else
762 {
763 /* HGCM call went wrong. */
764 rc = setError(E_UNEXPECTED,
765 tr("The service call failed with error %Rrc"), vrc);
766 }
767
768 for (unsigned i = 0; i < uNumArgs; i++)
769 RTMemFree(papszArgv[i]);
770 RTMemFree(papszArgv);
771 }
772 }
773 catch (std::bad_alloc &)
774 {
775 rc = E_OUTOFMEMORY;
776 }
777 return rc;
778#endif /* VBOX_WITH_GUEST_CONTROL */
779}
780
781// public methods only for internal purposes
782/////////////////////////////////////////////////////////////////////////////
783
784void Guest::setAdditionsVersion(Bstr aVersion, VBOXOSTYPE aOsType)
785{
786 AutoCaller autoCaller(this);
787 AssertComRCReturnVoid (autoCaller.rc());
788
789 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
790
791 mData.mAdditionsVersion = aVersion;
792 mData.mAdditionsActive = !aVersion.isEmpty();
793 /* Older Additions didn't have this finer grained capability bit,
794 * so enable it by default. Newer Additions will disable it immediately
795 * if relevant. */
796 mData.mSupportsGraphics = mData.mAdditionsActive;
797
798 mData.mOSTypeId = Global::OSTypeId (aOsType);
799}
800
801void Guest::setSupportsSeamless (BOOL aSupportsSeamless)
802{
803 AutoCaller autoCaller(this);
804 AssertComRCReturnVoid (autoCaller.rc());
805
806 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
807
808 mData.mSupportsSeamless = aSupportsSeamless;
809}
810
811void Guest::setSupportsGraphics (BOOL aSupportsGraphics)
812{
813 AutoCaller autoCaller(this);
814 AssertComRCReturnVoid (autoCaller.rc());
815
816 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
817
818 mData.mSupportsGraphics = aSupportsGraphics;
819}
820/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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