[前][次][番号順一覧][スレッド一覧]

ruby-changes:21544

From: akr <ko1@a...>
Date: Tue, 1 Nov 2011 06:01:28 +0900 (JST)
Subject: [ruby-changes:21544] akr:r33593 (trunk): * ext/socket/socket.c (rsock_socketpair): use SOCK_CLOEXEC if

akr	2011-11-01 06:01:18 +0900 (Tue, 01 Nov 2011)

  New Revision: 33593

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33593

  Log:
    * ext/socket/socket.c (rsock_socketpair): use SOCK_CLOEXEC if
      available.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/socket.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 33592)
+++ ChangeLog	(revision 33593)
@@ -1,3 +1,8 @@
+Tue Nov  1 05:59:41 2011  Tanaka Akira  <akr@f...>
+
+	* ext/socket/socket.c (rsock_socketpair): use SOCK_CLOEXEC if
+	  available.
+
 Tue Nov  1 02:56:17 2011  NAKAMURA Usaku  <usa@r...>
 
 	* ruby.c (load_file_internal): convert the encoding of load path if
Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c	(revision 33592)
+++ ext/socket/socket.c	(revision 33593)
@@ -78,14 +78,35 @@
 #if defined HAVE_SOCKETPAIR
 
 static int
-rsock_socketpair(int domain, int type, int protocol, int sv[2])
+rsock_socketpair(int domain, int type0, int protocol, int sv[2])
 {
-    int ret;
+    int ret, 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
+
     ret = socketpair(domain, type, protocol, sv);
-    if (ret < 0 && (errno == EMFILE || errno == ENFILE)) {
-        rb_gc();
-        ret = socketpair(domain, type, protocol, sv);
+    if (ret < 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();
+            ret = socketpair(domain, type, protocol, sv);
+        }
     }
 
     return ret;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]