no need fg_process_setup module

This commit is contained in:
WindSoilder 2022-08-02 18:24:06 +08:00
parent b560466676
commit 21ee4ffbf6

View File

@ -28,9 +28,9 @@ impl ForegroundProcess {
} }
pub fn spawn(&mut self) -> std::io::Result<ForegroundChild> { pub fn spawn(&mut self) -> std::io::Result<ForegroundChild> {
fg_process_setup::prepare_to_foreground(&mut self.inner); prepare_to_foreground(&mut self.inner);
self.inner.spawn().map(|child| { self.inner.spawn().map(|child| {
fg_process_setup::set_foreground(&child); set_foreground(&child);
ForegroundChild { inner: child } ForegroundChild { inner: child }
}) })
} }
@ -45,18 +45,21 @@ impl AsMut<Child> for ForegroundChild {
impl Drop for ForegroundChild { impl Drop for ForegroundChild {
fn drop(&mut self) { fn drop(&mut self) {
// It's ok to use here because we have called `set_foreground` during creation. // It's ok to use here because we have called `set_foreground` during creation.
unsafe { fg_process_setup::reset_foreground_id() } unsafe { reset_foreground_id() }
} }
} }
#[cfg(target_family = "unix")]
use std::os::unix::prelude::CommandExt;
// It's a simpler version of fish shell's external process handling. // It's a simpler version of fish shell's external process handling.
// //
// For more information, please check `child_setup_process` function in fish shell. // For more information, please check `child_setup_process` function in fish shell.
// https://github.com/fish-shell/fish-shell/blob/3f90efca38079922b4b21707001d7bb9630107eb/src/postfork.cpp#L140 // https://github.com/fish-shell/fish-shell/blob/3f90efca38079922b4b21707001d7bb9630107eb/src/postfork.cpp#L140
#[cfg(target_family = "unix")] //
mod fg_process_setup { // The function does nothing on windows.
use std::os::unix::prelude::CommandExt; fn prepare_to_foreground(external_command: &mut std::process::Command) {
pub fn prepare_to_foreground(external_command: &mut std::process::Command) { #[cfg(target_family = "unix")]
unsafe { unsafe {
libc::signal(libc::SIGTTOU, libc::SIG_IGN); libc::signal(libc::SIGTTOU, libc::SIG_IGN);
libc::signal(libc::SIGTTIN, libc::SIG_IGN); libc::signal(libc::SIGTTIN, libc::SIG_IGN);
@ -76,36 +79,33 @@ mod fg_process_setup {
Ok(()) Ok(())
}); });
} }
} }
// If `prepare_to_foreground` function is not called, the function will fail with silence and do nothing. // On unix, if `prepare_to_foreground` function is not called,
pub fn set_foreground(process: &std::process::Child) { // the function will fail with silence and do nothing.
//
// The function does nothing on windows.
fn set_foreground(process: &std::process::Child) {
// it's ok to use unsafe here // it's ok to use unsafe here
// the implementaion here is just the same as // the implementaion here is just the same as
// https://docs.rs/nix/latest/nix/unistd/fn.tcsetpgrp.html, which is a safe function. // https://docs.rs/nix/latest/nix/unistd/fn.tcsetpgrp.html, which is a safe function.
#[cfg(target_family = "unix")]
unsafe { unsafe {
libc::tcsetpgrp(libc::STDIN_FILENO, process.id() as i32); libc::tcsetpgrp(libc::STDIN_FILENO, process.id() as i32);
} }
} }
/// Reset foreground to current process, and reset back `SIGTTOU`, `SIGTTIN` single handler. /// Reset foreground to current process, and reset back `SIGTTOU`, `SIGTTIN` single handler.
/// ///
/// ## Safety /// ## Safety
/// It can only be called when you have called `set_foreground`, or results in undefined behavior. /// On unix, it can only be called when you have called `set_foreground`, or results in undefined behavior.
pub unsafe fn reset_foreground_id() { ///
/// The function does nothing on windows.
unsafe fn reset_foreground_id() {
#[cfg(target_family = "unix")]
{
libc::tcsetpgrp(libc::STDIN_FILENO, libc::getpgrp()); libc::tcsetpgrp(libc::STDIN_FILENO, libc::getpgrp());
libc::signal(libc::SIGTTOU, libc::SIG_DFL); libc::signal(libc::SIGTTOU, libc::SIG_DFL);
libc::signal(libc::SIGTTIN, libc::SIG_DFL); libc::signal(libc::SIGTTIN, libc::SIG_DFL);
} }
} }
// TODO: investigate if we can set foreground process through windows system call.
#[cfg(target_family = "windows")]
mod fg_process_setup {
pub fn prepare_to_foreground(_external_command: &mut std::process::Command) {}
pub fn set_foreground(_process: &std::process::Child) {}
pub unsafe fn reset_foreground_id() {}
}