summaryrefslogtreecommitdiff
path: root/ext/socket/init.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-10-31 14:38:50 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-10-31 14:38:50 +0000
commit1a8fbff3dbbddf3c0d4e58d0f7da9ad1eb0e2025 (patch)
treef750c561f90071667d754965d5c9dbda5d68a7a6 /ext/socket/init.c
parent6a96646de2bbbe24f8d6c0bd3c31374012ae419e (diff)
* ext/socket/init.c (rsock_socket): use SOCK_CLOEXEC if available.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33589 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/init.c')
-rw-r--r--ext/socket/init.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 70c92d7452..2f6b6eef82 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -240,19 +240,38 @@ rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type
}
int
-rsock_socket(int domain, int type, int proto)
+rsock_socket(int domain, int type0, int proto)
{
- int fd;
+ int fd, type;
+
+#ifdef SOCK_CLOEXEC
+ static int try_sock_cloexec = 1;
+ if (try_sock_cloexec)
+ type = type0|SOCK_CLOEXEC;
+ else
+ type = type0;
+ retry_without_sock_cloexec:;
+#else
+ type = type0;
+#endif
fd = socket(domain, type, proto);
if (fd < 0) {
+#ifdef SOCK_CLOEXEC
+ /* SOCK_CLOEXEC is available since Linux 2.6.27. Linux 2.6.18 fails with EINVAL */
+ if (try_sock_cloexec && errno == EINVAL) {
+ try_sock_cloexec = 0;
+ type = type0;
+ goto retry_without_sock_cloexec;
+ }
+#endif
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
fd = socket(domain, type, proto);
}
}
if (0 <= fd)
- rb_fd_fix_cloexec(fd);
+ rb_update_max_fd(fd);
return fd;
}