Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: SteveLauC <stevelauc@outlook.com>
  • Loading branch information
kosayoda and SteveLauC committed Apr 24, 2024
1 parent 4db179f commit ce6aeb3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 88 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ mod poll_timeout;
target_os = "haiku",
target_os = "linux",
target_os = "netbsd",
target_os = "macos",
target_os = "ios"
apple_targets
))]
feature! {
#![feature = "process"]
Expand Down
121 changes: 35 additions & 86 deletions src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ impl PosixSpawnAttr {
pub fn init() -> Result<PosixSpawnAttr> {
let mut attr = mem::MaybeUninit::uninit();
let res = unsafe { libc::posix_spawnattr_init(attr.as_mut_ptr()) };

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

let attr = unsafe { attr.assume_init() };
Ok(PosixSpawnAttr { attr })
Expand All @@ -49,18 +46,7 @@ impl PosixSpawnAttr {
&mut self.attr as *mut libc::posix_spawnattr_t,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}

let res = unsafe {
libc::posix_spawnattr_init(
&mut self.attr as *mut libc::posix_spawnattr_t,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(self)
}
Expand All @@ -75,27 +61,23 @@ impl PosixSpawnAttr {
flags.bits() as libc::c_short,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(())
}

/// Get spawn flags. See
/// [posix_spawnattr_getflags](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html).
#[doc(alias("posix_spawnattr_getflags"))]
pub fn flags(&mut self) -> Result<PosixSpawnFlags> {
pub fn flags(&self) -> Result<PosixSpawnFlags> {
let mut flags: libc::c_short = 0;
let res = unsafe {
libc::posix_spawnattr_getflags(
&mut self.attr as *mut libc::posix_spawnattr_t,
&self.attr as *const libc::posix_spawnattr_t,
&mut flags,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(PosixSpawnFlags::from_bits_truncate(flags.into()))
}
Expand All @@ -110,28 +92,22 @@ impl PosixSpawnAttr {
pgroup.as_raw(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}

Ok(())
Errno::result(res).map(drop)
}

/// Get spawn pgroup. See
/// [posix_spawnattr_getpgroup](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html).
#[doc(alias("posix_spawnattr_getpgroup"))]
pub fn pgroup(&mut self) -> Result<Pid> {
pub fn pgroup(&self) -> Result<Pid> {
let mut pid: libc::pid_t = 0;

let res = unsafe {
libc::posix_spawnattr_getpgroup(
&mut self.attr as *mut libc::posix_spawnattr_t,
&self.attr as *const libc::posix_spawnattr_t,
&mut pid,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(Pid::from_raw(pid))
}
Expand All @@ -148,28 +124,24 @@ impl PosixSpawnAttr {
sigdefault.as_ref(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(())
}

/// Get spawn sigdefault. See
/// [posix_spawnattr_getsigdefault](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html).
#[doc(alias("posix_spawnattr_getsigdefault"))]
pub fn sigdefault(&mut self) -> Result<SigSet> {
pub fn sigdefault(&self) -> Result<SigSet> {
let mut sigset = mem::MaybeUninit::uninit();

let res = unsafe {
libc::posix_spawnattr_getsigdefault(
&mut self.attr as *mut libc::posix_spawnattr_t,
&self.attr as *const libc::posix_spawnattr_t,
sigset.as_mut_ptr(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

let sigdefault =
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
Expand All @@ -186,28 +158,24 @@ impl PosixSpawnAttr {
sigdefault.as_ref(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(())
}

/// Get spawn sigmask. See
/// [posix_spawnattr_getsigmask](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html).
#[doc(alias("posix_spawnattr_getsigmask"))]
pub fn sigmask(&mut self) -> Result<SigSet> {
pub fn sigmask(&self) -> Result<SigSet> {
let mut sigset = mem::MaybeUninit::uninit();

let res = unsafe {
libc::posix_spawnattr_getsigmask(
&mut self.attr as *mut libc::posix_spawnattr_t,
&self.attr as *const libc::posix_spawnattr_t,
sigset.as_mut_ptr(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

let sigdefault =
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
Expand Down Expand Up @@ -268,16 +236,13 @@ impl PosixSpawnFileActions {
let res = unsafe {
libc::posix_spawn_file_actions_init(actions.as_mut_ptr())
};
Errno::result(res)?;

if res == 0 {
Ok(unsafe {
PosixSpawnFileActions {
fa: actions.assume_init(),
}
})
} else {
Err(Errno::from_raw(res))
}
Ok(unsafe {
PosixSpawnFileActions {
fa: actions.assume_init(),
}
})
}

/// Reinitialize the spawn file actions object.
Expand All @@ -292,18 +257,14 @@ impl PosixSpawnFileActions {
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

let res = unsafe {
libc::posix_spawn_file_actions_init(
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(self)
}
Expand All @@ -323,9 +284,7 @@ impl PosixSpawnFileActions {
newfd.as_fd().as_raw_fd(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(())
}
Expand All @@ -351,9 +310,7 @@ impl PosixSpawnFileActions {
mode.bits(),
)
})?;
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(())
}
Expand All @@ -369,9 +326,7 @@ impl PosixSpawnFileActions {
fd.as_fd().as_raw_fd(),
)
};
if res != 0 {
return Err(Errno::from_raw(res));
}
Errno::result(res)?;

Ok(())
}
Expand All @@ -392,9 +347,9 @@ impl Drop for PosixSpawnFileActions {
unsafe fn to_exec_array<S: AsRef<CStr>>(args: &[S]) -> Vec<*mut libc::c_char> {
let mut v: Vec<*mut libc::c_char> = args
.iter()
.map(|s| s.as_ref().as_ptr() as *mut libc::c_char)
.map(|s| s.as_ref().as_ptr().cast_mut())
.collect();
v.push(std::ptr::null::<libc::c_char>() as *mut libc::c_char);
v.push(std::ptr::null_mut());
v
}

Expand Down Expand Up @@ -422,12 +377,9 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
env_p.as_ptr(),
)
};
Errno::result(res)?;

if res == 0 {
Ok(Pid::from_raw(pid))
} else {
Err(Errno::from_raw(res))
}
Ok(Pid::from_raw(pid))
}

/// Create a new child process from the specified process image. See
Expand All @@ -454,10 +406,7 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
env_p.as_ptr(),
)
};
Errno::result(res)?;

if res == 0 {
Ok(Pid::from_raw(pid))
} else {
Err(Errno::from_raw(res))
}
Ok(Pid::from_raw(pid))
}

0 comments on commit ce6aeb3

Please sign in to comment.