Commit Graph
37 Commits
Author SHA1 Message Date
scottmarchant 5bf0267df4 build: Elide NIOPosix for WASI platforms only (#3485)
### Motivation:

Packages that consume NIOPosix should be able to compile to WASI
platforms without special configurations. This change elides the
NIOPosix source from WASI platforms to simplify configuration.

Without this change:

```swift
dependencies: [
    // Without this PR, downstream packages must maintain exhaustive platform list to exclude `.wasi`:
    .product(
        name: "NIOPosix",
        package: "swift-nio",
        condition: .when(platforms: [
            .macOS,
            .macCatalyst,
            .iOS,
            .tvOS,
            .watchOS,
            .visionOS,
            .driverKit,
            .linux,
            .windows,
            .android,
            .openbsd,
            // .wasi // <-- Need to exclude this, because there is no exclusion list api for SPM conditionals
        ])
    ),
]
```

With this change:

```swift
dependencies: [
    // Without this PR, downstream packages can consume NIOPosix simply:
    .product(name: "NIOPosix", package: "swift-nio"),
]
```

### Modifications:

- Add compiler directives (`#if !os(WASI)`) to all source files in
NIOPosix

### Result:

Downstream packages can compile to wasm without manually excluding the
NIOPosix dependency.

### Testing performed

- Verified `swift build --swift-sdk wasm32-unknown-wasip1 --target
NIOPosix` compiles, which demonstrates proper elision of source files in
NIOPosix that aren't wasm-ready.
- Confirmed GitHub [checks
pass](https://github.com/PassiveLogic/swift-nio/actions/runs/21151341738).
2026-01-23 16:39:32 +00:00
3405691582andRick Newton-Rogers 2bc71a6aff 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>
2025-11-26 09:07:02 +00:00
Cory Benfield a7da98c3ac Enable per-message GSO on Linux (#3436)
Motivation:

NIO has existing GSO support. This GSO support is applied at Channel
scope, which makes it somewhat less than ideal, as it forces the
datagram sizes to be known statically ahead of time. In real
applications this is less common than being able to do this on a
per-superbuffer
context.

Linux supports this already by using sendmsg/sendmmsg control data. We
just need to wire this up in NIO.

An extension to this is that we can also do per-message GRO in Linux.
I'll tackle this in a later patch to keep the diffs small.

Modifications:

- Adding a new field on AddressedEnvelope Metadata.
- Reject setting this field on non-Linux devices.
- On Linux, use this to set the control data.
- Added new tests that validate the behaviour is correct.

Result:

Enable per-message GSO.
2025-11-07 08:34:33 +00:00
Fabian FettandCory Benfield a3ed8e10ab Drop Swift 5.10 (#3393)
This PR drops Swift 5.10 and enables Swift 6 language mode.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-10-06 10:58:33 +01:00
Fabian Fett e95d742160 [Windows] Reimplement pwrite (#3364)
Very much like #3361. Just for writing.

Result: NIOPosix compiles on Windows.
2025-09-01 16:12:41 +02:00
Fabian Fett 5bf5054a83 [Windows] Reimplement pread (#3361)
- pread does not exist in ucrt. We need to reimplement it using read and
lseek.
- `FileDescriptor.setNonBlocking` is currently not needed in Windows to
compile. Let's not compile it.
- Set up correct sys calls for lseek and ftruncate
2025-08-29 13:39:04 +01:00
Fabian Fett 9cde8e3a00 [Windows] dup is deprecated on Windows, use _dup instead (#3352) 2025-08-19 13:21:59 +00:00
Fabian Fett a8b744cb61 [Windows] Make Thread.swift compile (#3341)
See title.
2025-08-12 16:11:18 +01:00
Johannes Weiss 40ee44c6b9 always @preconcurrency import Glibc/Musl/Android/Bionic/WASILibc (#3153)
### Motivation:

The non-`Darwin` libcs don't have the correct concurrency annotations.
But due to these Swift bugs, it's important that the _first_ importer
uses `@preconcurrency`:

- https://github.com/swiftlang/swift/issues/79414
- https://github.com/swiftlang/swift/issues/77866

### Modifications:

Much like the Foundation (& corelibs) PRs such as
https://github.com/swiftlang/swift-foundation/pull/1175 , use
`@preconcurrency import` for the non-`Darwin` libcs.

### Result:

Fewer bad warnings/errors in user code.
2025-03-21 15:35:05 +00:00
Cory Benfield c2ccad63e2 Add Sendable annotations to the syscall wrappers (#3087)
Motivation

Unfortunately there are a few places where we have to help the compiler
out to realise that our copies of the libc functions are also Sendable,
and -require-explicit-sendable can cause us some trouble if we don't
annotate our usableFromInline types.

Modifications

Annotate our usableFromInline types
Add some @Sendable

Results

The march continues
2025-01-24 15:10:20 +00:00
finagolfin 33b8a4801f Use the new Android overlay in the tests and update some Bionic declarations (#3009)
### Motivation:

Get this repo building again for Android with NDK 27

### Modifications:

- Update some networking declarations for newly added nullability
annotations
- Import the new Android overlay instead in some tests
- Add two force-unwraps on all platforms, that are needed for Android

### Result:

This repo and its tests build for Android again

I've been [using these patches on my Android
CI](https://github.com/finagolfin/swift-android-sdk/blob/main/swift-nio-ndk27.patch)
and natively on Android for a couple months now. I didn't bother keeping
this patch building for Android with Swift 5 anymore, as my Android CI
no longer tests Swift 5.

I built this pull and ran the tests on linux x86_64 to make sure there
was no regression.
2024-12-01 17:57:08 +00:00
Johannes Weiss 70dfce82b0 give common blocking functions a clear name (#2984)
### Motivation:

In many situations, for example continuous profiling with sampling
profilers, it's important to distinguish between on- and off-CPU work.
That's most easily done if there are clear function names/prefixes to
grep for that are common places to just wait (off-CPU) and do no real
work. In SwiftNIO those are chiefly three places:
1. The `EventLoops` waiting for work
2. `The NIOThreadPool` threads waiting for work
3. `EventLoopFuture.wait()`

This patch makes sure that each of those will have a function with
prefix `_blockingWaitFor` in the function name which is easily
greppable.

### Modifications:

- Create some non-inlinable functions with `_blockingWaitFor...` that
just wait for `...`.

### Result:

SwiftNIO makes operational excellence easier and your SRE team more
happy.
2024-11-23 22:47:34 +00:00
finagolfin 4feff3c2f6 Use the new Android overlay and Bionic module from Swift 6 (#2784)
Motivation:

Get this repo building again for Android with the new overlay

Modifications:

- Import the new module or overlay wherever `Glibc` is used
- Keep this repo building with Swift 5 by duplicating some declarations

Result:

All the same tests keep passing on my Android CI, finagolfin/swift-android-sdk#158
2024-07-22 09:58:09 +00:00
Franz Busch c9756e1083 Adopt swift-format (#2794)
* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
2024-07-19 11:48:17 +02:00
Franz Busch 5cc05490c4 [GHA] Unacceptable language check (#2766)
* [GHA] Unacceptable language check

# Motivation

Next up replacing part of our soundness script that checked for unacceptable language.

# Modification

This PR adds a new GH action that checks for unacceptable language.

# Result

One more script replaced

* PR review

* Change homepage link to repo
2024-07-09 07:56:26 +01:00
Alastair Houghton fc6e3c0eef Changes to support building with Musl (#2595)
* Changes to support building with Musl

Define `_GNU_SOURCE` in the `Package.swift` rather than in `shim.c`
(this is required because `_GNU_SOURCE` affects modular headers).

Add an import for Musl to IO.swift.

Add code to disable `SIGPIPE` to `SocketProtocols.swift`.

Remove types from a pile of functions in `System.swift`; Swift will
use the correct type automatically (except in cases where there are
multiple versions of a function, e.g. `ioctl()`, in which case we
need to be explicit which one we mean).

* Fix some test failures caused by failing to define `_GNU_SOURCE`.

A couple of the integration tests grab code and build it outside of the
normal `Package.swift`, so they needed fixing to define `_GNU_SOURCE`
themselves.
2023-11-29 17:27:13 +00:00
finagolfin 4223cb3776 Build for Android with NDK 26, by accounting for the new nullability annotations (#2600)
Motivation:

Fix build for the latest LTS NDK 26

Modifications:

- Update C declarations
- Add force unwraps where needed

Result:

Everything works on Android with NDK 26b
2023-11-27 09:05:30 +00:00
Si BeaumontandCory Benfield 858cb8927e Add bootstrap APIs for VSOCK sockets (#2479)
* Add bootstrap APIs for vsock sockets

Signed-off-by: Si Beaumont <beaumont@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-08-03 15:03:09 +01:00
finagolfin 231902a295 Correct a few Musl additions from #2449 for Android, plus error if libc not found (#2451)
Motivation

Fix build errors on Android

Modifications

- Fix previous Musl modifications that assumed Glibc wasn't imported on Android
- Add errors for all libc imports, so new platform ports error out early

Result

NIO builds natively on Android again, with all the same tests passing
2023-06-26 02:52:18 -07:00
Max Desiatov 43b1f59fde Add support for Musl libc (#2449)
* Add support for Musl libc

Since Musl is sufficiently different from Glibc (see https://wiki.musl-libc.org/functional-differences-from-glibc.html), it requires a different import, which now should be applied to files that have `import Glibc` in them.

* Fix `msghdr` initialization

* Fix msghdr mutability

* Fix `UnsafeMutableRawPointer` type conversions
2023-06-23 08:20:14 +01:00
Franz Busch e865f6163f Use #if canImport(Darwin) where possible (#2446) 2023-06-22 10:12:01 +01:00
David Nadoba 0b4edd8329 Add NIOBSDSocket.ProtocolSubtype (#2317)
* RawSocket prototype

* Conform `ProtocolSubtype` to `Hashable`

* Add public `NIOIPProtocol` type

Make `ProtocolSubtype` internal

* Subset of IANA protocols with an RFC

* Add `CustomStringConvertible` to `NIOIPProtocol`

* Add `init(_ rawValue: Int)`

* Rename `NIOBSDSocket.ProtocolSubtype.ip` to `.default`

* Add `NIOBSDSocket.ProtocolSubtype.mptcp`

and remove `NIOBSDSocket.mptcpProtocolSubtype`
2022-11-22 06:01:52 -08:00
buttaface 6c31c9a46c Add correct C directory function declarations for Android (#2302)
Motivation

Get the Android build working again

Modifications

- Modify close/open/readdir arguments, as they can be null
- Remove mkpath_np, as it's not there on Android

Result

Android builds again and the same tests pass
2022-10-25 18:12:42 +01:00
Artem Redkin d0ebe8da5d adds create directory method (#2296)
Motivation:
Creating directories is a common task.

Modifications:
 - Adds mkdir syscall
 - Adds create directory implementation
 - Test
2022-10-24 15:02:10 +01:00
Artem Redkin 7bfceed5dc Implements additional file operation in NonBlockingFileIO (#2244)
Motivation:
Basic set of function that NonBlockingFileIO provides is enough to read and write files, but as we get more and more into fully asynchronous world we need other non-blocking file-related function.

Modifications:
 - Adds support for getting file information using lstat
 - Adds ability to create a symlink, delete symlink and read it's destination
 - Adds ability to list directories
 - Adds ability to rename and remove files
2022-10-24 12:58:04 +01:00
Saleem Abdulrasool 01b13a5675 NIOPosix: reorganise packet info constants (#2182)
Co-locate the packet info constants and add Windows values.
2022-06-10 16:55:51 +01:00
Saleem Abdulrasool 57d41b502c NIOPosix: reorganise ECN constants, define for Windows (#2181)
Reorganise the ECN constants to colocate the definitions for the
different platforms.  Define the constants for Windows as the platform
does not provide them in the system headers.
2022-06-10 07:12:26 +01:00
Saleem Abdulrasool e697fd315a NIOPosix: silence some deprecation warnings on Windows (#2171)
Create a wrapper over `strerror` which is considered deprecated due to
the use of a shared global buffer.  Use `strerror_s` which requires a
buffer and size to prevent shared state or a buffer overrun.  This is
documented by Microsoft as requiring at most 94 characters, so we use a
buffer of size 95.
2022-06-08 10:20:52 +01:00
Saleem AbdulrasoolandCory Benfield 4448bb04aa NIOPosix: remove unused alias for function (#2172)
This cleans up a reference to `freeifaddrs` as that is no longer
referenced in the code base.  This avoids an error on Windows as
a side-effect.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 10:48:29 -07:00
Saleem Abdulrasool cd70dd3ec1 NIOPosix: shuffle typealias location (#2174)
Move the `sockaddr` typealias for Windows into the `BSDSocketAPIWindows`
file which is a better home for the platform specific typealias.
2022-06-07 10:24:35 -07:00
Saleem AbdulrasoolandCory Benfield a8bb008516 NIOPosix: use errno accessor on Windows (#2167)
`errno` is a thread local variable, and as such on many platforms is
accessed indirectly.  This is implemented as macro, but is considered
complex by the clang importer and cannot be directly used.  Resolve this
by using the accessor for the value on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 05:10:12 -07:00
Saleem AbdulrasoolandCory Benfield b883295b0e NIOPosix: re-export C library on Windows to match (#2166)
This matches the behaviour on the non-Windows targets and re-exports the
C library.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 04:08:57 -07:00
Franz Busch 51c3fc2e4a Precondition that read doesn't return EINVAL (#2022)
### Motivation:

`read` should never return `EINVAL`. If it does return it though it is a bug inside NIO and we should precondition for it similar to what we do for `EBADF` & `EFAULT`. Closes https://github.com/apple/swift-nio/issues/1339

### Modifications:

This PR adds a new precondition for `EINVAL` and adds test that exercise this precondition.

### Result:

We are now preconditioning against `read` returning `EINVAL`.
2022-01-12 04:16:36 -08:00
buttafaceandCory Benfield 0467886d0b Get Android constants used by Channel from Bionic instead (#2026)
Motivation:

An upcoming pull, apple/swift#35707, moves Android to the same single-header
modulemap for Bionic as used on linux, but that doesn't work unless this is
changed. This approach works both with the current release toolchain and
with that new modulemap.

Modifications:

Change how these Bionic constants are imported.

Result:

This repo compiles for Android and the tests pass with both modulemap approaches.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-01-06 09:45:46 +00:00
Cory Benfield d3a65106b2 Don't tolerate EBADF on close. (#2002)
Motivation:

It's like that #1999 was a little wide, and we should have forbidden
EBDAF on close.

Modifications:

Still forbid EBADF on close.

Result:

EBADF still preconditions on close.
2021-12-03 09:34:17 +00:00
John ConnollyandCory Benfield b465de0663 relax-precondition (#1999)
* relax-precondition

* Explanatory comment.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-12-03 08:31:38 +00:00
Cory Benfield b05c6f2206 Move NIO to NIOPosix, make NIO a shell. (#1936)
Motivation:

The remaining NIO code really conceptually belongs in a module called
NIOPosix, and NIOCore should really be called NIO. We can't really do
that last step, but we can prepare by pushing the bulk of the remaining
code into a module called NIOPosix.

Modifications:

- Move NIO to NIOPosix
- Make NIO an umbrella module.

Result:

NIOPosix exists.
2021-08-16 16:50:40 +01:00