From ce2aaf1792db76b94812c0785259b7d442d4e00a Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Tue, 14 Aug 2018 15:01:34 +0100 Subject: [PATCH] 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. --- Sources/NIO/BaseSocket.swift | 6 ++++-- Sources/NIO/System.swift | 13 +++++++----- Tests/NIOTests/NonBlockingFileIOTest.swift | 3 ++- Tests/NIOTests/SocketChannelTest+XCTest.swift | 1 + Tests/NIOTests/SocketChannelTest.swift | 20 +++++++++++++++++++ 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Sources/NIO/BaseSocket.swift b/Sources/NIO/BaseSocket.swift index a8bc6a1e6..0750f3c51 100644 --- a/Sources/NIO/BaseSocket.swift +++ b/Sources/NIO/BaseSocket.swift @@ -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)") } } diff --git a/Sources/NIO/System.swift b/Sources/NIO/System.swift index 35519019c..aa12a31a8 100644 --- a/Sources/NIO/System.swift +++ b/Sources/NIO/System.swift @@ -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 diff --git a/Tests/NIOTests/NonBlockingFileIOTest.swift b/Tests/NIOTests/NonBlockingFileIOTest.swift index 99f2a7911..6c72133a7 100644 --- a/Tests/NIOTests/NonBlockingFileIOTest.swift +++ b/Tests/NIOTests/NonBlockingFileIOTest.swift @@ -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, diff --git a/Tests/NIOTests/SocketChannelTest+XCTest.swift b/Tests/NIOTests/SocketChannelTest+XCTest.swift index 28ad35433..77e8fa236 100644 --- a/Tests/NIOTests/SocketChannelTest+XCTest.swift +++ b/Tests/NIOTests/SocketChannelTest+XCTest.swift @@ -44,6 +44,7 @@ extension SocketChannelTest { ("testWithConfiguredDatagramSocket", testWithConfiguredDatagramSocket), ("testPendingConnectNotificationOrder", testPendingConnectNotificationOrder), ("testLocalAndRemoteAddressNotNilInChannelInactiveAndHandlerRemoved", testLocalAndRemoteAddressNotNilInChannelInactiveAndHandlerRemoved), + ("testSocketFlagNONBLOCKWorks", testSocketFlagNONBLOCKWorks), ] } } diff --git a/Tests/NIOTests/SocketChannelTest.swift b/Tests/NIOTests/SocketChannelTest.swift index 4a5701f3d..ec5131b7b 100644 --- a/Tests/NIOTests/SocketChannelTest.swift +++ b/Tests/NIOTests/SocketChannelTest.swift @@ -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()) + } }