[newlib-cygwin] Cygwin: fhandler_pipe: control blocking mode of the Windows pipe

Corinna Vinschen corinna@sourceware.org
Tue Sep 14 15:04:48 GMT 2021


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

commit d0ad52aa6eb1a47ebb76a11118b19aa70e2671f0
Author: Ken Brown <kbrown@cornell.edu>
Date:   Sat May 25 13:05:49 2019 -0400

    Cygwin: fhandler_pipe: control blocking mode of the Windows pipe
    
    Add methods 'set_pipe_non_blocking' and 'fcntl' to keep the blocking
    mode of the Windows pipe in sync with that of the fhandler_pipe
    object.  This applies to pipes created with the 'pipe' and 'pipe2'
    system calls.

Diff:
---
 winsup/cygwin/fhandler.h       |  2 ++
 winsup/cygwin/fhandler_pipe.cc | 34 ++++++++++++++++++++++++++++++++--
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 12618db58..5873bb6be 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1243,6 +1243,7 @@ class fhandler_pipe: public fhandler_base
 private:
   pid_t popen_pid;
   size_t max_atomic_write;
+  void set_pipe_non_blocking (bool nonblocking);
 public:
   fhandler_pipe ();
 
@@ -1260,6 +1261,7 @@ public:
   void __reg3 raw_read (void *ptr, size_t& len);
   ssize_t __reg3 raw_write (const void *ptr, size_t len);
   int ioctl (unsigned int cmd, void *);
+  int fcntl (int cmd, intptr_t);
   int __reg2 fstat (struct stat *buf);
   int __reg2 fstatvfs (struct statvfs *buf);
   int __reg3 fadvise (off_t, off_t, int);
diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 61ab29187..75435b009 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -35,6 +35,23 @@ fhandler_pipe::fhandler_pipe ()
   need_fork_fixup (true);
 }
 
+/* This also sets the pipe's read mode to byte_stream unconditionally. */
+void
+fhandler_pipe::set_pipe_non_blocking (bool nonblocking)
+{
+  NTSTATUS status;
+  IO_STATUS_BLOCK io;
+  FILE_PIPE_INFORMATION fpi;
+
+  fpi.ReadMode = FILE_PIPE_BYTE_STREAM_MODE;
+  fpi.CompletionMode = nonblocking ? FILE_PIPE_COMPLETE_OPERATION
+    : FILE_PIPE_QUEUE_OPERATION;
+  status = NtSetInformationFile (get_handle (), &io, &fpi, sizeof fpi,
+				 FilePipeInformation);
+  if (!NT_SUCCESS (status))
+    debug_printf ("NtSetInformationFile(FilePipeInformation): %y", status);
+}
+
 int
 fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id)
 {
@@ -62,8 +79,7 @@ fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id)
   set_ino (uniq_id);
   set_unique_id (uniq_id | !!(mode & GENERIC_WRITE));
   if (opened_properly)
-    /* ... */
-    ;
+    set_pipe_non_blocking (is_nonblocking ());
   return 1;
 }
 
@@ -612,6 +628,20 @@ fhandler_pipe::ioctl (unsigned int cmd, void *p)
   return 0;
 }
 
+int
+fhandler_pipe::fcntl (int cmd, intptr_t arg)
+{
+  if (cmd != F_SETFL)
+    return fhandler_base::fcntl (cmd, arg);
+
+  const bool was_nonblocking = is_nonblocking ();
+  int res = fhandler_base::fcntl (cmd, arg);
+  const bool now_nonblocking = is_nonblocking ();
+  if (now_nonblocking != was_nonblocking)
+    set_pipe_non_blocking (now_nonblocking);
+  return res;
+}
+
 int __reg2
 fhandler_pipe::fstat (struct stat *buf)
 {


More information about the Cygwin-cvs mailing list