From 814697e4af04996e12fec57c75ebcb296a1478d6 Mon Sep 17 00:00:00 2001 From: Luke Howard Date: Fri, 17 May 2024 17:01:17 +0200 Subject: [PATCH] correctly handle functions which return global errno --- Sources/IORing/Message.swift | 3 ++- Sources/IORingUtils/Extensions.swift | 15 ++++++++++++--- Sources/IORingUtils/Socket.swift | 10 +++++----- Sources/IORingUtils/Tty.swift | 6 +++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Sources/IORing/Message.swift b/Sources/IORing/Message.swift index be491f0..3f202dd 100644 --- a/Sources/IORing/Message.swift +++ b/Sources/IORing/Message.swift @@ -24,7 +24,8 @@ import CIOURing import Glibc import SystemPackage -extension msghdr: @unchecked Sendable {} +extension msghdr: @unchecked +Sendable {} public struct Control { public var level: Int32 diff --git a/Sources/IORingUtils/Extensions.swift b/Sources/IORingUtils/Extensions.swift index d2ecbbf..453d39b 100644 --- a/Sources/IORingUtils/Extensions.swift +++ b/Sources/IORingUtils/Extensions.swift @@ -101,14 +101,14 @@ extension IORing { public extension FileDescriptorRepresentable { func set(flags: Int32, mask: Int32) throws { - var flags = try Errno.throwingErrno { fcntl(self.fileDescriptor, F_GETFL, 0) } + var flags = try Errno.throwingGlobalErrno { fcntl(self.fileDescriptor, F_GETFL, 0) } flags &= ~mask flags |= mask - try Errno.throwingErrno { fcntl(self.fileDescriptor, F_SETFL, flags) } + try Errno.throwingGlobalErrno { fcntl(self.fileDescriptor, F_SETFL, flags) } } func get(flag: Int32) throws -> Bool { - let flags = try Errno.throwingErrno { fcntl(self.fileDescriptor, F_GETFL, 0) } + let flags = try Errno.throwingGlobalErrno { fcntl(self.fileDescriptor, F_GETFL, 0) } return flags & flag != 0 } @@ -143,4 +143,13 @@ public extension FileDescriptorRepresentable { extension Errno { static var lastError: Errno { Errno(rawValue: errno) } + + @discardableResult + static func throwingGlobalErrno(_ body: @escaping () -> CInt) throws -> CInt { + let result = body() + if result < 0 { + throw Errno(rawValue: errno) + } + return result + } } diff --git a/Sources/IORingUtils/Socket.swift b/Sources/IORingUtils/Socket.swift index 150fba0..ecef94d 100644 --- a/Sources/IORingUtils/Socket.swift +++ b/Sources/IORingUtils/Socket.swift @@ -66,7 +66,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { _ = try withUnsafeMutablePointer(to: &ss) { pointer in try pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { sa in - try Errno.throwingErrno { + try Errno.throwingGlobalErrno { body(fileHandle.fileDescriptor, sa, &length) } } @@ -102,7 +102,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { public func setBooleanOption(level: CInt = SOL_SOCKET, option: CInt, to value: Bool) throws { guard let fileHandle else { throw Errno.badFileDescriptor } var value: CInt = value ? 1 : 0 - try Errno.throwingErrno { setsockopt( + try Errno.throwingGlobalErrno { setsockopt( fileHandle.fileDescriptor, level, option, @@ -148,7 +148,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { public func bind(to address: any SocketAddress) throws { guard let fileHandle else { throw Errno.badFileDescriptor } _ = try address.withSockAddr { sa in - try Errno.throwingErrno { + try Errno.throwingGlobalErrno { SwiftGlibc.bind(fileHandle.fileDescriptor, sa, address.size) } } @@ -156,7 +156,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { public func listen(backlog: Int = 128) throws { guard let fileHandle else { throw Errno.badFileDescriptor } - _ = try Errno.throwingErrno { + _ = try Errno.throwingGlobalErrno { SwiftGlibc.listen(fileHandle.fileDescriptor, Int32(backlog)) } } @@ -182,7 +182,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { guard let fileHandle else { throw Errno.badFileDescriptor } try fileHandle.setBlocking(false) _ = try address.withSockAddr { sa in - try Errno.throwingErrno { + try Errno.throwingGlobalErrno { SwiftGlibc.connect(fileHandle.fileDescriptor, sa, address.size) } } diff --git a/Sources/IORingUtils/Tty.swift b/Sources/IORingUtils/Tty.swift index 351d0ce..3ab9e0c 100644 --- a/Sources/IORingUtils/Tty.swift +++ b/Sources/IORingUtils/Tty.swift @@ -49,7 +49,7 @@ public extension termios { mutating func set(speed: UInt32) throws { var tty = self - try Errno.throwingErrno { + try Errno.throwingGlobalErrno { cfsetspeed(&tty, speed) } self = tty @@ -89,14 +89,14 @@ public extension termios { public extension FileDescriptorRepresentable { func set(tty: termios) throws { var tty = tty - try Errno.throwingErrno { + try Errno.throwingGlobalErrno { tcsetattr(self.fileDescriptor, TCSANOW, &tty) } } func getTty() throws -> termios { var tty = termios() - try Errno.throwingErrno { + try Errno.throwingGlobalErrno { tcgetattr(self.fileDescriptor, &tty) } return tty