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 2 commits 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
@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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of #error, how about if I just call getdtablesize unconditionally here and have it naturally error on platforms that don't support it.

@lin72h
Copy link

lin72h commented Jun 14, 2025

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

@iCharlesHu
Copy link
Contributor Author

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

Ahh thanks. Good to know.

char * _Nullable const args[_Nonnull],
char * _Nullable const env[_Nullable],
gid_t * _Nullable process_group_id
int _shims_snprintf(
Copy link

Choose a reason for hiding this comment

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

Why do we need this shim?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On Linux this function isn't exposed via import Glibc

@iCharlesHu iCharlesHu force-pushed the charles/fork-exec-close-fd branch from cd79141 to c217b39 Compare July 3, 2025 04:40
@iCharlesHu
Copy link
Contributor Author

Addressed review comments.

@lin72h
Copy link

lin72h commented Jul 7, 2025

Just a heads up - the PR has merged. I think the timing means it'll likely make it into the upcoming FreeBSD 15 release in December.

Copy link
Contributor

@itingliu itingliu left a comment

Choose a reason for hiding this comment

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

The Linux CI failure looks relevant

[7/22] Compiling CSystem shims.c
/__w/swift-subprocess/swift-subprocess/Sources/_SubprocessCShims/process_shims.c:318:28: error: use of undeclared identifier 'val'
  318 |     if (errno == ERANGE || val <= 0 || val > INT_MAX) {
      |         

@ricardobranco777
Copy link

ricardobranco777 commented Jul 26, 2025

Now that O_CLOFORK is merged in all *BSD, work can be started on support O_CLOFORK flags.

@iCharlesHu iCharlesHu force-pushed the charles/fork-exec-close-fd branch 3 times, most recently from e791d2e to d78a5c8 Compare August 12, 2025 17:49
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 d78a5c8 to 6176f07 Compare August 12, 2025 20:42

#if __DARWIN_NSIG /* Darwin */
# define _SUBPROCESS_SIG_MAX __DARWIN_NSIG
#elif defined(NSIG_MAX) /* POSIX issue 8 */
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit: the POSIX one should go first, being the standard thing, shouldn't it?


static int _highest_possibly_open_fd(void) {
#if defined(__linux__)
int hi = _highest_possibly_open_fd_dir_linux("/dev/fd");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: might be worth a comment that this is only needed for the Linux-older-than-5.9 path where we don't have close_range which makes this more efficient.

)
// Depending on the platform, standard error should be something like
// `/bin/bash: 7: Bad file descriptor
#expect(!result.standardError!.isEmpty)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: avoid force unwraps:

Suggested change
#expect(!result.standardError!.isEmpty)
let standardError = try #require(result.standardError)
#expect(!standardError.isEmpty)

// Close all other file descriptors
rc = -1;
errno = ENOSYS;
#if __has_include(<linux/close_range.h>) || defined(__FreeBSD__)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, one more thing. This breaks the build on Android because it doesn't have close_range.

/Volumes/Sources/OpenSource/swift-subprocess/Sources/_SubprocessCShims/process_shims.c:640:14: error: call to undeclared function 'close_range'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
640 | rc = close_range(STDERR_FILENO + 1, pipefd[1] - 1, 0);
| ^
1 error generated.
[8/22] Compiling CSystem shims.c

It was introduced in NDK r34 so you need an availability guard...

Copy link
Contributor

Choose a reason for hiding this comment

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

This will fix it:

Suggested change
#if __has_include(<linux/close_range.h>) || defined(__FreeBSD__)
#if (__has_include(<linux/close_range.h>) && (!defined(__ANDROID__) || __ANDROID_API__ >= 34)) || defined(__FreeBSD__)

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
8 participants