So it appears to be a bug in macos with posix_openpt
.
I’m not a pseudo-terminal expert, so I’m not in a position to say whether or not Posix guarantees that posix_openpt
will behave as you expect. However, standard practice is to open the client file descriptor before you start playing around with the server one. Consider this code:
import Foundation
func setNonBlocking(_ fd: CInt) -> CInt {
var flags = fcntl(fd, F_GETFL)
assert(flags >= 0)
flags |= O_NONBLOCK
let ok = fcntl(fd, F_SETFL, flags) >= 0
return ok ? 0 : errno
}
func main() {
let server = posix_openpt(O_RDWR | O_NOCTTY)
print("server before:", setNonBlocking(server))
var ok = grantpt(server) >= 0
assert(ok)
ok = unlockpt(server) >= 0
assert(ok)
let client = open(String(cString: ptsname(server)), O_RDWR | O_NOCTTY, 0)
assert(client >= 0)
print("server after: ", setNonBlocking(server))
}
main()
It prints:
server before: 25
server after: 0
So, once you have client
set up, you can make server
non-blocking. Indeed, this is exactly what openpty
does under the covers, which you can see in the Darwin source.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"