Windows: avoid the "dup dance" when spawning a child process

When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to

1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source

We used this idiom as well -- but we are not using the spawn() functions
anymore!

Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.

With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.

The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.

Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
3 files changed
tree: 996c0fdd16348c4496b3a35fee1d249036d58a91
  1. block-sha1/
  2. compat/
  3. contrib/
  4. Documentation/
  5. git-gui/
  6. git_remote_helpers/
  7. gitk-git/
  8. gitweb/
  9. perl/
  10. ppc/
  11. t/
  12. templates/
  13. xdiff/
  14. .gitattributes
  15. .gitignore
  16. .mailmap
  17. abspath.c
  18. advice.c
  19. advice.h
  20. alias.c
  21. alloc.c
  22. archive-tar.c
  23. archive-zip.c
  24. archive.c
  25. archive.h
  26. attr.c
  27. attr.h
  28. base85.c
  29. bisect.c
  30. bisect.h
  31. blob.c
  32. blob.h
  33. branch.c
  34. branch.h
  35. builtin-add.c
  36. builtin-annotate.c
  37. builtin-apply.c
  38. builtin-archive.c
  39. builtin-bisect--helper.c
  40. builtin-blame.c
  41. builtin-branch.c
  42. builtin-bundle.c
  43. builtin-cat-file.c
  44. builtin-check-attr.c
  45. builtin-check-ref-format.c
  46. builtin-checkout-index.c
  47. builtin-checkout.c
  48. builtin-clean.c
  49. builtin-clone.c
  50. builtin-commit-tree.c
  51. builtin-commit.c
  52. builtin-config.c
  53. builtin-count-objects.c
  54. builtin-describe.c
  55. builtin-diff-files.c
  56. builtin-diff-index.c
  57. builtin-diff-tree.c
  58. builtin-diff.c
  59. builtin-fast-export.c
  60. builtin-fetch-pack.c
  61. builtin-fetch.c
  62. builtin-fmt-merge-msg.c
  63. builtin-for-each-ref.c
  64. builtin-fsck.c
  65. builtin-gc.c
  66. builtin-grep.c
  67. builtin-help.c
  68. builtin-init-db.c
  69. builtin-log.c
  70. builtin-ls-files.c
  71. builtin-ls-remote.c
  72. builtin-ls-tree.c
  73. builtin-mailinfo.c
  74. builtin-mailsplit.c
  75. builtin-merge-base.c
  76. builtin-merge-file.c
  77. builtin-merge-ours.c
  78. builtin-merge-recursive.c
  79. builtin-merge.c
  80. builtin-mktree.c
  81. builtin-mv.c
  82. builtin-name-rev.c
  83. builtin-pack-objects.c
  84. builtin-pack-refs.c
  85. builtin-prune-packed.c
  86. builtin-prune.c
  87. builtin-push.c
  88. builtin-read-tree.c
  89. builtin-receive-pack.c
  90. builtin-reflog.c
  91. builtin-remote.c
  92. builtin-replace.c
  93. builtin-rerere.c
  94. builtin-reset.c
  95. builtin-rev-list.c
  96. builtin-rev-parse.c
  97. builtin-revert.c
  98. builtin-rm.c
  99. builtin-send-pack.c
  100. builtin-shortlog.c
  101. builtin-show-branch.c
  102. builtin-show-ref.c
  103. builtin-stripspace.c
  104. builtin-symbolic-ref.c
  105. builtin-tag.c
  106. builtin-tar-tree.c
  107. builtin-unpack-objects.c
  108. builtin-update-index.c
  109. builtin-update-ref.c
  110. builtin-update-server-info.c
  111. builtin-upload-archive.c
  112. builtin-verify-pack.c
  113. builtin-verify-tag.c
  114. builtin-write-tree.c
  115. builtin.h
  116. bundle.c
  117. bundle.h
  118. cache-tree.c
  119. cache-tree.h
  120. cache.h
  121. check-builtins.sh
  122. check-racy.c
  123. check_bindir
  124. color.c
  125. color.h
  126. combine-diff.c
  127. command-list.txt
  128. commit.c
  129. commit.h
  130. config.c
  131. config.mak.in
  132. configure.ac
  133. connect.c
  134. convert.c
  135. copy.c
  136. COPYING
  137. csum-file.c
  138. csum-file.h
  139. ctype.c
  140. daemon.c
  141. date.c
  142. decorate.c
  143. decorate.h
  144. delta.h
  145. diff-delta.c
  146. diff-lib.c
  147. diff-no-index.c
  148. diff.c
  149. diff.h
  150. diffcore-break.c
  151. diffcore-delta.c
  152. diffcore-order.c
  153. diffcore-pickaxe.c
  154. diffcore-rename.c
  155. diffcore.h
  156. dir.c
  157. dir.h
  158. editor.c
  159. entry.c
  160. environment.c
  161. exec_cmd.c
  162. exec_cmd.h
  163. fast-import.c
  164. fetch-pack.h
  165. fixup-builtins
  166. fsck.c
  167. fsck.h
  168. generate-cmdlist.sh
  169. git-add--interactive.perl
  170. git-am.sh
  171. git-archimport.perl
  172. git-bisect.sh
  173. git-compat-util.h
  174. git-cvsexportcommit.perl
  175. git-cvsimport.perl
  176. git-cvsserver.perl
  177. git-difftool--helper.sh
  178. git-difftool.perl
  179. git-filter-branch.sh
  180. git-instaweb.sh
  181. git-lost-found.sh
  182. git-merge-octopus.sh
  183. git-merge-one-file.sh
  184. git-merge-resolve.sh
  185. git-mergetool--lib.sh
  186. git-mergetool.sh
  187. git-notes.sh
  188. git-parse-remote.sh
  189. git-pull.sh
  190. git-quiltimport.sh
  191. git-rebase--interactive.sh
  192. git-rebase.sh
  193. git-relink.perl
  194. git-repack.sh
  195. git-request-pull.sh
  196. git-send-email.perl
  197. git-sh-setup.sh
  198. git-stash.sh
  199. git-submodule.sh
  200. git-svn.perl
  201. GIT-VERSION-GEN
  202. git-web--browse.sh
  203. git.c
  204. git.spec.in
  205. graph.c
  206. graph.h
  207. grep.c
  208. grep.h
  209. hash-object.c
  210. hash.c
  211. hash.h
  212. help.c
  213. help.h
  214. http-backend.c
  215. http-fetch.c
  216. http-push.c
  217. http-walker.c
  218. http.c
  219. http.h
  220. ident.c
  221. imap-send.c
  222. index-pack.c
  223. INSTALL
  224. levenshtein.c
  225. levenshtein.h
  226. list-objects.c
  227. list-objects.h
  228. ll-merge.c
  229. ll-merge.h
  230. lockfile.c
  231. log-tree.c
  232. log-tree.h
  233. mailmap.c
  234. mailmap.h
  235. Makefile
  236. match-trees.c
  237. merge-file.c
  238. merge-index.c
  239. merge-recursive.c
  240. merge-recursive.h
  241. merge-tree.c
  242. mktag.c
  243. name-hash.c
  244. notes.c
  245. notes.h
  246. object.c
  247. object.h
  248. pack-check.c
  249. pack-redundant.c
  250. pack-refs.c
  251. pack-refs.h
  252. pack-revindex.c
  253. pack-revindex.h
  254. pack-write.c
  255. pack.h
  256. pager.c
  257. parse-options.c
  258. parse-options.h
  259. patch-delta.c
  260. patch-id.c
  261. patch-ids.c
  262. patch-ids.h
  263. path.c
  264. pkt-line.c
  265. pkt-line.h
  266. preload-index.c
  267. pretty.c
  268. progress.c
  269. progress.h
  270. quote.c
  271. quote.h
  272. reachable.c
  273. reachable.h
  274. read-cache.c
  275. README
  276. reflog-walk.c
  277. reflog-walk.h
  278. refs.c
  279. refs.h
  280. remote-curl.c
  281. remote.c
  282. remote.h
  283. replace_object.c
  284. rerere.c
  285. rerere.h
  286. revision.c
  287. revision.h
  288. run-command.c
  289. run-command.h
  290. send-pack.h
  291. server-info.c
  292. setup.c
  293. sha1-lookup.c
  294. sha1-lookup.h
  295. sha1_file.c
  296. sha1_name.c
  297. shallow.c
  298. shell.c
  299. shortlog.h
  300. show-index.c
  301. sideband.c
  302. sideband.h
  303. sigchain.c
  304. sigchain.h
  305. strbuf.c
  306. strbuf.h
  307. string-list.c
  308. string-list.h
  309. submodule.c
  310. submodule.h
  311. symlinks.c
  312. tag.c
  313. tag.h
  314. tar.h
  315. test-chmtime.c
  316. test-ctype.c
  317. test-date.c
  318. test-delta.c
  319. test-dump-cache-tree.c
  320. test-genrandom.c
  321. test-index-version.c
  322. test-match-trees.c
  323. test-parse-options.c
  324. test-path-utils.c
  325. test-sha1.c
  326. test-sha1.sh
  327. test-sigchain.c
  328. thread-utils.c
  329. thread-utils.h
  330. trace.c
  331. transport-helper.c
  332. transport.c
  333. transport.h
  334. tree-diff.c
  335. tree-walk.c
  336. tree-walk.h
  337. tree.c
  338. tree.h
  339. unimplemented.sh
  340. unpack-file.c
  341. unpack-trees.c
  342. unpack-trees.h
  343. upload-pack.c
  344. usage.c
  345. userdiff.c
  346. userdiff.h
  347. utf8.c
  348. utf8.h
  349. var.c
  350. walker.c
  351. walker.h
  352. wrap-for-bin.sh
  353. wrapper.c
  354. write_or_die.c
  355. ws.c
  356. wt-status.c
  357. wt-status.h
  358. xdiff-interface.c
  359. xdiff-interface.h