Skip to content

Commit

Permalink
Add getpgid (#380)
Browse files Browse the repository at this point in the history
* Add getpgid
  • Loading branch information
carbotaniuman committed Jul 13, 2022
1 parent abd7471 commit 1ceb659
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/backend/libc/process/syscalls.rs
Expand Up @@ -145,6 +145,17 @@ pub(crate) fn getppid() -> Option<Pid> {
}
}

#[cfg(not(target_os = "wasi"))]
#[inline]
#[must_use]
pub(crate) fn getpgid(pid: Option<Pid>) -> Pid {
unsafe {
let pgid = c::getpgid(Pid::as_raw(pid) as _);
debug_assert_ne!(pgid, 0);
Pid::from_raw_nonzero(RawNonZeroPid::new_unchecked(pgid))
}
}

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
Expand Down
11 changes: 11 additions & 0 deletions src/backend/linux_raw/process/syscalls.rs
Expand Up @@ -99,6 +99,17 @@ pub(crate) fn getppid() -> Option<Pid> {
}
}

#[inline]
pub(crate) fn getpgid(pid: Option<Pid>) -> 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"))]
Expand Down
14 changes: 14 additions & 0 deletions src/process/id.rs
Expand Up @@ -267,6 +267,20 @@ pub fn getppid() -> Option<Pid> {
backend::process::syscalls::getppid()
}

/// `getpgid(pid)`—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>) -> Pid {
backend::process::syscalls::getpgid(pid)
}

/// `setsid()`—Create a new session.
///
/// # References
Expand Down
4 changes: 2 additions & 2 deletions src/process/mod.rs
Expand Up @@ -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};
Expand Down
12 changes: 12 additions & 0 deletions tests/process/id.rs
Expand Up @@ -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);
}
}

0 comments on commit 1ceb659

Please sign in to comment.