Skip to content

Commit feaebef

Browse files
authored
修复设置前台进程时未判断命令类型的错误 (#53)
1 parent d924632 commit feaebef

File tree

1 file changed

+17
-46
lines changed

1 file changed

+17
-46
lines changed

src/parser.rs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
sync::{Arc, Mutex},
1010
};
1111

12-
use nix::unistd::{read, write};
1312
use regex::Regex;
1413

1514
use crate::env::EnvManager;
@@ -408,7 +407,7 @@ impl Pipeline {
408407
internal = true;
409408

410409
// 用于同步父子进程的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");
412411

413412
// child_pid
414413
let child_pid = if self.backend {
@@ -419,24 +418,6 @@ impl Pipeline {
419418

420419
// 为子进程或前台运行
421420
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-
440421
let mut old_stdin: Option<i32> = None;
441422
let mut old_stdout: Option<i32> = None;
442423

@@ -528,12 +509,6 @@ impl Pipeline {
528509
// 当前进程为父进程
529510
drop(rfd);
530511
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-
537512
let mut status = 0;
538513
err = match libc::waitpid(child_pid, &mut status, 0) {
539514
-1 => Some(ExecuteErrorType::ExecuteFailed),
@@ -549,16 +524,6 @@ impl Pipeline {
549524
err = Some(ExecuteErrorType::ProcessTerminated);
550525
}
551526
}
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-
}
562527
}
563528
} else {
564529
err = Some(ExecuteErrorType::ExecuteFailed)
@@ -658,12 +623,16 @@ impl Pipeline {
658623

659624
// println!("exec command: {child_command:#?}");
660625

661-
unsafe {
626+
if !self.backend {
662627
// 设置前台进程
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+
665633
// 让子进程继续执行
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");
667636
drop(wfd);
668637
match child.wait() {
669638
Ok(exit_status) => match exit_status.code() {
@@ -679,12 +648,14 @@ impl Pipeline {
679648
Err(_) => err = Some(ExecuteErrorType::ExecuteFailed),
680649
};
681650

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+
}
688659
}
689660

690661
children.push(child);

0 commit comments

Comments
 (0)