]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Removed unnecessary definition that is already in core.schema.
[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         Debug( LDAP_DEBUG_ANY,
215                         "connection_init(%d): invalid.\n",
216                         s, 0, 0 );
217                 return -1;
218         }
219
220         assert( s >= 0 );
221 #ifndef HAVE_WINSOCK
222         assert( s < dtblsize );
223 #endif
224
225         ldap_pvt_thread_mutex_lock( &connections_mutex );
226
227 #ifndef HAVE_WINSOCK
228         c = &connections[s];
229
230 #else
231         {
232                 int i;
233
234                 c = NULL;
235
236         for( i=0; i < dtblsize; i++) {
237             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
238                 assert( connections[i].c_sb == 0 );
239                 c = &connections[i];
240                 break;
241             }
242
243             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
244                 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
245                 c = &connections[i];
246                 break;
247             }
248
249             assert( connections[i].c_struct_state == SLAP_C_USED );
250             assert( connections[i].c_conn_state != SLAP_C_INVALID );
251             assert( ber_pvt_sb_in_use( connections[i].c_sb ));
252         }
253
254         if( c == NULL ) {
255                 Debug( LDAP_DEBUG_ANY,
256                                 "connection_init(%d): connection table full (%d/%d).\n",
257                                 s, i, dtblsize);
258             ldap_pvt_thread_mutex_unlock( &connections_mutex );
259             return -1;
260         }
261     }
262 #endif
263
264     assert( c != NULL );
265     assert( c->c_struct_state != SLAP_C_USED );
266     assert( c->c_conn_state == SLAP_C_INVALID );
267
268     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
269         c->c_dn = NULL;
270         c->c_cdn = NULL;
271         c->c_client_name = NULL;
272         c->c_client_addr = NULL;
273         c->c_ops = NULL;
274         c->c_pending_ops = NULL;
275
276         c->c_sb = ber_sockbuf_alloc( );
277
278         /* should check status of thread calls */
279         ldap_pvt_thread_mutex_init( &c->c_mutex );
280         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
281         ldap_pvt_thread_cond_init( &c->c_write_cv );
282
283         c->c_struct_state = SLAP_C_UNUSED;
284     }
285
286     ldap_pvt_thread_mutex_lock( &c->c_mutex );
287
288     assert( c->c_struct_state == SLAP_C_UNUSED );
289     assert(     c->c_dn == NULL );
290     assert(     c->c_cdn == NULL );
291     assert( c->c_client_name == NULL );
292     assert( c->c_client_addr == NULL );
293     assert( c->c_ops == NULL );
294     assert( c->c_pending_ops == NULL );
295
296     c->c_client_name = ch_strdup( name == NULL ? "" : name );
297     c->c_client_addr = ch_strdup( addr );
298
299     c->c_n_ops_received = 0;
300 #ifdef LDAP_COUNTERS
301     c->c_n_ops_executing = 0;
302     c->c_n_ops_pending = 0;
303     c->c_n_ops_completed = 0;
304 #endif
305
306     c->c_starttime = slap_get_time();
307
308     ber_pvt_sb_set_desc( c->c_sb, s );
309     ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
310
311     if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
312         Debug( LDAP_DEBUG_ANY,
313             "connection_init(%d, %s, %s): set nonblocking failed\n",
314             s, c->c_client_name, c->c_client_addr);
315     }
316
317     id = c->c_connid = conn_nextid++;
318
319     c->c_conn_state = SLAP_C_INACTIVE;
320     c->c_struct_state = SLAP_C_USED;
321
322     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
323     ldap_pvt_thread_mutex_unlock( &connections_mutex );
324
325     return id;
326 }
327
328 static void
329 connection_destroy( Connection *c )
330 {
331         /* note: connections_mutex should be locked by caller */
332
333     assert( connections != NULL );
334     assert( c != NULL );
335     assert( c->c_struct_state != SLAP_C_UNUSED );
336     assert( c->c_conn_state != SLAP_C_INVALID );
337     assert( c->c_ops == NULL );
338
339     c->c_struct_state = SLAP_C_UNUSED;
340     c->c_conn_state = SLAP_C_INVALID;
341
342 #ifdef LDAP_COMPAT30
343     c->c_version = 0;
344 #endif
345     c->c_protocol = 0;
346
347     c->c_starttime = 0;
348
349     if(c->c_dn != NULL) {
350         free(c->c_dn);
351         c->c_dn = NULL;
352     }
353         if(c->c_cdn != NULL) {
354                 free(c->c_cdn);
355                 c->c_cdn = NULL;
356         }
357         if(c->c_client_name != NULL) {
358                 free(c->c_client_name);
359                 c->c_client_name = NULL;
360         }
361         if(c->c_client_addr != NULL) {
362                 free(c->c_client_addr);
363                 c->c_client_addr = NULL;
364         }
365
366         if ( ber_pvt_sb_in_use(c->c_sb) ) {
367                 int sd = ber_pvt_sb_get_desc(c->c_sb);
368
369                 slapd_remove( sd );
370                 ber_pvt_sb_close( c->c_sb );
371
372                 Statslog( LDAP_DEBUG_STATS,
373                     "conn=%d fd=%d closed.\n",
374                         c->c_connid, sd, 0, 0, 0 );
375         }
376
377         ber_pvt_sb_destroy( c->c_sb );
378 }
379
380 int connection_state_closing( Connection *c )
381 {
382         /* c_mutex must be locked by caller */
383
384         int state;
385         assert( c != NULL );
386         assert( c->c_struct_state == SLAP_C_USED );
387
388         state = c->c_conn_state;
389
390         assert( state != SLAP_C_INVALID );
391
392         return state == SLAP_C_CLOSING;
393 }
394
395 void connection_closing( Connection *c )
396 {
397         assert( connections != NULL );
398         assert( c != NULL );
399         assert( c->c_struct_state == SLAP_C_USED );
400         assert( c->c_conn_state != SLAP_C_INVALID );
401
402         /* c_mutex must be locked by caller */
403
404         if( c->c_conn_state != SLAP_C_CLOSING ) {
405                 Operation *o;
406
407                 Debug( LDAP_DEBUG_TRACE,
408                         "connection_closing: readying conn=%ld sd=%d for close.\n",
409                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
410
411                 /* update state to closing */
412                 c->c_conn_state = SLAP_C_CLOSING;
413
414                 /* don't listen on this port anymore */
415                 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
416
417                 /* shutdown I/O -- not yet implemented */
418
419                 /* abandon active operations */
420                 for( o = c->c_ops; o != NULL; o = o->o_next ) {
421                         ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
422                         o->o_abandon = 1;
423                         ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
424                 }
425
426                 /* remove pending operations */
427                 for( o = slap_op_pop( &c->c_pending_ops );
428                         o != NULL;
429                         o = slap_op_pop( &c->c_pending_ops ) )
430                 {
431                         slap_op_free( o );
432                 }
433
434                 /* wake write blocked operations */
435                 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
436                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
437         }
438 }
439
440 static void connection_close( Connection *c )
441 {
442         assert( connections != NULL );
443         assert( c != NULL );
444         assert( c->c_struct_state == SLAP_C_USED );
445         assert( c->c_conn_state == SLAP_C_CLOSING );
446
447         /* note: connections_mutex and c_mutex should be locked by caller */
448
449         if( c->c_ops != NULL ) {
450                 Debug( LDAP_DEBUG_TRACE,
451                         "connection_close: deferring conn=%ld sd=%d.\n",
452                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
453
454                 return;
455         }
456
457         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
458                 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
459
460         connection_destroy( c );
461 }
462
463 long connections_nextid(void)
464 {
465         long id;
466         assert( connections != NULL );
467
468         ldap_pvt_thread_mutex_lock( &connections_mutex );
469
470         id = conn_nextid;
471
472         ldap_pvt_thread_mutex_unlock( &connections_mutex );
473
474         return id;
475 }
476
477 Connection* connection_first( int *index )
478 {
479         assert( connections != NULL );
480         assert( index != NULL );
481
482         ldap_pvt_thread_mutex_lock( &connections_mutex );
483
484         *index = 0;
485
486         return connection_next(NULL, index);
487 }
488
489 Connection* connection_next( Connection *c, int *index )
490 {
491         assert( connections != NULL );
492         assert( index != NULL );
493         assert( *index <= dtblsize );
494
495         if( c != NULL ) {
496                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
497         }
498
499         c = NULL;
500
501         for(; *index < dtblsize; (*index)++) {
502                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
503                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
504 #ifndef HAVE_WINSOCK
505                         continue;
506 #else
507                         break;
508 #endif
509                 }
510
511                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
512                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
513                         c = &connections[(*index)++];
514                         break;
515                 }
516
517                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
518                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
519         }
520
521         if( c != NULL ) {
522                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
523         }
524
525         return c;
526 }
527
528 void connection_done( Connection *c )
529 {
530         assert( connections != NULL );
531
532         if( c != NULL ) {
533                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
534         }
535
536         ldap_pvt_thread_mutex_unlock( &connections_mutex );
537 }
538
539 /*
540  * connection_activity - handle the request operation op on connection
541  * conn.  This routine figures out what kind of operation it is and
542  * calls the appropriate stub to handle it.
543  */
544
545 static void *
546 connection_operation( void *arg_v )
547 {
548         struct co_arg   *arg = arg_v;
549         int tag = arg->co_op->o_tag;
550         Connection *conn = arg->co_conn;
551
552 #ifdef LDAP_COUNTERS
553         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
554         num_ops_initiated++;
555         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
556 #endif
557
558         switch ( tag ) {
559         case LDAP_REQ_BIND:
560                 do_bind( conn, arg->co_op );
561                 break;
562
563 #ifdef LDAP_COMPAT30
564         case LDAP_REQ_UNBIND_30:
565 #endif
566         case LDAP_REQ_UNBIND:
567                 do_unbind( conn, arg->co_op );
568                 break;
569
570         case LDAP_REQ_ADD:
571                 do_add( conn, arg->co_op );
572                 break;
573
574 #ifdef LDAP_COMPAT30
575         case LDAP_REQ_DELETE_30:
576 #endif
577         case LDAP_REQ_DELETE:
578                 do_delete( conn, arg->co_op );
579                 break;
580
581         case LDAP_REQ_MODRDN:
582                 do_modrdn( conn, arg->co_op );
583                 break;
584
585         case LDAP_REQ_MODIFY:
586                 do_modify( conn, arg->co_op );
587                 break;
588
589         case LDAP_REQ_COMPARE:
590                 do_compare( conn, arg->co_op );
591                 break;
592
593         case LDAP_REQ_SEARCH:
594                 do_search( conn, arg->co_op );
595                 break;
596
597 #ifdef LDAP_COMPAT30
598         case LDAP_REQ_ABANDON_30:
599 #endif
600         case LDAP_REQ_ABANDON:
601                 do_abandon( conn, arg->co_op );
602                 break;
603
604         default:
605                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
606                     arg->co_op->o_tag, 0, 0 );
607                 break;
608         }
609
610 #ifdef LDAP_COUNTERS
611         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
612         num_ops_completed++;
613         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
614 #endif
615
616         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
617
618 #ifdef LDAP_COUNTERS
619         conn->c_n_ops_completed++;
620 #endif
621
622         slap_op_remove( &conn->c_ops, arg->co_op );
623         slap_op_free( arg->co_op );
624         arg->co_op = NULL;
625         arg->co_conn = NULL;
626         free( (char *) arg );
627         arg = NULL;
628
629         switch( tag ) {
630 #ifdef LDAP_COMPAT30
631         case LDAP_REQ_UNBIND_30:
632 #endif
633         case LDAP_REQ_UNBIND:
634                 /* c_mutex is locked */
635                 connection_closing( conn );
636                 break;
637
638         case LDAP_REQ_BIND:
639                 if( conn->c_conn_state == SLAP_C_BINDING) {
640                         conn->c_conn_state = SLAP_C_ACTIVE;
641                 }
642         }
643
644         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
645         active_threads--;
646         if( active_threads < 1 ) {
647                 ldap_pvt_thread_cond_signal(&active_threads_cond);
648         }
649         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
650
651         connection_resched( conn );
652
653         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
654
655         return NULL;
656 }
657
658 int connection_read(int s)
659 {
660         int rc = 0;
661         Connection *c;
662         assert( connections != NULL );
663
664         ldap_pvt_thread_mutex_lock( &connections_mutex );
665
666         /* get (locked) connection */
667         c = connection_get( s );
668
669         if( c == NULL ) {
670                 Debug( LDAP_DEBUG_ANY,
671                         "connection_read(%d): no connection!\n",
672                         s, 0, 0 );
673                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
674                 return -1;
675         }
676
677         if( c->c_conn_state == SLAP_C_CLOSING ) {
678                 Debug( LDAP_DEBUG_TRACE,
679                         "connection_read(%d): closing, ignoring input for id=%ld\n",
680                         s, c->c_connid, 0 );
681
682                 connection_return( c );
683                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
684                 return 0;
685         }
686
687         Debug( LDAP_DEBUG_TRACE,
688                 "connection_read(%d): checking for input on id=%ld\n",
689                 s, c->c_connid, 0 );
690
691 #define CONNECTION_INPUT_LOOP 1
692
693 #ifdef DATA_READY_LOOP
694         while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
695 #elif CONNECTION_INPUT_LOOP
696         while(!rc)
697 #endif
698         {
699                 rc = connection_input( c );
700         }
701
702         if( rc < 0 ) {
703                 Debug( LDAP_DEBUG_TRACE,
704                         "connection_read(%d): input error=%d id=%ld, closing.\n",
705                         s, rc, c->c_connid );
706
707                 /* connections_mutex and c_mutex are locked */
708                 connection_closing( c );
709                 connection_close( c );
710         }
711
712         connection_return( c );
713         ldap_pvt_thread_mutex_unlock( &connections_mutex );
714         return 0;
715 }
716
717 static int
718 connection_input(
719     Connection *conn
720 )
721 {
722         Operation *op;
723         unsigned long   tag, len;
724         long            msgid;
725         BerElement      *ber;
726
727         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
728             == NULL ) {
729                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
730                 return -1;
731         }
732
733         errno = 0;
734         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
735             != LDAP_TAG_MESSAGE )
736         {
737                 int err = errno;
738
739                 Debug( LDAP_DEBUG_TRACE,
740                         "ber_get_next on fd %d failed errno %d (%s)\n",
741                         ber_pvt_sb_get_desc( conn->c_sb ), err,
742                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
743                 Debug( LDAP_DEBUG_TRACE,
744                         "\t*** got %ld of %lu so far\n",
745                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
746                         conn->c_currentber->ber_len, 0 );
747
748                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
749                         /* log, close and send error */
750                         ber_free( conn->c_currentber, 1 );
751                         conn->c_currentber = NULL;
752
753                         return -2;
754                 }
755                 return 1;
756         }
757
758         ber = conn->c_currentber;
759         conn->c_currentber = NULL;
760
761         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
762                 /* log, close and send error */
763                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
764                     0 );
765                 ber_free( ber, 1 );
766                 return -1;
767         }
768
769         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
770                 /* log, close and send error */
771                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
772                     0 );
773                 ber_free( ber, 1 );
774
775                 return -1;
776         }
777
778 #ifdef LDAP_COMPAT30
779         if ( conn->c_version == 30 ) {
780                 (void) ber_skip_tag( ber, &len );
781         }
782 #endif
783
784         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
785
786         if ( conn->c_conn_state == SLAP_C_BINDING
787                 || conn->c_conn_state == SLAP_C_CLOSING )
788         {
789                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
790                 slap_op_add( &conn->c_pending_ops, op );
791
792         } else {
793                 connection_op_activate( conn, op );
794         }
795
796 #ifdef NO_THREADS
797         if ( conn->c_struct_state != SLAP_C_USED ) {
798                 /* connection must have got closed underneath us */
799                 return 1;
800         }
801 #endif
802         assert( conn->c_struct_state == SLAP_C_USED );
803
804         return 0;
805 }
806
807 static int
808 connection_resched( Connection *conn )
809 {
810         Operation *op;
811
812         if( conn->c_conn_state == SLAP_C_CLOSING ) {
813                 Debug( LDAP_DEBUG_TRACE,
814                         "connection_resched: attempting closing conn=%ld sd=%d.\n",
815                         conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
816
817                 connection_close( conn );
818                 return 0;
819         }
820
821         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
822                 /* other states need different handling */
823                 return 0;
824         }
825
826         for( op = slap_op_pop( &conn->c_pending_ops );
827                 op != NULL;
828                 op = slap_op_pop( &conn->c_pending_ops ) )
829         {
830                 /* pending operations should not be marked for abandonment */
831                 assert(!op->o_abandon);
832
833                 connection_op_activate( conn, op );
834
835                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
836                         break;
837                 }
838         }
839         return 0;
840 }
841
842 static int connection_op_activate( Connection *conn, Operation *op )
843 {
844         struct co_arg *arg;
845         char *tmpdn;
846         int status;
847         unsigned long tag = op->o_tag;
848
849         if ( conn->c_dn != NULL ) {
850                 tmpdn = ch_strdup( conn->c_dn );
851         } else {
852                 tmpdn = NULL;
853         }
854
855         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
856         arg->co_conn = conn;
857         arg->co_op = op;
858
859         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
860         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
861
862         slap_op_add( &conn->c_ops, arg->co_op );
863
864         if(tag == LDAP_REQ_BIND) {
865                 conn->c_conn_state = SLAP_C_BINDING;
866         }
867
868         if ( tmpdn != NULL ) {
869                 free( tmpdn );
870         }
871
872         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
873         active_threads++;
874         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
875
876         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
877                                          connection_operation, (void *) arg );
878
879         if ( status != 0 ) {
880                 Debug( LDAP_DEBUG_ANY,
881                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
882
883                 /* should move op to pending list */
884         }
885
886         return status;
887 }
888
889 int connection_write(int s)
890 {
891         Connection *c;
892         assert( connections != NULL );
893
894         ldap_pvt_thread_mutex_lock( &connections_mutex );
895
896         c = connection_get( s );
897         if( c == NULL ) {
898                 Debug( LDAP_DEBUG_ANY,
899                         "connection_write(%d): no connection!\n",
900                         s, 0, 0 );
901                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
902                 return -1;
903         }
904
905         Debug( LDAP_DEBUG_TRACE,
906                 "connection_write(%d): waking output for id=%ld\n",
907                 s, c->c_connid, 0 );
908
909         ldap_pvt_thread_cond_signal( &c->c_write_cv );
910
911         connection_return( c );
912         ldap_pvt_thread_mutex_unlock( &connections_mutex );
913         return 0;
914 }