Skip to content
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

migrate to winapi 0.3 #1322

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
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
60 changes: 5 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ url = "1.1.0"
wait-timeout = "0.1.5"

[target."cfg(windows)".dependencies]
winapi = "0.2.8"
winapi = { version = "0.3", features = ["jobapi", "jobapi2", "processthreadsapi", "psapi", "synchapi", "winuser"] }
winreg = "0.4.0"
user32-sys = "0.2.0"
kernel32-sys = "0.2.1"
gcc = "0.3.50"
psapi-sys = "0.1"

[dev-dependencies]
rustup-mock = { path = "src/rustup-mock", version = "1.1.0" }
Expand Down
72 changes: 36 additions & 36 deletions src/rustup-cli/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ mod imp {

#[cfg(windows)]
mod imp {
extern crate kernel32;
extern crate winapi;
extern crate psapi;

use std::ffi::OsString;
use std::io;
use std::mem;
use std::os::windows::prelude::*;
use winapi::shared::*;
use winapi::um::*;

pub struct Setup {
job: Handle,
}

pub struct Handle {
inner: winapi::HANDLE,
inner: ntdef::HANDLE,
}

fn last_err() -> io::Error {
Expand All @@ -65,7 +65,7 @@ mod imp {
// use job objects, so we instead just ignore errors and assume that
// we're otherwise part of someone else's job object in this case.

let job = kernel32::CreateJobObjectW(0 as *mut _, 0 as *const _);
let job = jobapi2::CreateJobObjectW(0 as *mut _, 0 as *const _);
if job.is_null() {
return None
}
Expand All @@ -75,22 +75,22 @@ mod imp {
// process in the object should be killed. Note that this includes our
// entire process tree by default because we've added ourselves and and
// our children will reside in the job once we spawn a process.
let mut info: winapi::JOBOBJECT_EXTENDED_LIMIT_INFORMATION;
let mut info: winnt::JOBOBJECT_EXTENDED_LIMIT_INFORMATION;
info = mem::zeroed();
info.BasicLimitInformation.LimitFlags =
winapi::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
let r = kernel32::SetInformationJobObject(job.inner,
winapi::JobObjectExtendedLimitInformation,
&mut info as *mut _ as winapi::LPVOID,
mem::size_of_val(&info) as winapi::DWORD);
winnt::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
let r = jobapi2::SetInformationJobObject(job.inner,
winnt::JobObjectExtendedLimitInformation,
&mut info as *mut _ as minwindef::LPVOID,
mem::size_of_val(&info) as minwindef::DWORD);
if r == 0 {
return None
}

// Assign our process to this job object, meaning that our children will
// now live or die based on our existence.
let me = kernel32::GetCurrentProcess();
let r = kernel32::AssignProcessToJobObject(job.inner, me);
let me = processthreadsapi::GetCurrentProcess();
let r = jobapi2::AssignProcessToJobObject(job.inner, me);
if r == 0 {
return None
}
Expand Down Expand Up @@ -118,13 +118,13 @@ mod imp {
info!("killed some, going for more");
}

let mut info: winapi::JOBOBJECT_EXTENDED_LIMIT_INFORMATION;
let mut info: winnt::JOBOBJECT_EXTENDED_LIMIT_INFORMATION;
info = mem::zeroed();
let r = kernel32::SetInformationJobObject(
let r = jobapi2::SetInformationJobObject(
self.job.inner,
winapi::JobObjectExtendedLimitInformation,
&mut info as *mut _ as winapi::LPVOID,
mem::size_of_val(&info) as winapi::DWORD);
winnt::JobObjectExtendedLimitInformation,
&mut info as *mut _ as minwindef::LPVOID,
mem::size_of_val(&info) as minwindef::DWORD);
if r == 0 {
info!("failed to configure job object to defaults: {}",
last_err());
Expand All @@ -137,16 +137,16 @@ mod imp {
unsafe fn kill_remaining(&mut self) -> bool {
#[repr(C)]
struct Jobs {
header: winapi::JOBOBJECT_BASIC_PROCESS_ID_LIST,
list: [winapi::ULONG_PTR; 1024],
header: winnt::JOBOBJECT_BASIC_PROCESS_ID_LIST,
list: [basetsd::ULONG_PTR; 1024],
}

let mut jobs: Jobs = mem::zeroed();
let r = kernel32::QueryInformationJobObject(
let r = jobapi2::QueryInformationJobObject(
self.job.inner,
winapi::JobObjectBasicProcessIdList,
&mut jobs as *mut _ as winapi::LPVOID,
mem::size_of_val(&jobs) as winapi::DWORD,
winnt::JobObjectBasicProcessIdList,
&mut jobs as *mut _ as minwindef::LPVOID,
mem::size_of_val(&jobs) as minwindef::DWORD,
0 as *mut _);
if r == 0 {
info!("failed to query job object: {}", last_err());
Expand All @@ -159,17 +159,17 @@ mod imp {

let list = list.iter().filter(|&&id| {
// let's not kill ourselves
id as winapi::DWORD != kernel32::GetCurrentProcessId()
id as minwindef::DWORD != processthreadsapi::GetCurrentProcessId()
}).filter_map(|&id| {
// Open the process with the necessary rights, and if this
// fails then we probably raced with the process exiting so we
// ignore the problem.
let flags = winapi::PROCESS_QUERY_INFORMATION |
winapi::PROCESS_TERMINATE |
winapi::SYNCHRONIZE;
let p = kernel32::OpenProcess(flags,
winapi::FALSE,
id as winapi::DWORD);
let flags = winnt::PROCESS_QUERY_INFORMATION |
winnt::PROCESS_TERMINATE |
winnt::SYNCHRONIZE;
let p = processthreadsapi::OpenProcess(flags,
minwindef::FALSE,
id as minwindef::DWORD);
if p.is_null() {
None
} else {
Expand All @@ -180,12 +180,12 @@ mod imp {
// If it's not then we likely raced with something else
// recycling this PID, so we just skip this step.
let mut res = 0;
let r = kernel32::IsProcessInJob(p.inner, self.job.inner, &mut res);
let r = jobapi::IsProcessInJob(p.inner, self.job.inner, &mut res);
if r == 0 {
info!("failed to test is process in job: {}", last_err());
return false
}
res == winapi::TRUE
res == minwindef::TRUE
});


Expand All @@ -195,7 +195,7 @@ mod imp {
let mut buf = [0; 1024];
let r = psapi::GetProcessImageFileNameW(p.inner,
buf.as_mut_ptr(),
buf.len() as winapi::DWORD);
buf.len() as minwindef::DWORD);
if r == 0 {
info!("failed to get image name: {}", last_err());
continue
Expand Down Expand Up @@ -224,14 +224,14 @@ mod imp {
// Ok, this isn't mspdbsrv, let's kill the process. After we
// kill it we wait on it to ensure that the next time around in
// this function we're not going to see it again.
let r = kernel32::TerminateProcess(p.inner, 1);
let r = processthreadsapi::TerminateProcess(p.inner, 1);
if r == 0 {
info!("\tfailed to kill subprocess: {}", last_err());
info!("\tassuming subprocess is dead...");
} else {
info!("\tterminated subprocess");
}
let r = kernel32::WaitForSingleObject(p.inner, winapi::INFINITE);
let r = synchapi::WaitForSingleObject(p.inner, winbase::INFINITE);
if r != 0 {
info!("failed to wait for process to die: {}", last_err());
return false
Expand All @@ -245,7 +245,7 @@ mod imp {

impl Drop for Handle {
fn drop(&mut self) {
unsafe { kernel32::CloseHandle(self.inner); }
unsafe { handleapi::CloseHandle(self.inner); }
}
}
}
7 changes: 1 addition & 6 deletions src/rustup-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ extern crate gcc;
extern crate winapi;
#[cfg(windows)]
extern crate winreg;
#[cfg(windows)]
extern crate user32;
#[cfg(windows)]
extern crate kernel32;
extern crate libc;

#[macro_use]
Expand Down Expand Up @@ -172,8 +168,7 @@ fn make_environment_compatible() {
#[cfg(windows)]
fn fix_windows_reg_key() {
use winreg::RegKey;
use winreg::enums::RegType;
use winapi::*;
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};

let root = RegKey::predef(HKEY_CURRENT_USER);
let env = root.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE);
Expand Down
Loading