Skip to content

Commit

Permalink
arch/libc: Integrate vfork into fork, and vfork directly call up_fork
Browse files Browse the repository at this point in the history
Signed-off-by: liwenxiang1 <[email protected]>
  • Loading branch information
xianglyc committed Oct 9, 2024
1 parent 0647b49 commit f811550
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 84 deletions.
3 changes: 0 additions & 3 deletions libs/libc/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ endif()

if(CONFIG_ARCH_HAVE_FORK)
list(APPEND SRCS lib_fork.c)
if(CONFIG_SCHED_WAITPID)
list(APPEND SRCS lib_vfork.c)
endif()
endif()

target_sources(c PRIVATE ${SRCS})
3 changes: 0 additions & 3 deletions libs/libc/unistd/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ endif

ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
CSRCS += lib_fork.c
ifeq ($(CONFIG_SCHED_WAITPID),y)
CSRCS += lib_vfork.c
endif
endif

# Add the unistd directory to the build
Expand Down
65 changes: 65 additions & 0 deletions libs/libc/unistd/lib_fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <errno.h>
#include <debug.h>

#if defined(CONFIG_ARCH_HAVE_FORK)

Expand Down Expand Up @@ -170,4 +173,66 @@ pid_t fork(void)
return pid;
}

#if defined(CONFIG_SCHED_WAITPID)

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: vfork
*
* Description:
* The vfork() function is implemented based on fork() function, on
* vfork(), the parent task need to wait until the child task is performing
* exec or running finished.
*
* Returned Value:
* Upon successful completion, vfork() returns 0 to the child process and
* returns the process ID of the child process to the parent process.
* Otherwise, -1 is returned to the parent, no child process is created,
* and errno is set to indicate the error.
*
****************************************************************************/

pid_t vfork(void)
{
int status = 0;
int ret;
pid_t pid;

#ifdef CONFIG_PTHREAD_ATFORK
atfork_prepare();
#endif
pid = up_fork();

#ifdef CONFIG_PTHREAD_ATFORK
if (pid == 0)
{
atfork_child();
}
else
{
atfork_parent();
}
#endif

if (pid != 0)
{
/* we are in parent task, and we need to wait the child task
* until running finished or performing exec
*/

ret = waitpid(pid, &status, WNOWAIT);
if (ret < 0)
{
serr("ERROR: waitpid failed: %d\n", get_errno());
}
}

return pid;
}

#endif /* CONFIG_SCHED_WAITPID */

#endif /* CONFIG_ARCH_HAVE_FORK */
78 changes: 0 additions & 78 deletions libs/libc/unistd/lib_vfork.c

This file was deleted.

0 comments on commit f811550

Please sign in to comment.