public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Cygwin: set buffer size for pipes created by non-Cygwin processes
@ 2021-09-14 15:07 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2021-09-14 15:07 UTC (permalink / raw)
  To: cygwin-cvs

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

commit 99be238347bdd42db7bb57b9ba4b73d765d03cfe
Author: Ken Brown <kbrown@cornell.edu>
Date:   Tue Sep 7 17:40:21 2021 -0400

    Cygwin: set buffer size for pipes created by non-Cygwin processes
    
    Rename fhandler_pipe_and_fifo::max_atomic_write to pipe_buf_size.
    This reflect its actual meaning better.  The fhandler_pipe_and_fifo
    constructor initializes it to DEFAULT_PIPEBUFSIZE (== 64K), which is
    the buffer size for the windows pipes created by fhandler_pipe and
    fhandler_fifo.  But if we inherit a stdio pipe handle from a
    non-Cygwin process, the buffer size could be different.
    
    To remedy this, add a method fhandler_pipe::set_pipe_buf_size that
    queries the OS for the pipe buffer size, and use it in
    dtable::init_std_file_from_handle.

Diff:
---
 winsup/cygwin/dtable.cc        |  5 +++++
 winsup/cygwin/fhandler.h       |  3 ++-
 winsup/cygwin/fhandler_pipe.cc | 25 ++++++++++++++++++++-----
 3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index 8085e656e..a638a5995 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -406,6 +406,11 @@ dtable::init_std_file_from_handle (int fd, HANDLE handle)
 	}
       if (!fh->init (handle, access, bin))
 	api_fatal ("couldn't initialize fd %d for %s", fd, fh->get_name ());
+      if (fh->ispipe ())
+	{
+	  fhandler_pipe *fhp = (fhandler_pipe *) fh;
+	  fhp->set_pipe_buf_size ();
+	}
 
       fh->open_setup (openflags);
       fh->usecount = 0;
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index bb7eb09ce..7aed089eb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1172,7 +1172,7 @@ class fhandler_socket_unix : public fhandler_socket
 class fhandler_pipe_fifo: public fhandler_base
 {
  protected:
-  size_t max_atomic_write;
+  size_t pipe_buf_size;
 
  public:
   fhandler_pipe_fifo ();
@@ -1192,6 +1192,7 @@ public:
 
   bool ispipe() const { return true; }
   void set_read_mutex (HANDLE mtx) { read_mtx = mtx; }
+  void set_pipe_buf_size ();
 
   void set_popen_pid (pid_t pid) {popen_pid = pid;}
   pid_t get_popen_pid () const {return popen_pid;}
diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 4bd807a09..c75476040 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -29,7 +29,7 @@ STATUS_PIPE_EMPTY simply means there's no data to be read. */
 		   || _s == STATUS_PIPE_EMPTY; })
 
 fhandler_pipe_fifo::fhandler_pipe_fifo ()
-  : fhandler_base (), max_atomic_write (DEFAULT_PIPEBUFSIZE)
+  : fhandler_base (), pipe_buf_size (DEFAULT_PIPEBUFSIZE)
 {
 }
 
@@ -269,7 +269,7 @@ fhandler_pipe::raw_read (void *ptr, size_t& len)
 	     buffer size - 1. Pending read lowers WriteQuotaAvailable on
 	     the write side and thus affects select's ability to return
 	     more or less reliable info whether a write succeeds or not. */
-	  ULONG chunk = max_atomic_write - 1;
+	  ULONG chunk = pipe_buf_size - 1;
 	  status = NtQueryInformationFile (get_handle (), &io,
 					   &fpli, sizeof (fpli),
 					   FilePipeLocalInformation);
@@ -391,12 +391,12 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
   if (!len)
     return 0;
 
-  if (len <= max_atomic_write)
+  if (len <= pipe_buf_size)
     chunk = len;
   else if (is_nonblocking ())
-    chunk = len = max_atomic_write;
+    chunk = len = pipe_buf_size;
   else
-    chunk = max_atomic_write;
+    chunk = pipe_buf_size;
 
   /* Create a wait event if the pipe or fifo is in blocking mode. */
   if (!is_nonblocking () && !(evt = CreateEvent (NULL, false, false, NULL)))
@@ -894,6 +894,21 @@ nt_create (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE &r, HANDLE &w,
   return 0;
 }
 
+/* Called by dtable::init_std_file_from_handle for stdio handles
+   inherited from non-Cygwin processes. */
+void
+fhandler_pipe::set_pipe_buf_size ()
+{
+  NTSTATUS status;
+  IO_STATUS_BLOCK io;
+  FILE_PIPE_LOCAL_INFORMATION fpli;
+
+  status = NtQueryInformationFile (get_handle (), &io, &fpli, sizeof fpli,
+				   FilePipeLocalInformation);
+  if (NT_SUCCESS (status))
+    pipe_buf_size = fpli.InboundQuota;
+}
+
 int
 fhandler_pipe::ioctl (unsigned int cmd, void *p)
 {


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

only message in thread, other threads:[~2021-09-14 15:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-14 15:07 [newlib-cygwin] Cygwin: set buffer size for pipes created by non-Cygwin processes 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).