#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
+#include <poll.h>
#include <libubox/blob.h>
#include <libubox/blobmsg.h>
return err;
}
+static void wait_data(int fd, bool write)
+{
+ struct pollfd pfd = { .fd = fd };
+
+ pfd.events = write ? POLLOUT : POLLIN;
+ poll(&pfd, 1, 0);
+}
+
static int writev_retry(int fd, struct iovec *iov, int iov_len)
{
int len = 0;
if (cur_len < 0) {
switch(errno) {
case EAGAIN:
- /* turn off non-blocking mode */
- fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) &
- ~O_NONBLOCK);
+ wait_data(fd, true);
break;
case EINTR:
break;
int bytes;
while (iov->iov_len > 0) {
+ if (wait)
+ wait_data(fd, false);
+
bytes = read(fd, iov->iov_base, iov->iov_len);
if (bytes < 0) {
bytes = 0;
if (!ctx->local_id)
goto error_close;
+ fcntl(ctx->sock.fd, F_SETFL, fcntl(ctx->sock.fd, F_GETFL) | O_NONBLOCK);
+
return ctx;
error_free_buf: