func getServerFileDescriptorFromLaunchd()

in nio-launchd/Sources/nio-launchd/Server.swift [21:50]


    func getServerFileDescriptorFromLaunchd() throws -> CInt {
        let fds = UnsafeMutablePointer<UnsafeMutablePointer<CInt>>.allocate(capacity: 1)
        defer {
            fds.deallocate()
        }

        var count: Int = 0
        let ret = launch_activate_socket("Listeners", fds, &count)

        // Check the return code.
        guard ret == 0 else {
            print("error: launch_activate_socket returned with a non-zero exit code \(ret)")
            throw ExitCode(-1)
        }

        // launchd allows arbitary number of listeners but we only expect one in this example.
        guard count == 1 else {
            print("error: expected launch_activate_socket to return exactly one file descriptor")
            throw ExitCode(-1)
        }

        // This is safe because we already checked that we have exactly one result.
        let fd = fds.pointee.pointee

        defer {
            free(&fds.pointee.pointee)
        }

        return fd
    }