uucore: add getsid for process.rs (#6600)

* uucore: Add `getsid` for `process.rs`

* uucore: Changing the return type of the `getsid` function to `Result`

From the manpage of `getsid` we can find that the syscall may failed and set errno, so we have to process those case.

* uucore: Drop support of `getsid` for RedoxOS

* uucore: Add `getpid` for get pid of calling process.

* uucore: Add tests for `getsid` and `getpid`

* uucore: Fix tests failure for `test_getsid`

uucore: Fix tests failure for `test_getsid`

uucore: Fix tests failure for `test_getsid`

* uucore: Fix tests failure on FreeBSD

* uucore: Apply suggestions for `process.rs::tests::test_getsid`

Suggestions from @BenWiederhake , very thanks!
This commit is contained in:
Krysztal Huang 2024-07-30 04:09:12 +08:00 committed by GitHub
parent 810ec0f3cf
commit bc0b4880e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,11 +3,12 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (vars) cvar exitstatus cmdline kworker
// spell-checker:ignore (sys/unix) WIFSIGNALED
// spell-checker:ignore (vars) cvar exitstatus cmdline kworker getsid getpid
// spell-checker:ignore (sys/unix) WIFSIGNALED ESRCH
// spell-checker:ignore pgrep pwait snice
use libc::{gid_t, pid_t, uid_t};
use nix::errno::Errno;
use std::io;
use std::process::Child;
use std::process::ExitStatus;
@ -36,6 +37,37 @@ pub fn getuid() -> uid_t {
unsafe { libc::getuid() }
}
/// `getpid()` returns the pid of the calling process.
pub fn getpid() -> pid_t {
unsafe { libc::getpid() }
}
/// `getsid()` returns the session ID of the process with process ID pid.
///
/// If pid is 0, getsid() returns the session ID of the calling process.
///
/// # Error
///
/// - [Errno::EPERM] A process with process ID pid exists, but it is not in the same session as the calling process, and the implementation considers this an error.
/// - [Errno::ESRCH] No process with process ID pid was found.
///
///
/// # Platform
///
/// This function only support standard POSIX implementation platform,
/// so some system such as redox doesn't supported.
#[cfg(not(target_os = "redox"))]
pub fn getsid(pid: i32) -> Result<pid_t, Errno> {
unsafe {
let result = libc::getsid(pid);
if Errno::last() == Errno::UnknownErrno {
Ok(result)
} else {
Err(Errno::last())
}
}
}
/// Missing methods for Child objects
pub trait ChildExt {
/// Send a signal to a Child process.
@ -99,3 +131,25 @@ impl ChildExt for Child {
Ok(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(not(target_os = "redox"))]
fn test_getsid() {
assert_eq!(
getsid(getpid()).expect("getsid(getpid)"),
// zero is a special value for SID.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html
getsid(0).expect("getsid(0)")
);
// SID never be 0.
assert!(getsid(getpid()).expect("getsid(getpid)") > 0);
// This might caused tests failure but the probability is low.
assert!(getsid(999999).is_err());
}
}