HostHardwareLinux.cpp revision f9179d8d10177537b4b8cebe0d8ddbd3113b85a8
/* $Id$ */
/** @file
* Classes for handling hardware detection under Linux. Please feel free to
* expand these to work for other systems (Solaris!) or to add new ones for
* other systems.
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#define LOG_GROUP LOG_GROUP_MAIN
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <HostHardwareLinux.h>
#ifdef VBOX_USB_WITH_DBUS
#endif
#ifdef VBOX_USB_WITH_SYSFS
# ifdef VBOX_USB_WITH_FAM
# include <fam.h>
# endif
#endif
#include <vector>
#include <errno.h>
/******************************************************************************
* Global Variables *
******************************************************************************/
#ifdef TESTCASE
static bool testing() { return true; }
static bool fNoProbe = false;
#else
static bool testing() { return false; }
static bool noProbe() { return false; }
#endif
/******************************************************************************
* Typedefs and Defines *
******************************************************************************/
/** When waiting for hotplug events, we currently restart the wait after at
* most this many milliseconds. */
bool *pfSuccess);
bool *pfSuccess);
#ifdef VBOX_USB_WITH_SYSFS
# ifdef VBOX_USB_WITH_FAM
# endif
# ifdef VBOX_USB_WITH_DBUS
/* These must be extern to be usable in the RTMemAutoPtr template */
/*
static int halFindDeviceStringMatchVector (DBusConnection *pConnection,
const char *pszKey,
const char *pszValue,
std::vector<iprt::MiniString> *pMatches);
*/
const char **papszKeys, char **papszValues,
/*
static int halGetPropertyStringsVector (DBusConnection *pConnection,
const char *pszUdi, size_t cProps,
const char **papszKeys,
std::vector<iprt::MiniString> *pMatches,
bool *pfMatches, bool *pfSuccess);
*/
# endif /* VBOX_USB_WITH_DBUS */
#endif /* VBOX_USB_WITH_SYSFS */
/** Find the length of a string, ignoring trailing non-ascii or control
* characters */
{
cch = i;
return cch + 1;
}
/**
* Get the name of a floppy drive according to the Linux floppy driver.
* @returns true on success, false if the name was not available (i.e. the
* device was not readible, or the file name wasn't a PC floppy
* device)
* @param pcszNode the path to the device node for the device
* @param Number the Linux floppy driver number for the drive. Required.
* @param pszName where to store the name retreived
*/
{
AssertPtrReturn(pcszNode, false);
AssertPtrReturn(pszName, false);
int rc = RTFileOpen(&File, pcszNode, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK);
if (RT_SUCCESS(rc))
{
int rcIoCtl;
return true;
}
return false;
}
/**
* Create a UDI and a description for a floppy drive based on a number and the
* driver's name for it. We deliberately return an ugly sequence of
* characters as the description rather than an English language string to
* avoid translation issues.
*
* @returns true if we know the device to be valid, false otherwise
* @param pcszName the floppy driver name for the device (optional)
* @param Number the number of the floppy (0 to 3 on FDC 0, 4 to 7 on
* FDC 1)
* @param pszDesc where to store the device description (optional)
* @param cchDesc the size of the buffer in @a pszDesc
* @param pszUdi where to store the device UDI (optional)
* @param cchUdi the size of the buffer in @a pszUdi
*/
{
if (pcszName)
{
const char *pcszSize;
switch(pcszName[0])
{
case 'd': case 'q': case 'h':
pcszSize = "5.25\"";
break;
case 'D': case 'H': case 'E': case 'u':
pcszSize = "3.5\"";
break;
default:
pcszSize = "(unknown)";
}
if (pszDesc)
}
else
{
if (pszDesc)
}
if (pszUdi)
"/org/freedesktop/Hal/devices/platform_floppy_%u_storage",
Number);
}
/**
* Check whether a device number might correspond to a CD-ROM device according
* to Documentation/devices.txt in the Linux kernel source.
* @returns true if it might, false otherwise
* @param Number the device number (major and minor combination)
*/
{
return true;
if (major == SCSI_CDROM_MAJOR)
return true;
if (major == CDU31A_CDROM_MAJOR)
return true;
if (major == GOLDSTAR_CDROM_MAJOR)
return true;
if (major == OPTICS_CDROM_MAJOR)
return true;
if (major == SANYO_CDROM_MAJOR)
return true;
if (major == MITSUMI_X_CDROM_MAJOR)
return true;
return true;
if (major == MITSUMI_CDROM_MAJOR)
return true;
if (major == CDU535_CDROM_MAJOR)
return true;
if (major == MATSUSHITA_CDROM_MAJOR)
return true;
if (major == MATSUSHITA_CDROM2_MAJOR)
return true;
if (major == MATSUSHITA_CDROM3_MAJOR)
return true;
if (major == MATSUSHITA_CDROM4_MAJOR)
return true;
if (major == AZTECH_CDROM_MAJOR)
return true;
return true;
if (major == CM206_CDROM_MAJOR)
return true;
return true;
return true;
return true;
return true;
return true;
return true;
return true;
return true;
return true;
return false;
}
/**
* Send an SCSI INQUIRY command to a device and return selected information.
* @returns iprt status code
* @returns VERR_TRY_AGAIN if the query failed but might succeed next time
* @param pcszNode the full path to the device node
* @param pu8Type where to store the SCSI device type on success (optional)
* @param pchVendor where to store the vendor id string on success (optional)
* @param cchVendor the size of the @a pchVendor buffer
* @param pchModel where to store the product id string on success (optional)
* @param cchModel the size of the @a pchModel buffer
* @note check documentation on the SCSI INQUIRY command and the Linux kernel
* SCSI headers included above if you want to understand what is going
* on in this method.
*/
{
LogRelFlowFunc(("pcszNode=%s, pu8Type=%p, pchVendor=%p, cchVendor=%llu, pchModel=%p, cchModel=%llu\n",
cchModel));
int rc = RTFileOpen(&hFile, pcszNode, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_NON_BLOCK);
if (RT_SUCCESS(rc))
{
int rcIoCtl = 0;
unsigned char u8Response[96] = { 0 };
struct cdrom_generic_command CdromCommandReq;
if (RT_SUCCESS(rc))
{
if (pu8Type)
if (pchVendor)
if (pchModel)
LogRelFlowFunc(("returning success: type=%u, vendor=%.8s, product=%.16s\n",
return VINF_SUCCESS;
}
}
return rc;
}
/**
* Initialise the device strings (description and UDI) for a DVD drive based on
* vendor and model name strings.
* @param pcszVendor the vendor ID string
* @param pcszModel the product ID string
* @param pszDesc where to store the description string (optional)
* @param cchDesc the size of the buffer in @pszDesc
* @param pszUdi where to store the UDI string (optional)
* @param cchUdi the size of the buffer in @pszUdi
*/
/* static */
{
char szCleaned[128];
/* Create a cleaned version of the model string for the UDI string. */
else
szCleaned[i] = '_';
/* Construct the description string as "Vendor Product" */
if (pszDesc)
{
if (cchVendor > 0)
else
}
/* Construct the UDI string */
if (pszUdi)
{
if (cchModel > 0)
"/org/freedesktop/Hal/devices/storage_model_%s",
else
pszUdi[0] = '\0';
}
}
/**
* Check whether a device node points to a valid device and create a UDI and
* a description for it, and store the device number, if it does.
* @returns true if the device is valid, false otherwise
* @param pcszNode the path to the device node
* @param isDVD are we looking for a DVD device (or a floppy device)?
* @param pDevice where to store the device node (optional)
* @param pszDesc where to store the device description (optional)
* @param cchDesc the size of the buffer in @a pszDesc
* @param pszUdi where to store the device UDI (optional)
* @param cchUdi the size of the buffer in @a pszUdi
*/
{
AssertPtrReturn(pcszNode, false);
AssertPtrNullReturn(pDevice, false);
AssertPtrNullReturn(pszDesc, false);
AssertPtrNullReturn(pszUdi, false);
return false;
return false;
if (pDevice)
if (isDVD)
{
return false;
return false;
return false;
}
else
{
/* Floppies on Linux are legacy devices with hardcoded majors and
* minors */
unsigned Number;
return false;
{
case 0: case 1: case 2: case 3:
break;
case 128: case 129: case 130: case 131:
break;
default:
return false;
}
return false;
cchUdi);
}
return true;
}
int VBoxMainDriveInfo::updateDVDs ()
{
LogFlowThisFunc(("entered\n"));
int rc = VINF_SUCCESS;
bool success = false; /* Have we succeeded in finding anything yet? */
try
{
/* Always allow the user to override our auto-detection using an
* environment variable. */
&success);
setNoProbe(false);
{
setNoProbe(true);
}
/* Walk through the /dev subtree if nothing else has helped. */
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
int VBoxMainDriveInfo::updateFloppies ()
{
LogFlowThisFunc(("entered\n"));
int rc = VINF_SUCCESS;
bool success = false; /* Have we succeeded in finding anything yet? */
try
{
mFloppyList.clear ();
false /* isDVD */, &success);
setNoProbe(false);
&success);
{
setNoProbe(true);
}
/* Walk through the /dev subtree if nothing else has helped. */
&success);
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
/**
* Extract the names of drives from an environment variable and add them to a
* list if they are valid.
* @returns iprt status code
* @param pcszVar the name of the environment variable. The variable
* value should be a list of device node names, separated
* by ':' characters.
* @param pList the list to append the drives found to
* @param isDVD are we looking for DVD drives or for floppies?
* @param pfSuccess this will be set to true if we found at least one drive
* and to false otherwise. Optional.
*/
/* static */
{
int rc = VINF_SUCCESS;
bool success = false;
try
{
const char *pcszCurrent = pszFreeMe;
{
if (pcszNext)
else
{
success = true;
}
}
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
class sysfsBlockDev
{
public:
misValid(false)
{
if (findDeviceNode())
{
if (mwantDVD)
else
}
}
private:
const char *mpcszName;
/** Are we looking for a floppy or a DVD device? */
bool mwantDVD;
/** The device node for the device */
char mszNode[RTPATH_MAX];
/** Does the sysfs entry look like we expect it too? This is a canary
* for future sysfs ABI changes. */
bool misConsistent;
/** Is this entry a valid specimen of what we are looking for? */
bool misValid;
/** Human readible drive description string */
char mszDesc[256];
/** Unique identifier for the drive. Should be identical to hal's UDI for
* the device. May not be unique for two identical drives. */
char mszUdi[256];
private:
/* Private methods */
/**
* @returns boolean success value
*/
bool findDeviceNode()
{
if (dev == 0)
{
misConsistent = false;
return false;
}
return false;
return true;
}
/** Check whether the sysfs block entry is valid for a DVD device and
* initialise the string data members for the object. We try to get all
* the information we need from sysfs if possible, to avoid unnecessarily
* poking the device, and if that fails we fall back to an SCSI INQUIRY
* command. */
void validateAndInitForDVD()
{
return;
{
if (cchVendor >= 0)
{
if (cchModel >= 0)
{
misValid = true;
return;
}
}
}
if (!noProbe())
}
/** Try to find out whether a device is a DVD drive by sending it an
* SCSI INQUIRY command. If it is, initialise the string and validity
* data members for the object based on the returned data.
*/
void probeAndInitForDVD()
{
sizeof(szModel));
{
misValid = true;
}
}
/** Check whether the sysfs block entry is valid for a floppy device and
* initialise the string data members for the object. Since we only
* support floppies using the basic "floppy" driver, we check the driver
* using the entry name and a driver-specific ioctl. */
void validateAndInitForFloppy()
{
bool haveName = false;
char szDriver[8];
if ( mpcszName[0] != 'f'
return;
if (!noProbe())
{
return;
}
else if (!haveName)
return;
misValid = true;
}
public:
bool isConsistent()
{
return misConsistent;
}
bool isValid()
{
return misValid;
}
const char *getDesc()
{
return mszDesc;
}
const char *getUdi()
{
return mszUdi;
}
const char *getNode()
{
return mszNode;
}
};
/**
* Helper function to query the sysfs subsystem for information about DVD
* drives attached to the system.
* @returns iprt status code
* @param pList where to add information about the drives detected
* @param isDVD are we looking for DVDs or floppies?
* @param pfSuccess Did we find anything?
*
* @returns IPRT status code
*/
/* static */
{
LogFlowFunc (("pList=%p, isDVD=%u, pfSuccess=%p\n",
int rc;
bool fSuccess = false;
unsigned cFound = 0;
if (!RTPathExists("/sys"))
return VINF_SUCCESS;
/* This might mean that sysfs semantics have changed */
fSuccess = true;
if (RT_SUCCESS(rc))
for (;;)
{
break;
continue;
/* This might mean that sysfs semantics have changed */
continue;
try
{
}
{
rc = VERR_NO_MEMORY;
break;
}
++cFound;
}
if (rc == VERR_NO_MORE_FILES)
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
/* Clean up again */
for (unsigned i = 0; i < cFound; ++i)
if (pfSuccess)
return rc;
}
/** Structure for holding information about a drive we have found */
struct deviceNodeInfo
{
/** The device number */
/** The device node path */
char szPath[RTPATH_MAX];
/** The device description */
char szDesc[256];
/** The device UDI */
char szUdi[256];
};
/** The maximum number of devices we will search for. */
enum { MAX_DEVICE_NODES = 8 };
/** An array of MAX_DEVICE_NODES devices */
/**
* Recursive worker function to walk the /dev tree looking for DVD or floppy
* devices.
* @returns true if we have already found MAX_DEVICE_NODES devices, false
* otherwise
* @param pszPath the path to start recursing. The function can modify
* this string at and after the terminating zero
* @param cchPath the size of the buffer (not the string!) in @a pszPath
* @param aDevices where to fill in information about devices that we have
* found
* @param wantDVD are we looking for DVD devices (or floppies)?
*/
{
/*
* Check assumptions made by the code below.
*/
return false;
for (;;)
{
if (RT_FAILURE(rc))
break;
{
continue;
continue;
}
continue;
break;
/* Do the matching. */
continue;
unsigned i;
for (i = 0; i < MAX_DEVICE_NODES; ++i)
break;
AssertBreak(i < MAX_DEVICE_NODES);
continue;
"%s", pszPath);
if (i == MAX_DEVICE_NODES - 1)
break;
continue;
/* Recurse into subdirectories. */
continue;
continue;
continue;
break;
}
}
/**
* Recursively walk through the /dev tree and add any DVD or floppy drives we
* find and can access to our list. (If we can't access them we can't check
* whether or not they are really DVD or floppy drives).
* @note this is rather slow (a couple of seconds) for DVD probing on
* systems with a static /dev tree, as the current code tries to open
* a CD-ROM device, and opening a non-existent device can take a non.
* negligeable time on Linux. If it is ever necessary to improve this
* (static /dev trees are no longer very fashionable these days, and
* sysfs looks like it will be with us for a while), we could further
* reduce the number of device nodes we open by checking whether the
* there is a race, but it is probably not important for us).
* @returns iprt status code
* @param pList the list to append the drives found to
* @param isDVD are we looking for DVD drives or for floppies?
* @param pfSuccess this will be set to true if we found at least one drive
* and to false otherwise. Optional.
*/
/* static */
{
pfSuccess));
int rc = VINF_SUCCESS;
bool success = false;
try
{
for (unsigned i = 0; i < MAX_DEVICE_NODES; ++i)
{
{
success = true;
}
}
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
int VBoxMainUSBDeviceInfo::UpdateDevices ()
{
LogFlowThisFunc(("entered\n"));
int rc = VINF_SUCCESS;
bool success = false; /* Have we succeeded in finding anything yet? */
try
{
mDeviceList.clear();
#ifdef VBOX_USB_WITH_SYSFS
# ifdef VBOX_USB_WITH_DBUS
bool halSuccess = false;
if ( RT_SUCCESS(rc)
&& RT_SUCCESS(RTDBusLoadLib())
/* Try the old API if the new one *succeeded* as only one of them will
* pick up devices anyway. */
if (!success)
# endif /* VBOX_USB_WITH_DBUS */
# ifdef VBOX_USB_WITH_FAM
if ( RT_SUCCESS(rc)
# endif
#else /* !VBOX_USB_WITH_SYSFS */
#endif /* !VBOX_USB_WITH_SYSFS */
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
#if defined VBOX_USB_WITH_SYSFS && defined VBOX_USB_WITH_DBUS
class hotplugDBusImpl : public VBoxMainHotplugWaiterImpl
{
/** The connection to DBus */
/** Semaphore which is set when a device is hotplugged and reset when
* it is read. */
volatile bool mTriggered;
/** A flag to say that we wish to interrupt the current wait. */
volatile bool mInterrupt;
public:
/** Test whether this implementation can be used on the current system */
static bool HalAvailable(void)
{
/* Try to open a test connection to hal */
return !!dbusConnection;
return false;
}
/** Constructor */
hotplugDBusImpl (void);
virtual ~hotplugDBusImpl (void);
/** @copydoc VBoxMainHotplugWaiter::Wait */
/** @copydoc VBoxMainHotplugWaiter::Interrupt */
virtual void Interrupt (void);
};
/* This constructor sets up a private connection to the DBus daemon, connects
* to the hal service and installs a filter which sets the mTriggered flag in
* the Context structure when a device (not necessarily USB) is added or
* removed. */
{
int rc = VINF_SUCCESS;
if (RT_SUCCESS(RTDBusLoadLib()))
{
{
}
if (!mConnection)
while ( RT_SUCCESS(rc)
if ( RT_SUCCESS(rc)
(void *) &mTriggered, NULL))
rc = VERR_NO_MEMORY;
if (RT_FAILURE(rc))
mConnection.reset();
}
}
/* Destructor */
{
if (!!mConnection)
(void *) &mTriggered);
}
/* Currently this is implemented using a timed out wait on our private DBus
* connection. Because the connection is private we don't have to worry about
* blocking other users. */
{
int rc = VINF_SUCCESS;
if (!mConnection)
bool connected = true;
mTriggered = false;
mInterrupt = false;
unsigned cRealMillies;
if (cMillies != RT_INDEFINITE_WAIT)
else
&& !mInterrupt)
{
if (mInterrupt)
LogFlowFunc(("wait loop interrupted\n"));
if (cMillies != RT_INDEFINITE_WAIT)
mInterrupt = true;
}
if (!connected)
rc = VERR_TRY_AGAIN;
return rc;
}
/* Set a flag to tell the Wait not to resume next time it times out. */
void hotplugDBusImpl::Interrupt()
{
LogFlowFunc(("\n"));
mInterrupt = true;
}
#endif /* VBOX_USB_WITH_SYSFS && VBOX_USB_WITH_DBUS */
class hotplugNullImpl : public VBoxMainHotplugWaiterImpl
{
public:
hotplugNullImpl (void) {}
virtual ~hotplugNullImpl (void) {}
/** @copydoc VBoxMainHotplugWaiter::Wait */
virtual int Wait (RTMSINTERVAL)
{
return VERR_NOT_SUPPORTED;
}
/** @copydoc VBoxMainHotplugWaiter::Interrupt */
virtual void Interrupt (void) {}
};
#ifdef VBOX_USB_WITH_SYSFS
# ifdef VBOX_USB_WITH_FAM
# define SYSFS_USB_DEVICE_PATH "/dev/bus/usb"
# define SYSFS_WAKEUP_STRING "Wake up!"
class hotplugSysfsFAMImpl : public VBoxMainHotplugWaiterImpl
{
/** Pipe used to interrupt wait(), the read end. */
/** Pipe used to interrupt wait(), the write end. */
/** Our connection to FAM for polling for changes on sysfs. */
/** Has our connection been initialised? */
bool mfFAMInitialised;
/** The iprt native handle of the FAM fd socket. */
/** Poll set containing the FAM socket and the termination pipe */
/** Flag to mark that the Wait() method is currently being called, and to
* ensure that it isn't called multiple times in parallel. */
/** iprt result code from object initialisation. Should be AssertReturn-ed
* on at the start of all methods. I went this way because I didn't want
* to deal with exceptions. */
int mStatus;
/** ID values associates with the wakeup pipe and the FAM socket for polling
*/
enum
{
RPIPE_ID = 1,
};
/** Initialise the connection to the FAM daemon, allocating all required
* resources.
* @returns iprt status code
*/
int initConnection(void);
/** Clean up the connection to the FAM daemon, freeing any allocated
* resources and gracefully skipping over any which have not yet been
* allocated or already cleaned up.
* @returns iprt status code
*/
void termConnection(void);
/** Clean up any resources in use, gracefully skipping over any which have
* not yet been allocated or already cleaned up. Intended to be called
* from the destructor or after a failed initialisation. */
void term(void);
/** Make sure that the object is correctly initialised and re-initialises
* it if not, and if the maximum number of connection attempts has not been
* reached.
* @returns iprt status value
* @returns VERR_TRY_AGAIN if we are giving up on this attempt but may
* still succeed on future attempts
*/
int checkConnection(void);
/** Quick failure test of the checkConnection() function. */
void testCheckConnection(void);
/** Open our connection to FAM and convert the status to iprt.
* @todo really convert the status
*/
int openFAM(void)
{
if (FAMOpen(&mFAMConnection) < 0)
return VERR_FAM_OPEN_FAILED;
mfFAMInitialised = true;
return VINF_SUCCESS;
}
/** Monitor a file through the FAM connection. */
int monitorDirectoryFAM(const char *pszName)
{
return VINF_SUCCESS;
}
/** Quick failure test of the monitor function - we temporarily invalidate
* the connection FD to trigger an error path. */
void testMonitorDirectoryFAM(void)
{
}
{
return VINF_SUCCESS;
return VERR_TRY_AGAIN;
}
/** Quick failure test of the nextevent function. */
void testNextEventFAM(void)
{
}
/** Close our connection to FAM. We ignore errors as there is no
* documentation as to what they mean, and the only error which might
* interest us (EINTR) should be (but isn't) handled inside the library. */
void closeFAM(void)
{
if (mfFAMInitialised)
mfFAMInitialised = false;
}
/** Read the wakeup string from the wakeup pipe */
int drainWakeupPipe(void);
public:
hotplugSysfsFAMImpl(void);
virtual ~hotplugSysfsFAMImpl(void)
{
term();
#ifdef DEBUG
/** The first call to term should mark all resources as freed, so this
* should be a semantic no-op. */
term();
#endif
}
/** Is sysfs available on this system? If so we expect that this
* implementation will be usable. */
static bool SysfsAvailable(void)
{
return RTDirExists(SYSFS_USB_DEVICE_PATH);
}
/** @copydoc VBoxMainHotplugWaiter::Wait */
virtual int Wait(RTMSINTERVAL);
/** @copydoc VBoxMainHotplugWaiter::Interrupt */
virtual void Interrupt(void);
};
hotplugSysfsFAMImpl::hotplugSysfsFAMImpl(void) :
{
# ifdef DEBUG
/* Excercise the code path (term() on a not-fully-initialised object) as
* well as we can. On an uninitialised object this method is a sematic
* no-op. */
term();
/* For now this probing method should only be used if nothing else is
* available */
if (!testing())
{
# ifndef VBOX_USB_WITH_SYSFS_BY_DEFAULT
# endif
# ifdef VBOX_USB_WITH_DBUS
# endif
}
# endif
int rc;
do {
break;
break;
# ifdef DEBUG
/** Other tests */
# endif
} while(0);
if (RT_FAILURE(rc))
term();
}
int hotplugSysfsFAMImpl::initConnection(void)
{
int rc;
return rc;
return rc;
return rc;
return rc;
return rc;
return rc;
return VINF_SUCCESS;
}
void hotplugSysfsFAMImpl::termConnection(void)
{
closeFAM();
}
int hotplugSysfsFAMImpl::checkConnection(void)
{
/** We should only be called from within Wait(). */
if (mStatus == VERR_FAM_CONNECTION_LOST)
{
mStatus = initConnection();
}
return mStatus;
}
void hotplugSysfsFAMImpl::testCheckConnection(void)
{
mfWaiting = 0;
}
void hotplugSysfsFAMImpl::term(void)
{
/** This would probably be a pending segfault, so die cleanly */
}
/** Does a FAM event code mean that the available devices have (probably)
* changed? */
{
return VERR_TRY_AGAIN;
return VINF_SUCCESS;
}
int hotplugSysfsFAMImpl::drainWakeupPipe(void)
{
char szBuf[sizeof(SYSFS_WAKEUP_STRING)];
return VINF_SUCCESS;
}
{
int rc;
do {
break;
/* timeout returns */
break;
{
rc = drainWakeupPipe();
break;
}
break;
} while (false);
mfWaiting = 0;
return rc;
}
void hotplugSysfsFAMImpl::Interrupt(void)
{
sizeof(SYSFS_WAKEUP_STRING), &cbDummy);
if (RT_SUCCESS(rc))
}
# endif /* VBOX_USB_WITH_FAM */
#endif /* VBOX_USB_WTH_SYSFS */
{
#ifdef VBOX_USB_WITH_SYSFS
# ifdef VBOX_USB_WITH_DBUS
if (hotplugDBusImpl::HalAvailable())
{
mImpl = new hotplugDBusImpl;
return;
}
# endif /* VBOX_USB_WITH_DBUS */
# ifdef VBOX_USB_WITH_FAM
if (hotplugSysfsFAMImpl::SysfsAvailable())
{
mImpl = new hotplugSysfsFAMImpl;
return;
}
# endif /* VBOX_USB_WITH_FAM */
#endif /* VBOX_USB_WITH_SYSFS */
mImpl = new hotplugNullImpl;
}
#ifdef VBOX_USB_WITH_SYSFS
# ifdef VBOX_USB_WITH_FAM
class sysfsPathHandler
{
/** Called on each element of the sysfs directory. Can e.g. store
* interesting entries in a list. */
public:
{
}
};
/**
* Helper function to walk a sysfs directory for extracting information about
* devices.
* @returns iprt status code
* @param pcszPath Sysfs directory to walk. Must exist.
* @param pHandler Handler object which will be invoked on each directory
* entry
*
* @returns IPRT status code
*/
/* static */
{
int rc;
while (RT_SUCCESS(rc))
{
/* We break on "no more files" as well as on "real" errors */
if (RT_FAILURE(rc))
break;
continue;
if (RT_FAILURE(rc))
break;
break;
}
if (rc == VERR_NO_MORE_FILES)
rc = VINF_SUCCESS;
return rc;
}
#define USBDEVICE_MAJOR 189
/** Deduce the bus that a USB device is plugged into from the device node
{
AssertReturn(devNum, 0);
}
/** Deduce the device number of a USB device on the bus from the device node
{
AssertReturn(devNum, 0);
}
/**
* interface. To be used with getDeviceInfoFromSysfs().
*/
class matchUSBDevice : public sysfsPathHandler
{
public:
private:
{
return true;
/* Sanity test of our static helpers */
AssertReturn (devnum, true);
char szDevPath[RTPATH_MAX];
if (cchDevPath < 0)
return true;
try
{
}
{
return false;
}
return true;
}
};
/**
* device. To be used with getDeviceInfoFromSysfs().
*/
class matchUSBInterface : public sysfsPathHandler
{
public:
/** This constructor is currently used to unit test the class logic in
* debug builds. Since no access is made to anything outside the class,
* this shouldn't cause any slowdown worth mentioning. */
{
}
private:
/** The logic for testing whether a sysfs address corresponds to an
* interface of a device. Both must be referenced by their canonical
* sysfs paths. This is not tested, as the test requires file-system
* interaction. */
{
/* If this passes, pcszIface is at least cchDev long */
return false;
/* If this passes, pcszIface is longer than cchDev */
return false;
/* In sysfs an interface is an immediate subdirectory of the device */
return false;
/* And it always has a colon in its name */
return false;
/* And hopefully we have now elimitated everything else */
return true;
}
{
return true;
try
{
}
{
return false;
}
return true;
}
};
/**
* Helper function to query the sysfs subsystem for information about USB
* devices attached to the system.
* @returns iprt status code
* @param pList where to add information about the drives detected
* @param pfSuccess Did we find anything?
*
* @returns IPRT status code
*/
bool *pfSuccess)
{
LogFlowFunc (("pList=%p, pfSuccess=%p\n",
do {
if (RT_FAILURE(rc))
break;
{
if (RT_FAILURE(rc))
break;
}
} while(0);
if (RT_FAILURE(rc))
/* Clean up again */
if (pfSuccess)
return rc;
}
# endif /* VBOX_USB_WITH_FAM */
#endif /* VBOX_USB_WITH_SYSFS */
#if defined VBOX_USB_WITH_SYSFS && defined VBOX_USB_WITH_DBUS
/** Wrapper class around DBusError for automatic cleanup */
class autoDBusError
{
public:
~autoDBusError ()
{
if (IsSet())
}
bool IsSet ()
{
}
{
}
void FlowLog ()
{
if (IsSet ())
}
};
/**
* Helper function for setting up a connection to the DBus daemon and
* registering with the hal service.
*
* @note If libdbus is being loaded at runtime then be sure to call
* VBoxDBusCheckPresence before calling this.
* @returns iprt status code
* @param ppConnection where to store the connection handle
*/
/* static */
{
int rc = VINF_SUCCESS;
bool halSuccess = true;
if (!dbusConnection)
halSuccess = false;
if (halSuccess)
{
}
if (halSuccess)
{
"type='signal',"
"interface='org.freedesktop.Hal.Manager',"
"sender='org.freedesktop.Hal',"
"path='/org/freedesktop/Hal/Manager'",
}
rc = VERR_NO_MEMORY;
if (halSuccess)
return rc;
}
/**
* Helper function for setting up a private connection to the DBus daemon and
* registering with the hal service. Private connections are considered
* unsociable and should not be used unnecessarily (as per the DBus API docs).
*
* @note If libdbus is being loaded at runtime then be sure to call
* VBoxDBusCheckPresence before calling this.
* @returns iprt status code
* @param pConnection where to store the connection handle
*/
/* static */
{
int rc = VINF_SUCCESS;
bool halSuccess = true;
if (!dbusConnection)
halSuccess = false;
if (halSuccess)
{
}
if (halSuccess)
{
"type='signal',"
"interface='org.freedesktop.Hal.Manager',"
"sender='org.freedesktop.Hal',"
"path='/org/freedesktop/Hal/Manager'",
}
rc = VERR_NO_MEMORY;
if (halSuccess)
return rc;
}
/**
* Helper function for shutting down a connection to DBus and hal.
* @param pConnection the connection handle
*/
/* extern */
{
"type='signal',"
"interface='org.freedesktop.Hal.Manager',"
"sender='org.freedesktop.Hal',"
"path='/org/freedesktop/Hal/Manager'",
LogFlowFunc(("returning\n"));
}
/**
* Helper function for shutting down a private connection to DBus and hal.
* @param pConnection the connection handle
*/
/* extern */
{
"type='signal',"
"interface='org.freedesktop.Hal.Manager',"
"sender='org.freedesktop.Hal',"
"path='/org/freedesktop/Hal/Manager'",
LogFlowFunc(("returning\n"));
}
/** Wrapper around dbus_connection_unref. We need this to use it as a real
* function in auto pointers, as a function pointer won't wash here. */
/* extern */
{
}
/**
* This function closes and unrefs a private connection to dbus. It should
* only be called once no-one else is referencing the connection.
*/
/* extern */
{
}
/** Wrapper around dbus_message_unref. We need this to use it as a real
* function in auto pointers, as a function pointer won't wash here. */
/* extern */
{
}
/**
* Find the UDIs of hal entries that contain Key=Value property.
* @returns iprt status code. If a non-fatal error occurs, we return success
* but reset pMessage to NULL.
* @param pConnection an initialised connection DBus
* @param pszKey the property key
* @param pszValue the property value
* @param pMessage where to store the return DBus message. This must be
* parsed to get at the UDIs. NOT optional.
*/
/* static */
const char *pszValue,
{
LogFlowFunc (("pConnection=%p, pszKey=%s, pszValue=%s, pMessage=%p\n",
bool halSuccess = true; /* We set this to false to abort the operation. */
{
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"FindDeviceStringMatch");
if (!message)
rc = VERR_NO_MEMORY;
}
{
if (!reply)
halSuccess = false;
}
return rc;
}
/**
* Find the UDIs of hal entries that contain Key=Value property and return the
* result on the end of a vector of iprt::MiniString.
* @returns iprt status code. If a non-fatal error occurs, we return success
* but set *pfSuccess to false.
* @param pConnection an initialised connection DBus
* @param pszKey the property key
* @param pszValue the property value
* @param pMatches pointer to an array of iprt::MiniString to append the
* results to. NOT optional.
* @param pfSuccess will be set to true if the operation succeeds
*/
/* static */
bool *pfSuccess)
{
LogFlowFunc (("pConnection=%p, pszKey=%s, pszValue=%s, pMatches=%p, pfSuccess=%p\n",
bool halSuccess = true; /* We set this to false to abort the operation. */
{
&replyFind);
if (!replyFind)
halSuccess = false;
}
{
halSuccess = false;
}
{
/* Now get all UDIs from the iterator */
const char *pszUdi;
try
{
}
{
rc = VERR_NO_MEMORY;
}
}
*pfSuccess = halSuccess;
return rc;
}
/**
* Read a set of string properties for a device. If some of the properties are
* not of type DBUS_TYPE_STRING or do not exist then a NULL pointer will be
* returned for them.
* @returns iprt status code. If the operation failed for non-fatal reasons
* then we return success and leave pMessage untouched - reset it
* before the call to detect this.
* @param pConnection an initialised connection DBus
* @param pszUdi the Udi of the device
* @param cProps the number of property values to look up
* @param papszKeys the keys of the properties to be looked up
* @param papszValues where to store the values of the properties. The
* strings returned will be valid until the message
* returned in @a ppMessage is freed. Undefined if
* the message is NULL.
* @param pMessage where to store the return DBus message. The caller
* is responsible for freeing this once they have
* finished with the value strings. NOT optional.
*/
/* static */
char **papszValues,
{
LogFlowFunc (("pConnection=%p, pszUdi=%s, cProps=%llu, papszKeys=%p, papszValues=%p, pMessage=%p\n",
bool halSuccess = true; /* We set this to false to abort the operation. */
/* Initialise the return array to NULLs */
papszValues[i] = NULL;
/* Send a GetAllProperties message to hald */
"org.freedesktop.Hal.Device",
"GetAllProperties");
if (!message)
rc = VERR_NO_MEMORY;
{
if (!reply)
halSuccess = false;
}
/* Parse the reply */
{
halSuccess = false;
}
/* Go through all entries in the reply and see if any match our keys. */
{
const char *pszKey;
/* Fill in any matches. */
{
}
}
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Read a set of string properties for a device. If some properties do not
* exist or are not of type DBUS_TYPE_STRING, we will still fetch the others.
* @returns iprt status code. If the operation failed for non-fatal reasons
* then we return success and set *pfSuccess to false.
* @param pConnection an initialised connection DBus
* @param pszUdi the Udi of the device
* @param cProps the number of property values to look up
* @param papszKeys the keys of the properties to be looked up
* @param pMatches pointer to an empty array of iprt::MiniString to append the
* results to. NOT optional.
* @param pfMatches pointer to an array of boolean values indicating
* whether the respective property is a string. If this
* is not supplied then all properties must be strings
* for the operation to be considered successful
* @param pfSuccess will be set to true if the operation succeeds
*/
/* static */
const char **papszKeys,
{
LogFlowFunc (("pConnection=%p, pszUdi=%s, cProps=%llu, papszKeys=%p, pMatches=%p, pfMatches=%p, pfSuccess=%p\n",
bool halSuccess = true;
if (!message)
halSuccess = false;
{
else
try
{
}
{
rc = VERR_NO_MEMORY;
}
}
*pfSuccess = halSuccess;
{
|| (pfMatches[j] == true)
}
return rc;
}
/**
* Helper function to query the hal subsystem for information about USB devices
* attached to the system.
* @returns iprt status code
* @param pList where to add information about the devices detected
* @param pfSuccess will be set to true if all interactions with hal
* succeeded and to false otherwise. Optional.
*
* @returns IPRT status code
*/
/* static */
{
bool halSuccess = true; /* We set this to false to abort the operation. */
/* Connect to hal */
if (!dbusConnection)
halSuccess = false;
/* Get an array of all devices in the usb_device subsystem */
{
"usb_device", &replyFind);
if (!replyFind)
halSuccess = false;
}
{
halSuccess = false;
}
/* Recurse down into the array and query interesting information about the
* entries. */
{
/* Get the device node and the sysfs path for the current entry. */
const char *pszUdi;
/* Get the interfaces. */
{
bool ifaceSuccess = true; /* If we can't get the interfaces, just
* skip this one device. */
try
{
}
{
rc = VERR_NO_MEMORY;
}
}
}
rc = VERR_NO_MEMORY;
*pfSuccess = halSuccess;
return rc;
}
/**
* Helper function to query the hal subsystem for information about USB devices
* attached to the system, using the older API.
* @returns iprt status code
* @param pList where to add information about the devices detected
* @param pfSuccess will be set to true if all interactions with hal
* succeeded and to false otherwise. Optional.
*
* @returns IPRT status code
*/
/* static */
{
bool halSuccess = true; /* We set this to false to abort the operation. */
/* Connect to hal */
if (!dbusConnection)
halSuccess = false;
/* Get an array of all devices in the usb_device subsystem */
{
"usbraw", &replyFind);
if (!replyFind)
halSuccess = false;
}
{
halSuccess = false;
}
/* Recurse down into the array and query interesting information about the
* entries. */
{
/* Get the device node and the sysfs path for the current entry. */
const char *pszUdi;
/* Get the interfaces. */
{
bool ifaceSuccess = false; /* If we can't get the interfaces, just
* skip this one device. */
&ifaceSuccess);
try
{
}
{
rc = VERR_NO_MEMORY;
}
}
}
rc = VERR_NO_MEMORY;
*pfSuccess = halSuccess;
return rc;
}
/**
* Helper function to query the hal subsystem for information about USB devices
* attached to the system.
* @returns iprt status code
* @param pList where to add information about the devices detected. If
* certain interfaces are not found (@a pfFound is false on
* return) this may contain invalid information.
* @param pcszUdi the hal UDI of the device
* @param pfSuccess will be set to true if the operation succeeds and to
* false if it fails for non-critical reasons. Optional.
*
* @returns IPRT status code
*/
/* static */
{
pfSuccess));
bool halSuccess = true; /* We set this to false to abort the operation. */
if (!dbusConnection)
halSuccess = false;
{
/* Look for children of the current UDI. */
if (!replyFind)
halSuccess = false;
}
{
halSuccess = false;
}
{
/* Now get the sysfs path and the subsystem from the iterator */
const char *pszUdi;
"linux.subsystem" };
if (!replyGet)
halSuccess = false;
halSuccess = false;
try
{
}
{
rc = VERR_NO_MEMORY;
}
}
rc = VERR_NO_MEMORY;
*pfSuccess = halSuccess;
return rc;
}
/**
* When it is registered with DBus, this function will be called by
* dbus_connection_read_write_dispatch each time a message is received over the
* DBus connection. We check whether that message was caused by a hal device
* hotplug event, and if so we set a flag. dbus_connection_read_write_dispatch
* will return after calling its filter functions, and its caller should then
* check the status of the flag passed to the filter function.
*
* @param pConnection The DBus connection we are using.
* @param pMessage The DBus message which just arrived.
* @param pvUser A pointer to the flag variable we are to set.
*/
/* static */
{
volatile bool *pTriggered = reinterpret_cast<volatile bool *>(pvUser);
"DeviceAdded")
"DeviceRemoved"))
{
*pTriggered = true;
}
}
#endif /* VBOX_USB_WITH_SYSFS && VBOX_USB_WITH_DBUS */