1
1
use std:: collections:: HashMap ;
2
+ #[ cfg( unix) ]
3
+ use std:: ffi:: c_int;
2
4
use std:: ffi:: OsStr ;
3
5
use std:: fmt:: Debug ;
4
6
use std:: fs;
7
+ #[ cfg( unix) ]
8
+ use std:: os:: unix:: process:: ExitStatusExt ;
5
9
use std:: path:: { Path , PathBuf } ;
6
10
use std:: process:: { Command , ExitStatus , Output } ;
7
11
12
+ #[ cfg( unix) ]
13
+ extern "C" {
14
+ fn raise ( signal : c_int ) -> c_int ;
15
+ }
16
+
17
+ fn exec_command (
18
+ input : & [ & dyn AsRef < OsStr > ] ,
19
+ cwd : Option < & Path > ,
20
+ env : Option < & HashMap < String , String > > ,
21
+ ) -> Result < ExitStatus , String > {
22
+ let status = get_command_inner ( input, cwd, env)
23
+ . spawn ( )
24
+ . map_err ( |e| command_error ( input, & cwd, e) ) ?
25
+ . wait ( )
26
+ . map_err ( |e| command_error ( input, & cwd, e) ) ?;
27
+ #[ cfg( unix) ]
28
+ {
29
+ if let Some ( signal) = status. signal ( ) {
30
+ unsafe {
31
+ raise ( signal as _ ) ;
32
+ }
33
+ // In case the signal didn't kill the current process.
34
+ return Err ( command_error ( input, & cwd, format ! ( "Process received signal {}" , signal) ) ) ;
35
+ }
36
+ }
37
+ Ok ( status)
38
+ }
39
+
8
40
fn get_command_inner (
9
41
input : & [ & dyn AsRef < OsStr > ] ,
10
42
cwd : Option < & Path > ,
@@ -89,11 +121,7 @@ pub fn run_command_with_output(
89
121
input : & [ & dyn AsRef < OsStr > ] ,
90
122
cwd : Option < & Path > ,
91
123
) -> Result < ( ) , String > {
92
- let exit_status = get_command_inner ( input, cwd, None )
93
- . spawn ( )
94
- . map_err ( |e| command_error ( input, & cwd, e) ) ?
95
- . wait ( )
96
- . map_err ( |e| command_error ( input, & cwd, e) ) ?;
124
+ let exit_status = exec_command ( input, cwd, None ) ?;
97
125
check_exit_status ( input, cwd, exit_status, None , true ) ?;
98
126
Ok ( ( ) )
99
127
}
@@ -103,11 +131,7 @@ pub fn run_command_with_output_and_env(
103
131
cwd : Option < & Path > ,
104
132
env : Option < & HashMap < String , String > > ,
105
133
) -> Result < ( ) , String > {
106
- let exit_status = get_command_inner ( input, cwd, env)
107
- . spawn ( )
108
- . map_err ( |e| command_error ( input, & cwd, e) ) ?
109
- . wait ( )
110
- . map_err ( |e| command_error ( input, & cwd, e) ) ?;
134
+ let exit_status = exec_command ( input, cwd, env) ?;
111
135
check_exit_status ( input, cwd, exit_status, None , true ) ?;
112
136
Ok ( ( ) )
113
137
}
@@ -117,11 +141,7 @@ pub fn run_command_with_output_and_env_no_err(
117
141
cwd : Option < & Path > ,
118
142
env : Option < & HashMap < String , String > > ,
119
143
) -> Result < ( ) , String > {
120
- let exit_status = get_command_inner ( input, cwd, env)
121
- . spawn ( )
122
- . map_err ( |e| command_error ( input, & cwd, e) ) ?
123
- . wait ( )
124
- . map_err ( |e| command_error ( input, & cwd, e) ) ?;
144
+ let exit_status = exec_command ( input, cwd, env) ?;
125
145
check_exit_status ( input, cwd, exit_status, None , false ) ?;
126
146
Ok ( ( ) )
127
147
}
0 commit comments