VirtualBox

source: vbox/trunk/src/VBox/Main/HostImpl.cpp@ 33595

Last change on this file since 33595 was 33590, checked in by vboxsync, 14 years ago

VRDE: removed VBOX_WITH_VRDP from source code, also some obsolete code removed.

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