https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=4bcc6adec765ee8354bfc9e6c060c9d1c0c7bf46
commit 4bcc6adec765ee8354bfc9e6c060c9d1c0c7bf46 Author: Corinna Vinschen <cori...@vinschen.de> AuthorDate: Tue Feb 25 10:29:29 2025 +0100 Commit: Corinna Vinschen <cori...@vinschen.de> CommitDate: Tue Feb 25 10:29:29 2025 +0100 Cygwin: pipe_data_available: drop special casing select After 11a84cc757ef ("Cygwin: fix SSH hangs"), select returns writability if any number of bytes are left in the buffer. Thus, the reason for pipe_data_available() to return PIPE_BUF when called from select() is gone, and we can drop special casing select(). So together with 11a84cc757ef ("Cygwin: fix SSH hangs"), this patch essentially reverts 555afcb2f3a6 ("Cygwin: select: set pipe writable only if PIPE_BUF bytes left") Rather than reverting the flag parameter to a bool, keep a mode argument set to PDA_READ or PDA_WRITE. If we can't evaluate the number of bytes left, just return 1 to select(), as for any other caller. Signed-off-by: Corinna Vinschen <cori...@vinschen.de> Diff: --- winsup/cygwin/local_includes/select.h | 2 +- winsup/cygwin/select.cc | 29 ++++++++++------------------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/winsup/cygwin/local_includes/select.h b/winsup/cygwin/local_includes/select.h index dea023343f7c..43ceb1d7ea9a 100644 --- a/winsup/cygwin/local_includes/select.h +++ b/winsup/cygwin/local_includes/select.h @@ -141,7 +141,7 @@ extern "C" int cygwin_select (int , fd_set *, fd_set *, fd_set *, ssize_t pipe_data_available (int, fhandler_base *, HANDLE, int); +#define PDA_READ 0x00 #define PDA_WRITE 0x01 -#define PDA_SELECT 0x02 #endif /* _SELECT_H_ */ diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index 012578886011..422c8e830b33 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -593,12 +593,12 @@ no_verify (select_record *, fd_set *, fd_set *, fd_set *) } ssize_t -pipe_data_available (int fd, fhandler_base *fh, HANDLE h, int flags) +pipe_data_available (int fd, fhandler_base *fh, HANDLE h, int mode) { if (fh->get_device () == FH_PIPER) { DWORD nbytes_in_pipe; - if (!(flags & PDA_WRITE) + if (mode == PDA_READ && PeekNamedPipe (h, NULL, 0, NULL, &nbytes_in_pipe, NULL)) return nbytes_in_pipe; return -1; @@ -618,17 +618,9 @@ pipe_data_available (int fd, fhandler_base *fh, HANDLE h, int flags) access on the write end. */ select_printf ("fd %d, %s, NtQueryInformationFile failed, status %y", fd, fh->get_name (), status); - switch (flags) - { - case PDA_WRITE: - return 1; - case PDA_SELECT | PDA_WRITE: - return PIPE_BUF; - default: - return -1; - } + return (mode == PDA_WRITE) ? 1 : -1; } - if (flags & PDA_WRITE) + if (mode == PDA_WRITE) { /* If there is anything available in the pipe buffer then signal that. This means that a pipe could still block since you could @@ -665,7 +657,7 @@ pipe_data_available (int fd, fhandler_base *fh, HANDLE h, int flags) return 0; /* Full */ else if (!NT_SUCCESS (status)) /* We cannot know actual write pipe space. */ - return (flags & PDA_SELECT) ? PIPE_BUF : 1; + return 1; /* Restore pipe mode to blocking mode */ ((fhandler_pipe *) fh)->set_pipe_non_blocking (false); /* Empty */ @@ -732,10 +724,10 @@ peek_pipe (select_record *s, bool from_select) gotone = s->read_ready = true; goto out; } - ssize_t n = pipe_data_available (s->fd, fh, h, PDA_SELECT); + ssize_t n = pipe_data_available (s->fd, fh, h, PDA_READ); /* On PTY masters, check if input from the echo pipe is available. */ if (n == 0 && fh->get_echo_handle ()) - n = pipe_data_available (s->fd, fh, fh->get_echo_handle (), PDA_SELECT); + n = pipe_data_available (s->fd, fh, fh->get_echo_handle (), PDA_READ); if (n < 0) { @@ -776,7 +768,7 @@ out: gotone += s->except_ready = true; return gotone; } - ssize_t n = pipe_data_available (s->fd, fh, h, PDA_SELECT | PDA_WRITE); + ssize_t n = pipe_data_available (s->fd, fh, h, PDA_WRITE); select_printf ("write: %s, n %d", fh->get_name (), n); gotone += s->write_ready = (n > 0); if (n < 0 && s->except_selected) @@ -989,8 +981,7 @@ peek_fifo (select_record *s, bool from_select) out: if (s->write_selected) { - ssize_t n = pipe_data_available (s->fd, fh, fh->get_handle (), - PDA_SELECT | PDA_WRITE); + ssize_t n = pipe_data_available (s->fd, fh, fh->get_handle (), PDA_WRITE); select_printf ("write: %s, n %d", fh->get_name (), n); gotone += s->write_ready = (n > 0); if (n < 0 && s->except_selected) @@ -1416,7 +1407,7 @@ out: HANDLE h = ptys->get_output_handle (); if (s->write_selected) { - ssize_t n = pipe_data_available (s->fd, fh, h, PDA_SELECT | PDA_WRITE); + ssize_t n = pipe_data_available (s->fd, fh, h, PDA_WRITE); select_printf ("write: %s, n %d", fh->get_name (), n); gotone += s->write_ready = (n > 0); if (n < 0 && s->except_selected)