2008-11-17 21:25:28 +01:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_select.c
|
2008-11-17 21:25:28 +01:00
|
|
|
*
|
2013-03-07 18:59:21 +01:00
|
|
|
* Copyright (C) 2008-2009, 2012-2013 Gregory Nutt. All rights reserved.
|
2012-02-24 19:24:35 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2008-11-17 21:25:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/select.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2008-11-17 21:25:28 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-04-05 22:54:00 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2008-11-17 21:25:28 +01:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2008-11-17 21:25:28 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 01:18:32 +01:00
|
|
|
* Pre-processor Definitions
|
2008-11-17 21:25:28 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: select
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* select() allows a program to monitor multiple file descriptors, waiting
|
|
|
|
* until one or more of the file descriptors become "ready" for some class
|
|
|
|
* of I/O operation (e.g., input possible). A file descriptor is
|
|
|
|
* considered ready if it is possible to perform the corresponding I/O
|
|
|
|
* operation (e.g., read(2)) without blocking.
|
|
|
|
*
|
|
|
|
* NOTE: poll() is the fundamental API for performing such monitoring
|
|
|
|
* operation under NuttX. select() is provided for compatibility and
|
|
|
|
* is simply a layer of added logic on top of poll(). As such, select()
|
2008-11-19 00:00:05 +01:00
|
|
|
* is more wasteful of resources and poll() is the recommended API to be
|
2008-11-17 21:25:28 +01:00
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* Input parameters:
|
|
|
|
* nfds - the maximum fd number (+1) of any descriptor in any of the
|
|
|
|
* three sets.
|
|
|
|
* readfds - the set of descriptions to monitor for read-ready events
|
|
|
|
* writefds - the set of descriptions to monitor for write-ready events
|
|
|
|
* exceptfds - the set of descriptions to monitor for error events
|
|
|
|
* timeout - Return at this time if none of these events of interest
|
|
|
|
* occur.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* 0: Timer expired
|
|
|
|
* >0: The number of bits set in the three sets of descriptors
|
2008-11-19 00:00:05 +01:00
|
|
|
* -1: An error occurred (errno will be set appropriately)
|
2008-11-17 21:25:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int select(int nfds, FAR fd_set *readfds, FAR fd_set *writefds,
|
|
|
|
FAR fd_set *exceptfds, FAR struct timeval *timeout)
|
|
|
|
{
|
|
|
|
struct pollfd *pollset;
|
2015-07-05 19:18:56 +02:00
|
|
|
int errcode = OK;
|
2008-11-17 21:25:28 +01:00
|
|
|
int fd;
|
|
|
|
int npfds;
|
|
|
|
int msec;
|
|
|
|
int ndx;
|
|
|
|
int ret;
|
|
|
|
|
2013-03-07 18:59:21 +01:00
|
|
|
/* How many pollfd structures do we need to allocate? */
|
|
|
|
|
|
|
|
/* Initialize the descriptor list for poll() */
|
|
|
|
|
|
|
|
for (fd = 0, npfds = 0; fd < nfds; fd++)
|
|
|
|
{
|
|
|
|
/* Check if any monitor operation is requested on this fd */
|
|
|
|
|
2013-03-27 01:03:25 +01:00
|
|
|
if ((readfds && FD_ISSET(fd, readfds)) ||
|
|
|
|
(writefds && FD_ISSET(fd, writefds)) ||
|
|
|
|
(exceptfds && FD_ISSET(fd, exceptfds)))
|
2013-03-07 18:59:21 +01:00
|
|
|
{
|
|
|
|
/* Yes.. increment the count of pollfds structures needed */
|
|
|
|
|
|
|
|
npfds++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
/* Allocate the descriptor list for poll() */
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
pollset = (struct pollfd *)kmm_zalloc(npfds * sizeof(struct pollfd));
|
2008-11-17 21:25:28 +01:00
|
|
|
if (!pollset)
|
|
|
|
{
|
2012-07-14 23:05:40 +02:00
|
|
|
set_errno(ENOMEM);
|
2008-11-17 21:25:28 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the descriptor list for poll() */
|
|
|
|
|
2013-03-07 18:59:21 +01:00
|
|
|
for (fd = 0, ndx = 0; fd < nfds; fd++)
|
2008-11-17 21:25:28 +01:00
|
|
|
{
|
|
|
|
int incr = 0;
|
|
|
|
|
2012-02-24 19:24:35 +01:00
|
|
|
/* The readfs set holds the set of FDs that the caller can be assured
|
|
|
|
* of reading from without blocking. Note that POLLHUP is included as
|
|
|
|
* a read-able condition. POLLHUP will be reported at the end-of-file
|
|
|
|
* or when a connection is lost. In either case, the read() can then
|
|
|
|
* be performed without blocking.
|
|
|
|
*/
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
if (readfds && FD_ISSET(fd, readfds))
|
|
|
|
{
|
2013-03-07 18:59:21 +01:00
|
|
|
pollset[ndx].fd = fd;
|
|
|
|
pollset[ndx].events |= POLLIN;
|
|
|
|
incr = 1;
|
2008-11-17 21:25:28 +01:00
|
|
|
}
|
|
|
|
|
2012-02-24 19:24:35 +01:00
|
|
|
/* The writefds set holds the set of FDs that the caller can be assured
|
|
|
|
* of writing to without blocking.
|
|
|
|
*/
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
if (writefds && FD_ISSET(fd, writefds))
|
|
|
|
{
|
2013-03-07 18:59:21 +01:00
|
|
|
pollset[ndx].fd = fd;
|
|
|
|
pollset[ndx].events |= POLLOUT;
|
|
|
|
incr = 1;
|
2008-11-17 21:25:28 +01:00
|
|
|
}
|
|
|
|
|
2012-02-24 19:24:35 +01:00
|
|
|
/* The exceptfds set holds the set of FDs that are watched for exceptions */
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
if (exceptfds && FD_ISSET(fd, exceptfds))
|
|
|
|
{
|
2013-03-07 18:59:21 +01:00
|
|
|
pollset[ndx].fd = fd;
|
|
|
|
incr = 1;
|
2008-11-17 21:25:28 +01:00
|
|
|
}
|
|
|
|
|
2013-03-07 18:59:21 +01:00
|
|
|
ndx += incr;
|
2008-11-17 21:25:28 +01:00
|
|
|
}
|
|
|
|
|
2013-03-07 18:59:21 +01:00
|
|
|
DEBUGASSERT(ndx == npfds);
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
/* Convert the timeout to milliseconds */
|
|
|
|
|
2013-03-07 18:59:21 +01:00
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
/* Calculate the timeout in milliseconds */
|
|
|
|
|
|
|
|
msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Any negative value of msec means no timeout */
|
|
|
|
|
|
|
|
msec = -1;
|
|
|
|
}
|
2008-11-17 21:25:28 +01:00
|
|
|
|
|
|
|
/* Then let poll do all of the real work. */
|
|
|
|
|
|
|
|
ret = poll(pollset, npfds, msec);
|
2015-02-25 14:45:04 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* poll() failed! Save the errno value */
|
|
|
|
|
|
|
|
errcode = get_errno();
|
|
|
|
}
|
2008-11-17 21:25:28 +01:00
|
|
|
|
|
|
|
/* Now set up the return values */
|
|
|
|
|
|
|
|
if (readfds)
|
|
|
|
{
|
|
|
|
memset(readfds, 0, sizeof(fd_set));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writefds)
|
|
|
|
{
|
|
|
|
memset(writefds, 0, sizeof(fd_set));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exceptfds)
|
|
|
|
{
|
|
|
|
memset(exceptfds, 0, sizeof(fd_set));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the poll descriptor list back into selects 3 bitsets */
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
for (ndx = 0; ndx < npfds; ndx++)
|
|
|
|
{
|
2012-02-24 19:24:35 +01:00
|
|
|
/* Check for read conditions. Note that POLLHUP is included as a
|
|
|
|
* read condition. POLLHUP will be reported when no more data will
|
|
|
|
* be available (such as when a connection is lost). In either
|
|
|
|
* case, the read() can then be performed without blocking.
|
|
|
|
*/
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
if (readfds)
|
|
|
|
{
|
2015-10-11 19:39:29 +02:00
|
|
|
if (pollset[ndx].revents & (POLLIN | POLLHUP))
|
2008-11-17 21:25:28 +01:00
|
|
|
{
|
|
|
|
FD_SET(pollset[ndx].fd, readfds);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 19:24:35 +01:00
|
|
|
/* Check for write conditions */
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
if (writefds)
|
|
|
|
{
|
2008-11-17 22:18:03 +01:00
|
|
|
if (pollset[ndx].revents & POLLOUT)
|
2008-11-17 21:25:28 +01:00
|
|
|
{
|
|
|
|
FD_SET(pollset[ndx].fd, writefds);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 19:24:35 +01:00
|
|
|
/* Check for exceptions */
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
if (exceptfds)
|
|
|
|
{
|
2008-11-17 22:18:03 +01:00
|
|
|
if (pollset[ndx].revents & POLLERR)
|
2008-11-17 21:25:28 +01:00
|
|
|
{
|
|
|
|
FD_SET(pollset[ndx].fd, exceptfds);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(pollset);
|
2015-02-25 14:45:04 +01:00
|
|
|
|
|
|
|
/* Did poll() fail above? */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Yes.. restore the errno value */
|
|
|
|
|
|
|
|
set_errno(errcode);
|
|
|
|
}
|
|
|
|
|
2008-11-17 21:25:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_POLL */
|
|
|
|
|