Skip to content

Commit 67f695a

Browse files
kamalmarhubicarllerche
authored andcommitted
Add safe wrappers for getuid, geteuid, getgid, getegid
Fixes nix-rust#213
1 parent b40046e commit 67f695a

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/unistd.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use {Error, Result, NixPath, from_ffi};
44
use errno::Errno;
55
use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
66
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
7-
use libc::{c_char, c_void, c_int, size_t, pid_t, off_t};
7+
use libc::{c_char, c_void, c_int, size_t, pid_t, off_t, uid_t, gid_t};
88
use std::mem;
99
use std::ffi::CString;
1010
use std::os::unix::io::RawFd;
@@ -15,7 +15,7 @@ pub use self::linux::*;
1515
mod ffi {
1616
use libc::{c_char, c_int, size_t};
1717
pub use libc::{close, read, write, pipe, ftruncate, unlink, setpgid};
18-
pub use libc::funcs::posix88::unistd::{fork, getpid, getppid};
18+
pub use libc::funcs::posix88::unistd::{fork, getegid, geteuid, getgid, getpid, getppid, getuid};
1919

2020
extern {
2121
// duplicate a file descriptor
@@ -389,6 +389,32 @@ pub fn fdatasync(fd: RawFd) -> Result<()> {
389389
Ok(())
390390
}
391391

392+
// POSIX requires that getuid, geteuid, getgid, getegid are always successful,
393+
// so no need to check return value or errno. See:
394+
// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/getuid.html
395+
// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/geteuid.html
396+
// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/getgid.html
397+
// - http://pubs.opengroup.org/onlinepubs/9699919799/functions/geteuid.html
398+
#[inline]
399+
pub fn getuid() -> uid_t {
400+
unsafe { ffi::getuid() }
401+
}
402+
403+
#[inline]
404+
pub fn geteuid() -> uid_t {
405+
unsafe { ffi::geteuid() }
406+
}
407+
408+
#[inline]
409+
pub fn getgid() -> gid_t {
410+
unsafe { ffi::getgid() }
411+
}
412+
413+
#[inline]
414+
pub fn getegid() -> gid_t {
415+
unsafe { ffi::getegid() }
416+
}
417+
392418
#[cfg(any(target_os = "linux", target_os = "android"))]
393419
mod linux {
394420
use sys::syscall::{syscall, SYSPIVOTROOT};

0 commit comments

Comments
 (0)