Skip to content

Commit

Permalink
prepare for nix 0.29
Browse files Browse the repository at this point in the history
Summary:
nix 0.29 introduces IO safety into many of its APIs. Migrate some of
the hermetic_infra call sites in advance.

Reviewed By: jasonwhite

Differential Revision: D66907547

fbshipit-source-id: bfe0fba851289a9c06e4e2e18136243dd60d551a
  • Loading branch information
chadaustin authored and facebook-github-bot committed Dec 11, 2024
1 parent b317f75 commit d153151
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
7 changes: 7 additions & 0 deletions reverie-process/src/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::ffi::CStr;
use std::io;
use std::io::Read;
use std::io::Write;
use std::os::fd::OwnedFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::IntoRawFd;
Expand Down Expand Up @@ -232,6 +233,12 @@ impl From<Fd> for std::fs::File {
}
}

impl From<OwnedFd> for Fd {
fn from(fd: OwnedFd) -> Self {
Self(fd.into_raw_fd())
}
}

impl AsyncFd {
pub fn new(fd: Fd) -> Result<Self, Errno> {
fd.set_nonblocking()?;
Expand Down
19 changes: 19 additions & 0 deletions reverie-process/src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::pin::Pin;
use core::task::Context;
use core::task::Poll;
use std::io;
use std::os::fd::OwnedFd;
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::IntoRawFd;
Expand Down Expand Up @@ -207,18 +208,36 @@ impl FromRawFd for ChildStdin {
}
}

impl From<OwnedFd> for ChildStdin {
fn from(fd: OwnedFd) -> Self {
Self::new(fd.into()).unwrap()
}
}

impl FromRawFd for ChildStdout {
unsafe fn from_raw_fd(fd: i32) -> Self {
Self::new(Fd::new(fd)).unwrap()
}
}

impl From<OwnedFd> for ChildStdout {
fn from(fd: OwnedFd) -> Self {
Self::new(fd.into()).unwrap()
}
}

impl FromRawFd for ChildStderr {
unsafe fn from_raw_fd(fd: i32) -> Self {
Self::new(Fd::new(fd)).unwrap()
}
}

impl From<OwnedFd> for ChildStderr {
fn from(fd: OwnedFd) -> Self {
Self::new(fd.into()).unwrap()
}
}

impl From<tokio::process::ChildStdin> for ChildStdin {
fn from(io: tokio::process::ChildStdin) -> Self {
let fd = io.as_raw_fd();
Expand Down
31 changes: 17 additions & 14 deletions reverie-ptrace/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use std::io::Write;
use std::net::SocketAddr;
use std::os::fd::AsRawFd;
use std::os::fd::OwnedFd;
use std::path::PathBuf;
use std::sync::Arc;

Expand Down Expand Up @@ -582,6 +584,11 @@ where
let (read1, write1) = unistd::pipe().map_err(from_nix_error)?;
let (read2, write2) = unistd::pipe().map_err(from_nix_error)?;

let read1 = unsafe { OwnedFd::from_raw_fd(read1) };
let write1 = unsafe { OwnedFd::from_raw_fd(write1) };
let read2 = unsafe { OwnedFd::from_raw_fd(read2) };
let write2 = unsafe { OwnedFd::from_raw_fd(write2) };

// Disable io redirection just before forking. We want the child process to
// be able to call `println!()` and have that output go to stdout.
//
Expand All @@ -592,15 +599,13 @@ where
// panicking, etc). We make a best-effort attempt to solve some of these issues.
match unsafe { unistd::fork() }.expect("unistd::fork failed") {
ForkResult::Child => {
unistd::close(read1)
.and_then(|_| unistd::close(read2))
.map_err(from_nix_error)?;
drop(read1);
drop(read2);
if capture_output {
unistd::dup2(write1, 1)
.and_then(|_| unistd::dup2(write2, 2))
.and_then(|_| unistd::close(write1))
.and_then(|_| unistd::close(write2))
.map_err(from_nix_error)?;
unistd::dup2(write1.as_raw_fd(), 1).map_err(from_nix_error)?;
unistd::dup2(write2.as_raw_fd(), 2).map_err(from_nix_error)?;
drop(write1);
drop(write2);
}

init_tracee(events.has_rdtsc()).expect("init_tracee failed");
Expand All @@ -627,13 +632,11 @@ where

let guest_pid = Pid::from(child);
let child = Running::new(guest_pid);
unistd::close(write1)
.and_then(|_| unistd::close(write2))
.map_err(from_nix_error)
.unwrap();
drop(write1);
drop(write2);

let stdout = unsafe { ChildStdout::from_raw_fd(read1) };
let stderr = unsafe { ChildStderr::from_raw_fd(read2) };
let stdout = read1.into();
let stderr = read2.into();
let tracer = match postspawn::<L>(child, gref.clone(), config, &events, None).await {
Ok(tracer) => tracer,
Err(TraceError::Errno(err)) => return Err(Error::Errno(err)),
Expand Down

0 comments on commit d153151

Please sign in to comment.