[PATCH draft v2 3/6] Cygwin: fhandler_pipe: control blocking mode of the Windows pipe

Ken Brown kbrown@cornell.edu
Fri Jun 7 18:05:00 GMT 2019


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.
---
 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 b9cc7b471..5ae262c5c 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1192,6 +1192,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 ();
 
@@ -1209,6 +1210,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 d138226ba..7640fae36 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)
 {
-- 
2.21.0



More information about the Cygwin-patches mailing list