-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprocesses.rs
450 lines (372 loc) · 11.2 KB
/
processes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use super::constants::exports::*;
use super::errors::exports::*;
use super::globals::exports::*;
use super::ports::exports::*;
use super::runtime::exports::*;
use super::values::exports::*;
use super::prelude::*;
pub mod exports {
pub use super::{
Process, ProcessInternals,
ProcessState,
ProcessStatus,
ProcessSignal,
};
pub use super::{
ProcessConfiguration, ProcessConfigurationStream,
};
}
#[ derive ( Clone ) ] // OK
pub struct Process ( StdRc<StdRefCell<ProcessInternals>> );
#[ cfg_attr ( feature = "vonuvoli_fmt_debug", derive ( Debug ) ) ] // OK ~~
pub struct ProcessInternals {
pub state : ProcessState,
pub process : process::Child,
pub process_id : ext::libc::pid_t,
pub stdin : Option<Port>,
pub stdout : Option<Port>,
pub stderr : Option<Port>,
pub handle : Handle,
}
#[ cfg_attr ( feature = "vonuvoli_fmt_debug", derive ( Debug ) ) ] // OK ~~
pub enum ProcessState {
Running,
Terminated (process::ExitStatus),
Failed (Error),
}
#[ derive ( Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash ) ] // OK
#[ cfg_attr ( feature = "vonuvoli_fmt_debug", derive ( Debug ) ) ] // OK
pub enum ProcessStatus {
Running,
Succeeded,
Failed (u32),
Killed (u32),
}
#[ derive ( Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash ) ] // OK
#[ cfg_attr ( feature = "vonuvoli_fmt_debug", derive ( Debug ) ) ] // OK
pub enum ProcessSignal {
Terminate,
Interrupt,
Kill,
Stop,
Continue,
Opaque (u32),
}
#[ derive ( Default ) ] // OK
#[ cfg_attr ( feature = "vonuvoli_fmt_debug", derive ( Debug ) ) ] // OK
pub struct ProcessConfiguration {
pub executable : ffi::OsString,
pub argument0 : Option<ffi::OsString>,
pub arguments : Option<StdBox<[ffi::OsString]>>,
pub environment_empty : bool,
pub environment_include : Option<StdBox<[(ffi::OsString, ffi::OsString)]>>,
pub environment_exclude : Option<StdBox<[ffi::OsString]>>,
pub working_directory : Option<ffi::OsString>,
pub stdin : Option<ProcessConfigurationStream>,
pub stdout : Option<ProcessConfigurationStream>,
pub stderr : Option<ProcessConfigurationStream>,
}
#[ cfg_attr ( feature = "vonuvoli_fmt_debug", derive ( Debug ) ) ] // OK
pub enum ProcessConfigurationStream {
Inherited,
Piped,
Null,
Port (Port),
PortDescriptor (PortDescriptor),
File (fs::File),
}
impl Process {
pub fn spawn (configuration : &ProcessConfiguration) -> (Outcome<Process>) {
let configuration = r#try! (configuration.build ());
return Process::spawn_command (configuration);
}
pub fn spawn_command (configuration : process::Command) -> (Outcome<Process>) {
let mut configuration = configuration;
let child = try_or_fail! (configuration.spawn (), 0x4b026d76);
return Process::new (child);
}
pub fn exec (configuration : &ProcessConfiguration) -> (Outcome<!>) {
let configuration = r#try! (configuration.build ());
return Process::exec_command (configuration);
}
pub fn exec_command (configuration : process::Command) -> (Outcome<!>) {
let mut configuration = configuration;
unix_process::CommandExt::exec (&mut configuration);
fail! (0xd59df47c);
}
pub fn new (process : process::Child) -> (Outcome<Process>) {
let mut process = process;
let process_id = process.id () as ext::libc::pid_t;
TODO! ("add support for specifying the stdin/stdout/stderr buffer size");
let mut stdin = None;
let mut stdout = None;
let mut stderr = None;
mem::swap (&mut stdin, &mut process.stdin);
mem::swap (&mut stdout, &mut process.stdout);
mem::swap (&mut stderr, &mut process.stderr);
let stdin = option_map! (stdin, {
let descriptor = PortDescriptor::for_child_stdin (&stdin);
r#try! (Port::new_native_writer_from_unbuffered (StdBox::new (stdin), None, descriptor))
});
let stdout = option_map! (stdout, {
let descriptor = PortDescriptor::for_child_stdout (&stdout);
r#try! (Port::new_native_reader_from_unbuffered (StdBox::new (stdout), None, descriptor))
});
let stderr = option_map! (stderr, {
let descriptor = PortDescriptor::for_child_stderr (&stderr);
r#try! (Port::new_native_reader_from_unbuffered (StdBox::new (stderr), None, descriptor))
});
let internals = ProcessInternals {
state : ProcessState::Running,
process : process,
process_id : process_id,
stdin : stdin,
stdout : stdout,
stderr : stderr,
handle : process_handles_next (),
};
let process = Process (StdRc::new (StdRefCell::new (internals)));
succeed! (process);
}
pub fn id (&self) -> (Outcome<u32>) {
let self_0 = r#try! (self.internals_ref ());
succeed! (self_0.process_id as u32);
}
pub fn status (&self) -> (Outcome<ProcessStatus>) {
let self_0 = r#try! (self.internals_ref ());
return self_0.state.status ();
}
pub fn wait (&self, block : bool) -> (Outcome<ProcessStatus>) {
{
let self_0 = r#try! (self.internals_ref ());
match self_0.state {
ProcessState::Running =>
if ! block {
return self_0.state.status ();
},
ProcessState::Terminated (_) =>
return self_0.state.status (),
ProcessState::Failed (_) =>
return self_0.state.status (),
}
}
{
let mut self_0 = r#try! (self.internals_ref_mut ());
let exit = if block {
match self_0.process.wait () {
Ok (exit) =>
Ok (Some (exit)),
Err (_) =>
failed! (0x72e7f374),
}
} else {
match self_0.process.try_wait () {
Ok (exit) =>
Ok (exit),
Err (_) =>
failed! (0x1721311f),
}
};
match exit {
Ok (None) =>
(),
Ok (Some (exit)) =>
self_0.state = ProcessState::Terminated (exit),
Err (error) =>
self_0.state = ProcessState::Failed (error),
}
return self_0.state.status ();
}
}
pub fn signal (&self, signal : ProcessSignal) -> (Outcome<()>) {
{
let self_0 = r#try! (self.internals_ref ());
match self_0.state {
ProcessState::Running =>
(),
_ =>
fail! (0x248e1acf),
}
r#try! (libc_kill (self_0.process_id, signal.code ()));
}
r#try! (self.wait (false));
succeed! (());
}
pub fn stdin (&self) -> (Outcome<Option<Port>>) {
let self_0 = r#try! (self.internals_ref ());
succeed! (self_0.stdin.clone ());
}
pub fn stdout (&self) -> (Outcome<Option<Port>>) {
let self_0 = r#try! (self.internals_ref ());
succeed! (self_0.stdout.clone ());
}
pub fn stderr (&self) -> (Outcome<Option<Port>>) {
let self_0 = r#try! (self.internals_ref ());
succeed! (self_0.stderr.clone ());
}
pub fn internals_ref (&self) -> (Outcome<StdRef<ProcessInternals>>) {
succeed! (try_or_fail! (StdRefCell::try_borrow (StdRc::as_ref (&self.0)), 0xb01737f5));
}
pub fn internals_ref_mut (&self) -> (Outcome<StdRefMut<ProcessInternals>>) {
succeed! (try_or_fail! (StdRefCell::try_borrow_mut (StdRc::as_ref (&self.0)), 0x463b17c3));
}
pub fn handle (&self) -> (Outcome<Handle>) {
let self_0 = r#try! (self.internals_ref ());
succeed! (self_0.handle);
}
pub fn is_self (&self, other : &Process) -> (bool) {
StdRc::ptr_eq (&self.0, &other.0)
}
}
impl ProcessState {
pub fn status (&self) -> (Outcome<ProcessStatus>) {
match *self {
ProcessState::Running =>
succeed! (ProcessStatus::Running),
ProcessState::Terminated (ref exit) => {
if let Some (code) = exit.code () {
if code == 0 {
succeed! (ProcessStatus::Succeeded);
} else {
succeed! (ProcessStatus::Failed (code as u32));
}
} else if let Some (code) = unix_process::ExitStatusExt::signal (exit) {
succeed! (ProcessStatus::Killed (code as u32));
} else {
fail_panic! (0x4075cf67, github_issue_new);
}
},
ProcessState::Failed (ref error) =>
return Err (error.clone ()),
}
}
}
impl ProcessStatus {
pub fn code (self) -> (Option<i32>) {
match self {
ProcessStatus::Running =>
None,
ProcessStatus::Succeeded =>
Some (0),
ProcessStatus::Failed (code) =>
Some (code as i32),
ProcessStatus::Killed (code) =>
Some ((0 - code) as i32),
}
}
pub fn value (self) -> (Value) {
match self {
ProcessStatus::Running =>
FALSE_VALUE,
ProcessStatus::Succeeded =>
TRUE_VALUE,
ProcessStatus::Failed (code) =>
(code as i32) .into (),
ProcessStatus::Killed (code) =>
((0 - code) as i32) .into (),
}
}
}
impl ProcessSignal {
pub fn code (self) -> (i32) {
match self {
ProcessSignal::Interrupt =>
ext::libc::SIGINT,
ProcessSignal::Terminate =>
ext::libc::SIGTERM,
ProcessSignal::Kill =>
ext::libc::SIGKILL,
ProcessSignal::Stop =>
ext::libc::SIGSTOP,
ProcessSignal::Continue =>
ext::libc::SIGCONT,
ProcessSignal::Opaque (value) =>
value as i32,
}
}
}
impl ProcessConfiguration {
pub fn build (&self) -> (Outcome<process::Command>) {
let mut command = process::Command::new (&self.executable);
if let Some (ref _argument0) = self.argument0 {
fail! (0x6c2f442c);
}
if let Some (ref arguments) = self.arguments {
for argument in arguments.iter () {
command.arg (argument);
}
}
if self.environment_empty {
command.env_clear ();
}
if let Some (ref excludes) = self.environment_exclude {
for variable in excludes.iter () {
command.env_remove (variable);
}
}
if let Some (ref includes) = self.environment_include {
for &(ref variable, ref value) in includes.iter () {
command.env (variable, value);
}
}
if let Some (ref path) = self.working_directory {
command.current_dir (path);
}
if let Some (ref stdin) = self.stdin {
let stdin = r#try! (stdin.build ());
command.stdin (stdin);
}
if let Some (ref stdout) = self.stdout {
let stdout = r#try! (stdout.build ());
command.stdout (stdout);
}
if let Some (ref stderr) = self.stderr {
let stderr = r#try! (stderr.build ());
command.stderr (stderr);
}
succeed! (command);
}
}
impl ProcessConfigurationStream {
pub fn build (&self) -> (Outcome<process::Stdio>) {
match *self {
ProcessConfigurationStream::Inherited =>
succeed! (process::Stdio::inherit ()),
ProcessConfigurationStream::Piped =>
succeed! (process::Stdio::piped ()),
ProcessConfigurationStream::Null =>
succeed! (process::Stdio::null ()),
ProcessConfigurationStream::PortDescriptor (ref descriptor) =>
match *descriptor {
PortDescriptor::RawFd (descriptor) =>
unsafe {
succeed! (unix_io::FromRawFd::from_raw_fd (descriptor));
},
PortDescriptor::Stdin =>
unsafe {
succeed! (unix_io::FromRawFd::from_raw_fd (unix_io::AsRawFd::as_raw_fd (&io::stdin ())));
},
PortDescriptor::Stdout =>
unsafe {
succeed! (unix_io::FromRawFd::from_raw_fd (unix_io::AsRawFd::as_raw_fd (&io::stdout ())));
},
PortDescriptor::Stderr =>
unsafe {
succeed! (unix_io::FromRawFd::from_raw_fd (unix_io::AsRawFd::as_raw_fd (&io::stderr ())));
},
},
ProcessConfigurationStream::Port (ref port) => {
let descriptor = r#try! (port.descriptor ());
let descriptor = try_some! (descriptor, 0x2426d518);
let configuration = ProcessConfigurationStream::PortDescriptor (descriptor);
return configuration.build ();
},
ProcessConfigurationStream::File (ref file) => {
let descriptor = PortDescriptor::for_file (file);
let descriptor = try_some! (descriptor, 0x44aa4b5b);
let configuration = ProcessConfigurationStream::PortDescriptor (descriptor);
return configuration.build ();
},
}
}
}