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).
int request;
int err;
#endif
- int flags;
+ int flags, new_flags;
int res;
#endif
return -1;
}
- if (inheritable)
- flags &= ~FD_CLOEXEC;
- else
- flags |= FD_CLOEXEC;
+ if (inheritable) {
+ new_flags = 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);
if (res < 0) {
if (raise)