New way to connect a job_t.
[apps/madmutt.git] / lib-sys / evtloop.c
index 1d7e252..f9ae21e 100644 (file)
@@ -18,6 +18,7 @@
  *  Copyright © 2007 Pierre Habouzit
  */
 
+#include <netdb.h>
 #include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include "evtloop.h"
 #include "mutt.h"
 #include "mutt_ssl.li"
+#ifdef HAVE_LIBIDN
+#include <idna.h>
+#endif
 
 DO_ARRAY_TYPE(job_t, job);
 
 static int epollfd = -1;
 static job_array jobs;
-static pthread_mutex_t mx;
+static pthread_mutex_t el_mx;
+static pthread_cond_t el_cond;
 static pthread_t el_thread;
 
 static int el_job_setemode(job_t *w, el_mode emode)
@@ -113,6 +118,10 @@ job_t *el_job_start(const machine_t *m, void *cfg)
 
 int el_job_release(job_t *w, el_status reason)
 {
+    if (w->cond) {
+        pthread_cond_signal(&el_cond);
+        w->cond = false;
+    }
     w->state = EL_LLP_FINI;
     if (w->m && w->m->finalize) {
         w->m->finalize(w, reason);
@@ -223,6 +232,8 @@ int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
         goto error;
     if (fcntl(sock, F_SETFL, res | O_NONBLOCK) < 0)
         goto error;
+    if (fcntl(sock, F_SETFD, FD_CLOEXEC) < 0)
+        goto error;
     if (connect(sock, addr, len) < 0)
         goto error;
 
@@ -238,6 +249,47 @@ int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
     return el_job_release(w, EL_ERROR);
 }
 
+int el_job_connect2(job_t *w, const ACCOUNT *act)
+{
+    int rc;
+    char *host = NULL;
+    struct addrinfo *res;
+    struct addrinfo hints = {
+        .ai_family = AF_UNSPEC,
+        .ai_socktype = SOCK_STREAM,
+    };
+
+# ifdef HAVE_LIBIDN
+    if (idna_to_ascii_lz(act->host, &host, 1) != IDNA_SUCCESS) {
+        mutt_error(_("Bad IDN \"%s\"."), act->host);
+        return -1;
+    }
+# else
+    host = act->host;
+# endif
+    mutt_message(_("Looking up %s..."), act->host);
+    rc = getaddrinfo(host, NULL, &hints, &res);
+# ifdef HAVE_LIBIDN
+    p_delete(&host);
+# endif
+
+    if (rc) {
+        mutt_error(_("Could not find the host \"%s\""), act->host);
+        mutt_sleep(2);
+        return -1;
+    }
+    mutt_message(_("Connecting to %s..."), act->host);
+    rc = el_job_connect(w, res->ai_addr, res->ai_addrlen,
+                        res->ai_family, res->ai_socktype, res->ai_protocol);
+    freeaddrinfo (res);
+    if (rc) {
+        mutt_error(_("Could not connect to %s (%m)."), act->host);
+        mutt_sleep(2);
+        return -1;
+    }
+    return 0;
+}
+
 int el_job_starttls(job_t *w)
 {
     if (tls_negotiate(w) < 0)
@@ -298,12 +350,12 @@ ssize_t el_job_write(job_t *w, buffer_t *buf)
 
 void el_lock(void)
 {
-    pthread_mutex_lock(&mx);
+    pthread_mutex_lock(&el_mx);
 }
 
 void el_unlock(void)
 {
-    pthread_mutex_unlock(&mx);
+    pthread_mutex_unlock(&el_mx);
 }
 
 int el_dispatch(int timeout)
@@ -324,6 +376,10 @@ int el_dispatch(int timeout)
         int event = events[count].events;
         int evt   = 0;
 
+        if (w->cond) {
+            pthread_cond_signal(&el_cond);
+            w->cond = false;
+        }
         gettimeofday(&w->mru, NULL);
         switch (w->state) {
           case EL_LLP_INIT:
@@ -354,6 +410,12 @@ int el_dispatch(int timeout)
     return 0;
 }
 
+void el_wait(volatile job_t *w)
+{
+    w->cond = true;
+    pthread_cond_wait(&el_cond, &el_mx);
+}
+
 static void *el_loop(void *data)
 {
     time_t sec = time(NULL);
@@ -374,6 +436,10 @@ static void *el_loop(void *data)
         for (int i = jobs.len - 1; i >= 0; --i) {
             job_t *w = jobs.arr[i];
             if (timercmp(&now, &w->mru, >)) {
+                if (w->cond) {
+                    pthread_cond_signal(&el_cond);
+                    w->cond = false;
+                }
                 IGNORE(w->m->on_event(w, EL_EVT_WAKEUP));
             }
         }
@@ -387,7 +453,7 @@ void el_initialize(void)
 
     pthread_mutexattr_init(&attr);
     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
-    pthread_mutex_init(&mx, &attr);
+    pthread_mutex_init(&el_mx, &attr);
     pthread_mutexattr_destroy(&attr);
 
     gnutls_global_init();
@@ -407,5 +473,5 @@ void el_shutdown(void)
     job_array_wipe(&jobs);
     close(epollfd);
     gnutls_global_deinit();
-    pthread_mutex_destroy(&mx);
+    pthread_mutex_destroy(&el_mx);
 }