Code

git-daemon: don't ignore pid-file write failure
[git.git] / connect.c
index f7edba82c4d2fcc642ccae785aa7a315b1f819d6..da89c9cfcf3469dfab789f2644f953ea46666e90 100644 (file)
--- a/connect.c
+++ b/connect.c
@@ -3,12 +3,7 @@
 #include "pkt-line.h"
 #include "quote.h"
 #include "refs.h"
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <signal.h>
+#include "run-command.h"
 
 static char *server_capabilities;
 
@@ -102,7 +97,7 @@ int get_ack(int fd, unsigned char *result_sha1)
                line[--len] = 0;
        if (!strcmp(line, "NAK"))
                return 0;
-       if (!strncmp(line, "ACK ", 4)) {
+       if (!prefixcmp(line, "ACK ")) {
                if (!get_sha1_hex(line+4, result_sha1)) {
                        if (strstr(line+45, "continue"))
                                return 2;
@@ -202,8 +197,8 @@ static int count_refspec_match(const char *pattern,
                 */
                if (namelen != patlen &&
                    patlen != namelen - 5 &&
-                   strncmp(name, "refs/heads/", 11) &&
-                   strncmp(name, "refs/tags/", 10)) {
+                   prefixcmp(name, "refs/heads/") &&
+                   prefixcmp(name, "refs/tags/")) {
                        /* We want to catch the case where only weak
                         * matches are found and there are multiple
                         * matches, and where more than one strong
@@ -422,6 +417,8 @@ static int git_tcp_connect_sock(char *host)
        if (colon) {
                *colon = 0;
                port = colon + 1;
+               if (!*port)
+                       port = "<none>";
        }
 
        memset(&hints, 0, sizeof(hints));
@@ -430,7 +427,7 @@ static int git_tcp_connect_sock(char *host)
 
        gai = getaddrinfo(host, port, &hints, &ai);
        if (gai)
-               die("Unable to look up %s (%s)", host, gai_strerror(gai));
+               die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
 
        for (ai0 = ai; ai; ai = ai->ai_next) {
                sockfd = socket(ai->ai_family,
@@ -535,7 +532,7 @@ static void git_tcp_connect(int fd[2], char *host)
        int sockfd = git_tcp_connect_sock(host);
 
        fd[0] = sockfd;
-       fd[1] = sockfd;
+       fd[1] = dup(sockfd);
 }
 
 
@@ -604,8 +601,8 @@ static void git_proxy_connect(int fd[2], char *host)
 {
        const char *port = STR(DEFAULT_GIT_PORT);
        char *colon, *end;
-       int pipefd[2][2];
-       pid_t pid;
+       const char *argv[4];
+       struct child_process proxy;
 
        if (host[0] == '[') {
                end = strchr(host + 1, ']');
@@ -624,25 +621,18 @@ static void git_proxy_connect(int fd[2], char *host)
                port = colon + 1;
        }
 
-       if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
-               die("unable to create pipe pair for communication");
-       pid = fork();
-       if (!pid) {
-               dup2(pipefd[1][0], 0);
-               dup2(pipefd[0][1], 1);
-               close(pipefd[0][0]);
-               close(pipefd[0][1]);
-               close(pipefd[1][0]);
-               close(pipefd[1][1]);
-               execlp(git_proxy_command, git_proxy_command, host, port, NULL);
-               die("exec failed");
-       }
-       if (pid < 0)
-               die("fork failed");
-       fd[0] = pipefd[0][0];
-       fd[1] = pipefd[1][1];
-       close(pipefd[0][1]);
-       close(pipefd[1][0]);
+       argv[0] = git_proxy_command;
+       argv[1] = host;
+       argv[2] = port;
+       argv[3] = NULL;
+       memset(&proxy, 0, sizeof(proxy));
+       proxy.argv = argv;
+       proxy.in = -1;
+       proxy.out = -1;
+       if (start_command(&proxy))
+               die("cannot start proxy %s", argv[0]);
+       fd[0] = proxy.out; /* read from proxy stdout */
+       fd[1] = proxy.in;  /* write to proxy stdin */
 }
 
 #define MAX_CMD_LEN 1024