From e2a18ad33912e03877f576908856fc0c896a1f9c Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 16 Oct 2022 00:37:58 +0800 Subject: [PATCH] sched: Support envp argument of task_spawn and nxtask_create Signed-off-by: Xiang Xiao --- drivers/serial/serial.c | 4 +-- include/nuttx/sched.h | 7 ++-- sched/init/nx_bringup.c | 74 ++++++++++++++++------------------------ sched/task/task_create.c | 27 +++++++++------ sched/task/task_spawn.c | 14 ++++---- 5 files changed, 61 insertions(+), 65 deletions(-) diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c index 61d7674c32..de3ed611f0 100644 --- a/drivers/serial/serial.c +++ b/drivers/serial/serial.c @@ -1604,8 +1604,8 @@ static void uart_launch_worker(void *arg) nxtask_create(CONFIG_TTY_LAUNCH_ENTRYNAME, CONFIG_TTY_LAUNCH_PRIORITY, CONFIG_TTY_LAUNCH_STACKSIZE, - (main_t)CONFIG_TTY_LAUNCH_ENTRYPOINT, - argv); + CONFIG_TTY_LAUNCH_ENTRYPOINT, + argv, NULL); #else posix_spawnattr_t attr; diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 2bd184adda..88680f71ae 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -944,6 +944,8 @@ void nxtask_uninit(FAR struct task_tcb_s *tcb); * 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. * * Returned Value: * 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 stack_size, main_t entry, FAR char * const argv[]); +int nxtask_create(FAR const char *name, + int priority, int stack_size, main_t entry, + FAR char * const argv[], FAR char * const envp[]); /**************************************************************************** * Name: nxtask_delete diff --git a/sched/init/nx_bringup.c b/sched/init/nx_bringup.c index 94353210e0..3aa08fa453 100644 --- a/sched/init/nx_bringup.c +++ b/sched/init/nx_bringup.c @@ -224,19 +224,34 @@ static inline void nx_workqueues(void) static inline void nx_start_application(void) { -#ifdef CONFIG_INIT_ARGS - FAR char *const argv[] = +#ifndef CONFIG_INIT_NONE + FAR char * const argv[] = { +# ifdef 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, }; -#else - FAR char *const *argv = NULL; #endif - int ret; + #ifdef CONFIG_INIT_FILE posix_spawnattr_t attr; #endif + int ret; #ifdef CONFIG_BOARD_LATE_INITIALIZE /* 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"); -#ifdef CONFIG_BUILD_PROTECTED +# ifdef CONFIG_BUILD_PROTECTED DEBUGASSERT(USERSPACE->us_entrypoint != NULL); ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY, CONFIG_INIT_STACKSIZE, - USERSPACE->us_entrypoint, argv); -#else + USERSPACE->us_entrypoint, argv, envp); +# else ret = nxtask_create(CONFIG_INIT_ENTRYNAME, CONFIG_INIT_PRIORITY, CONFIG_INIT_STACKSIZE, - (main_t)CONFIG_INIT_ENTRYPOINT, argv); -#endif + CONFIG_INIT_ENTRYPOINT, argv, envp); +# endif DEBUGASSERT(ret > 0); #elif defined(CONFIG_INIT_FILE) -#ifdef CONFIG_INIT_MOUNT +# ifdef CONFIG_INIT_MOUNT /* Mount the file system containing the init program. */ ret = nx_mount(CONFIG_INIT_MOUNT_SOURCE, CONFIG_INIT_MOUNT_TARGET, CONFIG_INIT_MOUNT_FSTYPE, CONFIG_INIT_MOUNT_FLAGS, CONFIG_INIT_MOUNT_DATA); DEBUGASSERT(ret >= 0); -#endif +# endif /* Start the application initialization program from a program in a * 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); attr.priority = CONFIG_INIT_PRIORITY; -#ifndef CONFIG_ARCH_ADDRENV +# ifndef CONFIG_BUILD_KERNEL attr.stacksize = CONFIG_INIT_STACKSIZE; -#endif - ret = exec_spawn(CONFIG_INIT_FILEPATH, argv, NULL, +# endif + ret = exec_spawn(CONFIG_INIT_FILEPATH, argv, envp, CONFIG_INIT_SYMTAB, CONFIG_INIT_NEXPORTS, &attr); DEBUGASSERT(ret >= 0); #endif @@ -404,27 +419,6 @@ static inline void nx_create_initthread(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. * This should always be the first thread started because it may have to * resolve page faults in other threads @@ -444,13 +438,5 @@ int nx_bringup(void) */ 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; } diff --git a/sched/task/task_create.c b/sched/task/task_create.c index 380f5459c0..11692184b0 100644 --- a/sched/task/task_create.c +++ b/sched/task/task_create.c @@ -60,6 +60,8 @@ * 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. * * Returned Value: * 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, - int priority, FAR void *stack_ptr, int stack_size, - main_t entry, FAR char * const argv[]) +static int nxthread_create(FAR const char *name, uint8_t ttype, int priority, + FAR void *stack_ptr, int stack_size, main_t entry, + FAR char * const argv[], FAR char * const envp[]) { FAR struct task_tcb_s *tcb; pid_t pid; @@ -91,8 +93,8 @@ static int nxthread_create(FAR const char *name, uint8_t ttype, /* Initialize the task */ - ret = nxtask_init(tcb, name, priority, stack_ptr, stack_size, entry, argv, - NULL); + ret = nxtask_init(tcb, name, priority, stack_ptr, stack_size, + entry, argv, envp); if (ret < OK) { 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 * 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. * * Returned Value: * 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 stack_size, main_t entry, FAR char * const argv[]) +int nxtask_create(FAR const char *name, + 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, - NULL, stack_size, entry, argv); + return nxthread_create(name, TCB_FLAG_TTYPE_TASK, priority, NULL, + 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 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) { 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[]) { return nxthread_create(name, TCB_FLAG_TTYPE_KERNEL, priority, - stack_ptr, stack_size, entry, argv); + stack_ptr, stack_size, entry, argv, NULL); } /**************************************************************************** diff --git a/sched/task/task_spawn.c b/sched/task/task_spawn.c index e7c0e39b29..1dff3c758e 100644 --- a/sched/task/task_spawn.c +++ b/sched/task/task_spawn.c @@ -77,6 +77,9 @@ * array of pointers to null-terminated strings. The list is terminated * with a null pointer. * + * envp - A pointer to an array of environment strings. Terminated with + * a NULL entry. + * * 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. @@ -87,7 +90,7 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name, main_t entry, FAR const posix_spawnattr_t *attr, - FAR char * const *argv) + FAR char * const *argv, FAR char * const envp[]) { size_t stacksize; int priority; @@ -126,7 +129,7 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const char *name, /* Start the task */ - pid = nxtask_create(name, priority, stacksize, entry, argv); + pid = nxtask_create(name, priority, stacksize, entry, argv, envp); if (pid < 0) { 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, 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 if (ret == OK) @@ -334,7 +337,7 @@ int task_spawn(FAR const char *name, main_t entry, if ((file_actions == NULL || *file_actions == NULL) && (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) { 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, CONFIG_POSIX_SPAWN_PROXY_STACKSIZE, - (main_t)nxtask_spawn_proxy, - (FAR char * const *)NULL); + nxtask_spawn_proxy, NULL, NULL); if (proxy < 0) { ret = proxy;