If an application using libtevent starts a new process the epoll file descriptor
is leaked to the new process if the event context is not freed explicitly. By
setting FD_CLOEXEC this is not needed anymore.
Signed-off-by: Simo Sorce <idra@samba.org>
static int epoll_init_ctx(struct epoll_event_context *epoll_ev)
{
epoll_ev->epoll_fd = epoll_create(64);
- epoll_ev->pid = getpid();
- talloc_set_destructor(epoll_ev, epoll_ctx_destructor);
if (epoll_ev->epoll_fd == -1) {
+ tevent_debug(epoll_ev->ev, TEVENT_DEBUG_FATAL,
+ "Failed to create epoll handle.\n");
return -1;
}
+
+ if (!ev_set_close_on_exec(epoll_ev->epoll_fd)) {
+ tevent_debug(epoll_ev->ev, TEVENT_DEBUG_WARNING,
+ "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
+ }
+
+ epoll_ev->pid = getpid();
+ talloc_set_destructor(epoll_ev, epoll_ctx_destructor);
+
return 0;
}
"Failed to recreate epoll handle after fork\n");
return;
}
+
+ if (!ev_set_close_on_exec(epoll_ev->epoll_fd)) {
+ tevent_debug(epoll_ev->ev, TEVENT_DEBUG_WARNING,
+ "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
+ }
+
epoll_ev->pid = getpid();
for (fde=epoll_ev->ev->fd_events;fde;fde=fde->next) {
epoll_add_event(epoll_ev, fde);
static void epoll_init_ctx(struct std_event_context *std_ev)
{
std_ev->epoll_fd = epoll_create(64);
+ if (std_ev->epoll_fd == -1) {
+ tevent_debug(std_ev->ev, TEVENT_DEBUG_FATAL,
+ "Failed to create epoll handle.\n");
+ return;
+ }
+
+ if (!ev_set_close_on_exec(std_ev->epoll_fd)) {
+ tevent_debug(std_ev->ev, TEVENT_DEBUG_WARNING,
+ "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
+ }
+
std_ev->pid = getpid();
talloc_set_destructor(std_ev, epoll_ctx_destructor);
}
"Failed to recreate epoll handle after fork\n");
return;
}
+
+ if (!ev_set_close_on_exec(std_ev->epoll_fd)) {
+ tevent_debug(std_ev->ev, TEVENT_DEBUG_WARNING,
+ "Failed to set close-on-exec, file descriptor may be leaked to children.\n");
+ }
+
std_ev->pid = getpid();
for (fde=std_ev->ev->fd_events;fde;fde=fde->next) {
epoll_add_event(std_ev, fde);
return fcntl( fd, F_SETFL, val);
#undef FLAG_TO_SET
}
+
+bool ev_set_close_on_exec(int fd)
+{
+#ifdef FD_CLOEXEC
+ int val;
+
+ val = fcntl(fd, F_GETFD, 0);
+ if (val >= 0) {
+ val |= FD_CLOEXEC;
+ val = fcntl(fd, F_SETFD, val);
+ if (val != -1) {
+ return true;
+ }
+ }
+#endif
+ return false;
+}
const char **ev_str_list_add(const char **list, const char *s);
int ev_set_blocking(int fd, bool set);
size_t ev_str_list_length(const char **list);
+bool ev_set_close_on_exec(int fd);
/* Defined here so we can build against older talloc versions that don't
* have this define yet. */