diff options
| author | 2014-11-11 09:21:39 +0100 | |
|---|---|---|
| committer | 2014-11-11 13:53:25 +0100 | |
| commit | 23ecea077d8f22d9da5cae50df3e2ff3406fee90 (patch) | |
| tree | 16396042b7deac1d75471fbe547c308a07da42e8 | |
| parent | 4da37c1e130e32c4bbad13de7461e0612ca15597 (diff) | |
| download | usbmuxd-23ecea077d8f22d9da5cae50df3e2ff3406fee90.tar.gz usbmuxd-23ecea077d8f22d9da5cae50df3e2ff3406fee90.tar.bz2 | |
client: Make sure fd is writable before calling send() to avoid blocking
| -rw-r--r-- | src/client.c | 19 |
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 @@ | |||
| 30 | #include <unistd.h> | 30 | #include <unistd.h> |
| 31 | #include <sys/types.h> | 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> | 32 | #include <sys/socket.h> |
| 33 | #include <sys/select.h> | ||
| 33 | #include <sys/un.h> | 34 | #include <sys/un.h> |
| 34 | #include <arpa/inet.h> | 35 | #include <arpa/inet.h> |
| 35 | #include <pthread.h> | 36 | #include <pthread.h> |
| @@ -100,12 +101,28 @@ int client_read(struct mux_client *client, void *buffer, uint32_t len) | |||
| 100 | */ | 101 | */ |
| 101 | int client_write(struct mux_client *client, void *buffer, uint32_t len) | 102 | int client_write(struct mux_client *client, void *buffer, uint32_t len) |
| 102 | { | 103 | { |
| 104 | int sret = -1; | ||
| 105 | fd_set fds; | ||
| 106 | struct timeval to = {0, 0}; | ||
| 107 | |||
| 103 | usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len); | 108 | usbmuxd_log(LL_SPEW, "client_write fd %d buf %p len %d", client->fd, buffer, len); |
| 104 | if(client->state != CLIENT_CONNECTED) { | 109 | if(client->state != CLIENT_CONNECTED) { |
| 105 | usbmuxd_log(LL_ERROR, "Attempted to write to client %d not in CONNECTED state", client->fd); | 110 | usbmuxd_log(LL_ERROR, "Attempted to write to client %d not in CONNECTED state", client->fd); |
| 106 | return -1; | 111 | return -1; |
| 107 | } | 112 | } |
| 108 | return send(client->fd, buffer, len, 0); | 113 | |
| 114 | /* make sure fd is ready for writing */ | ||
| 115 | FD_ZERO(&fds); | ||
| 116 | FD_SET(client->fd, &fds); | ||
| 117 | sret = select(client->fd + 1, NULL, &fds, NULL, &to); | ||
| 118 | |||
| 119 | /* only send data if the fd is ready */ | ||
| 120 | if (sret > 0) { | ||
| 121 | sret = send(client->fd, buffer, len, 0); | ||
| 122 | } else { | ||
| 123 | usbmuxd_log(LL_ERROR, "ERROR: client_write: fd %d not ready for writing", client->fd); | ||
| 124 | } | ||
| 125 | return sret; | ||
| 109 | } | 126 | } |
| 110 | 127 | ||
| 111 | /** | 128 | /** |
