process: add Child::raw_handle() on windows (#3998)

Fixes #3987
This commit is contained in:
Félix Saparelli
2021-07-28 15:42:03 +00:00
committed by GitHub
parent 8b447649bb
commit f957f7f9a7
3 changed files with 42 additions and 1 deletions
+12
View File
@@ -199,6 +199,8 @@ use std::io;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, RawHandle};
#[cfg(windows)]
use std::os::windows::process::CommandExt;
use std::path::Path;
use std::pin::Pin;
@@ -952,6 +954,16 @@ impl Child {
}
}
/// Extracts the raw handle of the process associated with this child while
/// it is still running. Returns `None` if the child has exited.
#[cfg(windows)]
pub fn raw_handle(&self) -> Option<RawHandle> {
match &self.child {
FusedChild::Child(c) => Some(c.inner.as_raw_handle()),
FusedChild::Done(_) => None,
}
}
/// Attempts to force the child to exit, but does not wait for the request
/// to take effect.
///
+7 -1
View File
@@ -24,7 +24,7 @@ use mio::windows::NamedPipe;
use std::fmt;
use std::future::Future;
use std::io;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle};
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
use std::pin::Pin;
use std::process::Stdio;
use std::process::{Child as StdChild, Command as StdCommand, ExitStatus};
@@ -144,6 +144,12 @@ impl Future for Child {
}
}
impl AsRawHandle for Child {
fn as_raw_handle(&self) -> RawHandle {
self.child.as_raw_handle()
}
}
impl Drop for Waiting {
fn drop(&mut self) {
unsafe {
+23
View File
@@ -0,0 +1,23 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
#![cfg(windows)]
use tokio::process::Command;
use winapi::um::processthreadsapi::GetProcessId;
#[tokio::test]
async fn obtain_raw_handle() {
let mut cmd = Command::new("cmd");
cmd.kill_on_drop(true);
cmd.arg("/c");
cmd.arg("pause");
let child = cmd.spawn().unwrap();
let orig_id = child.id().expect("missing id");
assert!(orig_id > 0);
let handle = child.raw_handle().expect("process stopped");
let handled_id = unsafe { GetProcessId(handle as _) };
assert_eq!(handled_id, orig_id);
}