public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: fhandler_pipe: control blocking mode of the Windows pipe
Date: Tue, 14 Sep 2021 15:04:48 +0000 (GMT)	[thread overview]
Message-ID: <20210914150448.E59F73857C71@sourceware.org> (raw)

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


                 reply	other threads:[~2021-09-14 15:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210914150448.E59F73857C71@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /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).