tests/server: sync wait_ms() with the libcurl implementation

It contains a series of bugfixes and updates applied to libcurl's
`Curl_wait_ms()` over the years, but missed from the copy in
`tests/server/util.c`:
- d65321f939,
  52e822173a,
  5912da253b
- 4a8f459837
- 1ad49feb71

It fixes `wait_ms()` to check for, and return `SOCKERRNO`. Fixing error
handling on Windows.

Also:
- tests/server: change callers to check `SOCKERRNO`.
- `wait_ms()`: fix to check for the correct error code on Windows.
  Pending for `Curl_wait_ms()`: #16621.
- `Curl_wait_ms()`: tidy-up `Sleep()` argument cast (nit).
- lib/curl_trc: drop an unused header.

Closes #16627
This commit is contained in:
Viktor Szakats 2025-03-08 22:15:07 +01:00
parent 454762d5ca
commit 5681628e2d
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
9 changed files with 34 additions and 51 deletions

View file

@ -30,7 +30,6 @@
#include "urldata.h"
#include "easyif.h"
#include "cfilters.h"
#include "timeval.h"
#include "multiif.h"
#include "strcase.h"

View file

@ -86,7 +86,7 @@ int Curl_wait_ms(timediff_t timeout_ms)
timeout_ms = ULONG_MAX-1;
/* do not use ULONG_MAX, because that is equal to INFINITE */
#endif
Sleep((ULONG)timeout_ms);
Sleep((DWORD)timeout_ms);
#else
/* avoid using poll() for this since it behaves incorrectly with no sockets
on Apple operating systems */

View file

@ -762,9 +762,9 @@ static curl_socket_t mqttd_sockdaemon(curl_socket_t sock,
rc = wait_ms(delay);
if(rc) {
/* should not happen */
error = errno;
error = SOCKERRNO;
logmsg("wait_ms() failed with error (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}

View file

@ -982,9 +982,9 @@ static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
break;
if(res) {
/* should not happen */
error = errno;
error = SOCKERRNO;
logmsg("wait_ms() failed with error (%d) %s",
error, strerror(error));
error, sstrerror(error));
break;
}
}

View file

@ -1253,9 +1253,9 @@ static curl_socket_t sockfilt_sockdaemon(curl_socket_t sock,
rc = wait_ms(delay);
if(rc) {
/* should not happen */
error = errno;
error = SOCKERRNO;
logmsg("wait_ms() failed with error (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}

View file

@ -790,9 +790,9 @@ static curl_socket_t socksd_sockdaemon(curl_socket_t sock,
rc = wait_ms(delay);
if(rc) {
/* should not happen */
error = errno;
error = SOCKERRNO;
logmsg("wait_ms() failed with error (%d) %s",
error, strerror(error));
error, sstrerror(error));
sclose(sock);
return CURL_SOCKET_BAD;
}

View file

@ -1221,9 +1221,9 @@ retry:
res = wait_ms(250);
if(res) {
/* should not happen */
error = errno;
error = SOCKERRNO;
logmsg("wait_ms() failed with error (%d) %s",
error, strerror(error));
error, sstrerror(error));
break;
}
}

View file

@ -48,7 +48,6 @@
#include "curlx.h" /* from the private lib dir */
#include "getpart.h"
#include "util.h"
#include "timediff.h"
/* need init from main() */
const char *pidname = NULL;
@ -248,19 +247,6 @@ FILE *test2fopen(long testno, const char *logdir2)
return stream;
}
#if !defined(MSDOS) && !defined(USE_WINSOCK)
static long timediff(struct timeval newer, struct timeval older)
{
timediff_t diff = newer.tv_sec-older.tv_sec;
if(diff >= (LONG_MAX/1000))
return LONG_MAX;
else if(diff <= (LONG_MIN/1000))
return LONG_MIN;
return (long)(newer.tv_sec-older.tv_sec)*1000+
(long)(newer.tv_usec-older.tv_usec)/1000;
}
#endif
/*
* Portable function used for waiting a specific amount of ms.
* Waiting indefinitely with this function is not allowed, a
@ -270,45 +256,41 @@ static long timediff(struct timeval newer, struct timeval older)
* -1 = system call error, or invalid timeout value
* 0 = specified timeout has elapsed
*/
int wait_ms(int timeout_ms)
int wait_ms(timediff_t timeout_ms)
{
int r = 0;
if(!timeout_ms)
return 0;
if(timeout_ms < 0) {
CURL_SETERRNO(EINVAL);
SET_SOCKERRNO(SOCKEINVAL);
return -1;
}
#ifdef MSDOS
delay(timeout_ms);
#elif defined(USE_WINSOCK)
#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;
struct timeval initial_tv = tvnow();
int pending_ms = timeout_ms;
do {
int error;
pending_tv.tv_sec = pending_ms / 1000;
pending_tv.tv_usec = (pending_ms % 1000) * 1000;
r = select(0, NULL, NULL, NULL, &pending_tv);
if(r != -1)
break;
error = errno;
if(error && (error != SOCKEINTR))
break;
pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
if(pending_ms <= 0)
break;
} while(r == -1);
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;
}
#endif /* USE_WINSOCK */
if(r)
r = -1;
return r;
}

View file

@ -87,7 +87,9 @@ const char *sstrerror(int err);
/* fopens the test case file */
FILE *test2fopen(long testno, const char *logdir);
int wait_ms(int timeout_ms);
#include "timediff.h"
int wait_ms(timediff_t timeout_ms);
curl_off_t our_getpid(void);
int write_pidfile(const char *filename);
int write_portfile(const char *filename, int port);