Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 1 | #include "cache.h" |
H. Peter Anvin | f336af1 | 2005-10-10 14:46:14 -0700 | [diff] [blame] | 2 | #include "rsh.h" |
| 3 | #include "quote.h" |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 4 | |
| 5 | #define COMMAND_SIZE 4096 |
| 6 | |
Christian Couder | 86257aa | 2006-09-11 06:59:22 +0200 | [diff] [blame] | 7 | int setup_connection(int *fd_in, int *fd_out, const char *remote_prog, |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 8 | char *url, int rmt_argc, char **rmt_argv) |
| 9 | { |
| 10 | char *host; |
| 11 | char *path; |
| 12 | int sv[2]; |
| 13 | char command[COMMAND_SIZE]; |
| 14 | char *posn; |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 15 | int sizen; |
| 16 | int of; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 17 | int i; |
Paul T Darga | c9bc159 | 2006-06-08 14:14:47 -0400 | [diff] [blame] | 18 | pid_t pid; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 19 | |
| 20 | if (!strcmp(url, "-")) { |
| 21 | *fd_in = 0; |
| 22 | *fd_out = 1; |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | host = strstr(url, "//"); |
Linus Torvalds | 001d4a2 | 2005-06-07 14:23:46 -0700 | [diff] [blame] | 27 | if (host) { |
| 28 | host += 2; |
| 29 | path = strchr(host, '/'); |
| 30 | } else { |
| 31 | host = url; |
| 32 | path = strchr(host, ':'); |
Sven Verdoolaege | 479346a | 2005-06-14 12:37:38 +0200 | [diff] [blame] | 33 | if (path) |
| 34 | *(path++) = '\0'; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 35 | } |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 36 | if (!path) { |
| 37 | return error("Bad URL: %s", url); |
| 38 | } |
Ralf Baechle | 2573808 | 2005-10-13 10:01:38 -0700 | [diff] [blame] | 39 | /* $GIT_RSH <host> "env GIT_DIR=<path> <remote_prog> <args...>" */ |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 40 | sizen = COMMAND_SIZE; |
| 41 | posn = command; |
| 42 | of = 0; |
| 43 | of |= add_to_string(&posn, &sizen, "env ", 0); |
H. Peter Anvin | 977ed47 | 2005-10-10 14:46:12 -0700 | [diff] [blame] | 44 | of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT "=", 0); |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 45 | of |= add_to_string(&posn, &sizen, path, 1); |
| 46 | of |= add_to_string(&posn, &sizen, " ", 0); |
| 47 | of |= add_to_string(&posn, &sizen, remote_prog, 1); |
| 48 | |
| 49 | for ( i = 0 ; i < rmt_argc ; i++ ) { |
| 50 | of |= add_to_string(&posn, &sizen, " ", 0); |
| 51 | of |= add_to_string(&posn, &sizen, rmt_argv[i], 1); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 52 | } |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 53 | |
| 54 | of |= add_to_string(&posn, &sizen, " -", 0); |
| 55 | |
| 56 | if ( of ) |
| 57 | return error("Command line too long"); |
| 58 | |
| 59 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 60 | return error("Couldn't create socket"); |
H. Peter Anvin | 0de68d2 | 2005-09-15 12:33:14 -0700 | [diff] [blame] | 61 | |
Paul T Darga | c9bc159 | 2006-06-08 14:14:47 -0400 | [diff] [blame] | 62 | pid = fork(); |
| 63 | if (pid < 0) |
| 64 | return error("Couldn't fork"); |
| 65 | if (!pid) { |
Jason Riedy | c7c81b3 | 2005-08-23 13:34:07 -0700 | [diff] [blame] | 66 | const char *ssh, *ssh_basename; |
| 67 | ssh = getenv("GIT_SSH"); |
| 68 | if (!ssh) ssh = "ssh"; |
| 69 | ssh_basename = strrchr(ssh, '/'); |
Martin Sivak | 4852f72 | 2005-08-03 17:15:42 +0200 | [diff] [blame] | 70 | if (!ssh_basename) |
| 71 | ssh_basename = ssh; |
| 72 | else |
| 73 | ssh_basename++; |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 74 | close(sv[1]); |
| 75 | dup2(sv[0], 0); |
| 76 | dup2(sv[0], 1); |
Martin Sivak | 4852f72 | 2005-08-03 17:15:42 +0200 | [diff] [blame] | 77 | execlp(ssh, ssh_basename, host, command, NULL); |
Daniel Barkalow | 6eb7ed5 | 2005-04-23 18:47:23 -0700 | [diff] [blame] | 78 | } |
| 79 | close(sv[0]); |
| 80 | *fd_in = sv[1]; |
| 81 | *fd_out = sv[1]; |
| 82 | return 0; |
| 83 | } |