]> git.ipfire.org Git - thirdparty/git.git/blame - rsh.c
Use GIT_SSH environment to specify alternate ssh binary.
[thirdparty/git.git] / rsh.c
CommitLineData
6eb7ed54
DB
1#include "rsh.h"
2
3#include <string.h>
ec8f8116 4#include <sys/types.h>
6eb7ed54
DB
5#include <sys/socket.h>
6
7#include "cache.h"
8
9#define COMMAND_SIZE 4096
10
001d4a27 11int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
6eb7ed54
DB
12 char *url, int rmt_argc, char **rmt_argv)
13{
14 char *host;
15 char *path;
16 int sv[2];
17 char command[COMMAND_SIZE];
18 char *posn;
19 int i;
20
21 if (!strcmp(url, "-")) {
22 *fd_in = 0;
23 *fd_out = 1;
24 return 0;
25 }
26
27 host = strstr(url, "//");
001d4a27
LT
28 if (host) {
29 host += 2;
30 path = strchr(host, '/');
31 } else {
32 host = url;
33 path = strchr(host, ':');
479346ad
SV
34 if (path)
35 *(path++) = '\0';
6eb7ed54 36 }
6eb7ed54
DB
37 if (!path) {
38 return error("Bad URL: %s", url);
39 }
479346ad 40 /* ssh <host> 'cd <path>; stdio-pull <arg...> <commit-id>' */
6eb7ed54 41 snprintf(command, COMMAND_SIZE,
479346ad 42 "%s='%s' %s",
9182f89a 43 GIT_DIR_ENVIRONMENT, path, remote_prog);
44ab20cd 44 *path = '\0';
6eb7ed54
DB
45 posn = command + strlen(command);
46 for (i = 0; i < rmt_argc; i++) {
47 *(posn++) = ' ';
48 strncpy(posn, rmt_argv[i], COMMAND_SIZE - (posn - command));
49 posn += strlen(rmt_argv[i]);
50 if (posn - command + 4 >= COMMAND_SIZE) {
51 return error("Command line too long");
52 }
53 }
54 strcpy(posn, " -");
ae200ee5 55 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
6eb7ed54
DB
56 return error("Couldn't create socket");
57 }
58 if (!fork()) {
4852f723
MS
59 const char *ssh = getenv("GIT_SSH") ? : "ssh";
60 const char *ssh_basename = strrchr(ssh, '/');
61 if (!ssh_basename)
62 ssh_basename = ssh;
63 else
64 ssh_basename++;
6eb7ed54
DB
65 close(sv[1]);
66 dup2(sv[0], 0);
67 dup2(sv[0], 1);
4852f723 68 execlp(ssh, ssh_basename, host, command, NULL);
6eb7ed54
DB
69 }
70 close(sv[0]);
71 *fd_in = sv[1];
72 *fd_out = sv[1];
73 return 0;
74}