VirtualBox

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

Last change on this file since 48366 was 48333, checked in by vboxsync, 11 years ago

HistImpl: clean up

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