14 #define EWOULDBLOCK WSAEWOULDBLOCK
17 /* protected by connections_mutex */
18 static ldap_pvt_thread_mutex_t connections_mutex;
19 static Connection *connections = NULL;
20 static int conn_index = -1;
21 static long conn_nextid = 0;
23 /* structure state (protected by connections_mutex) */
24 #define SLAP_C_UNINITIALIZED 0x0 /* MUST BE ZERO (0) */
25 #define SLAP_C_UNUSED 0x1
26 #define SLAP_C_USED 0x2
28 /* connection state (protected by c_mutex ) */
29 #define SLAP_C_INVALID 0x0 /* MUST BE ZERO (0) */
30 #define SLAP_C_INACTIVE 0x1 /* zero threads */
31 #define SLAP_C_ACTIVE 0x2 /* one or more threads */
32 #define SLAP_C_BINDING 0x3 /* binding */
33 #define SLAP_C_CLOSING 0x4 /* closing */
35 void slapd_remove(int s);
36 static Connection* connection_get( int s );
38 static int connection_input( Connection *c );
40 static int connection_op_activate( Connection *conn, Operation *op );
41 static int connection_resched( Connection *conn );
49 * Initialize connection management infrastructure.
51 int connections_init(void)
55 assert( connections == NULL );
57 if( connections != NULL) { /* probably should assert this */
58 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
63 /* should check return of every call */
64 ldap_pvt_thread_mutex_init( &connections_mutex );
66 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
68 if( connections == NULL ) {
69 Debug( LDAP_DEBUG_ANY,
70 "connections_init: allocation (%d*%ld) of connection array failed.\n",
71 dtblsize, (long) sizeof(Connection), 0 );
75 for ( i = 0; i < dtblsize; i++ )
76 memset( &connections[i], 0, sizeof(Connection) );
79 * per entry initialization of the Connection array initialization
80 * will be done by connection_init()
86 static Connection* connection_get( int s )
90 assert( connections != NULL );
97 assert( connections[s].c_struct_state == SLAP_C_USED );
98 assert( connections[s].c_conn_state != SLAP_C_INVALID );
99 assert( connections[s].c_sb.sb_sd != -1 );
106 for(i=0; i<dtblsize; i++) {
107 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
108 assert( connections[i].c_conn_state == SLAP_C_INVALID );
109 assert( connections[i].c_sb.sb_sd == 0 );
113 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
114 assert( connections[i].c_conn_state == SLAP_C_INVALID );
115 assert( connections[i].c_sb.sb_sd == -1 );
119 assert( connections[i].c_struct_state == SLAP_C_USED );
120 assert( connections[i].c_conn_state != SLAP_C_INVALID );
121 assert( connections[i].c_sb.sb_sd != -1 );
123 if( connections[i].c_sb.sb_sd == s ) {
132 /* we do this BEFORE locking to aid in debugging */
133 Debug( LDAP_DEBUG_TRACE,
134 "connection_get(%d): got connid=%ld\n",
137 ldap_pvt_thread_mutex_lock( &c->c_mutex );
142 static void connection_return( Connection *c )
144 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
147 long connection_init(
154 assert( connections != NULL );
162 assert( s < dtblsize );
165 ldap_pvt_thread_mutex_lock( &connections_mutex );
174 for( i=0; i < dtblsize; i++) {
175 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
176 assert( connections[i].c_sb.sb_sd == 0 );
181 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
182 assert( connections[i].c_sb.sb_sd == -1 );
187 assert( connections[i].c_struct_state == SLAP_C_USED );
188 assert( connections[i].c_conn_state != SLAP_C_INVALID );
189 assert( connections[i].c_sb.sb_sd != -1 );
193 ldap_pvt_thread_mutex_unlock( &connections_mutex );
200 assert( c->c_struct_state != SLAP_C_USED );
201 assert( c->c_conn_state == SLAP_C_INVALID );
203 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
206 c->c_client_name = NULL;
207 c->c_client_addr = NULL;
209 c->c_pending_ops = NULL;
211 lber_pvt_sb_init( &c->c_sb );
213 /* should check status of thread calls */
214 ldap_pvt_thread_mutex_init( &c->c_mutex );
215 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
216 ldap_pvt_thread_cond_init( &c->c_write_cv );
218 c->c_struct_state = SLAP_C_UNUSED;
221 ldap_pvt_thread_mutex_lock( &c->c_mutex );
223 assert( c->c_struct_state == SLAP_C_UNUSED );
224 assert( c->c_dn == NULL );
225 assert( c->c_cdn == NULL );
226 assert( c->c_client_name == NULL );
227 assert( c->c_client_addr == NULL );
228 assert( c->c_ops == NULL );
229 assert( c->c_pending_ops == NULL );
231 c->c_client_name = ch_strdup( name == NULL ? "" : name );
232 c->c_client_addr = ch_strdup( addr );
234 c->c_n_ops_received = 0;
236 c->c_n_ops_executing = 0;
237 c->c_n_ops_pending = 0;
238 c->c_n_ops_completed = 0;
241 c->c_starttime = slap_get_time();
243 lber_pvt_sb_set_desc( &c->c_sb, s );
244 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
246 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
247 Debug( LDAP_DEBUG_ANY,
248 "connection_init(%d, %s, %s): set nonblocking failed\n",
249 s, c->c_client_name, c->c_client_addr);
252 id = c->c_connid = conn_nextid++;
254 c->c_conn_state = SLAP_C_INACTIVE;
255 c->c_struct_state = SLAP_C_USED;
257 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
258 ldap_pvt_thread_mutex_unlock( &connections_mutex );
264 connection_destroy( Connection *c )
266 assert( connections != NULL );
268 assert( c->c_struct_state != SLAP_C_UNUSED );
269 assert( c->c_conn_state != SLAP_C_INVALID );
270 assert( c->c_ops == NULL );
272 c->c_struct_state = SLAP_C_UNUSED;
273 c->c_conn_state = SLAP_C_INVALID;
280 if(c->c_dn != NULL) {
284 if(c->c_cdn != NULL) {
288 if(c->c_client_name != NULL) {
289 free(c->c_client_name);
290 c->c_client_name = NULL;
292 if(c->c_client_addr != NULL) {
293 free(c->c_client_addr);
294 c->c_client_addr = NULL;
297 if ( lber_pvt_sb_in_use(&c->c_sb) ) {
298 int sd = lber_pvt_sb_get_desc(&c->c_sb);
301 lber_pvt_sb_close( &c->c_sb );
303 Statslog( LDAP_DEBUG_STATS,
304 "conn=%d fd=%d closed.\n",
305 c->c_connid, sd, 0, 0, 0 );
308 lber_pvt_sb_destroy( &c->c_sb );
311 int connection_state_closing( Connection *c )
314 assert( c->c_struct_state == SLAP_C_USED );
315 assert( c->c_conn_state != SLAP_C_INVALID );
317 return c->c_conn_state == SLAP_C_CLOSING;
320 void connection_closing( Connection *c )
322 assert( connections != NULL );
324 assert( c->c_struct_state == SLAP_C_USED );
325 assert( c->c_conn_state != SLAP_C_INVALID );
327 if( c->c_conn_state != SLAP_C_CLOSING ) {
330 Debug( LDAP_DEBUG_TRACE,
331 "connection_closing: readying conn=%ld sd=%d for close.\n",
332 c->c_connid, c->c_sb.sb_sd, 0 );
334 /* update state to closing */
335 c->c_conn_state = SLAP_C_CLOSING;
337 /* don't listen on this port anymore */
338 slapd_clr_read( c->c_sb.sb_sd, 1 );
340 /* shutdown I/O -- not yet implemented */
342 /* abandon active operations */
343 for( o = c->c_ops; o != NULL; o = o->o_next ) {
344 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
346 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
349 /* remove pending operations */
350 for( o = slap_op_pop( &c->c_pending_ops );
352 o = slap_op_pop( &c->c_pending_ops ) )
357 /* wake write blocked operations */
358 ldap_pvt_thread_cond_signal( &c->c_write_cv );
362 static void connection_close( Connection *c )
364 assert( connections != NULL );
366 assert( c->c_struct_state == SLAP_C_USED );
367 assert( c->c_conn_state == SLAP_C_CLOSING );
369 if( c->c_ops != NULL ) {
370 Debug( LDAP_DEBUG_TRACE,
371 "connection_close: deferring conn=%ld sd=%d.\n",
372 c->c_connid, c->c_sb.sb_sd, 0 );
377 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
378 c->c_connid, c->c_sb.sb_sd, 0 );
380 connection_destroy( c );
383 long connections_nextid(void)
386 assert( connections != NULL );
388 ldap_pvt_thread_mutex_lock( &connections_mutex );
392 ldap_pvt_thread_mutex_unlock( &connections_mutex );
397 Connection* connection_first(void)
399 assert( connections != NULL );
401 ldap_pvt_thread_mutex_lock( &connections_mutex );
403 assert( conn_index == -1 );
406 return connection_next(NULL);
409 Connection* connection_next(Connection *c)
411 assert( connections != NULL );
412 assert( conn_index != -1 );
413 assert( conn_index <= dtblsize );
416 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
421 for(; conn_index < dtblsize; conn_index++) {
422 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
423 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
431 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
432 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
433 c = &connections[conn_index++];
437 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
438 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
442 ldap_pvt_thread_mutex_lock( &c->c_mutex );
448 void connection_done(Connection *c)
450 assert( connections != NULL );
451 assert( conn_index != -1 );
452 assert( conn_index <= dtblsize );
455 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
459 ldap_pvt_thread_mutex_unlock( &connections_mutex );
463 * connection_activity - handle the request operation op on connection
464 * conn. This routine figures out what kind of operation it is and
465 * calls the appropriate stub to handle it.
469 connection_operation( void *arg_v )
471 struct co_arg *arg = arg_v;
472 int tag = arg->co_op->o_tag;
473 Connection *conn = arg->co_conn;
476 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
478 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
483 do_bind( conn, arg->co_op );
487 case LDAP_REQ_UNBIND_30:
489 case LDAP_REQ_UNBIND:
490 do_unbind( conn, arg->co_op );
494 do_add( conn, arg->co_op );
498 case LDAP_REQ_DELETE_30:
500 case LDAP_REQ_DELETE:
501 do_delete( conn, arg->co_op );
504 case LDAP_REQ_MODRDN:
505 do_modrdn( conn, arg->co_op );
508 case LDAP_REQ_MODIFY:
509 do_modify( conn, arg->co_op );
512 case LDAP_REQ_COMPARE:
513 do_compare( conn, arg->co_op );
516 case LDAP_REQ_SEARCH:
517 do_search( conn, arg->co_op );
521 case LDAP_REQ_ABANDON_30:
523 case LDAP_REQ_ABANDON:
524 do_abandon( conn, arg->co_op );
528 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
529 arg->co_op->o_tag, 0, 0 );
534 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
536 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
539 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
542 conn->c_n_ops_completed++;
545 slap_op_remove( &conn->c_ops, arg->co_op );
546 slap_op_free( arg->co_op );
549 free( (char *) arg );
554 case LDAP_REQ_UNBIND_30:
556 case LDAP_REQ_UNBIND:
557 connection_closing( conn );
561 if( conn->c_conn_state == SLAP_C_BINDING) {
562 conn->c_conn_state = SLAP_C_ACTIVE;
566 if( conn->c_conn_state == SLAP_C_CLOSING ) {
567 Debug( LDAP_DEBUG_TRACE,
568 "connection_operation: attempting closing conn=%ld sd=%d.\n",
569 conn->c_connid, conn->c_sb.sb_sd, 0 );
571 connection_close( conn );
574 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
576 if( active_threads < 1 ) {
577 ldap_pvt_thread_cond_signal(&active_threads_cond);
579 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
581 connection_resched( conn );
583 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
588 int connection_read(int s)
592 assert( connections != NULL );
594 ldap_pvt_thread_mutex_lock( &connections_mutex );
596 c = connection_get( s );
598 Debug( LDAP_DEBUG_ANY,
599 "connection_read(%d): no connection!\n",
601 ldap_pvt_thread_mutex_unlock( &connections_mutex );
605 if( c->c_conn_state == SLAP_C_CLOSING ) {
606 Debug( LDAP_DEBUG_TRACE,
607 "connection_read(%d): closing, ignoring input for id=%ld\n",
610 connection_return( c );
611 ldap_pvt_thread_mutex_unlock( &connections_mutex );
615 Debug( LDAP_DEBUG_TRACE,
616 "connection_read(%d): checking for input on id=%ld\n",
619 #define CONNECTION_INPUT_LOOP 1
621 #ifdef DATA_READY_LOOP
622 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
623 #elif CONNECTION_INPUT_LOOP
627 rc = connection_input( c );
631 Debug( LDAP_DEBUG_TRACE,
632 "connection_read(%d): input error=%d id=%ld, closing.\n",
633 s, rc, c->c_connid );
635 connection_closing( c );
636 connection_close( c );
639 connection_return( c );
640 ldap_pvt_thread_mutex_unlock( &connections_mutex );
650 unsigned long tag, len;
654 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
656 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
661 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
662 != LDAP_TAG_MESSAGE )
666 Debug( LDAP_DEBUG_TRACE,
667 "ber_get_next on fd %d failed errno %d (%s)\n",
668 lber_pvt_sb_get_desc(&conn->c_sb), err,
669 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
670 Debug( LDAP_DEBUG_TRACE,
671 "\t*** got %ld of %lu so far\n",
672 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
673 conn->c_currentber->ber_len, 0 );
675 if ( err != EWOULDBLOCK && err != EAGAIN ) {
676 /* log, close and send error */
677 ber_free( conn->c_currentber, 1 );
678 conn->c_currentber = NULL;
685 ber = conn->c_currentber;
686 conn->c_currentber = NULL;
688 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
689 /* log, close and send error */
690 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
696 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
697 /* log, close and send error */
698 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
706 if ( conn->c_version == 30 ) {
707 (void) ber_skip_tag( ber, &len );
711 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
713 if ( conn->c_conn_state == SLAP_C_BINDING
714 || conn->c_conn_state == SLAP_C_CLOSING )
716 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
717 slap_op_add( &conn->c_pending_ops, op );
720 connection_op_activate( conn, op );
724 if ( conn->c_struct_state != SLAP_C_USED ) {
725 /* connection must have got closed underneath us */
729 assert( conn->c_struct_state == SLAP_C_USED );
735 connection_resched( Connection *conn )
739 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
740 /* other states need different handling */
744 for( op = slap_op_pop( &conn->c_pending_ops );
746 op = slap_op_pop( &conn->c_pending_ops ) )
748 /* pending operations should not be marked for abandonment */
749 assert(!op->o_abandon);
751 connection_op_activate( conn, op );
753 if ( conn->c_conn_state == SLAP_C_BINDING ) {
760 static int connection_op_activate( Connection *conn, Operation *op )
765 unsigned long tag = op->o_tag;
767 if ( conn->c_dn != NULL ) {
768 tmpdn = ch_strdup( conn->c_dn );
773 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
777 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
778 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
780 slap_op_add( &conn->c_ops, arg->co_op );
782 if(tag == LDAP_REQ_BIND) {
783 conn->c_conn_state = SLAP_C_BINDING;
786 if ( tmpdn != NULL ) {
790 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
792 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
794 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
795 connection_operation, (void *) arg );
798 Debug( LDAP_DEBUG_ANY,
799 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
801 /* should move op to pending list */
807 int connection_write(int s)
810 assert( connections != NULL );
812 ldap_pvt_thread_mutex_lock( &connections_mutex );
814 c = connection_get( s );
816 Debug( LDAP_DEBUG_ANY,
817 "connection_write(%d): no connection!\n",
819 ldap_pvt_thread_mutex_unlock( &connections_mutex );
823 Debug( LDAP_DEBUG_TRACE,
824 "connection_write(%d): waking output for id=%ld\n",
827 ldap_pvt_thread_cond_signal( &c->c_write_cv );
829 connection_return( c );
830 ldap_pvt_thread_mutex_unlock( &connections_mutex );