sig_procmask.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. * sched/signal/sig_procmask.c
  3. *
  4. * Copyright (C) 2007-2009, 2014, 2016-2017 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <unistd.h>
  41. #include <signal.h>
  42. #include <time.h>
  43. #include <sched.h>
  44. #include <assert.h>
  45. #include <errno.h>
  46. #include <debug.h>
  47. #include <nuttx/irq.h>
  48. #include <nuttx/arch.h>
  49. #include <nuttx/wdog.h>
  50. #include <nuttx/kmalloc.h>
  51. #include <nuttx/signal.h>
  52. #include "sched/sched.h"
  53. #include "signal/signal.h"
  54. /****************************************************************************
  55. * Public Functions
  56. ****************************************************************************/
  57. /****************************************************************************
  58. * Name: nxsig_procmask
  59. *
  60. * Description:
  61. * This function allows the calling process to examine and/or change its
  62. * signal mask. If the 'set' is not NULL, then it points to a set of
  63. * signals to be used to change the currently blocked set. The value of
  64. * 'how' indicates the manner in which the set is changed.
  65. *
  66. * If there any pending unblocked signals after the call to
  67. * nxsig_procmask(), those signals will be delivered before
  68. * nxsig_procmask() returns.
  69. *
  70. * If nxsig_procmask() fails, the signal mask of the process is not changed
  71. * by this function call.
  72. *
  73. * This is an internal OS interface. It is functionally equivalent to
  74. * sigprocmask() except that it does not modify the errno value.
  75. *
  76. * Input Parameters:
  77. * how - How the signal mast will be changed:
  78. * SIG_BLOCK - The resulting set is the union of the current set
  79. * and the signal set pointed to by 'set'.
  80. * SIG_UNBLOCK - The resulting set is the intersection of the current
  81. * set and the complement of the signal set pointed to
  82. * by 'set'.
  83. * SIG_SETMASK - The resulting set is the signal set pointed to by
  84. * 'set'.
  85. * set - Location of the new signal mask
  86. * oset - Location to store the old signal mask
  87. *
  88. * Returned Value:
  89. * This is an internal OS interface and should not be used by applications.
  90. * It follows the NuttX internal error return policy: Zero (OK) is
  91. * returned on success. A negated errno value is returned on failure.
  92. *
  93. * EINVAL - The 'how' argument is invalid.
  94. *
  95. ****************************************************************************/
  96. int nxsig_procmask(int how, FAR const sigset_t *set, FAR sigset_t *oset)
  97. {
  98. FAR struct tcb_s *rtcb = this_task();
  99. sigset_t oldsigprocmask;
  100. irqstate_t flags;
  101. int ret = OK;
  102. sched_lock();
  103. /* Return the old signal mask if requested */
  104. oldsigprocmask = rtcb->sigprocmask;
  105. if (oset != NULL)
  106. {
  107. *oset = oldsigprocmask;
  108. }
  109. /* Modify the current signal mask if so requested */
  110. if (set != NULL)
  111. {
  112. /* Some of these operations are non-atomic. We need to protect
  113. * ourselves from attempts to process signals from interrupts
  114. */
  115. flags = enter_critical_section();
  116. /* Okay, determine what we are supposed to do */
  117. switch (how)
  118. {
  119. /* The resulting set is the union of the current set and the
  120. * signal set pointed to by set.
  121. */
  122. case SIG_BLOCK:
  123. rtcb->sigprocmask |= *set;
  124. break;
  125. /* The resulting set is the intersection of the current set and
  126. * the complement of the signal set pointed to by _set.
  127. */
  128. case SIG_UNBLOCK:
  129. rtcb->sigprocmask &= ~(*set);
  130. break;
  131. /* The resulting set is the signal set pointed to by set. */
  132. case SIG_SETMASK:
  133. rtcb->sigprocmask = *set;
  134. break;
  135. default:
  136. ret = -EINVAL;
  137. break;
  138. }
  139. leave_critical_section(flags);
  140. /* Now, process any pending signals that were just unmasked */
  141. nxsig_unmask_pendingsignal();
  142. }
  143. sched_unlock();
  144. return ret;
  145. }
  146. /****************************************************************************
  147. * Name: sigprocmask
  148. *
  149. * Description:
  150. * This function allows the calling process to examine and/or change its
  151. * signal mask. If the 'set' is not NULL, then it points to a set of
  152. * signals to be used to change the currently blocked set. The value of
  153. * 'how' indicates the manner in which the set is changed.
  154. *
  155. * If there any pending unblocked signals after the call to sigprocmask(),
  156. * those signals will be delivered before sigprocmask() returns.
  157. *
  158. * If sigprocmask() fails, the signal mask of the process is not changed
  159. * by this function call.
  160. *
  161. * Input Parameters:
  162. * how - How the signal mast will be changed:
  163. * SIG_BLOCK - The resulting set is the union of the current set
  164. * and the signal set pointed to by 'set'.
  165. * SIG_UNBLOCK - The resulting set is the intersection of the current
  166. * set and the complement of the signal set pointed to
  167. * by 'set'.
  168. * SIG_SETMASK - The resulting set is the signal set pointed to by
  169. * 'set'.
  170. * set - Location of the new signal mask
  171. * oset - Location to store the old signal mask
  172. *
  173. * Returned Value:
  174. * This function will return 0 (OK) on success or -1 (ERROR) if how is
  175. * invalid. In the latter case, the errno variable will be set to EINVAL.
  176. *
  177. ****************************************************************************/
  178. int sigprocmask(int how, FAR const sigset_t *set, FAR sigset_t *oset)
  179. {
  180. int ret;
  181. /* Let nxsig_procmask do all of the work */
  182. ret = nxsig_procmask(how, set, oset);
  183. if (ret < 0)
  184. {
  185. set_errno(-ret);
  186. ret = ERROR;
  187. }
  188. return ret;
  189. }