From: Viktor Szakats Date: Mon, 16 Jun 2025 13:20:17 +0000 (+0200) Subject: lib: make `curlx_wait_ms()` and use it X-Git-Tag: rc-8_15_0-1~32 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=35d0c047ce713298f15771649ffe7662404628f0;p=thirdparty%2Fcurl.git lib: make `curlx_wait_ms()` and use it Move function to curlx/, change all callers. Also: - src: replace local implementation. - tests/client: replace local ad-hoc sleep code. - tests/libtest: replace local `wait_ms()` implementation. - lib1531: replace local ad-hoc sleep code. - tests/server: replace local, simplified copy. - tests/server: formatting, drop some unused headers. Closes #17641 --- diff --git a/lib/Makefile.inc b/lib/Makefile.inc index fd2546b1d7..8e33a23f6c 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -34,6 +34,7 @@ LIB_CURLX_CFILES = \ curlx/timediff.c \ curlx/timeval.c \ curlx/version_win32.c \ + curlx/wait.c \ curlx/warnless.c \ curlx/winapi.c @@ -49,6 +50,7 @@ LIB_CURLX_HFILES = \ curlx/timediff.h \ curlx/timeval.h \ curlx/version_win32.h \ + curlx/wait.h \ curlx/warnless.h \ curlx/winapi.h diff --git a/lib/curlx/curlx.h b/lib/curlx/curlx.h index e3598c23a0..5b97ec5b83 100644 --- a/lib/curlx/curlx.h +++ b/lib/curlx/curlx.h @@ -65,6 +65,9 @@ #include "timeval.h" #include "timediff.h" +#include "wait.h" +/* for curlx_wait_ms */ + #include "winapi.h" /* for curlx_winapi_strerror */ diff --git a/lib/curlx/wait.c b/lib/curlx/wait.c new file mode 100644 index 0000000000..f568fb6948 --- /dev/null +++ b/lib/curlx/wait.c @@ -0,0 +1,97 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + ***************************************************************************/ + +#include "../curl_setup.h" + +#ifndef HAVE_SELECT +#error "We cannot compile without select() support." +#endif + +#include + +#ifdef HAVE_SYS_SELECT_H +#include +#elif defined(HAVE_UNISTD_H) +#include +#endif + +#ifdef MSDOS +#include /* delay() */ +#endif + +#include "timediff.h" +#include "wait.h" + +/* + * Internal function used for waiting a specific amount of ms in + * Curl_socket_check() and Curl_poll() when no file descriptor is provided to + * wait on, just being used to delay execution. Winsock select() and poll() + * timeout mechanisms need a valid socket descriptor in a not null file + * descriptor set to work. Waiting indefinitely with this function is not + * allowed, a zero or negative timeout value will return immediately. Timeout + * resolution, accuracy, as well as maximum supported value is system + * dependent, neither factor is a critical issue for the intended use of this + * function in the library. + * + * Return values: + * -1 = system call error, or invalid timeout value + * 0 = specified timeout has elapsed, or interrupted + */ +int curlx_wait_ms(timediff_t timeout_ms) +{ + int r = 0; + + if(!timeout_ms) + return 0; + if(timeout_ms < 0) { + SET_SOCKERRNO(SOCKEINVAL); + return -1; + } +#if defined(MSDOS) + delay((unsigned int)timeout_ms); +#elif defined(_WIN32) + /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */ +#if TIMEDIFF_T_MAX >= ULONG_MAX + if(timeout_ms >= ULONG_MAX) + timeout_ms = ULONG_MAX-1; + /* do not use ULONG_MAX, because that is equal to INFINITE */ +#endif + Sleep((DWORD)timeout_ms); +#else + /* avoid using poll() for this since it behaves incorrectly with no sockets + on Apple operating systems */ + { + struct timeval pending_tv; + r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms)); + } +#endif /* _WIN32 */ + if(r) { + if((r == -1) && (SOCKERRNO == SOCKEINTR)) + /* make EINTR from select or poll not a "lethal" error */ + r = 0; + else + r = -1; + } + return r; +} diff --git a/src/tool_sleep.h b/lib/curlx/wait.h similarity index 86% rename from src/tool_sleep.h rename to lib/curlx/wait.h index de1d7e256b..208bc20acc 100644 --- a/src/tool_sleep.h +++ b/lib/curlx/wait.h @@ -1,5 +1,5 @@ -#ifndef HEADER_CURL_TOOL_SLEEP_H -#define HEADER_CURL_TOOL_SLEEP_H +#ifndef HEADER_CURL_WAIT_H +#define HEADER_CURL_WAIT_H /*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | @@ -23,8 +23,9 @@ * SPDX-License-Identifier: curl * ***************************************************************************/ -#include "tool_setup.h" -void tool_go_sleep(long ms); +#include "../curl_setup.h" -#endif /* HEADER_CURL_TOOL_SLEEP_H */ +int curlx_wait_ms(timediff_t timeout_ms); + +#endif /* HEADER_CURL_WAIT_H */ diff --git a/lib/easy.c b/lib/easy.c index 73b60f7f67..b9e3fa31e2 100644 --- a/lib/easy.c +++ b/lib/easy.c @@ -67,6 +67,7 @@ #include "amigaos.h" #include "macos.h" #include "curlx/warnless.h" +#include "curlx/wait.h" #include "sigpipe.h" #include "vssh/ssh.h" #include "setopt.h" @@ -611,7 +612,7 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev) #endif pollrc = 0; if(ev->ms > 0) - Curl_wait_ms(ev->ms); + curlx_wait_ms(ev->ms); } ev->msbump = FALSE; /* reset here */ diff --git a/lib/multi.c b/lib/multi.c index 79a0935c10..3114e6aecb 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -42,6 +42,7 @@ #include "http.h" #include "select.h" #include "curlx/warnless.h" +#include "curlx/wait.h" #include "speedcheck.h" #include "conncache.h" #include "multihandle.h" @@ -1458,7 +1459,7 @@ static CURLMcode multi_wait(struct Curl_multi *multi, timeout */ else if(sleep_ms < 0) sleep_ms = timeout_ms; - Curl_wait_ms(sleep_ms); + curlx_wait_ms(sleep_ms); } } diff --git a/lib/select.c b/lib/select.c index ee239b7321..2353c474df 100644 --- a/lib/select.c +++ b/lib/select.c @@ -36,75 +36,19 @@ #include #endif -#ifdef MSDOS -#include /* delay() */ -#endif - #include #include "urldata.h" #include "connect.h" #include "select.h" #include "curlx/timediff.h" +#include "curlx/wait.h" #include "curlx/warnless.h" /* The last 3 #include files should be in this order */ #include "curl_printf.h" #include "curl_memory.h" #include "memdebug.h" -/* - * Internal function used for waiting a specific amount of ms in - * Curl_socket_check() and Curl_poll() when no file descriptor is provided to - * wait on, just being used to delay execution. Winsock select() and poll() - * timeout mechanisms need a valid socket descriptor in a not null file - * descriptor set to work. Waiting indefinitely with this function is not - * allowed, a zero or negative timeout value will return immediately. Timeout - * resolution, accuracy, as well as maximum supported value is system - * dependent, neither factor is a critical issue for the intended use of this - * function in the library. - * - * Return values: - * -1 = system call error, or invalid timeout value - * 0 = specified timeout has elapsed, or interrupted - */ -int Curl_wait_ms(timediff_t timeout_ms) -{ - int r = 0; - - if(!timeout_ms) - return 0; - if(timeout_ms < 0) { - SET_SOCKERRNO(SOCKEINVAL); - return -1; - } -#if defined(MSDOS) - delay((unsigned int)timeout_ms); -#elif defined(_WIN32) - /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */ -#if TIMEDIFF_T_MAX >= ULONG_MAX - if(timeout_ms >= ULONG_MAX) - timeout_ms = ULONG_MAX-1; - /* do not use ULONG_MAX, because that is equal to INFINITE */ -#endif - Sleep((DWORD)timeout_ms); -#else - /* avoid using poll() for this since it behaves incorrectly with no sockets - on Apple operating systems */ - { - struct timeval pending_tv; - r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms)); - } -#endif /* _WIN32 */ - if(r) { - if((r == -1) && (SOCKERRNO == SOCKEINTR)) - /* make EINTR from select or poll not a "lethal" error */ - r = 0; - else - r = -1; - } - return r; -} - #ifndef HAVE_POLL /* * This is a wrapper around select() to aid in Windows compatibility. A @@ -132,7 +76,7 @@ static int our_select(curl_socket_t maxfd, /* highest socket number */ (!fds_write || fds_write->fd_count == 0) && (!fds_err || fds_err->fd_count == 0)) { /* no sockets, just wait */ - return Curl_wait_ms(timeout_ms); + return curlx_wait_ms(timeout_ms); } #endif @@ -194,7 +138,7 @@ int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */ if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) { /* no sockets, just wait */ - return Curl_wait_ms(timeout_ms); + return curlx_wait_ms(timeout_ms); } /* Avoid initial timestamp, avoid curlx_now() call, when elapsed @@ -289,7 +233,7 @@ int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms) } if(fds_none) { /* no sockets, just wait */ - return Curl_wait_ms(timeout_ms); + return curlx_wait_ms(timeout_ms); } /* Avoid initial timestamp, avoid curlx_now() call, when elapsed diff --git a/lib/select.h b/lib/select.h index 10968fab7f..e9cec600f3 100644 --- a/lib/select.h +++ b/lib/select.h @@ -82,7 +82,6 @@ int Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2, Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z) int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms); -int Curl_wait_ms(timediff_t timeout_ms); /* With Winsock the valid range is [0..INVALID_SOCKET-1] according to diff --git a/lib/vtls/openssl.c b/lib/vtls/openssl.c index a32307c05b..e7a82fffd5 100644 --- a/lib/vtls/openssl.c +++ b/lib/vtls/openssl.c @@ -54,6 +54,7 @@ #include "../connect.h" #include "../slist.h" #include "../select.h" +#include "../curlx/wait.h" #include "vtls.h" #include "vtls_int.h" #include "vtls_scache.h" @@ -1004,7 +1005,7 @@ static CURLcode ossl_seed(struct Curl_easy *data) size_t i, i_max; for(i = 0, i_max = len / sizeof(struct curltime); i < i_max; ++i) { struct curltime tv = curlx_now(); - Curl_wait_ms(1); + curlx_wait_ms(1); tv.tv_sec *= (time_t)i + 1; tv.tv_usec *= (int)i + 2; tv.tv_sec ^= ((curlx_now().tv_sec + (time_t)curlx_now().tv_usec) * diff --git a/packages/vms/gnv_link_curl.com b/packages/vms/gnv_link_curl.com index d7b29f5283..83a8e021e8 100644 --- a/packages/vms/gnv_link_curl.com +++ b/packages/vms/gnv_link_curl.com @@ -412,7 +412,7 @@ $ link'ldebug'/exe=[.src]curl.exe/dsf=[.src]curl.dsf - [.src]curl-tool_msgs.o, [.src]curl-tool_operate.o, - [.src]curl-tool_operhlp.o, - [.src]curl-tool_paramhlp.o, [.src]curl-tool_parsecfg.o, - - [.src]curl-tool_setopt.o, [.src]curl-tool_sleep.o, - + [.src]curl-tool_setopt.o, - [.src]curl-tool_urlglob.o, [.src]curl-tool_util.o, - [.src]curl-tool_vms.o, [.src]curl-tool_writeenv.o, - [.src]curl-tool_writeout.o, [.src]curl-tool_xattr.o, - diff --git a/src/Makefile.inc b/src/Makefile.inc index c83154474a..a4c3ca453a 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -41,6 +41,7 @@ CURLX_CFILES = \ ../lib/curlx/timediff.c \ ../lib/curlx/timeval.c \ ../lib/curlx/version_win32.c \ + ../lib/curlx/wait.c \ ../lib/curlx/warnless.c CURLX_HFILES = \ @@ -52,6 +53,7 @@ CURLX_HFILES = \ ../lib/curlx/timediff.h \ ../lib/curlx/timeval.h \ ../lib/curlx/version_win32.h \ + ../lib/curlx/wait.h \ ../lib/curlx/warnless.h CURL_CFILES = \ @@ -88,7 +90,6 @@ CURL_CFILES = \ tool_parsecfg.c \ tool_progress.c \ tool_setopt.c \ - tool_sleep.c \ tool_ssls.c \ tool_stderr.c \ tool_strdup.c \ @@ -136,7 +137,6 @@ CURL_HFILES = \ tool_sdecls.h \ tool_setopt.h \ tool_setup.h \ - tool_sleep.h \ tool_ssls.h \ tool_stderr.h \ tool_strdup.h \ diff --git a/src/tool_cb_rea.c b/src/tool_cb_rea.c index 86ae476a22..da93a9d3d0 100644 --- a/src/tool_cb_rea.c +++ b/src/tool_cb_rea.c @@ -34,7 +34,6 @@ #include "tool_operate.h" #include "tool_util.h" #include "tool_msgs.h" -#include "tool_sleep.h" #include /* keep this as LAST include */ @@ -155,7 +154,7 @@ int tool_readbusy_cb(void *clientp, } else /* sleep half a period */ - tool_go_sleep(25); + curlx_wait_ms(25); } return per->noprogress ? 0 : CURL_PROGRESSFUNC_CONTINUE; diff --git a/src/tool_operate.c b/src/tool_operate.c index 2397de1686..a52439caf8 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -80,7 +80,6 @@ #include "tool_paramhlp.h" #include "tool_parsecfg.h" #include "tool_setopt.h" -#include "tool_sleep.h" #include "tool_ssls.h" #include "tool_urlglob.h" #include "tool_util.h" @@ -1965,7 +1964,7 @@ static CURLcode serial_transfers(struct GlobalConfig *global, returncode = post_per_transfer(global, per, result, &retry, &delay_ms); if(retry) { - tool_go_sleep(delay_ms); + curlx_wait_ms(delay_ms); continue; } @@ -1998,7 +1997,7 @@ static CURLcode serial_transfers(struct GlobalConfig *global, "waits %ldms as set by --rate", milli, (long)(global->ms_per_transfer - milli)); /* The transfer took less time than wanted. Wait a little. */ - tool_go_sleep((long)(global->ms_per_transfer - milli)); + curlx_wait_ms((long)(global->ms_per_transfer - milli)); } } } diff --git a/src/tool_sleep.c b/src/tool_sleep.c deleted file mode 100644 index e10584a7eb..0000000000 --- a/src/tool_sleep.c +++ /dev/null @@ -1,59 +0,0 @@ -/*************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ - * \___|\___/|_| \_\_____| - * - * Copyright (C) Daniel Stenberg, , et al. - * - * This software is licensed as described in the file COPYING, which - * you should have received as part of this distribution. The terms - * are also available at https://curl.se/docs/copyright.html. - * - * You may opt to use, copy, modify, merge, publish, distribute and/or sell - * copies of the Software, and permit persons to whom the Software is - * furnished to do so, under the terms of the COPYING file. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - * SPDX-License-Identifier: curl - * - ***************************************************************************/ -#include "tool_setup.h" - -#ifdef HAVE_SYS_SELECT_H -# include -#elif defined(HAVE_UNISTD_H) -# include -#endif - -#ifdef HAVE_POLL_H -# include -#elif defined(HAVE_SYS_POLL_H) -# include -#endif - -#ifdef MSDOS -# include -#endif - -#include "tool_sleep.h" - -#include /* keep this as LAST include */ - -void tool_go_sleep(long ms) -{ -#if defined(MSDOS) - delay(ms); -#elif defined(_WIN32) - Sleep((DWORD)ms); -#else - struct timeval timeout; - timeout.tv_sec = ms / 1000L; - ms = ms % 1000L; - timeout.tv_usec = (int)ms * 1000; - select(0, NULL, NULL, NULL, &timeout); -#endif -} diff --git a/tests/client/Makefile.inc b/tests/client/Makefile.inc index 604ff515c9..5f44f12d05 100644 --- a/tests/client/Makefile.inc +++ b/tests/client/Makefile.inc @@ -30,7 +30,9 @@ BUNDLE_SRC = clients.c FIRSTFILES = first.c first.h CURLX_SRCS = \ - ../../lib/curlx/multibyte.c + ../../lib/curlx/multibyte.c \ + ../../lib/curlx/timediff.c \ + ../../lib/curlx/wait.c # All test clients TESTFILES = \ diff --git a/tests/client/first.h b/tests/client/first.h index a3e77ee5f3..9aabc538e3 100644 --- a/tests/client/first.h +++ b/tests/client/first.h @@ -34,18 +34,11 @@ struct entry_s { #include +#include "curlx.h" + #include /* for calloc(), free(), strtol() */ #include /* for strchr(), strcmp() */ -#ifndef _WIN32 -#include /* for usleep() */ -#include /* for usleep() */ -#endif - -#ifdef __TANDEM -#include /* for usleep() logic */ -#endif - #define ERR() \ do { \ curl_mfprintf(stderr, "something unexpected went wrong - bailing out!\n");\ diff --git a/tests/client/ws_data.c b/tests/client/ws_data.c index a5677a83f2..493809f807 100644 --- a/tests/client/ws_data.c +++ b/tests/client/ws_data.c @@ -136,18 +136,7 @@ static CURLcode data_echo(CURL *curl, size_t count, if(rblock && sblock) { curl_mfprintf(stderr, "EAGAIN, sleep, try again\n"); -#ifdef _WIN32 - Sleep(100); -#elif defined(__TANDEM) - /* NonStop only defines usleep when building for a threading model */ -# if defined(_PUT_MODEL_) || defined(_KLT_MODEL_) - usleep(100*1000); -# else - PROCESS_DELAY_(100*1000); -# endif -#else - usleep(100*1000); -#endif + curlx_wait_ms(100); } } diff --git a/tests/client/ws_pingpong.c b/tests/client/ws_pingpong.c index 6af8a9cd22..0059d315e4 100644 --- a/tests/client/ws_pingpong.c +++ b/tests/client/ws_pingpong.c @@ -75,18 +75,7 @@ static CURLcode pingpong(CURL *curl, const char *payload) curl_mfprintf(stderr, "Receive pong\n"); res = recv_pong(curl, payload); if(res == CURLE_AGAIN) { -#ifdef _WIN32 - Sleep(100); -#elif defined(__TANDEM) - /* NonStop only defines usleep when building for a threading model */ -# if defined(_PUT_MODEL_) || defined(_KLT_MODEL_) - usleep(100*1000); -# else - PROCESS_DELAY_(100*1000); -# endif -#else - usleep(100*1000); -#endif + curlx_wait_ms(100); continue; } websocket_close(curl); diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc index ecacd61d57..db91fe7c2d 100644 --- a/tests/libtest/Makefile.inc +++ b/tests/libtest/Makefile.inc @@ -34,13 +34,10 @@ UTILS = memptr.c testutil.c testutil.h testtrace.c testtrace.h test.h ../unit/cu CURLX_SRCS = \ ../../lib/curlx/warnless.c \ - ../../lib/curlx/warnless.h \ ../../lib/curlx/multibyte.c \ - ../../lib/curlx/multibyte.h \ ../../lib/curlx/timediff.c \ - ../../lib/curlx/timediff.h \ ../../lib/curl_threads.c \ - ../../lib/curl_threads.h + ../../lib/curlx/wait.c # All libtest programs TESTFILES = \ diff --git a/tests/libtest/first.c b/tests/libtest/first.c index 3a201fb6de..2a828a1b3f 100644 --- a/tests/libtest/first.c +++ b/tests/libtest/first.c @@ -50,21 +50,6 @@ int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc, return select(nfds, rd, wr, exc, tv); } -void wait_ms(int ms) -{ - if(ms < 0) - return; -#ifdef USE_WINSOCK - Sleep((DWORD)ms); -#else - { - struct timeval t; - curlx_mstotv(&t, ms); - select_wrapper(0, NULL, NULL, NULL, &t); - } -#endif -} - char *libtest_arg2 = NULL; char *libtest_arg3 = NULL; char *libtest_arg4 = NULL; diff --git a/tests/libtest/first.h b/tests/libtest/first.h index 5148616716..ad56056dae 100644 --- a/tests/libtest/first.h +++ b/tests/libtest/first.h @@ -32,4 +32,6 @@ struct entry_s { entry_func_t ptr; }; +#include "curlx.h" + #endif /* HEADER_LIBTEST_FIRST_H */ diff --git a/tests/libtest/lib1506.c b/tests/libtest/lib1506.c index f63156dc9a..b47eb4af06 100644 --- a/tests/libtest/lib1506.c +++ b/tests/libtest/lib1506.c @@ -114,7 +114,7 @@ static CURLcode test_lib1506(char *URL) abort_on_test_timeout(); } - wait_ms(1); /* to ensure different end times */ + curlx_wait_ms(1); /* to ensure different end times */ } test_cleanup: diff --git a/tests/libtest/lib1515.c b/tests/libtest/lib1515.c index 7d98cfe61c..11cdef6677 100644 --- a/tests/libtest/lib1515.c +++ b/tests/libtest/lib1515.c @@ -134,7 +134,7 @@ static CURLcode test_lib1515(char *URL) } if(i < count) - sleep(DNS_TIMEOUT + 1); + curlx_wait_ms((DNS_TIMEOUT + 1) * 1000); } test_cleanup: diff --git a/tests/libtest/lib1517.c b/tests/libtest/lib1517.c index 214a76928b..c997b33581 100644 --- a/tests/libtest/lib1517.c +++ b/tests/libtest/lib1517.c @@ -37,7 +37,7 @@ static size_t t1517_read_cb(char *ptr, size_t size, size_t nmemb, void *userp) /* Wait one second before return POST data * * so libcurl will wait before sending request body */ - wait_ms(1000); + curlx_wait_ms(1000); if(tocopy < 1 || !pooh->sizeleft) return 0; diff --git a/tests/libtest/lib1531.c b/tests/libtest/lib1531.c index e3096d1504..50698d0b4c 100644 --- a/tests/libtest/lib1531.c +++ b/tests/libtest/lib1531.c @@ -107,15 +107,7 @@ static CURLcode test_lib1531(char *URL) curl_multi_fdset() doc. */ if(maxfd == -1) { -#ifdef _WIN32 - Sleep(100); - rc = 0; -#else - /* Portable sleep for platforms other than Windows. */ - struct timeval wait = {0}; - wait.tv_usec = 100 * 1000; /* 100ms */ - rc = select(0, NULL, NULL, NULL, &wait); -#endif + rc = curlx_wait_ms(100); } else { /* Note that on some platforms 'timeout' may be modified by select(). diff --git a/tests/libtest/lib1542.c b/tests/libtest/lib1542.c index 32ee236d95..95ffbf1d77 100644 --- a/tests/libtest/lib1542.c +++ b/tests/libtest/lib1542.c @@ -63,7 +63,7 @@ static CURLcode test_lib1542(char *URL) /* CURLOPT_MAXLIFETIME_CONN is inclusive - the connection needs to be 2 * seconds old */ - sleep(2); + curlx_wait_ms(2000); res = curl_easy_perform(easy); if(res) diff --git a/tests/libtest/lib1565.c b/tests/libtest/lib1565.c index b03b17dec0..455a3026e4 100644 --- a/tests/libtest/lib1565.c +++ b/tests/libtest/lib1565.c @@ -51,7 +51,7 @@ static void *t1565_run_thread(void *ptr) (void)ptr; for(i = 0; i < CONN_NUM; i++) { - wait_ms(TIME_BETWEEN_START_SECS * 1000); + curlx_wait_ms(TIME_BETWEEN_START_SECS * 1000); easy_init(easy); diff --git a/tests/libtest/lib2301.c b/tests/libtest/lib2301.c index da3f0e5331..6562a50541 100644 --- a/tests/libtest/lib2301.c +++ b/tests/libtest/lib2301.c @@ -84,7 +84,7 @@ static void t2301_websocket(CURL *curl) return; if(t2301_recv_pong(curl, "foobar")) return; - sleep(2); + curlx_wait_ms(2000); } while(i++ < 10); t2301_websocket_close(curl); } diff --git a/tests/libtest/lib2304.c b/tests/libtest/lib2304.c index f3091ee1a3..d4d583ff5c 100644 --- a/tests/libtest/lib2304.c +++ b/tests/libtest/lib2304.c @@ -104,7 +104,7 @@ static void t2304_websocket(CURL *curl) curl_mprintf("Connection closed\n"); return; } - sleep(2); + curlx_wait_ms(2000); } while(i++ < 10); t2304_websocket_close(curl); } diff --git a/tests/libtest/lib2402.c b/tests/libtest/lib2402.c index be761793b8..c7b52027c0 100644 --- a/tests/libtest/lib2402.c +++ b/tests/libtest/lib2402.c @@ -117,7 +117,7 @@ static CURLcode test_lib2402(char *URL) abort_on_test_timeout(); } - wait_ms(1); /* to ensure different end times */ + curlx_wait_ms(1); /* to ensure different end times */ } test_cleanup: diff --git a/tests/libtest/lib2404.c b/tests/libtest/lib2404.c index 2737a267ba..a8c9ed1827 100644 --- a/tests/libtest/lib2404.c +++ b/tests/libtest/lib2404.c @@ -119,7 +119,7 @@ static CURLcode test_lib2404(char *URL) abort_on_test_timeout(); } - wait_ms(1); /* to ensure different end times */ + curlx_wait_ms(1); /* to ensure different end times */ } test_cleanup: diff --git a/tests/libtest/lib2502.c b/tests/libtest/lib2502.c index 2047e4753f..9b1f8ab734 100644 --- a/tests/libtest/lib2502.c +++ b/tests/libtest/lib2502.c @@ -121,7 +121,7 @@ static CURLcode test_lib2502(char *URL) abort_on_test_timeout(); } - wait_ms(1); /* to ensure different end times */ + curlx_wait_ms(1); /* to ensure different end times */ } test_cleanup: diff --git a/tests/libtest/lib670.c b/tests/libtest/lib670.c index 981a4e92f9..b26605e3f5 100644 --- a/tests/libtest/lib670.c +++ b/tests/libtest/lib670.c @@ -202,7 +202,7 @@ static CURLcode test_lib670(char *URL) break; #ifdef _WIN32 if(maxfd == -1) - Sleep(100); + curlx_wait_ms(100); else #endif rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcept, &timeout); diff --git a/tests/libtest/test.h b/tests/libtest/test.h index 369e9c6f04..92aebff82a 100644 --- a/tests/libtest/test.h +++ b/tests/libtest/test.h @@ -49,10 +49,6 @@ #define CURL_GNUC_DIAG #endif -#ifdef _WIN32 -#define sleep(sec) Sleep((sec)*1000) -#endif - #define test_setopt(A,B,C) \ if((res = curl_easy_setopt((A), (B), (C))) != CURLE_OK) \ goto test_cleanup @@ -74,8 +70,6 @@ extern struct timeval tv_test_start; /* for test timing */ extern int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc, struct timeval *tv); -extern void wait_ms(int ms); /* wait this many milliseconds */ - extern char *hexdump(const unsigned char *buffer, size_t len); extern int unitfail; diff --git a/tests/server/Makefile.inc b/tests/server/Makefile.inc index 3781064352..062a503000 100644 --- a/tests/server/Makefile.inc +++ b/tests/server/Makefile.inc @@ -41,6 +41,7 @@ CURLX_SRCS = \ ../../lib/curlx/timediff.c \ ../../lib/curlx/timeval.c \ ../../lib/curlx/version_win32.c \ + ../../lib/curlx/wait.c \ ../../lib/curlx/warnless.c \ ../../lib/curlx/winapi.c diff --git a/tests/server/dnsd.c b/tests/server/dnsd.c index 52e2951c0c..6bb04e0c57 100644 --- a/tests/server/dnsd.c +++ b/tests/server/dnsd.c @@ -42,12 +42,6 @@ #ifdef HAVE_NETDB_H #include #endif -#ifdef HAVE_SYS_FILIO_H -/* FIONREAD on Solaris 7 */ -#include -#endif - -#include #include diff --git a/tests/server/mqttd.c b/tests/server/mqttd.c index 4ece4ff8c9..db5007f109 100644 --- a/tests/server/mqttd.c +++ b/tests/server/mqttd.c @@ -761,11 +761,11 @@ static curl_socket_t mqttd_sockdaemon(curl_socket_t sock, logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", error, sstrerror(error)); if(maxretr) { - rc = wait_ms(delay); + rc = curlx_wait_ms(delay); if(rc) { /* should not happen */ error = SOCKERRNO; - logmsg("wait_ms() failed with error (%d) %s", + logmsg("curlx_wait_ms() failed with error (%d) %s", error, sstrerror(error)); sclose(sock); return CURL_SOCKET_BAD; diff --git a/tests/server/rtspd.c b/tests/server/rtspd.c index 0e99365393..211759e4fb 100644 --- a/tests/server/rtspd.c +++ b/tests/server/rtspd.c @@ -964,13 +964,13 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req) quarters = num * 4; while(quarters > 0) { quarters--; - res = wait_ms(250); + res = curlx_wait_ms(250); if(got_exit_signal) break; if(res) { /* should not happen */ error = SOCKERRNO; - logmsg("wait_ms() failed with error (%d) %s", + logmsg("curlx_wait_ms() failed with error (%d) %s", error, sstrerror(error)); break; } diff --git a/tests/server/sockfilt.c b/tests/server/sockfilt.c index 85314a6288..b4e62a50bb 100644 --- a/tests/server/sockfilt.c +++ b/tests/server/sockfilt.c @@ -1253,11 +1253,11 @@ static curl_socket_t sockfilt_sockdaemon(curl_socket_t sock, logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", error, sstrerror(error)); if(maxretr) { - rc = wait_ms(delay); + rc = curlx_wait_ms(delay); if(rc) { /* should not happen */ error = SOCKERRNO; - logmsg("wait_ms() failed with error (%d) %s", + logmsg("curlx_wait_ms() failed with error (%d) %s", error, sstrerror(error)); sclose(sock); return CURL_SOCKET_BAD; diff --git a/tests/server/socksd.c b/tests/server/socksd.c index 9739f532e3..af8083bbdb 100644 --- a/tests/server/socksd.c +++ b/tests/server/socksd.c @@ -781,11 +781,11 @@ static curl_socket_t socksd_sockdaemon(curl_socket_t sock, logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s", error, sstrerror(error)); if(maxretr) { - rc = wait_ms(delay); + rc = curlx_wait_ms(delay); if(rc) { /* should not happen */ error = SOCKERRNO; - logmsg("wait_ms() failed with error (%d) %s", + logmsg("curlx_wait_ms() failed with error (%d) %s", error, sstrerror(error)); sclose(sock); return CURL_SOCKET_BAD; diff --git a/tests/server/sws.c b/tests/server/sws.c index b8f12bedc4..553030e61d 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -1133,7 +1133,7 @@ retry: written = swrite(sock, buffer, num); if(written < 0) { if((SOCKEWOULDBLOCK == SOCKERRNO) || (EAGAIN == SOCKERRNO)) { - wait_ms(10); + curlx_wait_ms(10); goto retry; } sendfailure = TRUE; @@ -1157,7 +1157,7 @@ retry: int sleep_time = msecs_left > MAX_SLEEP_TIME_MS ? MAX_SLEEP_TIME_MS : msecs_left; intervals--; - wait_ms(sleep_time); + curlx_wait_ms(sleep_time); msecs_left -= sleep_time; } } @@ -1201,11 +1201,11 @@ retry: quarters = num * 4; while((quarters > 0) && !got_exit_signal) { quarters--; - res = wait_ms(250); + res = curlx_wait_ms(250); if(res) { /* should not happen */ error = SOCKERRNO; - logmsg("wait_ms() failed with error (%d) %s", + logmsg("curlx_wait_ms() failed with error (%d) %s", error, sstrerror(error)); break; } @@ -1428,7 +1428,7 @@ static void http_connect(curl_socket_t *infdp, 'end of headers' separate from the server data that follows. This is done to prevent triggering libcurl known bug #39. */ for(loop = 2; (loop > 0) && !got_exit_signal; loop--) - wait_ms(250); + curlx_wait_ms(250); if(got_exit_signal) goto http_connect_cleanup; @@ -1593,7 +1593,7 @@ static void http_connect(curl_socket_t *infdp, if(!err && req2->connect_request) { /* sleep to prevent triggering libcurl known bug #39. */ for(loop = 2; (loop > 0) && !got_exit_signal; loop--) - wait_ms(250); + curlx_wait_ms(250); if(!got_exit_signal) { /* connect to the server */ serverfd[SWS_DATA] = connect_to(ipaddr, req2->connect_port); @@ -1747,7 +1747,7 @@ static void http_connect(curl_socket_t *infdp, if(tcp_fin_wr) /* allow kernel to place FIN bit packet on the wire */ - wait_ms(250); + curlx_wait_ms(250); /* socket clearing */ for(i = 0; i <= max_tunnel_idx; i++) { @@ -2391,7 +2391,7 @@ static int test_sws(int argc, char *argv[]) if(CURL_SOCKET_BAD == msgsock) goto sws_cleanup; if(req->delay) - wait_ms(req->delay); + curlx_wait_ms(req->delay); } while(msgsock > 0); active--; } @@ -2423,7 +2423,7 @@ static int test_sws(int argc, char *argv[]) wait a very small amount of time before doing so. If this is not done client might get an ECONNRESET before reading a single byte of server-reply. */ - wait_ms(50); + curlx_wait_ms(50); if(all_sockets[socket_idx] != CURL_SOCKET_BAD) { sclose(all_sockets[socket_idx]); diff --git a/tests/server/tftpd.c b/tests/server/tftpd.c index 0216b5960c..8c3484b389 100644 --- a/tests/server/tftpd.c +++ b/tests/server/tftpd.c @@ -71,8 +71,7 @@ #include #endif #ifdef HAVE_SYS_FILIO_H -/* FIONREAD on Solaris 7 */ -#include +#include /* FIONREAD on Solaris 7 */ #endif #include @@ -1208,7 +1207,7 @@ static void sendtftp(struct testcase *test, const struct formats *pf) if(test->writedelay) { logmsg("Pausing %d seconds before %d bytes", test->writedelay, size); - wait_ms(1000*test->writedelay); + curlx_wait_ms(1000*test->writedelay); } send_data: diff --git a/tests/server/util.c b/tests/server/util.c index 1e0531f514..9c8d279cee 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -37,10 +37,6 @@ #include #endif -#ifdef MSDOS -#include /* delay() */ -#endif - #include /* from the private lib dir */ /* adjust for old MSVC */ @@ -304,53 +300,6 @@ static FILE *test2fopen(long testno, const char *logdir2) return stream; } -/* - * Portable function used for waiting a specific amount of ms. - * Waiting indefinitely with this function is not allowed, a - * zero or negative timeout value will return immediately. - * - * Return values: - * -1 = system call error, or invalid timeout value - * 0 = specified timeout has elapsed - */ -static int wait_ms(timediff_t timeout_ms) -{ - int r = 0; - - if(!timeout_ms) - return 0; - if(timeout_ms < 0) { - SET_SOCKERRNO(SOCKEINVAL); - return -1; - } -#if defined(MSDOS) - delay((unsigned int)timeout_ms); -#elif defined(_WIN32) - /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */ -#if TIMEDIFF_T_MAX >= ULONG_MAX - if(timeout_ms >= ULONG_MAX) - timeout_ms = ULONG_MAX-1; - /* do not use ULONG_MAX, because that is equal to INFINITE */ -#endif - Sleep((DWORD)timeout_ms); -#else - /* avoid using poll() for this since it behaves incorrectly with no sockets - on Apple operating systems */ - { - struct timeval pending_tv; - r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms)); - } -#endif /* _WIN32 */ - if(r) { - if((r == -1) && (SOCKERRNO == SOCKEINTR)) - /* make EINTR from select or poll not a "lethal" error */ - r = 0; - else - r = -1; - } - return r; -} - #ifdef _WIN32 #define t_getpid() GetCurrentProcessId() #else diff --git a/tests/unit/unit2600.c b/tests/unit/unit2600.c index 09af169029..90b235aa49 100644 --- a/tests/unit/unit2600.c +++ b/tests/unit/unit2600.c @@ -23,6 +23,8 @@ ***************************************************************************/ #include "curlcheck.h" +#include + #ifdef HAVE_NETINET_IN_H #include #endif @@ -144,7 +146,7 @@ static CURLcode cf_test_connect(struct Curl_cfilter *cf, } if(duration_ms) { infof(data, "%04dms: cf[%s] continuing", (int)duration_ms, ctx->id); - Curl_wait_ms(10); + curlx_wait_ms(10); } Curl_expire(data, ctx->fail_delay_ms - duration_ms, EXPIRE_RUN_NOW); return CURLE_OK; diff --git a/winbuild/MakefileBuild.vc b/winbuild/MakefileBuild.vc index ff95d58420..8bdb22d6c4 100644 --- a/winbuild/MakefileBuild.vc +++ b/winbuild/MakefileBuild.vc @@ -696,6 +696,7 @@ CURL_FROM_LIBCURL=\ $(CURL_DIROBJ)\strparse.obj \ $(CURL_DIROBJ)\strcase.obj \ $(CURL_DIROBJ)\timeval.obj \ + $(CURL_DIROBJ)\wait.obj \ $(CURL_DIROBJ)\warnless.obj \ $(CURL_DIROBJ)\multibyte.obj \ $(CURL_DIROBJ)\version_win32.obj \ @@ -729,6 +730,8 @@ $(CURL_DIROBJ)\multibyte.obj: ../lib/curlx/multibyte.c $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/curlx/multibyte.c $(CURL_DIROBJ)\version_win32.obj: ../lib/curlx/version_win32.c $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/curlx/version_win32.c +$(CURL_DIROBJ)\wait.obj: ../lib/curlx/wait.c + $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/curlx/wait.c $(CURL_DIROBJ)\warnless.obj: ../lib/curlx/warnless.c $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/curlx/warnless.c $(CURL_DIROBJ)\dynbuf.obj: ../lib/curlx/dynbuf.c