This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 2f26323388 arch/libc: Integrate vfork into fork, and vfork directly
call up_fork
2f26323388 is described below
commit 2f263233884182726283676d972c2b6922619dd3
Author: liwenxiang1 <[email protected]>
AuthorDate: Wed Oct 9 15:01:44 2024 +0800
arch/libc: Integrate vfork into fork, and vfork directly call up_fork
Signed-off-by: liwenxiang1 <[email protected]>
---
libs/libc/unistd/CMakeLists.txt | 3 --
libs/libc/unistd/Make.defs | 3 --
libs/libc/unistd/lib_fork.c | 65 ++++++++++++++++++++++++++++++++++
libs/libc/unistd/lib_vfork.c | 78 -----------------------------------------
4 files changed, 65 insertions(+), 84 deletions(-)
diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt
index 4771e780bd..5a62d8f19f 100644
--- a/libs/libc/unistd/CMakeLists.txt
+++ b/libs/libc/unistd/CMakeLists.txt
@@ -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})
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index bd8aa9f28e..965bf8d0ff 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -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
diff --git a/libs/libc/unistd/lib_fork.c b/libs/libc/unistd/lib_fork.c
index 5c947e334c..0e5ee9ca4c 100644
--- a/libs/libc/unistd/lib_fork.c
+++ b/libs/libc/unistd/lib_fork.c
@@ -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)
@@ -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 */
diff --git a/libs/libc/unistd/lib_vfork.c b/libs/libc/unistd/lib_vfork.c
deleted file mode 100644
index c5d9655a76..0000000000
--- a/libs/libc/unistd/lib_vfork.c
+++ /dev/null
@@ -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 */