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