VirtualBox

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

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

Guest Control/IPRT/VBoxManage: More gentle error messages, bug fix for process lookup with different user, correct return value of process start error.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 48.0 KB
Line 
1/* $Id: GuestImpl.cpp 29807 2010-05-26 10:13:20Z 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_DISCONNECTED)
487 {
488 LogFlowFunc(("GUEST_DISCONNECTED\n"));
489
490 PCALLBACKDATACLIENTDISCONNECTED pCBData = reinterpret_cast<PCALLBACKDATACLIENTDISCONNECTED>(pvParms);
491 AssertPtr(pCBData);
492 AssertReturn(sizeof(CALLBACKDATACLIENTDISCONNECTED) == cbParms, VERR_INVALID_PARAMETER);
493 AssertReturn(CALLBACKDATAMAGICCLIENTDISCONNECTED == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
494
495 rc = pGuest->notifyCtrlClientDisconnected(u32Function, pCBData);
496 }
497 else if (u32Function == GUEST_EXEC_SEND_STATUS)
498 {
499 LogFlowFunc(("GUEST_EXEC_SEND_STATUS\n"));
500
501 PCALLBACKDATAEXECSTATUS pCBData = reinterpret_cast<PCALLBACKDATAEXECSTATUS>(pvParms);
502 AssertPtr(pCBData);
503 AssertReturn(sizeof(CALLBACKDATAEXECSTATUS) == cbParms, VERR_INVALID_PARAMETER);
504 AssertReturn(CALLBACKDATAMAGICEXECSTATUS == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
505
506 rc = pGuest->notifyCtrlExecStatus(u32Function, pCBData);
507 }
508 else if (u32Function == GUEST_EXEC_SEND_OUTPUT)
509 {
510 LogFlowFunc(("GUEST_EXEC_SEND_OUTPUT\n"));
511
512 PCALLBACKDATAEXECOUT pCBData = reinterpret_cast<PCALLBACKDATAEXECOUT>(pvParms);
513 AssertPtr(pCBData);
514 AssertReturn(sizeof(CALLBACKDATAEXECOUT) == cbParms, VERR_INVALID_PARAMETER);
515 AssertReturn(CALLBACKDATAMAGICEXECOUT == pCBData->hdr.u32Magic, VERR_INVALID_PARAMETER);
516
517 rc = pGuest->notifyCtrlExecOut(u32Function, pCBData);
518 }
519 else
520 rc = VERR_NOT_SUPPORTED;
521 return rc;
522}
523
524/* Function for handling the execution start/termination notification. */
525int Guest::notifyCtrlExecStatus(uint32_t u32Function,
526 PCALLBACKDATAEXECSTATUS pData)
527{
528 LogFlowFuncEnter();
529 int rc = VINF_SUCCESS;
530
531 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
532
533 AssertPtr(pData);
534 CallbackListIter it = getCtrlCallbackContextByID(pData->hdr.u32ContextID);
535
536 /* Callback can be called several times. */
537 if (it != mCallbackList.end())
538 {
539 PCALLBACKDATAEXECSTATUS pCBData = (PCALLBACKDATAEXECSTATUS)it->pvData;
540 AssertPtr(pCBData);
541
542 pCBData->u32PID = pData->u32PID;
543 pCBData->u32Status = pData->u32Status;
544 pCBData->u32Flags = pData->u32Flags;
545 /** @todo Copy void* buffer contents! */
546
547 /* Was progress canceled before? */
548 BOOL fCancelled;
549 it->pProgress->COMGETTER(Canceled)(&fCancelled);
550
551 /* Do progress handling. */
552 Utf8Str errMsg;
553 HRESULT rc2 = S_OK;
554 switch (pData->u32Status)
555 {
556 case PROC_STS_STARTED:
557 break;
558
559 case PROC_STS_TEN: /* Terminated normally. */
560 if ( !it->pProgress->getCompleted()
561 && !fCancelled)
562 {
563 it->pProgress->notifyComplete(S_OK);
564 }
565 break;
566
567 case PROC_STS_TEA: /* Terminated abnormally. */
568 errMsg = Utf8StrFmt(Guest::tr("Process terminated abnormally with status '%u'"),
569 pCBData->u32Flags);
570 break;
571
572 case PROC_STS_TES: /* Terminated through signal. */
573 errMsg = Utf8StrFmt(Guest::tr("Process terminated via signal with status '%u'"),
574 pCBData->u32Flags);
575 break;
576
577 case PROC_STS_TOK:
578 errMsg = Utf8StrFmt(Guest::tr("Process timed out and was killed"));
579 break;
580
581 case PROC_STS_TOA:
582 errMsg = Utf8StrFmt(Guest::tr("Process timed out and could not be killed"));
583 break;
584
585 case PROC_STS_DWN:
586 errMsg = Utf8StrFmt(Guest::tr("Process exited because system is shutting down"));
587 break;
588
589 default:
590 break;
591 }
592
593 /* Handle process list. */
594 /** @todo What happens on/deal with PID reuse? */
595 /** @todo How to deal with multiple updates at once? */
596 GuestProcessIter it_proc = getProcessByPID(pCBData->u32PID);
597 if (it_proc == mGuestProcessList.end())
598 {
599 /* Not found, add to list. */
600 GuestProcess p;
601 p.mPID = pCBData->u32PID;
602 p.mStatus = pCBData->u32Status;
603 p.mExitCode = pCBData->u32Flags; /* Contains exit code. */
604 p.mFlags = 0;
605
606 mGuestProcessList.push_back(p);
607 }
608 else /* Update list. */
609 {
610 it_proc->mStatus = pCBData->u32Status;
611 it_proc->mExitCode = pCBData->u32Flags; /* Contains exit code. */
612 it_proc->mFlags = 0;
613 }
614
615 if ( !it->pProgress.isNull()
616 && errMsg.length())
617 {
618 if ( !it->pProgress->getCompleted()
619 && !fCancelled)
620 {
621 it->pProgress->notifyComplete(E_FAIL /** @todo Find a better rc! */, COM_IIDOF(IGuest),
622 (CBSTR)Guest::getComponentName(), errMsg.c_str());
623 LogFlowFunc(("Callback (context ID=%u, status=%u) progress marked as completed\n",
624 pData->hdr.u32ContextID, pData->u32Status));
625 }
626 else
627 LogFlowFunc(("Callback (context ID=%u, status=%u) progress already marked as completed\n",
628 pData->hdr.u32ContextID, pData->u32Status));
629 }
630 ASMAtomicWriteBool(&it->fCalled, true);
631 }
632 else
633 LogFlowFunc(("Unexpected callback (magic=%u, context ID=%u) arrived\n", pData->hdr.u32Magic, pData->hdr.u32ContextID));
634 LogFlowFuncLeave();
635 return rc;
636}
637
638/* Function for handling the execution output notification. */
639int Guest::notifyCtrlExecOut(uint32_t u32Function,
640 PCALLBACKDATAEXECOUT pData)
641{
642 LogFlowFuncEnter();
643 int rc = VINF_SUCCESS;
644
645 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
646
647 AssertPtr(pData);
648 CallbackListIter it = getCtrlCallbackContextByID(pData->hdr.u32ContextID);
649 if (it != mCallbackList.end())
650 {
651 Assert(!it->fCalled);
652 PCALLBACKDATAEXECOUT pCBData = (CALLBACKDATAEXECOUT*)it->pvData;
653 AssertPtr(pCBData);
654
655 pCBData->u32PID = pData->u32PID;
656 pCBData->u32HandleId = pData->u32HandleId;
657 pCBData->u32Flags = pData->u32Flags;
658
659 /* Make sure we really got something! */
660 if ( pData->cbData
661 && pData->pvData)
662 {
663 /* Allocate data buffer and copy it */
664 pCBData->pvData = RTMemAlloc(pData->cbData);
665 pCBData->cbData = pData->cbData;
666
667 AssertReturn(pCBData->pvData, VERR_NO_MEMORY);
668 memcpy(pCBData->pvData, pData->pvData, pData->cbData);
669 }
670 else
671 {
672 pCBData->pvData = NULL;
673 pCBData->cbData = 0;
674 }
675 ASMAtomicWriteBool(&it->fCalled, true);
676 }
677 else
678 LogFlowFunc(("Unexpected callback (magic=%u, context ID=%u) arrived\n", pData->hdr.u32Magic, pData->hdr.u32ContextID));
679 LogFlowFuncLeave();
680 return rc;
681}
682
683int Guest::notifyCtrlClientDisconnected(uint32_t u32Function,
684 PCALLBACKDATACLIENTDISCONNECTED pData)
685{
686 LogFlowFuncEnter();
687 int rc = VINF_SUCCESS;
688
689 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
690
691 /** @todo Maybe use a map instead of list for fast context lookup. */
692 CallbackListIter it;
693 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
694 {
695 if (it->mContextID == pData->hdr.u32ContextID)
696 destroyCtrlCallbackContext(it);
697 }
698
699 LogFlowFuncLeave();
700 return rc;
701}
702
703Guest::CallbackListIter Guest::getCtrlCallbackContextByID(uint32_t u32ContextID)
704{
705 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
706
707 /** @todo Maybe use a map instead of list for fast context lookup. */
708 CallbackListIter it;
709 for (it = mCallbackList.begin(); it != mCallbackList.end(); it++)
710 {
711 if (it->mContextID == u32ContextID)
712 return (it);
713 }
714 return it;
715}
716
717Guest::GuestProcessIter Guest::getProcessByPID(uint32_t u32PID)
718{
719 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
720
721 /** @todo Maybe use a map instead of list for fast context lookup. */
722 GuestProcessIter it;
723 for (it = mGuestProcessList.begin(); it != mGuestProcessList.end(); it++)
724 {
725 if (it->mPID == u32PID)
726 return (it);
727 }
728 return it;
729}
730
731/* No locking here; */
732void Guest::destroyCtrlCallbackContext(Guest::CallbackListIter it)
733{
734 LogFlowFuncEnter();
735 if (it->pvData)
736 {
737 RTMemFree(it->pvData);
738 it->pvData = NULL;
739 it->cbData = 0;
740 }
741
742 /* Notify outstanding waits for progress ... */
743 if (it->pProgress && !it->pProgress.isNull())
744 {
745 /* Only cancel if not canceled before! */
746 BOOL fCancelled;
747 if (SUCCEEDED(it->pProgress->COMGETTER(Canceled)(&fCancelled)) && !fCancelled)
748 it->pProgress->Cancel();
749 /*
750 * Do *not NULL pProgress here, because waiting function like executeProcess()
751 * will still rely on this object for checking whether they have to give up!
752 */
753 }
754 LogFlowFuncLeave();
755}
756
757/* Adds a callback with a user provided data block and an optional progress object
758 * to the callback list. A callback is identified by a unique context ID which is used
759 * to identify a callback from the guest side. */
760uint32_t Guest::addCtrlCallbackContext(eVBoxGuestCtrlCallbackType enmType, void *pvData, uint32_t cbData, Progress *pProgress)
761{
762 LogFlowFuncEnter();
763 uint32_t uNewContext = ASMAtomicIncU32(&mNextContextID);
764 if (uNewContext == UINT32_MAX)
765 ASMAtomicUoWriteU32(&mNextContextID, 1000);
766
767 /** @todo Put this stuff into a constructor! */
768 CallbackContext context;
769 context.mContextID = uNewContext;
770 context.mType = enmType;
771 context.fCalled = false;
772 context.pvData = pvData;
773 context.cbData = cbData;
774 context.pProgress = pProgress;
775
776 uint32_t nCallbacks;
777 {
778 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
779 mCallbackList.push_back(context);
780 nCallbacks = mCallbackList.size();
781 }
782
783#if 0
784 if (nCallbacks > 256) /* Don't let the container size get too big! */
785 {
786 Guest::CallbackListIter it = mCallbackList.begin();
787 destroyCtrlCallbackContext(it);
788 {
789 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
790 mCallbackList.erase(it);
791 }
792 }
793#endif
794
795 LogFlowFuncLeave();
796 return uNewContext;
797}
798#endif /* VBOX_WITH_GUEST_CONTROL */
799
800STDMETHODIMP Guest::ExecuteProcess(IN_BSTR aCommand, ULONG aFlags,
801 ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
802 IN_BSTR aUserName, IN_BSTR aPassword,
803 ULONG aTimeoutMS, ULONG *aPID, IProgress **aProgress)
804{
805#ifndef VBOX_WITH_GUEST_CONTROL
806 ReturnComNotImplemented();
807#else /* VBOX_WITH_GUEST_CONTROL */
808 using namespace guestControl;
809
810 CheckComArgStrNotEmptyOrNull(aCommand);
811 CheckComArgOutPointerValid(aPID);
812 CheckComArgOutPointerValid(aProgress);
813
814 /* Do not allow anonymous executions (with system rights). */
815 if (RT_UNLIKELY((aUserName) == NULL || *(aUserName) == '\0'))
816 return setError(E_INVALIDARG, tr("No user name specified"));
817
818 AutoCaller autoCaller(this);
819 if (FAILED(autoCaller.rc())) return autoCaller.rc();
820
821 if (aFlags != 0) /* Flags are not supported at the moment. */
822 return E_INVALIDARG;
823
824 HRESULT rc = S_OK;
825
826 try
827 {
828 /*
829 * Create progress object.
830 */
831 ComObjPtr <Progress> progress;
832 rc = progress.createObject();
833 if (SUCCEEDED(rc))
834 {
835 rc = progress->init(static_cast<IGuest*>(this),
836 BstrFmt(tr("Executing process")),
837 TRUE);
838 }
839 if (FAILED(rc)) return rc;
840
841 /*
842 * Prepare process execution.
843 */
844 int vrc = VINF_SUCCESS;
845 Utf8Str Utf8Command(aCommand);
846
847 /* Adjust timeout */
848 if (aTimeoutMS == 0)
849 aTimeoutMS = UINT32_MAX;
850
851 /* Prepare arguments. */
852 char **papszArgv = NULL;
853 uint32_t uNumArgs = 0;
854 if (aArguments > 0)
855 {
856 com::SafeArray<IN_BSTR> args(ComSafeArrayInArg(aArguments));
857 uNumArgs = args.size();
858 papszArgv = (char**)RTMemAlloc(sizeof(char*) * (uNumArgs + 1));
859 AssertReturn(papszArgv, E_OUTOFMEMORY);
860 for (unsigned i = 0; RT_SUCCESS(vrc) && i < uNumArgs; i++)
861 {
862 int cbLen = RTStrAPrintf(&papszArgv[i], "%s", Utf8Str(args[i]).raw());
863 if (cbLen < 0)
864 vrc = VERR_NO_MEMORY;
865
866 }
867 papszArgv[uNumArgs] = NULL;
868 }
869
870 Utf8Str Utf8UserName(aUserName);
871 Utf8Str Utf8Password(aPassword);
872 if (RT_SUCCESS(vrc))
873 {
874 uint32_t uContextID = 0;
875
876 char *pszArgs = NULL;
877 if (uNumArgs > 0)
878 vrc = RTGetOptArgvToString(&pszArgs, papszArgv, 0);
879 if (RT_SUCCESS(vrc))
880 {
881 uint32_t cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
882
883 /* Prepare environment. */
884 void *pvEnv = NULL;
885 uint32_t uNumEnv = 0;
886 uint32_t cbEnv = 0;
887 if (aEnvironment > 0)
888 {
889 com::SafeArray<IN_BSTR> env(ComSafeArrayInArg(aEnvironment));
890
891 for (unsigned i = 0; i < env.size(); i++)
892 {
893 vrc = prepareExecuteEnv(Utf8Str(env[i]).raw(), &pvEnv, &cbEnv, &uNumEnv);
894 if (RT_FAILURE(vrc))
895 break;
896 }
897 }
898
899 if (RT_SUCCESS(vrc))
900 {
901 PCALLBACKDATAEXECSTATUS pData = (PCALLBACKDATAEXECSTATUS)RTMemAlloc(sizeof(CALLBACKDATAEXECSTATUS));
902 AssertReturn(pData, VBOX_E_IPRT_ERROR);
903 uContextID = addCtrlCallbackContext(VBOXGUESTCTRLCALLBACKTYPE_EXEC_START,
904 pData, sizeof(CALLBACKDATAEXECSTATUS), progress);
905 Assert(uContextID > 0);
906
907 VBOXHGCMSVCPARM paParms[15];
908 int i = 0;
909 paParms[i++].setUInt32(uContextID);
910 paParms[i++].setPointer((void*)Utf8Command.raw(), (uint32_t)strlen(Utf8Command.raw()) + 1);
911 paParms[i++].setUInt32(aFlags);
912 paParms[i++].setUInt32(uNumArgs);
913 paParms[i++].setPointer((void*)pszArgs, cbArgs);
914 paParms[i++].setUInt32(uNumEnv);
915 paParms[i++].setUInt32(cbEnv);
916 paParms[i++].setPointer((void*)pvEnv, cbEnv);
917 paParms[i++].setPointer((void*)Utf8UserName.raw(), (uint32_t)strlen(Utf8UserName.raw()) + 1);
918 paParms[i++].setPointer((void*)Utf8Password.raw(), (uint32_t)strlen(Utf8Password.raw()) + 1);
919 paParms[i++].setUInt32(aTimeoutMS);
920
921 VMMDev *vmmDev;
922 {
923 /* Make sure mParent is valid, so set the read lock while using.
924 * Do not keep this lock while doing the actual call, because in the meanwhile
925 * another thread could request a write lock which would be a bad idea ... */
926 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
927
928 /* Forward the information to the VMM device. */
929 AssertPtr(mParent);
930 vmmDev = mParent->getVMMDev();
931 }
932
933 if (vmmDev)
934 {
935 LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
936 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_CMD,
937 i, paParms);
938 }
939 else
940 vrc = VERR_INVALID_VM_HANDLE;
941 RTMemFree(pvEnv);
942 }
943 RTStrFree(pszArgs);
944 }
945 if (RT_SUCCESS(vrc))
946 {
947 LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
948
949 /*
950 * Wait for the HGCM low level callback until the process
951 * has been started (or something went wrong). This is necessary to
952 * get the PID.
953 */
954 CallbackListIter it = getCtrlCallbackContextByID(uContextID);
955 BOOL fCanceled = FALSE;
956 if (it != mCallbackList.end())
957 {
958 uint64_t u64Started = RTTimeMilliTS();
959 while (!it->fCalled)
960 {
961 /* Check for timeout. */
962 unsigned cMsWait;
963 if (aTimeoutMS == RT_INDEFINITE_WAIT)
964 cMsWait = 10;
965 else
966 {
967 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
968 if (cMsElapsed >= aTimeoutMS)
969 break; /* Timed out. */
970 cMsWait = RT_MIN(10, aTimeoutMS - (uint32_t)cMsElapsed);
971 }
972
973 /* Check for manual stop. */
974 if (!it->pProgress.isNull())
975 {
976 rc = it->pProgress->COMGETTER(Canceled)(&fCanceled);
977 if (FAILED(rc)) throw rc;
978 if (fCanceled)
979 break; /* Client wants to abort. */
980 }
981 RTThreadSleep(cMsWait);
982 }
983 }
984
985 /* Was the whole thing canceled? */
986 if (!fCanceled)
987 {
988 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
989
990 PCALLBACKDATAEXECSTATUS pData = (PCALLBACKDATAEXECSTATUS)it->pvData;
991 Assert(it->cbData == sizeof(CALLBACKDATAEXECSTATUS));
992 AssertPtr(pData);
993
994 if (it->fCalled)
995 {
996 /* Did we get some status? */
997 switch (pData->u32Status)
998 {
999 case PROC_STS_STARTED:
1000 /* Process is (still) running; get PID. */
1001 *aPID = pData->u32PID;
1002 break;
1003
1004 /* In any other case the process either already
1005 * terminated or something else went wrong, so no PID ... */
1006 case PROC_STS_TEN: /* Terminated normally. */
1007 case PROC_STS_TEA: /* Terminated abnormally. */
1008 case PROC_STS_TES: /* Terminated through signal. */
1009 case PROC_STS_TOK:
1010 case PROC_STS_TOA:
1011 case PROC_STS_DWN:
1012 /*
1013 * Process (already) ended, but we want to get the
1014 * PID anyway to retrieve the output in a later call.
1015 */
1016 *aPID = pData->u32PID;
1017 break;
1018
1019 case PROC_STS_ERROR:
1020 vrc = pData->u32Flags; /* u32Flags member contains IPRT error code. */
1021 break;
1022
1023 default:
1024 vrc = VERR_INVALID_PARAMETER; /* Unknown status, should never happen! */
1025 break;
1026 }
1027 }
1028 else /* If callback not called within time ... well, that's a timeout! */
1029 vrc = VERR_TIMEOUT;
1030
1031 /*
1032 * Do *not* remove the callback yet - we might wait with the IProgress object on something
1033 * else (like end of process) ...
1034 */
1035 if (RT_FAILURE(vrc))
1036 {
1037 if (vrc == VERR_FILE_NOT_FOUND) /* This is the most likely error. */
1038 {
1039 rc = setError(VBOX_E_IPRT_ERROR,
1040 tr("The file '%s' was not found on guest"), Utf8Command.raw());
1041 }
1042 else if (vrc == VERR_PATH_NOT_FOUND)
1043 {
1044 rc = setError(VBOX_E_IPRT_ERROR,
1045 tr("The path to file '%s' was not found on guest"), Utf8Command.raw());
1046 }
1047 else if (vrc == VERR_BAD_EXE_FORMAT)
1048 {
1049 rc = setError(VBOX_E_IPRT_ERROR,
1050 tr("The file '%s' is not an executable format on guest"), Utf8Command.raw());
1051 }
1052 else if (vrc == VERR_LOGON_FAILURE)
1053 {
1054 rc = setError(VBOX_E_IPRT_ERROR,
1055 tr("The specified user '%s' was not able to logon on guest"), Utf8UserName.raw());
1056 }
1057 else if (vrc == VERR_TIMEOUT)
1058 {
1059 rc = setError(VBOX_E_IPRT_ERROR,
1060 tr("The guest did not respond within time (%ums)"), aTimeoutMS);
1061 }
1062 else if (vrc == VERR_PERMISSION_DENIED)
1063 {
1064 rc = setError(VBOX_E_IPRT_ERROR,
1065 tr("Invalid user/password credentials"));
1066 }
1067 else
1068 {
1069 if (pData->u32Status == PROC_STS_ERROR)
1070 rc = setError(VBOX_E_IPRT_ERROR,
1071 tr("Process could not be started: %Rrc"), pData->u32Flags);
1072 else
1073 rc = setError(E_UNEXPECTED,
1074 tr("The service call failed with error %Rrc"), vrc);
1075 }
1076 }
1077 else /* Execution went fine. */
1078 {
1079 /* Return the progress to the caller. */
1080 progress.queryInterfaceTo(aProgress);
1081 }
1082 }
1083 else /* Operation was canceled. */
1084 {
1085 rc = setError(VBOX_E_IPRT_ERROR,
1086 tr("The operation was canceled"));
1087 }
1088 }
1089 else /* HGCM related error codes .*/
1090 {
1091 if (vrc == VERR_INVALID_VM_HANDLE)
1092 {
1093 rc = setError(VBOX_E_VM_ERROR,
1094 tr("VMM device is not available (is the VM running?)"));
1095 }
1096 else if (vrc == VERR_TIMEOUT)
1097 {
1098 rc = setError(VBOX_E_VM_ERROR,
1099 tr("The guest execution service is not ready"));
1100 }
1101 else /* HGCM call went wrong. */
1102 {
1103 rc = setError(E_UNEXPECTED,
1104 tr("The HGCM call failed with error %Rrc"), vrc);
1105 }
1106 }
1107
1108 for (unsigned i = 0; i < uNumArgs; i++)
1109 RTMemFree(papszArgv[i]);
1110 RTMemFree(papszArgv);
1111 }
1112 }
1113 catch (std::bad_alloc &)
1114 {
1115 rc = E_OUTOFMEMORY;
1116 }
1117 return rc;
1118#endif /* VBOX_WITH_GUEST_CONTROL */
1119}
1120
1121STDMETHODIMP Guest::GetProcessOutput(ULONG aPID, ULONG aFlags, ULONG aTimeoutMS, ULONG64 aSize, ComSafeArrayOut(BYTE, aData))
1122{
1123#ifndef VBOX_WITH_GUEST_CONTROL
1124 ReturnComNotImplemented();
1125#else /* VBOX_WITH_GUEST_CONTROL */
1126 using namespace guestControl;
1127
1128 CheckComArgExpr(aPID, aPID > 0);
1129
1130 if (aFlags != 0) /* Flags are not supported at the moment. */
1131 return E_INVALIDARG;
1132
1133 AutoCaller autoCaller(this);
1134 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1135
1136 HRESULT rc = S_OK;
1137
1138 try
1139 {
1140 /*
1141 * Create progress object.
1142 * Note that we need at least a local progress object here in order
1143 * to get notified when someone cancels the operation.
1144 */
1145 ComObjPtr <Progress> progress;
1146 rc = progress.createObject();
1147 if (SUCCEEDED(rc))
1148 {
1149 rc = progress->init(static_cast<IGuest*>(this),
1150 BstrFmt(tr("Getting output of process")),
1151 TRUE);
1152 }
1153 if (FAILED(rc)) return rc;
1154
1155 /* Adjust timeout */
1156 if (aTimeoutMS == 0)
1157 aTimeoutMS = UINT32_MAX;
1158
1159 /* Search for existing PID. */
1160 PCALLBACKDATAEXECOUT pData = (CALLBACKDATAEXECOUT*)RTMemAlloc(sizeof(CALLBACKDATAEXECOUT));
1161 AssertReturn(pData, VBOX_E_IPRT_ERROR);
1162 uint32_t uContextID = addCtrlCallbackContext(VBOXGUESTCTRLCALLBACKTYPE_EXEC_OUTPUT,
1163 pData, sizeof(CALLBACKDATAEXECOUT), progress);
1164 Assert(uContextID > 0);
1165
1166 size_t cbData = (size_t)RT_MIN(aSize, _64K);
1167 com::SafeArray<BYTE> outputData(cbData);
1168
1169 VBOXHGCMSVCPARM paParms[5];
1170 int i = 0;
1171 paParms[i++].setUInt32(uContextID);
1172 paParms[i++].setUInt32(aPID);
1173 paParms[i++].setUInt32(aFlags); /** @todo Should represent stdout and/or stderr. */
1174
1175 int vrc = VINF_SUCCESS;
1176
1177 {
1178 VMMDev *vmmDev;
1179 {
1180 /* Make sure mParent is valid, so set the read lock while using.
1181 * Do not keep this lock while doing the actual call, because in the meanwhile
1182 * another thread could request a write lock which would be a bad idea ... */
1183 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1184
1185 /* Forward the information to the VMM device. */
1186 AssertPtr(mParent);
1187 vmmDev = mParent->getVMMDev();
1188 }
1189
1190 if (vmmDev)
1191 {
1192 LogFlowFunc(("hgcmHostCall numParms=%d\n", i));
1193 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_GET_OUTPUT,
1194 i, paParms);
1195 }
1196 }
1197
1198 if (RT_SUCCESS(vrc))
1199 {
1200 LogFlowFunc(("Waiting for HGCM callback (timeout=%ldms) ...\n", aTimeoutMS));
1201
1202 /*
1203 * Wait for the HGCM low level callback until the process
1204 * has been started (or something went wrong). This is necessary to
1205 * get the PID.
1206 */
1207 CallbackListIter it = getCtrlCallbackContextByID(uContextID);
1208 BOOL fCanceled = FALSE;
1209 if (it != mCallbackList.end())
1210 {
1211 uint64_t u64Started = RTTimeMilliTS();
1212 while (!it->fCalled)
1213 {
1214 /* Check for timeout. */
1215 unsigned cMsWait;
1216 if (aTimeoutMS == RT_INDEFINITE_WAIT)
1217 cMsWait = 10;
1218 else
1219 {
1220 uint64_t cMsElapsed = RTTimeMilliTS() - u64Started;
1221 if (cMsElapsed >= aTimeoutMS)
1222 break; /* Timed out. */
1223 cMsWait = RT_MIN(10, aTimeoutMS - (uint32_t)cMsElapsed);
1224 }
1225
1226 /* Check for manual stop. */
1227 if (!it->pProgress.isNull())
1228 {
1229 rc = it->pProgress->COMGETTER(Canceled)(&fCanceled);
1230 if (FAILED(rc)) throw rc;
1231 if (fCanceled)
1232 break; /* Client wants to abort. */
1233 }
1234 RTThreadSleep(cMsWait);
1235 }
1236
1237 /* Was the whole thing canceled? */
1238 if (!fCanceled)
1239 {
1240 if (it->fCalled)
1241 {
1242 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1243
1244 /* Did we get some output? */
1245 pData = (PCALLBACKDATAEXECOUT)it->pvData;
1246 Assert(it->cbData == sizeof(CALLBACKDATAEXECOUT));
1247 AssertPtr(pData);
1248
1249 if (pData->cbData)
1250 {
1251 /* Do we need to resize the array? */
1252 if (pData->cbData > cbData)
1253 outputData.resize(pData->cbData);
1254
1255 /* Fill output in supplied out buffer. */
1256 memcpy(outputData.raw(), pData->pvData, pData->cbData);
1257 outputData.resize(pData->cbData); /* Shrink to fit actual buffer size. */
1258 }
1259 else
1260 vrc = VERR_NO_DATA; /* This is not an error we want to report to COM. */
1261 }
1262 else /* If callback not called within time ... well, that's a timeout! */
1263 vrc = VERR_TIMEOUT;
1264 }
1265 else /* Operation was canceled. */
1266 vrc = VERR_CANCELLED;
1267
1268 if (RT_FAILURE(vrc))
1269 {
1270 if (vrc == VERR_NO_DATA)
1271 {
1272 /* This is not an error we want to report to COM. */
1273 }
1274 else if (vrc == VERR_TIMEOUT)
1275 {
1276 rc = setError(VBOX_E_IPRT_ERROR,
1277 tr("The guest did not output within time (%ums)"), aTimeoutMS);
1278 }
1279 else if (vrc == VERR_CANCELLED)
1280 {
1281 rc = setError(VBOX_E_IPRT_ERROR,
1282 tr("The operation was canceled"));
1283 }
1284 else
1285 {
1286 rc = setError(E_UNEXPECTED,
1287 tr("The service call failed with error %Rrc"), vrc);
1288 }
1289 }
1290
1291 {
1292 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1293 destroyCtrlCallbackContext(it);
1294 }
1295 }
1296 else /* PID lookup failed. */
1297 rc = setError(VBOX_E_IPRT_ERROR,
1298 tr("Process (PID %u) not found!"), aPID);
1299 }
1300 else /* HGCM operation failed. */
1301 rc = setError(E_UNEXPECTED,
1302 tr("The HGCM call failed with error %Rrc"), vrc);
1303
1304 /* Cleanup. */
1305 progress->uninit();
1306 progress.setNull();
1307
1308 /* If something failed (or there simply was no data, indicated by VERR_NO_DATA,
1309 * we return an empty array so that the frontend knows when to give up. */
1310 if (RT_FAILURE(vrc) || FAILED(rc))
1311 outputData.resize(0);
1312 outputData.detachTo(ComSafeArrayOutArg(aData));
1313 }
1314 catch (std::bad_alloc &)
1315 {
1316 rc = E_OUTOFMEMORY;
1317 }
1318 return rc;
1319#endif
1320}
1321
1322STDMETHODIMP Guest::GetProcessStatus(ULONG aPID, ULONG *aExitCode, ULONG *aFlags, ULONG *aStatus)
1323{
1324#ifndef VBOX_WITH_GUEST_CONTROL
1325 ReturnComNotImplemented();
1326#else /* VBOX_WITH_GUEST_CONTROL */
1327 using namespace guestControl;
1328
1329 AutoCaller autoCaller(this);
1330 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1331
1332 HRESULT rc = S_OK;
1333
1334 try
1335 {
1336 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1337
1338 GuestProcessIterConst it;
1339 for (it = mGuestProcessList.begin(); it != mGuestProcessList.end(); it++)
1340 {
1341 if (it->mPID == aPID)
1342 break;
1343 }
1344
1345 if (it != mGuestProcessList.end())
1346 {
1347 *aExitCode = it->mExitCode;
1348 *aFlags = it->mFlags;
1349 *aStatus = it->mStatus;
1350 }
1351 else
1352 rc = setError(VBOX_E_IPRT_ERROR,
1353 tr("Process (PID %u) not found!"), aPID);
1354 }
1355 catch (std::bad_alloc &)
1356 {
1357 rc = E_OUTOFMEMORY;
1358 }
1359 return rc;
1360#endif
1361}
1362
1363// public methods only for internal purposes
1364/////////////////////////////////////////////////////////////////////////////
1365
1366void Guest::setAdditionsVersion(Bstr aVersion, VBOXOSTYPE aOsType)
1367{
1368 AutoCaller autoCaller(this);
1369 AssertComRCReturnVoid (autoCaller.rc());
1370
1371 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1372
1373 mData.mAdditionsVersion = aVersion;
1374 mData.mAdditionsActive = !aVersion.isEmpty();
1375 /* Older Additions didn't have this finer grained capability bit,
1376 * so enable it by default. Newer Additions will disable it immediately
1377 * if relevant. */
1378 mData.mSupportsGraphics = mData.mAdditionsActive;
1379
1380 mData.mOSTypeId = Global::OSTypeId (aOsType);
1381}
1382
1383void Guest::setSupportsSeamless (BOOL aSupportsSeamless)
1384{
1385 AutoCaller autoCaller(this);
1386 AssertComRCReturnVoid (autoCaller.rc());
1387
1388 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1389
1390 mData.mSupportsSeamless = aSupportsSeamless;
1391}
1392
1393void Guest::setSupportsGraphics (BOOL aSupportsGraphics)
1394{
1395 AutoCaller autoCaller(this);
1396 AssertComRCReturnVoid (autoCaller.rc());
1397
1398 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1399
1400 mData.mSupportsGraphics = aSupportsGraphics;
1401}
1402/* 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