]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/connection.c
cleanup
[openldap] / servers / slapd / connection.c
index 30fe036880366a8ed63993aa7a98c180e30dca62..f067714cd8c88df9bbec81537856ada270d3f853 100644 (file)
@@ -656,6 +656,7 @@ connection_destroy( Connection *c )
        ber_socket_t    sd;
        unsigned long   connid;
        const char              *close_reason;
+       Sockbuf                 *sb;
 
        assert( connections != NULL );
        assert( c != NULL );
@@ -710,37 +711,43 @@ connection_destroy( Connection *c )
                c->c_currentber = NULL;
        }
 
-       ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
-       slapd_sd_lock();
-       ber_sockbuf_free( c->c_sb );
-       if ( sd != AC_SOCKET_INVALID ) {
-               slapd_remove( sd, 1, 0, 1 );
 
-               Statslog( LDAP_DEBUG_STATS, (close_reason
-                                                                        ? "conn=%lu fd=%ld closed (%s)\n"
-                                                                        : "conn=%lu fd=%ld closed\n"),
-                       connid, (long) sd, close_reason, 0, 0 );
-       } else {
-               slapd_sd_unlock();
+#ifdef LDAP_SLAPI
+       /* call destructors, then constructors; avoids unnecessary allocation */
+       if ( slapi_plugins_used ) {
+               slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
        }
+#endif
 
-       c->c_sb = ber_sockbuf_alloc( );
+       c->c_conn_state = SLAP_C_INVALID;
+       c->c_struct_state = SLAP_C_UNUSED;
+       c->c_close_reason = "?";                        /* should never be needed */
 
+       sb = c->c_sb;
+       c->c_sb = ber_sockbuf_alloc( );
        {
                ber_len_t max = sockbuf_max_incoming;
                ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
        }
 
-       c->c_conn_state = SLAP_C_INVALID;
-       c->c_struct_state = SLAP_C_UNUSED;
-       c->c_close_reason = "?";                        /* should never be needed */
+       ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+       slapd_sd_lock();
 
-#ifdef LDAP_SLAPI
-       /* call destructors, then constructors; avoids unnecessary allocation */
-       if ( slapi_plugins_used ) {
-               slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
+       ber_sockbuf_free( sb );
+
+       /* c must be fully reset by this point; when we call slapd_remove
+        * it may get immediately reused by a new connection.
+        */
+       if ( sd != AC_SOCKET_INVALID ) {
+               slapd_remove( sd, 1, 0, 1 );
+
+               Statslog( LDAP_DEBUG_STATS, (close_reason
+                                                                        ? "conn=%lu fd=%ld closed (%s)\n"
+                                                                        : "conn=%lu fd=%ld closed\n"),
+                       connid, (long) sd, close_reason, 0, 0 );
+       } else {
+               slapd_sd_unlock();
        }
-#endif
 }
 
 int connection_state_closing( Connection *c )
@@ -821,7 +828,7 @@ void connection_closing( Connection *c, const char *why )
                c->c_close_reason = why;
 
                /* don't listen on this port anymore */
-               slapd_clr_read( sd, 1 );
+               slapd_clr_read( sd, 0 );
 
                /* abandon active operations */
                connection_abandon( c );
@@ -830,9 +837,6 @@ void connection_closing( Connection *c, const char *why )
                slapd_clr_write( sd, 1 );
                if ( c->c_writewaiter ) {
                        ldap_pvt_thread_cond_signal( &c->c_write_cv );
-                       ldap_pvt_thread_mutex_unlock( &c->c_mutex );
-                       ldap_pvt_thread_yield();
-                       ldap_pvt_thread_mutex_lock( &c->c_mutex );
                }
 
        } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
@@ -915,6 +919,17 @@ Connection* connection_next( Connection *c, ber_socket_t *index )
                if( connections[*index].c_struct_state == SLAP_C_USED ) {
                        assert( connections[*index].c_conn_state != SLAP_C_INVALID );
                        c = &connections[(*index)++];
+                       if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
+                               /* avoid deadlock */
+                               ldap_pvt_thread_mutex_unlock( &connections_mutex );
+                               ldap_pvt_thread_mutex_lock( &c->c_mutex );
+                               ldap_pvt_thread_mutex_lock( &connections_mutex );
+                               if ( c->c_struct_state != SLAP_C_USED ) {
+                                       ldap_pvt_thread_mutex_unlock( &c->c_mutex );
+                                       c = NULL;
+                                       continue;
+                               }
+                       }
                        break;
                }
 
@@ -922,7 +937,6 @@ Connection* connection_next( Connection *c, ber_socket_t *index )
                assert( connections[*index].c_conn_state == SLAP_C_INVALID );
        }
 
-       if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex );
        ldap_pvt_thread_mutex_unlock( &connections_mutex );
        return c;
 }
@@ -968,7 +982,7 @@ void connection_done( Connection *c )
 /*
  * NOTE: keep in sync with enum in slapd.h
  */
-static int (*opfun[])( Operation *op, SlapReply *rs ) = {
+static BI_op_func *opfun[] = {
        do_bind,
        do_unbind,
        do_add,
@@ -1310,10 +1324,6 @@ int connection_read(ber_socket_t s)
                Debug( LDAP_DEBUG_TRACE,
                        "connection_read(%d): closing, ignoring input for id=%lu\n",
                        s, c->c_connid, 0 );
-
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
-               slapd_set_read( s, 1 );
-#endif
                connection_return( c );
                return 0;
        }
@@ -1348,26 +1358,6 @@ int connection_read(ber_socket_t s)
                        c->c_needs_tls_accept = 0;
                        /* c_mutex is locked */
                        connection_closing( c, "TLS negotiation failure" );
-
-#if 0
-                       {
-                               struct timeval tv;
-                               fd_set rfd;
-                               /* Drain input before close, to allow SSL error codes
-                                * to propagate to client. */
-                               FD_ZERO(&rfd);
-                               FD_SET(s, &rfd);
-                               for (rc=1; rc>0;) {
-                                       tv.tv_sec = 1;
-                                       tv.tv_usec = 0;
-                                       rc = select(s+1, &rfd, NULL, NULL, &tv);
-                                       if (rc == 1) {
-                                               ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
-                                       }
-                               }
-                       }
-#endif
-
                        connection_close( c );
                        connection_return( c );
                        return 0;
@@ -1400,8 +1390,7 @@ int connection_read(ber_socket_t s)
                }
 
                /* if success and data is ready, fall thru to data input loop */
-               if( rc != 0 ||
-                       !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
+               if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
                {
 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
                        slapd_set_read( s, 1 );
@@ -1463,7 +1452,7 @@ int connection_read(ber_socket_t s)
 #endif
 
        if( rc < 0 ) {
-               Debug( LDAP_DEBUG_TRACE,
+               Debug( LDAP_DEBUG_CONNS,
                        "connection_read(%d): input error=%d id=%lu, closing.\n",
                        s, rc, c->c_connid );
 
@@ -1727,21 +1716,13 @@ connection_resched( Connection *conn )
        Operation *op;
 
        if( conn->c_conn_state == SLAP_C_CLOSING ) {
-               int rc;
                ber_socket_t    sd;
                ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
 
-               if( conn->c_conn_state != SLAP_C_CLOSING ) {
-                       Debug( LDAP_DEBUG_TRACE, "connection_resched: "
-                               "closed by other thread conn=%lu sd=%d\n",
-                               conn->c_connid, sd, 0 );
-               } else {
-                       Debug( LDAP_DEBUG_TRACE, "connection_resched: "
-                               "attempting closing conn=%lu sd=%d\n",
-                               conn->c_connid, sd, 0 );
-                       connection_close( conn );
-               }
-
+               Debug( LDAP_DEBUG_TRACE, "connection_resched: "
+                       "attempting closing conn=%lu sd=%d\n",
+                       conn->c_connid, sd, 0 );
+               connection_close( conn );
                return 0;
        }