Exec: Support run exec in current task

Fix the problem when vfork() calls execv() (or execl()) to start a new application:
When the parent thread calls vfork() it receives and gets the pid of the vforked task,
and not the pid of the desired execv'ed application.

issue #3334

Signed-off-by: yangyalei <yangyalei@xiaomi.com>
This commit is contained in:
yangyalei
2023-08-23 20:37:42 +08:00
committed by Xiang Xiao
parent 0d4b42255b
commit 670c245ff2
5 changed files with 154 additions and 10 deletions
+93 -1
View File
@@ -36,6 +36,7 @@
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/sched.h>
#include <sched/sched.h>
#include <nuttx/spawn.h>
#include <nuttx/binfmt/binfmt.h>
@@ -96,6 +97,91 @@ static void exec_ctors(FAR void *arg)
}
#endif
/****************************************************************************
* Name: exec_swap
*
* Description:
* swap the pid of tasks, and reverse parent-child relationship.
*
* Input Parameters:
* ptcb - parent task tcb.
* chtcb - child task tcb.
*
* Returned Value:
* none
*
****************************************************************************/
static void exec_swap(FAR struct tcb_s *ptcb, FAR struct tcb_s *chtcb)
{
int pndx;
int chndx;
pid_t pid;
irqstate_t flags;
#ifdef HAVE_GROUP_MEMBERS
FAR pid_t *tg_members;
#endif
#ifdef CONFIG_SCHED_HAVE_PARENT
# ifdef CONFIG_SCHED_CHILD_STATUS
FAR struct child_status_s *tg_children;
# else
uint16_t tg_nchildren;
# endif
#endif
DEBUGASSERT(ptcb);
DEBUGASSERT(chtcb);
flags = enter_critical_section();
pndx = PIDHASH(ptcb->pid);
chndx = PIDHASH(chtcb->pid);
DEBUGASSERT(g_pidhash[pndx]);
DEBUGASSERT(g_pidhash[chndx]);
/* Exchange g_pidhash index */
g_pidhash[pndx] = chtcb;
g_pidhash[chndx] = ptcb;
/* Exchange pid */
pid = chtcb->pid;
chtcb->pid = ptcb->pid;
ptcb->pid = pid;
/* Exchange group info. This will reverse parent-child relationship */
pid = chtcb->group->tg_pid;
chtcb->group->tg_pid = ptcb->group->tg_pid;
ptcb->group->tg_pid = pid;
pid = chtcb->group->tg_ppid;
chtcb->group->tg_ppid = ptcb->group->tg_ppid;
ptcb->group->tg_ppid = pid;
#ifdef HAVE_GROUP_MEMBERS
tg_members = chtcb->group->tg_members;
chtcb->group->tg_members = ptcb->group->tg_members;
ptcb->group->tg_members = tg_members;
#endif
#ifdef CONFIG_SCHED_HAVE_PARENT
# ifdef CONFIG_SCHED_CHILD_STATUS
tg_children = chtcb->group->tg_children;
chtcb->group->tg_children = ptcb->group->tg_children;
ptcb->group->tg_children = tg_children;
# else
tg_nchildren = chtcb->group->tg_nchildren;
chtcb->group->tg_nchildren = ptcb->group->tg_nchildren;
ptcb->group->tg_nchildren = tg_nchildren;
# endif
#endif
leave_critical_section(flags);
}
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -116,7 +202,8 @@ static void exec_ctors(FAR void *arg)
int exec_module(FAR struct binary_s *binp,
FAR const char *filename, FAR char * const *argv,
FAR char * const *envp,
FAR const posix_spawn_file_actions_t *actions)
FAR const posix_spawn_file_actions_t *actions,
bool spawn)
{
FAR struct task_tcb_s *tcb;
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
@@ -292,6 +379,11 @@ int exec_module(FAR struct binary_s *binp,
}
#endif
if (!spawn)
{
exec_swap(this_task(), (FAR struct tcb_s *)tcb);
}
/* Then activate the task at the provided priority */
nxtask_activate((FAR struct tcb_s *)tcb);