net: Introduce red_socket_set_non_blocking() helper

This allows to move some low-level code out of reds.c

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
Christophe Fergeau 2017-03-16 16:02:08 +01:00
parent ecf05ed6be
commit b85ca4b8a9
3 changed files with 33 additions and 11 deletions

View File

@ -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;
}

View File

@ -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 /* RED_NET_UTILS_H_ */

View File

@ -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)) {