Commit Graph

578 Commits

Author SHA1 Message Date
Corinna Vinschen 323b48b975 Cygwin: lseek: return ESPIPE rather than EINVAL when called on a fifo
Thanks to Henri for tracking this down:
https://cygwin.com/ml/cygwin/2018-10/msg00062.html

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-10-10 13:20:45 +02:00
Mark Geisert 87253cbe38 POSIX Asynchronous I/O support: fhandler files
This code is where the AIO implementation is wired into existing Cygwin
mechanisms for file and device I/O: the fhandler* functions.  It makes
use of an existing internal routine prw_open to supply a "shadow fd"
that permits asynchronous operations on a file the user app accesses
via its own fd.  This allows AIO to read or write at arbitrary locations
within a file without disturbing the app's file pointer.  (This was
already the case with normal pread|pwrite; we're just adding "async"
to the mix.)
2018-07-25 09:36:24 +02:00
Corinna Vinschen 6c55be9dbb Cygwin: Allow to build without experimental AF_UNIX code by default
Introduce __WITH_AF_UNIX preprocessor flag to enable the new code

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-06-26 16:31:17 +02:00
Takashi Yano 9c84bfd479 Fix the handling of out-of-band (OOB) data in a socket.
* fhandler.h (class fhandler_socket_inet): Add variable bool oobinline.
* fhandler_socket_inet.cc (fhandler_socket_inet::fhandler_socket_inet):
  Initialize variable oobinline.
(fhandler_socket_inet::recv_internal): Make the handling of OOB data
  as consistent with POSIX as possible. Add simulation of inline mode
  for OOB data as a workaround for broken winsock behavior.
(fhandler_socket_inet::setsockopt): Ditto.
(fhandler_socket_inet::getsockopt): Ditto.
(fhandler_socket_wsock::ioctl): Fix return value of SIOCATMARK command.
  The return value of SIOCATMARK of winsock is almost opposite to
  expectation.
* fhandler_socket_local.cc (fhandler_socket_local::recv_internal):
  Remove the handling of OOB data from AF_LOCAL domain socket. Operation
  related to OOB data will result in an error like Linux does.
(fhandler_socket_local::sendto): Ditto.
(fhandler_socket_local::sendmsg): Ditto.

This fixes the issue reported in following post.
https://cygwin.com/ml/cygwin/2018-06/msg00143.html
2018-06-22 10:20:08 +02:00
Corinna Vinschen 23b5ecdaf3 Cygwin: delete /dev/kmsg and thus fhandler_mailslot without substitution
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-25 12:38:14 +02:00
Corinna Vinschen 4fe086c84f Cygwin: AF_UNIX: Redesign various aspects
* Change set_socket_type/get_socket_type to virtual methods
* Move various variables into af_unix_shmem_t
* Change sun_name_t to match new usage pattern
* Move shut_state definition and add a name for the 0 value
* Allow marking packet as administrative packet.  This allows
  filtering out info packets exchange between peers and tweak
  data accordingly.
* Rename send_my_name to send_sock_info and send credentials
  if not called from bind (so the socket was already connected)
* Handle SO_PASSCRED in setsockopt/getsockopt
* Add input size checking to setsockopt/getsockopt
* Use NT functions where appropriate

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-18 20:46:43 +01:00
Corinna Vinschen 848d5b70db Cygwin: AF_UNIX: Add state_lock to guard manipulating shared state info
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-18 20:06:43 +01:00
Corinna Vinschen 60ca1c1359 Cygwin: AF_UNIX: Use spinlock rather than SRWLOCKs
We need to share socket info between threads *and* processes.
SRWLOCKs are single-process only, unfortunately.  Provide a
sharable low-profile spinlock instead.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-18 18:46:15 +01:00
Corinna Vinschen edcf783dc2 Revert "ctype: align size of category bit fields to small targets needs"
This reverts commit e98d3eb3eb.

It has accidentally included some work in progress.
2018-03-14 11:36:06 +01:00
Corinna Vinschen e98d3eb3eb ctype: align size of category bit fields to small targets needs
E.g. arm ABI requires -fshort-enums for bare-metal toolchains.
Given there are only 29 category enums, the compiler chooses an
8 bit enum type, so a size of 11 bits for the bitfield leads to
a compile time error:

  error: width of 'cat' exceeds its type
    enum category cat: 11;
                  ^~~

Fix this by aligning the size of the category members to byte
borders.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-14 10:36:38 +01:00
Corinna Vinschen 725176612d Cygwin: AF_UNIX: store per-socket info in shared memory
Per-socket info in fhandler isn't correctly shared between multiple
instances of th same descriptor.  Implement a basic shared info which
is shared between all instances of a socket.

This also requires to move the fhandler_socket status bits into
fhandler_socket_wsock since the data is moved to the shared region
for AF_UNIX sockets.

Also, drop backing file requirement for socketpair server socket.
This will be handled differently in recvmsg/sendmsg.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-12 15:26:12 +01:00
Corinna Vinschen 7b1028974b Cygwin: AF_UNIX: Add fixup_after_exec method
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-10 21:09:28 +01:00
Corinna Vinschen b194d65615 Cygwin: AF_UNIX: Implemant socketpair
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-09 14:19:36 +01:00
Corinna Vinschen d7f7d292d8 Cygwin: reorder read/write calls in fhandler_socket_unix
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-07 21:52:29 +01:00
Corinna Vinschen cde2648c22 Cygwin: AF_UNIX: make sure connect wait thread is cleanly interruptible
Using TerminateThread potentially leaks resources.  In our case,
the connect wait thread may be forcefully terminated after
having successfully opened a client side pipe handle.  If this
occurs, we have a stale pipe server instance, so the pipe will
never be closed as long as the process lives.

Avoid this by changing the npfs handle to non-blocking, so we can
wait on a termination event object from inside the thread itself
and cleanly exit from the thread instead of terminating.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-07 16:19:43 +01:00
Corinna Vinschen 2f2a75b7bb Cygwin: AF_UNIX: fix creation of npfs handle
The handle to the device is never needed.  As the name impies,
FSCTL_PIPE_WAIT works on the file system, not on the device level.

Drop opening the device and make sure to open only one handle to NPFS.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-07 16:06:57 +01:00
Corinna Vinschen 4cd57934be Cygwin: AF_UNIX: Implement listen, accept4, connect, and others
* Implement helper functions
* Improve bind
* Implement setting blocking, ioctl(FIONBIO), fcntl(F_SETFL)
* Implement close_on_exec and fixup_after_fork
* Allow overriding sun_path and peer_sun_path
* Improve comments

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-06 19:10:54 +01:00
Corinna Vinschen cabfef78e9 Cygwin: AF_UNIX: create/open pipes relativ to NPFS rootdir handle
Only store and manipulate pipe basename.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-06 18:37:09 +01:00
Corinna Vinschen ae67198d55 Cygwin: move sun_name_t constructors into fhandler_socket_unix.cc
They are only used there anyway and it allows to use the AF_UNIX
macro without tweaking header files.  While at it, improve
both constructors.  The default constructor now creates the name
of an unnamed socket, the constructor taking parameters carefully
checks its input.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-05 18:08:47 +01:00
Corinna Vinschen a2c02d78be Cygwin: sockets: add bind state, and split out connect state to allow atomic operation
The connect state was stored in a bitfield which is not safe
to do atomic operations on.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-05 17:59:04 +01:00
Corinna Vinschen 97b7aaaeb7 Cygwin: fhandler_socket_unix: implement socket, bind, and close
...plus lots of helper functions.  Add comment to explain how everything
works.  This comment will be improved while implementing the yet missing
parts.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-02 18:17:17 +01:00
Corinna Vinschen dc3928fc75 Cygwin: convert sun_name_t into class
Add constructors and new/delete operators to make sure sun_name_t
objects are allocated on the cygheap.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-03-01 18:14:23 +01:00
Corinna Vinschen a27a7752ec Cygwin: improve storage and handling of AF_UNIX socket path
Define new struct sun_name_t and use throughout internally.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-28 19:06:53 +01:00
Corinna Vinschen 892efccb25 Cygwin: fhandler_socket_unix: store peer credentials in ucred member
* Split out cygwin/_ucred.h file
* drop local credentials

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-28 19:01:29 +01:00
Corinna Vinschen fbdae2c216 Cygwin: reduce size of fhandler_cygdrive
fhandler_cygdrive has a size of 696 bytes on x86_64, while the next
biggest fhandler type, fhandler_pty_master, is 584 bytes.  The members
responsible for the size are private to opendir/readdir/closedir usage.
fhandler_disk_file stores private readdir data in DIR->__d_internal
instead.  Use equivalent method with fhandler_cygdrive.  This drops
the size to 464 bytes.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-26 20:39:48 +01:00
Corinna Vinschen 1e5e44a9a5 Cygwin: fhandler_socket: define socketpair as virtual function
...in preparation of moving the type and protocol test into the
actual classes.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-26 17:56:09 +01:00
Corinna Vinschen 7f7532fafb Cygwin: Create empty fhandler_socket_unix
* Make distinct from AF_LOCAL for testing purposes.  This will have
  to be reverted as soon as fhandler_socket_unix goes life.

* Move saw_reuseaddr flag back to fhandler_socket status

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-23 21:00:43 +01:00
Corinna Vinschen b79018ee3a Cygwin: encapsulate Winsock based fhandler_socket classes
Insert another class fhandler_socket_wsock between fhandler_socket
and fhandler_socket_inet/fhandler_socket_local.

Also, add a new method fhandler::is_wsock_socket to allow asking
for sockets in general (is_socket) vs. Winsock-based sockets
(is_wsock_socket).

This allows to develop a new handler_socket_unix class as derived
class from fhandler_socket without any trace of wsock code left
in fhandler_socket.

While this is basically a temporary measure at this time, it may
prove useful for later interoperability with the upcoming Windows 10
AF_UNIX implementation at one point.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-23 15:24:18 +01:00
Corinna Vinschen cc9fe2c716 Cygwin: eliminate unused fhandler method hclose
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-23 14:56:30 +01:00
Corinna Vinschen b8a57a2d2a Cygwin: fhandler_socket: Move select functions into derived classes
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-22 16:59:55 +01:00
Corinna Vinschen 479080baec Cygwin: fhandler_socket: Rearrange methods
Follow the same pattern in all fhandler_socket classes.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-22 16:54:08 +01:00
Corinna Vinschen 9c593d9b39 Cygwin: fhandler_socket: Add derived fcntl methods
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-22 16:37:12 +01:00
Corinna Vinschen 79598f94f7 Cygwin: fhandler_socket: Add derived ioctl methods
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-22 16:31:07 +01:00
Corinna Vinschen 233bde3125 Cygwin: fhandler_socket: Move shutdown and close methods into derived classes
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-22 16:28:14 +01:00
Corinna Vinschen 84c5e0fd3d Cygwin: make socketpair an AF_LOCAL-only method
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-22 16:25:28 +01:00
Corinna Vinschen a5dfbc6940 Cygwin: inline get_socket_flags()
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-21 22:00:04 +01:00
Corinna Vinschen 859d215b7e Cygwin: split out fhandler_socket into inet and local classes
First cut, still incomplete

* fhandler_socket is now base class for other socket classes
* fhandler_socket_inet handles AF_INET and AF_INET6 sockets
* fhandler_socket_local handles AF_LOCAL/AF_UNIX sockets
* finally get rid of fdsock by using set_socket_handle in accept4
* align file-related calls (fstat,  fstatvfs, fchown, fchmod, facl)
  to Linux.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-21 21:40:01 +01:00
Corinna Vinschen ea1e5318d5 Cygwin: set/getsockopt: Move implementation into fhandler_socket class
This requires to export find_winsock_errno from net.cc.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-20 18:01:40 +01:00
Corinna Vinschen ea543d3ffa Cygwin: fix declaration of fhandler_socket::socketpair
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-16 17:34:57 +01:00
Corinna Vinschen 4e04751fc7 Cygwin: socketpair: Move socketpair creation inside fhandler_socket class
Add fhandler_socket::socketpair method

Deliberately disable AF_INET socketpairs for now

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-16 16:36:19 +01:00
Corinna Vinschen cff85eaddc Cygwin: sockets: move common settings into set_socket_handle()
Move setting address family, socket type and descriptor flags
into fhandler_socket::set_socket_handle method.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-16 16:23:32 +01:00
Corinna Vinschen 913c6ca2c1 Cygwin: socket: move socket creation inside fhandler_socket class
Add fhandler_socket::socket method
Add fhandler_socket::set_socket_handle method, basically duplicating
what fdsock is doing.  This is the first step in getting rid of fdsock.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-14 22:21:58 +01:00
Corinna Vinschen 7ae73be141 Cygwin: improve O_TMPFILE handling
Windows does not remove FILE_ATTRIBUTE_TEMPORARY by itself after a
file has been closed.  It's just some attribute which can be set or
removed at will, despite its purpose.

Apparently there are tools out there which use FILE_ATTRIBUTE_TEMPORARY
accidentally or wrongly, even Microsoft's own tools are affected.  In
the end, the filesystem is potentially full of files with this attribute
set.

Implement O_TMPFILE files with FILE_ATTRIBUTE_TEMPORARY and
FILE_ATTRIBUTE_HIDDEN set.  This combination is pretty unlikely.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-14 12:55:24 +01:00
Corinna Vinschen c51a0b74dc Cygwin: sockets: Handle SO_RCVTIMEO and SO_SNDTIMEO
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2018-02-07 16:18:07 +01:00
Yaakov Selkowitz 21a39b20a5 Revert "cygwin: only expose /dev/con{in,out,sole} when started from a Windows console"
This caused serious regressions when running from a cmd window:

https://cygwin.com/ml/cygwin/2017-09/msg00114.html

This reverts commit b706c6b479.

Signed-off-by: Yaakov Selkowitz <yselkowi@redhat.com>
2017-09-11 23:07:55 -05:00
Corinna Vinschen b706c6b479 cygwin: only expose /dev/con{in,out,sole} when started from a Windows console
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-08-23 17:43:41 +02:00
Corinna Vinschen dd757cc43a Implement fhandler_dev_null::write to workaround a problem with NUL
Windows NUL device returns only the lower 32 bit of the number of
bytes written.  Implement a fake write function to ignore the underlying
NUL device.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-03-12 12:17:43 +01:00
Corinna Vinschen 095cac4b8d Cygwin: Add IUTF8 termios iflag
The termios code doesn't handle erasing of multibyte characters
in canonical mode, it always erases a single byte.  When entering
a multibyte character and then pressing VERASE, the input ends up
with an invalid character.

Following Linux we introduce the IUTF8 input flag now, set by
default.  When this flag is set, VERASE or VWERASE will check
if the just erased input byte is a UTF-8 continuation byte.  If
so, it erases another byte and checks again until the entire
UTF-8 character has been removed from the input buffer.

Note that this (just as on Linux) does NOT work with arbitrary
multibyte codesets.  This only works with UTF-8.

For a discussion what happens, see
https://cygwin.com/ml/cygwin/2017-01/msg00299.html

Sidenote: The eat_readahead function is now member of fhandler_termios,
not fhandler_base.  That's necessary to get access to the terminal's
termios flags.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-01-31 15:36:24 +01:00
Corinna Vinschen d16a56306d Consolidate wctomb/mbtowc calls for POSIX-1.2008
- Remove charset parameter from low level __foo_wctomb/__foo_mbtowc calls.
- Instead, create array of function for ISO and Windows codepages to point
  to function which does not require to evaluate the charset string on
  each call.  Create matching helper functions.  I.e., __iso_wctomb,
  __iso_mbtowc, __cp_wctomb and __cp_mbtowc are functions returning the
  right function pointer now.
- Create __WCTOMB/__MBTOWC macros utilizing per-reent locale and replace
  calls to __wctomb/__mbtowc with calls to __WCTOMB/__MBTOWC.
- Drop global __wctomb/__mbtowc vars.
- Utilize aforementioned changes in Cygwin to get rid of charset in other,
  calling functions and simplify the code.
- In Cygwin restrict global cygheap locale info to the job performed
  by internal_setlocale.  Use UTF-8 instead of ASCII on the fly in
  internal conversion functions.
- In Cygwin dll_entry, make sure to initialize a TLS area with a NULL
  _REENT->_locale pointer.  Add comment to explain why.

Signed-off by: Corinna Vinschen <corinna@vinschen.de>
2016-08-15 10:56:57 +02:00
Corinna Vinschen 32b668d966 Don't raise SIGTTIN from poll/select
SIGTTIN should be raised when read() is made on a tty in a backgrounded
process, but not when it's tested with poll()/select().

I guess poll()/select() does need to call bg_check(), in order to detect the
error conditions that notices (that is, if bg_check() returns bg_eof or
bg_error, then fd is ready as an error condition exists) so add an optional
parameter to fhandler_base::bg_select() to indicate that signals aren't
desired.

See https://cygwin.com/ml/cygwin-developers/2016-07/msg00004.html
2016-07-27 17:02:08 +01:00