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