Skip to content

Commit 5e430ef

Browse files
authored
nix 0.30 (#145)
`dup`/`dup2` now encode their ownership semantics in the type system. Special variants of `dup2` were added for the I/O redirection which we use here. `dup` now takes an `Fd` instead of a `RawFd` and returns an `OwnedFd`, which is directly convertible to a `File`, so we can get rid of some conversion boilerplate in `get_file_handle`.
1 parent be7f1ab commit 5e430ef

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ which = ["dep:which"]
117117

118118
[dependencies]
119119
comma = "1.0"
120-
nix = { version = "0.29", features = ["fs", "process", "signal", "term"] }
120+
nix = { version = "0.30", features = ["fs", "process", "signal", "term"] }
121121
regex = "1"
122122
tempfile = "3"
123123
thiserror = "2.0.0"

src/process.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
use crate::error::Error;
44
use nix;
55
use nix::fcntl::{open, OFlag};
6-
use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
6+
use nix::libc::STDERR_FILENO;
77
use nix::pty::{grantpt, posix_openpt, unlockpt, PtyMaster};
88
pub use nix::sys::{signal, wait};
99
use nix::sys::{stat, termios};
10-
use nix::unistd::{close, dup, dup2, fork, setsid, ForkResult, Pid};
10+
use nix::unistd::{
11+
close, dup, dup2_stderr, dup2_stdin, dup2_stdout, fork, setsid, ForkResult, Pid,
12+
};
1113
use std;
1214
use std::fs::File;
1315
use std::io;
14-
use std::os::unix::io::{AsRawFd, FromRawFd};
16+
use std::os::unix::io::AsRawFd;
1517
use std::os::unix::process::CommandExt;
1618
use std::process::Command;
1719
use std::{thread, time};
@@ -40,8 +42,8 @@ use std::{thread, time};
4042
/// # fn main() {
4143
///
4244
/// let mut process = PtyProcess::new(Command::new("cat")).expect("could not execute cat");
43-
/// let fd = dup(process.pty.as_raw_fd()).unwrap();
44-
/// let f = unsafe { File::from_raw_fd(fd) };
45+
/// let fd = dup(&process.pty).unwrap();
46+
/// let f = File::from(fd);
4547
/// let mut writer = LineWriter::new(&f);
4648
/// let mut reader = BufReader::new(&f);
4749
/// process.exit().expect("could not terminate process");
@@ -109,12 +111,12 @@ impl PtyProcess {
109111
)?;
110112

111113
// assign stdin, stdout, stderr to the tty, just like a terminal does
112-
dup2(slave_fd, STDIN_FILENO)?;
113-
dup2(slave_fd, STDOUT_FILENO)?;
114-
dup2(slave_fd, STDERR_FILENO)?;
114+
dup2_stdin(&slave_fd)?;
115+
dup2_stdout(&slave_fd)?;
116+
dup2_stderr(&slave_fd)?;
115117

116118
// Avoid leaking slave fd
117-
if slave_fd > STDERR_FILENO {
119+
if slave_fd.as_raw_fd() > STDERR_FILENO {
118120
close(slave_fd)?;
119121
}
120122

@@ -138,8 +140,8 @@ impl PtyProcess {
138140
/// Get handle to pty fork for reading/writing
139141
pub fn get_file_handle(&self) -> Result<File, Error> {
140142
// needed because otherwise fd is closed both by dropping process and reader/writer
141-
let fd = dup(self.pty.as_raw_fd())?;
142-
unsafe { Ok(File::from_raw_fd(fd)) }
143+
let fd = dup(&self.pty)?;
144+
Ok(fd.into())
143145
}
144146

145147
/// At the drop of `PtyProcess` the running process is killed. This is blocking forever if

0 commit comments

Comments
 (0)