safe_poll.c 870 B

12345678910111213141516171819202122232425262728293031323334
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2007 by Denys Vlasenko <vda.linux@googlemail.com>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. /* Wrapper which restarts poll on EINTR or ENOMEM.
  11. * On other errors does perror("poll") and returns.
  12. * Warning! May take longer than timeout_ms to return! */
  13. int FAST_FUNC safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout)
  14. {
  15. while (1) {
  16. int n = poll(ufds, nfds, timeout);
  17. if (n >= 0)
  18. return n;
  19. /* Make sure we inch towards completion */
  20. if (timeout > 0)
  21. timeout--;
  22. /* E.g. strace causes poll to return this */
  23. if (errno == EINTR)
  24. continue;
  25. /* Kernel is very low on memory. Retry. */
  26. /* I doubt many callers would handle this correctly! */
  27. if (errno == ENOMEM)
  28. continue;
  29. bb_perror_msg("poll");
  30. return n;
  31. }
  32. }