]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Move WAKE_LISTENER call behind &connection_mutex to eliminate
[openldap] / servers / slapd / connection.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/socket.h>
6 #include <ac/errno.h>
7 #include <ac/signal.h>
8 #include <ac/string.h>
9 #include <ac/time.h>
10
11 #include "slap.h"
12
13 /* we need LBER internals */
14 #include "../../libraries/liblber/lber-int.h"
15
16 /* protected by connections_mutex */
17 static ldap_pvt_thread_mutex_t connections_mutex;
18 static Connection *connections = NULL;
19 static long conn_nextid = 0;
20
21 /* structure state (protected by connections_mutex) */
22 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
23 #define SLAP_C_UNUSED                   0x01
24 #define SLAP_C_USED                             0x02
25
26 /* connection state (protected by c_mutex ) */
27 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
28 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
29 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
30 #define SLAP_C_BINDING                  0x03    /* binding */
31 #define SLAP_C_CLOSING                  0x04    /* closing */
32
33 void slapd_remove(int s);
34 static Connection* connection_get( int s );
35
36 static int connection_input( Connection *c );
37 static void connection_close( Connection *c );
38
39 static int connection_op_activate( Connection *conn, Operation *op );
40 static int connection_resched( Connection *conn );
41
42 struct co_arg {
43         Connection      *co_conn;
44         Operation       *co_op;
45 };
46
47 /*
48  * Initialize connection management infrastructure.
49  */
50 int connections_init(void)
51 {
52         assert( connections == NULL );
53
54         if( connections != NULL) {
55                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
56                         0, 0, 0 );
57                 return -1;
58         }
59
60         /* should check return of every call */
61         ldap_pvt_thread_mutex_init( &connections_mutex );
62
63         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
64
65         if( connections == NULL ) {
66                 Debug( LDAP_DEBUG_ANY,
67                         "connections_init: allocation (%d*%ld) of connection array failed.\n",
68                         dtblsize, (long) sizeof(Connection), 0 );
69                 return -1;
70         }
71
72     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
73     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
74
75         /*
76          * per entry initialization of the Connection array initialization
77          * will be done by connection_init()
78          */ 
79
80         return 0;
81 }
82
83 /*
84  * Destroy connection management infrastructure.
85  */
86 int connections_destroy(void)
87 {
88         int i;
89
90         /* should check return of every call */
91
92         if( connections == NULL) {
93                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
94                         0, 0, 0 );
95                 return -1;
96         }
97
98         for ( i = 0; i < dtblsize; i++ ) {
99                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
100                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
101                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
102                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
103                 }
104         }
105
106         free( connections );
107         connections = NULL;
108
109         ldap_pvt_thread_mutex_destroy( &connections_mutex );
110         return 0;
111 }
112
113 /*
114  * shutdown all connections
115  */
116 int connections_shutdown(void)
117 {
118         int i;
119
120         ldap_pvt_thread_mutex_lock( &connections_mutex );
121
122         for ( i = 0; i < dtblsize; i++ ) {
123                 if( connections[i].c_struct_state != SLAP_C_USED ) {
124                         continue;
125                 }
126
127                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
128
129                 /* connections_mutex and c_mutex are locked */
130                 connection_closing( &connections[i] );
131                 connection_close( &connections[i] );
132
133                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
134         }
135
136         ldap_pvt_thread_mutex_unlock( &connections_mutex );
137
138         return 0;
139 }
140
141 static Connection* connection_get( int s )
142 {
143         /* connections_mutex should be locked by caller */
144
145         Connection *c = NULL;
146
147         assert( connections != NULL );
148
149         if(s < 0) {
150                 return NULL;
151         }
152
153 #ifndef HAVE_WINSOCK
154         assert( connections[s].c_struct_state == SLAP_C_USED );
155         assert( connections[s].c_conn_state != SLAP_C_INVALID );
156         assert( ber_pvt_sb_in_use( connections[s].c_sb ) );
157
158         c = &connections[s];
159 #else
160         {
161                 int i;
162
163                 for(i=0; i<dtblsize; i++) {
164                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
165                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
166                                 assert( connections[i].c_sb == 0 );
167                                 break;
168                         }
169
170                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
171                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
172                                 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
173                                 continue;
174                         }
175
176                         assert( connections[i].c_struct_state == SLAP_C_USED );
177                         assert( connections[i].c_conn_state != SLAP_C_INVALID );
178                         assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
179
180                         if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
181                                 c = &connections[i];
182                                 break;
183                         }
184                 }
185         }
186 #endif
187
188         if( c != NULL ) {
189                 /* we do this BEFORE locking to aid in debugging */
190                 Debug( LDAP_DEBUG_TRACE,
191                         "connection_get(%d): got connid=%ld\n",
192                         s, c->c_connid, 0 );
193
194                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
195         }
196         return c;
197 }
198
199 static void connection_return( Connection *c )
200 {
201         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
202 }
203
204 long connection_init(
205         int s,
206         const char* name,
207         const char* addr)
208 {
209         long id;
210         Connection *c;
211         assert( connections != NULL );
212
213         if( s < 0 ) {
214                 return -1;
215         }
216
217         assert( s >= 0 );
218 #ifndef HAVE_WINSOCK
219         assert( s < dtblsize );
220 #endif
221
222         ldap_pvt_thread_mutex_lock( &connections_mutex );
223
224 #ifndef HAVE_WINSOCK
225         c = &connections[s];
226
227 #else
228         {
229                 int i;
230
231         for( i=0; i < dtblsize; i++) {
232             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
233                 assert( connections[i].c_sb == 0 );
234                 c = &connections[i];
235                 break;
236             }
237
238             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
239                 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
240                 c = &connections[i];
241                 break;
242             }
243
244             assert( connections[i].c_struct_state == SLAP_C_USED );
245             assert( connections[i].c_conn_state != SLAP_C_INVALID );
246             assert( ber_pvt_sb_in_use( connections[i].c_sb ));
247         }
248
249         if( c == NULL ) {
250             ldap_pvt_thread_mutex_unlock( &connections_mutex );
251             return -1;
252         }
253     }
254 #endif
255
256     assert( c != NULL );
257     assert( c->c_struct_state != SLAP_C_USED );
258     assert( c->c_conn_state == SLAP_C_INVALID );
259
260     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
261         c->c_dn = NULL;
262         c->c_cdn = NULL;
263         c->c_client_name = NULL;
264         c->c_client_addr = NULL;
265         c->c_ops = NULL;
266         c->c_pending_ops = NULL;
267
268         c->c_sb = ber_sockbuf_alloc( );
269
270         /* should check status of thread calls */
271         ldap_pvt_thread_mutex_init( &c->c_mutex );
272         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
273         ldap_pvt_thread_cond_init( &c->c_write_cv );
274
275         c->c_struct_state = SLAP_C_UNUSED;
276     }
277
278     ldap_pvt_thread_mutex_lock( &c->c_mutex );
279
280     assert( c->c_struct_state == SLAP_C_UNUSED );
281     assert(     c->c_dn == NULL );
282     assert(     c->c_cdn == NULL );
283     assert( c->c_client_name == NULL );
284     assert( c->c_client_addr == NULL );
285     assert( c->c_ops == NULL );
286     assert( c->c_pending_ops == NULL );
287
288     c->c_client_name = ch_strdup( name == NULL ? "" : name );
289     c->c_client_addr = ch_strdup( addr );
290
291     c->c_n_ops_received = 0;
292 #ifdef LDAP_COUNTERS
293     c->c_n_ops_executing = 0;
294     c->c_n_ops_pending = 0;
295     c->c_n_ops_completed = 0;
296 #endif
297
298     c->c_starttime = slap_get_time();
299
300     ber_pvt_sb_set_desc( c->c_sb, s );
301     ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
302
303     if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
304         Debug( LDAP_DEBUG_ANY,
305             "connection_init(%d, %s, %s): set nonblocking failed\n",
306             s, c->c_client_name, c->c_client_addr);
307     }
308
309     id = c->c_connid = conn_nextid++;
310
311     c->c_conn_state = SLAP_C_INACTIVE;
312     c->c_struct_state = SLAP_C_USED;
313
314     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
315     ldap_pvt_thread_mutex_unlock( &connections_mutex );
316
317     return id;
318 }
319
320 static void
321 connection_destroy( Connection *c )
322 {
323         /* note: connections_mutex should be locked by caller */
324
325     assert( connections != NULL );
326     assert( c != NULL );
327     assert( c->c_struct_state != SLAP_C_UNUSED );
328     assert( c->c_conn_state != SLAP_C_INVALID );
329     assert( c->c_ops == NULL );
330
331     c->c_struct_state = SLAP_C_UNUSED;
332     c->c_conn_state = SLAP_C_INVALID;
333
334 #ifdef LDAP_COMPAT30
335     c->c_version = 0;
336 #endif
337     c->c_protocol = 0;
338
339     c->c_starttime = 0;
340
341     if(c->c_dn != NULL) {
342         free(c->c_dn);
343         c->c_dn = NULL;
344     }
345         if(c->c_cdn != NULL) {
346                 free(c->c_cdn);
347                 c->c_cdn = NULL;
348         }
349         if(c->c_client_name != NULL) {
350                 free(c->c_client_name);
351                 c->c_client_name = NULL;
352         }
353         if(c->c_client_addr != NULL) {
354                 free(c->c_client_addr);
355                 c->c_client_addr = NULL;
356         }
357
358         if ( ber_pvt_sb_in_use(c->c_sb) ) {
359                 int sd = ber_pvt_sb_get_desc(c->c_sb);
360
361                 slapd_remove( sd );
362                 ber_pvt_sb_close( c->c_sb );
363
364                 Statslog( LDAP_DEBUG_STATS,
365                     "conn=%d fd=%d closed.\n",
366                         c->c_connid, sd, 0, 0, 0 );
367         }
368
369         ber_pvt_sb_destroy( c->c_sb );
370 }
371
372 int connection_state_closing( Connection *c )
373 {
374         /* c_mutex must be locked by caller */
375
376         int state;
377         assert( c != NULL );
378         assert( c->c_struct_state == SLAP_C_USED );
379
380         state = c->c_conn_state;
381
382         assert( state != SLAP_C_INVALID );
383
384         return state == SLAP_C_CLOSING;
385 }
386
387 void connection_closing( Connection *c )
388 {
389         assert( connections != NULL );
390         assert( c != NULL );
391         assert( c->c_struct_state == SLAP_C_USED );
392         assert( c->c_conn_state != SLAP_C_INVALID );
393
394         /* c_mutex must be locked by caller */
395
396         if( c->c_conn_state != SLAP_C_CLOSING ) {
397                 Operation *o;
398
399                 Debug( LDAP_DEBUG_TRACE,
400                         "connection_closing: readying conn=%ld sd=%d for close.\n",
401                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
402
403                 /* update state to closing */
404                 c->c_conn_state = SLAP_C_CLOSING;
405
406                 /* don't listen on this port anymore */
407                 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
408
409                 /* shutdown I/O -- not yet implemented */
410
411                 /* abandon active operations */
412                 for( o = c->c_ops; o != NULL; o = o->o_next ) {
413                         ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
414                         o->o_abandon = 1;
415                         ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
416                 }
417
418                 /* remove pending operations */
419                 for( o = slap_op_pop( &c->c_pending_ops );
420                         o != NULL;
421                         o = slap_op_pop( &c->c_pending_ops ) )
422                 {
423                         slap_op_free( o );
424                 }
425
426                 /* wake write blocked operations */
427                 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
428                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
429         }
430 }
431
432 static void connection_close( Connection *c )
433 {
434         assert( connections != NULL );
435         assert( c != NULL );
436         assert( c->c_struct_state == SLAP_C_USED );
437         assert( c->c_conn_state == SLAP_C_CLOSING );
438
439         /* note: connections_mutex and c_mutex should be locked by caller */
440
441         if( c->c_ops != NULL ) {
442                 Debug( LDAP_DEBUG_TRACE,
443                         "connection_close: deferring conn=%ld sd=%d.\n",
444                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
445
446                 return;
447         }
448
449         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
450                 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
451
452         connection_destroy( c );
453 }
454
455 long connections_nextid(void)
456 {
457         long id;
458         assert( connections != NULL );
459
460         ldap_pvt_thread_mutex_lock( &connections_mutex );
461
462         id = conn_nextid;
463
464         ldap_pvt_thread_mutex_unlock( &connections_mutex );
465
466         return id;
467 }
468
469 Connection* connection_first( int *index )
470 {
471         assert( connections != NULL );
472         assert( index != NULL );
473
474         ldap_pvt_thread_mutex_lock( &connections_mutex );
475
476         *index = 0;
477
478         return connection_next(NULL, index);
479 }
480
481 Connection* connection_next( Connection *c, int *index )
482 {
483         assert( connections != NULL );
484         assert( index != NULL );
485         assert( *index <= dtblsize );
486
487         if( c != NULL ) {
488                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
489         }
490
491         c = NULL;
492
493         for(; *index < dtblsize; (*index)++) {
494                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
495                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
496 #ifndef HAVE_WINSOCK
497                         continue;
498 #else
499                         break;
500 #endif
501                 }
502
503                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
504                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
505                         c = &connections[(*index)++];
506                         break;
507                 }
508
509                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
510                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
511         }
512
513         if( c != NULL ) {
514                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
515         }
516
517         return c;
518 }
519
520 void connection_done( Connection *c )
521 {
522         assert( connections != NULL );
523
524         if( c != NULL ) {
525                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
526         }
527
528         ldap_pvt_thread_mutex_unlock( &connections_mutex );
529 }
530
531 /*
532  * connection_activity - handle the request operation op on connection
533  * conn.  This routine figures out what kind of operation it is and
534  * calls the appropriate stub to handle it.
535  */
536
537 static void *
538 connection_operation( void *arg_v )
539 {
540         struct co_arg   *arg = arg_v;
541         int tag = arg->co_op->o_tag;
542         Connection *conn = arg->co_conn;
543
544 #ifdef LDAP_COUNTERS
545         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
546         num_ops_initiated++;
547         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
548 #endif
549
550         switch ( tag ) {
551         case LDAP_REQ_BIND:
552                 do_bind( conn, arg->co_op );
553                 break;
554
555 #ifdef LDAP_COMPAT30
556         case LDAP_REQ_UNBIND_30:
557 #endif
558         case LDAP_REQ_UNBIND:
559                 do_unbind( conn, arg->co_op );
560                 break;
561
562         case LDAP_REQ_ADD:
563                 do_add( conn, arg->co_op );
564                 break;
565
566 #ifdef LDAP_COMPAT30
567         case LDAP_REQ_DELETE_30:
568 #endif
569         case LDAP_REQ_DELETE:
570                 do_delete( conn, arg->co_op );
571                 break;
572
573         case LDAP_REQ_MODRDN:
574                 do_modrdn( conn, arg->co_op );
575                 break;
576
577         case LDAP_REQ_MODIFY:
578                 do_modify( conn, arg->co_op );
579                 break;
580
581         case LDAP_REQ_COMPARE:
582                 do_compare( conn, arg->co_op );
583                 break;
584
585         case LDAP_REQ_SEARCH:
586                 do_search( conn, arg->co_op );
587                 break;
588
589 #ifdef LDAP_COMPAT30
590         case LDAP_REQ_ABANDON_30:
591 #endif
592         case LDAP_REQ_ABANDON:
593                 do_abandon( conn, arg->co_op );
594                 break;
595
596         default:
597                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
598                     arg->co_op->o_tag, 0, 0 );
599                 break;
600         }
601
602 #ifdef LDAP_COUNTERS
603         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
604         num_ops_completed++;
605         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
606 #endif
607
608         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
609
610 #ifdef LDAP_COUNTERS
611         conn->c_n_ops_completed++;
612 #endif
613
614         slap_op_remove( &conn->c_ops, arg->co_op );
615         slap_op_free( arg->co_op );
616         arg->co_op = NULL;
617         arg->co_conn = NULL;
618         free( (char *) arg );
619         arg = NULL;
620
621         switch( tag ) {
622 #ifdef LDAP_COMPAT30
623         case LDAP_REQ_UNBIND_30:
624 #endif
625         case LDAP_REQ_UNBIND:
626                 /* c_mutex is locked */
627                 connection_closing( conn );
628                 break;
629
630         case LDAP_REQ_BIND:
631                 if( conn->c_conn_state == SLAP_C_BINDING) {
632                         conn->c_conn_state = SLAP_C_ACTIVE;
633                 }
634         }
635
636         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
637         active_threads--;
638         if( active_threads < 1 ) {
639                 ldap_pvt_thread_cond_signal(&active_threads_cond);
640         }
641         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
642
643         connection_resched( conn );
644
645         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
646
647         return NULL;
648 }
649
650 int connection_read(int s)
651 {
652         int rc = 0;
653         Connection *c;
654         assert( connections != NULL );
655
656         ldap_pvt_thread_mutex_lock( &connections_mutex );
657
658         /* get (locked) connection */
659         c = connection_get( s );
660
661         if( c == NULL ) {
662                 Debug( LDAP_DEBUG_ANY,
663                         "connection_read(%d): no connection!\n",
664                         s, 0, 0 );
665                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
666                 return -1;
667         }
668
669         if( c->c_conn_state == SLAP_C_CLOSING ) {
670                 Debug( LDAP_DEBUG_TRACE,
671                         "connection_read(%d): closing, ignoring input for id=%ld\n",
672                         s, c->c_connid, 0 );
673
674                 connection_return( c );
675                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
676                 return 0;
677         }
678
679         Debug( LDAP_DEBUG_TRACE,
680                 "connection_read(%d): checking for input on id=%ld\n",
681                 s, c->c_connid, 0 );
682
683 #define CONNECTION_INPUT_LOOP 1
684
685 #ifdef DATA_READY_LOOP
686         while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
687 #elif CONNECTION_INPUT_LOOP
688         while(!rc)
689 #endif
690         {
691                 rc = connection_input( c );
692         }
693
694         if( rc < 0 ) {
695                 Debug( LDAP_DEBUG_TRACE,
696                         "connection_read(%d): input error=%d id=%ld, closing.\n",
697                         s, rc, c->c_connid );
698
699                 /* connections_mutex and c_mutex are locked */
700                 connection_closing( c );
701                 connection_close( c );
702         }
703
704         connection_return( c );
705         ldap_pvt_thread_mutex_unlock( &connections_mutex );
706         return 0;
707 }
708
709 static int
710 connection_input(
711     Connection *conn
712 )
713 {
714         Operation *op;
715         unsigned long   tag, len;
716         long            msgid;
717         BerElement      *ber;
718
719         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
720             == NULL ) {
721                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
722                 return -1;
723         }
724
725         errno = 0;
726         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
727             != LDAP_TAG_MESSAGE )
728         {
729                 int err = errno;
730
731                 Debug( LDAP_DEBUG_TRACE,
732                         "ber_get_next on fd %d failed errno %d (%s)\n",
733                         ber_pvt_sb_get_desc( conn->c_sb ), err,
734                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
735                 Debug( LDAP_DEBUG_TRACE,
736                         "\t*** got %ld of %lu so far\n",
737                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
738                         conn->c_currentber->ber_len, 0 );
739
740                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
741                         /* log, close and send error */
742                         ber_free( conn->c_currentber, 1 );
743                         conn->c_currentber = NULL;
744
745                         return -2;
746                 }
747                 return 1;
748         }
749
750         ber = conn->c_currentber;
751         conn->c_currentber = NULL;
752
753         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
754                 /* log, close and send error */
755                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
756                     0 );
757                 ber_free( ber, 1 );
758                 return -1;
759         }
760
761         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
762                 /* log, close and send error */
763                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
764                     0 );
765                 ber_free( ber, 1 );
766
767                 return -1;
768         }
769
770 #ifdef LDAP_COMPAT30
771         if ( conn->c_version == 30 ) {
772                 (void) ber_skip_tag( ber, &len );
773         }
774 #endif
775
776         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
777
778         if ( conn->c_conn_state == SLAP_C_BINDING
779                 || conn->c_conn_state == SLAP_C_CLOSING )
780         {
781                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
782                 slap_op_add( &conn->c_pending_ops, op );
783
784         } else {
785                 connection_op_activate( conn, op );
786         }
787
788 #ifdef NO_THREADS
789         if ( conn->c_struct_state != SLAP_C_USED ) {
790                 /* connection must have got closed underneath us */
791                 return 1;
792         }
793 #endif
794         assert( conn->c_struct_state == SLAP_C_USED );
795
796         return 0;
797 }
798
799 static int
800 connection_resched( Connection *conn )
801 {
802         Operation *op;
803
804         if( conn->c_conn_state == SLAP_C_CLOSING ) {
805                 Debug( LDAP_DEBUG_TRACE,
806                         "connection_resched: attempting closing conn=%ld sd=%d.\n",
807                         conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
808
809                 connection_close( conn );
810                 return 0;
811         }
812
813         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
814                 /* other states need different handling */
815                 return 0;
816         }
817
818         for( op = slap_op_pop( &conn->c_pending_ops );
819                 op != NULL;
820                 op = slap_op_pop( &conn->c_pending_ops ) )
821         {
822                 /* pending operations should not be marked for abandonment */
823                 assert(!op->o_abandon);
824
825                 connection_op_activate( conn, op );
826
827                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
828                         break;
829                 }
830         }
831         return 0;
832 }
833
834 static int connection_op_activate( Connection *conn, Operation *op )
835 {
836         struct co_arg *arg;
837         char *tmpdn;
838         int status;
839         unsigned long tag = op->o_tag;
840
841         if ( conn->c_dn != NULL ) {
842                 tmpdn = ch_strdup( conn->c_dn );
843         } else {
844                 tmpdn = NULL;
845         }
846
847         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
848         arg->co_conn = conn;
849         arg->co_op = op;
850
851         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
852         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
853
854         slap_op_add( &conn->c_ops, arg->co_op );
855
856         if(tag == LDAP_REQ_BIND) {
857                 conn->c_conn_state = SLAP_C_BINDING;
858         }
859
860         if ( tmpdn != NULL ) {
861                 free( tmpdn );
862         }
863
864         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
865         active_threads++;
866         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
867
868         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
869                                          connection_operation, (void *) arg );
870
871         if ( status != 0 ) {
872                 Debug( LDAP_DEBUG_ANY,
873                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
874
875                 /* should move op to pending list */
876         }
877
878         return status;
879 }
880
881 int connection_write(int s)
882 {
883         Connection *c;
884         assert( connections != NULL );
885
886         ldap_pvt_thread_mutex_lock( &connections_mutex );
887
888         c = connection_get( s );
889         if( c == NULL ) {
890                 Debug( LDAP_DEBUG_ANY,
891                         "connection_write(%d): no connection!\n",
892                         s, 0, 0 );
893                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
894                 return -1;
895         }
896
897         Debug( LDAP_DEBUG_TRACE,
898                 "connection_write(%d): waking output for id=%ld\n",
899                 s, c->c_connid, 0 );
900
901         ldap_pvt_thread_cond_signal( &c->c_write_cv );
902
903         connection_return( c );
904         ldap_pvt_thread_mutex_unlock( &connections_mutex );
905         return 0;
906 }