OSDN Git Service

* fhandler.h (fhandler_proc::opendir): Declare.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / fhandler.h
1 /* fhandler.h
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4    2005, 2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #ifndef _FHANDLER_H_
13 #define _FHANDLER_H_
14
15 #include "tty.h"
16 /* fcntl flags used only internaly. */
17 #define O_NOSYMLINK 0x080000
18 #define O_DIROPEN   0x100000
19
20 /* newlib used to define O_NDELAY differently from O_NONBLOCK.  Now it
21    properly defines both to be the same.  Unfortunately, we have to
22    behave properly the old version, too, to accommodate older executables. */
23 #define OLD_O_NDELAY    (CYGWIN_VERSION_CHECK_FOR_OLD_O_NONBLOCK ? 4 : 0)
24
25 /* Care for the old O_NDELAY flag. If one of the flags is set,
26    both flags are set. */
27 #define O_NONBLOCK_MASK (O_NONBLOCK | OLD_O_NDELAY)
28
29 /* It appears that 64K is the block size used for buffered I/O on NT.
30    Using this blocksize in read/write calls in the application results
31    in a much better performance than using smaller values. */
32 #define PREFERRED_IO_BLKSIZE ((blksize_t) 65536)
33
34 /* It also appears that this may be the only acceptable block size for
35    atomic writes to a pipe.  It is a shame that we have to make this
36    so small.  http://cygwin.com/ml/cygwin/2011-03/msg00541.html  */
37 #define DEFAULT_PIPEBUFSIZE PREFERRED_IO_BLKSIZE
38
39 extern const char *windows_device_names[];
40 extern struct __cygwin_perfile *perfile_table;
41 #define __fmode (*(user_data->fmode_ptr))
42 extern const char proc[];
43 extern const size_t proc_len;
44 extern const char procsys[];
45 extern const size_t procsys_len;
46
47 class select_record;
48 class select_stuff;
49 class fhandler_disk_file;
50 class inode_t;
51 typedef struct __DIR DIR;
52 struct dirent;
53 struct iovec;
54 struct __acl32;
55
56 enum dirent_states
57 {
58   dirent_ok             = 0x0000,
59   dirent_saw_dot        = 0x0001,
60   dirent_saw_dot_dot    = 0x0002,
61   dirent_saw_eof        = 0x0004,
62   dirent_isroot         = 0x0008,
63   dirent_set_d_ino      = 0x0010,
64   dirent_get_d_ino      = 0x0020,
65   dirent_nfs_d_ino      = 0x0040,
66
67   /* Global flags which must not be deleted on rewinddir or seekdir. */
68   dirent_info_mask      = 0x0078
69 };
70
71 enum conn_state
72 {
73   unconnected = 0,
74   connect_pending = 1,
75   connected = 2,
76   connect_failed = 3
77 };
78
79 enum line_edit_status
80 {
81   line_edit_ok = 0,
82   line_edit_input_done = 1,
83   line_edit_signalled = 2,
84   line_edit_error = 3,
85   line_edit_pipe_full = 4
86 };
87
88 enum bg_check_types
89 {
90   bg_error = -1,
91   bg_eof = 0,
92   bg_ok = 1,
93   bg_signalled = 2
94 };
95
96 enum query_state {
97   no_query = 0,
98   query_read_control = 1,
99   query_read_attributes = 2,
100   query_write_control = 3,
101   query_write_dac = 4,
102   query_write_attributes = 5
103 };
104
105 enum del_lock_called_from {
106   on_close,
107   after_fork,
108   after_exec
109 };
110
111 enum virtual_ftype_t {
112   virt_blk = -7,        /* Block special */
113   virt_chr = -6,        /* Character special */
114   virt_fsfile = -5,     /* FS-based file via /proc/sys */
115   virt_socket = -4,     /* Socket */
116   virt_pipe = -3,       /* Pipe */
117   virt_symlink = -2,    /* Symlink */
118   virt_file = -1,       /* Regular file */
119   virt_none = 0,        /* Invalid, Error */
120   virt_directory = 1,   /* Directory */
121   virt_rootdir = 2,     /* Root directory of virtual FS */
122   virt_fsdir = 3,       /* FS-based directory via /proc/sys */
123 };
124
125 class fhandler_base
126 {
127   friend class dtable;
128   friend void close_all_files (bool);
129
130   struct status_flags
131   {
132     unsigned rbinary            : 1; /* binary read mode */
133     unsigned rbinset            : 1; /* binary read mode explicitly set */
134     unsigned wbinary            : 1; /* binary write mode */
135     unsigned wbinset            : 1; /* binary write mode explicitly set */
136     unsigned nohandle           : 1; /* No handle associated with fhandler. */
137     unsigned did_lseek          : 1; /* set when lseek is called as a flag that
138                                         _write should check if we've moved
139                                         beyond EOF, zero filling or making
140                                         file sparse if so. */
141     unsigned query_open         : 3; /* open file without requesting either
142                                         read or write access */
143     unsigned close_on_exec      : 1; /* close-on-exec */
144     unsigned need_fork_fixup    : 1; /* Set if need to fixup after fork. */
145
146    public:
147     status_flags () :
148       rbinary (0), rbinset (0), wbinary (0), wbinset (0), nohandle (0),
149       did_lseek (0), query_open (no_query), close_on_exec (0),
150       need_fork_fixup (0)
151       {}
152   } status, open_status;
153
154  private:
155   ACCESS_MASK access;
156   ULONG options;
157
158   HANDLE io_handle;
159
160   __ino64_t ino;        /* file ID or hashed filename, depends on FS. */
161
162  protected:
163   /* File open flags from open () and fcntl () calls */
164   int openflags;
165
166   char *rabuf;          /* used for crlf conversion in text files */
167   size_t ralen;
168   size_t raixget;
169   size_t raixput;
170   size_t rabuflen;
171
172   /* Used for advisory file locking.  See flock.cc.  */
173   long long unique_id;
174   void del_my_locks (del_lock_called_from);
175
176   HANDLE read_state;
177
178  public:
179   class fhandler_base *archetype;
180   int usecount;
181
182   path_conv pc;
183
184   virtual bool use_archetype () const {return false;}
185   virtual void set_name (path_conv &pc);
186   virtual void set_name (const char *s)
187   {
188     pc.set_normalized_path (s);
189     pc.set_path (s);
190   }
191   int error () const {return pc.error;}
192   void set_error (int error) {pc.error = error;}
193   bool exists () const {return pc.exists ();}
194   int pc_binmode () const {return pc.binmode ();}
195   device& dev () {return pc.dev;}
196   operator DWORD& () {return (DWORD&) pc;}
197   virtual size_t size () const {return sizeof (*this);}
198
199   virtual fhandler_base& operator =(fhandler_base &x);
200   fhandler_base ();
201   virtual ~fhandler_base ();
202
203   /* Non-virtual simple accessor functions. */
204   void set_io_handle (HANDLE x) { io_handle = x; }
205
206   DWORD& get_device () { return dev (); }
207   DWORD get_major () { return dev ().get_major (); }
208   DWORD get_minor () { return dev ().get_minor (); }
209   virtual int get_unit () { return dev ().get_minor (); }
210
211   ACCESS_MASK get_access () const { return access; }
212   void set_access (ACCESS_MASK x) { access = x; }
213
214   ULONG get_options () const { return options; }
215   void set_options (ULONG x) { options = x; }
216
217   int get_flags () { return openflags; }
218   void set_flags (int x, int supplied_bin = 0);
219
220   bool is_nonblocking ();
221   void set_nonblocking (int);
222
223   bool wbinary () const { return status.wbinset ? status.wbinary : 1; }
224   bool rbinary () const { return status.rbinset ? status.rbinary : 1; }
225
226   void wbinary (bool b) {status.wbinary = b; status.wbinset = 1;}
227   void rbinary (bool b) {status.rbinary = b; status.rbinset = 1;}
228
229   void set_open_status () {open_status = status;}
230   void reset_to_open_binmode ()
231   {
232     set_flags ((get_flags () & ~(O_TEXT | O_BINARY))
233                | ((open_status.wbinary || open_status.rbinary)
234                    ? O_BINARY : O_TEXT));
235   }
236
237   IMPLEMENT_STATUS_FLAG (bool, wbinset)
238   IMPLEMENT_STATUS_FLAG (bool, rbinset)
239   IMPLEMENT_STATUS_FLAG (bool, nohandle)
240   IMPLEMENT_STATUS_FLAG (bool, did_lseek)
241   IMPLEMENT_STATUS_FLAG (query_state, query_open)
242   IMPLEMENT_STATUS_FLAG (bool, close_on_exec)
243   IMPLEMENT_STATUS_FLAG (bool, need_fork_fixup)
244
245   int get_default_fmode (int flags);
246
247   virtual void set_close_on_exec (bool val);
248
249   LPSECURITY_ATTRIBUTES get_inheritance (bool all = 0)
250   {
251     if (all)
252       return close_on_exec () ? &sec_all_nih : &sec_all;
253     else
254       return close_on_exec () ? &sec_none_nih : &sec_none;
255   }
256
257   virtual int fixup_before_fork_exec (DWORD) { return 0; }
258   virtual void fixup_after_fork (HANDLE);
259   virtual void fixup_after_exec ();
260   void create_read_state (LONG n)
261   {
262     read_state = CreateSemaphore (&sec_none_nih, 0, n, NULL);
263     ProtectHandle (read_state);
264   }
265
266   void signal_read_state (LONG n)
267   {
268     ReleaseSemaphore (read_state, n, NULL);
269   }
270
271   bool get_readahead_valid () { return raixget < ralen; }
272   int puts_readahead (const char *s, size_t len = (size_t) -1);
273   int put_readahead (char value);
274
275   int get_readahead ();
276   int peek_readahead (int queryput = 0);
277
278   int eat_readahead (int n);
279
280   void set_readahead_valid (int val, int ch = -1);
281
282   int get_readahead_into_buffer (char *buf, size_t buflen);
283
284   bool has_acls () const { return pc.has_acls (); }
285
286   bool isremote () { return pc.isremote (); }
287
288   bool has_attribute (DWORD x) const {return pc.has_attribute (x);}
289   const char *get_name () const { return pc.normalized_path; }
290   const char *get_win32_name () { return pc.get_win32 (); }
291   __dev32_t get_dev () { return pc.fs_serial_number (); }
292   __ino64_t get_ino () { return ino ?: ino = hash_path_name (0, pc.get_nt_native_path ()); }
293   long long get_unique_id () const { return unique_id; }
294   /* Returns name used for /proc/<pid>/fd in buf. */
295   virtual char *get_proc_fd_name (char *buf);
296
297   virtual void hclose (HANDLE h) {CloseHandle (h);}
298   virtual void set_no_inheritance (HANDLE &, bool);
299
300   /* fixup fd possibly non-inherited handles after fork */
301   bool fork_fixup (HANDLE, HANDLE &, const char *);
302   virtual bool need_fixup_before () const {return false;}
303
304   int open_with_arch (int, mode_t = 0);
305   virtual int open (int, mode_t);
306   virtual void open_setup (int flags) { return; }
307
308   int close_with_arch ();
309   virtual int close ();
310   virtual void cleanup () { return; }
311   int _archetype_usecount (const char *fn, int ln, int n)
312   {
313     if (!archetype)
314       return 0;
315     archetype->usecount += n;
316     if (strace.active ())
317       strace.prntf (_STRACE_ALL, fn, "line %d:  %s<%p> usecount + %d = %d", ln, get_name (), archetype, n, archetype->usecount);
318     return archetype->usecount;
319   }
320
321   int open_fs (int, mode_t = 0);
322 # define archetype_usecount(n) _archetype_usecount (__PRETTY_FUNCTION__, __LINE__, (n))
323   int close_fs () { return fhandler_base::close (); }
324   virtual int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
325   int __stdcall fstat_fs (struct __stat64 *buf) __attribute__ ((regparm (2)));
326 private:
327   int __stdcall fstat_helper (struct __stat64 *buf,
328                               DWORD nNumberOfLinks)
329                 __attribute__ ((regparm (3)));
330   int __stdcall fstat_by_nfs_ea (struct __stat64 *buf) __attribute__ ((regparm (2)));
331   int __stdcall fstat_by_handle (struct __stat64 *buf) __attribute__ ((regparm (2)));
332   int __stdcall fstat_by_name (struct __stat64 *buf) __attribute__ ((regparm (2)));
333 public:
334   virtual int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
335   int utimens_fs (const struct timespec *) __attribute__ ((regparm (2)));
336   virtual int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
337   virtual int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
338   virtual int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
339   virtual ssize_t __stdcall fgetxattr (const char *, void *, size_t) __attribute__ ((regparm (3)));
340   virtual int __stdcall fsetxattr (const char *, const void *, size_t, int) __attribute__ ((regparm (3)));
341   virtual int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3)));
342   virtual int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
343   virtual int __stdcall link (const char *) __attribute__ ((regparm (2)));
344   virtual int __stdcall utimens (const struct timespec *) __attribute__ ((regparm (2)));
345   virtual int __stdcall fsync () __attribute__ ((regparm (1)));
346   virtual int ioctl (unsigned int cmd, void *);
347   virtual int fcntl (int cmd, void *);
348   virtual char const *ttyname () { return get_name (); }
349   virtual void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
350   virtual ssize_t __stdcall write (const void *ptr, size_t len);
351   virtual ssize_t __stdcall readv (const struct iovec *, int iovcnt, ssize_t tot = -1);
352   virtual ssize_t __stdcall writev (const struct iovec *, int iovcnt, ssize_t tot = -1);
353   virtual ssize_t __stdcall pread (void *, size_t, _off64_t) __attribute__ ((regparm (3)));
354   virtual ssize_t __stdcall pwrite (void *, size_t, _off64_t) __attribute__ ((regparm (3)));
355   virtual _off64_t lseek (_off64_t offset, int whence);
356   virtual int lock (int, struct __flock64 *);
357   virtual int dup (fhandler_base *child);
358   virtual int fpathconf (int);
359
360   virtual HANDLE mmap (caddr_t *addr, size_t len, int prot,
361                        int flags, _off64_t off);
362   virtual int munmap (HANDLE h, caddr_t addr, size_t len);
363   virtual int msync (HANDLE h, caddr_t addr, size_t len, int flags);
364   virtual bool fixup_mmap_after_fork (HANDLE h, int prot, int flags,
365                                       _off64_t offset, DWORD size,
366                                       void *address);
367
368   void *operator new (size_t, void *p) __attribute__ ((nothrow)) {return p;}
369
370   virtual int init (HANDLE, DWORD, mode_t);
371
372   virtual int tcflush (int);
373   virtual int tcsendbreak (int);
374   virtual int tcdrain ();
375   virtual int tcflow (int);
376   virtual int tcsetattr (int a, const struct termios *t);
377   virtual int tcgetattr (struct termios *t);
378   virtual int tcsetpgrp (const pid_t pid);
379   virtual int tcgetpgrp ();
380   virtual bool is_tty () const { return false; }
381   virtual bool ispipe () const { return false; }
382   virtual pid_t get_popen_pid () const {return 0;}
383   virtual bool isdevice () const { return true; }
384   virtual bool isfifo () const { return false; }
385   virtual char *ptsname () { return NULL;}
386   virtual class fhandler_socket *is_socket () { return NULL; }
387   virtual class fhandler_console *is_console () { return 0; }
388   virtual int is_windows () {return 0; }
389
390   virtual void __stdcall raw_read (void *ptr, size_t& ulen) __attribute__ ((regparm (3)));
391   virtual ssize_t __stdcall raw_write (const void *ptr, size_t ulen) __attribute__ ((regparm (3)));
392
393   /* Virtual accessor functions to hide the fact
394      that some fd's have two handles. */
395   virtual HANDLE& get_handle () { return io_handle; }
396   virtual HANDLE& get_io_handle () { return io_handle; }
397   virtual HANDLE& get_output_handle () { return io_handle; }
398   virtual HANDLE get_stat_handle () { return pc.handle () ?: io_handle; }
399   virtual bool hit_eof () {return false;}
400   virtual select_record *select_read (select_stuff *);
401   virtual select_record *select_write (select_stuff *);
402   virtual select_record *select_except (select_stuff *);
403   virtual const char *get_native_name ()
404   {
405     return dev ().native;
406   }
407   virtual bg_check_types bg_check (int) {return bg_ok;}
408   void clear_readahead ()
409   {
410     raixput = raixget = ralen = rabuflen = 0;
411     rabuf = NULL;
412   }
413   void operator delete (void *);
414   virtual void set_eof () {}
415   virtual int mkdir (mode_t mode);
416   virtual int rmdir ();
417   virtual DIR *opendir (int fd) __attribute__ ((regparm (2)));
418   virtual int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
419   virtual long telldir (DIR *);
420   virtual void seekdir (DIR *, long);
421   virtual void rewinddir (DIR *);
422   virtual int closedir (DIR *);
423   bool is_auto_device () {return isdevice () && !dev ().isfs ();}
424   bool is_fs_special () {return pc.is_fs_special ();}
425   bool issymlink () {return pc.issymlink ();}
426   bool device_access_denied (int) __attribute__ ((regparm (2)));
427   int fhaccess (int flags, bool) __attribute__ ((regparm (3)));
428   virtual bool __stdcall has_ongoing_io () __attribute__ ((regparm (1))) {return false;}
429 };
430
431 struct wsa_event
432 {
433   LONG serial_number;
434   long events;
435   int  connect_errorcode;
436   pid_t owner;
437 };
438
439 class fhandler_socket: public fhandler_base
440 {
441  private:
442   int addr_family;
443   int type;
444   int connect_secret[4];
445
446   wsa_event *wsock_events;
447   HANDLE wsock_mtx;
448   HANDLE wsock_evt;
449  public:
450   bool init_events ();
451   int evaluate_events (const long event_mask, long &events, const bool erase);
452   const HANDLE wsock_event () const { return wsock_evt; }
453   const LONG serial_number () const { return wsock_events->serial_number; }
454  private:
455   int wait_for_events (const long event_mask, const DWORD flags);
456   void release_events ();
457
458   pid_t     sec_pid;
459   __uid32_t sec_uid;
460   __gid32_t sec_gid;
461   pid_t     sec_peer_pid;
462   __uid32_t sec_peer_uid;
463   __gid32_t sec_peer_gid;
464   void af_local_set_secret (char *);
465   void af_local_setblocking (bool &, bool &);
466   void af_local_unsetblocking (bool, bool);
467   void af_local_set_cred ();
468   void af_local_copy (fhandler_socket *);
469   bool af_local_recv_secret ();
470   bool af_local_send_secret ();
471   bool af_local_recv_cred ();
472   bool af_local_send_cred ();
473   int af_local_accept ();
474  public:
475   int af_local_connect ();
476   void af_local_set_sockpair_cred ();
477
478  private:
479   int       _rmem;
480   int       _wmem;
481  public:
482   int &rmem () { return _rmem; }
483   int &wmem () { return _wmem; }
484   void rmem (int nrmem) { _rmem = nrmem; }
485   void wmem (int nwmem) { _wmem = nwmem; }
486
487  private:
488   struct _WSAPROTOCOL_INFOW *prot_info_ptr;
489  public:
490   void init_fixup_before ();
491   bool need_fixup_before () const {return prot_info_ptr != NULL;}
492
493  private:
494   char *sun_path;
495   char *peer_sun_path;
496   struct status_flags
497   {
498     unsigned async_io              : 1; /* async I/O */
499     unsigned saw_shutdown_read     : 1; /* Socket saw a SHUT_RD */
500     unsigned saw_shutdown_write    : 1; /* Socket saw a SHUT_WR */
501     unsigned saw_reuseaddr         : 1; /* Socket saw SO_REUSEADDR call */
502     unsigned listener              : 1; /* listen called */
503     unsigned connect_state         : 2;
504    public:
505     status_flags () :
506       async_io (0), saw_shutdown_read (0), saw_shutdown_write (0),
507       listener (0), connect_state (unconnected)
508       {}
509   } status;
510
511  public:
512   fhandler_socket ();
513   ~fhandler_socket ();
514   int get_socket () { return (int) get_handle(); }
515   fhandler_socket *is_socket () { return this; }
516
517   IMPLEMENT_STATUS_FLAG (bool, async_io)
518   IMPLEMENT_STATUS_FLAG (bool, saw_shutdown_read)
519   IMPLEMENT_STATUS_FLAG (bool, saw_shutdown_write)
520   IMPLEMENT_STATUS_FLAG (bool, saw_reuseaddr)
521   IMPLEMENT_STATUS_FLAG (bool, listener)
522   IMPLEMENT_STATUS_FLAG (conn_state, connect_state)
523
524   int bind (const struct sockaddr *name, int namelen);
525   int connect (const struct sockaddr *name, int namelen);
526   int listen (int backlog);
527   int accept4 (struct sockaddr *peer, int *len, int flags);
528   int getsockname (struct sockaddr *name, int *namelen);
529   int getpeername (struct sockaddr *name, int *namelen);
530   int getpeereid (pid_t *pid, __uid32_t *euid, __gid32_t *egid);
531
532   int open (int flags, mode_t mode = 0);
533   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
534   ssize_t __stdcall readv (const struct iovec *, int iovcnt, ssize_t tot = -1);
535   inline ssize_t recv_internal (struct _WSAMSG *wsamsg);
536   ssize_t recvfrom (void *ptr, size_t len, int flags,
537                     struct sockaddr *from, int *fromlen);
538   ssize_t recvmsg (struct msghdr *msg, int flags);
539
540   ssize_t __stdcall write (const void *ptr, size_t len);
541   ssize_t __stdcall writev (const struct iovec *, int iovcnt, ssize_t tot = -1);
542   inline ssize_t send_internal (struct _WSAMSG *wsamsg, int flags);
543   ssize_t sendto (const void *ptr, size_t len, int flags,
544               const struct sockaddr *to, int tolen);
545   ssize_t sendmsg (const struct msghdr *msg, int flags);
546
547   int ioctl (unsigned int cmd, void *);
548   int fcntl (int cmd, void *);
549   _off64_t lseek (_off64_t, int) { return 0; }
550   int shutdown (int how);
551   int close ();
552   void hclose (HANDLE) {close ();}
553   int dup (fhandler_base *child);
554
555   void set_close_on_exec (bool val);
556   int fixup_before_fork_exec (DWORD);
557   void fixup_after_fork (HANDLE);
558   void fixup_after_exec ();
559   char *get_proc_fd_name (char *buf);
560
561   select_record *select_read (select_stuff *);
562   select_record *select_write (select_stuff *);
563   select_record *select_except (select_stuff *);
564   void set_addr_family (int af) {addr_family = af;}
565   int get_addr_family () {return addr_family;}
566   void set_socket_type (int st) { type = st;}
567   int get_socket_type () {return type;}
568   void set_sun_path (const char *path);
569   char *get_sun_path () {return sun_path;}
570   void set_peer_sun_path (const char *path);
571   char *get_peer_sun_path () {return peer_sun_path;}
572
573   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
574   int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
575   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
576   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
577   int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
578   int __stdcall link (const char *) __attribute__ ((regparm (2)));
579   size_t size () const { return sizeof (*this);}
580 };
581
582 class fhandler_base_overlapped: public fhandler_base
583 {
584 protected:
585   enum wait_return
586   {
587     overlapped_unknown = 0,
588     overlapped_success,
589     overlapped_signal,
590     overlapped_nonblocking_no_data,
591     overlapped_error
592   };
593   bool io_pending;
594   OVERLAPPED io_status;
595   OVERLAPPED *overlapped;
596   size_t max_atomic_write;
597 public:
598   wait_return __stdcall wait_overlapped (bool, bool, DWORD *, bool, DWORD = 0) __attribute__ ((regparm (3)));
599   int __stdcall setup_overlapped () __attribute__ ((regparm (1)));
600   void __stdcall destroy_overlapped () __attribute__ ((regparm (1)));
601   virtual void __stdcall raw_read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
602   virtual ssize_t __stdcall raw_write (const void *ptr, size_t len) __attribute__ ((regparm (3)));
603   OVERLAPPED *&get_overlapped () {return overlapped;}
604   OVERLAPPED *get_overlapped_buffer () {return &io_status;}
605   void set_overlapped (OVERLAPPED *ov) {overlapped = ov;}
606   fhandler_base_overlapped (): io_pending (false), overlapped (NULL), max_atomic_write (0)
607   {
608     memset (&io_status, 0, sizeof io_status);
609   }
610   bool __stdcall has_ongoing_io () __attribute__ ((regparm (1)));
611
612   void fixup_after_fork (HANDLE);
613   void fixup_after_exec ();
614
615   int close ();
616   int dup (fhandler_base *child);
617   virtual size_t size () const { return sizeof (*this);}        /* probably not needed */
618 };
619
620 class fhandler_pipe: public fhandler_base_overlapped
621 {
622 private:
623   pid_t popen_pid;
624 public:
625   fhandler_pipe ();
626
627
628   bool ispipe() const { return true; }
629
630   void set_popen_pid (pid_t pid) {popen_pid = pid;}
631   pid_t get_popen_pid () const {return popen_pid;}
632   _off64_t lseek (_off64_t offset, int whence);
633   select_record *select_read (select_stuff *);
634   select_record *select_write (select_stuff *);
635   select_record *select_except (select_stuff *);
636   char *get_proc_fd_name (char *buf);
637   int open (int flags, mode_t mode = 0);
638   int dup (fhandler_base *child);
639   int ioctl (unsigned int cmd, void *);
640   int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
641   int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3)));
642   int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
643   int init (HANDLE, DWORD, mode_t);
644   static int create (fhandler_pipe *[2], unsigned, int);
645   static int create_selectable (LPSECURITY_ATTRIBUTES, HANDLE&, HANDLE&, DWORD, const char * = NULL);
646   friend class fhandler_fifo;
647   size_t size () const { return sizeof (*this);}
648 };
649
650 class fhandler_fifo: public fhandler_base_overlapped
651 {
652   enum fifo_state
653   {
654     fifo_unknown,
655     fifo_wait_for_client,
656     fifo_wait_for_server,
657     fifo_wait_for_next_client,
658     fifo_eof,
659     fifo_error,
660     fifo_eintr,
661     fifo_ok
662   };
663   fifo_state wait_state;
664   HANDLE dummy_client;
665   HANDLE open_nonserver (const char *, unsigned, LPSECURITY_ATTRIBUTES);
666   bool wait (bool) __attribute__ ((regparm (1)));
667   char *fifo_name (char *) __attribute__ ((regparm (2)));
668 public:
669   fhandler_fifo ();
670   void __stdcall raw_read (void *, size_t&) __attribute__ ((regparm (3)));
671   ssize_t __stdcall raw_write (const void *, size_t) __attribute__ ((regparm (3)));
672   int open (int, mode_t);
673   int close ();
674   int dup (fhandler_base *child);
675   bool isfifo () const { return true; }
676   void set_close_on_exec (bool val);
677   int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
678   select_record *select_read (select_stuff *);
679   select_record *select_write (select_stuff *);
680   select_record *select_except (select_stuff *);
681   size_t size () const { return sizeof (*this);}
682 };
683
684 class fhandler_mailslot : public fhandler_base_overlapped
685 {
686   POBJECT_ATTRIBUTES get_object_attr (OBJECT_ATTRIBUTES &, PUNICODE_STRING, int);
687  public:
688   fhandler_mailslot ();
689   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
690   int open (int flags, mode_t mode = 0);
691   ssize_t __stdcall raw_write (const void *, size_t) __attribute__ ((regparm (3)));
692   int ioctl (unsigned int cmd, void *);
693   select_record *select_read (select_stuff *);
694   size_t size () const { return sizeof (*this);}
695 };
696
697 class fhandler_dev_raw: public fhandler_base
698 {
699  protected:
700   char *devbuf;
701   size_t devbufsiz;
702   size_t devbufstart;
703   size_t devbufend;
704   struct status_flags
705   {
706     unsigned lastblk_to_read : 1;
707    public:
708     status_flags () : lastblk_to_read (0) {}
709   } status;
710
711   IMPLEMENT_STATUS_FLAG (bool, lastblk_to_read)
712
713   fhandler_dev_raw ();
714
715  public:
716   ~fhandler_dev_raw ();
717
718   int open (int flags, mode_t mode = 0);
719
720   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
721
722   int dup (fhandler_base *child);
723   int ioctl (unsigned int cmd, void *buf);
724
725   void fixup_after_fork (HANDLE);
726   void fixup_after_exec ();
727   size_t size () const { return sizeof (*this);}
728 };
729
730 #define MAX_PARTITIONS 15
731
732 struct part_t
733 {
734   LONG refcnt;
735   HANDLE hdl[MAX_PARTITIONS];
736 };
737
738 class fhandler_dev_floppy: public fhandler_dev_raw
739 {
740  private:
741   _off64_t drive_size;
742   unsigned long bytes_per_sector;
743   part_t *partitions;
744   struct status_flags
745   {
746     unsigned eom_detected    : 1;
747    public:
748     status_flags () : eom_detected (0) {}
749   } status;
750
751   IMPLEMENT_STATUS_FLAG (bool, eom_detected)
752
753   inline _off64_t get_current_position ();
754   int get_drive_info (struct hd_geometry *geo);
755
756   int lock_partition (DWORD to_write);
757
758   BOOL write_file (const void *buf, DWORD to_write, DWORD *written, int *err);
759   BOOL read_file (void *buf, DWORD to_read, DWORD *read, int *err);
760
761  public:
762   fhandler_dev_floppy ();
763
764   int open (int flags, mode_t mode = 0);
765   int close ();
766   int dup (fhandler_base *child);
767   void __stdcall raw_read (void *ptr, size_t& ulen) __attribute__ ((regparm (3)));
768   ssize_t __stdcall raw_write (const void *ptr, size_t ulen) __attribute__ ((regparm (3)));
769   _off64_t lseek (_off64_t offset, int whence);
770   int ioctl (unsigned int cmd, void *buf);
771   size_t size () const { return sizeof (*this);}
772 };
773
774 class fhandler_dev_tape: public fhandler_dev_raw
775 {
776   HANDLE mt_mtx;
777   HANDLE mt_evt;
778
779   bool is_rewind_device () { return get_minor () < 128; }
780   unsigned int driveno () { return (unsigned int) get_minor () & 0x7f; }
781   void drive_init ();
782
783   inline bool _lock (bool);
784   inline int unlock (int ret = 0);
785
786  public:
787   fhandler_dev_tape ();
788
789   int open (int flags, mode_t mode = 0);
790   virtual int close ();
791
792   void __stdcall raw_read (void *ptr, size_t& ulen) __attribute__ ((regparm (3)));
793   ssize_t __stdcall raw_write (const void *ptr, size_t ulen) __attribute__ ((regparm (3)));
794
795   virtual _off64_t lseek (_off64_t offset, int whence);
796
797   virtual int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
798
799   virtual int dup (fhandler_base *child);
800   virtual void fixup_after_fork (HANDLE parent);
801   virtual void set_close_on_exec (bool val);
802   virtual int ioctl (unsigned int cmd, void *buf);
803   size_t size () const { return sizeof (*this);}
804 };
805
806 /* Standard disk file */
807
808 class fhandler_disk_file: public fhandler_base
809 {
810   HANDLE prw_handle;
811   int readdir_helper (DIR *, dirent *, DWORD, DWORD, PUNICODE_STRING fname) __attribute__ ((regparm (3)));
812
813   int prw_open (bool);
814
815  public:
816   fhandler_disk_file ();
817   fhandler_disk_file (path_conv &pc);
818
819   int open (int flags, mode_t mode);
820   int close ();
821   int dup (fhandler_base *child);
822   void fixup_after_fork (HANDLE parent);
823   int lock (int, struct __flock64 *);
824   bool isdevice () const { return false; }
825   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
826   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
827   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
828   int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
829   ssize_t __stdcall fgetxattr (const char *, void *, size_t) __attribute__ ((regparm (3)));
830   int __stdcall fsetxattr (const char *, const void *, size_t, int) __attribute__ ((regparm (3)));
831   int __stdcall fadvise (_off64_t, _off64_t, int) __attribute__ ((regparm (3)));
832   int __stdcall ftruncate (_off64_t, bool) __attribute__ ((regparm (3)));
833   int __stdcall link (const char *) __attribute__ ((regparm (2)));
834   int __stdcall utimens (const struct timespec *) __attribute__ ((regparm (2)));
835   int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
836
837   HANDLE mmap (caddr_t *addr, size_t len, int prot, int flags, _off64_t off);
838   int munmap (HANDLE h, caddr_t addr, size_t len);
839   int msync (HANDLE h, caddr_t addr, size_t len, int flags);
840   bool fixup_mmap_after_fork (HANDLE h, int prot, int flags,
841                               _off64_t offset, DWORD size, void *address);
842   int mkdir (mode_t mode);
843   int rmdir ();
844   DIR *opendir (int fd) __attribute__ ((regparm (2)));
845   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
846   long telldir (DIR *);
847   void seekdir (DIR *, long);
848   void rewinddir (DIR *);
849   int closedir (DIR *);
850
851   ssize_t __stdcall pread (void *, size_t, _off64_t) __attribute__ ((regparm (3)));
852   ssize_t __stdcall pwrite (void *, size_t, _off64_t) __attribute__ ((regparm (3)));
853   size_t size () const { return sizeof (*this);}
854 };
855
856 class fhandler_cygdrive: public fhandler_disk_file
857 {
858   enum
859   {
860     DRVSZ = sizeof ("x:\\")
861   };
862   int ndrives;
863   const char *pdrive;
864   char pdrive_buf[1 + (2 * 26 * DRVSZ)];
865   void set_drives ();
866  public:
867   fhandler_cygdrive ();
868   int open (int flags, mode_t mode);
869   int close ();
870   DIR *opendir (int fd) __attribute__ ((regparm (2)));
871   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
872   void rewinddir (DIR *);
873   int closedir (DIR *);
874   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
875   size_t size () const { return sizeof (*this);}
876 };
877
878 class fhandler_serial: public fhandler_base
879 {
880  private:
881   size_t vmin_;                         /* from termios */
882   unsigned int vtime_;                  /* from termios */
883   pid_t pgrp_;
884   int rts;                              /* for Windows 9x purposes only */
885   int dtr;                              /* for Windows 9x purposes only */
886
887  public:
888   int overlapped_armed;
889   OVERLAPPED io_status;
890   DWORD ev;
891
892   /* Constructor */
893   fhandler_serial ();
894
895   int open (int flags, mode_t mode);
896   int close ();
897   int init (HANDLE h, DWORD a, mode_t flags);
898   void overlapped_setup ();
899   int dup (fhandler_base *child);
900   void __stdcall raw_read (void *ptr, size_t& ulen) __attribute__ ((regparm (3)));
901   ssize_t __stdcall raw_write (const void *ptr, size_t ulen) __attribute__ ((regparm (3)));
902   int tcsendbreak (int);
903   int tcdrain ();
904   int tcflow (int);
905   int ioctl (unsigned int cmd, void *);
906   int switch_modem_lines (int set, int clr);
907   int tcsetattr (int a, const struct termios *t);
908   int tcgetattr (struct termios *t);
909   _off64_t lseek (_off64_t, int) { return 0; }
910   int tcflush (int);
911   bool is_tty () const { return true; }
912   void fixup_after_fork (HANDLE parent);
913   void fixup_after_exec ();
914
915   /* We maintain a pgrp so that tcsetpgrp and tcgetpgrp work, but we
916      don't use it for permissions checking.  fhandler_pty_slave does
917      permission checking on pgrps.  */
918   virtual int tcgetpgrp () { return pgrp_; }
919   virtual int tcsetpgrp (const pid_t pid) { pgrp_ = pid; return 0; }
920   select_record *select_read (select_stuff *);
921   select_record *select_write (select_stuff *);
922   select_record *select_except (select_stuff *);
923   size_t size () const { return sizeof (*this);}
924 };
925
926 #define acquire_output_mutex(ms) \
927   __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms)
928
929 #define release_output_mutex() \
930   __release_output_mutex (__PRETTY_FUNCTION__, __LINE__)
931
932 class tty;
933 class tty_min;
934 class fhandler_termios: public fhandler_base
935 {
936  protected:
937   HANDLE output_handle;
938   virtual void doecho (const void *, DWORD) {};
939   virtual int accept_input () {return 1;};
940  public:
941   tty_min *_tc;
942   virtual tty_min *tc () const {return _tc; }
943   fhandler_termios () :
944   fhandler_base ()
945   {
946     need_fork_fixup (true);
947   }
948   HANDLE& get_output_handle () { return output_handle; }
949   line_edit_status line_edit (const char *rptr, int nread, termios&);
950   void set_output_handle (HANDLE h) { output_handle = h; }
951   void tcinit (bool force);
952   bool is_tty () const { return true; }
953   tty *get_ttyp () { return (tty *) tc (); }
954   void sigflush ();
955   int tcgetpgrp ();
956   int tcsetpgrp (int pid);
957   bg_check_types bg_check (int sig);
958   virtual DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms) {return 1;}
959   virtual void __release_output_mutex (const char *fn, int ln) {}
960   void echo_erase (int force = 0);
961   virtual _off64_t lseek (_off64_t, int);
962   virtual size_t size () const { return sizeof (*this);}        /* probably not needed */
963 };
964
965 enum ansi_intensity
966 {
967   INTENSITY_INVISIBLE,
968   INTENSITY_DIM,
969   INTENSITY_NORMAL,
970   INTENSITY_BOLD
971 };
972
973 #define normal 0
974 #define gotesc 1
975 #define gotsquare 2
976 #define gotarg1 3
977 #define gotrsquare 4
978 #define gotcommand 5
979 #define gettitle 6
980 #define eattitle 7
981 #define gotparen 8
982 #define gotrparen 9
983 #define MAXARGS 10
984
985 class dev_console
986 {
987   WORD default_color, underline_color, dim_color;
988
989   /* Used to determine if an input keystroke should be modified with META. */
990   int meta_mask;
991
992 /* Output state */
993   int state_;
994   int args_[MAXARGS];
995   int nargs_;
996   unsigned rarg;
997   bool saw_question_mark;
998   bool saw_greater_than_sign;
999   bool vt100_graphics_mode_G0;
1000   bool vt100_graphics_mode_G1;
1001   bool iso_2022_G1;
1002   bool alternate_charset_active;
1003   bool metabit;
1004   char backspace_keycode;
1005
1006   char my_title_buf [TITLESIZE + 1];
1007
1008   WORD current_win32_attr;
1009   ansi_intensity intensity;
1010   bool underline, blink, reverse;
1011   WORD fg, bg;
1012
1013   /* saved cursor coordinates */
1014   int savex, savey;
1015
1016   /* saved screen */
1017   COORD savebufsiz;
1018   PCHAR_INFO savebuf;
1019
1020   struct
1021     {
1022       short Top, Bottom;
1023     } scroll_region;
1024   struct
1025     {
1026       SHORT winTop;
1027       SHORT winBottom;
1028       COORD dwWinSize;
1029       COORD dwBufferSize;
1030       COORD dwCursorPosition;
1031       WORD wAttributes;
1032     } info;
1033
1034   COORD dwLastCursorPosition;
1035   COORD dwMousePosition;        /* scroll-adjusted coord of mouse event */
1036   COORD dwLastMousePosition;    /* scroll-adjusted coord of previous mouse event */
1037   DWORD dwLastButtonState;      /* (not noting mouse wheel events) */
1038   int last_button_code;         /* transformed mouse report button code */
1039   int nModifiers;
1040
1041   bool insert_mode;
1042   int use_mouse;
1043   bool use_focus;
1044   bool raw_win32_keyboard_mode;
1045
1046   inline UINT get_console_cp ();
1047   DWORD con_to_str (char *d, int dlen, WCHAR w);
1048   DWORD str_to_con (mbtowc_p, const char *, PWCHAR d, const char *s, DWORD sz);
1049   void set_color (HANDLE);
1050   bool fillin_info (HANDLE);
1051   void set_default_attr ();
1052
1053   friend class fhandler_console;
1054 };
1055
1056 /* This is a input and output console handle */
1057 class fhandler_console: public fhandler_termios
1058 {
1059 public:
1060   struct console_state
1061   {
1062     tty_min tty_min_state;
1063     dev_console dev_state;
1064   };
1065 private:
1066   static const unsigned MAX_WRITE_CHARS;
1067   static console_state *shared_console_info;
1068   static bool invisible_console;
1069
1070   /* Used when we encounter a truncated multi-byte sequence.  The
1071      lead bytes are stored here and revisited in the next write call. */
1072   struct {
1073     int len;
1074     unsigned char buf[4]; /* Max len of valid UTF-8 sequence. */
1075   } trunc_buf;
1076   PWCHAR write_buf;
1077
1078 /* Output calls */
1079   void set_default_attr ();
1080
1081   void clear_screen (int, int, int, int);
1082   void scroll_screen (int, int, int, int, int, int);
1083   void cursor_set (bool, int, int);
1084   void cursor_get (int *, int *);
1085   void cursor_rel (int, int);
1086   inline void write_replacement_char ();
1087   inline bool write_console (PWCHAR, DWORD, DWORD&);
1088   const unsigned char *write_normal (unsigned const char*, unsigned const char *);
1089   void char_command (char);
1090   bool set_raw_win32_keyboard_mode (bool);
1091   int output_tcsetattr (int a, const struct termios *t);
1092
1093 /* Input calls */
1094   int igncr_enabled ();
1095   int input_tcsetattr (int a, const struct termios *t);
1096   void set_cursor_maybe ();
1097   static bool create_invisible_console (HWINSTA);
1098   static bool create_invisible_console_workaround ();
1099   static console_state *open_shared_console (HWND, HANDLE&, bool&);
1100   tty_min *tc () const {return &(shared_console_info->tty_min_state);}
1101
1102  public:
1103   fhandler_console (fh_devices);
1104   static console_state *open_shared_console (HWND hw, HANDLE& h)
1105   {
1106     bool createit = false;
1107     return open_shared_console (hw, h, createit);
1108   }
1109
1110   fhandler_console* is_console () { return this; }
1111
1112   bool use_archetype () const {return true;}
1113
1114   int open (int flags, mode_t mode);
1115   void open_setup (int flags);
1116
1117   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1118   ssize_t __stdcall write (const void *ptr, size_t len);
1119   void doecho (const void *str, DWORD len) { (void) write (str, len); }
1120   int close ();
1121
1122   int tcflush (int);
1123   int tcsetattr (int a, const struct termios *t);
1124   int tcgetattr (struct termios *t);
1125
1126   int ioctl (unsigned int cmd, void *);
1127   int init (HANDLE, DWORD, mode_t);
1128   bool mouse_aware (MOUSE_EVENT_RECORD& mouse_event);
1129   bool focus_aware () {return shared_console_info->dev_state.use_focus;}
1130
1131   select_record *select_read (select_stuff *);
1132   select_record *select_write (select_stuff *);
1133   select_record *select_except (select_stuff *);
1134   void fixup_after_fork_exec (bool);
1135   void fixup_after_exec () {fixup_after_fork_exec (true);}
1136   void fixup_after_fork (HANDLE) {fixup_after_fork_exec (false);}
1137   void set_close_on_exec (bool val);
1138   void set_input_state ();
1139   void send_winch_maybe ();
1140   void get_tty_stuff ();
1141   bool set_unit ();
1142   static bool need_invisible ();
1143   static bool has_a () {return !invisible_console;}
1144   size_t size () const { return sizeof (*this);}
1145   friend tty_min * tty_list::get_cttyp ();
1146 };
1147
1148 class fhandler_pty_common: public fhandler_termios
1149 {
1150  public:
1151   fhandler_pty_common ()
1152     : fhandler_termios (),
1153       output_mutex (NULL),
1154     input_mutex (NULL), input_available_event (NULL)
1155   {
1156     pc.file_attributes (FILE_ATTRIBUTE_NORMAL);
1157   }
1158   HANDLE output_mutex, input_mutex;
1159   HANDLE input_available_event;
1160
1161   DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms);
1162   void __release_output_mutex (const char *fn, int ln);
1163
1164   int close ();
1165   _off64_t lseek (_off64_t, int);
1166   void set_close_on_exec (bool val);
1167   select_record *select_read (select_stuff *);
1168   select_record *select_write (select_stuff *);
1169   select_record *select_except (select_stuff *);
1170   virtual size_t size () const { return sizeof (*this);}        /* probably not needed */
1171 };
1172
1173 class fhandler_pty_slave: public fhandler_pty_common
1174 {
1175   HANDLE inuse;                 // used to indicate that a tty is in use
1176
1177   /* Helper functions for fchmod and fchown. */
1178   bool fch_open_handles ();
1179   int fch_set_sd (security_descriptor &sd, bool chown);
1180   void fch_close_handles ();
1181
1182  public:
1183   /* Constructor */
1184   fhandler_pty_slave (int);
1185
1186   bool use_archetype () const {return true;}
1187   int open (int flags, mode_t mode = 0);
1188   void open_setup (int flags);
1189   ssize_t __stdcall write (const void *ptr, size_t len);
1190   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1191   int init (HANDLE, DWORD, mode_t);
1192
1193   int tcsetattr (int a, const struct termios *t);
1194   int tcgetattr (struct termios *t);
1195   int tcflush (int);
1196   int ioctl (unsigned int cmd, void *);
1197   int close ();
1198   void cleanup ();
1199   int dup (fhandler_base *child);
1200   void fixup_after_fork (HANDLE parent);
1201   void fixup_after_exec ();
1202
1203   select_record *select_read (select_stuff *);
1204   int get_unit ();
1205   virtual char const *ttyname () { return pc.dev.name; }
1206   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1207   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
1208   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
1209   size_t size () const { return sizeof (*this);}
1210 };
1211
1212 class fhandler_pty_master: public fhandler_pty_common
1213 {
1214   int pktmode;                  // non-zero if pty in a packet mode.
1215   HANDLE master_ctl;            // Control socket for handle duplication
1216   cygthread *master_thread;     // Master control thread
1217
1218 public:
1219   int need_nl;                  // Next read should start with \n
1220   DWORD dwProcessId;            // Owner of master handles
1221   HANDLE from_master, to_master;
1222
1223   /* Constructor */
1224   fhandler_pty_master ();
1225
1226   virtual bool use_archetype () const {return true;}
1227   DWORD pty_master_thread ();
1228   int process_slave_output (char *buf, size_t len, int pktmode_on);
1229   void doecho (const void *str, DWORD len);
1230   int accept_input ();
1231   int open (int flags, mode_t mode = 0);
1232   ssize_t __stdcall write (const void *ptr, size_t len);
1233   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1234   int close ();
1235   void cleanup ();
1236
1237   int tcsetattr (int a, const struct termios *t);
1238   int tcgetattr (struct termios *t);
1239   int tcflush (int);
1240   int ioctl (unsigned int cmd, void *);
1241
1242   char *ptsname ();
1243
1244   bool hit_eof ();
1245   bool setup ();
1246   int dup (fhandler_base *);
1247   void fixup_after_fork (HANDLE parent);
1248   void fixup_after_exec ();
1249   int tcgetpgrp ();
1250   size_t size () const { return sizeof (*this);}
1251 };
1252
1253 class fhandler_dev_null: public fhandler_base
1254 {
1255  public:
1256   fhandler_dev_null ();
1257
1258   select_record *select_read (select_stuff *);
1259   select_record *select_write (select_stuff *);
1260   select_record *select_except (select_stuff *);
1261   size_t size () const { return sizeof (*this);}
1262 };
1263
1264 class fhandler_dev_zero: public fhandler_base
1265 {
1266  public:
1267   fhandler_dev_zero ();
1268   int open (int flags, mode_t mode = 0);
1269   ssize_t __stdcall write (const void *ptr, size_t len);
1270   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1271   _off64_t lseek (_off64_t offset, int whence);
1272
1273   virtual HANDLE mmap (caddr_t *addr, size_t len, int prot,
1274                        int flags, _off64_t off);
1275   virtual int munmap (HANDLE h, caddr_t addr, size_t len);
1276   virtual int msync (HANDLE h, caddr_t addr, size_t len, int flags);
1277   virtual bool fixup_mmap_after_fork (HANDLE h, int prot, int flags,
1278                                       _off64_t offset, DWORD size,
1279                                       void *address);
1280   size_t size () const { return sizeof (*this);}
1281 };
1282
1283 class fhandler_dev_random: public fhandler_base
1284 {
1285  protected:
1286   HCRYPTPROV crypt_prov;
1287   long pseudo;
1288   _off64_t dummy_offset;
1289
1290   bool crypt_gen_random (void *ptr, size_t len);
1291   int pseudo_write (const void *ptr, size_t len);
1292   int pseudo_read (void *ptr, size_t len);
1293
1294  public:
1295   fhandler_dev_random ();
1296   int open (int flags, mode_t mode = 0);
1297   ssize_t __stdcall write (const void *ptr, size_t len);
1298   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1299   _off64_t lseek (_off64_t offset, int whence);
1300   int close ();
1301   int dup (fhandler_base *child);
1302   size_t size () const { return sizeof (*this);}
1303 };
1304
1305 class fhandler_dev_mem: public fhandler_base
1306 {
1307  protected:
1308   DWORD mem_size;
1309   _off64_t pos;
1310
1311  public:
1312   fhandler_dev_mem ();
1313   ~fhandler_dev_mem ();
1314
1315   int open (int flags, mode_t mode = 0);
1316   ssize_t __stdcall write (const void *ptr, size_t ulen);
1317   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1318   _off64_t lseek (_off64_t offset, int whence);
1319   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1320
1321   HANDLE mmap (caddr_t *addr, size_t len, int prot, int flags, _off64_t off);
1322   int munmap (HANDLE h, caddr_t addr, size_t len);
1323   int msync (HANDLE h, caddr_t addr, size_t len, int flags);
1324   bool fixup_mmap_after_fork (HANDLE h, int prot, int flags,
1325                               _off64_t offset, DWORD size, void *address);
1326   size_t size () const { return sizeof (*this);}
1327 };
1328
1329 class fhandler_dev_clipboard: public fhandler_base
1330 {
1331   _off64_t pos;
1332   void *membuffer;
1333   size_t msize;
1334   bool eof;
1335  public:
1336   fhandler_dev_clipboard ();
1337   int is_windows () { return 1; }
1338   int open (int flags, mode_t mode = 0);
1339   ssize_t __stdcall write (const void *ptr, size_t len);
1340   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1341   _off64_t lseek (_off64_t offset, int whence);
1342   int close ();
1343
1344   int dup (fhandler_base *child);
1345   void fixup_after_exec ();
1346   size_t size () const { return sizeof (*this);}
1347 };
1348
1349 class fhandler_windows: public fhandler_base
1350 {
1351  private:
1352   HWND hWnd_;   // the window whose messages are to be retrieved by read() call
1353   int method_;  // write method (Post or Send)
1354  public:
1355   fhandler_windows ();
1356   int is_windows () { return 1; }
1357   int open (int flags, mode_t mode = 0);
1358   ssize_t __stdcall write (const void *ptr, size_t len);
1359   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1360   int ioctl (unsigned int cmd, void *);
1361   _off64_t lseek (_off64_t, int) { return 0; }
1362   int close () { return 0; }
1363
1364   void set_close_on_exec (bool val);
1365   void fixup_after_fork (HANDLE parent);
1366   select_record *select_read (select_stuff *);
1367   select_record *select_write (select_stuff *);
1368   select_record *select_except (select_stuff *);
1369   size_t size () const { return sizeof (*this);}
1370 };
1371
1372 class fhandler_dev_dsp: public fhandler_base
1373 {
1374  public:
1375   class Audio;
1376   class Audio_out;
1377   class Audio_in;
1378  private:
1379   int audioformat_;
1380   int audiofreq_;
1381   int audiobits_;
1382   int audiochannels_;
1383   Audio_out *audio_out_;
1384   Audio_in  *audio_in_;
1385  public:
1386   fhandler_dev_dsp ();
1387
1388   int open (int flags, mode_t mode = 0);
1389   ssize_t __stdcall write (const void *ptr, size_t len);
1390   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1391   int ioctl (unsigned int cmd, void *);
1392   _off64_t lseek (_off64_t, int);
1393   int close ();
1394   void fixup_after_fork (HANDLE parent);
1395   void fixup_after_exec ();
1396  private:
1397   void close_audio_in ();
1398   void close_audio_out (bool immediately = false);
1399   bool use_archetype () const {return true;}
1400   size_t size () const { return sizeof (*this);}
1401 };
1402
1403 class fhandler_virtual : public fhandler_base
1404 {
1405  protected:
1406   char *filebuf;
1407   _off64_t filesize;
1408   _off64_t position;
1409   int fileid; // unique within each class
1410  public:
1411
1412   fhandler_virtual ();
1413   virtual ~fhandler_virtual();
1414
1415   virtual virtual_ftype_t exists();
1416   DIR *opendir (int fd) __attribute__ ((regparm (2)));
1417   long telldir (DIR *);
1418   void seekdir (DIR *, long);
1419   void rewinddir (DIR *);
1420   int closedir (DIR *);
1421   ssize_t __stdcall write (const void *ptr, size_t len);
1422   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1423   _off64_t lseek (_off64_t, int);
1424   int dup (fhandler_base *child);
1425   int open (int flags, mode_t mode = 0);
1426   int close ();
1427   int __stdcall fstat (struct stat *buf) __attribute__ ((regparm (2)));
1428   int __stdcall fstatvfs (struct statvfs *buf) __attribute__ ((regparm (2)));
1429   int __stdcall fchmod (mode_t mode) __attribute__ ((regparm (1)));
1430   int __stdcall fchown (__uid32_t uid, __gid32_t gid) __attribute__ ((regparm (2)));
1431   int __stdcall facl (int, int, __acl32 *) __attribute__ ((regparm (3)));
1432   virtual bool fill_filebuf ();
1433   char *get_filebuf () { return filebuf; }
1434   void fixup_after_exec ();
1435   virtual size_t size () const { return sizeof (*this);}
1436 };
1437
1438 class fhandler_proc: public fhandler_virtual
1439 {
1440  public:
1441   fhandler_proc ();
1442   virtual_ftype_t exists();
1443   DIR *opendir (int fd) __attribute__ ((regparm (2)));
1444   int closedir (DIR *);
1445   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1446   static fh_devices get_proc_fhandler (const char *path);
1447
1448   int open (int flags, mode_t mode = 0);
1449   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1450   bool fill_filebuf ();
1451   virtual size_t size () const { return sizeof (*this);}
1452 };
1453
1454 class fhandler_procsys: public fhandler_virtual
1455 {
1456  public:
1457   fhandler_procsys ();
1458   virtual_ftype_t exists(struct __stat64 *buf) __attribute__ ((regparm (2)));
1459   virtual_ftype_t exists();
1460   DIR *opendir (int fd) __attribute__ ((regparm (2)));
1461   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1462   long telldir (DIR *);
1463   void seekdir (DIR *, long);
1464   int closedir (DIR *);
1465   int open (int flags, mode_t mode = 0);
1466   int close ();
1467   void __stdcall read (void *ptr, size_t& len) __attribute__ ((regparm (3)));
1468   ssize_t __stdcall write (const void *ptr, size_t len);
1469   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1470   bool fill_filebuf ();
1471   size_t size () const { return sizeof (*this);}
1472 };
1473
1474 class fhandler_procsysvipc: public fhandler_proc
1475 {
1476   pid_t pid;
1477  public:
1478   fhandler_procsysvipc ();
1479   virtual_ftype_t exists();
1480   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1481   int open (int flags, mode_t mode = 0);
1482   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1483   bool fill_filebuf ();
1484   size_t size () const { return sizeof (*this);}
1485 };
1486
1487 class fhandler_netdrive: public fhandler_virtual
1488 {
1489  public:
1490   fhandler_netdrive ();
1491   virtual_ftype_t exists();
1492   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1493   void seekdir (DIR *, long);
1494   void rewinddir (DIR *);
1495   int closedir (DIR *);
1496   int open (int flags, mode_t mode = 0);
1497   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1498   size_t size () const { return sizeof (*this);}
1499 };
1500
1501 class fhandler_registry: public fhandler_proc
1502 {
1503  private:
1504   wchar_t *value_name;
1505   DWORD wow64;
1506   int prefix_len;
1507  public:
1508   fhandler_registry ();
1509   void set_name (path_conv &pc);
1510   virtual_ftype_t exists();
1511   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1512   long telldir (DIR *);
1513   void seekdir (DIR *, long);
1514   void rewinddir (DIR *);
1515   int closedir (DIR *);
1516
1517   int open (int flags, mode_t mode = 0);
1518   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1519   bool fill_filebuf ();
1520   int close ();
1521   int dup (fhandler_base *child);
1522   size_t size () const { return sizeof (*this);}
1523 };
1524
1525 class pinfo;
1526 class fhandler_process: public fhandler_proc
1527 {
1528   pid_t pid;
1529  public:
1530   fhandler_process ();
1531   virtual_ftype_t exists();
1532   DIR *opendir (int fd) __attribute__ ((regparm (2)));
1533   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1534   int open (int flags, mode_t mode = 0);
1535   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1536   bool fill_filebuf ();
1537   size_t size () const { return sizeof (*this);}
1538 };
1539
1540 class fhandler_procnet: public fhandler_proc
1541 {
1542   pid_t pid;
1543  public:
1544   fhandler_procnet ();
1545   virtual_ftype_t exists();
1546   int readdir (DIR *, dirent *) __attribute__ ((regparm (3)));
1547   int open (int flags, mode_t mode = 0);
1548   int __stdcall fstat (struct __stat64 *buf) __attribute__ ((regparm (2)));
1549   bool fill_filebuf ();
1550   size_t size () const { return sizeof (*this);}
1551 };
1552
1553 struct fhandler_nodevice: public fhandler_base
1554 {
1555   fhandler_nodevice ();
1556   int open (int flags, mode_t mode = 0);
1557   // int __stdcall fstat (struct __stat64 *buf, path_conv *);
1558 };
1559
1560 #define report_tty_counts(fh, call, use_op) \
1561   termios_printf ("%s %s, %susecount %d",\
1562                   fh->ttyname (), call,\
1563                   use_op, ((fhandler_pty_slave *) (fh->archetype ?: fh))->usecount);
1564
1565 typedef union
1566 {
1567   char __base[sizeof (fhandler_base)];
1568   char __console[sizeof (fhandler_console)];
1569   char __cygdrive[sizeof (fhandler_cygdrive)];
1570   char __dev_clipboard[sizeof (fhandler_dev_clipboard)];
1571   char __dev_dsp[sizeof (fhandler_dev_dsp)];
1572   char __dev_floppy[sizeof (fhandler_dev_floppy)];
1573   char __dev_mem[sizeof (fhandler_dev_mem)];
1574   char __dev_null[sizeof (fhandler_dev_null)];
1575   char __dev_random[sizeof (fhandler_dev_random)];
1576   char __dev_raw[sizeof (fhandler_dev_raw)];
1577   char __dev_tape[sizeof (fhandler_dev_tape)];
1578   char __dev_zero[sizeof (fhandler_dev_zero)];
1579   char __disk_file[sizeof (fhandler_disk_file)];
1580   char __fifo[sizeof (fhandler_fifo)];
1581   char __mailslot[sizeof (fhandler_mailslot)];
1582   char __netdrive[sizeof (fhandler_netdrive)];
1583   char __nodevice[sizeof (fhandler_nodevice)];
1584   char __pipe[sizeof (fhandler_pipe)];
1585   char __proc[sizeof (fhandler_proc)];
1586   char __process[sizeof (fhandler_process)];
1587   char __procnet[sizeof (fhandler_procnet)];
1588   char __procsys[sizeof (fhandler_procsys)];
1589   char __procsysvipc[sizeof (fhandler_procsysvipc)];
1590   char __pty_master[sizeof (fhandler_pty_master)];
1591   char __registry[sizeof (fhandler_registry)];
1592   char __serial[sizeof (fhandler_serial)];
1593   char __socket[sizeof (fhandler_socket)];
1594   char __termios[sizeof (fhandler_termios)];
1595   char __pty_common[sizeof (fhandler_pty_common)];
1596   char __pty_slave[sizeof (fhandler_pty_slave)];
1597   char __virtual[sizeof (fhandler_virtual)];
1598   char __windows[sizeof (fhandler_windows)];
1599 } fhandler_union;
1600 #endif /* _FHANDLER_H_ */