]>
Commit | Line | Data |
---|---|---|
35eb2d36 LT |
1 | #include "cache.h" |
2 | #include "quote.h" | |
77cb17e9 | 3 | #include "exec_cmd.h" |
0c696fe7 | 4 | #include "strbuf.h" |
35eb2d36 | 5 | |
5b8e6f85 DP |
6 | /* Stubs for functions that make no sense for git-shell. These stubs |
7 | * are provided here to avoid linking in external redundant modules. | |
8 | */ | |
9 | void release_pack_memory(size_t need, int fd){} | |
10 | void trace_argv_printf(const char **argv, const char *fmt, ...){} | |
11 | void trace_printf(const char *fmt, ...){} | |
12 | ||
13 | ||
35eb2d36 LT |
14 | static int do_generic_cmd(const char *me, char *arg) |
15 | { | |
16 | const char *my_argv[4]; | |
17 | ||
e1464ca7 | 18 | setup_path(); |
ab5f8627 | 19 | if (!arg || !(arg = sq_dequote(arg))) |
35eb2d36 | 20 | die("bad argument"); |
cc44c765 | 21 | if (prefixcmp(me, "git-")) |
77cb17e9 | 22 | die("bad command"); |
35eb2d36 | 23 | |
77cb17e9 | 24 | my_argv[0] = me + 4; |
35eb2d36 LT |
25 | my_argv[1] = arg; |
26 | my_argv[2] = NULL; | |
27 | ||
9201c707 | 28 | return execv_git_cmd(my_argv); |
35eb2d36 LT |
29 | } |
30 | ||
0c696fe7 JS |
31 | static int do_cvs_cmd(const char *me, char *arg) |
32 | { | |
33 | const char *cvsserver_argv[3] = { | |
34 | "cvsserver", "server", NULL | |
35 | }; | |
0c696fe7 JS |
36 | |
37 | if (!arg || strcmp(arg, "server")) | |
38 | die("git-cvsserver only handles server: %s", arg); | |
39 | ||
e1464ca7 | 40 | setup_path(); |
0c696fe7 JS |
41 | return execv_git_cmd(cvsserver_argv); |
42 | } | |
43 | ||
44 | ||
35eb2d36 LT |
45 | static struct commands { |
46 | const char *name; | |
47 | int (*exec)(const char *me, char *arg); | |
48 | } cmd_list[] = { | |
49 | { "git-receive-pack", do_generic_cmd }, | |
50 | { "git-upload-pack", do_generic_cmd }, | |
0c696fe7 | 51 | { "cvs", do_cvs_cmd }, |
35eb2d36 LT |
52 | { NULL }, |
53 | }; | |
54 | ||
55 | int main(int argc, char **argv) | |
56 | { | |
57 | char *prog; | |
58 | struct commands *cmd; | |
0cfeed2e PB |
59 | int devnull_fd; |
60 | ||
61 | /* | |
62 | * Always open file descriptors 0/1/2 to avoid clobbering files | |
63 | * in die(). It also avoids not messing up when the pipes are | |
64 | * dup'ed onto stdin/stdout/stderr in the child processes we spawn. | |
65 | */ | |
66 | devnull_fd = open("/dev/null", O_RDWR); | |
67 | while (devnull_fd >= 0 && devnull_fd <= 2) | |
68 | devnull_fd = dup(devnull_fd); | |
69 | if (devnull_fd == -1) | |
70 | die("opening /dev/null failed (%s)", strerror(errno)); | |
71 | close (devnull_fd); | |
35eb2d36 | 72 | |
bc7c73e2 JH |
73 | /* |
74 | * Special hack to pretend to be a CVS server | |
75 | */ | |
0c696fe7 JS |
76 | if (argc == 2 && !strcmp(argv[1], "cvs server")) |
77 | argv--; | |
bc7c73e2 JH |
78 | |
79 | /* | |
80 | * We do not accept anything but "-c" followed by "cmd arg", | |
81 | * where "cmd" is a very limited subset of git commands. | |
82 | */ | |
0c696fe7 | 83 | else if (argc != 3 || strcmp(argv[1], "-c")) |
35eb2d36 LT |
84 | die("What do you think I am? A shell?"); |
85 | ||
86 | prog = argv[2]; | |
bc7c73e2 JH |
87 | if (!strncmp(prog, "git", 3) && isspace(prog[3])) |
88 | /* Accept "git foo" as if the caller said "git-foo". */ | |
89 | prog[3] = '-'; | |
90 | ||
35eb2d36 LT |
91 | for (cmd = cmd_list ; cmd->name ; cmd++) { |
92 | int len = strlen(cmd->name); | |
93 | char *arg; | |
94 | if (strncmp(cmd->name, prog, len)) | |
95 | continue; | |
96 | arg = NULL; | |
97 | switch (prog[len]) { | |
98 | case '\0': | |
99 | arg = NULL; | |
100 | break; | |
101 | case ' ': | |
102 | arg = prog + len + 1; | |
103 | break; | |
104 | default: | |
105 | continue; | |
106 | } | |
107 | exit(cmd->exec(cmd->name, arg)); | |
108 | } | |
109 | die("unrecognized command '%s'", prog); | |
110 | } |