Skip to content

Commit 05d1ed6

Browse files
bwijengitster
authored andcommitted
mingw: ensure temporary file handles are not inherited by child processes
When the index is locked and child processes inherit the handle to said lock and the parent process wants to remove the lock before the child process exits, on Windows there is a problem: it won't work because files cannot be deleted if a process holds a handle on them. The symptom: Rename from 'xxx/.git/index.lock' to 'xxx/.git/index' failed. Should I try again? (y/n) Spawning child processes with bInheritHandles==FALSE would not work because no file handles would be inherited, not even the hStdXxx handles in STARTUPINFO (stdin/stdout/stderr). Opening every file with O_NOINHERIT does not work, either, as e.g. git-upload-pack expects inherited file handles. This leaves us with the only way out: creating temp files with the O_NOINHERIT flag. This flag is Windows-specific, however. For our purposes, it is equivalent to O_CLOEXEC (which does not exist on Windows), so let's just open temporary files with the O_CLOEXEC flag and map that flag to O_NOINHERIT on Windows. As Eric Wong pointed out, we need to be careful to handle the case where the Linux headers used to compile Git support O_CLOEXEC but the Linux kernel used to run Git does not: it returns an EINVAL. This fixes the test that we just introduced to demonstrate the problem. Signed-off-by: Ben Wijen <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ad65f7e commit 05d1ed6

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

compat/mingw.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ typedef int pid_t;
6767
#define F_SETFD 2
6868
#define FD_CLOEXEC 0x1
6969

70+
#if !defined O_CLOEXEC && defined O_NOINHERIT
71+
#define O_CLOEXEC O_NOINHERIT
72+
#endif
73+
7074
#ifndef EAFNOSUPPORT
7175
#define EAFNOSUPPORT WSAEAFNOSUPPORT
7276
#endif

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,10 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
650650
#define getpagesize() sysconf(_SC_PAGESIZE)
651651
#endif
652652

653+
#ifndef O_CLOEXEC
654+
#define O_CLOEXEC 0
655+
#endif
656+
653657
#ifdef FREAD_READS_DIRECTORIES
654658
#ifdef fopen
655659
#undef fopen

lockfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
* * calling `fdopen_lock_file()` to get a `FILE` pointer for the
5656
* open file and writing to the file using stdio.
5757
*
58+
* Note that the file descriptor returned by hold_lock_file_for_update()
59+
* is marked O_CLOEXEC, so the new contents must be written by the
60+
* current process, not a spawned one.
61+
*
5862
* When finished writing, the caller can:
5963
*
6064
* * Close the file descriptor and rename the lockfile to its final

t/t6026-merge-attr.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test_expect_success 'up-to-date merge without common ancestor' '
181181
)
182182
'
183183

184-
test_expect_success !MINGW 'custom merge does not lock index' '
184+
test_expect_success 'custom merge does not lock index' '
185185
git reset --hard anchor &&
186186
write_script sleep-one-second.sh <<-\EOF &&
187187
sleep 1 &

tempfile.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
120120
prepare_tempfile_object(tempfile);
121121

122122
strbuf_add_absolute_path(&tempfile->filename, path);
123-
tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
123+
tempfile->fd = open(tempfile->filename.buf,
124+
O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
125+
if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
126+
/* Try again w/o O_CLOEXEC: the kernel might not support it */
127+
tempfile->fd = open(tempfile->filename.buf,
128+
O_RDWR | O_CREAT | O_EXCL, 0666);
124129
if (tempfile->fd < 0) {
125130
strbuf_reset(&tempfile->filename);
126131
return -1;

tempfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
* * calling `fdopen_tempfile()` to get a `FILE` pointer for the
3434
* open file and writing to the file using stdio.
3535
*
36+
* Note that the file descriptor returned by create_tempfile()
37+
* is marked O_CLOEXEC, so the new contents must be written by
38+
* the current process, not any spawned one.
39+
*
3640
* When finished writing, the caller can:
3741
*
3842
* * Close the file descriptor and remove the temporary file by

0 commit comments

Comments
 (0)