From: Howard Chu Date: Wed, 12 Oct 2005 00:05:43 +0000 (+0000) Subject: Fix ITS#3832 must mutex protect ld->ld_conns X-Git-Tag: OPENLDAP_REL_ENG_2_2_MP~290 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=e8b1b004d4cdbb77bfbe563197585f55466cc895;p=openldap Fix ITS#3832 must mutex protect ld->ld_conns --- diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index d50da9370d..ba6e98a6dc 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -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 diff --git a/libraries/libldap/open.c b/libraries/libldap/open.c index dd97afa22f..eaa6a6cd1f 100644 --- a/libraries/libldap/open.c +++ b/libraries/libldap/open.c @@ -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; diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c index 184d37d7a3..93a98e5656 100644 --- a/libraries/libldap/request.c +++ b/libraries/libldap/request.c @@ -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 ) { diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c index fa36b83651..2a0a0d2a16 100644 --- a/libraries/libldap/result.c +++ b/libraries/libldap/result.c @@ -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 } } }