2013-02-02 20:31:30 +01:00
|
|
|
/****************************************************************************
|
2014-08-09 00:44:08 +02:00
|
|
|
* sched/task/task_spawn.c
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-29 19:57:53 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <spawn.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2013-02-02 20:31:30 +01:00
|
|
|
#include <debug.h>
|
2021-05-14 04:29:48 +02:00
|
|
|
#include <errno.h>
|
2013-02-02 20:31:30 +01:00
|
|
|
|
2023-10-22 11:06:46 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2018-01-30 18:07:36 +01:00
|
|
|
#include <nuttx/sched.h>
|
2018-01-31 23:11:54 +01:00
|
|
|
#include <nuttx/kthread.h>
|
2019-08-23 21:20:52 +02:00
|
|
|
#include <nuttx/spawn.h>
|
2018-01-30 18:07:36 +01: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-09 00:44:08 +02:00
|
|
|
#include "task/spawn.h"
|
|
|
|
#include "task/task.h"
|
2013-02-02 20:31:30 +01:00
|
|
|
|
2014-09-16 23:37:05 +02:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
2014-09-14 16:22:21 +02:00
|
|
|
|
2013-02-02 20:31:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxtask_spawn_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function creates and activates a new thread of the specified type
|
|
|
|
* with a specified priority and returns its system-assigned ID. It is the
|
|
|
|
* internal, common implementation of task_create() and kthread_create().
|
|
|
|
* See comments with task_create() for further information.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - Name of the new task
|
|
|
|
* ttype - Type of the new task
|
|
|
|
* priority - Priority of the new task
|
|
|
|
* stack_addr - Address of the stack needed
|
|
|
|
* stack_size - Size (in bytes) of the stack needed
|
|
|
|
* entry - Entry point of a new task
|
|
|
|
* arg - A pointer to an array of input parameters. The array
|
|
|
|
* should be terminated with a NULL argv[] value. If no
|
|
|
|
* parameters are required, argv may be NULL.
|
|
|
|
* envp - A pointer to an array of environment strings. Terminated
|
|
|
|
* with a NULL entry.
|
|
|
|
* actions - The spawn file actions
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns the positive, non-zero process ID of the new task or a negated
|
|
|
|
* errno value to indicate the nature of any failure. If memory is
|
|
|
|
* insufficient or the task cannot be created -ENOMEM will be returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxtask_spawn_create(FAR const char *name, int priority,
|
|
|
|
FAR void *stack_addr, int stack_size,
|
|
|
|
main_t entry, FAR char * const argv[],
|
|
|
|
FAR char * const envp[],
|
2023-10-25 09:39:17 +02:00
|
|
|
FAR const posix_spawn_file_actions_t *actions,
|
|
|
|
FAR const posix_spawnattr_t *attr)
|
2023-04-26 05:44:08 +02:00
|
|
|
{
|
|
|
|
FAR struct task_tcb_s *tcb;
|
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
tcb = kmm_zalloc(sizeof(struct task_tcb_s));
|
2023-04-26 05:44:08 +02:00
|
|
|
if (tcb == NULL)
|
|
|
|
{
|
|
|
|
serr("ERROR: Failed to allocate TCB\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the task type */
|
|
|
|
|
sched/tcb: add free tcb flag to support static tcb
Add support for static tcb, applications in some special case can
initialize system resources in advance through static tcb.
| static struct task_tcb_s g_tcb;
|
| memset(&g_tcb, 0, sizeof(struct task_tcb_s));
| g_tcb.cmn.flags = TCB_FLAG_TTYPE_KERNEL;
| nxtask_init(&g_tcb, "PTCB", 101, NULL, 1024, ptcb_task, NULL, NULL, NULL);
|
| ...
| nxtask_activate(&g_tcb.cmn);
Signed-off-by: chao an <anchao@lixiang.com>
2024-02-26 08:50:56 +01:00
|
|
|
tcb->cmn.flags = TCB_FLAG_TTYPE_TASK | TCB_FLAG_FREE_TCB;
|
2023-04-26 05:44:08 +02:00
|
|
|
|
|
|
|
/* Initialize the task */
|
|
|
|
|
|
|
|
ret = nxtask_init(tcb, name, priority, stack_addr, stack_size,
|
2023-11-13 13:59:42 +01:00
|
|
|
entry, argv, envp, actions);
|
2023-04-26 05:44:08 +02:00
|
|
|
if (ret < OK)
|
|
|
|
{
|
|
|
|
kmm_free(tcb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-25 09:39:17 +02:00
|
|
|
/* Get the assigned pid before we start the task */
|
|
|
|
|
|
|
|
pid = tcb->cmn.pid;
|
|
|
|
|
|
|
|
/* Set the attributes */
|
2023-04-26 05:44:08 +02:00
|
|
|
|
2023-10-25 09:39:17 +02:00
|
|
|
if (attr)
|
|
|
|
{
|
|
|
|
ret = spawn_execattrs(pid, attr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_taskinit;
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 05:44:08 +02:00
|
|
|
|
|
|
|
/* Activate the task */
|
|
|
|
|
|
|
|
nxtask_activate(&tcb->cmn);
|
|
|
|
|
2023-06-18 22:20:04 +02:00
|
|
|
return pid;
|
2023-10-25 09:39:17 +02:00
|
|
|
|
|
|
|
errout_with_taskinit:
|
|
|
|
nxtask_uninit(tcb);
|
|
|
|
return ret;
|
2023-04-26 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2013-02-02 20:31:30 +01:00
|
|
|
/****************************************************************************
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
* Name: nxtask_spawn_exec
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute the task from the file system.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* pidp - Upon successful completion, this will return the task ID of the
|
|
|
|
* child task in the variable pointed to by a non-NULL 'pid' argument.|
|
|
|
|
*
|
2018-12-18 14:29:01 +01:00
|
|
|
* name - The name to assign to the child task.
|
|
|
|
*
|
|
|
|
* entry - The child task's entry point (an address in memory)
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
2023-04-26 05:44:08 +02:00
|
|
|
* actions - The spawn file actions
|
|
|
|
*
|
2013-02-02 20:31:30 +01:00
|
|
|
* attr - If the value of the 'attr' parameter is NULL, the all default
|
|
|
|
* values for the POSIX spawn attributes will be used. Otherwise, the
|
|
|
|
* attributes will be set according to the spawn flags. The
|
|
|
|
* following spawn flags are supported:
|
|
|
|
*
|
|
|
|
* - POSIX_SPAWN_SETSCHEDPARAM: Set new tasks priority to the sched_param
|
|
|
|
* value.
|
|
|
|
* - POSIX_SPAWN_SETSCHEDULER: Set the new tasks scheduler priority to
|
|
|
|
* the sched_policy value.
|
|
|
|
*
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
* NOTE: POSIX_SPAWN_SETSIGMASK is handled in nxtask_spawn_proxy().
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
|
|
|
* argv - argv[] is the argument list for the new task. argv[] is an
|
|
|
|
* array of pointers to null-terminated strings. The list is terminated
|
|
|
|
* with a null pointer.
|
|
|
|
*
|
2022-10-15 18:37:58 +02:00
|
|
|
* envp - A pointer to an array of environment strings. Terminated with
|
|
|
|
* a NULL entry.
|
|
|
|
*
|
2013-02-02 20:31:30 +01:00
|
|
|
* Returned Value:
|
|
|
|
* This function will return zero on success. Otherwise, an error number
|
|
|
|
* will be returned as the function return value to indicate the error.
|
|
|
|
* This errno value may be that set by execv(), sched_setpolicy(), or
|
|
|
|
* sched_setparam().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name,
|
2023-04-26 05:44:08 +02:00
|
|
|
main_t entry,
|
|
|
|
FAR const posix_spawn_file_actions_t *actions,
|
|
|
|
FAR const posix_spawnattr_t *attr,
|
2022-10-15 18:37:58 +02:00
|
|
|
FAR char * const *argv, FAR char * const envp[])
|
2013-02-02 20:31:30 +01:00
|
|
|
{
|
2022-10-19 21:27:21 +02:00
|
|
|
FAR void *stackaddr = NULL;
|
2013-05-09 22:23:34 +02:00
|
|
|
size_t stacksize;
|
|
|
|
int priority;
|
2013-02-02 20:31:30 +01:00
|
|
|
int pid;
|
|
|
|
int ret = OK;
|
|
|
|
|
2020-05-02 12:36:48 +02:00
|
|
|
/* Use the default priority and stack size if no attributes are provided */
|
2013-05-09 22:23:34 +02:00
|
|
|
|
|
|
|
if (attr)
|
|
|
|
{
|
|
|
|
priority = attr->priority;
|
|
|
|
stacksize = attr->stacksize;
|
2022-10-19 21:27:21 +02:00
|
|
|
stackaddr = attr->stackaddr;
|
2013-05-09 22:23:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sched_param param;
|
|
|
|
|
|
|
|
/* Set the default priority to the same priority as this task */
|
|
|
|
|
2020-05-09 16:04:45 +02:00
|
|
|
ret = nxsched_get_param(0, ¶m);
|
2013-05-09 22:23:34 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-10-25 09:39:17 +02:00
|
|
|
return ret;
|
2013-05-09 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
priority = param.sched_priority;
|
2022-10-19 20:55:43 +02:00
|
|
|
stacksize = CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE;
|
2013-05-09 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
2013-02-02 20:31:30 +01:00
|
|
|
/* Start the task */
|
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
pid = nxtask_spawn_create(name, priority, stackaddr,
|
|
|
|
stacksize, entry, argv,
|
2023-10-25 09:39:17 +02:00
|
|
|
envp ? envp : environ, actions, attr);
|
2013-02-02 20:31:30 +01:00
|
|
|
if (pid < 0)
|
|
|
|
{
|
2021-05-12 05:34:50 +02:00
|
|
|
ret = pid;
|
2023-04-26 05:44:08 +02:00
|
|
|
serr("ERROR: nxtask_spawn_create failed: %d\n", ret);
|
2023-10-25 09:39:17 +02:00
|
|
|
return ret;
|
2013-02-02 20:31:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the task ID to the caller */
|
|
|
|
|
|
|
|
if (pid)
|
|
|
|
{
|
|
|
|
*pidp = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-05-12 05:34:50 +02:00
|
|
|
* Name: task_spawn
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The task_spawn() function will create a new, child task, where the
|
|
|
|
* entry point to the task is an address in memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* name - The name to assign to the child task.
|
|
|
|
*
|
|
|
|
* entry - The child task's entry point (an address in memory)
|
|
|
|
*
|
2014-04-13 22:32:20 +02:00
|
|
|
* file_actions - If 'file_actions' is a null pointer, then file
|
2013-02-02 20:31:30 +01:00
|
|
|
* descriptors open in the calling process will remain open in the
|
|
|
|
* child process (unless CONFIG_FDCLONE_STDIO is defined). If
|
|
|
|
* 'file_actions' is not NULL, then the file descriptors open in the
|
|
|
|
* child process will be those open in the calling process as modified
|
|
|
|
* by the spawn file actions object pointed to by file_actions.
|
|
|
|
*
|
|
|
|
* attr - If the value of the 'attr' parameter is NULL, the all default
|
|
|
|
* values for the POSIX spawn attributes will be used. Otherwise, the
|
|
|
|
* attributes will be set according to the spawn flags. The
|
2022-10-19 20:55:43 +02:00
|
|
|
* posix_spawnattr_t spawn attributes object type is defined in spawn.h.
|
2013-02-02 20:31:30 +01:00
|
|
|
* It will contains these attributes, not all of which are supported by
|
|
|
|
* NuttX:
|
|
|
|
*
|
|
|
|
* - POSIX_SPAWN_SETPGROUP: Setting of the new task's process group is
|
|
|
|
* not supported. NuttX does not support process groups.
|
|
|
|
* - POSIX_SPAWN_SETSCHEDPARAM: Set new tasks priority to the sched_param
|
|
|
|
* value.
|
|
|
|
* - POSIX_SPAWN_SETSCHEDULER: Set the new task's scheduler policy to
|
|
|
|
* the sched_policy value.
|
2020-03-08 13:43:21 +01:00
|
|
|
* - POSIX_SPAWN_RESETIDS: Resetting of the effective user ID of the
|
|
|
|
* child process is not supported. NuttX does not support effective
|
|
|
|
* user IDs.
|
2013-02-02 20:31:30 +01:00
|
|
|
* - POSIX_SPAWN_SETSIGMASK: Set the new task's signal mask.
|
|
|
|
* - POSIX_SPAWN_SETSIGDEF: Resetting signal default actions is not
|
|
|
|
* supported. NuttX does not support default signal actions.
|
|
|
|
*
|
|
|
|
* And the non-standard:
|
|
|
|
*
|
|
|
|
* - TASK_SPAWN_SETSTACKSIZE: Set the stack size for the new task.
|
|
|
|
*
|
|
|
|
* argv - argv[] is the argument list for the new task. argv[] is an
|
|
|
|
* array of pointers to null-terminated strings. The list is terminated
|
|
|
|
* with a null pointer.
|
|
|
|
*
|
2022-04-20 15:45:45 +02:00
|
|
|
* envp - envp[] is an array of character pointers to null-terminated
|
|
|
|
* strings that provide the environment for the new process image.
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-05-12 05:34:50 +02:00
|
|
|
* task_spawn() will return process ID of new task on success.
|
|
|
|
* Otherwise, a negative number will be returned as the function return
|
|
|
|
* value to indicate the error:
|
2013-02-02 20:31:30 +01:00
|
|
|
*
|
|
|
|
* - EINVAL: The value specified by 'file_actions' or 'attr' is invalid.
|
|
|
|
* - Any errors that might have been return if vfork() and excec[l|v]()
|
|
|
|
* had been called.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-12 05:34:50 +02:00
|
|
|
int task_spawn(FAR const char *name, main_t entry,
|
2017-08-15 01:19:27 +02:00
|
|
|
FAR const posix_spawn_file_actions_t *file_actions,
|
|
|
|
FAR const posix_spawnattr_t *attr,
|
2020-05-06 11:24:42 +02:00
|
|
|
FAR char * const argv[], FAR char * const envp[])
|
2013-02-02 20:31:30 +01:00
|
|
|
{
|
2021-06-07 14:21:19 +02:00
|
|
|
pid_t pid = INVALID_PROCESS_ID;
|
2013-02-02 20:31:30 +01:00
|
|
|
int ret;
|
|
|
|
|
2021-05-12 05:34:50 +02:00
|
|
|
sinfo("name=%s entry=%p file_actions=%p attr=%p argv=%p\n",
|
|
|
|
name, entry, file_actions, attr, argv);
|
2013-02-02 20:31:30 +01:00
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
ret = nxtask_spawn_exec(&pid, name, entry,
|
|
|
|
file_actions != NULL ? *file_actions : NULL,
|
|
|
|
attr, argv, envp);
|
2013-02-02 20:31:30 +01:00
|
|
|
|
2023-06-18 22:20:04 +02:00
|
|
|
return ret >= 0 ? pid : ret;
|
2013-02-02 20:31:30 +01:00
|
|
|
}
|
2014-09-14 16:22:21 +02:00
|
|
|
|
2014-09-16 23:37:05 +02:00
|
|
|
#endif /* CONFIG_BUILD_KERNEL */
|