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

Non-blocking on macOS / iOS #861

Closed
kristate opened this issue Feb 21, 2018 · 3 comments
Closed

Non-blocking on macOS / iOS #861

kristate opened this issue Feb 21, 2018 · 3 comments
Labels

Comments

@kristate
Copy link

Thanks to everyone for your hard work.

nix::sys::socket::socket provides a fourth parameter that ends-up calling fcntl()and friends but seemingly does not support macos.

What is the best way to open a non-blocking socket on macos/ios using nix?

ref: https://github.com/nix-rust/nix/blob/v0.10.0/src/sys/socket/mod.rs#L684-L718

@Susurrus
Copy link
Contributor

First off, your welcome! Hopefully even with this missing/unknown functionality you've found nix useful.

Unfortunately nix isn't the best place to start looking for answers to questions like these. nix doesn't have the manpower to support these questions, instead outsourcing that work to existing documentation for these C APIs. Please search for other sources that provide the documentation you need. It will likely be in C, but once you know the C equivalent, translating that into Rust using nix is usually straightforward.

Additionally, if the functionality you need isn't available in nix, we welcome PRs for POSIX/libc APIs. We do not have the manpower to implement features either, so requests such as those will just sit unanswered.

@kristate
Copy link
Author

@Susurrus I understand that you are trying to be professional in your answer. I know there is a lot of work to be done with both libc and and nix and I am here to help. Please reopen this issue -- as it is an issue:

pub fn socket<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: SockType, flags: SockFlag, protocol: T) -> Result<RawFd> {

has a giant cfg gate on it that doesn't support macOS even though macOS does support OFlag::O_NONBLOCK

In 0.9.0 it was okay to use SockFlag:: SOCK_NONBLOCK on macOS, but 0.10.0 changes this.

I will attempt to write a PR and look for your help. Thanks.

For anyone who hits this via google, etc. you will have to set it manually until this gets resolved:

use libc;
use nix;
use std::io;

/*
cvt from mio lib
https://github.com/carllerche/mio/blob/91f0832036303c7f83c286e049e31be29c43b519/src/sys/unix/mod.rs#L71
*/

trait IsMinusOne {
  fn is_minus_one(&self) -> bool;
}

impl IsMinusOne for i32 {
  fn is_minus_one(&self) -> bool { *self == -1 }
}
impl IsMinusOne for isize {
  fn is_minus_one(&self) -> bool { *self == -1 }
}

fn cvt<T: IsMinusOne>(t: T) -> ::io::Result<T> {
  use std::io;

  if t.is_minus_one() {
    Err(io::Error::last_os_error())
  } else {
    Ok(t)
  }
}

pub fn set_nonblock(fd: libc::c_int) -> io::Result<()> {
  unsafe {
    let flags = libc::fcntl(fd, libc::F_GETFL);
    cvt(libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK)).map(|_|())
  }
}

pub fn set_cloexec(fd: libc::c_int) -> io::Result<()> {
  unsafe {
    let flags = libc::fcntl(fd, libc::F_GETFD);
    cvt(libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC)).map(|_| ())
  }
}

/* setup your socket */
let fd: RawFd = socket( AddressFamily::System,
                        SockType::Datagram,
                        SockFlag::empty(),
                        SockProtocol::KextControl )?;

set_nonblock(fd)?;
set_cloexec(fd)?;

@Susurrus
Copy link
Contributor

You weren't clear in your original question, both here and in the other PR or issue you commented on, so I wasn't sure if a) it should work on mac/ios and doesn't or b) you weren't sure how these APIs worked at all. I assumed it was b because of the way you phrased it originally. If it's indeed a), especially if it's a feature regression, we'd definitely welcome a PR fixing it! No need to reopen this issue, however, if you're going to file a PR though.

@Susurrus Susurrus added the A-bug label Feb 21, 2018
kristate added a commit to kristate/nix that referenced this issue Feb 22, 2018
kristate added a commit to kristate/nix that referenced this issue Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants