run-command.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. #include "cache.h"
  2. #include "run-command.h"
  3. #include "exec-cmd.h"
  4. #include "sigchain.h"
  5. #include "strvec.h"
  6. #include "thread-utils.h"
  7. #include "strbuf.h"
  8. #include "string-list.h"
  9. #include "quote.h"
  10. void child_process_init(struct child_process *child)
  11. {
  12. memset(child, 0, sizeof(*child));
  13. strvec_init(&child->args);
  14. strvec_init(&child->env_array);
  15. }
  16. void child_process_clear(struct child_process *child)
  17. {
  18. strvec_clear(&child->args);
  19. strvec_clear(&child->env_array);
  20. }
  21. struct child_to_clean {
  22. pid_t pid;
  23. struct child_process *process;
  24. struct child_to_clean *next;
  25. };
  26. static struct child_to_clean *children_to_clean;
  27. static int installed_child_cleanup_handler;
  28. static void cleanup_children(int sig, int in_signal)
  29. {
  30. struct child_to_clean *children_to_wait_for = NULL;
  31. while (children_to_clean) {
  32. struct child_to_clean *p = children_to_clean;
  33. children_to_clean = p->next;
  34. if (p->process && !in_signal) {
  35. struct child_process *process = p->process;
  36. if (process->clean_on_exit_handler) {
  37. trace_printf(
  38. "trace: run_command: running exit handler for pid %"
  39. PRIuMAX, (uintmax_t)p->pid
  40. );
  41. process->clean_on_exit_handler(process);
  42. }
  43. }
  44. kill(p->pid, sig);
  45. if (p->process && p->process->wait_after_clean) {
  46. p->next = children_to_wait_for;
  47. children_to_wait_for = p;
  48. } else {
  49. if (!in_signal)
  50. free(p);
  51. }
  52. }
  53. while (children_to_wait_for) {
  54. struct child_to_clean *p = children_to_wait_for;
  55. children_to_wait_for = p->next;
  56. while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
  57. ; /* spin waiting for process exit or error */
  58. if (!in_signal)
  59. free(p);
  60. }
  61. }
  62. static void cleanup_children_on_signal(int sig)
  63. {
  64. cleanup_children(sig, 1);
  65. sigchain_pop(sig);
  66. raise(sig);
  67. }
  68. static void cleanup_children_on_exit(void)
  69. {
  70. cleanup_children(SIGTERM, 0);
  71. }
  72. static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
  73. {
  74. struct child_to_clean *p = xmalloc(sizeof(*p));
  75. p->pid = pid;
  76. p->process = process;
  77. p->next = children_to_clean;
  78. children_to_clean = p;
  79. if (!installed_child_cleanup_handler) {
  80. atexit(cleanup_children_on_exit);
  81. sigchain_push_common(cleanup_children_on_signal);
  82. installed_child_cleanup_handler = 1;
  83. }
  84. }
  85. static void clear_child_for_cleanup(pid_t pid)
  86. {
  87. struct child_to_clean **pp;
  88. for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
  89. struct child_to_clean *clean_me = *pp;
  90. if (clean_me->pid == pid) {
  91. *pp = clean_me->next;
  92. free(clean_me);
  93. return;
  94. }
  95. }
  96. }
  97. static inline void close_pair(int fd[2])
  98. {
  99. close(fd[0]);
  100. close(fd[1]);
  101. }
  102. int is_executable(const char *name)
  103. {
  104. struct stat st;
  105. if (stat(name, &st) || /* stat, not lstat */
  106. !S_ISREG(st.st_mode))
  107. return 0;
  108. #if defined(GIT_WINDOWS_NATIVE)
  109. /*
  110. * On Windows there is no executable bit. The file extension
  111. * indicates whether it can be run as an executable, and Git
  112. * has special-handling to detect scripts and launch them
  113. * through the indicated script interpreter. We test for the
  114. * file extension first because virus scanners may make
  115. * it quite expensive to open many files.
  116. */
  117. if (ends_with(name, ".exe"))
  118. return S_IXUSR;
  119. {
  120. /*
  121. * Now that we know it does not have an executable extension,
  122. * peek into the file instead.
  123. */
  124. char buf[3] = { 0 };
  125. int n;
  126. int fd = open(name, O_RDONLY);
  127. st.st_mode &= ~S_IXUSR;
  128. if (fd >= 0) {
  129. n = read(fd, buf, 2);
  130. if (n == 2)
  131. /* look for a she-bang */
  132. if (!strcmp(buf, "#!"))
  133. st.st_mode |= S_IXUSR;
  134. close(fd);
  135. }
  136. }
  137. #endif
  138. return st.st_mode & S_IXUSR;
  139. }
  140. /*
  141. * Search $PATH for a command. This emulates the path search that
  142. * execvp would perform, without actually executing the command so it
  143. * can be used before fork() to prepare to run a command using
  144. * execve() or after execvp() to diagnose why it failed.
  145. *
  146. * The caller should ensure that file contains no directory
  147. * separators.
  148. *
  149. * Returns the path to the command, as found in $PATH or NULL if the
  150. * command could not be found. The caller inherits ownership of the memory
  151. * used to store the resultant path.
  152. *
  153. * This should not be used on Windows, where the $PATH search rules
  154. * are more complicated (e.g., a search for "foo" should find
  155. * "foo.exe").
  156. */
  157. static char *locate_in_PATH(const char *file)
  158. {
  159. const char *p = getenv("PATH");
  160. struct strbuf buf = STRBUF_INIT;
  161. if (!p || !*p)
  162. return NULL;
  163. while (1) {
  164. const char *end = strchrnul(p, ':');
  165. strbuf_reset(&buf);
  166. /* POSIX specifies an empty entry as the current directory. */
  167. if (end != p) {
  168. strbuf_add(&buf, p, end - p);
  169. strbuf_addch(&buf, '/');
  170. }
  171. strbuf_addstr(&buf, file);
  172. if (is_executable(buf.buf))
  173. return strbuf_detach(&buf, NULL);
  174. if (!*end)
  175. break;
  176. p = end + 1;
  177. }
  178. strbuf_release(&buf);
  179. return NULL;
  180. }
  181. static int exists_in_PATH(const char *file)
  182. {
  183. char *r = locate_in_PATH(file);
  184. int found = r != NULL;
  185. free(r);
  186. return found;
  187. }
  188. int sane_execvp(const char *file, char * const argv[])
  189. {
  190. #ifndef GIT_WINDOWS_NATIVE
  191. /*
  192. * execvp() doesn't return, so we all we can do is tell trace2
  193. * what we are about to do and let it leave a hint in the log
  194. * (unless of course the execvp() fails).
  195. *
  196. * we skip this for Windows because the compat layer already
  197. * has to emulate the execvp() call anyway.
  198. */
  199. int exec_id = trace2_exec(file, (const char **)argv);
  200. #endif
  201. if (!execvp(file, argv))
  202. return 0; /* cannot happen ;-) */
  203. #ifndef GIT_WINDOWS_NATIVE
  204. {
  205. int ec = errno;
  206. trace2_exec_result(exec_id, ec);
  207. errno = ec;
  208. }
  209. #endif
  210. /*
  211. * When a command can't be found because one of the directories
  212. * listed in $PATH is unsearchable, execvp reports EACCES, but
  213. * careful usability testing (read: analysis of occasional bug
  214. * reports) reveals that "No such file or directory" is more
  215. * intuitive.
  216. *
  217. * We avoid commands with "/", because execvp will not do $PATH
  218. * lookups in that case.
  219. *
  220. * The reassignment of EACCES to errno looks like a no-op below,
  221. * but we need to protect against exists_in_PATH overwriting errno.
  222. */
  223. if (errno == EACCES && !strchr(file, '/'))
  224. errno = exists_in_PATH(file) ? EACCES : ENOENT;
  225. else if (errno == ENOTDIR && !strchr(file, '/'))
  226. errno = ENOENT;
  227. return -1;
  228. }
  229. static const char **prepare_shell_cmd(struct strvec *out, const char **argv)
  230. {
  231. if (!argv[0])
  232. BUG("shell command is empty");
  233. if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
  234. #ifndef GIT_WINDOWS_NATIVE
  235. strvec_push(out, SHELL_PATH);
  236. #else
  237. strvec_push(out, "sh");
  238. #endif
  239. strvec_push(out, "-c");
  240. /*
  241. * If we have no extra arguments, we do not even need to
  242. * bother with the "$@" magic.
  243. */
  244. if (!argv[1])
  245. strvec_push(out, argv[0]);
  246. else
  247. strvec_pushf(out, "%s \"$@\"", argv[0]);
  248. }
  249. strvec_pushv(out, argv);
  250. return out->v;
  251. }
  252. #ifndef GIT_WINDOWS_NATIVE
  253. static int child_notifier = -1;
  254. enum child_errcode {
  255. CHILD_ERR_CHDIR,
  256. CHILD_ERR_DUP2,
  257. CHILD_ERR_CLOSE,
  258. CHILD_ERR_SIGPROCMASK,
  259. CHILD_ERR_ENOENT,
  260. CHILD_ERR_SILENT,
  261. CHILD_ERR_ERRNO
  262. };
  263. struct child_err {
  264. enum child_errcode err;
  265. int syserr; /* errno */
  266. };
  267. static void child_die(enum child_errcode err)
  268. {
  269. struct child_err buf;
  270. buf.err = err;
  271. buf.syserr = errno;
  272. /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */
  273. xwrite(child_notifier, &buf, sizeof(buf));
  274. _exit(1);
  275. }
  276. static void child_dup2(int fd, int to)
  277. {
  278. if (dup2(fd, to) < 0)
  279. child_die(CHILD_ERR_DUP2);
  280. }
  281. static void child_close(int fd)
  282. {
  283. if (close(fd))
  284. child_die(CHILD_ERR_CLOSE);
  285. }
  286. static void child_close_pair(int fd[2])
  287. {
  288. child_close(fd[0]);
  289. child_close(fd[1]);
  290. }
  291. /*
  292. * parent will make it look like the child spewed a fatal error and died
  293. * this is needed to prevent changes to t0061.
  294. */
  295. static void fake_fatal(const char *err, va_list params)
  296. {
  297. vreportf("fatal: ", err, params);
  298. }
  299. static void child_error_fn(const char *err, va_list params)
  300. {
  301. const char msg[] = "error() should not be called in child\n";
  302. xwrite(2, msg, sizeof(msg) - 1);
  303. }
  304. static void child_warn_fn(const char *err, va_list params)
  305. {
  306. const char msg[] = "warn() should not be called in child\n";
  307. xwrite(2, msg, sizeof(msg) - 1);
  308. }
  309. static void NORETURN child_die_fn(const char *err, va_list params)
  310. {
  311. const char msg[] = "die() should not be called in child\n";
  312. xwrite(2, msg, sizeof(msg) - 1);
  313. _exit(2);
  314. }
  315. /* this runs in the parent process */
  316. static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
  317. {
  318. static void (*old_errfn)(const char *err, va_list params);
  319. old_errfn = get_error_routine();
  320. set_error_routine(fake_fatal);
  321. errno = cerr->syserr;
  322. switch (cerr->err) {
  323. case CHILD_ERR_CHDIR:
  324. error_errno("exec '%s': cd to '%s' failed",
  325. cmd->argv[0], cmd->dir);
  326. break;
  327. case CHILD_ERR_DUP2:
  328. error_errno("dup2() in child failed");
  329. break;
  330. case CHILD_ERR_CLOSE:
  331. error_errno("close() in child failed");
  332. break;
  333. case CHILD_ERR_SIGPROCMASK:
  334. error_errno("sigprocmask failed restoring signals");
  335. break;
  336. case CHILD_ERR_ENOENT:
  337. error_errno("cannot run %s", cmd->argv[0]);
  338. break;
  339. case CHILD_ERR_SILENT:
  340. break;
  341. case CHILD_ERR_ERRNO:
  342. error_errno("cannot exec '%s'", cmd->argv[0]);
  343. break;
  344. }
  345. set_error_routine(old_errfn);
  346. }
  347. static int prepare_cmd(struct strvec *out, const struct child_process *cmd)
  348. {
  349. if (!cmd->argv[0])
  350. BUG("command is empty");
  351. /*
  352. * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
  353. * attempt to interpret the command with 'sh'.
  354. */
  355. strvec_push(out, SHELL_PATH);
  356. if (cmd->git_cmd) {
  357. prepare_git_cmd(out, cmd->argv);
  358. } else if (cmd->use_shell) {
  359. prepare_shell_cmd(out, cmd->argv);
  360. } else {
  361. strvec_pushv(out, cmd->argv);
  362. }
  363. /*
  364. * If there are no dir separator characters in the command then perform
  365. * a path lookup and use the resolved path as the command to exec. If
  366. * there are dir separator characters, we have exec attempt to invoke
  367. * the command directly.
  368. */
  369. if (!has_dir_sep(out->v[1])) {
  370. char *program = locate_in_PATH(out->v[1]);
  371. if (program) {
  372. free((char *)out->v[1]);
  373. out->v[1] = program;
  374. } else {
  375. strvec_clear(out);
  376. errno = ENOENT;
  377. return -1;
  378. }
  379. }
  380. return 0;
  381. }
  382. static char **prep_childenv(const char *const *deltaenv)
  383. {
  384. extern char **environ;
  385. char **childenv;
  386. struct string_list env = STRING_LIST_INIT_DUP;
  387. struct strbuf key = STRBUF_INIT;
  388. const char *const *p;
  389. int i;
  390. /* Construct a sorted string list consisting of the current environ */
  391. for (p = (const char *const *) environ; p && *p; p++) {
  392. const char *equals = strchr(*p, '=');
  393. if (equals) {
  394. strbuf_reset(&key);
  395. strbuf_add(&key, *p, equals - *p);
  396. string_list_append(&env, key.buf)->util = (void *) *p;
  397. } else {
  398. string_list_append(&env, *p)->util = (void *) *p;
  399. }
  400. }
  401. string_list_sort(&env);
  402. /* Merge in 'deltaenv' with the current environ */
  403. for (p = deltaenv; p && *p; p++) {
  404. const char *equals = strchr(*p, '=');
  405. if (equals) {
  406. /* ('key=value'), insert or replace entry */
  407. strbuf_reset(&key);
  408. strbuf_add(&key, *p, equals - *p);
  409. string_list_insert(&env, key.buf)->util = (void *) *p;
  410. } else {
  411. /* otherwise ('key') remove existing entry */
  412. string_list_remove(&env, *p, 0);
  413. }
  414. }
  415. /* Create an array of 'char *' to be used as the childenv */
  416. ALLOC_ARRAY(childenv, env.nr + 1);
  417. for (i = 0; i < env.nr; i++)
  418. childenv[i] = env.items[i].util;
  419. childenv[env.nr] = NULL;
  420. string_list_clear(&env, 0);
  421. strbuf_release(&key);
  422. return childenv;
  423. }
  424. struct atfork_state {
  425. #ifndef NO_PTHREADS
  426. int cs;
  427. #endif
  428. sigset_t old;
  429. };
  430. #define CHECK_BUG(err, msg) \
  431. do { \
  432. int e = (err); \
  433. if (e) \
  434. BUG("%s: %s", msg, strerror(e)); \
  435. } while(0)
  436. static void atfork_prepare(struct atfork_state *as)
  437. {
  438. sigset_t all;
  439. if (sigfillset(&all))
  440. die_errno("sigfillset");
  441. #ifdef NO_PTHREADS
  442. if (sigprocmask(SIG_SETMASK, &all, &as->old))
  443. die_errno("sigprocmask");
  444. #else
  445. CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old),
  446. "blocking all signals");
  447. CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
  448. "disabling cancellation");
  449. #endif
  450. }
  451. static void atfork_parent(struct atfork_state *as)
  452. {
  453. #ifdef NO_PTHREADS
  454. if (sigprocmask(SIG_SETMASK, &as->old, NULL))
  455. die_errno("sigprocmask");
  456. #else
  457. CHECK_BUG(pthread_setcancelstate(as->cs, NULL),
  458. "re-enabling cancellation");
  459. CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
  460. "restoring signal mask");
  461. #endif
  462. }
  463. #endif /* GIT_WINDOWS_NATIVE */
  464. static inline void set_cloexec(int fd)
  465. {
  466. int flags = fcntl(fd, F_GETFD);
  467. if (flags >= 0)
  468. fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  469. }
  470. static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
  471. {
  472. int status, code = -1;
  473. pid_t waiting;
  474. int failed_errno = 0;
  475. while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
  476. ; /* nothing */
  477. if (in_signal)
  478. return 0;
  479. if (waiting < 0) {
  480. failed_errno = errno;
  481. error_errno("waitpid for %s failed", argv0);
  482. } else if (waiting != pid) {
  483. error("waitpid is confused (%s)", argv0);
  484. } else if (WIFSIGNALED(status)) {
  485. code = WTERMSIG(status);
  486. if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
  487. error("%s died of signal %d", argv0, code);
  488. /*
  489. * This return value is chosen so that code & 0xff
  490. * mimics the exit code that a POSIX shell would report for
  491. * a program that died from this signal.
  492. */
  493. code += 128;
  494. } else if (WIFEXITED(status)) {
  495. code = WEXITSTATUS(status);
  496. } else {
  497. error("waitpid is confused (%s)", argv0);
  498. }
  499. clear_child_for_cleanup(pid);
  500. errno = failed_errno;
  501. return code;
  502. }
  503. static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
  504. {
  505. struct string_list envs = STRING_LIST_INIT_DUP;
  506. const char *const *e;
  507. int i;
  508. int printed_unset = 0;
  509. /* Last one wins, see run-command.c:prep_childenv() for context */
  510. for (e = deltaenv; e && *e; e++) {
  511. struct strbuf key = STRBUF_INIT;
  512. char *equals = strchr(*e, '=');
  513. if (equals) {
  514. strbuf_add(&key, *e, equals - *e);
  515. string_list_insert(&envs, key.buf)->util = equals + 1;
  516. } else {
  517. string_list_insert(&envs, *e)->util = NULL;
  518. }
  519. strbuf_release(&key);
  520. }
  521. /* "unset X Y...;" */
  522. for (i = 0; i < envs.nr; i++) {
  523. const char *var = envs.items[i].string;
  524. const char *val = envs.items[i].util;
  525. if (val || !getenv(var))
  526. continue;
  527. if (!printed_unset) {
  528. strbuf_addstr(dst, " unset");
  529. printed_unset = 1;
  530. }
  531. strbuf_addf(dst, " %s", var);
  532. }
  533. if (printed_unset)
  534. strbuf_addch(dst, ';');
  535. /* ... followed by "A=B C=D ..." */
  536. for (i = 0; i < envs.nr; i++) {
  537. const char *var = envs.items[i].string;
  538. const char *val = envs.items[i].util;
  539. const char *oldval;
  540. if (!val)
  541. continue;
  542. oldval = getenv(var);
  543. if (oldval && !strcmp(val, oldval))
  544. continue;
  545. strbuf_addf(dst, " %s=", var);
  546. sq_quote_buf_pretty(dst, val);
  547. }
  548. string_list_clear(&envs, 0);
  549. }
  550. static void trace_run_command(const struct child_process *cp)
  551. {
  552. struct strbuf buf = STRBUF_INIT;
  553. if (!trace_want(&trace_default_key))
  554. return;
  555. strbuf_addstr(&buf, "trace: run_command:");
  556. if (cp->dir) {
  557. strbuf_addstr(&buf, " cd ");
  558. sq_quote_buf_pretty(&buf, cp->dir);
  559. strbuf_addch(&buf, ';');
  560. }
  561. /*
  562. * The caller is responsible for initializing cp->env from
  563. * cp->env_array if needed. We only check one place.
  564. */
  565. if (cp->env)
  566. trace_add_env(&buf, cp->env);
  567. if (cp->git_cmd)
  568. strbuf_addstr(&buf, " git");
  569. sq_quote_argv_pretty(&buf, cp->argv);
  570. trace_printf("%s", buf.buf);
  571. strbuf_release(&buf);
  572. }
  573. int start_command(struct child_process *cmd)
  574. {
  575. int need_in, need_out, need_err;
  576. int fdin[2], fdout[2], fderr[2];
  577. int failed_errno;
  578. char *str;
  579. if (!cmd->argv)
  580. cmd->argv = cmd->args.v;
  581. if (!cmd->env)
  582. cmd->env = cmd->env_array.v;
  583. /*
  584. * In case of errors we must keep the promise to close FDs
  585. * that have been passed in via ->in and ->out.
  586. */
  587. need_in = !cmd->no_stdin && cmd->in < 0;
  588. if (need_in) {
  589. if (pipe(fdin) < 0) {
  590. failed_errno = errno;
  591. if (cmd->out > 0)
  592. close(cmd->out);
  593. str = "standard input";
  594. goto fail_pipe;
  595. }
  596. cmd->in = fdin[1];
  597. }
  598. need_out = !cmd->no_stdout
  599. && !cmd->stdout_to_stderr
  600. && cmd->out < 0;
  601. if (need_out) {
  602. if (pipe(fdout) < 0) {
  603. failed_errno = errno;
  604. if (need_in)
  605. close_pair(fdin);
  606. else if (cmd->in)
  607. close(cmd->in);
  608. str = "standard output";
  609. goto fail_pipe;
  610. }
  611. cmd->out = fdout[0];
  612. }
  613. need_err = !cmd->no_stderr && cmd->err < 0;
  614. if (need_err) {
  615. if (pipe(fderr) < 0) {
  616. failed_errno = errno;
  617. if (need_in)
  618. close_pair(fdin);
  619. else if (cmd->in)
  620. close(cmd->in);
  621. if (need_out)
  622. close_pair(fdout);
  623. else if (cmd->out)
  624. close(cmd->out);
  625. str = "standard error";
  626. fail_pipe:
  627. error("cannot create %s pipe for %s: %s",
  628. str, cmd->argv[0], strerror(failed_errno));
  629. child_process_clear(cmd);
  630. errno = failed_errno;
  631. return -1;
  632. }
  633. cmd->err = fderr[0];
  634. }
  635. trace2_child_start(cmd);
  636. trace_run_command(cmd);
  637. fflush(NULL);
  638. #ifndef GIT_WINDOWS_NATIVE
  639. {
  640. int notify_pipe[2];
  641. int null_fd = -1;
  642. char **childenv;
  643. struct strvec argv = STRVEC_INIT;
  644. struct child_err cerr;
  645. struct atfork_state as;
  646. if (prepare_cmd(&argv, cmd) < 0) {
  647. failed_errno = errno;
  648. cmd->pid = -1;
  649. if (!cmd->silent_exec_failure)
  650. error_errno("cannot run %s", cmd->argv[0]);
  651. goto end_of_spawn;
  652. }
  653. if (pipe(notify_pipe))
  654. notify_pipe[0] = notify_pipe[1] = -1;
  655. if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
  656. null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
  657. if (null_fd < 0)
  658. die_errno(_("open /dev/null failed"));
  659. set_cloexec(null_fd);
  660. }
  661. childenv = prep_childenv(cmd->env);
  662. atfork_prepare(&as);
  663. /*
  664. * NOTE: In order to prevent deadlocking when using threads special
  665. * care should be taken with the function calls made in between the
  666. * fork() and exec() calls. No calls should be made to functions which
  667. * require acquiring a lock (e.g. malloc) as the lock could have been
  668. * held by another thread at the time of forking, causing the lock to
  669. * never be released in the child process. This means only
  670. * Async-Signal-Safe functions are permitted in the child.
  671. */
  672. cmd->pid = fork();
  673. failed_errno = errno;
  674. if (!cmd->pid) {
  675. int sig;
  676. /*
  677. * Ensure the default die/error/warn routines do not get
  678. * called, they can take stdio locks and malloc.
  679. */
  680. set_die_routine(child_die_fn);
  681. set_error_routine(child_error_fn);
  682. set_warn_routine(child_warn_fn);
  683. close(notify_pipe[0]);
  684. set_cloexec(notify_pipe[1]);
  685. child_notifier = notify_pipe[1];
  686. if (cmd->no_stdin)
  687. child_dup2(null_fd, 0);
  688. else if (need_in) {
  689. child_dup2(fdin[0], 0);
  690. child_close_pair(fdin);
  691. } else if (cmd->in) {
  692. child_dup2(cmd->in, 0);
  693. child_close(cmd->in);
  694. }
  695. if (cmd->no_stderr)
  696. child_dup2(null_fd, 2);
  697. else if (need_err) {
  698. child_dup2(fderr[1], 2);
  699. child_close_pair(fderr);
  700. } else if (cmd->err > 1) {
  701. child_dup2(cmd->err, 2);
  702. child_close(cmd->err);
  703. }
  704. if (cmd->no_stdout)
  705. child_dup2(null_fd, 1);
  706. else if (cmd->stdout_to_stderr)
  707. child_dup2(2, 1);
  708. else if (need_out) {
  709. child_dup2(fdout[1], 1);
  710. child_close_pair(fdout);
  711. } else if (cmd->out > 1) {
  712. child_dup2(cmd->out, 1);
  713. child_close(cmd->out);
  714. }
  715. if (cmd->dir && chdir(cmd->dir))
  716. child_die(CHILD_ERR_CHDIR);
  717. /*
  718. * restore default signal handlers here, in case
  719. * we catch a signal right before execve below
  720. */
  721. for (sig = 1; sig < NSIG; sig++) {
  722. /* ignored signals get reset to SIG_DFL on execve */
  723. if (signal(sig, SIG_DFL) == SIG_IGN)
  724. signal(sig, SIG_IGN);
  725. }
  726. if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0)
  727. child_die(CHILD_ERR_SIGPROCMASK);
  728. /*
  729. * Attempt to exec using the command and arguments starting at
  730. * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
  731. * be used in the event exec failed with ENOEXEC at which point
  732. * we will try to interpret the command using 'sh'.
  733. */
  734. execve(argv.v[1], (char *const *) argv.v + 1,
  735. (char *const *) childenv);
  736. if (errno == ENOEXEC)
  737. execve(argv.v[0], (char *const *) argv.v,
  738. (char *const *) childenv);
  739. if (errno == ENOENT) {
  740. if (cmd->silent_exec_failure)
  741. child_die(CHILD_ERR_SILENT);
  742. child_die(CHILD_ERR_ENOENT);
  743. } else {
  744. child_die(CHILD_ERR_ERRNO);
  745. }
  746. }
  747. atfork_parent(&as);
  748. if (cmd->pid < 0)
  749. error_errno("cannot fork() for %s", cmd->argv[0]);
  750. else if (cmd->clean_on_exit)
  751. mark_child_for_cleanup(cmd->pid, cmd);
  752. /*
  753. * Wait for child's exec. If the exec succeeds (or if fork()
  754. * failed), EOF is seen immediately by the parent. Otherwise, the
  755. * child process sends a child_err struct.
  756. * Note that use of this infrastructure is completely advisory,
  757. * therefore, we keep error checks minimal.
  758. */
  759. close(notify_pipe[1]);
  760. if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) {
  761. /*
  762. * At this point we know that fork() succeeded, but exec()
  763. * failed. Errors have been reported to our stderr.
  764. */
  765. wait_or_whine(cmd->pid, cmd->argv[0], 0);
  766. child_err_spew(cmd, &cerr);
  767. failed_errno = errno;
  768. cmd->pid = -1;
  769. }
  770. close(notify_pipe[0]);
  771. if (null_fd >= 0)
  772. close(null_fd);
  773. strvec_clear(&argv);
  774. free(childenv);
  775. }
  776. end_of_spawn:
  777. #else
  778. {
  779. int fhin = 0, fhout = 1, fherr = 2;
  780. const char **sargv = cmd->argv;
  781. struct strvec nargv = STRVEC_INIT;
  782. if (cmd->no_stdin)
  783. fhin = open("/dev/null", O_RDWR);
  784. else if (need_in)
  785. fhin = dup(fdin[0]);
  786. else if (cmd->in)
  787. fhin = dup(cmd->in);
  788. if (cmd->no_stderr)
  789. fherr = open("/dev/null", O_RDWR);
  790. else if (need_err)
  791. fherr = dup(fderr[1]);
  792. else if (cmd->err > 2)
  793. fherr = dup(cmd->err);
  794. if (cmd->no_stdout)
  795. fhout = open("/dev/null", O_RDWR);
  796. else if (cmd->stdout_to_stderr)
  797. fhout = dup(fherr);
  798. else if (need_out)
  799. fhout = dup(fdout[1]);
  800. else if (cmd->out > 1)
  801. fhout = dup(cmd->out);
  802. if (cmd->git_cmd)
  803. cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
  804. else if (cmd->use_shell)
  805. cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
  806. cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
  807. cmd->dir, fhin, fhout, fherr);
  808. failed_errno = errno;
  809. if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
  810. error_errno("cannot spawn %s", cmd->argv[0]);
  811. if (cmd->clean_on_exit && cmd->pid >= 0)
  812. mark_child_for_cleanup(cmd->pid, cmd);
  813. strvec_clear(&nargv);
  814. cmd->argv = sargv;
  815. if (fhin != 0)
  816. close(fhin);
  817. if (fhout != 1)
  818. close(fhout);
  819. if (fherr != 2)
  820. close(fherr);
  821. }
  822. #endif
  823. if (cmd->pid < 0) {
  824. trace2_child_exit(cmd, -1);
  825. if (need_in)
  826. close_pair(fdin);
  827. else if (cmd->in)
  828. close(cmd->in);
  829. if (need_out)
  830. close_pair(fdout);
  831. else if (cmd->out)
  832. close(cmd->out);
  833. if (need_err)
  834. close_pair(fderr);
  835. else if (cmd->err)
  836. close(cmd->err);
  837. child_process_clear(cmd);
  838. errno = failed_errno;
  839. return -1;
  840. }
  841. if (need_in)
  842. close(fdin[0]);
  843. else if (cmd->in)
  844. close(cmd->in);
  845. if (need_out)
  846. close(fdout[1]);
  847. else if (cmd->out)
  848. close(cmd->out);
  849. if (need_err)
  850. close(fderr[1]);
  851. else if (cmd->err)
  852. close(cmd->err);
  853. return 0;
  854. }
  855. int finish_command(struct child_process *cmd)
  856. {
  857. int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
  858. trace2_child_exit(cmd, ret);
  859. child_process_clear(cmd);
  860. return ret;
  861. }
  862. int finish_command_in_signal(struct child_process *cmd)
  863. {
  864. int ret = wait_or_whine(cmd->pid, cmd->argv[0], 1);
  865. trace2_child_exit(cmd, ret);
  866. return ret;
  867. }
  868. int run_command(struct child_process *cmd)
  869. {
  870. int code;
  871. if (cmd->out < 0 || cmd->err < 0)
  872. BUG("run_command with a pipe can cause deadlock");
  873. code = start_command(cmd);
  874. if (code)
  875. return code;
  876. return finish_command(cmd);
  877. }
  878. int run_command_v_opt(const char **argv, int opt)
  879. {
  880. return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
  881. }
  882. int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class)
  883. {
  884. return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, tr2_class);
  885. }
  886. int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
  887. {
  888. return run_command_v_opt_cd_env_tr2(argv, opt, dir, env, NULL);
  889. }
  890. int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir,
  891. const char *const *env, const char *tr2_class)
  892. {
  893. struct child_process cmd = CHILD_PROCESS_INIT;
  894. cmd.argv = argv;
  895. cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
  896. cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
  897. cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
  898. cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
  899. cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
  900. cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
  901. cmd.wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0;
  902. cmd.dir = dir;
  903. cmd.env = env;
  904. cmd.trace2_child_class = tr2_class;
  905. return run_command(&cmd);
  906. }
  907. #ifndef NO_PTHREADS
  908. static pthread_t main_thread;
  909. static int main_thread_set;
  910. static pthread_key_t async_key;
  911. static pthread_key_t async_die_counter;
  912. static void *run_thread(void *data)
  913. {
  914. struct async *async = data;
  915. intptr_t ret;
  916. if (async->isolate_sigpipe) {
  917. sigset_t mask;
  918. sigemptyset(&mask);
  919. sigaddset(&mask, SIGPIPE);
  920. if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
  921. ret = error("unable to block SIGPIPE in async thread");
  922. return (void *)ret;
  923. }
  924. }
  925. pthread_setspecific(async_key, async);
  926. ret = async->proc(async->proc_in, async->proc_out, async->data);
  927. return (void *)ret;
  928. }
  929. static NORETURN void die_async(const char *err, va_list params)
  930. {
  931. vreportf("fatal: ", err, params);
  932. if (in_async()) {
  933. struct async *async = pthread_getspecific(async_key);
  934. if (async->proc_in >= 0)
  935. close(async->proc_in);
  936. if (async->proc_out >= 0)
  937. close(async->proc_out);
  938. pthread_exit((void *)128);
  939. }
  940. exit(128);
  941. }
  942. static int async_die_is_recursing(void)
  943. {
  944. void *ret = pthread_getspecific(async_die_counter);
  945. pthread_setspecific(async_die_counter, (void *)1);
  946. return ret != NULL;
  947. }
  948. int in_async(void)
  949. {
  950. if (!main_thread_set)
  951. return 0; /* no asyncs started yet */
  952. return !pthread_equal(main_thread, pthread_self());
  953. }
  954. static void NORETURN async_exit(int code)
  955. {
  956. pthread_exit((void *)(intptr_t)code);
  957. }
  958. #else
  959. static struct {
  960. void (**handlers)(void);
  961. size_t nr;
  962. size_t alloc;
  963. } git_atexit_hdlrs;
  964. static int git_atexit_installed;
  965. static void git_atexit_dispatch(void)
  966. {
  967. size_t i;
  968. for (i=git_atexit_hdlrs.nr ; i ; i--)
  969. git_atexit_hdlrs.handlers[i-1]();
  970. }
  971. static void git_atexit_clear(void)
  972. {
  973. free(git_atexit_hdlrs.handlers);
  974. memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
  975. git_atexit_installed = 0;
  976. }
  977. #undef atexit
  978. int git_atexit(void (*handler)(void))
  979. {
  980. ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
  981. git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
  982. if (!git_atexit_installed) {
  983. if (atexit(&git_atexit_dispatch))
  984. return -1;
  985. git_atexit_installed = 1;
  986. }
  987. return 0;
  988. }
  989. #define atexit git_atexit
  990. static int process_is_async;
  991. int in_async(void)
  992. {
  993. return process_is_async;
  994. }
  995. static void NORETURN async_exit(int code)
  996. {
  997. exit(code);
  998. }
  999. #endif
  1000. void check_pipe(int err)
  1001. {
  1002. if (err == EPIPE) {
  1003. if (in_async())
  1004. async_exit(141);
  1005. signal(SIGPIPE, SIG_DFL);
  1006. raise(SIGPIPE);
  1007. /* Should never happen, but just in case... */
  1008. exit(141);
  1009. }
  1010. }
  1011. int start_async(struct async *async)
  1012. {
  1013. int need_in, need_out;
  1014. int fdin[2], fdout[2];
  1015. int proc_in, proc_out;
  1016. need_in = async->in < 0;
  1017. if (need_in) {
  1018. if (pipe(fdin) < 0) {
  1019. if (async->out > 0)
  1020. close(async->out);
  1021. return error_errno("cannot create pipe");
  1022. }
  1023. async->in = fdin[1];
  1024. }
  1025. need_out = async->out < 0;
  1026. if (need_out) {
  1027. if (pipe(fdout) < 0) {
  1028. if (need_in)
  1029. close_pair(fdin);
  1030. else if (async->in)
  1031. close(async->in);
  1032. return error_errno("cannot create pipe");
  1033. }
  1034. async->out = fdout[0];
  1035. }
  1036. if (need_in)
  1037. proc_in = fdin[0];
  1038. else if (async->in)
  1039. proc_in = async->in;
  1040. else
  1041. proc_in = -1;
  1042. if (need_out)
  1043. proc_out = fdout[1];
  1044. else if (async->out)
  1045. proc_out = async->out;
  1046. else
  1047. proc_out = -1;
  1048. #ifdef NO_PTHREADS
  1049. /* Flush stdio before fork() to avoid cloning buffers */
  1050. fflush(NULL);
  1051. async->pid = fork();
  1052. if (async->pid < 0) {
  1053. error_errno("fork (async) failed");
  1054. goto error;
  1055. }
  1056. if (!async->pid) {
  1057. if (need_in)
  1058. close(fdin[1]);
  1059. if (need_out)
  1060. close(fdout[0]);
  1061. git_atexit_clear();
  1062. process_is_async = 1;
  1063. exit(!!async->proc(proc_in, proc_out, async->data));
  1064. }
  1065. mark_child_for_cleanup(async->pid, NULL);
  1066. if (need_in)
  1067. close(fdin[0]);
  1068. else if (async->in)
  1069. close(async->in);
  1070. if (need_out)
  1071. close(fdout[1]);
  1072. else if (async->out)
  1073. close(async->out);
  1074. #else
  1075. if (!main_thread_set) {
  1076. /*
  1077. * We assume that the first time that start_async is called
  1078. * it is from the main thread.
  1079. */
  1080. main_thread_set = 1;
  1081. main_thread = pthread_self();
  1082. pthread_key_create(&async_key, NULL);
  1083. pthread_key_create(&async_die_counter, NULL);
  1084. set_die_routine(die_async);
  1085. set_die_is_recursing_routine(async_die_is_recursing);
  1086. }
  1087. if (proc_in >= 0)
  1088. set_cloexec(proc_in);
  1089. if (proc_out >= 0)
  1090. set_cloexec(proc_out);
  1091. async->proc_in = proc_in;
  1092. async->proc_out = proc_out;
  1093. {
  1094. int err = pthread_create(&async->tid, NULL, run_thread, async);
  1095. if (err) {
  1096. error(_("cannot create async thread: %s"), strerror(err));
  1097. goto error;
  1098. }
  1099. }
  1100. #endif
  1101. return 0;
  1102. error:
  1103. if (need_in)
  1104. close_pair(fdin);
  1105. else if (async->in)
  1106. close(async->in);
  1107. if (need_out)
  1108. close_pair(fdout);
  1109. else if (async->out)
  1110. close(async->out);
  1111. return -1;
  1112. }
  1113. int finish_async(struct async *async)
  1114. {
  1115. #ifdef NO_PTHREADS
  1116. return wait_or_whine(async->pid, "child process", 0);
  1117. #else
  1118. void *ret = (void *)(intptr_t)(-1);
  1119. if (pthread_join(async->tid, &ret))
  1120. error("pthread_join failed");
  1121. return (int)(intptr_t)ret;
  1122. #endif
  1123. }
  1124. int async_with_fork(void)
  1125. {
  1126. #ifdef NO_PTHREADS
  1127. return 1;
  1128. #else
  1129. return 0;
  1130. #endif
  1131. }
  1132. const char *find_hook(const char *name)
  1133. {
  1134. static struct strbuf path = STRBUF_INIT;
  1135. strbuf_reset(&path);
  1136. strbuf_git_path(&path, "hooks/%s", name);
  1137. if (access(path.buf, X_OK) < 0) {
  1138. int err = errno;
  1139. #ifdef STRIP_EXTENSION
  1140. strbuf_addstr(&path, STRIP_EXTENSION);
  1141. if (access(path.buf, X_OK) >= 0)
  1142. return path.buf;
  1143. if (errno == EACCES)
  1144. err = errno;
  1145. #endif
  1146. if (err == EACCES && advice_ignored_hook) {
  1147. static struct string_list advise_given = STRING_LIST_INIT_DUP;
  1148. if (!string_list_lookup(&advise_given, name)) {
  1149. string_list_insert(&advise_given, name);
  1150. advise(_("The '%s' hook was ignored because "
  1151. "it's not set as executable.\n"
  1152. "You can disable this warning with "
  1153. "`git config advice.ignoredHook false`."),
  1154. path.buf);
  1155. }
  1156. }
  1157. return NULL;
  1158. }
  1159. return path.buf;
  1160. }
  1161. int run_hook_ve(const char *const *env, const char *name, va_list args)
  1162. {
  1163. struct child_process hook = CHILD_PROCESS_INIT;
  1164. const char *p;
  1165. p = find_hook(name);
  1166. if (!p)
  1167. return 0;
  1168. strvec_push(&hook.args, p);
  1169. while ((p = va_arg(args, const char *)))
  1170. strvec_push(&hook.args, p);
  1171. hook.env = env;
  1172. hook.no_stdin = 1;
  1173. hook.stdout_to_stderr = 1;
  1174. hook.trace2_hook_name = name;
  1175. return run_command(&hook);
  1176. }
  1177. int run_hook_le(const char *const *env, const char *name, ...)
  1178. {
  1179. va_list args;
  1180. int ret;
  1181. va_start(args, name);
  1182. ret = run_hook_ve(env, name, args);
  1183. va_end(args);
  1184. return ret;
  1185. }
  1186. struct io_pump {
  1187. /* initialized by caller */
  1188. int fd;
  1189. int type; /* POLLOUT or POLLIN */
  1190. union {
  1191. struct {
  1192. const char *buf;
  1193. size_t len;
  1194. } out;
  1195. struct {
  1196. struct strbuf *buf;
  1197. size_t hint;
  1198. } in;
  1199. } u;
  1200. /* returned by pump_io */
  1201. int error; /* 0 for success, otherwise errno */
  1202. /* internal use */
  1203. struct pollfd *pfd;
  1204. };
  1205. static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
  1206. {
  1207. int pollsize = 0;
  1208. int i;
  1209. for (i = 0; i < nr; i++) {
  1210. struct io_pump *io = &slots[i];
  1211. if (io->fd < 0)
  1212. continue;
  1213. pfd[pollsize].fd = io->fd;
  1214. pfd[pollsize].events = io->type;
  1215. io->pfd = &pfd[pollsize++];
  1216. }
  1217. if (!pollsize)
  1218. return 0;
  1219. if (poll(pfd, pollsize, -1) < 0) {
  1220. if (errno == EINTR)
  1221. return 1;
  1222. die_errno("poll failed");
  1223. }
  1224. for (i = 0; i < nr; i++) {
  1225. struct io_pump *io = &slots[i];
  1226. if (io->fd < 0)
  1227. continue;
  1228. if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
  1229. continue;
  1230. if (io->type == POLLOUT) {
  1231. ssize_t len = xwrite(io->fd,
  1232. io->u.out.buf, io->u.out.len);
  1233. if (len < 0) {
  1234. io->error = errno;
  1235. close(io->fd);
  1236. io->fd = -1;
  1237. } else {
  1238. io->u.out.buf += len;
  1239. io->u.out.len -= len;
  1240. if (!io->u.out.len) {
  1241. close(io->fd);
  1242. io->fd = -1;
  1243. }
  1244. }
  1245. }
  1246. if (io->type == POLLIN) {
  1247. ssize_t len = strbuf_read_once(io->u.in.buf,
  1248. io->fd, io->u.in.hint);
  1249. if (len < 0)
  1250. io->error = errno;
  1251. if (len <= 0) {
  1252. close(io->fd);
  1253. io->fd = -1;
  1254. }
  1255. }
  1256. }
  1257. return 1;
  1258. }
  1259. static int pump_io(struct io_pump *slots, int nr)
  1260. {
  1261. struct pollfd *pfd;
  1262. int i;
  1263. for (i = 0; i < nr; i++)
  1264. slots[i].error = 0;
  1265. ALLOC_ARRAY(pfd, nr);
  1266. while (pump_io_round(slots, nr, pfd))
  1267. ; /* nothing */
  1268. free(pfd);
  1269. /* There may be multiple errno values, so just pick the first. */
  1270. for (i = 0; i < nr; i++) {
  1271. if (slots[i].error) {
  1272. errno = slots[i].error;
  1273. return -1;
  1274. }
  1275. }
  1276. return 0;
  1277. }
  1278. int pipe_command(struct child_process *cmd,
  1279. const char *in, size_t in_len,
  1280. struct strbuf *out, size_t out_hint,
  1281. struct strbuf *err, size_t err_hint)
  1282. {
  1283. struct io_pump io[3];
  1284. int nr = 0;
  1285. if (in)
  1286. cmd->in = -1;
  1287. if (out)
  1288. cmd->out = -1;
  1289. if (err)
  1290. cmd->err = -1;
  1291. if (start_command(cmd) < 0)
  1292. return -1;
  1293. if (in) {
  1294. io[nr].fd = cmd->in;
  1295. io[nr].type = POLLOUT;
  1296. io[nr].u.out.buf = in;
  1297. io[nr].u.out.len = in_len;
  1298. nr++;
  1299. }
  1300. if (out) {
  1301. io[nr].fd = cmd->out;
  1302. io[nr].type = POLLIN;
  1303. io[nr].u.in.buf = out;
  1304. io[nr].u.in.hint = out_hint;
  1305. nr++;
  1306. }
  1307. if (err) {
  1308. io[nr].fd = cmd->err;
  1309. io[nr].type = POLLIN;
  1310. io[nr].u.in.buf = err;
  1311. io[nr].u.in.hint = err_hint;
  1312. nr++;
  1313. }
  1314. if (pump_io(io, nr) < 0) {
  1315. finish_command(cmd); /* throw away exit code */
  1316. return -1;
  1317. }
  1318. return finish_command(cmd);
  1319. }
  1320. enum child_state {
  1321. GIT_CP_FREE,
  1322. GIT_CP_WORKING,
  1323. GIT_CP_WAIT_CLEANUP,
  1324. };
  1325. struct parallel_processes {
  1326. void *data;
  1327. int max_processes;
  1328. int nr_processes;
  1329. get_next_task_fn get_next_task;
  1330. start_failure_fn start_failure;
  1331. task_finished_fn task_finished;
  1332. struct {
  1333. enum child_state state;
  1334. struct child_process process;
  1335. struct strbuf err;
  1336. void *data;
  1337. } *children;
  1338. /*
  1339. * The struct pollfd is logically part of *children,
  1340. * but the system call expects it as its own array.
  1341. */
  1342. struct pollfd *pfd;
  1343. unsigned shutdown : 1;
  1344. int output_owner;
  1345. struct strbuf buffered_output; /* of finished children */
  1346. };
  1347. static int default_start_failure(struct strbuf *out,
  1348. void *pp_cb,
  1349. void *pp_task_cb)
  1350. {
  1351. return 0;
  1352. }
  1353. static int default_task_finished(int result,
  1354. struct strbuf *out,
  1355. void *pp_cb,
  1356. void *pp_task_cb)
  1357. {
  1358. return 0;
  1359. }
  1360. static void kill_children(struct parallel_processes *pp, int signo)
  1361. {
  1362. int i, n = pp->max_processes;
  1363. for (i = 0; i < n; i++)
  1364. if (pp->children[i].state == GIT_CP_WORKING)
  1365. kill(pp->children[i].process.pid, signo);
  1366. }
  1367. static struct parallel_processes *pp_for_signal;
  1368. static void handle_children_on_signal(int signo)
  1369. {
  1370. kill_children(pp_for_signal, signo);
  1371. sigchain_pop(signo);
  1372. raise(signo);
  1373. }
  1374. static void pp_init(struct parallel_processes *pp,
  1375. int n,
  1376. get_next_task_fn get_next_task,
  1377. start_failure_fn start_failure,
  1378. task_finished_fn task_finished,
  1379. void *data)
  1380. {
  1381. int i;
  1382. if (n < 1)
  1383. n = online_cpus();
  1384. pp->max_processes = n;
  1385. trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
  1386. pp->data = data;
  1387. if (!get_next_task)
  1388. BUG("you need to specify a get_next_task function");
  1389. pp->get_next_task = get_next_task;
  1390. pp->start_failure = start_failure ? start_failure : default_start_failure;
  1391. pp->task_finished = task_finished ? task_finished : default_task_finished;
  1392. pp->nr_processes = 0;
  1393. pp->output_owner = 0;
  1394. pp->shutdown = 0;
  1395. pp->children = xcalloc(n, sizeof(*pp->children));
  1396. pp->pfd = xcalloc(n, sizeof(*pp->pfd));
  1397. strbuf_init(&pp->buffered_output, 0);
  1398. for (i = 0; i < n; i++) {
  1399. strbuf_init(&pp->children[i].err, 0);
  1400. child_process_init(&pp->children[i].process);
  1401. pp->pfd[i].events = POLLIN | POLLHUP;
  1402. pp->pfd[i].fd = -1;
  1403. }
  1404. pp_for_signal = pp;
  1405. sigchain_push_common(handle_children_on_signal);
  1406. }
  1407. static void pp_cleanup(struct parallel_processes *pp)
  1408. {
  1409. int i;
  1410. trace_printf("run_processes_parallel: done");
  1411. for (i = 0; i < pp->max_processes; i++) {
  1412. strbuf_release(&pp->children[i].err);
  1413. child_process_clear(&pp->children[i].process);
  1414. }
  1415. free(pp->children);
  1416. free(pp->pfd);
  1417. /*
  1418. * When get_next_task added messages to the buffer in its last
  1419. * iteration, the buffered output is non empty.
  1420. */
  1421. strbuf_write(&pp->buffered_output, stderr);
  1422. strbuf_release(&pp->buffered_output);
  1423. sigchain_pop_common();
  1424. }
  1425. /* returns
  1426. * 0 if a new task was started.
  1427. * 1 if no new jobs was started (get_next_task ran out of work, non critical
  1428. * problem with starting a new command)
  1429. * <0 no new job was started, user wishes to shutdown early. Use negative code
  1430. * to signal the children.
  1431. */
  1432. static int pp_start_one(struct parallel_processes *pp)
  1433. {
  1434. int i, code;
  1435. for (i = 0; i < pp->max_processes; i++)
  1436. if (pp->children[i].state == GIT_CP_FREE)
  1437. break;
  1438. if (i == pp->max_processes)
  1439. BUG("bookkeeping is hard");
  1440. code = pp->get_next_task(&pp->children[i].process,
  1441. &pp->children[i].err,
  1442. pp->data,
  1443. &pp->children[i].data);
  1444. if (!code) {
  1445. strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
  1446. strbuf_reset(&pp->children[i].err);
  1447. return 1;
  1448. }
  1449. pp->children[i].process.err = -1;
  1450. pp->children[i].process.stdout_to_stderr = 1;
  1451. pp->children[i].process.no_stdin = 1;
  1452. if (start_command(&pp->children[i].process)) {
  1453. code = pp->start_failure(&pp->children[i].err,
  1454. pp->data,
  1455. pp->children[i].data);
  1456. strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
  1457. strbuf_reset(&pp->children[i].err);
  1458. if (code)
  1459. pp->shutdown = 1;
  1460. return code;
  1461. }
  1462. pp->nr_processes++;
  1463. pp->children[i].state = GIT_CP_WORKING;
  1464. pp->pfd[i].fd = pp->children[i].process.err;
  1465. return 0;
  1466. }
  1467. static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
  1468. {
  1469. int i;
  1470. while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
  1471. if (errno == EINTR)
  1472. continue;
  1473. pp_cleanup(pp);
  1474. die_errno("poll");
  1475. }
  1476. /* Buffer output from all pipes. */
  1477. for (i = 0; i < pp->max_processes; i++) {
  1478. if (pp->children[i].state == GIT_CP_WORKING &&
  1479. pp->pfd[i].revents & (POLLIN | POLLHUP)) {
  1480. int n = strbuf_read_once(&pp->children[i].err,
  1481. pp->children[i].process.err, 0);
  1482. if (n == 0) {
  1483. close(pp->children[i].process.err);
  1484. pp->children[i].state = GIT_CP_WAIT_CLEANUP;
  1485. } else if (n < 0)
  1486. if (errno != EAGAIN)
  1487. die_errno("read");
  1488. }
  1489. }
  1490. }
  1491. static void pp_output(struct parallel_processes *pp)
  1492. {
  1493. int i = pp->output_owner;
  1494. if (pp->children[i].state == GIT_CP_WORKING &&
  1495. pp->children[i].err.len) {
  1496. strbuf_write(&pp->children[i].err, stderr);
  1497. strbuf_reset(&pp->children[i].err);
  1498. }
  1499. }
  1500. static int pp_collect_finished(struct parallel_processes *pp)
  1501. {
  1502. int i, code;
  1503. int n = pp->max_processes;
  1504. int result = 0;
  1505. while (pp->nr_processes > 0) {
  1506. for (i = 0; i < pp->max_processes; i++)
  1507. if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
  1508. break;
  1509. if (i == pp->max_processes)
  1510. break;
  1511. code = finish_command(&pp->children[i].process);
  1512. code = pp->task_finished(code,
  1513. &pp->children[i].err, pp->data,
  1514. pp->children[i].data);
  1515. if (code)
  1516. result = code;
  1517. if (code < 0)
  1518. break;
  1519. pp->nr_processes--;
  1520. pp->children[i].state = GIT_CP_FREE;
  1521. pp->pfd[i].fd = -1;
  1522. child_process_init(&pp->children[i].process);
  1523. if (i != pp->output_owner) {
  1524. strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
  1525. strbuf_reset(&pp->children[i].err);
  1526. } else {
  1527. strbuf_write(&pp->children[i].err, stderr);
  1528. strbuf_reset(&pp->children[i].err);
  1529. /* Output all other finished child processes */
  1530. strbuf_write(&pp->buffered_output, stderr);
  1531. strbuf_reset(&pp->buffered_output);
  1532. /*
  1533. * Pick next process to output live.
  1534. * NEEDSWORK:
  1535. * For now we pick it randomly by doing a round
  1536. * robin. Later we may want to pick the one with
  1537. * the most output or the longest or shortest
  1538. * running process time.
  1539. */
  1540. for (i = 0; i < n; i++)
  1541. if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
  1542. break;
  1543. pp->output_owner = (pp->output_owner + i) % n;
  1544. }
  1545. }
  1546. return result;
  1547. }
  1548. int run_processes_parallel(int n,
  1549. get_next_task_fn get_next_task,
  1550. start_failure_fn start_failure,
  1551. task_finished_fn task_finished,
  1552. void *pp_cb)
  1553. {
  1554. int i, code;
  1555. int output_timeout = 100;
  1556. int spawn_cap = 4;
  1557. struct parallel_processes pp;
  1558. pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
  1559. while (1) {
  1560. for (i = 0;
  1561. i < spawn_cap && !pp.shutdown &&
  1562. pp.nr_processes < pp.max_processes;
  1563. i++) {
  1564. code = pp_start_one(&pp);
  1565. if (!code)
  1566. continue;
  1567. if (code < 0) {
  1568. pp.shutdown = 1;
  1569. kill_children(&pp, -code);
  1570. }
  1571. break;
  1572. }
  1573. if (!pp.nr_processes)
  1574. break;
  1575. pp_buffer_stderr(&pp, output_timeout);
  1576. pp_output(&pp);
  1577. code = pp_collect_finished(&pp);
  1578. if (code) {
  1579. pp.shutdown = 1;
  1580. if (code < 0)
  1581. kill_children(&pp, -code);
  1582. }
  1583. }
  1584. pp_cleanup(&pp);
  1585. return 0;
  1586. }
  1587. int run_processes_parallel_tr2(int n, get_next_task_fn get_next_task,
  1588. start_failure_fn start_failure,
  1589. task_finished_fn task_finished, void *pp_cb,
  1590. const char *tr2_category, const char *tr2_label)
  1591. {
  1592. int result;
  1593. trace2_region_enter_printf(tr2_category, tr2_label, NULL, "max:%d",
  1594. ((n < 1) ? online_cpus() : n));
  1595. result = run_processes_parallel(n, get_next_task, start_failure,
  1596. task_finished, pp_cb);
  1597. trace2_region_leave(tr2_category, tr2_label, NULL);
  1598. return result;
  1599. }
  1600. int run_auto_maintenance(int quiet)
  1601. {
  1602. struct child_process maint = CHILD_PROCESS_INIT;
  1603. maint.git_cmd = 1;
  1604. strvec_pushl(&maint.args, "maintenance", "run", "--auto", NULL);
  1605. strvec_push(&maint.args, quiet ? "--quiet" : "--no-quiet");
  1606. return run_command(&maint);
  1607. }