From: Alex Bligh Date: Sat, 28 May 2011 18:18:51 +0000 (+0100) Subject: Consume unwanted data on a write that fails X-Git-Url: http://git.alex.org.uk 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. --- 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;