@@ -282,19 +282,43 @@ mod imp {
282282 mem,
283283 os:: fd:: AsRawFd ,
284284 } ;
285+ use tracing:: debug;
285286
286287 pub ( super ) type Error = io:: Error ;
287288
288289 pub ( super ) fn is_foreground_process ( ) -> bool {
289290 if !std:: io:: stdin ( ) . is_terminal ( ) {
291+ debug ! ( "stdin is not a terminal => is_foreground_process() is false" ) ;
290292 return false ;
291293 }
292294
293295 // Also check that tcgetpgrp is the same. If tcgetpgrp fails, it'll
294296 // return -1 and this check will fail.
295297 //
296298 // See https://stackoverflow.com/a/2428429.
297- unsafe { libc:: getpgrp ( ) == libc:: tcgetpgrp ( std:: io:: stdin ( ) . as_raw_fd ( ) ) }
299+ let pgrp = unsafe { libc:: getpgrp ( ) } ;
300+ let tc_pgrp = unsafe { libc:: tcgetpgrp ( std:: io:: stdin ( ) . as_raw_fd ( ) ) } ;
301+ if tc_pgrp == -1 {
302+ debug ! (
303+ "stdin is a terminal, and pgrp = {pgrp}, but tcgetpgrp failed with error {} => \
304+ is_foreground_process() is false",
305+ io:: Error :: last_os_error( )
306+ ) ;
307+ return false ;
308+ }
309+ if pgrp != tc_pgrp {
310+ debug ! (
311+ "stdin is a terminal, but pgrp {} != tcgetpgrp {} => is_foreground_process() is false" ,
312+ pgrp, tc_pgrp
313+ ) ;
314+ return false ;
315+ }
316+
317+ debug ! (
318+ "stdin is a terminal, and pgrp {pgrp} == tcgetpgrp {tc_pgrp} => \
319+ is_foreground_process() is true"
320+ ) ;
321+ true
298322 }
299323
300324 /// A scope guard to enable non-canonical input mode on Unix platforms.
@@ -380,6 +404,7 @@ mod imp {
380404 io:: { self , IsTerminal } ,
381405 os:: windows:: io:: AsRawHandle ,
382406 } ;
407+ use tracing:: debug;
383408 use windows_sys:: Win32 :: System :: Console :: {
384409 CONSOLE_MODE , ENABLE_ECHO_INPUT , ENABLE_LINE_INPUT , GetConsoleMode , SetConsoleMode ,
385410 } ;
@@ -388,10 +413,18 @@ mod imp {
388413
389414 pub ( super ) fn is_foreground_process ( ) -> bool {
390415 // Windows doesn't have a notion of foreground and background process
391- // groups: https://github.com/microsoft/terminal/issues/680. But:
416+ // groups: https://github.com/microsoft/terminal/issues/680. So simply
417+ // checking that stdin is a terminal is enough.
392418 //
393- // XXX do we need to do checks other than is_terminal here?
394- std:: io:: stdin ( ) . is_terminal ( )
419+ // This function is written slightly non-idiomatically, because it
420+ // follows the same structure as the more complex Unix function above.
421+ if !std:: io:: stdin ( ) . is_terminal ( ) {
422+ debug ! ( "stdin is not a terminal => is_foreground_process() is false" ) ;
423+ return false ;
424+ }
425+
426+ debug ! ( "stdin is a terminal => is_foreground_process() is true" ) ;
427+ true
395428 }
396429
397430 /// A scope guard to enable raw input mode on Windows.
0 commit comments