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 <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Oct 31, 2016 at 10:41 UTC
1e3001a8e2386a1433d1769a5a78007596bbc04d
1 file changed
+19
-5
sha1_file.c
+19
-5
@@ -1561,14 +1561,28 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
1561
1562
int git_open_cloexec(const char *name, int flags)
1563
{
1564
- static int cloexec = O_CLOEXEC;
1565
- int fd = open(name, flags | cloexec);
1564
+ int fd;
1565
+ static int o_cloexec = O_CLOEXEC;
1566
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) {
1569
/* 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);
1572
}
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
1586
return fd;
1587
}
1588