2008-11-17 01:19:38 +08:00
|
|
|
/****************************************************************************
|
2014-09-29 01:46:11 +08:00
|
|
|
* fs/vfs/fs_poll.c
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
2024-11-05 16:32:55 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <poll.h>
|
2015-08-14 04:28:31 +08:00
|
|
|
#include <time.h>
|
2008-11-17 02:48:29 +08:00
|
|
|
#include <assert.h>
|
2015-08-14 04:28:31 +08:00
|
|
|
#include <errno.h>
|
2022-09-19 11:08:57 +08:00
|
|
|
#include <debug.h>
|
2008-11-17 01:19:38 +08:00
|
|
|
|
2008-11-18 04:27:26 +08:00
|
|
|
#include <nuttx/clock.h>
|
2016-11-04 02:23:31 +08:00
|
|
|
#include <nuttx/semaphore.h>
|
2016-12-10 23:08:26 +08:00
|
|
|
#include <nuttx/cancelpt.h>
|
2014-08-22 01:16:55 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-08-14 04:28:31 +08:00
|
|
|
#include <nuttx/net/net.h>
|
2024-02-05 22:55:13 +08:00
|
|
|
#include <nuttx/tls.h>
|
2008-11-17 02:48:29 +08:00
|
|
|
|
2014-09-26 20:28:20 +08:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-09-29 21:14:38 +08:00
|
|
|
#include "inode/inode.h"
|
2024-09-09 20:28:16 +08:00
|
|
|
#include "fs_heap.h"
|
2008-11-17 01:19:38 +08:00
|
|
|
|
2024-02-05 22:55:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct pollfd_s
|
|
|
|
{
|
|
|
|
FAR struct pollfd *fds;
|
|
|
|
nfds_t nfds;
|
|
|
|
};
|
|
|
|
|
2008-11-17 01:19:38 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-27 15:31:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown the poll operation for each descriptor in the list and return
|
|
|
|
* the count of non-zero poll events.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void poll_teardown(FAR struct pollfd *fds, nfds_t nfds,
|
|
|
|
FAR int *count)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Process each descriptor in the list */
|
|
|
|
|
|
|
|
*count = 0;
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
|
|
|
if (fds[i].fd >= 0)
|
|
|
|
{
|
|
|
|
int status = poll_fdsetup(fds[i].fd, &fds[i], false);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
fds[i].revents |= POLLERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if any events were posted */
|
|
|
|
|
|
|
|
if (fds[i].revents != 0)
|
|
|
|
{
|
|
|
|
(*count)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Un-initialize the poll structure */
|
|
|
|
|
|
|
|
fds[i].arg = NULL;
|
|
|
|
fds[i].cb = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-17 02:48:29 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup the poll operation for each descriptor in the list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-06-03 23:23:19 +08:00
|
|
|
static inline int poll_setup(FAR struct pollfd *fds, nfds_t nfds,
|
|
|
|
FAR sem_t *sem)
|
2008-11-17 02:48:29 +08:00
|
|
|
{
|
2014-01-15 03:30:22 +08:00
|
|
|
unsigned int i;
|
2018-08-27 03:44:07 +08:00
|
|
|
int ret = OK;
|
2024-08-27 15:31:32 +08:00
|
|
|
int count = 0;
|
2008-11-17 02:48:29 +08:00
|
|
|
|
|
|
|
/* Process each descriptor in the list */
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
2019-06-14 06:22:41 +08:00
|
|
|
/* Setup the poll descriptor
|
|
|
|
*
|
|
|
|
* REVISIT: In a multi-threaded environment, one use case might be to
|
|
|
|
* share a single, array of struct pollfd in poll() calls on different
|
|
|
|
* threads. That use case is not supportable here because the non-
|
|
|
|
* standard internal fields get reset here on each call to poll()
|
|
|
|
* on each thread.
|
|
|
|
*/
|
2008-11-17 02:48:29 +08:00
|
|
|
|
2022-09-28 11:30:28 +08:00
|
|
|
fds[i].arg = sem;
|
|
|
|
fds[i].cb = poll_default_cb;
|
2013-05-24 05:41:15 +08:00
|
|
|
fds[i].revents = 0;
|
2013-05-23 21:48:32 +08:00
|
|
|
fds[i].priv = NULL;
|
|
|
|
|
2013-05-24 05:41:15 +08: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 21:48:32 +08:00
|
|
|
|
2022-11-11 03:47:07 +08:00
|
|
|
if (fds[i].fd >= 0)
|
2013-05-23 21:48:32 +08:00
|
|
|
{
|
2022-11-11 03:47:07 +08:00
|
|
|
ret = poll_fdsetup(fds[i].fd, &fds[i], true);
|
2024-08-27 15:31:32 +08:00
|
|
|
if (ret < 0)
|
2018-08-27 03:44:07 +08:00
|
|
|
{
|
2024-08-27 15:31:32 +08:00
|
|
|
poll_teardown(fds, i, &count);
|
|
|
|
fds[i].revents |= POLLERR;
|
|
|
|
fds[i].arg = NULL;
|
|
|
|
fds[i].cb = NULL;
|
|
|
|
return count + 1;
|
|
|
|
}
|
|
|
|
else if (fds[i].revents != 0)
|
|
|
|
{
|
|
|
|
count++;
|
2013-05-23 21:02:44 +08:00
|
|
|
}
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 21:02:44 +08:00
|
|
|
|
2024-08-27 15:31:32 +08:00
|
|
|
if (count > 0)
|
2008-11-17 02:48:29 +08:00
|
|
|
{
|
2024-08-27 15:31:32 +08:00
|
|
|
/* If there are already events available in poll_setup,
|
|
|
|
* we execute teardown and return immediately.
|
|
|
|
*/
|
2013-05-23 21:02:44 +08:00
|
|
|
|
2024-08-27 15:31:32 +08:00
|
|
|
poll_teardown(fds, i, &count);
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
2012-07-15 05:05:40 +08:00
|
|
|
|
2024-08-27 15:31:32 +08:00
|
|
|
return count;
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
|
|
|
|
2024-02-05 22:55:13 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_cleanup
|
|
|
|
*
|
|
|
|
* Description:
|
2023-11-04 20:11:19 +08:00
|
|
|
* Cleanup the poll operation.
|
2024-02-05 22:55:13 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void poll_cleanup(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct pollfd_s *fdsinfo = (FAR struct pollfd_s *)arg;
|
|
|
|
int count;
|
|
|
|
|
2024-08-27 15:12:49 +08:00
|
|
|
poll_teardown(fdsinfo->fds, fdsinfo->nfds, &count);
|
2024-02-05 22:55:13 +08:00
|
|
|
}
|
|
|
|
|
2008-11-17 01:19:38 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-13 22:46:00 +08: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int poll_fdsetup(int fd, FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the file pointer corresponding to this file descriptor */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(filep != NULL);
|
|
|
|
|
|
|
|
/* Let file_poll() do the rest */
|
|
|
|
|
2023-12-25 15:04:01 +08:00
|
|
|
ret = file_poll(filep, fds, setup);
|
|
|
|
fs_putfilep(filep);
|
|
|
|
return ret;
|
2022-11-13 22:46:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_default_cb
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The default poll callback function, this function do the final step of
|
|
|
|
* poll notification.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fds - The fds
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void poll_default_cb(FAR struct pollfd *fds)
|
|
|
|
{
|
|
|
|
int semcount = 0;
|
|
|
|
FAR sem_t *pollsem;
|
|
|
|
|
|
|
|
if (fds->arg != NULL)
|
|
|
|
{
|
|
|
|
pollsem = (FAR sem_t *)fds->arg;
|
|
|
|
nxsem_get_value(pollsem, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(pollsem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 11:08:57 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: poll_notify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Notify the poll, this function should be called by drivers to notify
|
|
|
|
* the caller the poll is ready.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* afds - The fds array
|
|
|
|
* nfds - Number of fds array
|
|
|
|
* eventset - List of events to check for activity
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void poll_notify(FAR struct pollfd **afds, int nfds, pollevent_t eventset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FAR struct pollfd *fds;
|
|
|
|
|
|
|
|
DEBUGASSERT(afds != NULL && nfds >= 1);
|
|
|
|
|
|
|
|
for (i = 0; i < nfds && eventset; i++)
|
|
|
|
{
|
|
|
|
fds = afds[i];
|
|
|
|
if (fds != NULL)
|
|
|
|
{
|
|
|
|
/* The error event must be set in fds->revents */
|
|
|
|
|
|
|
|
fds->revents |= eventset & (fds->events | POLLERR | POLLHUP);
|
|
|
|
if ((fds->revents & (POLLERR | POLLHUP)) != 0)
|
|
|
|
{
|
2022-09-26 17:14:52 +08:00
|
|
|
/* Error or Hung up, clear POLLOUT event */
|
2022-09-19 11:08:57 +08:00
|
|
|
|
2022-09-26 17:14:52 +08:00
|
|
|
fds->revents &= ~POLLOUT;
|
2022-09-19 11:08:57 +08:00
|
|
|
}
|
|
|
|
|
2023-09-22 15:17:15 +08:00
|
|
|
if ((fds->revents != 0 || (fds->events & POLLALWAYS) != 0) &&
|
|
|
|
fds->cb != NULL)
|
2022-09-19 11:08:57 +08:00
|
|
|
{
|
|
|
|
finfo("Report events: %08" PRIx32 "\n", fds->revents);
|
2022-09-28 11:30:28 +08:00
|
|
|
fds->cb(fds);
|
2022-09-19 11:08:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 01:34:08 +08:00
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: file_poll
|
2016-07-16 01:34:08 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-27 03:44:07 +08:00
|
|
|
* Low-level poll operation based on struct file. This is used both to (1)
|
2021-02-15 18:49:34 +08:00
|
|
|
* support detached file, and also (2) by poll_fdsetup() to perform all
|
2020-04-13 22:41:12 +08:00
|
|
|
* normal operations on file descriptors.
|
2016-07-16 01:34:08 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
2021-09-09 11:38:03 +08:00
|
|
|
DEBUGASSERT(filep != NULL);
|
2016-07-16 01:34:08 +08:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2017-08-03 23:58:20 +08:00
|
|
|
if (inode != NULL)
|
2016-07-16 01:34:08 +08:00
|
|
|
{
|
2017-08-03 23:58:20 +08:00
|
|
|
/* Is a driver registered? Does it support the poll method?
|
|
|
|
* If not, return -ENOSYS
|
|
|
|
*/
|
|
|
|
|
2021-01-08 13:43:33 +08:00
|
|
|
if ((INODE_IS_DRIVER(inode) || INODE_IS_MQUEUE(inode) ||
|
2023-06-07 21:57:30 +08:00
|
|
|
INODE_IS_SOCKET(inode) || INODE_IS_PIPE(inode)) &&
|
2017-08-03 23:58:20 +08:00
|
|
|
inode->u.i_ops != NULL && inode->u.i_ops->poll != NULL)
|
|
|
|
{
|
|
|
|
/* Yes, it does... Setup the poll */
|
|
|
|
|
2023-11-25 20:11:03 +08:00
|
|
|
ret = inode->u.i_ops->poll(filep, fds, setup);
|
2024-07-26 11:24:12 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("poll failed:%p, setup:%d, ret:%d\n",
|
|
|
|
inode->u.i_ops->poll, setup, ret);
|
|
|
|
}
|
2017-08-03 23:58:20 +08:00
|
|
|
}
|
2023-11-25 20:11:03 +08:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
else if (INODE_IS_MOUNTPT(inode) && inode->u.i_mops != NULL &&
|
|
|
|
inode->u.i_mops->poll != NULL)
|
|
|
|
{
|
|
|
|
ret = inode->u.i_mops->poll(filep, fds, setup);
|
2024-07-26 11:24:12 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("poll failed:%p, setup:%d, ret:%d\n",
|
|
|
|
inode->u.i_ops->poll, setup, ret);
|
|
|
|
}
|
2023-11-25 20:11:03 +08:00
|
|
|
}
|
|
|
|
#endif
|
2017-08-03 23:58:20 +08:00
|
|
|
|
|
|
|
/* Regular files (and block devices) are always readable and
|
|
|
|
* writable. Open Group: "Regular files shall always poll TRUE for
|
|
|
|
* reading and writing."
|
|
|
|
*/
|
2016-07-16 01:34:08 +08:00
|
|
|
|
2023-11-25 20:11:03 +08:00
|
|
|
else if (INODE_IS_MOUNTPT(inode) || INODE_IS_BLOCK(inode) ||
|
|
|
|
INODE_IS_MTD(inode))
|
2017-08-03 23:58:20 +08:00
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
2022-09-19 11:08:57 +08:00
|
|
|
poll_notify(&fds, 1, POLLIN | POLLOUT);
|
2017-08-03 23:58:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
2016-07-16 01:34:08 +08:00
|
|
|
}
|
2021-09-09 11:38:03 +08:00
|
|
|
else
|
|
|
|
{
|
2022-09-19 11:08:57 +08:00
|
|
|
poll_notify(&fds, 1, POLLERR | POLLHUP);
|
2021-09-09 11:38:03 +08:00
|
|
|
ret = OK;
|
|
|
|
}
|
2016-07-16 01:34:08 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-17 01:19:38 +08:00
|
|
|
/****************************************************************************
|
2022-10-26 13:50:25 +08:00
|
|
|
* Name: poll
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2022-10-26 13:50:25 +08:00
|
|
|
* 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.
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
2022-10-26 13:50:25 +08:00
|
|
|
* Input Parameters:
|
|
|
|
* 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.
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2022-10-26 13:50:25 +08:00
|
|
|
* On success, the number of structures that have non-zero 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.
|
2008-11-17 01:19:38 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-10-26 13:50:25 +08:00
|
|
|
int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
|
2008-11-17 01:19:38 +08:00
|
|
|
{
|
2023-01-11 16:15:45 +08:00
|
|
|
FAR struct pollfd *kfds;
|
2008-11-17 02:48:29 +08:00
|
|
|
sem_t sem;
|
2009-12-18 03:52:47 +08:00
|
|
|
int count = 0;
|
2024-08-27 15:31:32 +08:00
|
|
|
int ret = OK;
|
2008-11-17 02:48:29 +08:00
|
|
|
|
2018-12-27 22:45:26 +08:00
|
|
|
DEBUGASSERT(nfds == 0 || fds != NULL);
|
2018-09-20 02:15:04 +08:00
|
|
|
|
2022-10-26 13:50:25 +08:00
|
|
|
/* poll() is a cancellation point */
|
|
|
|
|
|
|
|
enter_cancellation_point();
|
|
|
|
|
2023-01-10 23:24:34 +08:00
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
|
|
|
/* Allocate kernel memory for the fds */
|
|
|
|
|
2024-09-09 20:28:16 +08:00
|
|
|
kfds = fs_heap_malloc(nfds * sizeof(struct pollfd));
|
2023-01-10 23:24:34 +08:00
|
|
|
if (!kfds)
|
|
|
|
{
|
|
|
|
/* Out of memory */
|
|
|
|
|
2023-05-25 18:01:34 +08:00
|
|
|
ret = -ENOMEM;
|
2023-01-10 23:24:34 +08:00
|
|
|
goto out_with_cancelpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the user fds to neutral kernel memory */
|
|
|
|
|
|
|
|
memcpy(kfds, fds, nfds * sizeof(struct pollfd));
|
|
|
|
#else
|
|
|
|
/* Can use the user fds directly */
|
|
|
|
|
|
|
|
kfds = fds;
|
|
|
|
#endif
|
|
|
|
|
2024-02-05 22:55:13 +08:00
|
|
|
/* Set up the poll structure */
|
|
|
|
|
2017-10-04 02:51:15 +08:00
|
|
|
nxsem_init(&sem, 0, 0);
|
2024-08-27 15:31:32 +08:00
|
|
|
|
|
|
|
/* If there are already events available in poll_setup,
|
|
|
|
* we return immediately
|
|
|
|
*/
|
|
|
|
|
|
|
|
count = poll_setup(kfds, nfds, &sem);
|
|
|
|
if (count == 0)
|
2008-11-17 02:48:29 +08:00
|
|
|
{
|
2024-02-05 22:55:13 +08:00
|
|
|
struct pollfd_s fdsinfo;
|
|
|
|
|
|
|
|
/* Push a cancellation point onto the stack. This will be called if
|
|
|
|
* the thread is canceled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fdsinfo.fds = kfds;
|
|
|
|
fdsinfo.nfds = nfds;
|
|
|
|
tls_cleanup_push(tls_get_info(), poll_cleanup, &fdsinfo);
|
|
|
|
|
2024-08-27 15:31:32 +08:00
|
|
|
if (timeout > 0)
|
2008-11-17 02:48:29 +08:00
|
|
|
{
|
2017-04-28 22:42:37 +08: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.
|
|
|
|
*/
|
|
|
|
|
2014-11-19 23:31:51 +08: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 20:28:20 +08:00
|
|
|
*
|
2019-03-02 05:00:00 +08:00
|
|
|
* NOTE: If a poll event is pending (i.e., the semaphore has
|
|
|
|
* already been incremented), nxsem_tickwait() will not wait, but
|
|
|
|
* will return immediately.
|
2008-11-18 04:27:26 +08:00
|
|
|
*/
|
2008-11-17 02:48:29 +08:00
|
|
|
|
2024-08-20 15:57:04 +08:00
|
|
|
ret = nxsem_tickwait(&sem, MSEC2TICK((clock_t)timeout));
|
2019-10-28 01:48:14 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (ret == -ETIMEDOUT)
|
|
|
|
{
|
|
|
|
/* Return zero (OK) in the event of a timeout */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EINTR is the only other error expected in normal operation */
|
|
|
|
}
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
2024-08-27 15:31:32 +08:00
|
|
|
else if (timeout < 0)
|
2008-11-17 02:48:29 +08:00
|
|
|
{
|
2014-11-19 23:31:51 +08:00
|
|
|
/* Wait for the poll event or signal with no timeout */
|
2008-11-17 02:48:29 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxsem_wait(&sem);
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
|
|
|
|
2019-03-02 05:00:00 +08:00
|
|
|
/* Teardown the poll operation and get the count of events. Zero will
|
|
|
|
* be returned in the case of a timeout.
|
2016-01-28 03:42:39 +08:00
|
|
|
*
|
|
|
|
* Preserve ret, if negative, since it holds the result of the wait.
|
2014-09-26 22:25:00 +08:00
|
|
|
*/
|
2008-11-17 02:48:29 +08:00
|
|
|
|
2024-08-27 15:31:32 +08:00
|
|
|
poll_teardown(kfds, nfds, &count);
|
2024-02-05 22:55:13 +08:00
|
|
|
|
|
|
|
/* Pop the cancellation point */
|
|
|
|
|
|
|
|
tls_cleanup_pop(tls_get_info(), 0);
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
2012-07-15 05:05:40 +08:00
|
|
|
|
2017-10-04 05:35:24 +08:00
|
|
|
nxsem_destroy(&sem);
|
2023-01-10 23:24:34 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
|
|
|
/* Copy the events back to user */
|
|
|
|
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nfds; i++)
|
|
|
|
{
|
|
|
|
fds[i].revents = kfds[i].revents;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the temporary buffer */
|
|
|
|
|
2024-09-09 20:28:16 +08:00
|
|
|
fs_heap_free(kfds);
|
2023-01-10 23:24:34 +08:00
|
|
|
|
|
|
|
out_with_cancelpt:
|
|
|
|
#endif
|
|
|
|
|
2022-10-26 13:50:25 +08:00
|
|
|
leave_cancellation_point();
|
2008-11-17 02:48:29 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-07-15 05:05:40 +08:00
|
|
|
set_errno(-ret);
|
2022-10-26 13:50:25 +08:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return count;
|
2008-11-17 02:48:29 +08:00
|
|
|
}
|
2008-11-17 01:19:38 +08:00
|
|
|
}
|