VirtualBox

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

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

Guest Control: Update (extended error info, callback bugfixes).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.2 KB
Line 
1/* $Id: GuestImpl.cpp 28463 2010-04-19 14:04:12Z 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 LogFlowFuncEnter();
495 int rc = VINF_SUCCESS;
496
497 AssertPtr(pData);
498 CallbackListIter it = getCtrlCallbackContext(pData->hdr.u32ContextID);
499 if (it != mCallbackList.end())
500 {
501 PHOSTEXECCALLBACKDATA pCBData = (HOSTEXECCALLBACKDATA*)it->pvData;
502 AssertPtr(pCBData);
503
504 pCBData->u32PID = pData->u32PID;
505 pCBData->u32Status = pData->u32Status;
506 pCBData->u32Flags = pData->u32Flags;
507 /* @todo Copy void* buffer contents! */
508
509 /* Do progress handling. */
510 Utf8Str errMsg;
511 HRESULT rc2 = S_OK;
512 switch (pData->u32Status)
513 {
514 case PROC_STS_STARTED:
515 break;
516
517 case PROC_STS_TEN: /* Terminated normally. */
518 it->pProgress->notifyComplete(S_OK);
519 break;
520
521 case PROC_STS_TEA: /* Terminated abnormally. */
522 errMsg = Utf8StrFmt(Guest::tr("Process terminated abnormally with status '%u'"),
523 pCBData->u32Flags);
524 break;
525
526 case PROC_STS_TES: /* Terminated through signal. */
527 errMsg = Utf8StrFmt(Guest::tr("Process terminated via signal with status '%u'"),
528 pCBData->u32Flags);
529 break;
530
531 case PROC_STS_TOK:
532 errMsg = Utf8StrFmt(Guest::tr("Process timed out and was killed"));
533 break;
534
535 case PROC_STS_TOA:
536 errMsg = Utf8StrFmt(Guest::tr("Process timed out and could not be killed"));
537 break;
538
539 case PROC_STS_DWN:
540 errMsg = Utf8StrFmt(Guest::tr("Process exited because system is shutting down"));
541 break;
542
543 default:
544 break;
545 }
546
547 if ( !it->pProgress.isNull()
548 && errMsg.length())
549 {
550 it->pProgress->notifyComplete(E_FAIL /** @todo Find a better rc! */, COM_IIDOF(IGuest),
551 (CBSTR)Guest::getComponentName(), errMsg.c_str());
552 }
553 ASMAtomicWriteBool(&it->bCalled, true);
554 }
555 LogFlowFuncLeave();
556 return rc;
557}
558
559Guest::CallbackListIter Guest::getCtrlCallbackContext(uint32_t u32ContextID)
560{
561 CallbackListIter it;
562 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
563 {
564 if (it->mContextID == u32ContextID)
565 return (it);
566 }
567 return it;
568}
569
570void Guest::removeCtrlCallbackContext(Guest::CallbackListIter it)
571{
572 LogFlowFuncEnter();
573 if (it->cbData)
574 {
575 RTMemFree(it->pvData);
576 it->cbData = 0;
577 it->pvData = NULL;
578
579 /* Notify outstanding waits for progress ... */
580 if (!it->pProgress.isNull())
581 {
582 it->pProgress->notifyComplete(S_OK);
583 it->pProgress = NULL;
584 }
585 }
586 mCallbackList.erase(it);
587 LogFlowFuncLeave();
588}
589
590/* Adds a callback with a user provided data block and an optional progress object
591 * to the callback list. A callback is identified by a unique context ID which is used
592 * to identify a callback from the guest side. */
593uint32_t Guest::addCtrlCallbackContext(void *pvData, uint32_t cbData, Progress* pProgress)
594{
595 LogFlowFuncEnter();
596
597 uint32_t uNewContext = ASMAtomicIncU32(&mNextContextID);
598 /** @todo Add value clamping! */
599
600 CallbackContext context;
601 context.mContextID = uNewContext;
602 context.bCalled = false;
603 context.pvData = pvData;
604 context.cbData = cbData;
605 context.pProgress = pProgress;
606
607 mCallbackList.push_back(context);
608 if (mCallbackList.size() > 256) /* Don't let the container size get too big! */
609 removeCtrlCallbackContext(mCallbackList.begin());
610
611 LogFlowFuncLeave();
612 return uNewContext;
613}
614#endif /* VBOX_WITH_GUEST_CONTROL */
615
616STDMETHODIMP Guest::ExecuteProcess(IN_BSTR aCommand, ULONG aFlags,
617 ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
618 IN_BSTR aStdIn, IN_BSTR aStdOut, IN_BSTR aStdErr,
619 IN_BSTR aUserName, IN_BSTR aPassword,
620 ULONG aTimeoutMS, ULONG *aPID, IProgress **aProgress)
621{
622#ifndef VBOX_WITH_GUEST_CONTROL
623 ReturnComNotImplemented();
624#else /* VBOX_WITH_GUEST_CONTROL */
625 using namespace guestControl;
626
627 CheckComArgStrNotEmptyOrNull(aCommand);
628 CheckComArgOutPointerValid(aPID);
629 CheckComArgOutPointerValid(aProgress);
630 if (aFlags != 0) /* Flags are not supported at the moment. */
631 return E_INVALIDARG;
632
633 HRESULT rc = S_OK;
634
635 try
636 {
637 AutoCaller autoCaller(this);
638 if (FAILED(autoCaller.rc())) return autoCaller.rc();
639
640 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
641
642 /*
643 * Create progress object.
644 */
645 ComObjPtr <Progress> progress;
646 rc = progress.createObject();
647 if (SUCCEEDED(rc))
648 {
649 rc = progress->init(static_cast<IGuest*>(this),
650 //static_cast<IConsole *>(this),
651 BstrFmt(tr("Executing process")),
652 FALSE);
653 }
654 if (FAILED(rc)) return rc;
655
656 /*
657 * Prepare process execution.
658 */
659 int vrc = VINF_SUCCESS;
660 Utf8Str Utf8Command(aCommand);
661
662 /* Adjust timeout */
663 if (aTimeoutMS == 0)
664 aTimeoutMS = UINT32_MAX;
665
666 /* Prepare arguments. */
667 com::SafeArray<IN_BSTR> args(ComSafeArrayInArg(aArguments));
668 uint32_t uNumArgs = args.size();
669 char **papszArgv = NULL;
670 if(uNumArgs > 0)
671 {
672 papszArgv = (char**)RTMemAlloc(sizeof(char*) * (uNumArgs + 1));
673 AssertPtr(papszArgv);
674 for (unsigned i = 0; RT_SUCCESS(vrc) && i < uNumArgs; i++)
675 vrc = RTStrAPrintf(&papszArgv[i], "%s", Utf8Str(args[i]).raw());
676 papszArgv[uNumArgs] = NULL;
677 }
678
679 Utf8Str Utf8StdIn(aStdIn);
680 Utf8Str Utf8StdOut(aStdOut);
681 Utf8Str Utf8StdErr(aStdErr);
682 Utf8Str Utf8UserName(aUserName);
683 Utf8Str Utf8Password(aPassword);
684 if (RT_SUCCESS(vrc))
685 {
686 uint32_t uContextID = 0;
687
688 char *pszArgs = NULL;
689 if (uNumArgs > 0)
690 vrc = RTGetOptArgvToString(&pszArgs, papszArgv, 0);
691 if (RT_SUCCESS(vrc))
692 {
693 uint32_t cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
694
695 /* Prepare environment. */
696 com::SafeArray<IN_BSTR> env(ComSafeArrayInArg(aEnvironment));
697
698 void *pvEnv = NULL;
699 uint32_t uNumEnv = 0;
700 uint32_t cbEnv = 0;
701
702 for (unsigned i = 0; i < env.size(); i++)
703 {
704 vrc = prepareExecuteEnv(Utf8Str(env[i]).raw(), &pvEnv, &cbEnv, &uNumEnv);
705 if (RT_FAILURE(vrc))
706 break;
707 }
708
709 if (RT_SUCCESS(vrc))
710 {
711 PHOSTEXECCALLBACKDATA pData = (HOSTEXECCALLBACKDATA*)RTMemAlloc(sizeof(HOSTEXECCALLBACKDATA));
712 AssertPtr(pData);
713 uContextID = addCtrlCallbackContext(pData, sizeof(HOSTEXECCALLBACKDATA), progress);
714 Assert(uContextID > 0);
715
716 VBOXHGCMSVCPARM paParms[15];
717 int i = 0;
718 paParms[i++].setUInt32(uContextID);
719 paParms[i++].setPointer((void*)Utf8Command.raw(), (uint32_t)strlen(Utf8Command.raw()) + 1);
720 paParms[i++].setUInt32(aFlags);
721 paParms[i++].setUInt32(uNumArgs);
722 paParms[i++].setPointer((void*)pszArgs, cbArgs);
723 paParms[i++].setUInt32(uNumEnv);
724 paParms[i++].setUInt32(cbEnv);
725 paParms[i++].setPointer((void*)pvEnv, cbEnv);
726 paParms[i++].setPointer((void*)Utf8StdIn.raw(), (uint32_t)strlen(Utf8StdIn.raw()) + 1);
727 paParms[i++].setPointer((void*)Utf8StdOut.raw(), (uint32_t)strlen(Utf8StdOut.raw()) + 1);
728 paParms[i++].setPointer((void*)Utf8StdErr.raw(), (uint32_t)strlen(Utf8StdErr.raw()) + 1);
729 paParms[i++].setPointer((void*)Utf8UserName.raw(), (uint32_t)strlen(Utf8UserName.raw()) + 1);
730 paParms[i++].setPointer((void*)Utf8Password.raw(), (uint32_t)strlen(Utf8Password.raw()) + 1);
731 paParms[i++].setUInt32(aTimeoutMS);
732
733 /* Forward the information to the VMM device. */
734 AssertPtr(mParent);
735 VMMDev *vmmDev = mParent->getVMMDev();
736 if (vmmDev)
737 {
738 LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
739 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_CMD,
740 i, paParms);
741 }
742 RTMemFree(pvEnv);
743 }
744 RTStrFree(pszArgs);
745 }
746 if (RT_SUCCESS(vrc))
747 {
748 LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
749
750 /*
751 * Wait for the HGCM low level callback until the process
752 * has been started (or something went wrong). This is necessary to
753 * get the PID.
754 */
755 CallbackListIter it = getCtrlCallbackContext(uContextID);
756 uint64_t u64Started = RTTimeMilliTS();
757 do
758 {
759 unsigned cMsWait;
760 if (aTimeoutMS == RT_INDEFINITE_WAIT)
761 cMsWait = 1000;
762 else
763 {
764 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
765 if (cMsElapsed >= aTimeoutMS)
766 break; /* timed out */
767 cMsWait = RT_MIN(1000, aTimeoutMS - (uint32_t)cMsElapsed);
768 }
769 RTThreadSleep(100);
770 } while (it != mCallbackList.end() && !it->bCalled);
771
772 /* Did we get some status? */
773 PHOSTEXECCALLBACKDATA pData = (HOSTEXECCALLBACKDATA*)it->pvData;
774 Assert(it->cbData == sizeof(HOSTEXECCALLBACKDATA));
775 AssertPtr(pData);
776 if (it->bCalled)
777 {
778 switch (pData->u32Status)
779 {
780 case PROC_STS_STARTED:
781 *aPID = pData->u32PID;
782 break;
783
784 case PROC_STS_ERROR:
785 vrc = pData->u32Flags; /* u32Flags member contains IPRT error code. */
786 break;
787
788 default:
789 vrc = VERR_INVALID_PARAMETER;
790 break;
791 }
792 }
793 else /* If callback not called within time ... well, that's a timeout! */
794 vrc = VERR_TIMEOUT;
795
796 /*
797 * Do *not* remove the callback yet - we might wait with the IProgress object on something
798 * else (like end of process) ...
799 */
800 if (RT_FAILURE(vrc))
801 {
802 if (vrc == VERR_FILE_NOT_FOUND) /* This is the most likely error. */
803 {
804 rc = setError(VBOX_E_IPRT_ERROR,
805 tr("The file '%s' was not found on guest"), Utf8Command.raw());
806 }
807 else if (vrc == VERR_BAD_EXE_FORMAT)
808 {
809 rc = setError(VBOX_E_IPRT_ERROR,
810 tr("The file '%s' is not an executable format on guest"), Utf8Command.raw());
811 }
812 else if (vrc == VERR_LOGON_FAILURE)
813 {
814 rc = setError(VBOX_E_IPRT_ERROR,
815 tr("The specified user '%s' was not able to logon on guest"), Utf8UserName.raw());
816 }
817 else if (vrc == VERR_TIMEOUT)
818 {
819 rc = setError(VBOX_E_IPRT_ERROR,
820 tr("The guest did not respond within time (%ums)"), aTimeoutMS);
821 }
822 else
823 {
824 rc = setError(E_UNEXPECTED,
825 tr("The service call failed with error %Rrc"), vrc);
826 }
827 }
828 else
829 {
830 /* Return the progress to the caller. */
831 progress.queryInterfaceTo(aProgress);
832 }
833 }
834 else
835 {
836 /* HGCM call went wrong. */
837 rc = setError(E_UNEXPECTED,
838 tr("The service call failed with error %Rrc"), vrc);
839 }
840
841 for (unsigned i = 0; i < uNumArgs; i++)
842 RTMemFree(papszArgv[i]);
843 RTMemFree(papszArgv);
844 }
845 }
846 catch (std::bad_alloc &)
847 {
848 rc = E_OUTOFMEMORY;
849 }
850 return rc;
851#endif /* VBOX_WITH_GUEST_CONTROL */
852}
853
854STDMETHODIMP Guest::GetProcessOutput(ULONG aPID, ULONG aFlags, ULONG64 aSize, ComSafeArrayOut(BYTE, aData))
855{
856#ifndef VBOX_WITH_GUEST_CONTROL
857 ReturnComNotImplemented();
858#else /* VBOX_WITH_GUEST_CONTROL */
859 using namespace guestControl;
860
861 NOREF(aPID);
862 NOREF(aFlags);
863 NOREF(aSize);
864 NOREF(aData);
865
866 HRESULT rc = S_OK;
867
868 size_t cbData = (size_t)RT_MIN(aSize, _1K);
869 com::SafeArray<BYTE> outputData(cbData);
870
871 if (FAILED(rc))
872 outputData.resize(0);
873 outputData.detachTo(ComSafeArrayOutArg(aData));
874
875 return rc;
876#endif
877}
878
879// public methods only for internal purposes
880/////////////////////////////////////////////////////////////////////////////
881
882void Guest::setAdditionsVersion(Bstr aVersion, VBOXOSTYPE aOsType)
883{
884 AutoCaller autoCaller(this);
885 AssertComRCReturnVoid (autoCaller.rc());
886
887 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
888
889 mData.mAdditionsVersion = aVersion;
890 mData.mAdditionsActive = !aVersion.isEmpty();
891 /* Older Additions didn't have this finer grained capability bit,
892 * so enable it by default. Newer Additions will disable it immediately
893 * if relevant. */
894 mData.mSupportsGraphics = mData.mAdditionsActive;
895
896 mData.mOSTypeId = Global::OSTypeId (aOsType);
897}
898
899void Guest::setSupportsSeamless (BOOL aSupportsSeamless)
900{
901 AutoCaller autoCaller(this);
902 AssertComRCReturnVoid (autoCaller.rc());
903
904 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
905
906 mData.mSupportsSeamless = aSupportsSeamless;
907}
908
909void Guest::setSupportsGraphics (BOOL aSupportsGraphics)
910{
911 AutoCaller autoCaller(this);
912 AssertComRCReturnVoid (autoCaller.rc());
913
914 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
915
916 mData.mSupportsGraphics = aSupportsGraphics;
917}
918/* 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