diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/unix-timed.h | 3 | ||||
-rw-r--r-- | src/libunixonacid/ipc_timed_send.c | 31 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/include/skalibs/unix-timed.h b/src/include/skalibs/unix-timed.h index 5fad179..c3ae4cd 100644 --- a/src/include/skalibs/unix-timed.h +++ b/src/include/skalibs/unix-timed.h @@ -33,4 +33,7 @@ extern int timed_getlnmax (buffer *, char *, unsigned int, unsigned int *, char, extern int netstring_timed_get (buffer *, stralloc *, tain_t const *, tain_t *) ; #define netstring_timed_get_g(b, sa, deadline) netstring_timed_get(b, sa, (deadline), &STAMP) +extern int ipc_timed_send (int, char const *, unsigned int, tain_t const *, tain_t *) ; +#define ipc_timed_send_g(fd, s, len, deadline) ipc_timed_send(fd, s, len, (deadline), &STAMP) + #endif diff --git a/src/libunixonacid/ipc_timed_send.c b/src/libunixonacid/ipc_timed_send.c new file mode 100644 index 0000000..9ba1e32 --- /dev/null +++ b/src/libunixonacid/ipc_timed_send.c @@ -0,0 +1,31 @@ +/* ISC license. */ + +#include <sys/socket.h> +#include <errno.h> +#include <skalibs/error.h> +#include <skalibs/tai.h> +#include <skalibs/iopause.h> +#include <skalibs/unix-timed.h> + + /* Yay MacOS */ +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif + +int ipc_timed_send (int fd, char const *s, unsigned int len, tain_t const *deadline, tain_t *stamp) +{ + iopause_fd x = { .fd = fd, .events = IOPAUSE_WRITE, .revents = 0 } ; + for (;;) + { + register int r = iopause_stamp(&x, 1, deadline, stamp) ; + if (r < 0) return 0 ; + else if (!r) return (errno = ETIMEDOUT, 0) ; + else if (x.revents & IOPAUSE_WRITE) + { + if (send(fd, s, len, MSG_NOSIGNAL) == (int)len) break ; + if (!error_isagain(errno)) return 0 ; + } + else if (x.revents & IOPAUSE_EXCEPT) return (send(fd, s, len, MSG_NOSIGNAL) == (int)len) ; + } + return 1 ; +} |