[Spice-devel] [spice-server v4 3/6] net: Introduce red_socket_set_non_blocking() helper

Frediano Ziglio fziglio at redhat.com
Fri Mar 31 09:47:06 UTC 2017


> 
> This allows to move some low-level code out of reds.c
> 
> Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
> ---
>  server/net-utils.c | 30 ++++++++++++++++++++++++++++++
>  server/net-utils.h |  1 +
>  server/reds.c      | 13 ++-----------
>  3 files changed, 33 insertions(+), 11 deletions(-)
> 
> diff --git a/server/net-utils.c b/server/net-utils.c
> index 10f447b..b017daa 100644
> --- a/server/net-utils.c
> +++ b/server/net-utils.c
> @@ -52,3 +52,33 @@ bool red_socket_set_no_delay(int fd, bool no_delay)
>  
>      return true;
>  }
> +
> +/**
> + * red_socket_set_non_blocking:
> + * @fd: a socket file descriptor
> + * @non_blocking: whether to enable O_NONBLOCK on @fd
> + *
> + * Returns: #true if the operation succeeded, #false otherwise.
> + */
> +bool red_socket_set_non_blocking(int fd, bool non_blocking)
> +{
> +    int flags;
> +
> +    if ((flags = fcntl(fd, F_GETFL)) == -1) {
> +        spice_warning("fnctl(F_GETFL) failed, %s", strerror(errno));
> +        return false;
> +    }
> +
> +    if (non_blocking) {
> +        flags |= O_NONBLOCK;
> +    } else {
> +        flags &= ~O_NONBLOCK;
> +    }
> +
> +    if (fcntl(fd, F_SETFL, flags) == -1) {
> +        spice_warning("fnctl(F_SETFL) failed, %s", strerror(errno));
> +        return false;
> +    }
> +
> +    return true;
> +}
> diff --git a/server/net-utils.h b/server/net-utils.h
> index 9f4932e..023bc6b 100644
> --- a/server/net-utils.h
> +++ b/server/net-utils.h
> @@ -21,5 +21,6 @@
>  #include <stdbool.h>
>  
>  bool red_socket_set_no_delay(int fd, bool no_delay);
> +bool red_socket_set_non_blocking(int fd, bool non_blocking);
>  
>  #endif
> diff --git a/server/reds.c b/server/reds.c
> index 653a045..2f2dad8 100644
> --- a/server/reds.c
> +++ b/server/reds.c
> @@ -31,8 +31,6 @@
>  #include <limits.h>
>  #include <pthread.h>
>  #include <sys/mman.h>
> -#include <fcntl.h>
> -#include <errno.h>
>  #include <ctype.h>
>  
>  #include <openssl/err.h>
> @@ -2406,16 +2404,9 @@ static bool reds_init_keepalive(int socket)
>  static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
>  {
>      RedLinkInfo *link;
> -    int flags;
>  
> -    if ((flags = fcntl(socket, F_GETFL)) == -1) {
> -        spice_warning("accept failed, %s", strerror(errno));
> -        goto error;
> -    }
> -
> -    if (fcntl(socket, F_SETFL, flags | O_NONBLOCK) == -1) {
> -        spice_warning("accept failed, %s", strerror(errno));
> -        goto error;
> +    if (!red_socket_set_non_blocking(socket, TRUE)) {
> +       goto error;
>      }
>  
>      if (!red_socket_set_no_delay(socket, TRUE)) {

I would have removed the second argument instead considering
always true, in any case

Acked

Frediano


More information about the Spice-devel mailing list