From 285a1983f83864e055feee735966051bed6846a2 Mon Sep 17 00:00:00 2001 From: Steffen Butzer Date: Fri, 29 Dec 2017 23:32:39 +0100 Subject: [PATCH 1/2] migrate to winapi 0.3 --- Cargo.toml | 5 ++- src/cargo/core/shell.rs | 12 +++---- src/cargo/util/job.rs | 74 ++++++++++++++++++++--------------------- src/cargo/util/read2.rs | 2 +- tests/death.rs | 18 +++++----- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 254bad04adc..85f48710348 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,10 +57,9 @@ url = "1.1" core-foundation = { version = "0.4.4", features = ["mac_os_10_7_support"] } [target.'cfg(windows)'.dependencies] -kernel32-sys = "0.2" miow = "0.2" -psapi-sys = "0.1" -winapi = "0.2" +winapi = { version = "0.3", features = ["handleapi", "jobapi", "jobapi2", "minwindef", "ntdef", + "processenv", "processthreadsapi", "psapi", "synchapi", "winerror", "winbase", "wincon", "winnt"] } [dev-dependencies] bufstream = "0.1" diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index ade08505d57..16180fde66c 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -317,16 +317,16 @@ mod imp { #[cfg(windows)] mod imp { - use std::mem; - extern crate winapi; - extern crate kernel32; + + use std::mem; + use self::winapi::um::{processenv, winbase, wincon}; pub fn stderr_width() -> Option { unsafe { - let stdout = kernel32::GetStdHandle(winapi::STD_ERROR_HANDLE); - let mut csbi: winapi::CONSOLE_SCREEN_BUFFER_INFO = mem::zeroed(); - if kernel32::GetConsoleScreenBufferInfo(stdout, &mut csbi) == 0 { + let stdout = processenv::GetStdHandle(winbase::STD_ERROR_HANDLE); + let mut csbi: wincon::CONSOLE_SCREEN_BUFFER_INFO = mem::zeroed(); + if wincon::GetConsoleScreenBufferInfo(stdout, &mut csbi) == 0 { return None } Some((csbi.srWindow.Right - csbi.srWindow.Left) as usize) diff --git a/src/cargo/util/job.rs b/src/cargo/util/job.rs index 06f51356d25..0f03c94b641 100644 --- a/src/cargo/util/job.rs +++ b/src/cargo/util/job.rs @@ -42,21 +42,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 self::winapi::shared::*; + use self::winapi::um::*; pub struct Setup { job: Handle, } pub struct Handle { - inner: winapi::HANDLE, + inner: ntdef::HANDLE, } fn last_err() -> io::Error { @@ -73,7 +73,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 } @@ -83,22 +83,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 } @@ -126,13 +126,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()); @@ -145,16 +145,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()); @@ -168,17 +168,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 { @@ -189,12 +189,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 }); @@ -204,7 +204,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 @@ -233,14 +233,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 @@ -254,7 +254,7 @@ mod imp { impl Drop for Handle { fn drop(&mut self) { - unsafe { kernel32::CloseHandle(self.inner); } + unsafe { handleapi::CloseHandle(self.inner); } } } -} +} \ No newline at end of file diff --git a/src/cargo/util/read2.rs b/src/cargo/util/read2.rs index b3aa7d8b2cc..74d6d67f271 100644 --- a/src/cargo/util/read2.rs +++ b/src/cargo/util/read2.rs @@ -84,7 +84,7 @@ mod imp { use self::miow::iocp::{CompletionPort, CompletionStatus}; use self::miow::pipe::NamedPipe; use self::miow::Overlapped; - use self::winapi::ERROR_BROKEN_PIPE; + use self::winapi::shared::winerror::ERROR_BROKEN_PIPE; struct Pipe<'a> { dst: &'a mut Vec, diff --git a/tests/death.rs b/tests/death.rs index 3d04a24ccc6..33d6f573d97 100644 --- a/tests/death.rs +++ b/tests/death.rs @@ -1,5 +1,7 @@ extern crate cargotest; extern crate libc; +#[cfg(windows)] +extern crate winapi; use std::fs; use std::io::{self, Read}; @@ -23,16 +25,16 @@ fn enabled() -> bool { // can succeed or not. #[cfg(windows)] fn enabled() -> bool { - extern crate kernel32; - extern crate winapi; + use winapi::um::{handleapi, jobapi, jobapi2, processthreadsapi}; + unsafe { // If we're not currently in a job, then we can definitely run these // tests. - let me = kernel32::GetCurrentProcess(); + let me = processthreadsapi::GetCurrentProcess(); let mut ret = 0; - let r = kernel32::IsProcessInJob(me, 0 as *mut _, &mut ret); + let r = jobapi::IsProcessInJob(me, 0 as *mut _, &mut ret); assert!(r != 0); - if ret == winapi::FALSE { + if ret == winapi::shared::minwindef::FALSE { return true } @@ -42,10 +44,10 @@ fn enabled() -> bool { // // If we can't be added to a nested job, then these tests will // definitely fail, and there's not much we can do about that. - let job = kernel32::CreateJobObjectW(0 as *mut _, 0 as *const _); + let job = jobapi2::CreateJobObjectW(0 as *mut _, 0 as *const _); assert!(!job.is_null()); - let r = kernel32::AssignProcessToJobObject(job, me); - kernel32::CloseHandle(job); + let r = jobapi2::AssignProcessToJobObject(job, me); + handleapi::CloseHandle(job); r != 0 } } From 64828ba9cab77d7752733f932112314ee2946f1c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 3 Jan 2018 11:25:28 -0800 Subject: [PATCH 2/2] Touch up style of Windows imports --- Cargo.toml | 20 +++++++++- src/cargo/core/shell.rs | 10 +++-- src/cargo/lib.rs | 0 src/cargo/util/job.rs | 87 +++++++++++++++++++++++------------------ 4 files changed, 72 insertions(+), 45 deletions(-) mode change 100755 => 100644 src/cargo/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 85f48710348..17988a309d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,8 +58,24 @@ core-foundation = { version = "0.4.4", features = ["mac_os_10_7_support"] } [target.'cfg(windows)'.dependencies] miow = "0.2" -winapi = { version = "0.3", features = ["handleapi", "jobapi", "jobapi2", "minwindef", "ntdef", - "processenv", "processthreadsapi", "psapi", "synchapi", "winerror", "winbase", "wincon", "winnt"] } + +[target.'cfg(windows)'.dependencies.winapi] +version = "0.3" +features = [ + "handleapi", + "jobapi", + "jobapi2", + "minwindef", + "ntdef", + "processenv", + "processthreadsapi", + "psapi", + "synchapi", + "winerror", + "winbase", + "wincon", + "winnt", +] [dev-dependencies] bufstream = "0.1" diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 16180fde66c..a372110cdc9 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -320,13 +320,15 @@ mod imp { extern crate winapi; use std::mem; - use self::winapi::um::{processenv, winbase, wincon}; + use self::winapi::um::processenv::*; + use self::winapi::um::winbase::*; + use self::winapi::um::wincon::*; pub fn stderr_width() -> Option { unsafe { - let stdout = processenv::GetStdHandle(winbase::STD_ERROR_HANDLE); - let mut csbi: wincon::CONSOLE_SCREEN_BUFFER_INFO = mem::zeroed(); - if wincon::GetConsoleScreenBufferInfo(stdout, &mut csbi) == 0 { + let stdout = GetStdHandle(STD_ERROR_HANDLE); + let mut csbi: CONSOLE_SCREEN_BUFFER_INFO = mem::zeroed(); + if GetConsoleScreenBufferInfo(stdout, &mut csbi) == 0 { return None } Some((csbi.srWindow.Right - csbi.srWindow.Left) as usize) diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs old mode 100755 new mode 100644 diff --git a/src/cargo/util/job.rs b/src/cargo/util/job.rs index 0f03c94b641..42035e1713f 100644 --- a/src/cargo/util/job.rs +++ b/src/cargo/util/job.rs @@ -48,15 +48,26 @@ mod imp { use std::io; use std::mem; use std::os::windows::prelude::*; - use self::winapi::shared::*; - use self::winapi::um::*; + + use self::winapi::shared::basetsd::*; + use self::winapi::shared::minwindef::*; + use self::winapi::shared::minwindef::{FALSE, TRUE}; + use self::winapi::um::handleapi::*; + use self::winapi::um::jobapi2::*; + use self::winapi::um::jobapi::*; + use self::winapi::um::processthreadsapi::*; + use self::winapi::um::psapi::*; + use self::winapi::um::synchapi::*; + use self::winapi::um::winbase::*; + use self::winapi::um::winnt::*; + use self::winapi::um::winnt::HANDLE; pub struct Setup { job: Handle, } pub struct Handle { - inner: ntdef::HANDLE, + inner: HANDLE, } fn last_err() -> io::Error { @@ -73,7 +84,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 = jobapi2::CreateJobObjectW(0 as *mut _, 0 as *const _); + let job = CreateJobObjectW(0 as *mut _, 0 as *const _); if job.is_null() { return None } @@ -83,22 +94,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: winnt::JOBOBJECT_EXTENDED_LIMIT_INFORMATION; + let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION; info = mem::zeroed(); info.BasicLimitInformation.LimitFlags = - 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); + JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + let r = SetInformationJobObject(job.inner, + JobObjectExtendedLimitInformation, + &mut info as *mut _ as LPVOID, + mem::size_of_val(&info) as 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 = processthreadsapi::GetCurrentProcess(); - let r = jobapi2::AssignProcessToJobObject(job.inner, me); + let me = GetCurrentProcess(); + let r = AssignProcessToJobObject(job.inner, me); if r == 0 { return None } @@ -126,13 +137,13 @@ mod imp { info!("killed some, going for more"); } - let mut info: winnt::JOBOBJECT_EXTENDED_LIMIT_INFORMATION; + let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION; info = mem::zeroed(); - let r = jobapi2::SetInformationJobObject( + let r = SetInformationJobObject( self.job.inner, - winnt::JobObjectExtendedLimitInformation, - &mut info as *mut _ as minwindef::LPVOID, - mem::size_of_val(&info) as minwindef::DWORD); + JobObjectExtendedLimitInformation, + &mut info as *mut _ as LPVOID, + mem::size_of_val(&info) as DWORD); if r == 0 { info!("failed to configure job object to defaults: {}", last_err()); @@ -145,16 +156,16 @@ mod imp { unsafe fn kill_remaining(&mut self) -> bool { #[repr(C)] struct Jobs { - header: winnt::JOBOBJECT_BASIC_PROCESS_ID_LIST, - list: [basetsd::ULONG_PTR; 1024], + header: JOBOBJECT_BASIC_PROCESS_ID_LIST, + list: [ULONG_PTR; 1024], } let mut jobs: Jobs = mem::zeroed(); - let r = jobapi2::QueryInformationJobObject( + let r = QueryInformationJobObject( self.job.inner, - winnt::JobObjectBasicProcessIdList, - &mut jobs as *mut _ as minwindef::LPVOID, - mem::size_of_val(&jobs) as minwindef::DWORD, + JobObjectBasicProcessIdList, + &mut jobs as *mut _ as LPVOID, + mem::size_of_val(&jobs) as DWORD, 0 as *mut _); if r == 0 { info!("failed to query job object: {}", last_err()); @@ -168,17 +179,15 @@ mod imp { let list = list.iter().filter(|&&id| { // let's not kill ourselves - id as minwindef::DWORD != processthreadsapi::GetCurrentProcessId() + id as DWORD != 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 = winnt::PROCESS_QUERY_INFORMATION | - winnt::PROCESS_TERMINATE | - winnt::SYNCHRONIZE; - let p = processthreadsapi::OpenProcess(flags, - minwindef::FALSE, - id as minwindef::DWORD); + let flags = PROCESS_QUERY_INFORMATION | + PROCESS_TERMINATE | + SYNCHRONIZE; + let p = OpenProcess(flags, FALSE, id as DWORD); if p.is_null() { None } else { @@ -189,12 +198,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 = jobapi::IsProcessInJob(p.inner, self.job.inner, &mut res); + let r = IsProcessInJob(p.inner, self.job.inner, &mut res); if r == 0 { info!("failed to test is process in job: {}", last_err()); return false } - res == minwindef::TRUE + res == TRUE }); @@ -202,9 +211,9 @@ mod imp { // Load the file which this process was spawned from. We then // later use this for identification purposes. let mut buf = [0; 1024]; - let r = psapi::GetProcessImageFileNameW(p.inner, - buf.as_mut_ptr(), - buf.len() as minwindef::DWORD); + let r = GetProcessImageFileNameW(p.inner, + buf.as_mut_ptr(), + buf.len() as DWORD); if r == 0 { info!("failed to get image name: {}", last_err()); continue @@ -233,14 +242,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 = processthreadsapi::TerminateProcess(p.inner, 1); + let r = 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 = synchapi::WaitForSingleObject(p.inner, winbase::INFINITE); + let r = WaitForSingleObject(p.inner, INFINITE); if r != 0 { info!("failed to wait for process to die: {}", last_err()); return false @@ -254,7 +263,7 @@ mod imp { impl Drop for Handle { fn drop(&mut self) { - unsafe { handleapi::CloseHandle(self.inner); } + unsafe { CloseHandle(self.inner); } } } -} \ No newline at end of file +}