diff --git a/librb/include/rb_commio.h b/librb/include/rb_commio.h index b7a37db8..0462442d 100644 --- a/librb/include/rb_commio.h +++ b/librb/include/rb_commio.h @@ -118,6 +118,8 @@ void rb_note(rb_fde_t *, const char *); #define RB_SSL_CERTFP_LEN_SHA512 64 int rb_set_nb(rb_fde_t *); +int rb_set_cloexec(rb_fde_t *); +int rb_clear_cloexec(rb_fde_t *); int rb_set_buffers(rb_fde_t *, int); int rb_get_sockerr(rb_fde_t *); diff --git a/librb/src/commio.c b/librb/src/commio.c index c3f4d122..9d9bfaee 100644 --- a/librb/src/commio.c +++ b/librb/src/commio.c @@ -260,6 +260,50 @@ rb_set_nb(rb_fde_t *F) return 1; } +int +rb_set_cloexec(rb_fde_t *F) +{ +#ifdef _WIN32 + SetHandleInformation((HANDLE) F->fd, HANDLE_FLAG_INHERIT, 0); +#else + int res; + rb_platform_fd_t fd; + if(F == NULL) + return 0; + fd = F->fd; + + res = fcntl(fd, F_GETFD, NULL); + if(res == -1) + return 0; + if(fcntl(fd, F_SETFD, res | FD_CLOEXEC) == -1) + return 0; + + return 1; +#endif +} + +int +rb_clear_cloexec(rb_fde_t *F) +{ +#ifdef _WIN32 + SetHandleInformation((HANDLE) F->fd, HANDLE_FLAG_INHERIT, 1); +#else + int res; + rb_platform_fd_t fd; + if(F == NULL) + return 0; + fd = F->fd; + + res = fcntl(fd, F_GETFD, NULL); + if(res == -1) + return 0; + if(fcntl(fd, F_SETFD, res & ~FD_CLOEXEC) == -1) + return 0; + + return 1; +#endif +} + /* * rb_settimeout() - set the socket timeout * diff --git a/librb/src/export-syms.txt b/librb/src/export-syms.txt index 0c479b6d..f63869b8 100644 --- a/librb/src/export-syms.txt +++ b/librb/src/export-syms.txt @@ -13,6 +13,7 @@ rb_bh_usage rb_bh_usage_all rb_bind rb_checktimeouts +rb_clear_cloexec rb_clear_patricia rb_close rb_connect_sockaddr @@ -153,6 +154,7 @@ rb_sctp_bindx rb_select rb_send_fd_buf rb_set_buffers +rb_set_cloexec rb_set_nb rb_set_time rb_set_type