]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
upload-pack: no longer call rev-list
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
41cb7488 3#include "pkt-line.h"
b10d0ec7 4#include "quote.h"
6abf5c0c 5#include "refs.h"
f7192598 6#include <sys/wait.h>
2386d658
LT
7#include <sys/socket.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <netdb.h>
e898081d 11#include <signal.h>
f7192598 12
96f1e58f 13static char *server_capabilities;
211b5f9e 14
2718ff09
LT
15static int check_ref(const char *name, int len, unsigned int flags)
16{
17 if (!flags)
18 return 1;
19
c41e20b3 20 if (len < 5 || memcmp(name, "refs/", 5))
2718ff09
LT
21 return 0;
22
23 /* Skip the "refs/" part */
24 name += 5;
25 len -= 5;
26
27 /* REF_NORMAL means that we don't want the magic fake tag refs */
28 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
29 return 0;
30
31 /* REF_HEADS means that we want regular branch heads */
32 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
33 return 1;
34
35 /* REF_TAGS means that we want tags */
36 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
37 return 1;
38
39 /* All type bits clear means that we are ok with anything */
40 return !(flags & ~REF_NORMAL);
41}
42
d1c133f5
LT
43/*
44 * Read all the refs from the other end
45 */
1a7141ff 46struct ref **get_remote_heads(int in, struct ref **list,
2718ff09
LT
47 int nr_match, char **match,
48 unsigned int flags)
d1c133f5
LT
49{
50 *list = NULL;
51 for (;;) {
52 struct ref *ref;
53 unsigned char old_sha1[20];
54 static char buffer[1000];
55 char *name;
211b5f9e 56 int len, name_len;
d1c133f5
LT
57
58 len = packet_read_line(in, buffer, sizeof(buffer));
59 if (!len)
60 break;
61 if (buffer[len-1] == '\n')
62 buffer[--len] = 0;
63
64 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
65 die("protocol error: expected sha/ref, got '%s'", buffer);
66 name = buffer + 41;
1a7141ff 67
211b5f9e
JS
68 name_len = strlen(name);
69 if (len != name_len + 41) {
70 if (server_capabilities)
71 free(server_capabilities);
9befac47 72 server_capabilities = xstrdup(name + name_len + 1);
211b5f9e
JS
73 }
74
2718ff09 75 if (!check_ref(name, name_len, flags))
cfee10a7 76 continue;
d1c133f5
LT
77 if (nr_match && !path_match(name, nr_match, match))
78 continue;
f88395ac 79 ref = xcalloc(1, sizeof(*ref) + len - 40);
e702496e 80 hashcpy(ref->old_sha1, old_sha1);
d1c133f5 81 memcpy(ref->name, buffer + 41, len - 40);
d1c133f5
LT
82 *list = ref;
83 list = &ref->next;
84 }
85 return list;
86}
87
211b5f9e
JS
88int server_supports(const char *feature)
89{
1f5881bb
JS
90 return server_capabilities &&
91 strstr(server_capabilities, feature) != NULL;
211b5f9e
JS
92}
93
41cb7488
LT
94int get_ack(int fd, unsigned char *result_sha1)
95{
96 static char line[1000];
97 int len = packet_read_line(fd, line, sizeof(line));
98
99 if (!len)
100 die("git-fetch-pack: expected ACK/NAK, got EOF");
101 if (line[len-1] == '\n')
102 line[--len] = 0;
103 if (!strcmp(line, "NAK"))
104 return 0;
da2a95b2 105 if (!strncmp(line, "ACK ", 4)) {
c4c86f07
JS
106 if (!get_sha1_hex(line+4, result_sha1)) {
107 if (strstr(line+45, "continue"))
108 return 2;
41cb7488 109 return 1;
c4c86f07 110 }
41cb7488
LT
111 }
112 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
113}
114
013e7c7f
LT
115int path_match(const char *path, int nr, char **match)
116{
117 int i;
118 int pathlen = strlen(path);
119
120 for (i = 0; i < nr; i++) {
121 char *s = match[i];
122 int len = strlen(s);
123
124 if (!len || len > pathlen)
125 continue;
126 if (memcmp(path + pathlen - len, s, len))
127 continue;
128 if (pathlen > len && path[pathlen - len - 1] != '/')
129 continue;
130 *s = 0;
9546010b 131 return (i + 1);
013e7c7f
LT
132 }
133 return 0;
134}
135
f88395ac
JH
136struct refspec {
137 char *src;
138 char *dst;
ff27adf3 139 char force;
f88395ac
JH
140};
141
ff27adf3
JH
142/*
143 * A:B means fast forward remote B with local A.
144 * +A:B means overwrite remote B with local A.
145 * +A is a shorthand for +A:A.
146 * A is a shorthand for A:A.
147 */
f88395ac
JH
148static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
149{
150 int i;
ff27adf3 151 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
f88395ac
JH
152 for (i = 0; i < nr_refspec; i++) {
153 char *sp, *dp, *ep;
154 sp = refspec[i];
ff27adf3
JH
155 if (*sp == '+') {
156 rs[i].force = 1;
157 sp++;
158 }
f88395ac
JH
159 ep = strchr(sp, ':');
160 if (ep) {
161 dp = ep + 1;
162 *ep = 0;
163 }
164 else
165 dp = sp;
166 rs[i].src = sp;
167 rs[i].dst = dp;
168 }
169 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
170 return rs;
171}
172
173static int count_refspec_match(const char *pattern,
174 struct ref *refs,
175 struct ref **matched_ref)
176{
f88395ac 177 int patlen = strlen(pattern);
29561ad0
JH
178 struct ref *matched_weak = NULL;
179 struct ref *matched = NULL;
180 int weak_match = 0;
181 int match = 0;
f88395ac 182
29561ad0 183 for (weak_match = match = 0; refs; refs = refs->next) {
f88395ac
JH
184 char *name = refs->name;
185 int namelen = strlen(name);
29561ad0
JH
186 int weak_match;
187
f88395ac
JH
188 if (namelen < patlen ||
189 memcmp(name + namelen - patlen, pattern, patlen))
190 continue;
191 if (namelen != patlen && name[namelen - patlen - 1] != '/')
192 continue;
29561ad0
JH
193
194 /* A match is "weak" if it is with refs outside
195 * heads or tags, and did not specify the pattern
196 * in full (e.g. "refs/remotes/origin/master") or at
197 * least from the toplevel (e.g. "remotes/origin/master");
198 * otherwise "git push $URL master" would result in
199 * ambiguity between remotes/origin/master and heads/master
200 * at the remote site.
201 */
202 if (namelen != patlen &&
203 patlen != namelen - 5 &&
204 strncmp(name, "refs/heads/", 11) &&
205 strncmp(name, "refs/tags/", 10)) {
206 /* We want to catch the case where only weak
207 * matches are found and there are multiple
208 * matches, and where more than one strong
209 * matches are found, as ambiguous. One
210 * strong match with zero or more weak matches
211 * are acceptable as a unique match.
212 */
213 matched_weak = refs;
214 weak_match++;
215 }
216 else {
217 matched = refs;
218 match++;
219 }
220 }
221 if (!matched) {
222 *matched_ref = matched_weak;
223 return weak_match;
224 }
225 else {
226 *matched_ref = matched;
227 return match;
f88395ac 228 }
f88395ac
JH
229}
230
231static void link_dst_tail(struct ref *ref, struct ref ***tail)
232{
233 **tail = ref;
234 *tail = &ref->next;
235 **tail = NULL;
236}
237
15e02b37
JH
238static struct ref *try_explicit_object_name(const char *name)
239{
240 unsigned char sha1[20];
241 struct ref *ref;
242 int len;
243 if (get_sha1(name, sha1))
244 return NULL;
245 len = strlen(name) + 1;
246 ref = xcalloc(1, sizeof(*ref) + len);
247 memcpy(ref->name, name, len);
e702496e 248 hashcpy(ref->new_sha1, sha1);
15e02b37
JH
249 return ref;
250}
251
f88395ac
JH
252static int match_explicit_refs(struct ref *src, struct ref *dst,
253 struct ref ***dst_tail, struct refspec *rs)
254{
255 int i, errs;
256 for (i = errs = 0; rs[i].src; i++) {
257 struct ref *matched_src, *matched_dst;
258
259 matched_src = matched_dst = NULL;
260 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
261 case 1:
262 break;
263 case 0:
15e02b37
JH
264 /* The source could be in the get_sha1() format
265 * not a reference name.
266 */
267 matched_src = try_explicit_object_name(rs[i].src);
268 if (matched_src)
269 break;
f88395ac 270 errs = 1;
4ec99bf0
TS
271 error("src refspec %s does not match any.",
272 rs[i].src);
f88395ac
JH
273 break;
274 default:
275 errs = 1;
276 error("src refspec %s matches more than one.",
277 rs[i].src);
278 break;
279 }
280 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
281 case 1:
282 break;
283 case 0:
284 if (!memcmp(rs[i].dst, "refs/", 5)) {
285 int len = strlen(rs[i].dst) + 1;
286 matched_dst = xcalloc(1, sizeof(*dst) + len);
287 memcpy(matched_dst->name, rs[i].dst, len);
288 link_dst_tail(matched_dst, dst_tail);
289 }
290 else if (!strcmp(rs[i].src, rs[i].dst) &&
291 matched_src) {
292 /* pushing "master:master" when
293 * remote does not have master yet.
294 */
4fa1604f 295 int len = strlen(matched_src->name) + 1;
f88395ac
JH
296 matched_dst = xcalloc(1, sizeof(*dst) + len);
297 memcpy(matched_dst->name, matched_src->name,
298 len);
299 link_dst_tail(matched_dst, dst_tail);
300 }
301 else {
302 errs = 1;
303 error("dst refspec %s does not match any "
304 "existing ref on the remote and does "
305 "not start with refs/.", rs[i].dst);
306 }
307 break;
308 default:
309 errs = 1;
310 error("dst refspec %s matches more than one.",
311 rs[i].dst);
312 break;
313 }
314 if (errs)
315 continue;
f88395ac
JH
316 if (matched_dst->peer_ref) {
317 errs = 1;
318 error("dst ref %s receives from more than one src.",
319 matched_dst->name);
320 }
ff27adf3 321 else {
f88395ac 322 matched_dst->peer_ref = matched_src;
ff27adf3
JH
323 matched_dst->force = rs[i].force;
324 }
f88395ac
JH
325 }
326 return -errs;
327}
328
329static struct ref *find_ref_by_name(struct ref *list, const char *name)
330{
331 for ( ; list; list = list->next)
332 if (!strcmp(list->name, name))
333 return list;
334 return NULL;
335}
336
337int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
338 int nr_refspec, char **refspec, int all)
339{
340 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
341
342 if (nr_refspec)
343 return match_explicit_refs(src, dst, dst_tail, rs);
344
345 /* pick the remainder */
346 for ( ; src; src = src->next) {
347 struct ref *dst_peer;
348 if (src->peer_ref)
349 continue;
350 dst_peer = find_ref_by_name(dst, src->name);
635d37af 351 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
f88395ac
JH
352 continue;
353 if (!dst_peer) {
f88395ac
JH
354 /* Create a new one and link it */
355 int len = strlen(src->name) + 1;
356 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
357 memcpy(dst_peer->name, src->name, len);
e702496e 358 hashcpy(dst_peer->new_sha1, src->new_sha1);
f88395ac
JH
359 link_dst_tail(dst_peer, dst_tail);
360 }
361 dst_peer->peer_ref = src;
362 }
363 return 0;
364}
365
2386d658
LT
366enum protocol {
367 PROTO_LOCAL = 1,
368 PROTO_SSH,
369 PROTO_GIT,
370};
371
372static enum protocol get_protocol(const char *name)
373{
374 if (!strcmp(name, "ssh"))
375 return PROTO_SSH;
376 if (!strcmp(name, "git"))
377 return PROTO_GIT;
c05186cc
LT
378 if (!strcmp(name, "git+ssh"))
379 return PROTO_SSH;
380 if (!strcmp(name, "ssh+git"))
381 return PROTO_SSH;
2386d658
LT
382 die("I don't handle protocol '%s'", name);
383}
384
5ba88448
YH
385#define STR_(s) # s
386#define STR(s) STR_(s)
2386d658 387
49744d63 388#ifndef NO_IPV6
4c505f71 389
5ad312be
JL
390/*
391 * Returns a connected socket() fd, or else die()s.
392 */
393static int git_tcp_connect_sock(char *host)
2386d658 394{
ac3bc6c1 395 int sockfd = -1, saved_errno = 0;
5ba88448 396 char *colon, *end;
554fe20d 397 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
398 struct addrinfo hints, *ai0, *ai;
399 int gai;
400
401 if (host[0] == '[') {
402 end = strchr(host + 1, ']');
403 if (end) {
404 *end = 0;
405 end++;
406 host++;
407 } else
408 end = host;
409 } else
410 end = host;
411 colon = strchr(end, ':');
412
ce6f8e7e
LT
413 if (colon) {
414 *colon = 0;
5ba88448 415 port = colon + 1;
ce6f8e7e 416 }
5ba88448
YH
417
418 memset(&hints, 0, sizeof(hints));
419 hints.ai_socktype = SOCK_STREAM;
420 hints.ai_protocol = IPPROTO_TCP;
421
422 gai = getaddrinfo(host, port, &hints, &ai);
423 if (gai)
424 die("Unable to look up %s (%s)", host, gai_strerror(gai));
425
426 for (ai0 = ai; ai; ai = ai->ai_next) {
5ad312be
JL
427 sockfd = socket(ai->ai_family,
428 ai->ai_socktype, ai->ai_protocol);
ac3bc6c1
PB
429 if (sockfd < 0) {
430 saved_errno = errno;
5ba88448 431 continue;
ac3bc6c1 432 }
5ba88448 433 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
ac3bc6c1 434 saved_errno = errno;
5ba88448
YH
435 close(sockfd);
436 sockfd = -1;
437 continue;
2386d658 438 }
5ba88448 439 break;
2386d658
LT
440 }
441
5ba88448 442 freeaddrinfo(ai0);
2386d658 443
2386d658 444 if (sockfd < 0)
ac3bc6c1 445 die("unable to connect a socket (%s)", strerror(saved_errno));
5ba88448 446
5ad312be 447 return sockfd;
2386d658
LT
448}
449
49744d63 450#else /* NO_IPV6 */
4c505f71 451
5ad312be
JL
452/*
453 * Returns a connected socket() fd, or else die()s.
454 */
455static int git_tcp_connect_sock(char *host)
4c505f71 456{
ac3bc6c1 457 int sockfd = -1, saved_errno = 0;
4c505f71
PA
458 char *colon, *end;
459 char *port = STR(DEFAULT_GIT_PORT), *ep;
460 struct hostent *he;
461 struct sockaddr_in sa;
462 char **ap;
463 unsigned int nport;
464
465 if (host[0] == '[') {
466 end = strchr(host + 1, ']');
467 if (end) {
468 *end = 0;
469 end++;
470 host++;
471 } else
472 end = host;
473 } else
474 end = host;
475 colon = strchr(end, ':');
476
477 if (colon) {
478 *colon = 0;
479 port = colon + 1;
480 }
481
4c505f71
PA
482 he = gethostbyname(host);
483 if (!he)
484 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
485 nport = strtoul(port, &ep, 10);
486 if ( ep == port || *ep ) {
487 /* Not numeric */
488 struct servent *se = getservbyname(port,"tcp");
489 if ( !se )
490 die("Unknown port %s\n", port);
491 nport = se->s_port;
492 }
493
494 for (ap = he->h_addr_list; *ap; ap++) {
495 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
ac3bc6c1
PB
496 if (sockfd < 0) {
497 saved_errno = errno;
4c505f71 498 continue;
ac3bc6c1 499 }
4c505f71
PA
500
501 memset(&sa, 0, sizeof sa);
502 sa.sin_family = he->h_addrtype;
6573faff 503 sa.sin_port = htons(nport);
c6164218 504 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71
PA
505
506 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
ac3bc6c1 507 saved_errno = errno;
4c505f71
PA
508 close(sockfd);
509 sockfd = -1;
510 continue;
511 }
512 break;
513 }
514
515 if (sockfd < 0)
ac3bc6c1 516 die("unable to connect a socket (%s)", strerror(saved_errno));
4c505f71 517
5ad312be
JL
518 return sockfd;
519}
520
521#endif /* NO_IPV6 */
522
523
c78963d2 524static void git_tcp_connect(int fd[2], char *host)
5ad312be
JL
525{
526 int sockfd = git_tcp_connect_sock(host);
527
4c505f71
PA
528 fd[0] = sockfd;
529 fd[1] = sockfd;
4c505f71
PA
530}
531
4c505f71 532
96f1e58f
DR
533static char *git_proxy_command;
534static const char *rhost_name;
e814bc4d 535static int rhost_len;
f8014776
PC
536
537static int git_proxy_command_options(const char *var, const char *value)
538{
e814bc4d 539 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
540 const char *for_pos;
541 int matchlen = -1;
542 int hostlen;
543
e814bc4d 544 if (git_proxy_command)
f8014776 545 return 0;
e814bc4d
JH
546 /* [core]
547 * ;# matches www.kernel.org as well
548 * gitproxy = netcatter-1 for kernel.org
549 * gitproxy = netcatter-2 for sample.xz
550 * gitproxy = netcatter-default
551 */
c3df8568 552 for_pos = strstr(value, " for ");
e814bc4d
JH
553 if (!for_pos)
554 /* matches everybody */
555 matchlen = strlen(value);
556 else {
557 hostlen = strlen(for_pos + 5);
558 if (rhost_len < hostlen)
559 matchlen = -1;
560 else if (!strncmp(for_pos + 5,
561 rhost_name + rhost_len - hostlen,
562 hostlen) &&
563 ((rhost_len == hostlen) ||
564 rhost_name[rhost_len - hostlen -1] == '.'))
565 matchlen = for_pos - value;
566 else
567 matchlen = -1;
568 }
569 if (0 <= matchlen) {
570 /* core.gitproxy = none for kernel.org */
571 if (matchlen == 4 &&
572 !memcmp(value, "none", 4))
573 matchlen = 0;
574 git_proxy_command = xmalloc(matchlen + 1);
575 memcpy(git_proxy_command, value, matchlen);
576 git_proxy_command[matchlen] = 0;
f8014776 577 }
e814bc4d 578 return 0;
f8014776
PC
579 }
580
581 return git_default_config(var, value);
582}
583
e814bc4d 584static int git_use_proxy(const char *host)
f8014776 585{
e814bc4d
JH
586 rhost_name = host;
587 rhost_len = strlen(host);
f8014776
PC
588 git_proxy_command = getenv("GIT_PROXY_COMMAND");
589 git_config(git_proxy_command_options);
e814bc4d
JH
590 rhost_name = NULL;
591 return (git_proxy_command && *git_proxy_command);
f8014776
PC
592}
593
c78963d2 594static void git_proxy_connect(int fd[2], char *host)
f8014776 595{
554fe20d 596 const char *port = STR(DEFAULT_GIT_PORT);
f8014776
PC
597 char *colon, *end;
598 int pipefd[2][2];
599 pid_t pid;
600
601 if (host[0] == '[') {
602 end = strchr(host + 1, ']');
603 if (end) {
604 *end = 0;
605 end++;
606 host++;
607 } else
608 end = host;
609 } else
610 end = host;
611 colon = strchr(end, ':');
612
613 if (colon) {
614 *colon = 0;
615 port = colon + 1;
616 }
617
618 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
619 die("unable to create pipe pair for communication");
620 pid = fork();
621 if (!pid) {
622 dup2(pipefd[1][0], 0);
623 dup2(pipefd[0][1], 1);
624 close(pipefd[0][0]);
625 close(pipefd[0][1]);
626 close(pipefd[1][0]);
627 close(pipefd[1][1]);
628 execlp(git_proxy_command, git_proxy_command, host, port, NULL);
629 die("exec failed");
630 }
5ad312be
JL
631 if (pid < 0)
632 die("fork failed");
f8014776
PC
633 fd[0] = pipefd[0][0];
634 fd[1] = pipefd[1][1];
635 close(pipefd[0][1]);
636 close(pipefd[1][0]);
f8014776
PC
637}
638
0f503d77
CC
639#define MAX_CMD_LEN 1024
640
f7192598 641/*
f42a5c4e
FBH
642 * This returns 0 if the transport protocol does not need fork(2),
643 * or a process id if it does. Once done, finish the connection
644 * with finish_connect() with the value returned from this function
645 * (it is safe to call finish_connect() with 0 to support the former
646 * case).
647 *
648 * Does not return a negative value on error; it just dies.
f7192598 649 */
f42a5c4e 650pid_t git_connect(int fd[2], char *url, const char *prog)
f7192598 651{
faea9ccb 652 char *host, *path = url;
356bece0
YH
653 char *end;
654 int c;
f7192598
LT
655 int pipefd[2][2];
656 pid_t pid;
faea9ccb 657 enum protocol protocol = PROTO_LOCAL;
da2a95b2 658 int free_path = 0;
faea9ccb 659
f0b7367c
JH
660 /* Without this we cannot rely on waitpid() to tell
661 * what happened to our children.
662 */
663 signal(SIGCHLD, SIG_DFL);
664
faea9ccb
AE
665 host = strstr(url, "://");
666 if(host) {
667 *host = '\0';
668 protocol = get_protocol(url);
669 host += 3;
356bece0
YH
670 c = '/';
671 } else {
f7192598 672 host = url;
356bece0
YH
673 c = ':';
674 }
675
676 if (host[0] == '[') {
677 end = strchr(host + 1, ']');
678 if (end) {
679 *end = 0;
680 end++;
681 host++;
682 } else
683 end = host;
684 } else
685 end = host;
686
687 path = strchr(end, c);
688 if (c == ':') {
689 if (path) {
faea9ccb 690 protocol = PROTO_SSH;
356bece0
YH
691 *path++ = '\0';
692 } else
693 path = host;
f7192598 694 }
2386d658 695
faea9ccb
AE
696 if (!path || !*path)
697 die("No path specified. See 'man git-pull' for valid url syntax");
698
699 /*
700 * null-terminate hostname and point path to ~ for URL's like this:
701 * ssh://host.xz/~user/repo
702 */
703 if (protocol != PROTO_LOCAL && host != url) {
704 char *ptr = path;
705 if (path[1] == '~')
706 path++;
da2a95b2 707 else {
9befac47 708 path = xstrdup(ptr);
da2a95b2
SH
709 free_path = 1;
710 }
faea9ccb
AE
711
712 *ptr = '\0';
713 }
714
f8014776 715 if (protocol == PROTO_GIT) {
5ad312be
JL
716 /* These underlying connection commands die() if they
717 * cannot connect.
718 */
9befac47 719 char *target_host = xstrdup(host);
e814bc4d 720 if (git_use_proxy(host))
c78963d2 721 git_proxy_connect(fd, host);
da2a95b2 722 else
c78963d2 723 git_tcp_connect(fd, host);
5ad312be
JL
724 /*
725 * Separate original protocol components prog and path
726 * from extended components with a NUL byte.
727 */
728 packet_write(fd[1],
729 "%s %s%chost=%s%c",
730 prog, path, 0,
731 target_host, 0);
732 free(target_host);
da2a95b2
SH
733 if (free_path)
734 free(path);
5ad312be 735 return 0;
f8014776 736 }
2386d658 737
f7192598
LT
738 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
739 die("unable to create pipe pair for communication");
740 pid = fork();
c9bc159d
PD
741 if (pid < 0)
742 die("unable to fork");
f7192598 743 if (!pid) {
0f503d77
CC
744 char command[MAX_CMD_LEN];
745 char *posn = command;
746 int size = MAX_CMD_LEN;
747 int of = 0;
748
749 of |= add_to_string(&posn, &size, prog, 0);
750 of |= add_to_string(&posn, &size, " ", 0);
751 of |= add_to_string(&posn, &size, path, 1);
752
753 if (of)
754 die("command line too long");
755
f7192598
LT
756 dup2(pipefd[1][0], 0);
757 dup2(pipefd[0][1], 1);
758 close(pipefd[0][0]);
759 close(pipefd[0][1]);
760 close(pipefd[1][0]);
761 close(pipefd[1][1]);
4852f723 762 if (protocol == PROTO_SSH) {
c7c81b3a
JR
763 const char *ssh, *ssh_basename;
764 ssh = getenv("GIT_SSH");
765 if (!ssh) ssh = "ssh";
766 ssh_basename = strrchr(ssh, '/');
4852f723
MS
767 if (!ssh_basename)
768 ssh_basename = ssh;
769 else
770 ssh_basename++;
771 execlp(ssh, ssh_basename, host, command, NULL);
772 }
016fb48b
MD
773 else {
774 unsetenv(ALTERNATE_DB_ENVIRONMENT);
775 unsetenv(DB_ENVIRONMENT);
776 unsetenv(GIT_DIR_ENVIRONMENT);
777 unsetenv(GRAFT_ENVIRONMENT);
778 unsetenv(INDEX_ENVIRONMENT);
f7192598 779 execlp("sh", "sh", "-c", command, NULL);
016fb48b 780 }
f7192598 781 die("exec failed");
016fb48b 782 }
f7192598
LT
783 fd[0] = pipefd[0][0];
784 fd[1] = pipefd[1][1];
785 close(pipefd[0][1]);
786 close(pipefd[1][0]);
da2a95b2
SH
787 if (free_path)
788 free(path);
f7192598
LT
789 return pid;
790}
791
792int finish_connect(pid_t pid)
793{
f42a5c4e
FBH
794 if (pid == 0)
795 return 0;
796
53e1a761 797 while (waitpid(pid, NULL, 0) < 0) {
f7192598 798 if (errno != EINTR)
53e1a761 799 return -1;
f7192598 800 }
53e1a761 801 return 0;
f7192598 802}