Am 19.05.2014 22:29, schrieb Erik Faye-Lund: >> [...] >> Would we need to wrap both ends, shouldn't wrapping only reading be >> good enough to prevent deadlocking? >> >> compat/poll/poll.c already contains a function called IsSocketHandle >> that is able to tell if a HANDLE points to a socket or not. > > This very quick attempt did not work out :( > > diff --git a/compat/mingw.c b/compat/mingw.c > index 0335958..ec1d81f 100644 > --- a/compat/mingw.c > +++ b/compat/mingw.c > @@ -370,6 +370,65 @@ int mingw_open (const char *filename, int oflags, ...) > return fd; > } > > +#define is_console_handle(h) (((long) (h) & 3) == 3) > + > +static int is_socket_handle(HANDLE h) > +{ > + WSANETWORKEVENTS ev; > + > + if (is_console_handle(h)) > + return 0; > + > + /* > + * Under Wine, it seems that getsockopt returns 0 for pipes too. > + * WSAEnumNetworkEvents instead distinguishes the two correctly. > + */ > + ev.lNetworkEvents = 0xDEADBEEF; > + WSAEnumNetworkEvents((SOCKET) h, NULL, &ev); > + return ev.lNetworkEvents != 0xDEADBEEF; > +} > + > +#undef read > +ssize_t mingw_read(int fd, void *buf, size_t count) > +{ > + int ret; > + HANDLE fh = (HANDLE)_get_osfhandle(fd); > + > + if (fh == INVALID_HANDLE_VALUE) { > + errno = EBADF; > + return -1; > + } > + > + if (!is_socket_handle(fh)) > + return read(fd, buf, count); > + > + ret = recv((SOCKET)fh, buf, count, 0); > + if (ret < 0) > + errno = WSAGetLastError(); > + return ret; > +} > + > +#undef write > +ssize_t mingw_write(int fd, const void *buf, size_t count) > +{ > + int ret; > + HANDLE fh = (HANDLE)_get_osfhandle(fd); > + > + if (fh == INVALID_HANDLE_VALUE) { > + errno = EBADF; > + return -1; > + } > + > + if (!is_socket_handle(fh)) > + return write(fd, buf, count); > + > + return send((SOCKET)fh, buf, count, 0); > + if (ret < 0) > + errno = WSAGetLastError(); > + return ret; > +} > + > + > static BOOL WINAPI ctrl_ignore(DWORD type) > { > return TRUE; > diff --git a/compat/mingw.h b/compat/mingw.h > index 08b83fe..1690098 100644 > --- a/compat/mingw.h > +++ b/compat/mingw.h > @@ -177,6 +177,12 @@ int mingw_rmdir(const char *path); > int mingw_open (const char *filename, int oflags, ...); > #define open mingw_open > > +ssize_t mingw_read(int fd, void *buf, size_t count); > +#define read mingw_read > + > +ssize_t mingw_write(int fd, const void *buf, size_t count); > +#define write mingw_write > + > int mingw_fgetc(FILE *stream); > #define fgetc mingw_fgetc According to [1] you also have to pass WSA_FLAG_OVERLAPPED to avoid the deadlock. With that change I don't get a hang anymore but a read error with errno 10054 aka WSAECONNRESET. [1]: https://groups.google.com/forum/#!msg/msysgit/at8D7J-h7mw/PM9w-d41cDYJ diff --git a/compat/mingw.c b/compat/mingw.c index 383cafe..4a58135 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -366,6 +366,71 @@ int mingw_open (const char *filename, int oflags, ...) return fd; } +#define is_console_handle(h) (((long) (h) & 3) == 3) + +static int is_socket_handle(HANDLE h) +{ + WSANETWORKEVENTS ev; + + if (is_console_handle(h)) + return 0; + + /* + * Under Wine, it seems that getsockopt returns 0 for pipes too. + * WSAEnumNetworkEvents instead distinguishes the two correctly. + */ + ev.lNetworkEvents = 0xDEADBEEF; + WSAEnumNetworkEvents((SOCKET) h, NULL, &ev); + return ev.lNetworkEvents != 0xDEADBEEF; +} + +#undef read +ssize_t mingw_read(int fd, void *buf, size_t count) +{ + int ret; + HANDLE fh = (HANDLE)_get_osfhandle(fd); + + if (fh == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + + if (!is_socket_handle(fh)) + return read(fd, buf, count); + + ret = recv((SOCKET)fh, buf, count, 0); + if (ret < 0) + { + errno = WSAGetLastError(); + warning("errno %d",errno); + } + return ret; +} + +#undef write +ssize_t mingw_write(int fd, const void *buf, size_t count) +{ + int ret; + HANDLE fh = (HANDLE)_get_osfhandle(fd); + + if (fh == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + + if (!is_socket_handle(fh)) + return write(fd, buf, count); + + ret = send((SOCKET)fh, buf, count, 0); + if (ret < 0) + { + errno = WSAGetLastError(); + warning("errno %d",errno); + } + return ret; +} + + static BOOL WINAPI ctrl_ignore(DWORD type) { return TRUE; @@ -1542,7 +1607,7 @@ int mingw_socket(int domain, int type, int protocol) SOCKET s; ensure_socket_initialization(); - s = WSASocket(domain, type, protocol, NULL, 0, 0); + s = WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED); if (s == INVALID_SOCKET) { /* * WSAGetLastError() values are regular BSD error codes diff --git a/compat/mingw.h b/compat/mingw.h index 08b83fe..1690098 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -177,6 +177,12 @@ int mingw_rmdir(const char *path); int mingw_open (const char *filename, int oflags, ...); #define open mingw_open +ssize_t mingw_read(int fd, void *buf, size_t count); +#define read mingw_read + +ssize_t mingw_write(int fd, const void *buf, size_t count); +#define write mingw_write + int mingw_fgetc(FILE *stream); #define fgetc mingw_fgetc -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html