Hello, I'd like to pitch the start of socket support for *nix-like platforms in System. gist.
Socket Descriptors for Swift System
- Proposal: SYS-NNNN
- Authors: Michael Ilseman, Karoy Lorentey, Franz Busch
- Review Manager: TBD
- Status: Draft
- Implementation: TBD
- Review: TBD
Revision history
- v1 Initial version
Introduction
We propose adding support for socket programming to Swift System, allowing systems-level Swift programs to create, configure, and communicate over network and local sockets on Darwin, Linux, and other Unix-ish platforms.
This proposal is the first step: SocketDescriptor and nested types for the constants that socket calls take.
Future proposals will pitch more types, more constants, and more syscalls related to sockets.
Motivation
Socket programming is essential for building networked applications, servers, and systems software. Currently, this functionality is accessed through imported C APIs, which are error-prone and unwieldy.
More portable constant spellings
This pitch is just focused on a handful of the essential socket types and their corresponding constants, with other types, constants, and actual socket operations coming in a future pitch. Even with this limited subset, we can see potential for cleanup.
swift-corelibs-foundation plays an import dance to define a couple of constants:
#if canImport(Darwin)
import Darwin
fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
#endif
#if canImport(Glibc) && !os(OpenBSD) && !os(FreeBSD)
@preconcurrency import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
#endif
#if canImport(Musl)
@preconcurrency import Musl
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
#endif
#if canImport(Glibc) && (os(OpenBSD) || os(FreeBSD))
@preconcurrency import Glibc
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
fileprivate let INADDR_ANY: in_addr_t = 0
fileprivate let INADDR_LOOPBACK = 0x7f000001
#endif
#if canImport(Android)
@preconcurrency import Android
fileprivate let FOUNDATION_SOCK_STREAM = Int32(Android.SOCK_STREAM)
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
fileprivate let INADDR_ANY: in_addr_t = 0
#endif
With this proposal, that can be simplified to:
import SystemPackage
fileprivate let FOUNDATION_SOCK_STREAM = SocketDescriptor.ConnectionType.stream.rawValue
fileprivate let FOUNDATION_IPPROTO_TCP = SocketDescriptor.ProtocolID.tcp.rawValue
(The INADDR_ANY and INADDR_LOOPBACK definitions remain until socket addresses are pitched.)
Similarly, in swift-nio we find:
#if os(Android)
static let SOCK_CLOEXEC = Android.SOCK_CLOEXEC
static let SOCK_NONBLOCK = Android.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)
#endif
With this proposal:
static let SOCK_CLOEXEC = SocketDescriptor.OpenOptions.closeOnExec.rawValue
static let SOCK_NONBLOCK = SocketDescriptor.OpenOptions.nonBlocking.rawValue
swift-nio also defines the shutdown constants once per C library (excerpt):
#if canImport(Darwin)
static let SHUT_RD: CInt = CInt(Darwin.SHUT_RD)
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) || os(OpenBSD)
#if canImport(Glibc)
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 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)
#elseif canImport(Android)
static let SHUT_RD: CInt = CInt(Android.SHUT_RD)
static let SHUT_WR: CInt = CInt(Android.SHUT_WR)
static let SHUT_RDWR: CInt = CInt(Android.SHUT_RDWR)
#endif
#else
// Windows
#endif
With this proposal, the Unix branches become:
static let SHUT_RD = SocketDescriptor.ShutdownKind.read.rawValue
static let SHUT_WR = SocketDescriptor.ShutdownKind.write.rawValue
static let SHUT_RDWR = SocketDescriptor.ShutdownKind.readWrite.rawValue
And of course, as more socket API is added, the use sites of these constants will improve (and we can drop the .rawValue at that point).
Checking that a descriptor is a socket
One operation that we do pitch here is testing if a file descriptor is a socket.
Today, System can tell whether a file descriptor refers to a socket, using stat (SYS-0006):
guard try fd.stat().type == .socket else { throw Errno.notSocket }
// `fd` is still a `FileDescriptor`; nothing in its type says it's a socket
With this proposal, the check produces a value whose type says it's a socket:
let socket = try SocketDescriptor(fd) // throws Errno.notSocket or Errno.badFileDescriptor
Proposed solution
We introduce SocketDescriptor, a descriptor type that is known to refer to a socket, and six nested types for the constants that socket calls take: Domain, ConnectionType and ProtocolID (the arguments to socket()), OpenOptions (flags applied when a socket is created or accepted), MessageFlags (flags for sending and receiving) and ShutdownKind (the argument to shutdown()). Socket operations, addresses and options come in future pitches (see Future directions).
Detailed design
See the Appendix for a table of Swift API to C mappings.
All API is @_alwaysEmitIntoClient, except SocketDescriptor.init(_:), which calls getsockopt, and the description properties, which follow FileDescriptor.OpenOptions and are ordinary public API.
SocketDescriptor
/// A socket descriptor.
///
/// A `SocketDescriptor` is a type-safe wrapper around an operating system
/// socket handle.
///
/// Unlike `FileDescriptor`, a `SocketDescriptor` explicitly represents a
/// socket rather than a general file descriptor.
@frozen
public struct SocketDescriptor: RawRepresentable, Sendable, Hashable, Codable {
/// The raw C socket handle.
public let rawValue: CInt
/// Creates a strongly-typed socket descriptor from a raw C socket handle.
public init(rawValue: CInt)
/// The file descriptor for this socket.
public var fileDescriptor: FileDescriptor { get }
/// Creates a socket descriptor from a file descriptor, verifying that the
/// file descriptor refers to a socket.
///
/// If `fd` is open but doesn't refer to a socket, this initializer throws
/// `Errno.notSocket`. If `fd` isn't open, it throws `Errno.badFileDescriptor`.
///
/// The corresponding C function is `getsockopt` with the `SO_TYPE` option.
public init(_ fd: FileDescriptor) throws(Errno)
/// Creates a socket descriptor from a file descriptor without verification.
///
/// This initializer does not verify that the file descriptor actually
/// refers to a socket.
public init(unchecked fd: FileDescriptor)
}
Domain
extension SocketDescriptor {
/// Communications domain, identifying the protocol family.
///
/// The domain specifies the protocol family to be used for communication.
/// Common domains include `.ipv4` for IPv4, `.ipv6` for IPv6, and `.local`
/// for Unix domain sockets.
@frozen
public struct Domain: RawRepresentable, Sendable, Hashable, Codable {
public var rawValue: CInt
public init(rawValue: CInt)
/// Unspecified domain.
///
/// The corresponding C constants are `AF_UNSPEC` and `PF_UNSPEC`.
public static var unspecified: Domain { get }
/// Local (Unix domain) communication.
///
/// The corresponding C constants are `AF_LOCAL` and `PF_LOCAL`.
public static var local: Domain { get }
/// Local (Unix domain) communication; the same value as `local`.
///
/// The corresponding C constants are `AF_UNIX` and `PF_UNIX`.
public static var unix: Domain { get }
/// Internet Protocol version 4.
///
/// The corresponding C constants are `AF_INET` and `PF_INET`.
public static var ipv4: Domain { get }
/// Internet Protocol version 6.
///
/// The corresponding C constants are `AF_INET6` and `PF_INET6`.
public static var ipv6: Domain { get }
#if !os(Linux) && !os(Android)
/// Internal routing protocol.
///
/// The corresponding C constants are `AF_ROUTE` and `PF_ROUTE`.
public static var routing: Domain { get }
#endif
/// Internal key-management function.
///
/// The corresponding C constant is `PF_KEY`.
public static var keyManagement: Domain { get }
#if SYSTEM_PACKAGE_DARWIN || os(Linux) || os(Android)
/// Communication between a virtual machine and its host.
///
/// The corresponding C constants are `AF_VSOCK` and `PF_VSOCK`.
public static var vsock: Domain { get }
#endif
#if os(Linux) || os(Android)
/// Raw packets at the device-driver level.
///
/// The corresponding C constants are `AF_PACKET` and `PF_PACKET`.
public static var packet: Domain { get }
#endif
#if SYSTEM_PACKAGE_DARWIN
/// Kernel event and kernel control messages.
///
/// The corresponding C constants are `AF_SYSTEM` and `PF_SYSTEM`.
public static var system: Domain { get }
/// Raw access to network device.
///
/// The corresponding C constants are `AF_NDRV` and `PF_NDRV`.
public static var networkDevice: Domain { get }
#endif
}
}
ConnectionType
extension SocketDescriptor {
/// The socket type, specifying the semantics of communication.
///
/// The connection type determines how data is transmitted. Stream sockets
/// provide reliable, ordered byte streams, while datagram sockets provide
/// connectionless, unreliable messages.
@frozen
public struct ConnectionType: RawRepresentable, Sendable, Hashable, Codable {
public var rawValue: CInt
public init(rawValue: CInt)
/// Sequenced, reliable, two-way, connection-based byte streams.
///
/// The corresponding C constant is `SOCK_STREAM`.
public static var stream: ConnectionType { get }
/// Connectionless, unreliable datagrams of a fixed maximum length.
///
/// The corresponding C constant is `SOCK_DGRAM`.
public static var datagram: ConnectionType { get }
/// Raw network protocol access.
///
/// The corresponding C constant is `SOCK_RAW`.
public static var raw: ConnectionType { get }
#if os(Linux) || os(Android)
/// Reliably-delivered message.
///
/// The corresponding C constant is `SOCK_RDM`.
public static var reliablyDeliveredMessage: ConnectionType { get }
#endif
#if !SYSTEM_PACKAGE_DARWIN
/// Sequenced packet stream.
///
/// The corresponding C constant is `SOCK_SEQPACKET`.
public static var sequencedPacketStream: ConnectionType { get }
#endif
}
}
ProtocolID
extension SocketDescriptor {
/// Identifies a particular protocol to use for communication.
///
/// Protocol numbers are specific to the communication domain.
@frozen
public struct ProtocolID: RawRepresentable, Sendable, Hashable, Codable {
public var rawValue: CInt
public init(rawValue: CInt)
/// Default protocol for the domain and socket type.
public static var `default`: ProtocolID { get }
/// Internet Protocol (IP).
///
/// The corresponding C constant is `IPPROTO_IP`.
public static var ip: ProtocolID { get }
/// IPv4 encapsulation.
///
/// The corresponding C constant is `IPPROTO_IPV4`, or `IPPROTO_IPIP` on
/// Linux and Android.
public static var ipv4: ProtocolID { get }
/// IPv6 header.
///
/// The corresponding C constant is `IPPROTO_IPV6`.
public static var ipv6: ProtocolID { get }
/// Internet Control Message Protocol.
///
/// The corresponding C constant is `IPPROTO_ICMP`.
public static var icmp: ProtocolID { get }
/// Transmission Control Protocol (TCP).
///
/// The corresponding C constant is `IPPROTO_TCP`.
public static var tcp: ProtocolID { get }
/// User Datagram Protocol (UDP).
///
/// The corresponding C constant is `IPPROTO_UDP`.
public static var udp: ProtocolID { get }
/// Raw IP packet.
///
/// The corresponding C constant is `IPPROTO_RAW`.
public static var raw: ProtocolID { get }
#if os(Linux) || os(Android)
/// Multipath TCP.
///
/// The corresponding C constant is `IPPROTO_MPTCP`.
public static var mptcp: ProtocolID { get }
#endif
}
}
MessageFlags
extension SocketDescriptor {
/// Flags for send and receive operations.
@frozen
public struct MessageFlags: OptionSet, Sendable, Hashable, Codable {
public var rawValue: CInt
public init(rawValue: CInt)
/// Process out-of-band data.
///
/// The corresponding C constant is `MSG_OOB`.
public static var outOfBand: MessageFlags { get }
#if !os(OpenBSD)
/// Bypass routing, use direct interface.
///
/// The corresponding C constant is `MSG_DONTROUTE`.
public static var doNotRoute: MessageFlags { get }
#endif
/// Peek at incoming message without removing it.
///
/// The corresponding C constant is `MSG_PEEK`.
public static var peek: MessageFlags { get }
/// Wait for full request or error.
///
/// The corresponding C constant is `MSG_WAITALL`.
public static var waitForAll: MessageFlags { get }
/// End-of-record marker.
///
/// The corresponding C constant is `MSG_EOR`.
public static var endOfRecord: MessageFlags { get }
/// Data was truncated.
///
/// The corresponding C constant is `MSG_TRUNC`.
public static var dataTruncated: MessageFlags { get }
/// Control data was truncated.
///
/// The corresponding C constant is `MSG_CTRUNC`.
public static var controlTruncated: MessageFlags { get }
/// Do not generate `SIGPIPE` on broken pipe; the operation fails with
/// `Errno.brokenPipe` instead.
///
/// The corresponding C constant is `MSG_NOSIGNAL`.
public static var noSignal: MessageFlags { get }
#if os(Linux) || os(Android) || os(FreeBSD) || os(OpenBSD)
/// Set close-on-exec on file descriptors received in ancillary data.
///
/// The corresponding C constant is `MSG_CMSG_CLOEXEC`.
public static var receivedDescriptorsCloseOnExec: MessageFlags { get }
#endif
#if os(FreeBSD) || os(OpenBSD)
/// Set close-on-fork on file descriptors received in ancillary data.
///
/// The corresponding C constant is `MSG_CMSG_CLOFORK`.
public static var receivedDescriptorsCloseOnFork: MessageFlags { get }
#endif
}
}
ShutdownKind
extension SocketDescriptor {
/// Specifies which parts of a full-duplex connection to shut down.
@frozen
public struct ShutdownKind: RawRepresentable, Sendable, Hashable, Codable {
public var rawValue: CInt
public init(rawValue: CInt)
/// Disallow further receives.
///
/// The corresponding C constant is `SHUT_RD`.
public static var read: ShutdownKind { get }
/// Disallow further sends.
///
/// The corresponding C constant is `SHUT_WR`.
public static var write: ShutdownKind { get }
/// Disallow further sends and receives.
///
/// The corresponding C constant is `SHUT_RDWR`.
public static var readWrite: ShutdownKind { get }
}
}
OpenOptions
extension SocketDescriptor {
/// Options that specify behavior for a newly created or accepted socket,
/// applied atomically when the socket is created or accepted.
@frozen
public struct OpenOptions: OptionSet, Sendable, Hashable, Codable {
public var rawValue: CInt
public init(rawValue: CInt)
#if !SYSTEM_PACKAGE_DARWIN
/// Indicates that all subsequent input and output operations on the
/// socket will be nonblocking.
///
/// The corresponding C constant is `SOCK_NONBLOCK`.
public static var nonBlocking: OpenOptions { get }
/// Indicates that executing a program closes the socket.
///
/// The corresponding C constant is `SOCK_CLOEXEC`.
public static var closeOnExec: OpenOptions { get }
#endif
#if os(FreeBSD) || os(OpenBSD)
/// Indicates that forking a program closes the socket.
///
/// The corresponding C constant is `SOCK_CLOFORK`.
public static var closeOnFork: OpenOptions { get }
#endif
}
}
Descriptions
Each of the six constant types conforms to CustomStringConvertible and CustomDebugStringConvertible, as FileDescriptor.AccessMode and FileDescriptor.OpenOptions do. A value prints as its constant's name (stream); an option set prints as a list ([.peek, .waitForAll]); bits or values with no named constant print as the type and raw value (ConnectionType(rawValue: 10)). Where two names share a value, the description uses local and default.
Platform availability
Constants not available on every platform, either because the platform's C library doesn't define them or because its kernel doesn't support them:
| Constant | Darwin | Linux | Android | FreeBSD | OpenBSD |
|---|---|---|---|---|---|
routing |
yes | no | no | yes | yes |
vsock |
yes | yes | yes | no | no |
packet |
no | yes | yes | no | no |
system, networkDevice |
yes | no | no | no | no |
reliablyDeliveredMessage |
no | yes | yes | no | no |
sequencedPacketStream |
no | yes | yes | yes | yes |
doNotRoute |
yes | yes | yes | yes | no |
mptcp |
no | yes | yes | no | no |
receivedDescriptorsCloseOnExec |
no | yes | yes | yes | yes |
receivedDescriptorsCloseOnFork |
no | no | no | yes | yes |
nonBlocking, closeOnExec |
no | yes | yes | yes | yes |
closeOnFork |
no | no | no | yes | yes |
On Darwin, OpenOptions has no members: Darwin doesn't support creating or accepting a socket with these options atomically.
Source compatibility
This proposal is additive and source-compatible with existing code. It adds one top-level name, SocketDescriptor; the other types are nested in it.
ABI compatibility
This proposal is additive and ABI-compatible with existing code.
Implications on adoption
This feature can be freely adopted and un-adopted in source code without affecting source or ABI compatibility.
Constants are available only on the platforms whose C libraries define them and whose kernels support them (see Platform availability). Cross-platform code guards those uses with #if, as it already does for FileDescriptor.PipeOptions.closeOnFork.
Future directions
Socket operations, addresses, and options
Here's a rough breakdown of what socket functionality could come in future pitches:
| Proposal | Contents |
|---|---|
| Socket addresses | SocketAddress and its IPv4, IPv6 and Unix-domain forms |
| Socket operations | open, close, bind, listen, accept, connect, send, receive, shutdown, local and peer address, socket pairs |
| Socket options | getsockopt and setsockopt: settings on an existing socket, such as SO_REUSEADDR, SO_RCVBUF, TCP_NODELAY and IP_TTL |
| Address resolution | getaddrinfo, getnameinfo |
| Messages | sendmsg, recvmsg, ancillary data |
IORing socket requests |
Linux |
| Windows sockets | Winsock, alongside the Win32 proposals |
WASI
Supporting WASI is future work. wasi-libc defines a subset of these constants, and what sockets can do on WASI depends on its version: WASI 0.2 and later can create IPv4 and IPv6 sockets, while preview 1 can only use sockets provided by the host.
Alternatives considered
Extension on FileDescriptor
We considered adding socket operations directly to FileDescriptor:
extension FileDescriptor {
public func bind(to address: SocketAddress) throws
public func listen(backlog: Int) throws
// ...
}
This approach has drawbacks:
- Namespace pollution: Socket operations would clutter
FileDescriptor's API for non-socket uses - Type safety: Nothing prevents calling
listen()on a regular file - Intent documentation: A separate type makes socket usage explicit
We chose SocketDescriptor for cleaner namespacing, better type safety, and clearer documentation of intent.
Noncopyable SocketDescriptor
We considered making SocketDescriptor ~Copyable. We instead propose a copyable SocketDescriptor, following FileDescriptor.
The benefit of a noncopyable descriptor is that the compiler prevents double close and use-after-close. These bugs are dangerous because a closed descriptor's number can be reused immediately by an unrelated open, socket, or accept, so a stale descriptor silently refers to the wrong resource.
However, descriptors in the general case do not have lifetimes that can be statically modeled with non-copyable types. The OS manages descriptor lifetimes dynamically, through explicit calls such as dup and close and by passing descriptors between processes. A noncopyable type can express only a subset of these lifetimes: it defines out some bugs, but it also defines out important and plausible uses. For example, I/O code may keep the same descriptor in several places at once, such as an event loop's registry and a connection object. Some descriptors (e.g. received by C code), may have no clear owner in the program at all.
System's lifetime support facilities should be an opt-in to greater-but-constrained safety, not an opt-out that removes all the other guards. With only a noncopyable SocketDescriptor, code that doesn't fit a statically-linear lifetime model would drop down to the raw CInt, giving up System's strong types, Errno errors, and EINTR retry along with lifetime checking.
This is especially important for incremental adoption: a copyable type can be adopted one call site at a time. A noncopyable one potentially requires rearchitecting major portions of code. If incremental adoption entails frequent use of escape hatches, it can create a situation where the net result is less safe (akin to "Cross-Language Attacks," NDSS 2022).
System is a strongly typed, error-throwing, but faithful representation of low-level OS concepts. It is not a end-developer-facing library for I/O. Libraries built on System, which typically present their own (sometimes cross-platform) I/O abstractions, are better placed to define their common path and the lifetime model that fits those abstractions, as well as their escape hatches.
Future work could include an additional higher-level owned type, owning wrappers, etc. For example, System today has the opt-in FileDescriptor.closeAfter(_:), which predates non-copyable types. Furthermore, dependent-lifetime types in Swift (still under development) could provide an even better model for opt-in facilities in System and/or for those future I/O libraries to use.
Other names for OpenOptions
Some alternative names could be: SocketFlags, CreationOptions, TypeFlags, etc.
OpenOptions follows FileDescriptor.OpenOptions, PipeOptions and DuplicateOptions, named for the call that takes them.
OpenOptions unavailable on Darwin
Since OpenOptions has no members on Darwin, it could be unavailable there instead. This proposal keeps the type available on every platform so that API taking options:, such as a future open or accept, has the same signature everywhere: cross-platform code can pass [], or options built under #if, without guarding the call itself. Platform differences live in which members exist, as with FileDescriptor.PipeOptions, whose closeOnFork is unavailable on Linux.
Other names for ConnectionType
datagram and raw are connectionless, so the name ConnectionType fits only some of its members. C and POSIX call this argument the socket type, and the GNU C Library manual calls it the communication style. Alternatives include Style, Semantics and Kind; SocketType would repeat Socket inside SocketDescriptor. This proposal keeps ConnectionType, the name used by earlier drafts of this API.
routing on Linux
On Darwin and the BSDs, PF_ROUTE is the routing socket. On Linux, PF_ROUTE is defined as PF_NETLINK, a general kernel messaging family whose routing protocol uses a different message format, so code written for the routing socket wouldn't work there. This proposal makes routing unavailable on Linux and Android. Alternatives were to keep routing available everywhere with a note, or to add a separate netlink constant for Linux.
Open questions
- The name
OpenOptions(Other names forOpenOptions). - Names for
MSG_CMSG_CLOEXECandMSG_CMSG_CLOFORK, which set close-on-exec or close-on-fork on file descriptors received over a Unix-domain socket. Candidates:receivedDescriptorsCloseOnExec(this proposal),closeOnExecForReceivedDescriptors(groups withOpenOptions.closeOnExecin completion),controlMessageCloseOnExec(follows the C name).
Appendix
Swift API to C mappings
| Swift | C |
|---|---|
SocketDescriptor.init(_:) |
getsockopt(fd, SOL_SOCKET, SO_TYPE, ...) |
Domain |
AF_* and PF_* |
ConnectionType |
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET |
ProtocolID |
IPPROTO_* |
MessageFlags |
MSG_* |
ShutdownKind |
SHUT_* |
OpenOptions |
SOCK_NONBLOCK, SOCK_CLOEXEC, SOCK_CLOFORK |
Other descriptor kinds
We are not yet pitching these, but for illustration purposes here are some other descriptor-like types in System's future:
/// A descriptor that refers to a terminal.
@frozen
public struct TerminalDescriptor: RawRepresentable, Sendable, Hashable, Codable {
public let rawValue: CInt
public init(rawValue: CInt)
/// The file descriptor for this terminal.
public var fileDescriptor: FileDescriptor { get }
/// Throws `Errno.inappropriateIOCTLForDevice` if `fd` isn't a terminal.
///
/// The corresponding C function is `isatty`.
public init(_ fd: FileDescriptor) throws(Errno)
public init(unchecked fd: FileDescriptor)
}
/// A descriptor that refers to an open directory.
@frozen
public struct DirectoryDescriptor: RawRepresentable, Sendable, Hashable, Codable {
public let rawValue: CInt
public init(rawValue: CInt)
/// The file descriptor for this directory.
public var fileDescriptor: FileDescriptor { get }
/// Throws `Errno.notDirectory` if `fd` isn't a directory.
///
/// The corresponding C function is `fstat`.
public init(_ fd: FileDescriptor) throws(Errno)
public init(unchecked fd: FileDescriptor)
}

