diff --git a/CHANGELOG.md b/CHANGELOG.md index c29e7a165f..6b27ad8e25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#972](https://github.com/nix-rust/nix/pull/972)) - Added `symlinkat` wrapper. ([#997](https://github.com/nix-rust/nix/pull/997)) +- Support for no-change of parameters of `setresuid` and `setresgid` wrapper + ([#1009](https://github.com/nix-rust/nix/pull/1009)) ### Changed ### Fixed diff --git a/src/unistd.rs b/src/unistd.rs index 2c00f7f036..46d46b6cc3 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -2286,8 +2286,11 @@ mod setres { /// /// Err is returned if the user doesn't have permission to set this UID. #[inline] - pub fn setresuid(ruid: Uid, euid: Uid, suid: Uid) -> Result<()> { - let res = unsafe { libc::setresuid(ruid.into(), euid.into(), suid.into()) }; + pub fn setresuid(ruid: Option, euid: Option, suid: Option) -> Result<()> { + let ruid = ruid.map(Into::into).unwrap_or((0 as libc::uid_t).wrapping_sub(1)); + let euid = euid.map(Into::into).unwrap_or((0 as libc::uid_t).wrapping_sub(1)); + let suid = suid.map(Into::into).unwrap_or((0 as libc::uid_t).wrapping_sub(1)); + let res = unsafe { libc::setresuid(ruid, euid, suid) }; Errno::result(res).map(drop) } @@ -2302,8 +2305,11 @@ mod setres { /// /// Err is returned if the user doesn't have permission to set this GID. #[inline] - pub fn setresgid(rgid: Gid, egid: Gid, sgid: Gid) -> Result<()> { - let res = unsafe { libc::setresgid(rgid.into(), egid.into(), sgid.into()) }; + pub fn setresgid(rgid: Option, egid: Option, sgid: Option) -> Result<()> { + let rgid = rgid.map(Into::into).unwrap_or((0 as libc::gid_t).wrapping_sub(1)); + let egid = egid.map(Into::into).unwrap_or((0 as libc::gid_t).wrapping_sub(1)); + let sgid = sgid.map(Into::into).unwrap_or((0 as libc::gid_t).wrapping_sub(1)); + let res = unsafe { libc::setresgid(rgid, egid, sgid) }; Errno::result(res).map(drop) } diff --git a/test/test_unistd.rs b/test/test_unistd.rs index ead4b4874e..cbceea9ee7 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -570,3 +570,19 @@ fn test_symlinkat() { target ); } + +#[cfg(any(target_os = "android", target_os = "freebsd", +target_os = "linux", target_os = "openbsd"))] +#[test] +fn test_setresuid() { + setresuid(Some(getuid()), Some(getuid()), Some(getuid())).unwrap(); + assert_eq!(geteuid(), getuid()); +} + +#[cfg(any(target_os = "android", target_os = "freebsd", +target_os = "linux", target_os = "openbsd"))] +#[test] +fn test_setresgid() { + setresgid(Some(getgid()), Some(getgid()), Some(getgid())).unwrap(); + assert_eq!(getegid(), getgid()); +}