timed_get.c (573B)
1 /* ISC license. */ 2 3 #include <errno.h> 4 #include <skalibs/allreadwrite.h> 5 #include <skalibs/iopause.h> 6 #include <skalibs/unix-timed.h> 7 8 ssize_t timed_get (void *b, init_func_ref getfd, get_func_ref get, tain const *deadline, tain *stamp) 9 { 10 iopause_fd x = { .fd = (*getfd)(b), .events = IOPAUSE_READ, .revents = 0 } ; 11 ssize_t r = (*get)(b) ; 12 while (!r) 13 { 14 r = iopause_stamp(&x, 1, deadline, stamp) ; 15 if (!r) return (errno = ETIMEDOUT, -1) ; 16 else if (r > 0 && x.revents & (IOPAUSE_READ | IOPAUSE_EXCEPT)) r = (*get)(b) ; 17 } 18 return unsanitize_read(r) ; 19 }