Skip to content

Commit c0e8b3b

Browse files
committed
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
2 parents 15a2729 + 05d1ed6 commit c0e8b3b

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,17 @@ test_expect_success 'up-to-date merge without common ancestor' '
181181
)
182182
'
183183

184+
test_expect_success 'custom merge does not lock index' '
185+
git reset --hard anchor &&
186+
write_script sleep-one-second.sh <<-\EOF &&
187+
sleep 1 &
188+
EOF
189+
190+
test_write_lines >.gitattributes \
191+
"* merge=ours" "text merge=sleep-one-second" &&
192+
test_config merge.ours.driver true &&
193+
test_config merge.sleep-one-second.driver ./sleep-one-second.sh &&
194+
git merge master
195+
'
196+
184197
test_done

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)