diff --git a/Sources/NIOConcurrencyHelpers/NIOLock.swift b/Sources/NIOConcurrencyHelpers/NIOLock.swift index 2513885ad8..02224f7f57 100644 --- a/Sources/NIOConcurrencyHelpers/NIOLock.swift +++ b/Sources/NIOConcurrencyHelpers/NIOLock.swift @@ -17,8 +17,10 @@ import Darwin #elseif os(Windows) import ucrt import WinSDK -#else +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif #if os(Windows) diff --git a/Sources/NIOConcurrencyHelpers/atomics.swift b/Sources/NIOConcurrencyHelpers/atomics.swift index b558952902..8925d72a61 100644 --- a/Sources/NIOConcurrencyHelpers/atomics.swift +++ b/Sources/NIOConcurrencyHelpers/atomics.swift @@ -26,7 +26,12 @@ fileprivate func sys_sched_yield() { Sleep(0) } #else +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif + fileprivate func sys_sched_yield() { _ = sched_yield() } diff --git a/Sources/NIOConcurrencyHelpers/lock.swift b/Sources/NIOConcurrencyHelpers/lock.swift index d89e8728cc..e786ee8f86 100644 --- a/Sources/NIOConcurrencyHelpers/lock.swift +++ b/Sources/NIOConcurrencyHelpers/lock.swift @@ -17,8 +17,10 @@ import Darwin #elseif os(Windows) import ucrt import WinSDK -#else +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif /// A threading lock based on `libpthread` instead of `libdispatch`. diff --git a/Sources/NIOCore/BSDSocketAPI.swift b/Sources/NIOCore/BSDSocketAPI.swift index 438da969de..5c961a2689 100644 --- a/Sources/NIOCore/BSDSocketAPI.swift +++ b/Sources/NIOCore/BSDSocketAPI.swift @@ -61,7 +61,11 @@ import func WinSDK.WSAGetLastError internal typealias socklen_t = ucrt.size_t #elseif os(Linux) || os(Android) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif import CNIOLinux private let sysInet_ntop: @convention(c) (CInt, UnsafeRawPointer?, UnsafeMutablePointer?, socklen_t) -> UnsafePointer? = inet_ntop diff --git a/Sources/NIOCore/ByteBuffer-core.swift b/Sources/NIOCore/ByteBuffer-core.swift index 268707fe54..14e9939173 100644 --- a/Sources/NIOCore/ByteBuffer-core.swift +++ b/Sources/NIOCore/ByteBuffer-core.swift @@ -16,8 +16,10 @@ import ucrt #elseif canImport(Darwin) import Darwin -#else +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif @usableFromInline let sysMalloc: @convention(c) (size_t) -> UnsafeMutableRawPointer? = malloc diff --git a/Sources/NIOCore/FileHandle.swift b/Sources/NIOCore/FileHandle.swift index 9c916af9d0..dcbdac62a1 100644 --- a/Sources/NIOCore/FileHandle.swift +++ b/Sources/NIOCore/FileHandle.swift @@ -15,8 +15,10 @@ import ucrt #elseif canImport(Darwin) import Darwin -#elseif os(Linux) || os(Android) +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif #if os(Windows) diff --git a/Sources/NIOCore/FileRegion.swift b/Sources/NIOCore/FileRegion.swift index 4016731097..8a3dc21937 100644 --- a/Sources/NIOCore/FileRegion.swift +++ b/Sources/NIOCore/FileRegion.swift @@ -15,8 +15,10 @@ import ucrt #elseif canImport(Darwin) import Darwin -#elseif os(Linux) || os(Android) +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #endif diff --git a/Sources/NIOCore/Interfaces.swift b/Sources/NIOCore/Interfaces.swift index 4b09d1c36f..3288c9109f 100644 --- a/Sources/NIOCore/Interfaces.swift +++ b/Sources/NIOCore/Interfaces.swift @@ -12,7 +12,11 @@ // //===----------------------------------------------------------------------===// #if os(Linux) || os(FreeBSD) || os(Android) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif import CNIOLinux #elseif canImport(Darwin) import Darwin diff --git a/Sources/NIOCore/SocketAddresses.swift b/Sources/NIOCore/SocketAddresses.swift index 0426d138dd..90d73e95a4 100644 --- a/Sources/NIOCore/SocketAddresses.swift +++ b/Sources/NIOCore/SocketAddresses.swift @@ -44,7 +44,11 @@ fileprivate typealias sa_family_t = WinSDK.ADDRESS_FAMILY #elseif canImport(Darwin) import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif import CNIOLinux #endif diff --git a/Sources/NIOCore/SocketOptionProvider.swift b/Sources/NIOCore/SocketOptionProvider.swift index 27e7330c1d..23f0e5d17e 100644 --- a/Sources/NIOCore/SocketOptionProvider.swift +++ b/Sources/NIOCore/SocketOptionProvider.swift @@ -14,7 +14,11 @@ #if canImport(Darwin) import Darwin #elseif os(Linux) || os(Android) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif import CNIOLinux #elseif os(Windows) import WinSDK diff --git a/Sources/NIOCore/SystemCallHelpers.swift b/Sources/NIOCore/SystemCallHelpers.swift index f9c60fbf4d..beb8e2856d 100644 --- a/Sources/NIOCore/SystemCallHelpers.swift +++ b/Sources/NIOCore/SystemCallHelpers.swift @@ -22,7 +22,11 @@ #if canImport(Darwin) import Darwin.C #elseif os(Linux) || os(FreeBSD) || os(Android) +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif #elseif os(Windows) import CNIOWindows #else diff --git a/Sources/NIOCore/Utilities.swift b/Sources/NIOCore/Utilities.swift index 232552ef2a..18e9cb14cc 100644 --- a/Sources/NIOCore/Utilities.swift +++ b/Sources/NIOCore/Utilities.swift @@ -13,7 +13,11 @@ //===----------------------------------------------------------------------===// #if os(Linux) || os(FreeBSD) || os(Android) import CNIOLinux +#if canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl +#endif #elseif os(Windows) import let WinSDK.RelationProcessorCore diff --git a/Sources/NIOPosix/BSDSocketAPICommon.swift b/Sources/NIOPosix/BSDSocketAPICommon.swift index 087ee30a71..f33d3b663d 100644 --- a/Sources/NIOPosix/BSDSocketAPICommon.swift +++ b/Sources/NIOPosix/BSDSocketAPICommon.swift @@ -66,7 +66,7 @@ extension NIOBSDSocket.SocketType: Hashable { extension NIOBSDSocket.SocketType { /// Supports datagrams, which are connectionless, unreliable messages of a /// fixed (typically small) maximum length. - #if os(Linux) + #if canImport(Glibc) internal static let datagram: NIOBSDSocket.SocketType = NIOBSDSocket.SocketType(rawValue: CInt(SOCK_DGRAM.rawValue)) #else @@ -76,7 +76,7 @@ extension NIOBSDSocket.SocketType { /// Supports reliable, two-way, connection-based byte streams without /// duplication of data and without preservation of boundaries. - #if os(Linux) + #if canImport(Glibc) internal static let stream: NIOBSDSocket.SocketType = NIOBSDSocket.SocketType(rawValue: CInt(SOCK_STREAM.rawValue)) #else diff --git a/Sources/NIOPosix/DatagramVectorReadManager.swift b/Sources/NIOPosix/DatagramVectorReadManager.swift index 014dc6c6fa..679baacf06 100644 --- a/Sources/NIOPosix/DatagramVectorReadManager.swift +++ b/Sources/NIOPosix/DatagramVectorReadManager.swift @@ -112,13 +112,14 @@ struct DatagramVectorReadManager { } // Next we set up the msghdr structure. This points into the other vectors. - let msgHdr = msghdr(msg_name: self.sockaddrVector.baseAddress! + i , - msg_namelen: socklen_t(MemoryLayout.size), - msg_iov: self.ioVector.baseAddress! + i, - msg_iovlen: 1, // This is weird, but each message gets only one array. Duh. - msg_control: controlBytes.baseAddress, - msg_controllen: .init(controlBytes.count), - msg_flags: 0) + var msgHdr = msghdr() + msgHdr.msg_name = .init(self.sockaddrVector.baseAddress! + i) + msgHdr.msg_namelen = socklen_t(MemoryLayout.size) + msgHdr.msg_iov = self.ioVector.baseAddress! + i + msgHdr.msg_iovlen = 1 // This is weird, but each message gets only one array. Duh + msgHdr.msg_control = controlBytes.baseAddress + msgHdr.msg_controllen = .init(controlBytes.count) + msgHdr.msg_flags = 0 self.messageVector[i] = MMsgHdr(msg_hdr: msgHdr, msg_len: 0) // Note that we don't set up the sockaddr vector: that's because it needs no initialization, diff --git a/Sources/NIOPosix/Linux.swift b/Sources/NIOPosix/Linux.swift index 4d998a0fb6..d5f25f76cd 100644 --- a/Sources/NIOPosix/Linux.swift +++ b/Sources/NIOPosix/Linux.swift @@ -82,6 +82,13 @@ internal enum Epoll { internal static let EPOLLRDHUP: CUnsignedInt = 8192 //numericCast(CNIOLinux.EPOLLRDHUP) internal static let EPOLLHUP: CUnsignedInt = 16 //numericCast(CNIOLinux.EPOLLHUP) internal static let EPOLLET: CUnsignedInt = 2147483648 //numericCast(CNIOLinux.EPOLLET) + #elseif canImport(Musl) + internal static let EPOLLIN: CUnsignedInt = numericCast(CNIOLinux.EPOLLIN) + internal static let EPOLLOUT: CUnsignedInt = numericCast(CNIOLinux.EPOLLOUT) + internal static let EPOLLERR: CUnsignedInt = numericCast(CNIOLinux.EPOLLERR) + internal static let EPOLLRDHUP: CUnsignedInt = numericCast(CNIOLinux.EPOLLRDHUP) + internal static let EPOLLHUP: CUnsignedInt = numericCast(CNIOLinux.EPOLLHUP) + internal static let EPOLLET: CUnsignedInt = numericCast(CNIOLinux.EPOLLET) #else internal static let EPOLLIN: CUnsignedInt = numericCast(CNIOLinux.EPOLLIN.rawValue) internal static let EPOLLOUT: CUnsignedInt = numericCast(CNIOLinux.EPOLLOUT.rawValue) @@ -121,6 +128,9 @@ internal enum Linux { #if os(Android) static let SOCK_CLOEXEC = Glibc.SOCK_CLOEXEC static let SOCK_NONBLOCK = Glibc.SOCK_NONBLOCK +#elseif canImport(Musl) + static let SOCK_CLOEXEC = Musl.SOCK_CLOEXEC + static let SOCK_NONBLOCK = Musl.SOCK_NONBLOCK #else static let SOCK_CLOEXEC = CInt(bitPattern: Glibc.SOCK_CLOEXEC.rawValue) static let SOCK_NONBLOCK = CInt(bitPattern: Glibc.SOCK_NONBLOCK.rawValue) diff --git a/Sources/NIOPosix/PendingDatagramWritesManager.swift b/Sources/NIOPosix/PendingDatagramWritesManager.swift index e2bfea7816..b34cb4a2b8 100644 --- a/Sources/NIOPosix/PendingDatagramWritesManager.swift +++ b/Sources/NIOPosix/PendingDatagramWritesManager.swift @@ -135,13 +135,14 @@ private func doPendingDatagramWriteVectorOperation(pending: PendingDatagramWrite controlBytes.appendExplicitCongestionState(metadata: p.metadata, protocolFamily: protocolFamily) let controlMessageBytePointer = controlBytes.validControlBytes - let msg = msghdr(msg_name: address, - msg_namelen: addressLen, - msg_iov: iovecs.baseAddress! + c, - msg_iovlen: 1, - msg_control: controlMessageBytePointer.baseAddress, - msg_controllen: .init(controlMessageBytePointer.count), - msg_flags: 0) + var msg = msghdr() + msg.msg_name = .init(address) + msg.msg_namelen = addressLen + msg.msg_iov = iovecs.baseAddress! + c + msg.msg_iovlen = 1 + msg.msg_control = controlMessageBytePointer.baseAddress + msg.msg_controllen = .init(controlMessageBytePointer.count) + msg.msg_flags = 0 msgs[c] = MMsgHdr(msg_hdr: msg, msg_len: 0) } c += 1 diff --git a/Sources/NIOPosix/Socket.swift b/Sources/NIOPosix/Socket.swift index e470ec395e..ea82ee7c3c 100644 --- a/Sources/NIOPosix/Socket.swift +++ b/Sources/NIOPosix/Socket.swift @@ -186,13 +186,14 @@ typealias IOVector = iovec capacity: controlBytes.count)), dwFlags: 0) #else - var messageHeader = msghdr(msg_name: notConstCorrectDestinationPtr, - msg_namelen: destinationSize, - msg_iov: vecPtr, - msg_iovlen: 1, - msg_control: controlBytes.baseAddress, - msg_controllen: .init(controlBytes.count), - msg_flags: 0) + var messageHeader = msghdr() + messageHeader.msg_name = notConstCorrectDestinationPtr + messageHeader.msg_namelen = destinationSize + messageHeader.msg_iov = vecPtr + messageHeader.msg_iovlen = 1 + messageHeader.msg_control = controlBytes.baseAddress + messageHeader.msg_controllen = .init(controlBytes.count) + messageHeader.msg_flags = 0 #endif return try NIOBSDSocket.sendmsg(socket: handle, msgHdr: &messageHeader, flags: 0) } @@ -243,13 +244,14 @@ typealias IOVector = iovec storageLen = messageHeader.namelen } #else - var messageHeader = msghdr(msg_name: sockaddrPtr, - msg_namelen: storageLen, - msg_iov: vecPtr, - msg_iovlen: 1, - msg_control: controlBytes.controlBytesBuffer.baseAddress, - msg_controllen: .init(controlBytes.controlBytesBuffer.count), - msg_flags: 0) + var messageHeader = msghdr() + messageHeader.msg_name = .init(sockaddrPtr) + messageHeader.msg_namelen = storageLen + messageHeader.msg_iov = vecPtr + messageHeader.msg_iovlen = 1 + messageHeader.msg_control = controlBytes.controlBytesBuffer.baseAddress + messageHeader.msg_controllen = .init(controlBytes.controlBytesBuffer.count) + messageHeader.msg_flags = 0 defer { // We need to write back the length of the message. storageLen = messageHeader.msg_namelen diff --git a/Sources/NIOPosix/System.swift b/Sources/NIOPosix/System.swift index ba5d5c6d66..d7ad6ac7a6 100644 --- a/Sources/NIOPosix/System.swift +++ b/Sources/NIOPosix/System.swift @@ -22,7 +22,11 @@ import NIOCore import CNIODarwin internal typealias MMsgHdr = CNIODarwin_mmsghdr #elseif os(Linux) || os(FreeBSD) || os(Android) +#if canImport(Glibc) @_exported import Glibc +#elseif canImport(Musl) +@_exported import Musl +#endif import CNIOLinux internal typealias MMsgHdr = CNIOLinux_mmsghdr internal typealias in6_pktinfo = CNIOLinux_in6_pktinfo @@ -107,7 +111,7 @@ private let sysIfNameToIndex: @convention(c) (UnsafePointer?) -> CUnsigne private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointer?) -> CInt = socketpair #endif -#if os(Linux) +#if canImport(Glibc) private let sysFstat: @convention(c) (CInt, UnsafeMutablePointer) -> CInt = fstat private let sysStat: @convention(c) (UnsafePointer, UnsafeMutablePointer) -> CInt = stat private let sysLstat: @convention(c) (UnsafePointer, UnsafeMutablePointer) -> CInt = lstat @@ -384,11 +388,17 @@ internal enum Posix { static let SHUT_WR: CInt = CInt(Darwin.SHUT_WR) static let SHUT_RDWR: CInt = CInt(Darwin.SHUT_RDWR) #elseif os(Linux) || os(FreeBSD) || os(Android) - + #if canImport(Glibc) static let UIO_MAXIOV: Int = Int(Glibc.UIO_MAXIOV) static let SHUT_RD: CInt = CInt(Glibc.SHUT_RD) static let SHUT_WR: CInt = CInt(Glibc.SHUT_WR) static let SHUT_RDWR: CInt = CInt(Glibc.SHUT_RDWR) + #elseif canImport(Musl) + static let UIO_MAXIOV: Int = Int(Musl.UIO_MAXIOV) + static let SHUT_RD: CInt = CInt(Musl.SHUT_RD) + static let SHUT_WR: CInt = CInt(Musl.SHUT_WR) + static let SHUT_RDWR: CInt = CInt(Musl.SHUT_RDWR) + #endif #else static var UIO_MAXIOV: Int { fatalError("unsupported OS") @@ -659,7 +669,11 @@ internal enum Posix { return ssize_t(result) #elseif os(Linux) || os(FreeBSD) || os(Android) var off: off_t = offset + #if canImport(Glibc) let result: ssize_t = Glibc.sendfile(descriptor, fd, &off, count) + #elseif canImport(Musl) + let result: ssize_t = Musl.sendfile(descriptor, fd, &off, count) + #endif if result >= 0 { written = off_t(result) } else { diff --git a/Sources/NIOPosix/ThreadPosix.swift b/Sources/NIOPosix/ThreadPosix.swift index 76a0672674..b6e0ed4a30 100644 --- a/Sources/NIOPosix/ThreadPosix.swift +++ b/Sources/NIOPosix/ThreadPosix.swift @@ -39,17 +39,24 @@ private func sysPthread_create(handle: UnsafeMutablePointer, #if canImport(Darwin) return pthread_create(handle, nil, destructor, args) #else + #if canImport(Musl) + var handleLinux: OpaquePointer? = nil + let result = pthread_create(&handleLinux, + nil, + destructor, + args) + #else var handleLinux = pthread_t() let result = pthread_create(&handleLinux, nil, destructor, args) + #endif handle.pointee = handleLinux return result #endif } - typealias ThreadOpsSystem = ThreadOpsPosix enum ThreadOpsPosix: ThreadOps { diff --git a/Sources/_NIODataStructures/Heap.swift b/Sources/_NIODataStructures/Heap.swift index 0874cae862..b1c7847ab6 100644 --- a/Sources/_NIODataStructures/Heap.swift +++ b/Sources/_NIODataStructures/Heap.swift @@ -13,8 +13,10 @@ //===----------------------------------------------------------------------===// #if canImport(Darwin) import Darwin.C -#elseif os(Linux) || os(FreeBSD) || os(Android) +#elseif canImport(Glibc) import Glibc +#elseif canImport(Musl) +import Musl #elseif os(Windows) import ucrt #endif