exec-cmd.c 8.0 KB

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