librb: add rb_{set,clear}_cloexec

This commit is contained in:
Doug Freed 2020-12-13 08:38:25 +00:00
parent 4ce7aa6f70
commit 34a2afbc28
3 changed files with 48 additions and 0 deletions

View file

@ -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 *);

View file

@ -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
*

View file

@ -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