flexnbd read/write: Switch to a non-blocking connect() to allow us to time these out
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "nbdtypes.h"
|
||||
#include "ioutil.h"
|
||||
#include "sockutil.h"
|
||||
#include "util.h"
|
||||
#include "serve.h"
|
||||
|
||||
@@ -18,14 +19,14 @@ int socket_connect(struct sockaddr* to, struct sockaddr* from)
|
||||
if (NULL != from) {
|
||||
if ( 0 > bind(fd, from, sizeof(struct sockaddr_in6)) ){
|
||||
warn( "bind() failed");
|
||||
close( fd );
|
||||
FATAL_IF_NEGATIVE( close( fd ), SHOW_ERRNO( "FIXME" ) );
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if ( 0 > connect(fd, to, sizeof(struct sockaddr_in6)) ) {
|
||||
warn( "connect failed" );
|
||||
close( fd );
|
||||
if ( 0 > sock_try_connect( fd, to, sizeof( struct sockaddr_in6 ), 15 ) ) {
|
||||
warn( SHOW_ERRNO( "connect failed" ) );
|
||||
FATAL_IF_NEGATIVE( close( fd ), SHOW_ERRNO( "FIXME" ) );
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -113,9 +114,10 @@ void wait_for_data( int fd, int timeout_secs )
|
||||
|
||||
FD_ZERO( &fds );
|
||||
FD_SET( fd, &fds );
|
||||
selected = select( FD_SETSIZE,
|
||||
&fds, NULL, NULL,
|
||||
timeout_secs >=0 ? &tv : NULL );
|
||||
|
||||
selected = sock_try_select(
|
||||
FD_SETSIZE, &fds, NULL, NULL, timeout_secs >=0 ? &tv : NULL
|
||||
);
|
||||
|
||||
FATAL_IF( -1 == selected, "Select failed" );
|
||||
ERROR_IF( 0 == selected, "Timed out waiting for reply" );
|
||||
|
108
src/sockutil.c
108
src/sockutil.c
@@ -1,3 +1,10 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
#include "sockutil.h"
|
||||
#include "util.h"
|
||||
|
||||
@@ -58,6 +65,23 @@ int sock_set_tcp_nodelay(int fd, int optval)
|
||||
return setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval) );
|
||||
}
|
||||
|
||||
int sock_set_nonblock( int fd, int optval )
|
||||
{
|
||||
int flags = fcntl( fd, F_GETFL );
|
||||
|
||||
if ( flags == -1 ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ( optval ) {
|
||||
flags = flags | O_NONBLOCK;
|
||||
} else {
|
||||
flags = flags & (~O_NONBLOCK);
|
||||
}
|
||||
|
||||
return fcntl( fd, F_SETFL, flags );
|
||||
}
|
||||
|
||||
int sock_try_bind( int fd, const struct sockaddr* sa )
|
||||
{
|
||||
int bind_result;
|
||||
@@ -102,3 +126,87 @@ int sock_try_bind(int fd, const struct sockaddr* sa)
|
||||
return bind_result;
|
||||
}
|
||||
|
||||
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
|
||||
{
|
||||
int result;
|
||||
|
||||
do {
|
||||
result = select(nfds, readfds, writefds, exceptfds, timeout);
|
||||
if ( errno != EINTR ) {
|
||||
break;
|
||||
}
|
||||
|
||||
} while ( result == -1 );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait )
|
||||
{
|
||||
fd_set fds;
|
||||
struct timeval tv = { wait, 0 };
|
||||
int result = 0;
|
||||
|
||||
if ( sock_set_nonblock( fd, 1 ) == -1 ) {
|
||||
warn( SHOW_ERRNO( "Failed to set socket non-blocking for connect()" ) );
|
||||
return connect( fd, to, addrlen );
|
||||
}
|
||||
|
||||
FD_ZERO( &fds );
|
||||
FD_SET( fd, &fds );
|
||||
|
||||
do {
|
||||
result = connect( fd, to, addrlen );
|
||||
|
||||
if ( result == -1 ) {
|
||||
switch( errno ) {
|
||||
case EINPROGRESS:
|
||||
result = 0;
|
||||
break; /* success */
|
||||
case EAGAIN:
|
||||
case EINTR:
|
||||
break; /* Try again */
|
||||
default:
|
||||
warn( SHOW_ERRNO( "Failed to connect()") );
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
} while ( result == -1 );
|
||||
|
||||
if ( -1 == sock_try_select( FD_SETSIZE, NULL, &fds, NULL, &tv) ) {
|
||||
warn( SHOW_ERRNO( "failed to select() on non-blocking connect" ) );
|
||||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if ( !FD_ISSET( fd, &fds ) ) {
|
||||
result = -1;
|
||||
errno = ETIMEDOUT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
int scratch;
|
||||
socklen_t s_size = sizeof( scratch );
|
||||
if ( getsockopt( fd, SOL_SOCKET, SO_ERROR, &scratch, &s_size ) == -1 ) {
|
||||
result = -1;
|
||||
warn( SHOW_ERRNO( "getsockopt() failed" ) );
|
||||
goto out;
|
||||
}
|
||||
|
||||
if ( scratch == EINPROGRESS ) {
|
||||
scratch = ETIMEDOUT;
|
||||
}
|
||||
|
||||
result = scratch ? -1 : 0;
|
||||
errno = scratch;
|
||||
|
||||
out:
|
||||
if ( sock_set_nonblock( fd, 0 ) == -1 ) {
|
||||
warn( SHOW_ERRNO( "Failed to make socket blocking after connect()" ) );
|
||||
return -1;
|
||||
}
|
||||
|
||||
debug( "sock_try_connect: %i", result );
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@@ -2,11 +2,9 @@
|
||||
|
||||
#define SOCKUTIL_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/select.h>
|
||||
|
||||
/* Returns the size of the sockaddr, or 0 on error */
|
||||
size_t sockaddr_size(const struct sockaddr* sa);
|
||||
@@ -25,8 +23,16 @@ int sock_set_tcp_nodelay(int fd, int optval);
|
||||
/* TODO: Set the tcp_cork option */
|
||||
// int sock_set_cork(int fd, int optval);
|
||||
|
||||
int sock_set_nonblock(int fd, int optval);
|
||||
|
||||
/* Attempt to bind the fd to the sockaddr, retrying common transient failures */
|
||||
int sock_try_bind(int fd, const struct sockaddr* sa);
|
||||
|
||||
/* Try to call select(), retrying EINTR */
|
||||
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
|
||||
|
||||
/* Try to call connect(), timing out after wait seconds */
|
||||
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait );
|
||||
|
||||
#endif
|
||||
|
||||
|
Reference in New Issue
Block a user