@@ -4,7 +4,7 @@ use {Error, Result, NixPath, from_ffi};
4
4
use errno:: Errno ;
5
5
use fcntl:: { fcntl, OFlag , O_NONBLOCK , O_CLOEXEC , FD_CLOEXEC } ;
6
6
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 } ;
8
8
use std:: mem;
9
9
use std:: ffi:: CString ;
10
10
use std:: os:: unix:: io:: RawFd ;
@@ -15,7 +15,7 @@ pub use self::linux::*;
15
15
mod ffi {
16
16
use libc:: { c_char, c_int, size_t} ;
17
17
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 } ;
19
19
20
20
extern {
21
21
// duplicate a file descriptor
@@ -389,6 +389,32 @@ pub fn fdatasync(fd: RawFd) -> Result<()> {
389
389
Ok ( ( ) )
390
390
}
391
391
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
+
392
418
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
393
419
mod linux {
394
420
use sys:: syscall:: { syscall, SYSPIVOTROOT } ;
0 commit comments