From 509ed0d94fea08f9e08767ddfa8564b9f9237c88 Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 27 Oct 2011 21:07:23 +0000 Subject: [PATCH] * ext/sdbm/_sdbm.c (sdbm_prep): set FD_CLOEXEC flags for file descriptors. (fd_set_cloexec): new function. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ ext/sdbm/_sdbm.c | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8c1e884df0..e12dba5d3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Oct 28 06:06:08 2011 Tanaka Akira + + * ext/sdbm/_sdbm.c (sdbm_prep): set FD_CLOEXEC flags for file + descriptors. + (fd_set_cloexec): new function. + Fri Oct 28 03:01:27 2011 Aaron Patterson * vm_insnhelper.c (vm_call_cfunc): adding back useless hack. For some diff --git a/ext/sdbm/_sdbm.c b/ext/sdbm/_sdbm.c index 64cda29437..07903ca9b1 100644 --- a/ext/sdbm/_sdbm.c +++ b/ext/sdbm/_sdbm.c @@ -178,6 +178,29 @@ sdbm_open(register char *file, register int flags, register int mode) return db; } +static int +fd_set_cloexec(int fd) +{ + /* MinGW don't have F_GETFD and FD_CLOEXEC. [ruby-core:40281] */ +#ifdef F_GETFD + int flags, ret; + flags = fcntl(fd, F_GETFD); /* should not fail except EBADF. */ + if (flags == -1) { + return -1; + } + if (2 < fd) { + if (!(flags & FD_CLOEXEC)) { + flags |= FD_CLOEXEC; + ret = fcntl(fd, F_SETFD, flags); + if (ret == -1) { + return -1; + } + } + } +#endif + return 0; +} + DBM * sdbm_prep(char *dirname, char *pagname, int flags, int mode) { @@ -209,7 +232,9 @@ sdbm_prep(char *dirname, char *pagname, int flags, int mode) flags |= O_BINARY; if ((db->pagf = open(pagname, flags, mode)) == -1) goto err; + if (fd_set_cloexec(db->pagf) == -1) goto err; if ((db->dirf = open(dirname, flags, mode)) == -1) goto err; + if (fd_set_cloexec(db->dirf) == -1) goto err; /* * need the dirfile size to establish max bit number. */