2011-05-28 23:42:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_exithook.c
|
2011-05-28 23:42:18 +02:00
|
|
|
*
|
2015-08-14 18:10:32 +02:00
|
|
|
* Copyright (C) 2011-2013, 2015 Gregory Nutt. All rights reserved.
|
2012-02-01 00:39:12 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-05-28 23:42:18 +02:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2013-01-12 20:58:45 +01:00
|
|
|
#include <signal.h>
|
2011-05-28 23:42:18 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-09-13 21:33:47 +02:00
|
|
|
#include <nuttx/sched.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-05-28 23:42:18 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2014-08-08 20:44:44 +02:00
|
|
|
#include "signal/signal.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2011-05-28 23:42:18 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-01 19:47:54 +02:00
|
|
|
* Name: task_atexit
|
2011-05-28 23:42:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-04-27 19:38:24 +02:00
|
|
|
* Call any registered atexit function(s)
|
2011-05-28 23:42:18 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2012-11-01 17:50:53 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_SCHED_ATEXIT) && !defined(CONFIG_SCHED_ONEXIT)
|
2013-02-04 19:46:28 +01:00
|
|
|
static inline void task_atexit(FAR struct tcb_s *tcb)
|
2011-05-28 23:42:18 +02:00
|
|
|
{
|
2013-02-04 16:29:19 +01:00
|
|
|
FAR struct task_group_s *group = tcb->group;
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
/* Make sure that we have not already left the group. Only the final
|
2014-04-27 19:38:24 +02:00
|
|
|
* exiting thread in the task group should trigger the atexit()
|
2013-02-04 16:29:19 +01:00
|
|
|
* callbacks.
|
2016-12-08 14:52:02 +01:00
|
|
|
*
|
|
|
|
* REVISIT: This is a security problem In the PROTECTED and KERNEL builds:
|
2016-12-08 16:27:13 +01:00
|
|
|
* We must not call the registered function in supervisor mode! See also
|
|
|
|
* on_exit() and pthread_cleanup_pop() callbacks.
|
2016-12-08 17:24:40 +01:00
|
|
|
*
|
|
|
|
* REVISIT: In the case of task_delete(), the callback would execute in
|
|
|
|
* the context the caller of task_delete() cancel, not in the context of
|
|
|
|
* the exiting task (or process).
|
2011-05-28 23:42:18 +02:00
|
|
|
*/
|
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
if (group && group->tg_nmembers == 1)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-02-04 16:29:19 +01:00
|
|
|
#if defined(CONFIG_SCHED_ATEXIT_MAX) && CONFIG_SCHED_ATEXIT_MAX > 1
|
|
|
|
int index;
|
|
|
|
|
|
|
|
/* Call each atexit function in reverse order of registration atexit()
|
2014-04-27 19:38:24 +02:00
|
|
|
* functions are registered from lower to higher array indices; they
|
2013-02-04 16:29:19 +01:00
|
|
|
* must be called in the reverse order of registration when the task
|
|
|
|
* group exits, i.e., from higher to lower indices.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (index = CONFIG_SCHED_ATEXIT_MAX-1; index >= 0; index--)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-02-04 16:29:19 +01:00
|
|
|
if (group->tg_atexitfunc[index])
|
|
|
|
{
|
|
|
|
/* Call the atexit function */
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
(*group->tg_atexitfunc[index])();
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
/* Nullify the atexit function to prevent its reuse. */
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
group->tg_atexitfunc[index] = NULL;
|
|
|
|
}
|
2012-08-01 19:47:54 +02:00
|
|
|
}
|
|
|
|
#else
|
2013-02-04 16:29:19 +01:00
|
|
|
if (group->tg_atexitfunc)
|
|
|
|
{
|
|
|
|
/* Call the atexit function */
|
2012-04-23 15:59:31 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
(*group->tg_atexitfunc)();
|
2012-04-23 15:59:31 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
/* Nullify the atexit function to prevent its reuse. */
|
2012-04-23 15:59:31 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
group->tg_atexitfunc = NULL;
|
|
|
|
}
|
2011-05-28 23:42:18 +02:00
|
|
|
#endif
|
2013-02-04 16:29:19 +01:00
|
|
|
}
|
2012-11-25 19:00:40 +01:00
|
|
|
}
|
2012-08-01 19:47:54 +02:00
|
|
|
#else
|
|
|
|
# define task_atexit(tcb)
|
|
|
|
#endif
|
2011-05-28 23:42:18 +02:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_onexit
|
|
|
|
*
|
|
|
|
* Description:
|
2014-04-27 19:38:24 +02:00
|
|
|
* Call any registered on_exit function(s)
|
2012-08-01 19:47:54 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2013-04-24 00:41:43 +02:00
|
|
|
|
2012-02-01 00:39:12 +01:00
|
|
|
#ifdef CONFIG_SCHED_ONEXIT
|
2013-02-04 19:46:28 +01:00
|
|
|
static inline void task_onexit(FAR struct tcb_s *tcb, int status)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-02-04 16:29:19 +01:00
|
|
|
FAR struct task_group_s *group = tcb->group;
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
/* Make sure that we have not already left the group. Only the final
|
2014-04-27 19:38:24 +02:00
|
|
|
* exiting thread in the task group should trigger the atexit()
|
2013-02-04 16:29:19 +01:00
|
|
|
* callbacks.
|
2016-12-08 14:52:02 +01:00
|
|
|
*
|
|
|
|
* REVISIT: This is a security problem In the PROTECTED and KERNEL builds:
|
2016-12-08 16:27:13 +01:00
|
|
|
* We must not call the registered function in supervisor mode! See also
|
|
|
|
* atexit() and pthread_cleanup_pop() callbacks.
|
2016-12-08 17:24:40 +01:00
|
|
|
*
|
2016-12-09 20:49:36 +01:00
|
|
|
* REVISIT: In the case of task_delete(), the callback would execute in
|
2016-12-08 17:24:40 +01:00
|
|
|
* he context the caller of task_delete() cancel, not in the context of
|
|
|
|
* the exiting task (or process).
|
2012-08-01 19:47:54 +02:00
|
|
|
*/
|
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
if (group && group->tg_nmembers == 1)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-02-04 16:29:19 +01:00
|
|
|
#if defined(CONFIG_SCHED_ONEXIT_MAX) && CONFIG_SCHED_ONEXIT_MAX > 1
|
|
|
|
int index;
|
|
|
|
|
|
|
|
/* Call each on_exit function in reverse order of registration.
|
2014-04-27 19:38:24 +02:00
|
|
|
* on_exit() functions are registered from lower to higher array
|
2013-02-04 16:29:19 +01:00
|
|
|
* indices; they must be called in the reverse order of registration
|
2014-04-27 19:38:24 +02:00
|
|
|
* when the task group exits, i.e., from higher to lower indices.
|
2013-02-04 16:29:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (index = CONFIG_SCHED_ONEXIT_MAX-1; index >= 0; index--)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-02-04 16:29:19 +01:00
|
|
|
if (group->tg_onexitfunc[index])
|
|
|
|
{
|
|
|
|
/* Call the on_exit function */
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
(*group->tg_onexitfunc[index])(status, group->tg_onexitarg[index]);
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
/* Nullify the on_exit function to prevent its reuse. */
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
group->tg_onexitfunc[index] = NULL;
|
|
|
|
}
|
2012-08-01 19:47:54 +02:00
|
|
|
}
|
|
|
|
#else
|
2013-02-04 16:29:19 +01:00
|
|
|
if (group->tg_onexitfunc)
|
|
|
|
{
|
|
|
|
/* Call the on_exit function */
|
2012-04-23 15:59:31 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
(*group->tg_onexitfunc)(status, group->tg_onexitarg);
|
2012-04-23 15:59:31 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
/* Nullify the on_exit function to prevent its reuse. */
|
2012-04-23 15:59:31 +02:00
|
|
|
|
2013-02-04 16:29:19 +01:00
|
|
|
group->tg_onexitfunc = NULL;
|
|
|
|
}
|
2011-05-28 23:42:18 +02:00
|
|
|
#endif
|
2013-02-04 16:29:19 +01:00
|
|
|
}
|
2012-09-14 16:07:21 +02:00
|
|
|
}
|
2012-08-01 19:47:54 +02:00
|
|
|
#else
|
|
|
|
# define task_onexit(tcb,status)
|
|
|
|
#endif
|
2011-05-28 23:42:18 +02:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_exitstatus
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Report exit status when main task of a task group exits
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_CHILD_STATUS
|
|
|
|
static inline void task_exitstatus(FAR struct task_group_s *group, int status)
|
|
|
|
{
|
|
|
|
FAR struct child_status_s *child;
|
|
|
|
|
|
|
|
/* Check if the parent task group has suppressed retention of
|
|
|
|
* child exit status information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0)
|
|
|
|
{
|
|
|
|
/* No.. Find the exit status entry for this task in the parent TCB */
|
|
|
|
|
|
|
|
child = group_findchild(group, getpid());
|
|
|
|
DEBUGASSERT(child);
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
#ifndef HAVE_GROUP_MEMBERS
|
|
|
|
/* No group members? Save the exit status */
|
|
|
|
|
|
|
|
child->ch_status = status;
|
|
|
|
#endif
|
|
|
|
/* Save the exit status.. For the case of HAVE_GROUP_MEMBERS,
|
|
|
|
* the child status will be as exited until the last member
|
|
|
|
* of the task group exits.
|
|
|
|
*/
|
|
|
|
|
|
|
|
child->ch_status = status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
# define task_exitstatus(group,status)
|
|
|
|
|
|
|
|
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_groupexit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Mark that the final thread of a child task group as exited.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_CHILD_STATUS
|
|
|
|
static inline void task_groupexit(FAR struct task_group_s *group)
|
|
|
|
{
|
|
|
|
FAR struct child_status_s *child;
|
|
|
|
|
|
|
|
/* Check if the parent task group has suppressed retention of child exit
|
|
|
|
* status information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0)
|
|
|
|
{
|
|
|
|
/* No.. Find the exit status entry for this task in the parent TCB */
|
|
|
|
|
|
|
|
child = group_findchild(group, getpid());
|
|
|
|
DEBUGASSERT(child);
|
|
|
|
if (child)
|
|
|
|
{
|
2014-04-27 19:38:24 +02:00
|
|
|
/* Mark that all members of the child task group has exited */
|
2013-01-26 21:17:29 +01:00
|
|
|
|
|
|
|
child->ch_flags |= CHILD_FLAG_EXITED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# define task_groupexit(group)
|
|
|
|
|
|
|
|
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
|
|
|
|
2013-01-12 20:58:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_sigchild
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the SIGCHILD signal to the parent thread
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
2013-01-26 18:28:20 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
2013-02-04 19:46:28 +01:00
|
|
|
static inline void task_sigchild(gid_t pgid, FAR struct tcb_s *ctcb, int status)
|
2013-01-26 18:28:20 +01:00
|
|
|
{
|
|
|
|
FAR struct task_group_s *chgrp = ctcb->group;
|
|
|
|
FAR struct task_group_s *pgrp;
|
|
|
|
siginfo_t info;
|
|
|
|
|
|
|
|
DEBUGASSERT(chgrp);
|
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
/* Get the parent task group. It is possible that all of the members of
|
2013-04-24 00:41:43 +02:00
|
|
|
* the parent task group have exited. This would not be an error. In
|
2013-01-26 21:17:29 +01:00
|
|
|
* this case, the child task group has been orphaned.
|
|
|
|
*/
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
pgrp = group_findbygid(pgid);
|
2013-01-26 21:17:29 +01:00
|
|
|
if (!pgrp)
|
2013-01-26 18:28:20 +01:00
|
|
|
{
|
2013-01-26 21:17:29 +01:00
|
|
|
/* Set the task group ID to an invalid group ID. The dead parent
|
|
|
|
* task group ID could get reused some time in the future.
|
2013-01-26 18:28:20 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
chgrp->tg_pgid = INVALID_GROUP_ID;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
/* Save the exit status now if this is the main thread of the task group
|
|
|
|
* that is exiting. Only the exiting main task of a task group carries
|
|
|
|
* interpretable exit Check if this is the main task that is exiting.
|
|
|
|
*/
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if ((ctcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
#endif
|
2013-01-26 21:17:29 +01:00
|
|
|
{
|
|
|
|
task_exitstatus(pgrp, status);
|
|
|
|
}
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
/* But only the final exiting thread in a task group, whatever it is,
|
|
|
|
* should generate SIGCHLD.
|
|
|
|
*/
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
if (chgrp->tg_nmembers == 1)
|
|
|
|
{
|
|
|
|
/* Mark that all of the threads in the task group have exited */
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
task_groupexit(pgrp);
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
/* Create the siginfo structure. We don't actually know the cause.
|
2014-04-27 19:38:24 +02:00
|
|
|
* That is a bug. Let's just say that the child task just exited
|
2013-01-26 18:28:20 +01:00
|
|
|
* for now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
info.si_signo = SIGCHLD;
|
|
|
|
info.si_code = CLD_EXITED;
|
2015-08-14 18:10:32 +02:00
|
|
|
info.si_errno = OK;
|
2013-01-26 18:28:20 +01:00
|
|
|
info.si_value.sival_ptr = NULL;
|
2013-02-04 23:38:59 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
info.si_pid = chgrp->tg_task;
|
|
|
|
#else
|
2013-01-26 18:28:20 +01:00
|
|
|
info.si_pid = ctcb->pid;
|
2013-02-04 23:38:59 +01:00
|
|
|
#endif
|
2013-01-26 18:28:20 +01:00
|
|
|
info.si_status = status;
|
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
/* Send the signal to one thread in the group */
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
(void)group_signal(pgrp, &info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_GROUP_MEMBERS */
|
|
|
|
|
2013-02-04 23:38:59 +01:00
|
|
|
static inline void task_sigchild(FAR struct tcb_s *ptcb,
|
|
|
|
FAR struct tcb_s *ctcb, int status)
|
2013-01-12 20:58:45 +01:00
|
|
|
{
|
|
|
|
siginfo_t info;
|
|
|
|
|
2013-01-26 18:28:20 +01:00
|
|
|
/* If task groups are not supported then we will report SIGCHLD when the
|
|
|
|
* task exits. Unfortunately, there could still be threads in the group
|
|
|
|
* that are still running.
|
2013-01-23 23:23:46 +01:00
|
|
|
*/
|
2013-01-12 20:58:45 +01:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if ((ctcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
#endif
|
2013-01-23 23:23:46 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_SCHED_CHILD_STATUS
|
2013-01-26 21:17:29 +01:00
|
|
|
/* Save the exit status now of the main thread */
|
2013-01-13 19:53:00 +01:00
|
|
|
|
2013-01-26 21:17:29 +01:00
|
|
|
task_exitstatus(ptcb->group, status);
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
#else /* CONFIG_SCHED_CHILD_STATUS */
|
2016-09-06 16:28:31 +02:00
|
|
|
/* Exit status is not retained. Just decrement the number of
|
|
|
|
* children from this parent.
|
|
|
|
*/
|
2013-01-26 18:28:20 +01:00
|
|
|
|
2016-09-06 16:28:31 +02:00
|
|
|
DEBUGASSERT(ptcb->group != NULL && ptcb->group->tg_nchildren > 0);
|
|
|
|
ptcb->group->tg_nchildren--;
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_SCHED_CHILD_STATUS */
|
2013-01-23 23:23:46 +01:00
|
|
|
|
|
|
|
/* Create the siginfo structure. We don't actually know the cause.
|
2014-04-27 19:38:24 +02:00
|
|
|
* That is a bug. Let's just say that the child task just exited
|
2013-01-23 23:23:46 +01:00
|
|
|
* for now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
info.si_signo = SIGCHLD;
|
|
|
|
info.si_code = CLD_EXITED;
|
2015-08-14 18:10:32 +02:00
|
|
|
info.si_errno = OK;
|
2013-01-23 23:23:46 +01:00
|
|
|
info.si_value.sival_ptr = NULL;
|
2013-02-04 23:38:59 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2013-02-06 01:06:35 +01:00
|
|
|
info.si_pid = ctcb->group->tg_task;
|
2013-02-04 23:38:59 +01:00
|
|
|
#else
|
2013-01-25 18:23:38 +01:00
|
|
|
info.si_pid = ctcb->pid;
|
2013-02-04 23:38:59 +01:00
|
|
|
#endif
|
2013-01-23 23:23:46 +01:00
|
|
|
info.si_status = status;
|
|
|
|
|
|
|
|
/* Send the signal. We need to use this internal interface so that we
|
|
|
|
* can provide the correct si_code value with the signal.
|
|
|
|
*/
|
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
(void)sig_tcbdispatch(ptcb, &info);
|
2013-01-25 18:23:38 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
#endif /* HAVE_GROUP_MEMBERS */
|
|
|
|
#else /* CONFIG_SCHED_HAVE_PARENT */
|
|
|
|
|
|
|
|
# define task_sigchild(x,ctcb,status)
|
|
|
|
|
|
|
|
#endif /* CONFIG_SCHED_HAVE_PARENT */
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2013-04-24 01:41:30 +02:00
|
|
|
* Name: task_signalparent
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2013-04-24 01:41:30 +02:00
|
|
|
* Send the SIGCHILD signal to the parent task group
|
2013-01-25 18:23:38 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_HAVE_PARENT
|
2013-04-24 01:41:30 +02:00
|
|
|
static inline void task_signalparent(FAR struct tcb_s *ctcb, int status)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
2013-01-26 18:28:20 +01:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
DEBUGASSERT(ctcb && ctcb->group);
|
|
|
|
|
|
|
|
/* Keep things stationary throughout the following */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Send SIGCHLD to all members of the parent's task group */
|
|
|
|
|
|
|
|
task_sigchild(ctcb->group->tg_pgid, ctcb, status);
|
|
|
|
sched_unlock();
|
|
|
|
#else
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *ptcb;
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
/* Keep things stationary throughout the following */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Get the TCB of the receiving, parent task. We do this early to
|
2016-09-06 16:28:31 +02:00
|
|
|
* handle multiple calls to task_signalparent. ctcb->group->tg_ppid is
|
|
|
|
* set to an invalid value below and the following call will fail if we
|
|
|
|
* are called again.
|
2013-01-25 18:23:38 +01:00
|
|
|
*/
|
|
|
|
|
2016-09-06 16:28:31 +02:00
|
|
|
ptcb = sched_gettcb(ctcb->group->tg_ppid);
|
|
|
|
if (ptcb == NULL)
|
2013-01-25 18:23:38 +01:00
|
|
|
{
|
|
|
|
/* The parent no longer exists... bail */
|
|
|
|
|
2013-01-23 23:23:46 +01:00
|
|
|
sched_unlock();
|
2013-01-25 18:23:38 +01:00
|
|
|
return;
|
2013-01-23 23:23:46 +01:00
|
|
|
}
|
2013-01-25 18:23:38 +01:00
|
|
|
|
|
|
|
/* Send SIGCHLD to all members of the parent's task group */
|
|
|
|
|
|
|
|
task_sigchild(ptcb, ctcb, status);
|
|
|
|
|
2013-01-26 18:28:20 +01:00
|
|
|
/* Forget who our parent was */
|
2013-01-25 18:23:38 +01:00
|
|
|
|
2016-09-06 16:28:31 +02:00
|
|
|
ctcb->group->tg_ppid = INVALID_PROCESS_ID;
|
2013-01-25 18:23:38 +01:00
|
|
|
sched_unlock();
|
2013-01-26 18:28:20 +01:00
|
|
|
#endif
|
2013-01-12 20:58:45 +01:00
|
|
|
}
|
|
|
|
#else
|
2013-04-24 01:41:30 +02:00
|
|
|
# define task_signalparent(ctcb,status)
|
2013-01-12 20:58:45 +01:00
|
|
|
#endif
|
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_exitwakeup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Wakeup any tasks waiting for this task to exit
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-01-13 19:53:00 +01:00
|
|
|
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)
|
2013-02-04 19:46:28 +01:00
|
|
|
static inline void task_exitwakeup(FAR struct tcb_s *tcb, int status)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-02-04 17:02:20 +01:00
|
|
|
FAR struct task_group_s *group = tcb->group;
|
|
|
|
|
|
|
|
/* Have we already left the group? */
|
2011-05-28 23:42:18 +02:00
|
|
|
|
2013-02-04 17:02:20 +01:00
|
|
|
if (group)
|
2011-05-28 23:42:18 +02:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
/* Only tasks (and kernel threads) return valid status. Record the
|
|
|
|
* exit status when the task exists. The group, however, may still
|
|
|
|
* be executing.
|
2011-05-28 23:42:18 +02:00
|
|
|
*/
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
#endif
|
2011-05-28 23:42:18 +02:00
|
|
|
{
|
2013-02-04 17:02:20 +01:00
|
|
|
/* Report the exit status. We do not nullify tg_statloc here
|
2014-04-27 19:38:24 +02:00
|
|
|
* because we want to prevent other tasks from registering for
|
2013-02-04 17:02:20 +01:00
|
|
|
* the return status. There is only one task per task group,
|
|
|
|
* there for, this logic should execute exactly once in the
|
|
|
|
* lifetime of the task group.
|
|
|
|
*
|
|
|
|
* "If more than one thread is suspended in waitpid() awaiting
|
|
|
|
* termination of the same process, exactly one thread will
|
2013-04-24 00:41:43 +02:00
|
|
|
* return the process status at the time of the target process
|
|
|
|
* termination."
|
2013-02-04 17:02:20 +01:00
|
|
|
*
|
|
|
|
* Hmmm.. what do we return to the others?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (group->tg_statloc)
|
|
|
|
{
|
|
|
|
*group->tg_statloc = status << 8;
|
|
|
|
}
|
2011-05-28 23:42:18 +02:00
|
|
|
}
|
|
|
|
|
2013-02-04 17:02:20 +01:00
|
|
|
/* Is this the last thread in the group? */
|
2011-05-28 23:42:18 +02:00
|
|
|
|
2013-02-04 17:02:20 +01:00
|
|
|
if (group->tg_nmembers == 1)
|
|
|
|
{
|
|
|
|
/* Yes.. Wakeup any tasks waiting for this task to exit */
|
|
|
|
|
2015-10-08 03:59:14 +02:00
|
|
|
group->tg_statloc = NULL;
|
|
|
|
while (group->tg_exitsem.semcount < 0)
|
2013-04-24 00:41:43 +02:00
|
|
|
{
|
2013-02-04 17:02:20 +01:00
|
|
|
/* Wake up the thread */
|
|
|
|
|
|
|
|
sem_post(&group->tg_exitsem);
|
|
|
|
}
|
|
|
|
}
|
2011-05-28 23:42:18 +02:00
|
|
|
}
|
2012-08-01 19:47:54 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define task_exitwakeup(tcb, status)
|
2011-05-28 23:42:18 +02:00
|
|
|
#endif
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-04-18 19:41:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_flushstreams
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Flush all streams when the final thread of a group exits.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_STREAMS > 0
|
|
|
|
static inline void task_flushstreams(FAR struct tcb_s *tcb)
|
|
|
|
{
|
|
|
|
FAR struct task_group_s *group = tcb->group;
|
|
|
|
|
|
|
|
/* Have we already left the group? Are we the last thread in the group? */
|
|
|
|
|
|
|
|
if (group && group->tg_nmembers == 1)
|
|
|
|
{
|
2014-08-29 22:47:22 +02:00
|
|
|
#if (defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)) && \
|
|
|
|
defined(CONFIG_MM_KERNEL_HEAP)
|
2013-04-18 19:41:11 +02:00
|
|
|
(void)lib_flushall(tcb->group->tg_streamlist);
|
|
|
|
#else
|
|
|
|
(void)lib_flushall(&tcb->group->tg_streamlist);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define task_flushstreams(tcb)
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-04-18 19:41:11 +02:00
|
|
|
* Name: task_exithook
|
2012-08-01 19:47:54 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements some of the internal logic of exit() and
|
2014-04-27 19:38:24 +02:00
|
|
|
* task_delete(). This function performs some clean-up and other actions
|
2013-02-04 16:29:19 +01:00
|
|
|
* required when a task exits:
|
2012-08-01 19:47:54 +02:00
|
|
|
*
|
|
|
|
* - All open streams are flushed and closed.
|
|
|
|
* - All functions registered with atexit() and on_exit() are called, in
|
|
|
|
* the reverse order of their registration.
|
|
|
|
*
|
|
|
|
* When called from exit(), the tcb still resides at the head of the ready-
|
|
|
|
* to-run list. The following logic is safe because we will not be
|
|
|
|
* returning from the exit() call.
|
|
|
|
*
|
2013-04-24 00:41:43 +02:00
|
|
|
* When called from task_terminate() we are operating on a different thread;
|
2012-08-01 19:47:54 +02:00
|
|
|
* on the thread that called task_delete(). In this case, task_delete
|
|
|
|
* will have already removed the tcb from the ready-to-run list to prevent
|
|
|
|
* any further action on this task.
|
|
|
|
*
|
2013-04-24 00:41:43 +02:00
|
|
|
* nonblocking will be set true only when we are called from task_terminate()
|
|
|
|
* via _exit(). In that case, we must be careful to do nothing that can
|
|
|
|
* cause the cause the thread to block.
|
|
|
|
*
|
2012-08-01 19:47:54 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-04-24 00:41:43 +02:00
|
|
|
void task_exithook(FAR struct tcb_s *tcb, int status, bool nonblocking)
|
2012-08-01 19:47:54 +02:00
|
|
|
{
|
2013-01-26 18:28:20 +01:00
|
|
|
/* Under certain conditions, task_exithook() can be called multiple times.
|
|
|
|
* A bit in the TCB was set the first time this function was called. If
|
2013-02-04 17:02:20 +01:00
|
|
|
* that bit is set, then just exit doing nothing more..
|
2013-01-26 18:28:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if ((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-09 19:01:18 +01:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* Mark the task as non-cancelable to avoid additional calls to exit()
|
|
|
|
* due to any cancellation point logic that might get kicked off by
|
|
|
|
* actions taken during exit processing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcb->flags |= TCB_FLAG_NONCANCELABLE;
|
|
|
|
tcb->flags &= ~TCB_FLAG_CANCEL_PENDING;
|
|
|
|
tcb->cpcount = 0;
|
|
|
|
#endif
|
|
|
|
|
2014-09-15 20:45:41 +02:00
|
|
|
#if defined(CONFIG_SCHED_ATEXIT) || defined(CONFIG_SCHED_ONEXIT)
|
2012-08-01 19:47:54 +02:00
|
|
|
/* If exit function(s) were registered, call them now before we do any un-
|
2013-04-24 00:41:43 +02:00
|
|
|
* initialization.
|
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
*
|
|
|
|
* 1. In the case of task_delete(), the exit function will *not* be called
|
|
|
|
* on the thread execution of the task being deleted! That is probably
|
|
|
|
* a bug.
|
|
|
|
* 2. We cannot call the exit functions if nonblocking is requested: These
|
|
|
|
* functions might block.
|
|
|
|
* 3. This function will only be called with with non-blocking == true
|
|
|
|
* only when called through _exit(). _exit() behaviors requires that
|
|
|
|
* the exit functions *not* be called.
|
2012-08-01 19:47:54 +02:00
|
|
|
*/
|
|
|
|
|
2013-04-24 00:41:43 +02:00
|
|
|
if (!nonblocking)
|
|
|
|
{
|
|
|
|
task_atexit(tcb);
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-04-24 00:41:43 +02:00
|
|
|
/* Call any registered on_exit function(s) */
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-04-24 00:41:43 +02:00
|
|
|
task_onexit(tcb, status);
|
|
|
|
}
|
2013-04-24 01:41:30 +02:00
|
|
|
#endif
|
2012-08-01 19:47:54 +02:00
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
/* If the task was terminated by another task, it may be in an unknown
|
2013-02-06 16:43:28 +01:00
|
|
|
* state. Make some feeble effort to recover the state.
|
2013-02-05 20:50:37 +01:00
|
|
|
*/
|
|
|
|
|
2013-02-06 16:43:28 +01:00
|
|
|
task_recover(tcb);
|
2013-02-05 20:50:37 +01:00
|
|
|
|
2013-04-24 01:41:30 +02:00
|
|
|
/* Send the SIGCHILD signal to the parent task group */
|
2013-01-13 19:53:00 +01:00
|
|
|
|
2013-04-24 01:41:30 +02:00
|
|
|
task_signalparent(tcb, status);
|
2013-01-13 19:53:00 +01:00
|
|
|
|
2012-08-01 19:47:54 +02:00
|
|
|
/* Wakeup any tasks waiting for this task to exit */
|
|
|
|
|
|
|
|
task_exitwakeup(tcb, status);
|
|
|
|
|
2013-04-18 19:41:11 +02:00
|
|
|
/* If this is the last thread in the group, then flush all streams (File
|
|
|
|
* descriptors will be closed when the TCB is deallocated).
|
2013-04-24 00:41:43 +02:00
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
* 1. We cannot flush the buffered I/O if nonblocking is requested.
|
|
|
|
* that might cause this logic to block.
|
|
|
|
* 2. This function will only be called with with non-blocking == true
|
|
|
|
* only when called through _exit(). _exit() behavior does not
|
|
|
|
* require that the streams be flushed
|
2012-02-01 00:39:12 +01:00
|
|
|
*/
|
2011-05-28 23:42:18 +02:00
|
|
|
|
2013-04-24 00:41:43 +02:00
|
|
|
if (!nonblocking)
|
|
|
|
{
|
|
|
|
task_flushstreams(tcb);
|
|
|
|
}
|
2012-02-01 00:39:12 +01:00
|
|
|
|
2014-09-15 20:45:41 +02:00
|
|
|
#ifdef HAVE_TASK_GROUP
|
2013-01-25 18:23:38 +01:00
|
|
|
/* Leave the task group. Perhaps discarding any un-reaped child
|
|
|
|
* status (no zombies here!)
|
|
|
|
*/
|
2013-01-23 23:23:46 +01:00
|
|
|
|
2013-01-25 18:23:38 +01:00
|
|
|
group_leave(tcb);
|
2013-01-23 23:23:46 +01:00
|
|
|
#endif
|
|
|
|
|
2014-09-15 20:45:41 +02:00
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
2012-02-01 00:39:12 +01:00
|
|
|
/* Deallocate anything left in the TCB's queues */
|
|
|
|
|
|
|
|
sig_cleanup(tcb); /* Deallocate Signal lists */
|
2011-05-28 23:42:18 +02:00
|
|
|
#endif
|
2013-01-26 18:28:20 +01:00
|
|
|
|
|
|
|
/* This function can be re-entered in certain cases. Set a flag
|
|
|
|
* bit in the TCB to not that we have already completed this exit
|
|
|
|
* processing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tcb->flags |= TCB_FLAG_EXIT_PROCESSING;
|
2011-05-28 23:42:18 +02:00
|
|
|
}
|