diff options
author | Nick Mathewson <nickm@torproject.org> | 2012-02-11 21:20:47 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2012-02-11 21:20:47 -0500 |
commit | 279319766342c5d12c9f70716cab4d9d4895c94c (patch) | |
tree | 4efc9ada9ff733ca824313b53db24b8924a0367b /evutil.c | |
parent | a6503944ddf312c8270ac61a366502386aa1bd0c (diff) | |
parent | 03dce42dfa0380a96bdfc2e9c18b15318ec0ba5b (diff) | |
download | libevent-279319766342c5d12c9f70716cab4d9d4895c94c.tar.gz |
Merge remote-tracking branch 'origin/patches-2.0'
Diffstat (limited to 'evutil.c')
-rw-r--r-- | evutil.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -96,6 +96,30 @@ #define stat _stati64 #endif +int +evutil_open_closeonexec(const char *pathname, int flags, unsigned mode) +{ + int fd; + +#ifdef O_CLOEXEC + flags |= O_CLOEXEC; +#endif + + if (flags & O_CREAT) + fd = open(pathname, flags, (mode_t)mode); + else + fd = open(pathname, flags); + if (fd < 0) + return -1; + +#if !defined(O_CLOEXEC) && defined(FD_CLOEXEC) + if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) + return -1; +#endif + + return fd; +} + /** Read the contents of 'filename' into a newly allocated NUL-terminated string. Set *content_out to hold this string, and *len_out to hold its @@ -126,7 +150,7 @@ evutil_read_file(const char *filename, char **content_out, size_t *len_out, mode |= O_BINARY; #endif - fd = open(filename, mode); + fd = evutil_open_closeonexec(filename, mode, 0); if (fd < 0) return -1; if (fstat(fd, &st) || st.st_size < 0 || |