Skip to content

Commit 5390457

Browse files
pks-tgitster
authored andcommitted
compat/mingw: handle O_CLOEXEC in mingw_open_existing()
Our MinGW emulation of the open(3p) syscall uses one of three different code paths depending on the flags passed by the caller. Ideally, we would just use `_wopen()` for all of these directly and instead rely on the Windows SDK to implement the logic for us. But unfortunately, this interface does not allow us to set the `FILE_SHARING_*` flags, which we need to have control over to implement POSIX semantics. One of the code paths is for opening existing files, where we end up calling `mingw_open_existing()`. While this code path is executed when the user passes `O_NOINHERIT`, we don't know to handle `O_CLOEXEC` yet, which causes a couple of code paths that use the flag to not use the emulation. The consequence is that those code paths do not support POSIX semantics because we don't know to set the sharing mode correctly. Supporting `O_CLOEXEC` is quite trivial: we don't have to do anything, as Windows already closes the file handle by default when exec'ing into another process. This is further supported by the fact that we indeed define `O_CLOEXEC` as `O_NOINHERIT` in case the former isn't defined in "compat/mingw.h". Adapt the code so that we know to handle `O_CLOEXEC` in case it has a different definition than `O_NOINHERIT` to improve our POSIX semantics handling. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b68faf commit 5390457

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

compat/mingw.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
560560
int fd;
561561

562562
/* We only support basic flags. */
563-
if (oflags & ~(O_ACCMODE | O_NOINHERIT)) {
563+
if (oflags & ~(O_ACCMODE | O_NOINHERIT | O_CLOEXEC)) {
564564
errno = ENOSYS;
565565
return -1;
566566
}
@@ -632,7 +632,7 @@ int mingw_open (const char *filename, int oflags, ...)
632632

633633
if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
634634
open_fn = mingw_open_append;
635-
else if (!(oflags & ~(O_ACCMODE | O_NOINHERIT)))
635+
else if (!(oflags & ~(O_ACCMODE | O_NOINHERIT | O_CLOEXEC)))
636636
open_fn = mingw_open_existing;
637637
else
638638
open_fn = _wopen;

0 commit comments

Comments
 (0)