]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Reworked thread code to better support thread-library specific
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <limits.h>
11
12 #include <ac/socket.h>
13 #include <ac/errno.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "slap.h"
19
20 /* protected by connections_mutex */
21 static ldap_pvt_thread_mutex_t connections_mutex;
22 static Connection *connections = NULL;
23 static unsigned long conn_nextid = 0;
24
25 /* structure state (protected by connections_mutex) */
26 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
27 #define SLAP_C_UNUSED                   0x01
28 #define SLAP_C_USED                             0x02
29
30 /* connection state (protected by c_mutex ) */
31 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
32 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
33 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
34 #define SLAP_C_BINDING                  0x03    /* binding */
35 #define SLAP_C_CLOSING                  0x04    /* closing */
36
37 const char *
38 connection_state2str( int state )
39 {
40         switch( state ) {
41         case SLAP_C_INVALID:    return "!";             
42         case SLAP_C_INACTIVE:   return "|";             
43         case SLAP_C_ACTIVE:             return "";                      
44         case SLAP_C_BINDING:    return "B";
45         case SLAP_C_CLOSING:    return "C";                     
46         }
47
48         return "?";
49 }
50
51 static Connection* connection_get( ber_socket_t s );
52
53 static int connection_input( Connection *c );
54 static void connection_close( Connection *c );
55
56 static int connection_op_activate( Connection *conn, Operation *op );
57 static int connection_resched( Connection *conn );
58 static void connection_abandon( Connection *conn );
59 static void connection_destroy( Connection *c );
60
61 struct co_arg {
62         Connection      *co_conn;
63         Operation       *co_op;
64 };
65
66 /*
67  * Initialize connection management infrastructure.
68  */
69 int connections_init(void)
70 {
71         assert( connections == NULL );
72
73         if( connections != NULL) {
74                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
75                         0, 0, 0 );
76                 return -1;
77         }
78
79         /* should check return of every call */
80         ldap_pvt_thread_mutex_init( &connections_mutex );
81
82         connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
83
84         if( connections == NULL ) {
85                 Debug( LDAP_DEBUG_ANY,
86                         "connections_init: allocation (%d*%ld) of connection array failed\n",
87                         dtblsize, (long) sizeof(Connection), 0 );
88                 return -1;
89         }
90
91     assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
92     assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
93
94         /*
95          * per entry initialization of the Connection array initialization
96          * will be done by connection_init()
97          */ 
98
99         return 0;
100 }
101
102 /*
103  * Destroy connection management infrastructure.
104  */
105 int connections_destroy(void)
106 {
107         ber_socket_t i;
108
109         /* should check return of every call */
110
111         if( connections == NULL) {
112                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
113                         0, 0, 0 );
114                 return -1;
115         }
116
117         for ( i = 0; i < dtblsize; i++ ) {
118                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
119                         ber_sockbuf_free( connections[i].c_sb );
120                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
121                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
122                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
123                 }
124         }
125
126         free( connections );
127         connections = NULL;
128
129         ldap_pvt_thread_mutex_destroy( &connections_mutex );
130         return 0;
131 }
132
133 /*
134  * shutdown all connections
135  */
136 int connections_shutdown(void)
137 {
138         ber_socket_t i;
139
140         ldap_pvt_thread_mutex_lock( &connections_mutex );
141
142         for ( i = 0; i < dtblsize; i++ ) {
143                 if( connections[i].c_struct_state != SLAP_C_USED ) {
144                         continue;
145                 }
146
147                 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
148
149                 /* connections_mutex and c_mutex are locked */
150                 connection_closing( &connections[i] );
151                 connection_close( &connections[i] );
152
153                 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
154         }
155
156         ldap_pvt_thread_mutex_unlock( &connections_mutex );
157
158         return 0;
159 }
160
161 /*
162  * Timeout idle connections.
163  */
164 int connections_timeout_idle(time_t now)
165 {
166         int i = 0;
167         int connindex;
168         Connection* c;
169
170         ldap_pvt_thread_mutex_lock( &connections_mutex );
171
172         for( c = connection_first( &connindex );
173                 c != NULL;
174                 c = connection_next( c, &connindex ) )
175         {
176                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
177                         /* close it */
178                         connection_closing( c );
179                         connection_close( c );
180                         i++;
181                 }
182         }
183         connection_done( c );
184
185         ldap_pvt_thread_mutex_unlock( &connections_mutex );
186
187         return i;
188 }
189
190 static Connection* connection_get( ber_socket_t s )
191 {
192         /* connections_mutex should be locked by caller */
193
194         Connection *c;
195
196         Debug( LDAP_DEBUG_ARGS,
197                 "connection_get(%ld)\n",
198                 (long) s, 0, 0 );
199
200         assert( connections != NULL );
201
202         if(s == AC_SOCKET_INVALID) {
203                 return NULL;
204         }
205
206 #ifndef HAVE_WINSOCK
207         c = &connections[s];
208
209         assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
210
211 #else
212         c = NULL;
213         {
214                 ber_socket_t i, sd;
215
216                 for(i=0; i<dtblsize; i++) {
217                         ber_sockbuf_ctrl( connections[i].c_sb,
218                                 LBER_SB_OPT_GET_FD, &sd );
219
220                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
221                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
222                                 assert( connections[i].c_sb == 0 );
223                                 break;
224                         }
225
226                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
227                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
228                                 assert( sd == AC_SOCKET_INVALID );
229                                 continue;
230                         }
231
232                         /* state can actually change from used -> unused by resched,
233                          * so don't assert details here.
234                          */
235
236                         if( sd == s ) {
237                                 c = &connections[i];
238                                 break;
239                         }
240                 }
241         }
242 #endif
243
244         if( c != NULL ) {
245                 ber_socket_t    sd;
246
247                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
248
249                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
250                 if( c->c_struct_state != SLAP_C_USED ) {
251                         /* connection must have been closed due to resched */
252
253                         assert( c->c_conn_state == SLAP_C_INVALID );
254                         assert( sd == AC_SOCKET_INVALID );
255
256                         Debug( LDAP_DEBUG_TRACE,
257                                 "connection_get(%d): connection not used\n",
258                                 s, 0, 0 );
259
260                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
261                         return NULL;
262                 }
263
264                 Debug( LDAP_DEBUG_TRACE,
265                         "connection_get(%d): got connid=%ld\n",
266                         s, c->c_connid, 0 );
267
268                 c->c_n_get++;
269
270                 assert( c->c_struct_state == SLAP_C_USED );
271                 assert( c->c_conn_state != SLAP_C_INVALID );
272                 assert( sd != AC_SOCKET_INVALID );
273
274         c->c_activitytime = slap_get_time();
275         }
276
277         return c;
278 }
279
280 static void connection_return( Connection *c )
281 {
282         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
283 }
284
285 long connection_init(
286         ber_socket_t s,
287         const char* url,
288         const char* dnsname,
289         const char* peername,
290         const char* sockname,
291         int use_tls )
292 {
293         unsigned long id;
294         Connection *c;
295
296         assert( connections != NULL );
297
298         assert( dnsname != NULL );
299         assert( peername != NULL );
300         assert( sockname != NULL );
301
302 #ifndef HAVE_TLS
303         assert( !use_tls );
304 #endif
305
306         if( s == AC_SOCKET_INVALID ) {
307         Debug( LDAP_DEBUG_ANY,
308                         "connection_init(%ld): invalid.\n",
309                         (long) s, 0, 0 );
310                 return -1;
311         }
312
313         assert( s >= 0 );
314 #ifndef HAVE_WINSOCK
315         assert( s < dtblsize );
316 #endif
317
318         ldap_pvt_thread_mutex_lock( &connections_mutex );
319
320 #ifndef HAVE_WINSOCK
321         c = &connections[s];
322
323 #else
324         {
325                 unsigned int i;
326
327                 c = NULL;
328
329         for( i=0; i < dtblsize; i++) {
330                 ber_socket_t    sd;
331
332             if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
333                 assert( connections[i].c_sb == 0 );
334                 c = &connections[i];
335                 break;
336             }
337
338                         sd = AC_SOCKET_INVALID;
339                         if (connections[i].c_sb != NULL)
340                         ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
341             
342             if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
343                 assert( sd == AC_SOCKET_INVALID );
344                 c = &connections[i];
345                 break;
346             }
347
348             assert( connections[i].c_struct_state == SLAP_C_USED );
349             assert( connections[i].c_conn_state != SLAP_C_INVALID );
350             assert( sd != AC_SOCKET_INVALID );
351         }
352
353         if( c == NULL ) {
354                 Debug( LDAP_DEBUG_ANY,
355                                 "connection_init(%d): connection table full (%d/%d)\n",
356                                 s, i, dtblsize);
357             ldap_pvt_thread_mutex_unlock( &connections_mutex );
358             return -1;
359         }
360     }
361 #endif
362
363     assert( c != NULL );
364
365     if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
366                 c->c_authmech = NULL;
367         c->c_dn = NULL;
368         c->c_cdn = NULL;
369
370                 c->c_listener_url = NULL;
371                 c->c_peer_domain = NULL;
372         c->c_peer_name = NULL;
373         c->c_sock_name = NULL;
374
375         c->c_ops = NULL;
376         c->c_pending_ops = NULL;
377
378                 c->c_sasl_bind_mech = NULL;
379 #ifdef HAVE_CYRUS_SASL
380                 c->c_sasl_bind_context = NULL;
381 #endif
382
383         c->c_sb = ber_sockbuf_alloc( );
384                 c->c_currentber = NULL;
385
386         /* should check status of thread calls */
387         ldap_pvt_thread_mutex_init( &c->c_mutex );
388         ldap_pvt_thread_mutex_init( &c->c_write_mutex );
389         ldap_pvt_thread_cond_init( &c->c_write_cv );
390
391         c->c_struct_state = SLAP_C_UNUSED;
392     }
393
394     ldap_pvt_thread_mutex_lock( &c->c_mutex );
395
396     assert( c->c_struct_state == SLAP_C_UNUSED );
397         assert( c->c_authmech == NULL );
398     assert(     c->c_dn == NULL );
399     assert(     c->c_cdn == NULL );
400     assert( c->c_listener_url == NULL );
401     assert( c->c_peer_domain == NULL );
402     assert( c->c_peer_name == NULL );
403     assert( c->c_sock_name == NULL );
404     assert( c->c_ops == NULL );
405     assert( c->c_pending_ops == NULL );
406         assert( c->c_sasl_bind_mech == NULL );
407 #ifdef HAVE_CYRUS_SASL
408         assert( c->c_sasl_bind_context == NULL );
409 #endif
410         assert( c->c_currentber == NULL );
411
412         c->c_listener_url = ch_strdup( url  );
413         c->c_peer_domain = ch_strdup( dnsname  );
414     c->c_peer_name = ch_strdup( peername  );
415     c->c_sock_name = ch_strdup( sockname );
416
417     c->c_n_ops_received = 0;
418     c->c_n_ops_executing = 0;
419     c->c_n_ops_pending = 0;
420     c->c_n_ops_completed = 0;
421
422         c->c_n_get = 0;
423         c->c_n_read = 0;
424         c->c_n_write = 0;
425
426         /* assume LDAPv3 until bind */
427         c->c_protocol = LDAP_VERSION3;
428
429     c->c_activitytime = c->c_starttime = slap_get_time();
430
431     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
432         (void *)&s );
433     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
434         LBER_SBIOD_LEVEL_PROVIDER, NULL );
435 #ifdef LDAP_DEBUG
436     ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
437 #endif
438     if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
439         Debug( LDAP_DEBUG_ANY,
440             "connection_init(%d, %s): set nonblocking failed\n",
441             s, c->c_peer_name, 0 );
442     }
443
444     id = c->c_connid = conn_nextid++;
445
446     c->c_conn_state = SLAP_C_INACTIVE;
447     c->c_struct_state = SLAP_C_USED;
448
449 #ifdef HAVE_TLS
450     if ( use_tls ) {
451             c->c_is_tls = 1;
452             c->c_needs_tls_accept = 1;
453     } else {
454             c->c_is_tls = 0;
455             c->c_needs_tls_accept = 0;
456     }
457 #endif
458
459     ldap_pvt_thread_mutex_unlock( &c->c_mutex );
460     ldap_pvt_thread_mutex_unlock( &connections_mutex );
461
462     backend_connection_init(c);
463
464     return id;
465 }
466
467 static void
468 connection_destroy( Connection *c )
469 {
470         /* note: connections_mutex should be locked by caller */
471     ber_socket_t        sd;
472
473     assert( connections != NULL );
474     assert( c != NULL );
475     assert( c->c_struct_state != SLAP_C_UNUSED );
476     assert( c->c_conn_state != SLAP_C_INVALID );
477     assert( c->c_ops == NULL );
478
479     backend_connection_destroy(c);
480
481     c->c_protocol = 0;
482     c->c_connid = -1;
483
484     c->c_activitytime = c->c_starttime = 0;
485
486         if(c->c_authmech != NULL ) {
487                 free(c->c_authmech);
488                 c->c_authmech = NULL;
489         }
490     if(c->c_dn != NULL) {
491         free(c->c_dn);
492         c->c_dn = NULL;
493     }
494         if(c->c_cdn != NULL) {
495                 free(c->c_cdn);
496                 c->c_cdn = NULL;
497         }
498         if(c->c_listener_url != NULL) {
499                 free(c->c_listener_url);
500                 c->c_listener_url = NULL;
501         }
502         if(c->c_peer_domain != NULL) {
503                 free(c->c_peer_domain);
504                 c->c_peer_domain = NULL;
505         }
506         if(c->c_peer_name != NULL) {
507 #ifdef LDAP_PF_UNIX
508                 /*
509                  * If peer was a domain socket, unlink. Mind you,
510                  * they may be un-named. Should we leave this to
511                  * the client?
512                  */
513                 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
514                         char *path = c->c_peer_name + 5;
515                         if (path != '\0') {
516                                 (void)unlink(path);
517                         }
518                 }
519 #endif /* LDAP_PF_UNIX */
520
521                 free(c->c_peer_name);
522                 c->c_peer_name = NULL;
523         }
524         if(c->c_sock_name != NULL) {
525                 free(c->c_sock_name);
526                 c->c_sock_name = NULL;
527         }
528
529         c->c_sasl_bind_in_progress = 0;
530         if(c->c_sasl_bind_mech != NULL) {
531                 free(c->c_sasl_bind_mech);
532                 c->c_sasl_bind_mech = NULL;
533         }
534 #ifdef HAVE_CYRUS_SASL
535         if(c->c_sasl_bind_context != NULL ) {
536                 sasl_dispose( &c->c_sasl_bind_context );
537                 c->c_sasl_bind_context = NULL;
538         }
539 #endif
540
541         if ( c->c_currentber != NULL ) {
542                 ber_free( c->c_currentber, 1 );
543                 c->c_currentber = NULL;
544         }
545
546         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
547         if ( sd != AC_SOCKET_INVALID ) {
548                 slapd_remove( sd, 0 );
549
550                 Statslog( LDAP_DEBUG_STATS,
551                     "conn=%ld fd=%d closed\n",
552                         c->c_connid, sd, 0, 0, 0 );
553         }
554
555         ber_sockbuf_free( c->c_sb );
556         c->c_sb = ber_sockbuf_alloc( );
557
558     c->c_conn_state = SLAP_C_INVALID;
559     c->c_struct_state = SLAP_C_UNUSED;
560 }
561
562 int connection_state_closing( Connection *c )
563 {
564         /* c_mutex must be locked by caller */
565
566         int state;
567         assert( c != NULL );
568         assert( c->c_struct_state == SLAP_C_USED );
569
570         state = c->c_conn_state;
571
572         assert( state != SLAP_C_INVALID );
573
574         return state == SLAP_C_CLOSING;
575 }
576
577 static void connection_abandon( Connection *c )
578 {
579         /* c_mutex must be locked by caller */
580
581         Operation *o;
582
583         for( o = c->c_ops; o != NULL; o = o->o_next ) {
584                 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
585                 o->o_abandon = 1;
586                 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
587         }
588
589         /* remove pending operations */
590         for( o = slap_op_pop( &c->c_pending_ops );
591                 o != NULL;
592                 o = slap_op_pop( &c->c_pending_ops ) )
593         {
594                 slap_op_free( o );
595         }
596 }
597
598 void connection_closing( Connection *c )
599 {
600         assert( connections != NULL );
601         assert( c != NULL );
602         assert( c->c_struct_state == SLAP_C_USED );
603         assert( c->c_conn_state != SLAP_C_INVALID );
604
605         /* c_mutex must be locked by caller */
606
607         if( c->c_conn_state != SLAP_C_CLOSING ) {
608                 ber_socket_t    sd;
609
610                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
611                 Debug( LDAP_DEBUG_TRACE,
612                         "connection_closing: readying conn=%ld sd=%d for close\n",
613                         c->c_connid, sd, 0 );
614
615                 /* update state to closing */
616                 c->c_conn_state = SLAP_C_CLOSING;
617
618                 /* don't listen on this port anymore */
619                 slapd_clr_read( sd, 1 );
620
621                 /* abandon active operations */
622                 connection_abandon( c );
623
624                 /* wake write blocked operations */
625                 slapd_clr_write( sd, 1 );
626                 ldap_pvt_thread_cond_signal( &c->c_write_cv );
627         }
628 }
629
630 static void connection_close( Connection *c )
631 {
632         ber_socket_t    sd;
633
634         assert( connections != NULL );
635         assert( c != NULL );
636         assert( c->c_struct_state == SLAP_C_USED );
637         assert( c->c_conn_state == SLAP_C_CLOSING );
638
639         /* note: connections_mutex and c_mutex should be locked by caller */
640
641         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
642         if( c->c_ops != NULL ) {
643                 Debug( LDAP_DEBUG_TRACE,
644                         "connection_close: deferring conn=%ld sd=%d\n",
645                         c->c_connid, sd, 0 );
646
647                 return;
648         }
649
650         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
651                 c->c_connid, sd, 0 );
652
653         connection_destroy( c );
654 }
655
656 unsigned long connections_nextid(void)
657 {
658         unsigned long id;
659         assert( connections != NULL );
660
661         ldap_pvt_thread_mutex_lock( &connections_mutex );
662
663         id = conn_nextid;
664
665         ldap_pvt_thread_mutex_unlock( &connections_mutex );
666
667         return id;
668 }
669
670 Connection* connection_first( ber_socket_t *index )
671 {
672         assert( connections != NULL );
673         assert( index != NULL );
674
675         ldap_pvt_thread_mutex_lock( &connections_mutex );
676
677         *index = 0;
678
679         return connection_next(NULL, index);
680 }
681
682 Connection* connection_next( Connection *c, ber_socket_t *index )
683 {
684         assert( connections != NULL );
685         assert( index != NULL );
686         assert( *index <= dtblsize );
687
688         if( c != NULL ) {
689                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
690         }
691
692         c = NULL;
693
694         for(; *index < dtblsize; (*index)++) {
695                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
696                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
697 #ifndef HAVE_WINSOCK
698                         continue;
699 #else
700                         break;
701 #endif
702                 }
703
704                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
705                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
706                         c = &connections[(*index)++];
707                         break;
708                 }
709
710                 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
711                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
712         }
713
714         if( c != NULL ) {
715                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
716         }
717
718         return c;
719 }
720
721 void connection_done( Connection *c )
722 {
723         assert( connections != NULL );
724
725         if( c != NULL ) {
726                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
727         }
728
729         ldap_pvt_thread_mutex_unlock( &connections_mutex );
730 }
731
732 /*
733  * connection_activity - handle the request operation op on connection
734  * conn.  This routine figures out what kind of operation it is and
735  * calls the appropriate stub to handle it.
736  */
737
738 static void *
739 connection_operation( void *arg_v )
740 {
741         int rc;
742         struct co_arg   *arg = arg_v;
743         ber_tag_t tag = arg->co_op->o_tag;
744         Connection *conn = arg->co_conn;
745
746         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
747         num_ops_initiated++;
748         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
749
750         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
751                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
752                         "error: SASL bind in progress (tag=%ld).\n",
753                         (long) tag, 0, 0 );
754                 send_ldap_result( conn, arg->co_op,
755                         rc = LDAP_OPERATIONS_ERROR,
756                         NULL, "SASL bind in progress", NULL, NULL );
757                 goto operations_error;
758         }
759
760         switch ( tag ) {
761         case LDAP_REQ_BIND:
762                 rc = do_bind( conn, arg->co_op );
763                 break;
764
765         case LDAP_REQ_UNBIND:
766                 rc = do_unbind( conn, arg->co_op );
767                 break;
768
769         case LDAP_REQ_ADD:
770                 rc = do_add( conn, arg->co_op );
771                 break;
772
773         case LDAP_REQ_DELETE:
774                 rc = do_delete( conn, arg->co_op );
775                 break;
776
777         case LDAP_REQ_MODRDN:
778                 rc = do_modrdn( conn, arg->co_op );
779                 break;
780
781         case LDAP_REQ_MODIFY:
782                 rc = do_modify( conn, arg->co_op );
783                 break;
784
785         case LDAP_REQ_COMPARE:
786                 rc = do_compare( conn, arg->co_op );
787                 break;
788
789         case LDAP_REQ_SEARCH:
790                 rc = do_search( conn, arg->co_op );
791                 break;
792
793         case LDAP_REQ_ABANDON:
794                 rc = do_abandon( conn, arg->co_op );
795                 break;
796
797         case LDAP_REQ_EXTENDED:
798                 rc = do_extended( conn, arg->co_op );
799                 break;
800
801         default:
802                 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
803                     tag, 0, 0 );
804                 arg->co_op->o_tag = LBER_ERROR;
805                 send_ldap_disconnect( conn, arg->co_op,
806                         LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
807                 rc = -1;
808                 break;
809         }
810
811         if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
812
813 operations_error:
814         ldap_pvt_thread_mutex_lock( &num_ops_mutex );
815         num_ops_completed++;
816         ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
817
818         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
819
820         conn->c_n_ops_executing--;
821         conn->c_n_ops_completed++;
822
823         slap_op_remove( &conn->c_ops, arg->co_op );
824         slap_op_free( arg->co_op );
825         arg->co_op = NULL;
826         arg->co_conn = NULL;
827         free( (char *) arg );
828         arg = NULL;
829
830         switch( tag ) {
831         case LBER_ERROR:
832         case LDAP_REQ_UNBIND:
833                 /* c_mutex is locked */
834                 connection_closing( conn );
835                 break;
836
837         case LDAP_REQ_BIND:
838                 conn->c_sasl_bind_in_progress =
839                         rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
840
841                 if( conn->c_conn_state == SLAP_C_BINDING) {
842                         conn->c_conn_state = SLAP_C_ACTIVE;
843                 }
844         }
845
846         connection_resched( conn );
847
848         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
849
850         return NULL;
851 }
852
853 int connection_read(ber_socket_t s)
854 {
855         int rc = 0;
856         Connection *c;
857         assert( connections != NULL );
858
859         ldap_pvt_thread_mutex_lock( &connections_mutex );
860
861         /* get (locked) connection */
862         c = connection_get( s );
863
864         if( c == NULL ) {
865                 Debug( LDAP_DEBUG_ANY,
866                         "connection_read(%ld): no connection!\n",
867                         (long) s, 0, 0 );
868
869                 slapd_remove(s, 0);
870
871                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
872                 return -1;
873         }
874
875         c->c_n_read++;
876
877         if( c->c_conn_state == SLAP_C_CLOSING ) {
878                 Debug( LDAP_DEBUG_TRACE,
879                         "connection_read(%d): closing, ignoring input for id=%ld\n",
880                         s, c->c_connid, 0 );
881
882                 connection_return( c );
883                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
884                 return 0;
885         }
886
887         Debug( LDAP_DEBUG_TRACE,
888                 "connection_read(%d): checking for input on id=%ld\n",
889                 s, c->c_connid, 0 );
890
891 #ifdef HAVE_TLS
892         if ( c->c_is_tls && c->c_needs_tls_accept ) {
893                 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
894                 if ( rc < 0 ) {
895                         struct timeval tv;
896                         fd_set rfd;
897
898                         Debug( LDAP_DEBUG_TRACE,
899                                "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
900                                s, rc, c->c_connid );
901
902                         c->c_needs_tls_accept = 0;
903                         /* connections_mutex and c_mutex are locked */
904                         connection_closing( c );
905
906                         /* Drain input before close, to allow SSL error codes
907                          * to propagate to client. */
908                         FD_ZERO(&rfd);
909                         FD_SET(s, &rfd);
910                         for (rc=1; rc>0;)
911                         {
912                             tv.tv_sec = 1;
913                             tv.tv_usec = 0;
914                             rc = select(s+1, &rfd, NULL, NULL, &tv);
915                             if (rc == 1)
916                                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
917                                     NULL);
918                         }
919                         connection_close( c );
920                 } else if ( rc == 0 ) {
921                         c->c_needs_tls_accept = 0;
922                 }
923                 connection_return( c );
924                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
925                 return 0;
926         }
927 #endif
928
929 #define CONNECTION_INPUT_LOOP 1
930
931 #ifdef DATA_READY_LOOP
932         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
933 #elif CONNECTION_INPUT_LOOP
934         while(!rc)
935 #endif
936         {
937                 /* How do we do this without getting into a busy loop ? */
938                 rc = connection_input( c );
939         }
940
941         if( rc < 0 ) {
942                 Debug( LDAP_DEBUG_TRACE,
943                         "connection_read(%d): input error=%d id=%ld, closing.\n",
944                         s, rc, c->c_connid );
945
946                 /* connections_mutex and c_mutex are locked */
947                 connection_closing( c );
948                 connection_close( c );
949         }
950
951         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
952                 slapd_set_read( s, 1 );
953         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
954                 slapd_set_write( s, 1 );
955         connection_return( c );
956         ldap_pvt_thread_mutex_unlock( &connections_mutex );
957         return 0;
958 }
959
960 static int
961 connection_input(
962     Connection *conn
963 )
964 {
965         Operation *op;
966         ber_tag_t       tag;
967         ber_len_t       len;
968         ber_int_t       msgid;
969         BerElement      *ber;
970
971         if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
972             == NULL ) {
973                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
974                 return -1;
975         }
976
977         errno = 0;
978         if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
979             != LDAP_TAG_MESSAGE )
980         {
981                 int err = errno;
982                 ber_socket_t    sd;
983
984                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
985
986                 Debug( LDAP_DEBUG_TRACE,
987                         "ber_get_next on fd %d failed errno=%d (%s)\n",
988                         sd, err, sock_errstr(err) );
989
990                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
991                         /* log, close and send error */
992                         ber_free( conn->c_currentber, 1 );
993                         conn->c_currentber = NULL;
994
995                         return -2;
996                 }
997                 return 1;
998         }
999
1000         ber = conn->c_currentber;
1001         conn->c_currentber = NULL;
1002
1003         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1004                 /* log, close and send error */
1005                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1006                     0 );
1007                 ber_free( ber, 1 );
1008                 return -1;
1009         }
1010
1011         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1012                 /* log, close and send error */
1013                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1014                     0 );
1015                 ber_free( ber, 1 );
1016
1017                 return -1;
1018         }
1019
1020         if(tag == LDAP_REQ_BIND) {
1021                 /* immediately abandon all exiting operations upon BIND */
1022                 connection_abandon( conn );
1023         }
1024
1025         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1026
1027         if ( conn->c_conn_state == SLAP_C_BINDING
1028                 || conn->c_conn_state == SLAP_C_CLOSING )
1029         {
1030                 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1031                 conn->c_n_ops_pending++;
1032                 slap_op_add( &conn->c_pending_ops, op );
1033
1034         } else {
1035                 conn->c_n_ops_executing++;
1036                 connection_op_activate( conn, op );
1037         }
1038
1039 #ifdef NO_THREADS
1040         if ( conn->c_struct_state != SLAP_C_USED ) {
1041                 /* connection must have got closed underneath us */
1042                 return 1;
1043         }
1044 #endif
1045         assert( conn->c_struct_state == SLAP_C_USED );
1046
1047         return 0;
1048 }
1049
1050 static int
1051 connection_resched( Connection *conn )
1052 {
1053         Operation *op;
1054
1055         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1056                 ber_socket_t    sd;
1057
1058                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1059                 Debug( LDAP_DEBUG_TRACE,
1060                         "connection_resched: attempting closing conn=%ld sd=%d\n",
1061                         conn->c_connid, sd, 0 );
1062
1063                 ldap_pvt_thread_mutex_lock( &connections_mutex );
1064                 connection_close( conn );
1065                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1066                 return 0;
1067         }
1068
1069         if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1070                 /* other states need different handling */
1071                 return 0;
1072         }
1073
1074         for( op = slap_op_pop( &conn->c_pending_ops );
1075                 op != NULL;
1076                 op = slap_op_pop( &conn->c_pending_ops ) )
1077         {
1078                 /* pending operations should not be marked for abandonment */
1079                 assert(!op->o_abandon);
1080
1081                 conn->c_n_ops_pending--;
1082                 conn->c_n_ops_executing++;
1083
1084                 connection_op_activate( conn, op );
1085
1086                 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1087                         break;
1088                 }
1089         }
1090         return 0;
1091 }
1092
1093 static int connection_op_activate( Connection *conn, Operation *op )
1094 {
1095         struct co_arg *arg;
1096         char *tmpdn;
1097         int status;
1098         ber_tag_t tag = op->o_tag;
1099
1100         if(tag == LDAP_REQ_BIND) {
1101                 conn->c_conn_state = SLAP_C_BINDING;
1102         }
1103
1104         if ( conn->c_dn != NULL ) {
1105                 tmpdn = ch_strdup( conn->c_dn );
1106         } else {
1107                 tmpdn = NULL;
1108         }
1109
1110         arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1111         arg->co_conn = conn;
1112         arg->co_op = op;
1113
1114         arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1115         arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1116         (void) dn_normalize( arg->co_op->o_ndn );
1117
1118         arg->co_op->o_protocol = conn->c_protocol;
1119         arg->co_op->o_connid = conn->c_connid;
1120
1121         arg->co_op->o_authtype = conn->c_authtype;
1122         arg->co_op->o_authmech = conn->c_authmech != NULL
1123                 ?  ch_strdup( conn->c_authmech ) : NULL;
1124         
1125         slap_op_add( &conn->c_ops, arg->co_op );
1126
1127         if( tmpdn != NULL ) {
1128                 free( tmpdn );
1129         }
1130
1131         status = ldap_pvt_thread_pool_submit( &connection_pool,
1132                 connection_operation, (void *) arg );
1133
1134         if ( status != 0 ) {
1135                 Debug( LDAP_DEBUG_ANY,
1136                 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1137
1138                 /* should move op to pending list */
1139         }
1140
1141         return status;
1142 }
1143
1144 int connection_write(ber_socket_t s)
1145 {
1146         Connection *c;
1147         assert( connections != NULL );
1148
1149         ldap_pvt_thread_mutex_lock( &connections_mutex );
1150
1151         c = connection_get( s );
1152
1153         slapd_clr_write( s, 0);
1154
1155         if( c == NULL ) {
1156                 Debug( LDAP_DEBUG_ANY,
1157                         "connection_write(%ld): no connection!\n",
1158                         (long) s, 0, 0 );
1159                 slapd_remove(s, 0);
1160                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1161                 return -1;
1162         }
1163
1164         c->c_n_write++;
1165
1166         Debug( LDAP_DEBUG_TRACE,
1167                 "connection_write(%d): waking output for id=%ld\n",
1168                 s, c->c_connid, 0 );
1169
1170         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1171
1172         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1173                 slapd_set_read( s, 1 );
1174         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1175                 slapd_set_write( s, 1 );
1176         connection_return( c );
1177         ldap_pvt_thread_mutex_unlock( &connections_mutex );
1178         return 0;
1179 }