-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #31417 - alexcrichton:cloexec-all-the-things, r=brson
These commits finish up closing out #24237 by filling out all locations we create new file descriptors with variants that atomically create the file descriptor and set CLOEXEC where possible. Previous support for doing this in `File::open` was added in #27971 and support for `try_clone` was added in #27980. This commit fills out: * `Socket::new` now passes `SOCK_CLOEXEC` * `Socket::accept` now uses `accept4` * `pipe2` is used instead of `pipe` Unfortunately most of this support is Linux-specific, and most of it is post-2.6.18 (our oldest supported version), so all of the detection here is done dynamically. It looks like OSX does not have equivalent variants for these functions, so there's nothing more we can do there. Support for BSDs can be added over time if they also have these functions. Closes #24237
- Loading branch information
Showing
9 changed files
with
202 additions
and
45 deletions.
There are no files selected for viewing
Submodule liblibc
updated
from 30f70b to a64ee2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Support for "weak linkage" to symbols on Unix | ||
//! | ||
//! Some I/O operations we do in libstd require newer versions of OSes but we | ||
//! need to maintain binary compatibility with older releases for now. In order | ||
//! to use the new functionality when available we use this module for | ||
//! detection. | ||
//! | ||
//! One option to use here is weak linkage, but that is unfortunately only | ||
//! really workable on Linux. Hence, use dlsym to get the symbol value at | ||
//! runtime. This is also done for compatibility with older versions of glibc, | ||
//! and to avoid creating dependencies on GLIBC_PRIVATE symbols. It assumes that | ||
//! we've been dynamically linked to the library the symbol comes from, but that | ||
//! is currently always the case for things like libpthread/libc. | ||
//! | ||
//! A long time ago this used weak linkage for the __pthread_get_minstack | ||
//! symbol, but that caused Debian to detect an unnecessarily strict versioned | ||
//! dependency on libc6 (#23628). | ||
|
||
use libc; | ||
|
||
use ffi::CString; | ||
use marker; | ||
use mem; | ||
use sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
macro_rules! weak { | ||
(fn $name:ident($($t:ty),*) -> $ret:ty) => ( | ||
static $name: ::sys::weak::Weak<unsafe extern fn($($t),*) -> $ret> = | ||
::sys::weak::Weak::new(stringify!($name)); | ||
) | ||
} | ||
|
||
pub struct Weak<F> { | ||
name: &'static str, | ||
addr: AtomicUsize, | ||
_marker: marker::PhantomData<F>, | ||
} | ||
|
||
impl<F> Weak<F> { | ||
pub const fn new(name: &'static str) -> Weak<F> { | ||
Weak { | ||
name: name, | ||
addr: AtomicUsize::new(1), | ||
_marker: marker::PhantomData, | ||
} | ||
} | ||
|
||
pub fn get(&self) -> Option<&F> { | ||
assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>()); | ||
unsafe { | ||
if self.addr.load(Ordering::SeqCst) == 1 { | ||
self.addr.store(fetch(self.name), Ordering::SeqCst); | ||
} | ||
if self.addr.load(Ordering::SeqCst) == 0 { | ||
None | ||
} else { | ||
mem::transmute::<&AtomicUsize, Option<&F>>(&self.addr) | ||
} | ||
} | ||
} | ||
} | ||
|
||
unsafe fn fetch(name: &str) -> usize { | ||
let name = match CString::new(name) { | ||
Ok(cstr) => cstr, | ||
Err(..) => return 0, | ||
}; | ||
let lib = libc::dlopen(0 as *const _, libc::RTLD_LAZY); | ||
if lib.is_null() { | ||
return 0 | ||
} | ||
let ret = libc::dlsym(lib, name.as_ptr()) as usize; | ||
libc::dlclose(lib); | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters