Set FD_CLOEXEC flag on all sockets.
[tinc] / src / net_socket.c
index 20029e8..d8bb007 100644 (file)
@@ -1,7 +1,7 @@
 /*
     net_socket.c -- Handle various kinds of sockets.
     Copyright (C) 1998-2005 Ivo Timmermans,
-                  2000-2010 Guus Sliepen <guus@tinc-vpn.org>
+                  2000-2012 Guus Sliepen <guus@tinc-vpn.org>
                   2006      Scott Lamb <slamb@slamb.org>
                   2009      Florian Forster <octo@verplant.org>
 
@@ -180,6 +180,10 @@ int setup_listen_socket(const sockaddr_t *sa) {
                return -1;
        }
 
+#ifdef FD_CLOEXEC
+       fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
+
        /* Optimize TCP settings */
 
        option = 1;
@@ -238,6 +242,10 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
                return -1;
        }
 
+#ifdef FD_CLOEXEC
+       fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
+
 #ifdef O_NONBLOCK
        {
                int flags = fcntl(nfd, F_GETFL);
@@ -264,10 +272,10 @@ int setup_vpn_in_socket(const sockaddr_t *sa) {
        setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
 
        if(udp_rcvbuf && setsockopt(nfd, SOL_SOCKET, SO_RCVBUF, (void *)&udp_rcvbuf, sizeof(udp_rcvbuf)))
-               logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", option, strerror(errno));
+               logger(LOG_WARNING, "Can't set UDP SO_RCVBUF to %i: %s", udp_rcvbuf, strerror(errno));
 
        if(udp_sndbuf && setsockopt(nfd, SOL_SOCKET, SO_SNDBUF, (void *)&udp_sndbuf, sizeof(udp_sndbuf)))
-               logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", option, strerror(errno));
+               logger(LOG_WARNING, "Can't set UDP SO_SNDBUF to %i: %s", udp_sndbuf, strerror(errno));
 
 #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
        if(sa->sa.sa_family == AF_INET6)
@@ -410,6 +418,10 @@ begin:
 
        c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
 
+#ifdef FD_CLOEXEC
+       fcntl(c->socket, F_SETFD, FD_CLOEXEC);
+#endif
+
        if(c->socket == -1) {
                ifdebug(CONNECTIONS) logger(LOG_ERR, "Creating socket for %s failed: %s", c->hostname, sockstrerror(sockerrno));
                goto begin;
@@ -535,7 +547,7 @@ bool handle_new_meta_connection(int sock) {
        return true;
 }
 
-void free_outgoing(outgoing_t *outgoing) {
+static void free_outgoing(outgoing_t *outgoing) {
        if(outgoing->ai)
                freeaddrinfo(outgoing->ai);