diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-02-22 03:43:49 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-02-22 03:43:49 -0500 |
commit | edfffc8c490b9d179a62901dfb874a85ab0984e3 (patch) | |
tree | 59eb9120eb4f61d5d9d43cafb1a51eadaef6891f /src/command/std.rs | |
parent | 2d3e62e97d1dc80594bdb302426cc226c6a9735c (diff) | |
download | pty-process-edfffc8c490b9d179a62901dfb874a85ab0984e3.tar.gz pty-process-edfffc8c490b9d179a62901dfb874a85ab0984e3.zip |
refactor
Diffstat (limited to 'src/command/std.rs')
-rw-r--r-- | src/command/std.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/command/std.rs b/src/command/std.rs new file mode 100644 index 0000000..d544b83 --- /dev/null +++ b/src/command/std.rs @@ -0,0 +1,37 @@ +use crate::error::*; + +use std::os::unix::io::{AsRawFd as _, FromRawFd as _}; +use std::os::unix::process::CommandExt as _; + +impl super::Command<std::process::Child> for std::process::Command { + fn spawn_pty( + &mut self, + size: Option<&crate::pty::Size>, + ) -> Result<super::Child<std::process::Child>> { + let (pty, pts, stdin, stdout, stderr) = super::setup_pty(size)?; + + let pt_fd = pty.pt().as_raw_fd(); + let pts_fd = pts.as_raw_fd(); + + // safe because the fds are valid (otherwise pty.pts() or dup() would + // have returned an Err and we would have exited early) and are not + // owned by any other structure (since dup() returns a fresh copy of + // the file descriptor), allowing from_raw_fd to take ownership of it. + self.stdin(unsafe { std::process::Stdio::from_raw_fd(stdin) }) + .stdout(unsafe { std::process::Stdio::from_raw_fd(stdout) }) + .stderr(unsafe { std::process::Stdio::from_raw_fd(stderr) }); + + // safe because setsid() and close() are async-signal-safe functions + // and ioctl() is a raw syscall (which is inherently + // async-signal-safe). + unsafe { + self.pre_exec(move || { + super::pre_exec(pt_fd, pts_fd, stdin, stdout, stderr) + }); + } + + let child = self.spawn().map_err(Error::Spawn)?; + + Ok(super::Child { child, pty }) + } +} |