This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new e321f58a9ef sched/fs: move ppoll mask handling into nx_poll
e321f58a9ef is described below
commit e321f58a9ef3cbb9b4f81a5f9844b20a7894c6bf
Author: hujun5 <[email protected]>
AuthorDate: Fri Sep 18 13:41:23 2026 +0800
sched/fs: move ppoll mask handling into nx_poll
Install the temporary signal mask only after poll setup has completed.
This prevents SIGCHLD from being delivered while the task is runnable
and before the poll semaphore wait is armed.
Keep the mask swap, pending-signal check and semaphore wait in one
critical section. Use the existing signal-mask helpers so protected
and kernel builds retain the temporary mask until syscall exit when
signal dispatch is deferred.
Expose nx_poll() as the errno-preserving implementation shared by poll()
and ppoll(), while retaining the existing poll return and errno behavior.
Signed-off-by: hujun5 <[email protected]>
Signed-off-by: jingfei <[email protected]>
---
fs/vfs/fs_poll.c | 66 ++++++++++++++++++++++++++++++++++++++++++-----
include/nuttx/fs/fs.h | 15 +++++++++++
sched/signal/sig_ppoll.c | 67 ++++++++++++++----------------------------------
3 files changed, 93 insertions(+), 55 deletions(-)
diff --git a/fs/vfs/fs_poll.c b/fs/vfs/fs_poll.c
index f50f3fa1ebe..df6f96b6ab7 100644
--- a/fs/vfs/fs_poll.c
+++ b/fs/vfs/fs_poll.c
@@ -35,6 +35,7 @@
#include <nuttx/clock.h>
#include <nuttx/semaphore.h>
+#include <nuttx/signal.h>
#include <nuttx/cancelpt.h>
#include <nuttx/fs/fs.h>
#include <nuttx/net/net.h>
@@ -391,10 +392,10 @@ int file_poll(FAR struct file *filep, FAR struct pollfd_s
*fds, bool setup)
}
/****************************************************************************
- * Name: poll
+ * Name: nx_poll
*
* Description:
- * poll() waits for one of a set of file descriptors to become ready to
+ * nx_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.
@@ -405,11 +406,12 @@ int file_poll(FAR struct file *filep, FAR struct pollfd_s
*fds, bool setup)
* timeout - Specifies an upper limit on the time for which poll() will
* block in milliseconds. A negative value of timeout means an infinite
* timeout.
+ * sigmask - Optional temporary signal mask, installed after poll setup.
*
* Returned Value:
* 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:
+ * were ready. On error, a negated errno value is returned:
*
* EBADF - An invalid file descriptor was given in one of the sets.
* EFAULT - The fds address is invalid
@@ -421,12 +423,15 @@ int file_poll(FAR struct file *filep, FAR struct pollfd_s
*fds, bool setup)
*
****************************************************************************/
-int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
+int nx_poll(FAR struct pollfd *fds, nfds_t nfds, int timeout,
+ FAR const sigset_t *sigmask)
{
struct pollfd_s sfds[POLL_NSTACKFDS];
struct pollinfo_s fdsinfo;
FAR struct pollfd_s *kfds;
sem_t sem;
+ sigset_t saved;
+ irqstate_t flags = 0;
int count = 0;
int ret = OK;
nfds_t i;
@@ -491,7 +496,20 @@ int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
fdsinfo.heap = kfds != sfds;
tls_cleanup_push(tls_get_info(), poll_cleanup, &fdsinfo);
- if (timeout > 0)
+ /* Driver setup can block. Keep signals masked until setup is done
+ * so a signal cannot be consumed before the poll wait is armed.
+ */
+
+ if (sigmask != NULL)
+ {
+ flags = enter_critical_section();
+ if (nxsig_waitmask_set(sigmask, &saved))
+ {
+ ret = -EINTR;
+ }
+ }
+
+ if (ret == OK && timeout > 0)
{
/* "Implementations may place limitations on the granularity of
* timeout intervals. If the requested timeout interval requires
@@ -523,13 +541,19 @@ int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
/* EINTR is the only other error expected in normal operation */
}
}
- else if (timeout < 0)
+ else if (ret == OK && timeout < 0)
{
/* Wait for the poll event or signal with no timeout */
ret = nxsem_wait(&sem);
}
+ if (sigmask != NULL)
+ {
+ nxsig_waitmask_restore(&saved);
+ leave_critical_section(flags);
+ }
+
/* Teardown the poll operation and get the count of events. Zero will
* be returned in the case of a timeout.
*
@@ -542,6 +566,19 @@ int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
tls_cleanup_pop(tls_get_info(), 0);
}
+ else if (sigmask != NULL)
+ {
+ /* poll_setup already tore down the ready descriptors. */
+
+ flags = enter_critical_section();
+ if (nxsig_waitmask_set(sigmask, &saved))
+ {
+ ret = -EINTR;
+ }
+
+ nxsig_waitmask_restore(&saved);
+ leave_critical_section(flags);
+ }
nxsem_destroy(&sem);
@@ -560,6 +597,21 @@ int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
out_with_cancelpt:
leave_cancellation_point();
+ return ret < 0 ? ret : count;
+}
+
+/****************************************************************************
+ * Name: poll
+ *
+ * Description:
+ * Wait for file descriptor events, translating internal errors to errno.
+ *
+ ****************************************************************************/
+
+int poll(FAR struct pollfd *fds, nfds_t nfds, int timeout)
+{
+ int ret = nx_poll(fds, nfds, timeout, NULL);
+
if (ret < 0)
{
set_errno(-ret);
@@ -567,6 +619,6 @@ out_with_cancelpt:
}
else
{
- return count;
+ return ret;
}
}
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 9437619d637..f038d845e5c 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -1879,6 +1879,21 @@ int file_fcntl(FAR struct file *filep, int cmd, ...);
int file_poll(FAR struct file *filep, FAR struct pollfd_s *fds, bool setup);
+/****************************************************************************
+ * Name: nx_poll
+ *
+ * Description:
+ * Internal poll implementation. Returns the number of ready descriptors
+ * or a negated errno value. It is a cancellation point, like poll().
+ * A non-NULL sigmask is installed only after driver setup and is restored
+ * after the wait, with signal dispatch deferred to syscall exit as needed.
+ * NULL leaves signal-mask handling to the caller.
+ *
+ ****************************************************************************/
+
+int nx_poll(FAR struct pollfd *fds, nfds_t nfds, int timeout,
+ FAR const sigset_t *sigmask);
+
/****************************************************************************
* Name: file_fstat
*
diff --git a/sched/signal/sig_ppoll.c b/sched/signal/sig_ppoll.c
index a39c1919207..ad56a3d0857 100644
--- a/sched/signal/sig_ppoll.c
+++ b/sched/signal/sig_ppoll.c
@@ -28,8 +28,9 @@
#include <poll.h>
+#include <nuttx/fs/fs.h>
+
#include "sched/sched.h"
-#include "signal/signal.h"
/****************************************************************************
* Public Functions
@@ -70,59 +71,29 @@ int ppoll(FAR struct pollfd *fds, nfds_t nfds,
FAR const struct timespec *timeout_ts,
FAR const sigset_t *sigmask)
{
- FAR struct tcb_s *rtcb = this_task();
- sigset_t saved_sigprocmask;
- irqstate_t flags;
- int ret = ERROR;
-
- /* Several operations must be performed below: We must determine if any
- * signal is pending and, if not, wait for the signal. Since signals can
- * be posted from the interrupt level, there is a race condition that
- * can only be eliminated by disabling interrupts!
- */
-
- flags = enter_critical_section();
-
- /* Save a copy of the old sigprocmask and install the new (temporary)
- * sigprocmask. Check if there is a pending signal corresponding to one
- * of the signals that will be unblocked by the new sigprocmask.
- */
+ sigset_t mask;
+ int timeout = -1;
+ int ret;
- if (nxsig_waitmask_set(sigmask, &saved_sigprocmask))
+ if (timeout_ts != NULL)
{
- /* Dispatching one or more of the signals is sufficient to cause
- * us to not wait. Restore the original sigprocmask.
- */
-
- nxsig_restoremask(rtcb, &saved_sigprocmask);
- leave_critical_section(flags);
- set_errno(EINTR);
+ timeout = (int)(timeout_ts->tv_sec * MSEC_PER_SEC +
+ timeout_ts->tv_nsec / NSEC_PER_MSEC);
}
- else
- {
- int timeout = -1;
-
- /* And call poll to do the real work */
- if (timeout_ts)
- {
- timeout = (int)(timeout_ts->tv_sec * MSEC_PER_SEC +
- timeout_ts->tv_nsec / NSEC_PER_MSEC);
- }
+ /* A NULL mask keeps the caller's mask but still checks pending signals. */
- ret = poll(fds, nfds, timeout);
-
- /* We are running again, restore the original sigprocmask */
-
- nxsig_restoremask(rtcb, &saved_sigprocmask);
- leave_critical_section(flags);
-
- /* Now, handle the (rare?) case where (a) a blocked signal was received
- * while the task was suspended but (b) restoring the original
- * sigprocmask will unblock the signal.
- */
+ if (sigmask == NULL)
+ {
+ mask = this_task()->sigprocmask;
+ sigmask = &mask;
+ }
- nxsig_unmask_pendingsignal();
+ ret = nx_poll(fds, nfds, timeout, sigmask);
+ if (ret < 0)
+ {
+ set_errno(-ret);
+ return ERROR;
}
return ret;