tune_socket: use libusual for socket flags
authorMarko Kreen <markokr@gmail.com>
Mon, 8 Apr 2013 19:11:37 +0000 (22:11 +0300)
committerMarko Kreen <markokr@gmail.com>
Mon, 8 Apr 2013 19:11:37 +0000 (22:11 +0300)
src/util.c

index 5af06f1f2b6a5b1a51a4ce00b26219f2bf99e620..de0148953ad6bd0e01e169bc0722d61582c72484 100644 (file)
@@ -110,22 +110,14 @@ bool tune_socket(int sock, bool is_unix)
 {
        int res;
        int val;
+       bool ok;
 
-       /* close fd on exec */
-       res = fcntl(sock, F_SETFD, FD_CLOEXEC);
-       if (res < 0)
-               goto fail;
-
-       /* when no data available, return EAGAIN instead blocking */
-       socket_set_nonblocking(sock, 1);
-
-#ifdef SO_NOSIGPIPE
-       /* disallow SIGPIPE, if possible */
-       val = 1;
-       res = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val));
-       if (res < 0)
+       /*
+        * Generic stuff + nonblock.
+        */
+       ok = socket_setup(sock, true);
+       if (!ok)
                goto fail;
-#endif
 
        /*
         * Following options are for network sockets
@@ -133,48 +125,17 @@ bool tune_socket(int sock, bool is_unix)
        if (is_unix)
                return true;
 
-       /* the keepalive stuff needs some poking before enbling */
-       if (cf_tcp_keepalive) {
-               /* turn on socket keepalive */
-               val = 1;
-               res = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
-               if (res < 0)
-                       goto fail;
-#ifdef __linux__
-               /* set count of keepalive packets */
-               if (cf_tcp_keepcnt > 0) {
-                       val = cf_tcp_keepcnt;
-                       res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val));
-                       if (res < 0)
-                               goto fail;
-               }
-               /* how long the connection can stay idle before sending keepalive pkts */
-               if (cf_tcp_keepidle) {
-                       val = cf_tcp_keepidle;
-                       res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val));
-                       if (res < 0)
-                               goto fail;
-               }
-               /* time between packets */
-               if (cf_tcp_keepintvl) {
-                       val = cf_tcp_keepintvl;
-                       res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val));
-                       if (res < 0)
-                               goto fail;
-               }
-#else
-#ifdef TCP_KEEPALIVE
-               if (cf_tcp_keepidle) {
-                       val = cf_tcp_keepidle;
-                       res = setsockopt(sock, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val));
-                       if (res < 0)
-                               goto fail;
-               }
-#endif
-#endif
-       }
+       /*
+        * TCP Keepalive
+        */
+       ok = socket_set_keepalive(sock, cf_tcp_keepalive, cf_tcp_keepidle,
+                                 cf_tcp_keepintvl, cf_tcp_keepcnt);
+       if (!ok)
+               goto fail;
 
-       /* set in-kernel socket buffer size */
+       /*
+        * set in-kernel socket buffer size
+        */
        if (cf_tcp_socket_buffer) {
                val = cf_tcp_socket_buffer;
                res = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(val));