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