2018-08-27 19:40:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/signal/sig_default.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2018-09-02 23:36:25 +02:00
|
|
|
#include <sys/wait.h>
|
2018-08-29 15:33:28 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2018-08-27 19:40:09 +02:00
|
|
|
#include <string.h>
|
2018-08-29 15:33:28 +02:00
|
|
|
#include <pthread.h>
|
2018-08-27 19:40:09 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <nuttx/sched.h>
|
2018-08-28 06:32:57 +02:00
|
|
|
#include <nuttx/irq.h>
|
2018-08-30 18:27:18 +02:00
|
|
|
#include <nuttx/signal.h>
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-29 13:49:42 +02:00
|
|
|
#include "group/group.h"
|
|
|
|
#include "sched/sched.h"
|
2019-05-17 15:39:04 +02:00
|
|
|
#include "task/task.h"
|
2018-08-27 19:40:09 +02:00
|
|
|
#include "signal/signal.h"
|
|
|
|
|
2018-08-28 20:39:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-04 13:51:14 +02:00
|
|
|
#if defined(CONFIG_SIG_SIGUSR1_ACTION) || defined(CONFIG_SIG_SIGUSR2_ACTION) || \
|
|
|
|
defined(CONFIG_SIG_SIGALRM_ACTION) || defined(CONFIG_SIG_SIGPOLL_ACTION) || \
|
2020-01-15 14:42:32 +01:00
|
|
|
defined(CONFIG_SIG_SIGKILL_ACTION) || defined(CONFIG_SIG_SIGPIPE_ACTION)
|
2019-10-04 13:51:14 +02:00
|
|
|
# define HAVE_NXSIG_ABNORMAL_TERMINANTION 1
|
|
|
|
#endif
|
|
|
|
|
2018-08-28 20:39:03 +02:00
|
|
|
/* Bit definitions for the struct nxsig_defaction_s 'flags' field */
|
|
|
|
|
|
|
|
#define SIG_FLAG_NOCATCH (1 << 0) /* Signal cannot be caught or ignored */
|
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This type provides the default action associated with a signal */
|
|
|
|
|
|
|
|
struct nxsig_defaction_s
|
|
|
|
{
|
|
|
|
uint8_t signo; /* Signal number. Range 1..MAX_SIGNO */
|
2018-08-28 20:39:03 +02:00
|
|
|
uint8_t flags; /* See SIG_FLAG_ definitions */
|
2018-08-27 23:37:43 +02:00
|
|
|
_sa_handler_t action; /* Default signal action */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Default actions */
|
|
|
|
|
2019-10-04 13:51:14 +02:00
|
|
|
#ifdef HAVE_NXSIG_ABNORMAL_TERMINANTION
|
2018-08-27 23:37:43 +02:00
|
|
|
static void nxsig_abnormal_termination(int signo);
|
2019-10-04 13:51:14 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SIG_SIGSTOP_ACTION
|
|
|
|
static void nxsig_null_action(int signo);
|
2018-08-30 18:27:18 +02:00
|
|
|
static void nxsig_stop_task(int signo);
|
2019-10-04 13:51:14 +02:00
|
|
|
#endif
|
2018-08-27 23:37:43 +02:00
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static _sa_handler_t nxsig_default_action(int signo);
|
|
|
|
static void nxsig_setup_default_action(FAR struct task_group_s *group,
|
2020-03-10 05:50:49 +01:00
|
|
|
FAR const struct nxsig_defaction_s *
|
|
|
|
info);
|
2018-08-27 23:37:43 +02:00
|
|
|
|
2018-08-27 19:40:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
/* NOTE: Default actions are not currently supported for very many signals.
|
|
|
|
* However, this array is set up to support an indefinite number of default
|
|
|
|
* signal actions.
|
|
|
|
*/
|
|
|
|
|
2018-08-27 19:40:09 +02:00
|
|
|
/* This table is used to make the default action for a signal to the correct
|
2018-08-27 23:37:43 +02:00
|
|
|
* signal handler. Signals whose default action is be ignored do not need
|
|
|
|
* to be in this table (e.g., SIGCHLD).
|
2018-08-27 19:40:09 +02:00
|
|
|
*/
|
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
static const struct nxsig_defaction_s g_defactions[] =
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2018-08-27 23:37:43 +02:00
|
|
|
#ifdef CONFIG_SIG_SIGUSR1_ACTION
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGUSR1, 0, nxsig_abnormal_termination },
|
2018-08-27 23:37:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SIG_SIGUSR2_ACTION
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGUSR2, 0, nxsig_abnormal_termination },
|
2018-08-27 23:37:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SIG_SIGALRM_ACTION
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGALRM, 0, nxsig_abnormal_termination },
|
2018-08-27 19:40:09 +02:00
|
|
|
#endif
|
2018-08-27 23:37:43 +02:00
|
|
|
#ifdef CONFIG_SIG_SIGPOLL_ACTION
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGPOLL, 0, nxsig_abnormal_termination },
|
2018-08-27 23:37:43 +02:00
|
|
|
#endif
|
2018-08-30 18:27:18 +02:00
|
|
|
#ifdef CONFIG_SIG_SIGSTOP_ACTION
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGSTOP, SIG_FLAG_NOCATCH, nxsig_stop_task },
|
2020-10-29 05:53:41 +01:00
|
|
|
{ SIGTSTP, 0, nxsig_stop_task },
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGCONT, SIG_FLAG_NOCATCH, nxsig_null_action },
|
2018-08-30 18:27:18 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SIG_SIGKILL_ACTION
|
2019-06-30 18:35:10 +02:00
|
|
|
{ SIGINT, 0, nxsig_abnormal_termination },
|
2019-10-17 19:29:39 +02:00
|
|
|
{ SIGKILL, SIG_FLAG_NOCATCH, nxsig_abnormal_termination },
|
2020-07-14 05:21:03 +02:00
|
|
|
{ SIGQUIT, 0, nxsig_abnormal_termination },
|
|
|
|
{ SIGTERM, 0, nxsig_abnormal_termination },
|
2019-10-17 19:29:39 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SIG_SIGPIPE_ACTION
|
|
|
|
{ SIGPIPE, 0, nxsig_abnormal_termination }
|
2018-08-30 18:27:18 +02:00
|
|
|
#endif
|
2018-08-27 23:37:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NACTIONS (sizeof(g_defactions) / sizeof(struct nxsig_defaction_s))
|
2018-08-27 19:40:09 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* TODO: Take into account pthread-specific signal behaviors */
|
|
|
|
|
|
|
|
/* Default Actions:
|
|
|
|
*
|
|
|
|
* - Abnormal termination of the process. The process is terminated with
|
|
|
|
* all the consequences of _exit() except that the status made available
|
|
|
|
* to wait() and waitpid() indicates abnormal termination by the
|
|
|
|
* specified signal.
|
|
|
|
* - Abnormal termination of the process. Additionally with the XSI
|
|
|
|
* extension, implementation-defined abnormal termination actions, such
|
|
|
|
* as creation of a core file, may occur.
|
|
|
|
* - Ignore the signal.
|
|
|
|
* - Stop the process.
|
|
|
|
* - Continue the process, if it is stopped; otherwise, ignore the signal.
|
|
|
|
*/
|
|
|
|
|
2018-08-30 18:27:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_null_action
|
|
|
|
*
|
|
|
|
* Description:
|
2018-09-02 21:51:52 +02:00
|
|
|
* The do-nothing default signal action handler.
|
2018-08-30 18:27:18 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Standard signal handler parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-04 13:51:14 +02:00
|
|
|
#ifdef CONFIG_SIG_SIGSTOP_ACTION
|
2018-08-30 18:27:18 +02:00
|
|
|
static void nxsig_null_action(int signo)
|
|
|
|
{
|
|
|
|
}
|
2019-10-04 13:51:14 +02:00
|
|
|
#endif
|
2018-08-30 18:27:18 +02:00
|
|
|
|
2018-08-27 19:40:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_abnormal_termination
|
|
|
|
*
|
|
|
|
* Description:
|
2018-08-28 06:32:57 +02:00
|
|
|
* This is the handler for the abnormal termination default action.
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Standard signal handler parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-04 13:51:14 +02:00
|
|
|
#ifdef HAVE_NXSIG_ABNORMAL_TERMINANTION
|
2018-08-27 23:37:43 +02:00
|
|
|
static void nxsig_abnormal_termination(int signo)
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2018-08-29 15:33:28 +02:00
|
|
|
FAR struct tcb_s *rtcb = (FAR struct tcb_s *)this_task();
|
|
|
|
|
2020-06-06 18:41:47 +02:00
|
|
|
/* Notify the target if the non-cancelable or deferred cancellation set */
|
2019-05-17 15:39:04 +02:00
|
|
|
|
2020-06-06 18:41:47 +02:00
|
|
|
if (nxnotify_cancellation(rtcb))
|
2019-05-17 15:39:04 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-29 15:33:28 +02:00
|
|
|
/* Careful: In the multi-threaded task, the signal may be handled on a
|
|
|
|
* child pthread.
|
|
|
|
*/
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-29 15:33:28 +02:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2020-03-16 20:42:34 +01:00
|
|
|
/* Kill all of the children of the task. This will not kill the currently
|
2018-08-29 23:50:01 +02:00
|
|
|
* running task/pthread (this_task). It will kill the main thread of the
|
2020-03-16 20:42:34 +01:00
|
|
|
* task group if this_task is a pthread.
|
2018-08-29 15:33:28 +02:00
|
|
|
*/
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2020-06-03 13:33:56 +02:00
|
|
|
group_kill_children(rtcb);
|
2018-08-27 19:40:09 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-29 15:33:28 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
/* Check if the currently running task is actually a pthread */
|
|
|
|
|
|
|
|
if ((rtcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
{
|
|
|
|
/* Exit the final thread of the task group.
|
|
|
|
*
|
|
|
|
* REVISIT: This will not work if HAVE_GROUP_MEMBERS is not set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Exit to terminate the task (note that exit() vs. _exit() is used. */
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-29 15:33:28 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|
2019-10-04 13:51:14 +02:00
|
|
|
#endif
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-30 18:27:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_stop_task
|
|
|
|
*
|
|
|
|
* Description:
|
2020-10-31 01:27:02 +01:00
|
|
|
* This is the handler for the stop default action.
|
2018-08-30 18:27:18 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Standard signal handler parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-04 13:51:14 +02:00
|
|
|
#ifdef CONFIG_SIG_SIGSTOP_ACTION
|
2018-08-30 18:27:18 +02:00
|
|
|
static void nxsig_stop_task(int signo)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *rtcb = (FAR struct tcb_s *)this_task();
|
2018-09-02 23:36:25 +02:00
|
|
|
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
|
|
|
|
FAR struct task_group_s *group;
|
|
|
|
|
|
|
|
DEBUGASSERT(rtcb != NULL && rtcb->group != NULL);
|
|
|
|
group = rtcb->group;
|
|
|
|
#endif
|
2018-08-30 18:27:18 +02:00
|
|
|
|
|
|
|
/* Careful: In the multi-threaded task, the signal may be handled on a
|
|
|
|
* child pthread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2020-03-16 20:42:34 +01:00
|
|
|
/* Suspend all of the children of the task. This will not suspend the
|
2018-08-30 18:27:18 +02:00
|
|
|
* currently running task/pthread (this_task). It will suspend the
|
2020-03-16 20:42:34 +01:00
|
|
|
* main thread of the task group if this_task is a pthread.
|
2018-08-30 18:27:18 +02:00
|
|
|
*/
|
|
|
|
|
2020-05-16 17:06:29 +02:00
|
|
|
group_suspend_children(rtcb);
|
2018-08-30 18:27:18 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-02 23:36:25 +02:00
|
|
|
/* Lock the scheduler so this thread is not pre-empted until after we
|
2020-05-09 20:40:14 +02:00
|
|
|
* call nxsched_suspend().
|
2018-09-02 21:51:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
|
|
|
|
/* Notify via waitpid if any parent is waiting for this task to EXIT
|
|
|
|
* or STOP. This action is only performed if WUNTRACED is set in the
|
|
|
|
* waitpid flags.
|
|
|
|
*/
|
|
|
|
|
2018-09-02 23:36:25 +02:00
|
|
|
if ((group->tg_waitflags & WUNTRACED) != 0)
|
2018-09-02 21:51:52 +02:00
|
|
|
{
|
|
|
|
/* Return zero for exit status (we are not exiting, however) */
|
|
|
|
|
|
|
|
if (group->tg_statloc != NULL)
|
|
|
|
{
|
|
|
|
*group->tg_statloc = 0;
|
|
|
|
group->tg_statloc = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tg_waitflags == 0 means that the flags are available to another
|
|
|
|
* caller of waitpid().
|
|
|
|
*/
|
|
|
|
|
|
|
|
group->tg_waitflags = 0;
|
|
|
|
|
2020-10-31 00:32:37 +01:00
|
|
|
/* Wakeup any tasks waiting for this task to exit or stop. */
|
2018-09-02 21:51:52 +02:00
|
|
|
|
|
|
|
while (group->tg_exitsem.semcount < 0)
|
|
|
|
{
|
|
|
|
/* Wake up the thread */
|
|
|
|
|
|
|
|
nxsem_post(&group->tg_exitsem);
|
2019-06-30 18:35:10 +02:00
|
|
|
}
|
2018-09-02 21:51:52 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then, finally, suspend this the final thread of the task group */
|
2018-08-30 18:27:18 +02:00
|
|
|
|
2020-05-09 20:40:14 +02:00
|
|
|
nxsched_suspend(rtcb);
|
2018-09-02 21:51:52 +02:00
|
|
|
sched_unlock();
|
2018-08-30 18:27:18 +02:00
|
|
|
}
|
2019-10-04 13:51:14 +02:00
|
|
|
#endif
|
2018-08-30 18:27:18 +02:00
|
|
|
|
2018-08-27 19:40:09 +02:00
|
|
|
/****************************************************************************
|
2018-08-27 21:13:23 +02:00
|
|
|
* Name: nxsig_default_action
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-27 21:13:23 +02:00
|
|
|
* Look up the default action associated with this signal
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-08-27 21:13:23 +02:00
|
|
|
* signo - The signal number to use in the query
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-08-27 21:13:23 +02:00
|
|
|
* The default handler associated with this signal
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
static _sa_handler_t nxsig_default_action(int signo)
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2018-08-27 23:37:43 +02:00
|
|
|
int i;
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
/* Search the default action table for the entry associated with this
|
|
|
|
* signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < NACTIONS; i++)
|
|
|
|
{
|
|
|
|
if (g_defactions[i].signo == signo)
|
|
|
|
{
|
|
|
|
return g_defactions[i].action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No default action */
|
|
|
|
|
|
|
|
return SIG_IGN;
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_setup_default_action
|
|
|
|
*
|
|
|
|
* Description:
|
2018-08-28 06:32:57 +02:00
|
|
|
* Setup the default action for a signal.
|
|
|
|
*
|
|
|
|
* This function is called early in task setup, prior to the creation of
|
|
|
|
* any pthreads so we should have exclusive access to the group structure.
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-08-27 21:13:23 +02:00
|
|
|
* group - The group that the task belongs in.
|
|
|
|
* action - The new default signal action
|
|
|
|
* signo - The signal that will produce this default action
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-27 21:13:23 +02:00
|
|
|
static void nxsig_setup_default_action(FAR struct task_group_s *group,
|
2020-03-10 05:50:49 +01:00
|
|
|
FAR const struct nxsig_defaction_s *
|
|
|
|
info)
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2018-08-27 21:13:23 +02:00
|
|
|
/* Get the address of the handler for this signals default action. */
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
if (info->action != SIG_IGN)
|
2018-08-27 21:13:23 +02:00
|
|
|
{
|
2018-08-27 23:37:43 +02:00
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
/* Attach the signal handler.
|
|
|
|
*
|
2018-08-30 18:27:18 +02:00
|
|
|
* NOTE: nxsig_action will call nxsig_default(tcb, action, false).
|
|
|
|
* Don't be surprised.
|
2018-08-27 23:37:43 +02:00
|
|
|
*/
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-27 21:13:23 +02:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2018-08-27 23:37:43 +02:00
|
|
|
sa.sa_handler = info->action;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsig_action(info->signo, &sa, NULL, true);
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-27 21:13:23 +02:00
|
|
|
/* Indicate that the default signal handler has been attached */
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2020-04-29 18:53:36 +02:00
|
|
|
nxsig_addset(&group->tg_sigdefault, (int)info->signo);
|
2018-08-27 21:13:23 +02:00
|
|
|
}
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_isdefault
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return true if the specified signal is set to the default action.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Identifies the thread associated with the default handler
|
|
|
|
* signo - The signal number to be queried
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned
|
|
|
|
* on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool nxsig_isdefault(FAR struct tcb_s *tcb, int signo)
|
|
|
|
{
|
|
|
|
FAR struct task_group_s *group;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(tcb != NULL && tcb->group != NULL && GOOD_SIGNO(signo));
|
|
|
|
group = tcb->group;
|
|
|
|
|
|
|
|
/* Return true if the signo is marked as using the default action. Return
|
|
|
|
* false in all other cases.
|
|
|
|
*/
|
|
|
|
|
2020-04-29 18:53:36 +02:00
|
|
|
ret = nxsig_ismember(&group->tg_sigdefault, signo);
|
2018-08-27 19:40:09 +02:00
|
|
|
return ret < 0 ? false : (bool)ret;
|
|
|
|
}
|
|
|
|
|
2018-08-28 20:39:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_iscatchable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return true if the specified signal can be caught or ignored.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* signo - The signal number to be queried
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned
|
|
|
|
* on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool nxsig_iscatchable(int signo)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Search the default action table for the entry associated with this
|
|
|
|
* signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < NACTIONS; i++)
|
|
|
|
{
|
|
|
|
if (g_defactions[i].signo == signo)
|
|
|
|
{
|
|
|
|
return (g_defactions[i].flags & SIG_FLAG_NOCATCH) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it is not in the table, then it is catchable */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-27 19:40:09 +02:00
|
|
|
/****************************************************************************
|
2018-08-27 21:13:23 +02:00
|
|
|
* Name: nxsig_default
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-27 21:13:23 +02:00
|
|
|
* If 'defaction' is true, then return the default signal handler action
|
2020-02-23 09:50:23 +01:00
|
|
|
* for the specified signal and mark that the default signal handler is
|
2018-08-27 21:13:23 +02:00
|
|
|
* in place (it is not yet).
|
|
|
|
*
|
|
|
|
* If 'defaction' is false, then mark that the default signal handler is
|
|
|
|
* NOT in place and return SIG_IGN.
|
|
|
|
*
|
|
|
|
* This function is called form sigaction() to handle actions = SIG_DFL.
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Identifies the thread associated with the default handler
|
|
|
|
* signo - The signal number whose behavior will be modified.
|
|
|
|
* defaction - True: the default action is in place
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-08-27 21:13:23 +02:00
|
|
|
* The address of the default signal action handler is returne on success.
|
|
|
|
* SIG_IGN is returned if there is no default action.
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-06-30 18:35:10 +02:00
|
|
|
_sa_handler_t nxsig_default(FAR struct tcb_s *tcb, int signo, bool defaction)
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2018-08-27 21:13:23 +02:00
|
|
|
FAR struct task_group_s *group;
|
2018-08-27 23:37:43 +02:00
|
|
|
_sa_handler_t handler = SIG_IGN;
|
2018-08-28 06:32:57 +02:00
|
|
|
irqstate_t flags;
|
2018-08-27 21:13:23 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(tcb != NULL && tcb->group != NULL);
|
|
|
|
group = tcb->group;
|
|
|
|
|
2018-08-27 19:40:09 +02:00
|
|
|
/* Are we setting or unsetting the default action? */
|
|
|
|
|
|
|
|
if (defaction)
|
|
|
|
{
|
|
|
|
/* We are setting the default action. Look up the default action
|
|
|
|
* associated with signo.
|
|
|
|
*/
|
|
|
|
|
2018-08-27 21:13:23 +02:00
|
|
|
handler = nxsig_default_action(signo);
|
2018-08-27 23:37:43 +02:00
|
|
|
if (handler != SIG_IGN)
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2020-04-29 18:53:36 +02:00
|
|
|
/* nxsig_addset() is not atomic (but neither is sigaction()) */
|
2018-08-28 06:32:57 +02:00
|
|
|
|
|
|
|
flags = spin_lock_irqsave();
|
2020-04-29 18:53:36 +02:00
|
|
|
nxsig_addset(&group->tg_sigdefault, signo);
|
2018-08-28 06:32:57 +02:00
|
|
|
spin_unlock_irqrestore(flags);
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-27 21:13:23 +02:00
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
if (handler == SIG_IGN)
|
2018-08-27 19:40:09 +02:00
|
|
|
{
|
2020-04-29 18:53:36 +02:00
|
|
|
/* We are unsetting the default action. NOTE that nxsig_delset() is not
|
2019-03-01 17:50:02 +01:00
|
|
|
* atomic (but neither is sigaction()).
|
|
|
|
*/
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-28 06:32:57 +02:00
|
|
|
flags = spin_lock_irqsave();
|
2020-04-29 18:53:36 +02:00
|
|
|
nxsig_delset(&group->tg_sigdefault, signo);
|
2018-08-28 06:32:57 +02:00
|
|
|
spin_unlock_irqrestore(flags);
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 21:13:23 +02:00
|
|
|
return handler;
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxsig_default_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2019-06-30 18:35:10 +02:00
|
|
|
* Set all signals to their default action. This is called from
|
|
|
|
* nxtask_start() to configure the newly started task.
|
2018-08-27 19:40:09 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tcb - Identifies the thread associated with the default handlers
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned
|
|
|
|
* on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxsig_default_initialize(FAR struct tcb_s *tcb)
|
|
|
|
{
|
2018-08-27 21:13:23 +02:00
|
|
|
FAR struct task_group_s *group;
|
2018-08-27 23:37:43 +02:00
|
|
|
int i;
|
2018-08-27 19:40:09 +02:00
|
|
|
|
2018-08-27 21:13:23 +02:00
|
|
|
DEBUGASSERT(tcb != NULL && tcb->group != NULL);
|
|
|
|
group = tcb->group;
|
|
|
|
|
|
|
|
/* Initialize the set of default signal handlers */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
sigemptyset(&group->tg_sigdefault);
|
2018-08-27 21:13:23 +02:00
|
|
|
|
2018-08-28 06:32:57 +02:00
|
|
|
/* Setup the default action for each signal in g_defactions[] */
|
2018-08-27 21:13:23 +02:00
|
|
|
|
2018-08-27 23:37:43 +02:00
|
|
|
for (i = 0; i < NACTIONS; i++)
|
2018-08-27 21:13:23 +02:00
|
|
|
{
|
2018-08-27 23:37:43 +02:00
|
|
|
nxsig_setup_default_action(group, &g_defactions[i]);
|
2018-08-27 21:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2018-08-27 19:40:09 +02:00
|
|
|
}
|