Skip to content

Implement os.waitpid on Windows #2048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,6 @@ mod posix {
unsafe { libc::WEXITSTATUS(status) }
}

// TODO: os.wait[pid] for windows
#[pyfunction]
fn waitpid(pid: libc::pid_t, opt: i32, vm: &VirtualMachine) -> PyResult<(libc::pid_t, i32)> {
let mut status = 0;
Expand Down Expand Up @@ -2134,6 +2133,8 @@ mod nt {
use super::*;
pub(super) use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::RawHandle;
#[cfg(target_env = "msvc")]
use winapi::vc::vcruntime::intptr_t;

pub(super) type OpenFlags = u32;

Expand Down Expand Up @@ -2290,6 +2291,46 @@ mod nt {
get_stats().map_err(|e| convert_io_error(vm, e))
}

// cwait is available on MSVC only (according to CPython)
#[cfg(target_env = "msvc")]
extern "C" {
fn _cwait(termstat: *mut i32, procHandle: intptr_t, action: i32) -> intptr_t;
fn _get_errno(pValue: *mut i32) -> i32;
}

#[cfg(target_env = "msvc")]
#[pyfunction]
fn waitpid(pid: intptr_t, opt: i32, vm: &VirtualMachine) -> PyResult<(intptr_t, i32)> {
const ECHILD: i32 = 10;
const EINVAL: i32 = 22;

let mut status = 0;
let pid = unsafe { suppress_iph!(_cwait(&mut status, pid, opt)) };
if pid == -1 {
let mut errno = 0;
unsafe { _get_errno(&mut errno) };
match errno {
ECHILD => Err(vm.new_exception_msg(
vm.ctx.exceptions.os_error.clone(),
"ECHILD: No spawned processes".to_owned(),
)),
EINVAL => Err(vm.new_exception_msg(
vm.ctx.exceptions.os_error.clone(),
"EINVAL: Invalid argument".to_owned(),
)),
Comment on lines +2310 to +2320
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following unix convert_nix_error, here could be convert_nt_error

Copy link
Contributor Author

@BasixKOR BasixKOR Aug 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to use that kind of error handler, but it seems like in _cwait Microsoft uses some kind of POSIX-like errno which means it's neither WinAPI error nor POSIX error.

_ => unreachable!(),
}
} else {
Ok((pid, status << 8))
}
}

#[cfg(target_env = "msvc")]
#[pyfunction]
fn wait(vm: &VirtualMachine) -> PyResult<(intptr_t, i32)> {
waitpid(-1, 0, vm)
}

#[pyfunction]
fn kill(pid: i32, sig: isize, vm: &VirtualMachine) -> PyResult<()> {
{
Expand Down