Skip to content

Commit c783c17

Browse files
committed
Try stubbing pthread_setname_np and posix_spawn_file_actions_addclosefrom_np functions a different way
1 parent 32b58ed commit c783c17

File tree

5 files changed

+65
-31
lines changed

5 files changed

+65
-31
lines changed

Sources/Testing/ExitTests/SpawnProcess.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func spawnExecutable(
9898
// least close all file descriptors higher than the highest inherited one.
9999
// We are assuming here that the caller didn't set FD_CLOEXEC on any of
100100
// these file descriptors.
101-
_ = _posix_spawn_file_actions_addclosefrom_np?(fileActions.baseAddress!, highestFD + 1)
101+
_ = swt_posix_spawn_file_actions_addclosefrom_np(fileActions.baseAddress!, highestFD + 1)
102102
#else
103103
#warning("Platform-specific implementation missing: cannot close unused file descriptors")
104104
#endif
@@ -191,20 +191,6 @@ func spawnExecutable(
191191

192192
// MARK: -
193193

194-
#if os(Linux) || os(FreeBSD)
195-
/// Close file descriptors above a given value when spawing a new process.
196-
///
197-
/// This symbol is provided because the underlying function was added to glibc
198-
/// relatively recently and may not be available on all targets. Checking
199-
/// `__GLIBC_REQ()` is insufficient because `_DEFAULT_SOURCE` may not be
200-
/// defined at the point spawn.h is first included.
201-
private let _posix_spawn_file_actions_addclosefrom_np = {
202-
symbol(named: "posix_spawn_file_actions_addclosefrom_np").map {
203-
unsafeBitCast($0, to: (@convention(c) (UnsafeMutablePointer<posix_spawn_file_actions_t>, CInt) -> CInt).self)
204-
}
205-
}()
206-
#endif
207-
208194
#if os(Windows)
209195
/// Create a temporary instance of `STARTUPINFOEXW` to pass to
210196
/// `CreateProcessW()`.

Sources/Testing/ExitTests/WaitFor.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private let _createWaitThreadImpl: Void = {
109109
#if SWT_TARGET_OS_APPLE
110110
_ = pthread_setname_np("Swift Testing exit test monitor")
111111
#elseif os(Linux)
112-
_ = _pthread_setname_np?(pthread_self(), "SWT ExT monitor")
112+
_ = swt_pthread_setname_np(pthread_self(), "SWT ExT monitor")
113113
#elseif os(FreeBSD)
114114
_ = pthread_set_name_np(pthread_self(), "SWT ex test monitor")
115115
#else
@@ -232,19 +232,4 @@ func wait(for processHandle: consuming HANDLE) async throws -> ExitCondition {
232232
return .exitCode(CInt(bitPattern: .init(status)))
233233
}
234234
#endif
235-
236-
// MARK: -
237-
238-
#if os(Linux)
239-
/// Set the name of the current thread.
240-
///
241-
/// This symbol is provided because `pthread_setname_np()` is only declared if
242-
/// `_GNU_SOURCE` is set, but setting it causes build errors due to conflicts
243-
/// with Swift's Glibc module.
244-
private let _pthread_setname_np = {
245-
symbol(named: "pthread_setname_np").map {
246-
unsafeBitCast($0, to: (@convention(c) (pthread_t, UnsafePointer<CChar>) -> CInt).self)
247-
}
248-
}()
249-
#endif
250235
#endif

Sources/_TestingInternals/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include(LibraryVersion)
1212
include(TargetTriple)
1313
add_library(_TestingInternals STATIC
1414
Discovery.cpp
15+
Stubs.cpp
1516
Versions.cpp
1617
WillThrow.cpp)
1718
target_include_directories(_TestingInternals PUBLIC

Sources/_TestingInternals/Stubs.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
//
10+
11+
/// This source file includes implementations of functions that _should_ simply
12+
/// be `static` stubs in Stubs.h, but which for technical reasons cannot be
13+
/// imported into Swift when defined in a header.
14+
///
15+
/// Do not, as a rule, add function implementations in this file. Prefer to add
16+
/// them to Stubs.h so that they can be inlined at compile- or link-time. Only
17+
/// include functions here if Swift cannot successfully import and call them
18+
/// otherwise.
19+
20+
#if defined(__linux__) || defined(__FreeBSD__)
21+
#define _DEFAULT_SOURCE
22+
#define _GNU_SOURCE
23+
#endif
24+
25+
#include "Stubs.h"
26+
27+
28+
#if defined(__linux__)
29+
int swt_pthread_setname_np(pthread_t thread, const char *name) {
30+
return pthread_setname_np(thread, name);
31+
}
32+
#endif
33+
34+
#if defined(__linux__) || defined(__FreeBSD__)
35+
int swt_posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *fileActions, int from) {
36+
#if defined(__GLIBC_REQ) && __GLIBC_REQ(2, 34)
37+
return posix_spawn_file_actions_addclosefrom_np(fileActions, from);
38+
#else
39+
#warning glibc too old for posix_spawn_file_actions_addclosefrom_np
40+
return 0;
41+
#endif
42+
}
43+
#endif

Sources/_TestingInternals/include/Stubs.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ static char *_Nullable *_Null_unspecified swt_environ(void) {
109109
}
110110
#endif
111111

112+
#if defined(__linux__)
113+
/// Set the name of the current thread.
114+
///
115+
/// This function declaration is provided because `pthread_setname_np()` is
116+
/// only declared if `_GNU_SOURCE` is set, but setting it causes build errors
117+
/// due to conflicts with Swift's Glibc module.
118+
SWT_EXTERN int swt_pthread_setname_np(pthread_t thread, const char *name);
119+
#endif
120+
121+
#if defined(__linux__) || defined(__FreeBSD__)
122+
/// Close file descriptors above a given value when spawing a new process.
123+
///
124+
/// This symbol is provided because the underlying function was added to glibc
125+
/// relatively recently and may not be available on all targets. Checking
126+
/// `__GLIBC_REQ()` is insufficient because `_DEFAULT_SOURCE` may not be
127+
/// defined at the point spawn.h is first included.
128+
SWT_EXTERN int swt_posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *fileActions, int from);
129+
#endif
130+
112131
#if !defined(__ANDROID__)
113132
#if __has_include(<signal.h>) && defined(si_pid)
114133
/// Get the value of the `si_pid` field of a `siginfo_t` structure.

0 commit comments

Comments
 (0)