VirtualBox

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

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

Main/HostImpl-solaris: fix another unchecked strdup

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

© 2023 Oracle
ContactPrivacy policyTerms of Use