]>
Commit | Line | Data |
---|---|---|
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" |
15a1c012 | 6 | #include "run-command.h" |
6b62816c | 7 | #include "remote.h" |
f7192598 | 8 | |
96f1e58f | 9 | static char *server_capabilities; |
211b5f9e | 10 | |
2718ff09 LT |
11 | static int check_ref(const char *name, int len, unsigned int flags) |
12 | { | |
13 | if (!flags) | |
14 | return 1; | |
15 | ||
c41e20b3 | 16 | if (len < 5 || memcmp(name, "refs/", 5)) |
2718ff09 LT |
17 | return 0; |
18 | ||
19 | /* Skip the "refs/" part */ | |
20 | name += 5; | |
21 | len -= 5; | |
22 | ||
23 | /* REF_NORMAL means that we don't want the magic fake tag refs */ | |
24 | if ((flags & REF_NORMAL) && check_ref_format(name) < 0) | |
25 | return 0; | |
26 | ||
27 | /* REF_HEADS means that we want regular branch heads */ | |
28 | if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6)) | |
29 | return 1; | |
30 | ||
31 | /* REF_TAGS means that we want tags */ | |
32 | if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5)) | |
33 | return 1; | |
34 | ||
35 | /* All type bits clear means that we are ok with anything */ | |
36 | return !(flags & ~REF_NORMAL); | |
37 | } | |
38 | ||
d1c133f5 LT |
39 | /* |
40 | * Read all the refs from the other end | |
41 | */ | |
1a7141ff | 42 | struct ref **get_remote_heads(int in, struct ref **list, |
2718ff09 LT |
43 | int nr_match, char **match, |
44 | unsigned int flags) | |
d1c133f5 LT |
45 | { |
46 | *list = NULL; | |
47 | for (;;) { | |
48 | struct ref *ref; | |
49 | unsigned char old_sha1[20]; | |
50 | static char buffer[1000]; | |
51 | char *name; | |
211b5f9e | 52 | int len, name_len; |
d1c133f5 LT |
53 | |
54 | len = packet_read_line(in, buffer, sizeof(buffer)); | |
55 | if (!len) | |
56 | break; | |
57 | if (buffer[len-1] == '\n') | |
58 | buffer[--len] = 0; | |
59 | ||
60 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') | |
61 | die("protocol error: expected sha/ref, got '%s'", buffer); | |
62 | name = buffer + 41; | |
1a7141ff | 63 | |
211b5f9e JS |
64 | name_len = strlen(name); |
65 | if (len != name_len + 41) { | |
66 | if (server_capabilities) | |
67 | free(server_capabilities); | |
9befac47 | 68 | server_capabilities = xstrdup(name + name_len + 1); |
211b5f9e JS |
69 | } |
70 | ||
2718ff09 | 71 | if (!check_ref(name, name_len, flags)) |
cfee10a7 | 72 | continue; |
d1c133f5 LT |
73 | if (nr_match && !path_match(name, nr_match, match)) |
74 | continue; | |
90446a00 | 75 | ref = alloc_ref(name_len + 1); |
e702496e | 76 | hashcpy(ref->old_sha1, old_sha1); |
90446a00 | 77 | memcpy(ref->name, buffer + 41, name_len + 1); |
d1c133f5 LT |
78 | *list = ref; |
79 | list = &ref->next; | |
80 | } | |
81 | return list; | |
82 | } | |
83 | ||
211b5f9e JS |
84 | int server_supports(const char *feature) |
85 | { | |
1f5881bb JS |
86 | return server_capabilities && |
87 | strstr(server_capabilities, feature) != NULL; | |
211b5f9e JS |
88 | } |
89 | ||
41cb7488 LT |
90 | int get_ack(int fd, unsigned char *result_sha1) |
91 | { | |
92 | static char line[1000]; | |
93 | int len = packet_read_line(fd, line, sizeof(line)); | |
94 | ||
95 | if (!len) | |
96 | die("git-fetch-pack: expected ACK/NAK, got EOF"); | |
97 | if (line[len-1] == '\n') | |
98 | line[--len] = 0; | |
99 | if (!strcmp(line, "NAK")) | |
100 | return 0; | |
cc44c765 | 101 | if (!prefixcmp(line, "ACK ")) { |
c4c86f07 JS |
102 | if (!get_sha1_hex(line+4, result_sha1)) { |
103 | if (strstr(line+45, "continue")) | |
104 | return 2; | |
41cb7488 | 105 | return 1; |
c4c86f07 | 106 | } |
41cb7488 LT |
107 | } |
108 | die("git-fetch_pack: expected ACK/NAK, got '%s'", line); | |
109 | } | |
110 | ||
013e7c7f LT |
111 | int path_match(const char *path, int nr, char **match) |
112 | { | |
113 | int i; | |
114 | int pathlen = strlen(path); | |
115 | ||
116 | for (i = 0; i < nr; i++) { | |
117 | char *s = match[i]; | |
118 | int len = strlen(s); | |
119 | ||
120 | if (!len || len > pathlen) | |
121 | continue; | |
122 | if (memcmp(path + pathlen - len, s, len)) | |
123 | continue; | |
124 | if (pathlen > len && path[pathlen - len - 1] != '/') | |
125 | continue; | |
126 | *s = 0; | |
9546010b | 127 | return (i + 1); |
013e7c7f LT |
128 | } |
129 | return 0; | |
130 | } | |
131 | ||
2386d658 LT |
132 | enum protocol { |
133 | PROTO_LOCAL = 1, | |
134 | PROTO_SSH, | |
135 | PROTO_GIT, | |
136 | }; | |
137 | ||
138 | static enum protocol get_protocol(const char *name) | |
139 | { | |
140 | if (!strcmp(name, "ssh")) | |
141 | return PROTO_SSH; | |
142 | if (!strcmp(name, "git")) | |
143 | return PROTO_GIT; | |
c05186cc LT |
144 | if (!strcmp(name, "git+ssh")) |
145 | return PROTO_SSH; | |
146 | if (!strcmp(name, "ssh+git")) | |
147 | return PROTO_SSH; | |
72a4f4b6 LT |
148 | if (!strcmp(name, "file")) |
149 | return PROTO_LOCAL; | |
2386d658 LT |
150 | die("I don't handle protocol '%s'", name); |
151 | } | |
152 | ||
5ba88448 YH |
153 | #define STR_(s) # s |
154 | #define STR(s) STR_(s) | |
2386d658 | 155 | |
49744d63 | 156 | #ifndef NO_IPV6 |
4c505f71 | 157 | |
ba505322 AR |
158 | static const char *ai_name(const struct addrinfo *ai) |
159 | { | |
160 | static char addr[INET_ADDRSTRLEN]; | |
161 | if ( AF_INET == ai->ai_family ) { | |
162 | struct sockaddr_in *in; | |
163 | in = (struct sockaddr_in *)ai->ai_addr; | |
164 | inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr)); | |
165 | } else if ( AF_INET6 == ai->ai_family ) { | |
166 | struct sockaddr_in6 *in; | |
167 | in = (struct sockaddr_in6 *)ai->ai_addr; | |
168 | inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr)); | |
169 | } else { | |
170 | strcpy(addr, "(unknown)"); | |
171 | } | |
172 | return addr; | |
173 | } | |
174 | ||
5ad312be JL |
175 | /* |
176 | * Returns a connected socket() fd, or else die()s. | |
177 | */ | |
7841ce79 | 178 | static int git_tcp_connect_sock(char *host, int flags) |
2386d658 | 179 | { |
ac3bc6c1 | 180 | int sockfd = -1, saved_errno = 0; |
5ba88448 | 181 | char *colon, *end; |
554fe20d | 182 | const char *port = STR(DEFAULT_GIT_PORT); |
5ba88448 YH |
183 | struct addrinfo hints, *ai0, *ai; |
184 | int gai; | |
ba505322 | 185 | int cnt = 0; |
5ba88448 YH |
186 | |
187 | if (host[0] == '[') { | |
188 | end = strchr(host + 1, ']'); | |
189 | if (end) { | |
190 | *end = 0; | |
191 | end++; | |
192 | host++; | |
193 | } else | |
194 | end = host; | |
195 | } else | |
196 | end = host; | |
197 | colon = strchr(end, ':'); | |
198 | ||
ce6f8e7e LT |
199 | if (colon) { |
200 | *colon = 0; | |
5ba88448 | 201 | port = colon + 1; |
608d48b2 LT |
202 | if (!*port) |
203 | port = "<none>"; | |
ce6f8e7e | 204 | } |
5ba88448 YH |
205 | |
206 | memset(&hints, 0, sizeof(hints)); | |
207 | hints.ai_socktype = SOCK_STREAM; | |
208 | hints.ai_protocol = IPPROTO_TCP; | |
209 | ||
7841ce79 MT |
210 | if (flags & CONNECT_VERBOSE) |
211 | fprintf(stderr, "Looking up %s ... ", host); | |
212 | ||
5ba88448 YH |
213 | gai = getaddrinfo(host, port, &hints, &ai); |
214 | if (gai) | |
608d48b2 | 215 | die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai)); |
5ba88448 | 216 | |
7841ce79 MT |
217 | if (flags & CONNECT_VERBOSE) |
218 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
219 | ||
5ba88448 | 220 | for (ai0 = ai; ai; ai = ai->ai_next) { |
5ad312be JL |
221 | sockfd = socket(ai->ai_family, |
222 | ai->ai_socktype, ai->ai_protocol); | |
ac3bc6c1 PB |
223 | if (sockfd < 0) { |
224 | saved_errno = errno; | |
5ba88448 | 225 | continue; |
ac3bc6c1 | 226 | } |
5ba88448 | 227 | if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
ac3bc6c1 | 228 | saved_errno = errno; |
7cbf2f24 | 229 | fprintf(stderr, "%s[%d: %s]: errno=%s\n", |
ba505322 AR |
230 | host, |
231 | cnt, | |
232 | ai_name(ai), | |
ba505322 | 233 | strerror(saved_errno)); |
5ba88448 YH |
234 | close(sockfd); |
235 | sockfd = -1; | |
236 | continue; | |
2386d658 | 237 | } |
ba505322 AR |
238 | if (flags & CONNECT_VERBOSE) |
239 | fprintf(stderr, "%s ", ai_name(ai)); | |
5ba88448 | 240 | break; |
2386d658 LT |
241 | } |
242 | ||
5ba88448 | 243 | freeaddrinfo(ai0); |
2386d658 | 244 | |
2386d658 | 245 | if (sockfd < 0) |
ac3bc6c1 | 246 | die("unable to connect a socket (%s)", strerror(saved_errno)); |
5ba88448 | 247 | |
7841ce79 MT |
248 | if (flags & CONNECT_VERBOSE) |
249 | fprintf(stderr, "done.\n"); | |
250 | ||
5ad312be | 251 | return sockfd; |
2386d658 LT |
252 | } |
253 | ||
49744d63 | 254 | #else /* NO_IPV6 */ |
4c505f71 | 255 | |
5ad312be JL |
256 | /* |
257 | * Returns a connected socket() fd, or else die()s. | |
258 | */ | |
7841ce79 | 259 | static int git_tcp_connect_sock(char *host, int flags) |
4c505f71 | 260 | { |
ac3bc6c1 | 261 | int sockfd = -1, saved_errno = 0; |
4c505f71 PA |
262 | char *colon, *end; |
263 | char *port = STR(DEFAULT_GIT_PORT), *ep; | |
264 | struct hostent *he; | |
265 | struct sockaddr_in sa; | |
266 | char **ap; | |
267 | unsigned int nport; | |
ba505322 | 268 | int cnt; |
4c505f71 PA |
269 | |
270 | if (host[0] == '[') { | |
271 | end = strchr(host + 1, ']'); | |
272 | if (end) { | |
273 | *end = 0; | |
274 | end++; | |
275 | host++; | |
276 | } else | |
277 | end = host; | |
278 | } else | |
279 | end = host; | |
280 | colon = strchr(end, ':'); | |
281 | ||
282 | if (colon) { | |
283 | *colon = 0; | |
284 | port = colon + 1; | |
285 | } | |
286 | ||
7841ce79 MT |
287 | if (flags & CONNECT_VERBOSE) |
288 | fprintf(stderr, "Looking up %s ... ", host); | |
289 | ||
4c505f71 PA |
290 | he = gethostbyname(host); |
291 | if (!he) | |
292 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); | |
293 | nport = strtoul(port, &ep, 10); | |
294 | if ( ep == port || *ep ) { | |
295 | /* Not numeric */ | |
296 | struct servent *se = getservbyname(port,"tcp"); | |
297 | if ( !se ) | |
298 | die("Unknown port %s\n", port); | |
299 | nport = se->s_port; | |
300 | } | |
301 | ||
7841ce79 MT |
302 | if (flags & CONNECT_VERBOSE) |
303 | fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port); | |
304 | ||
ba505322 | 305 | for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) { |
4c505f71 | 306 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
ac3bc6c1 PB |
307 | if (sockfd < 0) { |
308 | saved_errno = errno; | |
4c505f71 | 309 | continue; |
ac3bc6c1 | 310 | } |
4c505f71 PA |
311 | |
312 | memset(&sa, 0, sizeof sa); | |
313 | sa.sin_family = he->h_addrtype; | |
6573faff | 314 | sa.sin_port = htons(nport); |
c6164218 | 315 | memcpy(&sa.sin_addr, *ap, he->h_length); |
4c505f71 PA |
316 | |
317 | if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { | |
ac3bc6c1 | 318 | saved_errno = errno; |
7cbf2f24 | 319 | fprintf(stderr, "%s[%d: %s]: errno=%s\n", |
ba505322 AR |
320 | host, |
321 | cnt, | |
322 | inet_ntoa(*(struct in_addr *)&sa.sin_addr), | |
ba505322 | 323 | strerror(saved_errno)); |
4c505f71 PA |
324 | close(sockfd); |
325 | sockfd = -1; | |
326 | continue; | |
327 | } | |
ba505322 AR |
328 | if (flags & CONNECT_VERBOSE) |
329 | fprintf(stderr, "%s ", | |
330 | inet_ntoa(*(struct in_addr *)&sa.sin_addr)); | |
4c505f71 PA |
331 | break; |
332 | } | |
333 | ||
334 | if (sockfd < 0) | |
ac3bc6c1 | 335 | die("unable to connect a socket (%s)", strerror(saved_errno)); |
4c505f71 | 336 | |
7841ce79 MT |
337 | if (flags & CONNECT_VERBOSE) |
338 | fprintf(stderr, "done.\n"); | |
339 | ||
5ad312be JL |
340 | return sockfd; |
341 | } | |
342 | ||
343 | #endif /* NO_IPV6 */ | |
344 | ||
345 | ||
7841ce79 | 346 | static void git_tcp_connect(int fd[2], char *host, int flags) |
5ad312be | 347 | { |
7841ce79 | 348 | int sockfd = git_tcp_connect_sock(host, flags); |
5ad312be | 349 | |
4c505f71 | 350 | fd[0] = sockfd; |
ec587fde | 351 | fd[1] = dup(sockfd); |
4c505f71 PA |
352 | } |
353 | ||
4c505f71 | 354 | |
96f1e58f DR |
355 | static char *git_proxy_command; |
356 | static const char *rhost_name; | |
e814bc4d | 357 | static int rhost_len; |
f8014776 PC |
358 | |
359 | static int git_proxy_command_options(const char *var, const char *value) | |
360 | { | |
e814bc4d | 361 | if (!strcmp(var, "core.gitproxy")) { |
c3df8568 YH |
362 | const char *for_pos; |
363 | int matchlen = -1; | |
364 | int hostlen; | |
365 | ||
e814bc4d | 366 | if (git_proxy_command) |
f8014776 | 367 | return 0; |
e814bc4d JH |
368 | /* [core] |
369 | * ;# matches www.kernel.org as well | |
370 | * gitproxy = netcatter-1 for kernel.org | |
371 | * gitproxy = netcatter-2 for sample.xz | |
372 | * gitproxy = netcatter-default | |
373 | */ | |
c3df8568 | 374 | for_pos = strstr(value, " for "); |
e814bc4d JH |
375 | if (!for_pos) |
376 | /* matches everybody */ | |
377 | matchlen = strlen(value); | |
378 | else { | |
379 | hostlen = strlen(for_pos + 5); | |
380 | if (rhost_len < hostlen) | |
381 | matchlen = -1; | |
382 | else if (!strncmp(for_pos + 5, | |
383 | rhost_name + rhost_len - hostlen, | |
384 | hostlen) && | |
385 | ((rhost_len == hostlen) || | |
386 | rhost_name[rhost_len - hostlen -1] == '.')) | |
387 | matchlen = for_pos - value; | |
388 | else | |
389 | matchlen = -1; | |
390 | } | |
391 | if (0 <= matchlen) { | |
392 | /* core.gitproxy = none for kernel.org */ | |
a6080a0a | 393 | if (matchlen == 4 && |
e814bc4d JH |
394 | !memcmp(value, "none", 4)) |
395 | matchlen = 0; | |
182af834 | 396 | git_proxy_command = xmemdupz(value, matchlen); |
f8014776 | 397 | } |
e814bc4d | 398 | return 0; |
f8014776 PC |
399 | } |
400 | ||
401 | return git_default_config(var, value); | |
402 | } | |
403 | ||
e814bc4d | 404 | static int git_use_proxy(const char *host) |
f8014776 | 405 | { |
e814bc4d JH |
406 | rhost_name = host; |
407 | rhost_len = strlen(host); | |
f8014776 PC |
408 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
409 | git_config(git_proxy_command_options); | |
e814bc4d JH |
410 | rhost_name = NULL; |
411 | return (git_proxy_command && *git_proxy_command); | |
f8014776 PC |
412 | } |
413 | ||
c78963d2 | 414 | static void git_proxy_connect(int fd[2], char *host) |
f8014776 | 415 | { |
554fe20d | 416 | const char *port = STR(DEFAULT_GIT_PORT); |
f8014776 | 417 | char *colon, *end; |
15a1c012 SP |
418 | const char *argv[4]; |
419 | struct child_process proxy; | |
f8014776 PC |
420 | |
421 | if (host[0] == '[') { | |
422 | end = strchr(host + 1, ']'); | |
423 | if (end) { | |
424 | *end = 0; | |
425 | end++; | |
426 | host++; | |
427 | } else | |
428 | end = host; | |
429 | } else | |
430 | end = host; | |
431 | colon = strchr(end, ':'); | |
432 | ||
433 | if (colon) { | |
434 | *colon = 0; | |
435 | port = colon + 1; | |
436 | } | |
437 | ||
15a1c012 SP |
438 | argv[0] = git_proxy_command; |
439 | argv[1] = host; | |
440 | argv[2] = port; | |
441 | argv[3] = NULL; | |
442 | memset(&proxy, 0, sizeof(proxy)); | |
443 | proxy.argv = argv; | |
444 | proxy.in = -1; | |
445 | proxy.out = -1; | |
446 | if (start_command(&proxy)) | |
447 | die("cannot start proxy %s", argv[0]); | |
448 | fd[0] = proxy.out; /* read from proxy stdout */ | |
449 | fd[1] = proxy.in; /* write to proxy stdin */ | |
f8014776 PC |
450 | } |
451 | ||
0f503d77 CC |
452 | #define MAX_CMD_LEN 1024 |
453 | ||
2e776665 LT |
454 | char *get_port(char *host) |
455 | { | |
456 | char *end; | |
457 | char *p = strchr(host, ':'); | |
458 | ||
459 | if (p) { | |
460 | strtol(p+1, &end, 10); | |
461 | if (*end == '\0') { | |
462 | *p = '\0'; | |
463 | return p+1; | |
464 | } | |
465 | } | |
466 | ||
467 | return NULL; | |
468 | } | |
469 | ||
f7192598 | 470 | /* |
98158e9c JS |
471 | * This returns NULL if the transport protocol does not need fork(2), or a |
472 | * struct child_process object if it does. Once done, finish the connection | |
f42a5c4e | 473 | * with finish_connect() with the value returned from this function |
98158e9c | 474 | * (it is safe to call finish_connect() with NULL to support the former |
f42a5c4e FBH |
475 | * case). |
476 | * | |
98158e9c | 477 | * If it returns, the connect is successful; it just dies on errors. |
f7192598 | 478 | */ |
98158e9c JS |
479 | struct child_process *git_connect(int fd[2], char *url, |
480 | const char *prog, int flags) | |
f7192598 | 481 | { |
faea9ccb | 482 | char *host, *path = url; |
356bece0 YH |
483 | char *end; |
484 | int c; | |
f7192598 | 485 | int pipefd[2][2]; |
98158e9c | 486 | struct child_process *conn; |
faea9ccb | 487 | enum protocol protocol = PROTO_LOCAL; |
da2a95b2 | 488 | int free_path = 0; |
2e776665 | 489 | char *port = NULL; |
faea9ccb | 490 | |
f0b7367c JH |
491 | /* Without this we cannot rely on waitpid() to tell |
492 | * what happened to our children. | |
493 | */ | |
494 | signal(SIGCHLD, SIG_DFL); | |
495 | ||
faea9ccb AE |
496 | host = strstr(url, "://"); |
497 | if(host) { | |
498 | *host = '\0'; | |
499 | protocol = get_protocol(url); | |
500 | host += 3; | |
356bece0 YH |
501 | c = '/'; |
502 | } else { | |
f7192598 | 503 | host = url; |
356bece0 YH |
504 | c = ':'; |
505 | } | |
506 | ||
507 | if (host[0] == '[') { | |
508 | end = strchr(host + 1, ']'); | |
509 | if (end) { | |
510 | *end = 0; | |
511 | end++; | |
512 | host++; | |
513 | } else | |
514 | end = host; | |
515 | } else | |
516 | end = host; | |
517 | ||
518 | path = strchr(end, c); | |
72a4f4b6 LT |
519 | if (path) { |
520 | if (c == ':') { | |
faea9ccb | 521 | protocol = PROTO_SSH; |
356bece0 | 522 | *path++ = '\0'; |
72a4f4b6 LT |
523 | } |
524 | } else | |
525 | path = end; | |
2386d658 | 526 | |
faea9ccb AE |
527 | if (!path || !*path) |
528 | die("No path specified. See 'man git-pull' for valid url syntax"); | |
529 | ||
530 | /* | |
531 | * null-terminate hostname and point path to ~ for URL's like this: | |
532 | * ssh://host.xz/~user/repo | |
533 | */ | |
534 | if (protocol != PROTO_LOCAL && host != url) { | |
535 | char *ptr = path; | |
536 | if (path[1] == '~') | |
537 | path++; | |
da2a95b2 | 538 | else { |
9befac47 | 539 | path = xstrdup(ptr); |
da2a95b2 SH |
540 | free_path = 1; |
541 | } | |
faea9ccb AE |
542 | |
543 | *ptr = '\0'; | |
544 | } | |
545 | ||
2e776665 LT |
546 | /* |
547 | * Add support for ssh port: ssh://host.xy:<port>/... | |
548 | */ | |
549 | if (protocol == PROTO_SSH && host != url) | |
550 | port = get_port(host); | |
551 | ||
f8014776 | 552 | if (protocol == PROTO_GIT) { |
5ad312be JL |
553 | /* These underlying connection commands die() if they |
554 | * cannot connect. | |
555 | */ | |
9befac47 | 556 | char *target_host = xstrdup(host); |
e814bc4d | 557 | if (git_use_proxy(host)) |
c78963d2 | 558 | git_proxy_connect(fd, host); |
da2a95b2 | 559 | else |
7841ce79 | 560 | git_tcp_connect(fd, host, flags); |
5ad312be JL |
561 | /* |
562 | * Separate original protocol components prog and path | |
563 | * from extended components with a NUL byte. | |
564 | */ | |
565 | packet_write(fd[1], | |
566 | "%s %s%chost=%s%c", | |
567 | prog, path, 0, | |
568 | target_host, 0); | |
569 | free(target_host); | |
da2a95b2 SH |
570 | if (free_path) |
571 | free(path); | |
98158e9c | 572 | return NULL; |
f8014776 | 573 | } |
2386d658 | 574 | |
f7192598 LT |
575 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) |
576 | die("unable to create pipe pair for communication"); | |
98158e9c JS |
577 | conn = xcalloc(1, sizeof(*conn)); |
578 | conn->pid = fork(); | |
579 | if (conn->pid < 0) | |
c9bc159d | 580 | die("unable to fork"); |
98158e9c | 581 | if (!conn->pid) { |
7a33bcbe | 582 | struct strbuf cmd; |
0f503d77 | 583 | |
7a33bcbe PH |
584 | strbuf_init(&cmd, MAX_CMD_LEN); |
585 | strbuf_addstr(&cmd, prog); | |
586 | strbuf_addch(&cmd, ' '); | |
587 | sq_quote_buf(&cmd, path); | |
588 | if (cmd.len >= MAX_CMD_LEN) | |
0f503d77 CC |
589 | die("command line too long"); |
590 | ||
f7192598 LT |
591 | dup2(pipefd[1][0], 0); |
592 | dup2(pipefd[0][1], 1); | |
593 | close(pipefd[0][0]); | |
594 | close(pipefd[0][1]); | |
595 | close(pipefd[1][0]); | |
596 | close(pipefd[1][1]); | |
4852f723 | 597 | if (protocol == PROTO_SSH) { |
c7c81b3a JR |
598 | const char *ssh, *ssh_basename; |
599 | ssh = getenv("GIT_SSH"); | |
600 | if (!ssh) ssh = "ssh"; | |
601 | ssh_basename = strrchr(ssh, '/'); | |
4852f723 MS |
602 | if (!ssh_basename) |
603 | ssh_basename = ssh; | |
604 | else | |
605 | ssh_basename++; | |
2e776665 LT |
606 | |
607 | if (!port) | |
7a33bcbe | 608 | execlp(ssh, ssh_basename, host, cmd.buf, NULL); |
2e776665 LT |
609 | else |
610 | execlp(ssh, ssh_basename, "-p", port, host, | |
7a33bcbe | 611 | cmd.buf, NULL); |
4852f723 | 612 | } |
016fb48b MD |
613 | else { |
614 | unsetenv(ALTERNATE_DB_ENVIRONMENT); | |
615 | unsetenv(DB_ENVIRONMENT); | |
616 | unsetenv(GIT_DIR_ENVIRONMENT); | |
892c41b9 | 617 | unsetenv(GIT_WORK_TREE_ENVIRONMENT); |
016fb48b MD |
618 | unsetenv(GRAFT_ENVIRONMENT); |
619 | unsetenv(INDEX_ENVIRONMENT); | |
7a33bcbe | 620 | execlp("sh", "sh", "-c", cmd.buf, NULL); |
016fb48b | 621 | } |
f7192598 | 622 | die("exec failed"); |
016fb48b | 623 | } |
f7192598 LT |
624 | fd[0] = pipefd[0][0]; |
625 | fd[1] = pipefd[1][1]; | |
626 | close(pipefd[0][1]); | |
627 | close(pipefd[1][0]); | |
da2a95b2 SH |
628 | if (free_path) |
629 | free(path); | |
98158e9c | 630 | return conn; |
f7192598 LT |
631 | } |
632 | ||
98158e9c | 633 | int finish_connect(struct child_process *conn) |
f7192598 | 634 | { |
98158e9c | 635 | if (!conn) |
f42a5c4e FBH |
636 | return 0; |
637 | ||
98158e9c | 638 | while (waitpid(conn->pid, NULL, 0) < 0) { |
f7192598 | 639 | if (errno != EINTR) |
53e1a761 | 640 | return -1; |
f7192598 | 641 | } |
98158e9c | 642 | free(conn); |
53e1a761 | 643 | return 0; |
f7192598 | 644 | } |