The pty
crate provides pty::fork()
. That makes a parent process fork with new pseudo-terminal (PTY).
This crate depends on followings:
libc
libraryAdd this to your Cargo.toml
:
```toml [dependencies]
pty = "0.1" ```
and this to your crate root:
rust
extern crate pty;
This function returns pty::Child
. It represents the child process and its PTY.
For example, the following code spawns tty(1)
command by pty::fork()
and outputs the result of the command.
```rust extern crate libc; extern crate pty;
use std::ffi::CString; use std::io::Read; use std::ptr;
fn main() { let child = pty::fork().unwrap();
if child.pid() == 0 {
// Child process just exec `tty`
let cmd = CString::new("tty").unwrap().as_ptr();
let args = [cmd, ptr::null()].as_mut_ptr();
unsafe { libc::execvp(cmd, args) };
}
else {
// Read output via PTY master
let mut output = String::new();
let mut pty_master = child.pty().unwrap();
match pty_master.read_to_string(&mut output) {
Ok(_nread) => println!("child tty is: {}", output.trim()),
Err(e) => panic!("read error: {}", e)
}
let _ = child.wait();
}
} ```
When run this, we get new PTY in the child process.
$ tty
/dev/pts/5
$ cargo run
Running `target/debug/example`
child tty is: /dev/pts/8
API documentation for latest version: http://hibariya.github.io/pty-rs/index.html
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Copyright (c) 2015 Hika Hibariya
Distributed under the MIT License.