122 lines
3.7 KiB
Rust
122 lines
3.7 KiB
Rust
use nix::fcntl::{fcntl, open, FcntlArg, OFlag};
|
|
use nix::pty::{grantpt, posix_openpt, ptsname, unlockpt, Winsize};
|
|
use nix::sys::stat::Mode;
|
|
use nix::unistd::{ForkResult, Pid};
|
|
use nix::{ioctl_none_bad, ioctl_write_ptr_bad};
|
|
|
|
use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO, TIOCSCTTY};
|
|
// ioctl request code to set window size of pty:
|
|
use libc::TIOCSWINSZ;
|
|
use std::ffi::CStr;
|
|
use std::fs::File;
|
|
use std::os::unix::process::CommandExt;
|
|
use std::path::Path;
|
|
use std::process::{Command, Stdio};
|
|
|
|
use std::os::unix::io::{FromRawFd, IntoRawFd};
|
|
|
|
// nix macro that generates an ioctl call to set window size of pty:
|
|
ioctl_write_ptr_bad!(set_window_size, TIOCSWINSZ, Winsize);
|
|
|
|
// request to "Make the given terminal the controlling terminal of the calling process"
|
|
ioctl_none_bad!(set_controlling_terminal, TIOCSCTTY);
|
|
|
|
pub struct Child {
|
|
pub pty: tokio::fs::File,
|
|
pub pid: Pid,
|
|
}
|
|
|
|
impl Child {
|
|
// copied from
|
|
// https://doc.rust-lang.org/nightly/src/std/sys/unix/process/process_unix.rs.html#744-757
|
|
pub fn try_wait(&self) -> std::io::Result<Option<i32>> {
|
|
let mut status = 0;
|
|
let pid = cvt(unsafe { libc::waitpid(self.pid.as_raw(), &mut status, libc::WNOHANG) })?;
|
|
if pid == 0 {
|
|
Ok(None)
|
|
} else {
|
|
Ok(Some(status))
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for Child {
|
|
fn drop(&mut self) {
|
|
_ = nix::sys::signal::kill(self.pid, nix::sys::signal::Signal::SIGTERM);
|
|
}
|
|
}
|
|
|
|
pub fn create_pty<S: AsRef<CStr> + std::fmt::Debug>(path: &CStr, argv: &[S]) -> nix::Result<Child> {
|
|
/* Create a new master */
|
|
let master_fd = posix_openpt(OFlag::O_RDWR)?;
|
|
|
|
/* For some reason, you have to give permission to the master to have a
|
|
* pty. What is it good for otherwise? */
|
|
grantpt(&master_fd)?;
|
|
unlockpt(&master_fd)?;
|
|
|
|
/* Get the path of the slave */
|
|
let slave_name = unsafe { ptsname(&master_fd) }?;
|
|
|
|
/* Try to open the slave */
|
|
let _slave_fd = open(Path::new(&slave_name), OFlag::O_RDWR, Mode::empty())?;
|
|
info!("master opened the slave_fd!");
|
|
|
|
/* Launch our child process. The main application loop can inspect and then
|
|
pass the stdin data to it. */
|
|
let child_pid = match unsafe { nix::unistd::fork() } {
|
|
Ok(ForkResult::Child) => {
|
|
init_child(path, argv, slave_name).unwrap();
|
|
unreachable!()
|
|
}
|
|
Ok(ForkResult::Parent { child }) => child,
|
|
Err(e) => panic!("{}", e),
|
|
};
|
|
|
|
let winsize = Winsize {
|
|
ws_row: 25,
|
|
ws_col: 80,
|
|
ws_xpixel: 0,
|
|
ws_ypixel: 0,
|
|
};
|
|
let master_fd = master_fd.into_raw_fd();
|
|
/* Tell the master the size of the terminal */
|
|
unsafe { set_window_size(master_fd, &winsize)? };
|
|
fcntl(master_fd, FcntlArg::F_SETFL(OFlag::O_NDELAY)).unwrap();
|
|
let master_file = unsafe { File::from_raw_fd(master_fd) };
|
|
Ok(Child {
|
|
pty: master_file.into(),
|
|
pid: child_pid,
|
|
})
|
|
}
|
|
|
|
fn init_child<S: AsRef<CStr> + std::fmt::Debug>(
|
|
path: &CStr,
|
|
argv: &[S],
|
|
slave_name: String,
|
|
) -> anyhow::Result<std::convert::Infallible> {
|
|
/* Open slave end for pseudoterminal */
|
|
let slave_fd = open(Path::new(&slave_name), OFlag::O_RDWR, Mode::empty())?;
|
|
info!("child opened the slave_fd!");
|
|
|
|
// assign stdin, stdout, stderr to the tty
|
|
nix::unistd::dup2(slave_fd, STDIN_FILENO)?;
|
|
nix::unistd::dup2(slave_fd, STDOUT_FILENO)?;
|
|
nix::unistd::dup2(slave_fd, STDERR_FILENO)?;
|
|
|
|
nix::unistd::setsid().unwrap();
|
|
unsafe { set_controlling_terminal(slave_fd) }.unwrap();
|
|
|
|
info!("running exec: {:?} {:?}", path, argv);
|
|
Ok(nix::unistd::execv(path, argv)?)
|
|
}
|
|
|
|
// copied from... somewhere in std
|
|
pub fn cvt(t: i32) -> std::io::Result<i32> {
|
|
if t == -1 {
|
|
Err(std::io::Error::last_os_error())
|
|
} else {
|
|
Ok(t)
|
|
}
|
|
}
|