Correction from Nelson Beebe: argument to functions such as isspace()
[rsync/rsync.git] / socket.c
index 344c42e..15dea8b 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -122,12 +122,14 @@ int try_bind_local(int s,
        for (r = bres_all; r; r = r->ai_next) {
                if (bind(s, r->ai_addr, r->ai_addrlen) == -1)
                        continue;
+               freeaddrinfo(bres_all);
                return s;
        }
 
        /* no error message; there might be some problem that allows
         * creation of the socket but not binding, perhaps if the
         * machine has no ipv6 address of this name. */
+       freeaddrinfo(bres_all);
        return -1;
 }
 
@@ -182,6 +184,10 @@ int open_socket_out(char *host, int port, const char *bind_address,
                *cp++ = '\0';
                strcpy(portbuf, cp);
                h = buffer;
+               if (verbose >= 2) {
+                       rprintf(FINFO, "connection via http proxy %s port %s\n",
+                               h, portbuf);
+               }
        } else {
                snprintf(portbuf, sizeof(portbuf), "%d", port);
                h = host;
@@ -325,7 +331,8 @@ static int open_socket_in(int type, int port, const char *bind_address,
                        close(s);
                        continue;
                }
-               
+
+               freeaddrinfo(all_ai);
                return s;
        }
 
@@ -387,6 +394,7 @@ void start_accept_loop(int port, int (*fn)(int ))
           for each incoming connection */
        while (1) {
                fd_set fds;
+               pid_t pid;
                int fd;
                struct sockaddr_storage addr;
                socklen_t addrlen = sizeof addr;
@@ -418,15 +426,26 @@ void start_accept_loop(int port, int (*fn)(int ))
                 while (waitpid(-1, NULL, WNOHANG) > 0);
 #endif
 
-               if (fork()==0) {
+               if ((pid = fork()) == 0) {
                        close(s);
                        /* open log file in child before possibly giving
                           up privileges  */
                        log_open();
                        _exit(fn(fd));
+               } else if (pid < 0) {
+                       rprintf(FERROR,
+                               RSYNC_NAME
+                               ": could not create child server process: %s\n",
+                               strerror(errno));
+                       close(fd);
+                       /* This might have happened because we're
+                        * overloaded.  Sleep briefly before trying to
+                        * accept again. */
+                       sleep(2);
+               } else {
+                       /* Parent doesn't need this fd anymore. */
+                       close(fd);
                }
-
-               close(fd);
        }
 }