If we do a partial read, the trace may look ok because the magic is there,
but we will have lost it and the next read() will be confused. So only
return early if we read zero bytes and we must not block, otherwise wait
around for the remaining data.
if (errno != EAGAIN)
perror("read");
- return -1;
+ /*
+ * never do partial reads. we can return if we
+ * didn't read anything and we should not block,
+ * otherwise wait for data
+ */
+ if ((bytes_left == bytes) && !block)
+ return 1;
+
+ usleep(10);
+ continue;
} else {
p += ret;
bytes_left -= ret;