From: Kern Sibbald Date: Sun, 23 Oct 2011 14:13:23 +0000 (+0200) Subject: Fix to Windows socket error detection -- should fix bug #1770 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=4b32662debab0904d57a2a308eebcea42a7ba6ca;hp=da7ba05c038b350acef181da4f1a79b9eb863bd8;p=bacula%2Fbacula Fix to Windows socket error detection -- should fix bug #1770 --- diff --git a/bacula/src/lib/bnet.c b/bacula/src/lib/bnet.c index 49fda5b144..a52c6a70cb 100644 --- a/bacula/src/lib/bnet.c +++ b/bacula/src/lib/bnet.c @@ -79,8 +79,27 @@ int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes) errno = 0; nread = socketRead(bsock->m_fd, ptr, nleft); if (bsock->is_timed_out() || bsock->is_terminated()) { - return nread; + return -1; } + +#ifdef HAVE_WIN32 + /* + * For Windows, we must simulate Unix erro on a socket + * error in order to handle errors correctly. + */ + if (nread == SOCKET_ERROR) { + DWORD err = WSAGetLastError(); + nread = -1; + if (err == WSAEINTR) { + errno = EINTR; + } else if (err == WSAEWOULDBLOCK) { + errno = EAGAIN; + } else { + errno = EIO; /* some other error */ + } + } +#endif + if (nread == -1) { if (errno == EINTR) { continue; @@ -91,7 +110,7 @@ int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes) } } if (nread <= 0) { - return nread; /* error, or EOF */ + return -1; /* error, or EOF */ } nleft -= nread; ptr += nread; @@ -135,8 +154,27 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes) errno = 0; nwritten = socketWrite(bsock->m_fd, ptr, nleft); if (bsock->is_timed_out() || bsock->is_terminated()) { - return nwritten; + return -1; } + +#ifdef HAVE_WIN32 + /* + * For Windows, we must simulate Unix erro on a socket + * error in order to handle errors correctly. + */ + if (nwritten == SOCKET_ERROR) { + DWORD err = WSAGetLastError(); + nwritten = -1; + if (err == WSAEINTR) { + errno = EINTR; + } else if (err == WSAEWOULDBLOCK) { + errno = EAGAIN; + } else { + errno = EIO; /* some other error */ + } + } +#endif + } while (nwritten == -1 && errno == EINTR); /* * If connection is non-blocking, we will get EAGAIN, so @@ -155,7 +193,7 @@ int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes) continue; } if (nwritten <= 0) { - return nwritten; /* error */ + return -1; /* error */ } nleft -= nwritten; ptr += nwritten;