]> git.sur5r.net Git - openldap/commitdiff
Fix ITS#3832 must mutex protect ld->ld_conns
authorHoward Chu <hyc@openldap.org>
Wed, 12 Oct 2005 00:05:43 +0000 (00:05 +0000)
committerHoward Chu <hyc@openldap.org>
Wed, 12 Oct 2005 00:05:43 +0000 (00:05 +0000)
libraries/libldap/ldap-int.h
libraries/libldap/open.c
libraries/libldap/request.c
libraries/libldap/result.c

index d50da9370dec3dfa66ac45135411d695db0d7ba7..ba6e98a6dc5b21f912e87076c73617b0dff8f09f 100644 (file)
@@ -334,6 +334,7 @@ struct ldap {
        LDAPMessage     *ld_responses;  /* list of outstanding responses */
 
 #ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_t ld_conn_mutex;
        ldap_pvt_thread_mutex_t ld_req_mutex;
        ldap_pvt_thread_mutex_t ld_res_mutex;
 #endif
index dd97afa22f5eb57c7c44356bc92a42e4e4b6c012..eaa6a6cd1f139e26c757fd4bd981f51e86bac5fb 100644 (file)
@@ -160,6 +160,7 @@ ldap_create( LDAP **ldp )
 #ifdef LDAP_R_COMPILE
        ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
        ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
+       ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
 #endif
        *ldp = ld;
        return LDAP_SUCCESS;
index 184d37d7a31c90c8de378bfa6812e3ca08fbffd4..93a98e5656bac9ea1801aac206e41f7f03b45554 100644 (file)
@@ -363,8 +363,14 @@ ldap_new_connection( LDAP *ld, LDAPURLDesc *srvlist, int use_ldsb,
        }
 
        lc->lconn_status = LDAP_CONNST_CONNECTED;
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
+#endif
        lc->lconn_next = ld->ld_conns;
        ld->ld_conns = lc;
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
 
        /*
         * XXX for now, we always do a synchronous bind.  This will have
@@ -458,7 +464,11 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
        LDAPConn        *lc;
        LDAPURLDesc     *lcu, *lsu;
        int lcu_port, lsu_port;
+       int found = 0;
 
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
+#endif
        for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
                lcu = lc->lconn_server;
                lcu_port = ldap_pvt_url_scheme_port( lcu->lud_scheme,
@@ -468,20 +478,25 @@ find_connection( LDAP *ld, LDAPURLDesc *srv, int any )
                        lsu_port = ldap_pvt_url_scheme_port( lsu->lud_scheme,
                                lsu->lud_port );
 
-                       if ( strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
+                       if ( lsu_port == lcu_port
+                               && strcmp( lcu->lud_scheme, lsu->lud_scheme ) == 0
                                && lcu->lud_host != NULL && *lcu->lud_host != '\0'
                            && lsu->lud_host != NULL && *lsu->lud_host != '\0'
-                               && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0
-                           && lsu_port == lcu_port )
+                               && strcasecmp( lsu->lud_host, lcu->lud_host ) == 0 )
                        {
-                               return lc;
+                               found = 1;
+                               break;
                        }
 
                        if ( !any ) break;
                }
+               if ( found )
+                       break;
        }
-
-       return NULL;
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
+       return lc;
 }
 
 
@@ -519,6 +534,9 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                ldap_int_sasl_close( ld, lc );
 
                prevlc = NULL;
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
+#endif
                for ( tmplc = ld->ld_conns;
                        tmplc != NULL;
                        tmplc = tmplc->lconn_next )
@@ -533,6 +551,9 @@ ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
                        }
                        prevlc = tmplc;
                }
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
                ldap_free_urllist( lc->lconn_server );
 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
                if ( lc->lconn_krbinstance != NULL ) {
index fa36b83651fae4a0a80b9f081e4e9007c3fa41c0..2a0a0d2a16b7777285e16aba82a5288f159fed45 100644 (file)
@@ -272,16 +272,28 @@ wait4msg(
                } else {
                        int lc_ready = 0;
 
+#ifdef LDAP_R_COMPILE
+                       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
+#endif
                        for ( lc = ld->ld_conns; lc != NULL; lc = nextlc ) {
                                nextlc = lc->lconn_next;
                                if ( ber_sockbuf_ctrl( lc->lconn_sb,
                                                LBER_SB_OPT_DATA_READY, NULL ) ) {
+#ifdef LDAP_R_COMPILE
+                                       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
                                        rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
                                                &lc, result );
+#ifdef LDAP_R_COMPILE
+                                       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
+#endif
                                        lc_ready = 1;
                                        break;
                                }
-                       }
+                       }
+#ifdef LDAP_R_COMPILE
+                       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
 
                        if ( !lc_ready ) {
                                rc = ldap_int_select( ld, tvp );
@@ -318,6 +330,7 @@ wait4msg(
                                        }
 #ifdef LDAP_R_COMPILE
                                        ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
+                                       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
 #endif
                                        for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
                                                lc = nextlc )
@@ -326,11 +339,20 @@ wait4msg(
                                                if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
                                                        ldap_is_read_ready( ld, lc->lconn_sb ))
                                                {
+#ifdef LDAP_R_COMPILE
+                                                       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
                                                        rc = try_read1msg( ld, msgid, all,
                                                                lc->lconn_sb, &lc, result );
                                                                if ( lc == NULL ) lc = nextlc;
+#ifdef LDAP_R_COMPILE
+                                                       ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
+#endif
                                                }
                                        }
+#ifdef LDAP_R_COMPILE
+                                       ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
+#endif
                                }
                        }
                }