From 7d85dac4c29a5c39eaed932c64c199827dda3265 Mon Sep 17 00:00:00 2001 From: petreeftime Date: Sun, 23 Apr 2023 18:26:25 +0300 Subject: [PATCH 1/4] PtyProcess: add NO_CTTY flag The default behavior of posix_openpt seems to be to replace the controlling terminal for the calling process, and PtyProcess should only change it for child instead. I think this might be a reason why some of the tests were failing non-deterministacally sometimes, although I am not 100% confident in this fix. Signed-off-by: Petre Eftime --- src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index 4b388bcc..b4362cea 100644 --- a/src/process.rs +++ b/src/process.rs @@ -86,7 +86,7 @@ impl PtyProcess { /// Start a process in a forked pty pub fn new(mut command: Command) -> Result { // Open a new PTY master - let master_fd = posix_openpt(OFlag::O_RDWR)?; + let master_fd = posix_openpt(OFlag::O_RDWR | OFlag::O_NOCTTY)?; // Allow a slave to be generated for it grantpt(&master_fd)?; From 67139b963b3f718a330c8ab5b6365026cedcc4f5 Mon Sep 17 00:00:00 2001 From: Petre Eftime Date: Sun, 23 Apr 2023 19:06:36 +0300 Subject: [PATCH 2/4] update to swatinem/rust-cache@v2 rust-cache@v1 seems to generate some deprecation warnings, so better update to v2. Signed-off-by: Petre Eftime --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc133e5e..7b4122bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,7 @@ jobs: uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - - uses: swatinem/rust-cache@v1 + - uses: swatinem/rust-cache@v2 - name: cargo-check uses: actions-rs/cargo@v1 with: @@ -51,7 +51,7 @@ jobs: uses: dtolnay/rust-toolchain@master with: toolchain: ${{ matrix.rust }} - - uses: swatinem/rust-cache@v1 + - uses: swatinem/rust-cache@v2 - name: cargo-test uses: actions-rs/cargo@v1 with: @@ -107,7 +107,7 @@ jobs: with: toolchain: 1.67.0 components: clippy - - uses: swatinem/rust-cache@v1 + - uses: swatinem/rust-cache@v2 - name: cargo-clippy run: cargo clippy --all --all-targets --all-features From 4441676eec43b9a27782d0b14a7cadba322187f6 Mon Sep 17 00:00:00 2001 From: Petre Eftime Date: Sun, 23 Apr 2023 20:32:58 +0300 Subject: [PATCH 3/4] try wait echo Signed-off-by: Petre Eftime --- src/process.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index b4362cea..672bab47 100644 --- a/src/process.rs +++ b/src/process.rs @@ -119,9 +119,17 @@ impl PtyProcess { // set echo off let mut flags = termios::tcgetattr(STDIN_FILENO)?; - flags.local_flags &= !termios::LocalFlags::ECHO; + flags.local_flags.remove(termios::LocalFlags::ECHO); termios::tcsetattr(STDIN_FILENO, termios::SetArg::TCSANOW, &flags)?; + loop { + flags = termios::tcgetattr(STDIN_FILENO)?; + if !flags.local_flags.contains(termios::LocalFlags::ECHO) { + break; + } + std::thread::sleep(std::time::Duration::from_millis(100)); + } + command.exec(); Err(Error::Nix(nix::Error::last())) } From f680ea38514030415dde642e6c50d9f905e8484e Mon Sep 17 00:00:00 2001 From: Petre Eftime Date: Sun, 23 Apr 2023 20:56:05 +0300 Subject: [PATCH 4/4] make new terminal controlling terminal Signed-off-by: Petre Eftime --- src/process.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/process.rs b/src/process.rs index 672bab47..10d99475 100644 --- a/src/process.rs +++ b/src/process.rs @@ -3,6 +3,7 @@ use crate::error::Error; use nix; use nix::fcntl::{open, OFlag}; +use nix::libc::{ioctl, TIOCSCTTY}; use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; use nix::pty::{grantpt, posix_openpt, unlockpt, PtyMaster}; pub use nix::sys::{signal, wait}; @@ -112,6 +113,13 @@ impl PtyProcess { dup2(slave_fd, STDOUT_FILENO)?; dup2(slave_fd, STDERR_FILENO)?; + unsafe { + match ioctl(master_fd.as_raw_fd(), TIOCSCTTY) { + 0 => Ok(()), + _ => Err(nix::Error::last()), + }?; + } + // Avoid leaking slave fd if slave_fd > STDERR_FILENO { close(slave_fd)?;