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