Skip to content

Commit a64cba1

Browse files
committed
fix
1 parent 8be5c68 commit a64cba1

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

Sources/CoreFoundation/CFPlatform.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#if TARGET_OS_ANDROID
5050
#include <sys/prctl.h>
51+
#include <fcntl.h>
5152
#endif
5253

5354
#if TARGET_OS_MAC || TARGET_OS_WIN32
@@ -2216,13 +2217,23 @@ static int _CFPosixSpawnImplPre28(pid_t *_CF_RESTRICT pid, const char *_CF_RESTR
22162217
for (size_t idx = 0; idx < actions->actionsCount; idx++) {
22172218
struct _CFPosixSpawnFileActionPre28 *action = &(actions->actions[idx]);
22182219
if (action->type == _CFPosixSpawnFileActionDup2Pre28) {
2219-
if (dup2(action->dup2Action.filedes, action->dup2Action.newfiledes) < 0) {
2220-
exit(127);
2220+
int fd = action->dup2Action.filedes;
2221+
int new_fd = action->dup2Action.newfiledes;
2222+
2223+
if (fd == new_fd) {
2224+
// MATCHING BIONIC:
2225+
// dup2(2) is a no-op if fd == new_fd, but POSIX suggests that we should
2226+
// manually remove the O_CLOEXEC flag in that case (because otherwise
2227+
// what use is the dup?).
2228+
int fdflags = fcntl(fd, F_GETFD, 0);
2229+
if (fdflags == -1 || fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) _exit(127);
2230+
} else {
2231+
if (dup2(fd, new_fd) == -1) _exit(127);
22212232
}
22222233
} else if (actions->actions[idx].type == _CFPosixSpawnFileActionClosePre28) {
2223-
if (close(action->closeAction.filedes) != 0) {
2224-
exit(127);
2225-
}
2234+
// MATCHING BIONIC:
2235+
// Failure to close is ignored (e.g. EBADF if already closed).
2236+
close(action->closeAction.filedes);
22262237
}
22272238
}
22282239
}

0 commit comments

Comments
 (0)