dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] ext/socket/init.c: use SOCK_NONBLOCK if available
Date: Mon, 11 May 2015 20:48:50 +0000	[thread overview]
Message-ID: <1431377330-23586-1-git-send-email-e@80x24.org> (raw)

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
---
 ext/socket/init.c            | 21 +++++++++++++++++----
 test/socket/test_nonblock.rb |  4 ++++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/ext/socket/init.c b/ext/socket/init.c
index 50d3f86..317dd67 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -471,7 +471,8 @@ make_fd_nonblock(int fd)
 }
 
 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 sockaddr *address, socklen_t *address_len)
 #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 sockaddr *address, socklen_t *address_len)
     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 *argv, VALUE klass, rb_io_t *fptr,
 
     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 *argv, VALUE klass, rb_io_t *fptr,
         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
 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
diff --git a/test/socket/test_nonblock.rb b/test/socket/test_nonblock.rb
index 98de00e..5268252 100644
--- a/test/socket/test_nonblock.rb
+++ b/test/socket/test_nonblock.rb
@@ -1,5 +1,6 @@
 begin
   require "socket"
+  require "io/nonblock"
 rescue LoadError
 end
 
@@ -24,6 +25,9 @@ class TestSocketNonblock < Test::Unit::TestCase
       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
-- 
EW


                 reply	other threads:[~2015-05-11 20:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1431377330-23586-1-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).