Skip to content
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

Add getpgid #380

Merged
merged 4 commits into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}