arch/libc: Integrate vfork into fork, and vfork directly call up_fork

Signed-off-by: liwenxiang1 <liwenxiang1@xiaomi.com>
This commit is contained in:
liwenxiang1 2024-10-09 15:01:44 +08:00 committed by Xiang Xiao
parent 67c9a7aabd
commit 2f26323388
4 changed files with 65 additions and 84 deletions

View file

@ -85,9 +85,6 @@ endif()
if(CONFIG_ARCH_HAVE_FORK) if(CONFIG_ARCH_HAVE_FORK)
list(APPEND SRCS lib_fork.c) list(APPEND SRCS lib_fork.c)
if(CONFIG_SCHED_WAITPID)
list(APPEND SRCS lib_vfork.c)
endif()
endif() endif()
target_sources(c PRIVATE ${SRCS}) target_sources(c PRIVATE ${SRCS})

View file

@ -53,9 +53,6 @@ endif
ifeq ($(CONFIG_ARCH_HAVE_FORK),y) ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
CSRCS += lib_fork.c CSRCS += lib_fork.c
ifeq ($(CONFIG_SCHED_WAITPID),y)
CSRCS += lib_vfork.c
endif
endif endif
# Add the unistd directory to the build # Add the unistd directory to the build

View file

@ -30,6 +30,9 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h>
#include <errno.h>
#include <debug.h>
#if defined(CONFIG_ARCH_HAVE_FORK) #if defined(CONFIG_ARCH_HAVE_FORK)
@ -170,4 +173,66 @@ pid_t fork(void)
return pid; 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 */ #endif /* CONFIG_ARCH_HAVE_FORK */

View file

@ -1,78 +0,0 @@
/****************************************************************************
* libs/libc/unistd/lib_vfork.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <debug.h>
#if defined(CONFIG_ARCH_HAVE_FORK) && 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 = fork();
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_ARCH_HAVE_FORK && CONFIG_SCHED_WAITPID */