public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: Michael Haubenwallner <michael.haubenwallner@ssi-schaefer.com>
Cc: cygwin@cygwin.com
Subject: Re: [ANNOUNCEMENT] TEST: Cygwin 3.0.0-0.7
Date: Fri, 08 Feb 2019 13:28:00 -0000	[thread overview]
Message-ID: <20190208132807.GP13951@calimero.vinschen.de> (raw)
In-Reply-To: <20190208130635.GO13951@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 4918 bytes --]

On Feb  8 14:06, Corinna Vinschen wrote:
> On Feb  8 13:52, Michael Haubenwallner wrote:
> > On 2/8/19 1:23 PM, Corinna Vinschen wrote:
> > > On Feb  8 13:21, Corinna Vinschen wrote:
> > >> On Feb  8 12:51, Michael Haubenwallner wrote:
> > >>>
> > >>> For now it seems like there's an inconsistency with PIDs:
> > >>> A first process PID 100, receives PID 101 from spawn(),
> > >>> but in the new process getpid() returns 102:
> > >>>
> > >>> $ ./dospawn /bin/bash -c 'echo $$'
> > >>> 12625
> > >>> waitpid: pid 12624 status 0x0
> > >>
> > >> Oh, hmm.  If you call spawnve, rather than execve, a new child pid
> > >> is generated in spawnve, rather than just keeping the callers pid.
> > >>
> > >> However, apparently the child invents its own pid in pinfo::thisproc
> > >> after being spawned.  But actually this should only occur for forked
> > >> processes aore processes started from non-Cygwin parents.
> > > 
> > > Does that help, by any chance:
> > > [nope]
> > 
> > How should the child be informed at all about the new cygpid value generated in
> > parent's child_info_spawn::worker() ?
> 
> I just realized this myself.  The old method creating Cygwin pids just
> fetched the pid from GetCurrentProcessId(), which was right for spawned
> (but not execed) processes.  For the new pid we might have to give this
> to the child via child_info_spawn.

This works for me, can you test this, too, please?

diff --git a/winsup/cygwin/child_info.h b/winsup/cygwin/child_info.h
index 67264119eae9..847614ff299a 100644
--- a/winsup/cygwin/child_info.h
+++ b/winsup/cygwin/child_info.h
@@ -144,6 +144,7 @@ class child_info_spawn: public child_info
 {
   HANDLE hExeced;
   HANDLE ev;
+  pid_t cygpid;
 public:
   cygheap_exec_info *moreinfo;
   int __stdin;
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 78506d43de29..11edcdf0d21b 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -652,11 +652,16 @@ child_info_spawn::handle_spawn ()
 	cygheap_fixup_in_child (true);
 	memory_init ();
       }
+
+  cygheap->pid = cygpid;
+
+  /* Spawned process sets h to INVALID_HANDLE_VALUE to notify
+     pinfo::thisproc not to create another pid. */
   if (!moreinfo->myself_pinfo ||
       !DuplicateHandle (GetCurrentProcess (), moreinfo->myself_pinfo,
 			GetCurrentProcess (), &h, 0,
 			FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
-    h = NULL;
+    h = (type == _CH_SPAWN) ? INVALID_HANDLE_VALUE : NULL;
 
   /* Setup our write end of the process pipe.  Clear the one in the structure.
      The destructor should never be called for this but, it can't hurt to be
diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
index 445bd35b224e..61fbc9bc19bf 100644
--- a/winsup/cygwin/pinfo.cc
+++ b/winsup/cygwin/pinfo.cc
@@ -57,11 +57,15 @@ pinfo::thisproc (HANDLE h)
   procinfo = NULL;
 
   DWORD flags = PID_IN_USE | PID_ACTIVE;
+  /* Forked process or process started from non-Cygwin parent needs a pid. */
   if (!h)
     {
       cygheap->pid = create_cygwin_pid ();
       flags |= PID_NEW;
     }
+  /* spawnve'd process got pid from parent. */
+  else if (h == INVALID_HANDLE_VALUE)
+    h = NULL;
 
   init (cygheap->pid, flags, h);
   procinfo->process_state |= PID_IN_USE;
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index d969c66ea19a..ebc34d10542f 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -258,7 +258,6 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
 			  int in__stdin, int in__stdout)
 {
   bool rc;
-  pid_t cygpid;
   int res = -1;
 
   /* Check if we have been called from exec{lv}p or spawn{lv}p and mask
@@ -578,6 +577,8 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
       if (!real_path.iscygexec () && mode == _P_OVERLAY)
 	myself->process_state |= PID_NOTCYGWIN;
 
+      cygpid = (mode != _P_OVERLAY) ? create_cygwin_pid () : myself->pid;
+
       wchar_t wcmd[(size_t) cmd];
       if (!::cygheap->user.issetuid ()
 	  || (::cygheap->user.saved_uid == ::cygheap->user.real_uid
@@ -708,11 +709,6 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
       if (::cygheap->fdtab.need_fixup_before ())
 	::cygheap->fdtab.fixup_before_exec (pi.dwProcessId);
 
-      if (mode != _P_OVERLAY)
-	cygpid = create_cygwin_pid ();
-      else
-	cygpid = myself->pid;
-
       /* Print the original program name here so the user can see that too.  */
       syscall_printf ("pid %d, prog_arg %s, cmd line %.9500s)",
 		      rc ? cygpid : (unsigned int) -1, prog_arg, (const char *) cmd);


I pushed your forkable branch to master, btw.  Would you mind to do the
honors in the ;rease notes at cygwin/release/3.0 and doc/new-features.xml?


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2019-02-08 13:28 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-05 15:24 Corinna Vinschen
2019-02-07 16:14 ` Michael Haubenwallner
2019-02-07 18:27   ` Corinna Vinschen
2019-02-08  6:46     ` Michael Haubenwallner
2019-02-08 11:32       ` Corinna Vinschen
2019-02-08 11:51         ` Michael Haubenwallner
2019-02-08 12:21           ` Corinna Vinschen
2019-02-08 12:23             ` Corinna Vinschen
2019-02-08 12:52               ` Michael Haubenwallner
2019-02-08 13:06                 ` Corinna Vinschen
2019-02-08 13:28                   ` Corinna Vinschen [this message]
2019-02-08 14:43                     ` Michael Haubenwallner
2019-02-08 14:49                       ` Corinna Vinschen
2019-02-08 16:18                         ` Michael Haubenwallner
2019-02-08 16:50                           ` Corinna Vinschen
2019-02-08 16:35                     ` Michael Haubenwallner
2019-02-08 16:58                       ` Corinna Vinschen
2019-02-08 17:00                         ` Corinna Vinschen
2019-02-11 11:50                           ` Michael Haubenwallner
2019-02-11 13:33                             ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190208132807.GP13951@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=cygwin@cygwin.com \
    --cc=michael.haubenwallner@ssi-schaefer.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).