mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
fix Posix.fcntl return type (#575)
Motivation: Previously the code said that fcntl doesn't return anything but that's just not true, it returns `CInt`. Modifications: Made `fcntl` return `CInt` and tested that. Result: more correct code and more tests Motivation: Explain here the context, and why you're making that change. What is the problem you're trying to solve. Modifications: Describe the modifications you've done. Result: After your change, what will change.
This commit is contained in:
1 parent
9c7cae55d9
commit
ce2aaf1792
5 files changed
+35
-8
No files matched your search
@@ -267,7 +267,8 @@ class BaseSocket: Selectable {
|
||||
#if !os(Linux)
|
||||
if setNonBlocking {
|
||||
do {
|
||||
try Posix.fcntl(descriptor: sock, command: F_SETFL, value: O_NONBLOCK)
|
||||
let ret = try Posix.fcntl(descriptor: sock, command: F_SETFL, value: O_NONBLOCK)
|
||||
assert(ret == 0, "unexpectedly, fcntl(\(sock), F_SETFL, O_NONBLOCK) returned \(ret)")
|
||||
} catch {
|
||||
_ = try? Posix.close(descriptor: sock)
|
||||
throw error
|
||||
@@ -316,7 +317,8 @@ class BaseSocket: Selectable {
|
||||
/// throws: An `IOError` if the operation failed.
|
||||
final func setNonBlocking() throws {
|
||||
return try withUnsafeFileDescriptor { fd in
|
||||
try Posix.fcntl(descriptor: fd, command: F_SETFL, value: O_NONBLOCK)
|
||||
let ret = try Posix.fcntl(descriptor: fd, command: F_SETFL, value: O_NONBLOCK)
|
||||
assert(ret == 0, "unexpectedly, fcntl(\(fd), F_SETFL, O_NONBLOCK) returned \(ret)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -217,8 +217,8 @@ internal enum Posix {
|
||||
|
||||
@inline(never)
|
||||
// TODO: Allow varargs
|
||||
public static func fcntl(descriptor: CInt, command: CInt, value: CInt) throws {
|
||||
_ = try wrapSyscall {
|
||||
public static func fcntl(descriptor: CInt, command: CInt, value: CInt) throws -> CInt {
|
||||
return try wrapSyscall {
|
||||
sysFcntl(descriptor, command, value)
|
||||
}
|
||||
}
|
||||
@@ -233,7 +233,9 @@ internal enum Posix {
|
||||
_ = unsafeBitCast(Glibc.signal(SIGPIPE, SIG_IGN) as sighandler_t?, to: Int.self)
|
||||
#else
|
||||
if fd != -1 {
|
||||
_ = try? Posix.fcntl(descriptor: fd, command: F_SETNOSIGPIPE, value: 1)
|
||||
let ret = try? Posix.fcntl(descriptor: fd, command: F_SETNOSIGPIPE, value: 1)
|
||||
assert(ret == .some(0),
|
||||
"unexpectedly, fcntl(\(fd), F_SETFL, F_SETNOSIGPIPE) returned \(ret.debugDescription)")
|
||||
}
|
||||
#endif
|
||||
return fd
|
||||
@@ -270,8 +272,9 @@ internal enum Posix {
|
||||
|
||||
#if !os(Linux)
|
||||
if fd != -1 {
|
||||
// TODO: Handle return code ?
|
||||
_ = try? Posix.fcntl(descriptor: fd, command: F_SETNOSIGPIPE, value: 1)
|
||||
let ret = try? Posix.fcntl(descriptor: fd, command: F_SETNOSIGPIPE, value: 1)
|
||||
assert(ret == .some(0),
|
||||
"unexpectedly, fcntl(\(fd), F_SETFL, F_SETNOSIGPIPE) returned \(ret.debugDescription)")
|
||||
}
|
||||
#endif
|
||||
return fd
|
||||
|
||||
@@ -405,7 +405,8 @@ class NonBlockingFileIOTest: XCTestCase {
|
||||
try withPipe { readFH, writeFH in
|
||||
do {
|
||||
try readFH.withUnsafeFileDescriptor { readFD in
|
||||
try Posix.fcntl(descriptor: readFD, command: F_SETFL, value: O_NONBLOCK)
|
||||
let ret = try Posix.fcntl(descriptor: readFD, command: F_SETFL, value: O_NONBLOCK)
|
||||
assert(ret == 0, "unexpectedly, fcntl(\(readFD), F_SETFL, O_NONBLOCK) returned \(ret)")
|
||||
}
|
||||
try self.fileIO.readChunked(fileHandle: readFH,
|
||||
byteCount: 10,
|
||||
|
||||
@@ -44,6 +44,7 @@ extension SocketChannelTest {
|
||||
("testWithConfiguredDatagramSocket", testWithConfiguredDatagramSocket),
|
||||
("testPendingConnectNotificationOrder", testPendingConnectNotificationOrder),
|
||||
("testLocalAndRemoteAddressNotNilInChannelInactiveAndHandlerRemoved", testLocalAndRemoteAddressNotNilInChannelInactiveAndHandlerRemoved),
|
||||
("testSocketFlagNONBLOCKWorks", testSocketFlagNONBLOCKWorks),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,4 +536,24 @@ public class SocketChannelTest : XCTestCase {
|
||||
XCTAssertNoThrow(try clientChannel.close().wait())
|
||||
XCTAssertNoThrow(try handler.promise.futureResult.wait())
|
||||
}
|
||||
|
||||
func testSocketFlagNONBLOCKWorks() throws {
|
||||
var socket = try assertNoThrowWithValue(try ServerSocket(protocolFamily: PF_INET, setNonBlocking: true))
|
||||
XCTAssertNoThrow(try socket.withUnsafeFileDescriptor { fd in
|
||||
let flags = try assertNoThrowWithValue(Posix.fcntl(descriptor: fd, command: F_GETFL, value: 0))
|
||||
XCTAssertEqual(O_NONBLOCK, flags & O_NONBLOCK)
|
||||
})
|
||||
XCTAssertNoThrow(try socket.close())
|
||||
|
||||
socket = try assertNoThrowWithValue(ServerSocket(protocolFamily: PF_INET, setNonBlocking: false))
|
||||
XCTAssertNoThrow(try socket.withUnsafeFileDescriptor { fd in
|
||||
var flags = try assertNoThrowWithValue(Posix.fcntl(descriptor: fd, command: F_GETFL, value: 0))
|
||||
XCTAssertEqual(0, flags & O_NONBLOCK)
|
||||
let ret = try assertNoThrowWithValue(Posix.fcntl(descriptor: fd, command: F_SETFL, value: O_NONBLOCK))
|
||||
XCTAssertEqual(0, ret)
|
||||
flags = try assertNoThrowWithValue(Posix.fcntl(descriptor: fd, command: F_GETFL, value: 0))
|
||||
XCTAssertEqual(O_NONBLOCK, flags & O_NONBLOCK)
|
||||
})
|
||||
XCTAssertNoThrow(try socket.close())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user