Commit 03847f9c authored by Kenton Varda's avatar Kenton Varda

Tweak: Use FIOCLEX and FIONBIO ioctls() when available.

These avoid the need for a read/modify/write.
parent 626a8c8a
...@@ -45,25 +45,35 @@ ...@@ -45,25 +45,35 @@
#include <set> #include <set>
#include <poll.h> #include <poll.h>
#include <limits.h> #include <limits.h>
#include <sys/ioctl.h>
namespace kj { namespace kj {
namespace { namespace {
void setNonblocking(int fd) { void setNonblocking(int fd) {
#ifdef FIONBIO
int opt = 1;
KJ_SYSCALL(ioctl(fd, FIONBIO, &opt));
#else
int flags; int flags;
KJ_SYSCALL(flags = fcntl(fd, F_GETFL)); KJ_SYSCALL(flags = fcntl(fd, F_GETFL));
if ((flags & O_NONBLOCK) == 0) { if ((flags & O_NONBLOCK) == 0) {
KJ_SYSCALL(fcntl(fd, F_SETFL, flags | O_NONBLOCK)); KJ_SYSCALL(fcntl(fd, F_SETFL, flags | O_NONBLOCK));
} }
#endif
} }
void setCloseOnExec(int fd) { void setCloseOnExec(int fd) {
#ifdef FIOCLEX
KJ_SYSCALL(ioctl(fd, FIOCLEX));
#else
int flags; int flags;
KJ_SYSCALL(flags = fcntl(fd, F_GETFD)); KJ_SYSCALL(flags = fcntl(fd, F_GETFD));
if ((flags & FD_CLOEXEC) == 0) { if ((flags & FD_CLOEXEC) == 0) {
KJ_SYSCALL(fcntl(fd, F_SETFD, flags | FD_CLOEXEC)); KJ_SYSCALL(fcntl(fd, F_SETFD, flags | FD_CLOEXEC));
} }
#endif
} }
static constexpr uint NEW_FD_FLAGS = static constexpr uint NEW_FD_FLAGS =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment