Skip to content

std: Fix inheriting standard handles on windows #24873

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
Apr 30, 2015
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
8 changes: 7 additions & 1 deletion src/libstd/sys/windows/fs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct OpenOptions {
share_mode: Option<libc::DWORD>,
creation_disposition: Option<libc::DWORD>,
flags_and_attributes: Option<libc::DWORD>,
security_attributes: usize, // *mut T doesn't have a Default impl
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -135,6 +136,9 @@ impl OpenOptions {
pub fn share_mode(&mut self, val: i32) {
self.share_mode = Some(val as libc::DWORD);
}
pub fn security_attributes(&mut self, attrs: libc::LPSECURITY_ATTRIBUTES) {
self.security_attributes = attrs as usize;
}

fn get_desired_access(&self) -> libc::DWORD {
self.desired_access.unwrap_or({
Expand Down Expand Up @@ -186,7 +190,7 @@ impl File {
libc::CreateFileW(path.as_ptr(),
opts.get_desired_access(),
opts.get_share_mode(),
ptr::null_mut(),
opts.security_attributes as *mut _,
opts.get_creation_disposition(),
opts.get_flags_and_attributes(),
ptr::null_mut())
Expand Down Expand Up @@ -263,6 +267,8 @@ impl File {
}

pub fn handle(&self) -> &Handle { &self.handle }

pub fn into_handle(self) -> Handle { self.handle }
}

impl FromInner<libc::HANDLE> for File {
Expand Down
13 changes: 13 additions & 0 deletions src/libstd/sys/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use prelude::v1::*;

use io::ErrorKind;
use io;
use libc::funcs::extra::kernel32::{GetCurrentProcess, DuplicateHandle};
use libc::{self, HANDLE};
use mem;
use ptr;
Expand Down Expand Up @@ -65,6 +66,18 @@ impl Handle {
}));
Ok(amt as usize)
}

pub fn duplicate(&self, access: libc::DWORD, inherit: bool,
options: libc::DWORD) -> io::Result<Handle> {
let mut ret = 0 as libc::HANDLE;
try!(cvt(unsafe {
let cur_proc = GetCurrentProcess();
DuplicateHandle(cur_proc, self.0, cur_proc, &mut ret,
access, inherit as libc::BOOL,
options)
}));
Ok(Handle::new(ret))
}
}

impl Drop for Handle {
Expand Down
Loading