How do I tell the "exceptional condition" to call select () to react to errorfds?
According to its man page, the select () system call offers to track three different aspects of one or more file descriptors: whether they are read-ready, write-ready, or whether an "error" or "exception" occurred (language). Which one should be controlled is determined by three arguments fd_set
called readfds
, writefds
and errorfds
. Although there is a lot of good documentation and examples of correct usage readfds
and writefds
, I could hardly find anything useful in errorfds
.
Anyway, for the reasons discussed below, I used the try errorfds
and did find several circumstances in which a call select()
in my runloop responded to an "exception" condition on one of its file descriptors. For example, a TTY associated with a PTY while the latter is closing on its main side will cause this condition.
But now what? I know that some kind of "exceptional condition" happened in the file descriptor, but how, in general, can I know what exactly caused it? Just looking at errno
, of course, does not give an answer (it is always 0 at this point). Perhaps there are some "magical" ioctl
ones I should be aware of?
Additional information: A number of my programs (mostly written in C) communicate with external devices via serial ports. For testing purposes, I also wrote a simple server that creates PTYs to which my other programs can connect to the corresponding TTYs as if it were a serial port. While this all works very well at a basic level, error handling or other exceptional conditions are not really implemented at all at this time, which sometimes leads to some pretty nasty behavior. This needs to be changed!
The only exclusive condition I'm particularly interested in is whether the connection has been broken. For example, it would be nice to notice when a port goes off, say, because the user pulled out a USB-to-serial adapter. Handling read and write errors correctly seems to avoid the most annoying side effects, but I was wondering if there are more (watch errorfds
or maybe some other signals) I should be doing. Unfortunately, UNIX signal handling is something I'm not at all familiar with.
source to share
I know that some "exceptional condition" happened in the file descriptor, but how, in general, can I know what exactly caused it? Perhaps there is some "magic" ioctl I should be aware of?
You should try reading 0 bytes. On linux at least man 2 read
states:
If count is zero, read () can detect the errors described below.
So then read(fd, NULL,0)
you should have errno
one that tells you something more, without having to read anything. The word weasel in the manpage means this is probably not very portable, though (cf read (fd, NULL, 0), what does it do? Is it well defined? )
source to share