Skip to content

Commit 4e2e131

Browse files
committed
mingw: make sure errno is set correctly when socket operations fail
The winsock2 library provides functions that work on different data types than file descriptors, therefore we wrap them. But that is not the only difference: they also do not set `errno` but expect the callers to enquire about errors via `WSAGetLastError()`. Let's translate that into appropriate `errno` values whenever the socket operations fail so that Git's code base does not have to change its expectations. This closes #2404 Helped-by: Jeff Hostetler <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 3088b34 commit 4e2e131

File tree

1 file changed

+147
-10
lines changed

1 file changed

+147
-10
lines changed

compat/mingw.c

Lines changed: 147 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,26 +2042,158 @@ static void ensure_socket_initialization(void)
20422042
initialized = 1;
20432043
}
20442044

2045+
static int winsock_error_to_errno(DWORD err)
2046+
{
2047+
switch (err) {
2048+
case WSAEINTR: return EINTR;
2049+
case WSAEBADF: return EBADF;
2050+
case WSAEACCES: return EACCES;
2051+
case WSAEFAULT: return EFAULT;
2052+
case WSAEINVAL: return EINVAL;
2053+
case WSAEMFILE: return EMFILE;
2054+
case WSAEWOULDBLOCK: return EWOULDBLOCK;
2055+
case WSAEINPROGRESS: return EINPROGRESS;
2056+
case WSAEALREADY: return EALREADY;
2057+
case WSAENOTSOCK: return ENOTSOCK;
2058+
case WSAEDESTADDRREQ: return EDESTADDRREQ;
2059+
case WSAEMSGSIZE: return EMSGSIZE;
2060+
case WSAEPROTOTYPE: return EPROTOTYPE;
2061+
case WSAENOPROTOOPT: return ENOPROTOOPT;
2062+
case WSAEPROTONOSUPPORT: return EPROTONOSUPPORT;
2063+
case WSAEOPNOTSUPP: return EOPNOTSUPP;
2064+
case WSAEAFNOSUPPORT: return EAFNOSUPPORT;
2065+
case WSAEADDRINUSE: return EADDRINUSE;
2066+
case WSAEADDRNOTAVAIL: return EADDRNOTAVAIL;
2067+
case WSAENETDOWN: return ENETDOWN;
2068+
case WSAENETUNREACH: return ENETUNREACH;
2069+
case WSAENETRESET: return ENETRESET;
2070+
case WSAECONNABORTED: return ECONNABORTED;
2071+
case WSAECONNRESET: return ECONNRESET;
2072+
case WSAENOBUFS: return ENOBUFS;
2073+
case WSAEISCONN: return EISCONN;
2074+
case WSAENOTCONN: return ENOTCONN;
2075+
case WSAETIMEDOUT: return ETIMEDOUT;
2076+
case WSAECONNREFUSED: return ECONNREFUSED;
2077+
case WSAELOOP: return ELOOP;
2078+
case WSAENAMETOOLONG: return ENAMETOOLONG;
2079+
case WSAEHOSTUNREACH: return EHOSTUNREACH;
2080+
case WSAENOTEMPTY: return ENOTEMPTY;
2081+
/* No errno equivalent; default to EIO */
2082+
case WSAESOCKTNOSUPPORT:
2083+
case WSAEPFNOSUPPORT:
2084+
case WSAESHUTDOWN:
2085+
case WSAETOOMANYREFS:
2086+
case WSAEHOSTDOWN:
2087+
case WSAEPROCLIM:
2088+
case WSAEUSERS:
2089+
case WSAEDQUOT:
2090+
case WSAESTALE:
2091+
case WSAEREMOTE:
2092+
case WSASYSNOTREADY:
2093+
case WSAVERNOTSUPPORTED:
2094+
case WSANOTINITIALISED:
2095+
case WSAEDISCON:
2096+
case WSAENOMORE:
2097+
case WSAECANCELLED:
2098+
case WSAEINVALIDPROCTABLE:
2099+
case WSAEINVALIDPROVIDER:
2100+
case WSAEPROVIDERFAILEDINIT:
2101+
case WSASYSCALLFAILURE:
2102+
case WSASERVICE_NOT_FOUND:
2103+
case WSATYPE_NOT_FOUND:
2104+
case WSA_E_NO_MORE:
2105+
case WSA_E_CANCELLED:
2106+
case WSAEREFUSED:
2107+
case WSAHOST_NOT_FOUND:
2108+
case WSATRY_AGAIN:
2109+
case WSANO_RECOVERY:
2110+
case WSANO_DATA:
2111+
case WSA_QOS_RECEIVERS:
2112+
case WSA_QOS_SENDERS:
2113+
case WSA_QOS_NO_SENDERS:
2114+
case WSA_QOS_NO_RECEIVERS:
2115+
case WSA_QOS_REQUEST_CONFIRMED:
2116+
case WSA_QOS_ADMISSION_FAILURE:
2117+
case WSA_QOS_POLICY_FAILURE:
2118+
case WSA_QOS_BAD_STYLE:
2119+
case WSA_QOS_BAD_OBJECT:
2120+
case WSA_QOS_TRAFFIC_CTRL_ERROR:
2121+
case WSA_QOS_GENERIC_ERROR:
2122+
case WSA_QOS_ESERVICETYPE:
2123+
case WSA_QOS_EFLOWSPEC:
2124+
case WSA_QOS_EPROVSPECBUF:
2125+
case WSA_QOS_EFILTERSTYLE:
2126+
case WSA_QOS_EFILTERTYPE:
2127+
case WSA_QOS_EFILTERCOUNT:
2128+
case WSA_QOS_EOBJLENGTH:
2129+
case WSA_QOS_EFLOWCOUNT:
2130+
#ifndef _MSC_VER
2131+
case WSA_QOS_EUNKNOWNPSOBJ:
2132+
#endif
2133+
case WSA_QOS_EPOLICYOBJ:
2134+
case WSA_QOS_EFLOWDESC:
2135+
case WSA_QOS_EPSFLOWSPEC:
2136+
case WSA_QOS_EPSFILTERSPEC:
2137+
case WSA_QOS_ESDMODEOBJ:
2138+
case WSA_QOS_ESHAPERATEOBJ:
2139+
case WSA_QOS_RESERVED_PETYPE:
2140+
default: return EIO;
2141+
}
2142+
}
2143+
2144+
/*
2145+
* On Windows, `errno` is a global macro to a function call.
2146+
* This makes it difficult to debug and single-step our mappings.
2147+
*/
2148+
static inline void set_wsa_errno(void)
2149+
{
2150+
DWORD wsa = WSAGetLastError();
2151+
int e = winsock_error_to_errno(wsa);
2152+
errno = e;
2153+
2154+
#ifdef DEBUG_WSA_ERRNO
2155+
fprintf(stderr, "winsock error: %d -> %d\n", wsa, e);
2156+
fflush(stderr);
2157+
#endif
2158+
}
2159+
2160+
static inline int winsock_return(int ret)
2161+
{
2162+
if (ret < 0)
2163+
set_wsa_errno();
2164+
2165+
return ret;
2166+
}
2167+
2168+
#define WINSOCK_RETURN(x) do { return winsock_return(x); } while (0)
2169+
20452170
#undef gethostname
20462171
int mingw_gethostname(char *name, int namelen)
20472172
{
2048-
ensure_socket_initialization();
2049-
return gethostname(name, namelen);
2173+
ensure_socket_initialization();
2174+
WINSOCK_RETURN(gethostname(name, namelen));
20502175
}
20512176

20522177
#undef gethostbyname
20532178
struct hostent *mingw_gethostbyname(const char *host)
20542179
{
2180+
struct hostent *ret;
2181+
20552182
ensure_socket_initialization();
2056-
return gethostbyname(host);
2183+
2184+
ret = gethostbyname(host);
2185+
if (!ret)
2186+
set_wsa_errno();
2187+
2188+
return ret;
20572189
}
20582190

20592191
#undef getaddrinfo
20602192
int mingw_getaddrinfo(const char *node, const char *service,
20612193
const struct addrinfo *hints, struct addrinfo **res)
20622194
{
20632195
ensure_socket_initialization();
2064-
return getaddrinfo(node, service, hints, res);
2196+
WINSOCK_RETURN(getaddrinfo(node, service, hints, res));
20652197
}
20662198

20672199
int mingw_socket(int domain, int type, int protocol)
@@ -2081,7 +2213,7 @@ int mingw_socket(int domain, int type, int protocol)
20812213
* in errno so that _if_ someone looks up the code somewhere,
20822214
* then it is at least the number that are usually listed.
20832215
*/
2084-
errno = WSAGetLastError();
2216+
set_wsa_errno();
20852217
return -1;
20862218
}
20872219
/* convert into a file descriptor */
@@ -2097,35 +2229,35 @@ int mingw_socket(int domain, int type, int protocol)
20972229
int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz)
20982230
{
20992231
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
2100-
return connect(s, sa, sz);
2232+
WINSOCK_RETURN(connect(s, sa, sz));
21012233
}
21022234

21032235
#undef bind
21042236
int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz)
21052237
{
21062238
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
2107-
return bind(s, sa, sz);
2239+
WINSOCK_RETURN(bind(s, sa, sz));
21082240
}
21092241

21102242
#undef setsockopt
21112243
int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen)
21122244
{
21132245
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
2114-
return setsockopt(s, lvl, optname, (const char*)optval, optlen);
2246+
WINSOCK_RETURN(setsockopt(s, lvl, optname, (const char*)optval, optlen));
21152247
}
21162248

21172249
#undef shutdown
21182250
int mingw_shutdown(int sockfd, int how)
21192251
{
21202252
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
2121-
return shutdown(s, how);
2253+
WINSOCK_RETURN(shutdown(s, how));
21222254
}
21232255

21242256
#undef listen
21252257
int mingw_listen(int sockfd, int backlog)
21262258
{
21272259
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
2128-
return listen(s, backlog);
2260+
WINSOCK_RETURN(listen(s, backlog));
21292261
}
21302262

21312263
#undef accept
@@ -2136,6 +2268,11 @@ int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
21362268
SOCKET s1 = (SOCKET)_get_osfhandle(sockfd1);
21372269
SOCKET s2 = accept(s1, sa, sz);
21382270

2271+
if (s2 == INVALID_SOCKET) {
2272+
set_wsa_errno();
2273+
return -1;
2274+
}
2275+
21392276
/* convert into a file descriptor */
21402277
if ((sockfd2 = _open_osfhandle(s2, O_RDWR|O_BINARY)) < 0) {
21412278
int err = errno;

0 commit comments

Comments
 (0)