Having more than one process read from a serial device (/dev/ttyXX) makes it so that both processes can\'t get all of the data -- the data will be split between them in some
The reason you can't find this is because there's no documented interface specifically to allow it. However, there is a trick that allows you to do it. After opening the pseudo-terminal master (assumed here to be file descriptor ptm
), you open and immediately close the slave side:
close(open(ptsname(ptm), O_RDWR | O_NOCTTY));
This sets the HUP flag on the tty master. You now poll the HUP flag regularly with poll()
(say, whenever data comes in from your data source):
struct pollfd pfd = { .fd = ptm, .events = POLLHUP };
poll(&pfd, 1, 10 /* or other small timeout */);
if (!(pfd.revents & POLLHUP))
{
/* There is now a reader on the slave side */
}
If the reader ever goes away, POLLHUP
will be set again.
In your case, you probably don't even need to remember from one loop to the next whether a given pty has a reader - just block on read()
on your data source, then when data is available, simultaneously poll()
all of your master ttys and send the data to any of them that do not have POLLHUP
set.
Add an inotify watch on the slave pty and poll on that. You can get an inotify event on open. You can then poll on the inotify file descriptor and the master pty file descriptor. You can get an inotify event for open (IN_OPEN). This will unblock the poll when the slave side is opened.