OSDN Git Service

Reintegrate socket duplication via WSADuplicateSocket/WSASocket.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / fhandler.h
index e3c8fd7..2755092 100644 (file)
@@ -1,7 +1,7 @@
 /* fhandler.h
 
    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-   2005, 2006, 2007 Red Hat, Inc.
+   2005, 2006, 2007, 2008, 2009 Red Hat, Inc.
 
 This file is part of Cygwin.
 
@@ -37,7 +37,9 @@ extern const char proc[];
 extern const int proc_len;
 
 class select_record;
+class select_stuff;
 class fhandler_disk_file;
+class inode_t;
 typedef struct __DIR DIR;
 struct dirent;
 struct iovec;
@@ -51,7 +53,11 @@ enum dirent_states
   dirent_saw_eof       = 0x0004,
   dirent_isroot                = 0x0008,
   dirent_set_d_ino     = 0x0010,
-  dirent_get_d_ino     = 0x0020
+  dirent_get_d_ino     = 0x0020,
+  dirent_nfs_d_ino     = 0x0040,
+
+  /* Global flags which must not be deleted on rewinddir or seekdir. */
+  dirent_info_mask     = 0x0078
 };
 
 enum conn_state
@@ -82,11 +88,17 @@ enum bg_check_types
 enum query_state {
   no_query = 0,
   query_read_control = 1,
-  query_stat_control = 2,
+  query_read_attributes = 2,
   query_write_control = 3,
   query_write_attributes = 4
 };
 
+enum del_lock_called_from {
+  on_close,
+  after_fork,
+  after_exec
+};
+
 class fhandler_base
 {
   friend class dtable;
@@ -100,7 +112,6 @@ class fhandler_base
     unsigned wbinset            : 1; /* binary write mode explicitly set */
     unsigned nohandle           : 1; /* No handle associated with fhandler. */
     unsigned uninterruptible_io : 1; /* Set if I/O should be uninterruptible. */
-    unsigned append_mode        : 1; /* always append */
     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
@@ -113,7 +124,7 @@ class fhandler_base
    public:
     status_flags () :
       rbinary (0), rbinset (0), wbinary (0), wbinset (0), nohandle (0),
-      uninterruptible_io (0), append_mode (0), did_lseek (0),
+      uninterruptible_io (0), did_lseek (0),
       query_open (no_query), close_on_exec (0), need_fork_fixup (0)
       {}
   } status, open_status;
@@ -122,7 +133,7 @@ class fhandler_base
   int access;
   HANDLE io_handle;
 
-  __ino64_t namehash;  /* hashed filename, used as inode num */
+  __ino64_t ino;       /* file ID or hashed filename, depends on FS. */
 
  protected:
   /* File open flags from open () and fcntl () calls */
@@ -134,15 +145,23 @@ class fhandler_base
   size_t raixput;
   size_t rabuflen;
 
-  DWORD fs_flags;
+  /* Used for advisory file locking.  See flock.cc.  */
+  long long unique_id;
+  void del_my_locks (del_lock_called_from);
+
   HANDLE read_state;
-  path_conv pc;
+  int wait_overlapped (bool, bool, DWORD *, DWORD = 0) __attribute__ ((regparm (3)));
+  bool setup_overlapped (bool doit = true) __attribute__ ((regparm (2)));
+  void destroy_overlapped () __attribute__ ((regparm (1)));
 
  public:
   class fhandler_base *archetype;
   int usecount;
 
+  path_conv pc;
+
   virtual void set_name (path_conv &pc);
+  virtual void set_name (const char *s) {pc.set_normalized_path (s);}
   int error () const {return pc.error;}
   void set_error (int error) {pc.error = error;}
   bool exists () const {return pc.exists ();}
@@ -190,7 +209,6 @@ class fhandler_base
   IMPLEMENT_STATUS_FLAG (bool, rbinset)
   IMPLEMENT_STATUS_FLAG (bool, nohandle)
   IMPLEMENT_STATUS_FLAG (bool, uninterruptible_io)
-  IMPLEMENT_STATUS_FLAG (bool, append_mode)
   IMPLEMENT_STATUS_FLAG (bool, did_lseek)
   IMPLEMENT_STATUS_FLAG (query_state, query_open)
   IMPLEMENT_STATUS_FLAG (bool, close_on_exec)
@@ -208,7 +226,7 @@ class fhandler_base
       return close_on_exec () ? &sec_none_nih : &sec_none;
   }
 
-  virtual void fixup_before_fork_exec (DWORD) {}
+  virtual int fixup_before_fork_exec (DWORD) { return 0; }
   virtual void fixup_after_fork (HANDLE);
   virtual void fixup_after_exec ();
   void create_read_state (LONG n)
@@ -222,10 +240,6 @@ class fhandler_base
     ReleaseSemaphore (read_state, n, NULL);
   }
 
-  void set_fs_flags (DWORD flags) { fs_flags = flags; }
-  bool get_fs_flags (DWORD flagval = UINT32_MAX)
-    { return (fs_flags & (flagval)); }
-
   bool get_readahead_valid () { return raixget < ralen; }
   int puts_readahead (const char *s, size_t len = (size_t) -1);
   int put_readahead (char value);
@@ -246,7 +260,9 @@ class fhandler_base
   bool has_attribute (DWORD x) const {return pc.has_attribute (x);}
   const char *get_name () const { return pc.normalized_path; }
   const char *get_win32_name () { return pc.get_win32 (); }
-    __ino64_t get_namehash () { return namehash ?: namehash = hash_path_name (0, get_win32_name ()); }
+  __dev32_t get_dev () { return pc.fs_serial_number (); }
+  __ino64_t get_ino () { return ino ?: ino = hash_path_name (0, pc.get_nt_native_path ()); }
+  long long get_unique_id () const { return unique_id; }
   /* Returns name used for /proc/<pid>/fd in buf. */
   virtual char *get_proc_fd_name (char *buf);
 
@@ -260,13 +276,14 @@ class fhandler_base
   virtual int open (int, mode_t = 0);
   int open_fs (int, mode_t = 0);
   virtual int close ();
-  int close_fs ();
+  int close_fs () { return fhandler_base::close (); }
   virtual int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
   int __stdcall fstat_fs (struct __stat64 *buf) __attribute__ ((regparm (2)));
   int __stdcall fstat_helper (struct __stat64 *buf,
                              FILETIME ftChangeTime,
                              FILETIME ftLastAccessTime,
                              FILETIME ftLastWriteTime,
+                             FILETIME ftCreationTime,
                              DWORD dwVolumeSerialNumber,
                              ULONGLONG nFileSize,
                              LONGLONG nAllocSize,
@@ -274,24 +291,31 @@ class fhandler_base
                              DWORD nNumberOfLinks,
                              DWORD dwFileAttributes)
     __attribute__ ((regparm (3)));
+  int __stdcall fstat_by_nfs_ea (struct __stat64 *buf) __attribute__ ((regparm (2)));
   int __stdcall fstat_by_handle (struct __stat64 *buf) __attribute__ ((regparm (2)));
   int __stdcall fstat_by_name (struct __stat64 *buf) __attribute__ ((regparm (2)));
-  int utimes_fs (const struct timeval *) __attribute__ ((regparm (2)));
+  virtual int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
+  int utimens_fs (const struct timespec *) __attribute__ ((regparm (2)));
   virtual int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
   virtual int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
   virtual int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
+  virtual ssize_t __stdcall fgetxattr (const char *, void *, size_t) __attribute__ ((regparm (3)));
+  virtual int __stdcall fsetxattr (const char *, const void *, size_t, int) __attribute__ ((regparm (3)));
   virtual int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3)));
   virtual int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
   virtual int __stdcall link (const char *) __attribute__ ((regparm (2)));
-  virtual int __stdcall utimes (const struct timeval *) __attribute__ ((regparm (2)));
+  virtual int __stdcall utimens (const struct timespec *) __attribute__ ((regparm (2)));
   virtual int __stdcall fsync () __attribute__ ((regparm (1)));
   virtual int ioctl (unsigned int cmd, void *);
   virtual int fcntl (int cmd, void *);
   virtual char const *ttyname () { return get_name (); }
   virtual void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
-  virtual int write (const void *ptr, size_t len);
-  virtual ssize_t readv (const struct iovec *, int iovcnt, ssize_t tot = -1);
-  virtual ssize_t writev (const struct iovec *, int iovcnt, ssize_t tot = -1);
+  virtual void __stdcall read_overlapped (void *ptr, size_t& len) __attribute__ ((regparm (3)));
+  virtual bool __stdcall has_ongoing_io (bool) __attribute__ ((regparm (2)));
+  virtual ssize_t __stdcall write (const void *ptr, size_t len);
+  virtual ssize_t __stdcall write_overlapped (const void *ptr, size_t len);
+  virtual ssize_t __stdcall readv (const struct iovec *, int iovcnt, ssize_t tot = -1);
+  virtual ssize_t __stdcall writev (const struct iovec *, int iovcnt, ssize_t tot = -1);
   virtual ssize_t __stdcall pread (void *, size_t, _off64_t) __attribute__ ((regparm (3)));
   virtual ssize_t __stdcall pwrite (void *, size_t, _off64_t) __attribute__ ((regparm (3)));
   virtual _off64_t lseek (_off64_t offset, int whence);
@@ -309,7 +333,7 @@ class fhandler_base
 
   void *operator new (size_t, void *p) __attribute__ ((nothrow)) {return p;}
 
-  virtual void init (HANDLE, DWORD, mode_t);
+  virtual int init (HANDLE, DWORD, mode_t);
 
   virtual int tcflush (int);
   virtual int tcsendbreak (int);
@@ -319,16 +343,21 @@ class fhandler_base
   virtual int tcgetattr (struct termios *t);
   virtual int tcsetpgrp (const pid_t pid);
   virtual int tcgetpgrp ();
-  virtual int is_tty () { return 0; }
-  virtual bool isdevice () { return true; }
-  virtual bool isfifo () { return false; }
+  virtual bool is_tty () const { return false; }
+  virtual bool ispipe () const { return false; }
+  virtual pid_t get_popen_pid () const {return 0;}
+  virtual bool isdevice () const { return true; }
+  virtual bool isfifo () const { return false; }
   virtual char *ptsname () { return NULL;}
   virtual class fhandler_socket *is_socket () { return NULL; }
   virtual class fhandler_console *is_console () { return 0; }
   virtual int is_windows () {return 0; }
 
-  virtual void raw_read (void *ptr, size_t& ulen);
-  virtual int raw_write (const void *ptr, size_t ulen);
+  virtual void __stdcall raw_read (void *ptr, size_t& ulen);
+  virtual ssize_t __stdcall raw_write (const void *ptr, size_t ulen);
+  virtual OVERLAPPED *get_overlapped () {return NULL;}
+  virtual OVERLAPPED *get_overlapped_buffer () {return NULL;}
+  virtual void set_overlapped (OVERLAPPED *) {}
 
   /* Virtual accessor functions to hide the fact
      that some fd's have two handles. */
@@ -336,9 +365,9 @@ class fhandler_base
   virtual HANDLE& get_io_handle () { return io_handle; }
   virtual HANDLE& get_output_handle () { return io_handle; }
   virtual bool hit_eof () {return false;}
-  virtual select_record *select_read (select_record *s);
-  virtual select_record *select_write (select_record *s);
-  virtual select_record *select_except (select_record *s);
+  virtual select_record *select_read (select_stuff *);
+  virtual select_record *select_write (select_stuff *);
+  virtual select_record *select_except (select_stuff *);
   virtual int ready_for_read (int fd, DWORD howlong);
   virtual const char *get_native_name ()
   {
@@ -351,34 +380,33 @@ class fhandler_base
     rabuf = NULL;
   }
   void operator delete (void *);
-  virtual HANDLE get_guard () const {return NULL;}
   virtual void set_eof () {}
   virtual int mkdir (mode_t mode);
   virtual int rmdir ();
-  virtual DIR *opendir ();
+  virtual DIR *opendir (int fd) __attribute__ ((regparm (2)));
   virtual int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
   virtual _off64_t telldir (DIR *);
   virtual void seekdir (DIR *, _off64_t);
   virtual void rewinddir (DIR *);
   virtual int closedir (DIR *);
-  virtual bool is_slow () {return 0;}
+  virtual bool is_slow () {return false;}
   bool is_auto_device () {return isdevice () && !dev ().isfs ();}
   bool is_fs_special () {return pc.is_fs_special ();}
   bool issymlink () {return pc.issymlink ();}
   bool device_access_denied (int) __attribute__ ((regparm (2)));
-  int fhaccess (int flags) __attribute__ ((regparm (2)));
-  friend class fhandler_fifo;
+  int fhaccess (int flags, bool) __attribute__ ((regparm (3)));
 };
 
 class fhandler_mailslot : public fhandler_base
 {
+  POBJECT_ATTRIBUTES get_object_attr (OBJECT_ATTRIBUTES &, PUNICODE_STRING);
  public:
   fhandler_mailslot ();
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   int ioctl (unsigned int cmd, void *);
-  select_record *select_read (select_record *s);
+  select_record *select_read (select_stuff *);
 };
 
 struct wsa_event
@@ -405,7 +433,7 @@ class fhandler_socket: public fhandler_base
   const HANDLE wsock_event () const { return wsock_evt; }
   const LONG serial_number () const { return wsock_events->serial_number; }
  private:
-  int wait_for_events (const long event_mask);
+  int wait_for_events (const long event_mask, bool dontwait = false);
   void release_events ();
 
   pid_t     sec_pid;
@@ -429,7 +457,23 @@ class fhandler_socket: public fhandler_base
   void af_local_set_sockpair_cred ();
 
  private:
+  int      _rmem;
+  int      _wmem;
+ public:
+  int &rmem () { return _rmem; }
+  int &wmem () { return _wmem; }
+  void rmem (int nrmem) { _rmem = nrmem; }
+  void wmem (int nwmem) { _wmem = nwmem; }
+
+ private:
+  struct _WSAPROTOCOL_INFOW *prot_info_ptr;
+ public:
+  void init_fixup_before ();
+  bool need_fixup_before () const {return prot_info_ptr != NULL;}
+
+ private:
   char *sun_path;
+  char *peer_sun_path;
   struct status_flags
   {
     unsigned async_io              : 1; /* async I/O */
@@ -467,17 +511,14 @@ class fhandler_socket: public fhandler_base
   int getpeereid (pid_t *pid, __uid32_t *euid, __gid32_t *egid);
 
   int open (int flags, mode_t mode = 0);
-  ssize_t readv (const struct iovec *, int iovcnt, ssize_t tot = -1);
-  inline ssize_t recv_internal (struct _WSABUF *wsabuf, DWORD wsacnt,
-                               DWORD flags,
-                               struct sockaddr *from, int *fromlen);
+  ssize_t __stdcall readv (const struct iovec *, int iovcnt, ssize_t tot = -1);
+  inline ssize_t recv_internal (struct _WSAMSG *wsamsg);
   ssize_t recvfrom (void *ptr, size_t len, int flags,
                    struct sockaddr *from, int *fromlen);
   ssize_t recvmsg (struct msghdr *msg, int flags);
 
-  ssize_t writev (const struct iovec *, int iovcnt, ssize_t tot = -1);
-  inline ssize_t send_internal (struct _WSABUF *wsabuf, DWORD wsacnt, int flags,
-                               const struct sockaddr *to, int tolen);
+  ssize_t __stdcall writev (const struct iovec *, int iovcnt, ssize_t tot = -1);
+  inline ssize_t send_internal (struct _WSAMSG *wsamsg, int flags);
   ssize_t sendto (const void *ptr, size_t len, int flags,
              const struct sockaddr *to, int tolen);
   ssize_t sendmsg (const struct msghdr *msg, int flags);
@@ -491,12 +532,14 @@ class fhandler_socket: public fhandler_base
   int dup (fhandler_base *child);
 
   void set_close_on_exec (bool val);
+  int fixup_before_fork_exec (DWORD);
   void fixup_after_fork (HANDLE);
+  void fixup_after_exec ();
   char *get_proc_fd_name (char *buf);
 
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
   int ready_for_read (int, DWORD) { return true; }
   void set_addr_family (int af) {addr_family = af;}
   int get_addr_family () {return addr_family;}
@@ -504,79 +547,89 @@ class fhandler_socket: public fhandler_base
   int get_socket_type () {return type;}
   void set_sun_path (const char *path);
   char *get_sun_path () {return sun_path;}
+  void set_peer_sun_path (const char *path);
+  char *get_peer_sun_path () {return peer_sun_path;}
 
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
+  int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
   int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
   int __stdcall link (const char *) __attribute__ ((regparm (2)));
-  bool is_slow () {return 1;}
+  bool is_slow () {return true;}
 };
 
 class fhandler_pipe: public fhandler_base
 {
-protected:
-  HANDLE guard;
-  bool broken_pipe;
-  HANDLE writepipe_exists;
-  DWORD orig_pid;
-  unsigned id;
 private:
   pid_t popen_pid;
+  OVERLAPPED io_status;
+  OVERLAPPED *overlapped;
 public:
   fhandler_pipe ();
+
+  OVERLAPPED *get_overlapped () {return overlapped;}
+  OVERLAPPED *get_overlapped_buffer () {return &io_status;}
+  void set_overlapped (OVERLAPPED *ov) {overlapped = ov;}
+
+  bool ispipe() const { return true; }
+
   void set_popen_pid (pid_t pid) {popen_pid = pid;}
   pid_t get_popen_pid () const {return popen_pid;}
   _off64_t lseek (_off64_t offset, int whence);
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
   char *get_proc_fd_name (char *buf);
-  void set_close_on_exec (bool val);
-  void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
+  void __stdcall raw_read (void *ptr, size_t& len);
+  ssize_t __stdcall raw_write (const void *, size_t);
   int open (int flags, mode_t mode = 0);
-  int close ();
-  void create_guard (SECURITY_ATTRIBUTES *sa)
-  {
-    guard = CreateMutex (sa, FALSE, NULL);
-    ProtectHandleINH (guard);
-  }
   int dup (fhandler_base *child);
   int ioctl (unsigned int cmd, void *);
+  int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
   int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3)));
   int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
-  void fixup_in_child ();
-  virtual void fixup_after_fork (HANDLE);
-  void fixup_after_exec ();
-  bool hit_eof ();
-  void set_eof () {broken_pipe = true;}
-  HANDLE get_guard () const {return guard;}
   int ready_for_read (int fd, DWORD howlong);
-  static int create (fhandler_pipe *[2], unsigned, int, bool = false);
-  bool is_slow () {return true;}
-  static int create_selectable (LPSECURITY_ATTRIBUTES, HANDLE&, HANDLE&, DWORD, bool);
+  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;
 };
 
-class fhandler_fifo: public fhandler_pipe
+class fhandler_fifo: public fhandler_base
 {
-  HANDLE output_handle;
-  long read_use;
-  long write_use;
-  virtual HANDLE& get_io_handle () { return io_handle ?: output_handle; }
+  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);
+  OVERLAPPED io_status;
+  bool wait (bool) __attribute__ ((regparm (1)));
+  char *fifo_name (char *) __attribute__ ((regparm (2)));
 public:
   fhandler_fifo ();
-  int open (int flags, mode_t mode = 0);
-  int open_not_mine (int flags) __attribute__ ((regparm (2)));
+  void __stdcall raw_read (void *, size_t&);
+  ssize_t __stdcall raw_write (const void *, size_t);
+  int open (int, mode_t);
   int close ();
-  void set_use (int flags) __attribute__ ((regparm (2)));
-  bool isfifo () { return true; }
-  HANDLE& get_output_handle () { return output_handle; }
-  void set_output_handle (HANDLE h) { output_handle = h; }
-  void set_use ();
   int dup (fhandler_base *child);
-  bool is_slow () {return true;}
-  void close_one_end ();
+  bool isfifo () const { return true; }
+  void set_close_on_exec (bool val);
+  int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
+  OVERLAPPED *get_overlapped () {return &io_status;}
+  OVERLAPPED *get_overlapped_buffer () {return &io_status;}
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
 };
 
 class fhandler_dev_raw: public fhandler_base
@@ -636,8 +689,8 @@ class fhandler_dev_floppy: public fhandler_dev_raw
 
   int open (int flags, mode_t mode = 0);
   int dup (fhandler_base *child);
-  void raw_read (void *ptr, size_t& ulen);
-  int raw_write (const void *ptr, size_t ulen);
+  void __stdcall raw_read (void *ptr, size_t& ulen);
+  ssize_t __stdcall raw_write (const void *ptr, size_t ulen);
   _off64_t lseek (_off64_t offset, int whence);
   int ioctl (unsigned int cmd, void *buf);
 };
@@ -660,8 +713,8 @@ class fhandler_dev_tape: public fhandler_dev_raw
   virtual int open (int flags, mode_t mode = 0);
   virtual int close ();
 
-  void raw_read (void *ptr, size_t& ulen);
-  int raw_write (const void *ptr, size_t ulen);
+  void __stdcall raw_read (void *ptr, size_t& ulen);
+  ssize_t __stdcall raw_write (const void *ptr, size_t ulen);
 
   virtual _off64_t lseek (_off64_t offset, int whence);
 
@@ -677,24 +730,26 @@ class fhandler_dev_tape: public fhandler_dev_raw
 
 class fhandler_disk_file: public fhandler_base
 {
-  int readdir_helper (DIR *, dirent *, DWORD, DWORD, char *) __attribute__ ((regparm (3)));
+  int readdir_helper (DIR *, dirent *, DWORD, DWORD, PUNICODE_STRING fname) __attribute__ ((regparm (3)));
 
  public:
   fhandler_disk_file ();
   fhandler_disk_file (path_conv &pc);
 
   int open (int flags, mode_t mode);
-  int close ();
   int lock (int, struct __flock64 *);
-  bool isdevice () { return false; }
+  bool isdevice () const { return false; }
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
   int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
+  ssize_t __stdcall fgetxattr (const char *, void *, size_t) __attribute__ ((regparm (3)));
+  int __stdcall fsetxattr (const char *, const void *, size_t, int) __attribute__ ((regparm (3)));
   int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3)));
   int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
   int __stdcall link (const char *) __attribute__ ((regparm (2)));
-  int __stdcall utimes (const struct timeval *) __attribute__ ((regparm (2)));
+  int __stdcall utimens (const struct timespec *) __attribute__ ((regparm (2)));
+  int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
 
   HANDLE mmap (caddr_t *addr, size_t len, int prot, int flags, _off64_t off);
   int munmap (HANDLE h, caddr_t addr, size_t len);
@@ -703,7 +758,7 @@ class fhandler_disk_file: public fhandler_base
                              _off64_t offset, DWORD size, void *address);
   int mkdir (mode_t mode);
   int rmdir ();
-  DIR *opendir ();
+  DIR *opendir (int fd) __attribute__ ((regparm (2)));
   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
   _off64_t telldir (DIR *);
   void seekdir (DIR *, _off64_t);
@@ -716,14 +771,19 @@ class fhandler_disk_file: public fhandler_base
 
 class fhandler_cygdrive: public fhandler_disk_file
 {
+  enum
+  {
+    DRVSZ = sizeof ("x:\\")
+  };
   int ndrives;
   const char *pdrive;
+  char pdrive_buf[1 + (2 * 26 * DRVSZ)];
   void set_drives ();
  public:
   fhandler_cygdrive ();
   int open (int flags, mode_t mode);
   int close ();
-  DIR *opendir ();
+  DIR *opendir (int fd) __attribute__ ((regparm (2)));
   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
   void rewinddir (DIR *);
   int closedir (DIR *);
@@ -749,11 +809,11 @@ class fhandler_serial: public fhandler_base
 
   int open (int flags, mode_t mode);
   int close ();
-  void init (HANDLE h, DWORD a, mode_t flags);
+  int init (HANDLE h, DWORD a, mode_t flags);
   void overlapped_setup ();
   int dup (fhandler_base *child);
-  void raw_read (void *ptr, size_t& ulen);
-  int raw_write (const void *ptr, size_t ulen);
+  void __stdcall raw_read (void *ptr, size_t& ulen);
+  ssize_t __stdcall raw_write (const void *ptr, size_t ulen);
   int tcsendbreak (int);
   int tcdrain ();
   int tcflow (int);
@@ -763,7 +823,7 @@ class fhandler_serial: public fhandler_base
   int tcgetattr (struct termios *t);
   _off64_t lseek (_off64_t, int) { return 0; }
   int tcflush (int);
-  int is_tty () { return 1; }
+  bool is_tty () const { return true; }
   void fixup_after_fork (HANDLE parent);
   void fixup_after_exec ();
 
@@ -772,17 +832,17 @@ class fhandler_serial: public fhandler_base
      permission checking on pgrps.  */
   virtual int tcgetpgrp () { return pgrp_; }
   virtual int tcsetpgrp (const pid_t pid) { pgrp_ = pid; return 0; }
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
-  bool is_slow () {return 1;}
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
+  bool is_slow () {return true;}
 };
 
 #define acquire_output_mutex(ms) \
-  __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms);
+  __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms)
 
 #define release_output_mutex() \
-  __release_output_mutex (__PRETTY_FUNCTION__, __LINE__);
+  __release_output_mutex (__PRETTY_FUNCTION__, __LINE__)
 
 class tty;
 class tty_min;
@@ -803,7 +863,7 @@ class fhandler_termios: public fhandler_base
   line_edit_status line_edit (const char *rptr, int nread, termios&);
   void set_output_handle (HANDLE h) { output_handle = h; }
   void tcinit (tty_min *this_tc, bool force = false);
-  virtual int is_tty () { return 1; }
+  bool is_tty () const { return true; }
   int tcgetpgrp ();
   int tcsetpgrp (int pid);
   bg_check_types bg_check (int sig);
@@ -883,8 +943,9 @@ class dev_console
   bool use_mouse;
   bool raw_win32_keyboard_mode;
 
-  bool con_to_str (char *d, const char *s, DWORD sz);
-  bool str_to_con (char *d, const char *s, DWORD sz);
+  inline UINT get_console_cp ();
+  DWORD con_to_str (char *d, int dlen, WCHAR w);
+  DWORD str_to_con (mbtowc_p, const char *, PWCHAR d, const char *s, DWORD sz);
   void set_color (HANDLE);
   bool fillin_info (HANDLE);
   void set_default_attr ();
@@ -896,9 +957,18 @@ class dev_console
 class fhandler_console: public fhandler_termios
 {
  private:
+  static const unsigned MAX_WRITE_CHARS;
   static dev_console *dev_state;
   static bool invisible_console;
 
+  /* Used when we encounter a truncated multi-byte sequence.  The
+     lead bytes are stored here and revisited in the next write call. */
+  struct {
+    int len;
+    unsigned char buf[4]; /* Max len of valid UTF-8 sequence. */
+  } trunc_buf;
+  PWCHAR write_buf;
+
 /* Output calls */
   void set_default_attr ();
 
@@ -907,6 +977,8 @@ class fhandler_console: public fhandler_termios
   void cursor_set (bool, int, int);
   void cursor_get (int *, int *);
   void cursor_rel (int, int);
+  inline void write_replacement_char ();
+  inline bool write_console (PWCHAR, DWORD, DWORD&);
   const unsigned char *write_normal (unsigned const char*, unsigned const char *);
   void char_command (char);
   bool set_raw_win32_keyboard_mode (bool);
@@ -916,6 +988,8 @@ class fhandler_console: public fhandler_termios
   int igncr_enabled ();
   int input_tcsetattr (int a, const struct termios *t);
   void set_cursor_maybe ();
+  static bool create_invisible_console (HWINSTA);
+  static bool create_invisible_console_workaround ();
 
  public:
   fhandler_console ();
@@ -924,9 +998,9 @@ class fhandler_console: public fhandler_termios
 
   int open (int flags, mode_t mode = 0);
 
-  int write (const void *ptr, size_t len);
-  void doecho (const void *str, DWORD len) { (void) write (str, len); }
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
+  ssize_t __stdcall write (const void *ptr, size_t len);
+  void doecho (const void *str, DWORD len) { (void) write (str, len); }
   int close ();
 
   int tcflush (int);
@@ -937,12 +1011,12 @@ class fhandler_console: public fhandler_termios
   int dup (fhandler_base *child);
 
   int ioctl (unsigned int cmd, void *);
-  void init (HANDLE, DWORD, mode_t);
+  int init (HANDLE, DWORD, mode_t);
   bool mouse_aware () {return dev_state->use_mouse;}
 
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
   void fixup_after_fork_exec (bool);
   void fixup_after_exec () {fixup_after_fork_exec (true);}
   void fixup_after_fork (HANDLE) {fixup_after_fork_exec (false);}
@@ -950,7 +1024,7 @@ class fhandler_console: public fhandler_termios
   void set_input_state ();
   void send_winch_maybe ();
   static tty_min *get_tty_stuff (int);
-  bool is_slow () {return 1;}
+  bool is_slow () {return true;}
   static bool need_invisible ();
   static bool has_a () {return !invisible_console;}
 };
@@ -982,10 +1056,10 @@ class fhandler_tty_common: public fhandler_termios
   int close ();
   _off64_t lseek (_off64_t, int);
   void set_close_on_exec (bool val);
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
-  bool is_slow () {return 1;}
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
+  bool is_slow () {return true;}
 };
 
 class fhandler_tty_slave: public fhandler_tty_common
@@ -996,9 +1070,9 @@ class fhandler_tty_slave: public fhandler_tty_common
   fhandler_tty_slave ();
 
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
-  void init (HANDLE, DWORD, mode_t);
+  int init (HANDLE, DWORD, mode_t);
 
   int tcsetattr (int a, const struct termios *t);
   int tcgetattr (struct termios *t);
@@ -1009,7 +1083,7 @@ class fhandler_tty_slave: public fhandler_tty_common
   void fixup_after_fork (HANDLE parent);
   void fixup_after_exec ();
 
-  select_record *select_read (select_record *s);
+  select_record *select_read (select_stuff *);
   int cygserver_attach_tty (HANDLE*, HANDLE*);
   int get_unit ();
   virtual char const *ttyname () { return pc.dev.name; }
@@ -1029,7 +1103,7 @@ public:
   void doecho (const void *str, DWORD len);
   int accept_input ();
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   int close ();
 
@@ -1046,6 +1120,7 @@ public:
   int dup (fhandler_base *);
   void fixup_after_fork (HANDLE parent);
   void fixup_after_exec ();
+  int tcgetpgrp ();
 };
 
 class fhandler_tty_master: public fhandler_pty_master
@@ -1058,7 +1133,7 @@ class fhandler_tty_master: public fhandler_pty_master
   int init ();
   int init_console ();
   void set_winsize (bool);
-  bool is_slow () {return 1;}
+  bool is_slow () {return true;}
 };
 
 class fhandler_dev_null: public fhandler_base
@@ -1066,9 +1141,9 @@ class fhandler_dev_null: public fhandler_base
  public:
   fhandler_dev_null ();
 
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
 };
 
 class fhandler_dev_zero: public fhandler_base
@@ -1076,7 +1151,7 @@ class fhandler_dev_zero: public fhandler_base
  public:
   fhandler_dev_zero ();
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   _off64_t lseek (_off64_t offset, int whence);
 
@@ -1094,6 +1169,7 @@ class fhandler_dev_random: public fhandler_base
  protected:
   HCRYPTPROV crypt_prov;
   long pseudo;
+  _off64_t dummy_offset;
 
   bool crypt_gen_random (void *ptr, size_t len);
   int pseudo_write (const void *ptr, size_t len);
@@ -1102,7 +1178,7 @@ class fhandler_dev_random: public fhandler_base
  public:
   fhandler_dev_random ();
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   _off64_t lseek (_off64_t offset, int whence);
   int close ();
@@ -1120,7 +1196,7 @@ class fhandler_dev_mem: public fhandler_base
   ~fhandler_dev_mem ();
 
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t ulen);
+  ssize_t __stdcall write (const void *ptr, size_t ulen);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   _off64_t lseek (_off64_t offset, int whence);
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
@@ -1143,7 +1219,7 @@ class fhandler_dev_clipboard: public fhandler_base
   fhandler_dev_clipboard ();
   int is_windows () { return 1; }
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   _off64_t lseek (_off64_t offset, int whence);
   int close ();
@@ -1161,7 +1237,7 @@ class fhandler_windows: public fhandler_base
   fhandler_windows ();
   int is_windows () { return 1; }
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   int ioctl (unsigned int cmd, void *);
   _off64_t lseek (_off64_t, int) { return 0; }
@@ -1169,10 +1245,10 @@ class fhandler_windows: public fhandler_base
 
   void set_close_on_exec (bool val);
   void fixup_after_fork (HANDLE parent);
-  select_record *select_read (select_record *s);
-  select_record *select_write (select_record *s);
-  select_record *select_except (select_record *s);
-  bool is_slow () {return 1;}
+  select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
+  bool is_slow () {return true;}
 };
 
 class fhandler_dev_dsp: public fhandler_base
@@ -1192,7 +1268,7 @@ class fhandler_dev_dsp: public fhandler_base
   fhandler_dev_dsp ();
 
   int open (int flags, mode_t mode = 0);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   int ioctl (unsigned int cmd, void *);
   _off64_t lseek (_off64_t, int);
@@ -1209,7 +1285,6 @@ class fhandler_virtual : public fhandler_base
 {
  protected:
   char *filebuf;
-  size_t bufalloc;
   _off64_t filesize;
   _off64_t position;
   int fileid; // unique within each class
@@ -1219,18 +1294,19 @@ class fhandler_virtual : public fhandler_base
   virtual ~fhandler_virtual();
 
   virtual int exists();
-  virtual DIR *opendir ();
+  DIR *opendir (int fd) __attribute__ ((regparm (2)));
   _off64_t telldir (DIR *);
   void seekdir (DIR *, _off64_t);
   void rewinddir (DIR *);
   int closedir (DIR *);
-  int write (const void *ptr, size_t len);
+  ssize_t __stdcall write (const void *ptr, size_t len);
   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
   _off64_t lseek (_off64_t, int);
   int dup (fhandler_base *child);
   int open (int flags, mode_t mode = 0);
   int close ();
   int __stdcall fstat (struct stat *buf) __attribute__ ((regparm (2)));
+  int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
   int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
@@ -1285,6 +1361,7 @@ class fhandler_registry: public fhandler_proc
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
   bool fill_filebuf ();
   int close ();
+  int dup (fhandler_base *child);
 };
 
 class pinfo;
@@ -1294,7 +1371,7 @@ class fhandler_process: public fhandler_proc
  public:
   fhandler_process ();
   int exists();
-  DIR *opendir ();
+  DIR *opendir (int fd) __attribute__ ((regparm (2)));
   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
   int open (int flags, mode_t mode = 0);
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
@@ -1307,7 +1384,6 @@ class fhandler_procnet: public fhandler_proc
  public:
   fhandler_procnet ();
   int exists();
-  DIR *opendir ();
   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
   int open (int flags, mode_t mode = 0);
   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
@@ -1359,57 +1435,4 @@ typedef union
   char __virtual[sizeof (fhandler_virtual)];
   char __windows[sizeof (fhandler_windows)];
 } fhandler_union;
-
-struct select_record
-{
-  int fd;
-  HANDLE h;
-  fhandler_base *fh;
-  int thread_errno;
-  bool windows_handle;
-  bool read_ready, write_ready, except_ready;
-  bool read_selected, write_selected, except_selected;
-  bool except_on_write;
-  int (*startup) (select_record *me, class select_stuff *stuff);
-  int (*peek) (select_record *, bool);
-  int (*verify) (select_record *me, fd_set *readfds, fd_set *writefds,
-                fd_set *exceptfds);
-  void (*cleanup) (select_record *me, class select_stuff *stuff);
-  struct select_record *next;
-  void set_select_errno () {__seterrno (); thread_errno = errno;}
-  int saw_error () {return thread_errno;}
-
-  select_record (fhandler_base *in_fh = NULL) : fd (0), h (NULL),
-                fh (in_fh), thread_errno (0), windows_handle (false),
-                read_ready (false), write_ready (false), except_ready (false),
-                read_selected (false), write_selected (false),
-                except_selected (false), except_on_write (false),
-                startup (NULL), peek (NULL), verify (NULL), cleanup (NULL),
-                next (NULL) {}
-};
-
-class select_stuff
-{
- public:
-  ~select_stuff ();
-  bool always_ready, windows_used;
-  select_record start;
-  void *device_specific_pipe;
-  void *device_specific_socket;
-  void *device_specific_serial;
-  void *device_specific_mailslot;
-
-  int test_and_set (int i, fd_set *readfds, fd_set *writefds,
-                    fd_set *exceptfds);
-  int poll (fd_set *readfds, fd_set *writefds, fd_set *exceptfds);
-  int wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds, DWORD ms);
-  void cleanup ();
-  select_stuff (): always_ready (0), windows_used (0), start (0),
-                  device_specific_pipe (0),
-                  device_specific_socket (0),
-                  device_specific_serial (0),
-                  device_specific_mailslot (0) {}
-};
-
-void __stdcall set_console_state_for_spawn (bool);
 #endif /* _FHANDLER_H_ */