Skip to content

Commit

Permalink
Merge pull request #959 from cgwalters/errcheck-mount
Browse files Browse the repository at this point in the history
mount: Some error handling fixups
  • Loading branch information
cgwalters authored Dec 11, 2024
2 parents 3b58317 + c783548 commit 785acc9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ pub(crate) fn open_tree_from_pidns(
// We're in the child. At this point we know we don't have multiple threads, so we
// can safely `setns`.

drop(sock_parent);

// Open up the namespace of the target process as a file descriptor, and enter it.
let pidlink = fs::File::open(format!("/proc/{}/ns/mnt", pid.as_raw_nonzero()))?;
rustix::thread::move_into_link_name_space(
Expand Down Expand Up @@ -210,6 +212,7 @@ pub(crate) fn open_tree_from_pidns(
n => {
// We're in the parent; create a pid (checking that n > 0).
let pid = rustix::process::Pid::from_raw(n).unwrap();
drop(sock_child);
// Receive the mount file descriptor from the child
let mut cmsg_space = vec![0; rustix::cmsg_space!(ScmRights(1))];
let mut cmsg_buffer = rustix::net::RecvAncillaryBuffer::new(&mut cmsg_space);
Expand All @@ -224,7 +227,7 @@ pub(crate) fn open_tree_from_pidns(
)
.context("recvmsg")?
.bytes;
assert_eq!(nread, DUMMY_DATA.len());
anyhow::ensure!(nread == DUMMY_DATA.len());
assert_eq!(buf, DUMMY_DATA);
// And extract the file descriptor
let r = cmsg_buffer
Expand All @@ -236,8 +239,14 @@ pub(crate) fn open_tree_from_pidns(
.flatten()
.next()
.ok_or_else(|| anyhow::anyhow!("Did not receive a file descriptor"))?;
rustix::process::waitpid(Some(pid), WaitOptions::empty())?;
Ok(r)
// SAFETY: Since we're not setting WNOHANG, this will always return Some().
let st =
rustix::process::waitpid(Some(pid), WaitOptions::empty())?.expect("Wait status");
if let Some(0) = st.exit_status() {
Ok(r)
} else {
anyhow::bail!("forked helper failed: {st:?}");
}
}
}
}
Expand Down

0 comments on commit 785acc9

Please sign in to comment.