1 #include "rsh.h"
3 #include <string.h>
4 #include <sys/socket.h>
6 #include "cache.h"
8 #define COMMAND_SIZE 4096
10 int setup_connection(int *fd_in, int *fd_out, char *remote_prog,
11 char *url, int rmt_argc, char **rmt_argv)
12 {
13 char *host;
14 char *path;
15 int sv[2];
16 char command[COMMAND_SIZE];
17 char *posn;
18 int i;
20 if (!strcmp(url, "-")) {
21 *fd_in = 0;
22 *fd_out = 1;
23 return 0;
24 }
26 host = strstr(url, "//");
27 if (!host) {
28 return error("Bad URL: %s", url);
29 }
30 host += 2;
31 path = strchr(host, '/');
32 if (!path) {
33 return error("Bad URL: %s", url);
34 }
35 *(path++) = '\0';
36 /* ssh <host> 'cd /<path>; stdio-pull <arg...> <commit-id>' */
37 snprintf(command, COMMAND_SIZE,
38 "cd /%s; SHA1_FILE_DIRECTORY=objects %s",
39 path, remote_prog);
40 posn = command + strlen(command);
41 for (i = 0; i < rmt_argc; i++) {
42 *(posn++) = ' ';
43 strncpy(posn, rmt_argv[i], COMMAND_SIZE - (posn - command));
44 posn += strlen(rmt_argv[i]);
45 if (posn - command + 4 >= COMMAND_SIZE) {
46 return error("Command line too long");
47 }
48 }
49 strcpy(posn, " -");
50 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)) {
51 return error("Couldn't create socket");
52 }
53 if (!fork()) {
54 close(sv[1]);
55 dup2(sv[0], 0);
56 dup2(sv[0], 1);
57 execlp("ssh", "ssh", host, command, NULL);
58 }
59 close(sv[0]);
60 *fd_in = sv[1];
61 *fd_out = sv[1];
62 return 0;
63 }