Skip to content
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

Fix Broken SockFlag::SOCK_NONBLOCK and SockFlag::SOCK_CLOEXEC on MacOS/iOS #863

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
75 changes: 49 additions & 26 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,49 @@ pub enum SockProtocol {
KextControl = libc::SYSPROTO_CONTROL,
}

libc_bitflags!{
/// Additional socket options
pub struct SockFlag: c_int {
/// Set non-blocking mode on the new socket
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
SOCK_NONBLOCK;
/// Set close-on-exec on the new descriptor
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
SOCK_CLOEXEC;
/// Return `EPIPE` instead of raising `SIGPIPE`
#[cfg(target_os = "netbsd")]
SOCK_NOSIGPIPE;
/// For domains `AF_INET(6)`, only allow `connect(2)`, `sendto(2)`, or `sendmsg(2)`
/// to the DNS port (typically 53)
#[cfg(target_os = "openbsd")]
SOCK_DNS;
cfg_if! {
/*
Because `nix` provides a new API that does not conform to
POSIX or BSD (or directly Linux), add virtual definitions
for macOS and iOS that are backed by their equivalent `fcntl` flags.
*/
if #[cfg(any(target_os = "macos",
target_os = "ios"))] {
bitflags! {
pub struct SockFlag: c_int {
const SOCK_NONBLOCK = libc::O_NONBLOCK;
const SOCK_CLOEXEC = libc::O_CLOEXEC;
}
}
} else {
libc_bitflags!{
/// Additional socket options
pub struct SockFlag: c_int {
/// Set non-blocking mode on the new socket
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
SOCK_NONBLOCK;
/// Set close-on-exec on the new descriptor
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
SOCK_CLOEXEC;
/// Return `EPIPE` instead of raising `SIGPIPE`
#[cfg(target_os = "netbsd")]
SOCK_NOSIGPIPE;
/// For domains `AF_INET(6)`, only allow `connect(2)`, `sendto(2)`, or `sendmsg(2)`
/// to the DNS port (typically 53)
#[cfg(target_os = "openbsd")]
SOCK_DNS;
}
}
}
}

Expand Down Expand Up @@ -709,7 +726,9 @@ pub fn socket<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: SockType
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
{
Expand Down Expand Up @@ -754,7 +773,9 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: Sock
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
{
Expand Down Expand Up @@ -836,7 +857,9 @@ fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> {
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
{
Expand Down
66 changes: 66 additions & 0 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,69 @@ pub fn test_syscontrol() {
// requires root privileges
// connect(fd, &sockaddr).expect("connect failed");
}

/// Test non-blocking mode on new sockets via SockFlag::O_NONBLOCK
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
#[test]
pub fn test_sockflag_nonblock() {
use libc;
use nix::fcntl::{fcntl};
use nix::fcntl::FcntlArg::{F_GETFL};
use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag};

/* first, try without SockFlag::SOCK_NONBLOCK */
let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None)
.expect("socket failed");

let fcntl_res = fcntl(sock, F_GETFL).expect("fcntl failed");

assert!(fcntl_res & libc::O_NONBLOCK == 0);

/* next, try with SockFlag::SOCK_NONBLOCK */
let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::SOCK_NONBLOCK, None)
.expect("socket failed");

let fcntl_res = fcntl(sock, F_GETFL).expect("fcntl failed");

assert!(fcntl_res & libc::O_NONBLOCK == libc::O_NONBLOCK);
}

/// Test close-on-exec on new sockets via SockFlag::SOCK_CLOEXEC
#[cfg(any(target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
#[test]
pub fn test_sockflag_cloexec() {
use libc;
use nix::fcntl::{fcntl};
use nix::fcntl::FcntlArg::{F_GETFD};
use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag};

/* first, test without SockFlag::SOCK_CLOEXEC */
let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::empty(), None)
.expect("socket failed");

let fcntl_res = fcntl(sock, F_GETFD).expect("fcntl failed");

assert!(fcntl_res & libc::FD_CLOEXEC == 0);

/* next, test without SockFlag::SOCK_CLOEXEC */
let sock = socket(AddressFamily::Unix, SockType::Stream, SockFlag::SOCK_CLOEXEC, None)
.expect("socket failed");

let fcntl_res = fcntl(sock, F_GETFD).expect("fcntl failed");

assert!(fcntl_res & libc::FD_CLOEXEC == libc::FD_CLOEXEC);
}