USBProxyServiceLinux.cpp revision bbd8414a6c9e8c73fbe9cffd73b5747c0570b118
/* $Id$ */
/** @file
* VirtualBox USB Proxy Service, Linux Specialization.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "USBProxyService.h"
#include "USBGetDevices.h"
#include "Logging.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#ifdef VBOX_WITH_LINUX_COMPILER_H
# include <linux/compiler.h>
#endif
#include <linux/usbdevice_fs.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Suffix translation. */
typedef struct USBSUFF
{
char szSuff[4];
unsigned cchSuff;
unsigned uMul;
unsigned uDiv;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Suffixes for the endpoint polling interval.
*/
static const USBSUFF s_aIntervalSuff[] =
{
{ "ms", 2, 1, 0 },
{ "us", 2, 1, 1000 },
{ "ns", 2, 1, 1000000 },
{ "s", 1, 1000, 0 },
{ "", 0, 0, 0 } /* term */
};
/**
* Initialize data members.
*/
USBProxyServiceLinux::USBProxyServiceLinux(Host *aHost, const char *aUsbfsRoot /* = "/proc/bus/usb" */)
mWakeupPipeW(NIL_RTFILE), mUsbfsRoot(aUsbfsRoot), mUsingUsbfsDevices(true /* see init */), mUdevPolls(0)
{
}
/**
* Initializes the object (called right after construction).
*
* @returns S_OK on success and non-fatal failures, some COM error otherwise.
*/
{
/*
* Call the superclass method first.
*/
/*
* We have two methods available for getting host USB device data - using
* settings and an environment variable; if the default is not available
* we fall back to the second.
* In the event of both failing, the error from the second method tried
* will be presented to the user.
*/
#ifdef VBOX_WITH_SYSFS_BY_DEFAULT
mUsingUsbfsDevices = false;
#else
mUsingUsbfsDevices = true;
#endif
if (pszUsbFromEnv)
{
{
LogRel(("Default USB access method set to \"usbfs\" from environment\n"));
mUsingUsbfsDevices = true;
}
{
LogRel(("Default USB method set to \"sysfs\" from environment\n"));
mUsingUsbfsDevices = false;
}
else
LogRel(("Invalid VBOX_USB environment variable setting \"%s\"\n",
}
if (RT_FAILURE(rc))
{
/* For the day when we have VBoxSVC release logging... */
LogRel(("Failed to initialise host USB using %s\n",
}
: "Failed to initialise host USB using %s\n",
mLastError = rc;
return S_OK;
}
/**
* Initializiation routine for the usbfs based operation.
*
* @returns iprt status code.
*/
int USBProxyServiceLinux::initUsbfs(void)
{
/*
* Open the devices file.
*/
int rc;
char *pszDevices;
if (pszDevices)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
int pipes[2];
{
mWakeupPipeR = pipes[0];
/*
* Start the poller thread.
*/
if (RT_SUCCESS(rc))
{
LogFlowThisFunc(("returns successfully - mWakeupPipeR/W=%d/%d\n",
return VINF_SUCCESS;
}
}
else
{
}
}
}
}
else
{
rc = VERR_NO_MEMORY;
Log(("USBProxyServiceLinux::USBProxyServiceLinux: out of memory!\n"));
}
return rc;
}
/**
* Initializiation routine for the sysfs based operation.
*
* @returns iprt status code
*/
int USBProxyServiceLinux::initSysfs(void)
{
#ifdef VBOX_USB_WITH_SYSFS
else if (rc == VERR_NOT_SUPPORTED)
/* This can legitimately happen if hal or DBus are not running, but of
* course we can't start in this case. */
rc = VINF_SUCCESS;
return rc;
#else /* !VBOX_USB_WITH_SYSFS */
return VERR_NOT_IMPLEMENTED;
#endif /* !VBOX_USB_WITH_SYSFS */
}
/**
* Stop all service threads and free the device chain.
*/
{
LogFlowThisFunc(("\n"));
/*
* Stop the service.
*/
if (isActive())
stop();
/*
* Free resources.
*/
/* (No extra work for !mUsingUsbfsDevices.) */
}
/**
* If any Usbfs-releated resources are currently allocated, then free them
* and mark them as freed.
*/
{
/*
* Free resources.
*/
if (mFile != NIL_RTFILE)
{
mFile = NIL_RTFILE;
}
if (mWakeupPipeR != NIL_RTFILE)
if (mWakeupPipeW != NIL_RTFILE)
}
{
/*
* Don't think we need to do anything when the device is held... fake it.
*/
return VINF_SUCCESS;
}
{
/*
* We're not really holding it atm., just fake it.
*/
return VINF_SUCCESS;
}
bool USBProxyServiceLinux::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
{
LogRel(("USBProxy: Device %04x:%04x (%s) has become accessible.\n",
}
/**
* A device was added, we need to adjust mUdevPolls.
*
* See USBProxyService::deviceAdded for details.
*/
void USBProxyServiceLinux::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice)
{
{
LogRel(("USBProxy: Device %04x:%04x (%s) isn't accessible. giving udev a few seconds to fix this...\n",
}
}
{
int rc;
if (mUsingUsbfsDevices)
else
return rc;
}
/** String written to the wakeup pipe. */
#define WAKE_UP_STRING "WakeUp!"
/** Length of the string written. */
{
/* Cap the wait interval if we're polling for udevd changing device permissions. */
{
mUdevPolls--;
aMillies = 500;
}
if (rc == 0)
return VERR_TIMEOUT;
if (rc > 0)
{
/* drain the pipe */
{
char szBuf[WAKE_UP_STRING_LEN];
}
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
{
#ifdef VBOX_USB_WITH_SYSFS
if (rc == VERR_TRY_AGAIN)
{
rc = VINF_SUCCESS;
}
return rc;
#else /* !VBOX_USB_WITH_SYSFS */
#endif /* !VBOX_USB_WITH_SYSFS */
}
int USBProxyServiceLinux::interruptWait(void)
{
#ifdef VBOX_USB_WITH_SYSFS
if (!mUsingUsbfsDevices)
{
LogFlowFunc(("Returning VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
#endif /* VBOX_USB_WITH_SYSFS */
if (RT_SUCCESS(rc))
return rc;
}
{
if (mUsingUsbfsDevices)
else
return USBProxyLinuxGetDevices(NULL);
}