From e62ae014beb5e35fc53f80a39e8c4cc58a83cc5a Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Mon, 11 Jul 2022 14:40:12 -0500 Subject: [PATCH 1/4] Add getpgid --- src/backend/libc/process/syscalls.rs | 13 ++++++++++++- src/backend/linux_raw/process/syscalls.rs | 11 +++++++++++ src/process/id.rs | 14 ++++++++++++++ src/process/mod.rs | 4 ++-- tests/process/id.rs | 12 ++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/backend/libc/process/syscalls.rs b/src/backend/libc/process/syscalls.rs index 9822a151a..6ce00fd6c 100644 --- a/src/backend/libc/process/syscalls.rs +++ b/src/backend/libc/process/syscalls.rs @@ -145,6 +145,17 @@ pub(crate) fn getppid() -> Option { } } +#[cfg(not(target_os = "wasi"))] +#[inline] +#[must_use] +pub(crate) fn getpgid(pid: Option) -> Pid { + unsafe { + let pgid = c::getpgid(Pid::as_raw(pid) as _); + debug_assert_ne!(pgid, 0); + Ok(Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid))) + } +} + #[cfg(any( target_os = "android", target_os = "dragonfly", @@ -389,7 +400,7 @@ pub(crate) fn exit_group(code: c::c_int) -> ! { #[inline] pub(crate) fn setsid() -> io::Result { unsafe { - let pid = ret_c_int(c::setsid())?; + let pid: bool = ret_c_int(c::setsid())?; debug_assert_ne!(pid, 0); Ok(Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pid))) } diff --git a/src/backend/linux_raw/process/syscalls.rs b/src/backend/linux_raw/process/syscalls.rs index 37284f803..d5bf60564 100644 --- a/src/backend/linux_raw/process/syscalls.rs +++ b/src/backend/linux_raw/process/syscalls.rs @@ -99,6 +99,17 @@ pub(crate) fn getppid() -> Option { } } +#[inline] +pub(crate) fn getpgid(pid: Option) -> Pid { + unsafe { + let pgid: i32 = + ret_usize_infallible(syscall_readonly!(__NR_getpgid, c_uint(Pid::as_raw(pid)))) + as __kernel_pid_t; + debug_assert!(pgid > 0); + Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid as u32)) + } +} + #[inline] pub(crate) fn getgid() -> Gid { #[cfg(any(target_arch = "x86", target_arch = "sparc", target_arch = "arm"))] diff --git a/src/process/id.rs b/src/process/id.rs index c8d2318bb..e95987379 100644 --- a/src/process/id.rs +++ b/src/process/id.rs @@ -267,6 +267,20 @@ pub fn getppid() -> Option { backend::process::syscalls::getppid() } +/// `getpgid()`—Returns the process group ID of the given process. +/// +/// # References +/// - [POSIX] +/// - [Linux] +/// +/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpgid.html +/// [Linux]: https://man7.org/linux/man-pages/man2/getpgid.2.html +#[inline] +#[must_use] +pub fn getpgid(pid: Option) -> Pid { + backend::process::syscalls::getpgid(pid) +} + /// `setsid()`—Create a new session. /// /// # References diff --git a/src/process/mod.rs b/src/process/mod.rs index 45673c0ea..6d7a61623 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -39,8 +39,8 @@ pub use exit::{EXIT_FAILURE, EXIT_SUCCESS}; pub use id::Cpuid; #[cfg(not(target_os = "wasi"))] pub use id::{ - getegid, geteuid, getgid, getpid, getppid, getuid, setsid, Gid, Pid, RawGid, RawNonZeroPid, - RawPid, RawUid, Uid, + getegid, geteuid, getgid, getpgid, getpid, getppid, getuid, setsid, Gid, Pid, RawGid, + RawNonZeroPid, RawPid, RawUid, Uid, }; #[cfg(not(target_os = "wasi"))] pub use kill::{kill_current_process_group, kill_process, kill_process_group, Signal}; diff --git a/tests/process/id.rs b/tests/process/id.rs index 10095fa60..33c2fda53 100644 --- a/tests/process/id.rs +++ b/tests/process/id.rs @@ -63,3 +63,15 @@ fn test_getppid() { } } } + +#[test] +fn test_getpgid() { + assert_eq!(process::getpgid(None), process::getpgid(None)); + unsafe { + assert_eq!( + process::getpgid(None).as_raw_nonzero().get() as libc::pid_t, + libc::getpgid(0) + ); + assert_eq!(process::getpgid(None).is_init(), libc::getpgid(0) == 1); + } +} From b1ba47cae287466822a4dd873e526f9c407bab6e Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Mon, 11 Jul 2022 17:13:48 -0500 Subject: [PATCH 2/4] Address nit --- src/process/id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process/id.rs b/src/process/id.rs index e95987379..9e061bf9c 100644 --- a/src/process/id.rs +++ b/src/process/id.rs @@ -267,7 +267,7 @@ pub fn getppid() -> Option { backend::process::syscalls::getppid() } -/// `getpgid()`—Returns the process group ID of the given process. +/// `getpgid(pid)`—Returns the process group ID of the given process. /// /// # References /// - [POSIX] From 9d970cd5bbed1a26c9eba0261bf035c13f5e2f72 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Mon, 11 Jul 2022 17:56:09 -0500 Subject: [PATCH 3/4] Fix copy paste bugs --- src/backend/libc/process/syscalls.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/libc/process/syscalls.rs b/src/backend/libc/process/syscalls.rs index 6ce00fd6c..7a1a4939a 100644 --- a/src/backend/libc/process/syscalls.rs +++ b/src/backend/libc/process/syscalls.rs @@ -152,7 +152,7 @@ pub(crate) fn getpgid(pid: Option) -> Pid { unsafe { let pgid = c::getpgid(Pid::as_raw(pid) as _); debug_assert_ne!(pgid, 0); - Ok(Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid))) + Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid as u32)) } } @@ -400,7 +400,7 @@ pub(crate) fn exit_group(code: c::c_int) -> ! { #[inline] pub(crate) fn setsid() -> io::Result { unsafe { - let pid: bool = ret_c_int(c::setsid())?; + let pid = ret_c_int(c::setsid())?; debug_assert_ne!(pid, 0); Ok(Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pid))) } From 086a602cc44e19bc9df8bac4a862f2fc74f4b755 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Mon, 11 Jul 2022 20:34:06 -0500 Subject: [PATCH 4/4] Maybe now? --- src/backend/libc/process/syscalls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/libc/process/syscalls.rs b/src/backend/libc/process/syscalls.rs index 7a1a4939a..1b100e26b 100644 --- a/src/backend/libc/process/syscalls.rs +++ b/src/backend/libc/process/syscalls.rs @@ -152,7 +152,7 @@ pub(crate) fn getpgid(pid: Option) -> Pid { unsafe { let pgid = c::getpgid(Pid::as_raw(pid) as _); debug_assert_ne!(pgid, 0); - Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid as u32)) + Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid)) } }