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