Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set close-on-exec flagas to avoid FD leaks to subprocesses #220

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ AC_CHECK_FUNCS([clock_gettime])
AC_CHECK_LIB([socket], [socket])
AC_CHECK_FUNCS([epoll_create], [AC_DEFINE([HAVE_EPOLL])])
AC_CHECK_FUNCS([kqueue], [AC_DEFINE([HAVE_KQUEUE])])
AC_CHECK_FUNCS([accept4], [AC_DEFINE([HAVE_ACCEPT4])])

dnl Check if struct sockaddr contains sa_len member
AC_CHECK_MEMBERS([struct sockaddr.sa_len], [], [], [
Expand Down Expand Up @@ -209,4 +210,3 @@ AC_CONFIG_MACRO_DIR([m4])

AC_OUTPUT([Makefile man/Makefile libdill.pc])
cp confdefs.h config.h

2 changes: 1 addition & 1 deletion epoll.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int dill_ctx_pollset_init(struct dill_ctx_pollset *ctx) {
/* Changelist is empty. */
ctx->changelist = DILL_ENDLIST;
/* Create the kernel-side pollset. */
ctx->efd = epoll_create(1);
ctx->efd = epoll_create1(EPOLL_CLOEXEC);
if(dill_slow(ctx->efd < 0)) {err = errno; goto error2;}
return 0;
error2:
Expand Down
33 changes: 31 additions & 2 deletions fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

*/

#ifdef HAVE_ACCEPT4
#define _GNU_SOURCE
#include <sys/socket.h>
#endif

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -127,12 +132,27 @@ int dill_fd_connect(int s, const struct sockaddr *addr, socklen_t addrlen,
return 0;
}

#ifdef HAVE_ACCEPT4
#define _dill_accept(fd, addr, addrlen) accept4((fd), (addr), (addrlen), SOCK_CLOEXEC)
#else
int _dill_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
int as = accept(sockfd, addr, addrlen);
if(dill_fast(as >= 0)) {
int fd_flags = fcntl(as, F_GETFD);
if (dill_fast(fd_flags != -1)) {
fcntl(as, F_SETFD, fd_flags | FD_CLOEXEC);
}
}
return as;
}
#endif

int dill_fd_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
int64_t deadline) {
int as;
while(1) {
/* Try to accept new connection synchronously. */
as = accept(s, addr, addrlen);
as = _dill_accept(s, addr, addrlen);
if(dill_fast(as >= 0))
break;
/* If connection was aborted by the peer grab the next one. */
Expand Down Expand Up @@ -399,8 +419,18 @@ void dill_fd_close(int s) {
}

int dill_fd_own(int s) {
#ifdef F_DUPFD_CLOEXEC
int n = fcntl(s, F_DUPFD_CLOEXEC, 0);
#else
int fd_flags = fcntl(s, F_GETFD);
int n = dup(s);
#endif
if(dill_slow(n < 0)) return -1;
#ifndef F_DUPFD_CLOEXEC
if (dill_fast(fd_flags != -1)) {
fcntl(n, F_SETFD, fd_flags);
}
#endif
dill_fd_close(s);
return n;
}
Expand All @@ -426,4 +456,3 @@ int dill_fd_check(int s, int type, int family1, int family2, int listening) {
errno = EINVAL; return -1;}
return 0;
}