set FD_CLOEXEC properly when O_CLOEXEC is not supported

FD_CLOEXEC only applies to the file descriptor, so it needs to be manipuluated via F_GETFD/F_SETFD. F_GETFL/F_SETFL are for file description flags. Verified via strace with o_cloexec set to zero. Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Jul 15, 2017 at 18:55 UTC 9fb9495dae744098a6eb16f62b4876c87b2c773f
1 file changed +3 -3
sha1_file.c
+3 -3
@@ -1571,14 +1571,14 @@ int git_open_cloexec(const char *name, int flags)
1571 fd = open(name, flags | o_cloexec);
1572 }
1573
1574 -#if defined(F_GETFL) && defined(F_SETFL) && defined(FD_CLOEXEC)
1574 +#if defined(F_GETFD) && defined(F_SETFD) && 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))
1580 + int flags = fcntl(fd, F_GETFD);
1581 + if (fcntl(fd, F_SETFD, flags | fd_cloexec))
1582 fd_cloexec = 0;
1583 }
1584 }