From 6c85f437c1df07a27e9e60c25c1322e9effc3a04 Mon Sep 17 00:00:00 2001 From: Dylan Reid Date: Tue, 25 Oct 2016 11:36:41 -0700 Subject: Add setresuid and setresgid These were both recently added to libc, add wrappers. Signed-off-by: Dylan Reid --- Changes since v2: Updated function comments and CHANGELOG Changes since v1: Add function comments, update CHANGELOG --- src/unistd.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 2c4a92c3..4549382d 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -568,6 +568,7 @@ pub fn mkstemp(template: &P) -> Result<(RawFd, PathBuf)> { #[cfg(any(target_os = "linux", target_os = "android"))] mod linux { + use libc::{self, uid_t, gid_t}; use sys::syscall::{syscall, SYSPIVOTROOT}; use {Errno, Result, NixPath}; @@ -587,6 +588,38 @@ mod linux { Errno::result(res).map(drop) } + /// Sets the real, effective, and saved uid. + /// ([see setresuid(2)](http://man7.org/linux/man-pages/man2/setresuid.2.html)) + /// + /// * `ruid`: real user id + /// * `euid`: effective user id + /// * `suid`: saved user id + /// * returns: Ok or libc error code. + /// + /// Err is returned if the user doesn't have permission to set this UID. + #[inline] + pub fn setresuid(ruid: uid_t, euid: uid_t, suid: uid_t) -> Result<()> { + let res = unsafe { libc::setresuid(ruid, euid, suid) }; + + Errno::result(res).map(drop) + } + + /// Sets the real, effective, and saved gid. + /// ([see setresuid(2)](http://man7.org/linux/man-pages/man2/setresuid.2.html)) + /// + /// * `rgid`: real user id + /// * `egid`: effective user id + /// * `sgid`: saved user id + /// * returns: Ok or libc error code. + /// + /// Err is returned if the user doesn't have permission to set this GID. + #[inline] + pub fn setresgid(rgid: gid_t, egid: gid_t, sgid: gid_t) -> Result<()> { + let res = unsafe { libc::setresgid(rgid, egid, sgid) }; + + Errno::result(res).map(drop) + } + #[inline] #[cfg(feature = "execvpe")] pub fn execvpe(filename: &CString, args: &[CString], env: &[CString]) -> Result<()> { -- cgit v1.2.3