diff --git a/include/haproxy/fd.h b/include/haproxy/fd.h index 8bf30cd25..cf2d02b78 100644 --- a/include/haproxy/fd.h +++ b/include/haproxy/fd.h @@ -59,6 +59,14 @@ extern volatile int ha_used_fds; // Number of FDs we're currently using void fd_delete(int fd); void _fd_delete_orphan(int fd); +/* makes the new fd non-blocking and clears all other O_* flags; + * this is meant to be used on new FDs. Returns -1 on failure. + */ +int fd_set_nonblock(int fd); + +/* makes the fd close-on-exec; returns -1 on failure. */ +int fd_set_cloexec(int fd); + /* * Take over a FD belonging to another thread. * Returns 0 on success, and -1 on failure. diff --git a/src/fd.c b/src/fd.c index c983b8c9a..529cba739 100644 --- a/src/fd.c +++ b/src/fd.c @@ -363,6 +363,27 @@ void fd_delete(int fd) _fd_delete_orphan(fd); } +/* makes the new fd non-blocking and clears all other O_* flags; + * this is meant to be used on new FDs. Returns -1 on failure. + */ +int fd_set_nonblock(int fd) +{ + int ret = fcntl(fd, F_SETFL, O_NONBLOCK); + + return ret; +} + +/* sets the close-on-exec flag on fd; returns -1 on failure. */ +int fd_set_cloexec(int fd) +{ + int flags, ret; + + flags = fcntl(fd, F_GETFD); + flags |= FD_CLOEXEC; + ret = fcntl(fd, F_SETFD, flags); + return ret; +} + /* * Take over a FD belonging to another thread. * unexpected_conn is the expected owner of the fd.