2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_poll.c
|
2008-11-16 18:19:38 +01:00
|
|
|
*
|
2018-11-08 16:59:18 +01:00
|
|
|
* Copyright (C) 2008-2009, 2012-2018 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 <stdbool.h>
|
2008-11-16 18:19:38 +01:00
|
|
|
#include <poll.h>
|
2015-08-13 22:28:31 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <semaphore.h>
|
2008-11-16 19:48:29 +01:00
|
|
|
#include <assert.h>
|
2015-08-13 22:28:31 +02:00
|
|
|
#include <errno.h>
|
2008-11-16 18:19:38 +01:00
|
|
|
|
2008-11-17 21:27:26 +01:00
|
|
|
#include <nuttx/clock.h>
|
2016-11-03 19:23:31 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2014-08-21 19:16:55 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-08-13 22:28:31 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2014-09-26 14:28:20 +02:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2008-11-16 18:19:38 +01:00
|
|
|
|
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
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define poll_semgive(sem) nxsem_post(sem)
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-19 16:31:51 +01:00
|
|
|
static int poll_semtake(FAR sem_t *sem)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
2014-11-19 16:31:51 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(sem);
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* The only case that an error should occur here is if the wait were
|
|
|
|
* awakened by a signal.
|
|
|
|
*/
|
2014-11-19 16:31:51 +01:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
|
|
|
return ret;
|
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
|
|
|
{
|
|
|
|
/* 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
|
2018-11-08 16:59:18 +01:00
|
|
|
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS))
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 19:21:11 +02:00
|
|
|
return fdesc_poll(fd, fds, setup);
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
#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;
|
2015-05-04 17:09:09 +02:00
|
|
|
unsigned int j;
|
2018-08-26 21:44:07 +02:00
|
|
|
int ret = OK;
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2018-08-26 21:44:07 +02:00
|
|
|
switch (fds[i].events & POLLMASK)
|
2013-05-23 15:48:32 +02:00
|
|
|
{
|
2018-08-26 21:44:07 +02:00
|
|
|
case POLLFD:
|
|
|
|
if (fds[i].fd >= 0)
|
|
|
|
{
|
|
|
|
ret = poll_fdsetup(fds[i].fd, &fds[i], true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POLLFILE:
|
|
|
|
if (fds[i].ptr != NULL)
|
|
|
|
{
|
|
|
|
ret = file_poll(fds[i].ptr, &fds[i], true);
|
|
|
|
}
|
|
|
|
break;
|
2013-05-23 15:48:32 +02:00
|
|
|
|
2018-08-26 21:44:07 +02:00
|
|
|
#ifdef CONFIG_NET
|
|
|
|
case POLLSOCK:
|
|
|
|
if (fds[i].ptr != NULL)
|
2013-05-23 15:02:44 +02:00
|
|
|
{
|
2018-08-26 21:44:07 +02:00
|
|
|
ret = psock_poll(fds[i].ptr, &fds[i], true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2015-05-04 17:09:09 +02:00
|
|
|
|
2018-08-26 21:44:07 +02:00
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Setup failed for fds[i]. We now need to teardown previously
|
|
|
|
* setup fds[0 .. (i - 1)] to release allocated resources and
|
|
|
|
* to prevent memory corruption by access to freed/released 'fds'
|
|
|
|
* and 'sem'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
{
|
|
|
|
switch (fds[j].events & POLLMASK)
|
2015-05-04 17:09:09 +02:00
|
|
|
{
|
2018-08-26 21:44:07 +02:00
|
|
|
case POLLFD:
|
2015-05-04 17:09:09 +02:00
|
|
|
(void)poll_fdsetup(fds[j].fd, &fds[j], false);
|
2018-08-26 21:44:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case POLLFILE:
|
|
|
|
(void)file_poll(fds[j].ptr, &fds[j], false);
|
|
|
|
break;
|
2015-05-04 17:09:09 +02:00
|
|
|
|
2018-08-26 21:44:07 +02:00
|
|
|
#ifdef CONFIG_NET
|
|
|
|
case POLLSOCK:
|
|
|
|
(void)psock_poll(fds[j].ptr, &fds[j], false);
|
|
|
|
break;
|
|
|
|
#endif
|
2015-08-13 22:58:52 +02:00
|
|
|
|
2018-08-26 21:44:07 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-05-23 15:02:44 +02:00
|
|
|
}
|
2018-08-26 21:44:07 +02:00
|
|
|
|
|
|
|
/* Indicate an error on the file descriptor */
|
|
|
|
|
|
|
|
fds[i].revents |= POLLERR;
|
|
|
|
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
|
2014-11-19 16:31:51 +01:00
|
|
|
static inline int poll_teardown(FAR struct pollfd *fds, nfds_t nfds, int *count,
|
|
|
|
int ret)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
2014-01-14 20:30:22 +01:00
|
|
|
unsigned int i;
|
2018-08-26 21:44:07 +02:00
|
|
|
int status = OK;
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
/* 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++)
|
|
|
|
{
|
2018-08-26 21:44:07 +02:00
|
|
|
switch (fds[i].events & POLLMASK)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
2018-08-26 21:44:07 +02:00
|
|
|
case POLLFD:
|
|
|
|
if (fds[i].fd >= 0)
|
|
|
|
{
|
|
|
|
status = poll_fdsetup(fds[i].fd, &fds[i], false);
|
|
|
|
}
|
|
|
|
break;
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2018-08-26 21:44:07 +02:00
|
|
|
case POLLFILE:
|
|
|
|
if (fds[i].ptr != NULL)
|
2013-05-23 15:02:44 +02:00
|
|
|
{
|
2018-08-26 21:44:07 +02:00
|
|
|
status = file_poll(fds[i].ptr, &fds[i], false);
|
2013-05-23 15:02:44 +02:00
|
|
|
}
|
2018-08-26 21:44:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
case POLLSOCK:
|
|
|
|
if (fds[i].ptr != NULL)
|
|
|
|
{
|
|
|
|
status = psock_poll(fds[i].ptr, &fds[i], false);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
status = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-15 19:34:08 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: file_poll
|
2016-07-15 19:34:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-26 21:44:07 +02:00
|
|
|
* Low-level poll operation based on struct file. This is used both to (1)
|
2016-07-15 19:34:08 +02:00
|
|
|
* support detached file, and also (2) by fdesc_poll() to perform all
|
|
|
|
* normal operations on file descriptors descriptors.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* file File structure instance
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
int file_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2017-08-03 17:58:20 +02:00
|
|
|
if (inode != NULL)
|
2016-07-15 19:34:08 +02:00
|
|
|
{
|
2017-08-03 17:58:20 +02:00
|
|
|
/* Is a driver registered? Does it support the poll method?
|
|
|
|
* If not, return -ENOSYS
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (INODE_IS_DRIVER(inode) &&
|
|
|
|
inode->u.i_ops != NULL && inode->u.i_ops->poll != NULL)
|
|
|
|
{
|
|
|
|
/* Yes, it does... Setup the poll */
|
|
|
|
|
|
|
|
ret = (int)inode->u.i_ops->poll(filep, fds, setup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Regular files (and block devices) are always readable and
|
|
|
|
* writable. Open Group: "Regular files shall always poll TRUE for
|
|
|
|
* reading and writing."
|
|
|
|
*/
|
2016-07-15 19:34:08 +02:00
|
|
|
|
2018-11-08 16:59:18 +01:00
|
|
|
if (INODE_IS_MOUNTPT(inode) || INODE_IS_BLOCK(inode) ||
|
|
|
|
INODE_IS_MTD(inode))
|
2017-08-03 17:58:20 +02:00
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
fds->revents |= (fds->events & (POLLIN | POLLOUT));
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(fds->sem);
|
2017-08-03 17:58:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
2016-07-15 19:34:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: fdesc_poll
|
2015-05-12 15:41:12 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on file descriptors
|
|
|
|
* to this function.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The file descriptor of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-10-11 16:39:19 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
2015-05-12 15:41:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2016-07-15 19:21:11 +02:00
|
|
|
int fdesc_poll(int fd, FAR struct pollfd *fds, bool setup)
|
2015-05-12 15:41:12 +02:00
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
2017-10-11 16:39:19 +02:00
|
|
|
int ret;
|
2015-05-12 15:41:12 +02:00
|
|
|
|
|
|
|
/* Get the file pointer corresponding to this file descriptor */
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
2015-05-12 15:41:12 +02:00
|
|
|
{
|
2017-10-11 16:39:19 +02:00
|
|
|
return ret;
|
2015-05-12 15:41:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:39:19 +02:00
|
|
|
DEBUGASSERT(filep != NULL);
|
|
|
|
|
2016-07-15 19:34:08 +02:00
|
|
|
/* Let file_poll() do the rest */
|
2015-05-12 15:41:12 +02:00
|
|
|
|
2016-07-15 19:34:08 +02:00
|
|
|
return file_poll(filep, fds, setup);
|
2015-05-12 15:41:12 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-16 18:19:38 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2008-11-16 18:19:38 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2014-09-25 14:30:25 +02:00
|
|
|
* On success, the number of structures that have non-zero revents fields.
|
2008-11-16 18:19:38 +01:00
|
|
|
* 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:
|
|
|
|
*
|
2014-09-26 16:25:00 +02:00
|
|
|
* EBADF - An invalid file descriptor was given in one of the sets.
|
2008-11-16 18:19:38 +01:00
|
|
|
* EFAULT - The fds address is invalid
|
2014-09-26 16:25:00 +02:00
|
|
|
* EINTR - A signal occurred before any requested event.
|
2008-11-16 18:19:38 +01:00
|
|
|
* 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
|
|
|
sem_t sem;
|
2009-12-17 20:52:47 +01:00
|
|
|
int count = 0;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2008-11-16 19:48:29 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-12-27 15:45:26 +01:00
|
|
|
DEBUGASSERT(nfds == 0 || fds != NULL);
|
2018-09-19 20:15:04 +02:00
|
|
|
|
2016-12-09 19:01:18 +01:00
|
|
|
/* poll() is a cancellation point */
|
|
|
|
|
2016-12-10 15:36:58 +01:00
|
|
|
(void)enter_cancellation_point();
|
2016-12-09 19:01:18 +01:00
|
|
|
|
2016-11-03 19:23:31 +01:00
|
|
|
/* This semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&sem, 0, 0);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_setprotocol(&sem, SEM_PRIO_NONE);
|
2016-11-03 19:23:31 +01:00
|
|
|
|
2008-11-16 19:48:29 +01:00
|
|
|
ret = poll_setup(fds, nfds, &sem);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2014-09-25 14:30:25 +02:00
|
|
|
if (timeout == 0)
|
|
|
|
{
|
2014-09-26 14:28:20 +02:00
|
|
|
/* Poll returns immediately whether we have a poll event or not. */
|
2014-11-19 16:31:51 +01:00
|
|
|
|
|
|
|
ret = OK;
|
2014-09-25 14:30:25 +02:00
|
|
|
}
|
|
|
|
else if (timeout > 0)
|
2008-11-16 19:48:29 +01:00
|
|
|
{
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t ticks;
|
2017-04-28 16:42:37 +02:00
|
|
|
|
|
|
|
/* "Implementations may place limitations on the granularity of
|
|
|
|
* timeout intervals. If the requested timeout interval requires
|
|
|
|
* a finer granularity than the implementation supports, the
|
|
|
|
* actual timeout interval will be rounded up to the next
|
|
|
|
* supported value." -- opengroup.org
|
|
|
|
*
|
|
|
|
* Round timeout up to next full tick.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if (MSEC_PER_TICK * USEC_PER_MSEC) != USEC_PER_TICK && \
|
|
|
|
defined(CONFIG_HAVE_LONG_LONG)
|
2017-06-09 19:37:30 +02:00
|
|
|
ticks = (((unsigned long long)timeout * USEC_PER_MSEC) + (USEC_PER_TICK - 1)) /
|
2017-04-28 16:42:37 +02:00
|
|
|
USEC_PER_TICK;
|
|
|
|
#else
|
2017-04-28 19:02:54 +02:00
|
|
|
ticks = ((unsigned int)timeout + (MSEC_PER_TICK - 1)) / MSEC_PER_TICK;
|
2017-04-28 16:42:37 +02:00
|
|
|
#endif
|
|
|
|
|
2014-11-19 16:31:51 +01:00
|
|
|
/* Either wait for either a poll event(s), for a signal to occur,
|
|
|
|
* or for the specified timeout to elapse with no event.
|
2014-09-26 14:28:20 +02:00
|
|
|
*
|
|
|
|
* NOTE: If a poll event is pending (i.e., the semaphore has already
|
2017-10-03 20:51:15 +02:00
|
|
|
* been incremented), nxsem_tickwait() will not wait, but will return
|
2014-09-26 14:28:20 +02:00
|
|
|
* immediately.
|
2008-11-17 21:27:26 +01:00
|
|
|
*/
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
ret = nxsem_tickwait(&sem, clock_systimer(), ticks);
|
2014-11-21 13:38:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-01-27 20:42:39 +01:00
|
|
|
if (ret == -ETIMEDOUT)
|
2014-11-21 13:38:26 +01:00
|
|
|
{
|
|
|
|
/* Return zero (OK) in the event of a timeout */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
2014-11-21 15:20:25 +01:00
|
|
|
|
2016-01-27 20:42:39 +01:00
|
|
|
/* EINTR is the only other error expected in normal operation */
|
2014-11-21 13:38:26 +01:00
|
|
|
}
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-19 16:31:51 +01:00
|
|
|
/* Wait for the poll event or signal with no timeout */
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2014-11-19 16:31:51 +01:00
|
|
|
ret = poll_semtake(&sem);
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
|
|
|
|
2014-09-26 16:25:00 +02:00
|
|
|
/* Teardown the poll operation and get the count of events. Zero will be
|
|
|
|
* returned in the case of a timeout.
|
2016-01-27 20:42:39 +01:00
|
|
|
*
|
|
|
|
* Preserve ret, if negative, since it holds the result of the wait.
|
2014-09-26 16:25:00 +02:00
|
|
|
*/
|
2008-11-16 19:48:29 +01:00
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = poll_teardown(fds, nfds, &count, ret);
|
|
|
|
if (errcode < 0 && ret >= 0)
|
2016-01-27 20:42:39 +01:00
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
ret = errcode;
|
2016-01-27 20:42:39 +01:00
|
|
|
}
|
2008-11-16 19:48:29 +01:00
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&sem);
|
2016-12-09 19:01:18 +01:00
|
|
|
leave_cancellation_point();
|
2008-11-16 19:48:29 +01:00
|
|
|
|
|
|
|
/* 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 */
|