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