[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] [PATCH] Make evutil_read_file() close-on-exec safe.



In a multi-process/threaded environment, ev_util_read_file()
could leak fds to child processes when not using O_CLOEXEC/FD_CLOEXEC.
---
 evutil.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/evutil.c b/evutil.c
index 306f037..2bdacaa 100644
--- a/evutil.c
+++ b/evutil.c
@@ -125,10 +125,16 @@ evutil_read_file(const char *filename, char **content_out, size_t *len_out,
 	if (is_binary)
 		mode |= O_BINARY;
 #endif
+#ifdef O_CLOEXEC
+	mode |= O_CLOEXEC;
+#endif
 
 	fd = open(filename, mode);
 	if (fd < 0)
 		return -1;
+#if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
+	fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
 	if (fstat(fd, &st) || st.st_size < 0 ||
 	    st.st_size > EV_SSIZE_MAX-1 ) {
 		close(fd);
-- 
1.7.5.4

***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.