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