Commit a858bbde authored by Victor Stinner's avatar Victor Stinner

Avoid fcntl() if possible in set_inheritable()

Issue #26770: set_inheritable() avoids calling fcntl() twice if the FD_CLOEXEC
is already set/cleared. This change only impacts platforms using the fcntl()
implementation of set_inheritable() (not Linux nor Windows).
parent b6a9c976
...@@ -798,7 +798,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) ...@@ -798,7 +798,7 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
int request; int request;
int err; int err;
#endif #endif
int flags; int flags, new_flags;
int res; int res;
#endif #endif
...@@ -884,10 +884,18 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) ...@@ -884,10 +884,18 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
return -1; return -1;
} }
if (inheritable) if (inheritable) {
flags &= ~FD_CLOEXEC; new_flags = flags & ~FD_CLOEXEC;
else }
flags |= FD_CLOEXEC; else {
new_flags = flags | FD_CLOEXEC;
}
if (new_flags == flags) {
/* FD_CLOEXEC flag already set/cleared: nothing to do */
return 0;
}
res = fcntl(fd, F_SETFD, flags); res = fcntl(fd, F_SETFD, flags);
if (res < 0) { if (res < 0) {
if (raise) if (raise)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment