@@ -9,16 +9,16 @@ use std::ffi::CString;
9
9
use std:: fs:: File ;
10
10
use std:: io:: Write ;
11
11
use std:: os:: unix:: prelude:: * ;
12
- use std:: env:: current_dir;
13
12
use tempfile:: tempfile;
14
13
use tempdir:: TempDir ;
15
- use libc:: off_t;
14
+ use libc:: { _exit , off_t} ;
16
15
17
16
#[ test]
18
17
fn test_fork_and_waitpid ( ) {
18
+ let m = :: FORK_MTX . lock ( ) . expect ( "Mutex got poisoned by another test" ) ;
19
19
let pid = fork ( ) ;
20
20
match pid {
21
- Ok ( Child ) => { } // ignore child here
21
+ Ok ( Child ) => { unsafe { _exit ( 0 ) } ; }
22
22
Ok ( Parent { child } ) => {
23
23
// assert that child was created and pid > 0
24
24
let child_raw: :: libc:: pid_t = child. into ( ) ;
@@ -29,23 +29,26 @@ fn test_fork_and_waitpid() {
29
29
Ok ( WaitStatus :: Exited ( pid_t, _) ) => assert ! ( pid_t == child) ,
30
30
31
31
// panic, must never happen
32
- Ok ( _) => panic ! ( "Child still alive , should never happen" ) ,
32
+ s @ Ok ( _) => panic ! ( "Child exited {:?} , should never happen" , s ) ,
33
33
34
34
// panic, waitpid should never fail
35
- Err ( _ ) => panic ! ( "Error: waitpid Failed" )
35
+ Err ( s ) => panic ! ( "Error: waitpid returned Err({:?}" , s )
36
36
}
37
37
38
38
} ,
39
39
// panic, fork should never fail unless there is a serious problem with the OS
40
40
Err ( _) => panic ! ( "Error: Fork Failed" )
41
41
}
42
+ drop ( m) ; // appease the unused_variable checker
42
43
}
43
44
44
45
#[ test]
45
46
fn test_wait ( ) {
47
+ // grab FORK_MTX so wait doesn't reap a different test's child process
48
+ let m = :: FORK_MTX . lock ( ) . expect ( "Mutex got poisoned by another test" ) ;
46
49
let pid = fork ( ) ;
47
50
match pid {
48
- Ok ( Child ) => { } // ignore child here
51
+ Ok ( Child ) => { unsafe { _exit ( 0 ) } ; }
49
52
Ok ( Parent { child } ) => {
50
53
let wait_status = wait ( ) ;
51
54
@@ -55,6 +58,7 @@ fn test_wait() {
55
58
// panic, fork should never fail unless there is a serious problem with the OS
56
59
Err ( _) => panic ! ( "Error: Fork Failed" )
57
60
}
61
+ drop ( m) ; // appease the unused_variable checker
58
62
}
59
63
60
64
#[ test]
@@ -100,6 +104,7 @@ macro_rules! execve_test_factory(
100
104
( $test_name: ident, $syscall: ident, $unix_sh: expr, $android_sh: expr) => (
101
105
#[ test]
102
106
fn $test_name( ) {
107
+ let m = :: FORK_MTX . lock( ) . expect( "Mutex got poisoned by another test" ) ;
103
108
// The `exec`d process will write to `writer`, and we'll read that
104
109
// data from `reader`.
105
110
let ( reader, writer) = pipe( ) . unwrap( ) ;
@@ -139,46 +144,47 @@ macro_rules! execve_test_factory(
139
144
assert!( string. contains( "baz=quux" ) ) ;
140
145
}
141
146
}
147
+ drop( m) ; // appease the unused_variable checker
142
148
}
143
149
)
144
150
) ;
145
151
146
152
#[ test]
147
153
fn test_fchdir ( ) {
154
+ // fchdir changes the process's cwd
155
+ let m = :: CWD_MTX . lock ( ) . expect ( "Mutex got poisoned by another test" ) ;
148
156
let tmpdir = TempDir :: new ( "test_fchdir" ) . unwrap ( ) ;
149
157
let tmpdir_path = tmpdir. path ( ) . canonicalize ( ) . unwrap ( ) ;
150
158
let tmpdir_fd = File :: open ( & tmpdir_path) . unwrap ( ) . into_raw_fd ( ) ;
151
- let olddir_path = getcwd ( ) . unwrap ( ) ;
152
- let olddir_fd = File :: open ( & olddir_path) . unwrap ( ) . into_raw_fd ( ) ;
153
159
154
160
assert ! ( fchdir( tmpdir_fd) . is_ok( ) ) ;
155
161
assert_eq ! ( getcwd( ) . unwrap( ) , tmpdir_path) ;
156
162
157
- assert ! ( fchdir( olddir_fd) . is_ok( ) ) ;
158
- assert_eq ! ( getcwd( ) . unwrap( ) , olddir_path) ;
159
-
160
- assert ! ( close( olddir_fd) . is_ok( ) ) ;
161
163
assert ! ( close( tmpdir_fd) . is_ok( ) ) ;
164
+ drop ( m) ; // appease the unused_variable checker
162
165
}
163
166
164
167
#[ test]
165
168
fn test_getcwd ( ) {
169
+ // chdir changes the process's cwd
170
+ let m = :: CWD_MTX . lock ( ) . expect ( "Mutex got poisoned by another test" ) ;
166
171
let tmp_dir = TempDir :: new ( "test_getcwd" ) . unwrap ( ) ;
167
172
assert ! ( chdir( tmp_dir. path( ) ) . is_ok( ) ) ;
168
- assert_eq ! ( getcwd( ) . unwrap( ) , current_dir ( ) . unwrap ( ) ) ;
173
+ assert_eq ! ( getcwd( ) . unwrap( ) , tmp_dir . path ( ) ) ;
169
174
170
- // make path 500 chars longer so that buffer doubling in getcwd kicks in.
171
- // Note: One path cannot be longer than 255 bytes (NAME_MAX)
172
- // whole path cannot be longer than PATH_MAX (usually 4096 on linux, 1024 on macos)
175
+ // make path 500 chars longer so that buffer doubling in getcwd
176
+ // kicks in. Note: One path cannot be longer than 255 bytes
177
+ // (NAME_MAX) whole path cannot be longer than PATH_MAX (usually
178
+ // 4096 on linux, 1024 on macos)
173
179
let mut inner_tmp_dir = tmp_dir. path ( ) . to_path_buf ( ) ;
174
180
for _ in 0 ..5 {
175
181
let newdir = iter:: repeat ( "a" ) . take ( 100 ) . collect :: < String > ( ) ;
176
- //inner_tmp_dir = inner_tmp_dir.join(newdir).path();
177
182
inner_tmp_dir. push ( newdir) ;
178
183
assert ! ( mkdir( inner_tmp_dir. as_path( ) , stat:: S_IRWXU ) . is_ok( ) ) ;
179
184
}
180
185
assert ! ( chdir( inner_tmp_dir. as_path( ) ) . is_ok( ) ) ;
181
- assert_eq ! ( getcwd( ) . unwrap( ) , current_dir( ) . unwrap( ) ) ;
186
+ assert_eq ! ( getcwd( ) . unwrap( ) , inner_tmp_dir. as_path( ) ) ;
187
+ drop ( m) ; // appease the unused_variable checker
182
188
}
183
189
184
190
#[ test]
0 commit comments