Skip to content

Commit cd66ada

Browse files
larsxschneidergitster
authored andcommitted
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 05d1ed6 to fix the leaked file descriptors. Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5436b5 commit cd66ada

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

sha1_file.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map,
15611561

15621562
int git_open(const char *name)
15631563
{
1564-
static int sha1_file_open_flag = O_NOATIME;
1564+
static int sha1_file_open_flag = O_NOATIME | O_CLOEXEC;
15651565

15661566
for (;;) {
15671567
int fd;
@@ -1571,12 +1571,17 @@ int git_open(const char *name)
15711571
if (fd >= 0)
15721572
return fd;
15731573

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;
15771577
continue;
15781578
}
15791579

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+
}
15801585
return -1;
15811586
}
15821587
}

0 commit comments

Comments
 (0)