HostHardwareLinux.cpp revision a690712a2857cda972bc86103d98cd90aea4bd6a
/* $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) 2006-2007 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 RT_OS_LINUX
# include <unistd.h>
# include <fcntl.h>
# include <mntent.h>
/* bird: This is a hack to work around conflicts between these linux kernel headers
* and the GLIBC tcpip headers. They have different declarations of the 4
* standard byte order functions. */
// # define _LINUX_BYTEORDER_GENERIC_H
# ifdef VBOX_WITH_DBUS
# include <vbox-dbus.h>
# endif
# include <errno.h>
#endif /* RT_OS_LINUX */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
bool g_testHostHardwareLinux = false;
static bool testing () { return g_testHostHardwareLinux; }
/*******************************************************************************
* Defines and Typedefs *
*******************************************************************************/
#ifdef VBOX_WITH_DBUS
/* This must be extern to be used in the RTMemAutoPtr template */
DBusMessage **ppMessage);
const char **papszKeys, char **papszValues,
DBusMessage **ppMessage);
bool *pfSuccess);
#endif /* VBOX_WITH_DBUS */
/**
* Updates the list of host DVD drives.
*
* @returns iprt status code
*/
int VBoxMainDriveInfo::updateDVDs ()
{
LogFlowThisFunc (("entered\n"));
int rc = VINF_SUCCESS;
bool success = false; /* Have we succeeded in finding anything yet? */
try
{
#if defined(RT_OS_LINUX)
#ifdef VBOX_WITH_DBUS
#endif /* VBOX_WITH_DBUS defined */
// On Linux without hal, the situation is much more complex. We will take a
// heuristical approach and also allow the user to specify a list of host
// CDROMs using an environment variable.
// The general strategy is to try some known device names and see of they
// API to parse it) for CDROM devices. Ok, let's start!
&success);
{
// this is a good guess usually
if (validateDevice("/dev/cdrom", true))
// check the mounted drives
// check the drives that can be mounted
if (RT_SUCCESS (rc))
}
#endif
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
/**
* Updates the list of host floppy drives.
*
* @returns iprt status code
*/
int VBoxMainDriveInfo::updateFloppies ()
{
LogFlowThisFunc (("entered\n"));
int rc = VINF_SUCCESS;
bool success = false; /* Have we succeeded in finding anything yet? */
try
{
mFloppyList.clear ();
#if defined(RT_OS_LINUX)
#ifdef VBOX_WITH_DBUS
#endif /* VBOX_WITH_DBUS defined */
// As with the CDROMs, on Linux we have to take a multi-level approach
// involving parsing the mount tables. As this is not bulletproof, we'll
// give the user the chance to override the detection by an environment
// variable and skip the detection.
&success);
{
char devName[10];
for (int i = 0; i <= 7; i++)
{
if (validateDevice(devName, false))
}
}
#endif
}
{
rc = VERR_NO_MEMORY;
}
return rc;
}
#ifdef RT_OS_LINUX
/**
* Helper function to check whether the given device node is a valid drive
*/
/* static */
{
bool retValue = false;
// sanity check
if (!deviceNode)
{
return false;
}
// first a simple stat() call
{
return false;
} else
{
if (isDVD)
{
{
int fileHandle;
// now try to open the device
if (fileHandle >= 0)
{
// this call will finally reveal the whole truth
#ifdef RT_OS_LINUX
#endif
{
retValue = true;
}
}
}
} else
{
// floppy case
{
/// @todo do some more testing, maybe a nice IOCTL!
retValue = true;
}
}
}
return retValue;
}
#else /* !RT_OS_LINUX */
#endif /* !RT_OS_LINUX */
/**
* Extract the names of drives from an environment variable and add them to a
* list if they are valid.
* @returns iprt status code
* @param pszVar 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;
{
if (!drive)
rc = VERR_NO_MEMORY;
}
{
{
if (pDriveNext != NULL)
*pDriveNext = '\0';
{
success = true;
}
if (pDriveNext != NULL)
{
}
else
}
}
return rc;
}
#ifdef RT_OS_LINUX
/**
* Helper function to parse the given mount file and add found entries
*/
/* static */
{
#ifdef RT_OS_LINUX
int rc = VINF_SUCCESS;
if (mtab)
{
char *tmp;
{
rc = VERR_NO_MEMORY;
// supermount fs case
{
if (tmp)
{
if (!mnt_type)
rc = VERR_NO_MEMORY;
else
{
if (tmp)
*tmp = '\0';
}
}
if (tmp)
{
if (!mnt_dev)
rc = VERR_NO_MEMORY;
else
{
if (tmp)
*tmp = '\0';
}
}
}
// use strstr here to cover things fs types like "udf,iso9660"
{
{
bool insert = true;
insert = false;
{
insert = false;
}
if (insert)
}
}
}
}
return rc;
#endif
}
#endif /* RT_OS_LINUX */
#if defined(RT_OS_LINUX) && defined(VBOX_WITH_DBUS)
/* Linux, load libdbus statically */
/** 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 hal
* @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;
return rc;
}
/**
* Helper function for shutting down a connection to hal
* @param pConnection the connection handle
*/
/* static */
{
"type='signal',"
"interface='org.freedesktop.Hal.Manager',"
"sender='org.freedesktop.Hal',"
"path='/org/freedesktop/Hal/Manager'",
LogFlowFunc(("returning\n"));
}
/**
* Find the UDIs of hal entries that contain Key=Value property.
* @returns iprt status code
* @param pConnection an initialised connection DBus
* @param pszKey the property key
* @param pszValue the property value
* @param ppMessage where to store the return DBus message. This must be
* parsed to get at the UDIs. NOT optional. The caller
* is responsible for freeing this.
*/
/* static */
{
LogFlowFunc (("pConnection=%p, pszKey=%s, pszValue=%s, ppMessage=%p\n",
int rc = VINF_SUCCESS;
bool halSuccess = true;
{
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"FindDeviceStringMatch");
if (!message)
rc = VERR_NO_MEMORY;
}
{
halSuccess = false;
}
return rc;
}
/**
* Read a set of string properties for a device. If some of the properties are
* not of type DBUS_TYPE_STRING then a NULL pointer will be returned for them.
* @returns iprt status code
* @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 ppMessage 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 */
{
LogFlowFunc (("pConnection=%p, pszUdi=%s, cProps=%llu, papszKeys=%p, papszValues=%p, ppMessage=%p\n",
int rc = VINF_SUCCESS;
bool halSuccess = true;
/* Initialise the return array to NULLs */
papszValues[i] = NULL;
"org.freedesktop.Hal.Device",
"GetAllProperties");
if (!message)
rc = VERR_NO_MEMORY;
{
if (!reply)
halSuccess = false;
}
{
halSuccess = false;
}
{
const char *pszKey;
{
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Helper function to query the hal subsystem for information about 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 will be set to true if all interactions with hal
* succeeded and to false otherwise. Optional.
*
* @returns IPRT status code
*/
/* static */
{
bool halSuccess = true; /* Did something go wrong with hal or DBus? */
if (!dbusConnection)
halSuccess = false;
{
if (!replyFind)
halSuccess = false;
}
{
halSuccess = false;
}
{
/* Now get all properties from the iterator */
const char *pszUdi;
static const char *papszKeys[] =
{ "block.device", "info.product", "info.vendor" };
{
}
}
rc = VERR_NO_MEMORY;
*pfSuccess = halSuccess;
return rc;
}
#endif /* RT_OS_LINUX && VBOX_WITH_DBUS */