Skip to content

Commit

Permalink
correctly handle functions which return global errno
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed May 17, 2024
1 parent ded2937 commit 814697e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Sources/IORing/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions Sources/IORingUtils/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
10 changes: 5 additions & 5 deletions Sources/IORingUtils/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -148,15 +148,15 @@ 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)
}
}
}

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))
}
}
Expand All @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/IORingUtils/Tty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 814697e

Please sign in to comment.