Skip to content

PtyProcess: add NO_CTTY flag #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
25 changes: 23 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -86,7 +87,7 @@ impl PtyProcess {
/// Start a process in a forked pty
pub fn new(mut command: Command) -> Result<Self, Error> {
// 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)?;
Expand All @@ -95,6 +96,11 @@ impl PtyProcess {
// on Linux this is the libc function, on OSX this is our implementation of ptsname_r
let slave_name = ptsname_r(&master_fd)?;

// set echo off
let mut flags = termios::tcgetattr(master_fd.as_raw_fd())?;
flags.local_flags.remove(termios::LocalFlags::ECHO);
termios::tcsetattr(master_fd.as_raw_fd(), termios::SetArg::TCSANOW, &flags)?;

match unsafe { fork()? } {
ForkResult::Child => {
// Avoid leaking master fd
Expand All @@ -112,16 +118,31 @@ 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()),
}?;
}
Comment on lines +121 to +126
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the unsafe block is only needed for the ioctl call here, isn't it?
also, we don't need to match here, I think.

Suggested change
unsafe {
match ioctl(master_fd.as_raw_fd(), TIOCSCTTY) {
0 => Ok(()),
_ => Err(nix::Error::last()),
}?;
}
if 0 != unsafe { ioctl(master_fd.as_raw_fd(), TIOCSCTTY) } {
return Err(nix::Error::last())
}


// Avoid leaking slave fd
if slave_fd > STDERR_FILENO {
close(slave_fd)?;
}

// 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()))
}
Expand Down
9 changes: 8 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ impl<W: Write> StreamSession<W> {
/// this is guaranteed to be flushed to the process
/// returns number of written bytes
pub fn send_line(&mut self, line: &str) -> Result<usize, Error> {
let mut len = self.send(line)?;
let mut len = self.send_internal(line)?;
len += self.writer.write(&[b'\n'])?;
std::thread::sleep(std::time::Duration::from_millis(10));
Ok(len)
}

Expand All @@ -39,6 +40,12 @@ impl<W: Write> StreamSession<W> {
///
/// Returns number of written bytes
pub fn send(&mut self, s: &str) -> Result<usize, Error> {
let len = self.send_internal(s)?;
std::thread::sleep(std::time::Duration::from_millis(10));
Ok(len)
}

fn send_internal(&mut self, s: &str) -> Result<usize, Error> {
self.writer.write(s.as_bytes()).map_err(Error::from)
}

Expand Down