sig_ppoll.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /****************************************************************************
  2. * sched/signal/sig_ppoll.c
  3. *
  4. * Copyright (C) 2018 Pinecone Inc. All rights reserved.
  5. * Author: Xiang Xiao <xiaoxiang@pinecone.net>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <poll.h>
  40. #include "sched/sched.h"
  41. #include "signal/signal.h"
  42. /****************************************************************************
  43. * Public Functions
  44. ****************************************************************************/
  45. /****************************************************************************
  46. * Name: ppoll
  47. *
  48. * Description:
  49. * ppoll() waits for one of a set of file descriptors to become ready to
  50. * perform I/O. If none of the events requested (and no error) has
  51. * occurred for any of the file descriptors, then ppoll() blocks until
  52. * one of the events occurs.
  53. *
  54. * Input Parameters:
  55. * fds - List of structures describing file descriptors to be monitored
  56. * nfds - The number of entries in the list
  57. * timeout - Specifies an upper limit on the time for which ppoll() will
  58. * block. A NULL pointer means an infinite timeout.
  59. * sigmask - Replace the current signal mask temporarily during execution
  60. *
  61. * Returned Value:
  62. * On success, the number of structures that have non-zero revents fields.
  63. * A value of 0 indicates that the call timed out and no file descriptors
  64. * were ready. On error, -1 is returned, and errno is set appropriately:
  65. *
  66. * EBADF - An invalid file descriptor was given in one of the sets.
  67. * EFAULT - The fds address is invalid
  68. * EINTR - A signal occurred before any requested event.
  69. * EINVAL - The nfds value exceeds a system limit.
  70. * ENOMEM - There was no space to allocate internal data structures.
  71. * ENOSYS - One or more of the drivers supporting the file descriptor
  72. * does not support the poll method.
  73. *
  74. ****************************************************************************/
  75. int ppoll(FAR struct pollfd *fds, nfds_t nfds,
  76. FAR const struct timespec *timeout_ts,
  77. FAR const sigset_t *sigmask)
  78. {
  79. FAR struct tcb_s *rtcb = this_task();
  80. sigset_t saved_sigprocmask;
  81. irqstate_t flags;
  82. int ret = ERROR;
  83. /* Several operations must be performed below: We must determine if any
  84. * signal is pending and, if not, wait for the signal. Since signals can
  85. * be posted from the interrupt level, there is a race condition that
  86. * can only be eliminated by disabling interrupts!
  87. */
  88. flags = enter_critical_section();
  89. /* Save a copy of the old sigprocmask and install
  90. * the new (temporary) sigprocmask
  91. */
  92. saved_sigprocmask = rtcb->sigprocmask;
  93. if (sigmask)
  94. {
  95. rtcb->sigprocmask = *sigmask;
  96. }
  97. rtcb->sigwaitmask = NULL_SIGNAL_SET;
  98. /* Check if there is a pending signal corresponding to one of the
  99. * signals that will be unblocked by the new sigprocmask.
  100. */
  101. if (nxsig_unmask_pendingsignal())
  102. {
  103. /* Dispatching one or more of the signals is sufficient to cause
  104. * us to not wait. Restore the original sigprocmask.
  105. */
  106. rtcb->sigprocmask = saved_sigprocmask;
  107. leave_critical_section(flags);
  108. set_errno(EINTR);
  109. }
  110. else
  111. {
  112. int timeout = -1;
  113. /* And call poll to do the real work */
  114. if (timeout_ts)
  115. {
  116. timeout = timeout_ts->tv_sec * 1000 + timeout_ts->tv_nsec / 1000000;
  117. }
  118. ret = poll(fds, nfds, timeout);
  119. /* We are running again, restore the original sigprocmask */
  120. rtcb->sigprocmask = saved_sigprocmask;
  121. leave_critical_section(flags);
  122. /* Now, handle the (rare?) case where (a) a blocked signal was received
  123. * while the task was suspended but (b) restoring the original
  124. * sigprocmask will unblock the signal.
  125. */
  126. nxsig_unmask_pendingsignal();
  127. }
  128. return ret;
  129. }