summaryrefslogtreecommitdiff
path: root/src/timeout.c
diff options
context:
space:
mode:
authorJannis Hoffmann <jannis@fehcom.de>2024-09-28 17:13:21 +0200
committerJannis Hoffmann <jannis@fehcom.de>2024-09-28 17:13:21 +0200
commit4ab19268268cd96b9706625d42a16d2a629134eb (patch)
tree0894a92709675955abb1b15647e8fe2911d89c7f /src/timeout.c
parent96cf8dffe4f7b0b910f790066ae622dc429eb522 (diff)
update to version 25
Diffstat (limited to 'src/timeout.c')
-rw-r--r--src/timeout.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/timeout.c b/src/timeout.c
new file mode 100644
index 0000000..7d3f0b2
--- /dev/null
+++ b/src/timeout.c
@@ -0,0 +1,60 @@
+#include <unistd.h>
+#include "error.h"
+#include "iopause.h"
+#include "timeout.h"
+
+/**
+ * @file timeout.c
+ * @author djb
+ * @ref qmail
+ * @brief read/write timeout handling
+ * @return same as read(2) or write(2); -1 on error
+ */
+
+int timeoutread(int t,int fd,char *buf,int len)
+{
+ struct taia now;
+ struct taia deadline;
+ iopause_fd x;
+
+ taia_now(&now);
+ taia_uint(&deadline,t);
+ taia_add(&deadline,&now,&deadline);
+
+ x.fd = fd;
+ x.events = IOPAUSE_READ;
+ for (;;) {
+ taia_now(&now);
+ iopause(&x,1,&deadline,&now);
+ if (x.revents) break;
+ if (taia_less(&deadline,&now)) {
+ errno = ETIMEDOUT;
+ return -1;
+ }
+ }
+ return read(fd,buf,len);
+}
+
+int timeoutwrite(int t,int fd,char *buf,int len)
+{
+ struct taia now;
+ struct taia deadline;
+ iopause_fd x;
+
+ taia_now(&now);
+ taia_uint(&deadline,t);
+ taia_add(&deadline,&now,&deadline);
+
+ x.fd = fd;
+ x.events = IOPAUSE_WRITE;
+ for (;;) {
+ taia_now(&now);
+ iopause(&x,1,&deadline,&now);
+ if (x.revents) break;
+ if (taia_less(&deadline,&now)) {
+ errno = ETIMEDOUT;
+ return -1;
+ }
+ }
+ return write(fd,buf,len);
+}