@@ -13,7 +13,7 @@ pub use self::linux::*;
13
13
14
14
mod ffi {
15
15
use libc:: { c_char, c_int, size_t} ;
16
- pub use libc:: { fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid} ;
16
+ pub use libc:: { fork, close, read, write, pipe, ftruncate, unlink, setpgid, getegid, geteuid, getgid, getpid, getppid, getuid, setuid , setgid , chown } ;
17
17
18
18
#[ allow( improper_ctypes) ]
19
19
extern {
@@ -28,7 +28,7 @@ mod ffi {
28
28
29
29
// Execute PATH with arguments ARGV and environment from `environ'.
30
30
// doc: http://man7.org/linux/man-pages/man3/execv.3.html
31
- pub fn execv ( path : * const c_char , argv : * const * const c_char ) -> c_int ;
31
+ pub fn execv ( path : * const c_char , argv : * const * const c_char ) -> c_int ;
32
32
33
33
// execute program
34
34
// doc: http://man7.org/linux/man-pages/man2/execve.2.html
@@ -157,6 +157,16 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
157
157
Errno :: result ( res) . map ( drop)
158
158
}
159
159
160
+ #[ inline]
161
+ pub fn chown < P : ?Sized + NixPath > ( path : & P , owner : Option < uid_t > , group : Option < gid_t > ) -> Result < ( ) > {
162
+ let res = try!( path. with_nix_path ( |cstr| {
163
+ // We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3).
164
+ unsafe { ffi:: chown ( cstr. as_ptr ( ) , owner. unwrap_or ( 0 - 1 ) , group. unwrap_or ( 0 - 1 ) ) }
165
+ } ) ) ;
166
+
167
+ Errno :: result ( res) . map ( drop)
168
+ }
169
+
160
170
fn to_exec_array ( args : & [ CString ] ) -> Vec < * const c_char > {
161
171
use std:: ptr;
162
172
use libc:: c_char;
@@ -369,6 +379,20 @@ pub fn getegid() -> gid_t {
369
379
unsafe { ffi:: getegid ( ) }
370
380
}
371
381
382
+ #[ inline]
383
+ pub fn setuid ( uid : uid_t ) -> Result < ( ) > {
384
+ let res = unsafe { ffi:: setuid ( uid) } ;
385
+
386
+ Errno :: result ( res) . map ( drop)
387
+ }
388
+
389
+ #[ inline]
390
+ pub fn setgid ( gid : gid_t ) -> Result < ( ) > {
391
+ let res = unsafe { ffi:: setgid ( gid) } ;
392
+
393
+ Errno :: result ( res) . map ( drop)
394
+ }
395
+
372
396
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
373
397
mod linux {
374
398
use sys:: syscall:: { syscall, SYSPIVOTROOT } ;
0 commit comments