From d2b5f7223abe4825410392a54ac34dc973465faf Mon Sep 17 00:00:00 2001 From: Ross Lagerwall Date: Sat, 11 Feb 2012 17:23:17 +0200 Subject: [PATCH 1/2] Make uses of open() close-on-exec safe by introducing evutil_open_closeonexec. In a multi-process/threaded environment, opening fds internally without the close-on-exec flag could leak fds to child processes. --- arc4random.c | 4 ++-- devpoll.c | 2 +- evutil.c | 23 ++++++++++++++++++++++- util-internal.h | 2 ++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/arc4random.c b/arc4random.c index ee2b73a2..cabc46f4 100644 --- a/arc4random.c +++ b/arc4random.c @@ -260,7 +260,7 @@ arc4_seed_proc_sys_kernel_random_uuid(void) unsigned char entropy[64]; int bytes, n, i, nybbles; for (bytes = 0; bytes Date: Sat, 11 Feb 2012 21:17:18 -0500 Subject: [PATCH 2/2] Tweak the evutil_open_closeonexec patch to work on windows, old unixes. Windows doesn't have a mode_t as far as I can tell. Some unixes, iirc, don't like three-argument open without O_CREAT. --- evutil.c | 7 +++++-- util-internal.h | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/evutil.c b/evutil.c index 7e392790..b6634591 100644 --- a/evutil.c +++ b/evutil.c @@ -88,7 +88,7 @@ #endif int -evutil_open_closeonexec(const char *pathname, int flags, mode_t mode) +evutil_open_closeonexec(const char *pathname, int flags, unsigned mode) { int fd; @@ -96,7 +96,10 @@ evutil_open_closeonexec(const char *pathname, int flags, mode_t mode) flags |= O_CLOEXEC; #endif - fd = open(pathname, flags, mode); + if (flags & O_CREAT) + fd = open(pathname, flags, (mode_t)mode); + else + fd = open(pathname, flags); if (fd < 0) return -1; diff --git a/util-internal.h b/util-internal.h index d09aa471..47fe962d 100644 --- a/util-internal.h +++ b/util-internal.h @@ -161,7 +161,10 @@ char EVUTIL_TOLOWER(char c); #define EVUTIL_UPCAST(ptr, type, field) \ ((type *)(((char*)(ptr)) - evutil_offsetof(type, field))) -int evutil_open_closeonexec(const char *pathname, int flags, mode_t mode); +/* As open(pathname, flags, mode), except that the file is always opened with + * the close-on-exec flag set. (And the mode argument is mandatory.) + */ +int evutil_open_closeonexec(const char *pathname, int flags, unsigned mode); int evutil_read_file(const char *filename, char **content_out, size_t *len_out, int is_binary);