VBoxUSBFilterMgr.cpp revision 59190ecd61435d19ba3515b876272aee7bd12298
/* $Id$ */
/** @file
* VirtualBox Ring-0 USB Filter Manager.
*/
/*
* Copyright (C) 2007 Oracle Corporation
*
* Oracle Corporation confidential
* All rights reserved
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <VBox/usbfilter.h>
#include "VBoxUSBFilterMgr.h"
# include <iprt/spinlock.h>
#else
# include <iprt/semaphore.h>
#endif
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** @def VBOXUSBFILTERMGR_LOCK
* Locks the filter list. Careful with scoping since this may
* create a temporary variable. Don't call twice in the same function.
*/
/** @def VBOXUSBFILTERMGR_UNLOCK
* Unlocks the filter list.
*/
# define VBOXUSBFILTERMGR_LOCK() \
# define VBOXUSBFILTERMGR_UNLOCK() \
#else
# define VBOXUSBFILTERMGR_LOCK() \
# define VBOXUSBFILTERMGR_UNLOCK() \
#endif
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Pointer to an VBoxUSB filter. */
typedef struct VBOXUSBFILTER *PVBOXUSBFILTER;
/** Pointer to PVBOXUSBFILTER. */
typedef PVBOXUSBFILTER *PPVBOXUSBFILTER;
/**
* VBoxUSB internal filter represenation.
*/
typedef struct VBOXUSBFILTER
{
/** The core filter. */
/** The filter owner. */
/** The filter Id. */
/** Pointer to the next filter in the list. */
/**
* VBoxUSB filter list.
*/
typedef struct VBOXUSBFILTERLIST
{
/** The head pointer. */
/** The tail pointer. */
/** Pointer to a VBOXUSBFILTERLIST. */
typedef VBOXUSBFILTERLIST *PVBOXUSBFILTERLIST;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Spinlock protecting the filter lists. */
#else
/** Mutex protecting the filter lists. */
#endif
/** The per-type filter lists.
* @remark The first entry is empty (USBFILTERTYPE_INVALID). */
/**
* Initalizes the VBoxUSB filter manager.
*
* @returns IPRT status code.
*/
int VBoxUSBFilterInit(void)
{
#else
#endif
if (RT_SUCCESS(rc))
{
/* not really required, but anyway... */
}
return rc;
}
/**
* Internal worker that frees a filter.
*
* @param pFilter The filter to free.
*/
{
}
/**
* Terminates the VBoxUSB filter manager.
*/
void VBoxUSBFilterTerm(void)
{
#else
#endif
{
while (pCur)
{
}
}
}
/**
* Adds a new filter.
*
* The filter will be validate, duplicated and added.
*
* @returns IPRT status code.
* @param pFilter The filter.
* @param Owner The filter owner. Must be non-zero.
* @param puId Where to store the filter ID.
*/
{
/*
* Validate input.
*/
if (RT_FAILURE(rc))
return rc;
return VERR_INVALID_PARAMETER;
return VERR_INVALID_POINTER;
/*
* Allocate a new filter.
*/
if (!pNew)
return VERR_NO_MEMORY;
/*
* Insert it.
*/
else
return VINF_SUCCESS;
}
/**
* Removes an existing filter.
*
* The filter will be validate, duplicated and added.
*
* @returns IPRT status code.
* @retval VINF_SUCCESS if successfully removed.
*
* @param Owner The filter owner.
* @param uId The ID of the filter that's to be removed.
* Returned by VBoxUSBFilterAdd().
*/
{
/*
* Validate input.
*/
if (!uId)
return VERR_INVALID_PARAMETER;
return VERR_INVALID_PARAMETER;
/*
* Locate and unlink it.
*/
{
while (pCur)
{
{
if (pPrev)
else
if (!pNext)
break;
}
}
}
/*
* Free it (if found).
*/
if (pCur)
{
return VINF_SUCCESS;
}
return VERR_FILE_NOT_FOUND;
}
/**
* Removes all filters belonging to the specified owner.
*
* This is typically called when an owner disconnects or
* terminates unexpectedly.
*
* @param Owner The owner
*/
{
/*
* Collect the filters that should be freed.
*/
{
while (pCur)
{
{
if (pPrev)
else
if (!pNext)
}
else
{
}
}
}
/*
* Free any filters we've found.
*/
while (pToFree)
{
}
}
/**
* Match the specified device against the filters.
*
* @returns Owner on if matched, NIL_RTPROCESS it not matched.
* @param pDevice The device data as a filter structure.
* See USBFilterMatch for how to construct this.
* @param puId Where to store the filter id (optional).
*/
{
/*
* Validate input.
*/
/*
* Search the lists for a match.
* (The lists are ordered by priority.)
*/
{
while (pCur)
{
{
/*
* Take list specific actions and return.
*
* The code does NOT implement the case where there are two or more
* filter clients, and one of them is releaseing a device that's
* requested by some of the others. It's just too much work for a
* situation that noone will encounter.
*/
if (puId)
&& i != USBFILTERTYPE_ONESHOT_IGNORE
if ( i == USBFILTERTYPE_ONESHOT_IGNORE
|| i == USBFILTERTYPE_ONESHOT_CAPTURE)
{
/* unlink */
if (pPrev)
else
if (!pNext)
}
if ( i == USBFILTERTYPE_ONESHOT_IGNORE
|| i == USBFILTERTYPE_ONESHOT_CAPTURE)
return Owner;
}
}
}
return NIL_RTPROCESS;
}