]> Pileus Git - ~andy/fetchmail/blobdiff - socket.c
correct call to gen_send()
[~andy/fetchmail] / socket.c
index 91ca662b00e537335c685b3500930abdfa23688e..930e1b0d7896dd9392ec329f230ca2589d1c743e 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -179,7 +179,7 @@ static int handle_plugin(const char *host,
                (void) close(fds[1]);
                if ( (dup2(fds[0],0) == -1) || (dup2(fds[0],1) == -1) ) {
                        report(stderr, GT_("dup2 failed\n"));
-                       exit(1);
+                       _exit(EXIT_FAILURE);
                }
                /* fds[0] is now connected to 0 and 1; close it */
                (void) close(fds[0]);
@@ -188,7 +188,7 @@ static int handle_plugin(const char *host,
                argvec = parse_plugin(plugin,host,service);
                execvp(*argvec, argvec);
                report(stderr, GT_("execvp(%s) failed\n"), *argvec);
-               exit(0);
+               _exit(EXIT_FAILURE);
                break;
        default:        /* parent */
                /* NOP */
@@ -200,6 +200,31 @@ static int handle_plugin(const char *host,
 }
 #endif /* HAVE_SOCKETPAIR */
 
+static int setsocktimeout(int sock, int which, int timeout) {
+    struct timeval tv;
+    int rc;
+
+    tv.tv_sec = timeout;
+    tv.tv_usec = 0;
+    rc = setsockopt(sock, SOL_SOCKET, which, &tv, sizeof(tv));
+    if (rc) {
+       report(stderr, GT_("setsockopt(%d, SOL_SOCKET) failed: %s"), sock, strerror(errno));
+    }
+    return rc;
+}
+
+/** Configure socket options such as send/receive timeout at the socket
+ * level, to avoid network-induced stalls.
+ */
+int SockTimeout(int sock, int timeout)
+{
+    int err = 0;
+
+    if (setsocktimeout(sock, SO_RCVTIMEO, timeout)) err = 1;
+    if (setsocktimeout(sock, SO_SNDTIMEO, timeout)) err = 1;
+    return err;
+}
+
 int UnixOpen(const char *path)
 {
     int sock = -1;