From 08d9ec01cf59c7bb3febe3c4600e9efeb81901e3 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 15 May 2018 00:27:52 +0200 Subject: device: Flush buffer to client when remote side unexpectedly terminates connection --- src/client.c | 3 ++- src/device.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/client.c b/src/client.c index a9c986a..bbdac84 100644 --- a/src/client.c +++ b/src/client.c @@ -113,7 +113,8 @@ int client_write(struct mux_client *client, void *buffer, uint32_t len) 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); + usbmuxd_log(LL_DEBUG, "client_write: fd %d not ready for writing", client->fd); + sret = 0; } else { usbmuxd_log(LL_ERROR, "ERROR: client_write: sending to fd %d failed: %s", client->fd, strerror(errno)); } diff --git a/src/device.c b/src/device.c index d5cf9f0..3edaea0 100644 --- a/src/device.c +++ b/src/device.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "device.h" #include "client.h" #include "preflight.h" @@ -314,10 +315,21 @@ static void connection_teardown(struct mux_connection *conn) } else { conn->state = CONN_DEAD; if((conn->events & POLLOUT) && conn->ib_size > 0){ + usbmuxd_log(LL_DEBUG, "%s: flushing buffer to client (%u bytes)", __func__, conn->ib_size); + uint64_t tm_last = mstime64(); while(1){ size = client_write(conn->client, conn->ib_buf, conn->ib_size); - if(size <= 0) { + if(size < 0) { + usbmuxd_log(LL_ERROR, "%s: aborting buffer flush to client after error.", __func__); break; + } else if (size == 0) { + uint64_t tm_now = mstime64(); + if (tm_now - tm_last > 1000) { + usbmuxd_log(LL_ERROR, "%s: aborting buffer flush to client after unsuccessfully attempting for %dms.", __func__, (int)(tm_now - tm_last)); + break; + } + usleep(10000); + continue; } if(size == (int)conn->ib_size) { conn->ib_size = 0; @@ -326,6 +338,7 @@ static void connection_teardown(struct mux_connection *conn) conn->ib_size -= size; memmove(conn->ib_buf, conn->ib_buf + size, conn->ib_size); } + tm_last = mstime64(); } } client_close(conn->client); -- cgit v1.1-32-gdbae