VirtualBox

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

Last change on this file since 10534 was 10534, checked in by vboxsync, 16 years ago

Backed out Performance API changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 93.3 KB
Line 
1/* $Id: HostImpl.cpp 10534 2008-07-11 15:21:53Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation: Host
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 RT_OS_LINUX
26# include <sys/types.h>
27# include <sys/stat.h>
28# include <unistd.h>
29# include <sys/ioctl.h>
30# include <fcntl.h>
31# include <mntent.h>
32/* bird: This is a hack to work around conflicts between these linux kernel headers
33 * and the GLIBC tcpip headers. They have different declarations of the 4
34 * standard byte order functions. */
35# define _LINUX_BYTEORDER_GENERIC_H
36# include <linux/cdrom.h>
37# ifdef VBOX_USE_LIBHAL
38// # include <libhal.h>
39// /* These are defined by libhal.h and by VBox header files. */
40// # undef TRUE
41// # undef FALSE
42# include "vbox-libhal.h"
43# endif
44# include <errno.h>
45#endif /* RT_OS_LINUX */
46
47#ifdef RT_OS_SOLARIS
48# include <fcntl.h>
49# include <unistd.h>
50# include <stropts.h>
51# include <errno.h>
52# include <limits.h>
53# include <stdio.h>
54# include <sys/types.h>
55# include <sys/stat.h>
56# include <sys/cdio.h>
57# include <sys/dkio.h>
58# include <sys/mnttab.h>
59# include <sys/mntent.h>
60# ifdef VBOX_USE_LIBHAL
61# include "vbox-libhal.h"
62extern "C" char *getfullrawname(char *);
63# endif
64#endif /* RT_OS_SOLARIS */
65
66#ifdef RT_OS_WINDOWS
67# define _WIN32_DCOM
68# include <windows.h>
69# include <shellapi.h>
70# define INITGUID
71# include <guiddef.h>
72# include <devguid.h>
73# include <objbase.h>
74# include <setupapi.h>
75# include <shlobj.h>
76# include <cfgmgr32.h>
77#endif /* RT_OS_WINDOWS */
78
79
80#include "HostImpl.h"
81#include "HostDVDDriveImpl.h"
82#include "HostFloppyDriveImpl.h"
83#ifdef VBOX_WITH_USB
84# include "HostUSBDeviceImpl.h"
85# include "USBDeviceFilterImpl.h"
86# include "USBProxyService.h"
87#endif
88#include "VirtualBoxImpl.h"
89#include "MachineImpl.h"
90#include "Logging.h"
91
92#ifdef RT_OS_DARWIN
93# include "darwin/iokit.h"
94#endif
95
96#ifdef RT_OS_WINDOWS
97# include "HostNetworkInterfaceImpl.h"
98#endif
99
100#include <VBox/usb.h>
101#include <VBox/err.h>
102#include <iprt/string.h>
103/** @todo the following line becomes obsolete after switching to Mp runtime functions */
104#include <iprt/system.h>
105#include <iprt/mp.h>
106#include <iprt/time.h>
107#include <iprt/param.h>
108#include <iprt/env.h>
109#ifdef RT_OS_SOLARIS
110# include <iprt/path.h>
111#endif
112
113#include <stdio.h>
114
115#include <algorithm>
116
117// constructor / destructor
118/////////////////////////////////////////////////////////////////////////////
119
120HRESULT Host::FinalConstruct()
121{
122 return S_OK;
123}
124
125void Host::FinalRelease()
126{
127 if (isReady())
128 uninit();
129}
130
131// public initializer/uninitializer for internal purposes only
132/////////////////////////////////////////////////////////////////////////////
133
134/**
135 * Initializes the host object.
136 *
137 * @returns COM result indicator
138 * @param parent handle of our parent object
139 */
140HRESULT Host::init (VirtualBox *parent)
141{
142 LogFlowThisFunc (("isReady=%d\n", isReady()));
143
144 ComAssertRet (parent, E_INVALIDARG);
145
146 AutoWriteLock alock (this);
147 ComAssertRet (!isReady(), E_UNEXPECTED);
148
149 mParent = parent;
150
151#ifdef VBOX_WITH_USB
152 /*
153 * Create and initialize the USB Proxy Service.
154 */
155# if defined (RT_OS_DARWIN)
156 mUSBProxyService = new USBProxyServiceDarwin (this);
157# elif defined (RT_OS_LINUX)
158 mUSBProxyService = new USBProxyServiceLinux (this);
159# elif defined (RT_OS_OS2)
160 mUSBProxyService = new USBProxyServiceOs2 (this);
161# elif defined (RT_OS_SOLARIS) && 0
162 mUSBProxyService = new USBProxyServiceSolaris (this);
163# elif defined (RT_OS_WINDOWS)
164 mUSBProxyService = new USBProxyServiceWindows (this);
165# else
166 mUSBProxyService = new USBProxyService (this);
167# endif
168 HRESULT hrc = mUSBProxyService->init();
169 AssertComRCReturn(hrc, hrc);
170#endif /* VBOX_WITH_USB */
171
172#ifdef VBOX_WITH_RESOURCE_USAGE_API
173 /* Start resource usage sampler */
174 {
175 int vrc = RTTimerCreate (&mUsageSampler, VBOX_USAGE_SAMPLER_INTERVAL,
176 UsageSamplerCallback, this);
177 AssertMsgRC (vrc, ("Failed to create resource usage sampling "
178 "timer (%Rra)\n", vrc));
179 if (RT_FAILURE (vrc))
180 return E_FAIL;
181 }
182#endif /* VBOX_WITH_RESOURCE_USAGE_API */
183
184 setReady(true);
185 return S_OK;
186}
187
188/**
189 * Uninitializes the host object and sets the ready flag to FALSE.
190 * Called either from FinalRelease() or by the parent when it gets destroyed.
191 */
192void Host::uninit()
193{
194 LogFlowThisFunc (("isReady=%d\n", isReady()));
195
196 AssertReturn (isReady(), (void) 0);
197
198#ifdef VBOX_WITH_USB
199 /* wait for USB proxy service to terminate before we uninit all USB
200 * devices */
201 LogFlowThisFunc (("Stopping USB proxy service...\n"));
202 delete mUSBProxyService;
203 mUSBProxyService = NULL;
204 LogFlowThisFunc (("Done stopping USB proxy service.\n"));
205#endif
206
207 /* uninit all USB device filters still referenced by clients */
208 uninitDependentChildren();
209
210#ifdef VBOX_WITH_USB
211 mUSBDeviceFilters.clear();
212#endif
213
214#ifdef VBOX_WITH_RESOURCE_USAGE_API
215 /* Destroy resource usage sampler */
216 {
217 int vrc = RTTimerDestroy (mUsageSampler);
218 AssertMsgRC (vrc, ("Failed to destroy resource usage "
219 "sampling timer (%Rra)\n", vrc));
220 }
221#endif /* VBOX_WITH_RESOURCE_USAGE_API */
222
223 setReady (FALSE);
224}
225
226// IHost properties
227/////////////////////////////////////////////////////////////////////////////
228
229/**
230 * Returns a list of host DVD drives.
231 *
232 * @returns COM status code
233 * @param drives address of result pointer
234 */
235STDMETHODIMP Host::COMGETTER(DVDDrives) (IHostDVDDriveCollection **drives)
236{
237 if (!drives)
238 return E_POINTER;
239 AutoWriteLock alock (this);
240 CHECK_READY();
241 std::list <ComObjPtr <HostDVDDrive> > list;
242
243#if defined(RT_OS_WINDOWS)
244 int sz = GetLogicalDriveStrings(0, NULL);
245 TCHAR *hostDrives = new TCHAR[sz+1];
246 GetLogicalDriveStrings(sz, hostDrives);
247 wchar_t driveName[3] = { '?', ':', '\0' };
248 TCHAR *p = hostDrives;
249 do
250 {
251 if (GetDriveType(p) == DRIVE_CDROM)
252 {
253 driveName[0] = *p;
254 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
255 hostDVDDriveObj.createObject();
256 hostDVDDriveObj->init (Bstr (driveName));
257 list.push_back (hostDVDDriveObj);
258 }
259 p += _tcslen(p) + 1;
260 }
261 while (*p);
262 delete[] hostDrives;
263
264#elif defined(RT_OS_SOLARIS)
265# ifdef VBOX_USE_LIBHAL
266 if (!getDVDInfoFromHal(list))
267# endif
268 // Not all Solaris versions ship with libhal.
269 // So use a fallback approach similar to Linux.
270 {
271 if (RTEnvGet("VBOX_CDROM"))
272 {
273 char *cdromEnv = strdup(RTEnvGet("VBOX_CDROM"));
274 char *cdromDrive;
275 cdromDrive = strtok(cdromEnv, ":"); /** @todo use strtok_r. */
276 while (cdromDrive)
277 {
278 if (validateDevice(cdromDrive, true))
279 {
280 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
281 hostDVDDriveObj.createObject();
282 hostDVDDriveObj->init (Bstr (cdromDrive));
283 list.push_back (hostDVDDriveObj);
284 }
285 cdromDrive = strtok(NULL, ":");
286 }
287 free(cdromEnv);
288 }
289 else
290 {
291 // this might work on Solaris version older than Nevada.
292 if (validateDevice("/cdrom/cdrom0", true))
293 {
294 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
295 hostDVDDriveObj.createObject();
296 hostDVDDriveObj->init (Bstr ("cdrom/cdrom0"));
297 list.push_back (hostDVDDriveObj);
298 }
299
300 // check the mounted drives
301 parseMountTable(MNTTAB, list);
302 }
303 }
304
305#elif defined(RT_OS_LINUX)
306#ifdef VBOX_USE_LIBHAL
307 if (!getDVDInfoFromHal(list)) /* Playing with #defines in this way is nasty, I know. */
308#endif /* USE_LIBHAL defined */
309 // On Linux without hal, the situation is much more complex. We will take a
310 // heuristical approach and also allow the user to specify a list of host
311 // CDROMs using an environment variable.
312 // The general strategy is to try some known device names and see of they
313 // exist. At last, we'll enumerate the /etc/fstab file (luckily there's an
314 // API to parse it) for CDROM devices. Ok, let's start!
315
316 {
317 if (RTEnvGet("VBOX_CDROM"))
318 {
319 char *cdromEnv = strdupa(RTEnvGet("VBOX_CDROM"));
320 char *cdromDrive;
321 cdromDrive = strtok(cdromEnv, ":"); /** @todo use strtok_r */
322 while (cdromDrive)
323 {
324 if (validateDevice(cdromDrive, true))
325 {
326 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
327 hostDVDDriveObj.createObject();
328 hostDVDDriveObj->init (Bstr (cdromDrive));
329 list.push_back (hostDVDDriveObj);
330 }
331 cdromDrive = strtok(NULL, ":");
332 }
333 }
334 else
335 {
336 // this is a good guess usually
337 if (validateDevice("/dev/cdrom", true))
338 {
339 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
340 hostDVDDriveObj.createObject();
341 hostDVDDriveObj->init (Bstr ("/dev/cdrom"));
342 list.push_back (hostDVDDriveObj);
343 }
344
345 // check the mounted drives
346 parseMountTable((char*)"/etc/mtab", list);
347
348 // check the drives that can be mounted
349 parseMountTable((char*)"/etc/fstab", list);
350 }
351 }
352#elif defined(RT_OS_DARWIN)
353 PDARWINDVD cur = DarwinGetDVDDrives();
354 while (cur)
355 {
356 ComObjPtr<HostDVDDrive> hostDVDDriveObj;
357 hostDVDDriveObj.createObject();
358 hostDVDDriveObj->init(Bstr(cur->szName));
359 list.push_back(hostDVDDriveObj);
360
361 /* next */
362 void *freeMe = cur;
363 cur = cur->pNext;
364 RTMemFree(freeMe);
365 }
366
367#else
368 /* PORTME */
369#endif
370
371 ComObjPtr<HostDVDDriveCollection> collection;
372 collection.createObject();
373 collection->init (list);
374 collection.queryInterfaceTo(drives);
375 return S_OK;
376}
377
378/**
379 * Returns a list of host floppy drives.
380 *
381 * @returns COM status code
382 * @param drives address of result pointer
383 */
384STDMETHODIMP Host::COMGETTER(FloppyDrives) (IHostFloppyDriveCollection **drives)
385{
386 if (!drives)
387 return E_POINTER;
388 AutoWriteLock alock (this);
389 CHECK_READY();
390
391 std::list <ComObjPtr <HostFloppyDrive> > list;
392
393#ifdef RT_OS_WINDOWS
394 int sz = GetLogicalDriveStrings(0, NULL);
395 TCHAR *hostDrives = new TCHAR[sz+1];
396 GetLogicalDriveStrings(sz, hostDrives);
397 wchar_t driveName[3] = { '?', ':', '\0' };
398 TCHAR *p = hostDrives;
399 do
400 {
401 if (GetDriveType(p) == DRIVE_REMOVABLE)
402 {
403 driveName[0] = *p;
404 ComObjPtr <HostFloppyDrive> hostFloppyDriveObj;
405 hostFloppyDriveObj.createObject();
406 hostFloppyDriveObj->init (Bstr (driveName));
407 list.push_back (hostFloppyDriveObj);
408 }
409 p += _tcslen(p) + 1;
410 }
411 while (*p);
412 delete[] hostDrives;
413#elif defined(RT_OS_LINUX)
414#ifdef VBOX_USE_LIBHAL
415 if (!getFloppyInfoFromHal(list)) /* Playing with #defines in this way is nasty, I know. */
416#endif /* USE_LIBHAL defined */
417 // As with the CDROMs, on Linux we have to take a multi-level approach
418 // involving parsing the mount tables. As this is not bulletproof, we'll
419 // give the user the chance to override the detection by an environment
420 // variable and skip the detection.
421
422 {
423 if (RTEnvGet("VBOX_FLOPPY"))
424 {
425 char *floppyEnv = strdupa(RTEnvGet("VBOX_FLOPPY"));
426 char *floppyDrive;
427 floppyDrive = strtok(floppyEnv, ":");
428 while (floppyDrive)
429 {
430 // check if this is an acceptable device
431 if (validateDevice(floppyDrive, false))
432 {
433 ComObjPtr <HostFloppyDrive> hostFloppyDriveObj;
434 hostFloppyDriveObj.createObject();
435 hostFloppyDriveObj->init (Bstr (floppyDrive));
436 list.push_back (hostFloppyDriveObj);
437 }
438 floppyDrive = strtok(NULL, ":");
439 }
440 }
441 else
442 {
443 // we assume that a floppy is always /dev/fd[x] with x from 0 to 7
444 char devName[10];
445 for (int i = 0; i <= 7; i++)
446 {
447 sprintf(devName, "/dev/fd%d", i);
448 if (validateDevice(devName, false))
449 {
450 ComObjPtr <HostFloppyDrive> hostFloppyDriveObj;
451 hostFloppyDriveObj.createObject();
452 hostFloppyDriveObj->init (Bstr (devName));
453 list.push_back (hostFloppyDriveObj);
454 }
455 }
456 }
457 }
458#else
459 /* PORTME */
460#endif
461
462 ComObjPtr<HostFloppyDriveCollection> collection;
463 collection.createObject();
464 collection->init (list);
465 collection.queryInterfaceTo(drives);
466 return S_OK;
467}
468
469#ifdef RT_OS_WINDOWS
470
471static bool IsTAPDevice(const char *guid)
472{
473 HKEY hNetcard;
474 LONG status;
475 DWORD len;
476 int i = 0;
477 bool ret = false;
478
479 status = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &hNetcard);
480 if (status != ERROR_SUCCESS)
481 return false;
482
483 while(true)
484 {
485 char szEnumName[256];
486 char szNetCfgInstanceId[256];
487 DWORD dwKeyType;
488 HKEY hNetCardGUID;
489
490 len = sizeof(szEnumName);
491 status = RegEnumKeyExA(hNetcard, i, szEnumName, &len, NULL, NULL, NULL, NULL);
492 if (status != ERROR_SUCCESS)
493 break;
494
495 status = RegOpenKeyExA(hNetcard, szEnumName, 0, KEY_READ, &hNetCardGUID);
496 if (status == ERROR_SUCCESS)
497 {
498 len = sizeof (szNetCfgInstanceId);
499 status = RegQueryValueExA(hNetCardGUID, "NetCfgInstanceId", NULL, &dwKeyType, (LPBYTE)szNetCfgInstanceId, &len);
500 if (status == ERROR_SUCCESS && dwKeyType == REG_SZ)
501 {
502 char szNetProductName[256];
503 char szNetProviderName[256];
504
505 szNetProductName[0] = 0;
506 len = sizeof(szNetProductName);
507 status = RegQueryValueExA(hNetCardGUID, "ProductName", NULL, &dwKeyType, (LPBYTE)szNetProductName, &len);
508
509 szNetProviderName[0] = 0;
510 len = sizeof(szNetProviderName);
511 status = RegQueryValueExA(hNetCardGUID, "ProviderName", NULL, &dwKeyType, (LPBYTE)szNetProviderName, &len);
512
513 if ( !strcmp(szNetCfgInstanceId, guid)
514 && !strcmp(szNetProductName, "VirtualBox TAP Adapter")
515 && (!strcmp(szNetProviderName, "innotek GmbH") || !strcmp(szNetProviderName, "Sun Microsystems, Inc.")))
516 {
517 ret = true;
518 RegCloseKey(hNetCardGUID);
519 break;
520 }
521 }
522 RegCloseKey(hNetCardGUID);
523 }
524 ++i;
525 }
526
527 RegCloseKey (hNetcard);
528 return ret;
529}
530
531/**
532 * Returns a list of host network interfaces.
533 *
534 * @returns COM status code
535 * @param drives address of result pointer
536 */
537STDMETHODIMP Host::COMGETTER(NetworkInterfaces) (IHostNetworkInterfaceCollection **networkInterfaces)
538{
539 if (!networkInterfaces)
540 return E_POINTER;
541 AutoWriteLock alock (this);
542 CHECK_READY();
543
544 std::list <ComObjPtr <HostNetworkInterface> > list;
545
546 static const char *NetworkKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\"
547 "{4D36E972-E325-11CE-BFC1-08002BE10318}";
548 HKEY hCtrlNet;
549 LONG status;
550 DWORD len;
551 status = RegOpenKeyExA (HKEY_LOCAL_MACHINE, NetworkKey, 0, KEY_READ, &hCtrlNet);
552 if (status != ERROR_SUCCESS)
553 return setError (E_FAIL, tr("Could not open registry key \"%s\""), NetworkKey);
554
555 for (int i = 0;; ++ i)
556 {
557 char szNetworkGUID [256];
558 HKEY hConnection;
559 char szNetworkConnection [256];
560
561 len = sizeof (szNetworkGUID);
562 status = RegEnumKeyExA (hCtrlNet, i, szNetworkGUID, &len, NULL, NULL, NULL, NULL);
563 if (status != ERROR_SUCCESS)
564 break;
565
566 if (!IsTAPDevice(szNetworkGUID))
567 continue;
568
569 RTStrPrintf (szNetworkConnection, sizeof (szNetworkConnection),
570 "%s\\Connection", szNetworkGUID);
571 status = RegOpenKeyExA (hCtrlNet, szNetworkConnection, 0, KEY_READ, &hConnection);
572 if (status == ERROR_SUCCESS)
573 {
574 DWORD dwKeyType;
575 status = RegQueryValueExW (hConnection, TEXT("Name"), NULL,
576 &dwKeyType, NULL, &len);
577 if (status == ERROR_SUCCESS && dwKeyType == REG_SZ)
578 {
579 size_t uniLen = (len + sizeof (OLECHAR) - 1) / sizeof (OLECHAR);
580 Bstr name (uniLen + 1 /* extra zero */);
581 status = RegQueryValueExW (hConnection, TEXT("Name"), NULL,
582 &dwKeyType, (LPBYTE) name.mutableRaw(), &len);
583 if (status == ERROR_SUCCESS)
584 {
585 RTLogPrintf("Connection name %ls\n", name.mutableRaw());
586 /* put a trailing zero, just in case (see MSDN) */
587 name.mutableRaw() [uniLen] = 0;
588 /* create a new object and add it to the list */
589 ComObjPtr <HostNetworkInterface> iface;
590 iface.createObject();
591 /* remove the curly bracket at the end */
592 szNetworkGUID [strlen(szNetworkGUID) - 1] = '\0';
593 if (SUCCEEDED (iface->init (name, Guid (szNetworkGUID + 1))))
594 list.push_back (iface);
595 }
596 }
597 RegCloseKey (hConnection);
598 }
599 }
600 RegCloseKey (hCtrlNet);
601
602 ComObjPtr <HostNetworkInterfaceCollection> collection;
603 collection.createObject();
604 collection->init (list);
605 collection.queryInterfaceTo (networkInterfaces);
606 return S_OK;
607}
608#endif /* RT_OS_WINDOWS */
609
610STDMETHODIMP Host::COMGETTER(USBDevices)(IHostUSBDeviceCollection **aUSBDevices)
611{
612#ifdef VBOX_WITH_USB
613 if (!aUSBDevices)
614 return E_POINTER;
615
616 AutoWriteLock alock (this);
617 CHECK_READY();
618
619 MultiResult rc = checkUSBProxyService();
620 CheckComRCReturnRC (rc);
621
622 return mUSBProxyService->getDeviceCollection (aUSBDevices);
623
624#else
625 /* Note: The GUI depends on this method returning E_NOTIMPL with no
626 * extended error info to indicate that USB is simply not available
627 * (w/o treting it as a failure), for example, as in OSE */
628 return E_NOTIMPL;
629#endif
630}
631
632STDMETHODIMP Host::COMGETTER(USBDeviceFilters) (IHostUSBDeviceFilterCollection **aUSBDeviceFilters)
633{
634#ifdef VBOX_WITH_USB
635 if (!aUSBDeviceFilters)
636 return E_POINTER;
637
638 AutoWriteLock alock (this);
639 CHECK_READY();
640
641 MultiResult rc = checkUSBProxyService();
642 CheckComRCReturnRC (rc);
643
644 ComObjPtr <HostUSBDeviceFilterCollection> collection;
645 collection.createObject();
646 collection->init (mUSBDeviceFilters);
647 collection.queryInterfaceTo (aUSBDeviceFilters);
648
649 return rc;
650#else
651 /* Note: The GUI depends on this method returning E_NOTIMPL with no
652 * extended error info to indicate that USB is simply not available
653 * (w/o treting it as a failure), for example, as in OSE */
654 return E_NOTIMPL;
655#endif
656}
657
658/**
659 * Returns the number of installed logical processors
660 *
661 * @returns COM status code
662 * @param count address of result variable
663 */
664STDMETHODIMP Host::COMGETTER(ProcessorCount)(ULONG *count)
665{
666 if (!count)
667 return E_POINTER;
668 AutoWriteLock alock (this);
669 CHECK_READY();
670 *count = RTSystemProcessorGetCount();
671 /** @todo after implementing the Mp runtime on all platforms replace with
672 * *count = RTMpGetOnlineCount(); */
673 return S_OK;
674}
675
676/**
677 * Returns the (approximate) maximum speed of the given host CPU in MHz
678 *
679 * @returns COM status code
680 * @param cpu id to get info for.
681 * @param speed address of result variable, speed is 0 if unknown or cpuId is invalid.
682 */
683STDMETHODIMP Host::GetProcessorSpeed(ULONG aCpuId, ULONG *speed)
684{
685 if (!speed)
686 return E_POINTER;
687 AutoWriteLock alock (this);
688 CHECK_READY();
689 *speed = RTMpGetMaxFrequency(aCpuId);
690 return S_OK;
691}
692/**
693 * Returns a description string for the host CPU
694 *
695 * @returns COM status code
696 * @param cpu id to get info for.
697 * @param description address of result variable, NULL if known or cpuId is invalid.
698 */
699STDMETHODIMP Host::GetProcessorDescription(ULONG cpuId, BSTR *description)
700{
701 if (!description)
702 return E_POINTER;
703 AutoWriteLock alock (this);
704 CHECK_READY();
705 /** @todo */
706 return E_NOTIMPL;
707}
708
709
710/**
711 * Returns the amount of installed system memory in megabytes
712 *
713 * @returns COM status code
714 * @param size address of result variable
715 */
716STDMETHODIMP Host::COMGETTER(MemorySize)(ULONG *size)
717{
718 if (!size)
719 return E_POINTER;
720 AutoWriteLock alock (this);
721 CHECK_READY();
722 /** @todo */
723 return E_NOTIMPL;
724}
725
726/**
727 * Returns the current system memory free space in megabytes
728 *
729 * @returns COM status code
730 * @param available address of result variable
731 */
732STDMETHODIMP Host::COMGETTER(MemoryAvailable)(ULONG *available)
733{
734 if (!available)
735 return E_POINTER;
736 AutoWriteLock alock (this);
737 CHECK_READY();
738 /** @todo */
739 return E_NOTIMPL;
740}
741
742/**
743 * Returns the name string of the host operating system
744 *
745 * @returns COM status code
746 * @param os address of result variable
747 */
748STDMETHODIMP Host::COMGETTER(OperatingSystem)(BSTR *os)
749{
750 if (!os)
751 return E_POINTER;
752 AutoWriteLock alock (this);
753 CHECK_READY();
754 /** @todo */
755 return E_NOTIMPL;
756}
757
758/**
759 * Returns the version string of the host operating system
760 *
761 * @returns COM status code
762 * @param os address of result variable
763 */
764STDMETHODIMP Host::COMGETTER(OSVersion)(BSTR *version)
765{
766 if (!version)
767 return E_POINTER;
768 AutoWriteLock alock (this);
769 CHECK_READY();
770 /** @todo */
771 return E_NOTIMPL;
772}
773
774/**
775 * Returns the current host time in milliseconds since 1970-01-01 UTC.
776 *
777 * @returns COM status code
778 * @param time address of result variable
779 */
780STDMETHODIMP Host::COMGETTER(UTCTime)(LONG64 *aUTCTime)
781{
782 if (!aUTCTime)
783 return E_POINTER;
784 AutoWriteLock alock (this);
785 CHECK_READY();
786 RTTIMESPEC now;
787 *aUTCTime = RTTimeSpecGetMilli(RTTimeNow(&now));
788 return S_OK;
789}
790
791// IHost methods
792////////////////////////////////////////////////////////////////////////////////
793
794#ifdef RT_OS_WINDOWS
795
796/**
797 * Returns TRUE if the Windows version is 6.0 or greater (i.e. it's Vista and
798 * later OSes) and it has the UAC (User Account Control) feature enabled.
799 */
800static BOOL IsUACEnabled()
801{
802 LONG rc = 0;
803
804 OSVERSIONINFOEX info;
805 ZeroMemory (&info, sizeof (OSVERSIONINFOEX));
806 info.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
807 rc = GetVersionEx ((OSVERSIONINFO *) &info);
808 AssertReturn (rc != 0, FALSE);
809
810 LogFlowFunc (("dwMajorVersion=%d, dwMinorVersion=%d\n",
811 info.dwMajorVersion, info.dwMinorVersion));
812
813 /* we are interested only in Vista (and newer versions...). In all
814 * earlier versions UAC is not present. */
815 if (info.dwMajorVersion < 6)
816 return FALSE;
817
818 /* the default EnableLUA value is 1 (Enabled) */
819 DWORD dwEnableLUA = 1;
820
821 HKEY hKey;
822 rc = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
823 "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System",
824 0, KEY_QUERY_VALUE, &hKey);
825
826 Assert (rc == ERROR_SUCCESS || rc == ERROR_PATH_NOT_FOUND);
827 if (rc == ERROR_SUCCESS)
828 {
829
830 DWORD cbEnableLUA = sizeof (dwEnableLUA);
831 rc = RegQueryValueExA (hKey, "EnableLUA", NULL, NULL,
832 (LPBYTE) &dwEnableLUA, &cbEnableLUA);
833
834 RegCloseKey (hKey);
835
836 Assert (rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND);
837 }
838
839 LogFlowFunc (("rc=%d, dwEnableLUA=%d\n", rc, dwEnableLUA));
840
841 return dwEnableLUA == 1;
842}
843
844struct NetworkInterfaceHelperClientData
845{
846 SVCHlpMsg::Code msgCode;
847 /* for SVCHlpMsg::CreateHostNetworkInterface */
848 Bstr name;
849 ComObjPtr <HostNetworkInterface> iface;
850 /* for SVCHlpMsg::RemoveHostNetworkInterface */
851 Guid guid;
852};
853
854STDMETHODIMP
855Host::CreateHostNetworkInterface (INPTR BSTR aName,
856 IHostNetworkInterface **aHostNetworkInterface,
857 IProgress **aProgress)
858{
859 if (!aName)
860 return E_INVALIDARG;
861 if (!aHostNetworkInterface)
862 return E_POINTER;
863 if (!aProgress)
864 return E_POINTER;
865
866 AutoWriteLock alock (this);
867 CHECK_READY();
868
869 HRESULT rc = S_OK;
870
871 /* first check whether an interface with the given name already exists */
872 {
873 ComPtr <IHostNetworkInterfaceCollection> coll;
874 rc = COMGETTER(NetworkInterfaces) (coll.asOutParam());
875 CheckComRCReturnRC (rc);
876 ComPtr <IHostNetworkInterface> iface;
877 if (SUCCEEDED (coll->FindByName (aName, iface.asOutParam())))
878 return setError (E_FAIL,
879 tr ("Host network interface '%ls' already exists"), aName);
880 }
881
882 /* create a progress object */
883 ComObjPtr <Progress> progress;
884 progress.createObject();
885 rc = progress->init (mParent, static_cast <IHost *> (this),
886 Bstr (tr ("Creating host network interface")),
887 FALSE /* aCancelable */);
888 CheckComRCReturnRC (rc);
889 progress.queryInterfaceTo (aProgress);
890
891 /* create a new uninitialized host interface object */
892 ComObjPtr <HostNetworkInterface> iface;
893 iface.createObject();
894 iface.queryInterfaceTo (aHostNetworkInterface);
895
896 /* create the networkInterfaceHelperClient() argument */
897 std::auto_ptr <NetworkInterfaceHelperClientData>
898 d (new NetworkInterfaceHelperClientData());
899 AssertReturn (d.get(), E_OUTOFMEMORY);
900
901 d->msgCode = SVCHlpMsg::CreateHostNetworkInterface;
902 d->name = aName;
903 d->iface = iface;
904
905 rc = mParent->startSVCHelperClient (
906 IsUACEnabled() == TRUE /* aPrivileged */,
907 networkInterfaceHelperClient,
908 static_cast <void *> (d.get()),
909 progress);
910
911 if (SUCCEEDED (rc))
912 {
913 /* d is now owned by networkInterfaceHelperClient(), so release it */
914 d.release();
915 }
916
917 return rc;
918}
919
920STDMETHODIMP
921Host::RemoveHostNetworkInterface (INPTR GUIDPARAM aId,
922 IHostNetworkInterface **aHostNetworkInterface,
923 IProgress **aProgress)
924{
925 if (!aHostNetworkInterface)
926 return E_POINTER;
927 if (!aProgress)
928 return E_POINTER;
929
930 AutoWriteLock alock (this);
931 CHECK_READY();
932
933 HRESULT rc = S_OK;
934
935 /* first check whether an interface with the given name already exists */
936 {
937 ComPtr <IHostNetworkInterfaceCollection> coll;
938 rc = COMGETTER(NetworkInterfaces) (coll.asOutParam());
939 CheckComRCReturnRC (rc);
940 ComPtr <IHostNetworkInterface> iface;
941 if (FAILED (coll->FindById (aId, iface.asOutParam())))
942 return setError (E_FAIL,
943 tr ("Host network interface with UUID {%Vuuid} does not exist"),
944 Guid (aId).raw());
945
946 /* return the object to be removed to the caller */
947 iface.queryInterfaceTo (aHostNetworkInterface);
948 }
949
950 /* create a progress object */
951 ComObjPtr <Progress> progress;
952 progress.createObject();
953 rc = progress->init (mParent, static_cast <IHost *> (this),
954 Bstr (tr ("Removing host network interface")),
955 FALSE /* aCancelable */);
956 CheckComRCReturnRC (rc);
957 progress.queryInterfaceTo (aProgress);
958
959 /* create the networkInterfaceHelperClient() argument */
960 std::auto_ptr <NetworkInterfaceHelperClientData>
961 d (new NetworkInterfaceHelperClientData());
962 AssertReturn (d.get(), E_OUTOFMEMORY);
963
964 d->msgCode = SVCHlpMsg::RemoveHostNetworkInterface;
965 d->guid = aId;
966
967 rc = mParent->startSVCHelperClient (
968 IsUACEnabled() == TRUE /* aPrivileged */,
969 networkInterfaceHelperClient,
970 static_cast <void *> (d.get()),
971 progress);
972
973 if (SUCCEEDED (rc))
974 {
975 /* d is now owned by networkInterfaceHelperClient(), so release it */
976 d.release();
977 }
978
979 return rc;
980}
981
982#endif /* RT_OS_WINDOWS */
983
984STDMETHODIMP Host::CreateUSBDeviceFilter (INPTR BSTR aName, IHostUSBDeviceFilter **aFilter)
985{
986#ifdef VBOX_WITH_USB
987 if (!aFilter)
988 return E_POINTER;
989
990 if (!aName || *aName == 0)
991 return E_INVALIDARG;
992
993 AutoWriteLock alock (this);
994 CHECK_READY();
995
996 ComObjPtr <HostUSBDeviceFilter> filter;
997 filter.createObject();
998 HRESULT rc = filter->init (this, aName);
999 ComAssertComRCRet (rc, rc);
1000 rc = filter.queryInterfaceTo (aFilter);
1001 AssertComRCReturn (rc, rc);
1002 return S_OK;
1003#else
1004 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1005 * extended error info to indicate that USB is simply not available
1006 * (w/o treting it as a failure), for example, as in OSE */
1007 return E_NOTIMPL;
1008#endif
1009}
1010
1011STDMETHODIMP Host::InsertUSBDeviceFilter (ULONG aPosition, IHostUSBDeviceFilter *aFilter)
1012{
1013#ifdef VBOX_WITH_USB
1014 if (!aFilter)
1015 return E_INVALIDARG;
1016
1017 /* Note: HostUSBDeviceFilter and USBProxyService also uses this lock. */
1018 AutoWriteLock alock (this);
1019 CHECK_READY();
1020
1021 MultiResult rc = checkUSBProxyService();
1022 CheckComRCReturnRC (rc);
1023
1024 ComObjPtr <HostUSBDeviceFilter> filter = getDependentChild (aFilter);
1025 if (!filter)
1026 return setError (E_INVALIDARG,
1027 tr ("The given USB device filter is not created within "
1028 "this VirtualBox instance"));
1029
1030 if (filter->mInList)
1031 return setError (E_INVALIDARG,
1032 tr ("The given USB device filter is already in the list"));
1033
1034 /* iterate to the position... */
1035 USBDeviceFilterList::iterator it = mUSBDeviceFilters.begin();
1036 std::advance (it, aPosition);
1037 /* ...and insert */
1038 mUSBDeviceFilters.insert (it, filter);
1039 filter->mInList = true;
1040
1041 /* notify the proxy (only when the filter is active) */
1042 if (mUSBProxyService->isActive() && filter->data().mActive)
1043 {
1044 ComAssertRet (filter->id() == NULL, E_FAIL);
1045 filter->id() = mUSBProxyService->insertFilter (&filter->data().mUSBFilter);
1046 }
1047
1048 /* save the global settings */
1049 alock.unlock();
1050 return rc = mParent->saveSettings();
1051#else
1052 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1053 * extended error info to indicate that USB is simply not available
1054 * (w/o treting it as a failure), for example, as in OSE */
1055 return E_NOTIMPL;
1056#endif
1057}
1058
1059STDMETHODIMP Host::RemoveUSBDeviceFilter (ULONG aPosition, IHostUSBDeviceFilter **aFilter)
1060{
1061#ifdef VBOX_WITH_USB
1062 if (!aFilter)
1063 return E_POINTER;
1064
1065 /* Note: HostUSBDeviceFilter and USBProxyService also uses this lock. */
1066 AutoWriteLock alock (this);
1067 CHECK_READY();
1068
1069 MultiResult rc = checkUSBProxyService();
1070 CheckComRCReturnRC (rc);
1071
1072 if (!mUSBDeviceFilters.size())
1073 return setError (E_INVALIDARG,
1074 tr ("The USB device filter list is empty"));
1075
1076 if (aPosition >= mUSBDeviceFilters.size())
1077 return setError (E_INVALIDARG,
1078 tr ("Invalid position: %lu (must be in range [0, %lu])"),
1079 aPosition, mUSBDeviceFilters.size() - 1);
1080
1081 ComObjPtr <HostUSBDeviceFilter> filter;
1082 {
1083 /* iterate to the position... */
1084 USBDeviceFilterList::iterator it = mUSBDeviceFilters.begin();
1085 std::advance (it, aPosition);
1086 /* ...get an element from there... */
1087 filter = *it;
1088 /* ...and remove */
1089 filter->mInList = false;
1090 mUSBDeviceFilters.erase (it);
1091 }
1092
1093 filter.queryInterfaceTo (aFilter);
1094
1095 /* notify the proxy (only when the filter is active) */
1096 if (mUSBProxyService->isActive() && filter->data().mActive)
1097 {
1098 ComAssertRet (filter->id() != NULL, E_FAIL);
1099 mUSBProxyService->removeFilter (filter->id());
1100 filter->id() = NULL;
1101 }
1102
1103 /* save the global settings */
1104 alock.unlock();
1105 return rc = mParent->saveSettings();
1106#else
1107 /* Note: The GUI depends on this method returning E_NOTIMPL with no
1108 * extended error info to indicate that USB is simply not available
1109 * (w/o treting it as a failure), for example, as in OSE */
1110 return E_NOTIMPL;
1111#endif
1112}
1113
1114STDMETHODIMP Host::GetProcessorUsage (ULONG *aUser, ULONG *aSystem, ULONG *aIdle)
1115{
1116#ifdef VBOX_WITH_RESOURCE_USAGE_API
1117 if (aUser == NULL || aSystem == NULL || aIdle == NULL)
1118 return E_POINTER;
1119
1120 *aUser = mCpuStats.u32User;
1121 *aSystem = mCpuStats.u32System;
1122 *aIdle = mCpuStats.u32Idle;
1123
1124 return S_OK;
1125#else /* !VBOX_WITH_RESOURCE_USAGE_API */
1126 return E_NOTIMPL;
1127#endif /* !VBOX_WITH_RESOURCE_USAGE_API */
1128}
1129
1130// public methods only for internal purposes
1131////////////////////////////////////////////////////////////////////////////////
1132
1133HRESULT Host::loadSettings (const settings::Key &aGlobal)
1134{
1135 using namespace settings;
1136
1137 AutoWriteLock alock (this);
1138 CHECK_READY();
1139
1140 AssertReturn (!aGlobal.isNull(), E_FAIL);
1141
1142 HRESULT rc = S_OK;
1143
1144#ifdef VBOX_WITH_USB
1145 Key::List filters = aGlobal.key ("USBDeviceFilters").keys ("DeviceFilter");
1146 for (Key::List::const_iterator it = filters.begin();
1147 it != filters.end(); ++ it)
1148 {
1149 Bstr name = (*it).stringValue ("name");
1150 bool active = (*it).value <bool> ("active");
1151
1152 Bstr vendorId = (*it).stringValue ("vendorId");
1153 Bstr productId = (*it).stringValue ("productId");
1154 Bstr revision = (*it).stringValue ("revision");
1155 Bstr manufacturer = (*it).stringValue ("manufacturer");
1156 Bstr product = (*it).stringValue ("product");
1157 Bstr serialNumber = (*it).stringValue ("serialNumber");
1158 Bstr port = (*it).stringValue ("port");
1159
1160 USBDeviceFilterAction_T action;
1161 action = USBDeviceFilterAction_Ignore;
1162 const char *actionStr = (*it).stringValue ("action");
1163 if (strcmp (actionStr, "Ignore") == 0)
1164 action = USBDeviceFilterAction_Ignore;
1165 else
1166 if (strcmp (actionStr, "Hold") == 0)
1167 action = USBDeviceFilterAction_Hold;
1168 else
1169 AssertMsgFailed (("Invalid action: '%s'\n", actionStr));
1170
1171 ComObjPtr <HostUSBDeviceFilter> filterObj;
1172 filterObj.createObject();
1173 rc = filterObj->init (this,
1174 name, active, vendorId, productId, revision,
1175 manufacturer, product, serialNumber, port,
1176 action);
1177 /* error info is set by init() when appropriate */
1178 CheckComRCBreakRC (rc);
1179
1180 mUSBDeviceFilters.push_back (filterObj);
1181 filterObj->mInList = true;
1182
1183 /* notify the proxy (only when the filter is active) */
1184 if (filterObj->data().mActive)
1185 {
1186 HostUSBDeviceFilter *flt = filterObj; /* resolve ambiguity */
1187 flt->id() = mUSBProxyService->insertFilter (&filterObj->data().mUSBFilter);
1188 }
1189 }
1190#endif /* VBOX_WITH_USB */
1191
1192 return rc;
1193}
1194
1195HRESULT Host::saveSettings (settings::Key &aGlobal)
1196{
1197 using namespace settings;
1198
1199 AutoWriteLock alock (this);
1200 CHECK_READY();
1201
1202 ComAssertRet (!aGlobal.isNull(), E_FAIL);
1203
1204#ifdef VBOX_WITH_USB
1205 /* first, delete the entry */
1206 Key filters = aGlobal.findKey ("USBDeviceFilters");
1207 if (!filters.isNull())
1208 filters.zap();
1209 /* then, recreate it */
1210 filters = aGlobal.createKey ("USBDeviceFilters");
1211
1212 USBDeviceFilterList::const_iterator it = mUSBDeviceFilters.begin();
1213 while (it != mUSBDeviceFilters.end())
1214 {
1215 AutoWriteLock filterLock (*it);
1216 const HostUSBDeviceFilter::Data &data = (*it)->data();
1217
1218 Key filter = filters.appendKey ("DeviceFilter");
1219
1220 filter.setValue <Bstr> ("name", data.mName);
1221 filter.setValue <bool> ("active", !!data.mActive);
1222
1223 /* all are optional */
1224 Bstr str;
1225 (*it)->COMGETTER (VendorId) (str.asOutParam());
1226 if (!str.isNull())
1227 filter.setValue <Bstr> ("vendorId", str);
1228
1229 (*it)->COMGETTER (ProductId) (str.asOutParam());
1230 if (!str.isNull())
1231 filter.setValue <Bstr> ("productId", str);
1232
1233 (*it)->COMGETTER (Revision) (str.asOutParam());
1234 if (!str.isNull())
1235 filter.setValue <Bstr> ("revision", str);
1236
1237 (*it)->COMGETTER (Manufacturer) (str.asOutParam());
1238 if (!str.isNull())
1239 filter.setValue <Bstr> ("manufacturer", str);
1240
1241 (*it)->COMGETTER (Product) (str.asOutParam());
1242 if (!str.isNull())
1243 filter.setValue <Bstr> ("product", str);
1244
1245 (*it)->COMGETTER (SerialNumber) (str.asOutParam());
1246 if (!str.isNull())
1247 filter.setValue <Bstr> ("serialNumber", str);
1248
1249 (*it)->COMGETTER (Port) (str.asOutParam());
1250 if (!str.isNull())
1251 filter.setValue <Bstr> ("port", str);
1252
1253 /* action is mandatory */
1254 USBDeviceFilterAction_T action = USBDeviceFilterAction_Null;
1255 (*it)->COMGETTER (Action) (&action);
1256 if (action == USBDeviceFilterAction_Ignore)
1257 filter.setStringValue ("action", "Ignore");
1258 else if (action == USBDeviceFilterAction_Hold)
1259 filter.setStringValue ("action", "Hold");
1260 else
1261 AssertMsgFailed (("Invalid action: %d\n", action));
1262
1263 ++ it;
1264 }
1265#endif /* VBOX_WITH_USB */
1266
1267 return S_OK;
1268}
1269
1270#ifdef VBOX_WITH_USB
1271/**
1272 * Called by setter methods of all USB device filters.
1273 */
1274HRESULT Host::onUSBDeviceFilterChange (HostUSBDeviceFilter *aFilter,
1275 BOOL aActiveChanged /* = FALSE */)
1276{
1277 AutoWriteLock alock (this);
1278 CHECK_READY();
1279
1280 if (aFilter->mInList)
1281 {
1282 if (aActiveChanged)
1283 {
1284 // insert/remove the filter from the proxy
1285 if (aFilter->data().mActive)
1286 {
1287 ComAssertRet (aFilter->id() == NULL, E_FAIL);
1288 aFilter->id() = mUSBProxyService->insertFilter (&aFilter->data().mUSBFilter);
1289 }
1290 else
1291 {
1292 ComAssertRet (aFilter->id() != NULL, E_FAIL);
1293 mUSBProxyService->removeFilter (aFilter->id());
1294 aFilter->id() = NULL;
1295 }
1296 }
1297 else
1298 {
1299 if (aFilter->data().mActive)
1300 {
1301 // update the filter in the proxy
1302 ComAssertRet (aFilter->id() != NULL, E_FAIL);
1303 mUSBProxyService->removeFilter (aFilter->id());
1304 aFilter->id() = mUSBProxyService->insertFilter (&aFilter->data().mUSBFilter);
1305 }
1306 }
1307
1308 // save the global settings... yeah, on every single filter property change
1309 alock.unlock();
1310 return mParent->saveSettings();
1311 }
1312
1313 return S_OK;
1314}
1315
1316
1317/**
1318 * Interface for obtaining a copy of the USBDeviceFilterList,
1319 * used by the USBProxyService.
1320 *
1321 * @param aGlobalFilters Where to put the global filter list copy.
1322 * @param aMachines Where to put the machine vector.
1323 */
1324void Host::getUSBFilters(Host::USBDeviceFilterList *aGlobalFilters, VirtualBox::SessionMachineVector *aMachines)
1325{
1326 AutoWriteLock alock (this);
1327
1328 mParent->getOpenedMachines (*aMachines);
1329 *aGlobalFilters = mUSBDeviceFilters;
1330}
1331
1332#endif /* VBOX_WITH_USB */
1333
1334// private methods
1335////////////////////////////////////////////////////////////////////////////////
1336
1337#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
1338# ifdef VBOX_USE_LIBHAL
1339/**
1340 * Helper function to query the hal subsystem for information about DVD drives attached to the
1341 * system.
1342 *
1343 * @returns true if information was successfully obtained, false otherwise
1344 * @retval list drives found will be attached to this list
1345 */
1346bool Host::getDVDInfoFromHal(std::list <ComObjPtr <HostDVDDrive> > &list)
1347{
1348 bool halSuccess = false;
1349 DBusError dbusError;
1350 if (!gLibHalCheckPresence())
1351 return false;
1352 gDBusErrorInit (&dbusError);
1353 DBusConnection *dbusConnection = gDBusBusGet(DBUS_BUS_SYSTEM, &dbusError);
1354 if (dbusConnection != 0)
1355 {
1356 LibHalContext *halContext = gLibHalCtxNew();
1357 if (halContext != 0)
1358 {
1359 if (gLibHalCtxSetDBusConnection (halContext, dbusConnection))
1360 {
1361 if (gLibHalCtxInit(halContext, &dbusError))
1362 {
1363 int numDevices;
1364 char **halDevices = gLibHalFindDeviceStringMatch(halContext,
1365 "storage.drive_type", "cdrom",
1366 &numDevices, &dbusError);
1367 if (halDevices != 0)
1368 {
1369 /* Hal is installed and working, so if no devices are reported, assume
1370 that there are none. */
1371 halSuccess = true;
1372 for (int i = 0; i < numDevices; i++)
1373 {
1374 char *devNode = gLibHalDeviceGetPropertyString(halContext,
1375 halDevices[i], "block.device", &dbusError);
1376#ifdef RT_OS_SOLARIS
1377 /* The CD/DVD ioctls work only for raw device nodes. */
1378 char *tmp = getfullrawname(devNode);
1379 gLibHalFreeString(devNode);
1380 devNode = tmp;
1381#endif
1382 if (devNode != 0)
1383 {
1384// if (validateDevice(devNode, true))
1385// {
1386 Utf8Str description;
1387 char *vendor, *product;
1388 /* We do not check the error here, as this field may
1389 not even exist. */
1390 vendor = gLibHalDeviceGetPropertyString(halContext,
1391 halDevices[i], "info.vendor", 0);
1392 product = gLibHalDeviceGetPropertyString(halContext,
1393 halDevices[i], "info.product", &dbusError);
1394 if ((product != 0 && product[0] != 0))
1395 {
1396 if ((vendor != 0) && (vendor[0] != 0))
1397 {
1398 description = Utf8StrFmt ("%s %s",
1399 vendor, product);
1400 }
1401 else
1402 {
1403 description = product;
1404 }
1405 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
1406 hostDVDDriveObj.createObject();
1407 hostDVDDriveObj->init (Bstr (devNode),
1408 Bstr (halDevices[i]),
1409 Bstr (description));
1410 list.push_back (hostDVDDriveObj);
1411 }
1412 else
1413 {
1414 if (product == 0)
1415 {
1416 LogRel(("Host::COMGETTER(DVDDrives): failed to get property \"info.product\" for device %s. dbus error: %s (%s)\n",
1417 halDevices[i], dbusError.name, dbusError.message));
1418 gDBusErrorFree(&dbusError);
1419 }
1420 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
1421 hostDVDDriveObj.createObject();
1422 hostDVDDriveObj->init (Bstr (devNode),
1423 Bstr (halDevices[i]));
1424 list.push_back (hostDVDDriveObj);
1425 }
1426 if (vendor != 0)
1427 {
1428 gLibHalFreeString(vendor);
1429 }
1430 if (product != 0)
1431 {
1432 gLibHalFreeString(product);
1433 }
1434// }
1435// else
1436// {
1437// LogRel(("Host::COMGETTER(DVDDrives): failed to validate the block device %s as a DVD drive\n"));
1438// }
1439#ifndef RT_OS_SOLARIS
1440 gLibHalFreeString(devNode);
1441#else
1442 free(devNode);
1443#endif
1444 }
1445 else
1446 {
1447 LogRel(("Host::COMGETTER(DVDDrives): failed to get property \"block.device\" for device %s. dbus error: %s (%s)\n",
1448 halDevices[i], dbusError.name, dbusError.message));
1449 gDBusErrorFree(&dbusError);
1450 }
1451 }
1452 gLibHalFreeStringArray(halDevices);
1453 }
1454 else
1455 {
1456 LogRel(("Host::COMGETTER(DVDDrives): failed to get devices with capability \"storage.cdrom\". dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1457 gDBusErrorFree(&dbusError);
1458 }
1459 if (!gLibHalCtxShutdown(halContext, &dbusError)) /* what now? */
1460 {
1461 LogRel(("Host::COMGETTER(DVDDrives): failed to shutdown the libhal context. dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1462 gDBusErrorFree(&dbusError);
1463 }
1464 }
1465 else
1466 {
1467 LogRel(("Host::COMGETTER(DVDDrives): failed to initialise libhal context. dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1468 gDBusErrorFree(&dbusError);
1469 }
1470 gLibHalCtxFree(halContext);
1471 }
1472 else
1473 {
1474 LogRel(("Host::COMGETTER(DVDDrives): failed to set libhal connection to dbus.\n"));
1475 }
1476 }
1477 else
1478 {
1479 LogRel(("Host::COMGETTER(DVDDrives): failed to get a libhal context - out of memory?\n"));
1480 }
1481 gDBusConnectionUnref(dbusConnection);
1482 }
1483 else
1484 {
1485 LogRel(("Host::COMGETTER(DVDDrives): failed to connect to dbus. dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1486 gDBusErrorFree(&dbusError);
1487 }
1488 return halSuccess;
1489}
1490
1491
1492/**
1493 * Helper function to query the hal subsystem for information about floppy drives attached to the
1494 * system.
1495 *
1496 * @returns true if information was successfully obtained, false otherwise
1497 * @retval list drives found will be attached to this list
1498 */
1499bool Host::getFloppyInfoFromHal(std::list <ComObjPtr <HostFloppyDrive> > &list)
1500{
1501 bool halSuccess = false;
1502 DBusError dbusError;
1503 if (!gLibHalCheckPresence())
1504 return false;
1505 gDBusErrorInit (&dbusError);
1506 DBusConnection *dbusConnection = gDBusBusGet(DBUS_BUS_SYSTEM, &dbusError);
1507 if (dbusConnection != 0)
1508 {
1509 LibHalContext *halContext = gLibHalCtxNew();
1510 if (halContext != 0)
1511 {
1512 if (gLibHalCtxSetDBusConnection (halContext, dbusConnection))
1513 {
1514 if (gLibHalCtxInit(halContext, &dbusError))
1515 {
1516 int numDevices;
1517 char **halDevices = gLibHalFindDeviceStringMatch(halContext,
1518 "storage.drive_type", "floppy",
1519 &numDevices, &dbusError);
1520 if (halDevices != 0)
1521 {
1522 /* Hal is installed and working, so if no devices are reported, assume
1523 that there are none. */
1524 halSuccess = true;
1525 for (int i = 0; i < numDevices; i++)
1526 {
1527 char *driveType = gLibHalDeviceGetPropertyString(halContext,
1528 halDevices[i], "storage.drive_type", 0);
1529 if (driveType != 0)
1530 {
1531 if (strcmp(driveType, "floppy") != 0)
1532 {
1533 gLibHalFreeString(driveType);
1534 continue;
1535 }
1536 gLibHalFreeString(driveType);
1537 }
1538 else
1539 {
1540 /* An error occurred. The attribute "storage.drive_type"
1541 probably didn't exist. */
1542 continue;
1543 }
1544 char *devNode = gLibHalDeviceGetPropertyString(halContext,
1545 halDevices[i], "block.device", &dbusError);
1546 if (devNode != 0)
1547 {
1548// if (validateDevice(devNode, false))
1549// {
1550 Utf8Str description;
1551 char *vendor, *product;
1552 /* We do not check the error here, as this field may
1553 not even exist. */
1554 vendor = gLibHalDeviceGetPropertyString(halContext,
1555 halDevices[i], "info.vendor", 0);
1556 product = gLibHalDeviceGetPropertyString(halContext,
1557 halDevices[i], "info.product", &dbusError);
1558 if ((product != 0) && (product[0] != 0))
1559 {
1560 if ((vendor != 0) && (vendor[0] != 0))
1561 {
1562 description = Utf8StrFmt ("%s %s",
1563 vendor, product);
1564 }
1565 else
1566 {
1567 description = product;
1568 }
1569 ComObjPtr <HostFloppyDrive> hostFloppyDrive;
1570 hostFloppyDrive.createObject();
1571 hostFloppyDrive->init (Bstr (devNode),
1572 Bstr (halDevices[i]),
1573 Bstr (description));
1574 list.push_back (hostFloppyDrive);
1575 }
1576 else
1577 {
1578 if (product == 0)
1579 {
1580 LogRel(("Host::COMGETTER(FloppyDrives): failed to get property \"info.product\" for device %s. dbus error: %s (%s)\n",
1581 halDevices[i], dbusError.name, dbusError.message));
1582 gDBusErrorFree(&dbusError);
1583 }
1584 ComObjPtr <HostFloppyDrive> hostFloppyDrive;
1585 hostFloppyDrive.createObject();
1586 hostFloppyDrive->init (Bstr (devNode),
1587 Bstr (halDevices[i]));
1588 list.push_back (hostFloppyDrive);
1589 }
1590 if (vendor != 0)
1591 {
1592 gLibHalFreeString(vendor);
1593 }
1594 if (product != 0)
1595 {
1596 gLibHalFreeString(product);
1597 }
1598// }
1599// else
1600// {
1601// LogRel(("Host::COMGETTER(FloppyDrives): failed to validate the block device %s as a floppy drive\n"));
1602// }
1603 gLibHalFreeString(devNode);
1604 }
1605 else
1606 {
1607 LogRel(("Host::COMGETTER(FloppyDrives): failed to get property \"block.device\" for device %s. dbus error: %s (%s)\n",
1608 halDevices[i], dbusError.name, dbusError.message));
1609 gDBusErrorFree(&dbusError);
1610 }
1611 }
1612 gLibHalFreeStringArray(halDevices);
1613 }
1614 else
1615 {
1616 LogRel(("Host::COMGETTER(FloppyDrives): failed to get devices with capability \"storage.cdrom\". dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1617 gDBusErrorFree(&dbusError);
1618 }
1619 if (!gLibHalCtxShutdown(halContext, &dbusError)) /* what now? */
1620 {
1621 LogRel(("Host::COMGETTER(FloppyDrives): failed to shutdown the libhal context. dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1622 gDBusErrorFree(&dbusError);
1623 }
1624 }
1625 else
1626 {
1627 LogRel(("Host::COMGETTER(FloppyDrives): failed to initialise libhal context. dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1628 gDBusErrorFree(&dbusError);
1629 }
1630 gLibHalCtxFree(halContext);
1631 }
1632 else
1633 {
1634 LogRel(("Host::COMGETTER(FloppyDrives): failed to set libhal connection to dbus.\n"));
1635 }
1636 }
1637 else
1638 {
1639 LogRel(("Host::COMGETTER(FloppyDrives): failed to get a libhal context - out of memory?\n"));
1640 }
1641 gDBusConnectionUnref(dbusConnection);
1642 }
1643 else
1644 {
1645 LogRel(("Host::COMGETTER(FloppyDrives): failed to connect to dbus. dbus error: %s (%s)\n", dbusError.name, dbusError.message));
1646 gDBusErrorFree(&dbusError);
1647 }
1648 return halSuccess;
1649}
1650# endif /* VBOX_USE_HAL defined */
1651
1652/**
1653 * Helper function to parse the given mount file and add found entries
1654 */
1655void Host::parseMountTable(char *mountTable, std::list <ComObjPtr <HostDVDDrive> > &list)
1656{
1657#ifdef RT_OS_LINUX
1658 FILE *mtab = setmntent(mountTable, "r");
1659 if (mtab)
1660 {
1661 struct mntent *mntent;
1662 char *mnt_type;
1663 char *mnt_dev;
1664 char *tmp;
1665 while ((mntent = getmntent(mtab)))
1666 {
1667 mnt_type = (char*)malloc(strlen(mntent->mnt_type) + 1);
1668 mnt_dev = (char*)malloc(strlen(mntent->mnt_fsname) + 1);
1669 strcpy(mnt_type, mntent->mnt_type);
1670 strcpy(mnt_dev, mntent->mnt_fsname);
1671 // supermount fs case
1672 if (strcmp(mnt_type, "supermount") == 0)
1673 {
1674 tmp = strstr(mntent->mnt_opts, "fs=");
1675 if (tmp)
1676 {
1677 free(mnt_type);
1678 mnt_type = strdup(tmp + strlen("fs="));
1679 if (mnt_type)
1680 {
1681 tmp = strchr(mnt_type, ',');
1682 if (tmp)
1683 *tmp = '\0';
1684 }
1685 }
1686 tmp = strstr(mntent->mnt_opts, "dev=");
1687 if (tmp)
1688 {
1689 free(mnt_dev);
1690 mnt_dev = strdup(tmp + strlen("dev="));
1691 if (mnt_dev)
1692 {
1693 tmp = strchr(mnt_dev, ',');
1694 if (tmp)
1695 *tmp = '\0';
1696 }
1697 }
1698 }
1699 // use strstr here to cover things fs types like "udf,iso9660"
1700 if (strstr(mnt_type, "iso9660") == 0)
1701 {
1702 /** @todo check whether we've already got the drive in our list! */
1703 if (validateDevice(mnt_dev, true))
1704 {
1705 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
1706 hostDVDDriveObj.createObject();
1707 hostDVDDriveObj->init (Bstr (mnt_dev));
1708 list.push_back (hostDVDDriveObj);
1709 }
1710 }
1711 free(mnt_dev);
1712 free(mnt_type);
1713 }
1714 endmntent(mtab);
1715 }
1716#else // RT_OS_SOLARIS
1717 FILE *mntFile = fopen(mountTable, "r");
1718 if (mntFile)
1719 {
1720 struct mnttab mntTab;
1721 while (getmntent(mntFile, &mntTab) == 0)
1722 {
1723 char *mountName = strdup(mntTab.mnt_special);
1724 char *mountPoint = strdup(mntTab.mnt_mountp);
1725 char *mountFSType = strdup(mntTab.mnt_fstype);
1726
1727 // skip devices we are not interested in
1728 if ((*mountName && mountName[0] == '/') && // skip 'fake' devices (like -hosts, proc, fd, swap)
1729 (*mountFSType && (strcmp(mountFSType, "devfs") != 0 && // skip devfs (i.e. /devices)
1730 strcmp(mountFSType, "dev") != 0 && // skip dev (i.e. /dev)
1731 strcmp(mountFSType, "lofs") != 0)) && // skip loop-back file-system (lofs)
1732 (*mountPoint && strcmp(mountPoint, "/") != 0)) // skip point '/' (Can CD/DVD be mounted at '/' ???)
1733 {
1734 char *rawDevName = getfullrawname(mountName);
1735 if (validateDevice(rawDevName, true))
1736 {
1737 ComObjPtr <HostDVDDrive> hostDVDDriveObj;
1738 hostDVDDriveObj.createObject();
1739 hostDVDDriveObj->init (Bstr (rawDevName));
1740 list.push_back (hostDVDDriveObj);
1741 }
1742 free(rawDevName);
1743 }
1744
1745 free(mountName);
1746 free(mountPoint);
1747 free(mountFSType);
1748 }
1749
1750 fclose(mntFile);
1751 }
1752#endif
1753}
1754
1755/**
1756 * Helper function to check whether the given device node is a valid drive
1757 */
1758bool Host::validateDevice(const char *deviceNode, bool isCDROM)
1759{
1760 struct stat statInfo;
1761 bool retValue = false;
1762
1763 // sanity check
1764 if (!deviceNode)
1765 {
1766 return false;
1767 }
1768
1769 // first a simple stat() call
1770 if (stat(deviceNode, &statInfo) < 0)
1771 {
1772 return false;
1773 } else
1774 {
1775 if (isCDROM)
1776 {
1777 if (S_ISCHR(statInfo.st_mode) || S_ISBLK(statInfo.st_mode))
1778 {
1779 int fileHandle;
1780 // now try to open the device
1781 fileHandle = open(deviceNode, O_RDONLY | O_NONBLOCK, 0);
1782 if (fileHandle >= 0)
1783 {
1784 cdrom_subchnl cdChannelInfo;
1785 cdChannelInfo.cdsc_format = CDROM_MSF;
1786 // this call will finally reveal the whole truth
1787#ifdef RT_OS_LINUX
1788 if ((ioctl(fileHandle, CDROMSUBCHNL, &cdChannelInfo) == 0) ||
1789 (errno == EIO) || (errno == ENOENT) ||
1790 (errno == EINVAL) || (errno == ENOMEDIUM))
1791#else
1792 if ((ioctl(fileHandle, CDROMSUBCHNL, &cdChannelInfo) == 0) ||
1793 (errno == EIO) || (errno == ENOENT) ||
1794 (errno == EINVAL))
1795#endif
1796 {
1797 retValue = true;
1798 }
1799 close(fileHandle);
1800 }
1801 }
1802 } else
1803 {
1804 // floppy case
1805 if (S_ISCHR(statInfo.st_mode) || S_ISBLK(statInfo.st_mode))
1806 {
1807 /// @todo do some more testing, maybe a nice IOCTL!
1808 retValue = true;
1809 }
1810 }
1811 }
1812 return retValue;
1813}
1814#endif // RT_OS_LINUX || RT_OS_SOLARIS
1815
1816#ifdef VBOX_WITH_USB
1817/**
1818 * Checks for the presense and status of the USB Proxy Service.
1819 * Returns S_OK when the Proxy is present and OK, or E_FAIL and a
1820 * corresponding error message otherwise. Intended to be used by methods
1821 * that rely on the Proxy Service availability.
1822 *
1823 * @note This method may return a warning result code. It is recommended to use
1824 * MultiError to store the return value.
1825 *
1826 * @note Locks this object for reading.
1827 */
1828HRESULT Host::checkUSBProxyService()
1829{
1830 AutoWriteLock alock (this);
1831 CHECK_READY();
1832
1833 AssertReturn (mUSBProxyService, E_FAIL);
1834 if (!mUSBProxyService->isActive())
1835 {
1836 /* disable the USB controller completely to avoid assertions if the
1837 * USB proxy service could not start. */
1838
1839 if (mUSBProxyService->getLastError() == VERR_FILE_NOT_FOUND)
1840 return setWarning (E_FAIL,
1841 tr ("Could not load the Host USB Proxy Service (%Vrc). "
1842 "The service might be not installed on the host computer"),
1843 mUSBProxyService->getLastError());
1844 if (mUSBProxyService->getLastError() == VINF_SUCCESS)
1845 return setWarning (E_FAIL,
1846 tr ("The USB Proxy Service has not yet been ported to this host"));
1847 return setWarning (E_FAIL,
1848 tr ("Could not load the Host USB Proxy service (%Vrc)"),
1849 mUSBProxyService->getLastError());
1850 }
1851
1852 return S_OK;
1853}
1854#endif /* VBOX_WITH_USB */
1855
1856#ifdef RT_OS_WINDOWS
1857
1858/* The original source of the VBoxTAP adapter creation/destruction code has the following copyright */
1859/*
1860 Copyright 2004 by the Massachusetts Institute of Technology
1861
1862 All rights reserved.
1863
1864 Permission to use, copy, modify, and distribute this software and its
1865 documentation for any purpose and without fee is hereby granted,
1866 provided that the above copyright notice appear in all copies and that
1867 both that copyright notice and this permission notice appear in
1868 supporting documentation, and that the name of the Massachusetts
1869 Institute of Technology (M.I.T.) not be used in advertising or publicity
1870 pertaining to distribution of the software without specific, written
1871 prior permission.
1872
1873 M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
1874 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
1875 M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
1876 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
1877 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
1878 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1879 SOFTWARE.
1880*/
1881
1882
1883#define NETSHELL_LIBRARY _T("netshell.dll")
1884
1885/**
1886 * Use the IShellFolder API to rename the connection.
1887 */
1888static HRESULT rename_shellfolder (PCWSTR wGuid, PCWSTR wNewName)
1889{
1890 /* This is the GUID for the network connections folder. It is constant.
1891 * {7007ACC7-3202-11D1-AAD2-00805FC1270E} */
1892 const GUID CLSID_NetworkConnections = {
1893 0x7007ACC7, 0x3202, 0x11D1, {
1894 0xAA, 0xD2, 0x00, 0x80, 0x5F, 0xC1, 0x27, 0x0E
1895 }
1896 };
1897
1898 LPITEMIDLIST pidl = NULL;
1899 IShellFolder *pShellFolder = NULL;
1900 HRESULT hr;
1901
1902 /* Build the display name in the form "::{GUID}". */
1903 if (wcslen (wGuid) >= MAX_PATH)
1904 return E_INVALIDARG;
1905 WCHAR szAdapterGuid[MAX_PATH + 2] = {0};
1906 swprintf (szAdapterGuid, L"::%ls", wGuid);
1907
1908 /* Create an instance of the network connections folder. */
1909 hr = CoCreateInstance (CLSID_NetworkConnections, NULL,
1910 CLSCTX_INPROC_SERVER, IID_IShellFolder,
1911 reinterpret_cast <LPVOID *> (&pShellFolder));
1912 /* Parse the display name. */
1913 if (SUCCEEDED (hr))
1914 {
1915 hr = pShellFolder->ParseDisplayName (NULL, NULL, szAdapterGuid, NULL,
1916 &pidl, NULL);
1917 }
1918 if (SUCCEEDED (hr))
1919 {
1920 hr = pShellFolder->SetNameOf (NULL, pidl, wNewName, SHGDN_NORMAL,
1921 &pidl);
1922 }
1923
1924 CoTaskMemFree (pidl);
1925
1926 if (pShellFolder)
1927 pShellFolder->Release();
1928
1929 return hr;
1930}
1931
1932extern "C" HRESULT RenameConnection (PCWSTR GuidString, PCWSTR NewName)
1933{
1934 typedef HRESULT (WINAPI *lpHrRenameConnection) (const GUID *, PCWSTR);
1935 lpHrRenameConnection RenameConnectionFunc = NULL;
1936 HRESULT status;
1937
1938 /* First try the IShellFolder interface, which was unimplemented
1939 * for the network connections folder before XP. */
1940 status = rename_shellfolder (GuidString, NewName);
1941 if (status == E_NOTIMPL)
1942 {
1943/** @todo that code doesn't seem to work! */
1944 /* The IShellFolder interface is not implemented on this platform.
1945 * Try the (undocumented) HrRenameConnection API in the netshell
1946 * library. */
1947 CLSID clsid;
1948 HINSTANCE hNetShell;
1949 status = CLSIDFromString ((LPOLESTR) GuidString, &clsid);
1950 if (FAILED(status))
1951 return E_FAIL;
1952 hNetShell = LoadLibrary (NETSHELL_LIBRARY);
1953 if (hNetShell == NULL)
1954 return E_FAIL;
1955 RenameConnectionFunc =
1956 (lpHrRenameConnection) GetProcAddress (hNetShell,
1957 "HrRenameConnection");
1958 if (RenameConnectionFunc == NULL)
1959 {
1960 FreeLibrary (hNetShell);
1961 return E_FAIL;
1962 }
1963 status = RenameConnectionFunc (&clsid, NewName);
1964 FreeLibrary (hNetShell);
1965 }
1966 if (FAILED (status))
1967 return status;
1968
1969 return S_OK;
1970}
1971
1972#define DRIVERHWID _T("vboxtap")
1973
1974#define SetErrBreak(strAndArgs) \
1975 if (1) { \
1976 aErrMsg = Utf8StrFmt strAndArgs; vrc = VERR_GENERAL_FAILURE; break; \
1977 } else do {} while (0)
1978
1979/* static */
1980int Host::createNetworkInterface (SVCHlpClient *aClient,
1981 const Utf8Str &aName,
1982 Guid &aGUID, Utf8Str &aErrMsg)
1983{
1984 LogFlowFuncEnter();
1985 LogFlowFunc (("Network connection name = '%s'\n", aName.raw()));
1986
1987 AssertReturn (aClient, VERR_INVALID_POINTER);
1988 AssertReturn (!aName.isNull(), VERR_INVALID_PARAMETER);
1989
1990 int vrc = VINF_SUCCESS;
1991
1992 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
1993 SP_DEVINFO_DATA DeviceInfoData;
1994 DWORD ret = 0;
1995 BOOL found = FALSE;
1996 BOOL registered = FALSE;
1997 BOOL destroyList = FALSE;
1998 TCHAR pCfgGuidString [50];
1999
2000 do
2001 {
2002 BOOL ok;
2003 GUID netGuid;
2004 SP_DRVINFO_DATA DriverInfoData;
2005 SP_DEVINSTALL_PARAMS DeviceInstallParams;
2006 TCHAR className [MAX_PATH];
2007 DWORD index = 0;
2008 PSP_DRVINFO_DETAIL_DATA pDriverInfoDetail;
2009 /* for our purposes, 2k buffer is more
2010 * than enough to obtain the hardware ID
2011 * of the VBoxTAP driver. */
2012 DWORD detailBuf [2048];
2013
2014 HKEY hkey = NULL;
2015 DWORD cbSize;
2016 DWORD dwValueType;
2017
2018 /* initialize the structure size */
2019 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
2020 DriverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
2021
2022 /* copy the net class GUID */
2023 memcpy(&netGuid, &GUID_DEVCLASS_NET, sizeof(GUID_DEVCLASS_NET));
2024
2025 /* create an empty device info set associated with the net class GUID */
2026 hDeviceInfo = SetupDiCreateDeviceInfoList (&netGuid, NULL);
2027 if (hDeviceInfo == INVALID_HANDLE_VALUE)
2028 SetErrBreak (("SetupDiCreateDeviceInfoList failed (0x%08X)",
2029 GetLastError()));
2030
2031 /* get the class name from GUID */
2032 ok = SetupDiClassNameFromGuid (&netGuid, className, MAX_PATH, NULL);
2033 if (!ok)
2034 SetErrBreak (("SetupDiClassNameFromGuid failed (0x%08X)",
2035 GetLastError()));
2036
2037 /* create a device info element and add the new device instance
2038 * key to registry */
2039 ok = SetupDiCreateDeviceInfo (hDeviceInfo, className, &netGuid, NULL, NULL,
2040 DICD_GENERATE_ID, &DeviceInfoData);
2041 if (!ok)
2042 SetErrBreak (("SetupDiCreateDeviceInfo failed (0x%08X)",
2043 GetLastError()));
2044
2045 /* select the newly created device info to be the currently
2046 selected member */
2047 ok = SetupDiSetSelectedDevice (hDeviceInfo, &DeviceInfoData);
2048 if (!ok)
2049 SetErrBreak (("SetupDiSetSelectedDevice failed (0x%08X)",
2050 GetLastError()));
2051
2052 /* build a list of class drivers */
2053 ok = SetupDiBuildDriverInfoList (hDeviceInfo, &DeviceInfoData,
2054 SPDIT_CLASSDRIVER);
2055 if (!ok)
2056 SetErrBreak (("SetupDiBuildDriverInfoList failed (0x%08X)",
2057 GetLastError()));
2058
2059 destroyList = TRUE;
2060
2061 /* enumerate the driver info list */
2062 while (TRUE)
2063 {
2064 BOOL ret;
2065
2066 ret = SetupDiEnumDriverInfo (hDeviceInfo, &DeviceInfoData,
2067 SPDIT_CLASSDRIVER, index, &DriverInfoData);
2068
2069 /* if the function failed and GetLastError() returned
2070 * ERROR_NO_MORE_ITEMS, then we have reached the end of the
2071 * list. Othewise there was something wrong with this
2072 * particular driver. */
2073 if (!ret)
2074 {
2075 if(GetLastError() == ERROR_NO_MORE_ITEMS)
2076 break;
2077 else
2078 {
2079 index++;
2080 continue;
2081 }
2082 }
2083
2084 pDriverInfoDetail = (PSP_DRVINFO_DETAIL_DATA) detailBuf;
2085 pDriverInfoDetail->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
2086
2087 /* if we successfully find the hardware ID and it turns out to
2088 * be the one for the loopback driver, then we are done. */
2089 if (SetupDiGetDriverInfoDetail (hDeviceInfo,
2090 &DeviceInfoData,
2091 &DriverInfoData,
2092 pDriverInfoDetail,
2093 sizeof (detailBuf),
2094 NULL))
2095 {
2096 TCHAR * t;
2097
2098 /* pDriverInfoDetail->HardwareID is a MULTISZ string. Go through the
2099 * whole list and see if there is a match somewhere. */
2100 t = pDriverInfoDetail->HardwareID;
2101 while (t && *t && t < (TCHAR *) &detailBuf [sizeof(detailBuf) / sizeof (detailBuf[0])])
2102 {
2103 if (!_tcsicmp(t, DRIVERHWID))
2104 break;
2105
2106 t += _tcslen(t) + 1;
2107 }
2108
2109 if (t && *t && t < (TCHAR *) &detailBuf [sizeof(detailBuf) / sizeof (detailBuf[0])])
2110 {
2111 found = TRUE;
2112 break;
2113 }
2114 }
2115
2116 index ++;
2117 }
2118
2119 if (!found)
2120 SetErrBreak ((tr ("Could not find Host Interface Networking driver! "
2121 "Please reinstall")));
2122
2123 /* set the loopback driver to be the currently selected */
2124 ok = SetupDiSetSelectedDriver (hDeviceInfo, &DeviceInfoData,
2125 &DriverInfoData);
2126 if (!ok)
2127 SetErrBreak (("SetupDiSetSelectedDriver failed (0x%08X)",
2128 GetLastError()));
2129
2130 /* register the phantom device to prepare for install */
2131 ok = SetupDiCallClassInstaller (DIF_REGISTERDEVICE, hDeviceInfo,
2132 &DeviceInfoData);
2133 if (!ok)
2134 SetErrBreak (("SetupDiCallClassInstaller failed (0x%08X)",
2135 GetLastError()));
2136
2137 /* registered, but remove if errors occur in the following code */
2138 registered = TRUE;
2139
2140 /* ask the installer if we can install the device */
2141 ok = SetupDiCallClassInstaller (DIF_ALLOW_INSTALL, hDeviceInfo,
2142 &DeviceInfoData);
2143 if (!ok)
2144 {
2145 if (GetLastError() != ERROR_DI_DO_DEFAULT)
2146 SetErrBreak (("SetupDiCallClassInstaller (DIF_ALLOW_INSTALL) failed (0x%08X)",
2147 GetLastError()));
2148 /* that's fine */
2149 }
2150
2151 /* install the files first */
2152 ok = SetupDiCallClassInstaller (DIF_INSTALLDEVICEFILES, hDeviceInfo,
2153 &DeviceInfoData);
2154 if (!ok)
2155 SetErrBreak (("SetupDiCallClassInstaller (DIF_INSTALLDEVICEFILES) failed (0x%08X)",
2156 GetLastError()));
2157
2158 /* get the device install parameters and disable filecopy */
2159 DeviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
2160 ok = SetupDiGetDeviceInstallParams (hDeviceInfo, &DeviceInfoData,
2161 &DeviceInstallParams);
2162 if (ok)
2163 {
2164 DeviceInstallParams.Flags |= DI_NOFILECOPY;
2165 ok = SetupDiSetDeviceInstallParams (hDeviceInfo, &DeviceInfoData,
2166 &DeviceInstallParams);
2167 if (!ok)
2168 SetErrBreak (("SetupDiSetDeviceInstallParams failed (0x%08X)",
2169 GetLastError()));
2170 }
2171
2172 /*
2173 * Register any device-specific co-installers for this device,
2174 */
2175
2176 ok = SetupDiCallClassInstaller (DIF_REGISTER_COINSTALLERS,
2177 hDeviceInfo,
2178 &DeviceInfoData);
2179 if (!ok)
2180 SetErrBreak (("SetupDiCallClassInstaller (DIF_REGISTER_COINSTALLERS) failed (0x%08X)",
2181 GetLastError()));
2182
2183 /*
2184 * install any installer-specified interfaces.
2185 * and then do the real install
2186 */
2187 ok = SetupDiCallClassInstaller (DIF_INSTALLINTERFACES,
2188 hDeviceInfo,
2189 &DeviceInfoData);
2190 if (!ok)
2191 SetErrBreak (("SetupDiCallClassInstaller (DIF_INSTALLINTERFACES) failed (0x%08X)",
2192 GetLastError()));
2193
2194 ok = SetupDiCallClassInstaller (DIF_INSTALLDEVICE,
2195 hDeviceInfo,
2196 &DeviceInfoData);
2197 if (!ok)
2198 SetErrBreak (("SetupDiCallClassInstaller (DIF_INSTALLDEVICE) failed (0x%08X)",
2199 GetLastError()));
2200
2201 /* Figure out NetCfgInstanceId */
2202 hkey = SetupDiOpenDevRegKey (hDeviceInfo,
2203 &DeviceInfoData,
2204 DICS_FLAG_GLOBAL,
2205 0,
2206 DIREG_DRV,
2207 KEY_READ);
2208 if (hkey == INVALID_HANDLE_VALUE)
2209 SetErrBreak (("SetupDiOpenDevRegKey failed (0x%08X)",
2210 GetLastError()));
2211
2212 cbSize = sizeof (pCfgGuidString);
2213 DWORD ret;
2214 ret = RegQueryValueEx (hkey, _T ("NetCfgInstanceId"), NULL,
2215 &dwValueType, (LPBYTE) pCfgGuidString, &cbSize);
2216 RegCloseKey (hkey);
2217
2218 ret = RenameConnection (pCfgGuidString, Bstr (aName));
2219 if (FAILED (ret))
2220 SetErrBreak (("Failed to set interface name (ret=0x%08X, "
2221 "pCfgGuidString='%ls', cbSize=%d)",
2222 ret, pCfgGuidString, cbSize));
2223 }
2224 while (0);
2225
2226 /*
2227 * cleanup
2228 */
2229
2230 if (hDeviceInfo != INVALID_HANDLE_VALUE)
2231 {
2232 /* an error has occured, but the device is registered, we must remove it */
2233 if (ret != 0 && registered)
2234 SetupDiCallClassInstaller (DIF_REMOVE, hDeviceInfo, &DeviceInfoData);
2235
2236 found = SetupDiDeleteDeviceInfo (hDeviceInfo, &DeviceInfoData);
2237
2238 /* destroy the driver info list */
2239 if (destroyList)
2240 SetupDiDestroyDriverInfoList (hDeviceInfo, &DeviceInfoData,
2241 SPDIT_CLASSDRIVER);
2242 /* clean up the device info set */
2243 SetupDiDestroyDeviceInfoList (hDeviceInfo);
2244 }
2245
2246 /* return the network connection GUID on success */
2247 if (VBOX_SUCCESS (vrc))
2248 {
2249 /* remove the curly bracket at the end */
2250 pCfgGuidString [_tcslen (pCfgGuidString) - 1] = '\0';
2251 LogFlowFunc (("Network connection GUID string = {%ls}\n", pCfgGuidString + 1));
2252
2253 aGUID = Guid (Utf8Str (pCfgGuidString + 1));
2254 LogFlowFunc (("Network connection GUID = {%Vuuid}\n", aGUID.raw()));
2255 Assert (!aGUID.isEmpty());
2256 }
2257
2258 LogFlowFunc (("vrc=%Vrc\n", vrc));
2259 LogFlowFuncLeave();
2260 return vrc;
2261}
2262
2263/* static */
2264int Host::removeNetworkInterface (SVCHlpClient *aClient,
2265 const Guid &aGUID,
2266 Utf8Str &aErrMsg)
2267{
2268 LogFlowFuncEnter();
2269 LogFlowFunc (("Network connection GUID = {%Vuuid}\n", aGUID.raw()));
2270
2271 AssertReturn (aClient, VERR_INVALID_POINTER);
2272 AssertReturn (!aGUID.isEmpty(), VERR_INVALID_PARAMETER);
2273
2274 int vrc = VINF_SUCCESS;
2275
2276 do
2277 {
2278 TCHAR lszPnPInstanceId [512] = {0};
2279
2280 /* We have to find the device instance ID through a registry search */
2281
2282 HKEY hkeyNetwork = 0;
2283 HKEY hkeyConnection = 0;
2284
2285 do
2286 {
2287 char strRegLocation [256];
2288 sprintf (strRegLocation,
2289 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
2290 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\{%s}",
2291 aGUID.toString().raw());
2292 LONG status;
2293 status = RegOpenKeyExA (HKEY_LOCAL_MACHINE, strRegLocation, 0,
2294 KEY_READ, &hkeyNetwork);
2295 if ((status != ERROR_SUCCESS) || !hkeyNetwork)
2296 SetErrBreak ((
2297 tr ("Host interface network is not found in registry (%s) [1]"),
2298 strRegLocation));
2299
2300 status = RegOpenKeyExA (hkeyNetwork, "Connection", 0,
2301 KEY_READ, &hkeyConnection);
2302 if ((status != ERROR_SUCCESS) || !hkeyConnection)
2303 SetErrBreak ((
2304 tr ("Host interface network is not found in registry (%s) [2]"),
2305 strRegLocation));
2306
2307 DWORD len = sizeof (lszPnPInstanceId);
2308 DWORD dwKeyType;
2309 status = RegQueryValueExW (hkeyConnection, L"PnPInstanceID", NULL,
2310 &dwKeyType, (LPBYTE) lszPnPInstanceId, &len);
2311 if ((status != ERROR_SUCCESS) || (dwKeyType != REG_SZ))
2312 SetErrBreak ((
2313 tr ("Host interface network is not found in registry (%s) [3]"),
2314 strRegLocation));
2315 }
2316 while (0);
2317
2318 if (hkeyConnection)
2319 RegCloseKey (hkeyConnection);
2320 if (hkeyNetwork)
2321 RegCloseKey (hkeyNetwork);
2322
2323 if (VBOX_FAILURE (vrc))
2324 break;
2325
2326 /*
2327 * Now we are going to enumerate all network devices and
2328 * wait until we encounter the right device instance ID
2329 */
2330
2331 HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
2332
2333 do
2334 {
2335 BOOL ok;
2336 DWORD ret = 0;
2337 GUID netGuid;
2338 SP_DEVINFO_DATA DeviceInfoData;
2339 DWORD index = 0;
2340 BOOL found = FALSE;
2341 DWORD size = 0;
2342
2343 /* initialize the structure size */
2344 DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA);
2345
2346 /* copy the net class GUID */
2347 memcpy (&netGuid, &GUID_DEVCLASS_NET, sizeof (GUID_DEVCLASS_NET));
2348
2349 /* return a device info set contains all installed devices of the Net class */
2350 hDeviceInfo = SetupDiGetClassDevs (&netGuid, NULL, NULL, DIGCF_PRESENT);
2351
2352 if (hDeviceInfo == INVALID_HANDLE_VALUE)
2353 SetErrBreak (("SetupDiGetClassDevs failed (0x%08X)", GetLastError()));
2354
2355 /* enumerate the driver info list */
2356 while (TRUE)
2357 {
2358 TCHAR *deviceHwid;
2359
2360 ok = SetupDiEnumDeviceInfo (hDeviceInfo, index, &DeviceInfoData);
2361
2362 if (!ok)
2363 {
2364 if (GetLastError() == ERROR_NO_MORE_ITEMS)
2365 break;
2366 else
2367 {
2368 index++;
2369 continue;
2370 }
2371 }
2372
2373 /* try to get the hardware ID registry property */
2374 ok = SetupDiGetDeviceRegistryProperty (hDeviceInfo,
2375 &DeviceInfoData,
2376 SPDRP_HARDWAREID,
2377 NULL,
2378 NULL,
2379 0,
2380 &size);
2381 if (!ok)
2382 {
2383 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
2384 {
2385 index++;
2386 continue;
2387 }
2388
2389 deviceHwid = (TCHAR *) malloc (size);
2390 ok = SetupDiGetDeviceRegistryProperty (hDeviceInfo,
2391 &DeviceInfoData,
2392 SPDRP_HARDWAREID,
2393 NULL,
2394 (PBYTE)deviceHwid,
2395 size,
2396 NULL);
2397 if (!ok)
2398 {
2399 free (deviceHwid);
2400 deviceHwid = NULL;
2401 index++;
2402 continue;
2403 }
2404 }
2405 else
2406 {
2407 /* something is wrong. This shouldn't have worked with a NULL buffer */
2408 index++;
2409 continue;
2410 }
2411
2412 for (TCHAR *t = deviceHwid;
2413 t && *t && t < &deviceHwid[size / sizeof(TCHAR)];
2414 t += _tcslen (t) + 1)
2415 {
2416 if (!_tcsicmp (DRIVERHWID, t))
2417 {
2418 /* get the device instance ID */
2419 TCHAR devID [MAX_DEVICE_ID_LEN];
2420 if (CM_Get_Device_ID(DeviceInfoData.DevInst,
2421 devID, MAX_DEVICE_ID_LEN, 0) == CR_SUCCESS)
2422 {
2423 /* compare to what we determined before */
2424 if (wcscmp(devID, lszPnPInstanceId) == 0)
2425 {
2426 found = TRUE;
2427 break;
2428 }
2429 }
2430 }
2431 }
2432
2433 if (deviceHwid)
2434 {
2435 free (deviceHwid);
2436 deviceHwid = NULL;
2437 }
2438
2439 if (found)
2440 break;
2441
2442 index++;
2443 }
2444
2445 if (found == FALSE)
2446 SetErrBreak ((tr ("Host Interface Network driver not found (0x%08X)"),
2447 GetLastError()));
2448
2449 ok = SetupDiSetSelectedDevice (hDeviceInfo, &DeviceInfoData);
2450 if (!ok)
2451 SetErrBreak (("SetupDiSetSelectedDevice failed (0x%08X)",
2452 GetLastError()));
2453
2454 ok = SetupDiCallClassInstaller (DIF_REMOVE, hDeviceInfo, &DeviceInfoData);
2455 if (!ok)
2456 SetErrBreak (("SetupDiCallClassInstaller (DIF_REMOVE) failed (0x%08X)",
2457 GetLastError()));
2458 }
2459 while (0);
2460
2461 /* clean up the device info set */
2462 if (hDeviceInfo != INVALID_HANDLE_VALUE)
2463 SetupDiDestroyDeviceInfoList (hDeviceInfo);
2464
2465 if (VBOX_FAILURE (vrc))
2466 break;
2467 }
2468 while (0);
2469
2470 LogFlowFunc (("vrc=%Vrc\n", vrc));
2471 LogFlowFuncLeave();
2472 return vrc;
2473}
2474
2475#undef SetErrBreak
2476
2477/* static */
2478HRESULT Host::networkInterfaceHelperClient (SVCHlpClient *aClient,
2479 Progress *aProgress,
2480 void *aUser, int *aVrc)
2481{
2482 LogFlowFuncEnter();
2483 LogFlowFunc (("aClient={%p}, aProgress={%p}, aUser={%p}\n",
2484 aClient, aProgress, aUser));
2485
2486 AssertReturn ((aClient == NULL && aProgress == NULL && aVrc == NULL) ||
2487 (aClient != NULL && aProgress != NULL && aVrc != NULL),
2488 E_POINTER);
2489 AssertReturn (aUser, E_POINTER);
2490
2491 std::auto_ptr <NetworkInterfaceHelperClientData>
2492 d (static_cast <NetworkInterfaceHelperClientData *> (aUser));
2493
2494 if (aClient == NULL)
2495 {
2496 /* "cleanup only" mode, just return (it will free aUser) */
2497 return S_OK;
2498 }
2499
2500 HRESULT rc = S_OK;
2501 int vrc = VINF_SUCCESS;
2502
2503 switch (d->msgCode)
2504 {
2505 case SVCHlpMsg::CreateHostNetworkInterface:
2506 {
2507 LogFlowFunc (("CreateHostNetworkInterface:\n"));
2508 LogFlowFunc (("Network connection name = '%ls'\n", d->name.raw()));
2509
2510 /* write message and parameters */
2511 vrc = aClient->write (d->msgCode);
2512 if (VBOX_FAILURE (vrc)) break;
2513 vrc = aClient->write (Utf8Str (d->name));
2514 if (VBOX_FAILURE (vrc)) break;
2515
2516 /* wait for a reply */
2517 bool endLoop = false;
2518 while (!endLoop)
2519 {
2520 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
2521
2522 vrc = aClient->read (reply);
2523 if (VBOX_FAILURE (vrc)) break;
2524
2525 switch (reply)
2526 {
2527 case SVCHlpMsg::CreateHostNetworkInterface_OK:
2528 {
2529 /* read the GUID */
2530 Guid guid;
2531 vrc = aClient->read (guid);
2532 if (VBOX_FAILURE (vrc)) break;
2533
2534 LogFlowFunc (("Network connection GUID = {%Vuuid}\n", guid.raw()));
2535
2536 /* initialize the object returned to the caller by
2537 * CreateHostNetworkInterface() */
2538 rc = d->iface->init (d->name, guid);
2539 endLoop = true;
2540 break;
2541 }
2542 case SVCHlpMsg::Error:
2543 {
2544 /* read the error message */
2545 Utf8Str errMsg;
2546 vrc = aClient->read (errMsg);
2547 if (VBOX_FAILURE (vrc)) break;
2548
2549 rc = setError (E_FAIL, errMsg);
2550 endLoop = true;
2551 break;
2552 }
2553 default:
2554 {
2555 endLoop = true;
2556 ComAssertMsgFailedBreak ((
2557 "Invalid message code %d (%08lX)\n",
2558 reply, reply),
2559 rc = E_FAIL);
2560 }
2561 }
2562 }
2563
2564 break;
2565 }
2566 case SVCHlpMsg::RemoveHostNetworkInterface:
2567 {
2568 LogFlowFunc (("RemoveHostNetworkInterface:\n"));
2569 LogFlowFunc (("Network connection GUID = {%Vuuid}\n", d->guid.raw()));
2570
2571 /* write message and parameters */
2572 vrc = aClient->write (d->msgCode);
2573 if (VBOX_FAILURE (vrc)) break;
2574 vrc = aClient->write (d->guid);
2575 if (VBOX_FAILURE (vrc)) break;
2576
2577 /* wait for a reply */
2578 bool endLoop = false;
2579 while (!endLoop)
2580 {
2581 SVCHlpMsg::Code reply = SVCHlpMsg::Null;
2582
2583 vrc = aClient->read (reply);
2584 if (VBOX_FAILURE (vrc)) break;
2585
2586 switch (reply)
2587 {
2588 case SVCHlpMsg::OK:
2589 {
2590 /* no parameters */
2591 rc = S_OK;
2592 endLoop = true;
2593 break;
2594 }
2595 case SVCHlpMsg::Error:
2596 {
2597 /* read the error message */
2598 Utf8Str errMsg;
2599 vrc = aClient->read (errMsg);
2600 if (VBOX_FAILURE (vrc)) break;
2601
2602 rc = setError (E_FAIL, errMsg);
2603 endLoop = true;
2604 break;
2605 }
2606 default:
2607 {
2608 endLoop = true;
2609 ComAssertMsgFailedBreak ((
2610 "Invalid message code %d (%08lX)\n",
2611 reply, reply),
2612 rc = E_FAIL);
2613 }
2614 }
2615 }
2616
2617 break;
2618 }
2619 default:
2620 ComAssertMsgFailedBreak ((
2621 "Invalid message code %d (%08lX)\n",
2622 d->msgCode, d->msgCode),
2623 rc = E_FAIL);
2624 }
2625
2626 if (aVrc)
2627 *aVrc = vrc;
2628
2629 LogFlowFunc (("rc=0x%08X, vrc=%Vrc\n", rc, vrc));
2630 LogFlowFuncLeave();
2631 return rc;
2632}
2633
2634/* static */
2635int Host::networkInterfaceHelperServer (SVCHlpClient *aClient,
2636 SVCHlpMsg::Code aMsgCode)
2637{
2638 LogFlowFuncEnter();
2639 LogFlowFunc (("aClient={%p}, aMsgCode=%d\n", aClient, aMsgCode));
2640
2641 AssertReturn (aClient, VERR_INVALID_POINTER);
2642
2643 int vrc = VINF_SUCCESS;
2644
2645 switch (aMsgCode)
2646 {
2647 case SVCHlpMsg::CreateHostNetworkInterface:
2648 {
2649 LogFlowFunc (("CreateHostNetworkInterface:\n"));
2650
2651 Utf8Str name;
2652 vrc = aClient->read (name);
2653 if (VBOX_FAILURE (vrc)) break;
2654
2655 Guid guid;
2656 Utf8Str errMsg;
2657 vrc = createNetworkInterface (aClient, name, guid, errMsg);
2658
2659 if (VBOX_SUCCESS (vrc))
2660 {
2661 /* write success followed by GUID */
2662 vrc = aClient->write (SVCHlpMsg::CreateHostNetworkInterface_OK);
2663 if (VBOX_FAILURE (vrc)) break;
2664 vrc = aClient->write (guid);
2665 if (VBOX_FAILURE (vrc)) break;
2666 }
2667 else
2668 {
2669 /* write failure followed by error message */
2670 if (errMsg.isEmpty())
2671 errMsg = Utf8StrFmt ("Unspecified error (%Vrc)", vrc);
2672 vrc = aClient->write (SVCHlpMsg::Error);
2673 if (VBOX_FAILURE (vrc)) break;
2674 vrc = aClient->write (errMsg);
2675 if (VBOX_FAILURE (vrc)) break;
2676 }
2677
2678 break;
2679 }
2680 case SVCHlpMsg::RemoveHostNetworkInterface:
2681 {
2682 LogFlowFunc (("RemoveHostNetworkInterface:\n"));
2683
2684 Guid guid;
2685 vrc = aClient->read (guid);
2686 if (VBOX_FAILURE (vrc)) break;
2687
2688 Utf8Str errMsg;
2689 vrc = removeNetworkInterface (aClient, guid, errMsg);
2690
2691 if (VBOX_SUCCESS (vrc))
2692 {
2693 /* write parameter-less success */
2694 vrc = aClient->write (SVCHlpMsg::OK);
2695 if (VBOX_FAILURE (vrc)) break;
2696 }
2697 else
2698 {
2699 /* write failure followed by error message */
2700 if (errMsg.isEmpty())
2701 errMsg = Utf8StrFmt ("Unspecified error (%Vrc)", vrc);
2702 vrc = aClient->write (SVCHlpMsg::Error);
2703 if (VBOX_FAILURE (vrc)) break;
2704 vrc = aClient->write (errMsg);
2705 if (VBOX_FAILURE (vrc)) break;
2706 }
2707
2708 break;
2709 }
2710 default:
2711 AssertMsgFailedBreakStmt (
2712 ("Invalid message code %d (%08lX)\n", aMsgCode, aMsgCode),
2713 VERR_GENERAL_FAILURE);
2714 }
2715
2716 LogFlowFunc (("vrc=%Vrc\n", vrc));
2717 LogFlowFuncLeave();
2718 return vrc;
2719}
2720
2721#endif /* RT_OS_WINDOWS */
2722
2723#ifdef VBOX_WITH_RESOURCE_USAGE_API
2724
2725/* static */
2726void Host::UsageSamplerCallback (PRTTIMER pTimer, void *pvUser, uint64_t iTick)
2727{
2728 AssertReturnVoid (pvUser != NULL);
2729 static_cast <Host *> (pvUser)->usageSamplerCallback();
2730}
2731
2732void Host::usageSamplerCallback()
2733{
2734 int vrc = RTSystemProcessorGetUsageStats (&mCpuStats);
2735 AssertMsgRC (vrc, ("Failed to get CPU stats (%Rra)\n", vrc));
2736
2737// LogFlowThisFunc (("user=%u%% system=%u%% &mCpuStats=%p\n",
2738// mCpuStats.u32User / 10000000,
2739// mCpuStats.u32System / 10000000, &mCpuStats);
2740// LogFlowThisFunc (("user=%.2f system=%.2f idle=%.2f\n", mCpuStats.u32User/10000000.,
2741// mCpuStats.u32System/10000000., mCpuStats.u32Idle/10000000.);
2742}
2743
2744#endif /* VBOX_WITH_RESOURCE_USAGE_API */
2745
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