wait.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "../curl_setup.h"
  25. #ifndef HAVE_SELECT
  26. #error "We cannot compile without select() support."
  27. #endif
  28. #include <limits.h>
  29. #ifdef HAVE_SYS_SELECT_H
  30. #include <sys/select.h>
  31. #elif defined(HAVE_UNISTD_H)
  32. #include <unistd.h>
  33. #endif
  34. #ifdef MSDOS
  35. #include <dos.h> /* delay() */
  36. #endif
  37. #include "timediff.h"
  38. #include "wait.h"
  39. /*
  40. * Internal function used for waiting a specific amount of ms in
  41. * Curl_socket_check() and Curl_poll() when no file descriptor is provided to
  42. * wait on, just being used to delay execution. Winsock select() and poll()
  43. * timeout mechanisms need a valid socket descriptor in a not null file
  44. * descriptor set to work. Waiting indefinitely with this function is not
  45. * allowed, a zero or negative timeout value will return immediately. Timeout
  46. * resolution, accuracy, as well as maximum supported value is system
  47. * dependent, neither factor is a critical issue for the intended use of this
  48. * function in the library.
  49. *
  50. * Return values:
  51. * -1 = system call error, or invalid timeout value
  52. * 0 = specified timeout has elapsed, or interrupted
  53. */
  54. int curlx_wait_ms(timediff_t timeout_ms)
  55. {
  56. int r = 0;
  57. if(!timeout_ms)
  58. return 0;
  59. if(timeout_ms < 0) {
  60. SET_SOCKERRNO(SOCKEINVAL);
  61. return -1;
  62. }
  63. #ifdef MSDOS
  64. delay((unsigned int)timeout_ms);
  65. #elif defined(_WIN32)
  66. /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */
  67. #if TIMEDIFF_T_MAX >= ULONG_MAX
  68. if(timeout_ms >= ULONG_MAX)
  69. timeout_ms = ULONG_MAX-1;
  70. /* do not use ULONG_MAX, because that is equal to INFINITE */
  71. #endif
  72. Sleep((DWORD)timeout_ms);
  73. #else
  74. /* avoid using poll() for this since it behaves incorrectly with no sockets
  75. on Apple operating systems */
  76. {
  77. struct timeval pending_tv;
  78. r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms));
  79. }
  80. #endif /* _WIN32 */
  81. if(r) {
  82. if((r == -1) && (SOCKERRNO == SOCKEINTR))
  83. /* make EINTR from select or poll not a "lethal" error */
  84. r = 0;
  85. else
  86. r = -1;
  87. }
  88. return r;
  89. }