From 67f695a051d8fc66bf68c94e4abd69b074d33780 Mon Sep 17 00:00:00 2001 From: Kamal Marhubi Date: Sun, 27 Dec 2015 23:42:15 +0400 Subject: Add safe wrappers for getuid, geteuid, getgid, getegid Fixes #213 --- src/unistd.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/unistd.rs') diff --git a/src/unistd.rs b/src/unistd.rs index aba2d79c..ce755d62 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -4,7 +4,7 @@ use {Error, Result, NixPath, from_ffi}; use errno::Errno; use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC}; use fcntl::FcntlArg::{F_SETFD, F_SETFL}; -use libc::{c_char, c_void, c_int, size_t, pid_t, off_t}; +use libc::{c_char, c_void, c_int, size_t, pid_t, off_t, uid_t, gid_t}; use std::mem; use std::ffi::CString; use std::os::unix::io::RawFd; @@ -15,7 +15,7 @@ pub use self::linux::*; mod ffi { use libc::{c_char, c_int, size_t}; pub use libc::{close, read, write, pipe, ftruncate, unlink, setpgid}; - pub use libc::funcs::posix88::unistd::{fork, getpid, getppid}; + pub use libc::funcs::posix88::unistd::{fork, getegid, geteuid, getgid, getpid, getppid, getuid}; extern { // duplicate a file descriptor @@ -389,6 +389,32 @@ pub fn fdatasync(fd: RawFd) -> Result<()> { Ok(()) } +// POSIX requires that getuid, geteuid, getgid, getegid are always successful, +// so no need to check return value or errno. See: +// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html +// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/geteuid.html +// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/getgid.html +// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/geteuid.html +#[inline] +pub fn getuid() -> uid_t { + unsafe { ffi::getuid() } +} + +#[inline] +pub fn geteuid() -> uid_t { + unsafe { ffi::geteuid() } +} + +#[inline] +pub fn getgid() -> gid_t { + unsafe { ffi::getgid() } +} + +#[inline] +pub fn getegid() -> gid_t { + unsafe { ffi::getegid() } +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod linux { use sys::syscall::{syscall, SYSPIVOTROOT}; -- cgit v1.2.3