Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uid>, euid: Option<Uid>, suid: Option<Uid>) -> 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)
}
Expand All @@ -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<Gid>, egid: Option<Gid>, sgid: Option<Gid>) -> 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)
}
Expand Down
16 changes: 16 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}