git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] compat/mingw: handle O_CLOEXEC in `mingw_open_existing()`

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 13, 2025, 14:17 UTC
Message-ID
<20250313-b4-pks-mingw-lockfile-flake-v1-1-bc5d3e70f516@pks.im>
In-Reply-To
<20250313-b4-pks-mingw-lockfile-flake-v1-0-bc5d3e70f516@pks.im>

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 <ps@pks.im>
---
 compat/mingw.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index f524c54d06d..101e380c5a3 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -560,7 +560,7 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
 	int fd;
 
 	/* We only support basic flags. */
-	if (oflags & ~(O_ACCMODE | O_NOINHERIT)) {
+	if (oflags & ~(O_ACCMODE | O_NOINHERIT | O_CLOEXEC)) {
 		errno = ENOSYS;
 		return -1;
 	}
@@ -632,7 +632,7 @@ int mingw_open (const char *filename, int oflags, ...)
 
 	if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
 		open_fn = mingw_open_append;
-	else if (!(oflags & ~(O_ACCMODE | O_NOINHERIT)))
+	else if (!(oflags & ~(O_ACCMODE | O_NOINHERIT | O_CLOEXEC)))
 		open_fn = mingw_open_existing;
 	else
 		open_fn = _wopen;
-- 
2.49.0.rc2.394.gf6994c5077.dirty
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 6 of 14 in “compat/mingw: fix EACCESS when opening files with `O_CREAT | O_EXCL`”
  1. 0/2 compat/mingw: fix EACCESS when opening files with `O_CREAT | O_EXCL`Patrick Steinhardt, Mar 13, 2025
  2. 2/2 compat/mingw: fix EACCESS when opening files with `O_CREAT | O_EXCL`Patrick Steinhardt, Mar 13, 2025
  3. Junio C HamanoMar 13, 2025
  4. Johannes SchindelinMar 16, 2025
  5. Patrick SteinhardtMar 17, 2025
  6. 1/2 compat/mingw: handle O_CLOEXEC in `mingw_open_existing()`Patrick Steinhardt, Mar 13, 2025
  7. Junio C HamanoMar 13, 2025
  8. 0/2 compat/mingw: fix EACCESS when opening files with `O_CREAT | O_EXCL`Patrick Steinhardt, Mar 20, 2025
  9. 1/2 meson: fix compat sources when compiling with MSVCPatrick Steinhardt, Mar 20, 2025
  10. 2/2 compat/mingw: fix EACCESS when opening files with `O_CREAT | O_EXCL`Patrick Steinhardt, Mar 20, 2025
  11. Johannes SchindelinMar 26, 2025
  12. Patrick SteinhardtMar 28, 2025
  13. Johannes SchindelinMar 28, 2025
  14. Patrick SteinhardtMar 31, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.