[newlib-cygwin/cygwin-3_6-branch] Cygwin: spawn: apply PID_NEW_PG flag to spawned child

Jeremy Drake jeremyd2019@sourceware.org
Mon Jul 7 20:19:45 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=dfa09739d737ee26dbb40683dbf46865c99ff915

commit dfa09739d737ee26dbb40683dbf46865c99ff915
Author: Jeremy Drake <cygwin@jdrake.com>
Date:   Mon Jul 7 11:26:37 2025 -0700

    Cygwin: spawn: apply PID_NEW_PG flag to spawned child
    
    Previously, it was always applied to "myself", which is only appropriate
    in the case of _P_OVERLAY (aka exec).  Apply the flag to "myself" only
    in that case, and apply it to the new child instead in other cases.
    Also, clear the flag from "myself" in the case of a failed exec.
    
    Fixes: 8d8724ee1b5a ("Cygwin: pty: Fix Ctrl-C handling further for non-cygwin apps.")
    Signed-off-by: Jeremy Drake <cygwin@jdrake.com>

Diff:
---
 winsup/cygwin/spawn.cc | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index ef175e708..cb58b6eed 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -544,9 +544,6 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
 	c_flags |= CREATE_NEW_PROCESS_GROUP;
       refresh_cygheap ();
 
-      if (c_flags & CREATE_NEW_PROCESS_GROUP)
-	InterlockedOr ((LONG *) &myself->process_state, PID_NEW_PG);
-
       if (mode == _P_DETACH)
 	/* all set */;
       else if (mode != _P_OVERLAY || !my_wr_proc_pipe)
@@ -605,7 +602,12 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
       ::cygheap->user.deimpersonate ();
 
       if (!real_path.iscygexec () && mode == _P_OVERLAY)
-	InterlockedOr ((LONG *) &myself->process_state, PID_NOTCYGWIN);
+	{
+	  LONG pidflags = PID_NOTCYGWIN;
+	  if (c_flags & CREATE_NEW_PROCESS_GROUP)
+	    pidflags |= PID_NEW_PG;
+	  InterlockedOr ((LONG *) &myself->process_state, pidflags);
+	}
 
       cygpid = (mode != _P_OVERLAY) ? create_cygwin_pid () : myself->pid;
 
@@ -707,7 +709,8 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
 	      myself->sendsig = myself->exec_sendsig;
 	      myself->exec_sendsig = NULL;
 	    }
-	  InterlockedAnd ((LONG *) &myself->process_state, ~PID_NOTCYGWIN);
+	  InterlockedAnd ((LONG *) &myself->process_state,
+			  ~(PID_NOTCYGWIN|PID_NEW_PG));
 	  /* Reset handle inheritance to default when the execution of a'
 	     non-Cygwin process fails.  Only need to do this for _P_OVERLAY
 	     since the handle will be closed otherwise.  Don't need to do
@@ -769,7 +772,9 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
 	  myself->set_has_pgid_children ();
 	  ProtectHandle (pi.hThread);
 	  pinfo child (cygpid,
-		       PID_IN_USE | (real_path.iscygexec () ? 0 : PID_NOTCYGWIN));
+		       PID_IN_USE |
+		       (real_path.iscygexec () ? 0 : PID_NOTCYGWIN) |
+		       ((c_flags & CREATE_NEW_PROCESS_GROUP) ? PID_NEW_PG : 0));
 	  if (!child)
 	    {
 	      syscall_printf ("pinfo failed");


More information about the Cygwin-cvs mailing list