Skip to content

Commit 2c039da

Browse files
author
Junio C Hamano
committed
mmap: set FD_CLOEXEC for file descriptors we keep open for mmap()
I do not have any proof that this matters to any existing problems I am seeing, but I do not think of any reason not to do this. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 9c18df1 commit 2c039da

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

sha1_file.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ static void open_packed_git(struct packed_git *p)
540540
struct pack_header hdr;
541541
unsigned char sha1[20];
542542
unsigned char *idx_sha1;
543+
long fd_flag;
543544

544545
p->pack_fd = open(p->pack_name, O_RDONLY);
545546
if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
@@ -553,6 +554,16 @@ static void open_packed_git(struct packed_git *p)
553554
} else if (p->pack_size != st.st_size)
554555
die("packfile %s size changed", p->pack_name);
555556

557+
/* We leave these file descriptors open with sliding mmap;
558+
* there is no point keeping them open across exec(), though.
559+
*/
560+
fd_flag = fcntl(p->pack_fd, F_GETFD, 0);
561+
if (fd_flag < 0)
562+
die("cannot determine file descriptor flags");
563+
fd_flag |= FD_CLOEXEC;
564+
if (fcntl(p->pack_fd, F_SETFD, fd_flag) == -1)
565+
die("cannot set FD_CLOEXEC");
566+
556567
/* Verify we recognize this pack file format. */
557568
read_or_die(p->pack_fd, &hdr, sizeof(hdr));
558569
if (hdr.hdr_signature != htonl(PACK_SIGNATURE))

0 commit comments

Comments
 (0)