Skip to content

fix: posix_spawn returns errno on error, not -1 #2595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/spawn.rs
Original file line number Diff line number Diff line change
@@ -370,7 +370,7 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
) -> Result<Pid> {
let mut pid = 0;

let res = unsafe {
let ret = unsafe {
let args_p = to_exec_array(args);
let env_p = to_exec_array(envp);

@@ -384,7 +384,10 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
)
};

Errno::result(res)?;
if ret != 0 {
return Err(Errno::from_raw(ret));
}

Ok(Pid::from_raw(pid))
}

@@ -399,7 +402,7 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
) -> Result<Pid> {
let mut pid = 0;

let res = unsafe {
let ret = unsafe {
let args_p = to_exec_array(args);
let env_p = to_exec_array(envp);

@@ -413,6 +416,9 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
)
};

Errno::result(res)?;
if ret != 0 {
return Err(Errno::from_raw(ret));
}

Ok(Pid::from_raw(pid))
}
8 changes: 6 additions & 2 deletions test/test_spawn.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::ffi::CString;

use super::FORK_MTX;
use nix::spawn::{self, PosixSpawnAttr, PosixSpawnFileActions};
use nix::sys::signal;
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use std::ffi::CString;

#[test]
fn spawn_true() {
let _guard = FORK_MTX.lock();

let bin = &CString::new("true").unwrap();
let args = &[
CString::new("true").unwrap(),
@@ -32,6 +34,8 @@ fn spawn_true() {

#[test]
fn spawn_sleep() {
let _guard = FORK_MTX.lock();

let bin = &CString::new("sleep").unwrap();
let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()];
let vars: &[CString] = &[];