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