Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Sources/Testing/ExitTests/SpawnProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ typealias ProcessID = HANDLE
typealias ProcessID = Never
#endif

#if os(Linux) && !SWT_NO_DYNAMIC_LINKING
/// Close file descriptors above a given value when spawing a new process.
///
/// This symbol is provided because the underlying function was added to glibc
/// relatively recently and may not be available on all targets. Checking
/// `__GLIBC_PREREQ()` is insufficient because `_DEFAULT_SOURCE` may not be
/// defined at the point spawn.h is first included.
private let _posix_spawn_file_actions_addclosefrom_np = symbol(named: "posix_spawn_file_actions_addclosefrom_np").map {
unsafeBitCast($0, to: (@convention(c) (UnsafeMutablePointer<posix_spawn_file_actions_t>, CInt) -> CInt).self)
}
#endif

/// Spawn a process and wait for it to terminate.
///
/// - Parameters:
Expand Down Expand Up @@ -141,17 +153,18 @@ func spawnExecutable(
// Close all other file descriptors open in the parent.
flags |= CShort(POSIX_SPAWN_CLOEXEC_DEFAULT)
#elseif os(Linux)
#if !SWT_NO_DYNAMIC_LINKING
// This platform doesn't have POSIX_SPAWN_CLOEXEC_DEFAULT, but we can at
// least close all file descriptors higher than the highest inherited one.
// We are assuming here that the caller didn't set FD_CLOEXEC on any of
// these file descriptors.
_ = swt_posix_spawn_file_actions_addclosefrom_np(fileActions, highestFD + 1)
_ = _posix_spawn_file_actions_addclosefrom_np?(fileActions, highestFD + 1)
#endif
#elseif os(FreeBSD)
// Like Linux, this platform doesn't have POSIX_SPAWN_CLOEXEC_DEFAULT.
// Unlike Linux, all non-EOL FreeBSD versions (≥13.1) support
// `posix_spawn_file_actions_addclosefrom_np`. Therefore, we don't need
// `swt_posix_spawn_file_actions_addclosefrom_np` to guard the
// availability of this function.
// `posix_spawn_file_actions_addclosefrom_np`, and FreeBSD does not use
// glibc nor guard symbols behind `_DEFAULT_SOURCE`.
_ = posix_spawn_file_actions_addclosefrom_np(fileActions, highestFD + 1)
#elseif os(OpenBSD)
// OpenBSD does not have posix_spawn_file_actions_addclosefrom_np().
Expand Down
15 changes: 14 additions & 1 deletion Sources/Testing/ExitTests/WaitFor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ private nonisolated(unsafe) let _waitThreadNoChildrenCondition = {
return result
}()

#if os(Linux) && !SWT_NO_DYNAMIC_LINKING
/// Set the name of the current thread.
///
/// This function declaration is provided because `pthread_setname_np()` is
/// only declared if `_GNU_SOURCE` is set, but setting it causes build errors
/// due to conflicts with Swift's Glibc module.
private let _pthread_setname_np = symbol(named: "pthread_setname_np").map {
unsafeBitCast($0, to: (@convention(c) (pthread_t, UnsafePointer<CChar>) -> CInt).self)
}
#endif

/// Create a waiter thread that is responsible for waiting for child processes
/// to exit.
private let _createWaitThread: Void = {
Expand Down Expand Up @@ -152,7 +163,9 @@ private let _createWaitThread: Void = {
#if SWT_TARGET_OS_APPLE
_ = pthread_setname_np("Swift Testing exit test monitor")
#elseif os(Linux)
_ = swt_pthread_setname_np(pthread_self(), "SWT ExT monitor")
#if !SWT_NO_DYNAMIC_LINKING
_ = _pthread_setname_np?(pthread_self(), "SWT ExT monitor")
#endif
#elseif os(FreeBSD)
_ = pthread_set_name_np(pthread_self(), "SWT ex test monitor")
#elseif os(OpenBSD)
Expand Down
1 change: 0 additions & 1 deletion Sources/_TestingInternals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ include(LibraryVersion)
include(TargetTriple)
add_library(_TestingInternals STATIC
Discovery.cpp
Stubs.cpp
Versions.cpp
WillThrow.cpp)
target_include_directories(_TestingInternals PUBLIC
Expand Down
45 changes: 0 additions & 45 deletions Sources/_TestingInternals/Stubs.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions Sources/_TestingInternals/include/Stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,6 @@ static char *_Nullable *_Null_unspecified swt_environ(void) {
}
#endif

#if defined(__linux__)
/// Set the name of the current thread.
///
/// This function declaration is provided because `pthread_setname_np()` is
/// only declared if `_GNU_SOURCE` is set, but setting it causes build errors
/// due to conflicts with Swift's Glibc module.
SWT_EXTERN int swt_pthread_setname_np(pthread_t thread, const char *name);
#endif

#if defined(__GLIBC__)
/// Close file descriptors above a given value when spawing a new process.
///
/// This symbol is provided because the underlying function was added to glibc
/// relatively recently and may not be available on all targets. Checking
/// `__GLIBC_PREREQ()` is insufficient because `_DEFAULT_SOURCE` may not be
/// defined at the point spawn.h is first included.
SWT_EXTERN int swt_posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *fileActions, int from);
#endif

#if !defined(__ANDROID__)
#if __has_include(<signal.h>) && defined(si_pid)
/// Get the value of the `si_pid` field of a `siginfo_t` structure.
Expand Down