coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] tee: Add --pipe-check to allow instantly detecting closed ou


From: Pádraig Brady
Subject: Re: [PATCH] tee: Add --pipe-check to allow instantly detecting closed outputs
Date: Fri, 2 Dec 2022 14:24:41 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.0

On 02/12/2022 11:47, Carl Edquist wrote:
PS:

On Fri, 2 Dec 2022, Carl Edquist wrote:

On the topic of implementation - I was thinking more about a general
solution for filter utils, and I am thinking the key thing is to provide
a replacement (wrapper) for read(2), that polls two fds together (one
input and one ouput), with no timeout.

It would check for POLLIN on input (in which case do the read()).
Otherwise if there is an error (POLLERR or POLLHUP) on input, treat it
as EOF. Otherwise if there's an error on output, remove this output, or
handle it similar to SIGPIPE/EPIPE.

(Nothing is written to the output fd here, it's just used for polling.)

...

I think this general approach of using poll(2) for a single input along
with a single ouput could be used for both tee and other filters that
only write to stdout.


A question for Pádraig (or anyone else who might know) -

Are there portability concerns with the system poll(2), or just with the
gnulib replacement for poll(), which is based on select(2) ?

The commit message (3a1c328cd) seems to suggest the latter.

In other words, are there any platforms that do not support using the
system poll(2) directly?

tail.c seems to use poll(2) for _AIX || __sun || __APPLE__ || HAVE_INOTIFY
and *otherwise* it uses select(2).

But I am thinking, if it's available, poll() is the more appropriate
interface for this check in the first place.

The 'trick' in tail.c of using select() to test when STDOUT becomes
available for reading, and taking that as an indication of an error
condition, doesn't seem to be documented (in my linux select(2)), and also
seems like it might not work as intended if fd 1 is open for read-write.


Anyway if it's possible just to use poll(2) (the system one, not the
gnulib replacement), that might simplify the portability logic.

Yes it would be better to use poll() if possible,
and that's what I attempted with tail(1) recently.
From the gnulib docs on poll():

"
 Portability problems fixed by Gnulib:

  This function is missing on some platforms:
  mingw, MSVC 14, HP NonStop.

  This function doesn't work on special files like @file{/dev/null} and ttys 
like
  @file{/dev/tty} on some platforms:
  AIX 5.3, Mac OS X 10.4.0

 Portability problems not fixed by Gnulib:

  Under Windows, when passing a pipe, Gnulib's @code{poll} replacement might
  return 0 even before the timeout has passed.  Programs using it with pipes can
  thus busy wait.

  On some platforms, file descriptors other than sockets do not support
  POLLHUP; they will return a "readable" or "writable" status instead:
  AIX 7.2, HP NonStop.
"

So to use poll() everywhere we'd need the gnulib module.
But empirically the replacement didn't work on macos at least
for this use case, and we know the select emulation wouldn't work on AIX.

So portable use of poll() will be awkward.

cheers,
Pádraig



reply via email to

[Prev in Thread] Current Thread [Next in Thread]