Browse Source

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>
maint
Eric Wong 8 years ago committed by Junio C Hamano
parent
commit
9fb9495dae
  1. 6
      sha1_file.c

6
sha1_file.c

@ -1571,14 +1571,14 @@ int git_open_cloexec(const char *name, int flags) @@ -1571,14 +1571,14 @@ int git_open_cloexec(const char *name, int flags)
fd = open(name, flags | o_cloexec);
}

#if defined(F_GETFL) && defined(F_SETFL) && defined(FD_CLOEXEC)
#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
{
static int fd_cloexec = FD_CLOEXEC;

if (!o_cloexec && 0 <= fd && fd_cloexec) {
/* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
int flags = fcntl(fd, F_GETFL);
if (fcntl(fd, F_SETFL, flags | fd_cloexec))
int flags = fcntl(fd, F_GETFD);
if (fcntl(fd, F_SETFD, flags | fd_cloexec))
fd_cloexec = 0;
}
}

Loading…
Cancel
Save