process: Update winapi to 0.3

This commit is contained in:
Alex Crichton
2019-06-24 16:56:51 -07:00
committed by Ivan Petkov
parent f0680617ee
commit f48944c1fb
4 changed files with 50 additions and 33 deletions
+13 -2
View File
@@ -26,10 +26,21 @@ env_logger = { version = "0.3", default-features = false }
log = "0.3"
[target.'cfg(windows)'.dependencies]
winapi = "0.2"
kernel32-sys = "0.2"
mio-named-pipes = "0.1"
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = [
"handleapi",
"winerror",
"minwindef",
"processthreadsapi",
"synchapi",
"threadpoollegacyapiset",
"winbase",
"winnt",
]
[target.'cfg(unix)'.dependencies]
libc = "0.2"
tokio-signal = "0.1"
+5 -5
View File
@@ -294,9 +294,9 @@ impl CommandExt for process::Command {
self.stdout(Stdio::piped());
self.stderr(Stdio::piped());
OutputAsync {
inner: self.spawn_async(handle).into_future().and_then(|c| {
inner: Box::new(self.spawn_async(handle).into_future().and_then(|c| {
c.wait_with_output()
}).boxed(),
})),
}
}
}
@@ -383,13 +383,13 @@ impl Child {
};
WaitWithOutput {
inner: self.join3(stdout, stderr).map(|(status, stdout, stderr)| {
inner: Box::new(self.join3(stdout, stderr).map(|(status, stdout, stderr)| {
Output {
status: status,
stdout: stdout,
stderr: stderr,
}
}).boxed()
}))
}
}
@@ -668,7 +668,7 @@ impl Command {
pub fn spawn(mut self) -> Spawn {
Spawn {
inner: self.inner.spawn_async(&self.handle).into_future().boxed()
inner: Box::new(self.inner.spawn_async(&self.handle).into_future()),
}
}
}
+27 -21
View File
@@ -16,7 +16,6 @@
//! from then on out.
extern crate winapi;
extern crate kernel32;
extern crate mio_named_pipes;
use std::fmt;
@@ -25,10 +24,18 @@ use std::os::windows::prelude::*;
use std::os::windows::process::ExitStatusExt;
use std::process::{self, ExitStatus};
use futures::{Future, Poll, Async} ;
use futures::sync::oneshot;
use futures::future::Fuse;
use futures::sync::oneshot;
use futures::{Future, Poll, Async} ;
use self::mio_named_pipes::NamedPipe;
use self::winapi::shared::minwindef::*;
use self::winapi::shared::winerror::*;
use self::winapi::um::handleapi::*;
use self::winapi::um::processthreadsapi::*;
use self::winapi::um::synchapi::*;
use self::winapi::um::threadpoollegacyapiset::*;
use self::winapi::um::winbase::*;
use self::winapi::um::winnt::*;
use tokio_core::reactor::{PollEvented, Handle};
#[must_use = "futures do nothing unless polled"]
@@ -49,7 +56,7 @@ impl fmt::Debug for Child {
struct Waiting {
rx: Fuse<oneshot::Receiver<()>>,
wait_object: winapi::HANDLE,
wait_object: HANDLE,
tx: *mut Option<oneshot::Sender<()>>,
}
@@ -105,13 +112,13 @@ impl Child {
let ptr = Box::into_raw(Box::new(Some(tx)));
let mut wait_object = 0 as *mut _;
let rc = unsafe {
kernel32::RegisterWaitForSingleObject(&mut wait_object,
self.child.as_raw_handle(),
Some(callback),
ptr as *mut _,
winapi::INFINITE,
winapi::WT_EXECUTEINWAITTHREAD |
winapi::WT_EXECUTEONLYONCE)
RegisterWaitForSingleObject(&mut wait_object,
self.child.as_raw_handle(),
Some(callback),
ptr as *mut _,
INFINITE,
WT_EXECUTEINWAITTHREAD |
WT_EXECUTEONLYONCE)
};
if rc == 0 {
let err = io::Error::last_os_error();
@@ -130,8 +137,7 @@ impl Child {
impl Drop for Waiting {
fn drop(&mut self) {
unsafe {
let rc = kernel32::UnregisterWaitEx(self.wait_object,
winapi::INVALID_HANDLE_VALUE);
let rc = UnregisterWaitEx(self.wait_object, INVALID_HANDLE_VALUE);
if rc == 0 {
panic!("failed to unregister: {}", io::Error::last_os_error());
}
@@ -140,22 +146,22 @@ impl Drop for Waiting {
}
}
unsafe extern "system" fn callback(ptr: winapi::PVOID,
_timer_fired: winapi::BOOLEAN) {
let mut complete = &mut *(ptr as *mut Option<oneshot::Sender<()>>);
unsafe extern "system" fn callback(ptr: PVOID,
_timer_fired: BOOLEAN) {
let complete = &mut *(ptr as *mut Option<oneshot::Sender<()>>);
drop(complete.take().unwrap().send(()));
}
pub fn try_wait(child: &process::Child) -> io::Result<Option<ExitStatus>> {
unsafe {
match kernel32::WaitForSingleObject(child.as_raw_handle(), 0) {
winapi::WAIT_OBJECT_0 => {}
winapi::WAIT_TIMEOUT => return Ok(None),
match WaitForSingleObject(child.as_raw_handle(), 0) {
WAIT_OBJECT_0 => {}
WAIT_TIMEOUT => return Ok(None),
_ => return Err(io::Error::last_os_error()),
}
let mut status = 0;
let rc = kernel32::GetExitCodeProcess(child.as_raw_handle(), &mut status);
if rc == winapi::FALSE {
let rc = GetExitCodeProcess(child.as_raw_handle(), &mut status);
if rc == FALSE {
Err(io::Error::last_os_error())
} else {
Ok(Some(ExitStatus::from_raw(status)))
+5 -5
View File
@@ -10,7 +10,7 @@ use std::io;
use std::process::{Stdio, ExitStatus, Command};
use std::time::Duration;
use futures::future::{BoxFuture, Future};
use futures::future::Future;
use futures::stream::{self, Stream};
use tokio_io::io::{read_until, write_all, read_to_end};
use tokio_core::reactor::{Core, Timeout};
@@ -25,13 +25,13 @@ fn cat() -> Command {
cmd
}
fn feed_cat(mut cat: Child, n: usize) -> BoxFuture<ExitStatus, io::Error> {
fn feed_cat(mut cat: Child, n: usize) -> Box<Future<Item = ExitStatus, Error = io::Error>> {
let stdin = cat.stdin().take().unwrap();
let stdout = cat.stdout().take().unwrap();
debug!("starting to feed");
// Produce n lines on the child's stdout.
let numbers = stream::iter((0..n).into_iter().map(Ok));
let numbers = stream::iter_ok(0..n);
let write = numbers.fold(stdin, |stdin, i| {
debug!("sending line {} to child", i);
write_all(stdin, format!("line {}\n", i).into_bytes()).map(|p| p.0)
@@ -40,7 +40,7 @@ fn feed_cat(mut cat: Child, n: usize) -> BoxFuture<ExitStatus, io::Error> {
// Try to read `n + 1` lines, ensuring the last one is empty
// (i.e. EOF is reached after `n` lines.
let reader = io::BufReader::new(stdout);
let expected_numbers = stream::iter((0..n + 1).map(Ok));
let expected_numbers = stream::iter_ok(0..n + 1);
let read = expected_numbers.fold((reader, 0), move |(reader, i), _| {
let done = i >= n;
debug!("starting read from child");
@@ -68,7 +68,7 @@ fn feed_cat(mut cat: Child, n: usize) -> BoxFuture<ExitStatus, io::Error> {
});
// Compose reading and writing concurrently.
write.join(read).and_then(|_| cat).boxed()
Box::new(write.join(read).and_then(|_| cat))
}
#[test]