Skip to content

don't rely on getdtablesize() #2586

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

Merged
merged 1 commit into from
Dec 12, 2019
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
39 changes: 38 additions & 1 deletion Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ extension CFSocketError {
}
#endif

#if !canImport(Darwin) && !os(Windows)
private func findMaximumOpenFromProcSelfFD() -> CInt? {
guard let dirPtr = opendir("/proc/self/fd") else {
return nil
}
defer {
closedir(dirPtr)
}
var highestFDSoFar = CInt(0)

while let dirEntPtr = readdir(dirPtr) {
var entryName = dirEntPtr.pointee.d_name
let thisFD = withUnsafeBytes(of: &entryName) { entryNamePtr -> CInt? in
CInt(String(decoding: entryNamePtr.prefix(while: { $0 != 0 }), as: Unicode.UTF8.self))
}
highestFDSoFar = max(thisFD ?? -1, highestFDSoFar)
}

return highestFDSoFar
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would
return try? FileManager.default.contentsOfDirectory(atPath: "/proc/self/fd").compactMap { CInt($0) }.max()

be a bit simpler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@spevans I wasn't sure about the guarantees around symlinks as in does it follow symlinks, does it not, what happens if the symlink is a broken link, etc. Therefore I went for readdir, happy to change if we think it's fine.


func findMaximumOpenFD() -> CInt {
if let maxFD = findMaximumOpenFromProcSelfFD() {
// the precise method worked, let's return this fd.
return maxFD
}

// We don't have /proc, let's go with the best estimate.
#if os(Linux)
return getdtablesize()
#else
return 4096
#endif
}
#endif


private func emptyRunLoopCallback(_ context : UnsafeMutableRawPointer?) -> Void {}


Expand Down Expand Up @@ -881,7 +918,7 @@ open class Process: NSObject {
posix_spawnattr_init(&spawnAttrs)
posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT))
#else
for fd in 3 ..< getdtablesize() {
for fd in 3 ... findMaximumOpenFD() {
guard adddup2[fd] == nil &&
!addclose.contains(fd) &&
fd != taskSocketPair[1] else {
Expand Down
8 changes: 7 additions & 1 deletion TestFoundation/xdgTestHelper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ func cat(_ args: ArraySlice<String>.Iterator) {

#if !os(Windows)
func printOpenFileDescriptors() {
for fd in 0..<getdtablesize() {
let reasonableMaxFD: CInt
#if os(Linux) || os(macOS)
reasonableMaxFD = getdtablesize()
#else
reasonableMaxFD = 4096
#endif
for fd in 0..<reasonableMaxFD {
if fcntl(fd, F_GETFD) != -1 {
print(fd)
}
Expand Down