Skip to content

Commit 8d0e312

Browse files
committed
Auto merge of #448 - dgreid:setresuid, r=posborne
Add setresuid and setresgid These were both recently added to libc, add wrappers. Signed-off-by: Dylan Reid <dgreid@chromium.org>
2 parents 284c9f4 + 6c85f43 commit 8d0e312

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1212
- Added function `epoll_create1` and bitflags `EpollCreateFlags` in
1313
`::nix::sys::epoll` in order to support `::libc::epoll_create1`.
1414
([#410](https://github.com/nix-rust/nix/pull/410))
15+
- Added `setresuid` and `setresgid` for Linux in `::nix::unistd`
16+
([#448](https://github.com/nix-rust/nix/pull/448))
1517

1618
### Changed
1719
- The minimum supported version of rustc is now 1.7.0.

src/unistd.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
568568

569569
#[cfg(any(target_os = "linux", target_os = "android"))]
570570
mod linux {
571+
use libc::{self, uid_t, gid_t};
571572
use sys::syscall::{syscall, SYSPIVOTROOT};
572573
use {Errno, Result, NixPath};
573574

@@ -587,6 +588,38 @@ mod linux {
587588
Errno::result(res).map(drop)
588589
}
589590

591+
/// Sets the real, effective, and saved uid.
592+
/// ([see setresuid(2)](http://man7.org/linux/man-pages/man2/setresuid.2.html))
593+
///
594+
/// * `ruid`: real user id
595+
/// * `euid`: effective user id
596+
/// * `suid`: saved user id
597+
/// * returns: Ok or libc error code.
598+
///
599+
/// Err is returned if the user doesn't have permission to set this UID.
600+
#[inline]
601+
pub fn setresuid(ruid: uid_t, euid: uid_t, suid: uid_t) -> Result<()> {
602+
let res = unsafe { libc::setresuid(ruid, euid, suid) };
603+
604+
Errno::result(res).map(drop)
605+
}
606+
607+
/// Sets the real, effective, and saved gid.
608+
/// ([see setresuid(2)](http://man7.org/linux/man-pages/man2/setresuid.2.html))
609+
///
610+
/// * `rgid`: real user id
611+
/// * `egid`: effective user id
612+
/// * `sgid`: saved user id
613+
/// * returns: Ok or libc error code.
614+
///
615+
/// Err is returned if the user doesn't have permission to set this GID.
616+
#[inline]
617+
pub fn setresgid(rgid: gid_t, egid: gid_t, sgid: gid_t) -> Result<()> {
618+
let res = unsafe { libc::setresgid(rgid, egid, sgid) };
619+
620+
Errno::result(res).map(drop)
621+
}
622+
590623
#[inline]
591624
#[cfg(feature = "execvpe")]
592625
pub fn execvpe(filename: &CString, args: &[CString], env: &[CString]) -> Result<()> {

0 commit comments

Comments
 (0)