VirtualBox

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

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

Guest Control/Main/VBoxManage: IProgress shutdown handling.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.7 KB
Line 
1/* $Id: GuestImpl.cpp 29645 2010-05-18 15:41:46Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Oracle Corporation
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
20#include "GuestImpl.h"
21
22#include "Global.h"
23#include "ConsoleImpl.h"
24#include "ProgressImpl.h"
25#include "VMMDev.h"
26
27#include "AutoCaller.h"
28#include "Logging.h"
29
30#include <VBox/VMMDev.h>
31#ifdef VBOX_WITH_GUEST_CONTROL
32# include <VBox/com/array.h>
33#endif
34#include <iprt/cpp/utils.h>
35#include <iprt/getopt.h>
36#include <VBox/pgm.h>
37
38// defines
39/////////////////////////////////////////////////////////////////////////////
40
41// constructor / destructor
42/////////////////////////////////////////////////////////////////////////////
43
44DEFINE_EMPTY_CTOR_DTOR (Guest)
45
46HRESULT Guest::FinalConstruct()
47{
48 return S_OK;
49}
50
51void Guest::FinalRelease()
52{
53 uninit ();
54}
55
56// public methods only for internal purposes
57/////////////////////////////////////////////////////////////////////////////
58
59/**
60 * Initializes the guest object.
61 */
62HRESULT Guest::init (Console *aParent)
63{
64 LogFlowThisFunc(("aParent=%p\n", aParent));
65
66 ComAssertRet(aParent, E_INVALIDARG);
67
68 /* Enclose the state transition NotReady->InInit->Ready */
69 AutoInitSpan autoInitSpan(this);
70 AssertReturn(autoInitSpan.isOk(), E_FAIL);
71
72 unconst(mParent) = aParent;
73
74 /* mData.mAdditionsActive is FALSE */
75
76 /* Confirm a successful initialization when it's the case */
77 autoInitSpan.setSucceeded();
78
79 ULONG aMemoryBalloonSize;
80 HRESULT ret = mParent->machine()->COMGETTER(MemoryBalloonSize)(&aMemoryBalloonSize);
81 if (ret == S_OK)
82 mMemoryBalloonSize = aMemoryBalloonSize;
83 else
84 mMemoryBalloonSize = 0; /* Default is no ballooning */
85
86 BOOL fPageFusionEnabled;
87 ret = mParent->machine()->COMGETTER(PageFusionEnabled)(&fPageFusionEnabled);
88 if (ret == S_OK)
89 mfPageFusionEnabled = fPageFusionEnabled;
90 else
91 mfPageFusionEnabled = false; /* Default is no page fusion*/
92
93 mStatUpdateInterval = 0; /* Default is not to report guest statistics at all */
94
95 /* Clear statistics. */
96 for (unsigned i = 0 ; i < GUESTSTATTYPE_MAX; i++)
97 mCurrentGuestStat[i] = 0;
98
99#ifdef VBOX_WITH_GUEST_CONTROL
100 /* Init the context ID counter at 1000. */
101 mNextContextID = 1000;
102#endif
103
104 return S_OK;
105}
106
107/**
108 * Uninitializes the instance and sets the ready flag to FALSE.
109 * Called either from FinalRelease() or by the parent when it gets destroyed.
110 */
111void Guest::uninit()
112{
113 LogFlowThisFunc(("\n"));
114
115#ifdef VBOX_WITH_GUEST_CONTROL
116 /*
117 * Cleanup must be done *before* AutoUninitSpan to cancel all
118 * all outstanding waits in API functions (which hold AutoCaller
119 * ref counts).
120 */
121 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
122
123 /* Clean up callback data. */
124 CallbackListIter it;
125 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
126 destroyCtrlCallbackContext(it);
127
128 /* Clear process list. */
129 mGuestProcessList.clear();
130#endif
131
132 /* Enclose the state transition Ready->InUninit->NotReady */
133 AutoUninitSpan autoUninitSpan(this);
134 if (autoUninitSpan.uninitDone())
135 return;
136
137 unconst(mParent) = NULL;
138}
139
140// IGuest properties
141/////////////////////////////////////////////////////////////////////////////
142
143STDMETHODIMP Guest::COMGETTER(OSTypeId) (BSTR *aOSTypeId)
144{
145 CheckComArgOutPointerValid(aOSTypeId);
146
147 AutoCaller autoCaller(this);
148 if (FAILED(autoCaller.rc())) return autoCaller.rc();
149
150 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
151
152 // redirect the call to IMachine if no additions are installed
153 if (mData.mAdditionsVersion.isEmpty())
154 return mParent->machine()->COMGETTER(OSTypeId)(aOSTypeId);
155
156 mData.mOSTypeId.cloneTo(aOSTypeId);
157
158 return S_OK;
159}
160
161STDMETHODIMP Guest::COMGETTER(AdditionsActive) (BOOL *aAdditionsActive)
162{
163 CheckComArgOutPointerValid(aAdditionsActive);
164
165 AutoCaller autoCaller(this);
166 if (FAILED(autoCaller.rc())) return autoCaller.rc();
167
168 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
169
170 *aAdditionsActive = mData.mAdditionsActive;
171
172 return S_OK;
173}
174
175STDMETHODIMP Guest::COMGETTER(AdditionsVersion) (BSTR *aAdditionsVersion)
176{
177 CheckComArgOutPointerValid(aAdditionsVersion);
178
179 AutoCaller autoCaller(this);
180 if (FAILED(autoCaller.rc())) return autoCaller.rc();
181
182 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
183
184 mData.mAdditionsVersion.cloneTo(aAdditionsVersion);
185
186 return S_OK;
187}
188
189STDMETHODIMP Guest::COMGETTER(SupportsSeamless) (BOOL *aSupportsSeamless)
190{
191 CheckComArgOutPointerValid(aSupportsSeamless);
192
193 AutoCaller autoCaller(this);
194 if (FAILED(autoCaller.rc())) return autoCaller.rc();
195
196 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
197
198 *aSupportsSeamless = mData.mSupportsSeamless;
199
200 return S_OK;
201}
202
203STDMETHODIMP Guest::COMGETTER(SupportsGraphics) (BOOL *aSupportsGraphics)
204{
205 CheckComArgOutPointerValid(aSupportsGraphics);
206
207 AutoCaller autoCaller(this);
208 if (FAILED(autoCaller.rc())) return autoCaller.rc();
209
210 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
211
212 *aSupportsGraphics = mData.mSupportsGraphics;
213
214 return S_OK;
215}
216
217STDMETHODIMP Guest::COMGETTER(PageFusionEnabled) (BOOL *aPageFusionEnabled)
218{
219 CheckComArgOutPointerValid(aPageFusionEnabled);
220
221 AutoCaller autoCaller(this);
222 if (FAILED(autoCaller.rc())) return autoCaller.rc();
223
224 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
225
226 *aPageFusionEnabled = mfPageFusionEnabled;
227
228 return S_OK;
229}
230
231STDMETHODIMP Guest::COMSETTER(PageFusionEnabled) (BOOL aPageFusionEnabled)
232{
233 AutoCaller autoCaller(this);
234 if (FAILED(autoCaller.rc())) return autoCaller.rc();
235
236 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
237
238 /** todo; API complete, but not implemented */
239
240 return E_NOTIMPL;
241}
242
243STDMETHODIMP Guest::COMGETTER(MemoryBalloonSize) (ULONG *aMemoryBalloonSize)
244{
245 CheckComArgOutPointerValid(aMemoryBalloonSize);
246
247 AutoCaller autoCaller(this);
248 if (FAILED(autoCaller.rc())) return autoCaller.rc();
249
250 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
251
252 *aMemoryBalloonSize = mMemoryBalloonSize;
253
254 return S_OK;
255}
256
257STDMETHODIMP Guest::COMSETTER(MemoryBalloonSize) (ULONG aMemoryBalloonSize)
258{
259 AutoCaller autoCaller(this);
260 if (FAILED(autoCaller.rc())) return autoCaller.rc();
261
262 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
263
264 /* We must be 100% sure that IMachine::COMSETTER(MemoryBalloonSize)
265 * does not call us back in any way! */
266 HRESULT ret = mParent->machine()->COMSETTER(MemoryBalloonSize)(aMemoryBalloonSize);
267 if (ret == S_OK)
268 {
269 mMemoryBalloonSize = aMemoryBalloonSize;
270 /* forward the information to the VMM device */
271 VMMDev *vmmDev = mParent->getVMMDev();
272 if (vmmDev)
273 vmmDev->getVMMDevPort()->pfnSetMemoryBalloon(vmmDev->getVMMDevPort(), aMemoryBalloonSize);
274 }
275
276 return ret;
277}
278
279STDMETHODIMP Guest::COMGETTER(StatisticsUpdateInterval)(ULONG *aUpdateInterval)
280{
281 CheckComArgOutPointerValid(aUpdateInterval);
282
283 AutoCaller autoCaller(this);
284 if (FAILED(autoCaller.rc())) return autoCaller.rc();
285
286 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
287
288 *aUpdateInterval = mStatUpdateInterval;
289 return S_OK;
290}
291
292STDMETHODIMP Guest::COMSETTER(StatisticsUpdateInterval)(ULONG aUpdateInterval)
293{
294 AutoCaller autoCaller(this);
295 if (FAILED(autoCaller.rc())) return autoCaller.rc();
296
297 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
298
299 mStatUpdateInterval = aUpdateInterval;
300 /* forward the information to the VMM device */
301 VMMDev *vmmDev = mParent->getVMMDev();
302 if (vmmDev)
303 vmmDev->getVMMDevPort()->pfnSetStatisticsInterval(vmmDev->getVMMDevPort(), aUpdateInterval);
304
305 return S_OK;
306}
307
308STDMETHODIMP Guest::InternalGetStatistics(ULONG *aCpuUser, ULONG *aCpuKernel, ULONG *aCpuIdle,
309 ULONG *aMemTotal, ULONG *aMemFree, ULONG *aMemBalloon, ULONG *aMemShared,
310 ULONG *aMemCache, ULONG *aPageTotal,
311 ULONG *aMemAllocTotal, ULONG *aMemFreeTotal, ULONG *aMemBalloonTotal, ULONG *aMemSharedTotal)
312{
313 CheckComArgOutPointerValid(aCpuUser);
314 CheckComArgOutPointerValid(aCpuKernel);
315 CheckComArgOutPointerValid(aCpuIdle);
316 CheckComArgOutPointerValid(aMemTotal);
317 CheckComArgOutPointerValid(aMemFree);
318 CheckComArgOutPointerValid(aMemBalloon);
319 CheckComArgOutPointerValid(aMemShared);
320 CheckComArgOutPointerValid(aMemCache);
321 CheckComArgOutPointerValid(aPageTotal);
322 CheckComArgOutPointerValid(aMemAllocTotal);
323 CheckComArgOutPointerValid(aMemFreeTotal);
324 CheckComArgOutPointerValid(aMemBalloonTotal);
325 CheckComArgOutPointerValid(aMemSharedTotal);
326
327 AutoCaller autoCaller(this);
328 if (FAILED(autoCaller.rc())) return autoCaller.rc();
329
330 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
331
332 *aCpuUser = mCurrentGuestStat[GUESTSTATTYPE_CPUUSER];
333 *aCpuKernel = mCurrentGuestStat[GUESTSTATTYPE_CPUKERNEL];
334 *aCpuIdle = mCurrentGuestStat[GUESTSTATTYPE_CPUIDLE];
335 *aMemTotal = mCurrentGuestStat[GUESTSTATTYPE_MEMTOTAL] * (_4K/_1K); /* page (4K) -> 1KB units */
336 *aMemFree = mCurrentGuestStat[GUESTSTATTYPE_MEMFREE] * (_4K/_1K); /* page (4K) -> 1KB units */
337 *aMemBalloon = mCurrentGuestStat[GUESTSTATTYPE_MEMBALLOON] * (_4K/_1K); /* page (4K) -> 1KB units */
338 *aMemCache = mCurrentGuestStat[GUESTSTATTYPE_MEMCACHE] * (_4K/_1K); /* page (4K) -> 1KB units */
339 *aPageTotal = mCurrentGuestStat[GUESTSTATTYPE_PAGETOTAL] * (_4K/_1K); /* page (4K) -> 1KB units */
340
341 Console::SafeVMPtr pVM (mParent);
342 if (pVM.isOk())
343 {
344 uint64_t uFreeTotal, uAllocTotal, uBalloonedTotal, uSharedTotal;
345 *aMemFreeTotal = 0;
346 int rc = PGMR3QueryVMMMemoryStats(pVM.raw(), &uAllocTotal, &uFreeTotal, &uBalloonedTotal, &uSharedTotal);
347 AssertRC(rc);
348 if (rc == VINF_SUCCESS)
349 {
350 *aMemAllocTotal = (ULONG)(uAllocTotal / _1K); /* bytes -> KB */
351 *aMemFreeTotal = (ULONG)(uFreeTotal / _1K);
352 *aMemBalloonTotal = (ULONG)(uBalloonedTotal / _1K);
353 *aMemSharedTotal = (ULONG)(uSharedTotal / _1K);
354 }
355
356 /* Query the missing per-VM memory statistics. */
357 *aMemShared = 0;
358 uint64_t uTotalMem, uPrivateMem, uSharedMem, uZeroMem;
359 rc = PGMR3QueryMemoryStats(pVM.raw(), &uTotalMem, &uPrivateMem, &uSharedMem, &uZeroMem);
360 if (rc == VINF_SUCCESS)
361 {
362 *aMemShared = (ULONG)(uSharedMem / _1K);
363 }
364 }
365 else
366 {
367 *aMemFreeTotal = 0;
368 *aMemShared = 0;
369 }
370
371 return S_OK;
372}
373
374HRESULT Guest::SetStatistic(ULONG aCpuId, GUESTSTATTYPE enmType, ULONG aVal)
375{
376 AutoCaller autoCaller(this);
377 if (FAILED(autoCaller.rc())) return autoCaller.rc();
378
379 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
380
381 if (enmType >= GUESTSTATTYPE_MAX)
382 return E_INVALIDARG;
383
384 mCurrentGuestStat[enmType] = aVal;
385 return S_OK;
386}
387
388STDMETHODIMP Guest::SetCredentials(IN_BSTR aUserName, IN_BSTR aPassword,
389 IN_BSTR aDomain, BOOL aAllowInteractiveLogon)
390{
391 AutoCaller autoCaller(this);
392 if (FAILED(autoCaller.rc())) return autoCaller.rc();
393
394 /* forward the information to the VMM device */
395 VMMDev *vmmDev = mParent->getVMMDev();
396 if (vmmDev)
397 {
398 uint32_t u32Flags = VMMDEV_SETCREDENTIALS_GUESTLOGON;
399 if (!aAllowInteractiveLogon)
400 u32Flags = VMMDEV_SETCREDENTIALS_NOLOCALLOGON;
401
402 vmmDev->getVMMDevPort()->pfnSetCredentials(vmmDev->getVMMDevPort(),
403 Utf8Str(aUserName).raw(), Utf8Str(aPassword).raw(),
404 Utf8Str(aDomain).raw(), u32Flags);
405 return S_OK;
406 }
407
408 return setError(VBOX_E_VM_ERROR,
409 tr("VMM device is not available (is the VM running?)"));
410}
411
412#ifdef VBOX_WITH_GUEST_CONTROL
413/**
414 * Appends environment variables to the environment block. Each var=value pair is separated
415 * by NULL (\0) sequence. The whole block will be stored in one blob and disassembled on the
416 * guest side later to fit into the HGCM param structure.
417 *
418 * @returns VBox status code.
419 *
420 * @todo
421 *
422 */
423int Guest::prepareExecuteEnv(const char *pszEnv, void **ppvList, uint32_t *pcbList, uint32_t *pcEnv)
424{
425 int rc = VINF_SUCCESS;
426 uint32_t cbLen = strlen(pszEnv);
427 if (*ppvList)
428 {
429 uint32_t cbNewLen = *pcbList + cbLen + 1; /* Include zero termination. */
430 char *pvTmp = (char*)RTMemRealloc(*ppvList, cbNewLen);
431 if (NULL == pvTmp)
432 {
433 rc = VERR_NO_MEMORY;
434 }
435 else
436 {
437 memcpy(pvTmp + *pcbList, pszEnv, cbLen);
438 pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
439 *ppvList = (void**)pvTmp;
440 }
441 }
442 else
443 {
444 char *pcTmp;
445 if (RTStrAPrintf(&pcTmp, "%s", pszEnv) > 0)
446 {
447 *ppvList = (void**)pcTmp;
448 /* Reset counters. */
449 *pcEnv = 0;
450 *pcbList = 0;
451 }
452 }
453 if (RT_SUCCESS(rc))
454 {
455 *pcbList += cbLen + 1; /* Include zero termination. */
456 *pcEnv += 1; /* Increase env pairs count. */
457 }
458 return rc;
459}
460
461/**
462 * Static callback function for receiving updates on guest control commands
463 * from the guest. Acts as a dispatcher for the actual class instance.
464 *
465 * @returns VBox status code.
466 *
467 * @todo
468 *
469 */
470DECLCALLBACK(int) Guest::doGuestCtrlNotification(void *pvExtension,
471 uint32_t u32Function,
472 void *pvParms,
473 uint32_t cbParms)
474{
475 using namespace guestControl;
476
477 /*
478 * No locking, as this is purely a notification which does not make any
479 * changes to the object state.
480 */
481 LogFlowFunc(("pvExtension = %p, u32Function = %d, pvParms = %p, cbParms = %d\n",
482 pvExtension, u32Function, pvParms, cbParms));
483 ComObjPtr<Guest> pGuest = reinterpret_cast<Guest *>(pvExtension);
484
485 int rc = VINF_SUCCESS;
486 if (u32Function == GUEST_EXEC_SEND_STATUS)
487 {
488 LogFlowFunc(("GUEST_EXEC_SEND_STATUS\n"));
489
490 PHOSTEXECCALLBACKDATA pCBData = reinterpret_cast<PHOSTEXECCALLBACKDATA>(pvParms);
491 AssertPtr(pCBData);
492 AssertReturn(sizeof(HOSTEXECCALLBACKDATA) == cbParms, VERR_INVALID_PARAMETER);
493 AssertReturn(HOSTEXECCALLBACKDATAMAGIC == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
494
495 rc = pGuest->notifyCtrlExec(u32Function, pCBData);
496 }
497 else if (u32Function == GUEST_EXEC_SEND_OUTPUT)
498 {
499 LogFlowFunc(("GUEST_EXEC_SEND_OUTPUT\n"));
500
501 PHOSTEXECOUTCALLBACKDATA pCBData = reinterpret_cast<PHOSTEXECOUTCALLBACKDATA>(pvParms);
502 AssertPtr(pCBData);
503 AssertReturn(sizeof(HOSTEXECOUTCALLBACKDATA) == cbParms, VERR_INVALID_PARAMETER);
504 AssertReturn(HOSTEXECOUTCALLBACKDATAMAGIC == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
505
506 rc = pGuest->notifyCtrlExecOut(u32Function, pCBData);
507 }
508 else
509 rc = VERR_NOT_SUPPORTED;
510 return rc;
511}
512
513/* Function for handling the execution start/termination notification. */
514int Guest::notifyCtrlExec(uint32_t u32Function,
515 PHOSTEXECCALLBACKDATA pData)
516{
517 LogFlowFuncEnter();
518 int rc = VINF_SUCCESS;
519
520 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
521
522 AssertPtr(pData);
523 CallbackListIter it = getCtrlCallbackContextByID(pData->hdr.u32ContextID);
524
525 /* Callback can be called several times. */
526 if (it != mCallbackList.end())
527 {
528 PHOSTEXECCALLBACKDATA pCBData = (HOSTEXECCALLBACKDATA*)it->pvData;
529 AssertPtr(pCBData);
530
531 pCBData->u32PID = pData->u32PID;
532 pCBData->u32Status = pData->u32Status;
533 pCBData->u32Flags = pData->u32Flags;
534 /** @todo Copy void* buffer contents! */
535
536 /* Was progress canceled before? */
537 BOOL fCancelled;
538 it->pProgress->COMGETTER(Canceled)(&fCancelled);
539
540 /* Do progress handling. */
541 Utf8Str errMsg;
542 HRESULT rc2 = S_OK;
543 switch (pData->u32Status)
544 {
545 case PROC_STS_STARTED:
546 break;
547
548 case PROC_STS_TEN: /* Terminated normally. */
549 if ( !it->pProgress->getCompleted()
550 && !fCancelled)
551 {
552 it->pProgress->notifyComplete(S_OK);
553 }
554 break;
555
556 case PROC_STS_TEA: /* Terminated abnormally. */
557 errMsg = Utf8StrFmt(Guest::tr("Process terminated abnormally with status '%u'"),
558 pCBData->u32Flags);
559 break;
560
561 case PROC_STS_TES: /* Terminated through signal. */
562 errMsg = Utf8StrFmt(Guest::tr("Process terminated via signal with status '%u'"),
563 pCBData->u32Flags);
564 break;
565
566 case PROC_STS_TOK:
567 errMsg = Utf8StrFmt(Guest::tr("Process timed out and was killed"));
568 break;
569
570 case PROC_STS_TOA:
571 errMsg = Utf8StrFmt(Guest::tr("Process timed out and could not be killed"));
572 break;
573
574 case PROC_STS_DWN:
575 errMsg = Utf8StrFmt(Guest::tr("Process exited because system is shutting down"));
576 break;
577
578 default:
579 break;
580 }
581
582 /* Handle process list. */
583 /** @todo What happens on/deal with PID reuse? */
584 /** @todo How to deal with multiple updates at once? */
585 GuestProcessIter it_proc = getProcessByPID(pCBData->u32PID);
586 if (it_proc == mGuestProcessList.end())
587 {
588 /* Not found, add to list. */
589 GuestProcess p;
590 p.mPID = pCBData->u32PID;
591 p.mStatus = pCBData->u32Status;
592 p.mExitCode = pCBData->u32Flags; /* Contains exit code. */
593 p.mFlags = 0;
594
595 mGuestProcessList.push_back(p);
596 }
597 else /* Update list. */
598 {
599 it_proc->mStatus = pCBData->u32Status;
600 it_proc->mExitCode = pCBData->u32Flags; /* Contains exit code. */
601 it_proc->mFlags = 0;
602 }
603
604 if ( !it->pProgress.isNull()
605 && errMsg.length())
606 {
607 if ( !it->pProgress->getCompleted()
608 && !fCancelled)
609 {
610 it->pProgress->notifyComplete(E_FAIL /** @todo Find a better rc! */, COM_IIDOF(IGuest),
611 (CBSTR)Guest::getComponentName(), errMsg.c_str());
612 LogFlowFunc(("Callback (context ID=%u, status=%u) progress marked as completed\n",
613 pData->hdr.u32ContextID, pData->u32Status));
614 }
615 else
616 LogFlowFunc(("Callback (context ID=%u, status=%u) progress already marked as completed\n",
617 pData->hdr.u32ContextID, pData->u32Status));
618 }
619 ASMAtomicWriteBool(&it->bCalled, true);
620 }
621 else
622 LogFlowFunc(("Unexpected callback (magic=%u, context ID=%u) arrived\n", pData->hdr.u32Magic, pData->hdr.u32ContextID));
623 LogFlowFuncLeave();
624 return rc;
625}
626
627/* Function for handling the execution output notification. */
628int Guest::notifyCtrlExecOut(uint32_t u32Function,
629 PHOSTEXECOUTCALLBACKDATA pData)
630{
631 LogFlowFuncEnter();
632 int rc = VINF_SUCCESS;
633
634 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
635
636 AssertPtr(pData);
637 CallbackListIter it = getCtrlCallbackContextByID(pData->hdr.u32ContextID);
638 if (it != mCallbackList.end())
639 {
640 Assert(!it->bCalled);
641 PHOSTEXECOUTCALLBACKDATA pCBData = (HOSTEXECOUTCALLBACKDATA*)it->pvData;
642 AssertPtr(pCBData);
643
644 pCBData->u32PID = pData->u32PID;
645 pCBData->u32HandleId = pData->u32HandleId;
646 pCBData->u32Flags = pData->u32Flags;
647
648 /* Make sure we really got something! */
649 if ( pData->cbData
650 && pData->pvData)
651 {
652 /* Allocate data buffer and copy it */
653 pCBData->pvData = RTMemAlloc(pData->cbData);
654 pCBData->cbData = pData->cbData;
655
656 AssertReturn(pCBData->pvData, VERR_NO_MEMORY);
657 memcpy(pCBData->pvData, pData->pvData, pData->cbData);
658 }
659 else
660 {
661 pCBData->pvData = NULL;
662 pCBData->cbData = 0;
663 }
664 ASMAtomicWriteBool(&it->bCalled, true);
665 }
666 else
667 LogFlowFunc(("Unexpected callback (magic=%u, context ID=%u) arrived\n", pData->hdr.u32Magic, pData->hdr.u32ContextID));
668 LogFlowFuncLeave();
669 return rc;
670}
671
672Guest::CallbackListIter Guest::getCtrlCallbackContextByID(uint32_t u32ContextID)
673{
674 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
675
676 /** @todo Maybe use a map instead of list for fast context lookup. */
677 CallbackListIter it;
678 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
679 {
680 if (it->mContextID == u32ContextID)
681 return (it);
682 }
683 return it;
684}
685
686Guest::GuestProcessIter Guest::getProcessByPID(uint32_t u32PID)
687{
688 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
689
690 /** @todo Maybe use a map instead of list for fast context lookup. */
691 GuestProcessIter it;
692 for (it = mGuestProcessList.begin(); it != mGuestProcessList.end(); it++)
693 {
694 if (it->mPID == u32PID)
695 return (it);
696 }
697 return it;
698}
699
700/* No locking here; */
701void Guest::destroyCtrlCallbackContext(Guest::CallbackListIter it)
702{
703 LogFlowFuncEnter();
704 if (it->pvData)
705 {
706 RTMemFree(it->pvData);
707 it->pvData = NULL;
708 it->cbData = 0;
709 }
710
711 /* Notify outstanding waits for progress ... */
712 if (it->pProgress && !it->pProgress.isNull())
713 {
714 /* Only cancel if not canceled before! */
715 BOOL fCancelled;
716 if (SUCCEEDED(it->pProgress->COMGETTER(Canceled)(&fCancelled)) && !fCancelled)
717 it->pProgress->Cancel();
718 /*
719 * Do *not NULL pProgress here, because waiting function like executeProcess()
720 * will still rely on this object for checking whether they have to give up!
721 */
722 }
723 LogFlowFuncLeave();
724}
725
726/* Adds a callback with a user provided data block and an optional progress object
727 * to the callback list. A callback is identified by a unique context ID which is used
728 * to identify a callback from the guest side. */
729uint32_t Guest::addCtrlCallbackContext(eVBoxGuestCtrlCallbackType enmType, void *pvData, uint32_t cbData, Progress *pProgress)
730{
731 LogFlowFuncEnter();
732 uint32_t uNewContext = ASMAtomicIncU32(&mNextContextID);
733 if (uNewContext == UINT32_MAX)
734 ASMAtomicUoWriteU32(&mNextContextID, 1000);
735
736 /** @todo Put this stuff into a constructor! */
737 CallbackContext context;
738 context.mContextID = uNewContext;
739 context.mType = enmType;
740 context.bCalled = false;
741 context.pvData = pvData;
742 context.cbData = cbData;
743 context.pProgress = pProgress;
744
745 uint32_t nCallbacks;
746 {
747 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
748 mCallbackList.push_back(context);
749 nCallbacks = mCallbackList.size();
750 }
751
752#if 0
753 if (nCallbacks > 256) /* Don't let the container size get too big! */
754 {
755 Guest::CallbackListIter it = mCallbackList.begin();
756 destroyCtrlCallbackContext(it);
757 {
758 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
759 mCallbackList.erase(it);
760 }
761 }
762#endif
763
764 LogFlowFuncLeave();
765 return uNewContext;
766}
767#endif /* VBOX_WITH_GUEST_CONTROL */
768
769STDMETHODIMP Guest::ExecuteProcess(IN_BSTR aCommand, ULONG aFlags,
770 ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
771 IN_BSTR aUserName, IN_BSTR aPassword,
772 ULONG aTimeoutMS, ULONG *aPID, IProgress **aProgress)
773{
774#ifndef VBOX_WITH_GUEST_CONTROL
775 ReturnComNotImplemented();
776#else /* VBOX_WITH_GUEST_CONTROL */
777 using namespace guestControl;
778
779 CheckComArgStrNotEmptyOrNull(aCommand);
780 CheckComArgOutPointerValid(aPID);
781 CheckComArgOutPointerValid(aProgress);
782
783 /* Do not allow anonymous executions (with system rights). */
784 if (RT_UNLIKELY((aUserName) == NULL || *(aUserName) == '\0'))
785 return setError(E_INVALIDARG, tr("No user name specified"));
786
787 AutoCaller autoCaller(this);
788 if (FAILED(autoCaller.rc())) return autoCaller.rc();
789
790 if (aFlags != 0) /* Flags are not supported at the moment. */
791 return E_INVALIDARG;
792
793 HRESULT rc = S_OK;
794
795 try
796 {
797 /*
798 * Create progress object.
799 */
800 ComObjPtr <Progress> progress;
801 rc = progress.createObject();
802 if (SUCCEEDED(rc))
803 {
804 rc = progress->init(static_cast<IGuest*>(this),
805 BstrFmt(tr("Executing process")),
806 TRUE);
807 }
808 if (FAILED(rc)) return rc;
809
810 /*
811 * Prepare process execution.
812 */
813 int vrc = VINF_SUCCESS;
814 Utf8Str Utf8Command(aCommand);
815
816 /* Adjust timeout */
817 if (aTimeoutMS == 0)
818 aTimeoutMS = UINT32_MAX;
819
820 /* Prepare arguments. */
821 char **papszArgv = NULL;
822 uint32_t uNumArgs = 0;
823 if (aArguments > 0)
824 {
825 com::SafeArray<IN_BSTR> args(ComSafeArrayInArg(aArguments));
826 uNumArgs = args.size();
827 papszArgv = (char**)RTMemAlloc(sizeof(char*) * (uNumArgs + 1));
828 AssertReturn(papszArgv, E_OUTOFMEMORY);
829 for (unsigned i = 0; RT_SUCCESS(vrc) && i < uNumArgs; i++)
830 {
831 int cbLen = RTStrAPrintf(&papszArgv[i], "%s", Utf8Str(args[i]).raw());
832 if (cbLen < 0)
833 vrc = VERR_NO_MEMORY;
834
835 }
836 papszArgv[uNumArgs] = NULL;
837 }
838
839 Utf8Str Utf8UserName(aUserName);
840 Utf8Str Utf8Password(aPassword);
841 if (RT_SUCCESS(vrc))
842 {
843 uint32_t uContextID = 0;
844
845 char *pszArgs = NULL;
846 if (uNumArgs > 0)
847 vrc = RTGetOptArgvToString(&pszArgs, papszArgv, 0);
848 if (RT_SUCCESS(vrc))
849 {
850 uint32_t cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
851
852 /* Prepare environment. */
853 void *pvEnv = NULL;
854 uint32_t uNumEnv = 0;
855 uint32_t cbEnv = 0;
856 if (aEnvironment > 0)
857 {
858 com::SafeArray<IN_BSTR> env(ComSafeArrayInArg(aEnvironment));
859
860 for (unsigned i = 0; i < env.size(); i++)
861 {
862 vrc = prepareExecuteEnv(Utf8Str(env[i]).raw(), &pvEnv, &cbEnv, &uNumEnv);
863 if (RT_FAILURE(vrc))
864 break;
865 }
866 }
867
868 if (RT_SUCCESS(vrc))
869 {
870 PHOSTEXECCALLBACKDATA pData = (HOSTEXECCALLBACKDATA*)RTMemAlloc(sizeof(HOSTEXECCALLBACKDATA));
871 AssertReturn(pData, VBOX_E_IPRT_ERROR);
872 uContextID = addCtrlCallbackContext(VBOXGUESTCTRLCALLBACKTYPE_EXEC_START,
873 pData, sizeof(HOSTEXECCALLBACKDATA), progress);
874 Assert(uContextID > 0);
875
876 VBOXHGCMSVCPARM paParms[15];
877 int i = 0;
878 paParms[i++].setUInt32(uContextID);
879 paParms[i++].setPointer((void*)Utf8Command.raw(), (uint32_t)strlen(Utf8Command.raw()) + 1);
880 paParms[i++].setUInt32(aFlags);
881 paParms[i++].setUInt32(uNumArgs);
882 paParms[i++].setPointer((void*)pszArgs, cbArgs);
883 paParms[i++].setUInt32(uNumEnv);
884 paParms[i++].setUInt32(cbEnv);
885 paParms[i++].setPointer((void*)pvEnv, cbEnv);
886 paParms[i++].setPointer((void*)Utf8UserName.raw(), (uint32_t)strlen(Utf8UserName.raw()) + 1);
887 paParms[i++].setPointer((void*)Utf8Password.raw(), (uint32_t)strlen(Utf8Password.raw()) + 1);
888 paParms[i++].setUInt32(aTimeoutMS);
889
890 VMMDev *vmmDev;
891 {
892 /* Make sure mParent is valid, so set the read lock while using.
893 * Do not keep this lock while doing the actual call, because in the meanwhile
894 * another thread could request a write lock which would be a bad idea ... */
895 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
896
897 /* Forward the information to the VMM device. */
898 AssertPtr(mParent);
899 vmmDev = mParent->getVMMDev();
900 }
901
902 if (vmmDev)
903 {
904 LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
905 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_CMD,
906 i, paParms);
907 }
908 else
909 vrc = VERR_INVALID_VM_HANDLE;
910 RTMemFree(pvEnv);
911 }
912 RTStrFree(pszArgs);
913 }
914 if (RT_SUCCESS(vrc))
915 {
916 LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
917
918 /*
919 * Wait for the HGCM low level callback until the process
920 * has been started (or something went wrong). This is necessary to
921 * get the PID.
922 */
923 CallbackListIter it = getCtrlCallbackContextByID(uContextID);
924 BOOL fCanceled = FALSE;
925 if (it != mCallbackList.end())
926 {
927 uint64_t u64Started = RTTimeMilliTS();
928 while (!it->bCalled)
929 {
930 /* Check for timeout. */
931 unsigned cMsWait;
932 if (aTimeoutMS == RT_INDEFINITE_WAIT)
933 cMsWait = 10;
934 else
935 {
936 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
937 if (cMsElapsed >= aTimeoutMS)
938 break; /* Timed out. */
939 cMsWait = RT_MIN(10, aTimeoutMS - (uint32_t)cMsElapsed);
940 }
941
942 /* Check for manual stop. */
943 if (!it->pProgress.isNull())
944 {
945 rc = it->pProgress->COMGETTER(Canceled)(&fCanceled);
946 if (FAILED(rc)) throw rc;
947 if (fCanceled)
948 break; /* Client wants to abort. */
949 }
950 RTThreadSleep(cMsWait);
951 }
952 }
953
954 /* Was the whole thing canceled? */
955 if (!fCanceled)
956 {
957 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
958
959 PHOSTEXECCALLBACKDATA pData = (HOSTEXECCALLBACKDATA*)it->pvData;
960 Assert(it->cbData == sizeof(HOSTEXECCALLBACKDATA));
961 AssertPtr(pData);
962
963 if (it->bCalled)
964 {
965 /* Did we get some status? */
966 switch (pData->u32Status)
967 {
968 case PROC_STS_STARTED:
969 /* Process is (still) running; get PID. */
970 *aPID = pData->u32PID;
971 break;
972
973 /* In any other case the process either already
974 * terminated or something else went wrong, so no PID ... */
975 case PROC_STS_TEN: /* Terminated normally. */
976 case PROC_STS_TEA: /* Terminated abnormally. */
977 case PROC_STS_TES: /* Terminated through signal. */
978 case PROC_STS_TOK:
979 case PROC_STS_TOA:
980 case PROC_STS_DWN:
981 /*
982 * Process (already) ended, but we want to get the
983 * PID anyway to retrieve the output in a later call.
984 */
985 *aPID = pData->u32PID;
986 break;
987
988 case PROC_STS_ERROR:
989 vrc = pData->u32Flags; /* u32Flags member contains IPRT error code. */
990 break;
991
992 default:
993 vrc = VERR_INVALID_PARAMETER; /* Unknown status, should never happen! */
994 break;
995 }
996 }
997 else /* If callback not called within time ... well, that's a timeout! */
998 vrc = VERR_TIMEOUT;
999
1000 /*
1001 * Do *not* remove the callback yet - we might wait with the IProgress object on something
1002 * else (like end of process) ...
1003 */
1004 if (RT_FAILURE(vrc))
1005 {
1006 if (vrc == VERR_FILE_NOT_FOUND) /* This is the most likely error. */
1007 {
1008 rc = setError(VBOX_E_IPRT_ERROR,
1009 tr("The file '%s' was not found on guest"), Utf8Command.raw());
1010 }
1011 else if (vrc == VERR_BAD_EXE_FORMAT)
1012 {
1013 rc = setError(VBOX_E_IPRT_ERROR,
1014 tr("The file '%s' is not an executable format on guest"), Utf8Command.raw());
1015 }
1016 else if (vrc == VERR_LOGON_FAILURE)
1017 {
1018 rc = setError(VBOX_E_IPRT_ERROR,
1019 tr("The specified user '%s' was not able to logon on guest"), Utf8UserName.raw());
1020 }
1021 else if (vrc == VERR_TIMEOUT)
1022 {
1023 rc = setError(VBOX_E_IPRT_ERROR,
1024 tr("The guest did not respond within time (%ums)"), aTimeoutMS);
1025 }
1026 else if (vrc == VERR_INVALID_PARAMETER)
1027 {
1028 rc = setError(VBOX_E_IPRT_ERROR,
1029 tr("The guest reported an unknown process status (%u)"), pData->u32Status);
1030 }
1031 else if (vrc == VERR_PERMISSION_DENIED)
1032 {
1033 rc = setError(VBOX_E_IPRT_ERROR,
1034 tr("Invalid user/password credentials"));
1035 }
1036 else
1037 {
1038 rc = setError(E_UNEXPECTED,
1039 tr("The service call failed with error %Rrc"), vrc);
1040 }
1041 }
1042 else /* Execution went fine. */
1043 {
1044 /* Return the progress to the caller. */
1045 progress.queryInterfaceTo(aProgress);
1046 }
1047 }
1048 else /* Operation was canceled. */
1049 {
1050 rc = setError(VBOX_E_IPRT_ERROR,
1051 tr("The operation was canceled"));
1052 }
1053 }
1054 else /* HGCM related error codes .*/
1055 {
1056 if (vrc == VERR_INVALID_VM_HANDLE)
1057 {
1058 rc = setError(VBOX_E_VM_ERROR,
1059 tr("VMM device is not available (is the VM running?)"));
1060 }
1061 else if (vrc == VERR_TIMEOUT)
1062 {
1063 rc = setError(VBOX_E_VM_ERROR,
1064 tr("The guest execution service is not ready"));
1065 }
1066 else /* HGCM call went wrong. */
1067 {
1068 rc = setError(E_UNEXPECTED,
1069 tr("The HGCM call failed with error %Rrc"), vrc);
1070 }
1071 }
1072
1073 for (unsigned i = 0; i < uNumArgs; i++)
1074 RTMemFree(papszArgv[i]);
1075 RTMemFree(papszArgv);
1076 }
1077 }
1078 catch (std::bad_alloc &)
1079 {
1080 rc = E_OUTOFMEMORY;
1081 }
1082 return rc;
1083#endif /* VBOX_WITH_GUEST_CONTROL */
1084}
1085
1086STDMETHODIMP Guest::GetProcessOutput(ULONG aPID, ULONG aFlags, ULONG aTimeoutMS, ULONG64 aSize, ComSafeArrayOut(BYTE, aData))
1087{
1088#ifndef VBOX_WITH_GUEST_CONTROL
1089 ReturnComNotImplemented();
1090#else /* VBOX_WITH_GUEST_CONTROL */
1091 using namespace guestControl;
1092
1093 CheckComArgExpr(aPID, aPID > 0);
1094
1095 if (aFlags != 0) /* Flags are not supported at the moment. */
1096 return E_INVALIDARG;
1097
1098 AutoCaller autoCaller(this);
1099 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1100
1101 HRESULT rc = S_OK;
1102
1103 try
1104 {
1105 /*
1106 * Create progress object.
1107 * Note that we need at least a local progress object here in order
1108 * to get notified when someone cancels the operation.
1109 */
1110 ComObjPtr <Progress> progress;
1111 rc = progress.createObject();
1112 if (SUCCEEDED(rc))
1113 {
1114 rc = progress->init(static_cast<IGuest*>(this),
1115 BstrFmt(tr("Getting output of process")),
1116 TRUE);
1117 }
1118 if (FAILED(rc)) return rc;
1119
1120 /* Adjust timeout */
1121 if (aTimeoutMS == 0)
1122 aTimeoutMS = UINT32_MAX;
1123
1124 /* Search for existing PID. */
1125 PHOSTEXECOUTCALLBACKDATA pData = (HOSTEXECOUTCALLBACKDATA*)RTMemAlloc(sizeof(HOSTEXECOUTCALLBACKDATA));
1126 AssertReturn(pData, VBOX_E_IPRT_ERROR);
1127 uint32_t uContextID = addCtrlCallbackContext(VBOXGUESTCTRLCALLBACKTYPE_EXEC_OUTPUT,
1128 pData, sizeof(HOSTEXECOUTCALLBACKDATA), progress);
1129 Assert(uContextID > 0);
1130
1131 size_t cbData = (size_t)RT_MIN(aSize, _64K);
1132 com::SafeArray<BYTE> outputData(cbData);
1133
1134 VBOXHGCMSVCPARM paParms[5];
1135 int i = 0;
1136 paParms[i++].setUInt32(uContextID);
1137 paParms[i++].setUInt32(aPID);
1138 paParms[i++].setUInt32(aFlags); /** @todo Should represent stdout and/or stderr. */
1139
1140 int vrc = VINF_SUCCESS;
1141
1142 {
1143 VMMDev *vmmDev;
1144 {
1145 /* Make sure mParent is valid, so set the read lock while using.
1146 * Do not keep this lock while doing the actual call, because in the meanwhile
1147 * another thread could request a write lock which would be a bad idea ... */
1148 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1149
1150 /* Forward the information to the VMM device. */
1151 AssertPtr(mParent);
1152 vmmDev = mParent->getVMMDev();
1153 }
1154
1155 if (vmmDev)
1156 {
1157 LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
1158 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_GET_OUTPUT,
1159 i, paParms);
1160 }
1161 }
1162
1163 if (RT_SUCCESS(vrc))
1164 {
1165 LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
1166
1167 /*
1168 * Wait for the HGCM low level callback until the process
1169 * has been started (or something went wrong). This is necessary to
1170 * get the PID.
1171 */
1172 CallbackListIter it = getCtrlCallbackContextByID(uContextID);
1173 BOOL fCanceled = FALSE;
1174 if (it != mCallbackList.end())
1175 {
1176 uint64_t u64Started = RTTimeMilliTS();
1177 while (!it->bCalled)
1178 {
1179 /* Check for timeout. */
1180 unsigned cMsWait;
1181 if (aTimeoutMS == RT_INDEFINITE_WAIT)
1182 cMsWait = 10;
1183 else
1184 {
1185 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
1186 if (cMsElapsed >= aTimeoutMS)
1187 break; /* Timed out. */
1188 cMsWait = RT_MIN(10, aTimeoutMS - (uint32_t)cMsElapsed);
1189 }
1190
1191 /* Check for manual stop. */
1192 if (!it->pProgress.isNull())
1193 {
1194 rc = it->pProgress->COMGETTER(Canceled)(&fCanceled);
1195 if (FAILED(rc)) throw rc;
1196 if (fCanceled)
1197 break; /* Client wants to abort. */
1198 }
1199 RTThreadSleep(cMsWait);
1200 }
1201
1202 /* Was the whole thing canceled? */
1203 if (!fCanceled)
1204 {
1205 if (it->bCalled)
1206 {
1207 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1208
1209 /* Did we get some output? */
1210 pData = (HOSTEXECOUTCALLBACKDATA*)it->pvData;
1211 Assert(it->cbData == sizeof(HOSTEXECOUTCALLBACKDATA));
1212 AssertPtr(pData);
1213
1214 if (pData->cbData)
1215 {
1216 /* Do we need to resize the array? */
1217 if (pData->cbData > cbData)
1218 outputData.resize(pData->cbData);
1219
1220 /* Fill output in supplied out buffer. */
1221 memcpy(outputData.raw(), pData->pvData, pData->cbData);
1222 outputData.resize(pData->cbData); /* Shrink to fit actual buffer size. */
1223 }
1224 else
1225 vrc = VERR_NO_DATA; /* This is not an error we want to report to COM. */
1226 }
1227 else /* If callback not called within time ... well, that's a timeout! */
1228 vrc = VERR_TIMEOUT;
1229 }
1230 else /* Operation was canceled. */
1231 vrc = VERR_CANCELLED;
1232
1233 if (RT_FAILURE(vrc))
1234 {
1235 if (vrc == VERR_NO_DATA)
1236 {
1237 /* This is not an error we want to report to COM. */
1238 }
1239 else if (vrc == VERR_TIMEOUT)
1240 {
1241 rc = setError(VBOX_E_IPRT_ERROR,
1242 tr("The guest did not output within time (%ums)"), aTimeoutMS);
1243 }
1244 else if (vrc == VERR_CANCELLED)
1245 {
1246 rc = setError(VBOX_E_IPRT_ERROR,
1247 tr("The operation was canceled"));
1248 }
1249 else
1250 {
1251 rc = setError(E_UNEXPECTED,
1252 tr("The service call failed with error %Rrc"), vrc);
1253 }
1254 }
1255
1256 {
1257 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1258 destroyCtrlCallbackContext(it);
1259 }
1260 }
1261 else /* PID lookup failed. */
1262 rc = setError(VBOX_E_IPRT_ERROR,
1263 tr("Process (PID %u) not found!"), aPID);
1264 }
1265 else /* HGCM operation failed. */
1266 rc = setError(E_UNEXPECTED,
1267 tr("The HGCM call failed with error %Rrc"), vrc);
1268
1269 /* Cleanup. */
1270 progress->uninit();
1271 progress.setNull();
1272
1273 /* If something failed (or there simply was no data, indicated by VERR_NO_DATA,
1274 * we return an empty array so that the frontend knows when to give up. */
1275 if (RT_FAILURE(vrc) || FAILED(rc))
1276 outputData.resize(0);
1277 outputData.detachTo(ComSafeArrayOutArg(aData));
1278 }
1279 catch (std::bad_alloc &)
1280 {
1281 rc = E_OUTOFMEMORY;
1282 }
1283 return rc;
1284#endif
1285}
1286
1287STDMETHODIMP Guest::GetProcessStatus(ULONG aPID, ULONG *aExitCode, ULONG *aFlags, ULONG *aStatus)
1288{
1289#ifndef VBOX_WITH_GUEST_CONTROL
1290 ReturnComNotImplemented();
1291#else /* VBOX_WITH_GUEST_CONTROL */
1292 using namespace guestControl;
1293
1294 AutoCaller autoCaller(this);
1295 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1296
1297 HRESULT rc = S_OK;
1298
1299 try
1300 {
1301 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1302
1303 GuestProcessIterConst it;
1304 for (it = mGuestProcessList.begin(); it != mGuestProcessList.end(); it++)
1305 {
1306 if (it->mPID == aPID)
1307 break;
1308 }
1309
1310 if (it != mGuestProcessList.end())
1311 {
1312 *aExitCode = it->mExitCode;
1313 *aFlags = it->mFlags;
1314 *aStatus = it->mStatus;
1315 }
1316 else
1317 rc = setError(VBOX_E_IPRT_ERROR,
1318 tr("Process (PID %u) not found!"), aPID);
1319 }
1320 catch (std::bad_alloc &)
1321 {
1322 rc = E_OUTOFMEMORY;
1323 }
1324 return rc;
1325#endif
1326}
1327
1328// public methods only for internal purposes
1329/////////////////////////////////////////////////////////////////////////////
1330
1331void Guest::setAdditionsVersion(Bstr aVersion, VBOXOSTYPE aOsType)
1332{
1333 AutoCaller autoCaller(this);
1334 AssertComRCReturnVoid (autoCaller.rc());
1335
1336 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1337
1338 mData.mAdditionsVersion = aVersion;
1339 mData.mAdditionsActive = !aVersion.isEmpty();
1340 /* Older Additions didn't have this finer grained capability bit,
1341 * so enable it by default. Newer Additions will disable it immediately
1342 * if relevant. */
1343 mData.mSupportsGraphics = mData.mAdditionsActive;
1344
1345 mData.mOSTypeId = Global::OSTypeId (aOsType);
1346}
1347
1348void Guest::setSupportsSeamless (BOOL aSupportsSeamless)
1349{
1350 AutoCaller autoCaller(this);
1351 AssertComRCReturnVoid (autoCaller.rc());
1352
1353 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1354
1355 mData.mSupportsSeamless = aSupportsSeamless;
1356}
1357
1358void Guest::setSupportsGraphics (BOOL aSupportsGraphics)
1359{
1360 AutoCaller autoCaller(this);
1361 AssertComRCReturnVoid (autoCaller.rc());
1362
1363 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1364
1365 mData.mSupportsGraphics = aSupportsGraphics;
1366}
1367/* 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