summaryrefslogtreecommitdiff
path: root/tempfile.c
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2016-09-08 21:35:56 -0700
committerLibravatar Junio C Hamano <gitster@pobox.com>2016-09-08 21:35:56 -0700
commitc0e8b3b444a701630c0793c195fd0e8162a95c2c (patch)
tree7f6e853f4bfe370ae3835c0bc210ed1b0c63bed2 /tempfile.c
parentMerge branch 'dg/document-git-c-in-git-config-doc' into maint (diff)
parentmingw: ensure temporary file handles are not inherited by child processes (diff)
downloadtgif-c0e8b3b444a701630c0793c195fd0e8162a95c2c.tar.xz
Merge branch 'bw/mingw-avoid-inheriting-fd-to-lockfile' into maint
The tempfile (hence its user lockfile) API lets the caller to open a file descriptor to a temporary file, write into it and then finalize it by first closing the filehandle and then either removing or renaming the temporary file. When the process spawns a subprocess after obtaining the file descriptor, and if the subprocess has not exited when the attempt to remove or rename is made, the last step fails on Windows, because the subprocess has the file descriptor still open. Open tempfile with O_CLOEXEC flag to avoid this (on Windows, this is mapped to O_NOINHERIT). * bw/mingw-avoid-inheriting-fd-to-lockfile: mingw: ensure temporary file handles are not inherited by child processes t6026-merge-attr: child processes must not inherit index.lock handles
Diffstat (limited to 'tempfile.c')
-rw-r--r--tempfile.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/tempfile.c b/tempfile.c
index 0af7ebf016..2990c92424 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -120,7 +120,12 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
prepare_tempfile_object(tempfile);
strbuf_add_absolute_path(&tempfile->filename, path);
- tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
+ tempfile->fd = open(tempfile->filename.buf,
+ O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
+ if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
+ /* Try again w/o O_CLOEXEC: the kernel might not support it */
+ tempfile->fd = open(tempfile->filename.buf,
+ O_RDWR | O_CREAT | O_EXCL, 0666);
if (tempfile->fd < 0) {
strbuf_reset(&tempfile->filename);
return -1;