Re: [PATCH v3 2/2] mingw: ensure temporary file handles are not inherited by child processes
From: Eric Wong <hidden>
Date: 2016-08-22 17:58:29
Johannes Schindelin [off-list ref] wrote:
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.
quoted hunk ↗ jump to hunk
+++ b/git-compat-util.h@@ -667,6 +667,10 @@ void *gitmemmem(const void *haystack, size_t haystacklen, #define getpagesize() sysconf(_SC_PAGESIZE) #endif +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif
quoted hunk ↗ jump to hunk
+++ 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;
Looks good to me from a Linux standpoint. Thanks.