diff options
author | Kamal Marhubi <kamal@marhubi.com> | 2015-12-27 23:42:15 +0400 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2016-01-13 09:34:30 -0800 |
commit | 67f695a051d8fc66bf68c94e4abd69b074d33780 (patch) | |
tree | 5940d3bf68f4704c058eaf26d020a7bc3c60c5f5 /src | |
parent | b40046e9852c43540a120ac60e0207e365e52547 (diff) | |
download | nix-67f695a051d8fc66bf68c94e4abd69b074d33780.zip |
Add safe wrappers for getuid, geteuid, getgid, getegid
Fixes #213
Diffstat (limited to 'src')
-rw-r--r-- | src/unistd.rs | 30 |
1 files changed, 28 insertions, 2 deletions
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}; |