33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6- // cSpell:ignore POLLERR POLLRDBAND pfds revents
7-
86use clap:: { Arg , ArgAction , Command , builder:: PossibleValue } ;
97use std:: ffi:: OsString ;
108use std:: fs:: OpenOptions ;
@@ -18,6 +16,8 @@ use uucore::{format_usage, show_error};
1816
1917// spell-checker:ignore nopipe
2018
19+ #[ cfg( target_os = "linux" ) ]
20+ use uucore:: signals:: ensure_stdout_not_broken;
2121#[ cfg( unix) ]
2222use uucore:: signals:: { enable_pipe_errors, ignore_interrupts} ;
2323
@@ -422,45 +422,3 @@ impl Read for NamedReader {
422422 }
423423 }
424424}
425-
426- /// Check that if stdout is a pipe, it is not broken.
427- #[ cfg( target_os = "linux" ) ]
428- pub fn ensure_stdout_not_broken ( ) -> Result < bool > {
429- use nix:: {
430- poll:: { PollFd , PollFlags , PollTimeout } ,
431- sys:: stat:: { SFlag , fstat} ,
432- } ;
433- use std:: os:: fd:: AsFd ;
434-
435- let out = stdout ( ) ;
436-
437- // First, check that stdout is a fifo and return true if it's not the case
438- let stat = fstat ( out. as_fd ( ) ) ?;
439- if !SFlag :: from_bits_truncate ( stat. st_mode ) . contains ( SFlag :: S_IFIFO ) {
440- return Ok ( true ) ;
441- }
442-
443- // POLLRDBAND is the flag used by GNU tee.
444- let mut pfds = [ PollFd :: new ( out. as_fd ( ) , PollFlags :: POLLRDBAND ) ] ;
445-
446- // Then, ensure that the pipe is not broken.
447- // Use ZERO timeout to return immediately - we just want to check the current state.
448- let res = nix:: poll:: poll ( & mut pfds, PollTimeout :: ZERO ) ?;
449-
450- if res > 0 {
451- // poll returned with events ready - check if POLLERR is set (pipe broken)
452- let error = pfds. iter ( ) . any ( |pfd| {
453- if let Some ( revents) = pfd. revents ( ) {
454- revents. contains ( PollFlags :: POLLERR )
455- } else {
456- true
457- }
458- } ) ;
459- return Ok ( !error) ;
460- }
461-
462- // res == 0 means no events ready (timeout reached immediately with ZERO timeout).
463- // This means the pipe is healthy (not broken).
464- // res < 0 would be an error, but nix returns Err in that case.
465- Ok ( true )
466- }
0 commit comments