diff --git a/common.c b/common.c index fb8702e0..bb6dafa2 100644 --- a/common.c +++ b/common.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -510,3 +511,28 @@ uint64_t get_absolute_time_in_fp() { #endif return time_now_fp; } + +ssize_t non_blocking_write(int fd, const void *buf, size_t count) { + // debug(1,"writing %u to pipe...",count); + // we are assuming that the count is always smaller than the FIFO's buffer + struct pollfd ufds[1]; + ssize_t reply; + do { + ufds[0].fd = fd; + ufds[0].events = POLLOUT; + int rv = poll(ufds, 1, 5000); + if (rv == -1) + debug(1, "error waiting for pipe to unblock..."); + if (rv == 0) + debug(1, "timeout waiting for pipe to unblock"); + reply = write(fd, buf, count); + if ((reply == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) + debug(1, "writing to pipe will block..."); + // else + // debug(1,"writing %u to pipe done...",reply); + } while ((reply == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))); + return reply; + + // return write(fd,buf,count); +} + diff --git a/common.h b/common.h index 2ed250a2..4a6c44f0 100644 --- a/common.h +++ b/common.h @@ -90,6 +90,8 @@ int get_requested_connection_state_to_output(); void set_requested_connection_state_to_output(int v); +ssize_t non_blocking_write(int fd, const void *buf, size_t count); // used in a few places + int debuglev; void die(char *format, ...); void warn(char *format, ...);