2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/fs_poll.c
|
|
|
|
*
|
2013-05-23 15:02:44 +02:00
|
|
|
* Copyright (C) 2008-2009, 2012-2013 Gregory Nutt. All rights reserved.
|
2012-02-04 22:02:45 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2008-11-16 18:19:38 +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>
|
|
|
|
|
2009-12-15 01:18:32 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-16 18:19:38 +01:00
|
|
|
#include <poll.h>
|
2008-11-16 19:48:29 +01:00
|
|
|
#include <wdog.h>
|
2008-11-16 18:19:38 +01:00
|
|
|
#include <errno.h>
|
2008-11-16 19:48:29 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2008-11-16 18:19:38 +01:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2008-11-16 19:48:29 +01:00
|
|
|
#include <nuttx/sched.h>
|
2008-11-17 21:27:26 +01:00
|
|
|
#include <nuttx/clock.h>
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2008-11-16 18:19:38 +01:00
|
|
|
#include "fs_internal.h"
|
|
|
|
|
2008-11-16 23:05:28 +01:00
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
/****************************************************************************
|
2009-12-15 01:18:32 +01:00
|
|
|
* Pre-processor Definitions
|
2008-11-16 19:48:29 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define poll_semgive(sem) sem_post(sem)
|
|
|
|
|
2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void poll_semtake(FAR sem_t *sem)
|
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
while (sem_wait(sem) != 0)
|
|
|
|
{
|
|
|
|
/* The only case that an error should occur here is if
|
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
ASSERT(get_errno() == EINTR);
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_fdsetup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure (or unconfigure) one file/socket descriptor for the poll
|
|
|
|
* operation. If fds and sem are non-null, then the poll is being setup.
|
|
|
|
* if fds and sem are NULL, then the poll is being torn down.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2009-12-15 01:18:32 +01:00
|
|
|
static int poll_fdsetup(int fd, FAR struct pollfd *fds, bool setup)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
|
|
|
FAR struct filelist *list;
|
2013-09-28 22:47:49 +02:00
|
|
|
FAR struct file *filep;
|
2008-11-16 19:48:29 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
/* Check for a valid file descriptor */
|
|
|
|
|
|
|
|
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
|
|
|
|
{
|
|
|
|
/* Perform the socket ioctl */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
|
|
|
|
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
|
|
|
|
{
|
2008-11-19 19:43:50 +01:00
|
|
|
return net_poll(fd, fds, setup);
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the thread-specific file list */
|
|
|
|
|
|
|
|
list = sched_getfiles();
|
2013-09-28 22:47:49 +02:00
|
|
|
DEBUGASSERT(list);
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
/* Is a driver registered? Does it support the poll method?
|
|
|
|
* If not, return -ENOSYS
|
|
|
|
*/
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
filep = &list->fl_files[fd];
|
|
|
|
inode = filep->f_inode;
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
if (inode && inode->u.i_ops && inode->u.i_ops->poll)
|
|
|
|
{
|
|
|
|
/* Yes, then setup the poll */
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
ret = (int)inode->u.i_ops->poll(filep, fds, setup);
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
2013-05-23 15:02:44 +02:00
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup the poll operation for each descriptor in the list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
static inline int poll_setup(FAR struct pollfd *fds, nfds_t nfds, sem_t *sem)
|
|
|
|
{
|
2014-01-14 20:30:22 +01:00
|
|
|
unsigned int i;
|
2008-11-16 19:48:29 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Process each descriptor in the list */
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
2013-05-23 15:48:32 +02:00
|
|
|
/* Setup the poll descriptor */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2013-05-23 15:48:32 +02:00
|
|
|
fds[i].sem = sem;
|
2013-05-23 23:41:15 +02:00
|
|
|
fds[i].revents = 0;
|
2013-05-23 15:48:32 +02:00
|
|
|
fds[i].priv = NULL;
|
|
|
|
|
2013-05-23 23:41:15 +02:00
|
|
|
/* Check for invalid descriptors. "If the value of fd is less than 0,
|
|
|
|
* events shall be ignored, and revents shall be set to 0 in that entry
|
|
|
|
* on return from poll()."
|
|
|
|
*
|
|
|
|
* NOTE: There is a potential problem here. If there is only one fd
|
|
|
|
* and if it is negative, then poll will hang. From my reading of the
|
|
|
|
* spec, that appears to be the correct behavior.
|
|
|
|
*/
|
2013-05-23 15:48:32 +02:00
|
|
|
|
2013-05-23 23:41:15 +02:00
|
|
|
if (fds[i].fd >= 0)
|
2013-05-23 15:48:32 +02:00
|
|
|
{
|
|
|
|
/* Set up the poll on this valid file descriptor */
|
|
|
|
|
2013-05-23 15:02:44 +02:00
|
|
|
ret = poll_fdsetup(fds[i].fd, &fds[i], true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 15:02:44 +02:00
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown the poll operation for each descriptor in the list and return
|
|
|
|
* the count of non-zero poll events.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
static inline int poll_teardown(FAR struct pollfd *fds, nfds_t nfds, int *count)
|
|
|
|
{
|
2014-01-14 20:30:22 +01:00
|
|
|
unsigned int i;
|
2008-11-16 19:48:29 +01:00
|
|
|
int status;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Process each descriptor in the list */
|
|
|
|
|
2008-11-17 21:27:26 +01:00
|
|
|
*count = 0;
|
2008-11-16 19:48:29 +01:00
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
2013-05-23 15:02:44 +02:00
|
|
|
/* Ignore negative descriptors */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2013-05-23 15:02:44 +02:00
|
|
|
if (fds[i].fd >= 0)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
2013-05-23 15:02:44 +02:00
|
|
|
/* Teardown the poll */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2013-05-23 15:02:44 +02:00
|
|
|
status = poll_fdsetup(fds[i].fd, &fds[i], false);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
ret = status;
|
|
|
|
}
|
2013-05-23 15:48:32 +02:00
|
|
|
}
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2013-05-23 15:48:32 +02:00
|
|
|
/* Check if any events were posted */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2013-05-23 15:48:32 +02:00
|
|
|
if (fds[i].revents != 0)
|
|
|
|
{
|
|
|
|
(*count)++;
|
|
|
|
}
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2013-05-23 15:48:32 +02:00
|
|
|
/* Un-initialize the poll structure */
|
2013-05-23 15:02:44 +02:00
|
|
|
|
2013-05-23 15:48:32 +02:00
|
|
|
fds[i].sem = NULL;
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The wdog expired before any other events were received.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 01:18:32 +01:00
|
|
|
static void poll_timeout(int argc, uint32_t isem, ...)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
|
|
|
/* Wake up the poller */
|
|
|
|
|
|
|
|
FAR sem_t *sem = (FAR sem_t *)isem;
|
|
|
|
poll_semgive(sem);
|
|
|
|
}
|
|
|
|
|
2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* poll() waits for one of a set of file descriptors to become ready to
|
|
|
|
* perform I/O. If none of the events requested (and no error) has
|
|
|
|
* occurred for any of the file descriptors, then poll() blocks until
|
|
|
|
* one of the events occurs.
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* fds - List of structures describing file descriptors to be monitored
|
|
|
|
* nfds - The number of entries in the list
|
|
|
|
* timeout - Specifies an upper limit on the time for which poll() will
|
|
|
|
* block in milliseconds. A negative value of timeout means an infinite
|
|
|
|
* timeout.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* On success, the number of structures that have nonzero revents fields.
|
|
|
|
* A value of 0 indicates that the call timed out and no file descriptors
|
|
|
|
* were ready. On error, -1 is returned, and errno is set appropriately:
|
|
|
|
*
|
|
|
|
* EBADF - An invalid file descriptor was given in one of the sets.
|
|
|
|
* EFAULT - The fds address is invalid
|
|
|
|
* EINTR - A signal occurred before any requested event.
|
|
|
|
* EINVAL - The nfds value exceeds a system limit.
|
|
|
|
* ENOMEM - There was no space to allocate internal data structures.
|
|
|
|
* ENOSYS - One or more of the drivers supporting the file descriptor
|
|
|
|
* does not support the poll method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
|
|
|
|
{
|
2008-11-16 19:48:29 +01:00
|
|
|
WDOG_ID wdog;
|
|
|
|
sem_t sem;
|
2009-12-17 20:52:47 +01:00
|
|
|
int count = 0;
|
2008-11-16 19:48:29 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
sem_init(&sem, 0, 0);
|
|
|
|
ret = poll_setup(fds, nfds, &sem);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
if (timeout >= 0)
|
|
|
|
{
|
2008-11-17 21:27:26 +01:00
|
|
|
/* Wait for the poll event with a timeout. Note that the
|
|
|
|
* millisecond timeout has to be converted to system clock
|
|
|
|
* ticks for wd_start
|
|
|
|
*/
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
wdog = wd_create();
|
2009-12-15 01:18:32 +01:00
|
|
|
wd_start(wdog, MSEC2TICK(timeout), poll_timeout, 1, (uint32_t)&sem);
|
2008-11-16 19:48:29 +01:00
|
|
|
poll_semtake(&sem);
|
|
|
|
wd_delete(wdog);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-17 21:27:26 +01:00
|
|
|
/* Wait for the poll event with no timeout */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
poll_semtake(&sem);
|
|
|
|
}
|
|
|
|
|
2009-06-25 19:44:35 +02:00
|
|
|
/* Teardown the poll operation and get the count of events */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
ret = poll_teardown(fds, nfds, &count);
|
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
sem_destroy(&sem);
|
|
|
|
|
|
|
|
/* Check for errors */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-07-14 23:05:40 +02:00
|
|
|
set_errno(-ret);
|
2008-11-16 19:48:29 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2008-11-16 18:19:38 +01:00
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
return count;
|
2008-11-16 18:19:38 +01:00
|
|
|
}
|
|
|
|
|
2008-11-16 23:05:28 +01:00
|
|
|
#endif /* CONFIG_DISABLE_POLL */
|