Implement lightweight getaddrinfo_a wrappers.
[apps/pfixtools.git] / postfix.c
index 533955e..f88feba 100644 (file)
--- a/postfix.c
+++ b/postfix.c
@@ -35,6 +35,7 @@
 
 #include <errno.h>
 #include <stdbool.h>
+#include <syslog.h>
 #include <unistd.h>
 
 #include "job.h"
@@ -46,13 +47,13 @@ struct jpriv_t {
     buffer_t obuf;
 };
 
-jpriv_t *postfix_jpriv_init(jpriv_t *jp)
+static jpriv_t *postfix_jpriv_init(jpriv_t *jp)
 {
     buffer_init(&jp->ibuf);
     buffer_init(&jp->obuf);
     return jp;
 }
-void postfix_jpriv_wipe(jpriv_t *jp)
+static void postfix_jpriv_wipe(jpriv_t *jp)
 {
     buffer_wipe(&jp->ibuf);
     buffer_wipe(&jp->obuf);
@@ -60,35 +61,30 @@ void postfix_jpriv_wipe(jpriv_t *jp)
 DO_NEW(jpriv_t, postfix_jpriv);
 DO_DELETE(jpriv_t, postfix_jpriv);
 
-
-void postfix_start(job_t *listener)
-{
-    job_t *job;
-
-    job = job_accept(listener, JOB_READ);
-    if (!job)
-        return;
-
-    job->jdata = postfix_jpriv_new();
-}
-
-void postfix_stop(job_t *job)
+static void postfix_stop(job_t *job)
 {
     postfix_jpriv_delete(&job->jdata);
 }
 
-void postfix_process(job_t *job)
+static void postfix_process(job_t *job)
 {
     int nb;
 
-    switch (job->state) {
+    switch (job->mode) {
       case JOB_LISTEN:
-        return postfix_start(job);
+        if ((job = job_accept(job, JOB_READ))) {
+            job->jdata   = postfix_jpriv_new();
+            job->process = &postfix_process;
+            job->stop    = &postfix_stop;
+        }
+        return;
 
       case JOB_WRITE:
         nb = write(job->fd, job->jdata->obuf.data, job->jdata->obuf.len);
         if (nb < 0) {
-            job->error = errno != EINTR && errno != EAGAIN;
+            if ((job->error = errno != EINTR && errno != EAGAIN)) {
+                syslog(LOG_ERR, "unexpected problem on the socket: %m");
+            }
             return;
         }
 
@@ -96,17 +92,20 @@ void postfix_process(job_t *job)
         if (job->jdata->obuf.len)
             return;
 
-        job_update_state(job, JOB_READ);
+        job_update_mode(job, JOB_READ);
 
         /* fall through */
 
       case JOB_READ:
         nb = buffer_read(&job->jdata->ibuf, job->fd, -1);
         if (nb < 0) {
-            job->error = errno != EINTR && errno != EAGAIN;
+            if ((job->error = errno != EINTR && errno != EAGAIN)) {
+                syslog(LOG_ERR, "unexpected problem on the socket: %m");
+            }
             return;
         }
         if (nb == 0) {
+            syslog(LOG_ERR, "unexpected eof");
             job->error = true;
             return;
         }
@@ -115,7 +114,7 @@ void postfix_process(job_t *job)
             return;
 
         /* TODO: do the parse */
-        job_update_state(job, JOB_IDLE);
+        job_update_mode(job, JOB_IDLE);
         return;
 
       default: