Feature #11138 ยป 0001-ext-socket-init.c-use-SOCK_NONBLOCK-if-available.patch
| ext/socket/init.c | ||
|---|---|---|
|
}
|
||
|
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;
|
||
| ... | ... | |
|
#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;
|
||
|
}
|
||
| ... | ... | |
|
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;
|
||
|
}
|
||
| ... | ... | |
|
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:
|
||
| ... | ... | |
|
rb_sys_fail("accept(2)");
|
||
|
}
|
||
|
rb_update_max_fd(fd2);
|
||
|
make_fd_nonblock(fd2);
|
||
|
return rsock_init_sock(rb_obj_alloc(klass), fd2);
|
||
|
}
|
||
| ... | ... | |
|
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
|
||
| test/socket/test_nonblock.rb | ||
|---|---|---|
|
begin
|
||
|
require "socket"
|
||
|
require "io/nonblock"
|
||
|
rescue LoadError
|
||
|
end
|
||
| ... | ... | |
|
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
|
||
|
-
|
||