@@ -9,7 +9,6 @@ use std::{
9
9
sync:: { Arc , Mutex } ,
10
10
} ;
11
11
12
- use nix:: unistd:: { read, write} ;
13
12
use regex:: Regex ;
14
13
15
14
use crate :: env:: EnvManager ;
@@ -408,7 +407,7 @@ impl Pipeline {
408
407
internal = true ;
409
408
410
409
// 用于同步父子进程的tty setpgrp行为的管道
411
- let ( rfd, wfd ) = nix:: unistd:: pipe ( ) . expect ( "Failed to create pipe" ) ;
410
+ let ( rfd, _wfd ) = nix:: unistd:: pipe ( ) . expect ( "Failed to create pipe" ) ;
412
411
413
412
// child_pid
414
413
let child_pid = if self . backend {
@@ -419,24 +418,6 @@ impl Pipeline {
419
418
420
419
// 为子进程或前台运行
421
420
if child_pid == 0 {
422
- if self . backend {
423
- drop ( wfd) ;
424
- let mut tmpbf = [ 0u8 ; 1 ] ;
425
- loop {
426
- let x = read ( rfd. as_raw_fd ( ) , & mut tmpbf)
427
- . expect ( "Failed to read from pipe" ) ;
428
- if x > 0 {
429
- break ;
430
- } else {
431
- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 30 ) ) ;
432
- }
433
- }
434
- drop ( rfd) ;
435
- } else {
436
- drop ( rfd) ;
437
- drop ( wfd) ;
438
- }
439
-
440
421
let mut old_stdin: Option < i32 > = None ;
441
422
let mut old_stdout: Option < i32 > = None ;
442
423
@@ -528,12 +509,6 @@ impl Pipeline {
528
509
// 当前进程为父进程
529
510
drop ( rfd) ;
530
511
unsafe {
531
- // 设置前台进程
532
- libc:: tcsetpgrp ( libc:: STDIN_FILENO , child_pid) ;
533
- // 让子进程开始运行
534
- write ( wfd. as_fd ( ) , & [ 1 ] ) . expect ( "Failed to write to pipe" ) ;
535
- drop ( wfd) ;
536
-
537
512
let mut status = 0 ;
538
513
err = match libc:: waitpid ( child_pid, & mut status, 0 ) {
539
514
-1 => Some ( ExecuteErrorType :: ExecuteFailed ) ,
@@ -549,16 +524,6 @@ impl Pipeline {
549
524
err = Some ( ExecuteErrorType :: ProcessTerminated ) ;
550
525
}
551
526
}
552
-
553
- // 还原前台进程
554
- let r = libc:: tcsetpgrp ( libc:: STDIN_FILENO , std:: process:: id ( ) as i32 ) ;
555
- if r == -1 {
556
- let errno = std:: io:: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) ;
557
- println ! (
558
- "[novashell error]: restore tty pgrp: tcsetpgrp failed: {}" ,
559
- errno
560
- ) ;
561
- }
562
527
}
563
528
} else {
564
529
err = Some ( ExecuteErrorType :: ExecuteFailed )
@@ -658,12 +623,16 @@ impl Pipeline {
658
623
659
624
// println!("exec command: {child_command:#?}");
660
625
661
- unsafe {
626
+ if ! self . backend {
662
627
// 设置前台进程
663
- libc:: tcsetpgrp ( libc:: STDIN_FILENO , child. id ( ) as i32 ) ;
664
- } ;
628
+ unsafe {
629
+ libc:: tcsetpgrp ( libc:: STDIN_FILENO , child. id ( ) as i32 ) ;
630
+ } ;
631
+ }
632
+
665
633
// 让子进程继续执行
666
- write ( wfd. as_fd ( ) , & [ 1u8 ] ) . expect ( "Failed to write to pipe" ) ;
634
+ nix:: unistd:: write ( wfd. as_fd ( ) , & [ 1u8 ] )
635
+ . expect ( "Failed to write to pipe" ) ;
667
636
drop ( wfd) ;
668
637
match child. wait ( ) {
669
638
Ok ( exit_status) => match exit_status. code ( ) {
@@ -679,12 +648,14 @@ impl Pipeline {
679
648
Err ( _) => err = Some ( ExecuteErrorType :: ExecuteFailed ) ,
680
649
} ;
681
650
682
- // 还原前台进程
683
- unsafe {
684
- libc:: tcsetpgrp (
685
- libc:: STDIN_FILENO ,
686
- std:: process:: id ( ) as i32 ,
687
- ) ;
651
+ if !self . backend {
652
+ // 还原前台进程
653
+ unsafe {
654
+ libc:: tcsetpgrp (
655
+ libc:: STDIN_FILENO ,
656
+ std:: process:: id ( ) as i32 ,
657
+ ) ;
658
+ }
688
659
}
689
660
690
661
children. push ( child) ;
0 commit comments