Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 2007 by Denys Vlasenko <vda.linux@googlemail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
| 11 | /* Wrapper which restarts poll on EINTR or ENOMEM. |
| 12 | * On other errors does perror("poll") and returns. |
| 13 | * Warning! May take longer than timeout_ms to return! */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 14 | int FAST_FUNC safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout) |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 15 | { |
| 16 | while (1) { |
| 17 | int n = poll(ufds, nfds, timeout); |
| 18 | if (n >= 0) |
| 19 | return n; |
| 20 | /* Make sure we inch towards completion */ |
| 21 | if (timeout > 0) |
| 22 | timeout--; |
| 23 | /* E.g. strace causes poll to return this */ |
| 24 | if (errno == EINTR) |
| 25 | continue; |
| 26 | /* Kernel is very low on memory. Retry. */ |
| 27 | /* I doubt many callers would handle this correctly! */ |
| 28 | if (errno == ENOMEM) |
| 29 | continue; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 30 | bb_simple_perror_msg("poll"); |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 31 | return n; |
| 32 | } |
| 33 | } |