cocci: remove 'unused.cocci'
[git.git] / exec-cmd.c
blobfae0d4b244a9d37cafb8f984311b0259bf702898
1 #include "cache.h"
2 #include "abspath.h"
3 #include "environment.h"
4 #include "exec-cmd.h"
5 #include "gettext.h"
6 #include "quote.h"
7 #include "strvec.h"
9 #if defined(RUNTIME_PREFIX)
11 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
12 #include <mach-o/dyld.h>
13 #endif
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
21 #endif /* RUNTIME_PREFIX */
23 #define MAX_ARGS 32
25 static const char *system_prefix(void);
27 #ifdef RUNTIME_PREFIX
29 /**
30 * When using a runtime prefix, Git dynamically resolves paths relative to its
31 * executable.
33 * The method for determining the path of the executable is highly
34 * platform-specific.
37 /**
38 * Path to the current Git executable. Resolved on startup by
39 * 'git_resolve_executable_dir'.
41 static const char *executable_dirname;
43 static const char *system_prefix(void)
45 static const char *prefix;
47 assert(executable_dirname);
48 assert(is_absolute_path(executable_dirname));
50 if (!prefix &&
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"))) {
54 prefix = FALLBACK_RUNTIME_PREFIX;
55 trace_printf("RUNTIME_PREFIX requested, "
56 "but prefix computation failed. "
57 "Using static fallback '%s'.\n", prefix);
59 return prefix;
63 * Resolves the executable path from argv[0], only if it is absolute.
65 * Returns 0 on success, -1 on failure.
67 static int git_get_exec_path_from_argv0(struct strbuf *buf, const char *argv0)
69 const char *slash;
71 if (!argv0 || !*argv0)
72 return -1;
74 slash = find_last_dir_sep(argv0);
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;
81 return -1;
84 #ifdef PROCFS_EXECUTABLE_PATH
86 * Resolves the executable path by examining a procfs symlink.
88 * Returns 0 on success, -1 on failure.
90 static int git_get_exec_path_procfs(struct strbuf *buf)
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;
98 return -1;
100 #endif /* PROCFS_EXECUTABLE_PATH */
102 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
104 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
106 * Returns 0 on success, -1 on failure.
108 static int git_get_exec_path_bsd_sysctl(struct strbuf *buf)
110 int mib[4];
111 char path[MAXPATHLEN];
112 size_t cb = sizeof(path);
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;
125 return -1;
127 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
129 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
131 * Resolves the executable path by querying Darwin application stack.
133 * Returns 0 on success, -1 on failure.
135 static int git_get_exec_path_darwin(struct strbuf *buf)
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;
146 return -1;
148 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
150 #ifdef HAVE_WPGMPTR
152 * Resolves the executable path by using the global variable _wpgmptr.
154 * Returns 0 on success, -1 on failure.
156 static int git_get_exec_path_wpgmptr(struct strbuf *buf)
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;
166 #endif /* HAVE_WPGMPTR */
169 * Resolves the absolute path of the current executable.
171 * Returns 0 on success, -1 on failure.
173 static int git_get_exec_path(struct strbuf *buf, const char *argv0)
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.
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.
186 * Each of these functions returns 0 on success, so evaluation will stop
187 * after the first successful method.
189 if (
190 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
191 git_get_exec_path_bsd_sysctl(buf) &&
192 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
194 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
195 git_get_exec_path_darwin(buf) &&
196 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
198 #ifdef PROCFS_EXECUTABLE_PATH
199 git_get_exec_path_procfs(buf) &&
200 #endif /* PROCFS_EXECUTABLE_PATH */
202 #ifdef HAVE_WPGMPTR
203 git_get_exec_path_wpgmptr(buf) &&
204 #endif /* HAVE_WPGMPTR */
206 git_get_exec_path_from_argv0(buf, argv0)) {
207 return -1;
210 if (strbuf_normalize_path(buf)) {
211 trace_printf("trace: could not normalize path: %s\n", buf->buf);
212 return -1;
215 trace2_cmd_path(buf->buf);
217 return 0;
220 void git_resolve_executable_dir(const char *argv0)
222 struct strbuf buf = STRBUF_INIT;
223 char *resolved;
224 const char *slash;
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;
234 resolved = strbuf_detach(&buf, NULL);
235 slash = find_last_dir_sep(resolved);
236 if (slash)
237 resolved[slash - resolved] = '\0';
239 executable_dirname = resolved;
240 trace_printf("trace: resolved executable dir: %s\n",
241 executable_dirname);
244 #else
247 * When not using a runtime prefix, Git uses a hard-coded path.
249 static const char *system_prefix(void)
251 return FALLBACK_RUNTIME_PREFIX;
255 * This is called during initialization, but No work needs to be done here when
256 * runtime prefix is not being used.
258 void git_resolve_executable_dir(const char *argv0 UNUSED)
262 #endif /* RUNTIME_PREFIX */
264 char *system_path(const char *path)
266 struct strbuf d = STRBUF_INIT;
268 if (is_absolute_path(path))
269 return xstrdup(path);
271 strbuf_addf(&d, "%s/%s", system_prefix(), path);
272 return strbuf_detach(&d, NULL);
275 static const char *exec_path_value;
277 void git_set_exec_path(const char *exec_path)
279 exec_path_value = exec_path;
281 * Propagate this setting to external programs.
283 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
286 /* Returns the highest-priority location to look for git programs. */
287 const char *git_exec_path(void)
289 if (!exec_path_value) {
290 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
291 if (env && *env)
292 exec_path_value = xstrdup(env);
293 else
294 exec_path_value = system_path(GIT_EXEC_PATH);
296 return exec_path_value;
299 static void add_path(struct strbuf *out, const char *path)
301 if (path && *path) {
302 strbuf_add_absolute_path(out, path);
303 strbuf_addch(out, PATH_SEP);
307 void setup_path(void)
309 const char *exec_path = git_exec_path();
310 const char *old_path = getenv("PATH");
311 struct strbuf new_path = STRBUF_INIT;
313 git_set_exec_path(exec_path);
314 add_path(&new_path, exec_path);
316 if (old_path)
317 strbuf_addstr(&new_path, old_path);
318 else
319 strbuf_addstr(&new_path, _PATH_DEFPATH);
321 setenv("PATH", new_path.buf, 1);
323 strbuf_release(&new_path);
326 const char **prepare_git_cmd(struct strvec *out, const char **argv)
328 strvec_push(out, "git");
329 strvec_pushv(out, argv);
330 return out->v;
333 int execv_git_cmd(const char **argv)
335 struct strvec nargv = STRVEC_INIT;
337 prepare_git_cmd(&nargv, argv);
338 trace_argv_printf(nargv.v, "trace: exec:");
340 /* execvp() can only ever return if it fails */
341 sane_execvp("git", (char **)nargv.v);
343 trace_printf("trace: exec failed: %s\n", strerror(errno));
345 strvec_clear(&nargv);
346 return -1;
349 int execl_git_cmd(const char *cmd, ...)
351 int argc;
352 const char *argv[MAX_ARGS + 1];
353 const char *arg;
354 va_list param;
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;
364 va_end(param);
365 if (MAX_ARGS <= argc)
366 return error(_("too many args to run %s"), cmd);
368 argv[argc] = NULL;
369 return execv_git_cmd(argv);