1 use os::windows::prelude::*;
9 use sync::atomic::Ordering::SeqCst;
10 use sync::atomic::AtomicUsize;
12 use sys::fs::{File, OpenOptions};
13 use sys::handle::Handle;
14 use sys::hashmap_random_keys;
16 ////////////////////////////////////////////////////////////////////////////////
18 ////////////////////////////////////////////////////////////////////////////////
29 /// Although this looks similar to `anon_pipe` in the Unix module it's actually
30 /// subtly different. Here we'll return two pipes in the `Pipes` return value,
31 /// but one is intended for "us" where as the other is intended for "someone
34 /// Currently the only use case for this function is pipes for stdio on
35 /// processes in the standard library, so "ours" is the one that'll stay in our
36 /// process whereas "theirs" will be inherited to a child.
38 /// The ours/theirs pipes are *not* specifically readable or writable. Each
39 /// one only supports a read or a write, but which is which depends on the
40 /// boolean flag given. If `ours_readable` is true then `ours` is readable where
41 /// `theirs` is writable. Conversely if `ours_readable` is false then `ours` is
42 /// writable where `theirs` is readable.
44 /// Also note that the `ours` pipe is always a handle opened up in overlapped
45 /// mode. This means that technically speaking it should only ever be used
46 /// with `OVERLAPPED` instances, but also works out ok if it's only ever used
47 /// once at a time (which we do indeed guarantee).
48 pub fn anon_pipe(ours_readable: bool) -> io::Result<Pipes> {
49 // Note that we specifically do *not* use `CreatePipe` here because
50 // unfortunately the anonymous pipes returned do not support overlapped
51 // operations. Instead, we create a "hopefully unique" name and create a
52 // named pipe which has overlapped operations enabled.
54 // Once we do this, we connect do it as usual via `CreateFileW`, and then
55 // we return those reader/writer halves. Note that the `ours` pipe return
56 // value is always the named pipe, whereas `theirs` is just the normal file.
57 // This should hopefully shield us from child processes which assume their
58 // stdout is a named pipe, which would indeed be odd!
63 let mut reject_remote_clients_flag = c::PIPE_REJECT_REMOTE_CLIENTS;
66 name = format!(r"\\.\pipe\__rust_anonymous_pipe1__.{}.{}",
67 c::GetCurrentProcessId(),
69 let wide_name = OsStr::new(&name)
73 let mut flags = c::FILE_FLAG_FIRST_PIPE_INSTANCE |
74 c::FILE_FLAG_OVERLAPPED;
76 flags |= c::PIPE_ACCESS_INBOUND;
78 flags |= c::PIPE_ACCESS_OUTBOUND;
81 let handle = c::CreateNamedPipeW(wide_name.as_ptr(),
84 c::PIPE_READMODE_BYTE |
86 reject_remote_clients_flag,
93 // We pass the `FILE_FLAG_FIRST_PIPE_INSTANCE` flag above, and we're
94 // also just doing a best effort at selecting a unique name. If
95 // `ERROR_ACCESS_DENIED` is returned then it could mean that we
96 // accidentally conflicted with an already existing pipe, so we try
99 // Don't try again too much though as this could also perhaps be a
101 // If `ERROR_INVALID_PARAMETER` is returned, this probably means we're
102 // running on pre-Vista version where `PIPE_REJECT_REMOTE_CLIENTS` is
103 // not supported, so we continue retrying without it. This implies
104 // reduced security on Windows versions older than Vista by allowing
105 // connections to this pipe from remote machines.
106 // Proper fix would increase the number of FFI imports and introduce
107 // significant amount of Windows XP specific code with no clean
109 // For more info, see https://github.com/rust-lang/rust/pull/37677.
110 if handle == c::INVALID_HANDLE_VALUE {
111 let err = io::Error::last_os_error();
112 let raw_os_err = err.raw_os_error();
114 if raw_os_err == Some(c::ERROR_ACCESS_DENIED as i32) {
116 } else if reject_remote_clients_flag != 0 &&
117 raw_os_err == Some(c::ERROR_INVALID_PARAMETER as i32) {
118 reject_remote_clients_flag = 0;
125 ours = Handle::new(handle);
129 // Connect to the named pipe we just created. This handle is going to be
130 // returned in `theirs`, so if `ours` is readable we want this to be
131 // writable, otherwise if `ours` is writable we want this to be
134 // Additionally we don't enable overlapped mode on this because most
135 // client processes aren't enabled to work with that.
136 let mut opts = OpenOptions::new();
137 opts.write(ours_readable);
138 opts.read(!ours_readable);
140 let theirs = File::open(Path::new(&name), &opts)?;
141 let theirs = AnonPipe { inner: theirs.into_handle() };
144 ours: AnonPipe { inner: ours },
145 theirs: AnonPipe { inner: theirs.into_handle() },
150 fn random_number() -> usize {
151 static N: AtomicUsize = AtomicUsize::new(0);
153 if N.load(SeqCst) != 0 {
154 return N.fetch_add(1, SeqCst)
157 N.store(hashmap_random_keys().0 as usize, SeqCst);
162 pub fn handle(&self) -> &Handle { &self.inner }
163 pub fn into_handle(self) -> Handle { self.inner }
165 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
169 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
170 self.inner.write(buf)
174 pub fn read2(p1: AnonPipe,
177 v2: &mut Vec<u8>) -> io::Result<()> {
178 let p1 = p1.into_handle();
179 let p2 = p2.into_handle();
181 let mut p1 = AsyncPipe::new(p1, v1)?;
182 let mut p2 = AsyncPipe::new(p2, v2)?;
183 let objs = [p1.event.raw(), p2.event.raw()];
185 // In a loop we wait for either pipe's scheduled read operation to complete.
186 // If the operation completes with 0 bytes, that means EOF was reached, in
187 // which case we just finish out the other pipe entirely.
189 // Note that overlapped I/O is in general super unsafe because we have to
190 // be careful to ensure that all pointers in play are valid for the entire
191 // duration of the I/O operation (where tons of operations can also fail).
192 // The destructor for `AsyncPipe` ends up taking care of most of this.
195 c::WaitForMultipleObjects(2, objs.as_ptr(), c::FALSE, c::INFINITE)
197 if res == c::WAIT_OBJECT_0 {
198 if !p1.result()? || !p1.schedule_read()? {
201 } else if res == c::WAIT_OBJECT_0 + 1 {
202 if !p2.result()? || !p2.schedule_read()? {
206 return Err(io::Error::last_os_error())
211 struct AsyncPipe<'a> {
214 overlapped: Box<c::OVERLAPPED>, // needs a stable address
215 dst: &'a mut Vec<u8>,
219 #[derive(PartialEq, Debug)]
226 impl<'a> AsyncPipe<'a> {
227 fn new(pipe: Handle, dst: &'a mut Vec<u8>) -> io::Result<AsyncPipe<'a>> {
228 // Create an event which we'll use to coordinate our overlapped
229 // operations, this event will be used in WaitForMultipleObjects
230 // and passed as part of the OVERLAPPED handle.
232 // Note that we do a somewhat clever thing here by flagging the
233 // event as being manually reset and setting it initially to the
234 // signaled state. This means that we'll naturally fall through the
235 // WaitForMultipleObjects call above for pipes created initially,
236 // and the only time an even will go back to "unset" will be once an
237 // I/O operation is successfully scheduled (what we want).
238 let event = Handle::new_event(true, true)?;
239 let mut overlapped: Box<c::OVERLAPPED> = unsafe {
240 Box::new(mem::zeroed())
242 overlapped.hEvent = event.raw();
248 state: State::NotReading,
252 /// Executes an overlapped read operation.
254 /// Must not currently be reading, and returns whether the pipe is currently
255 /// at EOF or not. If the pipe is not at EOF then `result()` must be called
256 /// to complete the read later on (may block), but if the pipe is at EOF
257 /// then `result()` should not be called as it will just block forever.
258 fn schedule_read(&mut self) -> io::Result<bool> {
259 assert_eq!(self.state, State::NotReading);
261 let slice = slice_to_end(self.dst);
262 self.pipe.read_overlapped(slice, &mut *self.overlapped)?
265 // If this read finished immediately then our overlapped event will
266 // remain signaled (it was signaled coming in here) and we'll progress
267 // down to the method below.
269 // Otherwise the I/O operation is scheduled and the system set our event
270 // to not signaled, so we flag ourselves into the reading state and move
272 self.state = match amt {
273 Some(0) => return Ok(false),
274 Some(amt) => State::Read(amt),
275 None => State::Reading,
280 /// Wait for the result of the overlapped operation previously executed.
282 /// Takes a parameter `wait` which indicates if this pipe is currently being
283 /// read whether the function should block waiting for the read to complete.
287 /// * `true` - finished any pending read and the pipe is not at EOF (keep
289 /// * `false` - finished any pending read and pipe is at EOF (stop issuing
291 fn result(&mut self) -> io::Result<bool> {
292 let amt = match self.state {
293 State::NotReading => return Ok(true),
295 self.pipe.overlapped_result(&mut *self.overlapped, true)?
297 State::Read(amt) => amt,
299 self.state = State::NotReading;
301 let len = self.dst.len();
302 self.dst.set_len(len + amt);
307 /// Finishes out reading this pipe entirely.
309 /// Waits for any pending and schedule read, and then calls `read_to_end`
310 /// if necessary to read all the remaining information.
311 fn finish(&mut self) -> io::Result<()> {
312 while self.result()? && self.schedule_read()? {
319 impl<'a> Drop for AsyncPipe<'a> {
326 // If we have a pending read operation, then we have to make sure that
327 // it's *done* before we actually drop this type. The kernel requires
328 // that the `OVERLAPPED` and buffer pointers are valid for the entire
331 // To do that, we call `CancelIo` to cancel any pending operation, and
332 // if that succeeds we wait for the overlapped result.
334 // If anything here fails, there's not really much we can do, so we leak
335 // the buffer/OVERLAPPED pointers to ensure we're at least memory safe.
336 if self.pipe.cancel_io().is_err() || self.result().is_err() {
337 let buf = mem::replace(self.dst, Vec::new());
338 let overlapped = Box::new(unsafe { mem::zeroed() });
339 let overlapped = mem::replace(&mut self.overlapped, overlapped);
340 mem::forget((buf, overlapped));
345 unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
346 if v.capacity() == 0 {
349 if v.capacity() == v.len() {
352 slice::from_raw_parts_mut(v.as_mut_ptr().add(v.len()),
353 v.capacity() - v.len())