mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
Use SOCK_CLOEXEC and accept4() on more platforms.
This commit is contained in:
parent
8df58ae03a
commit
59c6ae615e
@ -55,9 +55,18 @@ impl Socket {
|
|||||||
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
|
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
|
||||||
unsafe {
|
unsafe {
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(target_os = "linux")] {
|
if #[cfg(any(
|
||||||
// On Linux we pass the SOCK_CLOEXEC flag to atomically create
|
target_os = "android",
|
||||||
// the socket and set it as CLOEXEC, added in 2.6.27.
|
target_os = "dragonfly",
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "illumos",
|
||||||
|
target_os = "linux",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "opensbd",
|
||||||
|
))] {
|
||||||
|
// On platforms that support it we pass the SOCK_CLOEXEC
|
||||||
|
// flag to atomically create the socket and set it as
|
||||||
|
// CLOEXEC. On Linux this was added in 2.6.27.
|
||||||
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
|
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
|
||||||
Ok(Socket(FileDesc::new(fd)))
|
Ok(Socket(FileDesc::new(fd)))
|
||||||
} else {
|
} else {
|
||||||
@ -83,7 +92,15 @@ impl Socket {
|
|||||||
let mut fds = [0, 0];
|
let mut fds = [0, 0];
|
||||||
|
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(target_os = "linux")] {
|
if #[cfg(any(
|
||||||
|
target_os = "android",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "illumos",
|
||||||
|
target_os = "linux",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "opensbd",
|
||||||
|
))] {
|
||||||
// Like above, set cloexec atomically
|
// Like above, set cloexec atomically
|
||||||
cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
|
cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
|
||||||
Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1]))))
|
Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1]))))
|
||||||
@ -174,9 +191,18 @@ impl Socket {
|
|||||||
pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
|
pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
|
||||||
// Unfortunately the only known way right now to accept a socket and
|
// Unfortunately the only known way right now to accept a socket and
|
||||||
// atomically set the CLOEXEC flag is to use the `accept4` syscall on
|
// atomically set the CLOEXEC flag is to use the `accept4` syscall on
|
||||||
// Linux. This was added in 2.6.28, glibc 2.10 and musl 0.9.5.
|
// platforms that support it. On Linux, this was added in 2.6.28,
|
||||||
|
// glibc 2.10 and musl 0.9.5.
|
||||||
cfg_if::cfg_if! {
|
cfg_if::cfg_if! {
|
||||||
if #[cfg(target_os = "linux")] {
|
if #[cfg(any(
|
||||||
|
target_os = "android",
|
||||||
|
target_os = "dragonfly",
|
||||||
|
target_os = "freebsd",
|
||||||
|
target_os = "illumos",
|
||||||
|
target_os = "linux",
|
||||||
|
target_os = "netbsd",
|
||||||
|
target_os = "opensbd",
|
||||||
|
))] {
|
||||||
let fd = cvt_r(|| unsafe {
|
let fd = cvt_r(|| unsafe {
|
||||||
libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC)
|
libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC)
|
||||||
})?;
|
})?;
|
||||||
|
Loading…
Reference in New Issue
Block a user