From a3cae2b7a3dfe8120f2a65a1fae8640bb4f095a5 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 12 Nov 2014 19:53:06 +0100 Subject: Use non-blocking sockets for client communication This approach is better than using blocking sockets and select() since there's no guarantee that send() doesn't block. Plus we're using poll() anyway so send() and recv() will only be called if the socket is actually ready for writing/reading. --- src/client.c | 23 ++++++++++++++++++++++- src/main.c | 9 +++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/client.c b/src/client.c index 268c8b9..4ec4025 100644 --- a/src/client.c +++ b/src/client.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -100,12 +101,23 @@ int client_read(struct mux_client *client, void *buffer, uint32_t len) */ int client_write(struct mux_client *client, void *buffer, uint32_t len) { + int sret = -1; + usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len); if(client->state != CLIENT_CONNECTED) { usbmuxd_log(LL_ERROR, "Attempted to write to client %d not in CONNECTED state", client->fd); return -1; } - return send(client->fd, buffer, len, 0); + + sret = send(client->fd, buffer, len, 0); + if (sret < 0) { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { + usbmuxd_log(LL_ERROR, "ERROR: client_write: fd %d not ready for writing", client->fd); + } else { + usbmuxd_log(LL_ERROR, "ERROR: client_write: sending to fd %d failed: %s", client->fd, strerror(errno)); + } + } + return sret; } /** @@ -150,6 +162,15 @@ int client_accept(int listenfd) return cfd; } + int flags = fcntl(cfd, F_GETFL, 0); + if (flags < 0) { + usbmuxd_log(LL_ERROR, "ERROR: Could not get socket flags!"); + } else { + if (fcntl(cfd, F_SETFL, flags | O_NONBLOCK) < 0) { + usbmuxd_log(LL_ERROR, "ERROR: Could not set socket to non-blocking mode"); + } + } + struct mux_client *client; client = malloc(sizeof(struct mux_client)); memset(client, 0, sizeof(struct mux_client)); diff --git a/src/main.c b/src/main.c index b1f4eeb..2e4439c 100644 --- a/src/main.c +++ b/src/main.c @@ -82,6 +82,15 @@ static int create_socket(void) { return -1; } + int flags = fcntl(listenfd, F_GETFL, 0); + if (flags < 0) { + usbmuxd_log(LL_FATAL, "ERROR: Could not get flags for socket"); + } else { + if (fcntl(listenfd, F_SETFL, flags | O_NONBLOCK) < 0) { + usbmuxd_log(LL_FATAL, "ERROR: Could not set socket to non-blocking"); + } + } + bzero(&bind_addr, sizeof(bind_addr)); bind_addr.sun_family = AF_UNIX; strcpy(bind_addr.sun_path, socket_path); -- cgit v1.1-32-gdbae