@@ -2,13 +2,20 @@ use std::ptr;
2
2
use std:: c_str:: { CString , ToCStr } ;
3
3
use std:: path:: Path ;
4
4
use libc:: { c_char} ;
5
+ use fcntl:: { Fd , OFlag } ;
5
6
use syscall:: { syscall, SysPivotRoot } ;
6
7
use { SysResult , SysError } ;
7
8
8
9
mod ffi {
9
10
use libc:: { c_char, c_int} ;
10
11
11
12
extern {
13
+ pub fn dup ( oldfd : c_int ) -> c_int ;
14
+
15
+ pub fn dup2 ( oldfd : c_int , newfd : c_int ) -> c_int ;
16
+
17
+ pub fn dup3 ( oldfd : c_int , newfd : c_int , flags : c_int ) -> c_int ;
18
+
12
19
// change working directory
13
20
// doc: http://man7.org/linux/man-pages/man2/chdir.2.html
14
21
pub fn chdir ( path : * const c_char ) -> c_int ;
@@ -19,6 +26,40 @@ mod ffi {
19
26
}
20
27
}
21
28
29
+ #[ inline]
30
+ pub fn dup ( oldfd : Fd ) -> SysResult < Fd > {
31
+ let res = unsafe { ffi:: dup ( oldfd) } ;
32
+
33
+ if res < 0 {
34
+ return Err ( SysError :: last ( ) ) ;
35
+ }
36
+
37
+ Ok ( res)
38
+ }
39
+
40
+ #[ inline]
41
+ pub fn dup2 ( oldfd : Fd , newfd : Fd ) -> SysResult < Fd > {
42
+ let res = unsafe { ffi:: dup2 ( oldfd, newfd) } ;
43
+
44
+ if res < 0 {
45
+ return Err ( SysError :: last ( ) ) ;
46
+ }
47
+
48
+ Ok ( res)
49
+ }
50
+
51
+ #[ inline]
52
+ pub fn dup3 ( oldfd : Fd , newfd : Fd , flags : OFlag ) -> SysResult < Fd > {
53
+ let res = unsafe { ffi:: dup3 ( oldfd, newfd, flags. bits ( ) ) } ;
54
+
55
+ if res < 0 {
56
+ return Err ( SysError :: last ( ) ) ;
57
+ }
58
+
59
+ Ok ( res)
60
+ }
61
+
62
+ #[ inline]
22
63
pub fn chdir < S : ToCStr > ( path : S ) -> SysResult < ( ) > {
23
64
let path = path. to_c_str ( ) ;
24
65
let res = unsafe { ffi:: chdir ( path. as_ptr ( ) ) } ;
@@ -30,6 +71,7 @@ pub fn chdir<S: ToCStr>(path: S) -> SysResult<()> {
30
71
return Ok ( ( ) )
31
72
}
32
73
74
+ #[ inline]
33
75
pub fn execve ( filename : CString , args : & [ CString ] , env : & [ CString ] ) -> SysResult < ( ) > {
34
76
let mut args_p: Vec < * const c_char > = args. iter ( ) . map ( |s| s. as_ptr ( ) ) . collect ( ) ;
35
77
args_p. push ( ptr:: null ( ) ) ;
0 commit comments