sha1_file: open window into packfiles with O_CLOEXEC

All processes that the Git main process spawns inherit the open file descriptors of the main process. These leaked file descriptors can cause problems. Use the O_CLOEXEC flag similar to 05d1ed61 to fix the leaked file descriptors. Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Oct 24, 2016 at 20:02 UTC cd66ada06588f797a424dd1f6da1c6bb51de1660
1 file changed +9 -4
sha1_file.c
+9 -4
@@ -1561,7 +1561,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
1561
1562 int git_open(const char *name)
1563 {
1564 - static int sha1_file_open_flag = O_NOATIME;
1564 + static int sha1_file_open_flag = O_NOATIME | O_CLOEXEC;
1565
1566 for (;;) {
1567 int fd;
@@ -1571,12 +1571,17 @@ int git_open(const char *name)
1571 if (fd >= 0)
1572 return fd;
1573
1574 - /* Might the failure be due to O_NOATIME? */
1575 - if (errno != ENOENT && sha1_file_open_flag) {
1576 - sha1_file_open_flag = 0;
1574 + /* Try again w/o O_CLOEXEC: the kernel might not support it */
1575 + if ((sha1_file_open_flag & O_CLOEXEC) && errno == EINVAL) {
1576 + sha1_file_open_flag &= ~O_CLOEXEC;
1577 continue;
1578 }
1579
1580 + /* Might the failure be due to O_NOATIME? */
1581 + if (errno != ENOENT && (sha1_file_open_flag & O_NOATIME)) {
1582 + sha1_file_open_flag &= ~O_NOATIME;
1583 + continue;
1584 + }
1585 return -1;
1586 }
1587 }