Skip to content

Emulate POSIX_SPAWN_CLOEXEC_DEFAULT in fork/exec #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iCharlesHu
Copy link
Contributor

POSIX_SPAWN_CLOEXEC_DEFAULT is only available on Darwin. Emulate POSIX_SPAWN_CLOEXEC_DEFAULT on other platforms by calling close after fork, before exec.

This commit also removes _subprocess_posix_spawn_fallback because we can't emulate POSIX_SPAWN_CLOEXEC_DEFAULT in a thread-safe manner while using posix_spawn.

Resolves: #46

@iCharlesHu iCharlesHu requested review from parkera and itingliu June 13, 2025 22:57
@iCharlesHu iCharlesHu force-pushed the charles/fork-exec-close-fd branch from 6e9afb5 to 3e47e05 Compare June 13, 2025 23:01
// Close all other file descriptors
rc = -1;
errno = ENOSYS;
#if __has_include(<linux/close_range.h>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should add || defined(__FreeBSD__) (FreeBSD also has close_range, defined in unistd.h).

POSIX_SPAWN_CLOEXEC_DEFAULT is only available on Darwin. Emulate POSIX_SPAWN_CLOEXEC_DEFAULT on other platforms by calling close after fork, before exec.

This commit also removes _subprocess_posix_spawn_fallback because we can't emulate POSIX_SPAWN_CLOEXEC_DEFAULT in a thread-safe manner while using posix_spawn.
@iCharlesHu iCharlesHu force-pushed the charles/fork-exec-close-fd branch from 3e47e05 to 2d9a937 Compare June 13, 2025 23:11
hi = getdtablesize();
}
#else
int hi = 1024;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does '1024' come from and how safe is it to default to this vs making this #error and requiring a specific implementation for new platforms?

Also, the following additional platforms support getdtablesize: FreeBSD, OpenBSD, NetBSD, Solaris, QNX.

@lin72h
Copy link

lin72h commented Jun 14, 2025

FreeBSD PR #1698 adds support for POSIX O_CLOFORK, which could simplify this implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fork+exec fails to close other file descriptors
3 participants