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

ruby-changes:38437

From: normal <ko1@a...>
Date: Sun, 17 May 2015 14:56:27 +0900 (JST)
Subject: [ruby-changes:38437] normal:r50518 (trunk): ext/socket/init.c: use SOCK_NONBLOCK if available

normal	2015-05-17 14:56:07 +0900 (Sun, 17 May 2015)

  New Revision: 50518

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

  Log:
    ext/socket/init.c: use SOCK_NONBLOCK if available
    
    This saves a system call by allowing us to use SOCK_NONBLOCK in
    Linux when accept4 is available.
    
    Note: I do not agree accept_nonblock should always make accepted
    sockets non-blocking, and will propose a future API to allow
    controlling whether accepted sockets are non-blocking or not
    regardless of how they were created.
    
    * ext/socket/init.c (cloexec_accept): support nonblock flag and
      use SOCK_NONBLOCK if possible
    * ext/socket/init.c (rsock_s_accept_nonblock): update cloexec_accept call
    * ext/socket/init.c (accept_blocking): ditto for blocking
    * test/socket/test_nonblock.rb: check nonblock? on accepted socket
      [Feature #11138]

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/init.c
    trunk/test/socket/test_nonblock.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50517)
+++ ChangeLog	(revision 50518)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun May 17 12:13:33 2015  Eric Wong  <e@8...>
+
+	* ext/socket/init.c (cloexec_accept): support nonblock flag and
+	  use SOCK_NONBLOCK if possible
+	* ext/socket/init.c (rsock_s_accept_nonblock):
+	  update cloexec_accept call
+	* ext/socket/init.c (accept_blocking): ditto for blocking
+	* test/socket/test_nonblock.rb: check nonblock? on accepted socket
+	  [Feature #11138]
+
 Sun May 17 03:58:59 2015  Aaron Patterson <tenderlove@r...>
 
 	* load.c (loaded_feature_path): stop returning false negatives for
Index: ext/socket/init.c
===================================================================
--- ext/socket/init.c	(revision 50517)
+++ ext/socket/init.c	(revision 50518)
@@ -471,7 +471,8 @@ make_fd_nonblock(int fd) https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L471
 }
 
 static int
-cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
+cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len,
+	       int nonblock)
 {
     int ret;
     socklen_t len0 = 0;
@@ -485,11 +486,21 @@ cloexec_accept(int socket, struct sockad https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L486
 #ifdef SOCK_CLOEXEC
         flags |= SOCK_CLOEXEC;
 #endif
+#ifdef SOCK_NONBLOCK
+        if (nonblock) {
+            flags |= SOCK_NONBLOCK;
+        }
+#endif
         ret = accept4(socket, address, address_len, flags);
         /* accept4 is available since Linux 2.6.28, glibc 2.10. */
         if (ret != -1) {
             if (ret <= 2)
                 rb_maygvl_fd_fix_cloexec(ret);
+#ifndef SOCK_NONBLOCK
+            if (nonblock) {
+                make_fd_nonblock(ret);
+            }
+#endif
             if (address_len && len0 < *address_len) *address_len = len0;
             return ret;
         }
@@ -503,6 +514,9 @@ cloexec_accept(int socket, struct sockad https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L514
     if (ret == -1) return -1;
     if (address_len && len0 < *address_len) *address_len = len0;
     rb_maygvl_fd_fix_cloexec(ret);
+    if (nonblock) {
+        make_fd_nonblock(ret);
+    }
     return ret;
 }
 
@@ -521,7 +535,7 @@ rsock_s_accept_nonblock(int argc, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L535
 
     rb_secure(3);
     rb_io_set_nonblock(fptr);
-    fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len);
+    fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len, 1);
     if (fd2 < 0) {
 	switch (errno) {
 	  case EAGAIN:
@@ -539,7 +553,6 @@ rsock_s_accept_nonblock(int argc, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L553
         rb_sys_fail("accept(2)");
     }
     rb_update_max_fd(fd2);
-    make_fd_nonblock(fd2);
     return rsock_init_sock(rb_obj_alloc(klass), fd2);
 }
 
@@ -553,7 +566,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L566
 accept_blocking(void *data)
 {
     struct accept_arg *arg = data;
-    return (VALUE)cloexec_accept(arg->fd, arg->sockaddr, arg->len);
+    return (VALUE)cloexec_accept(arg->fd, arg->sockaddr, arg->len, 0);
 }
 
 VALUE
Index: test/socket/test_nonblock.rb
===================================================================
--- test/socket/test_nonblock.rb	(revision 50517)
+++ test/socket/test_nonblock.rb	(revision 50518)
@@ -1,5 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/test/socket/test_nonblock.rb#L1
 begin
   require "socket"
+  require "io/nonblock"
 rescue LoadError
 end
 
@@ -24,6 +25,9 @@ class TestSocketNonblock < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/socket/test_nonblock.rb#L25
       s, sockaddr = serv.accept_nonblock
     end
     assert_equal(Socket.unpack_sockaddr_in(c.getsockname), Socket.unpack_sockaddr_in(sockaddr))
+    if s.respond_to?(:nonblock?)
+      assert s.nonblock?, 'accepted socket is non-blocking'
+    end
   ensure
     serv.close if serv
     c.close if c

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

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