aboutsummaryrefslogtreecommitdiffstats
path: root/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'io.c')
-rw-r--r--io.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/io.c b/io.c
index bde6ff4a36..fad64cafe8 100644
--- a/io.c
+++ b/io.c
@@ -205,7 +205,23 @@ int
rb_cloexec_dup(int oldfd)
{
int ret;
+
+#ifdef F_DUPFD_CLOEXEC
+ static int try_fcntl = 1;
+ if (try_fcntl) {
+ ret = fcntl(oldfd, F_DUPFD_CLOEXEC, 0);
+ /* F_DUPFD_CLOEXEC is available since Linux 2.6.24. Linux 2.6.18 fails with EINVAL */
+ if (ret == -1 && errno == EINVAL) {
+ try_fcntl = 0;
+ ret = dup(oldfd);
+ }
+ }
+ else {
+ ret = dup(oldfd);
+ }
+#else
ret = dup(oldfd);
+#endif
if (ret == -1) return -1;
fd_set_cloexec(ret);
return ret;