]> git.sur5r.net Git - openldap/commitdiff
ITS#4606 errno is not per-thread on WIN32, always use WSAGet/SetLastError
authorHoward Chu <hyc@openldap.org>
Thu, 14 Sep 2006 06:35:34 +0000 (06:35 +0000)
committerHoward Chu <hyc@openldap.org>
Thu, 14 Sep 2006 06:35:34 +0000 (06:35 +0000)
(with notable exceptions: ignore tests for EINTR which winsock never sets)

configure
include/ac/socket.h
libraries/liblber/io.c
libraries/liblber/sockbuf.c
libraries/libldap/cyrus.c
libraries/libldap/os-ip.c
libraries/libldap/request.c
libraries/libldap/result.c
libraries/libldap/tls.c
servers/slapd/connection.c
servers/slapd/result.c

index 83f1233fd80dfe2c70657dd25eb3364fb92a4dfe..2657cd8e1cff8542944c883cdcef7c35673ef1c2 100755 (executable)
--- a/configure
+++ b/configure
@@ -16032,7 +16032,7 @@ cat >>conftest.$ac_ext <<_ACEOF
 #include <stdio.h>
 #include <sys/types.h>
 #include <errno.h>
-#ifdef WINNT
+#ifdef _WIN32
 #include <stdlib.h>
 #endif
 int
@@ -16397,7 +16397,7 @@ cat >>conftest.$ac_ext <<_ACEOF
 #include <stdio.h>
 #include <sys/types.h>
 #include <errno.h>
-#ifdef WINNT
+#ifdef _WIN32
 #include <stdlib.h>
 #endif
 int
index 5aac4fe44831c784b12669c1bdcc1f53c65cf6a6..384a80aa3b71e313b310f63d5453b203508d813d 100644 (file)
@@ -80,6 +80,7 @@
 #undef sock_errstr
 #define sock_errno()   errno
 #define sock_errstr(e) STRERROR(e)
+#define sock_errset(e) errno = (e)
 
 #ifdef HAVE_WINSOCK
 #      define tcp_read( s, buf, len )  recv( s, buf, len, 0 )
 #undef sock_errstr
 #define        sock_errno()    WSAGetLastError()
 #define        sock_errstr(e)  ber_pvt_wsa_err2string(e)
+#define        sock_errset(e)  WSASetLastError(e)
 
 LBER_F( char * ) ber_pvt_wsa_err2string LDAP_P((int));
 
index 730d05d4fdf350fb05432031bf917d754b3f2060..b697acee1fb008ef1c0fe6ffa0cabe3616ca9332 100644 (file)
@@ -509,7 +509,7 @@ ber_get_next(
                char buf[sizeof(ber->ber_len)-1];
                ber_len_t tlen = 0;
 
-               errno = 0;
+               sock_errset(0);
                sblen=ber_int_sb_read( sb, ber->ber_rwptr,
                        ((char *)&ber->ber_len + LENSIZE*2 - 1)-ber->ber_rwptr);
                if (sblen<=0) return LBER_DEFAULT;
@@ -529,16 +529,16 @@ ber_get_next(
                                                break;
                                        /* Is the tag too big? */
                                        if (i == sizeof(ber_tag_t)-1) {
-                                               errno = ERANGE;
+                                               sock_errset(ERANGE);
                                                return LBER_DEFAULT;
                                        }
                                }
                                /* Did we run out of bytes? */
                                if ((char *)p == ber->ber_rwptr) {
 #if defined( EWOULDBLOCK )
-                                       errno = EWOULDBLOCK;
+                                       sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-                                       errno = EAGAIN;
+                                       sock_errset(EAGAIN);
 #endif                 
                                        return LBER_DEFAULT;
                                }
@@ -549,9 +549,9 @@ ber_get_next(
 
                if ( ber->ber_ptr == ber->ber_rwptr ) {
 #if defined( EWOULDBLOCK )
-                       errno = EWOULDBLOCK;
+                       sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-                       errno = EAGAIN;
+                       sock_errset(EAGAIN);
 #endif                 
                        return LBER_DEFAULT;
                }
@@ -562,15 +562,15 @@ ber_get_next(
                        unsigned char *p = (unsigned char *)ber->ber_ptr;
                        int llen = *p++ & 0x7f;
                        if (llen > (int)sizeof(ber_len_t)) {
-                               errno = ERANGE;
+                               sock_errset(ERANGE);
                                return LBER_DEFAULT;
                        }
                        /* Not enough bytes? */
                        if (ber->ber_rwptr - (char *)p < llen) {
 #if defined( EWOULDBLOCK )
-                               errno = EWOULDBLOCK;
+                               sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-                               errno = EAGAIN;
+                               sock_errset(EAGAIN);
 #endif                 
                                return LBER_DEFAULT;
                        }
@@ -601,7 +601,7 @@ ber_get_next(
 
                /* make sure length is reasonable */
                if ( ber->ber_len == 0 ) {
-                       errno = ERANGE;
+                       sock_errset(ERANGE);
                        return LBER_DEFAULT;
                }
 
@@ -609,7 +609,7 @@ ber_get_next(
                        ber_log_printf( LDAP_DEBUG_CONNS, ber->ber_debug,
                                "ber_get_next: sockbuf_max_incoming exceeded "
                                "(%ld > %ld)\n", ber->ber_len, sb->sb_max_incoming );
-                       errno = ERANGE;
+                       sock_errset(ERANGE);
                        return LBER_DEFAULT;
                }
 
@@ -620,7 +620,7 @@ ber_get_next(
                         * already read.
                         */
                        if ( ber->ber_len < sblen + l ) {
-                               errno = ERANGE;
+                               sock_errset(ERANGE);
                                return LBER_DEFAULT;
                        }
                        ber->ber_buf = (char *) ber_memalloc_x( ber->ber_len + 1, ber->ber_memctx );
@@ -652,16 +652,16 @@ ber_get_next(
                to_go = ber->ber_end - ber->ber_rwptr;
                assert( to_go > 0 );
                
-               errno = 0;
+               sock_errset(0);
                res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
                if (res<=0) return LBER_DEFAULT;
                ber->ber_rwptr+=res;
                
                if (res<to_go) {
 #if defined( EWOULDBLOCK )
-                       errno = EWOULDBLOCK;
+                       sock_errset(EWOULDBLOCK);
 #elif defined( EAGAIN )
-                       errno = EAGAIN;
+                       sock_errset(EAGAIN);
 #endif                 
                        return LBER_DEFAULT;
                }
index 956de5971fd6e59556bba4f2f59cba01b383d960..27ed84e46653db056e80cc0e8dc2ecaa86bc1fb6 100644 (file)
@@ -467,22 +467,7 @@ sb_stream_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
 /*
  * 32-bit Windows Socket API (under Windows NT or Windows 95)
  */
-       {
-               int rc;
-
-               rc = recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
-
-#ifdef HAVE_WINSOCK
-               if ( rc < 0 ) {
-                       int err;
-
-                       err = WSAGetLastError();
-                       errno = err;
-               }
-#endif
-
-               return rc;
-       }
+       return recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
 
 #elif defined( HAVE_NCSA )
 /*
@@ -520,18 +505,7 @@ sb_stream_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
 /*
  * 32-bit Windows Socket API (under Windows NT or Windows 95)
  */
-       {
-               int rc = send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
-
-#ifdef HAVE_WINSOCK
-               if ( rc < 0 ) {
-                       int err;
-                       err = WSAGetLastError();
-                       errno = err;
-               }
-#endif
-               return rc;
-       }
+       return send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
 
 #elif defined(HAVE_NCSA)
        return netwrite( sbiod->sbiod_sb->sb_fd, buf, len );
@@ -820,11 +794,11 @@ sb_debug_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
 
        ret = LBER_SBIOD_READ_NEXT( sbiod, buf, len );
        if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
-               int err = errno;
+               int err = sock_errno();
                if ( ret < 0 ) {
                        ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
                                "%sread: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt,
-                               (long)len, AC_STRERROR_R( errno, ebuf, sizeof ebuf ) );
+                               (long)len, AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
                } else {
                        ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
                                "%sread: want=%ld, got=%ld\n", (char *)sbiod->sbiod_pvt,
@@ -832,7 +806,7 @@ sb_debug_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
                        ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
                                (const char *)buf, ret );
                }
-               errno = err;
+               sock_errset(err);
        }
        return ret;
 }
@@ -845,12 +819,12 @@ sb_debug_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
 
        ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
        if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
-               int err = errno;
+               int err = sock_errno();
                if ( ret < 0 ) {
                        ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
                                "%swrite: want=%ld error=%s\n",
                                (char *)sbiod->sbiod_pvt, (long)len,
-                               AC_STRERROR_R( errno, ebuf, sizeof ebuf ) );
+                               AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
                } else {
                        ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
                                "%swrite: want=%ld, written=%ld\n",
@@ -858,7 +832,7 @@ sb_debug_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
                        ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
                                (const char *)buf, ret );
                }
-               errno = err;
+               sock_errset(err);
        }
 
        return ret;
index 936f44d35b53e33b3faa9bb6e0250065a30375c1..52ade28cac059bef59924fdbd1eff9f71dfee2b1 100644 (file)
@@ -161,7 +161,7 @@ sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
        ber_pvt_sb_buf_init( &p->buf_out );
        if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
                LBER_FREE( p );
-               errno = ENOMEM;
+               sock_errset(ENOMEM);
                return -1;
        }
        sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
@@ -288,7 +288,7 @@ sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
        if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) && 
                ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
        {
-               errno = ENOMEM;
+               sock_errset(ENOMEM);
                return -1;
        }
        p->sec_buf_in.buf_end = ret;
@@ -327,7 +327,7 @@ sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
                ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
                        "sb_sasl_read: failed to decode packet: %s\n",
                        sasl_errstring( ret, NULL, NULL ) );
-               errno = EIO;
+               sock_errset(EIO);
                return -1;
        }
        
@@ -356,7 +356,7 @@ sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
 
                /* Still have something left?? */
                if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
-                       errno = EAGAIN;
+                       sock_errset(EAGAIN);
                        return -1;
                }
        }
@@ -383,7 +383,7 @@ sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
                ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
                        "sb_sasl_write: failed to encode packet: %s\n",
                        sasl_errstring( ret, NULL, NULL ) );
-               errno = EIO;
+               sock_errset(EIO);
                return -1;
        }
        p->buf_out.buf_end = p->buf_out.buf_size;
index 3f428ae9807cdf5752e5c42e4d183d83db51e64d..9f85e800ac23a6bbf1a93a13f48098ec3f23a7d3 100644 (file)
@@ -63,7 +63,7 @@ do { \
 static void
 ldap_pvt_set_errno(int err)
 {
-       errno = err;
+       sock_errset(err);
 }
 
 int
@@ -208,7 +208,7 @@ ldap_pvt_connect(LDAP *ld, ber_socket_t s,
        struct sockaddr *sin, socklen_t addrlen,
        int async)
 {
-       int rc;
+       int rc, err;
        struct timeval  tv = { 0 },
                        *opt_tv = NULL;
 
@@ -241,11 +241,8 @@ ldap_pvt_connect(LDAP *ld, ber_socket_t s,
                return ( 0 );
        }
 
-#ifdef HAVE_WINSOCK
-       ldap_pvt_set_errno( WSAGetLastError() );
-#endif
-
-       if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
+       err = sock_errno();
+       if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
                return ( -1 );
        }
        
index 219d2c0b0186fe963074e379370aa9439d4901d1..1357707c27085311df31f339fcf222371e2b9c39 100644 (file)
@@ -150,7 +150,7 @@ ldap_int_flush_request(
        LDAPConn *lc = lr->lr_conn;
 
        if ( ber_flush2( lc->lconn_sb, lr->lr_ber, LBER_FLUSH_FREE_NEVER ) != 0 ) {
-               if ( errno == EAGAIN ) {
+               if ( sock_errno() == EAGAIN ) {
                        /* need to continue write later */
                        lr->lr_status = LDAP_REQST_WRITING;
                        ldap_mark_select_write( ld, lc->lconn_sb );
index 0d9a429fd126367448a4613090eb9d066183c55e..1720b85df9b944401cb863b28968a18318f2b3d4 100644 (file)
@@ -322,13 +322,13 @@ wait4msg(
                                if ( rc == -1 ) {
                                        Debug( LDAP_DEBUG_TRACE,
                                                "ldap_int_select returned -1: errno %d\n",
-                                               errno, 0, 0 );
+                                               sock_errno(), 0, 0 );
                                }
 #endif
 
                                if ( rc == 0 || ( rc == -1 && (
                                        !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
-                                               || errno != EINTR ) ) )
+                                               || sock_errno() != EINTR ) ) )
                                {
                                        ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
                                                LDAP_TIMEOUT);
@@ -471,7 +471,7 @@ retry:
        assert( LBER_VALID (ber) );
 
        /* get the next message */
-       errno = 0;
+       sock_errset(0);
 #ifdef LDAP_CONNECTIONLESS
        if ( LDAP_IS_UDP(ld) ) {
                struct sockaddr from;
@@ -496,10 +496,10 @@ nextresp3:
                        "ber_get_next failed.\n", 0, 0, 0 );
 #endif            
 #ifdef EWOULDBLOCK                     
-               if ( errno == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
+               if ( sock_errno() == EWOULDBLOCK ) return LDAP_MSG_X_KEEP_LOOKING;
 #endif
 #ifdef EAGAIN
-               if ( errno == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
+               if ( sock_errno() == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
 #endif
                ld->ld_errno = LDAP_SERVER_DOWN;
                return -1;
index fc2e31d55a7b57506916069d547d1dad91eb0614..49cd1845959bc0a0972e4ebd128a3f6f30f5842e 100644 (file)
@@ -594,7 +594,7 @@ sb_tls_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
        err = SSL_get_error( p->ssl, ret );
        if (err == SSL_ERROR_WANT_READ ) {
                sbiod->sbiod_sb->sb_trans_needs_read = 1;
-               errno = EWOULDBLOCK;
+               sock_errset(EWOULDBLOCK);
        }
        else
                sbiod->sbiod_sb->sb_trans_needs_read = 0;
@@ -620,7 +620,7 @@ sb_tls_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
        err = SSL_get_error( p->ssl, ret );
        if (err == SSL_ERROR_WANT_WRITE ) {
                sbiod->sbiod_sb->sb_trans_needs_write = 1;
-               errno = EWOULDBLOCK;
+               sock_errset(EWOULDBLOCK);
 
        } else {
                sbiod->sbiod_sb->sb_trans_needs_write = 0;
@@ -676,7 +676,7 @@ sb_tls_bio_read( BIO *b, char *buf, int len )
 
        BIO_clear_retry_flags( b );
        if ( ret < 0 ) {
-               int err = errno;
+               int err = sock_errno();
                if ( err == EAGAIN || err == EWOULDBLOCK ) {
                        BIO_set_retry_read( b );
                }
@@ -703,7 +703,7 @@ sb_tls_bio_write( BIO *b, const char *buf, int len )
 
        BIO_clear_retry_flags( b );
        if ( ret < 0 ) {
-               int err = errno;
+               int err = sock_errno();
                if ( err == EAGAIN || err == EWOULDBLOCK ) {
                        BIO_set_retry_write( b );
                }
index 6afc81bbc6d3bfeb961f5b5960105ac8fba8c19d..5311091545826819ddcb87ca47500d5a93340344 100644 (file)
@@ -1483,7 +1483,7 @@ connection_input( Connection *conn )
                return -1;
        }
 
-       errno = 0;
+       sock_errset(0);
 
 #ifdef LDAP_CONNECTIONLESS
        if ( conn->c_is_udp ) {
@@ -1503,7 +1503,7 @@ connection_input( Connection *conn )
 
        tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
        if ( tag != LDAP_TAG_MESSAGE ) {
-               int err = errno;
+               int err = sock_errno();
                ber_socket_t    sd;
 
                ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
index c6ec83615941993bf1cfbc168d37c9b6d677bdc5..0cde942a2cf0b691a189d91f5879bed597c717c5 100644 (file)
@@ -170,7 +170,7 @@ static long send_ldap_ber(
                        break;
                }
 
-               err = errno;
+               err = sock_errno();
 
                /*
                 * we got an error.  if it's ewouldblock, we need to