]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/pipe.rs
Fix rebase fail
[rust.git] / src / libstd / sys / windows / pipe.rs
1 use os::windows::prelude::*;
2
3 use ffi::OsStr;
4 use io;
5 use mem;
6 use path::Path;
7 use ptr;
8 use slice;
9 use sync::atomic::Ordering::SeqCst;
10 use sync::atomic::AtomicUsize;
11 use sys::c;
12 use sys::fs::{File, OpenOptions};
13 use sys::handle::Handle;
14 use sys::hashmap_random_keys;
15
16 ////////////////////////////////////////////////////////////////////////////////
17 // Anonymous pipes
18 ////////////////////////////////////////////////////////////////////////////////
19
20 pub struct AnonPipe {
21     inner: Handle,
22 }
23
24 pub struct Pipes {
25     pub ours: AnonPipe,
26     pub theirs: AnonPipe,
27 }
28
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
32 /// else".
33 ///
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.
37 ///
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.
43 ///
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.
53     //
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!
59     unsafe {
60         let ours;
61         let mut name;
62         let mut tries = 0;
63         let mut reject_remote_clients_flag = c::PIPE_REJECT_REMOTE_CLIENTS;
64         loop {
65             tries += 1;
66             name = format!(r"\\.\pipe\__rust_anonymous_pipe1__.{}.{}",
67                            c::GetCurrentProcessId(),
68                            random_number());
69             let wide_name = OsStr::new(&name)
70                                   .encode_wide()
71                                   .chain(Some(0))
72                                   .collect::<Vec<_>>();
73             let mut flags = c::FILE_FLAG_FIRST_PIPE_INSTANCE |
74                 c::FILE_FLAG_OVERLAPPED;
75             if ours_readable {
76                 flags |= c::PIPE_ACCESS_INBOUND;
77             } else {
78                 flags |= c::PIPE_ACCESS_OUTBOUND;
79             }
80
81             let handle = c::CreateNamedPipeW(wide_name.as_ptr(),
82                                              flags,
83                                              c::PIPE_TYPE_BYTE |
84                                              c::PIPE_READMODE_BYTE |
85                                              c::PIPE_WAIT |
86                                              reject_remote_clients_flag,
87                                              1,
88                                              4096,
89                                              4096,
90                                              0,
91                                              ptr::null_mut());
92
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
97             // again.
98             //
99             // Don't try again too much though as this could also perhaps be a
100             // legit error.
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
108             // testing strategy
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();
113                 if tries < 10 {
114                     if raw_os_err == Some(c::ERROR_ACCESS_DENIED as i32) {
115                         continue
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;
119                         tries -= 1;
120                         continue
121                     }
122                 }
123                 return Err(err)
124             }
125             ours = Handle::new(handle);
126             break
127         }
128
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
132         // readable.
133         //
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);
139         opts.share_mode(0);
140         let theirs = File::open(Path::new(&name), &opts)?;
141         let theirs = AnonPipe { inner: theirs.into_handle() };
142
143         Ok(Pipes {
144             ours: AnonPipe { inner: ours },
145             theirs: AnonPipe { inner: theirs.into_handle() },
146         })
147     }
148 }
149
150 fn random_number() -> usize {
151     static N: AtomicUsize = AtomicUsize::new(0);
152     loop {
153         if N.load(SeqCst) != 0 {
154             return N.fetch_add(1, SeqCst)
155         }
156
157         N.store(hashmap_random_keys().0 as usize, SeqCst);
158     }
159 }
160
161 impl AnonPipe {
162     pub fn handle(&self) -> &Handle { &self.inner }
163     pub fn into_handle(self) -> Handle { self.inner }
164
165     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
166         self.inner.read(buf)
167     }
168
169     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
170         self.inner.write(buf)
171     }
172 }
173
174 pub fn read2(p1: AnonPipe,
175              v1: &mut Vec<u8>,
176              p2: AnonPipe,
177              v2: &mut Vec<u8>) -> io::Result<()> {
178     let p1 = p1.into_handle();
179     let p2 = p2.into_handle();
180
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()];
184
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.
188     //
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.
193     loop {
194         let res = unsafe {
195             c::WaitForMultipleObjects(2, objs.as_ptr(), c::FALSE, c::INFINITE)
196         };
197         if res == c::WAIT_OBJECT_0 {
198             if !p1.result()? || !p1.schedule_read()? {
199                 return p2.finish()
200             }
201         } else if res == c::WAIT_OBJECT_0 + 1 {
202             if !p2.result()? || !p2.schedule_read()? {
203                 return p1.finish()
204             }
205         } else {
206             return Err(io::Error::last_os_error())
207         }
208     }
209 }
210
211 struct AsyncPipe<'a> {
212     pipe: Handle,
213     event: Handle,
214     overlapped: Box<c::OVERLAPPED>, // needs a stable address
215     dst: &'a mut Vec<u8>,
216     state: State,
217 }
218
219 #[derive(PartialEq, Debug)]
220 enum State {
221     NotReading,
222     Reading,
223     Read(usize),
224 }
225
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.
231         //
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())
241         };
242         overlapped.hEvent = event.raw();
243         Ok(AsyncPipe {
244             pipe,
245             overlapped,
246             event,
247             dst,
248             state: State::NotReading,
249         })
250     }
251
252     /// Executes an overlapped read operation.
253     ///
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);
260         let amt = unsafe {
261             let slice = slice_to_end(self.dst);
262             self.pipe.read_overlapped(slice, &mut *self.overlapped)?
263         };
264
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.
268         //
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
271         // on.
272         self.state = match amt {
273             Some(0) => return Ok(false),
274             Some(amt) => State::Read(amt),
275             None => State::Reading,
276         };
277         Ok(true)
278     }
279
280     /// Wait for the result of the overlapped operation previously executed.
281     ///
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.
284     ///
285     /// Return values:
286     ///
287     /// * `true` - finished any pending read and the pipe is not at EOF (keep
288     ///            going)
289     /// * `false` - finished any pending read and pipe is at EOF (stop issuing
290     ///             reads)
291     fn result(&mut self) -> io::Result<bool> {
292         let amt = match self.state {
293             State::NotReading => return Ok(true),
294             State::Reading => {
295                 self.pipe.overlapped_result(&mut *self.overlapped, true)?
296             }
297             State::Read(amt) => amt,
298         };
299         self.state = State::NotReading;
300         unsafe {
301             let len = self.dst.len();
302             self.dst.set_len(len + amt);
303         }
304         Ok(amt != 0)
305     }
306
307     /// Finishes out reading this pipe entirely.
308     ///
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()? {
313             // ...
314         }
315         Ok(())
316     }
317 }
318
319 impl<'a> Drop for AsyncPipe<'a> {
320     fn drop(&mut self) {
321         match self.state {
322             State::Reading => {}
323             _ => return,
324         }
325
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
329         // I/O operation.
330         //
331         // To do that, we call `CancelIo` to cancel any pending operation, and
332         // if that succeeds we wait for the overlapped result.
333         //
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));
341         }
342     }
343 }
344
345 unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
346     if v.capacity() == 0 {
347         v.reserve(16);
348     }
349     if v.capacity() == v.len() {
350         v.reserve(1);
351     }
352     slice::from_raw_parts_mut(v.as_mut_ptr().add(v.len()),
353                               v.capacity() - v.len())
354 }