4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-08 18:19:08 +08:00

21673 Commits

Author SHA1 Message Date
Mark Geisert
c18037074c
Cygwin: Minor updates to load average calculations
Commentary wording now refers to tasks (i.e., threads) rather than
processes.  This makes it somewhat easier to justify adding two kinds of
counters together.  After researching what "load average" has meant over
time, we have what seems like a reasonable implementation, modulo
Windows differences to Linux.  The best resource I found is:
https://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html

At end of load_init(), obtain and discard the first measure of the
counters to deal with the first call always returning error, no data.
Follow this with a specific short delay so the next measure actually has
data to report.

At least one older version of Windows, i.e. Win10 Pro 21H1, has a different
name/location for the '% Processor Time' counter and is missing the
'Processor Queue Length' counter entirely.  Code is changed to support
both possible locations of the former and treat the missing latter as always
reporting 0.0.

A release note is added for 3.5.6.

Reported-by: Mark Liam Brown <brownmarkliam@gmail.com>
Addresses: https://cygwin.com/pipermail/cygwin/2024-August/256361.html
Signed-off-by: Mark Geisert <mark@maxrnd.com>
Fixes: 4dc982ddf60b (Cygwin: loadavg: improve debugging of load_init)
(cherry picked from commit 0135b94949d2c9ba0b0f839fae8ff49a5bfe8589)
2025-01-23 12:53:37 +00:00
Takashi Yano
f3a9f181b1 Cygwin: signal: Avoid frequent TLS lock/unlock for SIGCONT processing
It seems that current _cygtls::handle_SIGCONT() code sometimes falls
into a deadlock due to frequent TLS lock/unlock operation in the
yield() loop. With this patch, the yield() in the wait loop is placed
outside the TLS lock to avoid frequent TLS lock/unlock.

Fixes: 9ae51bcc51a7 ("Cygwin: signal: Fix another deadlock between main and sig thread")
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-01-22 19:18:41 +09:00
Takashi Yano
b838c8d21a Cygwin: Document hang fixs introduced in 3.5.5
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-01-22 08:04:32 +09:00
Mark Geisert
f9f66495e9 Cygwin: Add fd validation where needed in mq_* functions
Validate the fd returned by cygheap_getfd operating on given mqd.
A release note is provided for 3.5.6.

Reported-by: Christian Franke <Christian.Franke@t-online.de>
Addresses: https://cygwin.com/pipermail/cygwin/2025-January/257090.html
Signed-off-by: Mark Geisert <mark@maxrnd.com>
Fixes: 46f3b0ce85a9 (Cygwin: POSIX msg queues: move all mq_* functionality into fhandler_mqueue)
(cherry picked from commit 9a4a44523bb71e1fd453cdebe2182c9ab913a222)
2025-01-21 17:27:00 +01:00
Takashi Yano
12faeffa3c Cygwin: signal: Do not handle signal when __SIGFLUSHFAST is sent
The commit a22a0ad7c4f0 was not entirely correct. Even with the patch,
some hangs still occur. This patch overrides the previous commit along
with the patch that makes cygwait() reentrant, to fix these hangs.

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256954.html
Fixes: d243e51ef1d3 ("Cygwin: signal: Fix deadlock between main thread and sig thread")
Reported-by: Daisuke Fujimura <booleanlabel@gmail.com>
Reviewed-by: Corinna Vinschen <corinna@vinshcen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 83afe3e238cd12fb7d4799ba6b3c77e9e3618d91)
2025-01-21 23:32:27 +09:00
Takashi Yano
176a30642a Cygwin: cygwait: Make cygwait() reentrant
To allow cygwait() to be called in the signal handler, a locally
created timer is used instead of _cygtls::locals.cw_timer if it is
in use.

Co-Authored-By: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit ea1914000adcbed5c16fc0ba2676173b2f6016c4)
2025-01-21 23:29:02 +09:00
Corinna Vinschen
d9a45477cc Cygwin: doc: Remove executable name setup.exe from user guide
Use "Cygwin Setup program" and add a link to the homepage in some
cases.

Reported-by: David Dyck <david.dyck@gmail.com>
Reviewed-by: Jon Turney <jon.turney@dronecode.org.uk>
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 4adb6c380659c6f74f7971ceb93c82558331f74a)
2025-01-17 12:21:31 +01:00
Corinna Vinschen
050e44dc89 Cygwin: doc: clarify usage of sshd vs. cygsshd
When Microsoft started to claim the "sshd" service name, we had
to rename our service to "cygsshd" but we never updated the docs.

Make sure the docs reflect the changed service name and fix a
few bumpy expression.

Reported-by: Mario Emmenlauer <mario@emmenlauer.de>
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit d21eb90bbcadb30a00abf59dd64375601c108f96)
2025-01-17 11:46:15 +01:00
Corinna Vinschen
e9d83491a9 Cygwin: doc: remove "faq.using.sshd-in-domain"
This FAQ entry has been commented out for quite some time.
Drop it.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 37ed066c5f76d179c83342a816d72f996a8af3d2)
2025-01-17 11:46:15 +01:00
Corinna Vinschen
b1199ab7b0 Cygwin: unlink/rename: fix skipping deletion with POSIX semantics
unlink_nt() and rename2 () both check for the FILE_SUPPORTS_OPEN_BY_FILE_ID
flag to use POSIX delete/rename semantics.  Both erroneously check the flag
against the file attributes using has_attributes().

Given that this flag is a filesystem flag, check using fs_flags() instead.

Fixes: fe2545e9faaf ("Cygwin: don't use unlink/rename POSIX semantics on certain NTFS")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 264544bf72f6ed85530a1b058e9efdb73ab72e90)
2025-01-12 23:12:19 +01:00
Corinna Vinschen
5f7ed4659b Cygwin: access: Fix X_OK behaviour for backup operators and admins
After commit a0933cd17d19, access(_, X_OK) returns 0 if the user
holds SE_BACKUP_PRIVILEGE, even if the file's ACL denies execution
to the user.  This is triggered by trying to open the file with
FILE_OPEN_FOR_BACKUP_INTENT.

Fix check_file_access() so it checks for X_OK without specifying
the FILE_OPEN_FOR_BACKUP_INTENT flag if the file is not a directory.

Rearrange function slightly and add comments for easier comprehension.

Fixes: a0933cd17d19 ("Cygwin: access: Correction for samba/SMB share")
Reported-by: Bruno Haible <bruno@clisp.org>
Co-authored-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 2e4db338ac125579d555aeee516e48588a628a16)
2025-01-08 16:13:51 +01:00
Corinna Vinschen
2f6a0a82fa bump DLL version to 3.5.6
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2024-12-20 16:39:35 +01:00
Ken Brown
5bb9e8875d Cygwin: mmap_list::try_map: fix a condition in a test of an mmap request
In testing whether the requested area is contained in an existing
mapped region, an incorrect condition was used due to a
misinterpretation of the u_addr and u_len variables.

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256913.html
Fixes: c68de3a262fe5 ("* mmap.cc (class mmap_record):
Declare new map_pages method with address parameter.")
Signed-off-by: Ken Brown <kbrown@cornell.edu>
(cherry picked from commit 67bef16f7edf8642366ff55399bf9cf007c66d52)
cygwin-3.5.5
2024-12-19 10:47:57 -05:00
Ken Brown
fe598b89e8 Cygwin: mmap: fix protection when unused pages are recycled
Previously, when unused pages from an mmap_record were recycled, they
were given the protection of the mmap_record rather than the
protection requested in the mmap call.  Fix this by adding a
"new_prot" parameter to mmap_list::try_map() and
mmap_record::map_pages() to keep track of the requested protection.
Then use new_prot in the calls to VirtualProtect().

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256911.html
Fixes: f90e23f2714cb ("*autoload.cc (NtCreateSection): Define.")
Signed-off-by: Ken Brown <kbrown@cornell.edu>
(cherry picked from commit 677e3150907a83f17e50d546f79b7ca863ebd77d)
2024-12-19 10:47:56 -05:00
Takashi Yano
aecd8077bf Cygwin: signal: Fix high load when retrying to process pending signal
The commit e10f822a2b39 has a problem that CPU load gets high if
pending signal is not processed successfully for a long time.
With this patch, wait_sig() calls Sleep(1), rather than yield(),
if the pending signal has not been processed successfully for a
predetermined time to prevent CPU from high load.

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256884.html
Fixes: e10f822a2b39 ("Cygwin: signal: Handle queued signal without explicit __SIGFLUSH")
Reported-by: 凯夏 <walkerxk@gmail.com>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 1d1451ccd2a6c0f0146ddee68f386061b69863c0)
2024-12-12 21:40:48 +09:00
Takashi Yano
837e596f42 Cygwin: access: Correction for samba/SMB share
Previously, access() and eaccess() does not determine the permissions
for files on samba/SMB share correctly. Even if the user logs-in as
the owner of the file, access() and eaccess() referes to others'
permissions. With this patch, to determine the permissions correctly,
NtOpenFile() with desired access mask is used.

Fixes: cf762b08cfb0 ("* security.cc (check_file_access): Create.")
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit a0933cd17d19cca72b972a248e70210152f06808)
2024-12-11 07:33:35 +09:00
Corinna Vinschen
dac9ad1e0d Cygwin: path_conv: allow NULL handle in init_reopen_attr()
init_reopen_attr() doesn't guard against a NULL handle.  However,
there are scenarios calling functions deliberately with a NULL handle,
for instance, av::setup() calling check_file_access() only if opening
the file did NOT succeed.

So check for a NULL handle in init_reopen_attr() and if so, use the
name based approach filling the OBJECT_ATTRIBUTES struct, just as in
the has_buggy_reopen() case.

Fixes: 4c9d01fdad2a ("* mount.h (class fs_info): Add has_buggy_reopen flag and accessor methods.")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 815eba882e32ecadd6862c71c36fccdcb0842a76)
2024-12-10 16:57:10 +01:00
Takashi Yano
db55d548e3 Cygwin: Document several fixes for signal handling in release note
(cherry picked from commit cfadd852aa5787eb29403efc75bfa637028bfd40)
2024-12-06 11:43:30 +01:00
Takashi Yano
b38ba4b9af Cygwin: signal: Introduce a lock for the signal queue
Currently, the signal queue is touched by the thread sig as well as
other threads that call sigaction_worker(). This potentially has
a possibility to destroy the signal queue chain. A possible worst
result may be a self-loop chain which causes infinite loop. With
this patch, lock()/unlock() are introduce to avoid such a situation.

Fixes: 474048c26edf ("* sigproc.cc (pending_signals::add): Just index directly into signal array rather than treating the array as a heap.")
Suggested-by: Corinna Vinschen <corinna@vinschen.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 496fa7b2ce0052550eab8900723ebb59c33d25e7)
2024-12-06 11:43:26 +01:00
Takashi Yano
5bd4f9936f Cygwin: signal: Remove queue entry from the queue chain when cleared
The queue is cleaned up by removing the entries having si_signo == 0
while processing the queued signals, however, sigpacket::process() may
set si_signo in the queue to 0 of the entry already processed but not
succeed by calling sig_clear(). This patch ensures the sig_clear()
to remove the entry from the queue chain. For this purpose, the pointer
prev has been added to the sigpacket. This is to handle the following
case appropriately.

Consider the queued signal chain of:
A->B->C->D
without pointer prev. Assume that the pointer 'q' and 'qnext' point to
C, and process() is processing C. If B is cleared in process(), A->next
should be set to to C in sigpacket::clear().

Then, if process() for C succeeds, C should be removed from the queue,
so A->next should be set to D. However, we cannot do that because we do
not have the pointer to A in the while loop in wait_sig().

With the pointer prev, we can easily access A and C in sigpacket::clear()
as well as A and D in the while loop in wait_sig() using the pointer prev
and next without pursuing the chain.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 9d2155089e87 ("(wait_sig): Define variable q to be the start of the signal queue.  Just iterate through sigq queue, deleting processed or zeroed signals")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit d565aca46f06117ef16ec37c51767a5e140ee9e2)
2024-12-06 11:43:22 +01:00
Corinna Vinschen
2f9258b1c5 Cygwin: Add setjmp/longjmp fix to 3.5.5 release messages
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit f71550e0dbef51e0a4a706edae49c33c5372db19)
2024-12-06 11:43:17 +01:00
Corinna Vinschen
9c34e02991 Cygwin: setjmp/longjmp: drop setting spinning flag
Per the comment in _cygtls::interrupt_now(), the spinning flag
is supposed to guard that the targeted thread is about to enter
the Cygwin DLL.  Setting spinning has then been added to _sigfe,
_sigbe, sigdelayed and stabilize_sig_stack, the latter being called
from setjmp/longjmp.

However, setjmp/longjmp only enter the DLL in case of a pending
signal, calling _cygtls::call_signal_handler(). This in turn is
already guarded by setting the incyg flag, and there's no other
action in stabilize_sig_stack which might interfere with the
signal setup.  All the rest of setjmp/longjmp is plain userspace.

Therefore, drop setting the spinning flag from stabilize_sig_stack,
because it results in dropped signals in tight longjmp loops.

Fixes: edc4f86ad2827 ("* Makefile.in (clean): Remove sigfe.s.")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 96d856320a1d740546eaf8a6c0ddb3d489e10492)
2024-12-06 11:43:17 +01:00
Corinna Vinschen
52d1c851fb Cygwin: cygtls: add volatile qualifier to spinning
Given that spinning is only checked once at the start of a
_cygtls::interrupt_now() which is called in a loop, it's probably
not necessary to mark _cygtls::spinning as volatile.

However, spinning is changed from assembler code and we don't
want the compiler to make funny assumptions, so, better safe than
sorry.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 3a9fb7c561d9e8a78e444dd3920c57e857a92baa)
2024-12-06 11:43:17 +01:00
Corinna Vinschen
60fa8d793f Cygwin: setjmp/longjmp: decrement incyg after signal handling
Commit 0b6fbd396ca2f ("* exceptions.cc (_cygtls::interrupt_now): Revert
to checking for "spinning" when choosing to defer signal.") introduced
a bug in the loop inside the stabilize_sig_stack subroutine:

First, stabilize_sig_stack grabs the stacklock. The _cygtls::incyg
flag is then incremented before checking if a signal has to be handled
for the current thread.

If no signal waits, the code simply jumps out, decrements _cygtls::incyg
and returns to the caller, which eventually releases the stacklock.

However, if a signal is waiting, stabilize_sig_stack releases the
stacklock, calls _cygtls::call_signal_handler(), and returns to
the start of the subroutine, trying to grab the lock.

After grabbing the lock, it increments _cygtls::incyg... wait...
again?

The loop does not decrement _cygtls::incyg after
_cygtls::call_signal_handler(), which returns with _cygtls::incyg
set to 1.  So it increments incyg to 2.  If no other signal is
waiting, stabilize_sig_stack jumps out and decrements _cygtls::incyg
to 1.  Eventually, setjmp or longjmp both will return to user
code with _cygtls::incyg set to 1.  This *may* be fixed at some later
point when signals arrive, but there will be a time when the application
runs in user code with broken signal handling.

Fixes: 0b6fbd396ca2f ("* exceptions.cc (_cygtls::interrupt_now): Revert to checking for "spinning" when choosing to defer signal.")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 41e1013e6846f1774dfec085ff983990f67e6437)
2024-12-06 11:43:17 +01:00
Takashi Yano
7adfc92401 Cygwin: signal: Increase chance of handling signal in main thread
If the process() fails and the signal remains in the queue, the most
possible reason is that the target thread is already armed by another
signal and does not handle it yet. With this patch, to increase the
chance of handling it in the other threads, call yield() before
retrying process().

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: e10f822a2b39 ("Cygwin: signal: Handle queued signal without explicit __SIGFLUSH")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by:
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit c48d58d838d90df6247123c487686699742d75de)
2024-12-06 11:42:48 +01:00
Takashi Yano
e9fdf6f420 Cygwin: signal: Optimize the priority of the sig thread
Previously, the sig thread ran in THREAD_PRIORITY_HIGHEST priority.
This causes a critical delay in the signal handling in the main
thread if too many signals are received rapidly and the CPU is very
busy. In this case, most of the CPU time is allocated to the sig
thread, so the main thread cannot have a chance of handling signals.
With this patch, to avoid such a situation, the priority of the sig
thread is set to THREAD_PRIORITY_NORMAL priority.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 53ad6f1394aa ("(cygthread::cygthread): Use three only arguments for detached threads, and start the thread via QueueUserAPC/async_create.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 9a274a967d1fe9a28d0d658749b08e65d09d3ee0)
2024-12-06 11:42:43 +01:00
Takashi Yano
8fa1c02625 Cygwin: signal: Fix a short period of deadlock
The main thread waits for the sig thread to read the signal pipe by
calling Sleep(10) if writing to the signal pipe has failed. However,
if the signal thread waiting for another signal being handled in the
main thread, the sig thread does not read the signal pipe. To avoid
such a situation, this patch replaces Sleep(10) to cygwait().

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 6f05b327678f ("(sig_send): Retry WriteFiles which fail when there is no error but packbytes have not been sent.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 2544e753963e9c15a9e3fe35188d8dea7b39d748)
2024-12-06 11:42:32 +01:00
Takashi Yano
da6d5f838c Cygwin: cygtls: Prompt system to switch tasks explicitly in lock()
This patch calls Sleep(0) in the wait loop in lock() to increase the
chance of being unlocked in other threads. The lock(), unlock() and
locked() are moved from sigfe.s to cygtls.h so that allows inline
expansion.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 61522196c715 ("* Merge in cygwin-64bit-branch.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 9b7a84d24aa17bdd727eb402cc544fa36f4d812e)
2024-12-06 11:41:52 +01:00
Corinna Vinschen
3a28aac428 Cygwin: gendef: unify comments in terms of acquiring/releasing stacklock
Various forms of describing what we do with the stacklock are
used. Try to be consistent.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 63804a28b330ccda7e1ef1c04d2d1c5e22a8e40d)
2024-12-06 11:41:32 +01:00
Takashi Yano
f930a973bd Cygwin: signal: Fix another deadlock between main and sig thread
In _cygtls::handle_SIGCONT(), the sig thread waits for the main thread
to process the signal without unlocking the TLS area. This causes a
deadlock if the main thread tries to acquire a lock for the TLS area
in the meantime. With this patch, unlock the TLS before calling yield()
in handle_SIGCONT().

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 26158dc3e9c2("* exceptions.cc (sigpacket::process): Lock _cygtls area of thread before accessing it.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 9ae51bcc51a7901559c476e6301597760c2726fd)
2024-12-06 11:30:24 +01:00
Takashi Yano
e1b6996e7b Cygwin: signal: Drop unnecessary queue flush
Previously, the retry flag was always set when pending_signal::pending()
was called. However, if the queue is empty sig thread tries to flush
the queue even though it is not necessary. With this patch, the retry
flag is set only if the queue is not empty.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 5e31c80e4e8d ("(pending_signals::pending): Force an additional loop through wait_sig by setting retry whenever this function is called.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 57ce5f1e0bf4a6fef7173b2549edc4f2090dd0e7)
2024-12-06 11:30:19 +01:00
Takashi Yano
09be373e45 Cygwin: signal: Handle queued signal without explicit __SIGFLUSH
With the previous code, the queued signal is tried to resend only when
a new signal arrives or pending_signals::pending() is called.
With this patch, if the signal is queued and the retry flag is not set
and the new signal is not received yet, the sig thread tries to handle
the queued signal again. Without this patch, the chance to handle the
queue would be delayed.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 5e31c80e4e8d ("(pending_signals::pending): Force an additional loop through wait_sig by setting retry whenever this function is called.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit e10f822a2b39bade20858ba3c704a8bb7d965cb4)
2024-12-06 11:30:13 +01:00
Takashi Yano
a951054d99 Cygwin: signal: Fix deadlock between main thread and sig thread
Previously, a deadlock happened if many SIGSTOP/SIGCONT signals were
received rapidly. If the main thread sends __SIGFLUSH at the timing
when SIGSTOP is handled by the sig thread, but not is handled by the
main thread yet (sig_handle_tty_stop() not called yet), and if SIGCONT
is received, the sig thread waits for cygtls::current_sig (is SIGSTOP
now) cleared. However, the main thread waits for the pack.wakeup using
WaitForSingleObject(), so the main thread cannot handle SIGSTOP. This
is the mechanism of the deadlock. This patch uses cygwait() instead of
WaitForSingleObject() to be able to handle the pending SIGSTOP.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256744.html
Fixes: 7759daa979c4 ("(sig_send): Fill out sigpacket structure to send to signal thread rather than racily sending separate packets.")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit d243e51ef1d30312ba1e21b4d25a1ca9a8dc1f63)
2024-12-06 11:30:09 +01:00
Kito Cheng
bbf8484a5b libm/common: Fix nextafter and nextafterf when x == y
That according to C99/POSIX, nextafter(x,y) should return y if x==y.

[1] NetBSD fix for this: 3bc6852241
[2] glibc fix for this: bc9f6000f6 (diff-bcc0628a39c3c2003047dcb5a40a8b50c00f01a74b1c8c1100d770a8e48b1ce2)
[3] Linux man page: https://man7.org/linux/man-pages/man3/nextafter.3.html

(cherry picked from commit 7ccfe49e8ce908c581f82c8dbf0f9064df488a24)
2024-12-05 11:57:37 +01:00
Corinna Vinschen
bb854e0f11 Cygwin: try_to_bin: transpose deleted file name to valid Unicode chars
Since commit 314c2d2fedc5f ("* syscalls.cc (try_to_bin): Handle remote
shares as well.") try_to_bin() transposes the .cyg prefix for temporary
files to invalid low surrogate halfs on filesystems setting the
FILE_UNICODE_ON_DISK flag.

This works on NTFS, but not necessarily on other filesystems, which often
require all chars in a filename to be valid Unicode chars.  Fix this by
transposing into the private use area instead.

Fixes: 314c2d2fedc5f ("* syscalls.cc (try_to_bin): Handle remote shares as well.")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 0924d5f1078b368b6e290d11c9f3d57bc7767576)
2024-12-04 22:56:37 +01:00
Radek Bartoň
c8e978a3e5 Cygwin: Fix compatibility with GCC 15
Signed-off-by: Radek Bartoň <radek.barton@microsoft.com>
(cherry picked from commit c79954439e6d91dc6f421a761953f0d935c5f3d5)
2024-12-04 19:08:01 +01:00
Radek Barton
fff6372506 Change various declarations to ANSI style to avoid problems with gcc 15
(cherry picked from commit d636b11d2e167cbcc310000ac1a977219b819fee)
2024-12-04 19:08:01 +01:00
Takashi Yano
9af37caf78 Cygwin: termios: Trim buffer size for GetConsoleProcessList()
Currently, the buffer of 128KB is passed to GetConsoleProcessList().
This causes page fault in the select() loop for console due to:
https://github.com/microsoft/terminal/issues/18264
because the previous code calls GetConsoleProcessList() with large
buffer and PeekConsoleInput() with small buffer alternately.
With this patch, the minimum buffer size is used that is determined
by GetConsoleProcessList() with small buffer passed.

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256841.html
Fixes: 72770148ad0a ("Cygwin: pty: Prevent pty from changing code page of parent console.")
Reported-by: Steven Buehler <buehlersj@outlook.com>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 1a49c17840660bc0e493b7db2d9ce5289906049d)
2024-12-04 20:55:28 +09:00
Christian Franke
96d729bc00 Cygwin: sched_setscheduler: allow changes of the priority
Behave like sched_setparam() if the requested policy is identical
to the fixed value (SCHED_FIFO) returned by sched_getscheduler().

Fixes: 9a08b2c02eea ("* sched.cc: New file.  Implement sched*.")
Signed-off-by: Christian Franke <christian.franke@t-online.de>
(cherry picked from commit 522f3e921aa034c6a146021a918ce8b479a9111e)
2024-11-27 18:08:53 +01:00
Christian Franke
d06ba04550 Cygwin: sched_getscheduler: fix error handling
Fixes: 6b2a2aa4af1e ("Add missing files.")
Signed-off-by: Christian Franke <christian.franke@t-online.de>
2024-11-27 16:35:31 +01:00
Takashi Yano
46474ecf57 Cygwin: sigtimedwait: Fix segfault when timeout is used
Previously, two bugs exist in sigtimedwait(). One is, that since
_my_tls.sigwait_mask was left non-zero if the signal arrives after
the timeout, sigpacket::process() would wrongly try to handle it.
The other is if a timeout occurs after sigpacket::process() is
called, but not completed yet, the signal handler can be called
accidentally. If the signal handler is set to SIG_DFL or SIG_IGN,
access violation will occur in both cases.

With this patch, in sigwait_common(), check if sigwait_mask == 0
to confirm that sigpacket::process() cleared it. In this case,
do not treat WAIT_TIMEOUT, but call cygwait() again to retrieve
the signal. Furthermore, sigpacket::process() checks whether
timeout occurs in sigwait_common() and if timeout already happens,
do not treat the signal as waited. In both cases, to avoid race
issues, the code is guarded by cygtls::lock().

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256762.html
Fixes: 24ff42d79aab ("Cygwin: Implement sigtimedwait")
Reported-by: Christian Franke <Christian.Franke@t-online.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 26144e4008cd8f7288f3387eea697bba4006e16f)
2024-11-22 19:28:11 +09:00
Corinna Vinschen
2196f93fda Cygwin: SetThreadName: avoid spurious debug message
The following debug message occassionally shows up in strace output:

  SetThreadName: SetThreadDescription() failed. 00000000 10000000

The HRESULT of 0x10000000 is not an error, rather the set bit just
indicates that this HRESULT has been created from an NTSTATUS value.

Use the IS_ERROR() macro instead of just checking for S_OK.

Fixes: d4689b99c686 ("Cygwin: Set threadnames with SetThreadDescription()")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
(cherry picked from commit 21a2c9db6952954608cdf92638b411b15e7606c6)
2024-11-20 17:12:09 +01:00
Takashi Yano
67b31bc4ae Cygwin: flock: Fix overlap handling in lf_setlock() and lf_clearlock()
Currently, create_lock_obj() can create multiple locks with the same
lock range that have different version number. However, lf_setlock()
and lf_clearlock() cannot handle this case appropriately. With this
patch, make lf_setlock() and lf_clearlock() find overlap again even
when ovcase = 1 (lock and overlap have the same lock range).

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256750.html
Fixes: 2e560a092c1c ("* flock.cc (LOCK_OBJ_NAME_LEN): Change to accommodate extra lf_ver field.")
Reported-by: Sebastian Feld <sebastian.n.feld@gmail.com>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 8dee07a1f12682307be07e12a7fd8d5c8ecc1e2b)
2024-11-20 21:34:21 +09:00
Takashi Yano
c4102f82dd Cygwin: lockf: Fix access violation in lf_clearlock().
The commit ae181b0ff122 has a bug that the pointer is referred bofore
NULL check in the function lf_clearlock(). This patch fixes that.

Addresses: https://cygwin.com/pipermail/cygwin/2024-November/256750.html
Fixes: ae181b0ff122 ("Cygwin: lockf: Make lockf() return ENOLCK when too many locks")
Reported-by: Sebastian Feld <sebastian.n.feld@gmail.com>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit e7ef920d7d0dcff8cfe7a0c914f803b8c78900bb)
2024-11-20 21:34:10 +09:00
Bernhard Übelacker
22474a6e5d Cygwin: check_dir_not_empty: Avoid leaving the allocated buffer.
The pointer pfni gets allocated the buffer at the begin,
and is used in the NtQueryDirectoryFile call before the loops.
In the loop the pointer pfni is also used as iterator.
Therefore it holds no longer the initial buffer at the call
to NtQueryDirectoryFile in the while conditition at the bottom.

Fixes: 28fa2a72f8106 ("* syscalls.cc (check_dir_not_empty): Check surplus directory entries")
Co-authored-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Bernhard Übelacker <bernhardu@mailbox.org>
(cherry picked from commit dbb8069df56cb68ea1167b3bc0ceb66fa6c35d3f)
2024-11-19 11:28:27 +01:00
Mark Geisert
b9060e06a6 Cygwin: Change pthread_sigqueue() to accept thread id
Change the first parameter of pthread_sigqueue() to be a thread id rather
than a thread pointer. The change is to match the Linux implementation of
this function.

The user-visible function prototype is changed in include/pthread.h.
The pthread_sigqueue() function is modified to work with a passed-in thread
id rather than an indirect thread pointer as before.  (It used to be
"pthread_t *thread", i.e., class pthread **.)  The release note for Cygwin
3.5.5 is updated.

Reported-by: Christian Franke <Christian.Franke@t-online.de>
Addresses: https://cygwin.com/pipermail/cygwin/2024-September/256439.html
Signed-off-by: Mark Geisert <mark@maxrnd.com>
Fixes: 50350cafb375 ("* cygwin.din (pthread_sigqueue): Export.")
(cherry picked from commit 1e8c92e21d386d2e4a29fa92e8258979ff19ae6b)
2024-11-12 13:09:19 +01:00
Shaobo Song
9dad29c238 Cygwin: pthread: Correct pthread_cleanup macros to avoid potential syntax errors
This commit revises `pthread_cleanup_push` and `pthread_cleanup_pop`
macros to use a `do { ... } while(0)` wrapper, preventing syntax errors
when used in certain contexts. The original code could fail when they
are wrapped within a `do { ... } while(0)`, causing unintended behavior
or compilation issues.

Example of error:

  #include <pthread.h>

  #define pthread_cleanup_push_wrapper(_fn, _arg) do { \
    pthread_cleanup_push(_fn, _arg); \
  } while (0)

  #define pthread_cleanup_pop_wrapper(_execute) do { \
    pthread_cleanup_pop(_execute); \
  } while (0)

  void cleanup_fn (void *arg) {}

  void *thread_func (void *arg)
  {
    pthread_cleanup_push_wrapper(cleanup_fn, NULL);
    pthread_cleanup_pop_wrapper(1);
    return NULL;
  }

  int main (int argc, char **argv) {
    pthread_t thread_id;
    pthread_create(&thread_id, NULL, thread_func, NULL);
  }

This would fail due to unmatched braces in the macro expansion. The new
structure ensures the macro expands correctly in all cases.

Fixes: 007276b30e0a ("* cygwin.din: Add _pthread_cleanup_push and _pthread_cleanup_pop.")
Signed-off-by: Shaobo Song <shnusongshaobo@gmail.com>
2024-11-11 13:36:42 +01:00
Takashi Yano
38ab2b77e3 Cygwin: console: Fix clean up conditions in close()
Previously, the condition to clean up input/output mode was based
on wrong premise. This patch fixes that.

Fixes: 8ee8b0c974d7 ("Cygwin: console: Use GetCurrentProcessId() instead of myself->dwProcessId")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 30d266947842fec82cae9a190bc8b5bf2e108cd0)
2024-11-08 20:52:26 +09:00
Takashi Yano
92f4e103d1 Cygwin: console: Use GetCurrentProcessId() instead of myself->dwProcessId
The commit 90ddab98780d uses myself->dwProcessId to get windows pid.
However, it will be overridden in stub process if exec() is called.
With this patch, GetCurrentProcessId() instead of myself->dwProcessId.

Fixes: 90ddab98780d ("Cygwin: console: Re-fix open() failure on exec() by console owner")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 8ee8b0c974d723d618182fbbdeb1e9186b69c6f3)
2024-11-06 22:50:57 +09:00
Takashi Yano
f307544150 Cygwin: console: Re-fix open() failure on exec() by console owner
Previous fix (commit df0953aa298c) fixes only a part of the problem.
Since exec() overrides the cygwin pid of the caller process, it makes
console owner handling complex. This patch makes console use Windows
pid as the owner pid (con.owner) instead of cygwin pid to make the
handling simpler.

Fixes: df0953aa298c ("Cygwin: console: Fix open() failure when the console owner calls exec().")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
(cherry picked from commit 90ddab98780da0e888ff09987d4651107c0f48d6)
2024-11-06 17:47:21 +09:00