Skip to content

Commit 1e3001a

Browse files
committed
git_open_cloexec(): use fcntl(2) w/ FD_CLOEXEC fallback
A platform might not support open(2) with O_CLOEXEC but may support telling the same with fcntl(2) to flip FD_CLOEXEC bit on on an open file descriptor. It is a fallback that is inherently racy and this may not be worth doing, though. Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1b8ac5e commit 1e3001a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

sha1_file.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,14 +1561,28 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
15611561

15621562
int git_open_cloexec(const char *name, int flags)
15631563
{
1564-
static int cloexec = O_CLOEXEC;
1565-
int fd = open(name, flags | cloexec);
1564+
int fd;
1565+
static int o_cloexec = O_CLOEXEC;
15661566

1567-
if ((cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
1567+
fd = open(name, flags | o_cloexec);
1568+
if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
15681569
/* Try again w/o O_CLOEXEC: the kernel might not support it */
1569-
cloexec &= ~O_CLOEXEC;
1570-
fd = open(name, flags | cloexec);
1570+
o_cloexec &= ~O_CLOEXEC;
1571+
fd = open(name, flags | o_cloexec);
15711572
}
1573+
1574+
#if defined(F_GETFL) && defined(F_SETFL) && defined(FD_CLOEXEC)
1575+
{
1576+
static int fd_cloexec = FD_CLOEXEC;
1577+
1578+
if (!o_cloexec && 0 <= fd && fd_cloexec) {
1579+
/* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
1580+
int flags = fcntl(fd, F_GETFL);
1581+
if (fcntl(fd, F_SETFL, flags | fd_cloexec))
1582+
fd_cloexec = 0;
1583+
}
1584+
}
1585+
#endif
15721586
return fd;
15731587
}
15741588

0 commit comments

Comments
 (0)