aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-07-17 01:47:26 -0400
committerJesse Luehrs <doy@tozt.net>2020-07-17 01:47:26 -0400
commit27aef41bd21bbef3156d337a019a109d9ea72a85 (patch)
treef114303d58351cbb6f8d17ca31d20b1220521445
parent6985d5dd288ebc665fa5b342d013fba656276a61 (diff)
downloadpty-process-27aef41bd21bbef3156d337a019a109d9ea72a85.tar.gz
pty-process-27aef41bd21bbef3156d337a019a109d9ea72a85.zip
refactor to clean up the ioctl calls a bit
-rw-r--r--src/command.rs13
-rw-r--r--src/pty.rs34
2 files changed, 32 insertions, 15 deletions
diff --git a/src/command.rs b/src/command.rs
index 7377881..f7b1de8 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -43,7 +43,7 @@ impl Command for std::process::Command {
unsafe {
self.pre_exec(move || {
nix::unistd::setsid().map_err(|e| e.as_errno().unwrap())?;
- set_controlling_terminal(pts_fd, std::ptr::null())
+ set_controlling_terminal(&pts)
.map_err(|e| e.as_errno().unwrap())?;
// in the parent, destructors will handle closing these file
@@ -107,7 +107,16 @@ impl std::ops::DerefMut for Child {
}
nix::ioctl_write_ptr_bad!(
- set_controlling_terminal,
+ set_controlling_terminal_unsafe,
libc::TIOCSCTTY,
libc::c_int
);
+
+fn set_controlling_terminal(fh: &std::fs::File) -> nix::Result<()> {
+ // safe because std::fs::File is required to contain a valid file
+ // descriptor
+ unsafe {
+ set_controlling_terminal_unsafe(fh.as_raw_fd(), std::ptr::null())
+ }
+ .map(|_| ())
+}
diff --git a/src/pty.rs b/src/pty.rs
index d8e00d6..91e6f8c 100644
--- a/src/pty.rs
+++ b/src/pty.rs
@@ -88,19 +88,27 @@ impl Pty {
}
pub fn resize(&self, size: &Size) -> Result<()> {
- let size = size.into();
- let fd = self.pt().as_raw_fd();
-
- // safe because fd is guaranteed to be valid here (or else the
- // previous open call would have returned an error and exited the
- // function early), and size is guaranteed to be initialized
- // because it's a normal rust value, and nix::pty::Winsize is a
- // repr(C) struct with the same layout as `struct winsize` from
- // sys/ioctl.h.
- unsafe { set_term_size(fd, &size as *const nix::pty::Winsize) }
- .map_err(Error::SetTermSize)?;
- Ok(())
+ set_term_size(self.pt(), size).map_err(Error::SetTermSize)
}
}
-nix::ioctl_write_ptr_bad!(set_term_size, libc::TIOCSWINSZ, nix::pty::Winsize);
+nix::ioctl_write_ptr_bad!(
+ set_term_size_unsafe,
+ libc::TIOCSWINSZ,
+ nix::pty::Winsize
+);
+
+fn set_term_size(file: &std::fs::File, size: &Size) -> nix::Result<()> {
+ let size = size.into();
+ // safe because std::fs::File is required to contain a valid file
+ // descriptor and size is guaranteed to be initialized because it's a
+ // normal rust value, and nix::pty::Winsize is a repr(C) struct with the
+ // same layout as `struct winsize` from sys/ioctl.h.
+ unsafe {
+ set_term_size_unsafe(
+ file.as_raw_fd(),
+ &size as *const nix::pty::Winsize,
+ )
+ }
+ .map(|_| ())
+}