credential.c: store "wwwauth[]" values in `credential_read()`
[git/debian.git] / exec-cmd.c
blob6f618463896b6e840dd2620bdbd36877192854df
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"
8 #include "trace.h"
9 #include "trace2.h"
11 #if defined(RUNTIME_PREFIX)
13 #if defined(HAVE_NS_GET_EXECUTABLE_PATH)
14 #include <mach-o/dyld.h>
15 #endif
17 #if defined(HAVE_BSD_KERN_PROC_SYSCTL)
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
21 #endif
23 #endif /* RUNTIME_PREFIX */
25 #define MAX_ARGS 32
27 static const char *system_prefix(void);
29 #ifdef RUNTIME_PREFIX
31 /**
32 * When using a runtime prefix, Git dynamically resolves paths relative to its
33 * executable.
35 * The method for determining the path of the executable is highly
36 * platform-specific.
39 /**
40 * Path to the current Git executable. Resolved on startup by
41 * 'git_resolve_executable_dir'.
43 static const char *executable_dirname;
45 static const char *system_prefix(void)
47 static const char *prefix;
49 assert(executable_dirname);
50 assert(is_absolute_path(executable_dirname));
52 if (!prefix &&
53 !(prefix = strip_path_suffix(executable_dirname, GIT_EXEC_PATH)) &&
54 !(prefix = strip_path_suffix(executable_dirname, BINDIR)) &&
55 !(prefix = strip_path_suffix(executable_dirname, "git"))) {
56 prefix = FALLBACK_RUNTIME_PREFIX;
57 trace_printf("RUNTIME_PREFIX requested, "
58 "but prefix computation failed. "
59 "Using static fallback '%s'.\n", prefix);
61 return prefix;
65 * Resolves the executable path from argv[0], only if it is absolute.
67 * Returns 0 on success, -1 on failure.
69 static int git_get_exec_path_from_argv0(struct strbuf *buf, const char *argv0)
71 const char *slash;
73 if (!argv0 || !*argv0)
74 return -1;
76 slash = find_last_dir_sep(argv0);
77 if (slash) {
78 trace_printf("trace: resolved executable path from argv0: %s\n",
79 argv0);
80 strbuf_add_absolute_path(buf, argv0);
81 return 0;
83 return -1;
86 #ifdef PROCFS_EXECUTABLE_PATH
88 * Resolves the executable path by examining a procfs symlink.
90 * Returns 0 on success, -1 on failure.
92 static int git_get_exec_path_procfs(struct strbuf *buf)
94 if (strbuf_realpath(buf, PROCFS_EXECUTABLE_PATH, 0)) {
95 trace_printf(
96 "trace: resolved executable path from procfs: %s\n",
97 buf->buf);
98 return 0;
100 return -1;
102 #endif /* PROCFS_EXECUTABLE_PATH */
104 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
106 * Resolves the executable path using KERN_PROC_PATHNAME BSD sysctl.
108 * Returns 0 on success, -1 on failure.
110 static int git_get_exec_path_bsd_sysctl(struct strbuf *buf)
112 int mib[4];
113 char path[MAXPATHLEN];
114 size_t cb = sizeof(path);
116 mib[0] = CTL_KERN;
117 mib[1] = KERN_PROC;
118 mib[2] = KERN_PROC_PATHNAME;
119 mib[3] = -1;
120 if (!sysctl(mib, 4, path, &cb, NULL, 0)) {
121 trace_printf(
122 "trace: resolved executable path from sysctl: %s\n",
123 path);
124 strbuf_addstr(buf, path);
125 return 0;
127 return -1;
129 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
131 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
133 * Resolves the executable path by querying Darwin application stack.
135 * Returns 0 on success, -1 on failure.
137 static int git_get_exec_path_darwin(struct strbuf *buf)
139 char path[PATH_MAX];
140 uint32_t size = sizeof(path);
141 if (!_NSGetExecutablePath(path, &size)) {
142 trace_printf(
143 "trace: resolved executable path from Darwin stack: %s\n",
144 path);
145 strbuf_addstr(buf, path);
146 return 0;
148 return -1;
150 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
152 #ifdef HAVE_WPGMPTR
154 * Resolves the executable path by using the global variable _wpgmptr.
156 * Returns 0 on success, -1 on failure.
158 static int git_get_exec_path_wpgmptr(struct strbuf *buf)
160 int len = wcslen(_wpgmptr) * 3 + 1;
161 strbuf_grow(buf, len);
162 len = xwcstoutf(buf->buf, _wpgmptr, len);
163 if (len < 0)
164 return -1;
165 buf->len += len;
166 return 0;
168 #endif /* HAVE_WPGMPTR */
171 * Resolves the absolute path of the current executable.
173 * Returns 0 on success, -1 on failure.
175 static int git_get_exec_path(struct strbuf *buf, const char *argv0)
178 * Identifying the executable path is operating system specific.
179 * Selectively employ all available methods in order of preference,
180 * preferring highly-available authoritative methods over
181 * selectively-available or non-authoritative methods.
183 * All cases fall back on resolving against argv[0] if there isn't a
184 * better functional method. However, note that argv[0] can be
185 * used-supplied on many operating systems, and is not authoritative
186 * in those cases.
188 * Each of these functions returns 0 on success, so evaluation will stop
189 * after the first successful method.
191 if (
192 #ifdef HAVE_BSD_KERN_PROC_SYSCTL
193 git_get_exec_path_bsd_sysctl(buf) &&
194 #endif /* HAVE_BSD_KERN_PROC_SYSCTL */
196 #ifdef HAVE_NS_GET_EXECUTABLE_PATH
197 git_get_exec_path_darwin(buf) &&
198 #endif /* HAVE_NS_GET_EXECUTABLE_PATH */
200 #ifdef PROCFS_EXECUTABLE_PATH
201 git_get_exec_path_procfs(buf) &&
202 #endif /* PROCFS_EXECUTABLE_PATH */
204 #ifdef HAVE_WPGMPTR
205 git_get_exec_path_wpgmptr(buf) &&
206 #endif /* HAVE_WPGMPTR */
208 git_get_exec_path_from_argv0(buf, argv0)) {
209 return -1;
212 if (strbuf_normalize_path(buf)) {
213 trace_printf("trace: could not normalize path: %s\n", buf->buf);
214 return -1;
217 trace2_cmd_path(buf->buf);
219 return 0;
222 void git_resolve_executable_dir(const char *argv0)
224 struct strbuf buf = STRBUF_INIT;
225 char *resolved;
226 const char *slash;
228 if (git_get_exec_path(&buf, argv0)) {
229 trace_printf(
230 "trace: could not determine executable path from: %s\n",
231 argv0);
232 strbuf_release(&buf);
233 return;
236 resolved = strbuf_detach(&buf, NULL);
237 slash = find_last_dir_sep(resolved);
238 if (slash)
239 resolved[slash - resolved] = '\0';
241 executable_dirname = resolved;
242 trace_printf("trace: resolved executable dir: %s\n",
243 executable_dirname);
246 #else
249 * When not using a runtime prefix, Git uses a hard-coded path.
251 static const char *system_prefix(void)
253 return FALLBACK_RUNTIME_PREFIX;
257 * This is called during initialization, but No work needs to be done here when
258 * runtime prefix is not being used.
260 void git_resolve_executable_dir(const char *argv0 UNUSED)
264 #endif /* RUNTIME_PREFIX */
266 char *system_path(const char *path)
268 struct strbuf d = STRBUF_INIT;
270 if (is_absolute_path(path))
271 return xstrdup(path);
273 strbuf_addf(&d, "%s/%s", system_prefix(), path);
274 return strbuf_detach(&d, NULL);
277 static const char *exec_path_value;
279 void git_set_exec_path(const char *exec_path)
281 exec_path_value = exec_path;
283 * Propagate this setting to external programs.
285 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
288 /* Returns the highest-priority location to look for git programs. */
289 const char *git_exec_path(void)
291 if (!exec_path_value) {
292 const char *env = getenv(EXEC_PATH_ENVIRONMENT);
293 if (env && *env)
294 exec_path_value = xstrdup(env);
295 else
296 exec_path_value = system_path(GIT_EXEC_PATH);
298 return exec_path_value;
301 static void add_path(struct strbuf *out, const char *path)
303 if (path && *path) {
304 strbuf_add_absolute_path(out, path);
305 strbuf_addch(out, PATH_SEP);
309 void setup_path(void)
311 const char *exec_path = git_exec_path();
312 const char *old_path = getenv("PATH");
313 struct strbuf new_path = STRBUF_INIT;
315 git_set_exec_path(exec_path);
316 add_path(&new_path, exec_path);
318 if (old_path)
319 strbuf_addstr(&new_path, old_path);
320 else
321 strbuf_addstr(&new_path, _PATH_DEFPATH);
323 setenv("PATH", new_path.buf, 1);
325 strbuf_release(&new_path);
328 const char **prepare_git_cmd(struct strvec *out, const char **argv)
330 strvec_push(out, "git");
331 strvec_pushv(out, argv);
332 return out->v;
335 int execv_git_cmd(const char **argv)
337 struct strvec nargv = STRVEC_INIT;
339 prepare_git_cmd(&nargv, argv);
340 trace_argv_printf(nargv.v, "trace: exec:");
342 /* execvp() can only ever return if it fails */
343 sane_execvp("git", (char **)nargv.v);
345 trace_printf("trace: exec failed: %s\n", strerror(errno));
347 strvec_clear(&nargv);
348 return -1;
351 int execl_git_cmd(const char *cmd, ...)
353 int argc;
354 const char *argv[MAX_ARGS + 1];
355 const char *arg;
356 va_list param;
358 va_start(param, cmd);
359 argv[0] = cmd;
360 argc = 1;
361 while (argc < MAX_ARGS) {
362 arg = argv[argc++] = va_arg(param, char *);
363 if (!arg)
364 break;
366 va_end(param);
367 if (MAX_ARGS <= argc)
368 return error(_("too many args to run %s"), cmd);
370 argv[argc] = NULL;
371 return execv_git_cmd(argv);