2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
|
|
|
* sched/sig_received.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2011-07-20 15:19:07 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
|
2007-02-18 00:21:28 +01:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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.
|
2009-12-14 19:39:29 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-11-07 21:55:25 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
#include <string.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2011-07-20 15:19:07 +02:00
|
|
|
#include <errno.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <debug.h>
|
2011-07-20 15:19:07 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/arch.h>
|
2008-11-07 21:55:25 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include "os_internal.h"
|
|
|
|
#include "sem_internal.h"
|
|
|
|
#include "sig_internal.h"
|
2007-03-28 16:48:42 +02:00
|
|
|
#include "mq_internal.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Definitions
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Type Declarations
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Global Variables
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Variables
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: sig_queueaction
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Queue a signal action for delivery to a task.
|
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
static int sig_queueaction(FAR _TCB *stcb, siginfo_t *info)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR sigactq_t *sigact;
|
|
|
|
FAR sigq_t *sigq;
|
|
|
|
irqstate_t saved_state;
|
|
|
|
int ret = OK;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Find the sigaction associated with this signal */
|
|
|
|
|
|
|
|
sigact = sig_findaction(stcb, info->si_signo);
|
|
|
|
|
|
|
|
/* Check if a valid signal handler is available and if the signal is
|
|
|
|
* unblocked. NOTE: There is no default action.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((sigact) && (sigact->act.sa_u._sa_sigaction))
|
|
|
|
{
|
|
|
|
/* Allocate a new element for the signal queue. NOTE: sig_allocatependingsigaction
|
|
|
|
* will force a system crash if it is unable to allocate memory for the
|
|
|
|
* signal data */
|
|
|
|
|
|
|
|
sigq = sig_allocatependingsigaction();
|
|
|
|
if (!sigq) ret = ERROR;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Populate the new signal queue element */
|
|
|
|
|
|
|
|
sigq->action.sighandler = sigact->act.sa_u._sa_sigaction;
|
|
|
|
sigq->mask = sigact->act.sa_mask;
|
2007-02-21 22:55:16 +01:00
|
|
|
memcpy(&sigq->info, info, sizeof(siginfo_t));
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Put it at the end of the pending signals list */
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addlast((FAR sq_entry_t*)sigq, &(stcb->sigpendactionq));
|
2007-02-18 00:21:28 +01:00
|
|
|
irqrestore(saved_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: sig_findpendingsignal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a specified element in the pending signal list
|
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
static FAR sigpendq_t *sig_findpendingsignal(FAR _TCB *stcb, int signo)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR sigpendq_t *sigpend = NULL;
|
|
|
|
irqstate_t saved_state;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Verify the caller's sanity */
|
|
|
|
|
|
|
|
if (stcb)
|
|
|
|
{
|
|
|
|
/* Pending sigals can be added from interrupt level. */
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
|
|
|
|
|
|
|
/* Seach the list for a sigpendion on this signal */
|
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
for(sigpend = (FAR sigpendq_t*)stcb->sigpendingq.head;
|
2007-02-18 00:21:28 +01:00
|
|
|
(sigpend && sigpend->info.si_signo != signo);
|
|
|
|
sigpend = sigpend->flink);
|
|
|
|
irqrestore(saved_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigpend;
|
|
|
|
}
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: sig_allocatependingsignal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a pending signal list entry
|
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
static FAR sigpendq_t *sig_allocatependingsignal(void)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR sigpendq_t *sigpend;
|
|
|
|
irqstate_t saved_state;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Check if we were called from an interrupt handler. */
|
|
|
|
|
|
|
|
if (up_interrupt_context())
|
|
|
|
{
|
|
|
|
/* Try to get the pending signal structure from the free list */
|
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
sigpend = (FAR sigpendq_t*)sq_remfirst(&g_sigpendingsignal);
|
2011-08-07 14:58:54 +02:00
|
|
|
if (!sigpend)
|
|
|
|
{
|
|
|
|
/* If no pending signal structure is available in the free list,
|
|
|
|
* then try the special list of structures reserved for
|
|
|
|
* interrupt handlers
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-08-07 14:58:54 +02:00
|
|
|
sigpend = (FAR sigpendq_t*)sq_remfirst(&g_sigpendingirqsignal);
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we were not called from an interrupt handler, then we are
|
|
|
|
* free to allocate pending action structures if necessary. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to get the pending signal structure from the free list */
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
2007-02-27 22:17:21 +01:00
|
|
|
sigpend = (FAR sigpendq_t*)sq_remfirst(&g_sigpendingsignal);
|
2007-02-18 00:21:28 +01:00
|
|
|
irqrestore(saved_state);
|
|
|
|
|
|
|
|
/* Check if we got one. */
|
|
|
|
|
|
|
|
if (!sigpend)
|
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
/* No... Allocate the pending signal */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
if (!sigpend)
|
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
sigpend = (FAR sigpendq_t *)kmalloc((sizeof (sigpendq_t)));
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we got an allocated message */
|
|
|
|
|
|
|
|
if (sigpend)
|
|
|
|
{
|
|
|
|
sigpend->type = SIG_ALLOC_DYN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigpend;
|
|
|
|
}
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: sig_addpendingsignal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add the specified signal to the signal pending list.
|
|
|
|
* NOTE: This function will queue only one entry for each
|
|
|
|
* pending signal. This was done intentionally so that a
|
|
|
|
* run-away sender cannot consume all of memory.
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
static FAR sigpendq_t *sig_addpendingsignal(FAR _TCB *stcb, siginfo_t *info)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR sigpendq_t *sigpend;
|
|
|
|
irqstate_t saved_state;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Check if the signal is already pending */
|
|
|
|
|
|
|
|
sigpend = sig_findpendingsignal(stcb, info->si_signo);
|
|
|
|
if (sigpend)
|
|
|
|
{
|
|
|
|
/* The signal is already pending... retain only one copy */
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
memcpy(&sigpend->info, info, sizeof(siginfo_t));
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No... There is nothing pending for this signo */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Allocate a new pending signal entry */
|
|
|
|
|
|
|
|
sigpend = sig_allocatependingsignal();
|
|
|
|
if (sigpend)
|
|
|
|
{
|
|
|
|
/* Put the signal information into the allocated structure */
|
|
|
|
|
2007-02-21 22:55:16 +01:00
|
|
|
memcpy(&sigpend->info, info, sizeof(siginfo_t));
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Add the structure to the pending signal list */
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
2007-02-27 22:17:21 +01:00
|
|
|
sq_addlast((FAR sq_entry_t*)sigpend, &stcb->sigpendingq);
|
2007-02-18 00:21:28 +01:00
|
|
|
irqrestore(saved_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigpend;
|
|
|
|
}
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: sig_received
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* All signals received the task (whatever the source) go
|
|
|
|
* through this function to be processed. This function
|
|
|
|
* is responsible for:
|
|
|
|
*
|
|
|
|
* - Determining if the signal is blocked.
|
|
|
|
* - Queuing and dispatching signal actions
|
|
|
|
* - Unblocking tasks that are waiting for signals
|
|
|
|
* - Queuing pending signals.
|
|
|
|
*
|
2009-12-14 19:39:29 +01:00
|
|
|
************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
int sig_received(FAR _TCB *stcb, siginfo_t *info)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
irqstate_t saved_state;
|
|
|
|
int ret = ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("TCB=0x%08x signo=%d code=%d value=%d mask=%08x\n",
|
|
|
|
stcb, info->si_signo, info->si_code,
|
|
|
|
info->si_value.sival_int, stcb->sigprocmask);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
if (stcb && info)
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/****************** MASKED SIGNAL HANDLING ******************/
|
2007-03-19 17:38:47 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Check if the signal is masked -- if it is, it will be added to the
|
|
|
|
* list of pending signals.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sigismember(&stcb->sigprocmask, info->si_signo))
|
|
|
|
{
|
|
|
|
/* Check if the task is waiting for this pending signal. If so,
|
|
|
|
* then unblock it. This must be performed in a critical section
|
|
|
|
* because signals can be queued from the interrupt level.
|
|
|
|
*/
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
|
|
|
if (stcb->task_state == TSTATE_WAIT_SIG &&
|
|
|
|
sigismember(&stcb->sigwaitmask, info->si_signo))
|
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
memcpy(&stcb->sigunbinfo, info, sizeof(siginfo_t));
|
2007-02-18 00:21:28 +01:00
|
|
|
stcb->sigwaitmask = NULL_SIGNAL_SET;
|
|
|
|
up_unblock_task(stcb);
|
|
|
|
irqrestore(saved_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Its not one we are waiting for... Add it to the list of pending
|
|
|
|
* signals.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irqrestore(saved_state);
|
|
|
|
if (!sig_addpendingsignal(stcb, info))
|
|
|
|
{
|
|
|
|
PANIC(OSERR_FAILEDTOADDSIGNAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/****************** UNMASKED SIGNAL HANDLING ******************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Queue any sigaction's requested by this task. */
|
|
|
|
|
|
|
|
ret = sig_queueaction(stcb, info);
|
|
|
|
|
|
|
|
/* Then schedule execution of the signal handling action on
|
|
|
|
* the recipients thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
up_schedule_sigaction(stcb, sig_deliver);
|
|
|
|
|
|
|
|
/* Check if the task is waiting for an unmasked signal. If so,
|
|
|
|
* then unblock it. This must be performed in a critical section
|
|
|
|
* because signals can be queued from the interrupt level.
|
|
|
|
*/
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
|
|
|
if (stcb->task_state == TSTATE_WAIT_SIG)
|
|
|
|
{
|
2007-02-21 22:55:16 +01:00
|
|
|
memcpy(&stcb->sigunbinfo, info, sizeof(siginfo_t));
|
2007-02-18 00:21:28 +01:00
|
|
|
stcb->sigwaitmask = NULL_SIGNAL_SET;
|
|
|
|
up_unblock_task(stcb);
|
|
|
|
}
|
|
|
|
irqrestore(saved_state);
|
|
|
|
|
|
|
|
/* If the task neither was waiting for the signal nor had a signal
|
|
|
|
* handler attached to the signal, then the default action is
|
2007-03-29 15:31:08 +02:00
|
|
|
* simply to ignore the signal
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
/****************** OTHER SIGNAL HANDLING ******************/
|
2007-03-19 17:38:47 +01:00
|
|
|
|
2007-03-29 15:31:08 +02:00
|
|
|
/* If the task is blocked waiting for a semaphore, then that
|
|
|
|
* task must be unblocked when a signal is received.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:31:08 +02:00
|
|
|
if (stcb->task_state == TSTATE_WAIT_SEM)
|
|
|
|
{
|
2011-07-20 15:19:07 +02:00
|
|
|
sem_waitirq(stcb, EINTR);
|
2007-03-29 15:31:08 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:31:08 +02:00
|
|
|
/* If the task is blocked waiting on a message queue, then that
|
|
|
|
* task must be unblocked when a signal is received.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-28 16:48:42 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE
|
2007-03-29 15:31:08 +02:00
|
|
|
if (stcb->task_state == TSTATE_WAIT_MQNOTEMPTY ||
|
|
|
|
stcb->task_state == TSTATE_WAIT_MQNOTFULL)
|
|
|
|
{
|
2011-09-21 15:28:16 +02:00
|
|
|
mq_waitirq(stcb, EINTR);
|
2007-03-29 15:31:08 +02:00
|
|
|
}
|
2007-03-28 16:48:42 +02:00
|
|
|
#endif
|
2007-03-29 15:31:08 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|