casaroli commented on code in PR #19562:
URL: https://github.com/apache/nuttx/pull/19562#discussion_r3743829643


##########
sched/task/task_fork.c:
##########
@@ -268,65 +453,127 @@ FAR struct tcb_s *nxtask_setup_fork(start_t retaddr)
  * Name: nxtask_start_fork
  *
  * Description:
- *   The fork() function has the same effect as fork(), except that the
- *   behavior is undefined if the process created by fork() either modifies
- *   any data other than a variable of type pid_t used to store the return
- *   value from fork(), or returns from the function in which fork() was
- *   called, or calls any other function before successfully calling _exit()
- *   or one of the exec family of functions.
- *
- *   This function provides one step in the overall fork() sequence:  It
- *   starts execution of the previously initialized TCB.  The overall
- *   sequence is:
- *
- *   1) User code calls fork()
- *   2) Architecture-specific code provides fork()and calls
- *      nxtask_setup_fork().
- *   3) nxtask_setup_fork() allocates and configures the child task's TCB.
- *      This consists of:
- *      - Allocation of the child task's TCB.
- *      - Initialization of file descriptors and streams
- *      - Configuration of environment variables
- *      - Allocate and initialize the stack
- *      - Setup the input parameters for the task.
- *      - Initialization of the TCB (including call to up_initial_state())
- *   4) fork() provides any additional operating context. fork must:
- *      - Initialize special values in any CPU registers that were not
- *        already configured by up_initial_state()
- *   5) fork() then calls nxtask_start_fork()
- *   6) nxtask_start_fork() then executes the child thread.
+ *   The last step of both primitives:  finish the child and run it.  The
+ *   architecture-specific code calls this once it has built the child's
+ *   register context and stack.
+ *
+ *   A vfork() additionally suspends the caller until the child calls _exit()
+ *   or one of the exec family of functions.  The suspension lives here, in
+ *   the kernel primitive, rather than in a libc waitpid() as it once did.
+ *   Two things follow from that.  The parent is released when the child's
+ *   TCB is torn down (see nxtask_resume_vfork()), which for an exec()ing
+ *   child is immediately after exec_swap() has handed the child's pid to the
+ *   program it loaded -- so the parent resumes at exec(), holding a pid that
+ *   names the running program, as POSIX requires.  And vfork() no longer
+ *   depends on CONFIG_SCHED_WAITPID.
  *
  * Input Parameters:
- *   child - The tcb_s struct instance that created by
- *           nxtask_setup_fork() method
- *   wait_child - whether need to wait until the child is running finished
+ *   child - The tcb_s struct instance created by nxtask_setup_fork()
+ *   vfork - true for vfork(), false for fork()
  *
  * Returned Value:
- *   Upon successful completion, fork() 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.
+ *   The process ID of the child, or ERROR on failure.
  *
  ****************************************************************************/
 
-pid_t nxtask_start_fork(FAR struct tcb_s *child)
+pid_t nxtask_start_fork(FAR struct tcb_s *child, bool vfork)
 {
+#ifdef CONFIG_ARCH_HAVE_VFORK
+  struct vfork_s rel;
+  int ret;
+#endif
   pid_t pid;
 
-  sinfo("Starting Child TCB=%p\n", child);
+  sinfo("Starting Child TCB=%p vfork=%d\n", child, vfork);
   DEBUGASSERT(child);
 
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_HAVE_FORK)
+  /* The architecture code has finished writing the child's image, so put the
+   * parent back in its own address environment.  See nxtask_setup_fork().
+   */
+
+  if (!vfork &&
+      (child->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
+    {
+      fork_restore_parent_env();
+    }
+#endif
+
   /* Get the assigned pid before we start the task */
 
   pid = child->pid;
 
+#ifdef CONFIG_ARCH_HAVE_VFORK
+  if (vfork)
+    {
+      /* The rendezvous lives in this frame.  We are about to block in it and
+       * will not leave until the child has posted, so it outlives every use.
+       */
+
+      nxsem_init(&rel.sem, 0, 0);
+      rel.released     = false;
+      child->vfork_rel = &rel;
+    }
+#endif
+
   /* Activate the task */
 
   nxtask_activate(child);
 
+#ifdef CONFIG_ARCH_HAVE_VFORK
+  if (vfork)
+    {
+      /* Wait for the child to _exit() or exec().  This is not a cancellation
+       * point and must not be interrupted by a signal:  the child may be
+       * running on our stack, so returning early would corrupt it.
+       */
+
+      do
+        {
+          ret = nxsem_wait_uninterruptible(&rel.sem);
+        }
+      while (ret == -EINTR);
+
+      nxsem_destroy(&rel.sem);
+    }
+#endif
+
   return pid;
 }
 
+#ifdef CONFIG_ARCH_HAVE_VFORK
+/****************************************************************************
+ * Name: nxtask_resume_vfork
+ *
+ * Description:
+ *   Release the vfork() parent suspended on this child, if there is one.
+ *
+ *   Called from nxsched_release_tcb(), the last point in the child's life,
+ *   by which time an exec()ing child has already handed its pid to the
+ *   program it loaded.  nxtask_abort_fork() reaches it too, so a fork that
+ *   fails after the rendezvous also releases the parent.
+ *
+ * Input Parameters:
+ *   child - The TCB being torn down
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void nxtask_resume_vfork(FAR struct tcb_s *child)
+{
+  FAR struct vfork_s *rel = child->vfork_rel;
+
+  if (rel != NULL && !rel->released)
+    {
+      rel->released    = true;
+      child->vfork_rel = NULL;

Review Comment:
   you are right. removed 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to