Partial OpenBSD support. (#3394)

### Motivation:

Additional partial platform support.

### Modifications: 

* Create a new CNIOBSD module for OpenBSD, for general cleanliness, and
make use of it throughout. Some of the changes are borrowed directly
from CNIOLinux; some may technically be unnecessary; I am erring
somewhat on expediency to functionality.

* Since `malloc_size` is unavailable on OpenBSD, and some of the helpers
make use of ManagedBufferPointer which makes use of it, mark some of
this as unavailable as well.

* Usual pthread optional typing changes, since pthread types are
pointers on this platform.

* Add conditionals to exclude other functionality not available here,
like IP_RECVPKTINFO or IP_PKTINFO.

* d_ino is a backwards-compatibility macro valued token for dirent, so
instead, just expand with a conditional.

* Use kqueue on OpenBSD. This necessitates adding some conditionals for
type and feature compatibility.

* The vsock API is unavailable on OpenBSD.

### Result:

Tested the NIOTCPEchoClient and NIOTCPEchoServer appears to work and
that swift-nio-ssh (hopefully wlog) builds with a local repository with
these changes.

Because NIOFS/_NIOFileSystem depend on some non-portable components,
such as extended attributes, sendfile, non-portable linkat flags, and
renameat2, this is only just partial OpenBSD support, which means that
`swift build` on the full swift-nio project won't build cleanly, but at
least the portable parts can be used to build servers and clients. This
commit will mark the linked bug as fixed; I will open a new bug for file
system support.

Fixes #3383.

---------

Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
This commit is contained in:
3405691582andRick Newton-Rogers authored and GitHub committed 2025-11-26 09:07:02 +00:00
1 parent cda536de37
commit 2bc71a6aff
32 files changed
+415 -21

No files matched your search

+7
View File
@@ -65,6 +65,7 @@ let package = Package(
dependencies: [
"NIOConcurrencyHelpers",
"_NIOBase64",
"CNIOOpenBSD",
"CNIODarwin",
"CNIOLinux",
"CNIOWindows",
@@ -97,6 +98,7 @@ let package = Package(
.target(
name: "NIOPosix",
dependencies: [
"CNIOOpenBSD",
"CNIOLinux",
"CNIODarwin",
"CNIOWindows",
@@ -153,6 +155,10 @@ let package = Package(
name: "CNIOSHA1",
dependencies: []
),
.target(
name: "CNIOOpenBSD",
dependencies: []
),
.target(
name: "CNIOLinux",
dependencies: [],
@@ -488,6 +494,7 @@ let package = Package(
"NIOTestUtils",
"NIOConcurrencyHelpers",
"NIOEmbedded",
"CNIOOpenBSD",
"CNIOLinux",
"CNIODarwin",
"NIOTLS",
+2
View File
@@ -103,6 +103,8 @@ SwiftNIO aims to support all of the platforms where Swift is supported. Currentl
* Ubuntu 18.04+
* macOS 10.9+, iOS 7+; (macOS 10.14+, iOS 12+, tvOS 12+ or watchOS 6+ with [swift-nio-transport-services][repo-nio-transport-services])
SwiftNIO has experimental support on OpenBSD for all SwiftNIO libraries _except_ for NIOFileSystem, which is not yet supported. You can use all other SwiftNIO libraries on OpenBSD by adding them as dependencies in `Package.swift`.
### Compatibility
SwiftNIO follows [SemVer 2.0.0](https://semver.org/#semantic-versioning-200) with a separate document declaring [SwiftNIO's Public API](docs/public-api.md).
+101
View File
@@ -0,0 +1,101 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#ifndef C_NIO_OPENBSD_H
#define C_NIO_OPENBSD_H
#if defined(__OpenBSD__)
#include <sys/types.h>
#include <sys/event.h>
#include <pthread_np.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/utsname.h>
#include <dirent.h>
#include <fts.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <stdbool.h>
// Some explanation is required here.
//
// Due to SR-6772, we cannot get Swift code to directly see any of the mmsg structures or
// functions. However, we *can* get C code built by SwiftPM to see them. For this reason we
// elect to provide a selection of shims to enable Swift code to use recv_mmsg and send_mmsg.
// Mostly this is fine, but to minimise the overhead we want the Swift code to be able to
// create the msgvec directly without requiring further memory fussiness in our C shim.
// That requires us to also construct a C structure that has the same layout as struct mmsghdr.
//
// Conveniently glibc has pretty strict ABI stability rules, and this structure is part of the
// glibc ABI, so we can just reproduce the structure definition here and feel confident that it
// will be sufficient.
//
// If SR-6772 ever gets resolved we can remove this shim.
//
// https://bugs.swift.org/browse/SR-6772
typedef struct {
struct msghdr msg_hdr;
unsigned int msg_len;
} CNIOOpenBSD_mmsghdr;
typedef struct {
struct in6_addr ipi6_addr;
unsigned int ipi6_ifindex;
} CNIOOpenBSD_in6_pktinfo;
int CNIOOpenBSD_sendmmsg(int sockfd, CNIOOpenBSD_mmsghdr *msgvec, unsigned int vlen, int flags);
int CNIOOpenBSD_recvmmsg(int sockfd, CNIOOpenBSD_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout);
int CNIOOpenBSD_pthread_set_name_np(pthread_t thread, const char *name);
int CNIOOpenBSD_pthread_get_name_np(pthread_t thread, char *name, size_t len);
// Non-standard socket stuff.
int CNIOOpenBSD_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
// cmsghdr handling
struct cmsghdr *CNIOOpenBSD_CMSG_FIRSTHDR(const struct msghdr *);
struct cmsghdr *CNIOOpenBSD_CMSG_NXTHDR(struct msghdr *, struct cmsghdr *);
const void *CNIOOpenBSD_CMSG_DATA(const struct cmsghdr *);
void *CNIOOpenBSD_CMSG_DATA_MUTABLE(struct cmsghdr *);
size_t CNIOOpenBSD_CMSG_LEN(size_t);
size_t CNIOOpenBSD_CMSG_SPACE(size_t);
// awkward time_T pain
extern const int CNIOOpenBSD_SO_TIMESTAMP;
extern const int CNIOOpenBSD_SO_RCVTIMEO;
int CNIOOpenBSD_system_info(struct utsname *uname_data);
extern const unsigned long CNIOOpenBSD_IOCTL_VM_SOCKETS_GET_LOCAL_CID;
const char* CNIOOpenBSD_dirent_dname(struct dirent *ent);
extern const unsigned long CNIOOpenBSD_UTIME_OMIT;
extern const unsigned long CNIOOpenBSD_UTIME_NOW;
extern const long CNIOOpenBSD_UDP_MAX_SEGMENTS;
// A workaround for incorrect nullability annotations in the Android SDK.
// Probably unnecessary on BSD, but copying for consistency for now.
FTS *CNIOOpenBSD_fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
#endif
#endif
+131
View File
@@ -0,0 +1,131 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
// Xcode's Archive builds with Xcode's Package support struggle with empty .c files
// (https://bugs.swift.org/browse/SR-12939).
void CNIOOpenBSD_i_do_nothing_just_working_around_a_darwin_toolchain_bug(void) {}
#if defined(__OpenBSD__)
#include <CNIOOpenBSD.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
int CNIOOpenBSD_pthread_set_name_np(pthread_t thread, const char *name) {
pthread_set_name_np(thread, name);
return 0;
}
int CNIOOpenBSD_pthread_get_name_np(pthread_t thread, char *name, size_t len) {
pthread_get_name_np(thread, name, len);
return 0;
}
int CNIOOpenBSD_sendmmsg(int sockfd, CNIOOpenBSD_mmsghdr *msgvec, unsigned int vlen, int flags) {
// This is technically undefined behaviour, but it's basically fine because these types are the same size, and we
// don't think the compiler is inclined to blow anything up here.
// This comment is from CNIOLinux, but I haven't reverified this applies for OpenBSD.
return sendmmsg(sockfd, (struct mmsghdr *)msgvec, vlen, flags);
}
int CNIOOpenBSD_recvmmsg(int sockfd, CNIOOpenBSD_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout) {
// This is technically undefined behaviour, but it's basically fine because these types are the same size, and we
// don't think the compiler is inclined to blow anything up here.
// This comment is from CNIOLinux, but I haven't reverified this applies for OpenBSD.
return recvmmsg(sockfd, (struct mmsghdr *)msgvec, vlen, flags, timeout);
}
int CNIOOpenBSD_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
return accept4(sockfd, addr, addrlen, flags);
}
struct cmsghdr *CNIOOpenBSD_CMSG_FIRSTHDR(const struct msghdr *mhdr) {
assert(mhdr != NULL);
return CMSG_FIRSTHDR(mhdr);
}
struct cmsghdr *CNIOOpenBSD_CMSG_NXTHDR(struct msghdr *mhdr, struct cmsghdr *cmsg) {
assert(mhdr != NULL);
assert(cmsg != NULL);
return CMSG_NXTHDR(mhdr, cmsg);
}
const void *CNIOOpenBSD_CMSG_DATA(const struct cmsghdr *cmsg) {
assert(cmsg != NULL);
return CMSG_DATA(cmsg);
}
void *CNIOOpenBSD_CMSG_DATA_MUTABLE(struct cmsghdr *cmsg) {
assert(cmsg != NULL);
return CMSG_DATA(cmsg);
}
size_t CNIOOpenBSD_CMSG_LEN(size_t payloadSizeBytes) {
return CMSG_LEN(payloadSizeBytes);
}
size_t CNIOOpenBSD_CMSG_SPACE(size_t payloadSizeBytes) {
return CMSG_SPACE(payloadSizeBytes);
}
const int CNIOOpenBSD_SO_TIMESTAMP = SO_TIMESTAMP;
const int CNIOOpenBSD_SO_RCVTIMEO = SO_RCVTIMEO;
bool supports_udp_sockopt(int opt, int value) {
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
return false;
}
int rc = setsockopt(fd, IPPROTO_UDP, opt, &value, sizeof(value));
close(fd);
return rc == 0;
}
bool CNIOOpenBSD_supports_udp_segment() {
#ifndef UDP_SEGMENT
return false;
#else
return supports_udp_sockopt(UDP_SEGMENT, 512);
#endif
}
bool CNIOOpenBSD_supports_udp_gro() {
#ifndef UDP_GRO
return false;
#else
return supports_udp_sockopt(UDP_GRO, 1);
#endif
}
int CNIOOpenBSD_system_info(struct utsname* uname_data) {
return uname(uname_data);
}
const char* CNIOOpenBSD_dirent_dname(struct dirent* ent) {
return ent->d_name;
}
const unsigned long CNIOOpenBSD_UTIME_OMIT = UTIME_OMIT;
const unsigned long CNIOOpenBSD_UTIME_NOW = UTIME_NOW;
#ifdef UDP_MAX_SEGMENTS
const long CNIOOpenBSD_UDP_MAX_SEGMENTS = UDP_MAX_SEGMENTS;
#endif
const long CNIOOpenBSD_UDP_MAX_SEGMENTS = -1;
FTS *CNIOOpenBSD_fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)) {
return fts_open(path_argv, options, compar);
}
#endif
+1 -1
View File
@@ -54,7 +54,7 @@
#endif
#ifdef __ANDROID__
#include <sys/endian.h>
#elif defined(__linux__) || defined(__APPLE__) || defined(__wasm32__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__wasm32__) || defined(__OpenBSD__)
#include <sys/types.h>
#elif defined(_WIN32) || defined(_WIN64)
#ifndef LITTLE_ENDIAN
@@ -213,6 +213,7 @@ public final class NIOAtomic<T: NIOAtomicPrimitive> {
/// Create an atomic object with `value`
@inlinable
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
public static func makeAtomic(value: T) -> NIOAtomic {
let manager = Manager(bufferClass: self, minimumCapacity: 1) { _, _ in }
manager.withUnsafeMutablePointerToElements {
@@ -35,6 +35,9 @@ import wasi_pthread
#if os(Windows)
@usableFromInline
typealias LockPrimitive = SRWLOCK
#elseif os(OpenBSD)
@usableFromInline
typealias LockPrimitive = pthread_mutex_t?
#else
@usableFromInline
typealias LockPrimitive = pthread_mutex_t
@@ -50,6 +53,11 @@ extension LockOperations {
#if os(Windows)
InitializeSRWLock(mutex)
#elseif os(OpenBSD)
var attr = pthread_mutexattr_t(bitPattern: 0)
pthread_mutexattr_init(&attr)
let err = pthread_mutex_init(mutex, &attr)
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
var attr = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
@@ -463,6 +463,7 @@ extension UInt: AtomicPrimitive {
deprecated,
message: "AtomicBox is deprecated without replacement because the original implementation doesn't work."
)
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
public final class AtomicBox<T: AnyObject> {
private let storage: NIOAtomic<UInt>
@@ -608,4 +609,5 @@ public final class AtomicBox<T: AnyObject> {
}
@available(*, deprecated)
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
extension AtomicBox: @unchecked Sendable where T: Sendable {}
+10
View File
@@ -43,6 +43,9 @@ public final class Lock {
#if os(Windows)
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t?> =
UnsafeMutablePointer.allocate(capacity: 1)
#else
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t> =
UnsafeMutablePointer.allocate(capacity: 1)
@@ -52,6 +55,10 @@ public final class Lock {
public init() {
#if os(Windows)
InitializeSRWLock(self.mutex)
#elseif os(OpenBSD)
var attr = pthread_mutexattr_t(bitPattern: 0)
let err = pthread_mutex_init(self.mutex, &attr)
precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)")
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
var attr = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
@@ -134,6 +141,9 @@ public final class ConditionLock<T: Equatable> {
#if os(Windows)
private let cond: UnsafeMutablePointer<CONDITION_VARIABLE> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
private let cond: UnsafeMutablePointer<pthread_cond_t?> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
private let cond: UnsafeMutablePointer<pthread_cond_t> =
UnsafeMutablePointer.allocate(capacity: 1)
+8
View File
@@ -83,6 +83,14 @@ private let sysInet_ntop:
inet_ntop
private let sysInet_pton: @convention(c) (CInt, UnsafePointer<CChar>?, UnsafeMutableRawPointer?) -> CInt = inet_pton
#endif
#elseif os(OpenBSD)
@preconcurrency import Glibc
import CNIOOpenBSD
private let sysInet_ntop:
@convention(c) (CInt, UnsafeRawPointer?, UnsafeMutablePointer<CChar>?, socklen_t) -> UnsafePointer<CChar>? =
inet_ntop
private let sysInet_pton: @convention(c) (CInt, UnsafePointer<CChar>?, UnsafeMutableRawPointer?) -> CInt = inet_pton
#elseif canImport(Darwin)
import Darwin
+4 -2
View File
@@ -20,6 +20,8 @@
@preconcurrency import Bionic
#endif
import CNIOLinux
#elseif os(OpenBSD)
import CNIOOpenBSD
#elseif canImport(Darwin)
import Darwin
#elseif canImport(WASILibc)
@@ -51,7 +53,7 @@ extension ifaddrs {
fileprivate var dstaddr: UnsafeMutablePointer<sockaddr>? {
#if os(Linux) || os(Android)
return self.ifa_ifu.ifu_dstaddr
#elseif canImport(Darwin)
#elseif canImport(Darwin) || os(OpenBSD)
return self.ifa_dstaddr
#endif
}
@@ -59,7 +61,7 @@ extension ifaddrs {
fileprivate var broadaddr: UnsafeMutablePointer<sockaddr>? {
#if os(Linux) || os(Android)
return self.ifa_ifu.ifu_broadaddr
#elseif canImport(Darwin)
#elseif canImport(Darwin) || os(OpenBSD)
return self.ifa_dstaddr
#endif
}
+3
View File
@@ -52,6 +52,9 @@ import Darwin
@preconcurrency import Android
#endif
import CNIOLinux
#elseif os(OpenBSD)
@preconcurrency import Glibc
import CNIOOpenBSD
#elseif canImport(WASILibc)
@preconcurrency import WASILibc
#else
@@ -22,6 +22,9 @@ import Darwin
@preconcurrency import Bionic
#endif
import CNIOLinux
#elseif os(OpenBSD)
@preconcurrency import Glibc
import CNIOOpenBSD
#elseif os(Windows)
import WinSDK
#elseif canImport(WASILibc)
+3
View File
@@ -20,6 +20,9 @@ import CNIOLinux
#elseif canImport(Android)
@preconcurrency import Android
#endif
#elseif os(OpenBSD)
import CNIOOpenBSD
@preconcurrency import Glibc
#elseif os(Windows)
import ucrt
import let WinSDK.RelationProcessorCore
+1
View File
@@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
import CNIOLinux
import CNIOOpenBSD
import NIOCore
import NIOPosix
@@ -97,6 +97,7 @@ extension NIOBSDSocket.SocketType {
}
// IPv4 Options
#if !os(OpenBSD)
extension NIOBSDSocket.Option {
/// Request that we are passed type of service details when receiving
/// datagrams.
@@ -116,6 +117,7 @@ extension NIOBSDSocket.Option {
static let ip_recv_pktinfo: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: Posix.IP_RECVPKTINFO)
}
#endif
// IPv6 Options
extension NIOBSDSocket.Option {
@@ -128,6 +130,7 @@ extension NIOBSDSocket.Option {
static let ipv6_recv_tclass: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: IPV6_RECVTCLASS)
#if !os(OpenBSD)
/// Request that we are passed destination address and the receiving interface index when
/// receiving datagrams.
///
@@ -136,6 +139,7 @@ extension NIOBSDSocket.Option {
/// IPv4 and IPv6.
static let ipv6_recv_pktinfo: NIOBSDSocket.Option =
NIOBSDSocket.Option(rawValue: Posix.IPV6_RECVPKTINFO)
#endif
}
extension NIOBSDSocket {
+9 -1
View File
@@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//
import NIOCore
#if os(Linux) || os(Android) || os(FreeBSD) || canImport(Darwin)
#if os(Linux) || os(Android) || os(FreeBSD) || canImport(Darwin) || os(OpenBSD)
extension Shutdown {
internal var cValue: CInt {
@@ -276,6 +276,14 @@ private let CMSG_DATA = CNIODarwin_CMSG_DATA
private let CMSG_DATA_MUTABLE = CNIODarwin_CMSG_DATA_MUTABLE
private let CMSG_SPACE = CNIODarwin_CMSG_SPACE
private let CMSG_LEN = CNIODarwin_CMSG_LEN
#elseif os(OpenBSD)
import CNIOOpenBSD
private let CMSG_FIRSTHDR = CNIOOpenBSD_CMSG_FIRSTHDR
private let CMSG_NXTHDR = CNIOOpenBSD_CMSG_NXTHDR
private let CMSG_DATA = CNIOOpenBSD_CMSG_DATA
private let CMSG_DATA_MUTABLE = CNIOOpenBSD_CMSG_DATA_MUTABLE
private let CMSG_SPACE = CNIOOpenBSD_CMSG_SPACE
private let CMSG_LEN = CNIOOpenBSD_CMSG_LEN
#else
import CNIOLinux
private let CMSG_FIRSTHDR = CNIOLinux_CMSG_FIRSTHDR
+1
View File
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
import CNIOLinux
import CNIOOpenBSD
import NIOConcurrencyHelpers
import NIOCore
+1
View File
@@ -12,6 +12,7 @@
//
//===----------------------------------------------------------------------===//
import CNIOLinux
import CNIOOpenBSD
import NIOCore
#if os(Windows)
+5 -1
View File
@@ -17,6 +17,9 @@ import NIOCore
import CNIODarwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
import CNIOLinux
#elseif os(OpenBSD)
import CNIOOpenBSD
import Glibc
#elseif os(Windows)
import CNIOWindows
#endif
@@ -237,6 +240,7 @@ struct ControlMessageParser {
self.ecnValue = .init(receivedValue: readValue)
}
} else if controlMessage.type == Posix.IP_PKTINFO {
#if !os(OpenBSD)
if let data = controlMessage.data {
let info = data.load(as: in_pktinfo.self)
var addr = sockaddr_in()
@@ -248,7 +252,7 @@ struct ControlMessageParser {
interfaceIndex: Int(info.ipi_ifindex)
)
}
#endif
}
}
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
import CNIODarwin
import CNIOLinux
import CNIOOpenBSD
import NIOCore
#if canImport(WinSDK)
+13 -2
View File
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
import CNIOLinux
import CNIOOpenBSD
import CNIOWindows
import NIOConcurrencyHelpers
import NIOCore
@@ -829,8 +830,13 @@ public struct NonBlockingFileIO: Sendable {
let ptr = pointer.baseAddress!.assumingMemoryBound(to: CChar.self)
return String(cString: ptr)
}
#if os(OpenBSD)
let ino = entry.pointee.d_fileno
#else
let ino = entry.pointee.d_ino
#endif
entries.append(
NIODirectoryEntry(ino: UInt64(entry.pointee.d_ino), type: entry.pointee.d_type, name: name)
NIODirectoryEntry(ino: UInt64(ino), type: entry.pointee.d_type, name: name)
)
}
try? Posix.closedir(dir: dir)
@@ -1278,8 +1284,13 @@ extension NonBlockingFileIO {
let ptr = pointer.baseAddress!.assumingMemoryBound(to: CChar.self)
return String(cString: ptr)
}
#if os(OpenBSD)
let ino = entry.pointee.d_fileno
#else
let ino = entry.pointee.d_ino
#endif
entries.append(
NIODirectoryEntry(ino: UInt64(entry.pointee.d_ino), type: entry.pointee.d_type, name: name)
NIODirectoryEntry(ino: UInt64(ino), type: entry.pointee.d_type, name: name)
)
}
try? Posix.closedir(dir: dir)
@@ -15,6 +15,7 @@
import Atomics
import CNIODarwin
import CNIOLinux
import CNIOOpenBSD
import CNIOWindows
import NIOCore
@@ -14,6 +14,7 @@
import Atomics
import CNIOLinux
import CNIOOpenBSD
import NIOCore
private struct PendingStreamWrite {
+1 -1
View File
@@ -185,7 +185,7 @@ internal class Selector<R: Registration> {
var selectorFD: CInt = -1 // -1 == we're closed
// Here we add the stored properties that are used by the specific backends
#if canImport(Darwin)
#if canImport(Darwin) || os(OpenBSD)
@usableFromInline
typealias EventType = kevent
#elseif os(Linux) || os(Android)
+12 -1
View File
@@ -15,7 +15,7 @@
import NIOConcurrencyHelpers
import NIOCore
#if canImport(Darwin)
#if canImport(Darwin) || os(OpenBSD)
/// Represents the `kqueue` filters we might use:
///
@@ -131,7 +131,13 @@ extension Selector: _SelectorBackendProtocol {
// Clamp the timespec tv_sec value to the maximum supported by the Darwin-kernel.
// Whilst this schedules the event far into the future (several years) it will still be triggered provided
// the system stays up.
#if os(OpenBSD)
// Should double-check if this makes sense for OpenBSD, but
// let's just get this to compile for now.
ts.tv_sec = min(ts.tv_sec, time_t(sysIntervalTimerMaxSec))
#else
ts.tv_sec = min(ts.tv_sec, sysIntervalTimerMaxSec)
#endif
return ts
}
@@ -388,6 +394,7 @@ extension kevent {
// we do it with NOTE_LOWAT set to Int.max, which will ensure that there is never enough data
// in the send buffer to trigger EVFILT_EXCEPT. Thanks to the sensible design of kqueue,
// this only affects our EXCEPT filter: EVFILT_READ behaves separately.
#if canImport(Darwin)
if filter == EVFILT_EXCEPT {
self.fflags = CUnsignedInt(NOTE_LOWAT)
self.data = Int.max
@@ -395,6 +402,10 @@ extension kevent {
self.fflags = 0
self.data = 0
}
#else
self.fflags = 0
self.data = 0
#endif
}
}
+1
View File
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
import CNIOLinux
import CNIOOpenBSD
import NIOCore
#if canImport(WinSDK)
+23 -3
View File
@@ -28,6 +28,10 @@ import struct WinSDK.ipv6_mreq
import struct WinSDK.socklen_t
#endif
#if os(OpenBSD)
import CNIOOpenBSD
#endif
extension ByteBuffer {
mutating func withMutableWritePointer(
body: (UnsafeMutableRawBufferPointer) throws -> IOResult<Int>
@@ -121,7 +125,7 @@ final class SocketChannel: BaseStreamSocketChannel<Socket>, @unchecked Sendable
case _ as ChannelOptions.Types.ConnectTimeoutOption:
return connectTimeout as! Option.Value
case _ as ChannelOptions.Types.LocalVsockContextID:
#if os(Windows)
#if os(Windows) || os(OpenBSD)
fallthrough
#else
return try self.socket.getLocalVsockContextID() as! Option.Value
@@ -280,7 +284,7 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket>, @unchecked Sen
case _ as ChannelOptions.Types.BacklogOption:
return backlog as! Option.Value
case _ as ChannelOptions.Types.LocalVsockContextID:
#if os(Windows)
#if os(Windows) || os(OpenBSD)
fallthrough
#else
return try self.socket.getLocalVsockContextID() as! Option.Value
@@ -319,7 +323,7 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket>, @unchecked Sen
switch target {
case .socketAddress(let address):
try socket.bind(to: address)
#if os(Windows)
#if os(Windows) || os(OpenBSD)
case .vsockAddress:
fatalError(vsockUnimplemented)
#else
@@ -576,12 +580,16 @@ final class DatagramChannel: BaseSocketChannel<Socket>, @unchecked Sendable {
let valueAsInt: CInt = value as! Bool ? 1 : 0
switch self.localAddress?.protocol {
case .some(.inet):
#if os(OpenBSD)
throw ChannelError._operationUnsupported
#else
self.reportExplicitCongestionNotifications = true
try self.socket.setOption(
level: .ip,
name: .ip_recv_tos,
value: valueAsInt
)
#endif
case .some(.inet6):
self.reportExplicitCongestionNotifications = true
try self.socket.setOption(
@@ -595,6 +603,9 @@ final class DatagramChannel: BaseSocketChannel<Socket>, @unchecked Sendable {
}
case _ as ChannelOptions.Types.ReceivePacketInfo:
let valueAsInt: CInt = value as! Bool ? 1 : 0
#if os(OpenBSD)
throw ChannelError._operationUnsupported
#else
switch self.localAddress?.protocol {
case .some(.inet):
self.receivePacketInfo = true
@@ -614,6 +625,7 @@ final class DatagramChannel: BaseSocketChannel<Socket>, @unchecked Sendable {
// Receiving packet info is only supported for IP
throw ChannelError._operationUnsupported
}
#endif
case _ as ChannelOptions.Types.DatagramSegmentSize:
guard System.supportsUDPSegmentationOffload else {
throw ChannelError._operationUnsupported
@@ -654,11 +666,15 @@ final class DatagramChannel: BaseSocketChannel<Socket>, @unchecked Sendable {
case _ as ChannelOptions.Types.ExplicitCongestionNotificationsOption:
switch self.localAddress?.protocol {
case .some(.inet):
#if os(OpenBSD)
throw ChannelError._operationUnsupported
#else
return try
(self.socket.getOption(
level: .ip,
name: .ip_recv_tos
) != 0) as! Option.Value
#endif
case .some(.inet6):
return try
(self.socket.getOption(
@@ -670,6 +686,9 @@ final class DatagramChannel: BaseSocketChannel<Socket>, @unchecked Sendable {
throw ChannelError._operationUnsupported
}
case _ as ChannelOptions.Types.ReceivePacketInfo:
#if os(OpenBSD)
throw ChannelError._operationUnsupported
#else
switch self.localAddress?.protocol {
case .some(.inet):
return try
@@ -687,6 +706,7 @@ final class DatagramChannel: BaseSocketChannel<Socket>, @unchecked Sendable {
// Receiving packet info is only supported for IP
throw ChannelError._operationUnsupported
}
#endif
case _ as ChannelOptions.Types.DatagramSegmentSize:
guard System.supportsUDPSegmentationOffload else {
throw ChannelError._operationUnsupported
+2 -2
View File
@@ -77,7 +77,7 @@ protocol SocketProtocol: BaseSocketProtocol {
func ignoreSIGPIPE() throws
}
#if os(Linux) || os(Android)
#if os(Linux) || os(Android) || os(OpenBSD)
// This is a lazily initialised global variable that when read for the first time, will ignore SIGPIPE.
private let globallyIgnoredSIGPIPE: Bool = {
// no F_SETNOSIGPIPE on Linux :(
@@ -97,7 +97,7 @@ private let globallyIgnoredSIGPIPE: Bool = {
extension BaseSocketProtocol {
// used by `BaseSocket` and `PipePair`.
internal static func ignoreSIGPIPE(descriptor fd: CInt) throws {
#if os(Linux) || os(Android)
#if os(Linux) || os(Android) || os(OpenBSD)
let haveWeIgnoredSIGPIEThisIsHereToTriggerIgnoringIt = globallyIgnoredSIGPIPE
guard haveWeIgnoredSIGPIEThisIsHereToTriggerIgnoringIt else {
fatalError("BUG in NIO. We did not ignore SIGPIPE, this code path should definitely not be reachable.")
+40 -6
View File
@@ -32,6 +32,11 @@ internal typealias MMsgHdr = CNIODarwin_mmsghdr
import CNIOLinux
internal typealias MMsgHdr = CNIOLinux_mmsghdr
internal typealias in6_pktinfo = CNIOLinux_in6_pktinfo
#elseif os(OpenBSD)
@_exported @preconcurrency import Glibc
import CNIOOpenBSD
internal typealias MMsgHdr = CNIOOpenBSD_mmsghdr
let INADDR_ANY = UInt32(0)
#elseif os(Windows)
@_exported import ucrt
@@ -151,7 +156,7 @@ private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointe
private let sysSocketpair: @convention(c) (CInt, CInt, CInt, UnsafeMutablePointer<CInt>?) -> CInt = socketpair
#endif
#if os(Linux) || os(Android) || canImport(Darwin)
#if os(Linux) || os(Android) || canImport(Darwin) || os(OpenBSD)
private let sysFstat = fstat
private let sysStat = stat
private let sysLstat = lstat
@@ -168,6 +173,10 @@ private let sysRemove = remove
#if os(Linux) || os(Android)
private let sysSendMmsg = CNIOLinux_sendmmsg
private let sysRecvMmsg = CNIOLinux_recvmmsg
#elseif os(OpenBSD)
private let sysKevent = kevent
private let sysSendMmsg = CNIOOpenBSD_sendmmsg
private let sysRecvMmsg = CNIOOpenBSD_recvmmsg
#elseif canImport(Darwin)
private let sysKevent = kevent
private let sysMkpath = mkpath_np
@@ -347,7 +356,7 @@ internal func syscall<T>(
}
}
}
#elseif os(Linux) || os(Android)
#elseif os(Linux) || os(Android) || os(OpenBSD)
@inline(__always)
@inlinable
@discardableResult
@@ -450,7 +459,7 @@ internal enum Posix: Sendable {
static let SHUT_WR: CInt = CInt(Darwin.SHUT_WR)
@usableFromInline
static let SHUT_RDWR: CInt = CInt(Darwin.SHUT_RDWR)
#elseif os(Linux) || os(FreeBSD) || os(Android)
#elseif os(Linux) || os(FreeBSD) || os(Android) || os(OpenBSD)
#if canImport(Glibc)
@usableFromInline
static let UIO_MAXIOV: Int = Int(Glibc.UIO_MAXIOV)
@@ -514,6 +523,12 @@ internal enum Posix: Sendable {
static let IPTOS_ECN_ECT0: CInt = CInt(CNIOLinux.IPTOS_ECN_ECT0)
static let IPTOS_ECN_ECT1: CInt = CInt(CNIOLinux.IPTOS_ECN_ECT1)
static let IPTOS_ECN_CE: CInt = CInt(CNIOLinux.IPTOS_ECN_CE)
#elseif os(OpenBSD)
static let IPTOS_ECN_NOTECT: CInt = CInt(CNIOOpenBSD.IPTOS_ECN_NOTECT)
static let IPTOS_ECN_MASK: CInt = CInt(CNIOOpenBSD.IPTOS_ECN_MASK)
static let IPTOS_ECN_ECT0: CInt = CInt(CNIOOpenBSD.IPTOS_ECN_ECT0)
static let IPTOS_ECN_ECT1: CInt = CInt(CNIOOpenBSD.IPTOS_ECN_ECT1)
static let IPTOS_ECN_CE: CInt = CInt(CNIOOpenBSD.IPTOS_ECN_CE)
#elseif os(Windows)
static let IPTOS_ECN_NOTECT: CInt = CInt(0x00)
static let IPTOS_ECN_MASK: CInt = CInt(0x03)
@@ -534,6 +549,11 @@ internal enum Posix: Sendable {
static let IPV6_RECVPKTINFO: CInt = CInt(CNIOLinux.IPV6_RECVPKTINFO)
static let IPV6_PKTINFO: CInt = CInt(CNIOLinux.IPV6_PKTINFO)
#elseif os(OpenBSD)
static let IP_PKTINFO: CInt = CInt(-1) // Not actually present.
static let IPV6_RECVPKTINFO: CInt = CInt(CNIOOpenBSD.IPV6_RECVPKTINFO)
static let IPV6_PKTINFO: CInt = CInt(CNIOOpenBSD.IPV6_PKTINFO)
#elseif os(Windows)
static let IP_RECVPKTINFO: CInt = CInt(WinSDK.IP_PKTINFO)
static let IP_PKTINFO: CInt = CInt(WinSDK.IP_PKTINFO)
@@ -544,7 +564,7 @@ internal enum Posix: Sendable {
#if canImport(Darwin)
static let SOL_UDP: CInt = CInt(IPPROTO_UDP)
#elseif os(Linux) || os(FreeBSD) || os(Android)
#elseif os(Linux) || os(FreeBSD) || os(Android) || os(OpenBSD)
static let SOL_UDP: CInt = CInt(IPPROTO_UDP)
#elseif os(Windows)
static let SOL_UDP: CInt = CInt(IPPROTO_UDP)
@@ -978,7 +998,7 @@ internal enum Posix: Sendable {
sysClosedir(dir)
}
}
#elseif os(Linux) || os(FreeBSD) || os(Android)
#elseif os(Linux) || os(FreeBSD) || os(Android) || os(OpenBSD)
@inline(never)
public static func opendir(pathname: String) throws -> OpaquePointer {
try syscall {
@@ -1070,7 +1090,15 @@ extension Posix {
}
#endif
#if canImport(Darwin) || os(OpenBSD)
#if canImport(Darwin)
internal typealias kevent_timespec = Darwin.timespec
#elseif os(OpenBSD)
internal typealias kevent_timespec = CNIOOpenBSD.timespec
#else
#error("implementation missing")
#endif
@usableFromInline
internal enum KQueue: Sendable {
@@ -1079,7 +1107,13 @@ internal enum KQueue: Sendable {
@inline(never)
public static func kqueue() throws -> CInt {
try syscall(blocking: false) {
#if canImport(Darwin)
Darwin.kqueue()
#elseif os(OpenBSD)
CNIOOpenBSD.kqueue()
#else
#error("implementation missing")
#endif
}.result
}
@@ -1091,7 +1125,7 @@ internal enum KQueue: Sendable {
nchanges: CInt,
eventlist: UnsafeMutablePointer<kevent>?,
nevents: CInt,
timeout: UnsafePointer<Darwin.timespec>?
timeout: UnsafePointer<kevent_timespec>?
) throws -> CInt {
try syscall(blocking: false) {
sysKevent(kq, changelist, nchanges, eventlist, nevents, timeout)
+2
View File
@@ -16,6 +16,8 @@ import NIOConcurrencyHelpers
#if os(Linux) || os(FreeBSD) || os(Android)
import CNIOLinux
#elseif os(OpenBSD)
import CNIOOpenBSD
#elseif os(Windows)
import WinSDK
#endif
+13 -1
View File
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
#if os(Linux) || os(Android) || os(FreeBSD) || canImport(Darwin)
#if os(Linux) || os(Android) || os(FreeBSD) || canImport(Darwin) || os(OpenBSD)
#if os(Linux) || os(Android)
import CNIOLinux
@@ -24,6 +24,12 @@ private typealias ThreadDestructor = @convention(c) (UnsafeMutableRawPointer) ->
#else
private typealias ThreadDestructor = @convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer?
#endif
#elseif os(OpenBSD)
import CNIOOpenBSD
private let sys_pthread_getname_np = CNIOOpenBSD_pthread_get_name_np
private let sys_pthread_setname_np = CNIOOpenBSD_pthread_set_name_np
private typealias ThreadDestructor = @convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer?
#elseif canImport(Darwin)
private let sys_pthread_getname_np = pthread_getname_np
// Emulate the same method signature as pthread_setname_np on Linux.
@@ -49,6 +55,12 @@ private func sysPthread_create(
let thread = pthread_create(handle, &attr, destructor, args)
pthread_attr_destroy(&attr)
return thread
#elseif os(OpenBSD)
var attr: pthread_attr_t? = .init(bitPattern: 0)
pthread_attr_init(&attr)
let thread = pthread_create(handle, &attr, destructor, args)
pthread_attr_destroy(&attr)
return thread
#else
#if canImport(Musl)
var handleLinux: OpaquePointer? = nil