Commit Graph

13075 Commits

Author SHA1 Message Date
Corinna Vinschen e36811afb4 Cygwin: drop Vista WOW64 specific child process handle bug
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-10-29 14:52:58 +02:00
Corinna Vinschen 7a3df8bb6f Cygwin: bump DLL major version
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-10-28 22:29:42 +02:00
Corinna Vinschen 3860338968 Cygwin: bump DLL minor version
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-10-28 13:41:19 +02:00
Corinna Vinschen 44a79a6eca Cygwin: convert malloc lock to SRWLOCK
Per https://cygwin.com/pipermail/cygwin-developers/2021-October/012429.html,
we may encounter a crash when starting multiple threads during process
startup (here: fhandler_fifo::fixup_after_{fork,exec}) which in turn
allocate memory via malloc.

The problem is concurrent usage of malloc before the malloc muto has
been initialized.

To fix this issue, convert the muto to a SRWLOCK and make sure it is
statically initalized.  Thus, malloc can be called as early as necessary
and malloc_init is only required to check for user space provided malloc.

Note that this requires to implement a __malloc_trylock macro to be
called from fork.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-10-26 18:47:22 +02:00
Mark Geisert 23b1400f83 Cygwin: Make native clipboard layout same for 32- and 64-bit
This patch unifies the layout of the clipboard descriptor cygcb_t for
32- and 64-bit Cygwin.  It allows correct copy/paste between the two
environments without corruption of user's copied data and without access
violations due to interpreting that data as a size field.

The definitions of CYGWIN_NATIVE and cygcb_t are moved to a new include
file, sys/clipboard.h.  The include file is used by fhandler_clipboard.cc
as well as getclip.c and putclip.c in the Cygwin cygutils package.

When copy/pasting between 32- and 64-bit Cygwin environments, both must
be running version 3.3.0 or later for successful operation.
2021-10-26 12:42:21 +02:00
Ken Brown 65d6818661 Cygwin: document sig2str and str2sig in version.h 2021-10-22 13:56:05 -04:00
Corinna Vinschen cb21b9cc57 Cygwin: drop outdated -O3 optimization for malloc and sync
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-10-22 12:47:09 +02:00
Corinna Vinschen 548f124698 Cygwin: update cygcheck OS information
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-10-21 18:33:35 +02:00
Takashi Yano 745b547ebd Cygwin: pipe: Use NtQuerySystemInformation() instead of EnumProcesses().
- Using EnumProcess() breaks Windows Vista compatibility. This patch
  replaces EnumProcesses() with NtQuerySystemInformation().

Addresses:
https://cygwin.com/pipermail/cygwin-developers/2021-October/012422.html
2021-10-19 11:09:47 +02:00
Takashi Yano dcd564f65c Cygwin: pty: Fix handle leak regarding attach_mutex.
- If the process having master pty opened is forked, attach_mutex
  fails to be closed when master is closed. This patch fixes the
  issue.
2021-10-10 08:57:51 -04:00
Takashi Yano 8a09deb1b7 Cygwin: pty: Fix master closing error regarding attach_mutex.
- If two or more pty masters are opened in a process, closing master
  causes error when closing attach_mutex. This patch fixes the issue.

Addresses:
https://cygwin.com/pipermail/cygwin-developers/2021-October/012418.html
2021-10-08 13:27:50 -04:00
Takashi Yano 6c1f49f83f Cygwin: pipe: Use ProcessHandleInformation if available.
- The commit b531d6b0 introduced temporary_query_hdl() which uses
  SystemHandleInformation. With this patch, ProcessHandleInformation
  rather than SystemHandleInformation is used if it is available.
  This request is faster, however, is only available since Windows 8,
  therefore, SystemHandleInformation is used for Windows Vista and 7
  as before.
2021-09-23 12:24:19 -04:00
Takashi Yano b531d6b06e Cygwin: pipe: Introduce temporary query_hdl.
- The commit f79a4611 introduced query_hdl, which is the read pipe
  handle kept in the write pipe instance in order to determine if
  the pipe is ready to write in select().  This implementation has
  a potential risk that the write side fails to detect the closure
  of the read side if more than one writer exists and one of them
  is a non-cygwin process.

  With this patch, the strategy of commit f79a4611 is used only if
  the process is running as a service.  For a normal process,
  instead of keeping query_hdl in the write pipe instance, it is
  retrieved temporarily when select() is called.  Actually, we
  want to use tenporary query_hdl for all processes, however, it
  does not work for service processes due to OpenProcess()
  failure.
2021-09-21 14:47:33 -04:00
Ken Brown 643db9ec9e Cygwin: pipes: remove unnecessary NULL checks for hdl_cnt_mtx
In view of the previous changes to open_setup, we can always assume
that hdl_cnt_mtx is non-NULL.
2021-09-18 09:25:20 -04:00
Ken Brown be08c5c40b Cygwin: make callers of open_setup check for failure
open_setup is called by dtable::init_std_file_from_handle and
fhandler_base::open_with_arch.  In both cases, failure of open_setup
is now a fatal error.

Currently this can only happen in the following situation: A Cygwin
process is started by a non-Cygwin process, one of the standard IO
handles is a pipe handle, and Cygwin is unable to create a required
mutex (read_mtx or hdl_cnt_mtx).
2021-09-18 09:10:23 -04:00
Ken Brown 6fc0aa8e5d Cygwin: fhandler_pipe::open_setup: fail on error creating mutexes 2021-09-18 08:49:06 -04:00
Ken Brown e5fcb021cc Cygwin: allow open_setup to fail
Convert fhandler_base::open_setup to a (virtual) method that returns a
bool result.  For the moment, it and its overrides always return true.
2021-09-18 08:40:11 -04:00
Ken Brown 9b0841aa78 Cygwin: statically link the tools in winsup/utils/mingw
This fixes the problem reported here:

  https://cygwin.com/pipermail/cygwin-developers/2021-September/012375.html
2021-09-17 21:38:07 -04:00
Ken Brown 98859e00d0 Cygwin: fix typos in release notes 2021-09-17 16:16:14 -04:00
Takashi Yano 9814cfd8f6 Cygwin: pipe, fifo: Move query_hdl and hdl_cnt_mtx to fhandler_pipe.
- query_hdl and hdl_cnt_mtx are moved from fhandler_pipe_fifo to
  fhandler_pipe. Then reader_closed() is changed to virtual and
  overridden in fhandler_pipe.
2021-09-16 15:53:57 -04:00
Takashi Yano 199482654b Cygwin: pipe: Fix race issue regarding handle count.
- This patch fixes the race issue in the handle counting to detect
  closure of read pipe, which is introduced by commit f79a4611.
  A mutex hdl_cnt_mtx is introduced for this issue.
2021-09-16 10:57:36 -04:00
Takashi Yano d8614e355d Cygwin: pipe: Fix error handling in fhandler_pip::create().
- Currently, error handling in fhandler_pipe::create() is broken.
  This patch fixes that.
2021-09-16 10:57:24 -04:00
Takashi Yano 350806f882 Cygwin: close_all_files: Do not duplicate stderr for write pipe.
- Currently, the stderr handle is duplicated in close_all_files().
  This interferes the handle counting for detecting closure of read
  pipe, which is introduced by commit f79a4611. This patch stops
  duplicating stderr handle if it is write pipe.
2021-09-16 10:57:14 -04:00
Ken Brown 18fab2f834 Cygwin: select: check for negative return from pipe_data_available
Make sure except_ready is set (if except_selected) on a negative
return from pipe_data_available.
2021-09-16 08:53:40 -04:00
Corinna Vinschen 34b1447040 Cygwin: pipes: don't call NtQueryInformationFile on read side of pipes
NtQueryInformationFile hangs if it's called on the read side handle of
a pipe while another thread or process is performing a blocking read.

Avoid select potentially hanging by calling NtQueryInformationFile
only on the write side of the pipe and using PeekNamedPipe otherwise.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-15 14:20:03 +02:00
Corinna Vinschen f961a63ed6 Cygwin: drop useless method fhandler_base::has_ongoing_io
This was a remnant of the fhandler_base_overlapped class.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-15 13:58:36 +02:00
Takashi Yano 00cbbaa33e Cygwin: pipe: Do not call PeekNamedPipe() if it is not necessary.
- In pipe_data_available() in select.cc, PeekNamedPipe() call is
  not needed if WriteQuotaAvailable is non-zero because we already
  know the write pipe has a space. Therefore, with this patch,
  PeekNamedPipe() is called only when WriteQuotaAvailable is zero.
  This makes select() on pipe faster a bit.
2021-09-15 11:29:27 +02:00
Ken Brown 593a86f9b0 Cygwin: document the recent pipe changes 2021-09-14 14:59:28 -04:00
Takashi Yano a217fa98fd Cygwin: pipe: Fix handling of EPIPE and SIGPIPE in raw_write(). 2021-09-14 16:16:15 +02:00
Takashi Yano e4e4537979 Cygwin: pipe, fifo: Release select_sem semaphore as much as needed.
- Currently, raw_read(), raw_write() and close() release select_sem
  unconditionally even if no waiter for select_sem exists. With this
  patch, only the minimum number of semaphores required is released.
2021-09-14 13:02:51 +02:00
Takashi Yano f79a46112e Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
  on the write side reflects the space available in the inbound buffer
  on the read side. However, if a pipe read is currently pending,
  WriteQuotaAvailable on the write side is decremented by the number
  of bytes the read side is requesting. So it's possible (even likely)
  that WriteQuotaAvailable is 0, even if the inbound buffer on the
  read side is not full. This can lead to a deadlock situation:
  The reader is waiting for data, but select on the writer side
  assumes that no space is available in the read side inbound buffer.

  Currently, to avoid this stuation, read() does not request larger
  block than pipe size - 1. However, this mechanism does not take
  effect if the reader side is non-cygwin app.

  The only reliable information is available on the read side, so
  fetch info from the read side via the pipe-specific query handle
  (query_hdl) introduced.

  If the query_hdl (read handle) is kept in write side, writer can
  not detect closure of read pipe. Therefore, raw_write() counts
  write handle and query_hdl. If they are equal, only the pairs of
  write handle and query_hdl are alive. In this case, raw_write()
  returns EPIPE and raises SIGPIPE.

- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
  tools, so convert pipe handles to PIPE_WAIT handles when spawning
  a non-Cygwin process.
2021-09-14 13:01:40 +02:00
Takashi Yano 0d12015670 Cygwin: pipe, fifo: Call set_no_inheritance() for adjunct handles.
- Currntly, set_no_inheritance() is not called for the adjunct handles
  such as select_sem. This patch fixes the issue.
2021-09-14 10:22:15 +02:00
Takashi Yano 3e80b12fde Cygwin: fhandler_base::dup Reflect O_CLOEXEC to inheritance flag.
- Currently fhandler_base::dup duplicates handles with bInheritHandle
  TRUE unconditionally. This patch reflects O_CLOEXEC flag to that
  parameter.
2021-09-14 10:22:15 +02:00
Corinna Vinschen 8985f1c7c4 Cygwin: pipes: do not duplicate sec_none{_nih} locally when creating objects
We already fetched the correct SECURITY_ATTRIBUTES at the start of
fhandler_pipe::create, so using another SECURITY_ATTRIBUTES object for
the mutex and semaphore objects doesn't make much sense.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 22:04:04 +02:00
Corinna Vinschen c48361ad9e Cygwin: fix inheritence of select_sem on write side of pipe
select_sem gets created on the read side with inheritence settings
depending on the O_CLOEXEC flag.  Then it gets duplicated to the write
side with unconditional inheritence.  Fix that.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 19:34:07 +02:00
Takashi Yano 0063ffeb73 Cygwin: pipe: Fix deadlock if pipe is created by non-cygwin app. 2021-09-13 17:45:55 +02:00
Takashi Yano 0b538118b2 Cygwin: fifo: Utilize select_sem for fifo as well as pipe. 2021-09-13 17:45:55 +02:00
Corinna Vinschen ba4c58299f Cygwin: pipes: always signal select_sem if any bytes are read or written
Fold all code branches potentially having read or written data into
a single if branch, so signalling select_sem catches all cases.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 17:45:55 +02:00
Takashi Yano 08b8534d0a Cygwin: pipe: Fix notification timing of select_sem.
- Make select_sem notify even when read/write partially.
2021-09-13 17:45:54 +02:00
Takashi Yano 597f87294d Cygwin: select: Introduce select_sem semaphore for pipe.
- This patch introduces select_sem semaphore which notifies pipe status
  change.
2021-09-13 17:45:54 +02:00
Takashi Yano b07660ac19 Revert "Cygwin: select: Improve select/poll response."
... because this commit (23bb19ef) causes high CPU load.
2021-09-13 17:45:54 +02:00
Ken Brown 99be238347 Cygwin: set buffer size for pipes created by non-Cygwin processes
Rename fhandler_pipe_and_fifo::max_atomic_write to pipe_buf_size.
This reflect its actual meaning better.  The fhandler_pipe_and_fifo
constructor initializes it to DEFAULT_PIPEBUFSIZE (== 64K), which is
the buffer size for the windows pipes created by fhandler_pipe and
fhandler_fifo.  But if we inherit a stdio pipe handle from a
non-Cygwin process, the buffer size could be different.

To remedy this, add a method fhandler_pipe::set_pipe_buf_size that
queries the OS for the pipe buffer size, and use it in
dtable::init_std_file_from_handle.
2021-09-13 17:45:54 +02:00
Corinna Vinschen 27b24069d1 Cygwin: pipes: drop "tiny pipe" handling
Given we return 1 already if WriteQuotaAvailable is > 0, the condition
for tiny pipes is never true.  Fix the comments.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 17:45:54 +02:00
Corinna Vinschen 9d7fd8d416 Cygwin: pipes: handle signals and thread cancellation in blocking mode only
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 17:45:54 +02:00
Corinna Vinschen 4003e3dfa1 Cygwin: pipes: always terminate async IO in blocking mode
In blocking mode, the underlying IO must always be terminated,
one way or the other, to make sure the application knows the exact
state after returning from the IO function.  Therefore, always call
CancelIo in blocking mode.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 17:45:54 +02:00
Corinna Vinschen 477a593693 Cygwin: pipes: cancel async IO if thread cancellation is in progress
Just cancelling a thread doesn't cancel async IO started by this thread.
Fix this by returning from cygwait and calling CancelIo before canceling
self.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2021-09-13 17:45:54 +02:00
Takashi Yano fadbedd9ca Cygwin: pipe: Stop counting reader and read all available data.
- By guarding read with read_mtx, no more than one ReadFile can
  be called simultaneously. So couting read handles is no longer
  necessary.
- Make raw_read code as similar as possible to raw_write code.
2021-09-13 17:45:54 +02:00
Ken Brown 085fc12948 Cygwin: new class fhandler_pipe_fifo
This is a parent of fhandler_pipe and fhandler_fifo for code that is
common between the two classes.  Currently it just contains
max_atomic_write and raw_write().  The latter is identical to what
used to be fhandler_pipe::raw_write().
2021-09-13 17:45:54 +02:00
Ken Brown eb50f82677 Cygwin: FIFO: open pipes with FILE_READ_ATTRIBUTES access
This is needed by NtQueryInformationFile, which is used by select.
2021-09-13 17:45:54 +02:00
Ken Brown 44693e80b1 Cygwin: pipes: minor code cleanup
Clarify a comment in raw_read, and remove a useless line from
raw_write.
2021-09-13 17:45:54 +02:00