]> git.sur5r.net Git - openldap/blob - servers/slapd/connection.c
Don't log EAGAIN returns from ber_get_next()
[openldap] / servers / slapd / connection.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #ifdef HAVE_LIMITS_H
30 #include <limits.h>
31 #endif
32
33 #include <ac/socket.h>
34 #include <ac/errno.h>
35 #include <ac/string.h>
36 #include <ac/time.h>
37 #include <ac/unistd.h>
38
39 #include "lutil.h"
40 #include "slap.h"
41
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45
46 /* protected by connections_mutex */
47 static ldap_pvt_thread_mutex_t connections_mutex;
48 static Connection *connections = NULL;
49
50 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51 static unsigned long conn_nextid = 0;
52
53 static const char conn_lost_str[] = "connection lost";
54
55 /* structure state (protected by connections_mutex) */
56 #define SLAP_C_UNINITIALIZED    0x00    /* MUST BE ZERO (0) */
57 #define SLAP_C_UNUSED                   0x01
58 #define SLAP_C_USED                             0x02
59 #define SLAP_C_PENDING                  0x03
60
61 /* connection state (protected by c_mutex ) */
62 #define SLAP_C_INVALID                  0x00    /* MUST BE ZERO (0) */
63 #define SLAP_C_INACTIVE                 0x01    /* zero threads */
64 #define SLAP_C_ACTIVE                   0x02    /* one or more threads */
65 #define SLAP_C_BINDING                  0x03    /* binding */
66 #define SLAP_C_CLOSING                  0x04    /* closing */
67 #define SLAP_C_CLIENT                   0x05    /* outbound client conn */
68
69 const char *
70 connection_state2str( int state )
71 {
72         switch( state ) {
73         case SLAP_C_INVALID:    return "!";
74         case SLAP_C_INACTIVE:   return "|";
75         case SLAP_C_ACTIVE:             return "";
76         case SLAP_C_BINDING:    return "B";
77         case SLAP_C_CLOSING:    return "C";
78         case SLAP_C_CLIENT:             return "L";
79         }
80
81         return "?";
82 }
83
84 static Connection* connection_get( ber_socket_t s );
85
86 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
87
88 typedef struct conn_readinfo {
89         Operation *op;
90         ldap_pvt_thread_start_t *func;
91         void *arg;
92         int nullop;
93 } conn_readinfo;
94
95 static int connection_input( Connection *c, conn_readinfo *cri );
96 #else
97 static int connection_input( Connection *c );
98 #endif
99 static void connection_close( Connection *c );
100
101 static int connection_op_activate( Operation *op );
102 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
103 static void connection_op_queue( Operation *op );
104 #endif
105 static int connection_resched( Connection *conn );
106 static void connection_abandon( Connection *conn );
107 static void connection_destroy( Connection *c );
108
109 static ldap_pvt_thread_start_t connection_operation;
110
111 /*
112  * Initialize connection management infrastructure.
113  */
114 int connections_init(void)
115 {
116         int i;
117
118         assert( connections == NULL );
119
120         if( connections != NULL) {
121                 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
122                         0, 0, 0 );
123                 return -1;
124         }
125
126         /* should check return of every call */
127         ldap_pvt_thread_mutex_init( &connections_mutex );
128         ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
129
130         connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
131
132         if( connections == NULL ) {
133                 Debug( LDAP_DEBUG_ANY, "connections_init: "
134                         "allocation (%d*%ld) of connection array failed\n",
135                         dtblsize, (long) sizeof(Connection), 0 );
136                 return -1;
137         }
138
139         assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
140         assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
141
142         for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
143
144         /*
145          * per entry initialization of the Connection array initialization
146          * will be done by connection_init()
147          */ 
148
149         return 0;
150 }
151
152 /*
153  * Destroy connection management infrastructure.
154  */
155
156 int connections_destroy(void)
157 {
158         ber_socket_t i;
159
160         /* should check return of every call */
161
162         if( connections == NULL) {
163                 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
164                         0, 0, 0 );
165                 return -1;
166         }
167
168         for ( i = 0; i < dtblsize; i++ ) {
169                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
170                         ber_sockbuf_free( connections[i].c_sb );
171                         ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
172                         ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
173                         ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
174 #ifdef LDAP_SLAPI
175                         if ( slapi_plugins_used ) {
176                                 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
177                                         &connections[i] );
178                         }
179 #endif
180                 }
181         }
182
183         free( connections );
184         connections = NULL;
185
186         ldap_pvt_thread_mutex_destroy( &connections_mutex );
187         ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
188         return 0;
189 }
190
191 /*
192  * shutdown all connections
193  */
194 int connections_shutdown(void)
195 {
196         ber_socket_t i;
197
198         for ( i = 0; i < dtblsize; i++ ) {
199                 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
200                         ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
201                         if( connections[i].c_struct_state == SLAP_C_USED ) {
202
203                                 /* give persistent clients a chance to cleanup */
204                                 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
205                                         ldap_pvt_thread_pool_submit( &connection_pool,
206                                         connections[i].c_clientfunc, connections[i].c_clientarg );
207                                 } else {
208                                         /* c_mutex is locked */
209                                         connection_closing( &connections[i], "slapd shutdown" );
210                                         connection_close( &connections[i] );
211                                 }
212                         }
213                         ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
214                 }
215         }
216
217         return 0;
218 }
219
220 /*
221  * Timeout idle connections.
222  */
223 int connections_timeout_idle(time_t now)
224 {
225         int i = 0;
226         int connindex;
227         Connection* c;
228
229         for( c = connection_first( &connindex );
230                 c != NULL;
231                 c = connection_next( c, &connindex ) )
232         {
233                 /* Don't timeout a slow-running request or a persistent
234                  * outbound connection */
235                 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
236                         continue;
237                 }
238
239                 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
240                         /* close it */
241                         connection_closing( c, "idletimeout" );
242                         connection_close( c );
243                         i++;
244                 }
245         }
246         connection_done( c );
247
248         return i;
249 }
250
251 static Connection* connection_get( ber_socket_t s )
252 {
253         Connection *c;
254
255         Debug( LDAP_DEBUG_ARGS,
256                 "connection_get(%ld)\n",
257                 (long) s, 0, 0 );
258
259         assert( connections != NULL );
260
261         if(s == AC_SOCKET_INVALID) return NULL;
262
263 #ifndef HAVE_WINSOCK
264         assert( s < dtblsize );
265         c = &connections[s];
266
267 #else
268         c = NULL;
269         {
270                 ber_socket_t i, sd;
271
272                 ldap_pvt_thread_mutex_lock( &connections_mutex );
273                 for(i=0; i<dtblsize; i++) {
274                         if( connections[i].c_struct_state == SLAP_C_PENDING )
275                                 continue;
276
277                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
278                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
279                                 assert( connections[i].c_sb == 0 );
280                                 break;
281                         }
282
283                         ber_sockbuf_ctrl( connections[i].c_sb,
284                                 LBER_SB_OPT_GET_FD, &sd );
285
286                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
287                                 assert( connections[i].c_conn_state == SLAP_C_INVALID );
288                                 assert( sd == AC_SOCKET_INVALID );
289                                 continue;
290                         }
291
292                         /* state can actually change from used -> unused by resched,
293                          * so don't assert details here.
294                          */
295
296                         if( sd == s ) {
297                                 c = &connections[i];
298                                 break;
299                         }
300                 }
301                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
302         }
303 #endif
304
305         if( c != NULL ) {
306                 ber_socket_t    sd;
307
308                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
309
310                 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
311
312                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
313                 if( c->c_struct_state != SLAP_C_USED ) {
314                         /* connection must have been closed due to resched */
315
316                         assert( c->c_conn_state == SLAP_C_INVALID );
317                         assert( sd == AC_SOCKET_INVALID );
318
319                         Debug( LDAP_DEBUG_TRACE,
320                                 "connection_get(%d): connection not used\n",
321                                 s, 0, 0 );
322
323                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
324                         return NULL;
325                 }
326
327                 Debug( LDAP_DEBUG_TRACE,
328                         "connection_get(%d): got connid=%lu\n",
329                         s, c->c_connid, 0 );
330
331                 c->c_n_get++;
332
333                 assert( c->c_struct_state == SLAP_C_USED );
334                 assert( c->c_conn_state != SLAP_C_INVALID );
335                 assert( sd != AC_SOCKET_INVALID );
336
337 #ifndef SLAPD_MONITOR
338                 if ( global_idletimeout > 0 )
339 #endif /* ! SLAPD_MONITOR */
340                 {
341                         c->c_activitytime = slap_get_time();
342                 }
343         }
344
345         return c;
346 }
347
348 static void connection_return( Connection *c )
349 {
350         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
351 }
352
353 long connection_init(
354         ber_socket_t s,
355         Listener *listener,
356         const char* dnsname,
357         const char* peername,
358         int flags,
359         slap_ssf_t ssf,
360         struct berval *authid )
361 {
362         unsigned long id;
363         Connection *c;
364         int doinit = 0;
365
366         assert( connections != NULL );
367
368         assert( listener != NULL );
369         assert( dnsname != NULL );
370         assert( peername != NULL );
371
372 #ifndef HAVE_TLS
373         assert( flags != CONN_IS_TLS );
374 #endif
375
376         if( s == AC_SOCKET_INVALID ) {
377                 Debug( LDAP_DEBUG_ANY,
378                         "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
379                 return -1;
380         }
381
382         assert( s >= 0 );
383 #ifndef HAVE_WINSOCK
384         assert( s < dtblsize );
385         c = &connections[s];
386         if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
387                 doinit = 1;
388         } else {
389                 assert( c->c_struct_state == SLAP_C_UNUSED );
390         }
391 #else
392         {
393                 ber_socket_t i;
394                 c = NULL;
395
396                 ldap_pvt_thread_mutex_lock( &connections_mutex );
397                 for( i=0; i < dtblsize; i++) {
398                         ber_socket_t    sd;
399
400                         if ( connections[i].c_struct_state == SLAP_C_PENDING )
401                                 continue;
402
403                         if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
404                                 assert( connections[i].c_sb == 0 );
405                                 c = &connections[i];
406                                 c->c_struct_state = SLAP_C_PENDING;
407                                 doinit = 1;
408                                 break;
409                         }
410
411                         sd = AC_SOCKET_INVALID;
412                         if (connections[i].c_sb != NULL) {
413                                 ber_sockbuf_ctrl( connections[i].c_sb,
414                                         LBER_SB_OPT_GET_FD, &sd );
415                         }
416
417                         if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
418                                 assert( sd == AC_SOCKET_INVALID );
419                                 c = &connections[i];
420                                 c->c_struct_state = SLAP_C_PENDING;
421                                 break;
422                         }
423
424                         if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
425
426                         assert( connections[i].c_struct_state == SLAP_C_USED );
427                         assert( connections[i].c_conn_state != SLAP_C_INVALID );
428                         assert( sd != AC_SOCKET_INVALID );
429                 }
430                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
431
432                 if( c == NULL ) {
433                         Debug( LDAP_DEBUG_ANY,
434                                 "connection_init(%d): connection table full "
435                                 "(%d/%d)\n", s, i, dtblsize);
436                         return -1;
437                 }
438         }
439 #endif
440
441         if( doinit ) {
442                 c->c_send_ldap_result = slap_send_ldap_result;
443                 c->c_send_search_entry = slap_send_search_entry;
444                 c->c_send_search_reference = slap_send_search_reference;
445                 c->c_send_ldap_extended = slap_send_ldap_extended;
446                 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
447
448                 BER_BVZERO( &c->c_authmech );
449                 BER_BVZERO( &c->c_dn );
450                 BER_BVZERO( &c->c_ndn );
451
452                 c->c_listener = NULL;
453                 BER_BVZERO( &c->c_peer_domain );
454                 BER_BVZERO( &c->c_peer_name );
455
456                 LDAP_STAILQ_INIT(&c->c_ops);
457                 LDAP_STAILQ_INIT(&c->c_pending_ops);
458
459 #ifdef LDAP_X_TXN
460                 c->c_txn = CONN_TXN_INACTIVE;
461                 c->c_txn_backend = NULL;
462                 LDAP_STAILQ_INIT(&c->c_txn_ops);
463 #endif
464
465                 BER_BVZERO( &c->c_sasl_bind_mech );
466                 c->c_sasl_done = 0;
467                 c->c_sasl_authctx = NULL;
468                 c->c_sasl_sockctx = NULL;
469                 c->c_sasl_extra = NULL;
470                 c->c_sasl_bindop = NULL;
471
472                 c->c_sb = ber_sockbuf_alloc( );
473
474                 {
475                         ber_len_t max = sockbuf_max_incoming;
476                         ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
477                 }
478
479                 c->c_currentber = NULL;
480
481                 /* should check status of thread calls */
482                 ldap_pvt_thread_mutex_init( &c->c_mutex );
483                 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
484                 ldap_pvt_thread_cond_init( &c->c_write_cv );
485
486 #ifdef LDAP_SLAPI
487                 if ( slapi_plugins_used ) {
488                         slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
489                 }
490 #endif
491         }
492
493         ldap_pvt_thread_mutex_lock( &c->c_mutex );
494
495         assert( BER_BVISNULL( &c->c_authmech ) );
496         assert( BER_BVISNULL( &c->c_dn ) );
497         assert( BER_BVISNULL( &c->c_ndn ) );
498         assert( c->c_listener == NULL );
499         assert( BER_BVISNULL( &c->c_peer_domain ) );
500         assert( BER_BVISNULL( &c->c_peer_name ) );
501         assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
502         assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
503 #ifdef LDAP_X_TXN
504         assert( c->c_txn == CONN_TXN_INACTIVE );
505         assert( c->c_txn_backend == NULL );
506         assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
507 #endif
508         assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
509         assert( c->c_sasl_done == 0 );
510         assert( c->c_sasl_authctx == NULL );
511         assert( c->c_sasl_sockctx == NULL );
512         assert( c->c_sasl_extra == NULL );
513         assert( c->c_sasl_bindop == NULL );
514         assert( c->c_currentber == NULL );
515         assert( c->c_writewaiter == 0);
516
517         c->c_listener = listener;
518
519         if ( flags == CONN_IS_CLIENT ) {
520                 c->c_conn_state = SLAP_C_CLIENT;
521                 c->c_struct_state = SLAP_C_USED;
522                 c->c_close_reason = "?";                        /* should never be needed */
523                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
524                 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
525
526                 return 0;
527         }
528
529         ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
530         ber_str2bv( peername, 0, 1, &c->c_peer_name );
531
532         c->c_n_ops_received = 0;
533         c->c_n_ops_executing = 0;
534         c->c_n_ops_pending = 0;
535         c->c_n_ops_completed = 0;
536
537         c->c_n_get = 0;
538         c->c_n_read = 0;
539         c->c_n_write = 0;
540
541         /* set to zero until bind, implies LDAP_VERSION3 */
542         c->c_protocol = 0;
543
544 #ifndef SLAPD_MONITOR
545         if ( global_idletimeout > 0 )
546 #endif /* ! SLAPD_MONITOR */
547         {
548                 c->c_activitytime = c->c_starttime = slap_get_time();
549         }
550
551 #ifdef LDAP_CONNECTIONLESS
552         c->c_is_udp = 0;
553         if( flags == CONN_IS_UDP ) {
554                 c->c_is_udp = 1;
555 #ifdef LDAP_DEBUG
556                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
557                         LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
558 #endif
559                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
560                         LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
561                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
562                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
563         } else
564 #endif
565         {
566 #ifdef LDAP_DEBUG
567                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
568                         LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
569 #endif
570                 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
571                         LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
572         }
573
574 #ifdef LDAP_DEBUG
575         ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
576                 INT_MAX, (void*)"ldap_" );
577 #endif
578
579         if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
580                 c /* non-NULL */ ) < 0 )
581         {
582                 Debug( LDAP_DEBUG_ANY,
583                         "connection_init(%d, %s): set nonblocking failed\n",
584                         s, c->c_peer_name.bv_val, 0 );
585         }
586
587         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
588         id = c->c_connid = conn_nextid++;
589         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
590
591         c->c_conn_state = SLAP_C_INACTIVE;
592         c->c_struct_state = SLAP_C_USED;
593         c->c_close_reason = "?";                        /* should never be needed */
594
595         c->c_ssf = c->c_transport_ssf = ssf;
596         c->c_tls_ssf = 0;
597
598 #ifdef HAVE_TLS
599         if ( flags == CONN_IS_TLS ) {
600                 c->c_is_tls = 1;
601                 c->c_needs_tls_accept = 1;
602         } else {
603                 c->c_is_tls = 0;
604                 c->c_needs_tls_accept = 0;
605         }
606 #endif
607
608         slap_sasl_open( c, 0 );
609         slap_sasl_external( c, ssf, authid );
610
611         slapd_add_internal( s, 1 );
612         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
613
614         backend_connection_init(c);
615
616         return id;
617 }
618
619 void connection2anonymous( Connection *c )
620 {
621         assert( connections != NULL );
622         assert( c != NULL );
623
624         {
625                 ber_len_t max = sockbuf_max_incoming;
626                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
627         }
628
629         if ( !BER_BVISNULL( &c->c_authmech ) ) {
630                 ch_free(c->c_authmech.bv_val);
631         }
632         BER_BVZERO( &c->c_authmech );
633
634         if ( !BER_BVISNULL( &c->c_dn ) ) {
635                 ch_free(c->c_dn.bv_val);
636         }
637         BER_BVZERO( &c->c_dn );
638
639         if ( !BER_BVISNULL( &c->c_ndn ) ) {
640                 ch_free(c->c_ndn.bv_val);
641         }
642         BER_BVZERO( &c->c_ndn );
643
644         if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
645                 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
646         }
647         BER_BVZERO( &c->c_sasl_authz_dn );
648
649         c->c_authz_backend = NULL;
650 }
651
652 static void
653 connection_destroy( Connection *c )
654 {
655         ber_socket_t    sd;
656         unsigned long   connid;
657         const char              *close_reason;
658         Sockbuf                 *sb;
659
660         assert( connections != NULL );
661         assert( c != NULL );
662         assert( c->c_struct_state != SLAP_C_UNUSED );
663         assert( c->c_conn_state != SLAP_C_INVALID );
664         assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
665         assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
666 #ifdef LDAP_X_TXN
667         assert( c->c_txn == CONN_TXN_INACTIVE );
668         assert( c->c_txn_backend == NULL );
669         assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
670 #endif
671         assert( c->c_writewaiter == 0);
672
673         /* only for stats (print -1 as "%lu" may give unexpected results ;) */
674         connid = c->c_connid;
675         close_reason = c->c_close_reason;
676
677         ldap_pvt_thread_mutex_lock( &connections_mutex );
678         c->c_struct_state = SLAP_C_PENDING;
679         ldap_pvt_thread_mutex_unlock( &connections_mutex );
680
681         backend_connection_destroy(c);
682
683         c->c_protocol = 0;
684         c->c_connid = -1;
685
686         c->c_activitytime = c->c_starttime = 0;
687
688         connection2anonymous( c );
689         c->c_listener = NULL;
690
691         if(c->c_peer_domain.bv_val != NULL) {
692                 free(c->c_peer_domain.bv_val);
693         }
694         BER_BVZERO( &c->c_peer_domain );
695         if(c->c_peer_name.bv_val != NULL) {
696                 free(c->c_peer_name.bv_val);
697         }
698         BER_BVZERO( &c->c_peer_name );
699
700         c->c_sasl_bind_in_progress = 0;
701         if(c->c_sasl_bind_mech.bv_val != NULL) {
702                 free(c->c_sasl_bind_mech.bv_val);
703         }
704         BER_BVZERO( &c->c_sasl_bind_mech );
705
706         slap_sasl_close( c );
707
708         if ( c->c_currentber != NULL ) {
709                 ber_free( c->c_currentber, 1 );
710                 c->c_currentber = NULL;
711         }
712
713
714 #ifdef LDAP_SLAPI
715         /* call destructors, then constructors; avoids unnecessary allocation */
716         if ( slapi_plugins_used ) {
717                 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
718         }
719 #endif
720
721         c->c_conn_state = SLAP_C_INVALID;
722         c->c_struct_state = SLAP_C_UNUSED;
723         c->c_close_reason = "?";                        /* should never be needed */
724
725         sb = c->c_sb;
726         c->c_sb = ber_sockbuf_alloc( );
727         {
728                 ber_len_t max = sockbuf_max_incoming;
729                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
730         }
731
732         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
733
734         /* c must be fully reset by this point; when we call slapd_remove
735          * it may get immediately reused by a new connection.
736          */
737         if ( sd != AC_SOCKET_INVALID ) {
738                 slapd_remove( sd, sb, 1, 0, 0 );
739
740                 if ( close_reason == NULL ) {
741                         close_reason = "";
742                 }
743
744                 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
745                         connid, (long) sd, close_reason, 0, 0 );
746         }
747 }
748
749 int connection_state_closing( Connection *c )
750 {
751         /* c_mutex must be locked by caller */
752
753         int state;
754         assert( c != NULL );
755         assert( c->c_struct_state == SLAP_C_USED );
756
757         state = c->c_conn_state;
758
759         assert( state != SLAP_C_INVALID );
760
761         return state == SLAP_C_CLOSING;
762 }
763
764 static void connection_abandon( Connection *c )
765 {
766         /* c_mutex must be locked by caller */
767
768         Operation *o, *next, op = {0};
769         Opheader ohdr = {0};
770         SlapReply rs = {0};
771
772         op.o_hdr = &ohdr;
773         op.o_conn = c;
774         op.o_connid = c->c_connid;
775         op.o_tag = LDAP_REQ_ABANDON;
776
777         for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
778                 next = LDAP_STAILQ_NEXT( o, o_next );
779                 op.orn_msgid = o->o_msgid;
780                 o->o_abandon = 1;
781                 op.o_bd = frontendDB;
782                 frontendDB->be_abandon( &op, &rs );
783         }
784
785 #ifdef LDAP_X_TXN
786         /* remove operations in pending transaction */
787         while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
788                 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
789                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
790                 slap_op_free( o );
791         }
792
793         /* clear transaction */
794         c->c_txn_backend = NULL;
795         c->c_txn = CONN_TXN_INACTIVE;
796 #endif
797
798         /* remove pending operations */
799         while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
800                 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
801                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
802                 slap_op_free( o );
803         }
804 }
805
806 void connection_closing( Connection *c, const char *why )
807 {
808         assert( connections != NULL );
809         assert( c != NULL );
810         assert( c->c_struct_state == SLAP_C_USED );
811         assert( c->c_conn_state != SLAP_C_INVALID );
812
813         /* c_mutex must be locked by caller */
814
815         if( c->c_conn_state != SLAP_C_CLOSING ) {
816                 ber_socket_t    sd;
817
818                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
819                 Debug( LDAP_DEBUG_TRACE,
820                         "connection_closing: readying conn=%lu sd=%d for close\n",
821                         c->c_connid, sd, 0 );
822                 /* update state to closing */
823                 c->c_conn_state = SLAP_C_CLOSING;
824                 c->c_close_reason = why;
825
826                 /* don't listen on this port anymore */
827                 slapd_clr_read( sd, 0 );
828
829                 /* abandon active operations */
830                 connection_abandon( c );
831
832                 /* wake write blocked operations */
833                 if ( c->c_writewaiter ) {
834                         ldap_pvt_thread_cond_signal( &c->c_write_cv );
835                         /* ITS#4667 this may allow another thread to drop into
836                          * connection_resched / connection_close before we
837                          * finish, but that's OK.
838                          */
839                         slapd_clr_write( sd, 1 );
840                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
841                         ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
842                         ldap_pvt_thread_mutex_lock( &c->c_mutex );
843                         ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
844                 } else {
845                         slapd_clr_write( sd, 1 );
846                 }
847
848         } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
849                 /* Client closed connection after doing Unbind. */
850                 c->c_close_reason = NULL;
851         }
852 }
853
854 static void
855 connection_close( Connection *c )
856 {
857         ber_socket_t    sd = AC_SOCKET_INVALID;
858
859         assert( connections != NULL );
860         assert( c != NULL );
861
862         /* ITS#4667 we may have gotten here twice */
863         if ( c->c_conn_state == SLAP_C_INVALID )
864                 return;
865
866         assert( c->c_struct_state == SLAP_C_USED );
867         assert( c->c_conn_state == SLAP_C_CLOSING );
868
869         /* NOTE: c_mutex should be locked by caller */
870
871         /* NOTE: don't get the file descriptor if not needed */
872 #ifdef LDAP_DEBUG
873         if ( slap_debug & LDAP_DEBUG_TRACE ) {
874                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
875         }
876 #endif /* LDAP_DEBUG */
877
878         if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
879                 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
880         {
881                 Debug( LDAP_DEBUG_TRACE,
882                         "connection_close: deferring conn=%lu sd=%d\n",
883                         c->c_connid, sd, 0 );
884                 return;
885         }
886
887         Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
888                 c->c_connid, sd, 0 );
889
890         connection_destroy( c );
891 }
892
893 unsigned long connections_nextid(void)
894 {
895         unsigned long id;
896         assert( connections != NULL );
897
898         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
899
900         id = conn_nextid;
901
902         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
903
904         return id;
905 }
906
907 Connection* connection_first( ber_socket_t *index )
908 {
909         assert( connections != NULL );
910         assert( index != NULL );
911
912         ldap_pvt_thread_mutex_lock( &connections_mutex );
913         for( *index = 0; *index < dtblsize; (*index)++) {
914                 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
915                         break;
916                 }
917         }
918         ldap_pvt_thread_mutex_unlock( &connections_mutex );
919
920         return connection_next(NULL, index);
921 }
922
923 Connection* connection_next( Connection *c, ber_socket_t *index )
924 {
925         assert( connections != NULL );
926         assert( index != NULL );
927         assert( *index <= dtblsize );
928
929         if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
930
931         c = NULL;
932
933         ldap_pvt_thread_mutex_lock( &connections_mutex );
934         for(; *index < dtblsize; (*index)++) {
935                 int c_struct;
936                 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
937                         assert( connections[*index].c_conn_state == SLAP_C_INVALID );
938 #ifdef HAVE_WINSOCK
939                         break;
940 #else
941                         continue;
942 #endif
943                 }
944
945                 if( connections[*index].c_struct_state == SLAP_C_USED ) {
946                         assert( connections[*index].c_conn_state != SLAP_C_INVALID );
947                         c = &connections[(*index)++];
948                         if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
949                                 /* avoid deadlock */
950                                 ldap_pvt_thread_mutex_unlock( &connections_mutex );
951                                 ldap_pvt_thread_mutex_lock( &c->c_mutex );
952                                 ldap_pvt_thread_mutex_lock( &connections_mutex );
953                                 if ( c->c_struct_state != SLAP_C_USED ) {
954                                         ldap_pvt_thread_mutex_unlock( &c->c_mutex );
955                                         c = NULL;
956                                         continue;
957                                 }
958                         }
959                         break;
960                 }
961
962                 c_struct = connections[*index].c_struct_state;
963                 if ( c_struct == SLAP_C_PENDING )
964                         continue;
965                 assert( c_struct == SLAP_C_UNUSED );
966                 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
967         }
968
969         ldap_pvt_thread_mutex_unlock( &connections_mutex );
970         return c;
971 }
972
973 void connection_done( Connection *c )
974 {
975         assert( connections != NULL );
976
977         if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
978 }
979
980 /*
981  * connection_activity - handle the request operation op on connection
982  * conn.  This routine figures out what kind of operation it is and
983  * calls the appropriate stub to handle it.
984  */
985
986 #ifdef SLAPD_MONITOR
987 /* FIXME: returns 0 in case of failure */
988 #define INCR_OP_INITIATED(index) \
989         do { \
990                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
991                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
992                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
993         } while (0)
994 #define INCR_OP_COMPLETED(index) \
995         do { \
996                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
997                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
998                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
999                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1000         } while (0)
1001 #else /* !SLAPD_MONITOR */
1002 #define INCR_OP_INITIATED(index) do { } while (0)
1003 #define INCR_OP_COMPLETED(index) \
1004         do { \
1005                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1006                 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1007                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1008         } while (0)
1009 #endif /* !SLAPD_MONITOR */
1010
1011 /*
1012  * NOTE: keep in sync with enum in slapd.h
1013  */
1014 static BI_op_func *opfun[] = {
1015         do_bind,
1016         do_unbind,
1017         do_add,
1018         do_delete,
1019         do_modrdn,
1020         do_modify,
1021         do_compare,
1022         do_search,
1023         do_abandon,
1024         do_extended,
1025         NULL
1026 };
1027
1028 static void *
1029 connection_operation( void *ctx, void *arg_v )
1030 {
1031         int rc = LDAP_OTHER;
1032         Operation *op = arg_v;
1033         SlapReply rs = {REP_RESULT};
1034         ber_tag_t tag = op->o_tag;
1035         slap_op_t opidx = SLAP_OP_LAST;
1036         Connection *conn = op->o_conn;
1037         void *memctx = NULL;
1038         void *memctx_null = NULL;
1039         ber_len_t memsiz;
1040
1041         ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1042         /* FIXME: returns 0 in case of failure */
1043         ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1044         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1045
1046         op->o_threadctx = ctx;
1047 #ifdef LDAP_DEVEL
1048         op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1049 #endif /* LDAP_DEVEL */
1050
1051         switch ( tag ) {
1052         case LDAP_REQ_BIND:
1053         case LDAP_REQ_UNBIND:
1054         case LDAP_REQ_ADD:
1055         case LDAP_REQ_DELETE:
1056         case LDAP_REQ_MODDN:
1057         case LDAP_REQ_MODIFY:
1058         case LDAP_REQ_COMPARE:
1059         case LDAP_REQ_SEARCH:
1060         case LDAP_REQ_ABANDON:
1061         case LDAP_REQ_EXTENDED:
1062                 break;
1063         default:
1064                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1065                         "conn %lu unknown LDAP request 0x%lx\n",
1066                         conn->c_connid, tag, 0 );
1067                 op->o_tag = LBER_ERROR;
1068                 rs.sr_err = LDAP_PROTOCOL_ERROR;
1069                 rs.sr_text = "unknown LDAP request";
1070                 send_ldap_disconnect( op, &rs );
1071                 rc = SLAPD_DISCONNECT;
1072                 goto operations_error;
1073         }
1074
1075         if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1076                 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1077                         "error: SASL bind in progress (tag=%ld).\n",
1078                         (long) tag, 0, 0 );
1079                 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1080                         "SASL bind in progress" );
1081                 rc = LDAP_OPERATIONS_ERROR;
1082                 goto operations_error;
1083         }
1084
1085 #ifdef LDAP_X_TXN
1086         if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1087                 ( tag == LDAP_REQ_ADD ) ||
1088                 ( tag == LDAP_REQ_DELETE ) ||
1089                 ( tag == LDAP_REQ_MODIFY ) ||
1090                 ( tag == LDAP_REQ_MODRDN )))
1091         {
1092                 /* Disable SLAB allocator for all update operations
1093                         issued inside of a transaction */
1094                 op->o_tmpmemctx = NULL;
1095                 op->o_tmpmfuncs = &ch_mfuncs;
1096         } else
1097 #endif
1098         {
1099         /* We can use Thread-Local storage for most mallocs. We can
1100          * also use TL for ber parsing, but not on Add or Modify.
1101          */
1102 #if 0
1103         memsiz = ber_len( op->o_ber ) * 64;
1104         if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1105 #endif
1106         memsiz = SLAP_SLAB_SIZE;
1107
1108         memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1109         op->o_tmpmemctx = memctx;
1110         op->o_tmpmfuncs = &slap_sl_mfuncs;
1111         if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1112                 /* Note - the ber and its buffer are already allocated from
1113                  * regular memory; this only affects subsequent mallocs that
1114                  * ber_scanf may invoke.
1115                  */
1116                 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1117         }
1118         }
1119
1120         opidx = slap_req2op( tag );
1121         assert( opidx != SLAP_OP_LAST );
1122         INCR_OP_INITIATED( opidx );
1123         rc = (*(opfun[opidx]))( op, &rs );
1124
1125 operations_error:
1126         if ( rc == SLAPD_DISCONNECT ) {
1127                 tag = LBER_ERROR;
1128
1129         } else if ( opidx != SLAP_OP_LAST ) {
1130                 /* increment completed operations count 
1131                  * only if operation was initiated
1132                  * and rc != SLAPD_DISCONNECT */
1133                 INCR_OP_COMPLETED( opidx );
1134         }
1135
1136         if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1137                 if ( rc == SLAPD_ABANDON ) {
1138                         op->o_cancel = SLAP_CANCEL_ACK;
1139                 } else {
1140                         op->o_cancel = LDAP_TOO_LATE;
1141                 }
1142         }
1143
1144         while ( op->o_cancel != SLAP_CANCEL_NONE &&
1145                 op->o_cancel != SLAP_CANCEL_DONE )
1146         {
1147                 ldap_pvt_thread_yield();
1148         }
1149
1150         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1151
1152         ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1153
1154         LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1155         LDAP_STAILQ_NEXT(op, o_next) = NULL;
1156         slap_op_free( op );
1157         conn->c_n_ops_executing--;
1158         conn->c_n_ops_completed++;
1159
1160         switch( tag ) {
1161         case LBER_ERROR:
1162         case LDAP_REQ_UNBIND:
1163                 /* c_mutex is locked */
1164                 connection_closing( conn,
1165                         tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1166                 break;
1167         }
1168
1169         connection_resched( conn );
1170         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1171         return NULL;
1172 }
1173
1174 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1175
1176 int connection_client_setup(
1177         ber_socket_t s,
1178         ldap_pvt_thread_start_t *func,
1179         void *arg )
1180 {
1181         int rc;
1182         Connection *c;
1183
1184         rc = connection_init( s, (Listener *)&dummy_list, "", "",
1185                 CONN_IS_CLIENT, 0, NULL );
1186         if ( rc < 0 ) return -1;
1187
1188         c = connection_get( s );
1189         c->c_clientfunc = func;
1190         c->c_clientarg = arg;
1191
1192         slapd_add_internal( s, 0 );
1193         slapd_set_read( s, 1 );
1194         connection_return( c );
1195         return 0;
1196 }
1197
1198 void connection_client_enable(
1199         ber_socket_t s )
1200 {
1201         slapd_set_read( s, 1 );
1202 }
1203
1204 void connection_client_stop(
1205         ber_socket_t s )
1206 {
1207         Connection *c;
1208         Sockbuf *sb;
1209
1210         /* get (locked) connection */
1211         c = connection_get( s );
1212         
1213         assert( c->c_conn_state == SLAP_C_CLIENT );
1214
1215         c->c_listener = NULL;
1216         c->c_conn_state = SLAP_C_INVALID;
1217         c->c_struct_state = SLAP_C_UNUSED;
1218         c->c_close_reason = "?";                        /* should never be needed */
1219         sb = c->c_sb;
1220         c->c_sb = ber_sockbuf_alloc( );
1221         {
1222                 ber_len_t max = sockbuf_max_incoming;
1223                 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1224         }
1225         slapd_remove( s, sb, 0, 1, 0 );
1226
1227         connection_return( c );
1228 }
1229
1230 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1231
1232 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1233
1234 static void* connection_read_thread( void* ctx, void* argv )
1235 {
1236         int rc ;
1237         conn_readinfo cri = { NULL, NULL, NULL, 0 };
1238         ber_socket_t s = (long)argv;
1239
1240         /*
1241          * read incoming LDAP requests. If there is more than one,
1242          * the first one is returned with new_op
1243          */
1244         if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1245                 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1246                 return (void*)(long)rc;
1247         }
1248
1249         /* execute a single queued request in the same thread */
1250         if( cri.op && !cri.nullop ) {
1251                 rc = (long)connection_operation( ctx, cri.op );
1252         } else if ( cri.func ) {
1253                 rc = (long)cri.func( ctx, cri.arg );
1254         }
1255
1256         return (void*)(long)rc;
1257 }
1258
1259 int connection_read_activate( ber_socket_t s )
1260 {
1261         int rc;
1262
1263         /*
1264          * suspend reading on this file descriptor until a connection processing
1265          * thread reads data on it. Otherwise the listener thread will repeatedly
1266          * submit the same event on it to the pool.
1267          */
1268         rc = slapd_clr_read( s, 0 );
1269         if ( rc )
1270                 return rc;
1271
1272         rc = ldap_pvt_thread_pool_submit( &connection_pool,
1273                 connection_read_thread, (void *)(long)s );
1274
1275         if( rc != 0 ) {
1276                 Debug( LDAP_DEBUG_ANY,
1277                         "connection_read_activate(%d): submit failed (%d)\n",
1278                         s, rc, 0 );
1279         }
1280
1281         return rc;
1282 }
1283 #endif
1284
1285 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1286 static int
1287 connection_read( ber_socket_t s, conn_readinfo *cri )
1288 #else
1289 int connection_read(ber_socket_t s)
1290 #endif
1291 {
1292         int rc = 0;
1293         Connection *c;
1294
1295         assert( connections != NULL );
1296
1297         /* get (locked) connection */
1298         c = connection_get( s );
1299
1300         if( c == NULL ) {
1301                 Debug( LDAP_DEBUG_ANY,
1302                         "connection_read(%ld): no connection!\n",
1303                         (long) s, 0, 0 );
1304
1305                 return -1;
1306         }
1307
1308         c->c_n_read++;
1309
1310         if( c->c_conn_state == SLAP_C_CLOSING ) {
1311                 Debug( LDAP_DEBUG_TRACE,
1312                         "connection_read(%d): closing, ignoring input for id=%lu\n",
1313                         s, c->c_connid, 0 );
1314                 connection_return( c );
1315                 return 0;
1316         }
1317
1318         if ( c->c_conn_state == SLAP_C_CLIENT ) {
1319 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1320                 cri->func = c->c_clientfunc;
1321                 cri->arg = c->c_clientarg;
1322                 /* read should already be cleared */
1323 #else
1324                 slapd_clr_read( s, 0 );
1325                 ldap_pvt_thread_pool_submit( &connection_pool,
1326                         c->c_clientfunc, c->c_clientarg );
1327 #endif
1328                 connection_return( c );
1329                 return 0;
1330         }
1331
1332         Debug( LDAP_DEBUG_TRACE,
1333                 "connection_read(%d): checking for input on id=%lu\n",
1334                 s, c->c_connid, 0 );
1335
1336 #ifdef HAVE_TLS
1337         if ( c->c_is_tls && c->c_needs_tls_accept ) {
1338                 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1339                 if ( rc < 0 ) {
1340                         Debug( LDAP_DEBUG_TRACE,
1341                                 "connection_read(%d): TLS accept failure "
1342                                 "error=%d id=%lu, closing\n",
1343                                 s, rc, c->c_connid );
1344
1345                         c->c_needs_tls_accept = 0;
1346                         /* c_mutex is locked */
1347                         connection_closing( c, "TLS negotiation failure" );
1348                         connection_close( c );
1349                         connection_return( c );
1350                         return 0;
1351
1352                 } else if ( rc == 0 ) {
1353                         void *ssl;
1354                         struct berval authid = BER_BVNULL;
1355
1356                         c->c_needs_tls_accept = 0;
1357
1358                         /* we need to let SASL know */
1359                         ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1360
1361                         c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1362                         if( c->c_tls_ssf > c->c_ssf ) {
1363                                 c->c_ssf = c->c_tls_ssf;
1364                         }
1365
1366                         rc = dnX509peerNormalize( ssl, &authid );
1367                         if ( rc != LDAP_SUCCESS ) {
1368                                 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1369                                         "unable to get TLS client DN, error=%d id=%lu\n",
1370                                         s, rc, c->c_connid );
1371                         }
1372                         Statslog( LDAP_DEBUG_STATS,
1373                                 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1374                             c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1375                         slap_sasl_external( c, c->c_tls_ssf, &authid );
1376                         if ( authid.bv_val ) free( authid.bv_val );
1377                 }
1378
1379                 /* if success and data is ready, fall thru to data input loop */
1380                 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1381                 {
1382 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1383                         slapd_set_read( s, 1 );
1384 #endif
1385
1386                         connection_return( c );
1387                         return 0;
1388                 }
1389         }
1390 #endif
1391
1392 #ifdef HAVE_CYRUS_SASL
1393         if ( c->c_sasl_layers ) {
1394                 /* If previous layer is not removed yet, give up for now */
1395                 if ( !c->c_sasl_sockctx ) {
1396 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1397                         slapd_set_read( s, 1 );
1398 #endif
1399
1400                         connection_return( c );
1401                         return 0;
1402                 }
1403
1404                 c->c_sasl_layers = 0;
1405
1406                 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1407                 if( rc != LDAP_SUCCESS ) {
1408                         Debug( LDAP_DEBUG_TRACE,
1409                                 "connection_read(%d): SASL install error "
1410                                 "error=%d id=%lu, closing\n",
1411                                 s, rc, c->c_connid );
1412
1413                         /* c_mutex is locked */
1414                         connection_closing( c, "SASL layer install failure" );
1415                         connection_close( c );
1416                         connection_return( c );
1417                         return 0;
1418                 }
1419         }
1420 #endif
1421
1422 #define CONNECTION_INPUT_LOOP 1
1423 /* #define      DATA_READY_LOOP 1 */
1424
1425         do {
1426                 /* How do we do this without getting into a busy loop ? */
1427 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1428                 rc = connection_input( c, cri );
1429 #else
1430                 rc = connection_input( c );
1431 #endif
1432         }
1433 #ifdef DATA_READY_LOOP
1434         while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1435 #elif CONNECTION_INPUT_LOOP
1436         while(!rc);
1437 #else
1438         while(0);
1439 #endif
1440
1441         if( rc < 0 ) {
1442                 Debug( LDAP_DEBUG_CONNS,
1443                         "connection_read(%d): input error=%d id=%lu, closing.\n",
1444                         s, rc, c->c_connid );
1445
1446                 /* c_mutex is locked */
1447                 connection_closing( c, conn_lost_str );
1448                 connection_close( c );
1449                 connection_return( c );
1450                 return 0;
1451         }
1452
1453 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1454         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1455                 slapd_set_write( s, 0 );
1456         }
1457
1458         slapd_set_read( s, 1 );
1459 #else
1460         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1461                 slapd_set_read( s, 1 );
1462         }
1463
1464         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1465                 slapd_set_write( s, 1 );
1466         }
1467 #endif
1468
1469         connection_return( c );
1470
1471         return 0;
1472 }
1473
1474 static int
1475 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1476 connection_input( Connection *conn , conn_readinfo *cri )
1477 #else
1478 connection_input( Connection *conn )
1479 #endif
1480 {
1481         Operation *op;
1482         ber_tag_t       tag;
1483         ber_len_t       len;
1484         ber_int_t       msgid;
1485         BerElement      *ber;
1486         int             rc;
1487 #ifdef LDAP_CONNECTIONLESS
1488         Sockaddr        peeraddr;
1489         char            *cdn = NULL;
1490 #endif
1491         char *defer = NULL;
1492
1493         if ( conn->c_currentber == NULL &&
1494                 ( conn->c_currentber = ber_alloc()) == NULL )
1495         {
1496                 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1497                 return -1;
1498         }
1499
1500         sock_errset(0);
1501
1502 #ifdef LDAP_CONNECTIONLESS
1503         if ( conn->c_is_udp ) {
1504                 char peername[sizeof("IP=255.255.255.255:65336")];
1505
1506                 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1507                 if (len != sizeof(struct sockaddr)) return 1;
1508
1509                 sprintf( peername, "IP=%s:%d",
1510                         inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1511                         (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1512                 Statslog( LDAP_DEBUG_STATS,
1513                         "conn=%lu UDP request from %s (%s) accepted.\n",
1514                         conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1515         }
1516 #endif
1517
1518         tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1519         if ( tag != LDAP_TAG_MESSAGE ) {
1520                 int err = sock_errno();
1521                 ber_socket_t    sd;
1522
1523                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1524
1525                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1526                         /* log, close and send error */
1527                 Debug( LDAP_DEBUG_TRACE,
1528                         "ber_get_next on fd %d failed errno=%d (%s)\n",
1529                         sd, err, sock_errstr(err) );
1530                         ber_free( conn->c_currentber, 1 );
1531                         conn->c_currentber = NULL;
1532
1533                         return -2;
1534                 }
1535                 return 1;
1536         }
1537
1538         ber = conn->c_currentber;
1539         conn->c_currentber = NULL;
1540
1541         if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1542                 /* log, close and send error */
1543                 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1544                 ber_free( ber, 1 );
1545                 return -1;
1546         }
1547
1548         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1549                 /* log, close and send error */
1550                 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1551                 ber_free( ber, 1 );
1552
1553                 return -1;
1554         }
1555
1556 #ifdef LDAP_CONNECTIONLESS
1557         if( conn->c_is_udp ) {
1558                 if( tag == LBER_OCTETSTRING ) {
1559                         ber_get_stringa( ber, &cdn );
1560                         tag = ber_peek_tag(ber, &len);
1561                 }
1562                 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1563                         Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1564                         ber_free( ber, 1 );
1565                         return 0;
1566                 }
1567         }
1568 #endif
1569
1570         if(tag == LDAP_REQ_BIND) {
1571                 /* immediately abandon all existing operations upon BIND */
1572                 connection_abandon( conn );
1573         }
1574
1575         op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1576
1577         op->o_conn = conn;
1578         /* clear state if the connection is being reused from inactive */
1579         if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1580                 memset( &conn->c_pagedresults_state, 0,
1581                         sizeof( conn->c_pagedresults_state ) );
1582         }
1583
1584         op->o_res_ber = NULL;
1585
1586 #ifdef LDAP_CONNECTIONLESS
1587         if (conn->c_is_udp) {
1588                 if ( cdn ) {
1589                         ber_str2bv( cdn, 0, 1, &op->o_dn );
1590                         op->o_protocol = LDAP_VERSION2;
1591                 }
1592                 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1593                 if (op->o_res_ber == NULL) return 1;
1594
1595                 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1596                         sizeof(struct sockaddr), 0 );
1597
1598                 if (rc != sizeof(struct sockaddr)) {
1599                         Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1600                         return 1;
1601                 }
1602
1603                 if (op->o_protocol == LDAP_VERSION2) {
1604                         rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1605                         if (rc == -1) {
1606                                 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1607                                 return rc;
1608                         }
1609                 }
1610         }
1611 #endif /* LDAP_CONNECTIONLESS */
1612
1613         rc = 0;
1614
1615         /* Don't process requests when the conn is in the middle of a
1616          * Bind, or if it's closing. Also, don't let any single conn
1617          * use up all the available threads, and don't execute if we're
1618          * currently blocked on output. And don't execute if there are
1619          * already pending ops, let them go first.  Abandon operations
1620          * get exceptions to some, but not all, cases.
1621          */
1622         switch( tag ){
1623         default:
1624                 /* Abandon and Unbind are exempt from these checks */
1625                 if (conn->c_conn_state == SLAP_C_CLOSING) {
1626                         defer = "closing";
1627                         break;
1628                 } else if (conn->c_writewaiter) {
1629                         defer = "awaiting write";
1630                         break;
1631                 } else if (conn->c_n_ops_pending) {
1632                         defer = "pending operations";
1633                         break;
1634                 }
1635                 /* FALLTHRU */
1636         case LDAP_REQ_ABANDON:
1637                 /* Unbind is exempt from these checks */
1638                 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1639                         defer = "too many executing";
1640                         break;
1641                 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1642                         defer = "binding";
1643                         break;
1644                 }
1645                 /* FALLTHRU */
1646         case LDAP_REQ_UNBIND:
1647                 break;
1648         }
1649
1650         if( defer ) {
1651                 int max = conn->c_dn.bv_len
1652                         ? slap_conn_max_pending_auth
1653                         : slap_conn_max_pending;
1654
1655                 Debug( LDAP_DEBUG_ANY,
1656                         "connection_input: conn=%lu deferring operation: %s\n",
1657                         conn->c_connid, defer, 0 );
1658                 conn->c_n_ops_pending++;
1659                 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1660                 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1661
1662         } else {
1663                 conn->c_n_ops_executing++;
1664
1665 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1666                 /*
1667                  * The first op will be processed in the same thread context,
1668                  * as long as there is only one op total.
1669                  * Subsequent ops will be submitted to the pool by
1670                  * calling connection_op_activate()
1671                  */
1672                 if ( cri->op == NULL ) {
1673                         /* the first incoming request */
1674                         connection_op_queue( op );
1675                         cri->op = op;
1676                 } else {
1677                         if ( !cri->nullop ) {
1678                                 cri->nullop = 1;
1679                                 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1680                                         connection_operation, (void *) cri->op );
1681                         }
1682                         connection_op_activate( op );
1683                 }
1684 #else
1685                 connection_op_activate( op );
1686 #endif
1687         }
1688
1689 #ifdef NO_THREADS
1690         if ( conn->c_struct_state != SLAP_C_USED ) {
1691                 /* connection must have got closed underneath us */
1692                 return 1;
1693         }
1694 #endif
1695
1696         assert( conn->c_struct_state == SLAP_C_USED );
1697         return rc;
1698 }
1699
1700 static int
1701 connection_resched( Connection *conn )
1702 {
1703         Operation *op;
1704
1705         if( conn->c_conn_state == SLAP_C_CLOSING ) {
1706                 ber_socket_t    sd;
1707                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1708
1709                 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1710                         "attempting closing conn=%lu sd=%d\n",
1711                         conn->c_connid, sd, 0 );
1712                 connection_close( conn );
1713                 return 0;
1714         }
1715
1716         if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1717                 /* other states need different handling */
1718                 return 0;
1719         }
1720
1721         while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1722                 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1723
1724                 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1725                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1726
1727                 /* pending operations should not be marked for abandonment */
1728                 assert(!op->o_abandon);
1729
1730                 conn->c_n_ops_pending--;
1731                 conn->c_n_ops_executing++;
1732
1733                 connection_op_activate( op );
1734
1735                 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1736         }
1737         return 0;
1738 }
1739
1740 static void
1741 connection_init_log_prefix( Operation *op )
1742 {
1743         if ( op->o_connid == (unsigned long)(-1) ) {
1744                 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1745                         "conn=-1 op=%lu", op->o_opid );
1746
1747         } else {
1748                 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1749                         "conn=%lu op=%lu", op->o_connid, op->o_opid );
1750         }
1751 }
1752
1753 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1754 {
1755         op->o_conn->c_sasl_bindop = NULL;
1756
1757         ch_free( op->o_callback );
1758         op->o_callback = NULL;
1759
1760         return SLAP_CB_CONTINUE;
1761 }
1762
1763 static int connection_bind_cb( Operation *op, SlapReply *rs )
1764 {
1765         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1766         if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1767                 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1768         op->o_conn->c_sasl_bind_in_progress =
1769                 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1770
1771         /* Moved here from bind.c due to ITS#4158 */
1772         op->o_conn->c_sasl_bindop = NULL;
1773         if ( op->orb_method == LDAP_AUTH_SASL ) {
1774                 if( rs->sr_err == LDAP_SUCCESS ) {
1775                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1776                         if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1777                                 /* edn is always normalized already */
1778                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1779                         }
1780                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1781                         BER_BVZERO( &op->orb_edn );
1782                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1783                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1784
1785                         op->o_conn->c_sasl_ssf = op->orb_ssf;
1786                         if( op->orb_ssf > op->o_conn->c_ssf ) {
1787                                 op->o_conn->c_ssf = op->orb_ssf;
1788                         }
1789
1790                         if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1791                                 ber_len_t max = sockbuf_max_incoming_auth;
1792                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
1793                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
1794                         }
1795
1796                         /* log authorization identity */
1797                         Statslog( LDAP_DEBUG_STATS,
1798                                 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
1799                                 op->o_log_prefix,
1800                                 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1801                                 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
1802
1803                         Debug( LDAP_DEBUG_TRACE,
1804                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1805                                 op->o_conn->c_authmech.bv_val,
1806                                 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1807                                 op->orb_ssf );
1808
1809                 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1810                         if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1811                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
1812                                 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1813                         }
1814                 }
1815         }
1816         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1817
1818         ch_free( op->o_callback );
1819         op->o_callback = NULL;
1820
1821         return SLAP_CB_CONTINUE;
1822 }
1823
1824 static void connection_op_queue( Operation *op )
1825 {
1826         ber_tag_t tag = op->o_tag;
1827
1828         if (tag == LDAP_REQ_BIND) {
1829                 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1830                 sc->sc_response = connection_bind_cb;
1831                 sc->sc_cleanup = connection_bind_cleanup_cb;
1832                 sc->sc_next = op->o_callback;
1833                 op->o_callback = sc;
1834                 op->o_conn->c_conn_state = SLAP_C_BINDING;
1835         }
1836
1837         if (!op->o_dn.bv_len) {
1838                 op->o_authz = op->o_conn->c_authz;
1839                 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1840                         ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1841                         ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1842                 } else {
1843                         ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1844                         ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1845                 }
1846         }
1847
1848         op->o_authtype = op->o_conn->c_authtype;
1849         ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1850         
1851         if (!op->o_protocol) {
1852                 op->o_protocol = op->o_conn->c_protocol
1853                         ? op->o_conn->c_protocol : LDAP_VERSION3;
1854         }
1855
1856         if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1857                 op->o_protocol > LDAP_VERSION2)
1858         {
1859                 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1860         }
1861
1862         op->o_connid = op->o_conn->c_connid;
1863         connection_init_log_prefix( op );
1864
1865         LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1866 }
1867
1868 static int connection_op_activate( Operation *op )
1869 {
1870         int rc;
1871
1872         connection_op_queue( op );
1873
1874         rc = ldap_pvt_thread_pool_submit( &connection_pool,
1875                 connection_operation, (void *) op );
1876
1877         if ( rc != 0 ) {
1878                 Debug( LDAP_DEBUG_ANY,
1879                         "connection_op_activate: submit failed (%d) for conn=%lu\n",
1880                         rc, op->o_connid, 0 );
1881                 /* should move op to pending list */
1882         }
1883
1884         return rc;
1885 }
1886
1887 int connection_write(ber_socket_t s)
1888 {
1889         Connection *c;
1890         Operation *op;
1891
1892         assert( connections != NULL );
1893
1894         slapd_clr_write( s, 0 );
1895
1896         c = connection_get( s );
1897         if( c == NULL ) {
1898                 Debug( LDAP_DEBUG_ANY,
1899                         "connection_write(%ld): no connection!\n",
1900                         (long)s, 0, 0 );
1901                 return -1;
1902         }
1903
1904         c->c_n_write++;
1905
1906         Debug( LDAP_DEBUG_TRACE,
1907                 "connection_write(%d): waking output for id=%lu\n",
1908                 s, c->c_connid, 0 );
1909         ldap_pvt_thread_cond_signal( &c->c_write_cv );
1910
1911         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1912                 slapd_set_read( s, 1 );
1913         }
1914         if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1915                 slapd_set_write( s, 1 );
1916         }
1917
1918         /* If there are ops pending because of a writewaiter,
1919          * start one up.
1920          */
1921         while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1922                 if ( !c->c_writewaiter ) break;
1923                 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1924
1925                 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1926                 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1927
1928                 /* pending operations should not be marked for abandonment */
1929                 assert(!op->o_abandon);
1930
1931                 c->c_n_ops_pending--;
1932                 c->c_n_ops_executing++;
1933
1934                 connection_op_activate( op );
1935
1936                 break;
1937         }
1938
1939         connection_return( c );
1940         return 0;
1941 }
1942
1943 void
1944 connection_fake_init(
1945         Connection *conn,
1946         Operation *op,
1947         void *ctx )
1948 {
1949         conn->c_connid = -1;
1950         conn->c_send_ldap_result = slap_send_ldap_result;
1951         conn->c_send_search_entry = slap_send_search_entry;
1952         conn->c_send_search_reference = slap_send_search_reference;
1953         conn->c_listener = (Listener *)&dummy_list;
1954         conn->c_peer_domain = slap_empty_bv;
1955         conn->c_peer_name = slap_empty_bv;
1956
1957         memset(op, 0, OPERATION_BUFFER_SIZE);
1958         op->o_hdr = (Opheader *)(op+1);
1959         op->o_controls = (void **)(op->o_hdr+1);
1960         /* set memory context */
1961         op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1962         op->o_tmpmfuncs = &slap_sl_mfuncs;
1963         op->o_threadctx = ctx;
1964 #ifdef LDAP_DEVEL
1965         op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1966 #endif /* LDAP_DEVEL */
1967
1968         op->o_conn = conn;
1969         op->o_connid = op->o_conn->c_connid;
1970         connection_init_log_prefix( op );
1971
1972 #ifdef LDAP_SLAPI
1973         slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1974         slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1975 #endif /* LDAP_SLAPI */
1976
1977         slap_op_time( &op->o_time, &op->o_tincr );
1978 }
1979
1980 void
1981 connection_assign_nextid( Connection *conn )
1982 {
1983         ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1984         conn->c_connid = conn_nextid++;
1985         ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
1986 }