Skip to content

Commit 1c5f4dc

Browse files
committed
Cygwin: select: set pipe writable only if PIPE_BUF bytes left
Linux select(2) returns the pipe as writable if at least one free page (4K onl most systems) is left in a page-oriented buffer handling. This is the same as PIPE_BUF. Emulate this behaviour by only returning the pipe as writable if at least 4K space is left in the buffer. Signed-off-by: Corinna Vinschen <[email protected]>
1 parent 7f3c225 commit 1c5f4dc

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

winsup/cygwin/select.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ pipe_data_available (int fd, fhandler_base *fh, HANDLE h, bool writing)
591591
{
592592
DWORD nbytes_in_pipe;
593593
if (!writing && PeekNamedPipe (h, NULL, 0, NULL, &nbytes_in_pipe, NULL))
594-
return nbytes_in_pipe > 0;
594+
return nbytes_in_pipe;
595595
return -1;
596596
}
597597

@@ -609,7 +609,7 @@ pipe_data_available (int fd, fhandler_base *fh, HANDLE h, bool writing)
609609
access on the write end. */
610610
select_printf ("fd %d, %s, NtQueryInformationFile failed, status %y",
611611
fd, fh->get_name (), status);
612-
return writing ? 1 : -1;
612+
return writing ? PIPE_BUF : -1;
613613
}
614614
if (writing)
615615
{
@@ -644,30 +644,30 @@ pipe_data_available (int fd, fhandler_base *fh, HANDLE h, bool writing)
644644
if (!query_hdl)
645645
query_hdl = ((fhandler_pipe *) fh)->temporary_query_hdl ();
646646
if (!query_hdl)
647-
return 1; /* We cannot know actual write pipe space. */
647+
return PIPE_BUF; /* We cannot know actual write pipe space. */
648648
DWORD nbytes_in_pipe;
649649
BOOL res =
650650
PeekNamedPipe (query_hdl, NULL, 0, NULL, &nbytes_in_pipe, NULL);
651651
if (!((fhandler_pipe *) fh)->get_query_handle ())
652652
CloseHandle (query_hdl); /* Close temporary query_hdl */
653653
if (!res)
654-
return 1;
654+
return PIPE_BUF; /* We cannot know actual write pipe space. */
655655
fpli.WriteQuotaAvailable = fpli.InboundQuota - nbytes_in_pipe;
656656
}
657657
if (fpli.WriteQuotaAvailable > 0)
658658
{
659659
paranoid_printf ("fd %d, %s, write: size %u, avail %u", fd,
660660
fh->get_name (), fpli.InboundQuota,
661661
fpli.WriteQuotaAvailable);
662-
return 1;
662+
return fpli.WriteQuotaAvailable;
663663
}
664664
/* TODO: Buffer really full or non-Cygwin reader? */
665665
}
666666
else if (fpli.ReadDataAvailable)
667667
{
668668
paranoid_printf ("fd %d, %s, read avail %u", fd, fh->get_name (),
669669
fpli.ReadDataAvailable);
670-
return 1;
670+
return fpli.ReadDataAvailable;
671671
}
672672
if (fpli.NamedPipeState & FILE_PIPE_CLOSING_STATE)
673673
return -1;
@@ -761,7 +761,7 @@ peek_pipe (select_record *s, bool from_select)
761761
}
762762
int n = pipe_data_available (s->fd, fh, h, true);
763763
select_printf ("write: %s, n %d", fh->get_name (), n);
764-
gotone += s->write_ready = n;
764+
gotone += s->write_ready = (n >= PIPE_BUF);
765765
if (n < 0 && s->except_selected)
766766
gotone += s->except_ready = true;
767767
}
@@ -974,7 +974,7 @@ peek_fifo (select_record *s, bool from_select)
974974
{
975975
int n = pipe_data_available (s->fd, fh, fh->get_handle (), true);
976976
select_printf ("write: %s, n %d", fh->get_name (), n);
977-
gotone += s->write_ready = n;
977+
gotone += s->write_ready = (n >= PIPE_BUF);
978978
if (n < 0 && s->except_selected)
979979
gotone += s->except_ready = true;
980980
}
@@ -1400,7 +1400,7 @@ peek_pty_slave (select_record *s, bool from_select)
14001400
{
14011401
int n = pipe_data_available (s->fd, fh, h, true);
14021402
select_printf ("write: %s, n %d", fh->get_name (), n);
1403-
gotone += s->write_ready = n;
1403+
gotone += s->write_ready = (n >= PIPE_BUF);
14041404
if (n < 0 && s->except_selected)
14051405
gotone += s->except_ready = true;
14061406
}

0 commit comments

Comments
 (0)