commit d2ea06249ce96b034ae35567c33e0fca5d1ea477 Author: Daniel Kobras Date: Mon Jul 21 10:47:53 2014 +0200 sys_poll_intr: fix timeout arithmetic Callers of sys_poll_intr() assume timeout to be in milliseconds like poll(2) expects, but implementation used nanosecond units. Also make sure timeout doesn't become infinite by mistake during time arithmetic. Signed-off-by: Daniel Kobras diff --git a/lib/util/select.c b/lib/util/select.c index 5e66344..99cd772 100644 --- a/lib/util/select.c +++ b/lib/util/select.c @@ -42,9 +42,19 @@ int sys_poll_intr(struct pollfd *fds, int num_fds, int timeout) if (errno != EINTR) { break; } + /* Infinite timeout, no need to adjust. */ + if (timeout < 0) { + continue; + } clock_gettime_mono(&now); - elapsed = nsec_time_diff(&now, &start); - timeout = (orig_timeout - elapsed) / 1000000; + elapsed = nsec_time_diff(&now, &start) / 1000000; + timeout = orig_timeout - elapsed; + /* Unlikely, but might happen eg. when getting traced. + * Make sure we're not hanging in this case. + */ + if (timeout < 0) { + timeout = 0; + } }; return ret; }