diff --git a/CHANGELOG.md b/CHANGELOG.md index dbe6110ab7..d3831a8344 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added +- Added `getsid` in `::nix::unistd` + ([#850](https://github.com/nix-rust/nix/pull/850)) ### Changed diff --git a/src/unistd.rs b/src/unistd.rs index 0f47a7fc35..1c858c8ca7 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -264,6 +264,17 @@ pub fn setsid() -> Result { Errno::result(unsafe { libc::setsid() }).map(Pid) } +/// Get the process group ID of a session leader +/// [getsid(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsid.html). +/// +/// Obtain the process group ID of the process that is the session leader of the process specified +/// by pid. If pid is zero, it specifies the calling process. +#[inline] +pub fn getsid(pid: Option) -> Result { + let res = unsafe { libc::getsid(pid.unwrap_or(Pid(0)).into()) }; + Errno::result(res).map(Pid) +} + /// Get the terminal foreground process group (see /// [tcgetpgrp(3)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetpgrp.html)). diff --git a/test/test_unistd.rs b/test/test_unistd.rs index e15fec17d7..d0db55f66e 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -107,6 +107,14 @@ fn test_getpid() { assert!(ppid > 0); } +#[test] +fn test_getsid() { + let none_sid: ::libc::pid_t = getsid(None).unwrap().into(); + let pid_sid: ::libc::pid_t = getsid(Some(getpid())).unwrap().into(); + assert!(none_sid > 0); + assert!(none_sid == pid_sid); +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod linux_android { use nix::unistd::gettid;