diff --git a/detcore/tests/lit/child_should_inherit_fds/main.rs b/detcore/tests/lit/child_should_inherit_fds/main.rs index 0df18d4..056e1df 100644 --- a/detcore/tests/lit/child_should_inherit_fds/main.rs +++ b/detcore/tests/lit/child_should_inherit_fds/main.rs @@ -12,6 +12,7 @@ use std::os::fd::AsRawFd; +use close_err::Closable; use nix::sys::wait::waitpid; use nix::sys::wait::WaitStatus; use nix::unistd::fork; @@ -25,7 +26,7 @@ fn main() { let msg: [u8; 8] = [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]; match unsafe { fork().unwrap() } { ForkResult::Parent { child, .. } => { - drop(fdwrite); + fdwrite.close().expect("close failed"); let mut buf: [u8; 8] = [0; 8]; // XXX: The following SYS_read would timeout due to detcore issue. // add a 5 seconds timeout to abort. @@ -36,7 +37,7 @@ fn main() { unsafe { libc::syscall(libc::SYS_exit_group, 0) }; } ForkResult::Child => { - drop(fdread); + fdread.close().expect("close failed"); assert_eq!(write(&fdwrite, &msg), Ok(8)); unsafe { libc::syscall(libc::SYS_exit_group, 0) }; } diff --git a/detcore/tests/lit/close_on_exec/main.rs b/detcore/tests/lit/close_on_exec/main.rs index a620e06..7cbdc34 100644 --- a/detcore/tests/lit/close_on_exec/main.rs +++ b/detcore/tests/lit/close_on_exec/main.rs @@ -16,6 +16,7 @@ use std::os::fd::AsRawFd; use std::ptr; +use close_err::Closable; use nix::errno::Errno; use nix::fcntl::OFlag; use nix::sys::stat::fstat; @@ -46,7 +47,7 @@ fn main() { match unsafe { fork().unwrap() } { ForkResult::Parent { child, .. } => { - drop(fdwrite); + fdwrite.close().expect("close failed"); let mut msg = [0u8; 4]; @@ -59,7 +60,7 @@ fn main() { assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0))); } ForkResult::Child => { - drop(fdread); + fdread.close().expect("close failed"); let proc_self = "/proc/self/exe\0".as_ptr() as *const libc::c_char; diff --git a/detcore/tests/lit/dup2_existing_fd/main.rs b/detcore/tests/lit/dup2_existing_fd/main.rs index 431ec32..210aee7 100644 --- a/detcore/tests/lit/dup2_existing_fd/main.rs +++ b/detcore/tests/lit/dup2_existing_fd/main.rs @@ -9,6 +9,7 @@ // RUN: %me use std::os::fd::AsRawFd; +use close_err::Closable; use nix::sys::wait::waitpid; use nix::sys::wait::WaitStatus; use nix::unistd::alarm; @@ -26,7 +27,7 @@ fn main() { match unsafe { fork().unwrap() } { ForkResult::Parent { child, .. } => { - drop(fdwrite); + fdwrite.close().expect("close failed"); let mut buf = [0u8; 8]; // If the file descriptor newfd was previously open, @@ -45,7 +46,7 @@ fn main() { assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0))); } ForkResult::Child => { - drop(fdread); + fdread.close().expect("close failed"); assert_eq!(write(fdwrite, &msg), Ok(msg.len())); } } diff --git a/detcore/tests/lit/no_close_on_exec/main.rs b/detcore/tests/lit/no_close_on_exec/main.rs index 37ad61f..9c0cc83 100644 --- a/detcore/tests/lit/no_close_on_exec/main.rs +++ b/detcore/tests/lit/no_close_on_exec/main.rs @@ -17,6 +17,7 @@ use std::os::fd::AsRawFd; use std::os::fd::BorrowedFd; use std::ptr; +use close_err::Closable; use nix::fcntl::OFlag; use nix::sys::wait::waitpid; use nix::sys::wait::WaitStatus; @@ -49,7 +50,7 @@ fn main() { match unsafe { fork().unwrap() } { ForkResult::Parent { child, .. } => { - drop(fdwrite); + fdwrite.close().expect("close failed"); let mut msg = [0u8; 6]; @@ -60,7 +61,7 @@ fn main() { assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0))); } ForkResult::Child => { - drop(fdread); + fdread.close().expect("close failed"); let proc_self = "/proc/self/exe\0".as_ptr() as *const libc::c_char; diff --git a/detcore/tests/lit/openat_next_lowest_fd/main.rs b/detcore/tests/lit/openat_next_lowest_fd/main.rs index d4b2c1a..414451f 100644 --- a/detcore/tests/lit/openat_next_lowest_fd/main.rs +++ b/detcore/tests/lit/openat_next_lowest_fd/main.rs @@ -9,6 +9,7 @@ // RUN: %me use std::os::fd::AsRawFd; +use close_err::Closable; use nix::fcntl::openat; use nix::fcntl::OFlag; use nix::sys::stat::Mode; @@ -18,7 +19,7 @@ fn main() { assert_eq!(fd4.as_raw_fd(), fd3.as_raw_fd() + 1); let fd3_raw = fd3.as_raw_fd(); - drop(fd3); + fd3.close().expect("close failed"); assert_eq!( openat(None, "/dev/null", OFlag::O_RDONLY, Mode::S_IRUSR), diff --git a/detcore/tests/lit/pipe_creates_valid_fds/main.rs b/detcore/tests/lit/pipe_creates_valid_fds/main.rs index 02bf407..d20aa69 100644 --- a/detcore/tests/lit/pipe_creates_valid_fds/main.rs +++ b/detcore/tests/lit/pipe_creates_valid_fds/main.rs @@ -9,6 +9,7 @@ // RUN: %me use std::os::fd::AsRawFd; +use close_err::Closable; use nix::sys::stat::fstat; use nix::unistd::pipe; @@ -16,6 +17,6 @@ fn main() { let (fd_read, fd_write) = pipe().unwrap(); assert!(fstat(fd_read.as_raw_fd()).is_ok()); assert!(fstat(fd_write.as_raw_fd()).is_ok()); - drop(fd_write); - drop(fd_read); + fd_write.close().expect("close failed"); + fd_read.close().expect("close failed"); } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index b7b90ea..a21f259 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -161,6 +161,7 @@ name = "standalone_stacktrace_events" path = "standalone/stacktrace_events.rs" [dependencies] +close-err = "1.0.2" detcore = { version = "0.0.0", path = "../detcore" } libc = "0.2.139" nix = { version = "0.29.0", features = ["dir", "event", "hostname", "inotify", "ioctl", "mman", "mount", "net", "poll", "ptrace", "reboot", "resource", "sched", "term", "time", "user", "zerocopy"] } diff --git a/tests/helpers.bzl b/tests/helpers.bzl index 2f65da1..82aa2b7 100644 --- a/tests/helpers.bzl +++ b/tests/helpers.bzl @@ -212,6 +212,7 @@ def hermit_rust_test(path, raw, run, no_sequentialize_threads, no_deterministic_ srcs = [path, paths.dirname(path) + "/test_utils/mod.rs"], crate_root = path, deps = [ + "fbsource//third-party/rust:close-err", "fbsource//third-party/rust:libc", "fbsource//third-party/rust:nix", "fbsource//third-party/rust:tempfile", diff --git a/tests/rust/pipe_basics.rs b/tests/rust/pipe_basics.rs index 4b3c8f9..b03dfd1 100644 --- a/tests/rust/pipe_basics.rs +++ b/tests/rust/pipe_basics.rs @@ -9,6 +9,7 @@ use std::os::fd::AsRawFd; use std::str; +use close_err::Closable; use nix::unistd; fn main() { @@ -19,7 +20,7 @@ fn main() { assert_eq!(unistd::read(fdread.as_raw_fd(), &mut buf), Ok(14)); println!("Child received message: {}", str::from_utf8(&buf).unwrap()); } - drop(fdread); + fdread.close().expect("close failed"); }); for ix in 10..20 { @@ -27,7 +28,7 @@ fn main() { let msg = format!("hello world {}", ix); assert_eq!(unistd::write(&fdwrite, msg.as_bytes()), Ok(14)); } - drop(fdwrite); + fdwrite.close().expect("close failed"); println!("Joining child.."); handle.join().unwrap(); diff --git a/tests/rust/poll_spin.rs b/tests/rust/poll_spin.rs index d8f3bd1..c6973f9 100644 --- a/tests/rust/poll_spin.rs +++ b/tests/rust/poll_spin.rs @@ -31,6 +31,7 @@ use std::sync::Arc; use std::thread; use std::time; +use close_err::Closable; use nix::poll::poll; use nix::poll::PollFd; use nix::poll::PollFlags; @@ -64,6 +65,7 @@ fn main() { break; } } + fdread.close().expect("close failed"); }); while count2.load(Ordering::SeqCst) == 0 { @@ -73,7 +75,7 @@ fn main() { println!("Parent writing to pipe.."); let msg = "hello world\n"; assert_eq!(unistd::write(&fdwrite, msg.as_bytes()), Ok(12)); - drop(fdwrite); + fdwrite.close().expect("close failed"); println!("Joining child.."); handle.join().unwrap();