Commit Graph

8936 Commits

Author SHA1 Message Date
Takashi Yano via Cygwin-patches c8b076a233 Cygwin: select: Fix a bug on closing pi->bye event.
- Close event handle pi->bye only if it was created.
  Addresses:
  https://cygwin.com/pipermail/cygwin-developers/2020-August/011948.html
2020-08-28 15:24:22 +02:00
Corinna Vinschen 7c963c7ba0 Cygwin: sigproc: Allow more child processes per process
256 children per process is a bit tight in some scenarios.

Fix this by revamping the `procs' array.  Convert it to an
extensible class child_procs and rename procs to chld_procs.
Fix code throughout to use matching class methods rather than
direct access.

To allow a lot more child processes while trying to avoid
allocations at DLL startup, maintain two arrays within class
child_procs, one using a default size for 255 (i686) or 1023
(x86_64) children, the other, dynamically allocated on overflowing
the first array, giving room for another 1023 (i686) or 4095
(x86_64) processes.

On testing with a simple reproducer on a x86_64 machine with
4 Gigs RAM, a system memory overflow occured after forking
about 1450 child processes, so this simple dynamic should
suffice for a while.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-28 15:22:58 +02:00
Corinna Vinschen 163daed37c Cygwin: drop PROC_DETACHED_CHILD flag
pinfo::remember with the detach parameter set to true is
the only way to call proc_subproc with PROC_DETACHED_CHILD.
This call is exclusively used in spawn to set up a pinfo for
a detached child, and that pinfo goes out of scope right
afterwards without any further action.

Drop the flag and drop the detach parameter from pinfo::remember.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-28 11:10:48 +02:00
Corinna Vinschen 558fa888e5 Cygwin: sigproc: drop __stdcall
Nothing to gain here

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-28 09:44:18 +02:00
Corinna Vinschen eb3e3e4738 Cygwin: sigproc: return int from remove_proc
The return value is used in a numerical context and remove_proc
already returned inconsistently "true" vs. 0.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-28 09:44:18 +02:00
Corinna Vinschen 0a31ad6f4c Cygwin: fix up proc_subproc flags and matching pinfo methods
After patch 23a779bf3d
"Cygwin: pinfo: stop remember doing reattach",
PROC_ADDCHILD actually just sets up a new child, mirroring
PROC_DETACHED_CHILD.  The actual attaching of the child is
performed by action PROC_REATTACH_CHILD or pinfo::reattach
respectively.

To better reflect what's going on, rename PROC_REATTACH_CHILD
to PROC_ATTACH_CHILD and rename pinfo::reattach to pinfo::attach.
For better readability change PROC_ADDCHILD to PROC_ADD_CHILD.
Fix comments accordingly.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-28 09:44:18 +02:00
Ken Brown 49a9ffdf4b Cygwin: fhandler_fifo::delete_client_handler: improve efficiency
Delete a client handler by swapping it with the last one in the list
instead of calling memmove.
2020-08-27 07:14:19 -04:00
Takashi Yano via Cygwin-patches 69a2a8db58 Cygwin: console: Replace WriteConsoleA() with WriteConsoleW().
- To allow sending non-ASCII chars to console, all WriteConsoleA()
  are replaced by WriteConsoleW().
  Addresses:
  https://cygwin.com/pipermail/cygwin-patches/2020q3/010476.html
2020-08-27 10:51:31 +02:00
Ken Brown c1f177d6a9 Cygwin: cwdstuff::get: clean up debug_printf output
Set errno = 0 at the beginning so that the debug_printf call at the
end doesn't report a nonzero errno left over from some other function
call.
2020-08-23 18:50:00 -04:00
Takashi Yano bb42852062 Cygwin: pty: Implement new pseudo console support.
- In this implementation, pseudo console is created for each native
  console app. Advantages and disadvantages of this implementation
  over the previous implementation are as follows.

  Advantages:
  1) No performance degradation in pty output for cygwin process.
      https://cygwin.com/pipermail/cygwin/2020-February/243858.html
  2) Free from the problem caused by difference of behaviour of control
     sequences between real terminal and pseudo console.
      https://cygwin.com/pipermail/cygwin/2019-December/243281.html
      https://cygwin.com/pipermail/cygwin/2020-February/243855.html
  3) Free from the problem in cgdb and emacs gud.
      https://cygwin.com/pipermail/cygwin/2020-January/243601.html
      https://cygwin.com/pipermail/cygwin/2020-March/244146.html
  4) Redrawing screen on executing native console apps is not necessary.
  5) cygwin-console-helper is not necessary for the pseudo console
     support.
  6) The codes for pseudo console support are much simpler than that
     of the previous one.

  Disadvantages:
  1) The cygwin program which calls console API directly does not work.
  2) The apps which use console API cannot be debugged with gdb. This
     is because pseudo console is not activated since gdb uses
     CreateProcess() rather than exec(). Even with this limitation,
     attaching gdb to native apps, in which pseudo console is already
     activated, works.
  3) Typeahead key inputs are discarded while native console app is
     executed. Simirally, typeahead key inputs while cygwin app is
     executed are not inherited to native console app.
  4) Code page cannot be changed by chcp.com. Acctually, chcp works
     itself and changes code page of its own pseudo console.  However,
     since pseudo console is recreated for another process, it cannot
     inherit the code page.
  5) system_printf() does not work after stderr is closed. (Same with
     cygwin 3.0.7)
  6) Startup time of native console apps is about 3 times slower than
     previous implemenation.
  7) Pseudo console cannot be activated if it is already activated for
     another process on same pty.
2020-08-22 13:43:49 +02:00
Corinna Vinschen b9261fa1d9 Cygwin: bump version to 3.2.0
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-22 13:43:46 +02:00
Ken Brown 14c0a4c67d Cygwin: add header defining GCC exception codes
Include it in exceptions.cc instead of defining the exception codes
there.
2020-08-20 10:46:23 -04:00
Ken Brown 74cf7dabcb Cygwin: main exception handler (64-bit): continue GCC exceptions
This is necessary in order to be consistent with the following comment
in the definition of _Unwind_RaiseException() in the GCC source file
libgcc/unwind-seh.c:

     The exception handler installed in crt0 will continue any GCC
     exception that reaches there (and isn't marked non-continuable).

Previously we failed to do this and, as a consequence, the C++ runtime
didn't call std::terminate after an unhandled exception.

This fixes the problem reported here:

  https://cygwin.com/pipermail/cygwin/2019-October/242795.html
  https://sourceware.org/pipermail/cygwin/2020-August/245897.html
2020-08-18 07:04:25 -04:00
Takashi Yano via Cygwin-patches 70d02aaca6 Cygwin: pty: Change the timing of set_locale() call again.
- After commit 095972ce5b, charset
  conversion in mintty is broken if charset is set to other than
  UTF-8. This seems to be caused because mintty does not set locale
  yet at fork() call. This patch changes the timing of set_locale()
  call again to avoid this issue.
2020-08-17 11:12:59 +02:00
Takashi Yano via Cygwin-patches 095972ce5b Cygwin: pty: Change the timing of setup_locale() call.
- If native app is exec()'ed in a new pty, setup_locale() loses the
  chance to be called. For example, with "mintty -e cmd", charset
  conversion does not work as expected. This patch fixes the issue.
2020-08-13 10:20:53 +02:00
Takashi Yano f14d123ac6 Cygwin: pty: Add a workaround for issue of starting a lot of mintty.
- If a lot of mintty are started in a short time from a mintty, some
  of them hang with empty screen, crash immediately or hang on exiting
  mintty. The following report seems to be related to this issue.
    https://cygwin.com/pipermail/cygwin/2020-August/245751.html
  The cause is not clear at all, but this patch seems to solve the
  issue.
2020-08-11 12:19:33 +02:00
Corinna Vinschen 50ad198085 Cygwin: Add 'fallthrough' pseudo keyword for switch/case use
This patch has been inspired by the Linux kernel patch

  294f69e662d1 compiler_attributes.h: Add 'fallthrough' pseudo keyword for switch/case use

written by Joe Perches <joe AT perches DOT com> based on an idea from
Dan Carpenter <dan DOT carpenter AT oracle DOT com>.  The following text
is from the original log message:

Reserve the pseudo keyword 'fallthrough' for the ability to convert the
various case block /* fallthrough */ style comments to appear to be an
actual reserved word with the same gcc case block missing fallthrough
warning capability.

All switch/case blocks now should end in one of:

	break;
	fallthrough;
	goto <label>;
	return [expression];
	continue;

In C mode, GCC supports the __fallthrough__ attribute since 7.1,
the same time the warning and the comment parsing were introduced.

Cygwin-only: add an explicit -Wimplicit-fallthrough=5 to the build
flags.
2020-08-05 21:58:22 +02:00
Corinna Vinschen 5898a044c3 Cygwin: Fix missing breaks in switch statement
Two switch statements in sysconf() and
fhandler_fifo::take_ownership were missing breaks.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-05 21:46:53 +02:00
Ken Brown 4f5b52ffe7 Cygwin: FIFO: add a third pass to raw_read
Currently raw_read makes two passes through the list of clients.  On
the first pass it tries to read from the client from which it last
read successfully.  On the second pass it tries to read from all
connected clients.

Add a new pass in between these two, in which raw_read tries to read
from all clients that are in the fc_input_avail case.  This should be
more efficient in case select was previously called and detected input
available.

Slightly tweak the first pass.  If a client is marked as having the
last successful read but reading from it now finds no input, don't
unmark it unless we successfully read from a different client on one
of the later passes.
2020-08-04 08:15:19 -04:00
Ken Brown 55b93b27d6 Cygwin: FIFO: fix indentation 2020-08-04 08:15:19 -04:00
Ken Brown 0fda55133a Cygwin: FIFO: synchronize the fifo_reader and fifosel threads
The fifo_reader thread function and the function select.cc:peek_fifo()
can both change the state of a fifo_client_handler.  These changes are
made under fifo_client_lock, so there is no race, but the changes can
still be incompatible.

Add code to make sure that only one of these functions can change the
state from its initial fc_listening state.  Whichever function does
this calls the fhandler_fifo::record_connection method, which is now
public so that peek_fifo can call it.

Slightly modify that method to make it suitable for being called by
peek_fifo.

Make a few other small changes to the fifo_reader thread function to
change how it deals with the STATUS_PIPE_CLOSING value that can
(rarely) be returned by NtFsControlFile.

Add commentary to fhandler_fifo.cc to explain fifo_client connect
states and where they can be changed.
2020-08-04 08:15:19 -04:00
Ken Brown 251624a352 Cygwin: FIFO: don't read from pipes that are closing
Don't try to read from fifo_client_handlers that are in the fc_closing
state.  Experiments have shown that this always yields
STATUS_PIPE_BROKEN, so it just wastes a Windows system call.

Re-order the values in enum fifo_client_connect_state to reflect the
new status of fc_closing.
2020-08-04 08:15:19 -04:00
Ken Brown 289af73a89 Cygwin: FIFO: reorganize some fifo_client_handler methods
Rename the existing set_state() to query_and_set_state() to reflect
what it really does.  (It queries the O/S for the pipe state.)  Add a
new set_state() method, which is a standard setter, and a
corresponding getter get_state().
2020-08-04 08:15:19 -04:00
Ken Brown 6ed067a0ae Cygwin: FIFO: add a timeout to take_ownership
fhandler_fifo::take_ownership() is called from select.cc::peek_fifo
and fhandler_fifo::raw_read and could potentially block indefinitely
if something goes wrong.  This is always undesirable in peek_fifo, and
it is undesirable in a nonblocking read.  Fix this by adding a timeout
parameter to take_ownership.

Arbitrarily use a 1 ms timeout in peek_fifo and a 10 ms timeout in
raw_read.  These numbers may have to be tweaked based on experience.

Replace the call to cygwait in take_ownership by a call to WFSO.
There's no need to allow interruption now that we have a timeout.
2020-08-04 08:15:19 -04:00
Ken Brown 6acce025d0 Cygwin: FIFO: fix timing issue with owner change
fhandler_fifo::take_ownership() tacitly assumes that the current
owner's fifo_reader_thread will be woken up from WFMO when
update_needed_evt is signaled.  But it's possible that the the current
owner's fifo_reader_thread is at the beginning of its main loop rather
than in its WFMO call when that event is signaled.

In this case the owner will never see that the event has been
signaled, and it will never update the shared fifo_client_handlers.
The reader that wants to take ownership will then spin its wheels
forever.

Fix this by having the current owner call update_shared_handlers at
the beginning of its loop, if necessary.
2020-08-04 08:02:07 -04:00
Ken Brown e319fd0e62 Cygwin: FIFO: lock fixes
Add some missing locks and remove one extra unlock.  Clarify for some
functions whether caller or callee acquires lock, and add appropriate
comments.
2020-08-04 07:57:45 -04:00
Brian Inglis cb7fba2f3e fhandler_proc.cc(format_proc_cpuinfo): use _small_sprintf %X for microcode
microcode is unsigned long long, printed by _small_sprintf using %x;
Cygwin32 used last 4 bytes of microcode for next field MHz, printing 0;
use correct _small_sprintf format %X to print microcode, producing
correct MHz value under Cygwin32
2020-08-04 10:10:40 +02:00
Brian Inglis 4ecc804d54 fhandler_proc.cc(format_proc_cpuinfo): add SERIALIZE instruction flag
CPUID 7:0 EDX[14] serialize added in linux-next 5.8 by Ricardo Neri-Calderon:
The Intel architecture defines a set of Serializing Instructions (a
detailed definition can be found in Vol.3 Section 8.3 of the Intel "main"
manual, SDM). However, these instructions do more than what is required,
have side effects and/or may be rather invasive. Furthermore, some of
these instructions are only available in kernel mode or may cause VMExits.
Thus, software using these instructions only to serialize execution (as
defined in the manual) must handle the undesired side effects.

As indicated in the name, SERIALIZE is a new Intel architecture
Serializing Instruction. Crucially, it does not have any of the mentioned
side effects. Also, it does not cause VMExit and can be used in user mode.

This new instruction is currently documented in the latest "extensions"
manual (ISE). It will appear in the "main" manual in the future.

https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/arch/x86/include/asm/cpufeatures.h?id=85b23fbc7d88f8c6e3951721802d7845bc39663d
2020-08-04 10:10:40 +02:00
Corinna Vinschen 3fbfcd11fb Cygwin: posix_spawn: add Cygwin-specific code fixing process synchronisation
Newlib's posix_spawn has been taken from FreeBSD.  The code relies on
BSD-specific behaviour of vfork, namely the fact that vfork blocks
the parent until the child exits or calls execve as well as the fact
that the child shares parent memory in non-COW mode.

This behaviour can't be emulated by Cygwin.  Cygwin's vfork is
equivalent to fork.  This is POSIX-compliant, but it's lacking BSD's
vfork ingrained synchronization of the parent to wait for the child
calling execve, or the chance to just write a variable and the parent
will see the result.

So this requires a Cygwin-specific solution.  The core function of
posix_spawn, called do_posix_spawn is now implemented twice, once using
the BSD method, and once for Cygwin using Windows synchronization under
the hood waiting for the child to call execve and signalling errors
upstream.  The Windows specifics are hidden inside Cygwin, so newlib
only calls internal Cygwin functions.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-08-03 12:41:44 +02:00
Brian Inglis 0947efb859 fhandler_proc.cc(format_proc_cpuinfo): add flags and TLB size
update to Linux-next 5.8 order fields and flags:
add amd_dcm, arch_lbr, arch_perfmon, art, cpuid, extd_apicid, ibpb,
ibrs, ibrs_enhanced, nonstop_tsc_s3, nopl, rep_good, ring3mwait, ssbd,
stibp, tsc_known_freq, tsc_reliable, xtopology flags;
add TLB size line;
add ftuprint macro for feature test unconditional flag print;
add commented out flags requiring CR or MSR access in print order with
comment explaining issue;
make cpuid leaf numbers consistent 8 hex digits for searching
2020-07-24 10:10:47 +02:00
Corinna Vinschen 1c803a6d88 Cygwin: mmap: Remove AT_ROUND_TO_PAGE workaround
It's working on 32 bit OSes only anyway. It even fails on WOW64.

Drop unsupported NtMapViewOfSection flags.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-07-22 10:51:27 +02:00
Ken Brown eeb2dc1537 Cygwin: mmap: document recent bugfix 2020-07-21 17:57:37 -04:00
Corinna Vinschen 119e8d5c11 Cygwin: mmap: constify pagesize throughout
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-07-20 18:08:33 +02:00
Ken Brown d8a8d2ce59 Cygwin: mmap: fix mapping beyond EOF on 64 bit
Commit 605bdcd410 enabled mapping beyond
EOF in 64 bit environments.  But the variable 'orig_len' did not get
rounded up to a multiple of 64K.  This rounding was done on 32 bit
only.  Fix this by rounding up orig_len on 64 bit, in the same place
where 'len' is rounded up.

Rounding up is needed to make sigbus_page_len a multiple of the
allocation granularity.

In addition, failing to round up could cause orig_len to be smaller
than len.  Since these are both unsigned values, the statement
'orig_len -= len' could then cause orig_len to be huge, and mmap would
fail with errno EFBIG.

I observed this failure while debugging the problem reported in

  https://sourceware.org/pipermail/cygwin/2020-July/245557.html.

The failure can be seen by running the test case in that report under
gdb or strace.
2020-07-20 11:56:29 -04:00
Takashi Yano via Cygwin-patches e0a53d6625 Cygwin: pty: Fix a bug on redirecting something to /dev/pty*.
- After commit 0365031ce1, key input
  becomes not working by following steps.
   1) Start cmd.exe in mintty.
   2) Open another mintty.
   3) Execute "echo AAA > /dev/pty*" (pty* is the pty opened in 1.)
  This patch fixes the issue.
2020-07-20 10:06:37 +02:00
Corinna Vinschen 2aa3eb7503 Cygwin: sockets: Rearrange check for connect failure
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-07-20 09:49:34 +02:00
Ken Brown 53b7116705 Cygwin: FIFO: document recent fixes 2020-07-16 16:21:03 -04:00
Ken Brown ac371ee1ba Cygwin: FIFO: update commentary 2020-07-16 15:59:53 -04:00
Ken Brown f56dc33579 Cygwin: FIFO: clean up
Remove the fhandler_fifo::get_me method, which is no longer used.
Make the methods get_owner, set_owner, owner_lock, and owner_unlock
private.
2020-07-16 15:59:53 -04:00
Ken Brown 4eaa55463d Cygwin: FIFO: allow take_ownership to be interrupted
Use cygwait in take_ownership to allow interruption while waiting to
become owner.  Return the cygwait return value or a suitable value to
indicate an error.

raw_read now checks the return value and acts accordingly.
2020-07-16 15:59:53 -04:00
Ken Brown a4dc0eb15c Cygwin: fhandler_fifo::take_ownership: don't set event unnecessarily
Don't set update_needed_evt if there's currently no owner.  This will
cause unnecessary churn once I'm the owner and am listening for
connections.
2020-07-16 15:59:53 -04:00
Ken Brown 4f25d82cb1 Cygwin: FIFO: add missing lock 2020-07-16 15:59:53 -04:00
Ken Brown d3a01b7ec2 Cygwin: FIFO: make certain errors non-fatal
If update_my_handlers fails to duplicate one or more handles, just
mark the corresponding handlers as being in an error state.

But if update_my_handlers is unable to open the process of the
previous owner, it's likely that something serious has gone wrong, so
we continue to make that a fatal error.
2020-07-16 15:59:53 -04:00
Ken Brown b0418138fe Cygwin: FIFO: fix indentation 2020-07-16 15:59:53 -04:00
Ken Brown 6b8a829496 Cygwin: FIFO: improve taking ownership in fifo_reader_thread
When a reader takes ownership in fifo_reader_thread, it now goes
directly to the part of the main loop that listens for a connection.
Previously it went back to the beginning of the loop.

Also, if the reader has to delay taking ownership because the previous
owner has not finished updating the shared fifo_client handlers, it
now checks to see if cancel_evt has been set.  Previously it might
have had to spin its wheels unnecessarily only to eventually find that
its thread had been canceled.
2020-07-16 15:59:53 -04:00
Ken Brown 1c0cf5f4f9 Cygwin: FIFO: reduce I/O interleaving
Add a bool member 'last_read' to the fifo_client_handler structure,
which is set to true on a successful read.  This is used by raw_read
as follows.

When raw_read is called, it first locates the writer (if any) for
which last_read is true.  raw_read tries to read from that writer and
returns if there is input available.  Otherwise, it proceeds to poll
all the writers, as before.

The effect of this is that if a writer writes some data that is only
partially read, the next attempt to read will continue to read from
the same writer.  This should reduce the interleaving of output from
different writers.
2020-07-16 15:59:53 -04:00
Ken Brown e10425e1e3 Cygwin: fhandler_fifo::hit_eof: improve reliability
Use the writer count introduced in the previous commit to help detect
EOF.  Drop the maybe_eof method, which is no longer needed.
2020-07-16 15:59:53 -04:00
Ken Brown 8ca713d70a Cygwin: FIFO: keep a writer count in shared memory
When a reader opens, it needs to block if there are no writers open
(unless is is opened with O_NONBLOCK).  This is easy for the first
reader to test, since it can just wait for a writer to signal that it
is open (via the write_ready event).  But when a second reader wants
to open, all writers might have closed.

To check this, use a new '_nwriters' member of struct fifo_shmem_t,
which keeps track of the number of open writers.  This should be more
reliable than the previous method.

Add nwriters_lock to control access to shmem->_nwriters, and remove
reader_opening_lock, which is no longer needed.

Previously only readers had access to the shared memory, but now
writers access it too so that they can increment _nwriters during
open/dup/fork/exec and decrement it during close.

Add an optional 'only_open' argument to create_shmem for use by
writers, which only open the shared memory rather than first trying to
create it.  Since writers don't need to access the shared memory until
they have successfully connected to a pipe instance, they can safely
assume that a reader has already created the shared memory.

For debugging purposes, change create_shmem to return 1 instead of 0
when a reader successfully opens the shared memory after finding that
it had already been created.

Remove check_write_ready_evt, write_ready_ok_evt, and
check_write_ready(), which are no longer needed.

When opening a writer and looping to try to get a connection, recheck
read_ready at the top of the loop since the number of readers might
have changed.

To slightly speed up the process of opening the first reader, take
ownership immediately rather than waiting for the fifo_reader_thread
to handle it.
2020-07-16 15:59:53 -04:00
Ken Brown da9fea0759 Cygwin: FIFO: fix problems finding new owner
When the owning reader closes and there are still readers open, the
owner needs to wait for a new owner to be found before closing its
fifo_client handlers.  This involves a loop in which dec_nreaders is
called at the beginning and inc_nreaders is called at the end.  Any
other reader that tries to access shmem->_nreaders during this loop
will therefore get an inaccurate answer.

Fix this by adding an nreaders method and using it instead of
dec_nreaders and inc_nreaders.  Also add nreaders_lock to control
access to the shmem->_nreaders.

Make various other changes to improve the reliability of finding a new
owner.
2020-07-16 15:59:53 -04:00
Corinna Vinschen 52ad92e1b6 Cygwin: document previous poll/select patch
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-07-16 10:58:40 +02:00