From c3df3747b80f2813daecb2d7f82670bebb0b3079 Mon Sep 17 00:00:00 2001 From: MemoryShore <1353318529@qq.com> Date: Sat, 30 Nov 2024 02:02:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE=E5=89=8D?= =?UTF-8?q?=E5=8F=B0=E8=BF=9B=E7=A8=8B=E6=97=B6=E6=9C=AA=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E7=B1=BB=E5=9E=8B=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/parser.rs | 63 ++++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 46 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 2561deb..7771fa6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -9,7 +9,6 @@ use std::{ sync::{Arc, Mutex}, }; -use nix::unistd::{read, write}; use regex::Regex; use crate::env::EnvManager; @@ -408,7 +407,7 @@ impl Pipeline { internal = true; // 用于同步父子进程的tty setpgrp行为的管道 - let (rfd, wfd) = nix::unistd::pipe().expect("Failed to create pipe"); + let (rfd, _wfd) = nix::unistd::pipe().expect("Failed to create pipe"); // child_pid let child_pid = if self.backend { @@ -419,24 +418,6 @@ impl Pipeline { // 为子进程或前台运行 if child_pid == 0 { - if self.backend { - drop(wfd); - let mut tmpbf = [0u8; 1]; - loop { - let x = read(rfd.as_raw_fd(), &mut tmpbf) - .expect("Failed to read from pipe"); - if x > 0 { - break; - } else { - std::thread::sleep(std::time::Duration::from_millis(30)); - } - } - drop(rfd); - } else { - drop(rfd); - drop(wfd); - } - let mut old_stdin: Option = None; let mut old_stdout: Option = None; @@ -528,12 +509,6 @@ impl Pipeline { // 当前进程为父进程 drop(rfd); unsafe { - // 设置前台进程 - libc::tcsetpgrp(libc::STDIN_FILENO, child_pid); - // 让子进程开始运行 - write(wfd.as_fd(), &[1]).expect("Failed to write to pipe"); - drop(wfd); - let mut status = 0; err = match libc::waitpid(child_pid, &mut status, 0) { -1 => Some(ExecuteErrorType::ExecuteFailed), @@ -549,16 +524,6 @@ impl Pipeline { err = Some(ExecuteErrorType::ProcessTerminated); } } - - // 还原前台进程 - let r = libc::tcsetpgrp(libc::STDIN_FILENO, std::process::id() as i32); - if r == -1 { - let errno = std::io::Error::last_os_error().raw_os_error().unwrap(); - println!( - "[novashell error]: restore tty pgrp: tcsetpgrp failed: {}", - errno - ); - } } } else { err = Some(ExecuteErrorType::ExecuteFailed) @@ -658,12 +623,16 @@ impl Pipeline { // println!("exec command: {child_command:#?}"); - unsafe { + if !self.backend { // 设置前台进程 - libc::tcsetpgrp(libc::STDIN_FILENO, child.id() as i32); - }; + unsafe { + libc::tcsetpgrp(libc::STDIN_FILENO, child.id() as i32); + }; + } + // 让子进程继续执行 - write(wfd.as_fd(), &[1u8]).expect("Failed to write to pipe"); + nix::unistd::write(wfd.as_fd(), &[1u8]) + .expect("Failed to write to pipe"); drop(wfd); match child.wait() { Ok(exit_status) => match exit_status.code() { @@ -679,12 +648,14 @@ impl Pipeline { Err(_) => err = Some(ExecuteErrorType::ExecuteFailed), }; - // 还原前台进程 - unsafe { - libc::tcsetpgrp( - libc::STDIN_FILENO, - std::process::id() as i32, - ); + if !self.backend { + // 还原前台进程 + unsafe { + libc::tcsetpgrp( + libc::STDIN_FILENO, + std::process::id() as i32, + ); + } } children.push(child);