If I have a file descriptor that has been opened with open (2) and subsequently set to O_NONBLOCK using fcntl (2) using a FFI (Foreign Function Interface).
This means almost every call to read (2) will return -1, with errno set to EAGAIN, which means this time there was no data available.
Sadly, the FFI I am using cannot access the errno variable, so I cannot determine whether the -1 value was returned because there was no data or because the file handle is no longer valid.
So I am trying to determine somehow if the file descriptor is still valid without ever having to read errno. I already tried all the answers with fcntl from this question, but they don't work and never return -1.
Perhaps this is because I am reading a device file: /dev/input/js0?
Is there another function I can call that will tell me whether the file descriptor is invalid? (In the question above someone mentioned poll, but I am not sure how this was meant.)
I am using the Squeak FFI and am not allowed to add any custom C wrappers. I am trying to access a Gamepad and read button information from it, which is an optional assignment.
Smalltalk Source Code of what I tried with fcntl:
The FFI for fcntl (defined in Class Gamepad):
manipulateFileHandle: fileHandle command: command
< cdecl: long 'fcntl' ( long long ) module: 'libc.so.6' >
^ self externalCallFailed
Then in another method, I call it:
| handleTest |
handleTest := Gamepad manipulateFileHandle: externalFileHandle command: 1.
Transcript show: handleTest; cr.
The command 1 is F_GETFD, reading the file descriptor flags. But handleTest is never -1, even after unplugging the gamepad.