From 946a2a609ef813d7222463ad5c8655ff2842d99c Mon Sep 17 00:00:00 2001 From: Alex Bligh Date: Sat, 28 May 2011 19:18:51 +0100 Subject: [PATCH] Consume unwanted data on a write that fails When a write fails, we should consume the unwanted data sent to use. Whilst currently the server exits (under certain conditions), it does so messily (attempting to read some incoming data as commands). Instead, throw away the data for easier debugging. --- nbd-server.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/nbd-server.c b/nbd-server.c index a9098a5..695deef 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -360,6 +360,24 @@ static inline void readit(int f, void *buf, size_t len) { } /** + * Consume data from an FD that we don't want + * + * @param f a file descriptor + * @param buf a buffer + * @param len the number of bytes to consume + * @param bufsiz the size of the buffer + **/ +static inline void consume(int f, void * buf, size_t len, size_t bufsiz) { + size_t curlen; + while (len>0) { + curlen = (len>bufsiz)?bufsiz:len; + readit(f, buf, curlen); + len -= curlen; + } +} + + +/** * Write data from a buffer into a filedescriptor * * @param f a file descriptor @@ -1547,12 +1565,14 @@ int mainloop(CLIENT *client) { (client->server->flags & F_AUTOREADONLY)) { DEBUG("[WRITE to READONLY!]"); ERROR(client, reply, EPERM); + consume(client->net, buf, len-currlen, BUFSIZE); continue; } if (expwrite(request.from, buf, len, client, request.type & NBD_CMD_FLAG_FUA)) { DEBUG("Write failed: %m" ); ERROR(client, reply, errno); + consume(client->net, buf, len-currlen, BUFSIZE); continue; } len -= currlen; -- 1.7.10.4