| 27 | #endif |
| 28 | |
| 29 | int git_open_cloexec(const char *name, int flags) |
| 30 | { |
| 31 | int fd; |
| 32 | static int o_cloexec = O_CLOEXEC; |
| 33 | |
| 34 | fd = open(name, flags | o_cloexec); |
| 35 | if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) { |
| 36 | /* Try again w/o O_CLOEXEC: the kernel might not support it */ |
| 37 | o_cloexec &= ~O_CLOEXEC; |
| 38 | fd = open(name, flags | o_cloexec); |
| 39 | } |
| 40 | |
| 41 | #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC) |
| 42 | { |
| 43 | static int fd_cloexec = FD_CLOEXEC; |
| 44 | |
| 45 | if (!o_cloexec && 0 <= fd && fd_cloexec) { |
| 46 | /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */ |
| 47 | int flags = fcntl(fd, F_GETFD); |
| 48 | if (fcntl(fd, F_SETFD, flags | fd_cloexec)) |
| 49 | fd_cloexec = 0; |
| 50 | } |
| 51 | } |
| 52 | #endif |
| 53 | return fd; |
| 54 | } |
no test coverage detected