]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
213f3f45d277a583f90880dc9ca4bb7210d52e44
[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 #ifdef LDAP_COMPAT30
396     c->c_version = 0;
397 #endif
398     c->c_protocol = 0;
399
400     c->c_activitytime = c->c_starttime = 0;
401
402     if(c->c_dn != NULL) {
403         free(c->c_dn);
404         c->c_dn = NULL;
405     }
406         if(c->c_cdn != NULL) {
407                 free(c->c_cdn);
408                 c->c_cdn = NULL;
409         }
410         if(c->c_client_name != NULL) {
411                 free(c->c_client_name);
412                 c->c_client_name = NULL;
413         }
414         if(c->c_client_addr != NULL) {
415                 free(c->c_client_addr);
416                 c->c_client_addr = NULL;
417         }
418
419         if ( ber_pvt_sb_in_use(c->c_sb) ) {
420                 int sd = ber_pvt_sb_get_desc(c->c_sb);
421
422                 slapd_remove( sd, 0 );
423                 ber_pvt_sb_close( c->c_sb );
424
425                 Statslog( LDAP_DEBUG_STATS,
426                     "conn=%d fd=%d closed.\n",
427                         c->c_connid, sd, 0, 0, 0 );
428         }
429
430         ber_pvt_sb_destroy( c->c_sb );
431
432     c->c_conn_state = SLAP_C_INVALID;
433     c->c_struct_state = SLAP_C_UNUSED;
434 }
435
436 int connection_state_closing( Connection *c )
437 {
438         /* c_mutex must be locked by caller */
439
440         int state;
441         assert( c != NULL );
442         assert( c->c_struct_state == SLAP_C_USED );
443
444         state = c->c_conn_state;
445
446         assert( state != SLAP_C_INVALID );
447
448         return state == SLAP_C_CLOSING;
449 }
450
451 void connection_closing( Connection *c )
452 {
453         assert( connections != NULL );
454         assert( c != NULL );
455         assert( c->c_struct_state == SLAP_C_USED );
456         assert( c->c_conn_state != SLAP_C_INVALID );
457
458         /* c_mutex must be locked by caller */
459
460         if( c->c_conn_state != SLAP_C_CLOSING ) {
461                 Operation *o;
462
463                 Debug( LDAP_DEBUG_TRACE,
464                         "connection_closing: readying conn=%ld sd=%d for close.\n",
465                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
466
467                 /* update state to closing */
468                 c->c_conn_state = SLAP_C_CLOSING;
469
470                 /* don't listen on this port anymore */
471                 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
472
473                 /* shutdown I/O -- not yet implemented */
474
475                 /* abandon active operations */
476                 for( o = c->c_ops; o != NULL; o = o->o_next ) {
477                         ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
478                         o->o_abandon = 1;
479                         ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
480                 }
481
482                 /* remove pending operations */
483                 for( o = slap_op_pop( &c->c_pending_ops );
484                         o != NULL;
485                         o = slap_op_pop( &c->c_pending_ops ) )
486                 {
487                         slap_op_free( o );
488                 }
489
490                 /* wake write blocked operations */
491                 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
492                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
493         }
494 }
495
496 static void connection_close( Connection *c )
497 {
498         assert( connections != NULL );
499         assert( c != NULL );
500         assert( c->c_struct_state == SLAP_C_USED );
501         assert( c->c_conn_state == SLAP_C_CLOSING );
502
503         /* note: connections_mutex and c_mutex should be locked by caller */
504
505         if( c->c_ops != NULL ) {
506                 Debug( LDAP_DEBUG_TRACE,
507                         "connection_close: deferring conn=%ld sd=%d.\n",
508                         c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
509
510                 return;
511         }
512
513         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
514                 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
515
516         connection_destroy( c );
517 }
518
519 unsigned long connections_nextid(void)
520 {
521         unsigned long id;
522         assert( connections != NULL );
523
524         ldap_pvt_thread_mutex_lock( &connections_mutex );
525
526         id = conn_nextid;
527
528         ldap_pvt_thread_mutex_unlock( &connections_mutex );
529
530         return id;
531 }
532
533 Connection* connection_first( ber_socket_t *index )
534 {
535         assert( connections != NULL );
536         assert( index != NULL );
537
538         ldap_pvt_thread_mutex_lock( &connections_mutex );
539
540         *index = 0;
541
542         return connection_next(NULL, index);
543 }
544
545 Connection* connection_next( Connection *c, ber_socket_t *index )
546 {
547         assert( connections != NULL );
548         assert( index != NULL );
549         assert( *index <= dtblsize );
550
551         if( c != NULL ) {
552                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
553         }
554
555         c = NULL;
556
557         for(; *index < dtblsize; (*index)++) {
558                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
559                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
560 #ifndef HAVE_WINSOCK
561                         continue;
562 #else
563                         break;
564 #endif
565                 }
566
567                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
568                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
569                         c = &connections[(*index)++];
570                         break;
571                 }
572
573                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
574                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
575         }
576
577         if( c != NULL ) {
578                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
579         }
580
581         return c;
582 }
583
584 void connection_done( Connection *c )
585 {
586         assert( connections != NULL );
587
588         if( c != NULL ) {
589                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
590         }
591
592         ldap_pvt_thread_mutex_unlock( &connections_mutex );
593 }
594
595 /*
596  * connection_activity - handle the request operation op on connection
597  * conn.  This routine figures out what kind of operation it is and
598  * calls the appropriate stub to handle it.
599  */
600
601 static void *
602 connection_operation( void *arg_v )
603 {
604         struct co_arg   *arg = arg_v;
605         ber_tag_t tag = arg->co_op->o_tag;
606         Connection *conn = arg->co_conn;
607
608 #ifdef LDAP_COUNTERS
609         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
610         num_ops_initiated++;
611         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
612 #endif
613
614         switch ( tag ) {
615         case LDAP_REQ_BIND:
616                 do_bind( conn, arg->co_op );
617                 break;
618
619 #ifdef LDAP_COMPAT30
620         case LDAP_REQ_UNBIND_30:
621 #endif
622         case LDAP_REQ_UNBIND:
623                 do_unbind( conn, arg->co_op );
624                 break;
625
626         case LDAP_REQ_ADD:
627                 do_add( conn, arg->co_op );
628                 break;
629
630 #ifdef LDAP_COMPAT30
631         case LDAP_REQ_DELETE_30:
632 #endif
633         case LDAP_REQ_DELETE:
634                 do_delete( conn, arg->co_op );
635                 break;
636
637         case LDAP_REQ_MODRDN:
638                 do_modrdn( conn, arg->co_op );
639                 break;
640
641         case LDAP_REQ_MODIFY:
642                 do_modify( conn, arg->co_op );
643                 break;
644
645         case LDAP_REQ_COMPARE:
646                 do_compare( conn, arg->co_op );
647                 break;
648
649         case LDAP_REQ_SEARCH:
650                 do_search( conn, arg->co_op );
651                 break;
652
653 #ifdef LDAP_COMPAT30
654         case LDAP_REQ_ABANDON_30:
655 #endif
656         case LDAP_REQ_ABANDON:
657                 do_abandon( conn, arg->co_op );
658                 break;
659
660         default:
661                 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
662                     arg->co_op->o_tag, 0, 0 );
663                 break;
664         }
665
666 #ifdef LDAP_COUNTERS
667         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
668         num_ops_completed++;
669         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
670 #endif
671
672         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
673
674 #ifdef LDAP_COUNTERS
675         conn->c_n_ops_completed++;
676 #endif
677
678         slap_op_remove( &conn->c_ops, arg->co_op );
679         slap_op_free( arg->co_op );
680         arg->co_op = NULL;
681         arg->co_conn = NULL;
682         free( (char *) arg );
683         arg = NULL;
684
685         switch( tag ) {
686 #ifdef LDAP_COMPAT30
687         case LDAP_REQ_UNBIND_30:
688 #endif
689         case LDAP_REQ_UNBIND:
690                 /* c_mutex is locked */
691                 connection_closing( conn );
692                 break;
693
694         case LDAP_REQ_BIND:
695                 if( conn->c_conn_state == SLAP_C_BINDING) {
696                         conn->c_conn_state = SLAP_C_ACTIVE;
697                 }
698         }
699
700         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
701         active_threads--;
702         if( active_threads < 1 ) {
703                 ldap_pvt_thread_cond_signal(&active_threads_cond);
704         }
705         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
706
707         connection_resched( conn );
708
709         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
710
711         return NULL;
712 }
713
714 int connection_read(ber_socket_t s)
715 {
716         int rc = 0;
717         Connection *c;
718         assert( connections != NULL );
719
720         ldap_pvt_thread_mutex_lock( &connections_mutex );
721
722         /* get (locked) connection */
723         c = connection_get( s );
724
725         if( c == NULL ) {
726                 Debug( LDAP_DEBUG_ANY,
727                         "connection_read(%ld): no connection!\n",
728                         (long) s, 0, 0 );
729
730                 slapd_remove(s, 0);
731
732                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
733                 return -1;
734         }
735
736         if( c->c_conn_state == SLAP_C_CLOSING ) {
737                 Debug( LDAP_DEBUG_TRACE,
738                         "connection_read(%d): closing, ignoring input for id=%ld\n",
739                         s, c->c_connid, 0 );
740
741                 connection_return( c );
742                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
743                 return 0;
744         }
745
746         Debug( LDAP_DEBUG_TRACE,
747                 "connection_read(%d): checking for input on id=%ld\n",
748                 s, c->c_connid, 0 );
749
750 #define CONNECTION_INPUT_LOOP 1
751
752 #ifdef DATA_READY_LOOP
753         while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
754 #elif CONNECTION_INPUT_LOOP
755         while(!rc)
756 #endif
757         {
758                 rc = connection_input( c );
759         }
760
761         if( rc < 0 ) {
762                 Debug( LDAP_DEBUG_TRACE,
763                         "connection_read(%d): input error=%d id=%ld, closing.\n",
764                         s, rc, c->c_connid );
765
766                 /* connections_mutex and c_mutex are locked */
767                 connection_closing( c );
768                 connection_close( c );
769         }
770
771         connection_return( c );
772         ldap_pvt_thread_mutex_unlock( &connections_mutex );
773         return 0;
774 }
775
776 static int
777 connection_input(
778     Connection *conn
779 )
780 {
781         Operation *op;
782         ber_tag_t       tag;
783         ber_len_t       len;
784         ber_int_t       msgid;
785         BerElement      *ber;
786
787         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
788             == NULL ) {
789                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
790                 return -1;
791         }
792
793         errno = 0;
794         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
795             != LDAP_TAG_MESSAGE )
796         {
797                 int err = errno;
798
799                 Debug( LDAP_DEBUG_TRACE,
800                         "ber_get_next on fd %d failed errno %d (%s)\n",
801                         ber_pvt_sb_get_desc( conn->c_sb ), err,
802                         err > -1 && err < sys_nerr ?  sys_errlist[err] : "unknown" );
803                 Debug( LDAP_DEBUG_TRACE,
804                         "\t*** got %ld of %lu so far\n",
805                         (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
806                         conn->c_currentber->ber_len, 0 );
807
808                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
809                         /* log, close and send error */
810                         ber_free( conn->c_currentber, 1 );
811                         conn->c_currentber = NULL;
812
813                         return -2;
814                 }
815                 return 1;
816         }
817
818         ber = conn->c_currentber;
819         conn->c_currentber = NULL;
820
821         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
822                 /* log, close and send error */
823                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
824                     0 );
825                 ber_free( ber, 1 );
826                 return -1;
827         }
828
829         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
830                 /* log, close and send error */
831                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
832                     0 );
833                 ber_free( ber, 1 );
834
835                 return -1;
836         }
837
838 #ifdef LDAP_COMPAT30
839         if ( conn->c_version == 30 ) {
840                 (void) ber_skip_tag( ber, &len );
841         }
842 #endif
843
844         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
845
846         if ( conn->c_conn_state == SLAP_C_BINDING
847                 || conn->c_conn_state == SLAP_C_CLOSING )
848         {
849                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
850                 slap_op_add( &conn->c_pending_ops, op );
851
852         } else {
853                 connection_op_activate( conn, op );
854         }
855
856 #ifdef NO_THREADS
857         if ( conn->c_struct_state != SLAP_C_USED ) {
858                 /* connection must have got closed underneath us */
859                 return 1;
860         }
861 #endif
862         assert( conn->c_struct_state == SLAP_C_USED );
863
864         return 0;
865 }
866
867 static int
868 connection_resched( Connection *conn )
869 {
870         Operation *op;
871
872         if( conn->c_conn_state == SLAP_C_CLOSING ) {
873                 Debug( LDAP_DEBUG_TRACE,
874                         "connection_resched: attempting closing conn=%ld sd=%d.\n",
875                         conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
876
877                 connection_close( conn );
878                 return 0;
879         }
880
881         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
882                 /* other states need different handling */
883                 return 0;
884         }
885
886         for( op = slap_op_pop( &conn->c_pending_ops );
887                 op != NULL;
888                 op = slap_op_pop( &conn->c_pending_ops ) )
889         {
890                 /* pending operations should not be marked for abandonment */
891                 assert(!op->o_abandon);
892
893                 connection_op_activate( conn, op );
894
895                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
896                         break;
897                 }
898         }
899         return 0;
900 }
901
902 static int connection_op_activate( Connection *conn, Operation *op )
903 {
904         struct co_arg *arg;
905         char *tmpdn;
906         int status;
907         ber_tag_t tag = op->o_tag;
908
909         if ( conn->c_dn != NULL ) {
910                 tmpdn = ch_strdup( conn->c_dn );
911         } else {
912                 tmpdn = NULL;
913         }
914
915         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
916         arg->co_conn = conn;
917         arg->co_op = op;
918
919         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
920         arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
921
922         slap_op_add( &conn->c_ops, arg->co_op );
923
924         if(tag == LDAP_REQ_BIND) {
925                 conn->c_conn_state = SLAP_C_BINDING;
926         }
927
928         if ( tmpdn != NULL ) {
929                 free( tmpdn );
930         }
931
932         ldap_pvt_thread_mutex_lock( &active_threads_mutex );
933         active_threads++;
934         ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
935
936         status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
937                                          connection_operation, (void *) arg );
938
939         if ( status != 0 ) {
940                 Debug( LDAP_DEBUG_ANY,
941                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
942
943                 /* should move op to pending list */
944         }
945
946         return status;
947 }
948
949 int connection_write(ber_socket_t s)
950 {
951         Connection *c;
952         assert( connections != NULL );
953
954         ldap_pvt_thread_mutex_lock( &connections_mutex );
955
956         c = connection_get( s );
957
958         slapd_clr_write( s, 0);
959
960         if( c == NULL ) {
961                 Debug( LDAP_DEBUG_ANY,
962                         "connection_write(%ld): no connection!\n",
963                         (long) s, 0, 0 );
964                 slapd_remove(s, 0);
965                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
966                 return -1;
967         }
968
969         Debug( LDAP_DEBUG_TRACE,
970                 "connection_write(%d): waking output for id=%ld\n",
971                 s, c->c_connid, 0 );
972
973         ldap_pvt_thread_cond_signal( &c->c_write_cv );
974
975         connection_return( c );
976         ldap_pvt_thread_mutex_unlock( &connections_mutex );
977         return 0;
978 }