summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-11-11 09:21:39 +0100
committerGravatar Nikias Bassen2014-11-11 13:53:25 +0100
commit23ecea077d8f22d9da5cae50df3e2ff3406fee90 (patch)
tree16396042b7deac1d75471fbe547c308a07da42e8
parent4da37c1e130e32c4bbad13de7461e0612ca15597 (diff)
downloadusbmuxd-23ecea077d8f22d9da5cae50df3e2ff3406fee90.tar.gz
usbmuxd-23ecea077d8f22d9da5cae50df3e2ff3406fee90.tar.bz2
client: Make sure fd is writable before calling send() to avoid blocking
-rw-r--r--src/client.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/client.c b/src/client.c
index 268c8b9..417ba35 100644
--- a/src/client.c
+++ b/src/client.c
@@ -30,6 +30,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/select.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <pthread.h>
@@ -100,12 +101,28 @@ 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;
+ fd_set fds;
+ struct timeval to = {0, 0};
+
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);
+
+ /* make sure fd is ready for writing */
+ FD_ZERO(&fds);
+ FD_SET(client->fd, &fds);
+ sret = select(client->fd + 1, NULL, &fds, NULL, &to);
+
+ /* only send data if the fd is ready */
+ if (sret > 0) {
+ sret = send(client->fd, buffer, len, 0);
+ } else {
+ usbmuxd_log(LL_ERROR, "ERROR: client_write: fd %d not ready for writing", client->fd);
+ }
+ return sret;
}
/**