@@ -14,7 +14,7 @@ use mem;
14
14
use ptr;
15
15
16
16
use sys:: mx_cvt;
17
- use sys:: magenta:: { launchpad_t, mx_handle_t} ;
17
+ use sys:: magenta:: { Handle , launchpad_t, mx_handle_t} ;
18
18
use sys:: process:: process_common:: * ;
19
19
20
20
////////////////////////////////////////////////////////////////////////////////
@@ -33,7 +33,7 @@ impl Command {
33
33
34
34
let ( launchpad, process_handle) = unsafe { self . do_exec ( theirs) ? } ;
35
35
36
- Ok ( ( Process { launchpad : launchpad, handle : process_handle , status : None } , ours) )
36
+ Ok ( ( Process { launchpad : launchpad, handle : Handle :: new ( process_handle ) } , ours) )
37
37
}
38
38
39
39
pub fn exec ( & mut self , default : Stdio ) -> io:: Error {
@@ -116,7 +116,7 @@ impl Command {
116
116
117
117
let process_handle = mx_cvt ( launchpad_start ( launchpad) ) ?;
118
118
119
- // Successfully started the launchpad, so launchpad_destroy shouldn't get called
119
+ // Successfully started the launchpad
120
120
mem:: forget ( launchpad_destructor) ;
121
121
122
122
Ok ( ( launchpad, process_handle) )
@@ -129,62 +129,49 @@ impl Command {
129
129
130
130
pub struct Process {
131
131
launchpad : * mut launchpad_t ,
132
- handle : mx_handle_t ,
133
- status : Option < ExitStatus > ,
132
+ handle : Handle ,
134
133
}
135
134
136
135
impl Process {
137
136
pub fn id ( & self ) -> u32 {
138
- self . handle as u32
137
+ self . handle . raw ( ) as u32
139
138
}
140
139
141
140
pub fn kill ( & mut self ) -> io:: Result < ( ) > {
142
141
use sys:: magenta:: * ;
143
142
144
- // If we've already waited on this process then the pid can be recycled
145
- // and used for another process, and we probably shouldn't be killing
146
- // random processes, so just return an error.
147
- if self . status . is_some ( ) {
148
- Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
149
- "invalid argument: can't kill an exited process" ) )
150
- } else {
151
- unsafe {
152
- mx_cvt ( mx_handle_close ( self . handle ) ) ?;
153
- launchpad_destroy ( self . launchpad ) ;
154
- }
155
- Ok ( ( ) )
156
- }
143
+ unsafe { mx_cvt ( mx_task_kill ( self . handle . raw ( ) ) ) ?; }
144
+
145
+ Ok ( ( ) )
157
146
}
158
147
159
148
pub fn wait ( & mut self ) -> io:: Result < ExitStatus > {
160
149
use default:: Default ;
161
150
use sys:: magenta:: * ;
162
151
163
- if let Some ( status) = self . status {
164
- return Ok ( status)
165
- }
166
-
167
152
let mut proc_info: mx_info_process_t = Default :: default ( ) ;
168
153
let mut actual: mx_size_t = 0 ;
169
154
let mut avail: mx_size_t = 0 ;
170
155
171
156
unsafe {
172
- mx_cvt ( mx_handle_wait_one ( self . handle , MX_TASK_TERMINATED ,
157
+ mx_cvt ( mx_handle_wait_one ( self . handle . raw ( ) , MX_TASK_TERMINATED ,
173
158
MX_TIME_INFINITE , ptr:: null_mut ( ) ) ) ?;
174
- mx_cvt ( mx_object_get_info ( self . handle , MX_INFO_PROCESS ,
159
+ mx_cvt ( mx_object_get_info ( self . handle . raw ( ) , MX_INFO_PROCESS ,
175
160
& mut proc_info as * mut _ as * mut libc:: c_void ,
176
161
mem:: size_of :: < mx_info_process_t > ( ) , & mut actual,
177
162
& mut avail) ) ?;
178
163
}
179
164
if actual != 1 {
180
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
181
- "Failed to get exit status of process" ) ) ;
182
- }
183
- self . status = Some ( ExitStatus :: new ( proc_info. rec . return_code ) ) ;
184
- unsafe {
185
- mx_cvt ( mx_handle_close ( self . handle ) ) ?;
186
- launchpad_destroy ( self . launchpad ) ;
165
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData ,
166
+ "Failed to get exit status of process" ) ) ;
187
167
}
188
168
Ok ( ExitStatus :: new ( proc_info. rec . return_code ) )
189
169
}
190
170
}
171
+
172
+ impl Drop for Process {
173
+ fn drop ( & mut self ) {
174
+ use sys:: magenta:: launchpad_destroy;
175
+ unsafe { launchpad_destroy ( self . launchpad ) ; }
176
+ }
177
+ }
0 commit comments