sig_action.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /****************************************************************************
  2. * sched/signal/sig_action.c
  3. *
  4. * Copyright (C) 2007-2009, 2013, 2016-2018 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 <stdint.h>
  41. #include <stdbool.h>
  42. #include <signal.h>
  43. #include <queue.h>
  44. #include <sched.h>
  45. #include <errno.h>
  46. #include <nuttx/irq.h>
  47. #include "sched/sched.h"
  48. #include "group/group.h"
  49. #include "signal/signal.h"
  50. /****************************************************************************
  51. * Private Functions
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Name: nxsig_alloc_action
  55. *
  56. * Description:
  57. * Allocate a new element for a sigaction queue
  58. *
  59. ****************************************************************************/
  60. static FAR sigactq_t *nxsig_alloc_action(void)
  61. {
  62. FAR sigactq_t *sigact;
  63. /* Try to get the signal action structure from the free list */
  64. sigact = (FAR sigactq_t *)sq_remfirst(&g_sigfreeaction);
  65. /* Check if we got one. */
  66. if (!sigact)
  67. {
  68. /* Add another block of signal actions to the list */
  69. nxsig_alloc_actionblock();
  70. /* And try again */
  71. sigact = (FAR sigactq_t *)sq_remfirst(&g_sigfreeaction);
  72. DEBUGASSERT(sigact);
  73. }
  74. return sigact;
  75. }
  76. /****************************************************************************
  77. * Public Functions
  78. ****************************************************************************/
  79. /****************************************************************************
  80. * Name: nxsig_action and sigaction
  81. *
  82. * Description:
  83. * This function allows the calling process to examine and/or specify the
  84. * action to be associated with a specific signal.
  85. *
  86. * The structure sigaction, used to describe an action to be taken, is
  87. * defined to include the following members:
  88. *
  89. * - sa_u.sa_handler: Pointer to a signal-catching function
  90. * - sa_u.sa_sigaction: Alternative form of the signal-catching function
  91. * - sa_mask: An additional set of signals to be blocked during execution
  92. * of a signal catching function
  93. * - sa_flags. Special flags to affect the behavior of a signal.
  94. *
  95. * If the argument 'act' is not NULL, it points to a structure specifying
  96. * the action to be associated with the specified signal. If the argument
  97. * 'oact' is not NULL, the action previously associated with the signal
  98. * is stored in the location pointed to by the argument 'oact.'
  99. *
  100. * When a signal is caught by a signal-catching function installed by
  101. * sigaction() function, a new signal mask is calculated and installed for
  102. * the duration of the signal-catching function. This mask is formed by
  103. * taking the union of the current signal mask and the value of the
  104. * sa_mask for the signal being delivered and then including the signal
  105. * being delivered. If and when the user's signal handler returns, the
  106. * original signal mask is restored.
  107. *
  108. * Once an action is installed for a specific signal, it remains installed
  109. * until another action is explicitly requested by another call to
  110. * sigaction().
  111. *
  112. * nxsig_action() is an internal version of sigaction that adds an
  113. * additional parameter, force, that is used to set default signal actions
  114. * (which may not normally be settable). nxsig_action() does not alter the
  115. * errno variable.
  116. *
  117. * Input Parameters:
  118. * sig - Signal of interest
  119. * act - Location of new handler
  120. * oact - Location to store only handler
  121. * force - Force setup of the signal handler, even if it cannot normally
  122. * be caught or ignored (nxsig_action only)
  123. *
  124. * Returned Value:
  125. * nxsig_action:
  126. * Zero (OK) is returned on success; a negated errno value is returned
  127. * on failure
  128. * sigaction:
  129. * Zero (OK) is returned on success; -1 (ERROR) is returned on any
  130. * failure if the signal number is invalid with the errno set appropriately
  131. *
  132. * Assumptions:
  133. *
  134. * POSIX Compatibility:
  135. * - If CONFIG_SIG_DEFAULT is not defined, then there are no default actions
  136. * so the special value SIG_DFL is treated like SIG_IGN.
  137. * - All sa_flags in struct sigaction of act input are ignored (all
  138. * treated like SA_SIGINFO). The one exception is if CONFIG_SCHED_CHILD_STATUS
  139. * is defined; then SA_NOCLDWAIT is supported but only for SIGCHLD
  140. *
  141. ****************************************************************************/
  142. int nxsig_action(int signo, FAR const struct sigaction *act,
  143. FAR struct sigaction *oact, bool force)
  144. {
  145. FAR struct tcb_s *rtcb = this_task();
  146. FAR struct task_group_s *group;
  147. FAR sigactq_t *sigact;
  148. _sa_handler_t handler;
  149. /* Since sigactions can only be installed from the running thread of
  150. * execution, no special precautions should be necessary.
  151. */
  152. DEBUGASSERT(rtcb != NULL && rtcb->group != NULL);
  153. group = rtcb->group;
  154. /* Verify the signal number */
  155. if (!GOOD_SIGNO(signo))
  156. {
  157. return -EINVAL;
  158. }
  159. #ifdef CONFIG_SIG_DEFAULT
  160. /* Check if the user is trying to catch or ignore a signal that cannot be
  161. * caught or ignored.
  162. */
  163. if (act != NULL && !force && act->sa_handler != SIG_DFL &&
  164. !nxsig_iscatchable(signo))
  165. {
  166. return -EINVAL;
  167. }
  168. #endif
  169. /* Find the signal in the signal action queue */
  170. sigact = nxsig_find_action(group, signo);
  171. /* Return the old sigaction value if so requested */
  172. if (oact != NULL)
  173. {
  174. #ifdef CONFIG_SIG_DEFAULT
  175. if (nxsig_isdefault(rtcb, signo))
  176. {
  177. /* Return SIG_DFL if the default signal is attached */
  178. oact->sa_handler = SIG_DFL;
  179. oact->sa_mask = NULL_SIGNAL_SET;
  180. oact->sa_flags = SA_SIGINFO;
  181. }
  182. else
  183. #endif
  184. if (sigact)
  185. {
  186. /* Return the old signal action */
  187. oact->sa_handler = sigact->act.sa_handler;
  188. oact->sa_mask = sigact->act.sa_mask;
  189. oact->sa_flags = sigact->act.sa_flags;
  190. }
  191. else
  192. {
  193. /* There isn't an old value */
  194. oact->sa_handler = NULL;
  195. oact->sa_mask = NULL_SIGNAL_SET;
  196. oact->sa_flags = 0;
  197. }
  198. }
  199. /* If the argument act is a null pointer, signal handling is unchanged;
  200. * thus, the call can be used to inquire about the current handling of
  201. * a given signal.
  202. */
  203. if (act == NULL)
  204. {
  205. return OK;
  206. }
  207. #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
  208. /* Handle a special case. Retention of child status can be suppressed
  209. * if signo == SIGCHLD and sa_flags == SA_NOCLDWAIT.
  210. *
  211. * POSIX.1 leaves it unspecified whether a SIGCHLD signal is generated
  212. * when a child process terminates. In NuttX, a SIGCHLD signal is
  213. * generated in this case; but in some other implementations, it may not
  214. * be.
  215. */
  216. if (signo == SIGCHLD && (act->sa_flags & SA_NOCLDWAIT) != 0)
  217. {
  218. irqstate_t flags;
  219. /* We do require a critical section to muck with the TCB values that
  220. * can be modified by the child thread.
  221. */
  222. flags = enter_critical_section();
  223. /* Mark that status should be not be retained */
  224. rtcb->group->tg_flags |= GROUP_FLAG_NOCLDWAIT;
  225. /* Free all pending exit status */
  226. group_removechildren(rtcb->group);
  227. leave_critical_section(flags);
  228. }
  229. #endif
  230. handler = act->sa_handler;
  231. #ifdef CONFIG_SIG_DEFAULT
  232. /* If the caller is setting the handler to SIG_DFL, then we need to
  233. * replace this with the correct, internal default signal action handler.
  234. */
  235. if (handler == SIG_DFL)
  236. {
  237. /* nxsig_default() may returned SIG_IGN */
  238. handler = nxsig_default(rtcb, signo, true);
  239. }
  240. else
  241. {
  242. /* We will be replacing the default action (or ignoring it) */
  243. (void)nxsig_default(rtcb, signo, false);
  244. }
  245. #endif
  246. /* Handle the case where no sigaction is supplied (SIG_IGN) */
  247. if (handler == SIG_IGN)
  248. {
  249. /* Do we still have a sigaction container from the previous setting? */
  250. if (sigact)
  251. {
  252. /* Yes.. Remove it from signal action queue */
  253. sq_rem((FAR sq_entry_t *)sigact, &group->tg_sigactionq);
  254. /* And deallocate it */
  255. nxsig_release_action(sigact);
  256. }
  257. }
  258. /* A sigaction has been supplied */
  259. else
  260. {
  261. /* Do we still have a sigaction container from the previous setting?
  262. * If so, then re-use for the new signal action.
  263. */
  264. if (sigact == NULL)
  265. {
  266. /* No.. Then we need to allocate one for the new action. */
  267. sigact = nxsig_alloc_action();
  268. /* An error has occurred if we could not allocate the sigaction */
  269. if (!sigact)
  270. {
  271. return -ENOMEM;
  272. }
  273. /* Put the signal number in the queue entry */
  274. sigact->signo = (uint8_t)signo;
  275. /* Add the new sigaction to signal action queue */
  276. sq_addlast((FAR sq_entry_t *)sigact, &group->tg_sigactionq);
  277. }
  278. /* Set the new sigaction */
  279. sigact->act.sa_handler = handler;
  280. sigact->act.sa_mask = act->sa_mask;
  281. sigact->act.sa_flags = act->sa_flags;
  282. }
  283. return OK;
  284. }
  285. int sigaction(int signo, FAR const struct sigaction *act,
  286. FAR struct sigaction *oact)
  287. {
  288. int ret;
  289. /* nxsig_action() does all of the work */
  290. ret = nxsig_action(signo, act, oact, false);
  291. if (ret < 0)
  292. {
  293. set_errno(-ret);
  294. return ERROR;
  295. }
  296. return OK;
  297. }
  298. /****************************************************************************
  299. * Name: nxsig_release_action
  300. *
  301. * Description:
  302. * Deallocate a sigaction Q entry
  303. *
  304. ****************************************************************************/
  305. void nxsig_release_action(FAR sigactq_t *sigact)
  306. {
  307. /* Just put it back on the free list */
  308. sq_addlast((FAR sq_entry_t *)sigact, &g_sigfreeaction);
  309. }