VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostImpl.cpp@ 55200

Last change on this file since 55200 was 53815, checked in by vboxsync, 10 years ago

Rename include/VBox/VBoxOGLTest.h to more common name (VBoxOGL.h) since it is planned to use it in order to share functionality between 3D and GUI.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 114.4 KB
Line 
1/* $Id: HostImpl.cpp 53815 2015-01-15 11:15:07Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation: Host
4 */
5
6/*
7 * Copyright (C) 2004-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#define __STDC_LIMIT_MACROS
19#define __STDC_CONSTANT_MACROS
20
21// for some reason Windows burns in sdk\...\winsock.h if this isn't included first
22#include "VBox/com/ptr.h"
23
24#include "HostImpl.h"
25
26#ifdef VBOX_WITH_USB
27# include "HostUSBDeviceImpl.h"
28# include "USBDeviceFilterImpl.h"
29# include "USBProxyService.h"
30#endif // VBOX_WITH_USB
31
32#include "HostNetworkInterfaceImpl.h"
33#include "HostVideoInputDeviceImpl.h"
34#include "MachineImpl.h"
35#include "AutoCaller.h"
36#include "Logging.h"
37#include "Performance.h"
38
39#include "MediumImpl.h"
40#include "HostPower.h"
41
42#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
43# include <HostHardwareLinux.h>
44#endif
45
46#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
47# include <set>
48#endif
49
50#ifdef VBOX_WITH_RESOURCE_USAGE_API
51# include "PerformanceImpl.h"
52#endif /* VBOX_WITH_RESOURCE_USAGE_API */
53
54#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
55# include <VBox/VBoxNetCfg-win.h>
56#endif /* #if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) */
57
58#if defined(RT_OS_DARWIN) && ARCH_BITS == 32
59# include <sys/types.h>
60# include <sys/sysctl.h>
61#endif
62
63#ifdef RT_OS_LINUX
64# include <sys/ioctl.h>
65# include <errno.h>
66# include <net/if.h>
67# include <net/if_arp.h>
68#endif /* RT_OS_LINUX */
69
70#ifdef RT_OS_SOLARIS
71# include <fcntl.h>
72# include <unistd.h>
73# include <stropts.h>
74# include <errno.h>
75# include <limits.h>
76# include <stdio.h>
77# include <libdevinfo.h>
78# include <sys/mkdev.h>
79# include <sys/scsi/generic/inquiry.h>
80# include <net/if.h>
81# include <sys/socket.h>
82# include <sys/sockio.h>
83# include <net/if_arp.h>
84# include <net/if.h>
85# include <sys/types.h>
86# include <sys/stat.h>
87# include <sys/cdio.h>
88# include <sys/dkio.h>
89# include <sys/mnttab.h>
90# include <sys/mntent.h>
91/* Dynamic loading of libhal on Solaris hosts */
92# ifdef VBOX_USE_LIBHAL
93# include "vbox-libhal.h"
94extern "C" char *getfullrawname(char *);
95# endif
96# include "solaris/DynLoadLibSolaris.h"
97
98/**
99 * Solaris DVD drive list as returned by getDVDInfoFromDevTree().
100 */
101typedef struct SOLARISDVD
102{
103 struct SOLARISDVD *pNext;
104 char szDescription[512];
105 char szRawDiskPath[PATH_MAX];
106} SOLARISDVD;
107/** Pointer to a Solaris DVD descriptor. */
108typedef SOLARISDVD *PSOLARISDVD;
109
110
111
112#endif /* RT_OS_SOLARIS */
113
114#ifdef RT_OS_WINDOWS
115# define _WIN32_DCOM
116# include <windows.h>
117# include <shellapi.h>
118# define INITGUID
119# include <guiddef.h>
120# include <devguid.h>
121# include <objbase.h>
122//# include <setupapi.h>
123# include <shlobj.h>
124# include <cfgmgr32.h>
125
126#endif /* RT_OS_WINDOWS */
127
128#ifdef RT_OS_DARWIN
129# include "darwin/iokit.h"
130#endif
131
132#ifdef VBOX_WITH_CROGL
133#include <VBox/VBoxOGL.h>
134#endif /* VBOX_WITH_CROGL */
135
136#include <iprt/asm-amd64-x86.h>
137#include <iprt/string.h>
138#include <iprt/mp.h>
139#include <iprt/time.h>
140#include <iprt/param.h>
141#include <iprt/env.h>
142#include <iprt/mem.h>
143#include <iprt/system.h>
144#ifndef RT_OS_WINDOWS
145# include <iprt/path.h>
146#endif
147#ifdef RT_OS_SOLARIS
148# include <iprt/ctype.h>
149#endif
150#ifdef VBOX_WITH_HOSTNETIF_API
151# include "netif.h"
152#endif
153
154/* XXX Solaris: definitions in /usr/include/sys/regset.h clash with hm_svm.h */
155#undef DS
156#undef ES
157#undef CS
158#undef SS
159#undef FS
160#undef GS
161
162#include <VBox/usb.h>
163#include <VBox/vmm/hm_svm.h>
164#include <VBox/err.h>
165#include <VBox/settings.h>
166#include <VBox/sup.h>
167#include <iprt/x86.h>
168
169#include "VBox/com/MultiResult.h"
170#include "VBox/com/array.h"
171
172#include <stdio.h>
173
174#include <algorithm>
175#include <string>
176#include <vector>
177
178#include "HostDnsService.h"
179
180////////////////////////////////////////////////////////////////////////////////
181//
182// Host private data definition
183//
184////////////////////////////////////////////////////////////////////////////////
185
186struct Host::Data
187{
188 Data()
189 :
190 fDVDDrivesListBuilt(false),
191 fFloppyDrivesListBuilt(false)
192 {};
193
194 VirtualBox *pParent;
195
196 HostNetworkInterfaceList llNetIfs; // list of network interfaces
197
198#ifdef VBOX_WITH_USB
199 USBDeviceFilterList llChildren; // all USB device filters
200 USBDeviceFilterList llUSBDeviceFilters; // USB device filters in use by the USB proxy service
201
202 /** Pointer to the USBProxyService object. */
203 USBProxyService *pUSBProxyService;
204#endif /* VBOX_WITH_USB */
205
206 // list of host drives; lazily created by getDVDDrives() and getFloppyDrives(),
207 // and protected by the medium tree lock handle (including the bools).
208 MediaList llDVDDrives,
209 llFloppyDrives;
210 bool fDVDDrivesListBuilt,
211 fFloppyDrivesListBuilt;
212
213#if defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
214 /** Object with information about host drives */
215 VBoxMainDriveInfo hostDrives;
216#endif
217
218 /** @name Features that can be queried with GetProcessorFeature.
219 * @{ */
220 bool fVTSupported,
221 fLongModeSupported,
222 fPAESupported,
223 fNestedPagingSupported,
224 fRecheckVTSupported;
225
226 /** @} */
227
228 /** 3D hardware acceleration supported? Tristate, -1 meaning not probed. */
229 int f3DAccelerationSupported;
230
231 HostPowerService *pHostPowerService;
232 /** Host's DNS informaton fetching */
233 HostDnsMonitorProxy hostDnsMonitorProxy;
234};
235
236
237////////////////////////////////////////////////////////////////////////////////
238//
239// Constructor / destructor
240//
241////////////////////////////////////////////////////////////////////////////////
242DEFINE_EMPTY_CTOR_DTOR(Host)
243
244HRESULT Host::FinalConstruct()
245{
246 return BaseFinalConstruct();
247}
248
249void Host::FinalRelease()
250{
251 uninit();
252 BaseFinalRelease();
253}
254
255/**
256 * Initializes the host object.
257 *
258 * @param aParent VirtualBox parent object.
259 */
260HRESULT Host::init(VirtualBox *aParent)
261{
262 HRESULT hrc;
263 LogFlowThisFunc(("aParent=%p\n", aParent));
264
265 /* Enclose the state transition NotReady->InInit->Ready */
266 AutoInitSpan autoInitSpan(this);
267 AssertReturn(autoInitSpan.isOk(), E_FAIL);
268
269 m = new Data();
270
271 m->pParent = aParent;
272
273#ifdef VBOX_WITH_USB
274 /*
275 * Create and initialize the USB Proxy Service.
276 */
277# if defined(RT_OS_DARWIN)
278 m->pUSBProxyService = new USBProxyServiceDarwin(this);
279# elif defined(RT_OS_LINUX)
280 m->pUSBProxyService = new USBProxyServiceLinux(this);
281# elif defined(RT_OS_OS2)
282 m->pUSBProxyService = new USBProxyServiceOs2(this);
283# elif defined(RT_OS_SOLARIS)
284 m->pUSBProxyService = new USBProxyServiceSolaris(this);
285# elif defined(RT_OS_WINDOWS)
286 m->pUSBProxyService = new USBProxyServiceWindows(this);
287# elif defined(RT_OS_FREEBSD)
288 m->pUSBProxyService = new USBProxyServiceFreeBSD(this);
289# else
290 m->pUSBProxyService = new USBProxyService(this);
291# endif
292 hrc = m->pUSBProxyService->init();
293 AssertComRCReturn(hrc, hrc);
294#endif /* VBOX_WITH_USB */
295
296#ifdef VBOX_WITH_RESOURCE_USAGE_API
297 i_registerMetrics(aParent->i_performanceCollector());
298#endif /* VBOX_WITH_RESOURCE_USAGE_API */
299 /* Create the list of network interfaces so their metrics get registered. */
300 i_updateNetIfList();
301
302 m->hostDnsMonitorProxy.init(HostDnsMonitor::getHostDnsMonitor(), m->pParent);
303
304#if defined(RT_OS_WINDOWS)
305 m->pHostPowerService = new HostPowerServiceWin(m->pParent);
306#elif defined(RT_OS_DARWIN)
307 m->pHostPowerService = new HostPowerServiceDarwin(m->pParent);
308#else
309 m->pHostPowerService = new HostPowerService(m->pParent);
310#endif
311
312 /* Cache the features reported by GetProcessorFeature. */
313 m->fVTSupported = false;
314 m->fLongModeSupported = false;
315 m->fPAESupported = false;
316 m->fNestedPagingSupported = false;
317 m->fRecheckVTSupported = false;
318
319 if (ASMHasCpuId())
320 {
321 /* Note! This code is duplicated in SUPDrv.c and other places! */
322 uint32_t uMaxId, uVendorEBX, uVendorECX, uVendorEDX;
323 ASMCpuId(0, &uMaxId, &uVendorEBX, &uVendorECX, &uVendorEDX);
324 if (ASMIsValidStdRange(uMaxId))
325 {
326 /* PAE? */
327 uint32_t uDummy, fFeaturesEcx, fFeaturesEdx;
328 ASMCpuId(1, &uDummy, &uDummy, &fFeaturesEcx, &fFeaturesEdx);
329 m->fPAESupported = RT_BOOL(fFeaturesEdx & X86_CPUID_FEATURE_EDX_PAE);
330
331 /* Long Mode? */
332 uint32_t uExtMaxId, fExtFeaturesEcx, fExtFeaturesEdx;
333 ASMCpuId(0x80000000, &uExtMaxId, &uDummy, &uDummy, &uDummy);
334 ASMCpuId(0x80000001, &uDummy, &uDummy, &fExtFeaturesEcx, &fExtFeaturesEdx);
335 m->fLongModeSupported = ASMIsValidExtRange(uExtMaxId)
336 && (fExtFeaturesEdx & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE);
337
338#if defined(RT_OS_DARWIN) && ARCH_BITS == 32 /* darwin.x86 has some optimizations of 64-bit on 32-bit. */
339 int f64bitCapable = 0;
340 size_t cbParameter = sizeof(f64bitCapable);
341 if (sysctlbyname("hw.cpu64bit_capable", &f64bitCapable, &cbParameter, NULL, NULL) != -1)
342 m->fLongModeSupported = f64bitCapable != 0;
343#endif
344
345 /* VT-x? */
346 if ( ASMIsIntelCpuEx(uVendorEBX, uVendorECX, uVendorEDX)
347 || ASMIsViaCentaurCpuEx(uVendorEBX, uVendorECX, uVendorEDX))
348 {
349 if ( (fFeaturesEcx & X86_CPUID_FEATURE_ECX_VMX)
350 && (fFeaturesEdx & X86_CPUID_FEATURE_EDX_MSR)
351 && (fFeaturesEdx & X86_CPUID_FEATURE_EDX_FXSR)
352 )
353 {
354 int rc = SUPR3QueryVTxSupported();
355 if (RT_SUCCESS(rc))
356 m->fVTSupported = true;
357 }
358 }
359 /* AMD-V */
360 else if (ASMIsAmdCpuEx(uVendorEBX, uVendorECX, uVendorEDX))
361 {
362 if ( (fExtFeaturesEcx & X86_CPUID_AMD_FEATURE_ECX_SVM)
363 && (fFeaturesEdx & X86_CPUID_FEATURE_EDX_MSR)
364 && (fFeaturesEdx & X86_CPUID_FEATURE_EDX_FXSR)
365 && ASMIsValidExtRange(uExtMaxId)
366 )
367 {
368 m->fVTSupported = true;
369
370 /* Query AMD features. */
371 if (uExtMaxId >= 0x8000000a)
372 {
373 uint32_t fSVMFeaturesEdx;
374 ASMCpuId(0x8000000a, &uDummy, &uDummy, &uDummy, &fSVMFeaturesEdx);
375 if (fSVMFeaturesEdx & AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING)
376 m->fNestedPagingSupported = true;
377 }
378 }
379 }
380 }
381 }
382
383 /* Check with SUPDrv if VT-x and AMD-V are really supported (may fail). */
384 if (m->fVTSupported)
385 {
386 int rc = SUPR3InitEx(false /*fUnrestricted*/, NULL);
387 if (RT_SUCCESS(rc))
388 {
389 uint32_t fVTCaps;
390 rc = SUPR3QueryVTCaps(&fVTCaps);
391 if (RT_SUCCESS(rc))
392 {
393 Assert(fVTCaps & (SUPVTCAPS_AMD_V | SUPVTCAPS_VT_X));
394 if (fVTCaps & SUPVTCAPS_NESTED_PAGING)
395 m->fNestedPagingSupported = true;
396 else
397 Assert(m->fNestedPagingSupported == false);
398 }
399 else
400 {
401 LogRel(("SUPR0QueryVTCaps -> %Rrc\n", rc));
402 m->fVTSupported = m->fNestedPagingSupported = false;
403 }
404 }
405 else
406 m->fRecheckVTSupported = true; /* Try again later when the driver is loaded. */
407 }
408
409#ifdef VBOX_WITH_CROGL
410 /* Test for 3D hardware acceleration support later when (if ever) need. */
411 m->f3DAccelerationSupported = -1;
412#else
413 m->f3DAccelerationSupported = false;
414#endif
415
416#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
417 /* Extract the list of configured host-only interfaces */
418 std::set<Utf8Str> aConfiguredNames;
419 SafeArray<BSTR> aGlobalExtraDataKeys;
420 hrc = aParent->GetExtraDataKeys(ComSafeArrayAsOutParam(aGlobalExtraDataKeys));
421 AssertMsg(SUCCEEDED(hrc), ("VirtualBox::GetExtraDataKeys failed with %Rhrc\n", hrc));
422 for (size_t i = 0; i < aGlobalExtraDataKeys.size(); ++i)
423 {
424 Utf8Str strKey = aGlobalExtraDataKeys[i];
425
426 if (!strKey.startsWith("HostOnly/vboxnet"))
427 continue;
428
429 size_t pos = strKey.find("/", sizeof("HostOnly/vboxnet"));
430 if (pos != Utf8Str::npos)
431 aConfiguredNames.insert(strKey.substr(sizeof("HostOnly"),
432 pos - sizeof("HostOnly")));
433 }
434
435 for (std::set<Utf8Str>::const_iterator it = aConfiguredNames.begin();
436 it != aConfiguredNames.end();
437 ++it)
438 {
439 ComPtr<IHostNetworkInterface> hif;
440 ComPtr<IProgress> progress;
441
442 int r = NetIfCreateHostOnlyNetworkInterface(m->pParent,
443 hif.asOutParam(),
444 progress.asOutParam(),
445 it->c_str());
446 if (RT_FAILURE(r))
447 LogRel(("failed to create %s, error (0x%x)\n", it->c_str(), r));
448 }
449
450#endif /* defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD) */
451
452 /* Confirm a successful initialization */
453 autoInitSpan.setSucceeded();
454
455 return S_OK;
456}
457
458/**
459 * Uninitializes the host object and sets the ready flag to FALSE.
460 * Called either from FinalRelease() or by the parent when it gets destroyed.
461 */
462void Host::uninit()
463{
464 LogFlowThisFunc(("\n"));
465
466 /* Enclose the state transition Ready->InUninit->NotReady */
467 AutoUninitSpan autoUninitSpan(this);
468 if (autoUninitSpan.uninitDone())
469 return;
470
471#ifdef VBOX_WITH_RESOURCE_USAGE_API
472 PerformanceCollector *aCollector = m->pParent->i_performanceCollector();
473 i_unregisterMetrics(aCollector);
474#endif /* VBOX_WITH_RESOURCE_USAGE_API */
475 /*
476 * Note that unregisterMetrics() has unregistered all metrics associated
477 * with Host including network interface ones. We can destroy network
478 * interface objects now.
479 */
480 m->llNetIfs.clear();
481
482#ifdef VBOX_WITH_USB
483 /* wait for USB proxy service to terminate before we uninit all USB
484 * devices */
485 LogFlowThisFunc(("Stopping USB proxy service...\n"));
486 delete m->pUSBProxyService;
487 m->pUSBProxyService = NULL;
488 LogFlowThisFunc(("Done stopping USB proxy service.\n"));
489#endif
490
491 delete m->pHostPowerService;
492
493#ifdef VBOX_WITH_USB
494 /* uninit all USB device filters still referenced by clients
495 * Note! HostUSBDeviceFilter::uninit() will modify llChildren. */
496 while (!m->llChildren.empty())
497 {
498 ComObjPtr<HostUSBDeviceFilter> &pChild = m->llChildren.front();
499 m->llChildren.pop_front();
500 pChild->uninit();
501 }
502
503 m->llUSBDeviceFilters.clear();
504#endif
505
506 delete m;
507 m = NULL;
508}
509
510////////////////////////////////////////////////////////////////////////////////
511//
512// IHost public methods
513//
514////////////////////////////////////////////////////////////////////////////////
515
516/**
517 * Returns a list of host DVD drives.
518 *
519 * @returns COM status code
520 * @param drives address of result pointer
521 */
522
523HRESULT Host::getDVDDrives(std::vector<ComPtr<IMedium> > &aDVDDrives)
524{
525 AutoWriteLock treeLock(m->pParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
526
527 MediaList *pList;
528 HRESULT rc = i_getDrives(DeviceType_DVD, true /* fRefresh */, pList, treeLock);
529 if (FAILED(rc))
530 return rc;
531
532 aDVDDrives.resize(pList->size());
533 size_t i = 0;
534 for (MediaList::const_iterator it = pList->begin(); it != pList->end(); ++it, ++i)
535 (*it).queryInterfaceTo(aDVDDrives[i].asOutParam());
536
537 return S_OK;
538}
539
540/**
541 * Returns a list of host floppy drives.
542 *
543 * @returns COM status code
544 * @param drives address of result pointer
545 */
546HRESULT Host::getFloppyDrives(std::vector<ComPtr<IMedium> > &aFloppyDrives)
547{
548 AutoWriteLock treeLock(m->pParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
549
550 MediaList *pList;
551 HRESULT rc = i_getDrives(DeviceType_Floppy, true /* fRefresh */, pList, treeLock);
552 if (FAILED(rc))
553 return rc;
554
555 aFloppyDrives.resize(pList->size());
556 size_t i = 0;
557 for (MediaList::const_iterator it = pList->begin(); it != pList->end(); ++it, ++i)
558 (*it).queryInterfaceTo(aFloppyDrives[i].asOutParam());
559
560 return S_OK;
561}
562
563
564#if defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
565# define VBOX_APP_NAME L"VirtualBox"
566
567static int vboxNetWinAddComponent(std::list< ComObjPtr<HostNetworkInterface> > *pPist,
568 INetCfgComponent *pncc)
569{
570 LPWSTR lpszName;
571 GUID IfGuid;
572 HRESULT hr;
573 int rc = VERR_GENERAL_FAILURE;
574
575 hr = pncc->GetDisplayName(&lpszName);
576 Assert(hr == S_OK);
577 if (hr == S_OK)
578 {
579 Bstr name((CBSTR)lpszName);
580
581 hr = pncc->GetInstanceGuid(&IfGuid);
582 Assert(hr == S_OK);
583 if (hr == S_OK)
584 {
585 /* create a new object and add it to the list */
586 ComObjPtr<HostNetworkInterface> iface;
587 iface.createObject();
588 /* remove the curly bracket at the end */
589 if (SUCCEEDED(iface->init(name, name, Guid(IfGuid), HostNetworkInterfaceType_Bridged)))
590 {
591// iface->setVirtualBox(m->pParent);
592 pPist->push_back(iface);
593 rc = VINF_SUCCESS;
594 }
595 else
596 {
597 Assert(0);
598 }
599 }
600 CoTaskMemFree(lpszName);
601 }
602
603 return rc;
604}
605#endif /* defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT) */
606
607/**
608 * Returns a list of host network interfaces.
609 *
610 * @returns COM status code
611 * @param drives address of result pointer
612 */
613HRESULT Host::getNetworkInterfaces(std::vector<ComPtr<IHostNetworkInterface> > &aNetworkInterfaces)
614{
615#if defined(RT_OS_WINDOWS) || defined(VBOX_WITH_NETFLT) /*|| defined(RT_OS_OS2)*/
616 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
617
618# ifdef VBOX_WITH_HOSTNETIF_API
619 int rc = i_updateNetIfList();
620 if (rc)
621 Log(("Failed to get host network interface list with rc=%Rrc\n", rc));
622
623 aNetworkInterfaces.resize(m->llNetIfs.size());
624 size_t i = 0;
625 for (HostNetworkInterfaceList::iterator it = m->llNetIfs.begin(); it != m->llNetIfs.end(); ++it, ++i)
626 (*it).queryInterfaceTo(aNetworkInterfaces[i].asOutParam());
627
628 return S_OK;
629# else
630 std::list<ComObjPtr<HostNetworkInterface> > list;
631
632# if defined(RT_OS_DARWIN)
633 PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
634 while (pEtherNICs)
635 {
636 ComObjPtr<HostNetworkInterface> IfObj;
637 IfObj.createObject();
638 if (SUCCEEDED(IfObj->init(Bstr(pEtherNICs->szName), Guid(pEtherNICs->Uuid), HostNetworkInterfaceType_Bridged)))
639 list.push_back(IfObj);
640
641 /* next, free current */
642 void *pvFree = pEtherNICs;
643 pEtherNICs = pEtherNICs->pNext;
644 RTMemFree(pvFree);
645 }
646
647# elif defined RT_OS_WINDOWS
648# ifndef VBOX_WITH_NETFLT
649 hr = E_NOTIMPL;
650# else /* # if defined VBOX_WITH_NETFLT */
651 INetCfg *pNc;
652 INetCfgComponent *pMpNcc;
653 INetCfgComponent *pTcpIpNcc;
654 LPWSTR lpszApp;
655 HRESULT hr;
656 IEnumNetCfgBindingPath *pEnumBp;
657 INetCfgBindingPath *pBp;
658 IEnumNetCfgBindingInterface *pEnumBi;
659 INetCfgBindingInterface *pBi;
660
661 /* we are using the INetCfg API for getting the list of miniports */
662 hr = VBoxNetCfgWinQueryINetCfg(FALSE,
663 VBOX_APP_NAME,
664 &pNc,
665 &lpszApp);
666 Assert(hr == S_OK);
667 if (hr == S_OK)
668 {
669# ifdef VBOX_NETFLT_ONDEMAND_BIND
670 /* for the protocol-based approach for now we just get all miniports the MS_TCPIP protocol binds to */
671 hr = pNc->FindComponent(L"MS_TCPIP", &pTcpIpNcc);
672# else
673 /* for the filter-based approach we get all miniports our filter (oracle_VBoxNetLwf)is bound to */
674 hr = pNc->FindComponent(L"oracle_VBoxNetLwf", &pTcpIpNcc);
675 if (hr != S_OK)
676 {
677 /* fall back to NDIS5 miniport lookup (sun_VBoxNetFlt) */
678 hr = pNc->FindComponent(L"sun_VBoxNetFlt", &pTcpIpNcc);
679 }
680# ifndef VBOX_WITH_HARDENING
681 if (hr != S_OK)
682 {
683 /* TODO: try to install the netflt from here */
684 }
685# endif
686
687# endif
688
689 if (hr == S_OK)
690 {
691 hr = VBoxNetCfgWinGetBindingPathEnum(pTcpIpNcc, EBP_BELOW, &pEnumBp);
692 Assert(hr == S_OK);
693 if (hr == S_OK)
694 {
695 hr = VBoxNetCfgWinGetFirstBindingPath(pEnumBp, &pBp);
696 Assert(hr == S_OK || hr == S_FALSE);
697 while (hr == S_OK)
698 {
699 /* S_OK == enabled, S_FALSE == disabled */
700 if (pBp->IsEnabled() == S_OK)
701 {
702 hr = VBoxNetCfgWinGetBindingInterfaceEnum(pBp, &pEnumBi);
703 Assert(hr == S_OK);
704 if (hr == S_OK)
705 {
706 hr = VBoxNetCfgWinGetFirstBindingInterface(pEnumBi, &pBi);
707 Assert(hr == S_OK);
708 while (hr == S_OK)
709 {
710 hr = pBi->GetLowerComponent(&pMpNcc);
711 Assert(hr == S_OK);
712 if (hr == S_OK)
713 {
714 ULONG uComponentStatus;
715 hr = pMpNcc->GetDeviceStatus(&uComponentStatus);
716 Assert(hr == S_OK);
717 if (hr == S_OK)
718 {
719 if (uComponentStatus == 0)
720 {
721 vboxNetWinAddComponent(&list, pMpNcc);
722 }
723 }
724 VBoxNetCfgWinReleaseRef(pMpNcc);
725 }
726 VBoxNetCfgWinReleaseRef(pBi);
727
728 hr = VBoxNetCfgWinGetNextBindingInterface(pEnumBi, &pBi);
729 }
730 VBoxNetCfgWinReleaseRef(pEnumBi);
731 }
732 }
733 VBoxNetCfgWinReleaseRef(pBp);
734
735 hr = VBoxNetCfgWinGetNextBindingPath(pEnumBp, &pBp);
736 }
737 VBoxNetCfgWinReleaseRef(pEnumBp);
738 }
739 VBoxNetCfgWinReleaseRef(pTcpIpNcc);
740 }
741 else
742 {
743 LogRel(("failed to get the oracle_VBoxNetLwf(sun_VBoxNetFlt) component, error (0x%x)\n", hr));
744 }
745
746 VBoxNetCfgWinReleaseINetCfg(pNc, FALSE);
747 }
748# endif /* # if defined VBOX_WITH_NETFLT */
749
750
751# elif defined RT_OS_LINUX
752 int sock = socket(AF_INET, SOCK_DGRAM, 0);
753 if (sock >= 0)
754 {
755 char pBuffer[2048];
756 struct ifconf ifConf;
757 ifConf.ifc_len = sizeof(pBuffer);
758 ifConf.ifc_buf = pBuffer;
759 if (ioctl(sock, SIOCGIFCONF, &ifConf) >= 0)
760 {
761 for (struct ifreq *pReq = ifConf.ifc_req; (char*)pReq < pBuffer + ifConf.ifc_len; pReq++)
762 {
763 if (ioctl(sock, SIOCGIFHWADDR, pReq) >= 0)
764 {
765 if (pReq->ifr_hwaddr.sa_family == ARPHRD_ETHER)
766 {
767 RTUUID uuid;
768 Assert(sizeof(uuid) <= sizeof(*pReq));
769 memcpy(&uuid, pReq, sizeof(uuid));
770
771 ComObjPtr<HostNetworkInterface> IfObj;
772 IfObj.createObject();
773 if (SUCCEEDED(IfObj->init(Bstr(pReq->ifr_name), Guid(uuid), HostNetworkInterfaceType_Bridged)))
774 list.push_back(IfObj);
775 }
776 }
777 }
778 }
779 close(sock);
780 }
781# endif /* RT_OS_LINUX */
782
783 aNetworkInterfaces.resize(list.size());
784 size_t i = 0;
785 for (std::list<ComObjPtr<HostNetworkInterface> >::const_iterator it = list.begin(); it != list.end(); ++it, ++i)
786 aNetworkInterfaces[i] = *it;
787
788 return S_OK;
789# endif
790#else
791 /* Not implemented / supported on this platform. */
792 ReturnComNotImplemented();
793#endif
794}
795
796HRESULT Host::getUSBDevices(std::vector<ComPtr<IHostUSBDevice> > &aUSBDevices)
797{
798#ifdef VBOX_WITH_USB
799 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
800
801 MultiResult rc = i_checkUSBProxyService();
802 if (FAILED(rc))
803 return rc;
804
805 return m->pUSBProxyService->getDeviceCollection(aUSBDevices);
806#else
807 /* Note: The GUI depends on this method returning E_NOTIMPL with no
808 * extended error info to indicate that USB is simply not available
809 * (w/o treating it as a failure), for example, as in OSE. */
810 NOREF(aUSBDevices);
811# ifndef RT_OS_WINDOWS
812 NOREF(aUSBDevices);
813# endif
814 ReturnComNotImplemented();
815#endif
816}
817
818/**
819 * This method return the list of registered name servers
820 */
821HRESULT Host::getNameServers(std::vector<com::Utf8Str> &aNameServers)
822{
823 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
824 return m->hostDnsMonitorProxy.GetNameServers(aNameServers);
825}
826
827
828/**
829 * This method returns the domain name of the host
830 */
831HRESULT Host::getDomainName(com::Utf8Str &aDomainName)
832{
833 /* XXX: note here should be synchronization with thread polling state
834 * changes in name resoving system on host */
835 return m->hostDnsMonitorProxy.GetDomainName(&aDomainName);
836}
837
838
839/**
840 * This method returns the search string.
841 */
842HRESULT Host::getSearchStrings(std::vector<com::Utf8Str> &aSearchStrings)
843{
844 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
845 return m->hostDnsMonitorProxy.GetSearchStrings(aSearchStrings);
846}
847
848HRESULT Host::getUSBDeviceFilters(std::vector<ComPtr<IHostUSBDeviceFilter> > &aUSBDeviceFilters)
849{
850#ifdef VBOX_WITH_USB
851 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
852
853 MultiResult rc = i_checkUSBProxyService();
854 if (FAILED(rc))
855 return rc;
856
857 aUSBDeviceFilters.resize(m->llUSBDeviceFilters.size());
858 size_t i = 0;
859 for (USBDeviceFilterList::iterator it = m->llUSBDeviceFilters.begin(); it != m->llUSBDeviceFilters.end(); ++it, ++i)
860 (*it).queryInterfaceTo(aUSBDeviceFilters[i].asOutParam());
861
862 return rc;
863#else
864 /* Note: The GUI depends on this method returning E_NOTIMPL with no
865 * extended error info to indicate that USB is simply not available
866 * (w/o treating it as a failure), for example, as in OSE. */
867 NOREF(aUSBDeviceFilters);
868# ifndef RT_OS_WINDOWS
869 NOREF(aUSBDeviceFilters);
870# endif
871 ReturnComNotImplemented();
872#endif
873}
874
875/**
876 * Returns the number of installed logical processors
877 *
878 * @returns COM status code
879 * @param count address of result variable
880 */
881
882HRESULT Host::getProcessorCount(ULONG *aCount)
883{
884 // no locking required
885
886 *aCount = RTMpGetPresentCount();
887 return S_OK;
888}
889
890/**
891 * Returns the number of online logical processors
892 *
893 * @returns COM status code
894 * @param count address of result variable
895 */
896HRESULT Host::getProcessorOnlineCount(ULONG *aCount)
897{
898 // no locking required
899
900 *aCount = RTMpGetOnlineCount();
901 return S_OK;
902}
903
904/**
905 * Returns the number of installed physical processor cores.
906 *
907 * @returns COM status code
908 * @param count address of result variable
909 */
910HRESULT Host::getProcessorCoreCount(ULONG *aCount)
911{
912 // no locking required
913
914 *aCount = RTMpGetPresentCoreCount();
915 return S_OK;
916}
917
918/**
919 * Returns the number of installed physical processor cores.
920 *
921 * @returns COM status code
922 * @param count address of result variable
923 */
924HRESULT Host::getProcessorOnlineCoreCount(ULONG *aCount)
925{
926 // no locking required
927
928 *aCount = RTMpGetOnlineCoreCount();
929 return S_OK;
930}
931
932/**
933 * Returns the (approximate) maximum speed of the given host CPU in MHz
934 *
935 * @returns COM status code
936 * @param cpu id to get info for.
937 * @param speed address of result variable, speed is 0 if unknown or aCpuId is invalid.
938 */
939HRESULT Host::getProcessorSpeed(ULONG aCpuId,
940 ULONG *aSpeed)
941{
942 // no locking required
943
944 *aSpeed = RTMpGetMaxFrequency(aCpuId);
945 return S_OK;
946}
947
948/**
949 * Returns a description string for the host CPU
950 *
951 * @returns COM status code
952 * @param cpu id to get info for.
953 * @param description address of result variable, empty string if not known or aCpuId is invalid.
954 */
955HRESULT Host::getProcessorDescription(ULONG aCpuId, com::Utf8Str &aDescription)
956{
957 // no locking required
958
959 char szCPUModel[80];
960 szCPUModel[0] = 0;
961 int vrc = RTMpGetDescription(aCpuId, szCPUModel, sizeof(szCPUModel));
962 if (RT_FAILURE(vrc))
963 return E_FAIL; /** @todo error reporting? */
964
965 aDescription = Utf8Str(szCPUModel);
966
967 return S_OK;
968}
969
970/**
971 * Returns whether a host processor feature is supported or not
972 *
973 * @returns COM status code
974 * @param Feature to query.
975 * @param address of supported bool result variable
976 */
977HRESULT Host::getProcessorFeature(ProcessorFeature_T aFeature, BOOL *aSupported)
978{
979 /* Validate input. */
980 switch (aFeature)
981 {
982 case ProcessorFeature_HWVirtEx:
983 case ProcessorFeature_PAE:
984 case ProcessorFeature_LongMode:
985 case ProcessorFeature_NestedPaging:
986 break;
987 default:
988 return setError(E_INVALIDARG, tr("The aFeature value %d (%#x) is out of range."), (int)aFeature, (int)aFeature);
989 }
990
991 /* Do the job. */
992 AutoCaller autoCaller(this);
993 HRESULT hrc = autoCaller.rc();
994 if (SUCCEEDED(hrc))
995 {
996 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
997
998 if ( m->fRecheckVTSupported
999 && ( aFeature == ProcessorFeature_HWVirtEx
1000 || aFeature == ProcessorFeature_NestedPaging)
1001 )
1002 {
1003 alock.release();
1004
1005 /* Perhaps the driver is available now... */
1006 int rc = SUPR3InitEx(false /*fUnrestricted*/, NULL);
1007 if (RT_SUCCESS(rc))
1008 {
1009 uint32_t fVTCaps;
1010 rc = SUPR3QueryVTCaps(&fVTCaps);
1011
1012 AutoWriteLock wlock(this COMMA_LOCKVAL_SRC_POS);
1013 if (RT_SUCCESS(rc))
1014 {
1015 Assert(fVTCaps & (SUPVTCAPS_AMD_V | SUPVTCAPS_VT_X));
1016 if (fVTCaps & SUPVTCAPS_NESTED_PAGING)
1017 m->fNestedPagingSupported = true;
1018 else
1019 Assert(m->fNestedPagingSupported == false);
1020 }
1021 else
1022 {
1023 LogRel(("SUPR0QueryVTCaps -> %Rrc\n", rc));
1024 m->fVTSupported = m->fNestedPagingSupported = true;
1025 }
1026 }
1027
1028 alock.acquire();
1029 }
1030
1031 switch (aFeature)
1032 {
1033 case ProcessorFeature_HWVirtEx:
1034 *aSupported = m->fVTSupported;
1035 break;
1036
1037 case ProcessorFeature_PAE:
1038 *aSupported = m->fPAESupported;
1039 break;
1040
1041 case ProcessorFeature_LongMode:
1042 *aSupported = m->fLongModeSupported;
1043 break;
1044
1045 case ProcessorFeature_NestedPaging:
1046 *aSupported = m->fNestedPagingSupported;
1047 break;
1048
1049 default:
1050 AssertFailed();
1051 }
1052 }
1053 return hrc;
1054}
1055
1056/**
1057 * Returns the specific CPUID leaf.
1058 *
1059 * @returns COM status code
1060 * @param aCpuId The CPU number. Mostly ignored.
1061 * @param aLeaf The leaf number.
1062 * @param aSubLeaf The sub-leaf number.
1063 * @param aValEAX Where to return EAX.
1064 * @param aValEBX Where to return EBX.
1065 * @param aValECX Where to return ECX.
1066 * @param aValEDX Where to return EDX.
1067 */
1068HRESULT Host::getProcessorCPUIDLeaf(ULONG aCpuId, ULONG aLeaf, ULONG aSubLeaf,
1069 ULONG *aValEAX, ULONG *aValEBX, ULONG *aValECX, ULONG *aValEDX)
1070{
1071 // no locking required
1072
1073 /* Check that the CPU is online. */
1074 /** @todo later use RTMpOnSpecific. */
1075 if (!RTMpIsCpuOnline(aCpuId))
1076 return RTMpIsCpuPresent(aCpuId)
1077 ? setError(E_FAIL, tr("CPU no.%u is not present"), aCpuId)
1078 : setError(E_FAIL, tr("CPU no.%u is not online"), aCpuId);
1079
1080 uint32_t uEAX, uEBX, uECX, uEDX;
1081 ASMCpuId_Idx_ECX(aLeaf, aSubLeaf, &uEAX, &uEBX, &uECX, &uEDX);
1082 *aValEAX = uEAX;
1083 *aValEBX = uEBX;
1084 *aValECX = uECX;
1085 *aValEDX = uEDX;
1086
1087 return S_OK;
1088}
1089
1090/**
1091 * Returns the amount of installed system memory in megabytes
1092 *
1093 * @returns COM status code
1094 * @param size address of result variable
1095 */
1096HRESULT Host::getMemorySize(ULONG *aSize)
1097{
1098 // no locking required
1099
1100 uint64_t cb;
1101 int rc = RTSystemQueryTotalRam(&cb);
1102 if (RT_FAILURE(rc))
1103 return E_FAIL;
1104 *aSize = (ULONG)(cb / _1M);
1105 return S_OK;
1106}
1107
1108/**
1109 * Returns the current system memory free space in megabytes
1110 *
1111 * @returns COM status code
1112 * @param available address of result variable
1113 */
1114HRESULT Host::getMemoryAvailable(ULONG *aAvailable)
1115{
1116 // no locking required
1117
1118 uint64_t cb;
1119 int rc = RTSystemQueryAvailableRam(&cb);
1120 if (RT_FAILURE(rc))
1121 return E_FAIL;
1122 *aAvailable = (ULONG)(cb / _1M);
1123 return S_OK;
1124}
1125
1126/**
1127 * Returns the name string of the host operating system
1128 *
1129 * @returns COM status code
1130 * @param os address of result variable
1131 */
1132HRESULT Host::getOperatingSystem(com::Utf8Str &aOperatingSystem)
1133{
1134 // no locking required
1135
1136 char szOSName[80];
1137 int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szOSName, sizeof(szOSName));
1138 if (RT_FAILURE(vrc))
1139 return E_FAIL; /** @todo error reporting? */
1140 aOperatingSystem = Utf8Str(szOSName);
1141 return S_OK;
1142}
1143
1144/**
1145 * Returns the version string of the host operating system
1146 *
1147 * @returns COM status code
1148 * @param os address of result variable
1149 */
1150HRESULT Host::getOSVersion(com::Utf8Str &aVersion)
1151{
1152 // no locking required
1153
1154 /* Get the OS release. Reserve some buffer space for the service pack. */
1155 char szOSRelease[128];
1156 int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szOSRelease, sizeof(szOSRelease) - 32);
1157 if (RT_FAILURE(vrc))
1158 return E_FAIL; /** @todo error reporting? */
1159
1160 /* Append the service pack if present. */
1161 char szOSServicePack[80];
1162 vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szOSServicePack, sizeof(szOSServicePack));
1163 if (RT_FAILURE(vrc))
1164 {
1165 if (vrc != VERR_NOT_SUPPORTED)
1166 return E_FAIL; /** @todo error reporting? */
1167 szOSServicePack[0] = '\0';
1168 }
1169 if (szOSServicePack[0] != '\0')
1170 {
1171 char *psz = strchr(szOSRelease, '\0');
1172 RTStrPrintf(psz, &szOSRelease[sizeof(szOSRelease)] - psz, "sp%s", szOSServicePack);
1173 }
1174
1175 aVersion = szOSRelease;
1176 return S_OK;
1177}
1178
1179/**
1180 * Returns the current host time in milliseconds since 1970-01-01 UTC.
1181 *
1182 * @returns COM status code
1183 * @param time address of result variable
1184 */
1185HRESULT Host::getUTCTime(LONG64 *aUTCTime)
1186{
1187 // no locking required
1188
1189 RTTIMESPEC now;
1190 *aUTCTime = RTTimeSpecGetMilli(RTTimeNow(&now));
1191
1192 return S_OK;
1193}
1194
1195
1196HRESULT Host::getAcceleration3DAvailable(BOOL *aSupported)
1197{
1198 HRESULT hrc = S_OK;
1199 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1200 if (m->f3DAccelerationSupported != -1)
1201 *aSupported = m->f3DAccelerationSupported;
1202 else
1203 {
1204 alock.release();
1205
1206#ifdef VBOX_WITH_CROGL
1207 bool fSupported = VBoxOglIs3DAccelerationSupported();
1208#else
1209 bool fSupported = false; /* shoudn't get here, but just in case. */
1210#endif
1211 AutoWriteLock alock2(this COMMA_LOCKVAL_SRC_POS);
1212
1213 m->f3DAccelerationSupported = fSupported;
1214 alock2.release();
1215 *aSupported = fSupported;
1216 }
1217
1218#ifdef DEBUG_misha
1219 AssertMsgFailed(("should not be here any more!\n"));
1220#endif
1221
1222 return hrc;
1223}
1224
1225HRESULT Host::createHostOnlyNetworkInterface(ComPtr<IHostNetworkInterface> &aHostInterface,
1226 ComPtr<IProgress> &aProgress)
1227
1228{
1229#ifdef VBOX_WITH_HOSTNETIF_API
1230 /* No need to lock anything. If there ever will - watch out, the function
1231 * called below grabs the VirtualBox lock. */
1232
1233 int r = NetIfCreateHostOnlyNetworkInterface(m->pParent, aHostInterface.asOutParam(), aProgress.asOutParam());
1234 if (RT_SUCCESS(r))
1235 {
1236 if (aHostInterface.isNull())
1237 return setError(E_FAIL,
1238 tr("Unable to create a host network interface"));
1239
1240#if !defined(RT_OS_WINDOWS)
1241 Bstr tmpAddr, tmpMask, tmpName;
1242 HRESULT hrc;
1243 hrc = aHostInterface->COMGETTER(Name)(tmpName.asOutParam());
1244 ComAssertComRCRet(hrc, hrc);
1245 hrc = aHostInterface->COMGETTER(IPAddress)(tmpAddr.asOutParam());
1246 ComAssertComRCRet(hrc, hrc);
1247 hrc = aHostInterface->COMGETTER(NetworkMask)(tmpMask.asOutParam());
1248 ComAssertComRCRet(hrc, hrc);
1249 /*
1250 * We need to write the default IP address and mask to extra data now,
1251 * so the interface gets re-created after vboxnetadp.ko reload.
1252 * Note that we avoid calling EnableStaticIpConfig since it would
1253 * change the address on host's interface as well and we want to
1254 * postpone the change until VM actually starts.
1255 */
1256 hrc = m->pParent->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress",
1257 tmpName.raw()).raw(),
1258 tmpAddr.raw());
1259 ComAssertComRCRet(hrc, hrc);
1260
1261 hrc = m->pParent->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask",
1262 tmpName.raw()).raw(),
1263 tmpMask.raw());
1264 ComAssertComRCRet(hrc, hrc);
1265#endif
1266 }
1267
1268 return S_OK;
1269#else
1270 return E_NOTIMPL;
1271#endif
1272}
1273
1274HRESULT Host::removeHostOnlyNetworkInterface(const com::Guid &aId,
1275 ComPtr<IProgress> &aProgress)
1276
1277{
1278#ifdef VBOX_WITH_HOSTNETIF_API
1279 /* No need to lock anything, the code below does not touch the state
1280 * of the host object. If that ever changes then check for lock order
1281 * violations with the called functions. */
1282
1283 Bstr name;
1284 HRESULT rc;
1285
1286 /* first check whether an interface with the given name already exists */
1287 {
1288 ComPtr<IHostNetworkInterface> iface;
1289 rc = findHostNetworkInterfaceById(aId, iface);
1290 if (FAILED(rc))
1291 return setError(VBOX_E_OBJECT_NOT_FOUND,
1292 tr("Host network interface with UUID {%RTuuid} does not exist"),
1293 Guid(aId).raw());
1294 rc = iface->COMGETTER(Name)(name.asOutParam());
1295 ComAssertComRCRet(rc, rc);
1296 }
1297
1298 int r = NetIfRemoveHostOnlyNetworkInterface(m->pParent, Guid(aId).ref(), aProgress.asOutParam());
1299 if (RT_SUCCESS(r))
1300 {
1301 /* Drop configuration parameters for removed interface */
1302 rc = m->pParent->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", name.raw()).raw(), NULL);
1303 rc = m->pParent->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", name.raw()).raw(), NULL);
1304 rc = m->pParent->SetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", name.raw()).raw(), NULL);
1305 rc = m->pParent->SetExtraData(BstrFmt("HostOnly/%ls/IPV6NetMask", name.raw()).raw(), NULL);
1306
1307 return S_OK;
1308 }
1309
1310 return r == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
1311#else
1312 return E_NOTIMPL;
1313#endif
1314}
1315
1316HRESULT Host::createUSBDeviceFilter(const com::Utf8Str &aName,
1317 ComPtr<IHostUSBDeviceFilter> &aFilter)
1318{
1319#ifdef VBOX_WITH_USB
1320
1321 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1322
1323 ComObjPtr<HostUSBDeviceFilter> filter;
1324 filter.createObject();
1325 HRESULT rc = filter->init(this, Bstr(aName).raw());
1326 ComAssertComRCRet(rc, rc);
1327 rc = filter.queryInterfaceTo(aFilter.asOutParam());
1328 AssertComRCReturn(rc, rc);
1329 return S_OK;
1330#else
1331 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1332 * extended error info to indicate that USB is simply not available
1333 * (w/o treating it as a failure), for example, as in OSE. */
1334 NOREF(aName);
1335 NOREF(aFilter);
1336 ReturnComNotImplemented();
1337#endif
1338}
1339
1340HRESULT Host::insertUSBDeviceFilter(ULONG aPosition,
1341 const ComPtr<IHostUSBDeviceFilter> &aFilter)
1342{
1343#ifdef VBOX_WITH_USB
1344 /* Note: HostUSBDeviceFilter and USBProxyService also uses this lock. */
1345
1346 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1347
1348 MultiResult rc = i_checkUSBProxyService();
1349 if (FAILED(rc))
1350 return rc;
1351
1352 ComObjPtr<HostUSBDeviceFilter> pFilter;
1353 for (USBDeviceFilterList::iterator it = m->llChildren.begin();
1354 it != m->llChildren.end();
1355 ++it)
1356 {
1357 if (*it == aFilter)
1358 {
1359 pFilter = *it;
1360 break;
1361 }
1362 }
1363 if (pFilter.isNull())
1364 return setError(VBOX_E_INVALID_OBJECT_STATE,
1365 tr("The given USB device filter is not created within this VirtualBox instance"));
1366
1367 if (pFilter->mInList)
1368 return setError(E_INVALIDARG,
1369 tr("The given USB device filter is already in the list"));
1370
1371 /* iterate to the position... */
1372 USBDeviceFilterList::iterator itPos = m->llUSBDeviceFilters.begin();
1373 std::advance(itPos, aPosition);
1374 /* ...and insert */
1375 m->llUSBDeviceFilters.insert(itPos, pFilter);
1376 pFilter->mInList = true;
1377
1378 /* notify the proxy (only when the filter is active) */
1379 if ( m->pUSBProxyService->isActive()
1380 && pFilter->i_getData().mActive)
1381 {
1382 ComAssertRet(pFilter->i_getId() == NULL, E_FAIL);
1383 pFilter->i_getId() = m->pUSBProxyService->insertFilter(&pFilter->i_getData().mUSBFilter);
1384 }
1385
1386 // save the global settings; for that we should hold only the VirtualBox lock
1387 alock.release();
1388 AutoWriteLock vboxLock(m->pParent COMMA_LOCKVAL_SRC_POS);
1389 return rc = m->pParent->i_saveSettings();
1390#else
1391
1392 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1393 * extended error info to indicate that USB is simply not available
1394 * (w/o treating it as a failure), for example, as in OSE. */
1395 NOREF(aPosition);
1396 NOREF(aFilter);
1397 ReturnComNotImplemented();
1398#endif
1399}
1400
1401HRESULT Host::removeUSBDeviceFilter(ULONG aPosition)
1402{
1403#ifdef VBOX_WITH_USB
1404
1405 /* Note: HostUSBDeviceFilter and USBProxyService also uses this lock. */
1406 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1407
1408 MultiResult rc = i_checkUSBProxyService();
1409 if (FAILED(rc))
1410 return rc;
1411
1412 if (!m->llUSBDeviceFilters.size())
1413 return setError(E_INVALIDARG,
1414 tr("The USB device filter list is empty"));
1415
1416 if (aPosition >= m->llUSBDeviceFilters.size())
1417 return setError(E_INVALIDARG,
1418 tr("Invalid position: %lu (must be in range [0, %lu])"),
1419 aPosition, m->llUSBDeviceFilters.size() - 1);
1420
1421 ComObjPtr<HostUSBDeviceFilter> filter;
1422 {
1423 /* iterate to the position... */
1424 USBDeviceFilterList::iterator it = m->llUSBDeviceFilters.begin();
1425 std::advance(it, aPosition);
1426 /* ...get an element from there... */
1427 filter = *it;
1428 /* ...and remove */
1429 filter->mInList = false;
1430 m->llUSBDeviceFilters.erase(it);
1431 }
1432
1433 /* notify the proxy (only when the filter is active) */
1434 if (m->pUSBProxyService->isActive() && filter->i_getData().mActive)
1435 {
1436 ComAssertRet(filter->i_getId() != NULL, E_FAIL);
1437 m->pUSBProxyService->removeFilter(filter->i_getId());
1438 filter->i_getId() = NULL;
1439 }
1440
1441 // save the global settings; for that we should hold only the VirtualBox lock
1442 alock.release();
1443 AutoWriteLock vboxLock(m->pParent COMMA_LOCKVAL_SRC_POS);
1444 return rc = m->pParent->i_saveSettings();
1445#else
1446 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1447 * extended error info to indicate that USB is simply not available
1448 * (w/o treating it as a failure), for example, as in OSE. */
1449 NOREF(aPosition);
1450 ReturnComNotImplemented();
1451#endif
1452}
1453
1454HRESULT Host::findHostDVDDrive(const com::Utf8Str &aName,
1455 ComPtr<IMedium> &aDrive)
1456{
1457 ComObjPtr<Medium> medium;
1458 HRESULT rc = i_findHostDriveByNameOrId(DeviceType_DVD, aName, medium);
1459 if (SUCCEEDED(rc))
1460 rc = medium.queryInterfaceTo(aDrive.asOutParam());
1461 else
1462 rc = setError(rc, Medium::tr("The host DVD drive named '%s' could not be found"), aName.c_str());
1463 return rc;
1464}
1465
1466HRESULT Host::findHostFloppyDrive(const com::Utf8Str &aName, ComPtr<IMedium> &aDrive)
1467{
1468 aDrive = NULL;
1469
1470 ComObjPtr<Medium>medium;
1471
1472 HRESULT rc = i_findHostDriveByNameOrId(DeviceType_Floppy, aName, medium);
1473 if (SUCCEEDED(rc))
1474 return medium.queryInterfaceTo(aDrive.asOutParam());
1475 else
1476 return setError(rc, Medium::tr("The host floppy drive named '%s' could not be found"), aName.c_str());
1477}
1478
1479HRESULT Host::findHostNetworkInterfaceByName(const com::Utf8Str &aName,
1480 ComPtr<IHostNetworkInterface> &aNetworkInterface)
1481{
1482#ifndef VBOX_WITH_HOSTNETIF_API
1483 return E_NOTIMPL;
1484#else
1485 if (!aName.length())
1486 return E_INVALIDARG;
1487
1488 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1489
1490 aNetworkInterface = NULL;
1491
1492 ComObjPtr<HostNetworkInterface> found;
1493 int rc = i_updateNetIfList();
1494 if (RT_FAILURE(rc))
1495 {
1496 Log(("Failed to get host network interface list with rc=%Rrc\n", rc));
1497 return E_FAIL;
1498 }
1499 for (HostNetworkInterfaceList::iterator it = m->llNetIfs.begin(); it != m->llNetIfs.end(); ++it)
1500 {
1501 Bstr n;
1502 (*it)->COMGETTER(Name)(n.asOutParam());
1503 if (n == aName)
1504 found = *it;
1505 }
1506
1507 if (!found)
1508 return setError(E_INVALIDARG,
1509 HostNetworkInterface::tr("The host network interface with the given name could not be found"));
1510
1511 return found.queryInterfaceTo(aNetworkInterface.asOutParam());
1512#endif
1513}
1514
1515HRESULT Host::findHostNetworkInterfaceById(const com::Guid &aId,
1516 ComPtr<IHostNetworkInterface> &aNetworkInterface)
1517{
1518#ifndef VBOX_WITH_HOSTNETIF_API
1519 return E_NOTIMPL;
1520#else
1521 if (!aId.isValid())
1522 return E_INVALIDARG;
1523
1524 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1525
1526 aNetworkInterface = NULL;
1527
1528 ComObjPtr<HostNetworkInterface> found;
1529 int rc = i_updateNetIfList();
1530 if (RT_FAILURE(rc))
1531 {
1532 Log(("Failed to get host network interface list with rc=%Rrc\n", rc));
1533 return E_FAIL;
1534 }
1535 HostNetworkInterfaceList::iterator it;
1536 for (it = m->llNetIfs.begin(); it != m->llNetIfs.end(); ++it)
1537 {
1538 Bstr g;
1539 (*it)->COMGETTER(Id)(g.asOutParam());
1540 if (Guid(g) == aId)
1541 found = *it;
1542 }
1543
1544 if (!found)
1545 return setError(E_INVALIDARG,
1546 HostNetworkInterface::tr("The host network interface with the given GUID could not be found"));
1547 return found.queryInterfaceTo(aNetworkInterface.asOutParam());
1548
1549#endif
1550}
1551
1552HRESULT Host::findHostNetworkInterfacesOfType(HostNetworkInterfaceType_T aType,
1553 std::vector<ComPtr<IHostNetworkInterface> > &aNetworkInterfaces)
1554{
1555#ifdef VBOX_WITH_HOSTNETIF_API
1556 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1557 int rc = i_updateNetIfList();
1558 if (RT_FAILURE(rc))
1559 return E_FAIL;
1560
1561 HostNetworkInterfaceList resultList;
1562
1563 for (HostNetworkInterfaceList::iterator it = m->llNetIfs.begin(); it != m->llNetIfs.end(); ++it)
1564 {
1565 HostNetworkInterfaceType_T t;
1566 HRESULT hr = (*it)->COMGETTER(InterfaceType)(&t);
1567 if (FAILED(hr))
1568 return hr;
1569
1570 if (t == aType)
1571 resultList.push_back(*it);
1572 }
1573 aNetworkInterfaces.resize(resultList.size());
1574 size_t i = 0;
1575 for (HostNetworkInterfaceList::iterator it = resultList.begin(); it != resultList.end(); ++it, ++i)
1576 {
1577 (*it).queryInterfaceTo(aNetworkInterfaces[i].asOutParam());
1578 }
1579
1580 return S_OK;
1581#else
1582 return E_NOTIMPL;
1583#endif
1584}
1585
1586HRESULT Host::findUSBDeviceByAddress(const com::Utf8Str &aName,
1587 ComPtr<IHostUSBDevice> &aDevice)
1588{
1589#ifdef VBOX_WITH_USB
1590
1591 aDevice = NULL;
1592 SafeIfaceArray<IHostUSBDevice> devsvec;
1593 HRESULT rc = COMGETTER(USBDevices)(ComSafeArrayAsOutParam(devsvec));
1594 if (FAILED(rc))
1595 return rc;
1596
1597 for (size_t i = 0; i < devsvec.size(); ++i)
1598 {
1599 Bstr address;
1600 rc = devsvec[i]->COMGETTER(Address)(address.asOutParam());
1601 if (FAILED(rc))
1602 return rc;
1603 if (address == aName)
1604 {
1605 return (ComPtr<IHostUSBDevice>(devsvec[i]).queryInterfaceTo(aDevice.asOutParam()));
1606 }
1607 }
1608
1609 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
1610 tr("Could not find a USB device with address '%s'"),
1611 aName.c_str());
1612
1613#else /* !VBOX_WITH_USB */
1614 NOREF(aAddress);
1615 NOREF(aDevice);
1616 return E_NOTIMPL;
1617#endif /* !VBOX_WITH_USB */
1618}
1619HRESULT Host::findUSBDeviceById(const com::Guid &aId,
1620 ComPtr<IHostUSBDevice> &aDevice)
1621{
1622#ifdef VBOX_WITH_USB
1623 if (!aId.isValid())
1624 return E_INVALIDARG;
1625
1626 aDevice = NULL;
1627
1628 SafeIfaceArray<IHostUSBDevice> devsvec;
1629 HRESULT rc = COMGETTER(USBDevices)(ComSafeArrayAsOutParam(devsvec));
1630 if (FAILED(rc))
1631 return rc;
1632
1633 for (size_t i = 0; i < devsvec.size(); ++i)
1634 {
1635 Bstr id;
1636 rc = devsvec[i]->COMGETTER(Id)(id.asOutParam());
1637 if (FAILED(rc))
1638 return rc;
1639 if (Guid(id) == aId)
1640 {
1641 return (ComPtr<IHostUSBDevice>(devsvec[i]).queryInterfaceTo(aDevice.asOutParam()));
1642 }
1643 }
1644 return setErrorNoLog(VBOX_E_OBJECT_NOT_FOUND,
1645 tr("Could not find a USB device with uuid {%RTuuid}"),
1646 aId.raw());
1647
1648#else /* !VBOX_WITH_USB */
1649 NOREF(aId);
1650 NOREF(aDevice);
1651 return E_NOTIMPL;
1652#endif /* !VBOX_WITH_USB */
1653}
1654
1655HRESULT Host::generateMACAddress(com::Utf8Str &aAddress)
1656{
1657 // no locking required
1658 i_generateMACAddress(aAddress);
1659 return S_OK;
1660}
1661
1662/**
1663 * Returns a list of host video capture devices (webcams, etc).
1664 *
1665 * @returns COM status code
1666 * @param aVideoInputDevices Array of interface pointers to be filled.
1667 */
1668HRESULT Host::getVideoInputDevices(std::vector<ComPtr<IHostVideoInputDevice> > &aVideoInputDevices)
1669{
1670 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1671 HostVideoInputDeviceList list;
1672
1673 HRESULT rc = HostVideoInputDevice::queryHostDevices(m->pParent, &list);
1674 if (FAILED(rc))
1675 return rc;
1676
1677 aVideoInputDevices.resize(list.size());
1678 size_t i = 0;
1679 for (HostVideoInputDeviceList::const_iterator it = list.begin(); it != list.end(); ++it, ++i)
1680 (*it).queryInterfaceTo(aVideoInputDevices[i].asOutParam());
1681
1682 return S_OK;
1683}
1684
1685// public methods only for internal purposes
1686////////////////////////////////////////////////////////////////////////////////
1687
1688HRESULT Host::i_loadSettings(const settings::Host &data)
1689{
1690 HRESULT rc = S_OK;
1691#ifdef VBOX_WITH_USB
1692 AutoCaller autoCaller(this);
1693 if (FAILED(autoCaller.rc()))
1694 return autoCaller.rc();
1695
1696 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1697
1698 for (settings::USBDeviceFiltersList::const_iterator it = data.llUSBDeviceFilters.begin();
1699 it != data.llUSBDeviceFilters.end();
1700 ++it)
1701 {
1702 const settings::USBDeviceFilter &f = *it;
1703 ComObjPtr<HostUSBDeviceFilter> pFilter;
1704 pFilter.createObject();
1705 rc = pFilter->init(this, f);
1706 if (FAILED(rc))
1707 break;
1708
1709 m->llUSBDeviceFilters.push_back(pFilter);
1710 pFilter->mInList = true;
1711
1712 /* notify the proxy (only when the filter is active) */
1713 if (pFilter->i_getData().mActive)
1714 {
1715 HostUSBDeviceFilter *flt = pFilter; /* resolve ambiguity */
1716 flt->i_getId() = m->pUSBProxyService->insertFilter(&pFilter->i_getData().mUSBFilter);
1717 }
1718 }
1719#else
1720 NOREF(data);
1721#endif /* VBOX_WITH_USB */
1722 return rc;
1723}
1724
1725HRESULT Host::i_saveSettings(settings::Host &data)
1726{
1727#ifdef VBOX_WITH_USB
1728 AutoCaller autoCaller(this);
1729 if (FAILED(autoCaller.rc()))
1730 return autoCaller.rc();
1731
1732 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1733
1734 data.llUSBDeviceFilters.clear();
1735
1736 for (USBDeviceFilterList::const_iterator it = m->llUSBDeviceFilters.begin();
1737 it != m->llUSBDeviceFilters.end();
1738 ++it)
1739 {
1740 ComObjPtr<HostUSBDeviceFilter> pFilter = *it;
1741 settings::USBDeviceFilter f;
1742 pFilter->i_saveSettings(f);
1743 data.llUSBDeviceFilters.push_back(f);
1744 }
1745#else
1746 NOREF(data);
1747#endif /* VBOX_WITH_USB */
1748
1749 return S_OK;
1750}
1751
1752/**
1753 * Sets the given pointer to point to the static list of DVD or floppy
1754 * drives in the Host instance data, depending on the @a mediumType
1755 * parameter.
1756 *
1757 * This builds the list on the first call; it adds or removes host drives
1758 * that may have changed if fRefresh == true.
1759 *
1760 * The caller must hold the medium tree write lock before calling this.
1761 * To protect the list to which the caller's pointer points, the caller
1762 * must also hold that lock.
1763 *
1764 * @param mediumType Must be DeviceType_Floppy or DeviceType_DVD.
1765 * @param fRefresh Whether to refresh the host drives list even if this is not the first call.
1766 * @param pll Caller's pointer which gets set to the static list of host drives.
1767 * @param treeLock Reference to media tree lock, need to drop it temporarily.
1768 * @returns COM status code
1769 */
1770HRESULT Host::i_getDrives(DeviceType_T mediumType,
1771 bool fRefresh,
1772 MediaList *&pll,
1773 AutoWriteLock &treeLock)
1774{
1775 HRESULT rc = S_OK;
1776 Assert(m->pParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
1777
1778 MediaList llNew;
1779 MediaList *pllCached;
1780 bool *pfListBuilt = NULL;
1781
1782 switch (mediumType)
1783 {
1784 case DeviceType_DVD:
1785 if (!m->fDVDDrivesListBuilt || fRefresh)
1786 {
1787 rc = i_buildDVDDrivesList(llNew);
1788 if (FAILED(rc))
1789 return rc;
1790 pfListBuilt = &m->fDVDDrivesListBuilt;
1791 }
1792 pllCached = &m->llDVDDrives;
1793 break;
1794
1795 case DeviceType_Floppy:
1796 if (!m->fFloppyDrivesListBuilt || fRefresh)
1797 {
1798 rc = i_buildFloppyDrivesList(llNew);
1799 if (FAILED(rc))
1800 return rc;
1801 pfListBuilt = &m->fFloppyDrivesListBuilt;
1802 }
1803 pllCached = &m->llFloppyDrives;
1804 break;
1805
1806 default:
1807 return E_INVALIDARG;
1808 }
1809
1810 if (pfListBuilt)
1811 {
1812 // a list was built in llNew above:
1813 if (!*pfListBuilt)
1814 {
1815 // this was the first call (instance bool is still false): then just copy the whole list and return
1816 *pllCached = llNew;
1817 // and mark the instance data as "built"
1818 *pfListBuilt = true;
1819 }
1820 else
1821 {
1822 // list was built, and this was a subsequent call: then compare the old and the new lists
1823
1824 // remove drives from the cached list which are no longer present
1825 for (MediaList::iterator itCached = pllCached->begin();
1826 itCached != pllCached->end();
1827 /*nothing */)
1828 {
1829 Medium *pCached = *itCached;
1830 const Utf8Str strLocationCached = pCached->i_getLocationFull();
1831 bool fFound = false;
1832 for (MediaList::iterator itNew = llNew.begin();
1833 itNew != llNew.end();
1834 ++itNew)
1835 {
1836 Medium *pNew = *itNew;
1837 const Utf8Str strLocationNew = pNew->i_getLocationFull();
1838 if (strLocationNew == strLocationCached)
1839 {
1840 fFound = true;
1841 break;
1842 }
1843 }
1844 if (!fFound)
1845 itCached = pllCached->erase(itCached);
1846 else
1847 ++itCached;
1848 }
1849
1850 // add drives to the cached list that are not on there yet
1851 for (MediaList::iterator itNew = llNew.begin();
1852 itNew != llNew.end();
1853 ++itNew)
1854 {
1855 Medium *pNew = *itNew;
1856 const Utf8Str strLocationNew = pNew->i_getLocationFull();
1857 bool fFound = false;
1858 for (MediaList::iterator itCached = pllCached->begin();
1859 itCached != pllCached->end();
1860 ++itCached)
1861 {
1862 Medium *pCached = *itCached;
1863 const Utf8Str strLocationCached = pCached->i_getLocationFull();
1864 if (strLocationNew == strLocationCached)
1865 {
1866 fFound = true;
1867 break;
1868 }
1869 }
1870
1871 if (!fFound)
1872 pllCached->push_back(pNew);
1873 }
1874 }
1875 }
1876
1877 // return cached list to caller
1878 pll = pllCached;
1879
1880 // Make sure the media tree lock is released before llNew is cleared,
1881 // as this usually triggers calls to uninit().
1882 treeLock.release();
1883
1884 llNew.clear();
1885
1886 treeLock.acquire();
1887
1888 return rc;
1889}
1890
1891/**
1892 * Goes through the list of host drives that would be returned by getDrives()
1893 * and looks for a host drive with the given UUID. If found, it sets pMedium
1894 * to that drive; otherwise returns VBOX_E_OBJECT_NOT_FOUND.
1895 *
1896 * @param mediumType Must be DeviceType_DVD or DeviceType_Floppy.
1897 * @param uuid Medium UUID of host drive to look for.
1898 * @param fRefresh Whether to refresh the host drives list (see getDrives())
1899 * @param pMedium Medium object, if found...
1900 * @return VBOX_E_OBJECT_NOT_FOUND if not found, or S_OK if found, or errors from getDrives().
1901 */
1902HRESULT Host::i_findHostDriveById(DeviceType_T mediumType,
1903 const Guid &uuid,
1904 bool fRefresh,
1905 ComObjPtr<Medium> &pMedium)
1906{
1907 MediaList *pllMedia;
1908
1909 AutoWriteLock treeLock(m->pParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
1910 HRESULT rc = i_getDrives(mediumType, fRefresh, pllMedia, treeLock);
1911 if (SUCCEEDED(rc))
1912 {
1913 for (MediaList::iterator it = pllMedia->begin();
1914 it != pllMedia->end();
1915 ++it)
1916 {
1917 Medium *pThis = *it;
1918 AutoCaller mediumCaller(pThis);
1919 AutoReadLock mediumLock(pThis COMMA_LOCKVAL_SRC_POS);
1920 if (pThis->i_getId() == uuid)
1921 {
1922 pMedium = pThis;
1923 return S_OK;
1924 }
1925 }
1926 }
1927
1928 return VBOX_E_OBJECT_NOT_FOUND;
1929}
1930
1931/**
1932 * Goes through the list of host drives that would be returned by getDrives()
1933 * and looks for a host drive with the given name. If found, it sets pMedium
1934 * to that drive; otherwise returns VBOX_E_OBJECT_NOT_FOUND.
1935 *
1936 * @param mediumType Must be DeviceType_DVD or DeviceType_Floppy.
1937 * @param strLocationFull Name (path) of host drive to look for.
1938 * @param fRefresh Whether to refresh the host drives list (see getDrives())
1939 * @param pMedium Medium object, if found
1940 * @return VBOX_E_OBJECT_NOT_FOUND if not found, or S_OK if found, or errors from getDrives().
1941 */
1942HRESULT Host::i_findHostDriveByName(DeviceType_T mediumType,
1943 const Utf8Str &strLocationFull,
1944 bool fRefresh,
1945 ComObjPtr<Medium> &pMedium)
1946{
1947 MediaList *pllMedia;
1948
1949 AutoWriteLock treeLock(m->pParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
1950 HRESULT rc = i_getDrives(mediumType, fRefresh, pllMedia, treeLock);
1951 if (SUCCEEDED(rc))
1952 {
1953 for (MediaList::iterator it = pllMedia->begin();
1954 it != pllMedia->end();
1955 ++it)
1956 {
1957 Medium *pThis = *it;
1958 AutoCaller mediumCaller(pThis);
1959 AutoReadLock mediumLock(pThis COMMA_LOCKVAL_SRC_POS);
1960 if (pThis->i_getLocationFull() == strLocationFull)
1961 {
1962 pMedium = pThis;
1963 return S_OK;
1964 }
1965 }
1966 }
1967
1968 return VBOX_E_OBJECT_NOT_FOUND;
1969}
1970
1971/**
1972 * Goes through the list of host drives that would be returned by getDrives()
1973 * and looks for a host drive with the given name, location or ID. If found,
1974 * it sets pMedium to that drive; otherwise returns VBOX_E_OBJECT_NOT_FOUND.
1975 *
1976 * @param mediumType Must be DeviceType_DVD or DeviceType_Floppy.
1977 * @param strNameOrId Name or full location or UUID of host drive to look for.
1978 * @param pMedium Medium object, if found...
1979 * @return VBOX_E_OBJECT_NOT_FOUND if not found, or S_OK if found, or errors from getDrives().
1980 */
1981HRESULT Host::i_findHostDriveByNameOrId(DeviceType_T mediumType,
1982 const Utf8Str &strNameOrId,
1983 ComObjPtr<Medium> &pMedium)
1984{
1985 AutoWriteLock wlock(m->pParent->i_getMediaTreeLockHandle() COMMA_LOCKVAL_SRC_POS);
1986
1987 Guid uuid(strNameOrId);
1988 if (uuid.isValid() && !uuid.isZero())
1989 return i_findHostDriveById(mediumType, uuid, true /* fRefresh */, pMedium);
1990
1991 // string is not a syntactically valid UUID: try a name then
1992 return i_findHostDriveByName(mediumType, strNameOrId, true /* fRefresh */, pMedium);
1993}
1994
1995/**
1996 * Called from getDrives() to build the DVD drives list.
1997 * @param pll
1998 * @return
1999 */
2000HRESULT Host::i_buildDVDDrivesList(MediaList &list)
2001{
2002 HRESULT rc = S_OK;
2003
2004 Assert(m->pParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
2005
2006 try
2007 {
2008#if defined(RT_OS_WINDOWS)
2009 int sz = GetLogicalDriveStrings(0, NULL);
2010 TCHAR *hostDrives = new TCHAR[sz+1];
2011 GetLogicalDriveStrings(sz, hostDrives);
2012 wchar_t driveName[3] = { '?', ':', '\0' };
2013 TCHAR *p = hostDrives;
2014 do
2015 {
2016 if (GetDriveType(p) == DRIVE_CDROM)
2017 {
2018 driveName[0] = *p;
2019 ComObjPtr<Medium> hostDVDDriveObj;
2020 hostDVDDriveObj.createObject();
2021 hostDVDDriveObj->init(m->pParent, DeviceType_DVD, Bstr(driveName));
2022 list.push_back(hostDVDDriveObj);
2023 }
2024 p += _tcslen(p) + 1;
2025 }
2026 while (*p);
2027 delete[] hostDrives;
2028
2029#elif defined(RT_OS_SOLARIS)
2030# ifdef VBOX_USE_LIBHAL
2031 if (!i_getDVDInfoFromHal(list))
2032# endif
2033 {
2034 i_getDVDInfoFromDevTree(list);
2035 }
2036
2037#elif defined(RT_OS_LINUX) || defined(RT_OS_FREEBSD)
2038 if (RT_SUCCESS(m->hostDrives.updateDVDs()))
2039 for (DriveInfoList::const_iterator it = m->hostDrives.DVDBegin();
2040 SUCCEEDED(rc) && it != m->hostDrives.DVDEnd(); ++it)
2041 {
2042 ComObjPtr<Medium> hostDVDDriveObj;
2043 Utf8Str location(it->mDevice);
2044 Utf8Str description(it->mDescription);
2045 if (SUCCEEDED(rc))
2046 rc = hostDVDDriveObj.createObject();
2047 if (SUCCEEDED(rc))
2048 rc = hostDVDDriveObj->init(m->pParent, DeviceType_DVD, location, description);
2049 if (SUCCEEDED(rc))
2050 list.push_back(hostDVDDriveObj);
2051 }
2052#elif defined(RT_OS_DARWIN)
2053 PDARWINDVD cur = DarwinGetDVDDrives();
2054 while (cur)
2055 {
2056 ComObjPtr<Medium> hostDVDDriveObj;
2057 hostDVDDriveObj.createObject();
2058 hostDVDDriveObj->init(m->pParent, DeviceType_DVD, Bstr(cur->szName));
2059 list.push_back(hostDVDDriveObj);
2060
2061 /* next */
2062 void *freeMe = cur;
2063 cur = cur->pNext;
2064 RTMemFree(freeMe);
2065 }
2066#else
2067 /* PORTME */
2068#endif
2069 }
2070 catch(std::bad_alloc &)
2071 {
2072 rc = E_OUTOFMEMORY;
2073 }
2074 return rc;
2075}
2076
2077/**
2078 * Called from getDrives() to build the floppy drives list.
2079 * @param list
2080 * @return
2081 */
2082HRESULT Host::i_buildFloppyDrivesList(MediaList &list)
2083{
2084 HRESULT rc = S_OK;
2085
2086 Assert(m->pParent->i_getMediaTreeLockHandle().isWriteLockOnCurrentThread());
2087
2088 try
2089 {
2090#ifdef RT_OS_WINDOWS
2091 int sz = GetLogicalDriveStrings(0, NULL);
2092 TCHAR *hostDrives = new TCHAR[sz+1];
2093 GetLogicalDriveStrings(sz, hostDrives);
2094 wchar_t driveName[3] = { '?', ':', '\0' };
2095 TCHAR *p = hostDrives;
2096 do
2097 {
2098 if (GetDriveType(p) == DRIVE_REMOVABLE)
2099 {
2100 driveName[0] = *p;
2101 ComObjPtr<Medium> hostFloppyDriveObj;
2102 hostFloppyDriveObj.createObject();
2103 hostFloppyDriveObj->init(m->pParent, DeviceType_Floppy, Bstr(driveName));
2104 list.push_back(hostFloppyDriveObj);
2105 }
2106 p += _tcslen(p) + 1;
2107 }
2108 while (*p);
2109 delete[] hostDrives;
2110#elif defined(RT_OS_LINUX)
2111 if (RT_SUCCESS(m->hostDrives.updateFloppies()))
2112 for (DriveInfoList::const_iterator it = m->hostDrives.FloppyBegin();
2113 SUCCEEDED(rc) && it != m->hostDrives.FloppyEnd(); ++it)
2114 {
2115 ComObjPtr<Medium> hostFloppyDriveObj;
2116 Utf8Str location(it->mDevice);
2117 Utf8Str description(it->mDescription);
2118 if (SUCCEEDED(rc))
2119 rc = hostFloppyDriveObj.createObject();
2120 if (SUCCEEDED(rc))
2121 rc = hostFloppyDriveObj->init(m->pParent, DeviceType_Floppy, location, description);
2122 if (SUCCEEDED(rc))
2123 list.push_back(hostFloppyDriveObj);
2124 }
2125#else
2126 NOREF(list);
2127 /* PORTME */
2128#endif
2129 }
2130 catch(std::bad_alloc &)
2131 {
2132 rc = E_OUTOFMEMORY;
2133 }
2134
2135 return rc;
2136}
2137
2138#ifdef VBOX_WITH_USB
2139USBProxyService* Host::i_usbProxyService()
2140{
2141 return m->pUSBProxyService;
2142}
2143
2144HRESULT Host::i_addChild(HostUSBDeviceFilter *pChild)
2145{
2146 AutoCaller autoCaller(this);
2147 if (FAILED(autoCaller.rc()))
2148 return autoCaller.rc();
2149
2150 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2151
2152 m->llChildren.push_back(pChild);
2153
2154 return S_OK;
2155}
2156
2157HRESULT Host::i_removeChild(HostUSBDeviceFilter *pChild)
2158{
2159 AutoCaller autoCaller(this);
2160 if (FAILED(autoCaller.rc()))
2161 return autoCaller.rc();
2162
2163 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2164
2165 for (USBDeviceFilterList::iterator it = m->llChildren.begin();
2166 it != m->llChildren.end();
2167 ++it)
2168 {
2169 if (*it == pChild)
2170 {
2171 m->llChildren.erase(it);
2172 break;
2173 }
2174 }
2175
2176 return S_OK;
2177}
2178
2179VirtualBox* Host::i_parent()
2180{
2181 return m->pParent;
2182}
2183
2184/**
2185 * Called by setter methods of all USB device filters.
2186 */
2187HRESULT Host::i_onUSBDeviceFilterChange(HostUSBDeviceFilter *aFilter,
2188 BOOL aActiveChanged /* = FALSE */)
2189{
2190 AutoCaller autoCaller(this);
2191 if (FAILED(autoCaller.rc()))
2192 return autoCaller.rc();
2193
2194 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2195
2196 if (aFilter->mInList)
2197 {
2198 if (aActiveChanged)
2199 {
2200 // insert/remove the filter from the proxy
2201 if (aFilter->i_getData().mActive)
2202 {
2203 ComAssertRet(aFilter->i_getId() == NULL, E_FAIL);
2204 aFilter->i_getId() = m->pUSBProxyService->insertFilter(&aFilter->i_getData().mUSBFilter);
2205 }
2206 else
2207 {
2208 ComAssertRet(aFilter->i_getId() != NULL, E_FAIL);
2209 m->pUSBProxyService->removeFilter(aFilter->i_getId());
2210 aFilter->i_getId() = NULL;
2211 }
2212 }
2213 else
2214 {
2215 if (aFilter->i_getData().mActive)
2216 {
2217 // update the filter in the proxy
2218 ComAssertRet(aFilter->i_getId() != NULL, E_FAIL);
2219 m->pUSBProxyService->removeFilter(aFilter->i_getId());
2220 aFilter->i_getId() = m->pUSBProxyService->insertFilter(&aFilter->i_getData().mUSBFilter);
2221 }
2222 }
2223
2224 // save the global settings... yeah, on every single filter property change
2225 // for that we should hold only the VirtualBox lock
2226 alock.release();
2227 AutoWriteLock vboxLock(m->pParent COMMA_LOCKVAL_SRC_POS);
2228 return m->pParent->i_saveSettings();
2229 }
2230
2231 return S_OK;
2232}
2233
2234
2235/**
2236 * Interface for obtaining a copy of the USBDeviceFilterList,
2237 * used by the USBProxyService.
2238 *
2239 * @param aGlobalFilters Where to put the global filter list copy.
2240 * @param aMachines Where to put the machine vector.
2241 */
2242void Host::i_getUSBFilters(Host::USBDeviceFilterList *aGlobalFilters)
2243{
2244 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2245
2246 *aGlobalFilters = m->llUSBDeviceFilters;
2247}
2248
2249#endif /* VBOX_WITH_USB */
2250
2251// private methods
2252////////////////////////////////////////////////////////////////////////////////
2253
2254#if defined(RT_OS_SOLARIS) && defined(VBOX_USE_LIBHAL)
2255
2256/**
2257 * Helper function to get the slice number from a device path
2258 *
2259 * @param pszDevLinkPath Pointer to a device path (/dev/(r)dsk/c7d1t0d0s3 etc.)
2260 * @returns Pointer to the slice portion of the given path.
2261 */
2262static char *solarisGetSliceFromPath(const char *pszDevLinkPath)
2263{
2264 char *pszFound = NULL;
2265 char *pszSlice = strrchr(pszDevLinkPath, 's');
2266 char *pszDisk = strrchr(pszDevLinkPath, 'd');
2267 if (pszSlice && pszSlice > pszDisk)
2268 pszFound = pszSlice;
2269 else
2270 pszFound = pszDisk;
2271
2272 if (pszFound && RT_C_IS_DIGIT(pszFound[1]))
2273 return pszFound;
2274
2275 return NULL;
2276}
2277
2278/**
2279 * Walk device links and returns an allocated path for the first one in the snapshot.
2280 *
2281 * @param DevLink Handle to the device link being walked.
2282 * @param pvArg Opaque data containing the pointer to the path.
2283 * @returns Pointer to an allocated device path string.
2284 */
2285static int solarisWalkDevLink(di_devlink_t DevLink, void *pvArg)
2286{
2287 char **ppszPath = (char **)pvArg;
2288 *ppszPath = strdup(di_devlink_path(DevLink));
2289 return DI_WALK_TERMINATE;
2290}
2291
2292/**
2293 * Walk all devices in the system and enumerate CD/DVD drives.
2294 * @param Node Handle to the current node.
2295 * @param pvArg Opaque data (holds list pointer).
2296 * @returns Solaris specific code whether to continue walking or not.
2297 */
2298static int solarisWalkDeviceNodeForDVD(di_node_t Node, void *pvArg)
2299{
2300 PSOLARISDVD *ppDrives = (PSOLARISDVD *)pvArg;
2301
2302 /*
2303 * Check for "removable-media" or "hotpluggable" instead of "SCSI" so that we also include USB CD-ROMs.
2304 * As unfortunately the Solaris drivers only export these common properties.
2305 */
2306 int *pInt = NULL;
2307 if ( di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "removable-media", &pInt) >= 0
2308 || di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "hotpluggable", &pInt) >= 0)
2309 {
2310 if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "inquiry-device-type", &pInt) > 0
2311 && ( *pInt == DTYPE_RODIRECT /* CDROM */
2312 || *pInt == DTYPE_OPTICAL)) /* Optical Drive */
2313 {
2314 char *pszProduct = NULL;
2315 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "inquiry-product-id", &pszProduct) > 0)
2316 {
2317 char *pszVendor = NULL;
2318 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "inquiry-vendor-id", &pszVendor) > 0)
2319 {
2320 /*
2321 * Found a DVD drive, we need to scan the minor nodes to find the correct
2322 * slice that represents the whole drive. "s2" is always the whole drive for CD/DVDs.
2323 */
2324 int Major = di_driver_major(Node);
2325 di_minor_t Minor = DI_MINOR_NIL;
2326 di_devlink_handle_t DevLink = di_devlink_init(NULL /* name */, 0 /* flags */);
2327 if (DevLink)
2328 {
2329 while ((Minor = di_minor_next(Node, Minor)) != DI_MINOR_NIL)
2330 {
2331 dev_t Dev = di_minor_devt(Minor);
2332 if ( Major != (int)major(Dev)
2333 || di_minor_spectype(Minor) == S_IFBLK
2334 || di_minor_type(Minor) != DDM_MINOR)
2335 {
2336 continue;
2337 }
2338
2339 char *pszMinorPath = di_devfs_minor_path(Minor);
2340 if (!pszMinorPath)
2341 continue;
2342
2343 char *pszDevLinkPath = NULL;
2344 di_devlink_walk(DevLink, NULL, pszMinorPath, DI_PRIMARY_LINK, &pszDevLinkPath, solarisWalkDevLink);
2345 di_devfs_path_free(pszMinorPath);
2346
2347 if (pszDevLinkPath)
2348 {
2349 char *pszSlice = solarisGetSliceFromPath(pszDevLinkPath);
2350 if ( pszSlice && !strcmp(pszSlice, "s2")
2351 && !strncmp(pszDevLinkPath, RT_STR_TUPLE("/dev/rdsk"))) /* We want only raw disks */
2352 {
2353 /*
2354 * We've got a fully qualified DVD drive. Add it to the list.
2355 */
2356 PSOLARISDVD pDrive = (PSOLARISDVD)RTMemAllocZ(sizeof(SOLARISDVD));
2357 if (RT_LIKELY(pDrive))
2358 {
2359 RTStrPrintf(pDrive->szDescription, sizeof(pDrive->szDescription),
2360 "%s %s", pszVendor, pszProduct);
2361 RTStrCopy(pDrive->szRawDiskPath, sizeof(pDrive->szRawDiskPath), pszDevLinkPath);
2362 if (*ppDrives)
2363 pDrive->pNext = *ppDrives;
2364 *ppDrives = pDrive;
2365
2366 /* We're not interested in any of the other slices, stop minor nodes traversal. */
2367 free(pszDevLinkPath);
2368 break;
2369 }
2370 }
2371 free(pszDevLinkPath);
2372 }
2373 }
2374 di_devlink_fini(&DevLink);
2375 }
2376 }
2377 }
2378 }
2379 }
2380 return DI_WALK_CONTINUE;
2381}
2382
2383/**
2384 * Solaris specific function to enumerate CD/DVD drives via the device tree.
2385 * Works on Solaris 10 as well as OpenSolaris without depending on libhal.
2386 */
2387void Host::i_getDVDInfoFromDevTree(std::list<ComObjPtr<Medium> > &list)
2388{
2389 PSOLARISDVD pDrives = NULL;
2390 di_node_t RootNode = di_init("/", DINFOCPYALL);
2391 if (RootNode != DI_NODE_NIL)
2392 di_walk_node(RootNode, DI_WALK_CLDFIRST, &pDrives, solarisWalkDeviceNodeForDVD);
2393
2394 di_fini(RootNode);
2395
2396 while (pDrives)
2397 {
2398 ComObjPtr<Medium> hostDVDDriveObj;
2399 hostDVDDriveObj.createObject();
2400 hostDVDDriveObj->init(m->pParent, DeviceType_DVD, Bstr(pDrives->szRawDiskPath), Bstr(pDrives->szDescription));
2401 list.push_back(hostDVDDriveObj);
2402
2403 void *pvDrive = pDrives;
2404 pDrives = pDrives->pNext;
2405 RTMemFree(pvDrive);
2406 }
2407}
2408
2409/* Solaris hosts, loading libhal at runtime */
2410
2411/**
2412 * Helper function to query the hal subsystem for information about DVD drives attached to the
2413 * system.
2414 *
2415 * @returns true if information was successfully obtained, false otherwise
2416 * @retval list drives found will be attached to this list
2417 */
2418bool Host::i_getDVDInfoFromHal(std::list<ComObjPtr<Medium> > &list)
2419{
2420 bool halSuccess = false;
2421 DBusError dbusError;
2422 if (!gLibHalCheckPresence())
2423 return false;
2424 gDBusErrorInit(&dbusError);
2425 DBusConnection *dbusConnection = gDBusBusGet(DBUS_BUS_SYSTEM, &dbusError);
2426 if (dbusConnection != 0)
2427 {
2428 LibHalContext *halContext = gLibHalCtxNew();
2429 if (halContext != 0)
2430 {
2431 if (gLibHalCtxSetDBusConnection(halContext, dbusConnection))
2432 {
2433 if (gLibHalCtxInit(halContext, &dbusError))
2434 {
2435 int numDevices;
2436 char **halDevices = gLibHalFindDeviceStringMatch(halContext,
2437 "storage.drive_type", "cdrom",
2438 &numDevices, &dbusError);
2439 if (halDevices != 0)
2440 {
2441 /* Hal is installed and working, so if no devices are reported, assume
2442 that there are none. */
2443 halSuccess = true;
2444 for (int i = 0; i < numDevices; i++)
2445 {
2446 char *devNode = gLibHalDeviceGetPropertyString(halContext,
2447 halDevices[i], "block.device", &dbusError);
2448#ifdef RT_OS_SOLARIS
2449 /* The CD/DVD ioctls work only for raw device nodes. */
2450 char *tmp = getfullrawname(devNode);
2451 gLibHalFreeString(devNode);
2452 devNode = tmp;
2453#endif
2454
2455 if (devNode != 0)
2456 {
2457// if (validateDevice(devNode, true))
2458// {
2459 Utf8Str description;
2460 char *vendor, *product;
2461 /* We do not check the error here, as this field may
2462 not even exist. */
2463 vendor = gLibHalDeviceGetPropertyString(halContext,
2464 halDevices[i], "info.vendor", 0);
2465 product = gLibHalDeviceGetPropertyString(halContext,
2466 halDevices[i], "info.product", &dbusError);
2467 if ((product != 0 && product[0] != 0))
2468 {
2469 if ((vendor != 0) && (vendor[0] != 0))
2470 {
2471 description = Utf8StrFmt("%s %s",
2472 vendor, product);
2473 }
2474 else
2475 {
2476 description = product;
2477 }
2478 ComObjPtr<Medium> hostDVDDriveObj;
2479 hostDVDDriveObj.createObject();
2480 hostDVDDriveObj->init(m->pParent, DeviceType_DVD,
2481 Bstr(devNode), Bstr(description));
2482 list.push_back(hostDVDDriveObj);
2483 }
2484 else
2485 {
2486 if (product == 0)
2487 {
2488 LogRel(("Host::COMGETTER(DVDDrives): failed to get property \"info.product\" for device %s. dbus error: %s (%s)\n",
2489 halDevices[i], dbusError.name, dbusError.message));
2490 gDBusErrorFree(&dbusError);
2491 }
2492 ComObjPtr<Medium> hostDVDDriveObj;
2493 hostDVDDriveObj.createObject();
2494 hostDVDDriveObj->init(m->pParent, DeviceType_DVD,
2495 Bstr(devNode));
2496 list.push_back(hostDVDDriveObj);
2497 }
2498 if (vendor != 0)
2499 {
2500 gLibHalFreeString(vendor);
2501 }
2502 if (product != 0)
2503 {
2504 gLibHalFreeString(product);
2505 }
2506// }
2507// else
2508// {
2509// LogRel(("Host::COMGETTER(DVDDrives): failed to validate the block device %s as a DVD drive\n"));
2510// }
2511#ifndef RT_OS_SOLARIS
2512 gLibHalFreeString(devNode);
2513#else
2514 free(devNode);
2515#endif
2516 }
2517 else
2518 {
2519 LogRel(("Host::COMGETTER(DVDDrives): failed to get property \"block.device\" for device %s. dbus error: %s (%s)\n",
2520 halDevices[i], dbusError.name, dbusError.message));
2521 gDBusErrorFree(&dbusError);
2522 }
2523 }
2524 gLibHalFreeStringArray(halDevices);
2525 }
2526 else
2527 {
2528 LogRel(("Host::COMGETTER(DVDDrives): failed to get devices with capability \"storage.cdrom\". dbus error: %s (%s)\n", dbusError.name, dbusError.message));
2529 gDBusErrorFree(&dbusError);
2530 }
2531 if (!gLibHalCtxShutdown(halContext, &dbusError)) /* what now? */
2532 {
2533 LogRel(("Host::COMGETTER(DVDDrives): failed to shutdown the libhal context. dbus error: %s (%s)\n",
2534 dbusError.name, dbusError.message));
2535 gDBusErrorFree(&dbusError);
2536 }
2537 }
2538 else
2539 {
2540 LogRel(("Host::COMGETTER(DVDDrives): failed to initialise libhal context. dbus error: %s (%s)\n",
2541 dbusError.name, dbusError.message));
2542 gDBusErrorFree(&dbusError);
2543 }
2544 gLibHalCtxFree(halContext);
2545 }
2546 else
2547 {
2548 LogRel(("Host::COMGETTER(DVDDrives): failed to set libhal connection to dbus.\n"));
2549 }
2550 }
2551 else
2552 {
2553 LogRel(("Host::COMGETTER(DVDDrives): failed to get a libhal context - out of memory?\n"));
2554 }
2555 gDBusConnectionUnref(dbusConnection);
2556 }
2557 else
2558 {
2559 LogRel(("Host::COMGETTER(DVDDrives): failed to connect to dbus. dbus error: %s (%s)\n",
2560 dbusError.name, dbusError.message));
2561 gDBusErrorFree(&dbusError);
2562 }
2563 return halSuccess;
2564}
2565
2566
2567/**
2568 * Helper function to query the hal subsystem for information about floppy drives attached to the
2569 * system.
2570 *
2571 * @returns true if information was successfully obtained, false otherwise
2572 * @retval list drives found will be attached to this list
2573 */
2574bool Host::i_getFloppyInfoFromHal(std::list< ComObjPtr<Medium> > &list)
2575{
2576 bool halSuccess = false;
2577 DBusError dbusError;
2578 if (!gLibHalCheckPresence())
2579 return false;
2580 gDBusErrorInit(&dbusError);
2581 DBusConnection *dbusConnection = gDBusBusGet(DBUS_BUS_SYSTEM, &dbusError);
2582 if (dbusConnection != 0)
2583 {
2584 LibHalContext *halContext = gLibHalCtxNew();
2585 if (halContext != 0)
2586 {
2587 if (gLibHalCtxSetDBusConnection(halContext, dbusConnection))
2588 {
2589 if (gLibHalCtxInit(halContext, &dbusError))
2590 {
2591 int numDevices;
2592 char **halDevices = gLibHalFindDeviceStringMatch(halContext,
2593 "storage.drive_type", "floppy",
2594 &numDevices, &dbusError);
2595 if (halDevices != 0)
2596 {
2597 /* Hal is installed and working, so if no devices are reported, assume
2598 that there are none. */
2599 halSuccess = true;
2600 for (int i = 0; i < numDevices; i++)
2601 {
2602 char *driveType = gLibHalDeviceGetPropertyString(halContext,
2603 halDevices[i], "storage.drive_type", 0);
2604 if (driveType != 0)
2605 {
2606 if (strcmp(driveType, "floppy") != 0)
2607 {
2608 gLibHalFreeString(driveType);
2609 continue;
2610 }
2611 gLibHalFreeString(driveType);
2612 }
2613 else
2614 {
2615 /* An error occurred. The attribute "storage.drive_type"
2616 probably didn't exist. */
2617 continue;
2618 }
2619 char *devNode = gLibHalDeviceGetPropertyString(halContext,
2620 halDevices[i], "block.device", &dbusError);
2621 if (devNode != 0)
2622 {
2623// if (validateDevice(devNode, false))
2624// {
2625 Utf8Str description;
2626 char *vendor, *product;
2627 /* We do not check the error here, as this field may
2628 not even exist. */
2629 vendor = gLibHalDeviceGetPropertyString(halContext,
2630 halDevices[i], "info.vendor", 0);
2631 product = gLibHalDeviceGetPropertyString(halContext,
2632 halDevices[i], "info.product", &dbusError);
2633 if ((product != 0) && (product[0] != 0))
2634 {
2635 if ((vendor != 0) && (vendor[0] != 0))
2636 {
2637 description = Utf8StrFmt("%s %s",
2638 vendor, product);
2639 }
2640 else
2641 {
2642 description = product;
2643 }
2644 ComObjPtr<Medium> hostFloppyDrive;
2645 hostFloppyDrive.createObject();
2646 hostFloppyDrive->init(m->pParent, DeviceType_DVD,
2647 Bstr(devNode), Bstr(description));
2648 list.push_back(hostFloppyDrive);
2649 }
2650 else
2651 {
2652 if (product == 0)
2653 {
2654 LogRel(("Host::COMGETTER(FloppyDrives): failed to get property \"info.product\" for device %s. dbus error: %s (%s)\n",
2655 halDevices[i], dbusError.name, dbusError.message));
2656 gDBusErrorFree(&dbusError);
2657 }
2658 ComObjPtr<Medium> hostFloppyDrive;
2659 hostFloppyDrive.createObject();
2660 hostFloppyDrive->init(m->pParent, DeviceType_DVD,
2661 Bstr(devNode));
2662 list.push_back(hostFloppyDrive);
2663 }
2664 if (vendor != 0)
2665 {
2666 gLibHalFreeString(vendor);
2667 }
2668 if (product != 0)
2669 {
2670 gLibHalFreeString(product);
2671 }
2672// }
2673// else
2674// {
2675// LogRel(("Host::COMGETTER(FloppyDrives): failed to validate the block device %s as a floppy drive\n"));
2676// }
2677 gLibHalFreeString(devNode);
2678 }
2679 else
2680 {
2681 LogRel(("Host::COMGETTER(FloppyDrives): failed to get property \"block.device\" for device %s. dbus error: %s (%s)\n",
2682 halDevices[i], dbusError.name, dbusError.message));
2683 gDBusErrorFree(&dbusError);
2684 }
2685 }
2686 gLibHalFreeStringArray(halDevices);
2687 }
2688 else
2689 {
2690 LogRel(("Host::COMGETTER(FloppyDrives): failed to get devices with capability \"storage.cdrom\". dbus error: %s (%s)\n", dbusError.name, dbusError.message));
2691 gDBusErrorFree(&dbusError);
2692 }
2693 if (!gLibHalCtxShutdown(halContext, &dbusError)) /* what now? */
2694 {
2695 LogRel(("Host::COMGETTER(FloppyDrives): failed to shutdown the libhal context. dbus error: %s (%s)\n",
2696 dbusError.name, dbusError.message));
2697 gDBusErrorFree(&dbusError);
2698 }
2699 }
2700 else
2701 {
2702 LogRel(("Host::COMGETTER(FloppyDrives): failed to initialise libhal context. dbus error: %s (%s)\n",
2703 dbusError.name, dbusError.message));
2704 gDBusErrorFree(&dbusError);
2705 }
2706 gLibHalCtxFree(halContext);
2707 }
2708 else
2709 {
2710 LogRel(("Host::COMGETTER(FloppyDrives): failed to set libhal connection to dbus.\n"));
2711 }
2712 }
2713 else
2714 {
2715 LogRel(("Host::COMGETTER(FloppyDrives): failed to get a libhal context - out of memory?\n"));
2716 }
2717 gDBusConnectionUnref(dbusConnection);
2718 }
2719 else
2720 {
2721 LogRel(("Host::COMGETTER(FloppyDrives): failed to connect to dbus. dbus error: %s (%s)\n",
2722 dbusError.name, dbusError.message));
2723 gDBusErrorFree(&dbusError);
2724 }
2725 return halSuccess;
2726}
2727#endif /* RT_OS_SOLARIS and VBOX_USE_HAL */
2728
2729/** @todo get rid of dead code below - RT_OS_SOLARIS and RT_OS_LINUX are never both set */
2730#if defined(RT_OS_SOLARIS)
2731
2732/**
2733 * Helper function to parse the given mount file and add found entries
2734 */
2735void Host::i_parseMountTable(char *mountTable, std::list< ComObjPtr<Medium> > &list)
2736{
2737#ifdef RT_OS_LINUX
2738 FILE *mtab = setmntent(mountTable, "r");
2739 if (mtab)
2740 {
2741 struct mntent *mntent;
2742 char *mnt_type;
2743 char *mnt_dev;
2744 char *tmp;
2745 while ((mntent = getmntent(mtab)))
2746 {
2747 mnt_type = (char*)malloc(strlen(mntent->mnt_type) + 1);
2748 mnt_dev = (char*)malloc(strlen(mntent->mnt_fsname) + 1);
2749 strcpy(mnt_type, mntent->mnt_type);
2750 strcpy(mnt_dev, mntent->mnt_fsname);
2751 // supermount fs case
2752 if (strcmp(mnt_type, "supermount") == 0)
2753 {
2754 tmp = strstr(mntent->mnt_opts, "fs=");
2755 if (tmp)
2756 {
2757 free(mnt_type);
2758 mnt_type = strdup(tmp + strlen("fs="));
2759 if (mnt_type)
2760 {
2761 tmp = strchr(mnt_type, ',');
2762 if (tmp)
2763 *tmp = '\0';
2764 }
2765 }
2766 tmp = strstr(mntent->mnt_opts, "dev=");
2767 if (tmp)
2768 {
2769 free(mnt_dev);
2770 mnt_dev = strdup(tmp + strlen("dev="));
2771 if (mnt_dev)
2772 {
2773 tmp = strchr(mnt_dev, ',');
2774 if (tmp)
2775 *tmp = '\0';
2776 }
2777 }
2778 }
2779 // use strstr here to cover things fs types like "udf,iso9660"
2780 if (strstr(mnt_type, "iso9660") == 0)
2781 {
2782 /** @todo check whether we've already got the drive in our list! */
2783 if (i_validateDevice(mnt_dev, true))
2784 {
2785 ComObjPtr<Medium> hostDVDDriveObj;
2786 hostDVDDriveObj.createObject();
2787 hostDVDDriveObj->init(m->pParent, DeviceType_DVD, Bstr(mnt_dev));
2788 list.push_back (hostDVDDriveObj);
2789 }
2790 }
2791 free(mnt_dev);
2792 free(mnt_type);
2793 }
2794 endmntent(mtab);
2795 }
2796#else // RT_OS_SOLARIS
2797 FILE *mntFile = fopen(mountTable, "r");
2798 if (mntFile)
2799 {
2800 struct mnttab mntTab;
2801 while (getmntent(mntFile, &mntTab) == 0)
2802 {
2803 const char *mountName = mntTab.mnt_special;
2804 const char *mountPoint = mntTab.mnt_mountp;
2805 const char *mountFSType = mntTab.mnt_fstype;
2806 if (mountName && mountPoint && mountFSType)
2807 {
2808 // skip devices we are not interested in
2809 if ((*mountName && mountName[0] == '/') && // skip 'fake' devices (like -hosts,
2810 // proc, fd, swap)
2811 (*mountFSType && (strncmp(mountFSType, RT_STR_TUPLE("devfs")) != 0 && // skip devfs
2812 // (i.e. /devices)
2813 strncmp(mountFSType, RT_STR_TUPLE("dev")) != 0 && // skip dev (i.e. /dev)
2814 strncmp(mountFSType, RT_STR_TUPLE("lofs")) != 0))) // skip loop-back file-system (lofs)
2815 {
2816 char *rawDevName = getfullrawname((char *)mountName);
2817 if (i_validateDevice(rawDevName, true))
2818 {
2819 ComObjPtr<Medium> hostDVDDriveObj;
2820 hostDVDDriveObj.createObject();
2821 hostDVDDriveObj->init(m->pParent, DeviceType_DVD, Bstr(rawDevName));
2822 list.push_back(hostDVDDriveObj);
2823 }
2824 free(rawDevName);
2825 }
2826 }
2827 }
2828
2829 fclose(mntFile);
2830 }
2831#endif
2832}
2833
2834/**
2835 * Helper function to check whether the given device node is a valid drive
2836 */
2837bool Host::i_validateDevice(const char *deviceNode, bool isCDROM)
2838{
2839 struct stat statInfo;
2840 bool retValue = false;
2841
2842 // sanity check
2843 if (!deviceNode)
2844 {
2845 return false;
2846 }
2847
2848 // first a simple stat() call
2849 if (stat(deviceNode, &statInfo) < 0)
2850 {
2851 return false;
2852 }
2853 else
2854 {
2855 if (isCDROM)
2856 {
2857 if (S_ISCHR(statInfo.st_mode) || S_ISBLK(statInfo.st_mode))
2858 {
2859 int fileHandle;
2860 // now try to open the device
2861 fileHandle = open(deviceNode, O_RDONLY | O_NONBLOCK, 0);
2862 if (fileHandle >= 0)
2863 {
2864 cdrom_subchnl cdChannelInfo;
2865 cdChannelInfo.cdsc_format = CDROM_MSF;
2866 // this call will finally reveal the whole truth
2867#ifdef RT_OS_LINUX
2868 if ((ioctl(fileHandle, CDROMSUBCHNL, &cdChannelInfo) == 0) ||
2869 (errno == EIO) || (errno == ENOENT) ||
2870 (errno == EINVAL) || (errno == ENOMEDIUM))
2871#else
2872 if ((ioctl(fileHandle, CDROMSUBCHNL, &cdChannelInfo) == 0) ||
2873 (errno == EIO) || (errno == ENOENT) ||
2874 (errno == EINVAL))
2875#endif
2876 {
2877 retValue = true;
2878 }
2879 close(fileHandle);
2880 }
2881 }
2882 } else
2883 {
2884 // floppy case
2885 if (S_ISCHR(statInfo.st_mode) || S_ISBLK(statInfo.st_mode))
2886 {
2887 /// @todo do some more testing, maybe a nice IOCTL!
2888 retValue = true;
2889 }
2890 }
2891 }
2892 return retValue;
2893}
2894#endif // RT_OS_SOLARIS
2895
2896#ifdef VBOX_WITH_USB
2897/**
2898 * Checks for the presence and status of the USB Proxy Service.
2899 * Returns S_OK when the Proxy is present and OK, VBOX_E_HOST_ERROR (as a
2900 * warning) if the proxy service is not available due to the way the host is
2901 * configured (at present, that means that usbfs and hal/DBus are not
2902 * available on a Linux host) or E_FAIL and a corresponding error message
2903 * otherwise. Intended to be used by methods that rely on the Proxy Service
2904 * availability.
2905 *
2906 * @note This method may return a warning result code. It is recommended to use
2907 * MultiError to store the return value.
2908 *
2909 * @note Locks this object for reading.
2910 */
2911HRESULT Host::i_checkUSBProxyService()
2912{
2913 AutoCaller autoCaller(this);
2914 if (FAILED(autoCaller.rc()))
2915 return autoCaller.rc();
2916
2917 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2918
2919 AssertReturn(m->pUSBProxyService, E_FAIL);
2920 if (!m->pUSBProxyService->isActive())
2921 {
2922 /* disable the USB controller completely to avoid assertions if the
2923 * USB proxy service could not start. */
2924
2925 switch (m->pUSBProxyService->getLastError())
2926 {
2927 case VERR_FILE_NOT_FOUND: /** @todo what does this mean? */
2928 return setWarning(E_FAIL,
2929 tr("Could not load the Host USB Proxy Service (VERR_FILE_NOT_FOUND). The service might not be installed on the host computer"));
2930 case VERR_VUSB_USB_DEVICE_PERMISSION:
2931 return setWarning(E_FAIL,
2932 tr("VirtualBox is not currently allowed to access USB devices. You can change this by adding your user to the 'vboxusers' group. Please see the user manual for a more detailed explanation"));
2933 case VERR_VUSB_USBFS_PERMISSION:
2934 return setWarning(E_FAIL,
2935 tr("VirtualBox is not currently allowed to access USB devices. You can change this by allowing your user to access the 'usbfs' folder and files. Please see the user manual for a more detailed explanation"));
2936 case VINF_SUCCESS:
2937 return setWarning(E_FAIL,
2938 tr("The USB Proxy Service has not yet been ported to this host"));
2939 default:
2940 return setWarning(E_FAIL, "%s: %Rrc",
2941 tr("Could not load the Host USB Proxy service"),
2942 m->pUSBProxyService->getLastError());
2943 }
2944 }
2945
2946 return S_OK;
2947}
2948#endif /* VBOX_WITH_USB */
2949
2950HRESULT Host::i_updateNetIfList()
2951{
2952#ifdef VBOX_WITH_HOSTNETIF_API
2953 AssertReturn( getObjectState().getState() == ObjectState::InInit
2954 || isWriteLockOnCurrentThread(), E_FAIL);
2955
2956 HostNetworkInterfaceList list, listCopy;
2957 int rc = NetIfList(list);
2958 if (rc)
2959 {
2960 Log(("Failed to get host network interface list with rc=%Rrc\n", rc));
2961 return E_FAIL;
2962 }
2963 AssertReturn(m->pParent, E_FAIL);
2964 /* Make a copy as the original may be partially destroyed later. */
2965 listCopy = list;
2966 HostNetworkInterfaceList::iterator itOld, itNew;
2967# ifdef VBOX_WITH_RESOURCE_USAGE_API
2968 PerformanceCollector *aCollector = m->pParent->i_performanceCollector();
2969# endif
2970 for (itOld = m->llNetIfs.begin(); itOld != m->llNetIfs.end(); ++itOld)
2971 {
2972 bool fGone = true;
2973 Bstr nameOld;
2974 (*itOld)->COMGETTER(Name)(nameOld.asOutParam());
2975 for (itNew = listCopy.begin(); itNew != listCopy.end(); ++itNew)
2976 {
2977 Bstr nameNew;
2978 (*itNew)->COMGETTER(Name)(nameNew.asOutParam());
2979 if (nameNew == nameOld)
2980 {
2981 fGone = false;
2982 listCopy.erase(itNew);
2983 break;
2984 }
2985 }
2986 if (fGone)
2987 {
2988# ifdef VBOX_WITH_RESOURCE_USAGE_API
2989 (*itOld)->i_unregisterMetrics(aCollector, this);
2990# endif
2991 }
2992 }
2993 /*
2994 * Need to set the references to VirtualBox object in all interface objects
2995 * (see @bugref{6439}).
2996 */
2997 for (itNew = list.begin(); itNew != list.end(); ++itNew)
2998 (*itNew)->i_setVirtualBox(m->pParent);
2999 /* At this point listCopy will contain newly discovered interfaces only. */
3000 for (itNew = listCopy.begin(); itNew != listCopy.end(); ++itNew)
3001 {
3002 HostNetworkInterfaceType_T t;
3003 HRESULT hr = (*itNew)->COMGETTER(InterfaceType)(&t);
3004 if (FAILED(hr))
3005 {
3006 Bstr n;
3007 (*itNew)->COMGETTER(Name)(n.asOutParam());
3008 LogRel(("Host::updateNetIfList: failed to get interface type for %ls\n", n.raw()));
3009 }
3010 else if (t == HostNetworkInterfaceType_Bridged)
3011 {
3012# ifdef VBOX_WITH_RESOURCE_USAGE_API
3013 (*itNew)->i_registerMetrics(aCollector, this);
3014# endif
3015 }
3016 }
3017 m->llNetIfs = list;
3018 return S_OK;
3019#else
3020 return E_NOTIMPL;
3021#endif
3022}
3023
3024#ifdef VBOX_WITH_RESOURCE_USAGE_API
3025
3026void Host::i_registerDiskMetrics(PerformanceCollector *aCollector)
3027{
3028 pm::CollectorHAL *hal = aCollector->getHAL();
3029 /* Create sub metrics */
3030 Utf8StrFmt fsNameBase("FS/{%s}/Usage", "/");
3031 //Utf8StrFmt fsNameBase("Filesystem/[root]/Usage");
3032 pm::SubMetric *fsRootUsageTotal = new pm::SubMetric(fsNameBase + "/Total",
3033 "Root file system size.");
3034 pm::SubMetric *fsRootUsageUsed = new pm::SubMetric(fsNameBase + "/Used",
3035 "Root file system space currently occupied.");
3036 pm::SubMetric *fsRootUsageFree = new pm::SubMetric(fsNameBase + "/Free",
3037 "Root file system space currently empty.");
3038
3039 pm::BaseMetric *fsRootUsage = new pm::HostFilesystemUsage(hal, this,
3040 fsNameBase, "/",
3041 fsRootUsageTotal,
3042 fsRootUsageUsed,
3043 fsRootUsageFree);
3044 aCollector->registerBaseMetric(fsRootUsage);
3045
3046 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageTotal, 0));
3047 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageTotal,
3048 new pm::AggregateAvg()));
3049 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageTotal,
3050 new pm::AggregateMin()));
3051 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageTotal,
3052 new pm::AggregateMax()));
3053
3054 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageUsed, 0));
3055 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageUsed,
3056 new pm::AggregateAvg()));
3057 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageUsed,
3058 new pm::AggregateMin()));
3059 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageUsed,
3060 new pm::AggregateMax()));
3061
3062 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageFree, 0));
3063 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageFree,
3064 new pm::AggregateAvg()));
3065 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageFree,
3066 new pm::AggregateMin()));
3067 aCollector->registerMetric(new pm::Metric(fsRootUsage, fsRootUsageFree,
3068 new pm::AggregateMax()));
3069
3070 /* For now we are concerned with the root file system only. */
3071 pm::DiskList disksUsage, disksLoad;
3072 int rc = hal->getDiskListByFs("/", disksUsage, disksLoad);
3073 if (RT_FAILURE(rc))
3074 return;
3075 pm::DiskList::iterator it;
3076 for (it = disksLoad.begin(); it != disksLoad.end(); ++it)
3077 {
3078 Utf8StrFmt strName("Disk/%s", it->c_str());
3079 pm::SubMetric *fsLoadUtil = new pm::SubMetric(strName + "/Load/Util",
3080 "Percentage of time disk was busy serving I/O requests.");
3081 pm::BaseMetric *fsLoad = new pm::HostDiskLoadRaw(hal, this, strName + "/Load",
3082 *it, fsLoadUtil);
3083 aCollector->registerBaseMetric(fsLoad);
3084
3085 aCollector->registerMetric(new pm::Metric(fsLoad, fsLoadUtil, 0));
3086 aCollector->registerMetric(new pm::Metric(fsLoad, fsLoadUtil,
3087 new pm::AggregateAvg()));
3088 aCollector->registerMetric(new pm::Metric(fsLoad, fsLoadUtil,
3089 new pm::AggregateMin()));
3090 aCollector->registerMetric(new pm::Metric(fsLoad, fsLoadUtil,
3091 new pm::AggregateMax()));
3092 }
3093 for (it = disksUsage.begin(); it != disksUsage.end(); ++it)
3094 {
3095 Utf8StrFmt strName("Disk/%s", it->c_str());
3096 pm::SubMetric *fsUsageTotal = new pm::SubMetric(strName + "/Usage/Total",
3097 "Disk size.");
3098 pm::BaseMetric *fsUsage = new pm::HostDiskUsage(hal, this, strName + "/Usage",
3099 *it, fsUsageTotal);
3100 aCollector->registerBaseMetric(fsUsage);
3101
3102 aCollector->registerMetric(new pm::Metric(fsUsage, fsUsageTotal, 0));
3103 aCollector->registerMetric(new pm::Metric(fsUsage, fsUsageTotal,
3104 new pm::AggregateAvg()));
3105 aCollector->registerMetric(new pm::Metric(fsUsage, fsUsageTotal,
3106 new pm::AggregateMin()));
3107 aCollector->registerMetric(new pm::Metric(fsUsage, fsUsageTotal,
3108 new pm::AggregateMax()));
3109 }
3110}
3111
3112void Host::i_registerMetrics(PerformanceCollector *aCollector)
3113{
3114 pm::CollectorHAL *hal = aCollector->getHAL();
3115 /* Create sub metrics */
3116 pm::SubMetric *cpuLoadUser = new pm::SubMetric("CPU/Load/User",
3117 "Percentage of processor time spent in user mode.");
3118 pm::SubMetric *cpuLoadKernel = new pm::SubMetric("CPU/Load/Kernel",
3119 "Percentage of processor time spent in kernel mode.");
3120 pm::SubMetric *cpuLoadIdle = new pm::SubMetric("CPU/Load/Idle",
3121 "Percentage of processor time spent idling.");
3122 pm::SubMetric *cpuMhzSM = new pm::SubMetric("CPU/MHz",
3123 "Average of current frequency of all processors.");
3124 pm::SubMetric *ramUsageTotal = new pm::SubMetric("RAM/Usage/Total",
3125 "Total physical memory installed.");
3126 pm::SubMetric *ramUsageUsed = new pm::SubMetric("RAM/Usage/Used",
3127 "Physical memory currently occupied.");
3128 pm::SubMetric *ramUsageFree = new pm::SubMetric("RAM/Usage/Free",
3129 "Physical memory currently available to applications.");
3130 pm::SubMetric *ramVMMUsed = new pm::SubMetric("RAM/VMM/Used",
3131 "Total physical memory used by the hypervisor.");
3132 pm::SubMetric *ramVMMFree = new pm::SubMetric("RAM/VMM/Free",
3133 "Total physical memory free inside the hypervisor.");
3134 pm::SubMetric *ramVMMBallooned = new pm::SubMetric("RAM/VMM/Ballooned",
3135 "Total physical memory ballooned by the hypervisor.");
3136 pm::SubMetric *ramVMMShared = new pm::SubMetric("RAM/VMM/Shared",
3137 "Total physical memory shared between VMs.");
3138
3139
3140 /* Create and register base metrics */
3141 pm::BaseMetric *cpuLoad = new pm::HostCpuLoadRaw(hal, this, cpuLoadUser, cpuLoadKernel,
3142 cpuLoadIdle);
3143 aCollector->registerBaseMetric(cpuLoad);
3144 pm::BaseMetric *cpuMhz = new pm::HostCpuMhz(hal, this, cpuMhzSM);
3145 aCollector->registerBaseMetric(cpuMhz);
3146 pm::BaseMetric *ramUsage = new pm::HostRamUsage(hal, this,
3147 ramUsageTotal,
3148 ramUsageUsed,
3149 ramUsageFree);
3150 aCollector->registerBaseMetric(ramUsage);
3151 pm::BaseMetric *ramVmm = new pm::HostRamVmm(aCollector->getGuestManager(), this,
3152 ramVMMUsed,
3153 ramVMMFree,
3154 ramVMMBallooned,
3155 ramVMMShared);
3156 aCollector->registerBaseMetric(ramVmm);
3157
3158 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser, 0));
3159 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
3160 new pm::AggregateAvg()));
3161 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
3162 new pm::AggregateMin()));
3163 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadUser,
3164 new pm::AggregateMax()));
3165
3166 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel, 0));
3167 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
3168 new pm::AggregateAvg()));
3169 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
3170 new pm::AggregateMin()));
3171 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadKernel,
3172 new pm::AggregateMax()));
3173
3174 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadIdle, 0));
3175 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadIdle,
3176 new pm::AggregateAvg()));
3177 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadIdle,
3178 new pm::AggregateMin()));
3179 aCollector->registerMetric(new pm::Metric(cpuLoad, cpuLoadIdle,
3180 new pm::AggregateMax()));
3181
3182 aCollector->registerMetric(new pm::Metric(cpuMhz, cpuMhzSM, 0));
3183 aCollector->registerMetric(new pm::Metric(cpuMhz, cpuMhzSM,
3184 new pm::AggregateAvg()));
3185 aCollector->registerMetric(new pm::Metric(cpuMhz, cpuMhzSM,
3186 new pm::AggregateMin()));
3187 aCollector->registerMetric(new pm::Metric(cpuMhz, cpuMhzSM,
3188 new pm::AggregateMax()));
3189
3190 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageTotal, 0));
3191 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageTotal,
3192 new pm::AggregateAvg()));
3193 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageTotal,
3194 new pm::AggregateMin()));
3195 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageTotal,
3196 new pm::AggregateMax()));
3197
3198 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed, 0));
3199 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
3200 new pm::AggregateAvg()));
3201 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
3202 new pm::AggregateMin()));
3203 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageUsed,
3204 new pm::AggregateMax()));
3205
3206 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageFree, 0));
3207 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageFree,
3208 new pm::AggregateAvg()));
3209 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageFree,
3210 new pm::AggregateMin()));
3211 aCollector->registerMetric(new pm::Metric(ramUsage, ramUsageFree,
3212 new pm::AggregateMax()));
3213
3214 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMUsed, 0));
3215 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMUsed,
3216 new pm::AggregateAvg()));
3217 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMUsed,
3218 new pm::AggregateMin()));
3219 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMUsed,
3220 new pm::AggregateMax()));
3221
3222 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMFree, 0));
3223 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMFree,
3224 new pm::AggregateAvg()));
3225 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMFree,
3226 new pm::AggregateMin()));
3227 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMFree,
3228 new pm::AggregateMax()));
3229
3230 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMBallooned, 0));
3231 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMBallooned,
3232 new pm::AggregateAvg()));
3233 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMBallooned,
3234 new pm::AggregateMin()));
3235 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMBallooned,
3236 new pm::AggregateMax()));
3237
3238 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMShared, 0));
3239 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMShared,
3240 new pm::AggregateAvg()));
3241 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMShared,
3242 new pm::AggregateMin()));
3243 aCollector->registerMetric(new pm::Metric(ramVmm, ramVMMShared,
3244 new pm::AggregateMax()));
3245 i_registerDiskMetrics(aCollector);
3246}
3247
3248void Host::i_unregisterMetrics(PerformanceCollector *aCollector)
3249{
3250 aCollector->unregisterMetricsFor(this);
3251 aCollector->unregisterBaseMetricsFor(this);
3252}
3253
3254#endif /* VBOX_WITH_RESOURCE_USAGE_API */
3255
3256
3257/* static */
3258void Host::i_generateMACAddress(Utf8Str &mac)
3259{
3260 /*
3261 * Our strategy is as follows: the first three bytes are our fixed
3262 * vendor ID (080027). The remaining 3 bytes will be taken from the
3263 * start of a GUID. This is a fairly safe algorithm.
3264 */
3265 Guid guid;
3266 guid.create();
3267 mac = Utf8StrFmt("080027%02X%02X%02X",
3268 guid.raw()->au8[0], guid.raw()->au8[1], guid.raw()->au8[2]);
3269}
3270
3271/* 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