-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwindows.rs
105 lines (88 loc) · 2.16 KB
/
windows.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
use std::{
io::{Read, Result},
mem,
process::{Child, ChildStderr, ChildStdin, ChildStdout, ExitStatus},
};
use winapi::{
shared::{basetsd::ULONG_PTR, minwindef::DWORD},
um::{
handleapi::CloseHandle, ioapiset::GetQueuedCompletionStatus, jobapi2::TerminateJobObject,
minwinbase::LPOVERLAPPED, winbase::INFINITE, winnt::HANDLE,
},
};
use crate::winres::*;
pub(super) struct ChildImp {
inner: Child,
handles: JobPort,
}
impl ChildImp {
pub fn new(inner: Child, job: HANDLE, completion_port: HANDLE) -> Self {
Self {
inner,
handles: JobPort {
job,
completion_port,
},
}
}
pub(super) fn take_stdin(&mut self) -> Option<ChildStdin> {
self.inner.stdin.take()
}
pub(super) fn take_stdout(&mut self) -> Option<ChildStdout> {
self.inner.stdout.take()
}
pub(super) fn take_stderr(&mut self) -> Option<ChildStderr> {
self.inner.stderr.take()
}
pub fn inner(&mut self) -> &mut Child {
&mut self.inner
}
pub fn into_inner(self) -> Child {
// manually drop the completion port
let its = mem::ManuallyDrop::new(self.handles);
unsafe { CloseHandle(its.completion_port) };
// we leave the job handle unclosed, otherwise the Child is useless
// (as closing it will terminate the job)
// extract the Child
self.inner
}
pub fn kill(&mut self) -> Result<()> {
res_bool(unsafe { TerminateJobObject(self.handles.job, 1) })
}
pub fn id(&self) -> u32 {
self.inner.id()
}
fn wait_imp(&self, timeout: DWORD) -> Result<()> {
let mut code: DWORD = 0;
let mut key: ULONG_PTR = 0;
let mut overlapped = mem::MaybeUninit::<LPOVERLAPPED>::uninit();
res_bool(unsafe {
GetQueuedCompletionStatus(
self.handles.completion_port,
&mut code,
&mut key,
overlapped.as_mut_ptr(),
timeout,
)
})?;
Ok(())
}
pub fn wait(&mut self) -> Result<ExitStatus> {
self.wait_imp(INFINITE)?;
self.inner.wait()
}
pub fn try_wait(&mut self) -> Result<Option<ExitStatus>> {
self.wait_imp(0)?;
self.inner.try_wait()
}
pub(super) fn read_both(
mut out_r: ChildStdout,
out_v: &mut Vec<u8>,
mut err_r: ChildStderr,
err_v: &mut Vec<u8>,
) -> Result<()> {
out_r.read_to_end(out_v)?;
err_r.read_to_end(err_v)?;
Ok(())
}
}