From f6ae010e02c5fcda55424f405612d42e7d0a3cec Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sun, 2 Feb 2025 22:34:12 +0800 Subject: [PATCH 1/7] test: test posix_spawn() --- Cargo.toml | 1 + test/test_spawn.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 93ce205d31..27fca64510 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ nix = { path = ".", features = ["acct", "aio", "dir", "env", "event", "fanotify" "net", "personality", "poll", "pthread", "ptrace", "quota", "process", "reboot", "resource", "sched", "signal", "socket", "syslog", "term", "time", "ucontext", "uio", "user", "zerocopy"] } +which = "7.0.1" [target.'cfg(any(target_os = "android", target_os = "linux"))'.dev-dependencies] caps = "0.5.3" diff --git a/test/test_spawn.rs b/test/test_spawn.rs index 58dfec31e6..05db0d3273 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -8,6 +8,85 @@ use std::ffi::CString; fn spawn_true() { let _guard = FORK_MTX.lock(); + let bin = which::which("true").unwrap(); + let args = &[ + CString::new("true").unwrap(), + CString::new("story").unwrap(), + ]; + let vars: &[CString] = &[]; + let actions = PosixSpawnFileActions::init().unwrap(); + let attr = PosixSpawnAttr::init().unwrap(); + + let pid = + spawn::posix_spawn(bin.as_path(), &actions, &attr, args, vars).unwrap(); + + let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap(); + + match status { + WaitStatus::Exited(wpid, ret) => { + assert_eq!(pid, wpid); + assert_eq!(ret, 0); + } + _ => { + panic!("Invalid WaitStatus"); + } + }; +} + +#[test] +fn spawn_sleep() { + let _guard = FORK_MTX.lock(); + + let bin = which::which("sleep").unwrap(); + let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()]; + let vars: &[CString] = &[]; + let actions = PosixSpawnFileActions::init().unwrap(); + let attr = PosixSpawnAttr::init().unwrap(); + + let pid = + spawn::posix_spawn(bin.as_path(), &actions, &attr, args, vars).unwrap(); + + let status = + waitpid(pid, WaitPidFlag::from_bits(WaitPidFlag::WNOHANG.bits())) + .unwrap(); + match status { + WaitStatus::StillAlive => {} + _ => { + panic!("Invalid WaitStatus"); + } + }; + + signal::kill(pid, signal::SIGTERM).unwrap(); + + let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap(); + match status { + WaitStatus::Signaled(wpid, wsignal, _) => { + assert_eq!(pid, wpid); + assert_eq!(wsignal, signal::SIGTERM); + } + _ => { + panic!("Invalid WaitStatus"); + } + }; +} + +#[test] +fn spawn_cmd_does_not_exist() { + let _guard = FORK_MTX.lock(); + + let args = &[CString::new("buzz").unwrap()]; + let envs: &[CString] = &[]; + let actions = PosixSpawnFileActions::init().unwrap(); + let attr = PosixSpawnAttr::init().unwrap(); + + let bin = "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf"; + let pid = spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap(); +} + +#[test] +fn spawnp_true() { + let _guard = FORK_MTX.lock(); + let bin = &CString::new("true").unwrap(); let args = &[ CString::new("true").unwrap(), @@ -33,7 +112,7 @@ fn spawn_true() { } #[test] -fn spawn_sleep() { +fn spawnp_sleep() { let _guard = FORK_MTX.lock(); let bin = &CString::new("sleep").unwrap(); From 3274fb25aff812138036d6b2216719faca05e79b Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sun, 2 Feb 2025 22:39:33 +0800 Subject: [PATCH 2/7] chore: downgrade which to 5.0.0 due to MSRV limit --- Cargo.toml | 2 +- test/test_spawn.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 27fca64510..9ea515d5cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,7 @@ nix = { path = ".", features = ["acct", "aio", "dir", "env", "event", "fanotify" "net", "personality", "poll", "pthread", "ptrace", "quota", "process", "reboot", "resource", "sched", "signal", "socket", "syslog", "term", "time", "ucontext", "uio", "user", "zerocopy"] } -which = "7.0.1" +which = "5.0.0" [target.'cfg(any(target_os = "android", target_os = "linux"))'.dev-dependencies] caps = "0.5.3" diff --git a/test/test_spawn.rs b/test/test_spawn.rs index 05db0d3273..bf3b54b576 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -80,7 +80,7 @@ fn spawn_cmd_does_not_exist() { let attr = PosixSpawnAttr::init().unwrap(); let bin = "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf"; - let pid = spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap(); + let _pid = spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap(); } #[test] From b6fc0a84a5a370f4482ccc11f82ee06a19e1a129 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 4 Feb 2025 17:46:57 +0800 Subject: [PATCH 3/7] chore: use a homemade which --- Cargo.toml | 1 - test/test_spawn.rs | 20 ++++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9ea515d5cd..93ce205d31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,6 @@ nix = { path = ".", features = ["acct", "aio", "dir", "env", "event", "fanotify" "net", "personality", "poll", "pthread", "ptrace", "quota", "process", "reboot", "resource", "sched", "signal", "socket", "syslog", "term", "time", "ucontext", "uio", "user", "zerocopy"] } -which = "5.0.0" [target.'cfg(any(target_os = "android", target_os = "linux"))'.dev-dependencies] caps = "0.5.3" diff --git a/test/test_spawn.rs b/test/test_spawn.rs index bf3b54b576..ff67072688 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -4,11 +4,27 @@ use nix::sys::signal; use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus}; use std::ffi::CString; +/// Helper function to find a binary in the $PATH +fn which(exe_name: &str) -> Option { + std::env::var_os("PATH").and_then(|paths| { + std::env::split_paths(&paths) + .filter_map(|dir| { + let full_path = dir.join(&exe_name); + if full_path.is_file() { + Some(full_path) + } else { + None + } + }) + .next() + }) +} + #[test] fn spawn_true() { let _guard = FORK_MTX.lock(); - let bin = which::which("true").unwrap(); + let bin = which("true").unwrap(); let args = &[ CString::new("true").unwrap(), CString::new("story").unwrap(), @@ -37,7 +53,7 @@ fn spawn_true() { fn spawn_sleep() { let _guard = FORK_MTX.lock(); - let bin = which::which("sleep").unwrap(); + let bin = which("sleep").unwrap(); let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()]; let vars: &[CString] = &[]; let actions = PosixSpawnFileActions::init().unwrap(); From ac9fe9e98c804550dca029524a7ec4b2fd3ef125 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 4 Feb 2025 17:50:23 +0800 Subject: [PATCH 4/7] refactor: clippy fix --- test/test_spawn.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_spawn.rs b/test/test_spawn.rs index ff67072688..50dd6d92dd 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -9,7 +9,7 @@ fn which(exe_name: &str) -> Option { std::env::var_os("PATH").and_then(|paths| { std::env::split_paths(&paths) .filter_map(|dir| { - let full_path = dir.join(&exe_name); + let full_path = dir.join(exe_name); if full_path.is_file() { Some(full_path) } else { From 792fdd1bd627e01c8448119d3f268e80ae0e8ded Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 4 Feb 2025 17:57:10 +0800 Subject: [PATCH 5/7] test: test posix_spawn() --- test/test_spawn.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/test_spawn.rs b/test/test_spawn.rs index 50dd6d92dd..2b705377ea 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -1,4 +1,5 @@ use super::FORK_MTX; +use nix::errno::Errno; use nix::spawn::{self, PosixSpawnAttr, PosixSpawnFileActions}; use nix::sys::signal; use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus}; @@ -96,7 +97,9 @@ fn spawn_cmd_does_not_exist() { let attr = PosixSpawnAttr::init().unwrap(); let bin = "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf"; - let _pid = spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap(); + let errno = + spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap_err(); + assert_eq!(errno, Errno::ENOENT); } #[test] @@ -162,3 +165,18 @@ fn spawnp_sleep() { } }; } + +#[test] +fn spawnp_cmd_does_not_exist() { + let _guard = FORK_MTX.lock(); + + let args = &[CString::new("buzz").unwrap()]; + let envs: &[CString] = &[]; + let actions = PosixSpawnFileActions::init().unwrap(); + let attr = PosixSpawnAttr::init().unwrap(); + + let bin = c"2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf"; + let errno = + spawn::posix_spawnp(bin, &actions, &attr, args, envs).unwrap_err(); + assert_eq!(errno, Errno::ENOENT); +} From 1aa9d057228f192c5acf7d1071296da336eaa1b1 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 4 Feb 2025 18:01:24 +0800 Subject: [PATCH 6/7] refactor: CStr literal syntax is not supported --- test/test_spawn.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/test_spawn.rs b/test/test_spawn.rs index 2b705377ea..6fac955be1 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -3,7 +3,7 @@ use nix::errno::Errno; use nix::spawn::{self, PosixSpawnAttr, PosixSpawnFileActions}; use nix::sys::signal; use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus}; -use std::ffi::CString; +use std::ffi::{CStr, CString}; /// Helper function to find a binary in the $PATH fn which(exe_name: &str) -> Option { @@ -175,7 +175,10 @@ fn spawnp_cmd_does_not_exist() { let actions = PosixSpawnFileActions::init().unwrap(); let attr = PosixSpawnAttr::init().unwrap(); - let bin = c"2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf"; + let bin = CStr::from_bytes_with_nul( + "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf\0".as_bytes(), + ) + .unwrap(); let errno = spawn::posix_spawnp(bin, &actions, &attr, args, envs).unwrap_err(); assert_eq!(errno, Errno::ENOENT); From 832594e3a4268820dcc1e995a04b3f321248a938 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 4 Feb 2025 18:17:12 +0800 Subject: [PATCH 7/7] test: skip testing error cases under QEMU --- test/test_spawn.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_spawn.rs b/test/test_spawn.rs index 6fac955be1..a5e69b97f3 100644 --- a/test/test_spawn.rs +++ b/test/test_spawn.rs @@ -88,6 +88,10 @@ fn spawn_sleep() { } #[test] +// `posix_spawn(path_not_exist)` succeeds under QEMU, so ignore the test. No need +// to investigate the root cause, this test still works in native environments, which +// is sufficient to test the binding. +#[cfg_attr(qemu, ignore)] fn spawn_cmd_does_not_exist() { let _guard = FORK_MTX.lock(); @@ -167,6 +171,10 @@ fn spawnp_sleep() { } #[test] +// `posix_spawnp(bin_not_exist)` succeeds under QEMU, so ignore the test. No need +// to investigate the root cause, this test still works in native environments, which +// is sufficient to test the binding. +#[cfg_attr(qemu, ignore)] fn spawnp_cmd_does_not_exist() { let _guard = FORK_MTX.lock();