OSDN Git Service

* fhandler.h (fhandler_pipe::create_selectable): Remove optional argument, take
authorcgf <cgf>
Sun, 30 Oct 2011 04:50:33 +0000 (04:50 +0000)
committercgf <cgf>
Sun, 30 Oct 2011 04:50:33 +0000 (04:50 +0000)
an options argument for CreateNamedPipe/CreateFile.  Change handle arguments to
expect pointers.
(fhandler_fifo::fifo_state): Delete.
(fhandler_fifo::dummy_client): Ditto.
(fhandler_fifo::open_nonserver): Ditto.
(fhandler_fifo::wait_state): Ditto.
(fhandler_fifo::raw_write): Ditto.
(fhandler_fifo::read_ready): New field.
(fhandler_fifo::write_ready): Ditto.
(fhandler_fifo::wait): Modify argument.
(fhandler_fifo::fifo_name): Add a new argument.
(fhandler_fifo::fixup_after_fork): New function.
* fhandler_fifo.cc (fhandler_fifo::fhandler_fifo): Remove initialization of
expunged elements.  Initialize new handles to NULL.
(fhandler_fifo::open_nonserver): Delete.
(fnevent): New macro for creating a named event.
(fnpipe): New macro for creating a unique named pipe name.
(create_pipe): New macro for simplification of named pipe creation.
(fhandler_fifo::fifo_name): Use new argument when creating a shared name.
(fhandler_fifo::open): Rewrite.  Use events to synchronize.
(pure_debug_printf): New macro, active only when DEBUGGING.
(fhandler_fifo::wait): Rewrite to wait for new fifo events which are supplied
as a parameter.
(fhandler_fifo::raw_read): Rewrite to use handle mechanism to detect
client-side disconnect.
(fhandler_fifo::raw_write): Delete.
(fhandler_fifo::close): Remove accommodations for expunged fields.  Close event
handles.
(fhandler_fifo::dup): Remove accommodations for expunged fields.  Duplicate
event handles.
(fhandler_fifo::fixup_after_fork): New function.  Perform fixups on event
handles.
(fhandler_fifo::set_close_on_exec): Remove accommodations for expunged fields.
Set inheritance for new handle fields.
* miscfuncs.cc (CreatePipeOverlapped): Accommodate changes in
fhandler_pipe::create_selectable.
* tty.cc (tty::not_allocated): Ditto.
* pipe.cc (fhandler_pipe::create): Ditto.
(fhandler_pipe::create_selectable): Accept an extra open_mode argument.  Pass
arguments by reference and allow opening one end of the pipe at a time.
* sys/strace.h (debug_only_printf): Define new macro which calls debug_printf
only when DEBUGGING is defined.

winsup/cygwin/ChangeLog
winsup/cygwin/fhandler.cc
winsup/cygwin/fhandler.h
winsup/cygwin/fhandler_fifo.cc
winsup/cygwin/fhandler_tty.cc
winsup/cygwin/include/sys/strace.h
winsup/cygwin/miscfuncs.cc
winsup/cygwin/pipe.cc
winsup/cygwin/tty.cc

index 07ba4f9..0ec1d3e 100644 (file)
@@ -1,3 +1,52 @@
+2011-10-30  Christopher Faylor  <me.cygwin2011@cgf.cx>
+
+       * fhandler.h (fhandler_pipe::create_selectable): Remove optional
+       argument, take an options argument for CreateNamedPipe/CreateFile.
+       Change handle arguments to expect pointers.
+       (fhandler_fifo::fifo_state): Delete.
+       (fhandler_fifo::dummy_client): Ditto.
+       (fhandler_fifo::open_nonserver): Ditto.
+       (fhandler_fifo::wait_state): Ditto.
+       (fhandler_fifo::raw_write): Ditto.
+       (fhandler_fifo::read_ready): New field.
+       (fhandler_fifo::write_ready): Ditto.
+       (fhandler_fifo::wait): Modify argument.
+       (fhandler_fifo::fifo_name): Add a new argument.
+       (fhandler_fifo::fixup_after_fork): New function.
+       * fhandler_fifo.cc (fhandler_fifo::fhandler_fifo): Remove
+       initialization of expunged elements.  Initialize new handles to NULL.
+       (fhandler_fifo::open_nonserver): Delete.
+       (fnevent): New macro for creating a named event.
+       (fnpipe): New macro for creating a unique named pipe name.
+       (create_pipe): New macro for simplification of named pipe creation.
+       (fhandler_fifo::fifo_name): Use new argument when creating a shared
+       name.
+       (fhandler_fifo::open): Rewrite.  Use events to synchronize.
+       (pure_debug_printf): New macro, active only when DEBUGGING.
+       (fhandler_fifo::wait): Rewrite to wait for new fifo events which are
+       supplied as a parameter.
+       (fhandler_fifo::raw_read): Rewrite to use handle mechanism to detect
+       client-side disconnect.
+       (fhandler_fifo::raw_write): Delete.
+       (fhandler_fifo::close): Remove accommodations for expunged fields.
+       Close event handles.
+       (fhandler_fifo::dup): Remove accommodations for expunged fields.
+       Duplicate event handles.
+       (fhandler_fifo::fixup_after_fork): New function.  Perform fixups on
+       event handles.
+       (fhandler_fifo::set_close_on_exec): Remove accommodations for expunged
+       fields.  Set inheritance for new handle fields.
+       * miscfuncs.cc (CreatePipeOverlapped): Accommodate changes in
+       fhandler_pipe::create_selectable.
+       * tty.cc (tty::not_allocated): Ditto.
+       * pipe.cc (fhandler_pipe::create): Ditto.
+       (fhandler_pipe::create_selectable): Accept an extra open_mode argument.
+       Pass arguments by reference and allow opening one end of the pipe at a
+       time.
+
+       * sys/strace.h (debug_only_printf): Define new macro which calls
+       debug_printf only when DEBUGGING is defined.
+
 2011-10-28  Christopher Faylor  <me.cygwin2011@cgf.cx>
 
        * exceptions.cc (sigpacket::process): Avoid a potential deadlock when
index e02f961..ee5a0c8 100644 (file)
@@ -275,8 +275,8 @@ retry:
 
 /* Cover function to WriteFile to provide Posix interface and semantics
    (as much as possible).  */
-static LARGE_INTEGER off_current = { QuadPart:FILE_USE_FILE_POINTER_POSITION };
-static LARGE_INTEGER off_append = { QuadPart:FILE_WRITE_TO_END_OF_FILE };
+static NO_COPY LARGE_INTEGER off_current = { QuadPart:FILE_USE_FILE_POINTER_POSITION };
+static NO_COPY LARGE_INTEGER off_append = { QuadPart:FILE_WRITE_TO_END_OF_FILE };
 
 ssize_t __stdcall
 fhandler_base::raw_write (const void *ptr, size_t len)
@@ -1914,7 +1914,7 @@ fhandler_base_overlapped::wait_overlapped (bool inres, bool writing, DWORD *byte
     }
   else if (err == ERROR_HANDLE_EOF || err == ERROR_BROKEN_PIPE)
     {
-      debug_printf ("EOF");
+      debug_printf ("EOF, %E");
       *bytes = 0;
       res = overlapped_success;
       if (writing && err == ERROR_BROKEN_PIPE)
index d463667..900dd93 100644 (file)
@@ -129,16 +129,16 @@ class fhandler_base
 
   struct status_flags
   {
-    unsigned rbinary            : 1; /* binary read mode */
-    unsigned rbinset            : 1; /* binary read mode explicitly set */
-    unsigned wbinary            : 1; /* binary write mode */
-    unsigned wbinset            : 1; /* binary write mode explicitly set */
-    unsigned nohandle           : 1; /* No handle associated with fhandler. */
-    unsigned did_lseek          : 1; /* set when lseek is called as a flag that
+    unsigned rbinary           : 1; /* binary read mode */
+    unsigned rbinset           : 1; /* binary read mode explicitly set */
+    unsigned wbinary           : 1; /* binary write mode */
+    unsigned wbinset           : 1; /* binary write mode explicitly set */
+    unsigned nohandle          : 1; /* No handle associated with fhandler. */
+    unsigned did_lseek         : 1; /* set when lseek is called as a flag that
                                        _write should check if we've moved
                                        beyond EOF, zero filling or making
                                        file sparse if so. */
-    unsigned query_open         : 3; /* open file without requesting either
+    unsigned query_open                : 3; /* open file without requesting either
                                        read or write access */
     unsigned close_on_exec      : 1; /* close-on-exec */
     unsigned need_fork_fixup    : 1; /* Set if need to fixup after fork. */
@@ -437,8 +437,8 @@ public:
 
   virtual fhandler_base *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_base)); 
-    fhandler_base *fh = new (ptr) fhandler_base (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_base));
+    fhandler_base *fh = new (ptr) fhandler_base (ptr);
     copyto (fh);
     return fh;
   }
@@ -511,12 +511,12 @@ class fhandler_socket: public fhandler_base
   char *peer_sun_path;
   struct status_flags
   {
-    unsigned async_io              : 1; /* async I/O */
+    unsigned async_io             : 1; /* async I/O */
     unsigned saw_shutdown_read     : 1; /* Socket saw a SHUT_RD */
     unsigned saw_shutdown_write    : 1; /* Socket saw a SHUT_WR */
-    unsigned saw_reuseaddr         : 1; /* Socket saw SO_REUSEADDR call */
-    unsigned listener              : 1; /* listen called */
-    unsigned connect_state         : 2;
+    unsigned saw_reuseaddr        : 1; /* Socket saw SO_REUSEADDR call */
+    unsigned listener             : 1; /* listen called */
+    unsigned connect_state        : 2;
    public:
     status_flags () :
       async_io (0), saw_shutdown_read (0), saw_shutdown_write (0),
@@ -604,8 +604,8 @@ class fhandler_socket: public fhandler_base
 
   fhandler_socket *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_socket)); 
-    fhandler_socket *fh = new (ptr) fhandler_socket (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_socket));
+    fhandler_socket *fh = new (ptr) fhandler_socket (ptr);
     copyto (fh);
     return fh;
   }
@@ -658,8 +658,8 @@ public:
 
   virtual fhandler_base_overlapped *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_base_overlapped)); 
-    fhandler_base_overlapped *fh = new (ptr) fhandler_base_overlapped (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_base_overlapped));
+    fhandler_base_overlapped *fh = new (ptr) fhandler_base_overlapped (ptr);
     copyto (fh);
     return fh;
   }
@@ -690,9 +690,8 @@ public:
   int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
   int init (HANDLE, DWORD, mode_t);
   static int create (fhandler_pipe *[2], unsigned, int);
-  static int create_selectable (LPSECURITY_ATTRIBUTES, HANDLE&, HANDLE&, DWORD, const char * = NULL);
-  friend class fhandler_fifo;
-
+  static int create_selectable (LPSECURITY_ATTRIBUTES, HANDLE *, HANDLE *, DWORD,
+                               const char *, DWORD);
   fhandler_pipe (void *) {}
 
   void copyto (fhandler_base *x)
@@ -704,8 +703,8 @@ public:
 
   fhandler_pipe *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pipe)); 
-    fhandler_pipe *fh = new (ptr) fhandler_pipe (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pipe));
+    fhandler_pipe *fh = new (ptr) fhandler_pipe (ptr);
     copyto (fh);
     return fh;
   }
@@ -713,31 +712,19 @@ public:
 
 class fhandler_fifo: public fhandler_base_overlapped
 {
-  enum fifo_state
-  {
-    fifo_unknown,
-    fifo_wait_for_client,
-    fifo_wait_for_server,
-    fifo_wait_for_next_client,
-    fifo_eof,
-    fifo_error,
-    fifo_eintr,
-    fifo_ok
-  };
-  fifo_state wait_state;
-  HANDLE dummy_client;
-  HANDLE open_nonserver (const char *, unsigned, LPSECURITY_ATTRIBUTES);
-  bool wait (bool) __attribute__ ((regparm (1)));
-  char *fifo_name (char *) __attribute__ ((regparm (2)));
+  HANDLE read_ready;
+  HANDLE write_ready;
+  bool wait (HANDLE) __attribute__ ((regparm (2)));
+  char *fifo_name (char *, const char *) __attribute__ ((regparm (2)));
 public:
   fhandler_fifo ();
-  void __stdcall raw_read (void *, size_t&) __attribute__ ((regparm (3)));
-  ssize_t __stdcall raw_write (const void *, size_t) __attribute__ ((regparm (3)));
   int open (int, mode_t);
   int close ();
   int dup (fhandler_base *child, int);
   bool isfifo () const { return true; }
   void set_close_on_exec (bool val);
+  void __stdcall raw_read (void *ptr, size_t& ulen) __attribute__ ((regparm (3)));
+  void fixup_after_fork (HANDLE);
   int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
   select_record *select_read (select_stuff *);
   select_record *select_write (select_stuff *);
@@ -754,8 +741,8 @@ public:
 
   fhandler_fifo *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_fifo)); 
-    fhandler_fifo *fh = new (ptr) fhandler_fifo (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_fifo));
+    fhandler_fifo *fh = new (ptr) fhandler_fifo (ptr);
     copyto (fh);
     return fh;
   }
@@ -783,8 +770,8 @@ class fhandler_mailslot : public fhandler_base_overlapped
 
   fhandler_mailslot *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_mailslot)); 
-    fhandler_mailslot *fh = new (ptr) fhandler_mailslot (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_mailslot));
+    fhandler_mailslot *fh = new (ptr) fhandler_mailslot (ptr);
     copyto (fh);
     return fh;
   }
@@ -832,8 +819,8 @@ class fhandler_dev_raw: public fhandler_base
 
   fhandler_dev_raw *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_raw)); 
-    fhandler_dev_raw *fh = new (ptr) fhandler_dev_raw (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_raw));
+    fhandler_dev_raw *fh = new (ptr) fhandler_dev_raw (ptr);
     copyto (fh);
     return fh;
   }
@@ -892,8 +879,8 @@ class fhandler_dev_floppy: public fhandler_dev_raw
 
   fhandler_dev_floppy *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_floppy)); 
-    fhandler_dev_floppy *fh = new (ptr) fhandler_dev_floppy (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_floppy));
+    fhandler_dev_floppy *fh = new (ptr) fhandler_dev_floppy (ptr);
     copyto (fh);
     return fh;
   }
@@ -940,8 +927,8 @@ class fhandler_dev_tape: public fhandler_dev_raw
 
   fhandler_dev_tape *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_tape)); 
-    fhandler_dev_tape *fh = new (ptr) fhandler_dev_tape (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_tape));
+    fhandler_dev_tape *fh = new (ptr) fhandler_dev_tape (ptr);
     copyto (fh);
     return fh;
   }
@@ -1006,8 +993,8 @@ class fhandler_disk_file: public fhandler_base
 
   fhandler_disk_file *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_disk_file)); 
-    fhandler_disk_file *fh = new (ptr) fhandler_disk_file (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_disk_file));
+    fhandler_disk_file *fh = new (ptr) fhandler_disk_file (ptr);
     copyto (fh);
     return fh;
   }
@@ -1044,8 +1031,8 @@ class fhandler_cygdrive: public fhandler_disk_file
 
   fhandler_cygdrive *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_cygdrive)); 
-    fhandler_cygdrive *fh = new (ptr) fhandler_cygdrive (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_cygdrive));
+    fhandler_cygdrive *fh = new (ptr) fhandler_cygdrive (ptr);
     copyto (fh);
     return fh;
   }
@@ -1108,8 +1095,8 @@ class fhandler_serial: public fhandler_base
 
   fhandler_serial *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_serial)); 
-    fhandler_serial *fh = new (ptr) fhandler_serial (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_serial));
+    fhandler_serial *fh = new (ptr) fhandler_serial (ptr);
     copyto (fh);
     return fh;
   }
@@ -1166,8 +1153,8 @@ class fhandler_termios: public fhandler_base
 
   virtual fhandler_termios *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_termios)); 
-    fhandler_termios *fh = new (ptr) fhandler_termios (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_termios));
+    fhandler_termios *fh = new (ptr) fhandler_termios (ptr);
     copyto (fh);
     return fh;
   }
@@ -1365,8 +1352,8 @@ private:
 
   fhandler_console *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_console)); 
-    fhandler_console *fh = new (ptr) fhandler_console (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_console));
+    fhandler_console *fh = new (ptr) fhandler_console (ptr);
     copyto (fh);
     return fh;
   }
@@ -1408,8 +1395,8 @@ class fhandler_pty_common: public fhandler_termios
 
   virtual fhandler_pty_common *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pty_common)); 
-    fhandler_pty_common *fh = new (ptr) fhandler_pty_common (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pty_common));
+    fhandler_pty_common *fh = new (ptr) fhandler_pty_common (ptr);
     copyto (fh);
     return fh;
   }
@@ -1462,8 +1449,8 @@ class fhandler_pty_slave: public fhandler_pty_common
 
   fhandler_pty_slave *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pty_slave)); 
-    fhandler_pty_slave *fh = new (ptr) fhandler_pty_slave (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pty_slave));
+    fhandler_pty_slave *fh = new (ptr) fhandler_pty_slave (ptr);
     copyto (fh);
     return fh;
   }
@@ -1520,8 +1507,8 @@ public:
 
   fhandler_pty_master *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pty_master)); 
-    fhandler_pty_master *fh = new (ptr) fhandler_pty_master (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_pty_master));
+    fhandler_pty_master *fh = new (ptr) fhandler_pty_master (ptr);
     copyto (fh);
     return fh;
   }
@@ -1547,8 +1534,8 @@ class fhandler_dev_null: public fhandler_base
 
   fhandler_dev_null *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_null)); 
-    fhandler_dev_null *fh = new (ptr) fhandler_dev_null (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_null));
+    fhandler_dev_null *fh = new (ptr) fhandler_dev_null (ptr);
     copyto (fh);
     return fh;
   }
@@ -1582,8 +1569,8 @@ class fhandler_dev_zero: public fhandler_base
 
   fhandler_dev_zero *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_zero)); 
-    fhandler_dev_zero *fh = new (ptr) fhandler_dev_zero (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_zero));
+    fhandler_dev_zero *fh = new (ptr) fhandler_dev_zero (ptr);
     copyto (fh);
     return fh;
   }
@@ -1620,8 +1607,8 @@ class fhandler_dev_random: public fhandler_base
 
   fhandler_dev_random *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_random)); 
-    fhandler_dev_random *fh = new (ptr) fhandler_dev_random (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_random));
+    fhandler_dev_random *fh = new (ptr) fhandler_dev_random (ptr);
     copyto (fh);
     return fh;
   }
@@ -1660,8 +1647,8 @@ class fhandler_dev_mem: public fhandler_base
 
   fhandler_dev_mem *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_mem)); 
-    fhandler_dev_mem *fh = new (ptr) fhandler_dev_mem (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_mem));
+    fhandler_dev_mem *fh = new (ptr) fhandler_dev_mem (ptr);
     copyto (fh);
     return fh;
   }
@@ -1696,8 +1683,8 @@ class fhandler_dev_clipboard: public fhandler_base
 
   fhandler_dev_clipboard *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_clipboard)); 
-    fhandler_dev_clipboard *fh = new (ptr) fhandler_dev_clipboard (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_clipboard));
+    fhandler_dev_clipboard *fh = new (ptr) fhandler_dev_clipboard (ptr);
     copyto (fh);
     return fh;
   }
@@ -1735,8 +1722,8 @@ class fhandler_windows: public fhandler_base
 
   fhandler_windows *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_windows)); 
-    fhandler_windows *fh = new (ptr) fhandler_windows (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_windows));
+    fhandler_windows *fh = new (ptr) fhandler_windows (ptr);
     copyto (fh);
     return fh;
   }
@@ -1782,8 +1769,8 @@ class fhandler_dev_dsp: public fhandler_base
 
   fhandler_dev_dsp *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_dsp)); 
-    fhandler_dev_dsp *fh = new (ptr) fhandler_dev_dsp (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_dev_dsp));
+    fhandler_dev_dsp *fh = new (ptr) fhandler_dev_dsp (ptr);
     copyto (fh);
     return fh;
   }
@@ -1833,8 +1820,8 @@ class fhandler_virtual : public fhandler_base
 
   virtual fhandler_virtual *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_virtual)); 
-    fhandler_virtual *fh = new (ptr) fhandler_virtual (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_virtual));
+    fhandler_virtual *fh = new (ptr) fhandler_virtual (ptr);
     copyto (fh);
     return fh;
   }
@@ -1865,8 +1852,8 @@ class fhandler_proc: public fhandler_virtual
 
   virtual fhandler_proc *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_proc)); 
-    fhandler_proc *fh = new (ptr) fhandler_proc (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_proc));
+    fhandler_proc *fh = new (ptr) fhandler_proc (ptr);
     copyto (fh);
     return fh;
   }
@@ -1901,8 +1888,8 @@ class fhandler_procsys: public fhandler_virtual
 
   fhandler_procsys *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_procsys)); 
-    fhandler_procsys *fh = new (ptr) fhandler_procsys (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_procsys));
+    fhandler_procsys *fh = new (ptr) fhandler_procsys (ptr);
     copyto (fh);
     return fh;
   }
@@ -1930,8 +1917,8 @@ class fhandler_procsysvipc: public fhandler_proc
 
   fhandler_procsysvipc *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_procsysvipc)); 
-    fhandler_procsysvipc *fh = new (ptr) fhandler_procsysvipc (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_procsysvipc));
+    fhandler_procsysvipc *fh = new (ptr) fhandler_procsysvipc (ptr);
     copyto (fh);
     return fh;
   }
@@ -1960,8 +1947,8 @@ class fhandler_netdrive: public fhandler_virtual
 
   fhandler_netdrive *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_netdrive)); 
-    fhandler_netdrive *fh = new (ptr) fhandler_netdrive (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_netdrive));
+    fhandler_netdrive *fh = new (ptr) fhandler_netdrive (ptr);
     copyto (fh);
     return fh;
   }
@@ -2000,8 +1987,8 @@ class fhandler_registry: public fhandler_proc
 
   fhandler_registry *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_registry)); 
-    fhandler_registry *fh = new (ptr) fhandler_registry (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_registry));
+    fhandler_registry *fh = new (ptr) fhandler_registry (ptr);
     copyto (fh);
     return fh;
   }
@@ -2032,8 +2019,8 @@ class fhandler_process: public fhandler_proc
 
   fhandler_process *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_process)); 
-    fhandler_process *fh = new (ptr) fhandler_process (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_process));
+    fhandler_process *fh = new (ptr) fhandler_process (ptr);
     copyto (fh);
     return fh;
   }
@@ -2061,8 +2048,8 @@ class fhandler_procnet: public fhandler_proc
 
   fhandler_procnet *clone ()
   {
-    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_procnet)); 
-    fhandler_procnet *fh = new (ptr) fhandler_procnet (ptr); 
+    void *ptr = (void *) ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_procnet));
+    fhandler_procnet *fh = new (ptr) fhandler_procnet (ptr);
     copyto (fh);
     return fh;
   }
index c08f546..0b1b7b5 100644 (file)
 #include "ntdll.h"
 
 fhandler_fifo::fhandler_fifo ():
-  fhandler_base_overlapped (), wait_state (fifo_unknown), dummy_client (NULL)
+  fhandler_base_overlapped (),
+  read_ready (NULL), write_ready (NULL)
 {
   max_atomic_write = DEFAULT_PIPEBUFSIZE;
   need_fork_fixup (true);
 }
 
-HANDLE
-fhandler_fifo::open_nonserver (const char *npname, unsigned low_flags,
-                              LPSECURITY_ATTRIBUTES sa_buf)
-{
-  DWORD mode = 0;
-  if (low_flags == O_RDONLY)
-    mode = GENERIC_READ;
-  else if (low_flags == O_WRONLY)
-    mode = GENERIC_WRITE;
-  else
-    mode = GENERIC_READ | GENERIC_WRITE;
-  while (1)
-    {
-      HANDLE h = CreateFile (npname, mode, 0, sa_buf, OPEN_EXISTING,
-                            FILE_FLAG_OVERLAPPED, NULL);
-      if (h != INVALID_HANDLE_VALUE || GetLastError () != ERROR_PIPE_NOT_CONNECTED)
-       return h;
-      if (IsEventSignalled (signal_arrived))
-       {
-         set_errno (EINTR);
-         return NULL;
-       }
-    }
-}
+#define fnevent(w) fifo_name (npbuf, w "-event")
+#define fnpipe() fifo_name (npbuf, "fifo")
+#define create_pipe(r, w) \
+  fhandler_pipe::create_selectable (sa_buf, (r), (w), 0, fnpipe (), open_mode)
 
 char *
-fhandler_fifo::fifo_name (char *buf)
+fhandler_fifo::fifo_name (char *buf, const char *what)
 {
   /* Generate a semi-unique name to associate with this fifo. */
-  __small_sprintf (buf, "\\\\.\\pipe\\__cygfifo__%S_%08x_%016X",
-                  &installation_key, get_dev (), get_ino ());
+  __small_sprintf (buf, "%s.%08x.%016X", what, get_dev (),
+                  get_ino ());
   return buf;
 }
 
-#define FIFO_PIPE_MODE (PIPE_TYPE_BYTE | PIPE_READMODE_BYTE)
-#define FIFO_BUF_SIZE  4096
-#define cnp(m, s) CreateNamedPipe(npname, (m), FIFO_PIPE_MODE, \
-                              PIPE_UNLIMITED_INSTANCES, (s), (s), \
-                              NMPWAIT_WAIT_FOREVER, sa_buf)
-
 inline PSECURITY_ATTRIBUTES
 sec_user_cloexec (bool cloexec, PSECURITY_ATTRIBUTES sa, PSID sid)
 {
@@ -79,207 +54,242 @@ sec_user_cloexec (bool cloexec, PSECURITY_ATTRIBUTES sa, PSID sid)
 int
 fhandler_fifo::open (int flags, mode_t)
 {
-  int res = 1;
-  char npname[MAX_PATH];
+  enum
+  {
+    success,
+    error_errno_set,
+    error_set_errno
+  } res;
+  bool reader, writer;
+  DWORD open_mode = FILE_FLAG_OVERLAPPED;
 
-  fifo_name (npname);
-  unsigned low_flags = flags & O_ACCMODE;
-  DWORD mode = 0;
-  if (low_flags == O_WRONLY)
-    mode = PIPE_ACCESS_OUTBOUND;
-  else if (low_flags == O_RDONLY || low_flags == O_RDWR)
-    mode = PIPE_ACCESS_DUPLEX;
-  else
+  /* Determine what we're doing with this fhandler: reading, writing, both */
+  switch (flags & O_ACCMODE)
     {
+    case O_RDONLY:
+      reader = true;
+      writer = false;
+      break;
+    case O_WRONLY:
+      writer = true;
+      reader = false;
+      break;
+    case O_RDWR:
+      reader = true;
+      writer = true;
+      open_mode |= PIPE_ACCESS_DUPLEX;
+      break;
+    default:
       set_errno (EINVAL);
-      res = 0;
+      res = error_errno_set;
+      goto out;
     }
 
-  if (res)
+  set_flags (flags);
+  char char_sa_buf[1024];
+  LPSECURITY_ATTRIBUTES sa_buf;
+  sa_buf = sec_user_cloexec (flags & O_CLOEXEC, (PSECURITY_ATTRIBUTES) char_sa_buf,
+                     cygheap->user.sid());
+  char npbuf[MAX_PATH];
+
+  /* Create control events for this named pipe */
+  if (!(read_ready = CreateEvent (sa_buf, true, false, fnevent ("r"))))
+    {
+      debug_printf ("CreatEvent for %s failed, %E", npbuf);
+      res = error_set_errno;
+      goto out;
+    }
+  if (!(write_ready = CreateEvent (sa_buf, true, false, fnevent ("w"))))
     {
-      char char_sa_buf[1024];
-      LPSECURITY_ATTRIBUTES sa_buf =
-       sec_user_cloexec (flags & O_CLOEXEC, (PSECURITY_ATTRIBUTES) char_sa_buf,
-                         cygheap->user.sid());
-      bool do_seterrno = true;
+      debug_printf ("CreatEvent for %s failed, %E", npbuf);
+      res = error_set_errno;
+      goto out;
+    }
 
-      HANDLE h;
-      bool nonblocking_write = !!((flags & (O_WRONLY | O_NONBLOCK)) == (O_WRONLY | O_NONBLOCK));
-      wait_state = fifo_unknown;
-      if (mode != PIPE_ACCESS_OUTBOUND)
-       {
-         h = cnp (mode |  FILE_FLAG_OVERLAPPED, FIFO_BUF_SIZE);
-         wait_state = fifo_wait_for_client;
-       }
-      else
-       {
-         h = open_nonserver (npname, low_flags, sa_buf);
-         if (h != INVALID_HANDLE_VALUE)
-           wait_state = fifo_ok;
-         else if (nonblocking_write)
-           {
-             set_errno (ENXIO);
-             do_seterrno = false;
-           }
-         else if ((h = cnp (PIPE_ACCESS_DUPLEX, 1)) != INVALID_HANDLE_VALUE)
-           {
-             if ((dummy_client = open_nonserver (npname, low_flags, sa_buf))
-                 != INVALID_HANDLE_VALUE)
-               {
-                 wait_state = fifo_wait_for_server;
-                 ProtectHandle (dummy_client);
-               }
-             else
-               {
-                 DWORD saveerr = GetLastError ();
-                 CloseHandle (h);
-                 h = INVALID_HANDLE_VALUE;
-                 SetLastError (saveerr);
-               }
-           }
-       }
-      if (h == INVALID_HANDLE_VALUE)
+  /* If we're reading, create the pipe, signal that we're ready and wait for
+     a writer.
+     FIXME: Probably need to special case O_RDWR case.  */
+  if (!reader)
+    /* We are not a reader */;
+  else if (create_pipe (&get_io_handle (), NULL))
+    {
+      debug_printf ("create of reader failed");
+      res = error_set_errno;
+      goto out;
+    }
+  else if (!SetEvent (read_ready))
+    {
+      debug_printf ("SetEvent for read_ready failed, %E");
+      res = error_set_errno;
+      goto out;
+    }
+  else if (!writer && !wait (write_ready))
+    {
+      debug_printf ("wait for write_ready failed, %E");
+      res = error_errno_set;
+      goto out;
+    }
+
+  /* If we're writing, it's a little tricky since it is possible that
+     we're attempting to open the other end of a pipe which is already
+     connected.  In that case, we detect ERROR_PIPE_BUSY, reset the
+     read_ready event and wait for the reader to allow us to connect
+     by signalling read_ready.
+
+     Once the pipe has been set up, we signal write_ready.  */
+  if (writer)
+    {
+      int err;
+      while (1)
+       if (!wait (read_ready))
+         {
+           res = error_errno_set;
+           goto out;
+         }
+       else if ((err = create_pipe (NULL, &get_io_handle ())) == 0)
+         break;
+       else if (err == ERROR_PIPE_BUSY)
+         {
+           debug_only_printf ("pipe busy");
+           ResetEvent (read_ready);
+         }
+       else
+         {
+           debug_printf ("create of writer failed");
+           res = error_set_errno;
+           goto out;
+         }
+      if (!SetEvent (write_ready))
        {
-         if (do_seterrno)
-           __seterrno ();
-         res = 0;
+         debug_printf ("SetEvent for write_ready failed, %E");
+         res = error_set_errno;
+         goto out;
        }
-      else if (setup_overlapped ())
+    }
+
+  /* If setup_overlapped() succeeds (and why wouldn't it?) we are all set. */
+  if (setup_overlapped () == 0)
+    res = success;
+  else
+    {
+      debug_printf ("setup_overlapped failed, %E");
+      res = error_set_errno;
+    }
+
+out:
+  if (res == error_set_errno)
+    __seterrno ();
+  if (res != success)
+    {
+      if (read_ready)
        {
-         CloseHandle (h);
-         __seterrno ();
-         res = 0;
+         CloseHandle (read_ready);
+         read_ready = NULL;
        }
-      else
+      if (write_ready)
        {
-         set_io_handle (h);
-         set_flags (flags);
-         res = 1;
+         CloseHandle (write_ready);
+         write_ready = NULL;
        }
+      if (get_io_handle ())
+       CloseHandle (get_io_handle ());
     }
-
-  debug_printf ("returning %d, errno %d", res, get_errno ());
-  return res;
+  debug_printf ("res %d", res);
+  return res == success;
 }
 
 bool
-fhandler_fifo::wait (bool iswrite)
+fhandler_fifo::wait (HANDLE h)
 {
-  DWORD ninstances;
-  switch (wait_state)
+#ifdef DEBUGGING
+  const char *what;
+  if (h == read_ready)
+    what = "reader";
+  else if (h == write_ready)
+    what = "writer";
+  else
+    what = "overlapped event";
+#endif
+  HANDLE w4[3] = {h, signal_arrived, pthread::get_cancel_event ()};
+
+  /* Set the wait to zero for non-blocking I/O-related events. */
+  DWORD wait = ((h == read_ready || h == write_ready)
+               && get_flags () & O_NONBLOCK) ? 0 : INFINITE;
+
+  debug_only_printf ("waiting for %s", what);
+  /* Wait for the event.  Set errno, as appropriate if something goes wrong. */
+  switch (WaitForMultipleObjects (3, w4, false, wait))
     {
-    case fifo_wait_for_next_client:
-      DisconnectNamedPipe (get_handle ());
-      if (!GetNamedPipeHandleState (get_handle (), NULL, &ninstances, NULL, NULL, NULL, 0))
+    case WAIT_OBJECT_0:
+      debug_only_printf ("successfully waited for %s", what);
+      return true;
+    case WAIT_TIMEOUT:
+      if (h == write_ready)
        {
-         __seterrno ();
-         wait_state = fifo_error;
-         return false;
+         debug_only_printf ("wait timed out waiting for write but will still open reader since non-blocking mode");
+         return true;
        }
-      if (ninstances <= 1)
+      else
        {
-         wait_state = fifo_eof;
+         set_errno (ENXIO);
          return false;
        }
-    case fifo_wait_for_client:
-      {
-       DWORD dummy_bytes;
-       while (1)
-         {
-           int res = ConnectNamedPipe (get_handle (), get_overlapped ());
-           if (GetLastError () != ERROR_NO_DATA && GetLastError () != ERROR_PIPE_CONNECTED)
-             {
-               res = wait_overlapped (res, iswrite, &dummy_bytes, false);
-               if (!res)
-                 {
-                   if (get_errno () != EINTR)
-                     wait_state = fifo_error;
-                   else if (!_my_tls.call_signal_handler ())
-                     wait_state = fifo_eintr;
-                   else
-                     continue;
-                   return false;
-                 }
-             }
-           wait_state = fifo_ok;
-           break;
-         }
-      }
       break;
-    case fifo_wait_for_server:
-      char npname[MAX_PATH];
-      fifo_name (npname);
-      char char_sa_buf[1024];
-      LPSECURITY_ATTRIBUTES sa_buf;
-      sa_buf = sec_user_cloexec (close_on_exec (),
-                                (PSECURITY_ATTRIBUTES) char_sa_buf,
-                                cygheap->user.sid());
-      while (1)
-       {
-         if (WaitNamedPipe (npname, 10))
-           /* connected, maybe */;
-         else if (GetLastError () != ERROR_SEM_TIMEOUT)
-           {
-             __seterrno ();
-             return false;
-           }
-         else if (!IsEventSignalled (signal_arrived))
-           continue;
-         else if (_my_tls.call_signal_handler ())
-           continue;
-         else
-           {
-             set_errno (EINTR);
-             return false;
-           }
-         HANDLE h = open_nonserver (npname, get_flags () & O_ACCMODE, sa_buf);
-         if (h != INVALID_HANDLE_VALUE)
-           {
-             ForceCloseHandle (get_handle ());
-             ForceCloseHandle (dummy_client);
-             dummy_client = NULL;
-             wait_state = fifo_ok;
-             set_io_handle (h);
-             break;
-           }
-         if (GetLastError () == ERROR_PIPE_LISTENING)
-           continue;
-         else
-           {
-             __seterrno ();
-             return false;
-           }
-       }
-    default:
+    case WAIT_OBJECT_0 + 1:
+      debug_only_printf ("interrupted by signal while waiting for %s", what);
+      set_errno (EINTR);
+      return false;
+    case WAIT_OBJECT_0 + 2:
+      debug_only_printf ("cancellable interruption while waiting for %s", what);
+      pthread::static_cancel_self ();  /* never returns */
       break;
-    }
-    return true;
+    default:
+      debug_only_printf ("unknown error while waiting for %s", what);
+      __seterrno ();
+      return false;
+   }
 }
 
 void __stdcall
 fhandler_fifo::raw_read (void *in_ptr, size_t& len)
 {
-  while (wait_state != fifo_eof && wait_state != fifo_error && wait_state != fifo_eintr)
-    if (!wait (false))
-      len = (wait_state == fifo_error || wait_state == fifo_eintr) ? (size_t) -1 : 0;
-    else
-      {
-       size_t prev_len = len;
-       fhandler_base_overlapped::raw_read (in_ptr, len);
-       if (len)
-         break;
-       wait_state = fifo_wait_for_next_client;
-       len = prev_len;
-      }
-  if (wait_state == fifo_eintr)
-    wait_state = fifo_wait_for_client;
-  debug_printf ("returning %d, mode %d, %E\n", len, get_errno ());
-}
+  size_t orig_len = len;
+  for (int i = 0; i < 2; i++)
+    {
+      fhandler_base_overlapped::raw_read (in_ptr, len);
+      if (len || i || WaitForSingleObject (read_ready, 0) == WAIT_OBJECT_0)
+       break;
+      /* If we got here, then fhandler_base_overlapped::raw_read returned 0,
+        indicating "EOF" and something has set read_ready to zero.  That means
+        we should have a client waiting to connect.
+        FIXME: If the client CTRL-C's the open during this time then this
+        could hang indefinitely.  Maybe implement a timeout?  */
+      if (!DisconnectNamedPipe (get_io_handle ()))
+       {
+         debug_printf ("DisconnecttNamedPipe failed, %E");
+         goto errno_out;
+       }
+      else if (!ConnectNamedPipe (get_io_handle (), get_overlapped ())
+              && GetLastError () != ERROR_IO_PENDING)
+       {
+         debug_printf ("ConnectNamedPipe failed, %E");
+         goto errno_out;
+       }
+      else if (!SetEvent (read_ready))
+       {
+         debug_printf ("SetEvent (read_ready) failed, %E");
+         goto errno_out;
+       }
+      else if (!wait (get_overlapped_buffer ()->hEvent))
+       goto errout;    /* If wait() fails, errno is set so no need to set it */
+      len = orig_len;  /* Reset since raw_read above set it to zero. */
+    }
+  return;
 
-ssize_t __stdcall
-fhandler_fifo::raw_write (const void *ptr, size_t len)
-{
-  return wait (true) ? fhandler_base_overlapped::raw_write (ptr, len) : -1;
+errno_out:
+  __seterrno ();
+errout:
+  len = -1;
 }
 
 int __stdcall
@@ -293,41 +303,52 @@ fhandler_fifo::fstatvfs (struct statvfs *sfs)
 int
 fhandler_fifo::close ()
 {
-  wait_state = fifo_eof;
-  if (dummy_client)
-    {
-      ForceCloseHandle (dummy_client);
-      dummy_client = NULL;
-    }
+  CloseHandle (read_ready);
+  CloseHandle (write_ready);
   return fhandler_base::close ();
 }
 
 int
 fhandler_fifo::dup (fhandler_base *child, int flags)
 {
-  int res = fhandler_base_overlapped::dup (child, flags);
-  fhandler_fifo *fifo_child = (fhandler_fifo *) child;
-  if (res == 0 && dummy_client)
+  if (fhandler_base_overlapped::dup (child, flags))
     {
-      bool dres = DuplicateHandle (GetCurrentProcess (), dummy_client,
-                                  GetCurrentProcess (),
-                                  &fifo_child->dummy_client, 0,
-                                  TRUE, DUPLICATE_SAME_ACCESS);
-      if (!dres)
-       {
-         fifo_child->dummy_client = NULL;
-         child->close ();
-         __seterrno ();
-         res = -1;
-       }
+      __seterrno ();
+      return -1;
+    }
+  fhandler_fifo *fhf = (fhandler_fifo *) child;
+  if (!DuplicateHandle (GetCurrentProcess (), read_ready,
+                       GetCurrentProcess (), &fhf->read_ready,
+                       0, true, DUPLICATE_SAME_ACCESS))
+    {
+      fhf->close ();
+      __seterrno ();
+      return -1;
+    }
+  if (!DuplicateHandle (GetCurrentProcess (), write_ready,
+                       GetCurrentProcess (), &fhf->write_ready,
+                       0, true, DUPLICATE_SAME_ACCESS))
+    {
+      CloseHandle (fhf->read_ready);
+      fhf->close ();
+      __seterrno ();
+      return -1;
     }
-  return res;
+  return 0;
+}
+
+void
+fhandler_fifo::fixup_after_fork (HANDLE parent)
+{
+  fhandler_base_overlapped::fixup_after_fork (parent);
+  fork_fixup (parent, read_ready, "read_ready");
+  fork_fixup (parent, write_ready, "write_ready");
 }
 
 void
 fhandler_fifo::set_close_on_exec (bool val)
 {
   fhandler_base::set_close_on_exec (val);
-  if (dummy_client)
-    set_no_inheritance (dummy_client, val);
+  set_no_inheritance (read_ready, val);
+  set_no_inheritance (write_ready, val);
 }
index b019db0..a8a5659 100644 (file)
@@ -1624,8 +1624,8 @@ fhandler_pty_master::setup ()
 
   char pipename[sizeof("ptyNNNN-from-master")];
   __small_sprintf (pipename, "pty%d-to-master", unit);
-  res = fhandler_pipe::create_selectable (&sec_none, get_io_handle (),
-                                         to_master, 128 * 1024, pipename);
+  res = fhandler_pipe::create_selectable (&sec_none, &get_io_handle (),
+                                         &to_master, 128 * 1024, pipename, 0);
   if (res)
     {
       errstr = "output pipe";
index 89f2d50..a8eb05f 100644 (file)
@@ -125,6 +125,11 @@ void strace_printf (unsigned, const char *func, const char *, ...);
     }))
 #endif /*NOSTRACE*/
 
+#ifdef DEBUGGING
+#define debug_only_printf(fmt, args...) debug_printf (fmt , ## args)
+#else
+#define debug_only_printf(fmt, args...) do {} while (0)
+#endif
 #define debug_printf(fmt, args...) strace_printf_wrap(DEBUG, fmt , ## args)
 #define malloc_printf(fmt, args...) strace_printf_wrap1(MALLOC, fmt , ## args)
 #define minimal_printf(fmt, args...) strace_printf_wrap1(MINIMAL, fmt , ## args)
index 755a058..b94f099 100644 (file)
@@ -337,7 +337,8 @@ nice_to_winprio (int &nice)
 BOOL WINAPI
 CreatePipeOverlapped (PHANDLE hr, PHANDLE hw, LPSECURITY_ATTRIBUTES sa)
 {
-  int ret = fhandler_pipe::create_selectable (sa, *hr, *hw, 0);
+  int ret = fhandler_pipe::create_selectable (sa, hr, hw, 0, NULL,
+                                             FILE_FLAG_OVERLAPPED);
   if (ret)
     SetLastError (ret);
   return ret == 0;
index 99c7912..7bbe96f 100644 (file)
@@ -197,11 +197,14 @@ fhandler_pipe::dup (fhandler_base *child, int flags)
    Note that the return value is either 0 or GetLastError,
    unlike CreatePipe, which returns a bool for success or failure.  */
 int
-fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLEr,
-                                 HANDLE& w, DWORD psize, const char *name)
+fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE *r,
+                                 HANDLE *w, DWORD psize, const char *name, DWORD open_mode)
 {
   /* Default to error. */
-  r = w = INVALID_HANDLE_VALUE;
+  if (r)
+    *r = NULL;
+  if (w)
+    *w = NULL;
 
   /* Ensure that there is enough pipe buffer space for atomic writes.  */
   if (psize < DEFAULT_PIPEBUFSIZE)
@@ -210,26 +213,24 @@ fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE& r,
   char pipename[MAX_PATH];
   const size_t len = __small_sprintf (pipename, PIPE_INTRO "%S-",
                                      &installation_key);
+  if (name)
+    strcpy (pipename + len, name);
 
-  /* FIXME: Eventually make ttys work with overlapped I/O. */
-  DWORD overlapped = name ? 0 : FILE_FLAG_OVERLAPPED;
+  open_mode |= PIPE_ACCESS_INBOUND;
 
   /* Retry CreateNamedPipe as long as the pipe name is in use.
      Retrying will probably never be necessary, but we want
      to be as robust as possible.  */
-  DWORD err;
-  do
+  DWORD err = 0;
+  while (r && !*r)
     {
       static volatile ULONG pipe_unique_id;
       if (!name)
        __small_sprintf (pipename + len, "pipe-%p-%p", myself->pid,
                        InterlockedIncrement ((LONG *) &pipe_unique_id));
-      else
-       strcpy (pipename + len, name);
 
       debug_printf ("CreateNamedPipe: name %s, size %lu", pipename, psize);
 
-      err = 0;
       /* Use CreateNamedPipe instead of CreatePipe, because the latter
         returns a write handle that does not permit FILE_READ_ATTRIBUTES
         access, on versions of win32 earlier than WinXP SP2.
@@ -245,13 +246,14 @@ fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE& r,
         definitely required for pty handling since fhandler_pty_master
         writes to the pipe in chunks, terminated by newline when CANON mode
         is specified.  */
-      r = CreateNamedPipe (pipename, PIPE_ACCESS_INBOUND | overlapped,
+      *r = CreateNamedPipe (pipename, open_mode,
                           PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE, 1, psize,
                           psize, NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
 
-      if (r != INVALID_HANDLE_VALUE)
+      if (*r != INVALID_HANDLE_VALUE)
        {
-         debug_printf ("pipe read handle %p", r);
+         debug_printf ("pipe read handle %p", *r);
+         err = 0;
          break;
        }
 
@@ -261,43 +263,58 @@ fhandler_pipe::create_selectable (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE& r,
        case ERROR_PIPE_BUSY:
          /* The pipe is already open with compatible parameters.
             Pick a new name and retry.  */
-         debug_printf ("pipe busy", name ? ", retrying" : "");
+         debug_printf ("pipe busy", !name ? ", retrying" : "");
+         if (!*name)
+           *r = NULL;
          break;
        case ERROR_ACCESS_DENIED:
          /* The pipe is already open with incompatible parameters.
             Pick a new name and retry.  */
-         debug_printf ("pipe access denied%s", name ? ", retrying" : "");
+         debug_printf ("pipe access denied%s", !name ? ", retrying" : "");
+         if (!*name)
+           *r = NULL;
          break;
        default:
          {
            err = GetLastError ();
-           debug_printf ("CreatePipe failed, %E");
-           return err;
+           debug_printf ("failed, %E");
          }
        }
     }
-  while (!name);
 
   if (err)
-    return err;
-
-  debug_printf ("CreateFile: name %s", pipename);
-
-  /* Open the named pipe for writing.
-     Be sure to permit FILE_READ_ATTRIBUTES access.  */
-  w = CreateFile (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, sa_ptr,
-                 OPEN_EXISTING, overlapped, 0);
-
-  if (!w || w == INVALID_HANDLE_VALUE)
     {
-      /* Failure. */
-      DWORD err = GetLastError ();
-      debug_printf ("CreateFile failed, %E");
-      CloseHandle (r);
+      *r = NULL;
       return err;
     }
 
-  debug_printf ("pipe write handle %p", w);
+  if (!w)
+    debug_printf ("pipe write handle NULL");
+  else
+    {
+      debug_printf ("CreateFile: name %s", pipename);
+
+      /* Open the named pipe for writing.
+        Be sure to permit FILE_READ_ATTRIBUTES access.  */
+      DWORD access = GENERIC_WRITE | FILE_READ_ATTRIBUTES;
+      if ((open_mode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
+       access |= GENERIC_READ | FILE_WRITE_ATTRIBUTES;
+      *w = CreateFile (pipename, access, 0, sa_ptr, OPEN_EXISTING,
+                     open_mode & FILE_FLAG_OVERLAPPED, 0);
+
+      if (!*w || *w == INVALID_HANDLE_VALUE)
+       {
+         /* Failure. */
+         DWORD err = GetLastError ();
+         debug_printf ("CreateFile failed, %E");
+         if (r)
+           CloseHandle (*r);
+         *w = NULL;
+         return err;
+       }
+
+      debug_printf ("pipe write handle %p", *w);
+    }
 
   /* Success. */
   return 0;
@@ -310,7 +327,7 @@ fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode)
   SECURITY_ATTRIBUTES *sa = sec_none_cloexec (mode);
   int res = -1;
 
-  int ret = create_selectable (sa, r, w, psize);
+  int ret = create_selectable (sa, &r, &w, psize, NULL, FILE_FLAG_OVERLAPPED);
   if (ret)
     __seterrno_from_win_error (ret);
   else if ((fhs[0] = (fhandler_pipe *) build_fh_dev (*piper_dev)) == NULL)
index 0f7cc9a..095c5b5 100644 (file)
@@ -159,8 +159,8 @@ tty::not_allocated (HANDLE& r, HANDLE& w)
   char pipename[sizeof("ptyNNNN-from-master")];
   __small_sprintf (pipename, "pty%d-from-master", get_unit ());
   /* fhandler_pipe::create_selectable returns 0 when creation succeeds */
-  return fhandler_pipe::create_selectable (&sec_none, r, w, 128 * 1024,
-                                          pipename) == 0;
+  return fhandler_pipe::create_selectable (&sec_none, &r, &w, 128 * 1024,
+                                          pipename, 0) == 0;
 }
 
 bool