public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Cygwin: FIFO: fix clone
@ 2019-04-16 11:16 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2019-04-16 11:16 UTC (permalink / raw)
  To: cygwin-cvs

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

commit 7b28776d3fe6909abb10273af2a3bfdca6351292
Author: Ken Brown <kbrown@cornell.edu>
Date:   Sun Apr 14 19:16:02 2019 +0000

    Cygwin: FIFO: fix clone
    
    After copyto is called, make the new fhandler's pipe_name point to the
    new fhandler's pipe_name_buf, which is a *copy* of the old fhandler's
    pipe_name_buf.  Previously, get_pipe_name would return the wrong
    result after a clone/dup, causing create_pipe_instance and open_pipe
    to fail.
    
    Also, stop the listen_client thread when cloning.  Otherwise the
    thread can keep accepting connections that the cloned fhandler won't
    know about.
    
    Do this via a new method fhandler_fifo::stop_listen_client, extracted
    from fhandler_fifo::close.

Diff:
---
 winsup/cygwin/fhandler.h       |  6 ++++++
 winsup/cygwin/fhandler_fifo.cc | 20 +++++++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 1e26c65..aea02c2 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1280,6 +1280,7 @@ class fhandler_fifo: public fhandler_base
   int disconnect_and_reconnect (int);
   int add_client_handler ();
   bool listen_client ();
+  int stop_listen_client ();
 public:
   fhandler_fifo ();
   bool hit_eof ();
@@ -1326,7 +1327,12 @@ public:
   {
     void *ptr = (void *) ccalloc (malloc_type, 1, sizeof (fhandler_fifo));
     fhandler_fifo *fhf = new (ptr) fhandler_fifo (ptr);
+    /* We don't want our client list to change any more. */
+    stop_listen_client ();
     copyto (fhf);
+    /* fhf->pipe_name_buf is a *copy* of this->pipe_name_buf, but
+       fhf->pipe_name.Buffer == this->pipe_name_buf. */
+    fhf->pipe_name.Buffer = fhf->pipe_name_buf;
     for (int i = 0; i < nhandlers; i++)
       fhf->fc_handler[i].fh = fc_handler[i].fh->clone ();
     return fhf;
diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc
index d4d2b38..a3ecbeb 100644
--- a/winsup/cygwin/fhandler_fifo.cc
+++ b/winsup/cygwin/fhandler_fifo.cc
@@ -812,9 +812,9 @@ fifo_client_handler::close ()
 }
 
 int
-fhandler_fifo::close ()
+fhandler_fifo::stop_listen_client ()
 {
-  int res = 0;
+  int ret = 0;
   HANDLE evt = InterlockedExchangePointer (&lct_termination_evt, NULL);
   HANDLE thr = InterlockedExchangePointer (&listen_client_thr, NULL);
   if (thr)
@@ -825,19 +825,29 @@ fhandler_fifo::close ()
       DWORD err;
       GetExitCodeThread (thr, &err);
       if (err)
-	debug_printf ("listen_client_thread exited with code %d", err);
+	{
+	  ret = -1;
+	  debug_printf ("listen_client_thread exited with error, %E");
+	}
       CloseHandle (thr);
     }
   if (evt)
     CloseHandle (evt);
+  return ret;
+}
+
+int
+fhandler_fifo::close ()
+{
+  int ret = stop_listen_client ();
   if (read_ready)
     CloseHandle (read_ready);
   if (write_ready)
     CloseHandle (write_ready);
   for (int i = 0; i < nhandlers; i++)
     if (fc_handler[i].close () < 0)
-      res = -1;
-  return fhandler_base::close () || res;
+      ret = -1;
+  return fhandler_base::close () || ret;
 }
 
 int


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-04-16 11:16 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-16 11:16 [newlib-cygwin] Cygwin: FIFO: fix clone Corinna Vinschen

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).