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 getresuid() and getresgid() to unistd #1430

Merged
merged 1 commit into from
May 31, 2021
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.
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 @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased] - ReleaseDate
### Added
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the CHANGELOG. There was a merge error, and now there are two "Added" sections.

- Added `getresuid` and `getresgid`
(#[1430](https://github.com/nix-rust/nix/pull/1430))
- Added TIMESTAMPNS support for linux
(#[1402](https://github.com/nix-rust/nix/pull/1402))
- Added `sendfile64` (#[1439](https://github.com/nix-rust/nix/pull/1439))
Expand Down
64 changes: 64 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub use self::pivot_root::*;
target_os = "linux", target_os = "openbsd"))]
pub use self::setres::*;

#[cfg(any(target_os = "android", target_os = "linux"))]
pub use self::getres::*;

/// User identifier
///
/// Newtype pattern around `uid_t` (which is just alias). It prevents bugs caused by accidentally
Expand Down Expand Up @@ -2516,6 +2519,67 @@ mod setres {
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
mod getres {
use crate::Result;
use crate::errno::Errno;
use super::{Uid, Gid};

/// Real, effective and saved user IDs.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ResUid {
pub real: Uid,
pub effective: Uid,
pub saved: Uid
}

/// Real, effective and saved group IDs.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct ResGid {
pub real: Gid,
pub effective: Gid,
pub saved: Gid
}

/// Gets the real, effective, and saved user IDs.
///
/// ([see getresuid(2)](http://man7.org/linux/man-pages/man2/getresuid.2.html))
jerylvaz marked this conversation as resolved.
Show resolved Hide resolved
///
/// #Returns
///
/// - `Ok((Uid, Uid, Uid))`: tuple of real, effective and saved uids on success.
/// - `Err(x)`: libc error code on failure.
///
#[inline]
pub fn getresuid() -> Result<ResUid> {
let mut ruid = libc::uid_t::max_value();
let mut euid = libc::uid_t::max_value();
let mut suid = libc::uid_t::max_value();
let res = unsafe { libc::getresuid(&mut ruid, &mut euid, &mut suid) };

Errno::result(res).map(|_| ResUid{ real: Uid(ruid), effective: Uid(euid), saved: Uid(suid) })
}

/// Gets the real, effective, and saved group IDs.
///
/// ([see getresgid(2)](http://man7.org/linux/man-pages/man2/getresgid.2.html))
///
/// #Returns
///
/// - `Ok((Gid, Gid, Gid))`: tuple of real, effective and saved gids on success.
/// - `Err(x)`: libc error code on failure.
///
#[inline]
pub fn getresgid() -> Result<ResGid> {
let mut rgid = libc::gid_t::max_value();
let mut egid = libc::gid_t::max_value();
let mut sgid = libc::gid_t::max_value();
let res = unsafe { libc::getresgid(&mut rgid, &mut egid, &mut sgid) };

Errno::result(res).map(|_| ResGid { real: Gid(rgid), effective: Gid(egid), saved: Gid(sgid) } )
}
}

libc_bitflags!{
/// Options for access()
pub struct AccessFlags : c_int {
Expand Down
19 changes: 19 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,25 @@ fn test_sysconf_unsupported() {
assert!(open_max.expect("sysconf failed").is_none())
}


#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
fn test_getresuid() {
let resuids = getresuid().unwrap();
assert!(resuids.real.as_raw() != libc::uid_t::max_value());
assert!(resuids.effective.as_raw() != libc::uid_t::max_value());
assert!(resuids.saved.as_raw() != libc::uid_t::max_value());
}

#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
fn test_getresgid() {
let resgids = getresgid().unwrap();
assert!(resgids.real.as_raw() != libc::gid_t::max_value());
assert!(resgids.effective.as_raw() != libc::gid_t::max_value());
assert!(resgids.saved.as_raw() != libc::gid_t::max_value());
}

// Test that we can create a pair of pipes. No need to verify that they pass
// data; that's the domain of the OS, not nix.
#[test]
Expand Down