Skip to content

Commit

Permalink
Fall back to fcntl if ioctl fails with ENOTTY when setting non-blocki…
Browse files Browse the repository at this point in the history
…ng mode.
  • Loading branch information
chriskohlhoff committed Nov 8, 2022
1 parent e519168 commit 42e1c46
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions asio/include/asio/detail/impl/descriptor_ops.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,18 @@ int close(int d, state_type& state, asio::error_code& ec)
::fcntl(d, F_SETFL, flags & ~O_NONBLOCK);
#else // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
ioctl_arg_type arg = 0;
# if defined(ENOTTY)
result = ::ioctl(d, FIONBIO, &arg);
get_last_error(ec, result < 0);
if (ec.value() == ENOTTY)
{
int flags = ::fcntl(d, F_GETFL, 0);
if (flags >= 0)
::fcntl(d, F_SETFL, flags & ~O_NONBLOCK);
}
# else // defined(ENOTTY)
::ioctl(d, FIONBIO, &arg);
# endif // defined(ENOTTY)
#endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)
state &= ~non_blocking;

Expand Down Expand Up @@ -103,6 +114,19 @@ bool set_user_non_blocking(int d, state_type& state,
ioctl_arg_type arg = (value ? 1 : 0);
int result = ::ioctl(d, FIONBIO, &arg);
get_last_error(ec, result < 0);
# if defined(ENOTTY)
if (ec.value() == ENOTTY)
{
int result = ::fcntl(d, F_GETFL, 0);
get_last_error(ec, result < 0);
if (result >= 0)
{
int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
result = ::fcntl(d, F_SETFL, flag);
get_last_error(ec, result < 0);
}
}
# endif // defined(ENOTTY)
#endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)

if (result >= 0)
Expand Down Expand Up @@ -153,6 +177,19 @@ bool set_internal_non_blocking(int d, state_type& state,
ioctl_arg_type arg = (value ? 1 : 0);
int result = ::ioctl(d, FIONBIO, &arg);
get_last_error(ec, result < 0);
# if defined(ENOTTY)
if (ec.value() == ENOTTY)
{
int result = ::fcntl(d, F_GETFL, 0);
get_last_error(ec, result < 0);
if (result >= 0)
{
int flag = (value ? (result | O_NONBLOCK) : (result & ~O_NONBLOCK));
result = ::fcntl(d, F_SETFL, flag);
get_last_error(ec, result < 0);
}
}
# endif // defined(ENOTTY)
#endif // defined(__SYMBIAN32__) || defined(__EMSCRIPTEN__)

if (result >= 0)
Expand Down

0 comments on commit 42e1c46

Please sign in to comment.