Why does sys::unix::fd::FileDesc::set_nonblocking use FIONBIO instead of O_NONBLOCK?

I have been looking at sys::unix::fd extensively as I am forced to reimplement parts of it for a project.

One thing that stuck out to me is the use of ioctl FIONBIO to set nonblocking mode on Linux specifically.

From my understanding, FIONBIO is old and was superceded by POSIX in the form of O_NONBLOCK

This is counter to how set_cloexec is implemented, which uses the newer (and POSIX) FD_CLOEXEC

Is there a rationale for this?

1 Like

I think this is strange, especially as it does use O_NONBLOCK on other UNIX-ish operating systems. As far as I know FIONBIO are and O_NONBLOCK are handled exactly the same on Linux.

If this is intentional then I think it's because it's one system call instead of two. Requiring only one round-trip to the kernel is a little bit faster.

On other Unix FIONBIO might do something different so the same "trick" cannot be used.

Looks like this indeed was the reasoning when the change was done: https://github.com/rust-lang/rust/commit/efeb42be2837842d1beb47b51bb693c7474aba3d


Ah. I suspected it was just to avoid extraneous syscalls, but the lack of comments made it unclear. Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.