@matheusbsbs / matheus-bagliano / commits / 77cb17e940
Fork 0

Exec git programs without using PATH.

The git suite may not be in PATH (and thus programs such as git-send-pack could not exec git-rev-list). Thus there is a need for logic that will locate these programs. Modifying PATH is not desirable as it result in behavior differing from the user's intentions, as we may end up prepending "/usr/bin" to PATH. - git C programs will use exec*_git_cmd() APIs to exec sub-commands. - exec*_git_cmd() will execute a git program by searching for it in the following directories: 1. --exec-path (as used by "git") 2. The GIT_EXEC_PATH environment variable. 3. $(gitexecdir) as set in Makefile (default value $(bindir)). - git wrapper will modify PATH as before to enable shell scripts to invoke "git-foo" commands. Ideally, shell scripts should use the git wrapper to become independent of PATH, and then modifying PATH will not be necessary. [jc: with minor updates after a brief review.] Signed-off-by: Michal Ostrowski <mostrows@watson.ibm.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Michal Ostrowski committed Jan 10, 2006 at 21:12 UTC 77cb17e940c806a4cc811bfd66187277b729a1e7
12 files changed +176 -62
Makefile
+9 -4
@@ -69,6 +69,7 @@ STRIP ?= strip
69
70 prefix = $(HOME)
71 bindir = $(prefix)/bin
72 +gitexecdir = $(prefix)/bin
73 template_dir = $(prefix)/share/git-core/templates/
74 GIT_PYTHON_DIR = $(prefix)/share/git-core/python
75 # DESTDIR=
@@ -142,7 +143,7 @@ PROGRAMS = \
143 git-describe$X
144
145 # what 'all' will build and 'install' will install.
145 -ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS) git$X
146 +ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
147
148 # Backward compatibility -- to be removed after 1.0
149 PROGRAMS += git-ssh-pull$X git-ssh-push$X
@@ -174,7 +175,7 @@ DIFF_OBJS = \
175
176 LIB_OBJS = \
177 blob.o commit.o connect.o count-delta.o csum-file.o \
177 - date.o diff-delta.o entry.o ident.o index.o \
178 + date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \
179 object.o pack-check.o patch-delta.o path.o pkt-line.o \
180 quote.o read-cache.o refs.o run-command.o \
181 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
@@ -367,7 +368,7 @@ LIB_OBJS += $(COMPAT_OBJS)
368 export prefix TAR INSTALL DESTDIR SHELL_PATH template_dir
369 ### Build rules
370
370 -all: $(ALL_PROGRAMS)
371 +all: $(ALL_PROGRAMS) git$X
372
373 all:
374 $(MAKE) -C templates
@@ -376,7 +377,7 @@ strip: $(PROGRAMS) git$X
377 $(STRIP) $(STRIP_OPTS) $(PROGRAMS) git$X
378
379 git$X: git.c $(LIB_FILE)
379 - $(CC) -DGIT_EXEC_PATH='"$(bindir)"' -DGIT_VERSION='"$(GIT_VERSION)"' \
380 + $(CC) -DGIT_VERSION='"$(GIT_VERSION)"' \
381 $(CFLAGS) $(COMPAT_CFLAGS) -o $@ $(filter %.c,$^) $(LIB_FILE)
382
383 $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
@@ -416,6 +417,8 @@ git$X git.spec \
417 %.o: %.S
418 $(CC) -o $*.o -c $(ALL_CFLAGS) $<
419
420 +exec_cmd.o: ALL_CFLAGS += -DGIT_EXEC_PATH=\"$(gitexecdir)\"
421 +
422 git-%$X: %.o $(LIB_FILE)
423 $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
424
@@ -472,7 +475,9 @@ check:
475
476 install: all
477 $(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(bindir))
478 + $(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(gitexecdir))
479 $(INSTALL) $(ALL_PROGRAMS) $(call shellquote,$(DESTDIR)$(bindir))
480 + $(INSTALL) git$X $(call shellquote,$(DESTDIR)$(gitexecdir))
481 $(MAKE) -C templates install
482 $(INSTALL) -d -m755 $(call shellquote,$(DESTDIR)$(GIT_PYTHON_DIR))
483 $(INSTALL) $(PYMODULES) $(call shellquote,$(DESTDIR)$(GIT_PYTHON_DIR))
daemon.c
+2 -1
@@ -9,6 +9,7 @@
9 #include <syslog.h>
10 #include "pkt-line.h"
11 #include "cache.h"
12 +#include "exec_cmd.h"
13
14 static int log_syslog;
15 static int verbose;
@@ -227,7 +228,7 @@ static int upload(char *dir)
228 snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
229
230 /* git-upload-pack only ever reads stuff, so this is safe */
230 - execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL);
231 + execl_git_cmd("upload-pack", "--strict", timeout_buf, ".", NULL);
232 return -1;
233 }
234
exec_cmd.c new
+117
@@ -0,0 +1,117 @@
1 +#include "cache.h"
2 +#include "exec_cmd.h"
3 +#define MAX_ARGS 32
4 +
5 +extern char **environ;
6 +static const char *builtin_exec_path = GIT_EXEC_PATH;
7 +static const char *current_exec_path = NULL;
8 +
9 +void git_set_exec_path(const char *exec_path)
10 +{
11 + current_exec_path = exec_path;
12 +}
13 +
14 +
15 +/* Returns the highest-priority, location to look for git programs. */
16 +const char *git_exec_path()
17 +{
18 + const char *env;
19 +
20 + if (current_exec_path)
21 + return current_exec_path;
22 +
23 + env = getenv("GIT_EXEC_PATH");
24 + if (env) {
25 + return env;
26 + }
27 +
28 + return builtin_exec_path;
29 +}
30 +
31 +
32 +int execv_git_cmd(char **argv)
33 +{
34 + char git_command[PATH_MAX + 1];
35 + char *tmp;
36 + int len, err, i;
37 + const char *paths[] = { current_exec_path,
38 + getenv("GIT_EXEC_PATH"),
39 + builtin_exec_path };
40 +
41 + for (i = 0; i < sizeof(paths)/sizeof(paths[0]); ++i) {
42 + const char *exec_dir = paths[i];
43 + if (!exec_dir) continue;
44 +
45 + if (*exec_dir != '/') {
46 + if (!getcwd(git_command, sizeof(git_command))) {
47 + fprintf(stderr, "git: cannot determine "
48 + "current directory\n");
49 + exit(1);
50 + }
51 + len = strlen(git_command);
52 +
53 + /* Trivial cleanup */
54 + while (!strncmp(exec_dir, "./", 2)) {
55 + exec_dir += 2;
56 + while (*exec_dir == '/')
57 + exec_dir++;
58 + }
59 + snprintf(git_command + len, sizeof(git_command) - len,
60 + "/%s", exec_dir);
61 + } else {
62 + strcpy(git_command, exec_dir);
63 + }
64 +
65 + len = strlen(git_command);
66 + len += snprintf(git_command + len, sizeof(git_command) - len,
67 + "/git-%s", argv[0]);
68 +
69 + if (sizeof(git_command) <= len) {
70 + fprintf(stderr,
71 + "git: command name given is too long.\n");
72 + break;
73 + }
74 +
75 + /* argv[0] must be the git command, but the argv array
76 + * belongs to the caller, and my be reused in
77 + * subsequent loop iterations. Save argv[0] and
78 + * restore it on error.
79 + */
80 +
81 + tmp = argv[0];
82 + argv[0] = git_command;
83 +
84 + /* execve() can only ever return if it fails */
85 + execve(git_command, argv, environ);
86 +
87 + err = errno;
88 +
89 + argv[0] = tmp;
90 + }
91 + return -1;
92 +
93 +}
94 +
95 +
96 +int execl_git_cmd(char *cmd,...)
97 +{
98 + int argc;
99 + char *argv[MAX_ARGS + 1];
100 + char *arg;
101 + va_list param;
102 +
103 + va_start(param, cmd);
104 + argv[0] = cmd;
105 + argc = 1;
106 + while (argc < MAX_ARGS) {
107 + arg = argv[argc++] = va_arg(param, char *);
108 + if (!arg)
109 + break;
110 + }
111 + va_end(param);
112 + if (MAX_ARGS <= argc)
113 + return error("too many args to run %s", cmd);
114 +
115 + argv[argc] = NULL;
116 + return execv_git_cmd(argv);
117 +}
exec_cmd.h new
+10
@@ -0,0 +1,10 @@
1 +#ifndef __GIT_EXEC_CMD_H_
2 +#define __GIT_EXEC_CMD_H_
3 +
4 +extern void git_set_exec_path(const char *exec_path);
5 +extern const char* git_exec_path(void);
6 +extern int execv_git_cmd(char **argv); /* NULL terminated */
7 +extern int execl_git_cmd(char *cmd, ...);
8 +
9 +
10 +#endif /* __GIT_EXEC_CMD_H_ */
fetch-clone.c
+3 -4
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "exec_cmd.h"
3 #include <sys/wait.h>
4
5 static int finish_pack(const char *pack_tmp_name, const char *me)
@@ -27,8 +28,7 @@ static int finish_pack(const char *pack_tmp_name, const char *me)
28 dup2(pipe_fd[1], 1);
29 close(pipe_fd[0]);
30 close(pipe_fd[1]);
30 - execlp("git-index-pack","git-index-pack",
31 - "-o", idx, pack_tmp_name, NULL);
31 + execl_git_cmd("index-pack", "-o", idx, pack_tmp_name, NULL);
32 error("cannot exec git-index-pack <%s> <%s>",
33 idx, pack_tmp_name);
34 exit(1);
@@ -105,8 +105,7 @@ int receive_unpack_pack(int fd[2], const char *me, int quiet)
105 dup2(fd[0], 0);
106 close(fd[0]);
107 close(fd[1]);
108 - execlp("git-unpack-objects", "git-unpack-objects",
109 - quiet ? "-q" : NULL, NULL);
108 + execl_git_cmd("unpack-objects", quiet ? "-q" : NULL, NULL);
109 die("git-unpack-objects exec failed");
110 }
111 close(fd[0]);
git.c
+11 -39
@@ -10,6 +10,7 @@
10 #include <stdarg.h>
11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
13 +#include "exec_cmd.h"
14
15 #ifndef PATH_MAX
16 # define PATH_MAX 4096
@@ -233,14 +234,11 @@ int main(int argc, char **argv, char **envp)
234 {
235 char git_command[PATH_MAX + 1];
236 char wd[PATH_MAX + 1];
236 - int i, len, show_help = 0;
237 - char *exec_path = getenv("GIT_EXEC_PATH");
237 + int i, show_help = 0;
238 + const char *exec_path;
239
240 getcwd(wd, PATH_MAX);
241
241 - if (!exec_path)
242 - exec_path = GIT_EXEC_PATH;
243 -
242 for (i = 1; i < argc; i++) {
243 char *arg = argv[i];
244
@@ -256,10 +254,11 @@ int main(int argc, char **argv, char **envp)
254
255 if (!strncmp(arg, "exec-path", 9)) {
256 arg += 9;
259 - if (*arg == '=')
257 + if (*arg == '=') {
258 exec_path = arg + 1;
261 - else {
262 - puts(exec_path);
259 + git_set_exec_path(exec_path);
260 + } else {
261 + puts(git_exec_path());
262 exit(0);
263 }
264 }
@@ -275,42 +274,15 @@ int main(int argc, char **argv, char **envp)
274
275 if (i >= argc || show_help) {
276 if (i >= argc)
278 - cmd_usage(exec_path, NULL);
277 + cmd_usage(git_exec_path(), NULL);
278
279 show_man_page(argv[i]);
280 }
281
283 - if (*exec_path != '/') {
284 - if (!getcwd(git_command, sizeof(git_command))) {
285 - fprintf(stderr,
286 - "git: cannot determine current directory\n");
287 - exit(1);
288 - }
289 - len = strlen(git_command);
290 -
291 - /* Trivial cleanup */
292 - while (!strncmp(exec_path, "./", 2)) {
293 - exec_path += 2;
294 - while (*exec_path == '/')
295 - exec_path++;
296 - }
297 - snprintf(git_command + len, sizeof(git_command) - len,
298 - "/%s", exec_path);
299 - }
300 - else
301 - strcpy(git_command, exec_path);
302 - len = strlen(git_command);
303 - prepend_to_path(git_command, len);
304 -
305 - len += snprintf(git_command + len, sizeof(git_command) - len,
306 - "/git-%s", argv[i]);
307 - if (sizeof(git_command) <= len) {
308 - fprintf(stderr, "git: command name given is too long.\n");
309 - exit(1);
310 - }
282 + exec_path = git_exec_path();
283 + prepend_to_path(exec_path, strlen(exec_path));
284
312 - /* execve() can only ever return if it fails */
313 - execve(git_command, &argv[i], envp);
285 + execv_git_cmd(argv + i);
286
287 if (errno == ENOENT)
288 cmd_usage(exec_path, "'%s' is not a git-command", argv[i]);
receive-pack.c
+2 -2
@@ -6,7 +6,7 @@
6
7 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
8
9 -static const char unpacker[] = "git-unpack-objects";
9 +static char *unpacker[] = { "unpack-objects", NULL };
10
11 static int report_status = 0;
12
@@ -257,7 +257,7 @@ static void read_head_info(void)
257
258 static const char *unpack(int *error_code)
259 {
260 - int code = run_command(unpacker, NULL);
260 + int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
261
262 *error_code = 0;
263 switch (code) {
run-command.c
+7 -2
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "run-command.h"
3 #include <sys/wait.h>
4 +#include "exec_cmd.h"
5
6 int run_command_v_opt(int argc, char **argv, int flags)
7 {
@@ -13,9 +14,13 @@ int run_command_v_opt(int argc, char **argv, int flags)
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, 0);
16 dup2(fd, 1);
16 - close(fd);
17 + close(fd);
18 + }
19 + if (flags & RUN_GIT_CMD) {
20 + execv_git_cmd(argv);
21 + } else {
22 + execvp(argv[0], (char *const*) argv);
23 }
18 - execvp(argv[0], (char *const*) argv);
24 die("exec %s failed.", argv[0]);
25 }
26 for (;;) {
run-command.h
+1 -1
@@ -12,7 +12,7 @@ enum {
12 };
13
14 #define RUN_COMMAND_NO_STDIO 1
15 -
15 +#define RUN_GIT_CMD 2 /*If this is to be git sub-command */
16 int run_command_v_opt(int argc, char **argv, int opt);
17 int run_command_v(int argc, char **argv);
18 int run_command(const char *cmd, ...);
send-pack.c
+5 -4
@@ -3,6 +3,7 @@
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 +#include "exec_cmd.h"
7
8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
@@ -26,11 +27,11 @@ static int is_zero_sha1(const unsigned char *sha1)
27 static void exec_pack_objects(void)
28 {
29 static char *args[] = {
29 - "git-pack-objects",
30 + "pack-objects",
31 "--stdout",
32 NULL
33 };
33 - execvp("git-pack-objects", args);
34 + execv_git_cmd(args);
35 die("git-pack-objects exec failed (%s)", strerror(errno));
36 }
37
@@ -39,7 +40,7 @@ static void exec_rev_list(struct ref *refs)
40 static char *args[1000];
41 int i = 0;
42
42 - args[i++] = "git-rev-list"; /* 0 */
43 + args[i++] = "rev-list"; /* 0 */
44 args[i++] = "--objects"; /* 1 */
45 while (refs) {
46 char *buf = malloc(100);
@@ -58,7 +59,7 @@ static void exec_rev_list(struct ref *refs)
59 refs = refs->next;
60 }
61 args[i] = NULL;
61 - execvp("git-rev-list", args);
62 + execv_git_cmd(args);
63 die("git-rev-list exec failed (%s)", strerror(errno));
64 }
65
shell.c
+5 -2
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "quote.h"
3 +#include "exec_cmd.h"
4
5 static int do_generic_cmd(const char *me, char *arg)
6 {
@@ -7,12 +8,14 @@ static int do_generic_cmd(const char *me, char *arg)
8
9 if (!arg || !(arg = sq_dequote(arg)))
10 die("bad argument");
11 + if (strncmp(me, "git-", 4))
12 + die("bad command");
13
11 - my_argv[0] = me;
14 + my_argv[0] = me + 4;
15 my_argv[1] = arg;
16 my_argv[2] = NULL;
17
15 - return execvp(me, (char**) my_argv);
18 + return execv_git_cmd((char**) my_argv);
19 }
20
21 static struct commands {
upload-pack.c
+4 -3
@@ -4,6 +4,7 @@
4 #include "tag.h"
5 #include "object.h"
6 #include "commit.h"
7 +#include "exec_cmd.h"
8
9 static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
10
@@ -60,7 +61,7 @@ static void create_pack_file(void)
61 close(0);
62 close(fd[0]);
63 close(fd[1]);
63 - *p++ = "git-rev-list";
64 + *p++ = "rev-list";
65 *p++ = "--objects";
66 if (create_full_pack || MAX_NEEDS <= nr_needs)
67 *p++ = "--all";
@@ -79,13 +80,13 @@ static void create_pack_file(void)
80 buf += 41;
81 }
82 *p++ = NULL;
82 - execvp("git-rev-list", argv);
83 + execv_git_cmd(argv);
84 die("git-upload-pack: unable to exec git-rev-list");
85 }
86 dup2(fd[0], 0);
87 close(fd[0]);
88 close(fd[1]);
88 - execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
89 + execl_git_cmd("pack-objects", "--stdout", NULL);
90 die("git-upload-pack: unable to exec git-pack-objects");
91 }
92