mirror of
https://github.com/curl/curl.git
synced 2026-04-14 22:01:41 +03:00
Before this patch, standard `E*` errno codes were redefined on Windows,
onto matching winsock2 `WSA*` error codes, which have different values.
This broke uses where using the `E*` value in non-socket context, or
other places expecting a POSIX `errno`, e.g. file I/O, threads, IDN or
interfacing with dependencies.
Fix it by introducing a curl-specific `SOCKE*` set of macros that map to
`WSA*` on Windows and standard POSIX codes on other platforms. Then
verify and update the code to use `SOCKE*` or `E*` macro depending on
context.
- Add `SOCKE*` macros that map to either winsock2 or POSIX error codes.
And use them with `SOCKERRNO` or in contexts requiring
platform-dependent socket error codes.
This fixes `E*` uses which were supposed be POSIX values, not `WSA*`
socket errors, on Windows:
- lib/curl_multibyte.c
- lib/curl_threads.c
- lib/idn.c
- lib/vtls/gtls.c
- lib/vtls/rustls.c
- src/tool_cb_wrt.c
- src/tool_dirhie.c
- Ban `E*` codes having a `SOCKE*` mapping, via checksrc.
Authored-by: Daniel Stenberg
- Add exceptions for `E*` codes used in file I/O, or other contexts
requiring POSIX error codes.
Also:
- ftp: fix missing `SOCKEACCES` mapping for Windows.
- add `SOCKENOMEM` for `Curl_getaddrinfo()` via `asyn-thread.c`.
- tests/server/sockfilt: fix to set `SOCKERRNO` in local `select()`
override on Windows.
- lib/inet_ntop: fix to return `WSAEINVAL` on Windows, where `ENOSPC` is
used on other platforms. To simulate Windows' built-in `inet_ntop()`,
as tested on a Win10 machine.
Note:
- WINE returns `STATUS_INVALID_PARAMETER` = `0xC000000D`.
- Microsoft documentation says it returns `WSA_INVALID_PARAMETER`
(= `ERROR_INVALID_PARAMETER`) 87:
https://learn.microsoft.com/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_ntop#return-value
- lib/inet_ntop: drop redundant `CURL_SETERRNO(ENOSPC)`.
`inet_ntop4()` already sets it before returning `NULL`.
- replace stray `WSAEWOULDBLOCK` with `USE_WINSOCK` macro to detect
winsock2.
- move existing `SOCKE*` mappings from `tests/server` to
`curl_setup_once.h`.
- add missing `EINTR`, `EINVAL` constants for WinCE.
Follow-up to abf80aae38 #16612
Follow-up to d69425ed7d #16615
Bug: https://github.com/curl/curl/pull/16553#issuecomment-2704679377
Closes #16621
163 lines
3.9 KiB
C
163 lines
3.9 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#if defined(USE_THREADS_POSIX)
|
|
# ifdef HAVE_PTHREAD_H
|
|
# include <pthread.h>
|
|
# endif
|
|
#elif defined(USE_THREADS_WIN32)
|
|
# include <process.h>
|
|
#endif
|
|
|
|
#include "curl_threads.h"
|
|
#ifdef BUILDING_LIBCURL
|
|
#include "curl_memory.h"
|
|
#endif
|
|
/* The last #include file should be: */
|
|
#include "memdebug.h"
|
|
|
|
#if defined(USE_THREADS_POSIX)
|
|
|
|
struct Curl_actual_call {
|
|
unsigned int (*func)(void *);
|
|
void *arg;
|
|
};
|
|
|
|
static void *curl_thread_create_thunk(void *arg)
|
|
{
|
|
struct Curl_actual_call *ac = arg;
|
|
unsigned int (*func)(void *) = ac->func;
|
|
void *real_arg = ac->arg;
|
|
|
|
free(ac);
|
|
|
|
(*func)(real_arg);
|
|
|
|
return 0;
|
|
}
|
|
|
|
curl_thread_t Curl_thread_create(unsigned int (*func) (void *), void *arg)
|
|
{
|
|
curl_thread_t t = malloc(sizeof(pthread_t));
|
|
struct Curl_actual_call *ac = malloc(sizeof(struct Curl_actual_call));
|
|
if(!(ac && t))
|
|
goto err;
|
|
|
|
ac->func = func;
|
|
ac->arg = arg;
|
|
|
|
if(pthread_create(t, NULL, curl_thread_create_thunk, ac) != 0)
|
|
goto err;
|
|
|
|
return t;
|
|
|
|
err:
|
|
free(t);
|
|
free(ac);
|
|
return curl_thread_t_null;
|
|
}
|
|
|
|
void Curl_thread_destroy(curl_thread_t hnd)
|
|
{
|
|
if(hnd != curl_thread_t_null) {
|
|
pthread_detach(*hnd);
|
|
free(hnd);
|
|
}
|
|
}
|
|
|
|
int Curl_thread_join(curl_thread_t *hnd)
|
|
{
|
|
int ret = (pthread_join(**hnd, NULL) == 0);
|
|
|
|
free(*hnd);
|
|
*hnd = curl_thread_t_null;
|
|
|
|
return ret;
|
|
}
|
|
|
|
#elif defined(USE_THREADS_WIN32)
|
|
|
|
curl_thread_t Curl_thread_create(
|
|
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
|
|
DWORD
|
|
#else
|
|
unsigned int
|
|
#endif
|
|
(CURL_STDCALL *func) (void *),
|
|
void *arg)
|
|
{
|
|
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
|
|
typedef HANDLE curl_win_thread_handle_t;
|
|
#else
|
|
typedef uintptr_t curl_win_thread_handle_t;
|
|
#endif
|
|
curl_thread_t t;
|
|
curl_win_thread_handle_t thread_handle;
|
|
#if defined(CURL_WINDOWS_UWP) || defined(UNDER_CE)
|
|
thread_handle = CreateThread(NULL, 0, func, arg, 0, NULL);
|
|
#else
|
|
thread_handle = _beginthreadex(NULL, 0, func, arg, 0, NULL);
|
|
#endif
|
|
t = (curl_thread_t)thread_handle;
|
|
if((t == 0) || (t == LongToHandle(-1L))) {
|
|
#ifdef UNDER_CE
|
|
DWORD gle = GetLastError();
|
|
/* !checksrc! disable ERRNOVAR 1 */
|
|
int err = (gle == ERROR_ACCESS_DENIED ||
|
|
gle == ERROR_NOT_ENOUGH_MEMORY) ?
|
|
EACCES : EINVAL;
|
|
CURL_SETERRNO(err);
|
|
#endif
|
|
return curl_thread_t_null;
|
|
}
|
|
return t;
|
|
}
|
|
|
|
void Curl_thread_destroy(curl_thread_t hnd)
|
|
{
|
|
if(hnd != curl_thread_t_null)
|
|
CloseHandle(hnd);
|
|
}
|
|
|
|
int Curl_thread_join(curl_thread_t *hnd)
|
|
{
|
|
#if !defined(_WIN32_WINNT) || !defined(_WIN32_WINNT_VISTA) || \
|
|
(_WIN32_WINNT < _WIN32_WINNT_VISTA)
|
|
int ret = (WaitForSingleObject(*hnd, INFINITE) == WAIT_OBJECT_0);
|
|
#else
|
|
int ret = (WaitForSingleObjectEx(*hnd, INFINITE, FALSE) == WAIT_OBJECT_0);
|
|
#endif
|
|
|
|
Curl_thread_destroy(*hnd);
|
|
|
|
*hnd = curl_thread_t_null;
|
|
|
|
return ret;
|
|
}
|
|
|
|
#endif /* USE_THREADS_* */
|