diff --git a/Sources/CSystem/CMakeLists.txt b/Sources/CSystem/CMakeLists.txt index 70e08e4..7f0a79f 100644 --- a/Sources/CSystem/CMakeLists.txt +++ b/Sources/CSystem/CMakeLists.txt @@ -13,12 +13,9 @@ target_include_directories(CSystem PUBLIC $) install(FILES - include/CSystemDarwin.h include/CSystemLinux.h include/CSystemShared.h include/CSystemWASI.h - include/CSystemWindows.h - include/CSystemFreeBSD.h include/io_uring.h include/module.modulemap DESTINATION include/CSystem) diff --git a/Sources/CSystem/include/CSystemDarwin.h b/Sources/CSystem/include/CSystemDarwin.h deleted file mode 100644 index bcef0a6..0000000 --- a/Sources/CSystem/include/CSystemDarwin.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - 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 -*/ - -#if defined(__APPLE__) - -#include - -#ifdef O_CLOFORK -#define COMPATIBILITY_O_CLOFORK O_CLOFORK -#else -#define COMPATIBILITY_O_CLOFORK 0 -#endif - -#endif // defined(__APPLE__) diff --git a/Sources/CSystem/include/CSystemFreeBSD.h b/Sources/CSystem/include/CSystemFreeBSD.h deleted file mode 100644 index d90c343..0000000 --- a/Sources/CSystem/include/CSystemFreeBSD.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - 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 -*/ - -#if defined(__FreeBSD__) - -#include - -#ifdef O_CLOFORK -#define COMPATIBILITY_O_CLOFORK O_CLOFORK -#else -#define COMPATIBILITY_O_CLOFORK 0 -#endif - -#endif // defined(__FreeBSD__) diff --git a/Sources/CSystem/include/CSystemLinux.h b/Sources/CSystem/include/CSystemLinux.h index 815e142..3c99b69 100644 --- a/Sources/CSystem/include/CSystemLinux.h +++ b/Sources/CSystem/include/CSystemLinux.h @@ -1,7 +1,7 @@ /* 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 @@ -9,16 +9,24 @@ #ifdef __linux__ -#include +// Glibc doesn't export eventfd, which the tests use. #include -#include -#include -#include -#include -#include -#include -#include -#include -#include "io_uring.h" -#endif +#if !defined(__ANDROID__) +#include +#include "io_uring.h" + +// Defined in shims.c, which can include the libc headers they need. +extern int csystem_io_uring_setup(unsigned int entries, + struct io_uring_params *p); +extern int csystem_io_uring_enter(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *sig); +extern int csystem_io_uring_enter2(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *args, size_t sz); +extern int csystem_io_uring_register(int fd, unsigned int opcode, void *arg, + unsigned int nr_args); +#endif // !defined(__ANDROID__) + +#endif // __linux__ diff --git a/Sources/CSystem/include/CSystemWindows.h b/Sources/CSystem/include/CSystemWindows.h deleted file mode 100644 index 7719be9..0000000 --- a/Sources/CSystem/include/CSystemWindows.h +++ /dev/null @@ -1,17 +0,0 @@ -/* - This source file is part of the Swift System open source project - - Copyright (c) 2020 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(_WIN32) - -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN -#define VC_EXTRA_LEAN -#include - -#endif diff --git a/Sources/CSystem/include/io_uring.h b/Sources/CSystem/include/io_uring.h index defc284..b633283 100644 --- a/Sources/CSystem/include/io_uring.h +++ b/Sources/CSystem/include/io_uring.h @@ -1,7 +1,6 @@ -#include -#include -#include -#include +#pragma once + +// Types only: the syscall wrappers need libc headers, so they're in shims.c. #define __SWIFT_IORING_SQE_FALLBACK_STRUCT { \ __u8 opcode; \ @@ -193,19 +192,6 @@ struct io_uring_params { }; #endif // __has_include() -#ifndef SWIFT_IORING_C_WRAPPER -#define SWIFT_IORING_C_WRAPPER - -# ifndef __NR_io_uring_setup -# define __NR_io_uring_setup 425 -# endif -# ifndef __NR_io_uring_enter -# define __NR_io_uring_enter 426 -# endif -# ifndef __NR_io_uring_register -# define __NR_io_uring_register 427 -# endif - /* struct io_uring_getevents_arg { __u64 sigmask; @@ -220,29 +206,3 @@ struct swift_io_uring_getevents_arg { __u32 min_wait_usec; __u64 ts; }; - -static inline int io_uring_register(int fd, unsigned int opcode, void *arg, - unsigned int nr_args) -{ - return syscall(__NR_io_uring_register, fd, opcode, arg, nr_args); -} - -static inline int io_uring_setup(unsigned int entries, struct io_uring_params *p) -{ - return syscall(__NR_io_uring_setup, entries, p); -} - -static inline int io_uring_enter2(int fd, unsigned int to_submit, unsigned int min_complete, - unsigned int flags, void *args, size_t sz) -{ - return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, - flags, args, sz); -} - -static inline int io_uring_enter(int fd, unsigned int to_submit, unsigned int min_complete, - unsigned int flags, sigset_t *sig) -{ - return io_uring_enter2(fd, to_submit, min_complete, flags, sig, _NSIG / 8); -} - -#endif diff --git a/Sources/CSystem/include/module.modulemap b/Sources/CSystem/include/module.modulemap index 31e1371..64a5c41 100644 --- a/Sources/CSystem/include/module.modulemap +++ b/Sources/CSystem/include/module.modulemap @@ -1,9 +1,17 @@ +// System imports CSystem with `internal import`. Don't include a header that +// the platform's C module also includes: CSystem would then co-own its +// declarations, so Swift would limit them to CSystem's import access level and +// could credit their members to CSystem under MemberImportVisibility. +// (CSystemWASI.h still does; see Constants.swift.) module CSystem { header "CSystemShared.h" - header "CSystemDarwin.h" header "CSystemLinux.h" header "CSystemWASI.h" - header "CSystemWindows.h" - header "CSystemFreeBSD.h" export * } + +// IORing's inlinable code stores these types, so System imports this module +// with `public import`. Keep libc headers out of it too. +module CSystemIOUring { + header "io_uring.h" +} diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index b32bd84..b0425ad 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -14,19 +14,14 @@ #include #endif -#if defined(__FreeBSD__) -#include -#endif - #ifdef __linux__ #define _GNU_SOURCE +#include +#include +#include #include #endif -#if defined(_WIN32) -#include -#endif - #include #if !defined(_WIN32) && !defined(__wasi__) && !defined(__APPLE__) @@ -70,3 +65,39 @@ extern int csystem_posix_dup3(int fildes, int fildes2, int flag) { return -1; #endif } + +#if defined(__linux__) && !defined(__ANDROID__) +# ifndef __NR_io_uring_setup +# define __NR_io_uring_setup 425 +# endif +# ifndef __NR_io_uring_enter +# define __NR_io_uring_enter 426 +# endif +# ifndef __NR_io_uring_register +# define __NR_io_uring_register 427 +# endif + +extern int csystem_io_uring_setup(unsigned int entries, + struct io_uring_params *p) { + return syscall(__NR_io_uring_setup, entries, p); +} + +extern int csystem_io_uring_enter2(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *args, size_t sz) { + return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, flags, + args, sz); +} + +extern int csystem_io_uring_enter(int fd, unsigned int to_submit, + unsigned int min_complete, + unsigned int flags, void *sig) { + return csystem_io_uring_enter2(fd, to_submit, min_complete, flags, sig, + _NSIG / 8); +} + +extern int csystem_io_uring_register(int fd, unsigned int opcode, void *arg, + unsigned int nr_args) { + return syscall(__NR_io_uring_register, fd, opcode, arg, nr_args); +} +#endif // defined(__linux__) && !defined(__ANDROID__) diff --git a/Sources/System/FilePath/FilePathTempPosix.swift b/Sources/System/FilePath/FilePathTempPosix.swift index 942b211..8b548a8 100644 --- a/Sources/System/FilePath/FilePathTempPosix.swift +++ b/Sources/System/FilePath/FilePathTempPosix.swift @@ -9,7 +9,6 @@ #if !os(Windows) -import CSystem #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) @@ -17,6 +16,7 @@ import Glibc #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) +internal import CSystem // For the members of system_dirent import WASILibc #elseif canImport(Android) import Android diff --git a/Sources/System/FileSystem/Stat.swift b/Sources/System/FileSystem/Stat.swift index ce4b6b5..e4a7f9d 100644 --- a/Sources/System/FileSystem/Stat.swift +++ b/Sources/System/FileSystem/Stat.swift @@ -15,15 +15,12 @@ #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import WASILibc #elseif canImport(Android) -import CSystem import Android #else #error("Unsupported Platform") diff --git a/Sources/System/IORing/IOCompletion.swift b/Sources/System/IORing/IOCompletion.swift index 9c7d55d..cdec927 100644 --- a/Sources/System/IORing/IOCompletion.swift +++ b/Sources/System/IORing/IOCompletion.swift @@ -10,7 +10,7 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +public import CSystemIOUring public extension IORing { struct Completion: ~Copyable { diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index ac46c3f..10129d8 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -10,7 +10,7 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +public import CSystemIOUring @usableFromInline internal enum IORequestCore { diff --git a/Sources/System/IORing/IORing+Util.swift b/Sources/System/IORing/IORing+Util.swift index be25886..2ff757c 100644 --- a/Sources/System/IORing/IORing+Util.swift +++ b/Sources/System/IORing/IORing+Util.swift @@ -10,14 +10,15 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +internal import CSystem +public import CSystemIOUring #if canImport(Glibc) import Glibc #elseif canImport(Musl) import Musl #endif -/// Throwing wrapper around the `io_uring_enter2` shim. +/// Throwing wrapper around the `csystem_io_uring_enter2` shim. /// /// On success returns the syscall's non-negative result (the number of SQEs /// consumed by the kernel). On failure, reads `errno` and throws the matching @@ -32,14 +33,14 @@ internal func _ioUringEnter2( argsSize: Int ) throws(Errno) -> Int32 { let result = valueOrErrno(retryOnInterrupt: true) { - io_uring_enter2( + csystem_io_uring_enter2( ringDescriptor, toSubmit, minComplete, flags, args, argsSize ) } return try result.get() } -/// Throwing wrapper around the `io_uring_enter` shim. +/// Throwing wrapper around the `csystem_io_uring_enter` shim. /// /// On success returns the syscall's non-negative result (the number of SQEs /// consumed by the kernel). On failure, reads `errno` and throws the matching @@ -53,12 +54,12 @@ internal func _ioUringEnter( sig: UnsafeMutablePointer? ) throws(Errno) -> Int32 { let result = valueOrErrno(retryOnInterrupt: true) { - io_uring_enter(ringDescriptor, toSubmit, minComplete, flags, sig) + csystem_io_uring_enter(ringDescriptor, toSubmit, minComplete, flags, sig) } return try result.get() } -/// Throwing wrapper around the `io_uring_register` shim. +/// Throwing wrapper around the `csystem_io_uring_register` shim. /// /// On success returns the syscall's non-negative result. On failure, reads /// `errno` and throws the matching `Errno`. `EINTR` is retried automatically. @@ -70,7 +71,7 @@ internal func _ioUringRegister( nrArgs: UInt32 ) throws(Errno) -> Int32 { let result = valueOrErrno(retryOnInterrupt: true) { - io_uring_register(ringDescriptor, opcode, arg, nrArgs) + csystem_io_uring_register(ringDescriptor, opcode, arg, nrArgs) } return try result.get() } diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index 473fe43..f592c9f 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -10,7 +10,8 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +internal import CSystem +public import CSystemIOUring // needed for mmap #if canImport(Glibc) import Glibc @@ -232,7 +233,7 @@ private func setUpRing( var err: Errno? = nil let ringDescriptor = withUnsafeMutablePointer(to: ¶ms) { - let result = io_uring_setup(queueDepth, $0) + let result = csystem_io_uring_setup(queueDepth, $0) if result < 0 { err = Errno.current } diff --git a/Sources/System/IORing/RawIORequest.swift b/Sources/System/IORing/RawIORequest.swift index 51e37ba..652f8bb 100644 --- a/Sources/System/IORing/RawIORequest.swift +++ b/Sources/System/IORing/RawIORequest.swift @@ -10,7 +10,7 @@ #if compiler(>=6.2) && $Lifetimes #if os(Linux) -import CSystem +public import CSystemIOUring #if canImport(Glibc) import Glibc #elseif canImport(Musl) diff --git a/Sources/System/Internals/CInterop.swift b/Sources/System/Internals/CInterop.swift index 5570d1e..6d98f12 100644 --- a/Sources/System/Internals/CInterop.swift +++ b/Sources/System/Internals/CInterop.swift @@ -10,19 +10,15 @@ #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif os(Windows) -import CSystem import ucrt #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import WASILibc -#elseif canImport(Bionic) -import CSystem -import Bionic +#elseif canImport(Android) +import Android #else #error("Unsupported Platform") #endif diff --git a/Sources/System/Internals/Constants.swift b/Sources/System/Internals/Constants.swift index 7a2ad9a..f74d784 100644 --- a/Sources/System/Internals/Constants.swift +++ b/Sources/System/Internals/Constants.swift @@ -11,11 +11,11 @@ // they can be used anywhere without imports and without confusion to // unavailable local decls. -import CSystem #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif os(Windows) import ucrt +import WinSDK #elseif canImport(Glibc) import Glibc #elseif canImport(Musl) @@ -28,6 +28,11 @@ import Android #error("Unsupported Platform") #endif +// Some WASI constants come from CSystem and are inlined into clients. +#if os(WASI) +public import CSystem +#endif + // MARK: errno #if SYSTEM_PACKAGE_DARWIN @_alwaysEmitIntoClient @@ -629,7 +634,7 @@ internal var _O_CLOEXEC: CInt { @_alwaysEmitIntoClient internal var _O_CLOFORK: CInt { #if SYSTEM_PACKAGE_DARWIN || os(FreeBSD) - COMPATIBILITY_O_CLOFORK + 0x0800_0000 // O_CLOFORK, which SDKs before macOS 27 and older FreeBSD lack #elseif !os(WASI) && !os(Linux) && !os(Android) O_CLOFORK #else diff --git a/Sources/System/Internals/Exports.swift b/Sources/System/Internals/Exports.swift index 6ce7d4d..7dd9d4e 100644 --- a/Sources/System/Internals/Exports.swift +++ b/Sources/System/Internals/Exports.swift @@ -10,23 +10,18 @@ // Internal wrappers and typedefs which help reduce #if littering in System's // code base. -// TODO: Should CSystem just include all the header files we need? - #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif os(Windows) -import CSystem import ucrt +import WinSDK #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import WASILibc #elseif canImport(Android) -import CSystem import Android #else #error("Unsupported Platform") diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index b0d9dae..0909029 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -7,7 +7,7 @@ See https://swift.org/LICENSE.txt for license information */ -import CSystem +internal import CSystem #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) diff --git a/Tests/MemberImportVisibility/Probe.swift b/Tests/MemberImportVisibility/Probe.swift index a27c836..9359931 100644 --- a/Tests/MemberImportVisibility/Probe.swift +++ b/Tests/MemberImportVisibility/Probe.swift @@ -16,8 +16,9 @@ // - Access raw C members of vended types (e.g. CInterop.Stat) as a downstream // consumer would. Don't go through the Swift (e.g. Stat) wrapper since its // accesses happen inside System, where importing CSystem is fine. -// - Touch every direct member the public API exposes. One field is not always -// a safe proxy for the rest. +// - Touch every direct member the public API exposes, and the members of C +// types it exposes through them (e.g. timespec). One field is not always a +// safe proxy for the rest. #if canImport(Glibc) || canImport(Musl) || canImport(Android) #if canImport(Glibc) @@ -43,5 +44,7 @@ func _mivProbe(_ s: CInterop.Stat) { _ = s.st_atim _ = s.st_mtim _ = s.st_ctim + _ = s.st_atim.tv_sec + _ = s.st_atim.tv_nsec } #endif diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index f62dc48..2434bb5 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -201,6 +201,22 @@ final class FileOperationsTest: XCTestCase { } } } + + #if canImport(Darwin, _version: 428) // The macOS 27 SDK defines O_CLOFORK. + func testAdHocPipeCloseOnFork() throws { + guard #available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *) else { + throw XCTSkip("pipe2 and PipeOptions require anyAppleOS 27 or newer") + } + // System spells O_CLOFORK as a literal on Darwin. + XCTAssertEqual(FileDescriptor.PipeOptions.closeOnFork.rawValue, O_CLOFORK) + let pipe = try FileDescriptor.pipe(options: .closeOnFork) + try pipe.readEnd.closeAfter { + try pipe.writeEnd.closeAfter { + XCTAssertEqual(fcntl(pipe.readEnd.rawValue, F_GETFD) & FD_CLOFORK, FD_CLOFORK) + } + } + } + #endif #endif // !os(WASI) #if !os(Windows) diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 3b11ea3..6a51bba 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -2,7 +2,8 @@ #if os(Linux) import XCTest -import CSystem //for eventfd +import CSystem // for eventfd and csystem_io_uring_setup +import CSystemIOUring #if canImport(Glibc) import Glibc // for errno #elseif canImport(Musl) @@ -30,7 +31,7 @@ let failureMessage = "Runtime environment does not support IORing." func isUringEnabled() throws -> Bool { // Even if the kernel supports io_uring, the SystemPackage build may have // been compiled against older kernel headers that lack features it needs - // (gated on IORING_TIMEOUT_BOOTTIME in CSystem); in that configuration + // (gated on IORING_TIMEOUT_BOOTTIME in io_uring.h); in that configuration // IORing.init throws ENOTSUP. Treat that as disabled so tests skip cleanly // instead of failing. do throws(Errno) { @@ -61,7 +62,7 @@ func isUringEnabled() throws -> Bool { // - EPERM or EACCES -> treat as disabled // - propagate any other error. var params = io_uring_params() - let raw = io_uring_setup(1, ¶ms) + let raw = csystem_io_uring_setup(1, ¶ms) if raw < 0 { let err = Errno(rawValue: errno) switch err { diff --git a/Tests/SystemTests/StatTests.swift b/Tests/SystemTests/StatTests.swift index 11876b8..37e2aa2 100644 --- a/Tests/SystemTests/StatTests.swift +++ b/Tests/SystemTests/StatTests.swift @@ -16,10 +16,8 @@ import Testing #if SYSTEM_PACKAGE_DARWIN import Darwin #elseif canImport(Glibc) -import CSystem import Glibc #elseif canImport(Musl) -import CSystem import Musl #elseif canImport(WASILibc) import CSystem