Skip to content

Commit

Permalink
Remove emulation of FD_CLOEXEC/O_NONBLOCK
Browse files Browse the repository at this point in the history
Rather than using the native implementation of these constants
on supported platforms, the native implementation was instead
emulated. This was also hidden from the user even though this
could result in data races and the functionality being broken.

Native functionality is, however, not support on macos/ios.
Rather than enable this emulation solely for this platform, it
should be removed as this is a dangerous abstraction.
  • Loading branch information
Bryant Mairs committed Jun 1, 2018
1 parent badb451 commit 7171945
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 97 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#892](https://github.com/nix-rust/nix/pull/892))
- Remove `IFF_NOTRAILERS` on OpenBSD, as it has been removed in OpenBSD 6.3
([#893](https://github.com/nix-rust/nix/pull/893))
- Emulation of `FD_CLOEXEC` and `O_NONBLOCK` was removed from `socket()`, `accept4()`, and
`socketpair()`.
([#907](https://github.com/nix-rust/nix/pull/907))

### Fixed
- Fixed possible panics when using `SigAction::flags` on Linux
Expand Down
114 changes: 17 additions & 97 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! [Further reading](http://man7.org/linux/man-pages/man7/socket.7.html)
use {Error, Result};
use errno::Errno;
use features;
use libc::{self, c_void, c_int, socklen_t, size_t};
use std::{fmt, mem, ptr, slice};
use std::os::unix::io::RawFd;
Expand Down Expand Up @@ -692,87 +691,44 @@ pub fn recvmsg<'a, T>(fd: RawFd, iov: &[IoVec<&mut [u8]>], cmsg_buffer: Option<&
///
/// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html)
pub fn socket<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: SockType, flags: SockFlag, protocol: T) -> Result<RawFd> {
let mut ty = ty as c_int;
let protocol = match protocol.into() {
None => 0,
Some(p) => p as c_int,
};
let feat_atomic = features::socket_atomic_cloexec();

if feat_atomic {
ty |= flags.bits();
}
// SockFlags are usually embedded into `ty`, but we don't do that in `nix` because it's a
// little easier to understand by separating it out. So we have to merge these bitfields
// here.
let mut ty = ty as c_int;
ty |= flags.bits();

// TODO: Check the kernel version
let res = try!(Errno::result(unsafe { libc::socket(domain as c_int, ty, protocol) }));

#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
{
use fcntl::{fcntl, FdFlag, OFlag};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};

if !feat_atomic {
if flags.contains(SockFlag::SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FdFlag::FD_CLOEXEC)));
}

if flags.contains(SockFlag::SOCK_NONBLOCK) {
try!(fcntl(res, F_SETFL(OFlag::O_NONBLOCK)));
}
}
}
let res = unsafe { libc::socket(domain as c_int, ty, protocol) };

Ok(res)
Errno::result(res)
}

/// Create a pair of connected sockets
///
/// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/socketpair.html)
pub fn socketpair<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: SockType, protocol: T,
flags: SockFlag) -> Result<(RawFd, RawFd)> {
let mut ty = ty as c_int;
let protocol = match protocol.into() {
None => 0,
Some(p) => p as c_int,
};
let feat_atomic = features::socket_atomic_cloexec();

if feat_atomic {
ty |= flags.bits();
}
// SockFlags are usually embedded into `ty`, but we don't do that in `nix` because it's a
// little easier to understand by separating it out. So we have to merge these bitfields
// here.
let mut ty = ty as c_int;
ty |= flags.bits();

let mut fds = [-1, -1];
let res = unsafe {
libc::socketpair(domain as c_int, ty, protocol, fds.as_mut_ptr())
};
try!(Errno::result(res));

#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
{
use fcntl::{fcntl, FdFlag, OFlag};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};

if !feat_atomic {
if flags.contains(SockFlag::SOCK_CLOEXEC) {
try!(fcntl(fds[0], F_SETFD(FdFlag::FD_CLOEXEC)));
try!(fcntl(fds[1], F_SETFD(FdFlag::FD_CLOEXEC)));
}
let res = unsafe { libc::socketpair(domain as c_int, ty, protocol, fds.as_mut_ptr()) };
Errno::result(res)?;

if flags.contains(SockFlag::SOCK_NONBLOCK) {
try!(fcntl(fds[0], F_SETFL(OFlag::O_NONBLOCK)));
try!(fcntl(fds[1], F_SETFL(OFlag::O_NONBLOCK)));
}
}
}
Ok((fds[0], fds[1]))
}

Expand Down Expand Up @@ -810,45 +766,9 @@ pub fn accept(sockfd: RawFd) -> Result<RawFd> {
///
/// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html)
pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
accept4_polyfill(sockfd, flags)
}

#[inline]
fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
let res = try!(Errno::result(unsafe { libc::accept(sockfd, ptr::null_mut(), ptr::null_mut()) }));

#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
{
use fcntl::{fcntl, FdFlag, OFlag};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};

if flags.contains(SockFlag::SOCK_CLOEXEC) {
try!(fcntl(res, F_SETFD(FdFlag::FD_CLOEXEC)));
}

if flags.contains(SockFlag::SOCK_NONBLOCK) {
try!(fcntl(res, F_SETFL(OFlag::O_NONBLOCK)));
}
}

// Disable unused variable warning on some platforms
#[cfg(not(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd")))]
{
let _ = flags;
}
let res = unsafe { libc::accept4(sockfd, ptr::null_mut(), ptr::null_mut(), flags.bits()) };


Ok(res)
Errno::result(res)
}

/// Initiate a connection on a socket
Expand Down

0 comments on commit 7171945

Please sign in to comment.