2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_start.c
|
2007-03-11 23:19:01 +01:00
|
|
|
*
|
2013-02-04 22:24:00 +01:00
|
|
|
* Copyright (C) 2007-2010, 2013 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-11 23:19:01 +01: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.
|
2008-01-29 01:50:16 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-11 23:19:01 +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.
|
|
|
|
*
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-03-11 23:19:01 +01:00
|
|
|
* Included Files
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2007-03-11 23:19:01 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
2018-08-26 16:49:08 +02:00
|
|
|
#include <string.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2013-03-14 23:44:06 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/sched.h>
|
|
|
|
|
2018-08-26 16:49:08 +02:00
|
|
|
#include "group/group.h"
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 00:44:08 +02:00
|
|
|
#include "task/task.h"
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2013-03-14 23:44:06 +01:00
|
|
|
* Pre-processor Definitions
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2018-08-26 16:49:08 +02:00
|
|
|
|
2014-11-13 01:54:35 +01:00
|
|
|
/* This is an artificial limit to detect error conditions where an argv[]
|
|
|
|
* list is not properly terminated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_START_ARGS 256
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2018-08-26 16:49:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_sigkill_action
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the default action for the SIGKILL signal.
|
|
|
|
*
|
|
|
|
* REVISIT: I think there is an issue here in the PROTECTED and KERNEL
|
|
|
|
* build modes. In those cases, the signal handler will go through a
|
|
|
|
* trampoline that drops to user mode for execution of the signal handler.
|
|
|
|
* In the PROTECTED mode, this will forward the call to here in user mode
|
|
|
|
* which will result in a crash. The behavior in KERNEL mode in
|
|
|
|
* indeterminate.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Standard signal handler parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SIG_SIGKILL
|
|
|
|
static void task_sigkill_action(int signo, siginfo_t *siginfo, void *arg)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
FAR struct task_tcb_s *tcb = (FAR struct task_tcb_s *)this_task();
|
|
|
|
group_killchildren(tcb);
|
|
|
|
#endif
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: task_setup_sigkill
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup the default action for the SIGKILL signal
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void task_setup_sigkill(void)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_sigaction = task_sigkill_action;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
sigaction(SIGKILL, &sa, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-03-11 23:19:01 +01:00
|
|
|
* Public Functions
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2008-01-29 01:50:16 +01:00
|
|
|
/****************************************************************************
|
2007-03-11 23:19:01 +01:00
|
|
|
* Name: task_start
|
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function is the low level entry point into the main thread of
|
|
|
|
* execution of a task. It receives initial control when the task is
|
|
|
|
* started and calls main entry point of the newly started task.
|
2007-03-11 23:19:01 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2007-03-11 23:19:01 +01:00
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-03-11 23:19:01 +01:00
|
|
|
* None
|
|
|
|
*
|
2008-01-29 01:50:16 +01:00
|
|
|
****************************************************************************/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
|
|
|
void task_start(void)
|
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct task_tcb_s *tcb = (FAR struct task_tcb_s *)this_task();
|
2013-03-14 23:44:06 +01:00
|
|
|
int exitcode;
|
2007-03-11 23:19:01 +01:00
|
|
|
int argc;
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
DEBUGASSERT((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD);
|
|
|
|
|
2018-08-26 16:49:08 +02:00
|
|
|
/* Set up default signal actions */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SIG_SIGKILL
|
|
|
|
task_setup_sigkill();
|
|
|
|
#endif
|
|
|
|
|
2013-01-27 16:52:58 +01:00
|
|
|
/* Execute the start hook if one has been registered */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SCHED_STARTHOOK
|
2017-10-02 22:04:25 +02:00
|
|
|
if (tcb->starthook != NULL)
|
2013-01-27 16:52:58 +01:00
|
|
|
{
|
|
|
|
tcb->starthook(tcb->starthookarg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-13 02:01:29 +01:00
|
|
|
/* Count how many non-null arguments we are passing. The first non-null
|
|
|
|
* argument terminates the list .
|
|
|
|
*/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2014-11-13 02:01:29 +01:00
|
|
|
argc = 1;
|
|
|
|
while (tcb->argv[argc])
|
2007-03-11 23:19:01 +01:00
|
|
|
{
|
2014-11-13 02:01:29 +01:00
|
|
|
/* Increment the number of args. Here is a sanity check to
|
|
|
|
* prevent running away with an unterminated argv[] list.
|
|
|
|
* MAX_START_ARGS should be sufficiently large that this never
|
|
|
|
* happens in normal usage.
|
|
|
|
*/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2014-11-13 02:01:29 +01:00
|
|
|
if (++argc > MAX_START_ARGS)
|
2012-07-14 21:30:31 +02:00
|
|
|
{
|
2014-11-13 02:01:29 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2012-07-14 21:30:31 +02:00
|
|
|
}
|
2007-03-11 23:19:01 +01:00
|
|
|
}
|
|
|
|
|
2013-03-14 23:44:06 +01:00
|
|
|
/* Call the 'main' entry point passing argc and argv. In the kernel build
|
|
|
|
* this has to be handled differently if we are starting a user-space task;
|
|
|
|
* we have to switch to user-mode before calling the task.
|
2010-09-05 21:13:48 +02:00
|
|
|
*/
|
2007-03-11 23:19:01 +01:00
|
|
|
|
2014-08-29 22:47:22 +02:00
|
|
|
#if defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)
|
2013-03-14 23:44:06 +01:00
|
|
|
if ((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
|
|
|
|
{
|
|
|
|
up_task_start(tcb->cmn.entry.main, argc, tcb->argv);
|
|
|
|
exitcode = EXIT_FAILURE; /* Should not get here */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
exitcode = tcb->cmn.entry.main(argc, tcb->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call exit() if/when the task returns */
|
|
|
|
|
|
|
|
exit(exitcode);
|
2007-03-11 23:19:01 +01:00
|
|
|
}
|