diff mbox

[1/4] net: ensure "socket" backend uses non-blocking fds

Message ID 1364314072-2474-2-git-send-email-stefanha@redhat.com
State New
Headers show

Commit Message

Stefan Hajnoczi March 26, 2013, 4:07 p.m. UTC
There are several code paths in net_init_socket() depending on how the
socket is created: file descriptor passing, UDP multicast, TCP, or UDP.
Some of these support both listen and connect.

Not all code paths set the socket to non-blocking.  This patch addresses
the file descriptor passing and UDP cases which were missing
socket_set_nonblock(fd) calls.

I considered moving socket_set_nonblock(fd) to a central location but it
turns out the code paths are different enough to require non-blocking at
different places.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 net/socket.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Juan Quintela March 26, 2013, 4:25 p.m. UTC | #1
Stefan Hajnoczi <stefanha@redhat.com> wrote:
> There are several code paths in net_init_socket() depending on how the
> socket is created: file descriptor passing, UDP multicast, TCP, or UDP.
> Some of these support both listen and connect.
>
> Not all code paths set the socket to non-blocking.  This patch addresses
> the file descriptor passing and UDP cases which were missing
> socket_set_nonblock(fd) calls.
>
> I considered moving socket_set_nonblock(fd) to a central location but it
> turns out the code paths are different enough to require non-blocking at
> different places.
>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  net/socket.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/net/socket.c b/net/socket.c
> index 6c3752b..4a88142 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -674,6 +674,7 @@ static int net_socket_udp_init(NetClientState *peer,
>          closesocket(fd);
>          return -1;
>      }
> +    socket_set_nonblock(fd);
>  
>      s = net_socket_fd_init(peer, model, name, fd, 0);
>      if (!s) {
> @@ -712,6 +713,7 @@ int net_init_socket(const NetClientOptions *opts, const char *name,
>          int fd;
>  
>          fd = monitor_handle_fd_param(cur_mon, sock->fd);
> +        socket_set_nonblock(fd);
>          if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) {

Shouldn't be better to refactor the code to _not_ call
socket_set_nonblock() in the case that fd == -1?  It will just clobber
errno?

>              return -1;
>          }
Eric Blake March 26, 2013, 4:33 p.m. UTC | #2
On 03/26/2013 10:07 AM, Stefan Hajnoczi wrote:
> There are several code paths in net_init_socket() depending on how the
> socket is created: file descriptor passing, UDP multicast, TCP, or UDP.
> Some of these support both listen and connect.
> 
> Not all code paths set the socket to non-blocking.  This patch addresses
> the file descriptor passing and UDP cases which were missing
> socket_set_nonblock(fd) calls.
> 
> I considered moving socket_set_nonblock(fd) to a central location but it
> turns out the code paths are different enough to require non-blocking at
> different places.

Is it worth rearranging patch 3 first, so that you don't have to churn
on these newly-added lines?
Stefan Hajnoczi March 27, 2013, 6:36 a.m. UTC | #3
On Tue, Mar 26, 2013 at 05:25:50PM +0100, Juan Quintela wrote:
> Stefan Hajnoczi <stefanha@redhat.com> wrote:
> > There are several code paths in net_init_socket() depending on how the
> > socket is created: file descriptor passing, UDP multicast, TCP, or UDP.
> > Some of these support both listen and connect.
> >
> > Not all code paths set the socket to non-blocking.  This patch addresses
> > the file descriptor passing and UDP cases which were missing
> > socket_set_nonblock(fd) calls.
> >
> > I considered moving socket_set_nonblock(fd) to a central location but it
> > turns out the code paths are different enough to require non-blocking at
> > different places.
> >
> > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> > ---
> >  net/socket.c | 2 ++
> >  1 file changed, 2 insertions(+)
> >
> > diff --git a/net/socket.c b/net/socket.c
> > index 6c3752b..4a88142 100644
> > --- a/net/socket.c
> > +++ b/net/socket.c
> > @@ -674,6 +674,7 @@ static int net_socket_udp_init(NetClientState *peer,
> >          closesocket(fd);
> >          return -1;
> >      }
> > +    socket_set_nonblock(fd);
> >  
> >      s = net_socket_fd_init(peer, model, name, fd, 0);
> >      if (!s) {
> > @@ -712,6 +713,7 @@ int net_init_socket(const NetClientOptions *opts, const char *name,
> >          int fd;
> >  
> >          fd = monitor_handle_fd_param(cur_mon, sock->fd);
> > +        socket_set_nonblock(fd);
> >          if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) {
> 
> Shouldn't be better to refactor the code to _not_ call
> socket_set_nonblock() in the case that fd == -1?  It will just clobber
> errno?

I'll send a fixed version.

Stefan
Stefan Hajnoczi March 27, 2013, 6:37 a.m. UTC | #4
On Tue, Mar 26, 2013 at 10:33:45AM -0600, Eric Blake wrote:
> On 03/26/2013 10:07 AM, Stefan Hajnoczi wrote:
> > There are several code paths in net_init_socket() depending on how the
> > socket is created: file descriptor passing, UDP multicast, TCP, or UDP.
> > Some of these support both listen and connect.
> > 
> > Not all code paths set the socket to non-blocking.  This patch addresses
> > the file descriptor passing and UDP cases which were missing
> > socket_set_nonblock(fd) calls.
> > 
> > I considered moving socket_set_nonblock(fd) to a central location but it
> > turns out the code paths are different enough to require non-blocking at
> > different places.
> 
> Is it worth rearranging patch 3 first, so that you don't have to churn
> on these newly-added lines?

Will do that in v2.

Stefan
diff mbox

Patch

diff --git a/net/socket.c b/net/socket.c
index 6c3752b..4a88142 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -674,6 +674,7 @@  static int net_socket_udp_init(NetClientState *peer,
         closesocket(fd);
         return -1;
     }
+    socket_set_nonblock(fd);
 
     s = net_socket_fd_init(peer, model, name, fd, 0);
     if (!s) {
@@ -712,6 +713,7 @@  int net_init_socket(const NetClientOptions *opts, const char *name,
         int fd;
 
         fd = monitor_handle_fd_param(cur_mon, sock->fd);
+        socket_set_nonblock(fd);
         if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) {
             return -1;
         }