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