VirtualBox

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

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

Guest Control/VBoxService+Main: Bugfix for multiple environment blocks.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.3 KB
Line 
1/* $Id: GuestImpl.cpp 28029 2010-04-07 07:31:23Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include "GuestImpl.h"
25
26#include "Global.h"
27#include "ConsoleImpl.h"
28#include "VMMDev.h"
29
30#include "AutoCaller.h"
31#include "Logging.h"
32
33#include <VBox/VMMDev.h>
34#ifdef VBOX_WITH_GUEST_CONTROL
35# include <VBox/HostServices/GuestControlSvc.h>
36# include <VBox/com/array.h>
37#endif
38#include <iprt/cpp/utils.h>
39#include <iprt/getopt.h>
40#include <VBox/pgm.h>
41
42// defines
43/////////////////////////////////////////////////////////////////////////////
44
45// constructor / destructor
46/////////////////////////////////////////////////////////////////////////////
47
48DEFINE_EMPTY_CTOR_DTOR (Guest)
49
50HRESULT Guest::FinalConstruct()
51{
52 return S_OK;
53}
54
55void Guest::FinalRelease()
56{
57 uninit ();
58}
59
60// public methods only for internal purposes
61/////////////////////////////////////////////////////////////////////////////
62
63/**
64 * Initializes the guest object.
65 */
66HRESULT Guest::init (Console *aParent)
67{
68 LogFlowThisFunc(("aParent=%p\n", aParent));
69
70 ComAssertRet(aParent, E_INVALIDARG);
71
72 /* Enclose the state transition NotReady->InInit->Ready */
73 AutoInitSpan autoInitSpan(this);
74 AssertReturn(autoInitSpan.isOk(), E_FAIL);
75
76 unconst(mParent) = aParent;
77
78 /* mData.mAdditionsActive is FALSE */
79
80 /* Confirm a successful initialization when it's the case */
81 autoInitSpan.setSucceeded();
82
83 ULONG aMemoryBalloonSize;
84 HRESULT ret = mParent->machine()->COMGETTER(MemoryBalloonSize)(&aMemoryBalloonSize);
85 if (ret == S_OK)
86 mMemoryBalloonSize = aMemoryBalloonSize;
87 else
88 mMemoryBalloonSize = 0; /* Default is no ballooning */
89
90 mStatUpdateInterval = 0; /* Default is not to report guest statistics at all */
91
92 /* Clear statistics. */
93 for (unsigned i = 0 ; i < GUESTSTATTYPE_MAX; i++)
94 mCurrentGuestStat[i] = 0;
95
96 return S_OK;
97}
98
99/**
100 * Uninitializes the instance and sets the ready flag to FALSE.
101 * Called either from FinalRelease() or by the parent when it gets destroyed.
102 */
103void Guest::uninit()
104{
105 LogFlowThisFunc(("\n"));
106
107 /* Enclose the state transition Ready->InUninit->NotReady */
108 AutoUninitSpan autoUninitSpan(this);
109 if (autoUninitSpan.uninitDone())
110 return;
111
112 unconst(mParent) = NULL;
113}
114
115// IGuest properties
116/////////////////////////////////////////////////////////////////////////////
117
118STDMETHODIMP Guest::COMGETTER(OSTypeId) (BSTR *aOSTypeId)
119{
120 CheckComArgOutPointerValid(aOSTypeId);
121
122 AutoCaller autoCaller(this);
123 if (FAILED(autoCaller.rc())) return autoCaller.rc();
124
125 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
126
127 // redirect the call to IMachine if no additions are installed
128 if (mData.mAdditionsVersion.isEmpty())
129 return mParent->machine()->COMGETTER(OSTypeId)(aOSTypeId);
130
131 mData.mOSTypeId.cloneTo(aOSTypeId);
132
133 return S_OK;
134}
135
136STDMETHODIMP Guest::COMGETTER(AdditionsActive) (BOOL *aAdditionsActive)
137{
138 CheckComArgOutPointerValid(aAdditionsActive);
139
140 AutoCaller autoCaller(this);
141 if (FAILED(autoCaller.rc())) return autoCaller.rc();
142
143 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
144
145 *aAdditionsActive = mData.mAdditionsActive;
146
147 return S_OK;
148}
149
150STDMETHODIMP Guest::COMGETTER(AdditionsVersion) (BSTR *aAdditionsVersion)
151{
152 CheckComArgOutPointerValid(aAdditionsVersion);
153
154 AutoCaller autoCaller(this);
155 if (FAILED(autoCaller.rc())) return autoCaller.rc();
156
157 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
158
159 mData.mAdditionsVersion.cloneTo(aAdditionsVersion);
160
161 return S_OK;
162}
163
164STDMETHODIMP Guest::COMGETTER(SupportsSeamless) (BOOL *aSupportsSeamless)
165{
166 CheckComArgOutPointerValid(aSupportsSeamless);
167
168 AutoCaller autoCaller(this);
169 if (FAILED(autoCaller.rc())) return autoCaller.rc();
170
171 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
172
173 *aSupportsSeamless = mData.mSupportsSeamless;
174
175 return S_OK;
176}
177
178STDMETHODIMP Guest::COMGETTER(SupportsGraphics) (BOOL *aSupportsGraphics)
179{
180 CheckComArgOutPointerValid(aSupportsGraphics);
181
182 AutoCaller autoCaller(this);
183 if (FAILED(autoCaller.rc())) return autoCaller.rc();
184
185 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
186
187 *aSupportsGraphics = mData.mSupportsGraphics;
188
189 return S_OK;
190}
191
192STDMETHODIMP Guest::COMGETTER(MemoryBalloonSize) (ULONG *aMemoryBalloonSize)
193{
194 CheckComArgOutPointerValid(aMemoryBalloonSize);
195
196 AutoCaller autoCaller(this);
197 if (FAILED(autoCaller.rc())) return autoCaller.rc();
198
199 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
200
201 *aMemoryBalloonSize = mMemoryBalloonSize;
202
203 return S_OK;
204}
205
206STDMETHODIMP Guest::COMSETTER(MemoryBalloonSize) (ULONG aMemoryBalloonSize)
207{
208 AutoCaller autoCaller(this);
209 if (FAILED(autoCaller.rc())) return autoCaller.rc();
210
211 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
212
213 HRESULT ret = mParent->machine()->COMSETTER(MemoryBalloonSize)(aMemoryBalloonSize);
214 if (ret == S_OK)
215 {
216 mMemoryBalloonSize = aMemoryBalloonSize;
217 /* forward the information to the VMM device */
218 VMMDev *vmmDev = mParent->getVMMDev();
219 if (vmmDev)
220 vmmDev->getVMMDevPort()->pfnSetMemoryBalloon(vmmDev->getVMMDevPort(), aMemoryBalloonSize);
221 }
222
223 return ret;
224}
225
226STDMETHODIMP Guest::COMGETTER(StatisticsUpdateInterval)(ULONG *aUpdateInterval)
227{
228 CheckComArgOutPointerValid(aUpdateInterval);
229
230 AutoCaller autoCaller(this);
231 if (FAILED(autoCaller.rc())) return autoCaller.rc();
232
233 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
234
235 *aUpdateInterval = mStatUpdateInterval;
236 return S_OK;
237}
238
239STDMETHODIMP Guest::COMSETTER(StatisticsUpdateInterval)(ULONG aUpdateInterval)
240{
241 AutoCaller autoCaller(this);
242 if (FAILED(autoCaller.rc())) return autoCaller.rc();
243
244 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
245
246 mStatUpdateInterval = aUpdateInterval;
247 /* forward the information to the VMM device */
248 VMMDev *vmmDev = mParent->getVMMDev();
249 if (vmmDev)
250 vmmDev->getVMMDevPort()->pfnSetStatisticsInterval(vmmDev->getVMMDevPort(), aUpdateInterval);
251
252 return S_OK;
253}
254
255STDMETHODIMP Guest::InternalGetStatistics(ULONG *aCpuUser, ULONG *aCpuKernel, ULONG *aCpuIdle,
256 ULONG *aMemTotal, ULONG *aMemFree, ULONG *aMemBalloon,
257 ULONG *aMemCache, ULONG *aPageTotal, ULONG *aMemFreeTotal)
258{
259 CheckComArgOutPointerValid(aCpuUser);
260 CheckComArgOutPointerValid(aCpuKernel);
261 CheckComArgOutPointerValid(aCpuIdle);
262 CheckComArgOutPointerValid(aMemTotal);
263 CheckComArgOutPointerValid(aMemFree);
264 CheckComArgOutPointerValid(aMemBalloon);
265 CheckComArgOutPointerValid(aMemCache);
266 CheckComArgOutPointerValid(aPageTotal);
267
268 AutoCaller autoCaller(this);
269 if (FAILED(autoCaller.rc())) return autoCaller.rc();
270
271 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
272
273 *aCpuUser = mCurrentGuestStat[GUESTSTATTYPE_CPUUSER];
274 *aCpuKernel = mCurrentGuestStat[GUESTSTATTYPE_CPUKERNEL];
275 *aCpuIdle = mCurrentGuestStat[GUESTSTATTYPE_CPUIDLE];
276 *aMemTotal = mCurrentGuestStat[GUESTSTATTYPE_MEMTOTAL];
277 *aMemFree = mCurrentGuestStat[GUESTSTATTYPE_MEMFREE];
278 *aMemBalloon = mCurrentGuestStat[GUESTSTATTYPE_MEMBALLOON];
279 *aMemCache = mCurrentGuestStat[GUESTSTATTYPE_MEMCACHE];
280 *aPageTotal = mCurrentGuestStat[GUESTSTATTYPE_PAGETOTAL];
281
282 Console::SafeVMPtr pVM (mParent);
283 if (pVM.isOk())
284 {
285 unsigned uFreeTotal;
286 *aMemFreeTotal = 0;
287 int rc = PGMR3QueryFreeMemory(pVM.raw(), &uFreeTotal);
288 AssertRC(rc);
289 if (rc == VINF_SUCCESS)
290 *aMemFreeTotal = uFreeTotal;
291 }
292 else
293 *aMemFreeTotal = 0;
294
295 return S_OK;
296}
297
298HRESULT Guest::SetStatistic(ULONG aCpuId, GUESTSTATTYPE enmType, ULONG aVal)
299{
300 AutoCaller autoCaller(this);
301 if (FAILED(autoCaller.rc())) return autoCaller.rc();
302
303 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
304
305 if (enmType >= GUESTSTATTYPE_MAX)
306 return E_INVALIDARG;
307
308 mCurrentGuestStat[enmType] = aVal;
309 return S_OK;
310}
311
312STDMETHODIMP Guest::SetCredentials(IN_BSTR aUserName, IN_BSTR aPassword,
313 IN_BSTR aDomain, BOOL aAllowInteractiveLogon)
314{
315 AutoCaller autoCaller(this);
316 if (FAILED(autoCaller.rc())) return autoCaller.rc();
317
318 /* forward the information to the VMM device */
319 VMMDev *vmmDev = mParent->getVMMDev();
320 if (vmmDev)
321 {
322 uint32_t u32Flags = VMMDEV_SETCREDENTIALS_GUESTLOGON;
323 if (!aAllowInteractiveLogon)
324 u32Flags = VMMDEV_SETCREDENTIALS_NOLOCALLOGON;
325
326 vmmDev->getVMMDevPort()->pfnSetCredentials(vmmDev->getVMMDevPort(),
327 Utf8Str(aUserName).raw(), Utf8Str(aPassword).raw(),
328 Utf8Str(aDomain).raw(), u32Flags);
329 return S_OK;
330 }
331
332 return setError(VBOX_E_VM_ERROR,
333 tr("VMM device is not available (is the VM running?)"));
334}
335
336#ifdef VBOX_WITH_GUEST_CONTROL
337/**
338 * Creates the argument list as an array used for executing a program.
339 *
340 * @returns VBox status code.
341 *
342 * @todo
343 *
344 * @todo Respect spaces when quoting for arguments, e.g. "c:\\program files\\".
345 * @todo Handle empty ("") argguments.
346 */
347int Guest::prepareExecuteArgs(const char *pszArgs, void **ppvList, uint32_t *pcbList, uint32_t *pcArgs)
348{
349 char **ppaArg;
350 int iArgs;
351 int rc = RTGetOptArgvFromString(&ppaArg, &iArgs, pszArgs, NULL);
352 if (RT_SUCCESS(rc))
353 {
354 char *pszTemp = NULL;
355 *pcbList = 0;
356 for (int i=0; i<iArgs; i++)
357 {
358 if (i > 0) /* Insert space as delimiter. */
359 rc = RTStrAAppendN(&pszTemp, " ", 1);
360
361 if (RT_FAILURE(rc))
362 break;
363 else
364 {
365 rc = RTStrAAppendN(&pszTemp, ppaArg[i], strlen(ppaArg[i]));
366 if (RT_FAILURE(rc))
367 break;
368 }
369 }
370 RTGetOptArgvFree(ppaArg);
371 if (RT_SUCCESS(rc))
372 {
373 *ppvList = pszTemp;
374 *pcArgs = iArgs;
375 if (pszTemp)
376 *pcbList = strlen(pszTemp) + 1; /* Include zero termination. */
377 }
378 else
379 RTStrFree(pszTemp);
380 }
381 return rc;
382}
383
384/**
385 * Appends environment variables to the environment block. Each var=value pair is separated
386 * by NULL (\0) sequence. The whole block will be stored in one blob and disassembled on the
387 * guest side later to fit into the HGCM param structure.
388 *
389 * @returns VBox status code.
390 *
391 * @todo
392 *
393 */
394int Guest::prepareExecuteEnv(const char *pszEnv, void **ppvList, uint32_t *pcbList, uint32_t *pcEnv)
395{
396 int rc = VINF_SUCCESS;
397 uint32_t cbLen = strlen(pszEnv);
398 if (*ppvList)
399 {
400 uint32_t cbNewLen = *pcbList + cbLen + 1; /* Include zero termination. */
401 char *pvTmp = (char*)RTMemRealloc(*ppvList, cbNewLen);
402 if (NULL == pvTmp)
403 {
404 rc = VERR_NO_MEMORY;
405 }
406 else
407 {
408 memcpy(pvTmp + *pcbList, pszEnv, cbLen);
409 pvTmp[cbNewLen - 1] = '\0'; /* Add zero termination. */
410 *ppvList = (void**)pvTmp;
411 }
412 }
413 else
414 {
415 char *pcTmp;
416 if (RTStrAPrintf(&pcTmp, "%s", pszEnv) > 0)
417 {
418 *ppvList = (void**)pcTmp;
419 /* Reset counters. */
420 *pcEnv = 0;
421 *pcbList = 0;
422 }
423 }
424 if (RT_SUCCESS(rc))
425 {
426 *pcbList += cbLen + 1; /* Include zero termination. */
427 *pcEnv += 1; /* Increase env pairs count. */
428 }
429 return rc;
430}
431#endif /* VBOX_WITH_GUEST_CONTROL */
432
433STDMETHODIMP Guest::ExecuteProgram(IN_BSTR aCommand, ULONG aFlags,
434 ComSafeArrayIn(IN_BSTR, aArguments), ComSafeArrayIn(IN_BSTR, aEnvironment),
435 IN_BSTR aStdIn, IN_BSTR aStdOut, IN_BSTR aStdErr,
436 IN_BSTR aUserName, IN_BSTR aPassword,
437 ULONG aTimeoutMS, ULONG *aPID, IProgress **aProgress)
438{
439#ifndef VBOX_WITH_GUEST_CONTROL
440 ReturnComNotImplemented();
441#else /* VBOX_WITH_GUEST_CONTROL */
442 using namespace guestControl;
443
444 CheckComArgStrNotEmptyOrNull(aCommand);
445 CheckComArgOutPointerValid(aProgress);
446 CheckComArgOutPointerValid(aPID);
447 /* Flags are not supported at the moment. */
448 if (aFlags != 0)
449 return E_INVALIDARG;
450
451 HRESULT rc = S_OK;
452
453 try
454 {
455 AutoCaller autoCaller(this);
456 if (FAILED(autoCaller.rc())) return autoCaller.rc();
457
458 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
459
460 /* Just be on the safe side when calling another process. */
461 alock.leave();
462
463 HRESULT rc = E_UNEXPECTED;
464 using namespace guestControl;
465
466 int vrc = VINF_SUCCESS;
467 Utf8Str Utf8Command(aCommand);
468
469 /* Prepare arguments. */
470 com::SafeArray<IN_BSTR> args(ComSafeArrayInArg(aArguments));
471 uint32_t uNumArgs = args.size();
472 char **papszArgv = NULL;
473 if(uNumArgs > 0)
474 {
475 papszArgv = (char**)RTMemAlloc(sizeof(char*) * (uNumArgs + 1));
476 AssertPtr(papszArgv);
477 for (unsigned i = 0; RT_SUCCESS(vrc) && i < uNumArgs; i++)
478 vrc = RTStrAPrintf(&papszArgv[i], "%s", Utf8Str(args[i]).raw());
479 papszArgv[uNumArgs] = NULL;
480 }
481
482 if (RT_SUCCESS(vrc))
483 {
484 char *pszArgs = NULL;
485 if (uNumArgs > 0)
486 vrc = RTGetOptArgvToString(&pszArgs, papszArgv, 0);
487 if (RT_SUCCESS(vrc))
488 {
489 uint32_t cbArgs = pszArgs ? strlen(pszArgs) + 1 : 0; /* Include terminating zero. */
490
491 /* Prepare environment. */
492 com::SafeArray<IN_BSTR> env(ComSafeArrayInArg(aEnvironment));
493
494 void *pvEnv = NULL;
495 uint32_t uNumEnv = 0;
496 uint32_t cbEnv = 0;
497
498 for (unsigned i = 0; i < env.size(); i++)
499 {
500 vrc = prepareExecuteEnv(Utf8Str(env[i]).raw(), &pvEnv, &cbEnv, &uNumEnv);
501 if (RT_FAILURE(vrc))
502 break;
503 }
504
505 if (RT_SUCCESS(vrc))
506 {
507 Utf8Str Utf8StdIn(aStdIn);
508 Utf8Str Utf8StdOut(aStdOut);
509 Utf8Str Utf8StdErr(aStdErr);
510 Utf8Str Utf8UserName(aUserName);
511 Utf8Str Utf8Password(aPassword);
512
513 VBOXHGCMSVCPARM paParms[14];
514 int i = 0;
515 paParms[i++].setPointer((void*)Utf8Command.raw(), (uint32_t)strlen(Utf8Command.raw()) + 1);
516 paParms[i++].setUInt32(aFlags);
517 paParms[i++].setUInt32(uNumArgs);
518 paParms[i++].setPointer((void*)pszArgs, cbArgs);
519 paParms[i++].setUInt32(uNumEnv);
520 paParms[i++].setUInt32(cbEnv);
521 paParms[i++].setPointer((void*)pvEnv, cbEnv);
522 paParms[i++].setPointer((void*)Utf8StdIn.raw(), (uint32_t)strlen(Utf8StdIn.raw()) + 1);
523 paParms[i++].setPointer((void*)Utf8StdOut.raw(), (uint32_t)strlen(Utf8StdOut.raw()) + 1);
524 paParms[i++].setPointer((void*)Utf8StdErr.raw(), (uint32_t)strlen(Utf8StdErr.raw()) + 1);
525 paParms[i++].setPointer((void*)Utf8UserName.raw(), (uint32_t)strlen(Utf8UserName.raw()) + 1);
526 paParms[i++].setPointer((void*)Utf8Password.raw(), (uint32_t)strlen(Utf8Password.raw()) + 1);
527 paParms[i++].setUInt32(aTimeoutMS);
528
529 /* Forward the information to the VMM device. */
530 AssertPtr(mParent);
531 VMMDev *vmmDev = mParent->getVMMDev();
532 if (vmmDev)
533 {
534 LogFlow(("Guest::ExecuteProgram: numParms=%d\n", i));
535 vrc = vmmDev->hgcmHostCall("VBoxGuestControlSvc", HOST_EXEC_CMD,
536 i, paParms);
537 /** @todo Get the PID. */
538 }
539 RTMemFree(pvEnv);
540 }
541 RTStrFree(pszArgs);
542 }
543 if (RT_SUCCESS(vrc))
544 rc = S_OK;
545 else
546 rc = setError(E_UNEXPECTED,
547 tr("The service call failed with the error %Rrc"),
548 vrc);
549
550 for (unsigned i = 0; i < uNumArgs; i++)
551 RTMemFree(papszArgv[i]);
552 RTMemFree(papszArgv);
553 }
554 }
555 catch (std::bad_alloc &)
556 {
557 rc = E_OUTOFMEMORY;
558 };
559
560 return rc;
561#endif /* VBOX_WITH_GUEST_CONTROL */
562}
563
564// public methods only for internal purposes
565/////////////////////////////////////////////////////////////////////////////
566
567void Guest::setAdditionsVersion(Bstr aVersion, VBOXOSTYPE aOsType)
568{
569 AutoCaller autoCaller(this);
570 AssertComRCReturnVoid (autoCaller.rc());
571
572 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
573
574 mData.mAdditionsVersion = aVersion;
575 mData.mAdditionsActive = !aVersion.isEmpty();
576 /* Older Additions didn't have this finer grained capability bit,
577 * so enable it by default. Newer Additions will disable it immediately
578 * if relevant. */
579 mData.mSupportsGraphics = mData.mAdditionsActive;
580
581 mData.mOSTypeId = Global::OSTypeId (aOsType);
582}
583
584void Guest::setSupportsSeamless (BOOL aSupportsSeamless)
585{
586 AutoCaller autoCaller(this);
587 AssertComRCReturnVoid (autoCaller.rc());
588
589 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
590
591 mData.mSupportsSeamless = aSupportsSeamless;
592}
593
594void Guest::setSupportsGraphics (BOOL aSupportsGraphics)
595{
596 AutoCaller autoCaller(this);
597 AssertComRCReturnVoid (autoCaller.rc());
598
599 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
600
601 mData.mSupportsGraphics = aSupportsGraphics;
602}
603/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette