]> git.mikk.net Git - smtpsink/commitdiff
Set connection fds to nonblocking, detect blocked writes
authorchris mikkelson <chris@mikk.net>
Thu, 2 Apr 2009 04:47:31 +0000 (23:47 -0500)
committerchris mikkelson <chris@mikk.net>
Thu, 2 Apr 2009 04:47:31 +0000 (23:47 -0500)
(reads *should* not block, since they only occur when the
I/O thread gets a readability event).

io.c
smtp.c

diff --git a/io.c b/io.c
index 2d56936481e56b3e9d6171639a95172b4bd5dc66..537a14b3b4b833faac1db93a5e21944d3cef010c 100644 (file)
--- a/io.c
+++ b/io.c
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <assert.h>
 #include <string.h>
 
@@ -96,6 +97,9 @@ write_cb(int fd, short event, void *data)
 void
 io_initconn(struct conn *c)
 {
+       int flags = fcntl(c->fd, F_GETFL);
+       flags |= O_NONBLOCK;
+       fcntl(c->fd, F_SETFL, flags);
        event_set(&c->re, c->fd, EV_READ, read_cb, (void *)c);
        event_base_set(io_base, &c->re);
        event_set(&c->we, c->fd, EV_WRITE, write_cb, (void *)c);
diff --git a/smtp.c b/smtp.c
index 7b1630089f550df1071f06443d17fefcab1c7451..187f7287b9aac7c51d9f590f0a72b01da2e2a256 100644 (file)
--- a/smtp.c
+++ b/smtp.c
@@ -3,6 +3,7 @@
 #include <stdarg.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/errno.h>
 #include <assert.h>
 #include <ctype.h>
 
@@ -39,8 +40,11 @@ respond(struct conn *c, const char *fmt, ...)
        if (ret < 0) assert(0); 
        nw = write(c->fd, buf, ret);
        if (nw < 0) {
-               c->state = SMTP_CLOSED;
-               return 0;
+               if (errno != EAGAIN) {
+                       c->state = SMTP_CLOSED;
+                       return 0;
+               }
+               nw = 0;
        }
        if (nw < ret) {
                if (!(c->wdata.s = malloc(ret - nw))) {