diff --git a/Sources/CSystem/CMakeLists.txt b/Sources/CSystem/CMakeLists.txt index faf730e..6bde43c 100644 --- a/Sources/CSystem/CMakeLists.txt +++ b/Sources/CSystem/CMakeLists.txt @@ -1,19 +1,20 @@ #[[ This source file is part of the Swift System open source project -Copyright (c) 2020 Apple Inc. and the Swift System project authors +Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information #]] -add_library(CSystem INTERFACE) -target_include_directories(CSystem INTERFACE +add_library(CSystem STATIC shims.c) +target_include_directories(CSystem PUBLIC "$" $) install(FILES include/CSystemLinux.h + include/CSystemShared.h include/CSystemWindows.h include/module.modulemap DESTINATION include/CSystem) diff --git a/Sources/CSystem/include/CSystemShared.h b/Sources/CSystem/include/CSystemShared.h new file mode 100644 index 0000000..ccc05b1 --- /dev/null +++ b/Sources/CSystem/include/CSystemShared.h @@ -0,0 +1,11 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2026 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +extern int csystem_posix_pipe2(int fildes[2], int flag); +extern int csystem_posix_dup3(int fildes, int fildes2, int flag); diff --git a/Sources/CSystem/include/module.modulemap b/Sources/CSystem/include/module.modulemap index 6e8b89e..73b1004 100644 --- a/Sources/CSystem/include/module.modulemap +++ b/Sources/CSystem/include/module.modulemap @@ -1,4 +1,5 @@ module CSystem { + header "CSystemShared.h" header "CSystemLinux.h" header "CSystemWASI.h" header "CSystemWindows.h" diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index f492a2a..433cc78 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -1,18 +1,46 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information */ +#if defined(__FreeBSD__) || defined(__OpenBSD__) +#define __BSD_VISIBLE +#include +#endif + #ifdef __linux__ - +#define _GNU_SOURCE #include - #endif #if defined(_WIN32) #include #endif + +#include + +#if !defined(_WIN32) && !defined(__wasi__) && !defined(__APPLE__) +#define HAVE_PIPE2_DUP3 +#endif + +// Wrappers are required because _GNU_SOURCE causes a conflict with other imports when defined in CSystemLinux.h +extern int csystem_posix_pipe2(int fildes[2], int flag) { + #ifdef HAVE_PIPE2_DUP3 + return pipe2(fildes, flag); + #else + errno = ENOSYS; + return -1; + #endif +} +extern int csystem_posix_dup3(int fildes, int fildes2, int flag) { + #ifdef HAVE_PIPE2_DUP3 + return dup3(fildes, fildes2, flag); + #else + errno = ENOSYS; + return -1; + #endif +} diff --git a/Sources/System/FileDescriptor.swift b/Sources/System/FileDescriptor.swift index d5f5883..86f36bb 100644 --- a/Sources/System/FileDescriptor.swift +++ b/Sources/System/FileDescriptor.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -306,12 +306,10 @@ extension FileDescriptor { /// /// Normally, file descriptors remain open /// across calls to the `exec(2)` family of functions. - /// If you specify this option, - /// the file descriptor is closed when replacing this process - /// with another process. + /// If you specify this option, the system closes the file + /// descriptor when replacing this process with another process. /// - /// The state of the file - /// descriptor flags can be inspected using `F_GETFD`, + /// You can inspect the file descriptor flag state using `F_GETFD`, /// as described in the `fcntl(2)` man page. /// /// The corresponding C constant is `O_CLOEXEC`. @@ -324,6 +322,133 @@ extension FileDescriptor { #endif } + /// Options that specify behavior for a newly-created pipe. + @frozen + @available(Windows, unavailable) + @available(macOS, unavailable) + @available(iOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS, unavailable) + @available(visionOS, unavailable) + public struct PipeOptions: OptionSet, Sendable, Hashable, Codable { + /// The raw C options. + @_alwaysEmitIntoClient + public var rawValue: CInt + + /// Create a strongly-typed options value from raw C options. + @_alwaysEmitIntoClient + public init(rawValue: CInt) { self.rawValue = rawValue } + +#if !os(Windows) + /// Indicates that all subsequent input and output operations + /// on the pipe's file descriptors will be nonblocking. + /// + /// The corresponding C constant is `O_NONBLOCK`. + @_alwaysEmitIntoClient + public static var nonBlocking: PipeOptions { .init(rawValue: _O_NONBLOCK) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "nonBlocking") + public static var O_NONBLOCK: PipeOptions { nonBlocking } + + /// Indicates that executing a program closes the file. + /// + /// Normally, file descriptors remain open + /// across calls to the `exec(2)` family of functions. + /// If you specify this option, the system closes the file + /// descriptor when replacing this process with another process. + /// + /// You can inspect the file descriptor flag state using `F_GETFD`, + /// as described in the `fcntl(2)` man page. + /// + /// The corresponding C constant is `O_CLOEXEC`. + @_alwaysEmitIntoClient + public static var closeOnExec: PipeOptions { .init(rawValue: _O_CLOEXEC) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "closeOnExec") + public static var O_CLOEXEC: PipeOptions { closeOnExec } + +#if !os(WASI) && !os(Linux) && !os(Android) + /// Indicates that forking a program closes the file. + /// + /// Normally, file descriptors remain open + /// across calls to the `fork(2)` function. + /// If you specify this option, the system closes the file + /// descriptor when forking this process into another process. + /// + /// You can inspect the file descriptor flag state using `F_GETFD`, + /// as described in the `fcntl(2)` man page. + /// + /// The corresponding C constant is `O_CLOFORK`. + @_alwaysEmitIntoClient + public static var closeOnFork: PipeOptions { .init(rawValue: _O_CLOFORK) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "closeOnFork") + public static var O_CLOFORK: PipeOptions { closeOnFork } +#endif +#endif + } + + /// Options that specify behavior for a duplicated file descriptor. + @frozen + @available(Windows, unavailable) + @available(macOS, unavailable) + @available(iOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS, unavailable) + @available(visionOS, unavailable) + public struct DuplicateOptions: OptionSet, Sendable, Hashable, Codable { + /// The raw C options. + @_alwaysEmitIntoClient + public var rawValue: CInt + + /// Create a strongly-typed options value from raw C options. + @_alwaysEmitIntoClient + public init(rawValue: CInt) { self.rawValue = rawValue } + +#if !os(Windows) + /// Indicates that executing a program closes the file. + /// + /// Normally, file descriptors remain open + /// across calls to the `exec(2)` family of functions. + /// If you specify this option, the system closes the file + /// descriptor when replacing this process with another process. + /// + /// You can inspect the file descriptor flag state using `F_GETFD`, + /// as described in the `fcntl(2)` man page. + /// + /// The corresponding C constant is `O_CLOEXEC`. + @_alwaysEmitIntoClient + public static var closeOnExec: DuplicateOptions { .init(rawValue: _O_CLOEXEC) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "closeOnExec") + public static var O_CLOEXEC: DuplicateOptions { closeOnExec } + +#if !os(WASI) && !os(Linux) && !os(Android) + /// Indicates that forking a program closes the file. + /// + /// Normally, file descriptors remain open + /// across calls to the `fork(2)` function. + /// If you specify this option, the system closes the file + /// descriptor when forking this process into another process. + /// + /// You can inspect the file descriptor flag state using `F_GETFD`, + /// as described in the `fcntl(2)` man page. + /// + /// The corresponding C constant is `O_CLOFORK`. + @_alwaysEmitIntoClient + public static var closeOnFork: DuplicateOptions { .init(rawValue: _O_CLOFORK) } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "closeOnFork") + public static var O_CLOFORK: DuplicateOptions { closeOnFork } +#endif +#endif + } + /// Options for specifying what a file descriptor's offset is relative to. @frozen @available(System 0.0.1, *) diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index 9ddc16c..f69374f 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -395,7 +395,6 @@ extension FileDescriptor { /// /// However, each file descriptor maintains its own close-on-exec flag. /// - /// /// The corresponding C functions are `dup` and `dup2`. @_alwaysEmitIntoClient @available(System 0.0.2, *) @@ -406,17 +405,77 @@ extension FileDescriptor { try _duplicate(as: target, retryOnInterrupt: retryOnInterrupt).get() } + /// Duplicate this file descriptor and return the newly created copy. + /// + /// - Parameters: + /// - target: The desired target file descriptor. + /// - options: The behavior for creating the target file descriptor. + /// - retryOnInterrupt: Whether to retry the operation + /// if it throws ``Errno/interrupted``. The default is `true`. + /// Pass `false` to try only once and throw an error upon interruption. + /// - Returns: The new file descriptor. + /// + /// If the `target` descriptor is already in use, then it is first + /// deallocated as if a close(2) call had been done first. + /// + /// File descriptors are merely references to some underlying system resource. + /// The system does not distinguish between the original and the new file + /// descriptor in any way. For example, read, write and seek operations on + /// one of them also affect the logical file position in the other, and + /// append mode, non-blocking I/O and asynchronous I/O options are shared + /// between the references. If a separate pointer into the file is desired, + /// a different object reference to the file must be obtained by issuing an + /// additional call to `open`. + /// + /// However, each file descriptor maintains its own close-on-exec and + /// close-on-fork flags. + /// + /// The corresponding C function is `dup3`. + @discardableResult + @_alwaysEmitIntoClient + @available(Windows, unavailable) + @available(macOS, unavailable) + @available(iOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS, unavailable) + @available(visionOS, unavailable) + public func duplicate( + as target: FileDescriptor, + options: DuplicateOptions, + retryOnInterrupt: Bool = true + ) throws(Errno) -> FileDescriptor { + try _duplicate(as: target, options: options.rawValue, retryOnInterrupt: retryOnInterrupt).get() + } + @available(System 0.0.2, *) @usableFromInline internal func _duplicate( as target: FileDescriptor?, retryOnInterrupt: Bool - ) throws -> Result { + ) -> Result { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { - if let target = target { - return system_dup2(self.rawValue, target.rawValue) + if let target { + system_dup2(self.rawValue, target.rawValue) + } else { + system_dup(self.rawValue) } - return system_dup(self.rawValue) + }.map(FileDescriptor.init(rawValue:)) + } + + @available(Windows, unavailable) + @available(macOS, unavailable) + @available(iOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS, unavailable) + @available(visionOS, unavailable) + @usableFromInline + internal func _duplicate( + as target: FileDescriptor, + options: Int32, + retryOnInterrupt: Bool + ) -> Result { + valueOrErrno(retryOnInterrupt: retryOnInterrupt) { + system_dup3(self.rawValue, target.rawValue, options) }.map(FileDescriptor.init(rawValue:)) } @@ -431,6 +490,12 @@ extension FileDescriptor { public func dup2() throws -> FileDescriptor { fatalError("Not implemented") } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "duplicate") + public func dup3() throws -> FileDescriptor { + fatalError("Not implemented") + } } #endif @@ -444,13 +509,37 @@ extension FileDescriptor { /// The corresponding C function is `pipe`. @_alwaysEmitIntoClient @available(System 1.1.0, *) - public static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) { + public static func pipe( + ) throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) { try _pipe().get() } + /// Creates a unidirectional data channel, which can be used for + /// interprocess communication. + /// + /// - Parameters: + /// - options: The behavior for creating the pipe. + /// + /// - Returns: The pair of file descriptors. + /// + /// The corresponding C function is `pipe2`. + @_alwaysEmitIntoClient + @available(Windows, unavailable) + @available(macOS, unavailable) + @available(iOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS, unavailable) + @available(visionOS, unavailable) + public static func pipe( + options: PipeOptions + ) throws(Errno) -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) { + try _pipe(options: options.rawValue).get() + } + @available(System 1.1.0, *) @usableFromInline - internal static func _pipe() -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> { + internal static func _pipe( + ) -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> { var fds: (Int32, Int32) = (-1, -1) return withUnsafeMutablePointer(to: &fds) { pointer in pointer.withMemoryRebound(to: Int32.self, capacity: 2) { fds in @@ -460,6 +549,32 @@ extension FileDescriptor { } } } + + @available(Windows, unavailable) + @available(macOS, unavailable) + @available(iOS, unavailable) + @available(tvOS, unavailable) + @available(watchOS, unavailable) + @available(visionOS, unavailable) + @usableFromInline + internal static func _pipe( + options: Int32, + ) -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> { + var fds: (Int32, Int32) = (-1, -1) + return withUnsafeMutablePointer(to: &fds) { pointer in + pointer.withMemoryRebound(to: Int32.self, capacity: 2) { fds in + valueOrErrno(retryOnInterrupt: false) { + system_pipe2(fds, options) + }.map { _ in (.init(rawValue: fds[0]), .init(rawValue: fds[1])) } + } + } + } + + @_alwaysEmitIntoClient + @available(*, unavailable, renamed: "pipe") + public static func pipe2() throws -> FileDescriptor { + fatalError("Not implemented") + } } #endif diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index b026c97..8f9fc48 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -622,6 +622,15 @@ internal var _O_SYMLINK: CInt { O_SYMLINK } #if !os(Windows) @_alwaysEmitIntoClient internal var _O_CLOEXEC: CInt { O_CLOEXEC } + +@_alwaysEmitIntoClient +internal var _O_CLOFORK: CInt { + #if !os(WASI) && !os(Linux) && !os(Android) && !canImport(Darwin) + O_CLOFORK + #else + 0 + #endif +} #endif @_alwaysEmitIntoClient diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index f6eb533..7deae4d 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2026 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -14,7 +14,6 @@ import Glibc #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) -import CSystem import WASILibc #elseif os(Windows) import ucrt @@ -24,6 +23,8 @@ import Android #error("Unsupported Platform") #endif +import CSystem + // Interacting with the mocking system, tracing, etc., is a potentially significant // amount of code size, so we hand outline that code for every syscall @@ -139,6 +140,13 @@ internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 { #endif return dup2(fd, fd2) } + +internal func system_dup3(_ fd: Int32, _ fd2: Int32, _ oflag: Int32) -> Int32 { + #if ENABLE_MOCKING + if mockingEnabled { return _mock(fd, fd2, oflag) } + #endif + return csystem_posix_dup3(fd, fd2, oflag) +} #endif #if !os(WASI) @@ -148,6 +156,13 @@ internal func system_pipe(_ fds: UnsafeMutablePointer) -> CInt { #endif return pipe(fds) } + +internal func system_pipe2(_ fds: UnsafeMutablePointer, _ oflag: Int32) -> CInt { +#if ENABLE_MOCKING + if mockingEnabled { return _mock(fds, oflag) } +#endif + return csystem_posix_pipe2(fds, oflag) +} #endif internal func system_ftruncate(_ fd: Int32, _ length: off_t) -> Int32 {