mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50518 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a9ca74cd70
commit
8ff35b816a
3 changed files with 31 additions and 4 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
Sun May 17 12:13:33 2015 Eric Wong <e@80x24.org>
|
||||
|
||||
* 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@ruby-lang.org>
|
||||
|
||||
* load.c (loaded_feature_path): stop returning false negatives for
|
||||
|
|
|
@ -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;
|
||||
|
@ -484,12 +485,22 @@ cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
|
|||
int flags = 0;
|
||||
#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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue