sched: Support envp argument of task_spawn and nxtask_create

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-10-16 00:37:58 +08:00 committed by Masayuki Ishikawa
parent 9f4bb7da97
commit e2a18ad339
5 changed files with 61 additions and 65 deletions

View File

@ -1604,8 +1604,8 @@ static void uart_launch_worker(void *arg)
nxtask_create(CONFIG_TTY_LAUNCH_ENTRYNAME, nxtask_create(CONFIG_TTY_LAUNCH_ENTRYNAME,
CONFIG_TTY_LAUNCH_PRIORITY, CONFIG_TTY_LAUNCH_PRIORITY,
CONFIG_TTY_LAUNCH_STACKSIZE, CONFIG_TTY_LAUNCH_STACKSIZE,
(main_t)CONFIG_TTY_LAUNCH_ENTRYPOINT, CONFIG_TTY_LAUNCH_ENTRYPOINT,
argv); argv, NULL);
#else #else
posix_spawnattr_t attr; posix_spawnattr_t attr;

View File

@ -944,6 +944,8 @@ void nxtask_uninit(FAR struct task_tcb_s *tcb);
* arg - A pointer to an array of input parameters. The array * arg - A pointer to an array of input parameters. The array
* should be terminated with a NULL argv[] value. If no * should be terminated with a NULL argv[] value. If no
* parameters are required, argv may be NULL. * parameters are required, argv may be NULL.
* envp - A pointer to an array of environment strings. Terminated
* with a NULL entry.
* *
* Returned Value: * Returned Value:
* Returns the positive, non-zero process ID of the new task or a negated * Returns the positive, non-zero process ID of the new task or a negated
@ -952,8 +954,9 @@ void nxtask_uninit(FAR struct task_tcb_s *tcb);
* *
****************************************************************************/ ****************************************************************************/
int nxtask_create(FAR const char *name, int priority, int nxtask_create(FAR const char *name,
int stack_size, main_t entry, FAR char * const argv[]); int priority, int stack_size, main_t entry,
FAR char * const argv[], FAR char * const envp[]);
/**************************************************************************** /****************************************************************************
* Name: nxtask_delete * Name: nxtask_delete

View File

@ -224,19 +224,34 @@ static inline void nx_workqueues(void)
static inline void nx_start_application(void) static inline void nx_start_application(void)
{ {
#ifdef CONFIG_INIT_ARGS #ifndef CONFIG_INIT_NONE
FAR char *const argv[] = FAR char * const argv[] =
{ {
# ifdef CONFIG_INIT_ARGS
CONFIG_INIT_ARGS, CONFIG_INIT_ARGS,
# endif
NULL,
};
FAR char * const envp[] =
{
# ifdef CONFIG_LIBC_HOMEDIR
"PWD=" CONFIG_LIBC_HOMEDIR,
# endif
# ifdef CONFIG_PATH_INITIAL
"PATH=" CONFIG_PATH_INITIAL,
# endif
# ifdef CONFIG_LDPATH_INITIAL
"LD_LIBRARY_PATH=" CONFIG_LDPATH_INITIAL,
# endif
NULL, NULL,
}; };
#else
FAR char *const *argv = NULL;
#endif #endif
int ret;
#ifdef CONFIG_INIT_FILE #ifdef CONFIG_INIT_FILE
posix_spawnattr_t attr; posix_spawnattr_t attr;
#endif #endif
int ret;
#ifdef CONFIG_BOARD_LATE_INITIALIZE #ifdef CONFIG_BOARD_LATE_INITIALIZE
/* Perform any last-minute, board-specific initialization, if so /* Perform any last-minute, board-specific initialization, if so
@ -256,28 +271,28 @@ static inline void nx_start_application(void)
sinfo("Starting init thread\n"); sinfo("Starting init thread\n");
#ifdef CONFIG_BUILD_PROTECTED # ifdef CONFIG_BUILD_PROTECTED
DEBUGASSERT(USERSPACE->us_entrypoint != NULL); DEBUGASSERT(USERSPACE->us_entrypoint != NULL);
ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY, ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY,
CONFIG_INIT_STACKSIZE, CONFIG_INIT_STACKSIZE,
USERSPACE->us_entrypoint, argv); USERSPACE->us_entrypoint, argv, envp);
#else # else
ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY, ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY,
CONFIG_INIT_STACKSIZE, CONFIG_INIT_STACKSIZE,
(main_t)CONFIG_INIT_ENTRYPOINT, argv); CONFIG_INIT_ENTRYPOINT, argv, envp);
#endif # endif
DEBUGASSERT(ret > 0); DEBUGASSERT(ret > 0);
#elif defined(CONFIG_INIT_FILE) #elif defined(CONFIG_INIT_FILE)
#ifdef CONFIG_INIT_MOUNT # ifdef CONFIG_INIT_MOUNT
/* Mount the file system containing the init program. */ /* Mount the file system containing the init program. */
ret = nx_mount(CONFIG_INIT_MOUNT_SOURCE, CONFIG_INIT_MOUNT_TARGET, ret = nx_mount(CONFIG_INIT_MOUNT_SOURCE, CONFIG_INIT_MOUNT_TARGET,
CONFIG_INIT_MOUNT_FSTYPE, CONFIG_INIT_MOUNT_FLAGS, CONFIG_INIT_MOUNT_FSTYPE, CONFIG_INIT_MOUNT_FLAGS,
CONFIG_INIT_MOUNT_DATA); CONFIG_INIT_MOUNT_DATA);
DEBUGASSERT(ret >= 0); DEBUGASSERT(ret >= 0);
#endif # endif
/* Start the application initialization program from a program in a /* Start the application initialization program from a program in a
* mounted file system. Presumably the file system was mounted as part * mounted file system. Presumably the file system was mounted as part
@ -289,10 +304,10 @@ static inline void nx_start_application(void)
posix_spawnattr_init(&attr); posix_spawnattr_init(&attr);
attr.priority = CONFIG_INIT_PRIORITY; attr.priority = CONFIG_INIT_PRIORITY;
#ifndef CONFIG_ARCH_ADDRENV # ifndef CONFIG_BUILD_KERNEL
attr.stacksize = CONFIG_INIT_STACKSIZE; attr.stacksize = CONFIG_INIT_STACKSIZE;
#endif # endif
ret = exec_spawn(CONFIG_INIT_FILEPATH, argv, NULL, ret = exec_spawn(CONFIG_INIT_FILEPATH, argv, envp,
CONFIG_INIT_SYMTAB, CONFIG_INIT_NEXPORTS, &attr); CONFIG_INIT_SYMTAB, CONFIG_INIT_NEXPORTS, &attr);
DEBUGASSERT(ret >= 0); DEBUGASSERT(ret >= 0);
#endif #endif
@ -404,27 +419,6 @@ static inline void nx_create_initthread(void)
int nx_bringup(void) int nx_bringup(void)
{ {
#ifndef CONFIG_DISABLE_ENVIRON
/* Setup up the initial environment for the idle task. At present, this
* may consist of only the initial PATH variable and/or and init library
* path variable. These path variables are not used by the IDLE task.
* However, the environment containing the PATH variable will be inherited
* by all of the threads created by the IDLE task.
*/
#ifdef CONFIG_LIBC_HOMEDIR
setenv("PWD", CONFIG_LIBC_HOMEDIR, 1);
#endif
#ifdef CONFIG_PATH_INITIAL
setenv("PATH", CONFIG_PATH_INITIAL, 1);
#endif
#ifdef CONFIG_LDPATH_INITIAL
setenv("LD_LIBRARY_PATH", CONFIG_LDPATH_INITIAL, 1);
#endif
#endif
/* Start the page fill worker kernel thread that will resolve page faults. /* Start the page fill worker kernel thread that will resolve page faults.
* This should always be the first thread started because it may have to * This should always be the first thread started because it may have to
* resolve page faults in other threads * resolve page faults in other threads
@ -444,13 +438,5 @@ int nx_bringup(void)
*/ */
nx_create_initthread(); nx_create_initthread();
#if !defined(CONFIG_DISABLE_ENVIRON) && (defined(CONFIG_PATH_INITIAL) || \
defined(CONFIG_LDPATH_INITIAL))
/* We an save a few bytes by discarding the IDLE thread's environment. */
clearenv();
#endif
return OK; return OK;
} }

View File

@ -60,6 +60,8 @@
* arg - A pointer to an array of input parameters. The array * arg - A pointer to an array of input parameters. The array
* should be terminated with a NULL argv[] value. If no * should be terminated with a NULL argv[] value. If no
* parameters are required, argv may be NULL. * parameters are required, argv may be NULL.
* envp - A pointer to an array of environment strings. Terminated
* with a NULL entry.
* *
* Returned Value: * Returned Value:
* Returns the positive, non-zero process ID of the new task or a negated * Returns the positive, non-zero process ID of the new task or a negated
@ -68,9 +70,9 @@
* *
****************************************************************************/ ****************************************************************************/
static int nxthread_create(FAR const char *name, uint8_t ttype, static int nxthread_create(FAR const char *name, uint8_t ttype, int priority,
int priority, FAR void *stack_ptr, int stack_size, FAR void *stack_ptr, int stack_size, main_t entry,
main_t entry, FAR char * const argv[]) FAR char * const argv[], FAR char * const envp[])
{ {
FAR struct task_tcb_s *tcb; FAR struct task_tcb_s *tcb;
pid_t pid; pid_t pid;
@ -91,8 +93,8 @@ static int nxthread_create(FAR const char *name, uint8_t ttype,
/* Initialize the task */ /* Initialize the task */
ret = nxtask_init(tcb, name, priority, stack_ptr, stack_size, entry, argv, ret = nxtask_init(tcb, name, priority, stack_ptr, stack_size,
NULL); entry, argv, envp);
if (ret < OK) if (ret < OK)
{ {
kmm_free(tcb); kmm_free(tcb);
@ -144,6 +146,8 @@ static int nxthread_create(FAR const char *name, uint8_t ttype,
* arg - A pointer to an array of input parameters. The array * arg - A pointer to an array of input parameters. The array
* should be terminated with a NULL argv[] value. If no * should be terminated with a NULL argv[] value. If no
* parameters are required, argv may be NULL. * parameters are required, argv may be NULL.
* envp - A pointer to an array of environment strings. Terminated
* with a NULL entry.
* *
* Returned Value: * Returned Value:
* Returns the positive, non-zero process ID of the new task or a negated * Returns the positive, non-zero process ID of the new task or a negated
@ -152,11 +156,12 @@ static int nxthread_create(FAR const char *name, uint8_t ttype,
* *
****************************************************************************/ ****************************************************************************/
int nxtask_create(FAR const char *name, int priority, int nxtask_create(FAR const char *name,
int stack_size, main_t entry, FAR char * const argv[]) int priority, int stack_size, main_t entry,
FAR char * const argv[], FAR char * const envp[])
{ {
return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, NULL,
NULL, stack_size, entry, argv); stack_size, entry, argv, envp ? envp : environ);
} }
/**************************************************************************** /****************************************************************************
@ -195,7 +200,7 @@ int nxtask_create(FAR const char *name, int priority,
int task_create(FAR const char *name, int priority, int task_create(FAR const char *name, int priority,
int stack_size, main_t entry, FAR char * const argv[]) int stack_size, main_t entry, FAR char * const argv[])
{ {
int ret = nxtask_create(name, priority, stack_size, entry, argv); int ret = nxtask_create(name, priority, stack_size, entry, argv, NULL);
if (ret < 0) if (ret < 0)
{ {
set_errno(-ret); set_errno(-ret);
@ -236,7 +241,7 @@ int kthread_create_with_stack(FAR const char *name, int priority,
main_t entry, FAR char * const argv[]) main_t entry, FAR char * const argv[])
{ {
return nxthread_create(name, TCB_FLAG_TTYPE_KERNEL, priority, return nxthread_create(name, TCB_FLAG_TTYPE_KERNEL, priority,
stack_ptr, stack_size, entry, argv); stack_ptr, stack_size, entry, argv, NULL);
} }
/**************************************************************************** /****************************************************************************

View File

@ -77,6 +77,9 @@
* array of pointers to null-terminated strings. The list is terminated * array of pointers to null-terminated strings. The list is terminated
* with a null pointer. * with a null pointer.
* *
* envp - A pointer to an array of environment strings. Terminated with
* a NULL entry.
*
* Returned Value: * Returned Value:
* This function will return zero on success. Otherwise, an error number * This function will return zero on success. Otherwise, an error number
* will be returned as the function return value to indicate the error. * will be returned as the function return value to indicate the error.
@ -87,7 +90,7 @@
static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name, static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name,
main_t entry, FAR const posix_spawnattr_t *attr, main_t entry, FAR const posix_spawnattr_t *attr,
FAR char * const *argv) FAR char * const *argv, FAR char * const envp[])
{ {
size_t stacksize; size_t stacksize;
int priority; int priority;
@ -126,7 +129,7 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name,
/* Start the task */ /* Start the task */
pid = nxtask_create(name, priority, stacksize, entry, argv); pid = nxtask_create(name, priority, stacksize, entry, argv, envp);
if (pid < 0) if (pid < 0)
{ {
ret = pid; ret = pid;
@ -215,7 +218,7 @@ static int nxtask_spawn_proxy(int argc, FAR char *argv[])
ret = nxtask_spawn_exec(g_spawn_parms.pid, g_spawn_parms.u.task.name, ret = nxtask_spawn_exec(g_spawn_parms.pid, g_spawn_parms.u.task.name,
g_spawn_parms.u.task.entry, g_spawn_parms.attr, g_spawn_parms.u.task.entry, g_spawn_parms.attr,
g_spawn_parms.argv); g_spawn_parms.argv, g_spawn_parms.envp);
#ifdef CONFIG_SCHED_HAVE_PARENT #ifdef CONFIG_SCHED_HAVE_PARENT
if (ret == OK) if (ret == OK)
@ -334,7 +337,7 @@ int task_spawn(FAR const char *name, main_t entry,
if ((file_actions == NULL || *file_actions == NULL) && if ((file_actions == NULL || *file_actions == NULL) &&
(attr == NULL || (attr->flags & POSIX_SPAWN_SETSIGMASK) == 0)) (attr == NULL || (attr->flags & POSIX_SPAWN_SETSIGMASK) == 0))
{ {
ret = nxtask_spawn_exec(&pid, name, entry, attr, argv); ret = nxtask_spawn_exec(&pid, name, entry, attr, argv, envp);
if (ret < 0) if (ret < 0)
{ {
return ret; return ret;
@ -403,8 +406,7 @@ int task_spawn(FAR const char *name, main_t entry,
proxy = nxtask_create("nxtask_spawn_proxy", param.sched_priority, proxy = nxtask_create("nxtask_spawn_proxy", param.sched_priority,
CONFIG_POSIX_SPAWN_PROXY_STACKSIZE, CONFIG_POSIX_SPAWN_PROXY_STACKSIZE,
(main_t)nxtask_spawn_proxy, nxtask_spawn_proxy, NULL, NULL);
(FAR char * const *)NULL);
if (proxy < 0) if (proxy < 0)
{ {
ret = proxy; ret = proxy;