USBProxyServiceLinux.cpp revision 37c53e9b2c955f92dff6f11aff63809004945f17
/* $Id$ */
/** @file
* VirtualBox USB Proxy Service, Linux Specialization.
*/
/*
* Copyright (C) 2006-2011 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 <fcntl.h>
#include <unistd.h>
#ifdef VBOX_WITH_LINUX_COMPILER_H
# include <linux/compiler.h>
#endif
#include <linux/usbdevice_fs.h>
/**
* Initialize data members.
*/
#ifdef UNIT_TEST
#endif
{
}
#ifdef UNIT_TEST
/* For testing we redefine anything that accesses the outside world to
* return test values. */
# define RTEnvGet(a) \
: NULL)
( ((fUseNodes) && mfTestDevicesAccessible \
|| (!(fUseNodes) && mfTestUsbfsAccessible \
# define RTDirExists(pcszDir) \
( (pcszDir) \
# define RTFileExists(pcszFile) \
( (pcszFile) \
&& mpcszTestUsbfsRoot \
#endif
/**
* 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
* USBFS and using sysfs. The default choice is sysfs; if that is not
* available we fall back to USBFS.
* In the event of both failing, an appropriate error will be returned.
*/
bool fUseSysfs;
const char *pcszUsbRoot = NULL;
if (pcszUsbFromEnv)
{
bool fValidVBoxUSB = true;
{
LogRel(("Default USB access method set to \"usbfs\" from environment\n"));
fUseSysfs = false;
}
{
LogRel(("Default USB method set to \"sysfs\" from environment\n"));
fUseSysfs = true;
}
else
{
LogRel(("Invalid VBOX_USB environment variable setting \"%s\"\n",
fValidVBoxUSB = false;
}
if (!fValidVBoxUSB && pcszUsbRoot)
pcszUsbRoot = NULL;
}
if (!pcszUsbRoot)
{
if (USBProxyLinuxCheckDeviceRoot("/dev/vboxusb", true))
{
fUseSysfs = true;
pcszUsbRoot = "/dev/vboxusb";
}
else if (USBProxyLinuxCheckDeviceRoot("/proc/bus/usb", false))
{
fUseSysfs = false;
pcszUsbRoot = "/proc/bus/usb";
}
}
if (pcszUsbRoot)
{
#ifndef UNIT_TEST /* Hack for now */
#else
int rc = mrcTestMethodInitResult;
#endif
/* For the day when we have VBoxSVC release logging... */
: "Failed to initialise host USB using %s\n",
mLastError = rc;
}
else
return S_OK;
}
#ifdef UNIT_TEST
#endif
/**
* Initialization routine for the usbfs based operation.
*
* @returns iprt status code.
*/
int USBProxyServiceLinux::initUsbfs(void)
{
/*
* Open the devices file.
*/
int rc;
if (pszDevices)
{
if (RT_SUCCESS(rc))
{
int pipes[2];
{
/* Set close on exec (race here!) */
{
mWakeupPipeR = pipes[0];
/*
* Start the poller thread.
*/
if (RT_SUCCESS(rc))
{
LogFlowThisFunc(("returns successfully - mWakeupPipeR/W=%d/%d\n",
return VINF_SUCCESS;
}
}
else
{
}
}
else
{
}
}
}
else
{
rc = VERR_NO_MEMORY;
Log(("USBProxyServiceLinux::USBProxyServiceLinux: out of memory!\n"));
}
return rc;
}
/**
* Initialization routine for the sysfs based operation.
*
* @returns iprt status code
*/
int USBProxyServiceLinux::initSysfs(void)
{
#ifdef VBOX_USB_WITH_SYSFS
try
{
}
{
return VERR_NO_MEMORY;
}
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.
*/
#ifdef VBOX_USB_WITH_SYSFS
if (mpWaiter)
delete mpWaiter;
#endif
}
/**
* If any Usbfs-related 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;
}
{
}