git
/
exec-cmd.c
373 строки · 8.1 Кб
1#include "git-compat-util.h"
2#include "abspath.h"
3#include "environment.h"
4#include "exec-cmd.h"
5#include "gettext.h"
6#include "path.h"
7#include "run-command.h"
8#include "strvec.h"
9#include "trace.h"
10#include "trace2.h"
11
12#if defined(RUNTIME_PREFIX)
13
14#if defined(HAVE_NS_GET_EXECUTABLE_PATH)
15#include <mach-o/dyld.h>
16#endif
17
18#if defined(HAVE_BSD_KERN_PROC_SYSCTL)
19#include <sys/param.h>
20#include <sys/types.h>
21#include <sys/sysctl.h>
22#endif
23
24#endif /* RUNTIME_PREFIX */
25
26#define MAX_ARGS 32
27
28static const char *system_prefix(void);
29
30#ifdef RUNTIME_PREFIX
31
32/**
33* When using a runtime prefix, Git dynamically resolves paths relative to its
34* executable.
35*
36* The method for determining the path of the executable is highly
37* platform-specific.
38*/
39
40/**
41* Path to the current Git executable. Resolved on startup by
42* 'git_resolve_executable_dir'.
43*/
44static const char *executable_dirname;
45
46static const char *system_prefix(void)
47{
48static const char *prefix;
49
50assert(executable_dirname);
51assert(is_absolute_path(executable_dirname));
52
53if (!prefix &&
54!(prefix = strip_path_suffix(executable_dirname, GIT_EXEC_PATH)) &&
55!(prefix = strip_path_suffix(executable_dirname, BINDIR)) &&
56!(prefix = strip_path_suffix(executable_dirname, "git"))) {
57prefix = FALLBACK_RUNTIME_PREFIX;
58trace_printf("RUNTIME_PREFIX requested, "
59"but prefix computation failed. "
60"Using static fallback '%s'.\n", prefix);
61}
62return prefix;
63}
64
65/*
66* Resolves the executable path from argv[0], only if it is absolute.
67*
68* Returns 0 on success, -1 on failure.
69*/
70static int git_get_exec_path_from_argv0(struct strbuf *buf, const char *argv0)
71{
72const char *slash;
73
74if (!argv0 || !*argv0)
75return -1;
76
77slash = find_last_dir_sep(argv0);
78if (slash) {
79trace_printf("trace: resolved executable path from argv0: %s\n",
80argv0);
81strbuf_add_absolute_path(buf, argv0);
82return 0;
83}
84return -1;
85}
86
87#ifdef PROCFS_EXECUTABLE_PATH
88/*
89* Resolves the executable path by examining a procfs symlink.
90*
91* Returns 0 on success, -1 on failure.
92*/
93static int git_get_exec_path_procfs(struct strbuf *buf)
94{
95if (strbuf_realpath(buf, PROCFS_EXECUTABLE_PATH, 0)) {
96trace_printf(
97"trace: resolved executable path from procfs: %s\n",
98buf->buf);
99return 0;
100}
101return -1;
102}
103#endif /* PROCFS_EXECUTABLE_PATH */
104
105#ifdef HAVE_BSD_KERN_PROC_SYSCTL
106/*
107* Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
108*
109* Returns 0 on success, -1 on failure.
110*/
111static int git_get_exec_path_bsd_sysctl(struct strbuf *buf)
112{
113int mib[4];
114char path[MAXPATHLEN];
115size_t cb = sizeof(path);
116
117mib[0] = CTL_KERN;
118mib[1] = KERN_PROC;
119mib[2] = KERN_PROC_PATHNAME;
120mib[3] = -1;
121if (!sysctl(mib, 4, path, &cb, NULL, 0)) {
122trace_printf(
123"trace: resolved executable path from sysctl: %s\n",
124path);
125strbuf_addstr(buf, path);
126return 0;
127}
128return -1;
129}
130#endif /* HAVE_BSD_KERN_PROC_SYSCTL */
131
132#ifdef HAVE_NS_GET_EXECUTABLE_PATH
133/*
134* Resolves the executable path by querying Darwin application stack.
135*
136* Returns 0 on success, -1 on failure.
137*/
138static int git_get_exec_path_darwin(struct strbuf *buf)
139{
140char path[PATH_MAX];
141uint32_t size = sizeof(path);
142if (!_NSGetExecutablePath(path, &size)) {
143trace_printf(
144"trace: resolved executable path from Darwin stack: %s\n",
145path);
146strbuf_addstr(buf, path);
147return 0;
148}
149return -1;
150}
151#endif /* HAVE_NS_GET_EXECUTABLE_PATH */
152
153#ifdef HAVE_WPGMPTR
154/*
155* Resolves the executable path by using the global variable _wpgmptr.
156*
157* Returns 0 on success, -1 on failure.
158*/
159static int git_get_exec_path_wpgmptr(struct strbuf *buf)
160{
161int len = wcslen(_wpgmptr) * 3 + 1;
162strbuf_grow(buf, len);
163len = xwcstoutf(buf->buf, _wpgmptr, len);
164if (len < 0)
165return -1;
166buf->len += len;
167return 0;
168}
169#endif /* HAVE_WPGMPTR */
170
171/*
172* Resolves the absolute path of the current executable.
173*
174* Returns 0 on success, -1 on failure.
175*/
176static int git_get_exec_path(struct strbuf *buf, const char *argv0)
177{
178/*
179* Identifying the executable path is operating system specific.
180* Selectively employ all available methods in order of preference,
181* preferring highly-available authoritative methods over
182* selectively-available or non-authoritative methods.
183*
184* All cases fall back on resolving against argv[0] if there isn't a
185* better functional method. However, note that argv[0] can be
186* used-supplied on many operating systems, and is not authoritative
187* in those cases.
188*
189* Each of these functions returns 0 on success, so evaluation will stop
190* after the first successful method.
191*/
192if (
193#ifdef HAVE_BSD_KERN_PROC_SYSCTL
194git_get_exec_path_bsd_sysctl(buf) &&
195#endif /* HAVE_BSD_KERN_PROC_SYSCTL */
196
197#ifdef HAVE_NS_GET_EXECUTABLE_PATH
198git_get_exec_path_darwin(buf) &&
199#endif /* HAVE_NS_GET_EXECUTABLE_PATH */
200
201#ifdef PROCFS_EXECUTABLE_PATH
202git_get_exec_path_procfs(buf) &&
203#endif /* PROCFS_EXECUTABLE_PATH */
204
205#ifdef HAVE_WPGMPTR
206git_get_exec_path_wpgmptr(buf) &&
207#endif /* HAVE_WPGMPTR */
208
209git_get_exec_path_from_argv0(buf, argv0)) {
210return -1;
211}
212
213if (strbuf_normalize_path(buf)) {
214trace_printf("trace: could not normalize path: %s\n", buf->buf);
215return -1;
216}
217
218trace2_cmd_path(buf->buf);
219
220return 0;
221}
222
223void git_resolve_executable_dir(const char *argv0)
224{
225struct strbuf buf = STRBUF_INIT;
226char *resolved;
227const char *slash;
228
229if (git_get_exec_path(&buf, argv0)) {
230trace_printf(
231"trace: could not determine executable path from: %s\n",
232argv0);
233strbuf_release(&buf);
234return;
235}
236
237resolved = strbuf_detach(&buf, NULL);
238slash = find_last_dir_sep(resolved);
239if (slash)
240resolved[slash - resolved] = '\0';
241
242executable_dirname = resolved;
243trace_printf("trace: resolved executable dir: %s\n",
244executable_dirname);
245}
246
247#else
248
249/*
250* When not using a runtime prefix, Git uses a hard-coded path.
251*/
252static const char *system_prefix(void)
253{
254return FALLBACK_RUNTIME_PREFIX;
255}
256
257/*
258* This is called during initialization, but No work needs to be done here when
259* runtime prefix is not being used.
260*/
261void git_resolve_executable_dir(const char *argv0 UNUSED)
262{
263}
264
265#endif /* RUNTIME_PREFIX */
266
267char *system_path(const char *path)
268{
269struct strbuf d = STRBUF_INIT;
270
271if (is_absolute_path(path))
272return xstrdup(path);
273
274strbuf_addf(&d, "%s/%s", system_prefix(), path);
275return strbuf_detach(&d, NULL);
276}
277
278static const char *exec_path_value;
279
280void git_set_exec_path(const char *exec_path)
281{
282exec_path_value = exec_path;
283/*
284* Propagate this setting to external programs.
285*/
286setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
287}
288
289/* Returns the highest-priority location to look for git programs. */
290const char *git_exec_path(void)
291{
292if (!exec_path_value) {
293const char *env = getenv(EXEC_PATH_ENVIRONMENT);
294if (env && *env)
295exec_path_value = xstrdup(env);
296else
297exec_path_value = system_path(GIT_EXEC_PATH);
298}
299return exec_path_value;
300}
301
302static void add_path(struct strbuf *out, const char *path)
303{
304if (path && *path) {
305strbuf_add_absolute_path(out, path);
306strbuf_addch(out, PATH_SEP);
307}
308}
309
310void setup_path(void)
311{
312const char *exec_path = git_exec_path();
313const char *old_path = getenv("PATH");
314struct strbuf new_path = STRBUF_INIT;
315
316git_set_exec_path(exec_path);
317add_path(&new_path, exec_path);
318
319if (old_path)
320strbuf_addstr(&new_path, old_path);
321else
322strbuf_addstr(&new_path, _PATH_DEFPATH);
323
324setenv("PATH", new_path.buf, 1);
325
326strbuf_release(&new_path);
327}
328
329const char **prepare_git_cmd(struct strvec *out, const char **argv)
330{
331strvec_push(out, "git");
332strvec_pushv(out, argv);
333return out->v;
334}
335
336int execv_git_cmd(const char **argv)
337{
338struct strvec nargv = STRVEC_INIT;
339
340prepare_git_cmd(&nargv, argv);
341trace_argv_printf(nargv.v, "trace: exec:");
342
343/* execvp() can only ever return if it fails */
344sane_execvp("git", (char **)nargv.v);
345
346trace_printf("trace: exec failed: %s\n", strerror(errno));
347
348strvec_clear(&nargv);
349return -1;
350}
351
352int execl_git_cmd(const char *cmd, ...)
353{
354int argc;
355const char *argv[MAX_ARGS + 1];
356const char *arg;
357va_list param;
358
359va_start(param, cmd);
360argv[0] = cmd;
361argc = 1;
362while (argc < MAX_ARGS) {
363arg = argv[argc++] = va_arg(param, char *);
364if (!arg)
365break;
366}
367va_end(param);
368if (MAX_ARGS <= argc)
369return error(_("too many args to run %s"), cmd);
370
371argv[argc] = NULL;
372return execv_git_cmd(argv);
373}
374