]>
Commit | Line | Data |
---|---|---|
64c85595 | 1 | #include "git-compat-util.h" |
0b027f6c | 2 | #include "abspath.h" |
32a8f510 | 3 | #include "environment.h" |
d807c4a0 | 4 | #include "exec-cmd.h" |
f394e093 | 5 | #include "gettext.h" |
d1cbe1e6 | 6 | #include "path.h" |
575ba9d6 | 7 | #include "quote.h" |
64c85595 | 8 | #include "run-command.h" |
dbbcd44f | 9 | #include "strvec.h" |
74ea5c95 EN |
10 | #include "trace.h" |
11 | #include "trace2.h" | |
77cb17e9 | 12 | |
226c0ddd DJ |
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); | |
77cb17e9 | 30 | |
35fb0e86 | 31 | #ifdef RUNTIME_PREFIX |
226c0ddd DJ |
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; | |
026fa0d5 | 46 | |
39b2f6af JK |
47 | static const char *system_prefix(void) |
48 | { | |
49 | static const char *prefix; | |
026fa0d5 | 50 | |
226c0ddd DJ |
51 | assert(executable_dirname); |
52 | assert(is_absolute_path(executable_dirname)); | |
35fb0e86 | 53 | |
024aa7d8 | 54 | if (!prefix && |
226c0ddd DJ |
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"))) { | |
4d5b4c24 | 58 | prefix = FALLBACK_RUNTIME_PREFIX; |
aa094570 | 59 | trace_printf("RUNTIME_PREFIX requested, " |
35fb0e86 SP |
60 | "but prefix computation failed. " |
61 | "Using static fallback '%s'.\n", prefix); | |
62 | } | |
39b2f6af JK |
63 | return prefix; |
64 | } | |
c1bb33c9 | 65 | |
226c0ddd DJ |
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) | |
c1bb33c9 JK |
72 | { |
73 | const char *slash; | |
74 | ||
75 | if (!argv0 || !*argv0) | |
226c0ddd | 76 | return -1; |
c1bb33c9 JK |
77 | |
78 | slash = find_last_dir_sep(argv0); | |
226c0ddd DJ |
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 | } | |
c1bb33c9 | 87 | |
226c0ddd DJ |
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 | ||
c1be1cb7 JS |
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 | ||
226c0ddd DJ |
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 | ||
c1be1cb7 JS |
206 | #ifdef HAVE_WPGMPTR |
207 | git_get_exec_path_wpgmptr(buf) && | |
208 | #endif /* HAVE_WPGMPTR */ | |
209 | ||
226c0ddd DJ |
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 | ||
ee4512ed JH |
219 | trace2_cmd_path(buf->buf); |
220 | ||
226c0ddd DJ |
221 | return 0; |
222 | } | |
c1bb33c9 | 223 | |
226c0ddd DJ |
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); | |
c1bb33c9 | 240 | if (slash) |
226c0ddd DJ |
241 | resolved[slash - resolved] = '\0'; |
242 | ||
243 | executable_dirname = resolved; | |
244 | trace_printf("trace: resolved executable dir: %s\n", | |
245 | executable_dirname); | |
c1bb33c9 JK |
246 | } |
247 | ||
39b2f6af JK |
248 | #else |
249 | ||
226c0ddd DJ |
250 | /* |
251 | * When not using a runtime prefix, Git uses a hard-coded path. | |
252 | */ | |
39b2f6af JK |
253 | static const char *system_prefix(void) |
254 | { | |
4d5b4c24 | 255 | return FALLBACK_RUNTIME_PREFIX; |
39b2f6af JK |
256 | } |
257 | ||
226c0ddd DJ |
258 | /* |
259 | * This is called during initialization, but No work needs to be done here when | |
260 | * runtime prefix is not being used. | |
261 | */ | |
808e9195 | 262 | void git_resolve_executable_dir(const char *argv0 UNUSED) |
c1bb33c9 JK |
263 | { |
264 | } | |
265 | ||
39b2f6af JK |
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); | |
35fb0e86 | 274 | |
39b2f6af | 275 | strbuf_addf(&d, "%s/%s", system_prefix(), path); |
59362e56 | 276 | return strbuf_detach(&d, NULL); |
2de9de5e SP |
277 | } |
278 | ||
226c0ddd DJ |
279 | static const char *exec_path_value; |
280 | ||
281 | void git_set_exec_path(const char *exec_path) | |
77cb17e9 | 282 | { |
226c0ddd | 283 | exec_path_value = exec_path; |
c90d565a JS |
284 | /* |
285 | * Propagate this setting to external programs. | |
286 | */ | |
287 | setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1); | |
77cb17e9 MO |
288 | } |
289 | ||
226c0ddd | 290 | /* Returns the highest-priority location to look for git programs. */ |
962554c6 | 291 | const char *git_exec_path(void) |
77cb17e9 | 292 | { |
226c0ddd | 293 | if (!exec_path_value) { |
007ac544 JK |
294 | const char *env = getenv(EXEC_PATH_ENVIRONMENT); |
295 | if (env && *env) | |
226c0ddd | 296 | exec_path_value = xstrdup(env); |
007ac544 | 297 | else |
226c0ddd | 298 | exec_path_value = system_path(GIT_EXEC_PATH); |
77cb17e9 | 299 | } |
226c0ddd | 300 | return exec_path_value; |
77cb17e9 MO |
301 | } |
302 | ||
511707d4 SP |
303 | static void add_path(struct strbuf *out, const char *path) |
304 | { | |
305 | if (path && *path) { | |
9610decf | 306 | strbuf_add_absolute_path(out, path); |
80ba074f | 307 | strbuf_addch(out, PATH_SEP); |
511707d4 SP |
308 | } |
309 | } | |
310 | ||
e1464ca7 | 311 | void setup_path(void) |
511707d4 | 312 | { |
226c0ddd | 313 | const char *exec_path = git_exec_path(); |
511707d4 | 314 | const char *old_path = getenv("PATH"); |
f285a2d7 | 315 | struct strbuf new_path = STRBUF_INIT; |
511707d4 | 316 | |
226c0ddd DJ |
317 | git_set_exec_path(exec_path); |
318 | add_path(&new_path, exec_path); | |
511707d4 SP |
319 | |
320 | if (old_path) | |
321 | strbuf_addstr(&new_path, old_path); | |
322 | else | |
cb6a22c0 | 323 | strbuf_addstr(&new_path, _PATH_DEFPATH); |
511707d4 SP |
324 | |
325 | setenv("PATH", new_path.buf, 1); | |
326 | ||
327 | strbuf_release(&new_path); | |
328 | } | |
77cb17e9 | 329 | |
ef8d7ac4 | 330 | const char **prepare_git_cmd(struct strvec *out, const char **argv) |
77cb17e9 | 331 | { |
ef8d7ac4 JK |
332 | strvec_push(out, "git"); |
333 | strvec_pushv(out, argv); | |
d70a9eb6 | 334 | return out->v; |
4933e5eb SP |
335 | } |
336 | ||
226c0ddd DJ |
337 | int execv_git_cmd(const char **argv) |
338 | { | |
ef8d7ac4 | 339 | struct strvec nargv = STRVEC_INIT; |
20574f55 JK |
340 | |
341 | prepare_git_cmd(&nargv, argv); | |
d70a9eb6 | 342 | trace_argv_printf(nargv.v, "trace: exec:"); |
575ba9d6 | 343 | |
511707d4 | 344 | /* execvp() can only ever return if it fails */ |
d70a9eb6 | 345 | sane_execvp("git", (char **)nargv.v); |
77cb17e9 | 346 | |
511707d4 | 347 | trace_printf("trace: exec failed: %s\n", strerror(errno)); |
575ba9d6 | 348 | |
ef8d7ac4 | 349 | strvec_clear(&nargv); |
511707d4 | 350 | return -1; |
77cb17e9 MO |
351 | } |
352 | ||
226c0ddd | 353 | int execl_git_cmd(const char *cmd, ...) |
77cb17e9 MO |
354 | { |
355 | int argc; | |
9201c707 JH |
356 | const char *argv[MAX_ARGS + 1]; |
357 | const char *arg; | |
77cb17e9 MO |
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) | |
31a55e91 | 370 | return error(_("too many args to run %s"), cmd); |
77cb17e9 MO |
371 | |
372 | argv[argc] = NULL; | |
373 | return execv_git_cmd(argv); | |
374 | } |