libxl: datacopier: Avoid eof/POLLHUP race

When the bootloader exits, several things change, all at once:
 (a) The master pty fd (held by libxl) starts to signal POLLHUP
    and maybe also POLLIN.
 (b) The child exits (so that the SIGCHLD self-pipe signals POLLIN,
    which will be handled by the libxl child process code.
 (c) reads on the master pty fd start to return EOF

From the point of view of the datacopier these might happen in any
order.

(c) can be detected only after a previous POLLIN without POLLHUP and
that previous POLLIN would be associated with data which was read,
which must therefore have ended up in the dc's buffer.  But nothing
stops the dc from writing that data into the output fd and reporting
eof before it calls poll again.

This race is unlikely.  But  nevertheless it should be fixed.

We solve the race with a poll of the reading fd, to double-check, when
we detect eof via read.  (This is only necessary if the caller has
specified callback_pollhup, as otherwise POLLHUP|POLLIN - and,
presumably, POLLIN followed perhaps by POLLHUP|POLLIN, is to be
treated as eof anyway.)

With a testing patch supplied by me, Roger Pau Monné has reproduced
the failure on FreeBSD and verified that this patch fixes the problem.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Tested-by: Roger Pau Monné <roger.pau@citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
CC: Andrew Cooper <andrew.cooper3@citrix.com>
CC: Ross Lagerwall <ross.lagerwall@citrix.com>
CC: Wei Liu <wei.liu2@citrix.com>
This commit is contained in:
Ian Jackson authored and Ian Campbell committed 2015-04-15 13:57:47 +01:00
1 parent df9f5676b3
commit ef8da0184d
1 file changed
+16
+16
View File
@@ -250,6 +250,22 @@ static void datacopier_readable(libxl__egc *egc, libxl__ev_fd *ev,
return;
}
if (r == 0) {
if (dc->callback_pollhup) {
/* It might be that this "eof" is actually a HUP. If
* the caller cares about the difference,
* double-check using poll(2). */
struct pollfd hupchk;
hupchk.fd = ev->fd;
hupchk.events = POLLIN;
hupchk.revents = 0;
r = poll(&hupchk, 1, 0);
if (r < 0)
LIBXL__EVENT_DISASTER(egc,
"unexpected failure polling fd for datacopier eof hup check",
errno, 0);
if (datacopier_pollhup_handled(egc, dc, hupchk.revents, 0))
return;
}
libxl__ev_fd_deregister(gc, &dc->toread);
break;
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.