aboutsummaryrefslogtreecommitdiff
path: root/winsup
AgeCommit message (Collapse)AuthorFilesLines
23 hoursCygwin: add find_fast_cwd_pointer_aarch64.HEADgithub/mastergithub/mainmastermainJeremy Drake3-6/+229
This works for aarch64 hosts when the target is aarch64, x86_64, or i686, with only a small #if block in one function that needs to care. Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
23 hoursCygwin: use udis86 to find fast cwd pointer on x64Jeremy Drake1-79/+151
This makes find_fast_cwd_pointer more resiliant in the face of changes to the generated code in ntdll. Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
23 hoursCygwin: patch libudis86 to build as part of CygwinJeremy Drake9-18/+55
This ifdefs out the large table of opcode strings (and the function that references it) since we're only interested in walking machine code, not generating disassembly, and makes a couple of other tables "const" so that they end up in .rdata instead of .data. Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
23 hoursCygwin: vendor libudis86 1.7.2/libudis86Jeremy Drake8-0/+11287
This does not include the source files responsible for generating AT&T- or Intel-syntax assembly output, or upstream's Makefile.{am,in}. Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
23 hoursCygwin: factor out find_fast_cwd_pointer to arch-specific file.Jeremy Drake3-117/+134
This is in preparation for rewriting it using udis86, and adding an implementation for aarch64 hosts. Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
24 hoursCygwin: posix.xml: Mark libacl functions correctlyCorinna Vinschen1-10/+10
The functions defined by libacl, but not in POSIX.1e draft 17, are accidentally documented as POSIX functions. Mark them as libacl functions instead. Fixes: 004e95cf3594 ("Cygwin: winsup/doc/posix.xml: add missing POSIX.1e functions to docs") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
24 hoursCygwin: ACLs: Add some useful non-POSIX ACL functions from FreeBSDCorinna Vinschen5-16/+193
Add the following functions: acl_get_fd_np acl_get_link_np acl_get_perm_np acl_is_trivial_np acl_set_fd_np acl_set_link_np acl_strip_np Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
28 hoursCygwin: dll::detach: drop declaration of unimplemented methodCorinna Vinschen1-1/+0
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
28 hoursCygwin: dlfcn: add native DLLs to dll listCorinna Vinschen4-19/+55
If native DLLs are dlopen'ed, we have to keep track of the load count, so dlclose doesn't misbehave. We also have to keep track for the sake of a forked child so the handle returned by dlopen is still valid in the child. To accomplish that, add a new type DLL_NATIVE. Fixes: 82b31085f6ad ("Cygwin: dlfcn: avoid ENOENT on dlcose after dlopen(cygwin1.dll)") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
29 hoursCygwin: pipe: Add workaround for native ninjaTakashi Yano2-1/+8
Native (non-cygwin) ninja creates pipe with size == 0, and starts cygwin process with that pipe. This causes infinite loop in the fhandler_fifo_pipe::raw_write(). Ideally, the pipe implementation in cygwin could work even with pipe size == 0, however, it seems impossible due to: (1) select() does not work for that pipe because PeekNamedPipe() always returns 0. Read side is ready to read only when the write side is about to write, but there is no way to know that. (2) The cause of the problem: https://cygwin.com/pipermail/cygwin/2025-January/257143.html cannot be avoidable. To avoid CancelIo() problem, the patch https://cygwin.com/pipermail/cygwin-patches/2025q1/013451.html restricts the data size less than the current pipe space. However, if pipe size is zero this is impossible. This patch adds just a workaround for native ninja that avoid infinite loop in raw_write(). Addresses: https://github.com/msys2/msys2-runtime/issues/270 Reported-by: Christoph Reiter <reiter.christoph@gmail.com> Co-authored-by: Johannes Schindelin <johannes.schindelin@gmx.de> Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
29 hoursCygwin: pty: Return EMFILE when too meny ptmx are openedTakashi Yano3-1/+7
Previously, opening /dev/ptmx fails without setting errno when it is opened too many times. With this patch, return EMFILE in that situation. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257786.html Fixes: 09738c30627c ("Cygwin: pty: setup new pty on opening the master, not in constructor") Reported-by: Christian Franke <christian.franke@t-online.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
33 hoursCygwin: exceptions: check ExceptionFlags for EXCEPTION_NONCONTINUABLECorinna Vinschen3-1/+9
So far, exception::handle returned prematurely if the value of EXCEPTION_RECORD::ExceptionFlags was non-0. Starting with Windows 11 we're getting into trouble with that, if the exception is software generated, for instance by calling "throw" in a C++ application. In that case EXCEPTION_SOFTWARE_ORIGINATE (0x80) is set in EXCEPTION_RECORD::ExceptionFlags. Change the condition for exiting prematurely to do this only if any other flag except EXCEPTION_SOFTWARE_ORIGINATE is set in EXCEPTION_RECORD::ExceptionFlags. Fixes: Silent change in Windows exception handling Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
36 hoursCygwin: faq: add test of fork/exec slowdown by anti-virusChristian Franke1-0/+20
Signed-off-by: Christian Franke <christian.franke@t-online.de>
48 hoursCygwin: ACLs: don't allow special accounts as USER entryCorinna Vinschen2-1/+18
While accounts from the BUILTIN, NT AUTHORITY, and NT SERVICE domains can be owner of a file, they are always treated as group entries if they show up as additional entrys in a Windows ACL. Consequentially, it shouldn't be possible to add or remove them as USER entry, for instance, via setfacl. Add a check to disallow BUILTIN, NT AUTHORITY, and NT SERVICE accounts as USER entries in a POSIX ACL. Fixes: bc444e5aa4ca ("Reapply POSIX ACL changes.") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2 daysCygwin: set_posix_access: Use DEF objects when creating default ACL entries ↵Corinna Vinschen1-2/+2
from scratch When creating default ACL entries from scratch, set_posix_access accidentally sets the type to GROUP_OBJ and OTHER_OBJ, rather than DEF_GROUP_OBJ/DEF_OTHER_OBJ. This shouldn't be noticable most of time since creating from scratch is seldom necessary. Fixes: bc444e5aa4ca ("Reapply POSIX ACL changes.") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2 daysCygwin: dlopen: rename gmheflags to nodeleteCorinna Vinschen1-7/+6
It's more helpful when reading the source. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2 daysCygwin: dlfcn: fix dlopen refcounting for DLLs loaded with RTLD_NODELETECorinna Vinschen3-7/+26
Refcounting is performed even if the DLL is loaded at least once with RTLD_NODELETE. In that case we must not decrement the count on dlclose, even if FreeLibrary will never unload the DLL. Use INT_MAX as dll count if RTLD_NODELETE has been used at least once. Skip any further refcounting and make sure the DLL is still pinned in memory after fork. Fixes: 33297d810d90 ("Cygwin: dlfcn: Fix reference counting") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2 daysCygwin: dlfcn: avoid ENOENT on dlcose after dlopen(cygwin1.dll)Corinna Vinschen4-5/+8
When dlopen'ing the Cygwin DLL, refcounting the number of dlopen calls fails because dll_list::find returns NULL if the address resolves to the Cygwin DLL. Fix this by adding a bool parameter to dll_list::find which allows find to return the dll entry for the Cygwin DLL for the sake of dlopen/dlclose/fork. Fixes: 33297d810d90 ("Cygwin: dlfcn: Fix reference counting") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
4 daysCygwin: testsuite: Fix compilation for arm64Thirumalai Nagalingam3-8/+22
Introduce a cpu_relax.h header to provide architecture-specific processor idling instructions for the`cancel3` and `cancel5` tests.
4 daysCygwin: testsuite: Fix compatibility with GCC 15Thirumalai Nagalingam14-55/+44
GCC 15 defaults to `-std=gnu23`, causing build failures in `testsuite` due to outdated C function declarations. This patch updates the function declarations to align with modern standards.
7 daysCygwin: signal: Copy context to alternate stack in the SA_ONSTACK caseTakashi Yano2-0/+13
After the commit 0210c77311ae, the context passed to signal handler cannot be accessed from the signal handler that uses alternate stack. This is because the context locally copied is on the stack that is different area from the signal handler uses. With this patch, copy the context to alternate signal stack area to avoid this situation. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257714.html Fixes: 0210c77311ae ("Cygwin: signal: Use context locally copied in call_signal_handler()") Reported-by: Bruno Haible <bruno@clisp.org> Reviewed-by: Corinna Vischen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
8 daysCygwin: document getlocalename_l fixesCorinna Vinschen1-0/+4
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
8 daysCygwin: ntdll.h: enum _FILE_INFORMATION_CLASS: add FileKnownFolderInformationCorinna Vinschen1-0/+1
...for completeness. Introduced with Windows Server 2022. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
8 daysRevert "Cygwin: CI: XFAIL umask03"Jon Turney1-5/+1
This reverts commit cbe7543cdfdb7f3d270214877d4a4c3e78710bd3.
8 daysCygwin: signal: Clear direction flag in sigdeleyedTakashi Yano2-0/+4
x86_64 ABI requires the direction flag in CPU flags register cleared. https://learn.microsoft.com/en-us/cpp/build/x64-software-conventions However, currently that flag is not maintained in signal handler. Therefore, if the signal handler is called when that flag is set, it destroys the data and may crash if rep instruction is used in the signal handler. With this patch, the direction flag is cleared in sigdelayed() by adding cld instruction. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257704.html Fixes: 1fd5e000ace5 ("import winsup-2000-02-17 snapshot") Reported-by: Christian Franke <Christian.Franke@t-online.de> Reviewed-by: Corinna Vischen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
12 daysCygwin: console: tty::restore really restores the previous modeTakashi Yano3-26/+32
Previously, tty::restore sets the console mode to a predetermined console mode that is widely common for many non-cygwin console apps. So, if a non-cygwin app that is started from cygwin process changes the console mode and executes cygwin sub-process, the console mode is changed to the predetermined mode rather than being restored the original mode that is set by the non-cygwin app. With this patch, the console mode is stored when a cygwin process is started from non-cygwin app, then tty::restore restores the previous console mode that is used by the previous non-cygwin app when the cygwin app exits. Addresses: https://github.com/msys2/msys2-runtime/issues/268 Fixes: 3312f2d21f13 ("Cygwin: console: Redesign mode set strategy on close().") Reported-by: Eu Pin Tien, Jeremy Drake <cygwin@jdrake.com> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
12 daysCygwin: doc: improve Solaris ACL documentationCorinna Vinschen1-10/+21
Document including cygwin/acl.h functions when using Solaris ACL functions. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-18Cygwin: bump DLL version to 3.7.0cygwin-3.7.0-devCorinna Vinschen1-1/+1
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-18Cygwin: doc: rename sched_setpolicy(2) to sched_setscheduler(2)cygwin-3.6.0Christian Franke1-2/+2
The function sched_setpolicy(2) does not exist. Fixes: 757424f74400 ("Cygwin: doc: document sched_setpolicy(2) and priority mapping") Signed-off-by: Christian Franke <christian.franke@t-online.de>
2025-03-18Cygwin: doc: clarify release message in terms of old NT4/Samba sharesCorinna Vinschen2-2/+2
Fixes: b9ed33dec0cb ("Cygwin: readdir: drop support for NT4/Win2K shares and Samba < 3.0.22") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-18Cygwin: Carry process affinity through to resultMark Geisert2-2/+13
Due to deficient testing, the current code doesn't return a valid result to users of sched_getaffinity(). The updated code carries the determined procmask through to the generation of result cpu mask. Recognize Windows' limitation that if the process is multi-group (i.e., has threads in multiple cpu groups) there is no visibility to which processors in other groups are being used. One could remedy this by looping through all the process' threads, but that could be expensive so is left for future contemplation. In addition, we'd have to maintain our own copy of each thread's current group and mask in internal overhead. (By the way, multi-group processes are only possible on Windows systems with more than 64 hardware threads.) Reported-by: Christian Franke <Christian.Franke@t-online.de> Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257616.html Signed-off-by: Mark Geisert <mark@maxrnd.com> Fixes: 641ecb07533e ("Cygwin: Implement sched_[gs]etaffinity()")
2025-03-17fix native symlink spawn passing wrong arg0Chris Denton1-1/+3
Currently when starting a process from bash via a native symlink, argv[0] is set to the realpath of the executable and not to the link name. This patch fixes it so the path of the symlink is seen instead. The cause is a path conversion in perhaps_suffix which follows native symlinks. Hence the fix this patch uses is to add PC_SYM_NOFOLLOW_REP when calling path_conv::check to prevent that. Fixes: be371651146c ("* path.cc (path_conv::check): Don't follow reparse point symlinks if PC_SYM_NOFOLLOW_REP flag is set.") Signed-off-by: Chris Denton <chris@chrisdenton.dev>
2025-03-14Cygwin: don't export arc4random_addrandom/arc4random_stirCorinna Vinschen2-5/+2
Keep the functions for backward compatibility, but don't export them from libcygwin.a for new applications. Drop the workaround from commit ad29c33229e5 ("Cygwin: stdlib.h: revert declaring arc4random_stir() as function"). Fixes: ad29c33229e5 ("Cygwin: stdlib.h: revert declaring arc4random_stir() as function") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-15Cygwin: signal: Remove context_copy in call_signal_handler()Takashi Yano1-2/+1
After the commit 1d97f8aa4385, context_copy in call_signal_handle() is not used anymore. This patch removes it. Fixes: 1d97f8aa4385 ("Cygwin: signals: don't evaluate SA_SIGINFO context after handler returns") Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-03-15Cygwin: signal: Use context locally copied in call_signal_handler()Takashi Yano1-19/+22
If the signal handler is called from inside of another signal handler, _cygtls::context may be destroyed by call_signal_handler() newly called. To avoid this situation, this patch used context locally copied in call_signal_handler(). Addresses: https://cygwin.com/pipermail/cygwin-patches/2025q1/013483.html Fixes: 9043956ce859 ("Only construct ucontext for SA_SIGINFO signal handlers") Reported-by: Takashi Yano <takashi.yano@nifty.ne.jp> Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-03-15Cygwin: signal: Do not send __SIGFLUSHFAST if the pipe/queue is fullTakashi Yano1-1/+48
If __SIGFLUSHFAST is sent while the signal queue or the pipe is full, it causes deadlock. The mechanism is as follows. When sending __SIGFLUSHFAST, sig_send() waits for response (wakeup event) from wait_sig(). Therefore, the main thread does not process signals while waiting the wakeup event. However, if main thread does not process signals, signals in the queue are not dequeued. As a result, the sigpacket for __SIGFLUSHFAST stays in the signal pipe and not being processed. Thus the wakeup event will never be triggered. This did not occur with old signal queue design, because the queue never becomes full. This patch use alternative way for flushing the queue if the queue and the pipe is full, i.e., sending a event to trigger flushing in wait_sig(). Do the same for __SIGFLUSH as well. Addresses: https://cygwin.com/pipermail/cygwin-patches/2025q1/013461.html Fixes: 8ac22f8f16a8 ("Cygwin: signal: Redesign signal queue handling") 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>
2025-03-15Cygwin: signal: Ignore SIGSTOP while process is already stoppingTakashi Yano1-1/+4
On Linux, SIGSTOP while the process is stopping is ignored. With this patch, SIGSTOP is processed as a success with no effect while the process is already stopping. Fixes: 8f8eeb70ba73 ("* exceptions.cc (sigpacket::process): ...") Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-03-15Cygwin: signal: Do not clear signals in the queueTakashi Yano1-15/+1
Currently, sigpacket::process() clears some signals from the queue. For instance, SIGSTOP clears SIGCONT and SIGCONT clears SIGSTOP, etc. These might be needed by previous queue design, however, new queue design does not require that. On the contrary, that causes signal lost. With this patch, these sig_clear() calls are removed. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257582.html Fixes: 1fd5e000ace5 ("import winsup-2000-02-17 snapshot") 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>
2025-03-15Cygwin: signal: Redesign signal queue handlingTakashi Yano2-23/+105
The previous implementation of the signal queue behaves as: 1) Signals in the queue are processed in a disordered manner. 2) If the same signal is already in the queue, new signal is discarded. Strictly speaking, these behaviours do not violate POSIX. However, these could be a cause of unexpected behaviour in some software. In Linux, some important signals such as SIGSTOP/SIGCONT do not seem to behave like that. This patch prevents SIGKILL, SIGSTOP, SIGCONT, and SIGRT* from that issue. Moreover, if SA_SIGINFO is set in sa_flags, the signal is treated almost as the same. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257582.html Fixes: 7ac6173643b1 ("(pending_signals): New class.") Reported by: Christian Franke <Christian.Franke@t-online.de> Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Christian Franke <Christian.Franke@t-online.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-03-14Cygwin: signals: pop return address from signal stack earlierTakashi Yano3-32/+49
Commit a942476236b5 ("Cygwin: sigdelayed: pop return address from signal stack earlier") failed to take two facts into account: - _cygtls::call_signal_handler() potentially needs the return address as well, and - the signal handler may be interrupted by another signal. Revert the change in sigdelayed() and handle the signal stack manipulation in _cygtls::call_signal_handler() instead. Given we're poping the latest addresses from the signal stack early, there's no need for a big signal stack anymore. Reduce the size of the stack to 4 entries, plus one dummy entry. Move _cygtls::pop() from assembler to C++ code and make sure that stackptr neither underflows nor overflows the signal stack. Fixes: a942476236b5 ("Cygwin: sigdelayed: pop return address from signal stack earlier") Co-authored-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp> Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-13Cygwin: stdlib.h: revert declaring arc4random_stir() as functionCorinna Vinschen1-3/+3
The idea to define arc4random_stir() as macro to avoid autoconf problems in commit a8891c932192 ("Cygwin: stdlib.h: resurrect arc4random_stir() definition") worked fine for OpenSSH, but not for libbsd. During the build process the macro shadows the definition of arc4random_stir() and results in an error message like this: error: macro "arc4random_stir" passed 1 arguments, but takes just 0 Give up and just declare arc4random_stir() in a compatible way with libbsd again. Fixes: a8891c932192 ("Cygwin: stdlib.h: resurrect arc4random_stir() definition") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-12Cygwin: sigdelayed: pop return address from signal stack earlierCorinna Vinschen2-6/+22
When a signal handler is called via sigdelayed, sigdelayed pops the original address to return to from the signal stack. It only does so after the signal handler has been called and returned. If the signal handler never returns, for instance because it calls longjmp or setcontext, the address stays on the signal stack and the stack loses one slot. Given the stack has a depth of 256 entries, no signal handler will be called after 256 signal handlers long jumping out. Pop the return address from the signal stack prior to calling the signal handler and store it in a callee-saved register. This avoids filling up the signal stack in a scenario like this. Addresses: https://cygwin.com/pipermail/cygwin/2025-March/257648.html Fixes: 61522196c715 ("* Merge in cygwin-64bit-branch.") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-11Cygwin: faq: fix supported windows version claimCorinna Vinschen1-5/+5
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-10Cygwin: signals: don't evaluate SA_SIGINFO context after handler returnsCorinna Vinschen1-8/+0
After return from a signal handler created with the SA_SIGINFO flag, Cygwin checks if the handler changed the context. If so, Cygwin erroneously uses the context in a call to setcontext(). Drop this special behaviour. Neither Linux man pages, nor POSIX documentation treat the context pointer given to the handler as anything other than a read-only area. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-10Revert "Cygwin: signals: make setcontext work from signal handlers"Corinna Vinschen1-1/+0
This reverts commit c5f9eed1c045ad05e968c9a9b8fb22b1d46cd8ea. This was the wrong thing to do because it leads to signal handling not working after swapcontext. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-10Cygwin: exceptions: drop setting ContextFlags when calling RtlCaptureContextCorinna Vinschen1-2/+0
RtlCaptureContext always returnes a CONTEXT_FULL type context anyway. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2025-03-08Cygwin: sched_setaffinity: fix EACCES if pid of other process is usedChristian Franke2-1/+4
GetProcessGroupAffinity() requires PROCESS_QUERY_LIMITED_INFORMATION. Fixes: 641ecb07533e ("Cygwin: Implement sched_[gs]etaffinity()") Signed-off-by: Christian Franke <christian.franke@t-online.de>
2025-03-06Cygwin: signal: Add one more guard to stop signal handling on exit().Takashi Yano1-1/+1
The commit 3c1308ed890e adds a guard to stop signal handling on exit() in call_signal_handler(). However, the signal that is already queued but does not use signal handler may be going to process even with that patch. This patch add one more guard at the begining of sigpacket::process() to avoid that situation. Fixes: 3c1308ed890e ("Cygwin: signal: Fix a problem that process hangs on exit") Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-03-06Cygwin: pipe: Fix 'lost connection' issue in scpTakashi Yano2-8/+23
When transferring huge file using scp, the "lost connection" error sometimes happen. This is due to fhandler_pipe_fifo::raw_write() accidentally sends data that is not reported in th return value when interrupted by a signal. The cause of the problem is that CancelIo() responds success even if NtWriteFile() already sends the data. The following testcase using plain Win32 APIs reproduces the issue. The output will be something like: W: 8589934592 R: 9280061440 Much more data was received than the sender thought it had sent. #include <windows.h> #include <stdio.h> DWORD WINAPI read_thread(LPVOID arg) { char buf[32768]; ssize_t sum = 0; HANDLE pr = (HANDLE) arg; while (1) { DWORD nb; BOOL res = ReadFile(pr, buf, sizeof(buf), &nb, NULL); if (!res) break; sum += nb; } printf("R: %lld\n", sum); } int main() { const char pipename[] = "\\\\.\\pipe\\testpipe"; char buf[65536*4] = {0,}; OVERLAPPED ov = {0,}; ssize_t sum = 0; HANDLE pw = CreateNamedPipe(pipename, PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 65536, 0, 0, NULL); HANDLE pr = CreateFile(pipename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); HANDLE thr = CreateThread(NULL, 0, read_thread, pr, 0, NULL); ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); int cnt = 0; while (sum < 8192LL*1024*1024) { DWORD nb; BOOL res = WriteFile(pw, buf, sizeof(buf), &nb, &ov); if (res) sum += nb; else if (GetLastError() == ERROR_IO_PENDING) { if ((cnt & 3) == 0) CancelIo (pw); res = GetOverlappedResult(pw, &ov, &nb, TRUE); if (!res && GetLastError() != ERROR_OPERATION_ABORTED) { printf("%08x\n", GetLastError()); break; } sum += nb; } cnt++; } printf("W: %lld\n", sum); CloseHandle(pw); CloseHandle(ov.hEvenet); WaitForSingleObject(thr, INFINITE); CloseHandle(pr); return 0; } It seems that CancelIo() works as expected when the NtWriteFile() is stucked on the pipe full. If the writing is on-going, CancelIo() reports cancelled, but the data may already be written. To avoid this issue, this patch waits for completion of writing a while before issuing CancelIo(), and if it has not completed yet, call CancelIo(). The wait is done using existing cygwait(select_sem, 10, cw_cancel). If the semaphore select_sem is released in the read side, the write operation will be on-going. Otherwise, the write operation is still stucking, so CancelIo() can safely cancel the I/O. Addresses: https://cygwin.com/pipermail/cygwin/2025-January/257143.html Fixes: 4003e3dfa1b9 ("Cygwin: pipes: always terminate async IO in blocking mode") Reported-by: Jay M Martin <jaymmartin_buy@cox.net> Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2025-03-04Cygwin: stdlib.h: resurrect arc4random_stir() definitionCorinna Vinschen1-0/+5
An autoconf test for arc4random_stir() succeeds on Cygwin, because we export the symbol for backward compatibility. However, when building with -Werror afterwards, the definition of arc4random_stir() is missing in the stdlib.h header, so there's a chance to encounter an error like this with newer GCC: sshd.c:1113:25: error: implicit declaration of function ‘arc4random_stir’; did you mean ‘arc4random_buf’? [-Wimplicit-function-declaration] 1113 | arc4random_stir(); | ^~~~~~~~~~~~~~~ | arc4random_buf This occurs for instance when building OpenSSH 9.9p2 with GCC 15.0. Define arc4random_stir() as an empty macro in cygwin/stdlib.h to avoid the above compiler warning. Do not define arc4random_addrandom() yet, in the hope that this function call is really, really not used anymore. Fixes: 1cca343e4714 ("Drop redundant arc4random prototypes from cygwin/stdlib.h") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>