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>
Junio C Hamano committed
Dec 29, 2006 at 00:30 UTC
2c039da804ee0542ff41d2f22a444d04a2d37856
1 file changed
+11
sha1_file.c
+11
@@ -540,6 +540,7 @@ static void open_packed_git(struct packed_git *p)
540
struct pack_header hdr;
541
unsigned char sha1[20];
542
unsigned char *idx_sha1;
543
+ long fd_flag;
544
545
p->pack_fd = open(p->pack_name, O_RDONLY);
546
if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
@@ -553,6 +554,16 @@ static void open_packed_git(struct packed_git *p)
554
} else if (p->pack_size != st.st_size)
555
die("packfile %s size changed", p->pack_name);
556
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
+
567
/* Verify we recognize this pack file format. */
568
read_or_die(p->pack_fd, &hdr, sizeof(hdr));
569
if (hdr.hdr_signature != htonl(PACK_SIGNATURE))